Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring chain with config file #3156

Merged
merged 41 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
da4ebaa
Add settings
casey Feb 22, 2024
5884e11
Add a test
casey Feb 22, 2024
a36804a
Enhance
casey Feb 22, 2024
ae30ad0
Enhance
casey Feb 22, 2024
b267946
Rename
casey Feb 22, 2024
e38a6e0
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 23, 2024
cd3f065
Modify
casey Feb 24, 2024
f6a4bb2
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 24, 2024
409779a
Add settings subcommand
casey Feb 24, 2024
0500cc5
Move tests
casey Feb 24, 2024
1452f48
Test rpc user pass setting
casey Feb 24, 2024
630cda0
Test that inscriptions can be hidden with config
casey Feb 24, 2024
8512aa4
Use builder
casey Feb 24, 2024
9f74749
Enhance
casey Feb 25, 2024
ad17e24
Remove
casey Feb 25, 2024
9281020
Note failing test
casey Feb 25, 2024
d320b6f
Only pass settings in
casey Feb 25, 2024
1e4ee45
Handle some errors
casey Feb 25, 2024
26078b1
Modify
casey Feb 25, 2024
17c0f84
fuck race conditions
casey Feb 25, 2024
d24289d
Clean up
casey Feb 25, 2024
eb5e2af
Tweak
casey Feb 25, 2024
b96f3e3
Enhance
casey Feb 25, 2024
8ac809e
Enhance
casey Feb 25, 2024
4c95144
Remove env var test
casey Feb 25, 2024
4052766
Make setting_opt infallible
casey Feb 25, 2024
baf9333
Make ORD_INTEGRATION_TEST a normal env variable
casey Feb 25, 2024
23fcef7
Tweak
casey Feb 26, 2024
9c04e20
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 27, 2024
dcc1747
Enhance
casey Feb 27, 2024
d776d96
Remove unused core workflow
casey Feb 27, 2024
8b1a928
Better error messages when loading config file
casey Feb 27, 2024
8e21983
Modify
casey Feb 27, 2024
1d71ec2
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 27, 2024
0f62edd
Add settings guide
casey Feb 27, 2024
0d7bc3b
Tweak
casey Feb 27, 2024
42a2a91
Enhance
casey Feb 27, 2024
75986ef
Amend
casey Feb 27, 2024
65462ff
Modify
casey Feb 27, 2024
6bf24c8
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 27, 2024
95db4ca
Merge remote-tracking branch 'upstream/master' into settings
casey Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub(crate) struct Arguments {

impl Arguments {
pub(crate) fn run(self) -> SubcommandResult {
self.subcommand.run(self.options)
self.subcommand.run(Settings::new(self.options)?)
}
}
27 changes: 27 additions & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,30 @@ impl Display for Chain {
)
}
}

impl FromStr for Chain {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mainnet" => Ok(Self::Mainnet),
"regtest" => Ok(Self::Regtest),
"signet" => Ok(Self::Signet),
"testnet" => Ok(Self::Testnet),
_ => bail!("invalid chain: {s}"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from_str() {
assert_eq!("mainnet".parse::<Chain>().unwrap(), Chain::Mainnet);
assert_eq!("regtest".parse::<Chain>().unwrap(), Chain::Regtest);
assert_eq!("signet".parse::<Chain>().unwrap(), Chain::Signet);
assert_eq!("testnet".parse::<Chain>().unwrap(), Chain::Testnet);
}
}
5 changes: 3 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::*;

#[derive(Deserialize, Default, PartialEq, Debug)]
#[derive(Deserialize, Default, PartialEq, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct Config {
pub(crate) hidden: HashSet<InscriptionId>,
pub(crate) bitcoin_rpc_pass: Option<String>,
pub(crate) bitcoin_rpc_user: Option<String>,
pub(crate) chain: Option<Chain>,
pub(crate) hidden: HashSet<InscriptionId>,
}

