diff --git a/crates/brk_computer/src/blocks/difficulty/import.rs b/crates/brk_computer/src/blocks/difficulty/import.rs index 56bdde520..b1e46aeaf 100644 --- a/crates/brk_computer/src/blocks/difficulty/import.rs +++ b/crates/brk_computer/src/blocks/difficulty/import.rs @@ -26,7 +26,7 @@ impl Vecs { indexes, ), as_hash: ComputedFromHeight::forced_import(db, "difficulty_as_hash", version, indexes)?, - adjustment: PercentFromHeight::forced_import_bps32(db, "difficulty_adjustment", version, indexes)?, + adjustment: PercentFromHeight::forced_import(db, "difficulty_adjustment", version, indexes)?, epoch: ComputedFromHeight::forced_import(db, "difficulty_epoch", version, indexes)?, blocks_before_next_adjustment: ComputedFromHeight::forced_import( db, diff --git a/crates/brk_computer/src/blocks/weight/import.rs b/crates/brk_computer/src/blocks/weight/import.rs index 895bf9f46..509939140 100644 --- a/crates/brk_computer/src/blocks/weight/import.rs +++ b/crates/brk_computer/src/blocks/weight/import.rs @@ -22,7 +22,7 @@ impl Vecs { )?; let fullness = - PercentFromHeightDistribution::forced_import_bp16(db, "block_fullness", version, indexes)?; + PercentFromHeightDistribution::forced_import(db, "block_fullness", version, indexes)?; Ok(Self { weight, fullness }) } diff --git a/crates/brk_computer/src/cointime/adjusted/import.rs b/crates/brk_computer/src/cointime/adjusted/import.rs index 232d6e21d..2881cbf35 100644 --- a/crates/brk_computer/src/cointime/adjusted/import.rs +++ b/crates/brk_computer/src/cointime/adjusted/import.rs @@ -11,7 +11,7 @@ use crate::{ impl Vecs { pub(crate) fn forced_import(db: &Database, version: Version, indexes: &indexes::Vecs) -> Result { Ok(Self { - cointime_adj_inflation_rate: PercentFromHeight::forced_import_bps32( + cointime_adj_inflation_rate: PercentFromHeight::forced_import( db, "cointime_adj_inflation_rate", version, diff --git a/crates/brk_computer/src/cointime/import.rs b/crates/brk_computer/src/cointime/import.rs index 40bfd808a..7885fb6e2 100644 --- a/crates/brk_computer/src/cointime/import.rs +++ b/crates/brk_computer/src/cointime/import.rs @@ -7,7 +7,7 @@ use vecdb::{Database, PAGE_SIZE}; use super::{ ActivityVecs, AdjustedVecs, CapVecs, DB_NAME, PricingVecs, ReserveRiskVecs, SupplyVecs, - VERSION, ValueVecs, Vecs, + ValueVecs, Vecs, }; use crate::indexes; @@ -20,7 +20,7 @@ impl Vecs { let db = Database::open(&parent_path.join(DB_NAME))?; db.set_min_len(PAGE_SIZE * 1_000_000)?; - let version = parent_version + VERSION; + let version = parent_version; let v1 = version + Version::ONE; let activity = ActivityVecs::forced_import(&db, version, indexes)?; let supply = SupplyVecs::forced_import(&db, v1, indexes)?; diff --git a/crates/brk_computer/src/cointime/mod.rs b/crates/brk_computer/src/cointime/mod.rs index 999739cab..c39ef5afe 100644 --- a/crates/brk_computer/src/cointime/mod.rs +++ b/crates/brk_computer/src/cointime/mod.rs @@ -10,7 +10,6 @@ mod compute; mod import; use brk_traversable::Traversable; -use brk_types::Version; use vecdb::{Database, Rw, StorageMode}; pub use activity::Vecs as ActivityVecs; @@ -22,7 +21,6 @@ pub use supply::Vecs as SupplyVecs; pub use value::Vecs as ValueVecs; pub const DB_NAME: &str = "cointime"; -const VERSION: Version = Version::ZERO; #[derive(Traversable)] pub struct Vecs { diff --git a/crates/brk_computer/src/distribution/address/growth_rate.rs b/crates/brk_computer/src/distribution/address/growth_rate.rs index 1cd96de59..f871d04e7 100644 --- a/crates/brk_computer/src/distribution/address/growth_rate.rs +++ b/crates/brk_computer/src/distribution/address/growth_rate.rs @@ -27,7 +27,7 @@ impl GrowthRateVecs { version: Version, indexes: &indexes::Vecs, ) -> Result { - let all = PercentFromHeightDistribution::forced_import_bp16( + let all = PercentFromHeightDistribution::forced_import( db, "growth_rate", version, @@ -35,7 +35,7 @@ impl GrowthRateVecs { )?; let by_addresstype = ByAddressType::new_with_name(|name| { - PercentFromHeightDistribution::forced_import_bp16( + PercentFromHeightDistribution::forced_import( db, &format!("{name}_growth_rate"), version, diff --git a/crates/brk_computer/src/distribution/cohorts/address/vecs.rs b/crates/brk_computer/src/distribution/cohorts/address/vecs.rs index 7c87163fb..6a3b7169a 100644 --- a/crates/brk_computer/src/distribution/cohorts/address/vecs.rs +++ b/crates/brk_computer/src/distribution/cohorts/address/vecs.rs @@ -19,8 +19,6 @@ use crate::distribution::metrics::{BasicCohortMetrics, CohortMetricsBase, Import use super::super::traits::{CohortVecs, DynCohortVecs}; -const VERSION: Version = Version::ZERO; - /// Address cohort with metrics and optional runtime state. #[derive(Traversable)] pub struct AddressCohortVecs { @@ -70,13 +68,13 @@ impl AddressCohortVecs { addr_count: ComputedFromHeight::forced_import( db, &cfg.name("addr_count"), - version + VERSION, + version, indexes, )?, addr_count_change_1m: ComputedFromHeight::forced_import( db, &cfg.name("addr_count_change_1m"), - version + VERSION, + version, indexes, )?, }) diff --git a/crates/brk_computer/src/distribution/compute/block_loop.rs b/crates/brk_computer/src/distribution/compute/block_loop.rs index f986aa65d..f146d9c51 100644 --- a/crates/brk_computer/src/distribution/compute/block_loop.rs +++ b/crates/brk_computer/src/distribution/compute/block_loop.rs @@ -67,8 +67,8 @@ pub(crate) fn process_blocks( let height_to_first_txoutindex = &indexer.vecs.outputs.first_txoutindex; let height_to_first_txinindex = &indexer.vecs.inputs.first_txinindex; let height_to_tx_count = &transactions.count.tx_count.height; - let height_to_output_count = &outputs.count.total_count.full.sum_cumulative.sum.0; - let height_to_input_count = &inputs.count.full.sum_cumulative.sum.0; + let height_to_output_count = &outputs.count.total_count.full.sum; + let height_to_input_count = &inputs.count.full.sum; let txindex_to_output_count = &indexes.txindex.output_count; let txindex_to_input_count = &indexes.txindex.input_count; diff --git a/crates/brk_computer/src/distribution/metrics/config.rs b/crates/brk_computer/src/distribution/metrics/config.rs index 551558a36..ad54caca4 100644 --- a/crates/brk_computer/src/distribution/metrics/config.rs +++ b/crates/brk_computer/src/distribution/metrics/config.rs @@ -69,7 +69,7 @@ impl<'a> ImportConfig<'a> { suffix: &str, offset: Version, ) -> Result> { - PercentFromHeight::forced_import_bp16(self.db, &self.name(suffix), self.version + offset, self.indexes) + PercentFromHeight::forced_import(self.db, &self.name(suffix), self.version + offset, self.indexes) } pub(crate) fn import_percent_bps16( @@ -77,7 +77,7 @@ impl<'a> ImportConfig<'a> { suffix: &str, offset: Version, ) -> Result> { - PercentFromHeight::forced_import_bps16(self.db, &self.name(suffix), self.version + offset, self.indexes) + PercentFromHeight::forced_import(self.db, &self.name(suffix), self.version + offset, self.indexes) } // --- Value types --- @@ -121,7 +121,7 @@ impl<'a> ImportConfig<'a> { } pub(crate) fn import_percent_rolling_bp16(&self, suffix: &str, offset: Version) -> Result> { - PercentRollingWindows::forced_import_bp16(self.db, &self.name(suffix), self.version + offset, self.indexes) + PercentRollingWindows::forced_import(self.db, &self.name(suffix), self.version + offset, self.indexes) } pub(crate) fn import_emas_1w_1m(&self, suffix: &str, offset: Version) -> Result> { @@ -129,7 +129,7 @@ impl<'a> ImportConfig<'a> { } pub(crate) fn import_percent_emas_1w_1m_bp16(&self, suffix: &str, offset: Version) -> Result> { - PercentRollingEmas1w1m::forced_import_bp16(self.db, &self.name(suffix), self.version + offset, self.indexes) + PercentRollingEmas1w1m::forced_import(self.db, &self.name(suffix), self.version + offset, self.indexes) } pub(crate) fn import_emas_2w(&self, suffix: &str, offset: Version) -> Result { diff --git a/crates/brk_computer/src/indexes/mod.rs b/crates/brk_computer/src/indexes/mod.rs index b9de0f230..75fc6a9d7 100644 --- a/crates/brk_computer/src/indexes/mod.rs +++ b/crates/brk_computer/src/indexes/mod.rs @@ -53,7 +53,6 @@ pub use week1::Vecs as Week1Vecs; pub use year1::Vecs as Year1Vecs; pub use year10::Vecs as Year10Vecs; -const VERSION: Version = Version::ZERO; pub const DB_NAME: &str = "indexes"; #[derive(Traversable)] @@ -90,7 +89,7 @@ impl Vecs { let db = Database::open(&parent.join(DB_NAME))?; db.set_min_len(PAGE_SIZE * 10_000_000)?; - let version = parent_version + VERSION; + let version = parent_version; let this = Self { address: AddressVecs::forced_import(version, indexer), diff --git a/crates/brk_computer/src/internal/aggregate/distribution.rs b/crates/brk_computer/src/internal/aggregate/distribution.rs index e16232637..2cad6fe9a 100644 --- a/crates/brk_computer/src/internal/aggregate/distribution.rs +++ b/crates/brk_computer/src/internal/aggregate/distribution.rs @@ -1,37 +1,37 @@ use brk_error::Result; +use brk_traversable::Traversable; use schemars::JsonSchema; use vecdb::{ - Database, Exit, ReadableVec, Ro, Rw, VecIndex, VecValue, Version, + Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableVec, Ro, Rw, StorageMode, StoredVec, + VecIndex, VecValue, Version, }; -use crate::internal::{ - AverageVec, ComputedVecValue, DistributionStats, MaxVec, MedianVec, MinVec, Pct10Vec, - Pct25Vec, Pct75Vec, Pct90Vec, -}; +use crate::internal::{ComputedVecValue, DistributionStats}; -/// Distribution stats (average + min + max + percentiles) — concrete vec type alias. -pub type Distribution = DistributionStats< - AverageVec, - MinVec, - MaxVec, - Pct10Vec, - Pct25Vec, - MedianVec, - Pct75Vec, - Pct90Vec, ->; +#[derive(Traversable)] +pub struct Distribution { + pub average: M::Stored>>, + pub min: M::Stored>>, + pub max: M::Stored>>, + pub pct10: M::Stored>>, + pub pct25: M::Stored>>, + pub median: M::Stored>>, + pub pct75: M::Stored>>, + pub pct90: M::Stored>>, +} impl Distribution { pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { + let s = DistributionStats::<()>::SUFFIXES; Ok(Self { - average: AverageVec::forced_import(db, name, version)?, - min: MinVec::forced_import(db, name, version)?, - max: MaxVec::forced_import(db, name, version)?, - pct10: Pct10Vec::forced_import(db, name, version)?, - pct25: Pct25Vec::forced_import(db, name, version)?, - median: MedianVec::forced_import(db, name, version)?, - pct75: Pct75Vec::forced_import(db, name, version)?, - pct90: Pct90Vec::forced_import(db, name, version)?, + average: EagerVec::forced_import(db, &format!("{name}_{}", s[0]), version)?, + min: EagerVec::forced_import(db, &format!("{name}_{}", s[1]), version)?, + max: EagerVec::forced_import(db, &format!("{name}_{}", s[2]), version)?, + pct10: EagerVec::forced_import(db, &format!("{name}_{}", s[3]), version)?, + pct25: EagerVec::forced_import(db, &format!("{name}_{}", s[4]), version)?, + median: EagerVec::forced_import(db, &format!("{name}_{}", s[5]), version)?, + pct75: EagerVec::forced_import(db, &format!("{name}_{}", s[6]), version)?, + pct90: EagerVec::forced_import(db, &format!("{name}_{}", s[7]), version)?, }) } @@ -59,16 +59,16 @@ impl Distribution { skip_count, None, // first None, // last - Some(&mut self.min.0), - Some(&mut self.max.0), - Some(&mut self.average.0), + Some(&mut self.min), + Some(&mut self.max), + Some(&mut self.average), None, // sum None, // cumulative - Some(&mut self.median.0), - Some(&mut self.pct10.0), - Some(&mut self.pct25.0), - Some(&mut self.pct75.0), - Some(&mut self.pct90.0), + Some(&mut self.median), + Some(&mut self.pct10), + Some(&mut self.pct25), + Some(&mut self.pct75), + Some(&mut self.pct90), ) } @@ -94,27 +94,27 @@ impl Distribution { count_indexes, n_blocks, exit, - &mut self.min.0, - &mut self.max.0, - &mut self.average.0, - &mut self.median.0, - &mut self.pct10.0, - &mut self.pct25.0, - &mut self.pct75.0, - &mut self.pct90.0, + &mut self.min, + &mut self.max, + &mut self.average, + &mut self.median, + &mut self.pct10, + &mut self.pct25, + &mut self.pct75, + &mut self.pct90, ) } pub fn read_only_clone(&self) -> Distribution { - DistributionStats { - average: self.average.read_only_clone(), - min: self.min.read_only_clone(), - max: self.max.read_only_clone(), - pct10: self.pct10.read_only_clone(), - pct25: self.pct25.read_only_clone(), - median: self.median.read_only_clone(), - pct75: self.pct75.read_only_clone(), - pct90: self.pct90.read_only_clone(), + Distribution { + average: StoredVec::read_only_clone(&self.average), + min: StoredVec::read_only_clone(&self.min), + max: StoredVec::read_only_clone(&self.max), + pct10: StoredVec::read_only_clone(&self.pct10), + pct25: StoredVec::read_only_clone(&self.pct25), + median: StoredVec::read_only_clone(&self.median), + pct75: StoredVec::read_only_clone(&self.pct75), + pct90: StoredVec::read_only_clone(&self.pct90), } } } diff --git a/crates/brk_computer/src/internal/aggregate/full.rs b/crates/brk_computer/src/internal/aggregate/full.rs index 499ddb955..66c299bdd 100644 --- a/crates/brk_computer/src/internal/aggregate/full.rs +++ b/crates/brk_computer/src/internal/aggregate/full.rs @@ -1,27 +1,30 @@ use brk_error::Result; use brk_traversable::Traversable; use schemars::JsonSchema; -use vecdb::{Database, Exit, ReadableVec, Ro, Rw, StorageMode, VecIndex, VecValue, Version}; +use vecdb::{ + Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableVec, Ro, Rw, StorageMode, StoredVec, + VecIndex, VecValue, Version, +}; use crate::internal::ComputedVecValue; -use super::{Distribution, SumCumulative}; +use super::Distribution; -/// Full stats aggregate: distribution + sum_cumulative -/// Matches the common full_stats() pattern: average + minmax + percentiles + sum + cumulative +/// Full stats aggregate: distribution + sum + cumulative #[derive(Traversable)] pub struct Full { #[traversable(flatten)] pub distribution: Distribution, - #[traversable(flatten)] - pub sum_cumulative: SumCumulative, + pub sum: M::Stored>>, + pub cumulative: M::Stored>>, } impl Full { pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { Ok(Self { distribution: Distribution::forced_import(db, name, version)?, - sum_cumulative: SumCumulative::forced_import(db, name, version)?, + sum: EagerVec::forced_import(db, &format!("{name}_sum"), version)?, + cumulative: EagerVec::forced_import(db, &format!("{name}_cumulative"), version)?, }) } @@ -49,23 +52,24 @@ impl Full { skip_count, None, // first None, // last - Some(&mut self.distribution.min.0), - Some(&mut self.distribution.max.0), - Some(&mut self.distribution.average.0), - Some(&mut self.sum_cumulative.sum.0), - Some(&mut self.sum_cumulative.cumulative.0), - Some(&mut self.distribution.median.0), - Some(&mut self.distribution.pct10.0), - Some(&mut self.distribution.pct25.0), - Some(&mut self.distribution.pct75.0), - Some(&mut self.distribution.pct90.0), + Some(&mut self.distribution.min), + Some(&mut self.distribution.max), + Some(&mut self.distribution.average), + Some(&mut self.sum), + Some(&mut self.cumulative), + Some(&mut self.distribution.median), + Some(&mut self.distribution.pct10), + Some(&mut self.distribution.pct25), + Some(&mut self.distribution.pct75), + Some(&mut self.distribution.pct90), ) } pub fn read_only_clone(&self) -> Full { Full { distribution: self.distribution.read_only_clone(), - sum_cumulative: self.sum_cumulative.read_only_clone(), + sum: StoredVec::read_only_clone(&self.sum), + cumulative: StoredVec::read_only_clone(&self.cumulative), } } } diff --git a/crates/brk_computer/src/internal/aggregate/mod.rs b/crates/brk_computer/src/internal/aggregate/mod.rs index 084778e94..b0d01da0e 100644 --- a/crates/brk_computer/src/internal/aggregate/mod.rs +++ b/crates/brk_computer/src/internal/aggregate/mod.rs @@ -1,7 +1,5 @@ mod distribution; mod full; -mod sum_cumulative; pub use distribution::*; pub use full::*; -pub use sum_cumulative::*; diff --git a/crates/brk_computer/src/internal/aggregate/sum_cumulative.rs b/crates/brk_computer/src/internal/aggregate/sum_cumulative.rs deleted file mode 100644 index fbd4ae696..000000000 --- a/crates/brk_computer/src/internal/aggregate/sum_cumulative.rs +++ /dev/null @@ -1,31 +0,0 @@ -use brk_error::Result; -use brk_traversable::Traversable; -use schemars::JsonSchema; -use vecdb::{Database, Ro, Rw, StorageMode, VecIndex, Version}; - -use crate::internal::{ComputedVecValue, CumulativeVec, SumVec}; - -/// Sum + Cumulative (12% of usage) -#[derive(Traversable)] -pub struct SumCumulative { - #[traversable(flatten)] - pub sum: SumVec, - #[traversable(flatten)] - pub cumulative: CumulativeVec, -} - -impl SumCumulative { - pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { - Ok(Self { - sum: SumVec::forced_import(db, name, version)?, - cumulative: CumulativeVec::forced_import(db, name, version)?, - }) - } - - pub fn read_only_clone(&self) -> SumCumulative { - SumCumulative { - sum: self.sum.read_only_clone(), - cumulative: self.cumulative.read_only_clone(), - } - } -} diff --git a/crates/brk_computer/src/internal/derived/full.rs b/crates/brk_computer/src/internal/derived/full.rs index a3f1e814b..0c18d8935 100644 --- a/crates/brk_computer/src/internal/derived/full.rs +++ b/crates/brk_computer/src/internal/derived/full.rs @@ -26,8 +26,6 @@ where pub rolling: RollingFull, } -const VERSION: Version = Version::ZERO; - impl ComputedHeightDerivedFull where T: NumericValue + JsonSchema, @@ -38,11 +36,9 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - let cumulative = - ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), v, indexes)?; - let rolling = RollingFull::forced_import(db, name, v, indexes)?; + ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), version, indexes)?; + let rolling = RollingFull::forced_import(db, name, version, indexes)?; Ok(Self { cumulative, diff --git a/crates/brk_computer/src/internal/derived/last.rs b/crates/brk_computer/src/internal/derived/last.rs index f7bd5129a..47714ba02 100644 --- a/crates/brk_computer/src/internal/derived/last.rs +++ b/crates/brk_computer/src/internal/derived/last.rs @@ -52,8 +52,6 @@ where } } -const VERSION: Version = Version::ZERO; - impl ComputedHeightDerived where T: NumericValue + JsonSchema, @@ -64,13 +62,11 @@ where version: Version, indexes: &indexes::Vecs, ) -> Self { - let v = version + VERSION; - macro_rules! period { ($idx:ident) => { LazyAggVec::sparse_from_first_index( name, - v, + version, height_source.clone(), indexes.$idx.first_height.read_only_boxed_clone(), ) @@ -112,7 +108,7 @@ where ($idx:ident) => { LazyAggVec::new( name, - v, + version, height_source.clone(), indexes.$idx.identity.read_only_boxed_clone(), for_each_range_from_coarser, diff --git a/crates/brk_computer/src/internal/derived/lazy_last.rs b/crates/brk_computer/src/internal/derived/lazy_last.rs index 98b0ba062..bb0b1407a 100644 --- a/crates/brk_computer/src/internal/derived/lazy_last.rs +++ b/crates/brk_computer/src/internal/derived/lazy_last.rs @@ -81,8 +81,6 @@ where T: VecValue + PartialOrd + JsonSchema, S1T: VecValue; -const VERSION: Version = Version::ZERO; - impl LazyHeightDerived where T: VecValue + PartialOrd + JsonSchema + 'static, @@ -121,13 +119,11 @@ where where S1T: NumericValue, { - let v = version + VERSION; - macro_rules! period { ($p:ident) => { LazyTransformLast::from_boxed::>( name, - v, + version, source.$p.read_only_boxed_clone(), ) }; @@ -135,7 +131,7 @@ where macro_rules! epoch { ($p:ident) => { - LazyTransformLast::from_boxed::(name, v, source.$p.read_only_boxed_clone()) + LazyTransformLast::from_boxed::(name, version, source.$p.read_only_boxed_clone()) }; } @@ -151,13 +147,11 @@ where F: UnaryTransform, S2T: ComputedVecValue + JsonSchema, { - let v = version + VERSION; - macro_rules! period { ($p:ident) => { LazyTransformLast::from_boxed::>( name, - v, + version, source.$p.read_only_boxed_clone(), ) }; @@ -165,7 +159,7 @@ where macro_rules! epoch { ($p:ident) => { - LazyTransformLast::from_boxed::(name, v, source.$p.read_only_boxed_clone()) + LazyTransformLast::from_boxed::(name, version, source.$p.read_only_boxed_clone()) }; } diff --git a/crates/brk_computer/src/internal/derived/lazy_value.rs b/crates/brk_computer/src/internal/derived/lazy_value.rs index 56452bd89..52077359c 100644 --- a/crates/brk_computer/src/internal/derived/lazy_value.rs +++ b/crates/brk_computer/src/internal/derived/lazy_value.rs @@ -6,8 +6,6 @@ use vecdb::UnaryTransform; use crate::internal::{LazyHeightDerived, ValueFromHeight}; -const VERSION: Version = Version::ZERO; - #[derive(Clone, Traversable)] pub struct LazyValueHeightDerived { pub sats: LazyHeightDerived, @@ -28,29 +26,27 @@ impl LazyValueHeightDerived { CentsTransform: UnaryTransform, DollarsTransform: UnaryTransform, { - let v = version + VERSION; - let sats = LazyHeightDerived::from_derived_computed::( name, - v, + version, &source.sats.rest, ); let btc = LazyHeightDerived::from_derived_computed::( &format!("{name}_btc"), - v, + version, &source.sats.rest, ); let cents = LazyHeightDerived::from_derived_computed::( &format!("{name}_cents"), - v, + version, &source.cents.rest, ); let usd = LazyHeightDerived::from_lazy::( &format!("{name}_usd"), - v, + version, &source.usd.rest, ); diff --git a/crates/brk_computer/src/internal/distribution_stats.rs b/crates/brk_computer/src/internal/distribution_stats.rs index 5d64bc6c8..951c8d94b 100644 --- a/crates/brk_computer/src/internal/distribution_stats.rs +++ b/crates/brk_computer/src/internal/distribution_stats.rs @@ -1,19 +1,15 @@ -//! Base generic struct with 8 type parameters — one per distribution statistic. -//! -//! Foundation for all distribution-style types (average, min, max, percentiles). - use brk_traversable::Traversable; #[derive(Clone, Traversable)] -pub struct DistributionStats { +pub struct DistributionStats { pub average: A, - pub min: B, - pub max: C, - pub pct10: D, - pub pct25: E, - pub median: F, - pub pct75: G, - pub pct90: H, + pub min: A, + pub max: A, + pub pct10: A, + pub pct25: A, + pub median: A, + pub pct75: A, + pub pct90: A, } impl DistributionStats { @@ -58,5 +54,4 @@ impl DistributionStats { .min(f(&self.pct75)) .min(f(&self.pct90)) } - } diff --git a/crates/brk_computer/src/internal/eager_indexes.rs b/crates/brk_computer/src/internal/eager_indexes.rs index 25f08e3ed..b86fa0a3f 100644 --- a/crates/brk_computer/src/internal/eager_indexes.rs +++ b/crates/brk_computer/src/internal/eager_indexes.rs @@ -47,18 +47,14 @@ pub struct EagerIndexes( where T: ComputedVecValue + PartialOrd + JsonSchema; -const VERSION: Version = Version::ZERO; - impl EagerIndexes where T: NumericValue + JsonSchema, { pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { - let v = version + VERSION; - macro_rules! period { ($idx:ident) => { - ImportableVec::forced_import(db, name, v)? + ImportableVec::forced_import(db, name, version)? }; } diff --git a/crates/brk_computer/src/internal/emas.rs b/crates/brk_computer/src/internal/emas.rs index 8107f73dd..c2a91bc2f 100644 --- a/crates/brk_computer/src/internal/emas.rs +++ b/crates/brk_computer/src/internal/emas.rs @@ -1,13 +1,11 @@ -//! Generic EMA window containers. - use brk_traversable::Traversable; #[derive(Clone, Traversable)] -pub struct Emas1w1m { +pub struct Emas1w1m { #[traversable(rename = "1w")] pub _1w: A, #[traversable(rename = "1m")] - pub _1m: B, + pub _1m: A, } impl Emas1w1m { @@ -30,29 +28,3 @@ impl Emas1w1m { [&mut self._1w, &mut self._1m] } } - -#[derive(Clone, Traversable)] -pub struct Emas2w { - #[traversable(rename = "2w")] - pub _2w: A, -} - -impl Emas2w { - pub const SUFFIXES: [&'static str; 1] = ["ema_2w"]; - - pub fn try_from_fn( - mut f: impl FnMut(&str) -> std::result::Result, - ) -> std::result::Result { - Ok(Self { - _2w: f(Self::SUFFIXES[0])?, - }) - } - - pub fn as_array(&self) -> [&A; 1] { - [&self._2w] - } - - pub fn as_mut_array(&mut self) -> [&mut A; 1] { - [&mut self._2w] - } -} diff --git a/crates/brk_computer/src/internal/from_height/aggregated.rs b/crates/brk_computer/src/internal/from_height/aggregated.rs index a8d956b0c..b87e16a51 100644 --- a/crates/brk_computer/src/internal/from_height/aggregated.rs +++ b/crates/brk_computer/src/internal/from_height/aggregated.rs @@ -26,8 +26,6 @@ where pub rolling: RollingFull, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeightAggregated where T: NumericValue + JsonSchema, @@ -38,10 +36,8 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height = Full::forced_import(db, name, v)?; - let rolling = RollingFull::forced_import(db, name, v, indexes)?; + let height = Full::forced_import(db, name, version)?; + let rolling = RollingFull::forced_import(db, name, version, indexes)?; Ok(Self { full: height, @@ -65,7 +61,7 @@ where self.rolling.compute( max_from, windows, - self.full.sum_cumulative.sum.inner(), + &self.full.sum, exit, )?; Ok(()) diff --git a/crates/brk_computer/src/internal/from_height/base.rs b/crates/brk_computer/src/internal/from_height/base.rs index 6e88709aa..20254fde6 100644 --- a/crates/brk_computer/src/internal/from_height/base.rs +++ b/crates/brk_computer/src/internal/from_height/base.rs @@ -28,8 +28,6 @@ where pub rest: Box>, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeight where T: NumericValue + JsonSchema, @@ -40,14 +38,12 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height: EagerVec> = EagerVec::forced_import(db, name, v)?; + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; let rest = ComputedHeightDerived::forced_import( name, height.read_only_boxed_clone(), - v, + version, indexes, ); diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs b/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs index 1e3678f9a..3eb3e8d95 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs +++ b/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs @@ -73,8 +73,6 @@ impl RollingFullSlot { #[traversable(transparent)] pub struct RollingFullByUnit(pub Windows>); -const VERSION: Version = Version::ZERO; - impl RollingFullByUnit { pub(crate) fn forced_import( db: &Database, @@ -82,9 +80,8 @@ impl RollingFullByUnit { version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self(Windows::try_from_fn(|suffix| { - RollingFullSlot::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + RollingFullSlot::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?)) } diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs b/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs index 821b4372c..00babf4f7 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs +++ b/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs @@ -16,8 +16,6 @@ use crate::{ #[traversable(transparent)] pub struct RollingSumByUnit(pub Windows>); -const VERSION: Version = Version::ZERO; - impl RollingSumByUnit { pub(crate) fn forced_import( db: &Database, @@ -25,8 +23,7 @@ impl RollingSumByUnit { version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - Ok(Self(Windows::::forced_import(db, &format!("{name}_sum"), v, indexes)?)) + Ok(Self(Windows::::forced_import(db, &format!("{name}_sum"), version, indexes)?)) } pub(crate) fn compute_rolling_sum( diff --git a/crates/brk_computer/src/internal/from_height/cumulative.rs b/crates/brk_computer/src/internal/from_height/cumulative.rs index 7421579b6..6fb70fb61 100644 --- a/crates/brk_computer/src/internal/from_height/cumulative.rs +++ b/crates/brk_computer/src/internal/from_height/cumulative.rs @@ -24,8 +24,6 @@ where pub cumulative: ComputedFromHeight, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeightCumulative where T: NumericValue + JsonSchema, @@ -36,11 +34,9 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height: EagerVec> = EagerVec::forced_import(db, name, v)?; + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; let cumulative = - ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), v, indexes)?; + ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), version, indexes)?; Ok(Self { height, cumulative }) } diff --git a/crates/brk_computer/src/internal/from_height/cumulative_sum.rs b/crates/brk_computer/src/internal/from_height/cumulative_sum.rs index 0dace89d2..b8ab055d6 100644 --- a/crates/brk_computer/src/internal/from_height/cumulative_sum.rs +++ b/crates/brk_computer/src/internal/from_height/cumulative_sum.rs @@ -27,8 +27,6 @@ where pub sum: RollingWindows, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeightCumulativeSum where T: NumericValue + JsonSchema, @@ -39,12 +37,10 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height: EagerVec> = EagerVec::forced_import(db, name, v)?; + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; let cumulative = - ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), v, indexes)?; - let rolling = RollingWindows::forced_import(db, name, v, indexes)?; + ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), version, indexes)?; + let rolling = RollingWindows::forced_import(db, name, version, indexes)?; Ok(Self { height, diff --git a/crates/brk_computer/src/internal/from_height/distribution.rs b/crates/brk_computer/src/internal/from_height/distribution.rs index c18f1191c..83695e68c 100644 --- a/crates/brk_computer/src/internal/from_height/distribution.rs +++ b/crates/brk_computer/src/internal/from_height/distribution.rs @@ -25,8 +25,6 @@ where pub rolling: RollingDistribution, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeightDistribution where T: NumericValue + JsonSchema, @@ -37,10 +35,8 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height: EagerVec> = EagerVec::forced_import(db, name, v)?; - let rolling = RollingDistribution::forced_import(db, name, v, indexes)?; + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; + let rolling = RollingDistribution::forced_import(db, name, version, indexes)?; Ok(Self { height, rolling }) } diff --git a/crates/brk_computer/src/internal/from_height/full.rs b/crates/brk_computer/src/internal/from_height/full.rs index 778a8f06d..88c7bb247 100644 --- a/crates/brk_computer/src/internal/from_height/full.rs +++ b/crates/brk_computer/src/internal/from_height/full.rs @@ -27,8 +27,6 @@ where pub rolling: RollingFull, } -const VERSION: Version = Version::ZERO; - impl ComputedFromHeightFull where T: NumericValue + JsonSchema, @@ -39,12 +37,10 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let height: EagerVec> = EagerVec::forced_import(db, name, v)?; + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; let cumulative = - ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), v, indexes)?; - let rolling = RollingFull::forced_import(db, name, v, indexes)?; + ComputedFromHeight::forced_import(db, &format!("{name}_cumulative"), version, indexes)?; + let rolling = RollingFull::forced_import(db, name, version, indexes)?; Ok(Self { height, diff --git a/crates/brk_computer/src/internal/from_height/lazy.rs b/crates/brk_computer/src/internal/from_height/lazy.rs index a36354113..c3f4dea3b 100644 --- a/crates/brk_computer/src/internal/from_height/lazy.rs +++ b/crates/brk_computer/src/internal/from_height/lazy.rs @@ -24,8 +24,6 @@ where pub rest: Box>, } -const VERSION: Version = Version::ZERO; - impl LazyFromHeight where T: ComputedVecValue + JsonSchema + 'static, @@ -40,10 +38,9 @@ where where S1T: NumericValue, { - let v = version + VERSION; Self { - height: LazyVecFrom1::transformed::(name, v, height_source), - rest: Box::new(LazyHeightDerived::from_computed::(name, v, source)), + height: LazyVecFrom1::transformed::(name, version, height_source), + rest: Box::new(LazyHeightDerived::from_computed::(name, version, source)), } } @@ -56,12 +53,11 @@ where where S1T: NumericValue, { - let v = version + VERSION; Self { - height: LazyVecFrom1::transformed::(name, v, height_source.clone()), + height: LazyVecFrom1::transformed::(name, version, height_source.clone()), rest: Box::new(LazyHeightDerived::from_height_source::( name, - v, + version, height_source, indexes, )), @@ -78,12 +74,11 @@ where F: UnaryTransform, S2T: ComputedVecValue + JsonSchema, { - let v = version + VERSION; Self { - height: LazyVecFrom1::transformed::(name, v, source.height.read_only_boxed_clone()), + height: LazyVecFrom1::transformed::(name, version, source.height.read_only_boxed_clone()), rest: Box::new(LazyHeightDerived::from_lazy::( name, - v, + version, &source.rest, )), } diff --git a/crates/brk_computer/src/internal/from_height/percent.rs b/crates/brk_computer/src/internal/from_height/percent.rs index df89ccdbe..098af48aa 100644 --- a/crates/brk_computer/src/internal/from_height/percent.rs +++ b/crates/brk_computer/src/internal/from_height/percent.rs @@ -1,17 +1,11 @@ use brk_error::Result; use brk_traversable::Traversable; -use brk_types::{ - BasisPoints16, BasisPointsSigned16, BasisPointsSigned32, Height, StoredF32, Version, -}; -use schemars::JsonSchema; -use vecdb::{BinaryTransform, Database, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, UnaryTransform, VecValue}; +use brk_types::{Height, StoredF32, Version}; +use vecdb::{BinaryTransform, Database, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, VecValue}; use crate::{ indexes, - internal::{ - Bp16ToFloat, Bp16ToPercent, Bps16ToFloat, Bps16ToPercent, Bps32ToFloat, Bps32ToPercent, - NumericValue, - }, + internal::BpsType, traits::ComputeDrawdown, }; @@ -21,44 +15,32 @@ use super::{ComputedFromHeight, LazyFromHeight}; /// /// Stores integer basis points on disk (Pco-compressed), /// exposes two lazy StoredF32 views: -/// - `ratio`: bps ÷ 10000 (e.g., 4523 bps → 0.4523) -/// - `percent`: bps ÷ 100 (e.g., 4523 bps → 45.23%) -/// -/// Use for dominance, adoption, RSI, and other percentage-valued metrics. +/// - `ratio`: bps / 10000 (e.g., 4523 bps -> 0.4523) +/// - `percent`: bps / 100 (e.g., 4523 bps -> 45.23%) #[derive(Traversable)] -pub struct PercentFromHeight -where - B: NumericValue + JsonSchema, -{ +pub struct PercentFromHeight { pub bps: ComputedFromHeight, pub ratio: LazyFromHeight, pub percent: LazyFromHeight, } -impl PercentFromHeight -where - B: NumericValue + JsonSchema, -{ - pub(crate) fn forced_import( +impl PercentFromHeight { + pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, - ) -> Result - where - RatioTransform: UnaryTransform, - PercentTransform: UnaryTransform, - { + ) -> Result { let bps = ComputedFromHeight::forced_import(db, &format!("{name}_bps"), version, indexes)?; - let ratio = LazyFromHeight::from_computed::( + let ratio = LazyFromHeight::from_computed::( &format!("{name}_ratio"), version, bps.height.read_only_boxed_clone(), &bps, ); - let percent = LazyFromHeight::from_computed::( + let percent = LazyFromHeight::from_computed::( name, version, bps.height.read_only_boxed_clone(), @@ -68,45 +50,6 @@ where Ok(Self { bps, ratio, percent }) } -} - -impl PercentFromHeight { - pub(crate) fn forced_import_bp16( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - Self::forced_import::(db, name, version, indexes) - } -} - -impl PercentFromHeight { - pub(crate) fn forced_import_bps16( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - Self::forced_import::(db, name, version, indexes) - } -} - -impl PercentFromHeight { - pub(crate) fn forced_import_bps32( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - Self::forced_import::(db, name, version, indexes) - } -} - -impl PercentFromHeight -where - B: NumericValue + JsonSchema, -{ pub(crate) fn compute_binary( &mut self, max_from: Height, diff --git a/crates/brk_computer/src/internal/from_height/percent_distribution.rs b/crates/brk_computer/src/internal/from_height/percent_distribution.rs index 82b18c2a4..9617b2394 100644 --- a/crates/brk_computer/src/internal/from_height/percent_distribution.rs +++ b/crates/brk_computer/src/internal/from_height/percent_distribution.rs @@ -1,48 +1,37 @@ use brk_error::Result; use brk_traversable::Traversable; -use brk_types::{BasisPoints16, Height, StoredF32, Version}; -use schemars::JsonSchema; -use vecdb::{Database, EagerVec, Exit, PcoVec, ReadableCloneableVec, Rw, StorageMode, UnaryTransform}; +use brk_types::{Height, StoredF32, Version}; +use vecdb::{Database, EagerVec, Exit, PcoVec, ReadableCloneableVec, Rw, StorageMode}; -use crate::{indexes, internal::{Bp16ToFloat, Bp16ToPercent, NumericValue, WindowStarts}}; +use crate::{indexes, internal::{BpsType, WindowStarts}}; use super::{ComputedFromHeightDistribution, LazyFromHeight}; /// Like PercentFromHeight but with rolling distribution stats on the bps data. #[derive(Traversable)] -pub struct PercentFromHeightDistribution -where - B: NumericValue + JsonSchema, -{ +pub struct PercentFromHeightDistribution { pub bps: ComputedFromHeightDistribution, pub ratio: LazyFromHeight, pub percent: LazyFromHeight, } -impl PercentFromHeightDistribution -where - B: NumericValue + JsonSchema, -{ - pub(crate) fn forced_import( +impl PercentFromHeightDistribution { + pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, - ) -> Result - where - RatioTransform: UnaryTransform, - PercentTransform: UnaryTransform, - { + ) -> Result { let bps = ComputedFromHeightDistribution::forced_import(db, &format!("{name}_bps"), version, indexes)?; - let ratio = LazyFromHeight::from_height_source::( + let ratio = LazyFromHeight::from_height_source::( &format!("{name}_ratio"), version, bps.height.read_only_boxed_clone(), indexes, ); - let percent = LazyFromHeight::from_height_source::( + let percent = LazyFromHeight::from_height_source::( name, version, bps.height.read_only_boxed_clone(), @@ -52,23 +41,6 @@ where Ok(Self { bps, ratio, percent }) } -} - -impl PercentFromHeightDistribution { - pub(crate) fn forced_import_bp16( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - Self::forced_import::(db, name, version, indexes) - } -} - -impl PercentFromHeightDistribution -where - B: NumericValue + JsonSchema, -{ pub(crate) fn compute( &mut self, max_from: Height, diff --git a/crates/brk_computer/src/internal/from_height/value/change.rs b/crates/brk_computer/src/internal/from_height/value/change.rs index d085449a7..421e841b1 100644 --- a/crates/brk_computer/src/internal/from_height/value/change.rs +++ b/crates/brk_computer/src/internal/from_height/value/change.rs @@ -12,8 +12,6 @@ use crate::{ }, }; -const VERSION: Version = Version::ZERO; - /// Change values indexed by height - sats (stored), btc (lazy), cents (stored), usd (lazy). #[derive(Traversable)] pub struct ValueFromHeightChange { @@ -30,13 +28,11 @@ impl ValueFromHeightChange { version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; - - let sats = ComputedFromHeight::forced_import(db, name, v, indexes)?; + let sats = ComputedFromHeight::forced_import(db, name, version, indexes)?; let btc = LazyFromHeight::from_computed::( &format!("{name}_btc"), - v, + version, sats.height.read_only_boxed_clone(), &sats, ); @@ -44,13 +40,13 @@ impl ValueFromHeightChange { let cents = ComputedFromHeight::forced_import( db, &format!("{name}_cents"), - v, + version, indexes, )?; let usd = LazyFromHeight::from_computed::( &format!("{name}_usd"), - v, + version, cents.height.read_only_boxed_clone(), ¢s, ); diff --git a/crates/brk_computer/src/internal/from_height/value/lazy.rs b/crates/brk_computer/src/internal/from_height/value/lazy.rs index d04808970..2a8826fdf 100644 --- a/crates/brk_computer/src/internal/from_height/value/lazy.rs +++ b/crates/brk_computer/src/internal/from_height/value/lazy.rs @@ -7,8 +7,6 @@ use vecdb::UnaryTransform; use crate::internal::{LazyValue, LazyValueHeightDerived, ValueFromHeight}; -const VERSION: Version = Version::ZERO; - /// Lazy value wrapper with height + all derived last transforms from ValueFromHeight. #[derive(Clone, Deref, DerefMut, Traversable)] #[traversable(merge)] @@ -33,14 +31,12 @@ impl LazyValueFromHeight { CentsTransform: UnaryTransform, DollarsTransform: UnaryTransform, { - let v = version + VERSION; - let height = - LazyValue::from_block_source::(name, source, v); + LazyValue::from_block_source::(name, source, version); let rest = LazyValueHeightDerived::from_block_source::( - name, source, v, + name, source, version, ); Self { height, rest: Box::new(rest) } diff --git a/crates/brk_computer/src/internal/from_height/value/rolling.rs b/crates/brk_computer/src/internal/from_height/value/rolling.rs index 2b92f16ee..2a2cf4537 100644 --- a/crates/brk_computer/src/internal/from_height/value/rolling.rs +++ b/crates/brk_computer/src/internal/from_height/value/rolling.rs @@ -25,8 +25,6 @@ pub struct ValueFromHeightRolling { pub rolling: ValueFromHeightWindows, } -const VERSION: Version = Version::ZERO; - impl ValueFromHeightRolling { pub(crate) fn forced_import( db: &Database, @@ -34,10 +32,9 @@ impl ValueFromHeightRolling { version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self { - value: Value::forced_import(db, name, v)?, - rolling: ValueFromHeightWindows::forced_import(db, name, v, indexes)?, + value: Value::forced_import(db, name, version)?, + rolling: ValueFromHeightWindows::forced_import(db, name, version, indexes)?, }) } diff --git a/crates/brk_computer/src/internal/lazy_value.rs b/crates/brk_computer/src/internal/lazy_value.rs index 6e5925fd0..5ca946123 100644 --- a/crates/brk_computer/src/internal/lazy_value.rs +++ b/crates/brk_computer/src/internal/lazy_value.rs @@ -6,8 +6,6 @@ use vecdb::{ReadableCloneableVec, LazyVecFrom1, UnaryTransform, VecIndex}; use crate::internal::ValueFromHeight; -const VERSION: Version = Version::ZERO; - /// Fully lazy value type at height level. /// /// All fields are lazy transforms from existing sources - no storage. @@ -31,26 +29,24 @@ impl LazyValue { CentsTransform: UnaryTransform, DollarsTransform: UnaryTransform, { - let v = version + VERSION; - let sats = - LazyVecFrom1::transformed::(name, v, source.sats.height.read_only_boxed_clone()); + LazyVecFrom1::transformed::(name, version, source.sats.height.read_only_boxed_clone()); let btc = LazyVecFrom1::transformed::( &format!("{name}_btc"), - v, + version, source.sats.height.read_only_boxed_clone(), ); let cents = LazyVecFrom1::transformed::( &format!("{name}_cents"), - v, + version, source.cents.height.read_only_boxed_clone(), ); let usd = LazyVecFrom1::transformed::( &format!("{name}_usd"), - v, + version, source.usd.height.read_only_boxed_clone(), ); diff --git a/crates/brk_computer/src/internal/mod.rs b/crates/brk_computer/src/internal/mod.rs index 360f3e4c7..2e31458eb 100644 --- a/crates/brk_computer/src/internal/mod.rs +++ b/crates/brk_computer/src/internal/mod.rs @@ -10,7 +10,6 @@ mod indexes; mod lazy_eager_indexes; mod lazy_value; mod rolling; -mod stat_vec; mod traits; mod transform; mod value; @@ -28,7 +27,6 @@ pub(crate) use indexes::*; pub(crate) use lazy_eager_indexes::*; pub(crate) use lazy_value::*; pub(crate) use rolling::*; -pub(crate) use stat_vec::*; pub(crate) use traits::*; pub use transform::*; pub(crate) use value::*; diff --git a/crates/brk_computer/src/internal/rolling/distribution.rs b/crates/brk_computer/src/internal/rolling/distribution.rs index eb4dd5557..b4aa58f44 100644 --- a/crates/brk_computer/src/internal/rolling/distribution.rs +++ b/crates/brk_computer/src/internal/rolling/distribution.rs @@ -24,8 +24,6 @@ pub struct RollingDistribution(pub DistributionStats RollingDistribution where T: NumericValue + JsonSchema, @@ -36,9 +34,8 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self(DistributionStats::try_from_fn(|suffix| { - RollingWindows::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + RollingWindows::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?)) } diff --git a/crates/brk_computer/src/internal/rolling/emas/percent_1w_1m.rs b/crates/brk_computer/src/internal/rolling/emas/percent_1w_1m.rs index 40fca736b..5e5d0e9cf 100644 --- a/crates/brk_computer/src/internal/rolling/emas/percent_1w_1m.rs +++ b/crates/brk_computer/src/internal/rolling/emas/percent_1w_1m.rs @@ -1,67 +1,37 @@ use brk_error::Result; use brk_traversable::Traversable; -use brk_types::{BasisPoints16, Height, StoredF32, Version}; +use brk_types::{Height, Version}; use derive_more::{Deref, DerefMut}; -use schemars::JsonSchema; -use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode, UnaryTransform}; +use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; use crate::{ indexes, - internal::{Bp16ToFloat, Bp16ToPercent, Emas1w1m, NumericValue, PercentFromHeight}, + internal::{BpsType, Emas1w1m, PercentFromHeight}, }; -const VERSION: Version = Version::ZERO; - /// 2 EMA vecs (1w, 1m) sourced from 24h rolling window, /// each storing basis points with lazy ratio and percent float views. #[derive(Deref, DerefMut, Traversable)] #[traversable(transparent)] -pub struct PercentRollingEmas1w1m(pub Emas1w1m>) -where - B: NumericValue + JsonSchema; +pub struct PercentRollingEmas1w1m(pub Emas1w1m>); -impl PercentRollingEmas1w1m -where - B: NumericValue + JsonSchema, -{ - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result - where - RatioTransform: UnaryTransform, - PercentTransform: UnaryTransform, - { - let v = version + VERSION; - Ok(Self(Emas1w1m::try_from_fn(|suffix| { - PercentFromHeight::forced_import::( - db, - &format!("{name}_{suffix}"), - v, - indexes, - ) - })?)) - } - -} - -impl PercentRollingEmas1w1m { - pub(crate) fn forced_import_bp16( +impl PercentRollingEmas1w1m { + pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, ) -> Result { - Self::forced_import::(db, name, version, indexes) + Ok(Self(Emas1w1m::try_from_fn(|suffix| { + PercentFromHeight::forced_import( + db, + &format!("{name}_{suffix}"), + version, + indexes, + ) + })?)) } -} -impl PercentRollingEmas1w1m -where - B: NumericValue + JsonSchema, -{ pub(crate) fn compute_from_24h( &mut self, max_from: Height, diff --git a/crates/brk_computer/src/internal/rolling/full.rs b/crates/brk_computer/src/internal/rolling/full.rs index 3d32569f7..cfccc6d5a 100644 --- a/crates/brk_computer/src/internal/rolling/full.rs +++ b/crates/brk_computer/src/internal/rolling/full.rs @@ -27,8 +27,6 @@ where pub distribution: RollingDistribution, } -const VERSION: Version = Version::ZERO; - impl RollingFull where T: NumericValue + JsonSchema, @@ -39,10 +37,9 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self { - sum: RollingWindows::forced_import(db, &format!("{name}_sum"), v, indexes)?, - distribution: RollingDistribution::forced_import(db, name, v, indexes)?, + sum: RollingWindows::forced_import(db, &format!("{name}_sum"), version, indexes)?, + distribution: RollingDistribution::forced_import(db, name, version, indexes)?, }) } diff --git a/crates/brk_computer/src/internal/rolling/percent_windows.rs b/crates/brk_computer/src/internal/rolling/percent_windows.rs index cdc8dadf1..b7b658327 100644 --- a/crates/brk_computer/src/internal/rolling/percent_windows.rs +++ b/crates/brk_computer/src/internal/rolling/percent_windows.rs @@ -1,58 +1,34 @@ use brk_error::Result; use brk_traversable::Traversable; -use brk_types::{BasisPoints16, StoredF32, Version}; +use brk_types::Version; use derive_more::{Deref, DerefMut}; -use schemars::JsonSchema; -use vecdb::{Database, Rw, StorageMode, UnaryTransform}; +use vecdb::{Database, Rw, StorageMode}; use crate::{ indexes, - internal::{Bp16ToFloat, Bp16ToPercent, NumericValue, PercentFromHeight, Windows}, + internal::{BpsType, PercentFromHeight, Windows}, }; -const VERSION: Version = Version::ZERO; - /// 4 rolling window vecs (24h, 1w, 1m, 1y), each storing basis points /// with lazy ratio and percent float views. #[derive(Deref, DerefMut, Traversable)] #[traversable(transparent)] -pub struct PercentRollingWindows(pub Windows>) -where - B: NumericValue + JsonSchema; +pub struct PercentRollingWindows(pub Windows>); -impl PercentRollingWindows -where - B: NumericValue + JsonSchema, -{ - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result - where - RatioTransform: UnaryTransform, - PercentTransform: UnaryTransform, - { - let v = version + VERSION; - Ok(Self(Windows::try_from_fn(|suffix| { - PercentFromHeight::forced_import::( - db, - &format!("{name}_{suffix}"), - v, - indexes, - ) - })?)) - } -} - -impl PercentRollingWindows { - pub(crate) fn forced_import_bp16( +impl PercentRollingWindows { + pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, ) -> Result { - Self::forced_import::(db, name, version, indexes) + Ok(Self(Windows::try_from_fn(|suffix| { + PercentFromHeight::forced_import( + db, + &format!("{name}_{suffix}"), + version, + indexes, + ) + })?)) } } diff --git a/crates/brk_computer/src/internal/rolling/value_windows.rs b/crates/brk_computer/src/internal/rolling/value_windows.rs index eb7dc0c39..946a60fab 100644 --- a/crates/brk_computer/src/internal/rolling/value_windows.rs +++ b/crates/brk_computer/src/internal/rolling/value_windows.rs @@ -17,8 +17,6 @@ use crate::{ internal::{ValueFromHeight, WindowStarts, Windows}, }; -const VERSION: Version = Version::ZERO; - /// Value rolling windows — window-first, currency-last. /// /// Each window contains `ValueFromHeight` (sats + btc lazy + usd). @@ -35,9 +33,8 @@ impl ValueFromHeightWindows { version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self(Windows::try_from_fn(|suffix| { - ValueFromHeight::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + ValueFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?)) } diff --git a/crates/brk_computer/src/internal/rolling/windows.rs b/crates/brk_computer/src/internal/rolling/windows.rs index 110732399..681324dcd 100644 --- a/crates/brk_computer/src/internal/rolling/windows.rs +++ b/crates/brk_computer/src/internal/rolling/windows.rs @@ -28,8 +28,6 @@ pub struct RollingWindows(pub Windows RollingWindows where T: NumericValue + JsonSchema, @@ -40,9 +38,8 @@ where version: Version, indexes: &indexes::Vecs, ) -> Result { - let v = version + VERSION; Ok(Self(Windows::try_from_fn(|suffix| { - ComputedFromHeight::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + ComputedFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?)) } diff --git a/crates/brk_computer/src/internal/stat_vec.rs b/crates/brk_computer/src/internal/stat_vec.rs deleted file mode 100644 index be8b6995d..000000000 --- a/crates/brk_computer/src/internal/stat_vec.rs +++ /dev/null @@ -1,103 +0,0 @@ -use brk_error::Result; -use brk_traversable::Traversable; -use derive_more::{Deref, DerefMut}; -use schemars::JsonSchema; -use vecdb::{ - Database, EagerVec, ImportableVec, PcoVec, Ro, Rw, StorageMode, StoredVec, VecIndex, Version, -}; - -use crate::internal::ComputedVecValue; - -macro_rules! define_stat_vec { - ($name:ident, $suffix:literal, $doc:literal) => { - #[doc = $doc] - #[derive(Deref, DerefMut, Traversable)] - pub struct $name( - pub M::Stored>>, - ); - - impl $name { - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - ) -> Result { - Ok(Self(EagerVec::forced_import( - db, - &format!("{name}_{}", $suffix), - version, - )?)) - } - - pub fn read_only_clone(&self) -> $name { - $name(StoredVec::read_only_clone(&self.0)) - } - } - }; -} - -macro_rules! define_stat_vec_transparent { - ($name:ident, $suffix:literal, $doc:literal) => { - #[doc = $doc] - #[derive(Deref, DerefMut, Traversable)] - #[traversable(transparent)] - pub struct $name( - pub M::Stored>>, - ); - - impl $name { - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - ) -> Result { - Ok(Self(EagerVec::forced_import( - db, - &format!("{name}_{}", $suffix), - version, - )?)) - } - - pub fn read_only_clone(&self) -> $name { - $name(StoredVec::read_only_clone(&self.0)) - } - } - }; -} - -define_stat_vec!(AverageVec, "average", "Average value in an aggregation period"); -define_stat_vec!(MinVec, "min", "Minimum value in an aggregation period"); -define_stat_vec!(MaxVec, "max", "Maximum value in an aggregation period"); -define_stat_vec!(Pct10Vec, "pct10", "10th percentile in an aggregation period"); -define_stat_vec!(Pct25Vec, "pct25", "25th percentile in an aggregation period"); -define_stat_vec!(MedianVec, "median", "Median (50th percentile) in an aggregation period"); -define_stat_vec!(Pct75Vec, "pct75", "75th percentile in an aggregation period"); -define_stat_vec!(Pct90Vec, "pct90", "90th percentile in an aggregation period"); - -define_stat_vec_transparent!(CumulativeVec, "cumulative", "Cumulative sum across aggregation periods"); - -/// Sum of values in an aggregation period -#[derive(Deref, DerefMut, Traversable)] -#[traversable(transparent)] -pub struct SumVec( - pub M::Stored>>, -); - -impl SumVec { - pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { - Ok(Self(EagerVec::forced_import( - db, - &format!("{name}_sum"), - version, - )?)) - } - - #[inline] - pub(crate) fn inner(&self) -> &EagerVec> { - &self.0 - } - - pub fn read_only_clone(&self) -> SumVec { - SumVec(StoredVec::read_only_clone(&self.0)) - } -} diff --git a/crates/brk_computer/src/internal/traits.rs b/crates/brk_computer/src/internal/traits.rs index 2646723a6..eea8904d7 100644 --- a/crates/brk_computer/src/internal/traits.rs +++ b/crates/brk_computer/src/internal/traits.rs @@ -1,9 +1,13 @@ -//! Internal traits for computed vec values. - use std::ops::{Add, AddAssign, Div}; +use brk_types::{BasisPoints16, BasisPointsSigned16, BasisPointsSigned32, StoredF32}; +use schemars::JsonSchema; use serde::Serialize; -use vecdb::{Formattable, PcoVecValue}; +use vecdb::{Formattable, PcoVecValue, UnaryTransform}; + +use crate::internal::{ + Bp16ToFloat, Bp16ToPercent, Bps16ToFloat, Bps16ToPercent, Bps32ToFloat, Bps32ToPercent, +}; pub trait ComputedVecValue where @@ -32,3 +36,24 @@ impl ComputedVecValue for T where pub trait NumericValue: ComputedVecValue + From + Into {} impl NumericValue for T where T: ComputedVecValue + From + Into {} + +/// Trait that associates a basis-point type with its transforms to ratio and percent. +pub trait BpsType: NumericValue + JsonSchema { + type ToRatio: UnaryTransform; + type ToPercent: UnaryTransform; +} + +impl BpsType for BasisPoints16 { + type ToRatio = Bp16ToFloat; + type ToPercent = Bp16ToPercent; +} + +impl BpsType for BasisPointsSigned16 { + type ToRatio = Bps16ToFloat; + type ToPercent = Bps16ToPercent; +} + +impl BpsType for BasisPointsSigned32 { + type ToRatio = Bps32ToFloat; + type ToPercent = Bps32ToPercent; +} diff --git a/crates/brk_computer/src/internal/windows.rs b/crates/brk_computer/src/internal/windows.rs index 875adf04e..db7da1622 100644 --- a/crates/brk_computer/src/internal/windows.rs +++ b/crates/brk_computer/src/internal/windows.rs @@ -1,19 +1,15 @@ -//! Base generic struct with 4 type parameters — one per rolling window duration. -//! -//! Foundation for all rolling window types (24h, 1w, 1m, 1y). - use brk_traversable::Traversable; #[derive(Clone, Traversable)] -pub struct Windows { +pub struct Windows { #[traversable(rename = "24h")] pub _24h: A, #[traversable(rename = "1w")] - pub _1w: B, + pub _1w: A, #[traversable(rename = "1m")] - pub _1m: C, + pub _1m: A, #[traversable(rename = "1y")] - pub _1y: D, + pub _1y: A, } impl Windows { diff --git a/crates/brk_computer/src/market/ath/import.rs b/crates/brk_computer/src/market/ath/import.rs index 78cbe9297..49620fce8 100644 --- a/crates/brk_computer/src/market/ath/import.rs +++ b/crates/brk_computer/src/market/ath/import.rs @@ -40,7 +40,7 @@ impl Vecs { ); let price_drawdown = - PercentFromHeight::forced_import_bps16(db, "price_drawdown", v, indexes)?; + PercentFromHeight::forced_import(db, "price_drawdown", v, indexes)?; Ok(Self { price_ath, diff --git a/crates/brk_computer/src/market/dca/import.rs b/crates/brk_computer/src/market/dca/import.rs index 082017bef..0e94e6cd3 100644 --- a/crates/brk_computer/src/market/dca/import.rs +++ b/crates/brk_computer/src/market/dca/import.rs @@ -23,7 +23,7 @@ impl Vecs { })?; let period_return = ByDcaPeriod::try_new(|name, _days| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("dca_return_{name}"), version, @@ -32,7 +32,7 @@ impl Vecs { })?; let period_cagr = ByDcaCagr::try_new(|name, _days| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("dca_cagr_{name}"), version, @@ -45,7 +45,7 @@ impl Vecs { })?; let period_lump_sum_return = ByDcaPeriod::try_new(|name, _days| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("lump_sum_return_{name}"), version, @@ -62,7 +62,7 @@ impl Vecs { })?; let class_return = ByDcaClass::try_new(|name, _year, _day1| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("dca_return_{name}"), version, diff --git a/crates/brk_computer/src/market/indicators/import.rs b/crates/brk_computer/src/market/indicators/import.rs index 2d12b07dd..6cce6cf1a 100644 --- a/crates/brk_computer/src/market/indicators/import.rs +++ b/crates/brk_computer/src/market/indicators/import.rs @@ -33,7 +33,7 @@ impl RsiChain { macro_rules! percent_import { ($name:expr) => { - PercentFromHeight::forced_import_bp16( + PercentFromHeight::forced_import( db, &format!("rsi_{}_{}", $name, tf), version, @@ -45,7 +45,7 @@ impl RsiChain { let average_gain = import!("average_gain"); let average_loss = import!("average_loss"); - let rsi = PercentFromHeight::forced_import_bp16( + let rsi = PercentFromHeight::forced_import( db, &format!("rsi_{tf}"), version, @@ -127,9 +127,9 @@ impl Vecs { let rsi = Windows::try_from_fn(|tf| RsiChain::forced_import(db, tf, v, indexes))?; let macd = Windows::try_from_fn(|tf| MacdChain::forced_import(db, tf, v, indexes))?; - let stoch_k = PercentFromHeight::forced_import_bp16(db, "stoch_k", v, indexes)?; - let stoch_d = PercentFromHeight::forced_import_bp16(db, "stoch_d", v, indexes)?; - let gini = PercentFromHeight::forced_import_bp16(db, "gini", v, indexes)?; + let stoch_k = PercentFromHeight::forced_import(db, "stoch_k", v, indexes)?; + let stoch_d = PercentFromHeight::forced_import(db, "stoch_d", v, indexes)?; + let gini = PercentFromHeight::forced_import(db, "gini", v, indexes)?; let pi_cycle = ComputedFromHeightRatio::forced_import_raw(db, "pi_cycle", v, indexes)?; diff --git a/crates/brk_computer/src/market/range/import.rs b/crates/brk_computer/src/market/range/import.rs index f3ec8d77e..7dfbb42a8 100644 --- a/crates/brk_computer/src/market/range/import.rs +++ b/crates/brk_computer/src/market/range/import.rs @@ -24,7 +24,7 @@ impl Vecs { price_true_range_sum_2w: ComputedFromHeight::forced_import( db, "price_true_range_sum_2w", version + v1, indexes, )?, - price_choppiness_index_2w: PercentFromHeight::forced_import_bp16( + price_choppiness_index_2w: PercentFromHeight::forced_import( db, "price_choppiness_index_2w", version + v1, indexes, )?, }) diff --git a/crates/brk_computer/src/market/returns/import.rs b/crates/brk_computer/src/market/returns/import.rs index 14c3c574e..6d9fc5b40 100644 --- a/crates/brk_computer/src/market/returns/import.rs +++ b/crates/brk_computer/src/market/returns/import.rs @@ -19,7 +19,7 @@ impl Vecs { let v1 = Version::ONE; let price_return = ByLookbackPeriod::try_new(|name, _days| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("price_return_{name}"), version, @@ -29,7 +29,7 @@ impl Vecs { // CAGR (computed, 2y+ only) let price_cagr = ByDcaCagr::try_new(|name, _days| { - PercentFromHeight::forced_import_bps32( + PercentFromHeight::forced_import( db, &format!("price_cagr_{name}"), version, diff --git a/crates/brk_computer/src/mining/hashrate/import.rs b/crates/brk_computer/src/mining/hashrate/import.rs index 3ac4d3074..3ff8b2b91 100644 --- a/crates/brk_computer/src/mining/hashrate/import.rs +++ b/crates/brk_computer/src/mining/hashrate/import.rs @@ -52,7 +52,7 @@ impl Vecs { version, indexes, )?, - hash_rate_drawdown: PercentFromHeight::forced_import_bps16( + hash_rate_drawdown: PercentFromHeight::forced_import( db, "hash_rate_drawdown", version, @@ -82,7 +82,7 @@ impl Vecs { version + v4, indexes, )?, - hash_price_rebound: PercentFromHeight::forced_import_bps32( + hash_price_rebound: PercentFromHeight::forced_import( db, "hash_price_rebound", version + v4, @@ -112,7 +112,7 @@ impl Vecs { version + v4, indexes, )?, - hash_value_rebound: PercentFromHeight::forced_import_bps32( + hash_value_rebound: PercentFromHeight::forced_import( db, "hash_value_rebound", version + v4, diff --git a/crates/brk_computer/src/mining/rewards/import.rs b/crates/brk_computer/src/mining/rewards/import.rs index 1caa941c4..e8477e88c 100644 --- a/crates/brk_computer/src/mining/rewards/import.rs +++ b/crates/brk_computer/src/mining/rewards/import.rs @@ -27,25 +27,25 @@ impl Vecs { version, indexes, )?, - fee_dominance: PercentFromHeight::forced_import_bp16( + fee_dominance: PercentFromHeight::forced_import( db, "fee_dominance", version, indexes, )?, - fee_dominance_rolling: PercentRollingWindows::forced_import_bp16( + fee_dominance_rolling: PercentRollingWindows::forced_import( db, "fee_dominance", version, indexes, )?, - subsidy_dominance: PercentFromHeight::forced_import_bp16( + subsidy_dominance: PercentFromHeight::forced_import( db, "subsidy_dominance", version, indexes, )?, - subsidy_dominance_rolling: PercentRollingWindows::forced_import_bp16( + subsidy_dominance_rolling: PercentRollingWindows::forced_import( db, "subsidy_dominance", version, diff --git a/crates/brk_computer/src/outputs/count/compute.rs b/crates/brk_computer/src/outputs/count/compute.rs index 50ef75f95..68cf7d35c 100644 --- a/crates/brk_computer/src/outputs/count/compute.rs +++ b/crates/brk_computer/src/outputs/count/compute.rs @@ -33,8 +33,8 @@ impl Vecs { self.utxo_count.height.compute_transform3( starting_indexes.height, - &*self.total_count.full.sum_cumulative.cumulative, - &*inputs_count.full.sum_cumulative.cumulative, + &self.total_count.full.cumulative, + &inputs_count.full.cumulative, &scripts_count.opreturn.cumulative.height, |(h, output_count, input_count, opreturn_count, ..)| { let block_count = u64::from(h + 1_usize); diff --git a/crates/brk_computer/src/pools/vecs.rs b/crates/brk_computer/src/pools/vecs.rs index 445493e22..ec32f16f2 100644 --- a/crates/brk_computer/src/pools/vecs.rs +++ b/crates/brk_computer/src/pools/vecs.rs @@ -58,9 +58,9 @@ impl Vecs { ValueFromHeightCumulativeSum::forced_import(db, &suffix("coinbase"), version, indexes)?; let dominance = - PercentFromHeight::forced_import_bp16(db, &suffix("dominance"), version, indexes)?; + PercentFromHeight::forced_import(db, &suffix("dominance"), version, indexes)?; let dominance_rolling = - PercentRollingWindows::forced_import_bp16(db, &suffix("dominance"), version, indexes)?; + PercentRollingWindows::forced_import(db, &suffix("dominance"), version, indexes)?; Ok(Self { dominance, diff --git a/crates/brk_computer/src/positions.rs b/crates/brk_computer/src/positions.rs index 583336823..21d402a1c 100644 --- a/crates/brk_computer/src/positions.rs +++ b/crates/brk_computer/src/positions.rs @@ -1,16 +1,16 @@ -use std::path::Path; +use std::{fs, path::Path}; use brk_error::Result; use brk_indexer::Indexer; -use brk_reader::Reader; +use brk_reader::{Reader, XOR_LEN, XORBytes}; use brk_traversable::Traversable; use brk_types::{BlkPosition, Height, Indexes, TxIndex, Version}; +use tracing::info; use vecdb::{ - AnyStoredVec, AnyVec, Database, Exit, WritableVec, ImportableVec, PAGE_SIZE, PcoVec, - ReadableVec, Rw, StorageMode, VecIndex, + AnyStoredVec, AnyVec, Database, Exit, ImportableVec, PAGE_SIZE, PcoVec, ReadableVec, Rw, + StorageMode, VecIndex, WritableVec, }; - pub const DB_NAME: &str = "positions"; #[derive(Traversable)] @@ -58,6 +58,29 @@ impl Vecs { Ok(()) } + fn check_xor_bytes(&mut self, reader: &Reader) -> Result<()> { + let xor_path = self.db.path().join("xor.dat"); + let current = reader.xor_bytes(); + let cached = fs::read(&xor_path) + .ok() + .and_then(|b| <[u8; XOR_LEN]>::try_from(b).ok()) + .map(XORBytes::from); + + match cached { + Some(c) if c == current => return Ok(()), + Some(_) => { + info!("XOR bytes changed, resetting positions..."); + self.block_position.reset()?; + self.tx_position.reset()?; + } + None => {} + } + + fs::write(&xor_path, *current)?; + + Ok(()) + } + fn compute_( &mut self, indexer: &Indexer, @@ -65,6 +88,8 @@ impl Vecs { parser: &Reader, exit: &Exit, ) -> Result<()> { + self.check_xor_bytes(parser)?; + // Validate computed versions against dependencies let dep_version = indexer.vecs.transactions.first_txindex.version() + indexer.vecs.transactions.height.version(); diff --git a/crates/brk_computer/src/scripts/adoption.rs b/crates/brk_computer/src/scripts/adoption.rs index 3cae9db6e..a9197dca7 100644 --- a/crates/brk_computer/src/scripts/adoption.rs +++ b/crates/brk_computer/src/scripts/adoption.rs @@ -24,13 +24,13 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - taproot: PercentFromHeight::forced_import_bp16( + taproot: PercentFromHeight::forced_import( db, "taproot_adoption", version, indexes, )?, - segwit: PercentFromHeight::forced_import_bp16( + segwit: PercentFromHeight::forced_import( db, "segwit_adoption", version, @@ -49,14 +49,14 @@ impl Vecs { self.taproot.compute_binary::<_, _, RatioU64Bp16>( starting_indexes.height, &count.p2tr.height, - &outputs_count.total_count.full.sum_cumulative.sum.0, + &outputs_count.total_count.full.sum, exit, )?; self.segwit.compute_binary::<_, _, RatioU64Bp16>( starting_indexes.height, &count.segwit.height, - &outputs_count.total_count.full.sum_cumulative.sum.0, + &outputs_count.total_count.full.sum, exit, )?; diff --git a/crates/brk_computer/src/supply/import.rs b/crates/brk_computer/src/supply/import.rs index a6ae38487..94579088a 100644 --- a/crates/brk_computer/src/supply/import.rs +++ b/crates/brk_computer/src/supply/import.rs @@ -42,7 +42,7 @@ impl Vecs { // Inflation rate let inflation_rate = - PercentFromHeight::forced_import_bps32(&db, "inflation_rate", version, indexes)?; + PercentFromHeight::forced_import(&db, "inflation_rate", version, indexes)?; // Velocity let velocity = super::velocity::Vecs::forced_import(&db, version, indexes)?; @@ -55,13 +55,13 @@ impl Vecs { ); // Growth rates - let market_cap_growth_rate = PercentFromHeight::forced_import_bps32( + let market_cap_growth_rate = PercentFromHeight::forced_import( &db, "market_cap_growth_rate", version + Version::ONE, indexes, )?; - let realized_cap_growth_rate = PercentFromHeight::forced_import_bps32( + let realized_cap_growth_rate = PercentFromHeight::forced_import( &db, "realized_cap_growth_rate", version + Version::ONE, diff --git a/crates/brk_computer/src/transactions/volume/compute.rs b/crates/brk_computer/src/transactions/volume/compute.rs index 7b172fc00..8890db3ea 100644 --- a/crates/brk_computer/src/transactions/volume/compute.rs +++ b/crates/brk_computer/src/transactions/volume/compute.rs @@ -75,13 +75,13 @@ impl Vecs { )?; self.inputs_per_sec.height.compute_binary::<_, Timestamp, PerSec>( starting_indexes.height, - &inputs_count.full.sum_cumulative.sum.0, + &inputs_count.full.sum, &blocks.interval.height, exit, )?; self.outputs_per_sec.height.compute_binary::<_, Timestamp, PerSec>( starting_indexes.height, - &outputs_count.total_count.full.sum_cumulative.sum.0, + &outputs_count.total_count.full.sum, &blocks.interval.height, exit, )?; diff --git a/crates/brk_reader/src/xor_bytes.rs b/crates/brk_reader/src/xor_bytes.rs index 5f9820272..a0920463d 100644 --- a/crates/brk_reader/src/xor_bytes.rs +++ b/crates/brk_reader/src/xor_bytes.rs @@ -4,9 +4,15 @@ use derive_more::Deref; pub const XOR_LEN: usize = 8; -#[derive(Debug, Clone, Copy, Deref)] +#[derive(Debug, Clone, Copy, Deref, PartialEq, Eq)] pub struct XORBytes([u8; XOR_LEN]); +impl From<[u8; XOR_LEN]> for XORBytes { + fn from(value: [u8; XOR_LEN]) -> Self { + Self(value) + } +} + impl From<&Path> for XORBytes { #[inline] fn from(value: &Path) -> Self {