mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 23:29:58 -07:00
computer: simplified a bunch of things
This commit is contained in:
@@ -15,17 +15,24 @@ impl Vecs {
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Block count height + cumulative first (rolling computed after window starts)
|
||||
self.block_count.height.compute_range(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.weight,
|
||||
|h| (h, StoredU32::from(1_u32)),
|
||||
exit,
|
||||
)?;
|
||||
self.block_count
|
||||
.compute_cumulative(starting_indexes, exit)?;
|
||||
self.block_count.cumulative.height.compute_cumulative(
|
||||
starting_indexes.height,
|
||||
&self.block_count.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute rolling window starts (collect monotonic data once for all windows)
|
||||
let monotonic_data: Vec<Timestamp> = time.timestamp_monotonic.collect();
|
||||
self.compute_rolling_start_hours(&monotonic_data, time, starting_indexes, exit, 1, |s| {
|
||||
&mut s.height_1h_ago
|
||||
})?;
|
||||
self.compute_rolling_start(&monotonic_data, time, starting_indexes, exit, 1, |s| {
|
||||
&mut s.height_24h_ago
|
||||
})?;
|
||||
@@ -157,13 +164,19 @@ impl Vecs {
|
||||
|s| &mut s.height_10y_ago,
|
||||
)?;
|
||||
|
||||
// Compute rolling window block counts
|
||||
// Compute rolling window block counts (both block_count's own rolling + separate block_count_sum)
|
||||
let ws = crate::internal::WindowStarts {
|
||||
_24h: &self.height_24h_ago,
|
||||
_7d: &self.height_1w_ago,
|
||||
_30d: &self.height_1m_ago,
|
||||
_1y: &self.height_1y_ago,
|
||||
};
|
||||
self.block_count.rolling.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&ws,
|
||||
&self.block_count.height,
|
||||
exit,
|
||||
)?;
|
||||
self.block_count_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&ws,
|
||||
@@ -202,4 +215,33 @@ impl Vecs {
|
||||
exit,
|
||||
)?)
|
||||
}
|
||||
|
||||
fn compute_rolling_start_hours<F>(
|
||||
&mut self,
|
||||
monotonic_data: &[Timestamp],
|
||||
time: &time::Vecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
hours: usize,
|
||||
get_field: F,
|
||||
) -> Result<()>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> &mut EagerVec<PcoVec<Height, Height>>,
|
||||
{
|
||||
let mut prev = Height::ZERO;
|
||||
Ok(get_field(self).compute_transform(
|
||||
starting_indexes.height,
|
||||
&time.timestamp_monotonic,
|
||||
|(h, t, ..)| {
|
||||
while t.difference_in_hours_between(monotonic_data[prev.to_usize()]) >= hours {
|
||||
prev.increment();
|
||||
if prev > h {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
(h, prev)
|
||||
},
|
||||
exit,
|
||||
)?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use vecdb::{Database, ImportableVec};
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{BlockCountTarget, ComputedFromHeightSumCum, ConstantVecs, RollingWindows},
|
||||
internal::{BlockCountTarget, ComputedFromHeightCumulativeSum, ConstantVecs, RollingWindows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -16,12 +16,13 @@ impl Vecs {
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
block_count: ComputedFromHeightSumCum::forced_import(
|
||||
block_count: ComputedFromHeightCumulativeSum::forced_import(
|
||||
db,
|
||||
"block_count",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
height_1h_ago: ImportableVec::forced_import(db, "height_1h_ago", version)?,
|
||||
height_24h_ago: ImportableVec::forced_import(db, "height_24h_ago", version)?,
|
||||
height_3d_ago: ImportableVec::forced_import(db, "height_3d_ago", version)?,
|
||||
height_1w_ago: ImportableVec::forced_import(db, "height_1w_ago", version)?,
|
||||
|
||||
@@ -2,14 +2,17 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Height, StoredU32, StoredU64};
|
||||
use vecdb::{EagerVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightSumCum, ConstantVecs, RollingWindows, WindowStarts};
|
||||
use crate::internal::{
|
||||
BlockWindowStarts, ComputedFromHeightCumulativeSum, ConstantVecs, RollingWindows, WindowStarts,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub block_count_target: ConstantVecs<StoredU64>,
|
||||
pub block_count: ComputedFromHeightSumCum<StoredU32, M>,
|
||||
pub block_count: ComputedFromHeightCumulativeSum<StoredU32, M>,
|
||||
pub block_count_sum: RollingWindows<StoredU32, M>,
|
||||
|
||||
pub height_1h_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub height_24h_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub height_3d_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub height_1w_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
@@ -53,6 +56,14 @@ impl Vecs {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the 2 block-count rolling window start heights (1h, 24h) for tx-derived metrics.
|
||||
pub fn block_window_starts(&self) -> BlockWindowStarts<'_> {
|
||||
BlockWindowStarts {
|
||||
_1h: &self.height_1h_ago,
|
||||
_24h: &self.height_24h_ago,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_vec(&self, days: usize) -> &EagerVec<PcoVec<Height, Height>> {
|
||||
match days {
|
||||
1 => &self.height_24h_ago,
|
||||
|
||||
@@ -27,8 +27,8 @@ impl Vecs {
|
||||
|
||||
let count = CountVecs::forced_import(&db, version, indexes)?;
|
||||
let interval = IntervalVecs::forced_import(&db, version, indexes)?;
|
||||
let size = SizeVecs::forced_import(&db, version, indexer, indexes)?;
|
||||
let weight = WeightVecs::forced_import(&db, version, indexer, indexes)?;
|
||||
let size = SizeVecs::forced_import(&db, version, indexes)?;
|
||||
let weight = WeightVecs::forced_import(&db, version, indexes)?;
|
||||
let time = TimeVecs::forced_import(&db, version)?;
|
||||
let difficulty = DifficultyVecs::forced_import(&db, version, indexer, indexes)?;
|
||||
let halving = HalvingVecs::forced_import(&db, version, indexes)?;
|
||||
|
||||
@@ -1,32 +1,29 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, ReadableCloneableVec};
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightCumFull, ComputedHeightDerivedCumFull},
|
||||
internal::{ComputedFromHeightCumulativeFull, ComputedHeightDerivedCumulativeFull},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
vbytes: ComputedFromHeightCumFull::forced_import(
|
||||
vbytes: ComputedFromHeightCumulativeFull::forced_import(
|
||||
db,
|
||||
"block_vbytes",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
size: ComputedHeightDerivedCumFull::forced_import(
|
||||
size: ComputedHeightDerivedCumulativeFull::forced_import(
|
||||
db,
|
||||
"block_size",
|
||||
indexer.vecs.blocks.total_size.read_only_boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
|
||||
@@ -2,10 +2,10 @@ use brk_traversable::Traversable;
|
||||
use brk_types::StoredU64;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightCumFull, ComputedHeightDerivedCumFull};
|
||||
use crate::internal::{ComputedFromHeightCumulativeFull, ComputedHeightDerivedCumulativeFull};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub vbytes: ComputedFromHeightCumFull<StoredU64, M>,
|
||||
pub size: ComputedHeightDerivedCumFull<StoredU64, M>,
|
||||
pub vbytes: ComputedFromHeightCumulativeFull<StoredU64, M>,
|
||||
pub size: ComputedHeightDerivedCumulativeFull<StoredU64, M>,
|
||||
}
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::Version;
|
||||
use vecdb::{Database, ReadableCloneableVec};
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightLast, ComputedHeightDerivedCumFull, RollingDistribution},
|
||||
internal::{ComputedFromHeightLast, ComputedHeightDerivedCumulativeFull, RollingDistribution},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let weight = ComputedHeightDerivedCumFull::forced_import(
|
||||
let weight = ComputedHeightDerivedCumulativeFull::forced_import(
|
||||
db,
|
||||
"block_weight",
|
||||
indexer.vecs.blocks.weight.read_only_boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
|
||||
@@ -3,12 +3,12 @@ use brk_types::{StoredF32, Weight};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
ComputedFromHeightLast, ComputedHeightDerivedCumFull, RollingDistribution,
|
||||
ComputedFromHeightLast, ComputedHeightDerivedCumulativeFull, RollingDistribution,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub weight: ComputedHeightDerivedCumFull<Weight, M>,
|
||||
pub weight: ComputedHeightDerivedCumulativeFull<Weight, M>,
|
||||
pub fullness: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub fullness_rolling: RollingDistribution<StoredF32, M>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user