mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 14:49:58 -07:00
computer: indexes + rolling
This commit is contained in:
@@ -2,7 +2,7 @@ use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use vecdb::Exit;
|
||||
|
||||
use crate::ComputeIndexes;
|
||||
use crate::{blocks, outputs, ComputeIndexes};
|
||||
|
||||
use super::Vecs;
|
||||
|
||||
@@ -10,11 +10,13 @@ impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
blocks: &blocks::Vecs,
|
||||
outputs: &outputs::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.count
|
||||
.compute(indexer, starting_indexes, exit)?;
|
||||
.compute(indexer, &blocks.count, &outputs.count, starting_indexes, exit)?;
|
||||
|
||||
self.value
|
||||
.compute(indexer, starting_indexes, exit)?;
|
||||
|
||||
@@ -1,164 +1,175 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::StoredF32;
|
||||
use brk_types::StoredU64;
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::ComputeIndexes;
|
||||
use crate::{blocks, outputs, ComputeIndexes};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
count_vecs: &blocks::CountVecs,
|
||||
outputs_count: &outputs::CountVecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.p2a.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
self.p2a.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2aaddressindex,
|
||||
&indexer.vecs.addresses.p2abytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2ms
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_p2msoutputindex,
|
||||
&indexer.vecs.scripts.p2ms_to_txindex,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2ms.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_p2msoutputindex,
|
||||
&indexer.vecs.scripts.p2ms_to_txindex,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2pk33
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk33addressindex,
|
||||
&indexer.vecs.addresses.p2pk33bytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2pk33.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk33addressindex,
|
||||
&indexer.vecs.addresses.p2pk33bytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2pk65
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk65addressindex,
|
||||
&indexer.vecs.addresses.p2pk65bytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2pk65.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pk65addressindex,
|
||||
&indexer.vecs.addresses.p2pk65bytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2pkh
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pkhaddressindex,
|
||||
&indexer.vecs.addresses.p2pkhbytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2pkh.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2pkhaddressindex,
|
||||
&indexer.vecs.addresses.p2pkhbytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2sh
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2shaddressindex,
|
||||
&indexer.vecs.addresses.p2shbytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2sh.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2shaddressindex,
|
||||
&indexer.vecs.addresses.p2shbytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2tr
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2traddressindex,
|
||||
&indexer.vecs.addresses.p2trbytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2tr.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2traddressindex,
|
||||
&indexer.vecs.addresses.p2trbytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2wpkh
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wpkhaddressindex,
|
||||
&indexer.vecs.addresses.p2wpkhbytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2wpkh.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wpkhaddressindex,
|
||||
&indexer.vecs.addresses.p2wpkhbytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.p2wsh
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wshaddressindex,
|
||||
&indexer.vecs.addresses.p2wshbytes,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.p2wsh.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.addresses.first_p2wshaddressindex,
|
||||
&indexer.vecs.addresses.p2wshbytes,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.opreturn
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_opreturnindex,
|
||||
&indexer.vecs.scripts.opreturn_to_txindex,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.opreturn.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_opreturnindex,
|
||||
&indexer.vecs.scripts.opreturn_to_txindex,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.unknownoutput
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_unknownoutputindex,
|
||||
&indexer.vecs.scripts.unknown_to_txindex,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.unknownoutput.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_unknownoutputindex,
|
||||
&indexer.vecs.scripts.unknown_to_txindex,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
self.emptyoutput
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_emptyoutputindex,
|
||||
&indexer.vecs.scripts.empty_to_txindex,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.emptyoutput.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_count_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.scripts.first_emptyoutputindex,
|
||||
&indexer.vecs.scripts.empty_to_txindex,
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
// Compute segwit = p2wpkh + p2wsh + p2tr
|
||||
self.segwit
|
||||
.compute(starting_indexes, exit,|v| {
|
||||
v.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&self.p2wpkh.height,
|
||||
&self.p2wsh.height,
|
||||
&self.p2tr.height,
|
||||
|(h, p2wpkh, p2wsh, p2tr, ..)| {
|
||||
(h, StoredU64::from(*p2wpkh + *p2wsh + *p2tr))
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
self.segwit.compute(starting_indexes.height, &window_starts, exit, |v| {
|
||||
Ok(v.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&self.p2wpkh.last.height,
|
||||
&self.p2wsh.last.height,
|
||||
&self.p2tr.last.height,
|
||||
|(h, p2wpkh, p2wsh, p2tr, ..)| {
|
||||
(h, StoredU64::from(*p2wpkh + *p2wsh + *p2tr))
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
})?;
|
||||
|
||||
// Adoption ratios: per-block ratio of script type / total outputs
|
||||
self.taproot_adoption.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.p2tr.last.height,
|
||||
&outputs_count.total_count.sum_cum.sum.0,
|
||||
|(h, p2tr, total, ..)| {
|
||||
let ratio = if *total > 0 {
|
||||
StoredF32::from(*p2tr as f64 / *total as f64)
|
||||
} else {
|
||||
StoredF32::from(0.0)
|
||||
};
|
||||
(h, ratio)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.segwit_adoption.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.segwit.last.height,
|
||||
&outputs_count.total_count.sum_cum.sum.0,
|
||||
|(h, segwit, total, ..)| {
|
||||
let ratio = if *total > 0 {
|
||||
StoredF32::from(*segwit as f64 / *total as f64)
|
||||
} else {
|
||||
StoredF32::from(0.0)
|
||||
};
|
||||
(h, ratio)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use brk_error::Result;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, ReadableCloneableVec};
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightFull, LazyBinaryFromHeightFull, PercentageU64F32},
|
||||
outputs,
|
||||
internal::{ComputedFromHeightCumSum, ComputedFromHeightLast},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -14,40 +13,21 @@ impl Vecs {
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
outputs: &outputs::Vecs,
|
||||
) -> Result<Self> {
|
||||
let p2a = ComputedFromHeightFull::forced_import(db, "p2a_count", version, indexes)?;
|
||||
let p2ms = ComputedFromHeightFull::forced_import(db, "p2ms_count", version, indexes)?;
|
||||
let p2pk33 = ComputedFromHeightFull::forced_import(db, "p2pk33_count", version, indexes)?;
|
||||
let p2pk65 = ComputedFromHeightFull::forced_import(db, "p2pk65_count", version, indexes)?;
|
||||
let p2pkh = ComputedFromHeightFull::forced_import(db, "p2pkh_count", version, indexes)?;
|
||||
let p2sh = ComputedFromHeightFull::forced_import(db, "p2sh_count", version, indexes)?;
|
||||
let p2tr = ComputedFromHeightFull::forced_import(db, "p2tr_count", version, indexes)?;
|
||||
let p2wpkh = ComputedFromHeightFull::forced_import(db, "p2wpkh_count", version, indexes)?;
|
||||
let p2wsh = ComputedFromHeightFull::forced_import(db, "p2wsh_count", version, indexes)?;
|
||||
|
||||
// Aggregate counts (computed from per-type counts)
|
||||
let segwit = ComputedFromHeightFull::forced_import(db, "segwit_count", version, indexes)?;
|
||||
|
||||
// Adoption ratios (lazy)
|
||||
// Uses outputs.count.count as denominator (total output count)
|
||||
// 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.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.read_only_boxed_clone(),
|
||||
outputs.count.total_count.height.sum_cum.sum.0.read_only_boxed_clone(),
|
||||
&segwit,
|
||||
&outputs.count.total_count,
|
||||
);
|
||||
let p2a = ComputedFromHeightCumSum::forced_import(db, "p2a_count", version, indexes)?;
|
||||
let p2ms = ComputedFromHeightCumSum::forced_import(db, "p2ms_count", version, indexes)?;
|
||||
let p2pk33 =
|
||||
ComputedFromHeightCumSum::forced_import(db, "p2pk33_count", version, indexes)?;
|
||||
let p2pk65 =
|
||||
ComputedFromHeightCumSum::forced_import(db, "p2pk65_count", version, indexes)?;
|
||||
let p2pkh = ComputedFromHeightCumSum::forced_import(db, "p2pkh_count", version, indexes)?;
|
||||
let p2sh = ComputedFromHeightCumSum::forced_import(db, "p2sh_count", version, indexes)?;
|
||||
let p2tr = ComputedFromHeightCumSum::forced_import(db, "p2tr_count", version, indexes)?;
|
||||
let p2wpkh =
|
||||
ComputedFromHeightCumSum::forced_import(db, "p2wpkh_count", version, indexes)?;
|
||||
let p2wsh = ComputedFromHeightCumSum::forced_import(db, "p2wsh_count", version, indexes)?;
|
||||
let segwit =
|
||||
ComputedFromHeightCumSum::forced_import(db, "segwit_count", version, indexes)?;
|
||||
|
||||
Ok(Self {
|
||||
p2a,
|
||||
@@ -59,22 +39,37 @@ impl Vecs {
|
||||
p2tr,
|
||||
p2wpkh,
|
||||
p2wsh,
|
||||
opreturn: ComputedFromHeightFull::forced_import(db, "opreturn_count", version, indexes)?,
|
||||
emptyoutput: ComputedFromHeightFull::forced_import(
|
||||
opreturn: ComputedFromHeightCumSum::forced_import(
|
||||
db,
|
||||
"opreturn_count",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
emptyoutput: ComputedFromHeightCumSum::forced_import(
|
||||
db,
|
||||
"emptyoutput_count",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
unknownoutput: ComputedFromHeightFull::forced_import(
|
||||
unknownoutput: ComputedFromHeightCumSum::forced_import(
|
||||
db,
|
||||
"unknownoutput_count",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
segwit,
|
||||
taproot_adoption,
|
||||
segwit_adoption,
|
||||
taproot_adoption: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"taproot_adoption",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
segwit_adoption: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"segwit_adoption",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,29 +2,29 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{StoredF32, StoredU64};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightFull, LazyBinaryFromHeightFull};
|
||||
use crate::internal::{ComputedFromHeightCumSum, ComputedFromHeightLast};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
// Per-type output counts
|
||||
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>,
|
||||
pub p2a: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2ms: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2pk33: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2pk65: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2pkh: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2sh: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2tr: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2wpkh: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub p2wsh: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub opreturn: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub emptyoutput: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
pub unknownoutput: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
|
||||
// Aggregate counts
|
||||
/// SegWit output count (p2wpkh + p2wsh + p2tr)
|
||||
pub segwit: ComputedFromHeightFull<StoredU64, M>,
|
||||
pub segwit: ComputedFromHeightCumSum<StoredU64, M>,
|
||||
|
||||
// Adoption ratios
|
||||
pub taproot_adoption: LazyBinaryFromHeightFull<StoredF32, StoredU64, StoredU64>,
|
||||
pub segwit_adoption: LazyBinaryFromHeightFull<StoredF32, StoredU64, StoredU64>,
|
||||
// Adoption ratios (stored per-block, lazy period views)
|
||||
pub taproot_adoption: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub segwit_adoption: ComputedFromHeightLast<StoredF32, M>,
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, PAGE_SIZE};
|
||||
|
||||
use crate::{indexes, outputs, prices};
|
||||
use crate::{indexes, prices};
|
||||
|
||||
use super::{CountVecs, ValueVecs, Vecs};
|
||||
|
||||
@@ -15,14 +15,13 @@ impl Vecs {
|
||||
parent_version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
outputs: &outputs::Vecs,
|
||||
) -> Result<Self> {
|
||||
let db = Database::open(&parent_path.join(super::DB_NAME))?;
|
||||
db.set_min_len(PAGE_SIZE * 50_000_000)?;
|
||||
|
||||
let version = parent_version;
|
||||
|
||||
let count = CountVecs::forced_import(&db, version, indexes, outputs)?;
|
||||
let count = CountVecs::forced_import(&db, version, indexes)?;
|
||||
let value = ValueVecs::forced_import(&db, version, indexes, prices)?;
|
||||
|
||||
let this = Self { db, count, value };
|
||||
|
||||
Reference in New Issue
Block a user