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

Store and count the total number of outputs traversed when building the index #525

Merged
merged 8 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 10 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<[u8; 36], [u8]> =
TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES");
const OUTPOINT_TO_TXID: TableDefinition<[u8; 36], [u8; 32]> =
TableDefinition::new("OUTPOINT_TO_TXID");
const STATISTICS: TableDefinition<u64, u64> = TableDefinition::new("STATISTICS");

pub(crate) struct Index {
client: Client,
Expand All @@ -26,6 +27,17 @@ pub(crate) enum List {
Unspent(Vec<(u64, u64)>),
}

#[repr(u64)]
enum Statistic {
OutputsTraversed = 0,
}

impl From<Statistic> for u64 {
fn from(statistic: Statistic) -> Self {
statistic as u64
}
}

impl Index {
pub(crate) fn open(options: &Options) -> Result<Self> {
let rpc_url = options.rpc_url();
Expand Down Expand Up @@ -56,6 +68,7 @@ impl Index {
tx.open_table(HEIGHT_TO_HASH)?;
tx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?;
tx.open_table(OUTPOINT_TO_TXID)?;
tx.open_table(STATISTICS)?;

tx.commit()?;

Expand Down Expand Up @@ -87,12 +100,18 @@ impl Index {
.map(|(height, _hash)| height + 1)
.unwrap_or(0);

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

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

let stats = wtx.stats()?;

println!("blocks indexed: {}", blocks_indexed);
println!("outputs indexed: {}", outputs_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()));
Expand Down Expand Up @@ -156,9 +175,11 @@ impl Index {
let mut height_to_hash = wtx.open_table(HEIGHT_TO_HASH)?;
let mut outpoint_to_ordinal_ranges = wtx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?;
let mut outpoint_to_txid = wtx.open_table(OUTPOINT_TO_TXID)?;
let mut statistics = wtx.open_table(STATISTICS)?;

let start = Instant::now();
let mut ordinal_ranges_written = 0;
let mut outputs_in_block = 0;

let height = height_to_hash
.range(0..)?
Expand Down Expand Up @@ -248,6 +269,7 @@ impl Index {
&mut outpoint_to_txid,
&mut input_ordinal_ranges,
&mut ordinal_ranges_written,
&mut outputs_in_block,
)?;

coinbase_inputs.extend(input_ordinal_ranges);
Expand All @@ -261,11 +283,20 @@ impl Index {
&mut outpoint_to_txid,
&mut coinbase_inputs,
&mut ordinal_ranges_written,
&mut outputs_in_block,
)?;
}

height_to_hash.insert(&height, &block.block_hash())?;

statistics.insert(
&Statistic::OutputsTraversed.into(),
&(statistics
.get(&(Statistic::OutputsTraversed.into()))?
.unwrap_or(0)
+ outputs_in_block),
)?;

log::info!(
"Wrote {ordinal_ranges_written} ordinal ranges in {}ms",
(Instant::now() - start).as_millis(),
Expand Down Expand Up @@ -310,6 +341,7 @@ impl Index {
#[allow(unused)] outpoint_to_txid: &mut Table<[u8; 36], [u8; 32]>,
input_ordinal_ranges: &mut VecDeque<(u64, u64)>,
ordinal_ranges_written: &mut u64,
outputs_traversed: &mut u64,
) -> Result {
for (vout, output) in tx.output.iter().enumerate() {
let outpoint = OutPoint {
Expand Down Expand Up @@ -346,6 +378,8 @@ impl Index {
*ordinal_ranges_written += 1;
}

*outputs_traversed += 1;

outpoint_to_ordinal_ranges.insert(&serialize(&outpoint).try_into().unwrap(), &ordinals)?;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn basic() {
.stdout_regex(
r"
blocks indexed: 1
outputs indexed: 1
utxos indexed: 1
outputs traversed: 1
tree height: \d+
free pages: \d+
stored: .*
Expand Down