mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 14:49:58 -07:00
global: MASSIVE snapshot
This commit is contained in:
@@ -2,23 +2,22 @@ use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use vecdb::Exit;
|
||||
|
||||
use crate::{indexes, ComputeIndexes};
|
||||
use crate::ComputeIndexes;
|
||||
|
||||
use super::Vecs;
|
||||
|
||||
impl Vecs {
|
||||
pub fn compute(
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.count
|
||||
.compute(indexer, indexes, starting_indexes, exit)?;
|
||||
.compute(indexer, starting_indexes, exit)?;
|
||||
|
||||
self.value
|
||||
.compute(indexer, indexes, starting_indexes, exit)?;
|
||||
.compute(indexer, starting_indexes, exit)?;
|
||||
|
||||
let _lock = exit.lock();
|
||||
self.db.compact()?;
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::StoredU64;
|
||||
use vecdb::{Exit, TypedVecIterator};
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{ComputeIndexes, indexes};
|
||||
use crate::ComputeIndexes;
|
||||
|
||||
impl Vecs {
|
||||
pub fn compute(
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.p2a.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
self.p2a.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2aaddressindex,
|
||||
@@ -25,7 +24,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2ms
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_p2msoutputindex,
|
||||
@@ -36,7 +35,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2pk33
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk33addressindex,
|
||||
@@ -47,7 +46,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2pk65
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk65addressindex,
|
||||
@@ -58,7 +57,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2pkh
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pkhaddressindex,
|
||||
@@ -69,7 +68,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2sh
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2shaddressindex,
|
||||
@@ -80,7 +79,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2tr
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2traddressindex,
|
||||
@@ -91,7 +90,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2wpkh
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wpkhaddressindex,
|
||||
@@ -102,7 +101,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.p2wsh
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wshaddressindex,
|
||||
@@ -113,7 +112,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.opreturn
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_opreturnindex,
|
||||
@@ -124,7 +123,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.unknownoutput
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_unknownoutputindex,
|
||||
@@ -135,7 +134,7 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
self.emptyoutput
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_emptyoutputindex,
|
||||
@@ -146,17 +145,15 @@ impl Vecs {
|
||||
})?;
|
||||
|
||||
// Compute segwit = p2wpkh + p2wsh + p2tr
|
||||
let mut p2wsh_iter = self.p2wsh.height.into_iter();
|
||||
let mut p2tr_iter = self.p2tr.height.into_iter();
|
||||
|
||||
self.segwit
|
||||
.compute_all(indexes, starting_indexes, exit, |v| {
|
||||
v.compute_transform(
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&self.p2wpkh.height,
|
||||
|(h, p2wpkh, ..)| {
|
||||
let sum = *p2wpkh + *p2wsh_iter.get_unwrap(h) + *p2tr_iter.get_unwrap(h);
|
||||
(h, StoredU64::from(sum))
|
||||
&self.p2wsh.height,
|
||||
&self.p2tr.height,
|
||||
|(h, p2wpkh, p2wsh, p2tr, ..)| {
|
||||
(h, StoredU64::from(*p2wpkh + *p2wsh + *p2tr))
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, IterableCloneableVec};
|
||||
use vecdb::{Database, ReadableCloneableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
@@ -10,7 +10,7 @@ use crate::{
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub fn forced_import(
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
@@ -31,20 +31,20 @@ impl Vecs {
|
||||
|
||||
// Adoption ratios (lazy)
|
||||
// Uses outputs.count.count as denominator (total output count)
|
||||
// At height level: per-block ratio; at dateindex level: sum-based ratio (% of new outputs)
|
||||
// At height level: per-block ratio; at day1 level: sum-based ratio (% of new outputs)
|
||||
let taproot_adoption = LazyBinaryFromHeightFull::from_height_and_txindex::<PercentageU64F32>(
|
||||
"taproot_adoption",
|
||||
version,
|
||||
p2tr.height.boxed_clone(),
|
||||
outputs.count.total_count.height.sum_cum.sum.0.boxed_clone(),
|
||||
p2tr.height.read_only_boxed_clone(),
|
||||
outputs.count.total_count.height.sum_cum.sum.0.read_only_boxed_clone(),
|
||||
&p2tr,
|
||||
&outputs.count.total_count,
|
||||
);
|
||||
let segwit_adoption = LazyBinaryFromHeightFull::from_height_and_txindex::<PercentageU64F32>(
|
||||
"segwit_adoption",
|
||||
version,
|
||||
segwit.height.boxed_clone(),
|
||||
outputs.count.total_count.height.sum_cum.sum.0.boxed_clone(),
|
||||
segwit.height.read_only_boxed_clone(),
|
||||
outputs.count.total_count.height.sum_cum.sum.0.read_only_boxed_clone(),
|
||||
&segwit,
|
||||
&outputs.count.total_count,
|
||||
);
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{StoredF32, StoredU64};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightFull, LazyBinaryFromHeightFull};
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct Vecs {
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
// Per-type output counts
|
||||
pub p2a: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2ms: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2pk33: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2pk65: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2pkh: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2sh: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2tr: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2wpkh: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2wsh: ComputedFromHeightFull<StoredU64>,
|
||||
pub opreturn: ComputedFromHeightFull<StoredU64>,
|
||||
pub emptyoutput: ComputedFromHeightFull<StoredU64>,
|
||||
pub unknownoutput: ComputedFromHeightFull<StoredU64>,
|
||||
pub p2a: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2ms: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2pk33: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2pk65: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2pkh: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2sh: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2tr: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2wpkh: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub p2wsh: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub opreturn: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub emptyoutput: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub unknownoutput: ComputedFromHeightFull<StoredU64, M>,
|
||||
|
||||
// Aggregate counts
|
||||
/// SegWit output count (p2wpkh + p2wsh + p2tr)
|
||||
pub segwit: ComputedFromHeightFull<StoredU64>,
|
||||
pub segwit: ComputedFromHeightFull<StoredU64, M>,
|
||||
|
||||
// Adoption ratios
|
||||
pub taproot_adoption: LazyBinaryFromHeightFull<StoredF32, StoredU64, StoredU64>,
|
||||
|
||||
@@ -5,16 +5,16 @@ use brk_traversable::Traversable;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, PAGE_SIZE};
|
||||
|
||||
use crate::{indexes, outputs, price};
|
||||
use crate::{indexes, outputs, prices};
|
||||
|
||||
use super::{CountVecs, ValueVecs, Vecs};
|
||||
|
||||
impl Vecs {
|
||||
pub fn forced_import(
|
||||
pub(crate) fn forced_import(
|
||||
parent_path: &Path,
|
||||
parent_version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
price: Option<&price::Vecs>,
|
||||
prices: &prices::Vecs,
|
||||
outputs: &outputs::Vecs,
|
||||
) -> Result<Self> {
|
||||
let db = Database::open(&parent_path.join(super::DB_NAME))?;
|
||||
@@ -23,7 +23,7 @@ impl Vecs {
|
||||
let version = parent_version;
|
||||
|
||||
let count = CountVecs::forced_import(&db, version, indexes, outputs)?;
|
||||
let value = ValueVecs::forced_import(&db, version, indexes, price)?;
|
||||
let value = ValueVecs::forced_import(&db, version, indexes, prices)?;
|
||||
|
||||
let this = Self { db, count, value };
|
||||
|
||||
|
||||
@@ -5,18 +5,18 @@ mod compute;
|
||||
mod import;
|
||||
|
||||
use brk_traversable::Traversable;
|
||||
use vecdb::Database;
|
||||
use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
pub use count::Vecs as CountVecs;
|
||||
pub use value::Vecs as ValueVecs;
|
||||
|
||||
pub const DB_NAME: &str = "scripts";
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct Vecs {
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub(crate) db: Database,
|
||||
|
||||
pub count: CountVecs,
|
||||
pub value: ValueVecs,
|
||||
pub count: CountVecs<M>,
|
||||
pub value: ValueVecs<M>,
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{Height, OutputType, Sats, TxOutIndex};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, GenericStoredVec, TypedVecIterator, VecIndex};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, WritableVec, VecIndex};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{ComputeIndexes, indexes};
|
||||
use crate::ComputeIndexes;
|
||||
|
||||
impl Vecs {
|
||||
pub fn compute(
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.opreturn
|
||||
.compute_all(indexes, starting_indexes, exit, |height_vec| {
|
||||
.compute(starting_indexes, exit, |height_vec| {
|
||||
// Validate computed versions against dependencies
|
||||
let dep_version = indexer.vecs.outputs.first_txoutindex.version()
|
||||
+ indexer.vecs.outputs.outputtype.version()
|
||||
@@ -38,35 +37,38 @@ impl Vecs {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Prepare iterators
|
||||
let mut height_to_first_txoutindex =
|
||||
indexer.vecs.outputs.first_txoutindex.iter()?;
|
||||
let mut txoutindex_to_outputtype = indexer.vecs.outputs.outputtype.iter()?;
|
||||
let mut txoutindex_to_value = indexer.vecs.outputs.value.iter()?;
|
||||
// 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()));
|
||||
|
||||
// Iterate blocks
|
||||
for h in starting_height.to_usize()..=target_height.to_usize() {
|
||||
let height = Height::from(h);
|
||||
let local_idx = h - starting_height.to_usize();
|
||||
|
||||
// Get output range for this block
|
||||
let first_txoutindex = height_to_first_txoutindex.get_unwrap(height);
|
||||
let next_first_txoutindex = if height < target_height {
|
||||
height_to_first_txoutindex.get_unwrap(height.incremented())
|
||||
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())
|
||||
};
|
||||
|
||||
// Sum opreturn values
|
||||
let mut opreturn_value = Sats::ZERO;
|
||||
for i in first_txoutindex.to_usize()..next_first_txoutindex.to_usize() {
|
||||
let txoutindex = TxOutIndex::from(i);
|
||||
let outputtype = txoutindex_to_outputtype.get_unwrap(txoutindex);
|
||||
let out_start = first_txoutindex.to_usize();
|
||||
let out_end = next_first_txoutindex.to_usize();
|
||||
|
||||
if outputtype == OutputType::OpReturn {
|
||||
let value = txoutindex_to_value.get_unwrap(txoutindex);
|
||||
opreturn_value += value;
|
||||
}
|
||||
}
|
||||
// Sum opreturn values — batch read both ranges for the block
|
||||
let values = indexer.vecs.outputs.value.collect_range_at(out_start, out_end);
|
||||
let opreturn_value = indexer.vecs.outputs.outputtype.fold_range_at(
|
||||
out_start, out_end,
|
||||
(Sats::ZERO, 0_usize),
|
||||
|(mut sum, idx), ot| {
|
||||
if ot == OutputType::OpReturn {
|
||||
sum += values[idx];
|
||||
}
|
||||
(sum, idx + 1)
|
||||
},
|
||||
).0;
|
||||
|
||||
height_vec.truncate_push(height, opreturn_value)?;
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::ValueFromHeightFull, price};
|
||||
use crate::{indexes, internal::ValueFromHeightFull, prices};
|
||||
|
||||
impl Vecs {
|
||||
pub fn forced_import(
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
price: Option<&price::Vecs>,
|
||||
prices: &prices::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
opreturn: ValueFromHeightFull::forced_import(
|
||||
@@ -18,7 +18,7 @@ impl Vecs {
|
||||
"opreturn_value",
|
||||
version,
|
||||
indexes,
|
||||
price,
|
||||
prices,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use brk_traversable::Traversable;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::ValueFromHeightFull;
|
||||
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct Vecs {
|
||||
pub opreturn: ValueFromHeightFull,
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub opreturn: ValueFromHeightFull<M>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user