Skip to content

Commit

Permalink
Track ordinal ranges (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 1, 2022
1 parent 6f02112 commit 0a535ee
Showing 4 changed files with 133 additions and 18 deletions.
25 changes: 16 additions & 9 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ pub(crate) enum List {
pub(crate) enum Statistic {
OutputsTraversed = 0,
Commits = 1,
OrdinalRanges = 2,
}

impl From<Statistic> for u64 {
@@ -188,23 +189,29 @@ impl Index {

let utxos_indexed = wtx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?.len()?;

let ordinal_ranges = wtx
.open_table(STATISTIC_TO_COUNT)?
.get(&Statistic::OrdinalRanges.into())?
.unwrap_or(0);

let outputs_traversed = wtx
.open_table(STATISTIC_TO_COUNT)?
.get(&Statistic::OutputsTraversed.into())?
.unwrap_or(0);

let stats = wtx.stats()?;

println!("blocks indexed: {}", blocks_indexed);
println!("utxos indexed: {}", utxos_indexed);
println!("outputs traversed: {}", outputs_traversed);
println!("tree height: {}", stats.tree_height());
println!("free pages: {}", stats.free_pages());
println!("stored: {}", Bytes(stats.stored_bytes()));
println!("overhead: {}", Bytes(stats.metadata_bytes()));
println!("fragmented: {}", Bytes(stats.fragmented_bytes()));
println!("blocks indexed\t{}", blocks_indexed);
println!("utxos indexed\t{}", utxos_indexed);
println!("outputs traversed\t{}", outputs_traversed);
println!("ordinal ranges\t{}", ordinal_ranges);
println!("tree height\t{}", stats.tree_height());
println!("free pages\t{}", stats.free_pages());
println!("stored\t{}", Bytes(stats.stored_bytes()));
println!("overhead\t{}", Bytes(stats.metadata_bytes()));
println!("fragmented\t{}", Bytes(stats.fragmented_bytes()));
println!(
"index size: {}",
"index size\t{}",
Bytes(std::fs::metadata(&self.database_path)?.len().try_into()?)
);

11 changes: 11 additions & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ pub struct Updater {
cache: HashMap<[u8; 36], Vec<u8>>,
outputs_traversed: u64,
outputs_cached: u64,
ordinal_ranges_since_flush: u64,
outputs_inserted_since_flush: u64,
height: u64,
}
@@ -25,6 +26,7 @@ impl Updater {
outputs_traversed: 0,
outputs_cached: 0,
outputs_inserted_since_flush: 0,
ordinal_ranges_since_flush: 0,
height,
};

@@ -201,6 +203,7 @@ impl Updater {
if h.subsidy() > 0 {
let start = h.starting_ordinal();
coinbase_inputs.push_front((start.n(), (start + h.subsidy()).n()));
self.ordinal_ranges_since_flush += 1;
}

for (tx_offset, tx) in block.txdata.iter().enumerate().skip(1) {
@@ -301,6 +304,7 @@ impl Updater {
let count = range.1 - range.0;

let assigned = if count > remaining {
self.ordinal_ranges_since_flush += 1;
let middle = range.0 + remaining;
input_ordinal_ranges.push_front((middle, range.1));
(range.0, middle)
@@ -357,6 +361,13 @@ impl Updater {
}

Index::increment_statistic(&wtx, Statistic::OutputsTraversed, self.outputs_traversed)?;
self.outputs_traversed = 0;
Index::increment_statistic(
&wtx,
Statistic::OrdinalRanges,
self.ordinal_ranges_since_flush,
)?;
self.ordinal_ranges_since_flush = 0;
Index::increment_statistic(&wtx, Statistic::Commits, 1)?;
wtx.commit()?;
Ok(())
96 changes: 96 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
@@ -1452,6 +1452,7 @@ mod tests {
1
);

server.bitcoin_rpc_server.mine_blocks(1);
server.bitcoin_rpc_server.mine_blocks(1);

server.index.update().unwrap();
@@ -1461,7 +1462,102 @@ mod tests {
.index
.statistic(crate::index::Statistic::OutputsTraversed)
.unwrap(),
3
);
}

#[test]
fn coinbase_ordinal_ranges_are_tracked() {
let server = TestServer::new();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
1
);

server.bitcoin_rpc_server.mine_blocks(1);
server.index.update().unwrap();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
2
);

server.bitcoin_rpc_server.mine_blocks(1);
server.index.update().unwrap();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
3
);
}

#[test]
fn split_ordinal_ranges_are_tracked() {
let server = TestServer::new();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
1
);

server.bitcoin_rpc_server.mine_blocks(1);
server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
input_slots: &[(1, 0, 0)],
output_count: 2,
fee: 0,
});
server.bitcoin_rpc_server.mine_blocks(1);
server.index.update().unwrap();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
4,
);
}

#[test]
fn fee_ordinal_ranges_are_tracked() {
let server = TestServer::new();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
1
);

server.bitcoin_rpc_server.mine_blocks(1);
server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
input_slots: &[(1, 0, 0)],
output_count: 2,
fee: 2,
});
server.bitcoin_rpc_server.mine_blocks(1);
server.index.update().unwrap();

assert_eq!(
server
.index
.statistic(crate::index::Statistic::OrdinalRanges)
.unwrap(),
5,
);
}
}
19 changes: 10 additions & 9 deletions tests/info.rs
Original file line number Diff line number Diff line change
@@ -6,15 +6,16 @@ fn basic() {
CommandBuilder::new("info")
.rpc_server(&rpc_server)
.stdout_regex(
r"
blocks indexed: 1
utxos indexed: 1
outputs traversed: 1
tree height: \d+
free pages: \d+
stored: .*
overhead: .*
fragmented: .*
"
blocks indexed\t1
utxos indexed\t1
outputs traversed\t1
ordinal ranges\t1
tree height\t\\d+
free pages\t\\d+
stored\t.*
overhead\t.*
fragmented\t.*
"
.unindent(),
)

0 comments on commit 0a535ee

Please sign in to comment.