mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 14:49:58 -07:00
global: snapshot
This commit is contained in:
@@ -24,18 +24,8 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
taproot: PercentFromHeight::forced_import(
|
||||
db,
|
||||
"taproot_adoption",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
segwit: PercentFromHeight::forced_import(
|
||||
db,
|
||||
"segwit_adoption",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
taproot: PercentFromHeight::forced_import(db, "taproot_adoption", version, indexes)?,
|
||||
segwit: PercentFromHeight::forced_import(db, "segwit_adoption", version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -11,18 +11,24 @@ impl Vecs {
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let p2a = ComputedFromHeightCumulativeSum::forced_import(db, "p2a_count", version, indexes)?;
|
||||
let p2ms = ComputedFromHeightCumulativeSum::forced_import(db, "p2ms_count", version, indexes)?;
|
||||
let p2a =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2a_count", version, indexes)?;
|
||||
let p2ms =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2ms_count", version, indexes)?;
|
||||
let p2pk33 =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2pk33_count", version, indexes)?;
|
||||
let p2pk65 =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2pk65_count", version, indexes)?;
|
||||
let p2pkh = ComputedFromHeightCumulativeSum::forced_import(db, "p2pkh_count", version, indexes)?;
|
||||
let p2sh = ComputedFromHeightCumulativeSum::forced_import(db, "p2sh_count", version, indexes)?;
|
||||
let p2tr = ComputedFromHeightCumulativeSum::forced_import(db, "p2tr_count", version, indexes)?;
|
||||
let p2pkh =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2pkh_count", version, indexes)?;
|
||||
let p2sh =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2sh_count", version, indexes)?;
|
||||
let p2tr =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2tr_count", version, indexes)?;
|
||||
let p2wpkh =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2wpkh_count", version, indexes)?;
|
||||
let p2wsh = ComputedFromHeightCumulativeSum::forced_import(db, "p2wsh_count", version, indexes)?;
|
||||
let p2wsh =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "p2wsh_count", version, indexes)?;
|
||||
let segwit =
|
||||
ComputedFromHeightCumulativeSum::forced_import(db, "segwit_count", version, indexes)?;
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ use crate::internal::ComputedFromHeightCumulativeSum;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
// Per-type output counts
|
||||
pub p2a: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
pub p2ms: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
pub p2pk33: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
@@ -20,7 +19,5 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub emptyoutput: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
pub unknownoutput: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
|
||||
// Aggregate counts
|
||||
/// SegWit output count (p2wpkh + p2wsh + p2tr)
|
||||
pub segwit: ComputedFromHeightCumulativeSum<StoredU64, M>,
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, PAGE_SIZE};
|
||||
|
||||
use crate::indexes;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{finalize_db, open_db},
|
||||
};
|
||||
|
||||
use super::{AdoptionVecs, CountVecs, ValueVecs, Vecs};
|
||||
|
||||
@@ -15,24 +16,20 @@ impl Vecs {
|
||||
parent_version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let db = Database::open(&parent_path.join(super::DB_NAME))?;
|
||||
db.set_min_len(PAGE_SIZE * 50_000_000)?;
|
||||
|
||||
let db = open_db(parent_path, super::DB_NAME, 50_000_000)?;
|
||||
let version = parent_version;
|
||||
|
||||
let count = CountVecs::forced_import(&db, version, indexes)?;
|
||||
let value = ValueVecs::forced_import(&db, version, indexes)?;
|
||||
let adoption = AdoptionVecs::forced_import(&db, version, indexes)?;
|
||||
|
||||
let this = Self { db, count, value, adoption };
|
||||
|
||||
this.db.retain_regions(
|
||||
this.iter_any_exportable()
|
||||
.flat_map(|v| v.region_names())
|
||||
.collect(),
|
||||
)?;
|
||||
this.db.compact()?;
|
||||
|
||||
let this = Self {
|
||||
db,
|
||||
count,
|
||||
value,
|
||||
adoption,
|
||||
};
|
||||
finalize_db(&this.db, &this)?;
|
||||
Ok(this)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{Height, Indexes, OutputType, Sats, TxOutIndex};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, WritableVec, VecIndex};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, VecIndex, WritableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{blocks, prices};
|
||||
@@ -17,9 +17,12 @@ impl Vecs {
|
||||
) -> Result<()> {
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
self.opreturn
|
||||
.compute(starting_indexes.height, &window_starts, prices, exit, |height_vec| {
|
||||
|
||||
self.opreturn.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
prices,
|
||||
exit,
|
||||
|height_vec| {
|
||||
// Validate computed versions against dependencies
|
||||
let dep_version = indexer.vecs.outputs.first_txoutindex.version()
|
||||
+ indexer.vecs.outputs.outputtype.version()
|
||||
@@ -43,8 +46,12 @@ impl Vecs {
|
||||
}
|
||||
|
||||
// Pre-collect height-indexed data
|
||||
let first_txoutindexes: Vec<TxOutIndex> = indexer.vecs.outputs.first_txoutindex
|
||||
.collect_range_at(starting_height.to_usize(), target_height.to_usize() + 2.min(indexer.vecs.outputs.first_txoutindex.len()));
|
||||
let first_txoutindexes: Vec<TxOutIndex> =
|
||||
indexer.vecs.outputs.first_txoutindex.collect_range_at(
|
||||
starting_height.to_usize(),
|
||||
target_height.to_usize()
|
||||
+ 2.min(indexer.vecs.outputs.first_txoutindex.len()),
|
||||
);
|
||||
|
||||
// Iterate blocks
|
||||
for h in starting_height.to_usize()..=target_height.to_usize() {
|
||||
@@ -53,28 +60,35 @@ impl Vecs {
|
||||
|
||||
// Get output range for this block
|
||||
let first_txoutindex = first_txoutindexes[local_idx];
|
||||
let next_first_txoutindex = if let Some(&next) = first_txoutindexes.get(local_idx + 1) {
|
||||
next
|
||||
} else {
|
||||
TxOutIndex::from(indexer.vecs.outputs.value.len())
|
||||
};
|
||||
let next_first_txoutindex =
|
||||
if let Some(&next) = first_txoutindexes.get(local_idx + 1) {
|
||||
next
|
||||
} else {
|
||||
TxOutIndex::from(indexer.vecs.outputs.value.len())
|
||||
};
|
||||
|
||||
let out_start = first_txoutindex.to_usize();
|
||||
let out_end = next_first_txoutindex.to_usize();
|
||||
|
||||
// Sum opreturn values — fold over both vecs without allocation
|
||||
let opreturn_value = indexer.vecs.outputs.outputtype.fold_range_at(
|
||||
out_start, out_end,
|
||||
(Sats::ZERO, out_start),
|
||||
|(sum, vi), ot| {
|
||||
let new_sum = if ot == OutputType::OpReturn {
|
||||
sum + indexer.vecs.outputs.value.collect_one_at(vi).unwrap()
|
||||
} else {
|
||||
sum
|
||||
};
|
||||
(new_sum, vi + 1)
|
||||
},
|
||||
).0;
|
||||
let opreturn_value = indexer
|
||||
.vecs
|
||||
.outputs
|
||||
.outputtype
|
||||
.fold_range_at(
|
||||
out_start,
|
||||
out_end,
|
||||
(Sats::ZERO, out_start),
|
||||
|(sum, vi), ot| {
|
||||
let new_sum = if ot == OutputType::OpReturn {
|
||||
sum + indexer.vecs.outputs.value.collect_one_at(vi).unwrap()
|
||||
} else {
|
||||
sum
|
||||
};
|
||||
(new_sum, vi + 1)
|
||||
},
|
||||
)
|
||||
.0;
|
||||
|
||||
height_vec.truncate_push(height, opreturn_value)?;
|
||||
}
|
||||
@@ -82,7 +96,8 @@ impl Vecs {
|
||||
height_vec.write()?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -12,12 +12,7 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
opreturn: ValueFromHeightFull::forced_import(
|
||||
db,
|
||||
"opreturn_value",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
opreturn: ValueFromHeightFull::forced_import(db, "opreturn_value", version, indexes)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user