Skip to content

Commit

Permalink
Print index path in ord info (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 16, 2023
1 parent 6aa3a2b commit 71a00ba
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
22 changes: 12 additions & 10 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) struct Index {
auth: Auth,
client: Client,
database: Database,
database_path: PathBuf,
path: PathBuf,
first_inscription_height: u64,
genesis_block_coinbase_transaction: Transaction,
genesis_block_coinbase_txid: Txid,
Expand Down Expand Up @@ -87,11 +87,12 @@ pub(crate) struct Info {
pub(crate) branch_pages: usize,
pub(crate) fragmented_bytes: usize,
pub(crate) index_file_size: u64,
pub(crate) index_path: PathBuf,
pub(crate) leaf_pages: usize,
pub(crate) metadata_bytes: usize,
pub(crate) sat_ranges: u64,
pub(crate) outputs_traversed: u64,
pub(crate) page_size: usize,
pub(crate) sat_ranges: u64,
pub(crate) stored_bytes: usize,
pub(crate) transactions: Vec<TransactionInfo>,
pub(crate) tree_height: usize,
Expand Down Expand Up @@ -147,13 +148,13 @@ impl Index {
bail!("failed to create data dir `{}`: {err}", data_dir.display());
}

let database_path = if let Some(database_path) = &options.index {
database_path.clone()
let path = if let Some(path) = &options.index {
path.clone()
} else {
data_dir.join("index.redb")
};

let database = match unsafe { redb::Database::builder().open_mmapped(&database_path) } {
let database = match unsafe { redb::Database::builder().open_mmapped(&path) } {
Ok(database) => {
let schema_version = database
.begin_read()?
Expand All @@ -166,12 +167,12 @@ impl Index {
cmp::Ordering::Less =>
bail!(
"index at `{}` appears to have been built with an older, incompatible version of ord, consider deleting and rebuilding the index: index schema {schema_version}, ord schema {SCHEMA_VERSION}",
database_path.display()
path.display()
),
cmp::Ordering::Greater =>
bail!(
"index at `{}` appears to have been built with a newer, incompatible version of ord, consider updating ord: index schema {schema_version}, ord schema {SCHEMA_VERSION}",
database_path.display()
path.display()
),
cmp::Ordering::Equal => {
}
Expand All @@ -187,7 +188,7 @@ impl Index {
} else {
WriteStrategy::TwoPhase
})
.create_mmapped(&database_path)?
.create_mmapped(&path)?
};
let tx = database.begin_write()?;

Expand Down Expand Up @@ -230,7 +231,7 @@ impl Index {
auth,
client,
database,
database_path,
path,
first_inscription_height: options.first_inscription_height(),
genesis_block_coinbase_transaction,
height_limit: options.height_limit,
Expand Down Expand Up @@ -271,6 +272,7 @@ impl Index {
.map(|x| x.value())
.unwrap_or(0);
Info {
index_path: self.path.clone(),
blocks_indexed: wtx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
Expand All @@ -280,7 +282,7 @@ impl Index {
.unwrap_or(0),
branch_pages: stats.branch_pages(),
fragmented_bytes: stats.fragmented_bytes(),
index_file_size: fs::metadata(&self.database_path)?.len(),
index_file_size: fs::metadata(&self.path)?.len(),
leaf_pages: stats.leaf_pages(),
metadata_bytes: stats.metadata_bytes(),
sat_ranges,
Expand Down
6 changes: 6 additions & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ mod supply;
mod traits;
mod wallet;

fn print_json(output: impl Serialize) -> Result {
serde_json::to_writer_pretty(io::stdout(), &output)?;
println!();
Ok(())
}

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
#[clap(about = "List the first satoshis of each reward epoch")]
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Info {
);
}
} else {
serde_json::to_writer(io::stdout(), &info)?;
print_json(info)?;
}

Ok(())
Expand Down
13 changes: 5 additions & 8 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,11 @@ impl Inscribe {
.send_raw_transaction(&reveal_tx)
.context("Failed to send reveal transaction")?;

serde_json::to_writer_pretty(
io::stdout(),
&Output {
commit,
reveal,
inscription: reveal.into(),
},
)?;
print_json(Output {
commit,
reveal,
inscription: reveal.into(),
})?;

Ok(())
}
Expand Down
46 changes: 44 additions & 2 deletions tests/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@ fn json_with_satoshi_index() {
CommandBuilder::new("--index-sats info")
.rpc_server(&rpc_server)
.stdout_regex(
r#"\{"blocks_indexed":1,"branch_pages":\d+,"fragmented_bytes":\d+,"index_file_size":\d+,"leaf_pages":\d+,"metadata_bytes":\d+,"sat_ranges":1,"outputs_traversed":1,"page_size":\d+,"stored_bytes":\d+,"transactions":\[\{"starting_block_count":0,"starting_timestamp":\d+\}\],"tree_height":\d+,"utxos_indexed":1\}"#
r#"\{
"blocks_indexed": 1,
"branch_pages": \d+,
"fragmented_bytes": \d+,
"index_file_size": \d+,
"index_path": ".*\.redb",
"leaf_pages": \d+,
"metadata_bytes": \d+,
"outputs_traversed": 1,
"page_size": \d+,
"sat_ranges": 1,
"stored_bytes": \d+,
"transactions": \[
\{
"starting_block_count": 0,
"starting_timestamp": \d+
\}
\],
"tree_height": \d+,
"utxos_indexed": 1
\}
"#,
)
.run();
}
Expand All @@ -17,7 +38,28 @@ fn json_without_satoshi_index() {
CommandBuilder::new("info")
.rpc_server(&rpc_server)
.stdout_regex(
r#"\{"blocks_indexed":1,"branch_pages":\d+,"fragmented_bytes":\d+,"index_file_size":\d+,"leaf_pages":\d+,"metadata_bytes":\d+,"sat_ranges":0,"outputs_traversed":0,"page_size":\d+,"stored_bytes":\d+,"transactions":\[\{"starting_block_count":0,"starting_timestamp":\d+\}\],"tree_height":\d+,"utxos_indexed":0\}"#
r#"\{
"blocks_indexed": 1,
"branch_pages": \d+,
"fragmented_bytes": \d+,
"index_file_size": \d+,
"index_path": ".*\.redb",
"leaf_pages": \d+,
"metadata_bytes": \d+,
"outputs_traversed": 0,
"page_size": \d+,
"sat_ranges": 0,
"stored_bytes": \d+,
"transactions": \[
\{
"starting_block_count": 0,
"starting_timestamp": \d+
\}
\],
"tree_height": \d+,
"utxos_indexed": 0
\}
"#,
)
.run();
}
Expand Down

0 comments on commit 71a00ba

Please sign in to comment.