diff --git a/Cargo.lock b/Cargo.lock index 1db8ffaf07..e90870b305 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" + [[package]] name = "atty" version = "0.2.14" @@ -982,6 +988,7 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" name = "ord" version = "0.0.0" dependencies = [ + "anyhow", "bitcoin", "bitcoincore-rpc", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 6f9bffbd0e..7feb987687 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" autotests = false [dependencies] +anyhow = "1.0.56" bitcoin = "0.27.1" bitcoincore-rpc = "0.14.0" chrono = "0.4.19" diff --git a/src/bytes.rs b/src/bytes.rs index 7744f77f68..2004ab7c49 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -32,7 +32,7 @@ impl FromStr for Bytes { "tib" => TI, "pib" => PI, "eib" => EI, - _ => return Err("invalid suffix".into()), + _ => return Err(anyhow!("invalid suffix")), }; Ok(Bytes((value * multiple as f64) as usize)) diff --git a/src/index.rs b/src/index.rs index 83a8d927d5..d107614e8c 100644 --- a/src/index.rs +++ b/src/index.rs @@ -16,17 +16,18 @@ impl Index { options .rpc_url .as_ref() - .ok_or("This command requires `--rpc-url`")?, + .ok_or_else(|| anyhow!("This command requires `--rpc-url`"))?, options .cookie_file .as_ref() .map(|path| Auth::CookieFile(path.clone())) .unwrap_or(Auth::None), - )?; + ) + .context("Failed to connect to RPC URL")?; Ok(Self { client, - database: Database::open(options)?, + database: Database::open(options).context("Failed to open database")?, sleep_until: Cell::new(Instant::now()), }) } @@ -45,17 +46,19 @@ impl Index { } fn client(&self) -> &Client { - let now = Instant::now(); + if cfg!(target_os = "macos") { + let now = Instant::now(); - let sleep_until = self.sleep_until.get(); + let sleep_until = self.sleep_until.get(); - if sleep_until > now { - std::thread::sleep(sleep_until - now); - } + if sleep_until > now { + std::thread::sleep(sleep_until - now); + } - self - .sleep_until - .set(Instant::now() + Duration::from_millis(2)); + self + .sleep_until + .set(Instant::now() + Duration::from_millis(2)); + } &self.client } @@ -108,7 +111,7 @@ impl Index { let prev_hash = wtx.blockhash_at_height(prev_height)?.unwrap(); if prev_hash != block.header.prev_blockhash.as_ref() { - return Err("Reorg detected at or before {prev_height}".into()); + return Err(anyhow!("Reorg detected at or before {prev_height}")); } } @@ -138,7 +141,7 @@ impl Index { let ordinal_ranges = wtx .get_ordinal_ranges(key.as_slice())? - .ok_or("Could not find outpoint in index")?; + .ok_or_else(|| anyhow!("Could not find outpoint in index"))?; let new = input_ordinal_ranges.len(); @@ -224,7 +227,7 @@ impl Index { while remaining > 0 { let range = input_ordinal_ranges .pop_front() - .ok_or("Insufficient inputs for transaction outputs")?; + .ok_or_else(|| anyhow!("Insufficient inputs for transaction outputs"))?; let count = range.1 - range.0; diff --git a/src/key.rs b/src/key.rs index 9a23587afa..328d8ddfaa 100644 --- a/src/key.rs +++ b/src/key.rs @@ -25,7 +25,7 @@ impl Key { pub(crate) fn decode(buffer: &[u8]) -> Result { if buffer.len() != 24 { - return Err("Buffer too small to decode key from".into()); + return Err(anyhow!("Buffer too small to decode key from")); } Ok(Key { diff --git a/src/main.rs b/src/main.rs index 75e3d51bcf..8d6e74bc11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use { arguments::Arguments, bytes::Bytes, epoch::Epoch, height::Height, index::Index, key::Key, options::Options, ordinal::Ordinal, sat_point::SatPoint, subcommand::Subcommand, }, + anyhow::{anyhow, Context, Error}, bitcoin::{ blockdata::constants::COIN_VALUE, consensus::Decodable, consensus::Encodable, Block, BlockHash, OutPoint, Transaction, Txid, @@ -29,28 +30,27 @@ use { }, }; +#[cfg(feature = "redb")] +use redb_database::{Database, WriteTransaction}; + +#[cfg(not(feature = "redb"))] +use lmdb_database::{Database, WriteTransaction}; + mod arguments; mod bytes; mod epoch; mod height; mod index; mod key; +#[cfg(not(feature = "redb"))] +mod lmdb_database; mod options; mod ordinal; -mod sat_point; -mod subcommand; - #[cfg(feature = "redb")] mod redb_database; -#[cfg(feature = "redb")] -use redb_database::{Database, WriteTransaction}; - -#[cfg(not(feature = "redb"))] -mod lmdb_database; -#[cfg(not(feature = "redb"))] -use lmdb_database::{Database, WriteTransaction}; +mod sat_point; +mod subcommand; -type Error = Box; type Result = std::result::Result; static INTERRUPTS: AtomicU64 = AtomicU64::new(0); diff --git a/src/subcommand/find.rs b/src/subcommand/find.rs index def66e7b13..e4e96e2164 100644 --- a/src/subcommand/find.rs +++ b/src/subcommand/find.rs @@ -23,7 +23,7 @@ impl Find { } Ok(()) } - None => Err("Ordinal has not been mined as of index height".into()), + None => Err(anyhow!("Ordinal has not been mined as of index height")), } } } diff --git a/src/subcommand/list.rs b/src/subcommand/list.rs index a2ed797334..181600b4c5 100644 --- a/src/subcommand/list.rs +++ b/src/subcommand/list.rs @@ -16,7 +16,7 @@ impl List { } Ok(()) } - None => Err("Output not found".into()), + None => Err(anyhow!("Output not found")), } } } diff --git a/src/subcommand/name.rs b/src/subcommand/name.rs index 068039fde5..c9d9198dd3 100644 --- a/src/subcommand/name.rs +++ b/src/subcommand/name.rs @@ -8,7 +8,7 @@ pub(crate) struct Name { impl Name { pub(crate) fn run(self) -> Result { if self.name.is_empty() || self.name.chars().any(|c| !('a'..='z').contains(&c)) { - return Err("Invalid name".into()); + return Err(anyhow!("Invalid name")); } let mut min = 0; @@ -32,7 +32,7 @@ impl Name { } if max - min == 0 { - return Err("Name out of range".into()); + return Err(anyhow!("Name out of range")); } guess = min + (max - min) / 2; diff --git a/src/subcommand/traits.rs b/src/subcommand/traits.rs index 84ba6085b5..002a6b375a 100644 --- a/src/subcommand/traits.rs +++ b/src/subcommand/traits.rs @@ -8,7 +8,7 @@ pub(crate) struct Traits { impl Traits { pub(crate) fn run(self) -> Result { if self.ordinal > Ordinal::LAST { - return Err("Invalid ordinal".into()); + return Err(anyhow!("Invalid ordinal")); } let n = self.ordinal.n();