mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 23:29:58 -07:00
computer: indexes + rolling
This commit is contained in:
@@ -14,11 +14,19 @@ impl Vecs {
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.interval.compute(indexer, starting_indexes, exit)?;
|
||||
self.time.timestamp.compute_first(
|
||||
starting_indexes,
|
||||
&indexer.vecs.blocks.timestamp,
|
||||
indexes,
|
||||
exit,
|
||||
)?;
|
||||
self.count
|
||||
.compute(indexer, &self.time, starting_indexes, exit)?;
|
||||
self.size.compute(indexer, starting_indexes, exit)?;
|
||||
self.weight.compute(indexer, starting_indexes, exit)?;
|
||||
self.interval
|
||||
.compute(indexer, &self.count, starting_indexes, exit)?;
|
||||
self.size.compute(indexer, &self.count, starting_indexes, exit)?;
|
||||
self.weight
|
||||
.compute(indexer, &self.count, starting_indexes, exit)?;
|
||||
self.difficulty
|
||||
.compute(indexer, indexes, starting_indexes, exit)?;
|
||||
self.halving.compute(indexes, starting_indexes, exit)?;
|
||||
|
||||
@@ -158,28 +158,16 @@ impl Vecs {
|
||||
)?;
|
||||
|
||||
// Compute rolling window block counts
|
||||
self.block_count_24h_sum.height.compute_transform(
|
||||
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_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&self.height_24h_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
)?;
|
||||
self.block_count_1w_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1w_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
)?;
|
||||
self.block_count_1m_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1m_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
exit,
|
||||
)?;
|
||||
self.block_count_1y_sum.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&self.height_1y_ago,
|
||||
|(h, start, ..)| (h, StoredU32::from(*h + 1 - *start)),
|
||||
&ws,
|
||||
&self.block_count.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use vecdb::{Database, ImportableVec};
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{BlockCountTarget, ComputedFromHeightLast, ComputedFromHeightSumCum, ConstantVecs},
|
||||
internal::{BlockCountTarget, ComputedFromHeightSumCum, ConstantVecs, RollingWindows},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -52,27 +52,9 @@ impl Vecs {
|
||||
height_6y_ago: ImportableVec::forced_import(db, "height_6y_ago", version)?,
|
||||
height_8y_ago: ImportableVec::forced_import(db, "height_8y_ago", version)?,
|
||||
height_10y_ago: ImportableVec::forced_import(db, "height_10y_ago", version)?,
|
||||
block_count_24h_sum: ComputedFromHeightLast::forced_import(
|
||||
block_count_sum: RollingWindows::forced_import(
|
||||
db,
|
||||
"block_count_24h_sum",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
block_count_1w_sum: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"block_count_1w_sum",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
block_count_1m_sum: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"block_count_1m_sum",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
block_count_1y_sum: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
"block_count_1y_sum",
|
||||
"block_count_sum",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
|
||||
@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Height, StoredU32, StoredU64};
|
||||
use vecdb::{EagerVec, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightLast, ComputedFromHeightSumCum, ConstantVecs};
|
||||
use crate::internal::{ComputedFromHeightSumCum, ConstantVecs, RollingWindows, WindowStarts};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
@@ -40,13 +40,20 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub height_8y_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
pub height_10y_ago: M::Stored<EagerVec<PcoVec<Height, Height>>>,
|
||||
// Rolling window block counts
|
||||
pub block_count_24h_sum: ComputedFromHeightLast<StoredU32, M>,
|
||||
pub block_count_1w_sum: ComputedFromHeightLast<StoredU32, M>,
|
||||
pub block_count_1m_sum: ComputedFromHeightLast<StoredU32, M>,
|
||||
pub block_count_1y_sum: ComputedFromHeightLast<StoredU32, M>,
|
||||
pub block_count_sum: RollingWindows<StoredU32, M>,
|
||||
}
|
||||
|
||||
impl Vecs {
|
||||
/// Get the standard 4 rolling window start heights (24h, 7d, 30d, 1y).
|
||||
pub fn window_starts(&self) -> WindowStarts<'_> {
|
||||
WindowStarts {
|
||||
_24h: &self.height_24h_ago,
|
||||
_7d: &self.height_1w_ago,
|
||||
_30d: &self.height_1m_ago,
|
||||
_1y: &self.height_1y_ago,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_vec(&self, days: usize) -> &EagerVec<PcoVec<Height, Height>> {
|
||||
match days {
|
||||
1 => &self.height_24h_ago,
|
||||
|
||||
@@ -6,7 +6,7 @@ use vecdb::{Database, ReadableCloneableVec};
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightLast, ComputedFromHeightSum, ComputedHeightDerivedLast},
|
||||
internal::{ComputedFromHeightLast, ComputedHeightDerivedLast},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -26,7 +26,7 @@ impl Vecs {
|
||||
indexes,
|
||||
),
|
||||
as_hash: ComputedFromHeightLast::forced_import(db, "difficulty_as_hash", version, indexes)?,
|
||||
adjustment: ComputedFromHeightSum::forced_import(db, "difficulty_adjustment", version, indexes)?,
|
||||
adjustment: ComputedFromHeightLast::forced_import(db, "difficulty_adjustment", version, indexes)?,
|
||||
epoch: ComputedFromHeightLast::forced_import(db, "difficulty_epoch", version, indexes)?,
|
||||
blocks_before_next_adjustment: ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
|
||||
@@ -2,7 +2,7 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{DifficultyEpoch, StoredF32, StoredF64, StoredU32};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedFromHeightLast, ComputedFromHeightSum, ComputedHeightDerivedLast};
|
||||
use crate::internal::{ComputedFromHeightLast, ComputedHeightDerivedLast};
|
||||
|
||||
/// Difficulty metrics: raw difficulty, derived stats, adjustment, and countdown
|
||||
#[derive(Traversable)]
|
||||
@@ -10,7 +10,7 @@ pub struct Vecs<M: StorageMode = Rw> {
|
||||
/// Raw difficulty with day1/period stats - merges with indexer's raw
|
||||
pub raw: ComputedHeightDerivedLast<StoredF64>,
|
||||
pub as_hash: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub adjustment: ComputedFromHeightSum<StoredF32, M>,
|
||||
pub adjustment: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub epoch: ComputedFromHeightLast<DifficultyEpoch, M>,
|
||||
pub blocks_before_next_adjustment: ComputedFromHeightLast<StoredU32, M>,
|
||||
pub days_before_next_adjustment: ComputedFromHeightLast<StoredF32, M>,
|
||||
|
||||
@@ -29,7 +29,7 @@ impl Vecs {
|
||||
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 time = TimeVecs::forced_import(&db, version, indexer, 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)?;
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@ use brk_types::{CheckedSub, Timestamp};
|
||||
use vecdb::{Exit, ReadableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::ComputeIndexes;
|
||||
use crate::{blocks, ComputeIndexes};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
count_vecs: &blocks::CountVecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
@@ -31,6 +32,15 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let window_starts = count_vecs.window_starts();
|
||||
self.interval_rolling.compute_distribution(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.interval.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ use brk_types::Version;
|
||||
use vecdb::Database;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::ComputedFromHeightDistribution};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightLast, RollingDistribution},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
@@ -11,13 +14,15 @@ impl Vecs {
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let interval = ComputedFromHeightDistribution::forced_import(
|
||||
db,
|
||||
"block_interval",
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
let interval =
|
||||
ComputedFromHeightLast::forced_import(db, "block_interval", version, indexes)?;
|
||||
|
||||
Ok(Self { interval })
|
||||
let interval_rolling =
|
||||
RollingDistribution::forced_import(db, "block_interval", version, indexes)?;
|
||||
|
||||
Ok(Self {
|
||||
interval,
|
||||
interval_rolling,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ use brk_traversable::Traversable;
|
||||
use brk_types::Timestamp;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::ComputedFromHeightDistribution;
|
||||
use crate::internal::{ComputedFromHeightLast, RollingDistribution};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
#[traversable(flatten)]
|
||||
pub interval: ComputedFromHeightDistribution<Timestamp, M>,
|
||||
pub interval: ComputedFromHeightLast<Timestamp, M>,
|
||||
pub interval_rolling: RollingDistribution<Timestamp, M>,
|
||||
}
|
||||
|
||||
@@ -1,21 +1,43 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::StoredU64;
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::ComputeIndexes;
|
||||
use crate::{blocks, ComputeIndexes};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
count_vecs: &blocks::CountVecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.size
|
||||
.compute_cumulative(starting_indexes, &indexer.vecs.blocks.total_size, exit)?;
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
self.vbytes.compute_cumulative(starting_indexes, exit)?;
|
||||
// vbytes = ceil(weight / 4), stored at height level
|
||||
self.vbytes.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
exit,
|
||||
|height| {
|
||||
Ok(height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.weight,
|
||||
|(h, weight, ..)| (h, StoredU64::from(weight.to_vbytes_floor())),
|
||||
exit,
|
||||
)?)
|
||||
},
|
||||
)?;
|
||||
|
||||
// size from indexer total_size
|
||||
self.size.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&indexer.vecs.blocks.total_size,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ use brk_types::Version;
|
||||
use vecdb::{Database, ReadableCloneableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::{ComputedHeightDerivedFull, LazyComputedFromHeightFull, WeightToVbytes}};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedFromHeightCumFull, ComputedHeightDerivedCumFull},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
@@ -14,14 +17,13 @@ impl Vecs {
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
vbytes: LazyComputedFromHeightFull::forced_import::<WeightToVbytes>(
|
||||
vbytes: ComputedFromHeightCumFull::forced_import(
|
||||
db,
|
||||
"block_vbytes",
|
||||
version,
|
||||
&indexer.vecs.blocks.weight,
|
||||
indexes,
|
||||
)?,
|
||||
size: ComputedHeightDerivedFull::forced_import(
|
||||
size: ComputedHeightDerivedCumFull::forced_import(
|
||||
db,
|
||||
"block_size",
|
||||
indexer.vecs.blocks.total_size.read_only_boxed_clone(),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{StoredU64, Weight};
|
||||
use brk_types::StoredU64;
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedHeightDerivedFull, LazyComputedFromHeightFull};
|
||||
use crate::internal::{ComputedFromHeightCumFull, ComputedHeightDerivedCumFull};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub vbytes: LazyComputedFromHeightFull<StoredU64, Weight, M>,
|
||||
pub size: ComputedHeightDerivedFull<StoredU64, M>,
|
||||
pub vbytes: ComputedFromHeightCumFull<StoredU64, M>,
|
||||
pub size: ComputedHeightDerivedCumFull<StoredU64, M>,
|
||||
}
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::{Date, Height, Version};
|
||||
use vecdb::{Database, EagerVec, ImportableVec, ReadableCloneableVec, LazyVecFrom1};
|
||||
use vecdb::{Database, EagerVec, ImportableVec, LazyVecFrom1, ReadableCloneableVec};
|
||||
|
||||
use super::Vecs;
|
||||
use crate::{indexes, internal::ComputedHeightDerivedFirst};
|
||||
use crate::internal::EagerIndexes;
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
version: Version,
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
pub(crate) fn forced_import(db: &Database, version: Version) -> Result<Self> {
|
||||
let timestamp_monotonic =
|
||||
EagerVec::forced_import(db, "timestamp_monotonic", version)?;
|
||||
|
||||
@@ -24,12 +18,7 @@ impl Vecs {
|
||||
|_height: Height, timestamp| Date::from(timestamp),
|
||||
),
|
||||
timestamp_monotonic,
|
||||
timestamp: ComputedHeightDerivedFirst::forced_import(
|
||||
"timestamp",
|
||||
indexer.vecs.blocks.timestamp.read_only_boxed_clone(),
|
||||
version,
|
||||
indexes,
|
||||
),
|
||||
timestamp: EagerIndexes::forced_import(db, "timestamp", version)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Date, Height, Timestamp};
|
||||
use vecdb::{EagerVec, LazyVecFrom1, PcoVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::ComputedHeightDerivedFirst;
|
||||
use crate::internal::EagerIndexes;
|
||||
|
||||
/// Timestamp and date metrics for blocks
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub date: LazyVecFrom1<Height, Date, Height, Timestamp>,
|
||||
pub timestamp_monotonic: M::Stored<EagerVec<PcoVec<Height, Timestamp>>>,
|
||||
pub timestamp: ComputedHeightDerivedFirst<Timestamp>,
|
||||
pub timestamp: EagerIndexes<Timestamp, M>,
|
||||
}
|
||||
|
||||
@@ -1,19 +1,41 @@
|
||||
use brk_error::Result;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::StoredF32;
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::Vecs;
|
||||
use crate::ComputeIndexes;
|
||||
use crate::{blocks, ComputeIndexes};
|
||||
|
||||
impl Vecs {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
indexer: &Indexer,
|
||||
count_vecs: &blocks::CountVecs,
|
||||
starting_indexes: &ComputeIndexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.weight
|
||||
.compute_cumulative(starting_indexes, &indexer.vecs.blocks.weight, exit)?;
|
||||
let window_starts = count_vecs.window_starts();
|
||||
|
||||
self.weight.compute(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&indexer.vecs.blocks.weight,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.fullness.height.compute_transform(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.blocks.weight,
|
||||
|(h, weight, ..)| (h, StoredF32::from(weight.fullness())),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.fullness_rolling.compute_distribution(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.fullness.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use vecdb::{Database, ReadableCloneableVec};
|
||||
use super::Vecs;
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{ComputedHeightDerivedFull, LazyFromHeightTransformDistribution, WeightToFullness},
|
||||
internal::{ComputedFromHeightLast, ComputedHeightDerivedCumFull, RollingDistribution},
|
||||
};
|
||||
|
||||
impl Vecs {
|
||||
@@ -16,7 +16,7 @@ impl Vecs {
|
||||
indexer: &Indexer,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let weight = ComputedHeightDerivedFull::forced_import(
|
||||
let weight = ComputedHeightDerivedCumFull::forced_import(
|
||||
db,
|
||||
"block_weight",
|
||||
indexer.vecs.blocks.weight.read_only_boxed_clone(),
|
||||
@@ -24,13 +24,16 @@ impl Vecs {
|
||||
indexes,
|
||||
)?;
|
||||
|
||||
let fullness = LazyFromHeightTransformDistribution::from_derived::<WeightToFullness>(
|
||||
"block_fullness",
|
||||
version,
|
||||
indexer.vecs.blocks.weight.read_only_boxed_clone(),
|
||||
&weight,
|
||||
);
|
||||
let fullness =
|
||||
ComputedFromHeightLast::forced_import(db, "block_fullness", version, indexes)?;
|
||||
|
||||
Ok(Self { weight, fullness })
|
||||
let fullness_rolling =
|
||||
RollingDistribution::forced_import(db, "block_fullness", version, indexes)?;
|
||||
|
||||
Ok(Self {
|
||||
weight,
|
||||
fullness,
|
||||
fullness_rolling,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{StoredF32, Weight};
|
||||
use vecdb::{Rw, StorageMode};
|
||||
|
||||
use crate::internal::{ComputedHeightDerivedFull, LazyFromHeightTransformDistribution};
|
||||
use crate::internal::{
|
||||
ComputedFromHeightLast, ComputedHeightDerivedCumFull, RollingDistribution,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
pub weight: ComputedHeightDerivedFull<Weight, M>,
|
||||
pub fullness: LazyFromHeightTransformDistribution<StoredF32, Weight>,
|
||||
pub weight: ComputedHeightDerivedCumFull<Weight, M>,
|
||||
pub fullness: ComputedFromHeightLast<StoredF32, M>,
|
||||
pub fullness_rolling: RollingDistribution<StoredF32, M>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user