impl Config {
Expand Down
45 changes: 23 additions & 22 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,28 @@ pub struct Index {
index_sats: bool,
index_spent_sats: bool,
index_transactions: bool,
options: Options,
settings: Settings,
path: PathBuf,
started: DateTime<Utc>,
unrecoverably_reorged: AtomicBool,
}

impl Index {
pub fn open(options: &Options) -> Result<Self> {
Index::open_with_event_sender(options, None)
pub fn open(settings: &Settings) -> Result<Self> {
Index::open_with_event_sender(settings, None)
}

pub fn open_with_event_sender(
options: &Options,
settings: &Settings,
event_sender: Option<tokio::sync::mpsc::Sender<Event>>,
) -> Result<Self> {
let client = options.bitcoin_rpc_client(None)?;
let client = settings.bitcoin_rpc_client(None)?;

let path = options
let path = settings
.options
.index
.clone()
.unwrap_or(options.data_dir().clone().join("index.redb"));
.unwrap_or_else(|| settings.data_dir().clone().join("index.redb"));

if let Err(err) = fs::create_dir_all(path.parent().unwrap()) {
bail!(
Expand All @@ -242,7 +243,7 @@ impl Index {
);
}

let db_cache_size = match options.db_cache_size {
let db_cache_size = match settings.options.db_cache_size {
Some(db_cache_size) => db_cache_size,
None => {
let mut sys = System::new();
Expand Down Expand Up @@ -349,32 +350,32 @@ impl Index {
let mut outpoint_to_sat_ranges = tx.open_table(OUTPOINT_TO_SAT_RANGES)?;
let mut statistics = tx.open_table(STATISTIC_TO_COUNT)?;

if options.index_sats {
if settings.options.index_sats {
outpoint_to_sat_ranges.insert(&OutPoint::null().store(), [].as_slice())?;
}

Self::set_statistic(
&mut statistics,
Statistic::IndexRunes,
u64::from(options.index_runes()),
u64::from(settings.index_runes()),
)?;

Self::set_statistic(
&mut statistics,
Statistic::IndexSats,
u64::from(options.index_sats || options.index_spent_sats),
u64::from(settings.options.index_sats || settings.options.index_spent_sats),
)?;

Self::set_statistic(
&mut statistics,
Statistic::IndexSpentSats,
u64::from(options.index_spent_sats),
u64::from(settings.options.index_spent_sats),
)?;

Self::set_statistic(
&mut statistics,
Statistic::IndexTransactions,
u64::from(options.index_transactions),
u64::from(settings.options.index_transactions),
)?;

Self::set_statistic(&mut statistics, Statistic::Schema, SCHEMA_VERSION)?;
Expand Down Expand Up @@ -402,22 +403,22 @@ impl Index {
}

let genesis_block_coinbase_transaction =
options.chain().genesis_block().coinbase().unwrap().clone();
settings.chain().genesis_block().coinbase().unwrap().clone();

Ok(Self {
genesis_block_coinbase_txid: genesis_block_coinbase_transaction.txid(),
client,
database,
durability,
event_sender,
first_inscription_height: options.first_inscription_height(),
first_inscription_height: settings.first_inscription_height(),
genesis_block_coinbase_transaction,
height_limit: options.height_limit,
height_limit: settings.options.height_limit,
index_runes,
index_sats,
index_spent_sats,
index_transactions,
options: options.clone(),
settings: settings.clone(),
path,
started: Utc::now(),
unrecoverably_reorged: AtomicBool::new(false),
Expand Down Expand Up @@ -486,14 +487,14 @@ impl Index {

Ok(StatusHtml {
blessed_inscriptions,
chain: self.options.chain(),
chain: self.settings.chain(),
content_type_counts,
cursed_inscriptions,
height,
inscriptions: blessed_inscriptions + cursed_inscriptions,
lost_sats: statistic(Statistic::LostSats)?,
minimum_rune_for_next_block: Rune::minimum_at_height(
self.options.chain(),
self.settings.chain(),
Height(next_height),
),
rune_index: statistic(Statistic::IndexRunes)? != 0,
Expand Down Expand Up @@ -670,7 +671,7 @@ impl Index {
.nth(satpoint.outpoint.vout.try_into().unwrap())
.unwrap();
self
.options
.settings
.chain()
.address_from_script(&output.script_pubkey)
.map(|address| address.to_string())
Expand Down Expand Up @@ -1482,7 +1483,7 @@ impl Index {
pub(crate) fn is_output_spent(&self, outpoint: OutPoint) -> Result<bool> {
Ok(
outpoint != OutPoint::null()
&& outpoint != self.options.chain().genesis_coinbase_outpoint()
&& outpoint != self.settings.chain().genesis_coinbase_outpoint()
&& self
.client
.get_tx_out(&outpoint.txid, outpoint.vout, Some(false))?
Expand All @@ -1495,7 +1496,7 @@ impl Index {
return Ok(true);
}

if outpoint == self.options.chain().genesis_coinbase_outpoint() {
if outpoint == self.settings.chain().genesis_coinbase_outpoint() {
return Ok(true);
}

Expand Down
10 changes: 5 additions & 5 deletions src/index/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ struct JsonError {
}

impl Fetcher {
pub(crate) fn new(options: &Options) -> Result<Self> {
pub(crate) fn new(settings: &Settings) -> Result<Self> {
let client = Client::new();

let url = if options.rpc_url(None).starts_with("http://") {
options.rpc_url(None)
let url = if settings.rpc_url(None).starts_with("http://") {
settings.rpc_url(None)
} else {
"http://".to_string() + &options.rpc_url(None)
"http://".to_string() + &settings.rpc_url(None)
};

let url = Uri::try_from(&url).map_err(|e| anyhow!("Invalid rpc url {url}: {e}"))?;

let (user, password) = options.auth()?.get_user_pass()?;
let (user, password) = settings.auth()?.get_user_pass()?;
let auth = format!("{}:{}", user.unwrap(), password.unwrap());
let auth = format!(
"Basic {}",
Expand Down
2 changes: 1 addition & 1 deletion src/index/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Reorg {
if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0)
&& u32::try_from(
index
.options
.settings
.bitcoin_rpc_client(None)?
.get_blockchain_info()?
.headers,
Expand Down
7 changes: 6 additions & 1 deletion src/index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ impl ContextBuilder {
];

let options = Options::try_parse_from(command.into_iter().chain(self.args)).unwrap();
let index = Index::open_with_event_sender(&options, self.event_sender)?;
let settings = Settings {
chain: self.chain,
config: Default::default(),
options,
};
let index = Index::open_with_event_sender(&settings, self.event_sender)?;
index.update().unwrap();

Ok(Context {
Expand Down
12 changes: 6 additions & 6 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'index> Updater<'_> {

let height_limit = index.height_limit;

let client = index.options.bitcoin_rpc_client(None)?;
let client = index.settings.bitcoin_rpc_client(None)?;

let first_inscription_height = index.first_inscription_height;

Expand Down Expand Up @@ -241,7 +241,7 @@ impl<'index> Updater<'_> {
}

fn spawn_fetcher(index: &Index) -> Result<(Sender<OutPoint>, Receiver<u64>)> {
let fetcher = Fetcher::new(&index.options)?;
let fetcher = Fetcher::new(&index.settings)?;

// Not sure if any block has more than 20k inputs, but none so far after first inscription block
const CHANNEL_BUFFER_SIZE: usize = 20_000;
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<'index> Updater<'_> {
let mut outpoint_to_value = wtx.open_table(OUTPOINT_TO_VALUE)?;

let index_inscriptions = self.height >= self.index.first_inscription_height
&& !self.index.options.no_index_inscriptions;
&& !self.index.settings.options.no_index_inscriptions;

if index_inscriptions {
// Send all missing input outpoints to be fetched right away
Expand Down Expand Up @@ -423,7 +423,7 @@ impl<'index> Updater<'_> {

let mut inscription_updater = InscriptionUpdater {
blessed_inscription_count,
chain: self.index.options.chain(),
chain: self.index.settings.chain(),
content_type_to_count: &mut content_type_to_count,
cursed_inscription_count,
event_sender: self.index.event_sender.as_ref(),
Expand Down Expand Up @@ -583,7 +583,7 @@ impl<'index> Updater<'_> {
&inscription_updater.unbound_inscriptions,
)?;

if self.index.index_runes && self.height >= self.index.options.first_rune_height() {
if self.index.index_runes && self.height >= self.index.settings.first_rune_height() {
let mut outpoint_to_rune_balances = wtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;
let mut rune_id_to_rune_entry = wtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;
let mut rune_to_rune_id = wtx.open_table(RUNE_TO_RUNE_ID)?;
Expand All @@ -599,7 +599,7 @@ impl<'index> Updater<'_> {
height: self.height,
id_to_entry: &mut rune_id_to_rune_entry,
inscription_id_to_sequence_number: &mut inscription_id_to_sequence_number,
minimum: Rune::minimum_at_height(self.index.options.chain(), Height(self.height)),
minimum: Rune::minimum_at_height(self.index.settings.chain(), Height(self.height)),
outpoint_to_balances: &mut outpoint_to_rune_balances,
rune_to_id: &mut rune_to_rune_id,
runes,
Expand Down
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use {
},
representation::Representation,
runes::{Etching, Pile, SpacedRune},
settings::Settings,
subcommand::{Subcommand, SubcommandResult},
tally::Tally,
},
Expand Down Expand Up @@ -94,13 +95,12 @@ mod test;
use self::test::*;

macro_rules! tprintln {
($($arg:tt)*) => {

if cfg!(test) {
eprint!("==> ");
eprintln!($($arg)*);
}
};
($($arg:tt)*) => {
if cfg!(test) {
eprint!("==> ");
eprintln!($($arg)*);
}
};
}

pub mod arguments;
Expand All @@ -117,6 +117,7 @@ pub mod outgoing;
mod representation;
pub mod runes;
mod server_config;
mod settings;
pub mod subcommand;
mod tally;
pub mod templates;
Expand Down Expand Up @@ -186,10 +187,10 @@ fn unbound_outpoint() -> OutPoint {
}
}

pub fn parse_ord_server_args(args: &str) -> (Options, crate::subcommand::server::Server) {
pub fn parse_ord_server_args(args: &str) -> (Settings, crate::subcommand::server::Server) {
match Arguments::try_parse_from(args.split_whitespace()) {
Ok(arguments) => match arguments.subcommand {
Subcommand::Server(server) => (arguments.options, server),
Subcommand::Server(server) => (Settings::new(arguments.options).unwrap(), server),
subcommand => panic!("unexpected subcommand: {subcommand:?}"),
},
Err(err) => panic!("error parsing arguments: {err}"),
Expand Down
Loading
Loading