From 3e8cf4a975cec1dd46895b80ff8fd19c600f7c2a Mon Sep 17 00:00:00 2001 From: nym21 Date: Mon, 9 Mar 2026 11:16:50 +0100 Subject: [PATCH] computer: snapshot --- crates/brk_computer/examples/tree.rs | 36 +++ crates/brk_computer/src/blocks/compute.rs | 10 +- .../brk_computer/src/blocks/count/compute.rs | 163 +--------- .../brk_computer/src/blocks/count/import.rs | 53 +-- crates/brk_computer/src/blocks/count/vecs.rs | 112 +------ crates/brk_computer/src/blocks/import.rs | 5 +- .../src/blocks/interval/compute.rs | 4 +- crates/brk_computer/src/blocks/lookback.rs | 306 ++++++++++++++++++ crates/brk_computer/src/blocks/mod.rs | 3 + .../brk_computer/src/blocks/size/compute.rs | 4 +- .../brk_computer/src/blocks/weight/compute.rs | 4 +- .../src/cointime/activity/compute.rs | 2 +- .../src/cointime/reserve_risk/compute.rs | 2 +- .../src/cointime/supply/import.rs | 11 +- .../brk_computer/src/cointime/supply/vecs.rs | 6 +- .../src/cointime/value/compute.rs | 2 +- .../distribution/cohorts/address/groups.rs | 2 +- .../src/distribution/cohorts/utxo/groups.rs | 97 +++--- .../src/distribution/metrics/activity/core.rs | 2 +- .../src/distribution/metrics/activity/full.rs | 3 +- .../src/distribution/metrics/cohort/all.rs | 4 +- .../distribution/metrics/cohort/extended.rs | 4 +- .../src/distribution/metrics/config.rs | 27 +- .../src/distribution/metrics/outputs.rs | 2 +- .../distribution/metrics/realized/adjusted.rs | 44 ++- .../src/distribution/metrics/realized/base.rs | 4 +- .../src/distribution/metrics/realized/core.rs | 8 +- .../src/distribution/metrics/realized/full.rs | 14 +- .../distribution/metrics/realized/minimal.rs | 4 +- .../src/distribution/metrics/supply.rs | 16 +- .../metrics/unrealized/minimal.rs | 18 +- crates/brk_computer/src/distribution/vecs.rs | 2 +- .../brk_computer/src/inputs/count/compute.rs | 2 +- .../src/internal/{value => amount}/base.rs | 17 +- .../src/internal/{value => amount}/lazy.rs | 12 +- .../src/internal/{value => amount}/mod.rs | 0 .../internal/containers/distribution_stats.rs | 2 +- .../derived/{lazy_value.rs => lazy_amount.rs} | 12 +- .../brk_computer/src/internal/derived/mod.rs | 4 +- .../{value => amount}/cumulative.rs | 19 +- .../{value => amount}/cumulative_sum.rs | 23 +- .../from_height/{value => amount}/full.rs | 23 +- .../from_height/{value => amount}/lazy.rs | 20 +- .../src/internal/from_height/amount/mod.rs | 122 +++++++ .../from_height/{value => amount}/rolling.rs | 24 +- .../{by_unit => amount}/rolling_full.rs | 16 +- .../{by_unit => amount}/rolling_sum.rs | 8 +- .../src/internal/from_height/by_unit/mod.rs | 77 ----- .../src/internal/from_height/fiat.rs | 2 +- .../src/internal/from_height/lazy_fiat.rs | 2 +- .../src/internal/from_height/mod.rs | 6 +- .../internal/from_height/ratio/percentiles.rs | 4 +- .../src/internal/from_height/stddev/mod.rs | 2 +- .../src/internal/from_height/value/base.rs | 69 ---- .../src/internal/from_height/value/mod.rs | 13 - crates/brk_computer/src/internal/mod.rs | 4 +- .../{value_windows.rs => amount_windows.rs} | 12 +- .../brk_computer/src/internal/rolling/mod.rs | 4 +- crates/brk_computer/src/market/dca/compute.rs | 2 +- crates/brk_computer/src/market/dca/import.rs | 8 +- crates/brk_computer/src/market/dca/vecs.rs | 8 +- .../src/market/indicators/compute.rs | 2 +- .../src/market/indicators/macd.rs | 6 +- .../brk_computer/src/market/indicators/rsi.rs | 4 +- .../src/market/lookback/compute.rs | 2 +- .../src/market/moving_average/compute.rs | 4 +- .../brk_computer/src/market/range/compute.rs | 12 +- crates/brk_computer/src/mining/compute.rs | 3 +- .../src/mining/hashrate/compute.rs | 12 +- .../src/mining/rewards/compute.rs | 6 +- .../brk_computer/src/mining/rewards/import.rs | 14 +- .../brk_computer/src/mining/rewards/vecs.rs | 12 +- .../brk_computer/src/outputs/count/compute.rs | 2 +- crates/brk_computer/src/pools/major.rs | 27 +- crates/brk_computer/src/scripts/compute.rs | 2 +- .../brk_computer/src/scripts/count/compute.rs | 4 +- .../brk_computer/src/scripts/value/import.rs | 4 +- crates/brk_computer/src/scripts/value/vecs.rs | 4 +- .../brk_computer/src/supply/burned/compute.rs | 4 +- .../brk_computer/src/supply/burned/import.rs | 6 +- crates/brk_computer/src/supply/burned/vecs.rs | 6 +- crates/brk_computer/src/supply/compute.rs | 6 +- crates/brk_computer/src/supply/import.rs | 4 +- crates/brk_computer/src/supply/vecs.rs | 4 +- .../src/supply/velocity/compute.rs | 4 +- .../brk_computer/src/transactions/compute.rs | 4 +- .../src/transactions/count/compute.rs | 4 +- .../src/transactions/versions/compute.rs | 4 +- .../src/transactions/volume/compute.rs | 4 +- .../src/transactions/volume/import.rs | 8 +- .../src/transactions/volume/vecs.rs | 8 +- crates/brk_indexer/src/vecs/blocks.rs | 1 + 92 files changed, 853 insertions(+), 825 deletions(-) create mode 100644 crates/brk_computer/examples/tree.rs create mode 100644 crates/brk_computer/src/blocks/lookback.rs rename crates/brk_computer/src/internal/{value => amount}/base.rs (78%) rename crates/brk_computer/src/internal/{value => amount}/lazy.rs (91%) rename crates/brk_computer/src/internal/{value => amount}/mod.rs (100%) rename crates/brk_computer/src/internal/derived/{lazy_value.rs => lazy_amount.rs} (88%) rename crates/brk_computer/src/internal/from_height/{value => amount}/cumulative.rs (75%) rename crates/brk_computer/src/internal/from_height/{value => amount}/cumulative_sum.rs (70%) rename crates/brk_computer/src/internal/from_height/{value => amount}/full.rs (71%) rename crates/brk_computer/src/internal/from_height/{value => amount}/lazy.rs (72%) create mode 100644 crates/brk_computer/src/internal/from_height/amount/mod.rs rename crates/brk_computer/src/internal/from_height/{value => amount}/rolling.rs (65%) rename crates/brk_computer/src/internal/from_height/{by_unit => amount}/rolling_full.rs (83%) rename crates/brk_computer/src/internal/from_height/{by_unit => amount}/rolling_sum.rs (84%) delete mode 100644 crates/brk_computer/src/internal/from_height/by_unit/mod.rs delete mode 100644 crates/brk_computer/src/internal/from_height/value/base.rs delete mode 100644 crates/brk_computer/src/internal/from_height/value/mod.rs rename crates/brk_computer/src/internal/rolling/{value_windows.rs => amount_windows.rs} (75%) diff --git a/crates/brk_computer/examples/tree.rs b/crates/brk_computer/examples/tree.rs new file mode 100644 index 000000000..f25278399 --- /dev/null +++ b/crates/brk_computer/examples/tree.rs @@ -0,0 +1,36 @@ +use std::{env, fs, path::Path}; + +use brk_computer::Computer; +use brk_indexer::Indexer; +use brk_traversable::{Traversable, TreeNode}; + +pub fn main() -> color_eyre::Result<()> { + color_eyre::install()?; + + let tmp = env::temp_dir().join("brk_tree_gen"); + fs::create_dir_all(&tmp)?; + + let indexer = Indexer::forced_import(&tmp)?; + let computer = Computer::forced_import(&tmp, &indexer)?; + + let tree = TreeNode::Branch( + [ + ("indexed".to_string(), indexer.vecs.to_tree_node()), + ("computed".to_string(), computer.to_tree_node()), + ] + .into_iter() + .collect(), + ) + .merge_branches() + .expect("Tree merge failed"); + + let json = serde_json::to_string_pretty(&tree)?; + + let out_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tree.json"); + fs::write(&out_path, &json)?; + eprintln!("Wrote {} bytes to {}", json.len(), out_path.display()); + + fs::remove_dir_all(&tmp)?; + + Ok(()) +} diff --git a/crates/brk_computer/src/blocks/compute.rs b/crates/brk_computer/src/blocks/compute.rs index c9306bef1..cfa8a7ae3 100644 --- a/crates/brk_computer/src/blocks/compute.rs +++ b/crates/brk_computer/src/blocks/compute.rs @@ -18,14 +18,16 @@ impl Vecs { self.time .timestamp .compute(indexer, indexes, starting_indexes, exit)?; + self.lookback + .compute(&self.time, starting_indexes, exit)?; self.count - .compute(indexer, &self.time, starting_indexes, exit)?; + .compute(indexer, &self.lookback, starting_indexes, exit)?; self.interval - .compute(indexer, &self.count, starting_indexes, exit)?; + .compute(indexer, &self.lookback, starting_indexes, exit)?; self.size - .compute(indexer, &self.count, starting_indexes, exit)?; + .compute(indexer, &self.lookback, starting_indexes, exit)?; self.weight - .compute(indexer, &self.count, starting_indexes, exit)?; + .compute(indexer, &self.lookback, starting_indexes, exit)?; self.difficulty .compute(indexer, indexes, starting_indexes, exit)?; self.halving.compute(indexes, starting_indexes, exit)?; diff --git a/crates/brk_computer/src/blocks/count/compute.rs b/crates/brk_computer/src/blocks/count/compute.rs index 110d97285..62709a7aa 100644 --- a/crates/brk_computer/src/blocks/count/compute.rs +++ b/crates/brk_computer/src/blocks/count/compute.rs @@ -1,21 +1,21 @@ use brk_error::Result; use brk_indexer::Indexer; -use brk_types::{Height, Indexes, StoredU32, Timestamp}; -use vecdb::{AnyVec, Cursor, EagerVec, Exit, PcoVec, ReadableVec, VecIndex}; +use brk_types::{Indexes, StoredU32}; +use vecdb::Exit; -use crate::internal::WindowStarts; +use super::Vecs; -use super::{super::time, Vecs}; +use crate::blocks::lookback; impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - time: &time::Vecs, + lookback: &lookback::Vecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - // Block count height + cumulative first (rolling computed after window starts) + // Block count height + cumulative self.block_count.height.compute_range( starting_indexes.height, &indexer.vecs.blocks.weight, @@ -28,162 +28,15 @@ impl Vecs { exit, )?; - // Compute rolling window starts - self.compute_rolling_start_hours(time, starting_indexes, exit, 1, |s| { - &mut s.height_1h_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 1, |s| &mut s.height_24h_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 3, |s| &mut s.height_3d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 7, |s| &mut s.height_1w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 8, |s| &mut s.height_8d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 9, |s| &mut s.height_9d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 12, |s| &mut s.height_12d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 13, |s| &mut s.height_13d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 14, |s| &mut s.height_2w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 21, |s| &mut s.height_21d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 26, |s| &mut s.height_26d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 30, |s| &mut s.height_1m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 34, |s| &mut s.height_34d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 55, |s| &mut s.height_55d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 60, |s| &mut s.height_2m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 63, |s| &mut s.height_9w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 84, |s| &mut s.height_12w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 89, |s| &mut s.height_89d_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 90, |s| &mut s.height_3m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 98, |s| &mut s.height_14w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 111, |s| { - &mut s.height_111d_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 144, |s| { - &mut s.height_144d_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 180, |s| &mut s.height_6m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 182, |s| &mut s.height_26w_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 200, |s| { - &mut s.height_200d_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 270, |s| &mut s.height_9m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 350, |s| { - &mut s.height_350d_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 360, |s| &mut s.height_12m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 365, |s| &mut s.height_1y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 420, |s| &mut s.height_14m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 730, |s| &mut s.height_2y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 780, |s| &mut s.height_26m_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 1095, |s| &mut s.height_3y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 1400, |s| { - &mut s.height_200w_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 1460, |s| &mut s.height_4y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 1825, |s| &mut s.height_5y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 2190, |s| &mut s.height_6y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 2920, |s| &mut s.height_8y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 3285, |s| &mut s.height_9y_ago)?; - self.compute_rolling_start(time, starting_indexes, exit, 3650, |s| { - &mut s.height_10y_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 4380, |s| { - &mut s.height_12y_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 5110, |s| { - &mut s.height_14y_ago - })?; - self.compute_rolling_start(time, starting_indexes, exit, 9490, |s| { - &mut s.height_26y_ago - })?; - - // Compute rolling window block counts (both block_count's own rolling + separate block_count_sum) - let ws = WindowStarts { - _24h: &self.height_24h_ago, - _1w: &self.height_1w_ago, - _1m: &self.height_1m_ago, - _1y: &self.height_1y_ago, - }; + // Rolling window block counts + let ws = lookback.window_starts(); self.block_count.sum.compute_rolling_sum( starting_indexes.height, &ws, &self.block_count.height, exit, )?; - self.block_count_sum.compute_rolling_sum( - starting_indexes.height, - &ws, - &self.block_count.height, - exit, - )?; Ok(()) } - - fn compute_rolling_start( - &mut self, - time: &time::Vecs, - starting_indexes: &Indexes, - exit: &Exit, - days: usize, - get_field: F, - ) -> Result<()> - where - F: FnOnce(&mut Self) -> &mut EagerVec>, - { - self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| { - t.difference_in_days_between(prev_ts) >= days - }) - } - - fn compute_rolling_start_hours( - &mut self, - time: &time::Vecs, - starting_indexes: &Indexes, - exit: &Exit, - hours: usize, - get_field: F, - ) -> Result<()> - where - F: FnOnce(&mut Self) -> &mut EagerVec>, - { - self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| { - t.difference_in_hours_between(prev_ts) >= hours - }) - } - - fn compute_rolling_start_inner( - &mut self, - time: &time::Vecs, - starting_indexes: &Indexes, - exit: &Exit, - get_field: F, - expired: D, - ) -> Result<()> - where - F: FnOnce(&mut Self) -> &mut EagerVec>, - D: Fn(Timestamp, Timestamp) -> bool, - { - let field = get_field(self); - let resume_from = field.len().min(starting_indexes.height.to_usize()); - let mut prev = if resume_from > 0 { - field.collect_one_at(resume_from - 1).unwrap() - } else { - Height::ZERO - }; - let mut cursor = Cursor::new(&time.timestamp_monotonic); - cursor.advance(prev.to_usize()); - let mut prev_ts = cursor.next().unwrap(); - Ok(field.compute_transform( - starting_indexes.height, - &time.timestamp_monotonic, - |(h, t, ..)| { - while expired(t, prev_ts) { - prev.increment(); - prev_ts = cursor.next().unwrap(); - if prev > h { - unreachable!() - } - } - (h, prev) - }, - exit, - )?) - } } diff --git a/crates/brk_computer/src/blocks/count/import.rs b/crates/brk_computer/src/blocks/count/import.rs index 53fc9a31a..8bcca01ed 100644 --- a/crates/brk_computer/src/blocks/count/import.rs +++ b/crates/brk_computer/src/blocks/count/import.rs @@ -1,11 +1,11 @@ use brk_error::Result; use brk_types::Version; -use vecdb::{Database, ImportableVec}; +use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{BlockCountTarget, ComputedFromHeightCumulativeSum, ConstantVecs, RollingWindows}, + internal::{BlockCountTarget, ComputedFromHeightCumulativeSum, ConstantVecs}, }; impl Vecs { @@ -26,55 +26,6 @@ impl Vecs { 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)?, - height_8d_ago: ImportableVec::forced_import(db, "height_8d_ago", version)?, - height_9d_ago: ImportableVec::forced_import(db, "height_9d_ago", version)?, - height_12d_ago: ImportableVec::forced_import(db, "height_12d_ago", version)?, - height_13d_ago: ImportableVec::forced_import(db, "height_13d_ago", version)?, - height_2w_ago: ImportableVec::forced_import(db, "height_2w_ago", version)?, - height_21d_ago: ImportableVec::forced_import(db, "height_21d_ago", version)?, - height_26d_ago: ImportableVec::forced_import(db, "height_26d_ago", version)?, - height_1m_ago: ImportableVec::forced_import(db, "height_1m_ago", version)?, - height_34d_ago: ImportableVec::forced_import(db, "height_34d_ago", version)?, - height_55d_ago: ImportableVec::forced_import(db, "height_55d_ago", version)?, - height_2m_ago: ImportableVec::forced_import(db, "height_2m_ago", version)?, - height_9w_ago: ImportableVec::forced_import(db, "height_9w_ago", version)?, - height_12w_ago: ImportableVec::forced_import(db, "height_12w_ago", version)?, - height_89d_ago: ImportableVec::forced_import(db, "height_89d_ago", version)?, - height_3m_ago: ImportableVec::forced_import(db, "height_3m_ago", version)?, - height_14w_ago: ImportableVec::forced_import(db, "height_14w_ago", version)?, - height_111d_ago: ImportableVec::forced_import(db, "height_111d_ago", version)?, - height_144d_ago: ImportableVec::forced_import(db, "height_144d_ago", version)?, - height_6m_ago: ImportableVec::forced_import(db, "height_6m_ago", version)?, - height_26w_ago: ImportableVec::forced_import(db, "height_26w_ago", version)?, - height_200d_ago: ImportableVec::forced_import(db, "height_200d_ago", version)?, - height_9m_ago: ImportableVec::forced_import(db, "height_9m_ago", version)?, - height_350d_ago: ImportableVec::forced_import(db, "height_350d_ago", version)?, - height_12m_ago: ImportableVec::forced_import(db, "height_12m_ago", version)?, - height_1y_ago: ImportableVec::forced_import(db, "height_1y_ago", version)?, - height_14m_ago: ImportableVec::forced_import(db, "height_14m_ago", version)?, - height_2y_ago: ImportableVec::forced_import(db, "height_2y_ago", version)?, - height_26m_ago: ImportableVec::forced_import(db, "height_26m_ago", version)?, - height_3y_ago: ImportableVec::forced_import(db, "height_3y_ago", version)?, - height_200w_ago: ImportableVec::forced_import(db, "height_200w_ago", version)?, - height_4y_ago: ImportableVec::forced_import(db, "height_4y_ago", version)?, - height_5y_ago: ImportableVec::forced_import(db, "height_5y_ago", version)?, - height_6y_ago: ImportableVec::forced_import(db, "height_6y_ago", version)?, - height_8y_ago: ImportableVec::forced_import(db, "height_8y_ago", version)?, - height_9y_ago: ImportableVec::forced_import(db, "height_9y_ago", version)?, - height_10y_ago: ImportableVec::forced_import(db, "height_10y_ago", version)?, - height_12y_ago: ImportableVec::forced_import(db, "height_12y_ago", version)?, - height_14y_ago: ImportableVec::forced_import(db, "height_14y_ago", version)?, - height_26y_ago: ImportableVec::forced_import(db, "height_26y_ago", version)?, - block_count_sum: RollingWindows::forced_import( - db, - "block_count_sum", - version, - indexes, - )?, }) } } diff --git a/crates/brk_computer/src/blocks/count/vecs.rs b/crates/brk_computer/src/blocks/count/vecs.rs index 7bd8be3d1..4ee93a92d 100644 --- a/crates/brk_computer/src/blocks/count/vecs.rs +++ b/crates/brk_computer/src/blocks/count/vecs.rs @@ -1,117 +1,11 @@ use brk_traversable::Traversable; -use brk_types::{Height, StoredU32, StoredU64}; -use vecdb::{EagerVec, PcoVec, Rw, StorageMode}; +use brk_types::{StoredU32, StoredU64}; +use vecdb::{Rw, StorageMode}; -use crate::internal::{ - ComputedFromHeightCumulativeSum, ConstantVecs, RollingWindows, WindowStarts, -}; +use crate::internal::{ComputedFromHeightCumulativeSum, ConstantVecs}; #[derive(Traversable)] pub struct Vecs { pub block_count_target: ConstantVecs, pub block_count: ComputedFromHeightCumulativeSum, - pub block_count_sum: RollingWindows, - - pub height_1h_ago: M::Stored>>, - pub height_24h_ago: M::Stored>>, // 1d - pub height_3d_ago: M::Stored>>, - pub height_1w_ago: M::Stored>>, // 7d - pub height_8d_ago: M::Stored>>, - pub height_9d_ago: M::Stored>>, - pub height_12d_ago: M::Stored>>, - pub height_13d_ago: M::Stored>>, - pub height_2w_ago: M::Stored>>, // 14d - pub height_21d_ago: M::Stored>>, - pub height_26d_ago: M::Stored>>, - pub height_1m_ago: M::Stored>>, // 30d - pub height_34d_ago: M::Stored>>, - pub height_55d_ago: M::Stored>>, - pub height_2m_ago: M::Stored>>, // 60d - pub height_9w_ago: M::Stored>>, // 63d - pub height_12w_ago: M::Stored>>, // 84d - pub height_89d_ago: M::Stored>>, - pub height_3m_ago: M::Stored>>, // 90d - pub height_14w_ago: M::Stored>>, // 98d - pub height_111d_ago: M::Stored>>, - pub height_144d_ago: M::Stored>>, - pub height_6m_ago: M::Stored>>, // 180d - pub height_26w_ago: M::Stored>>, // 182d - pub height_200d_ago: M::Stored>>, - pub height_9m_ago: M::Stored>>, // 270d - pub height_350d_ago: M::Stored>>, - pub height_12m_ago: M::Stored>>, // 360d - pub height_1y_ago: M::Stored>>, // 365d - pub height_14m_ago: M::Stored>>, // 420d - pub height_2y_ago: M::Stored>>, // 730d - pub height_26m_ago: M::Stored>>, // 780d - pub height_3y_ago: M::Stored>>, // 1095d - pub height_200w_ago: M::Stored>>, // 1400d - pub height_4y_ago: M::Stored>>, // 1460d - pub height_5y_ago: M::Stored>>, // 1825d - pub height_6y_ago: M::Stored>>, // 2190d - pub height_8y_ago: M::Stored>>, // 2920d - pub height_9y_ago: M::Stored>>, // 3285d - pub height_10y_ago: M::Stored>>, // 3650d - pub height_12y_ago: M::Stored>>, // 4380d - pub height_14y_ago: M::Stored>>, // 5110d - pub height_26y_ago: M::Stored>>, // 9490d -} - -impl Vecs { - pub fn window_starts(&self) -> WindowStarts<'_> { - WindowStarts { - _24h: &self.height_24h_ago, - _1w: &self.height_1w_ago, - _1m: &self.height_1m_ago, - _1y: &self.height_1y_ago, - } - } - - pub fn start_vec(&self, days: usize) -> &EagerVec> { - match days { - 1 => &self.height_24h_ago, - 3 => &self.height_3d_ago, - 7 => &self.height_1w_ago, - 8 => &self.height_8d_ago, - 9 => &self.height_9d_ago, - 12 => &self.height_12d_ago, - 13 => &self.height_13d_ago, - 14 => &self.height_2w_ago, - 21 => &self.height_21d_ago, - 26 => &self.height_26d_ago, - 30 => &self.height_1m_ago, - 34 => &self.height_34d_ago, - 55 => &self.height_55d_ago, - 60 => &self.height_2m_ago, - 63 => &self.height_9w_ago, - 84 => &self.height_12w_ago, - 89 => &self.height_89d_ago, - 90 => &self.height_3m_ago, - 98 => &self.height_14w_ago, - 111 => &self.height_111d_ago, - 144 => &self.height_144d_ago, - 180 => &self.height_6m_ago, - 182 => &self.height_26w_ago, - 200 => &self.height_200d_ago, - 270 => &self.height_9m_ago, - 350 => &self.height_350d_ago, - 360 => &self.height_12m_ago, - 365 => &self.height_1y_ago, - 420 => &self.height_14m_ago, - 730 => &self.height_2y_ago, - 780 => &self.height_26m_ago, - 1095 => &self.height_3y_ago, - 1400 => &self.height_200w_ago, - 1460 => &self.height_4y_ago, - 1825 => &self.height_5y_ago, - 2190 => &self.height_6y_ago, - 2920 => &self.height_8y_ago, - 3285 => &self.height_9y_ago, - 3650 => &self.height_10y_ago, - 4380 => &self.height_12y_ago, - 5110 => &self.height_14y_ago, - 9490 => &self.height_26y_ago, - _ => panic!("No start vec for {days} days"), - } - } } diff --git a/crates/brk_computer/src/blocks/import.rs b/crates/brk_computer/src/blocks/import.rs index 9830c2994..3d8989fb6 100644 --- a/crates/brk_computer/src/blocks/import.rs +++ b/crates/brk_computer/src/blocks/import.rs @@ -10,7 +10,8 @@ use crate::{ }; use super::{ - CountVecs, DifficultyVecs, HalvingVecs, IntervalVecs, SizeVecs, TimeVecs, Vecs, WeightVecs, + CountVecs, DifficultyVecs, HalvingVecs, IntervalVecs, LookbackVecs, SizeVecs, TimeVecs, Vecs, + WeightVecs, }; impl Vecs { @@ -24,6 +25,7 @@ impl Vecs { let version = parent_version; let count = CountVecs::forced_import(&db, version, indexes)?; + let lookback = LookbackVecs::forced_import(&db, version)?; let interval = IntervalVecs::forced_import(&db, version, indexes)?; let size = SizeVecs::forced_import(&db, version, indexes)?; let weight = WeightVecs::forced_import(&db, version, indexes)?; @@ -34,6 +36,7 @@ impl Vecs { let this = Self { db, count, + lookback, interval, size, weight, diff --git a/crates/brk_computer/src/blocks/interval/compute.rs b/crates/brk_computer/src/blocks/interval/compute.rs index 9d0e04b18..2dcb6f1ab 100644 --- a/crates/brk_computer/src/blocks/interval/compute.rs +++ b/crates/brk_computer/src/blocks/interval/compute.rs @@ -10,12 +10,12 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { let mut prev_timestamp = None; - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); self.0 .compute(starting_indexes.height, &window_starts, exit, |vec| { vec.compute_transform( diff --git a/crates/brk_computer/src/blocks/lookback.rs b/crates/brk_computer/src/blocks/lookback.rs new file mode 100644 index 000000000..b175b2c16 --- /dev/null +++ b/crates/brk_computer/src/blocks/lookback.rs @@ -0,0 +1,306 @@ +use brk_error::Result; +use brk_traversable::Traversable; +use brk_types::{Height, Indexes, Timestamp, Version}; +use vecdb::{AnyVec, Cursor, Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableVec, Rw, StorageMode, VecIndex}; + +use crate::internal::WindowStarts; + +use super::time; + +#[derive(Traversable)] +pub struct Vecs { + pub height_1h_ago: M::Stored>>, + pub height_24h_ago: M::Stored>>, // 1d + pub height_3d_ago: M::Stored>>, + pub height_1w_ago: M::Stored>>, // 7d + pub height_8d_ago: M::Stored>>, + pub height_9d_ago: M::Stored>>, + pub height_12d_ago: M::Stored>>, + pub height_13d_ago: M::Stored>>, + pub height_2w_ago: M::Stored>>, // 14d + pub height_21d_ago: M::Stored>>, + pub height_26d_ago: M::Stored>>, + pub height_1m_ago: M::Stored>>, // 30d + pub height_34d_ago: M::Stored>>, + pub height_55d_ago: M::Stored>>, + pub height_2m_ago: M::Stored>>, // 60d + pub height_9w_ago: M::Stored>>, // 63d + pub height_12w_ago: M::Stored>>, // 84d + pub height_89d_ago: M::Stored>>, + pub height_3m_ago: M::Stored>>, // 90d + pub height_14w_ago: M::Stored>>, // 98d + pub height_111d_ago: M::Stored>>, + pub height_144d_ago: M::Stored>>, + pub height_6m_ago: M::Stored>>, // 180d + pub height_26w_ago: M::Stored>>, // 182d + pub height_200d_ago: M::Stored>>, + pub height_9m_ago: M::Stored>>, // 270d + pub height_350d_ago: M::Stored>>, + pub height_12m_ago: M::Stored>>, // 360d + pub height_1y_ago: M::Stored>>, // 365d + pub height_14m_ago: M::Stored>>, // 420d + pub height_2y_ago: M::Stored>>, // 730d + pub height_26m_ago: M::Stored>>, // 780d + pub height_3y_ago: M::Stored>>, // 1095d + pub height_200w_ago: M::Stored>>, // 1400d + pub height_4y_ago: M::Stored>>, // 1460d + pub height_5y_ago: M::Stored>>, // 1825d + pub height_6y_ago: M::Stored>>, // 2190d + pub height_8y_ago: M::Stored>>, // 2920d + pub height_9y_ago: M::Stored>>, // 3285d + pub height_10y_ago: M::Stored>>, // 3650d + pub height_12y_ago: M::Stored>>, // 4380d + pub height_14y_ago: M::Stored>>, // 5110d + pub height_26y_ago: M::Stored>>, // 9490d +} + +impl Vecs { + pub(crate) fn forced_import(db: &Database, version: Version) -> Result { + Ok(Self { + 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)?, + height_8d_ago: ImportableVec::forced_import(db, "height_8d_ago", version)?, + height_9d_ago: ImportableVec::forced_import(db, "height_9d_ago", version)?, + height_12d_ago: ImportableVec::forced_import(db, "height_12d_ago", version)?, + height_13d_ago: ImportableVec::forced_import(db, "height_13d_ago", version)?, + height_2w_ago: ImportableVec::forced_import(db, "height_2w_ago", version)?, + height_21d_ago: ImportableVec::forced_import(db, "height_21d_ago", version)?, + height_26d_ago: ImportableVec::forced_import(db, "height_26d_ago", version)?, + height_1m_ago: ImportableVec::forced_import(db, "height_1m_ago", version)?, + height_34d_ago: ImportableVec::forced_import(db, "height_34d_ago", version)?, + height_55d_ago: ImportableVec::forced_import(db, "height_55d_ago", version)?, + height_2m_ago: ImportableVec::forced_import(db, "height_2m_ago", version)?, + height_9w_ago: ImportableVec::forced_import(db, "height_9w_ago", version)?, + height_12w_ago: ImportableVec::forced_import(db, "height_12w_ago", version)?, + height_89d_ago: ImportableVec::forced_import(db, "height_89d_ago", version)?, + height_3m_ago: ImportableVec::forced_import(db, "height_3m_ago", version)?, + height_14w_ago: ImportableVec::forced_import(db, "height_14w_ago", version)?, + height_111d_ago: ImportableVec::forced_import(db, "height_111d_ago", version)?, + height_144d_ago: ImportableVec::forced_import(db, "height_144d_ago", version)?, + height_6m_ago: ImportableVec::forced_import(db, "height_6m_ago", version)?, + height_26w_ago: ImportableVec::forced_import(db, "height_26w_ago", version)?, + height_200d_ago: ImportableVec::forced_import(db, "height_200d_ago", version)?, + height_9m_ago: ImportableVec::forced_import(db, "height_9m_ago", version)?, + height_350d_ago: ImportableVec::forced_import(db, "height_350d_ago", version)?, + height_12m_ago: ImportableVec::forced_import(db, "height_12m_ago", version)?, + height_1y_ago: ImportableVec::forced_import(db, "height_1y_ago", version)?, + height_14m_ago: ImportableVec::forced_import(db, "height_14m_ago", version)?, + height_2y_ago: ImportableVec::forced_import(db, "height_2y_ago", version)?, + height_26m_ago: ImportableVec::forced_import(db, "height_26m_ago", version)?, + height_3y_ago: ImportableVec::forced_import(db, "height_3y_ago", version)?, + height_200w_ago: ImportableVec::forced_import(db, "height_200w_ago", version)?, + height_4y_ago: ImportableVec::forced_import(db, "height_4y_ago", version)?, + height_5y_ago: ImportableVec::forced_import(db, "height_5y_ago", version)?, + height_6y_ago: ImportableVec::forced_import(db, "height_6y_ago", version)?, + height_8y_ago: ImportableVec::forced_import(db, "height_8y_ago", version)?, + height_9y_ago: ImportableVec::forced_import(db, "height_9y_ago", version)?, + height_10y_ago: ImportableVec::forced_import(db, "height_10y_ago", version)?, + height_12y_ago: ImportableVec::forced_import(db, "height_12y_ago", version)?, + height_14y_ago: ImportableVec::forced_import(db, "height_14y_ago", version)?, + height_26y_ago: ImportableVec::forced_import(db, "height_26y_ago", version)?, + }) + } + + pub fn window_starts(&self) -> WindowStarts<'_> { + WindowStarts { + _24h: &self.height_24h_ago, + _1w: &self.height_1w_ago, + _1m: &self.height_1m_ago, + _1y: &self.height_1y_ago, + } + } + + pub fn start_vec(&self, days: usize) -> &EagerVec> { + match days { + 1 => &self.height_24h_ago, + 3 => &self.height_3d_ago, + 7 => &self.height_1w_ago, + 8 => &self.height_8d_ago, + 9 => &self.height_9d_ago, + 12 => &self.height_12d_ago, + 13 => &self.height_13d_ago, + 14 => &self.height_2w_ago, + 21 => &self.height_21d_ago, + 26 => &self.height_26d_ago, + 30 => &self.height_1m_ago, + 34 => &self.height_34d_ago, + 55 => &self.height_55d_ago, + 60 => &self.height_2m_ago, + 63 => &self.height_9w_ago, + 84 => &self.height_12w_ago, + 89 => &self.height_89d_ago, + 90 => &self.height_3m_ago, + 98 => &self.height_14w_ago, + 111 => &self.height_111d_ago, + 144 => &self.height_144d_ago, + 180 => &self.height_6m_ago, + 182 => &self.height_26w_ago, + 200 => &self.height_200d_ago, + 270 => &self.height_9m_ago, + 350 => &self.height_350d_ago, + 360 => &self.height_12m_ago, + 365 => &self.height_1y_ago, + 420 => &self.height_14m_ago, + 730 => &self.height_2y_ago, + 780 => &self.height_26m_ago, + 1095 => &self.height_3y_ago, + 1400 => &self.height_200w_ago, + 1460 => &self.height_4y_ago, + 1825 => &self.height_5y_ago, + 2190 => &self.height_6y_ago, + 2920 => &self.height_8y_ago, + 3285 => &self.height_9y_ago, + 3650 => &self.height_10y_ago, + 4380 => &self.height_12y_ago, + 5110 => &self.height_14y_ago, + 9490 => &self.height_26y_ago, + _ => panic!("No start vec for {days} days"), + } + } + + pub(crate) fn compute( + &mut self, + time: &time::Vecs, + starting_indexes: &Indexes, + exit: &Exit, + ) -> Result<()> { + self.compute_rolling_start_hours(time, starting_indexes, exit, 1, |s| { + &mut s.height_1h_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 1, |s| &mut s.height_24h_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 3, |s| &mut s.height_3d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 7, |s| &mut s.height_1w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 8, |s| &mut s.height_8d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 9, |s| &mut s.height_9d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 12, |s| &mut s.height_12d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 13, |s| &mut s.height_13d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 14, |s| &mut s.height_2w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 21, |s| &mut s.height_21d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 26, |s| &mut s.height_26d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 30, |s| &mut s.height_1m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 34, |s| &mut s.height_34d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 55, |s| &mut s.height_55d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 60, |s| &mut s.height_2m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 63, |s| &mut s.height_9w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 84, |s| &mut s.height_12w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 89, |s| &mut s.height_89d_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 90, |s| &mut s.height_3m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 98, |s| &mut s.height_14w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 111, |s| { + &mut s.height_111d_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 144, |s| { + &mut s.height_144d_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 180, |s| &mut s.height_6m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 182, |s| &mut s.height_26w_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 200, |s| { + &mut s.height_200d_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 270, |s| &mut s.height_9m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 350, |s| { + &mut s.height_350d_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 360, |s| &mut s.height_12m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 365, |s| &mut s.height_1y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 420, |s| &mut s.height_14m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 730, |s| &mut s.height_2y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 780, |s| &mut s.height_26m_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 1095, |s| &mut s.height_3y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 1400, |s| { + &mut s.height_200w_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 1460, |s| &mut s.height_4y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 1825, |s| &mut s.height_5y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 2190, |s| &mut s.height_6y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 2920, |s| &mut s.height_8y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 3285, |s| &mut s.height_9y_ago)?; + self.compute_rolling_start(time, starting_indexes, exit, 3650, |s| { + &mut s.height_10y_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 4380, |s| { + &mut s.height_12y_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 5110, |s| { + &mut s.height_14y_ago + })?; + self.compute_rolling_start(time, starting_indexes, exit, 9490, |s| { + &mut s.height_26y_ago + })?; + + Ok(()) + } + + fn compute_rolling_start( + &mut self, + time: &time::Vecs, + starting_indexes: &Indexes, + exit: &Exit, + days: usize, + get_field: F, + ) -> Result<()> + where + F: FnOnce(&mut Self) -> &mut EagerVec>, + { + self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| { + t.difference_in_days_between(prev_ts) >= days + }) + } + + fn compute_rolling_start_hours( + &mut self, + time: &time::Vecs, + starting_indexes: &Indexes, + exit: &Exit, + hours: usize, + get_field: F, + ) -> Result<()> + where + F: FnOnce(&mut Self) -> &mut EagerVec>, + { + self.compute_rolling_start_inner(time, starting_indexes, exit, get_field, |t, prev_ts| { + t.difference_in_hours_between(prev_ts) >= hours + }) + } + + fn compute_rolling_start_inner( + &mut self, + time: &time::Vecs, + starting_indexes: &Indexes, + exit: &Exit, + get_field: F, + expired: D, + ) -> Result<()> + where + F: FnOnce(&mut Self) -> &mut EagerVec>, + D: Fn(Timestamp, Timestamp) -> bool, + { + let field = get_field(self); + let resume_from = field.len().min(starting_indexes.height.to_usize()); + let mut prev = if resume_from > 0 { + field.collect_one_at(resume_from - 1).unwrap() + } else { + Height::ZERO + }; + let mut cursor = Cursor::new(&time.timestamp_monotonic); + cursor.advance(prev.to_usize()); + let mut prev_ts = cursor.next().unwrap(); + Ok(field.compute_transform( + starting_indexes.height, + &time.timestamp_monotonic, + |(h, t, ..)| { + while expired(t, prev_ts) { + prev.increment(); + prev_ts = cursor.next().unwrap(); + if prev > h { + unreachable!() + } + } + (h, prev) + }, + exit, + )?) + } +} diff --git a/crates/brk_computer/src/blocks/mod.rs b/crates/brk_computer/src/blocks/mod.rs index e82a7564e..1d32ed51e 100644 --- a/crates/brk_computer/src/blocks/mod.rs +++ b/crates/brk_computer/src/blocks/mod.rs @@ -2,6 +2,7 @@ pub mod count; pub mod difficulty; pub mod halving; pub mod interval; +pub mod lookback; pub mod size; pub mod time; pub mod weight; @@ -16,6 +17,7 @@ pub use count::Vecs as CountVecs; pub use difficulty::Vecs as DifficultyVecs; pub use halving::Vecs as HalvingVecs; pub use interval::Vecs as IntervalVecs; +pub use lookback::Vecs as LookbackVecs; pub use size::Vecs as SizeVecs; pub use time::Vecs as TimeVecs; pub use weight::Vecs as WeightVecs; @@ -46,6 +48,7 @@ pub struct Vecs { pub(crate) db: Database, pub count: CountVecs, + pub lookback: LookbackVecs, pub interval: IntervalVecs, #[traversable(flatten)] pub size: SizeVecs, diff --git a/crates/brk_computer/src/blocks/size/compute.rs b/crates/brk_computer/src/blocks/size/compute.rs index 46b9d72ad..b50dcde4f 100644 --- a/crates/brk_computer/src/blocks/size/compute.rs +++ b/crates/brk_computer/src/blocks/size/compute.rs @@ -10,11 +10,11 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); // vbytes = floor(weight / 4), stored at height level self.vbytes diff --git a/crates/brk_computer/src/blocks/weight/compute.rs b/crates/brk_computer/src/blocks/weight/compute.rs index 560198635..2741a57e0 100644 --- a/crates/brk_computer/src/blocks/weight/compute.rs +++ b/crates/brk_computer/src/blocks/weight/compute.rs @@ -10,11 +10,11 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); self.weight.compute( starting_indexes.height, diff --git a/crates/brk_computer/src/cointime/activity/compute.rs b/crates/brk_computer/src/cointime/activity/compute.rs index 171bb49e9..dad3e566e 100644 --- a/crates/brk_computer/src/cointime/activity/compute.rs +++ b/crates/brk_computer/src/cointime/activity/compute.rs @@ -13,7 +13,7 @@ impl Vecs { distribution: &distribution::Vecs, exit: &Exit, ) -> Result<()> { - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); let all_metrics = &distribution.utxo_cohorts.all.metrics; let circulating_supply = &all_metrics.supply.total.sats.height; diff --git a/crates/brk_computer/src/cointime/reserve_risk/compute.rs b/crates/brk_computer/src/cointime/reserve_risk/compute.rs index e469c6703..334304c11 100644 --- a/crates/brk_computer/src/cointime/reserve_risk/compute.rs +++ b/crates/brk_computer/src/cointime/reserve_risk/compute.rs @@ -16,7 +16,7 @@ impl Vecs { ) -> Result<()> { self.vocdd_median_1y.compute_rolling_median_from_starts( starting_indexes.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, &value.vocdd.height, exit, )?; diff --git a/crates/brk_computer/src/cointime/supply/import.rs b/crates/brk_computer/src/cointime/supply/import.rs index bfa7bb2ff..f7900a2c2 100644 --- a/crates/brk_computer/src/cointime/supply/import.rs +++ b/crates/brk_computer/src/cointime/supply/import.rs @@ -3,7 +3,7 @@ use brk_types::Version; use vecdb::Database; use super::Vecs; -use crate::{indexes, internal::ValueFromHeight}; +use crate::{indexes, internal::AmountFromHeight}; impl Vecs { pub(crate) fn forced_import( @@ -12,8 +12,13 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - vaulted_supply: ValueFromHeight::forced_import(db, "vaulted_supply", version, indexes)?, - active_supply: ValueFromHeight::forced_import(db, "active_supply", version, indexes)?, + vaulted_supply: AmountFromHeight::forced_import( + db, + "vaulted_supply", + version, + indexes, + )?, + active_supply: AmountFromHeight::forced_import(db, "active_supply", version, indexes)?, }) } } diff --git a/crates/brk_computer/src/cointime/supply/vecs.rs b/crates/brk_computer/src/cointime/supply/vecs.rs index 2e6d37b7d..4ae433878 100644 --- a/crates/brk_computer/src/cointime/supply/vecs.rs +++ b/crates/brk_computer/src/cointime/supply/vecs.rs @@ -1,10 +1,10 @@ use brk_traversable::Traversable; use vecdb::{Rw, StorageMode}; -use crate::internal::ValueFromHeight; +use crate::internal::AmountFromHeight; #[derive(Traversable)] pub struct Vecs { - pub vaulted_supply: ValueFromHeight, - pub active_supply: ValueFromHeight, + pub vaulted_supply: AmountFromHeight, + pub active_supply: AmountFromHeight, } diff --git a/crates/brk_computer/src/cointime/value/compute.rs b/crates/brk_computer/src/cointime/value/compute.rs index 783aea688..43c079291 100644 --- a/crates/brk_computer/src/cointime/value/compute.rs +++ b/crates/brk_computer/src/cointime/value/compute.rs @@ -16,7 +16,7 @@ impl Vecs { activity: &activity::Vecs, exit: &Exit, ) -> Result<()> { - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); let all_metrics = &distribution.utxo_cohorts.all.metrics; let coinblocks_destroyed = &all_metrics.activity.coinblocks_destroyed; diff --git a/crates/brk_computer/src/distribution/cohorts/address/groups.rs b/crates/brk_computer/src/distribution/cohorts/address/groups.rs index 85199a01a..7d88d8fa6 100644 --- a/crates/brk_computer/src/distribution/cohorts/address/groups.rs +++ b/crates/brk_computer/src/distribution/cohorts/address/groups.rs @@ -98,7 +98,7 @@ impl AddressCohorts { self.par_iter_mut().try_for_each(|v| { v.addr_count_delta.compute( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, &v.addr_count.height, exit, ) diff --git a/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs b/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs index 6a2f711cb..312cb833a 100644 --- a/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs +++ b/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs @@ -8,7 +8,9 @@ use brk_error::Result; use brk_traversable::Traversable; use brk_types::{Dollars, Height, Indexes, Sats, Version}; use rayon::prelude::*; -use vecdb::{AnyStoredVec, Database, Exit, ReadOnlyClone, ReadableVec, Rw, StorageMode, WritableVec}; +use vecdb::{ + AnyStoredVec, Database, Exit, ReadOnlyClone, ReadableVec, Rw, StorageMode, WritableVec, +}; use crate::{ blocks, @@ -22,7 +24,7 @@ use crate::{ state::UTXOCohortState, }, indexes, - internal::ValueFromHeight, + internal::AmountFromHeight, prices, }; @@ -47,7 +49,7 @@ pub struct UTXOCohorts { #[traversable(rename = "type")] pub type_: BySpendableType>>, pub profitability: ProfitabilityMetrics, - pub matured: ByAgeRange>, + pub matured: ByAgeRange>, #[traversable(skip)] pub(super) fenwick: CostBasisFenwick, /// Cached partition_point positions for tick_tock boundary searches. @@ -226,8 +228,10 @@ impl UTXOCohorts { let lt_amount = ByLowerThanAmount::try_new(&minimal_no_state)?; let ge_amount = ByGreatEqualAmount::try_new(&minimal_no_state)?; - let matured = ByAgeRange::try_new(&|_f: Filter, name: &'static str| -> Result { - ValueFromHeight::forced_import(db, &format!("utxo_{name}_matured"), v, indexes) + let matured = ByAgeRange::try_new(&|_f: Filter, + name: &'static str| + -> Result { + AmountFromHeight::forced_import(db, &format!("utxo_{name}_matured"), v, indexes) })?; Ok(Self { @@ -257,7 +261,10 @@ impl UTXOCohorts { return; } let Self { - sth, fenwick, age_range, .. + sth, + fenwick, + age_range, + .. } = self; fenwick.compute_is_sth(&sth.metrics.filter, age_range.iter().map(|v| v.filter())); @@ -334,9 +341,7 @@ impl UTXOCohorts { /// Sequential mutable iterator over all separate (stateful) cohorts. /// Use instead of `par_iter_separate_mut` when per-item work is trivial. - pub(crate) fn iter_separate_mut( - &mut self, - ) -> impl Iterator { + pub(crate) fn iter_separate_mut(&mut self) -> impl Iterator { let Self { age_range, epoch, @@ -575,35 +580,20 @@ impl UTXOCohorts { }), Box::new(|| { age_range.par_iter_mut().try_for_each(|v| { - v.metrics.compute_rest_part2( - blocks, - prices, - starting_indexes, - ss, - exit, - ) + v.metrics + .compute_rest_part2(blocks, prices, starting_indexes, ss, exit) }) }), Box::new(|| { max_age.par_iter_mut().try_for_each(|v| { - v.metrics.compute_rest_part2( - blocks, - prices, - starting_indexes, - ss, - exit, - ) + v.metrics + .compute_rest_part2(blocks, prices, starting_indexes, ss, exit) }) }), Box::new(|| { min_age.par_iter_mut().try_for_each(|v| { - v.metrics.compute_rest_part2( - blocks, - prices, - starting_indexes, - ss, - exit, - ) + v.metrics + .compute_rest_part2(blocks, prices, starting_indexes, ss, exit) }) }), Box::new(|| { @@ -613,24 +603,14 @@ impl UTXOCohorts { }), Box::new(|| { epoch.par_iter_mut().try_for_each(|v| { - v.metrics.compute_rest_part2( - blocks, - prices, - starting_indexes, - ss, - exit, - ) + v.metrics + .compute_rest_part2(blocks, prices, starting_indexes, ss, exit) }) }), Box::new(|| { class.par_iter_mut().try_for_each(|v| { - v.metrics.compute_rest_part2( - blocks, - prices, - starting_indexes, - ss, - exit, - ) + v.metrics + .compute_rest_part2(blocks, prices, starting_indexes, ss, exit) }) }), Box::new(|| { @@ -695,9 +675,8 @@ impl UTXOCohorts { } vecs.extend(self.profitability.collect_all_vecs_mut()); for v in self.matured.iter_mut() { - let base = &mut v.base; - vecs.push(&mut base.sats.height); - vecs.push(&mut base.cents.height); + vecs.push(&mut v.sats.height); + vecs.push(&mut v.cents.height); } vecs.into_par_iter() } @@ -712,13 +691,23 @@ impl UTXOCohorts { pub(crate) fn min_separate_stateful_height_len(&self) -> Height { self.iter_separate() .map(|v| Height::from(v.min_stateful_height_len())) - .chain(self.matured.iter().map(|v| Height::from(v.min_stateful_len()))) + .chain( + self.matured + .iter() + .map(|v| Height::from(v.min_stateful_len())), + ) .min() .unwrap_or_default() .min(Height::from(self.profitability.min_stateful_height_len())) - .min(Height::from(self.all.metrics.realized.min_stateful_height_len())) - .min(Height::from(self.sth.metrics.realized.min_stateful_height_len())) - .min(Height::from(self.lth.metrics.realized.min_stateful_height_len())) + .min(Height::from( + self.all.metrics.realized.min_stateful_height_len(), + )) + .min(Height::from( + self.sth.metrics.realized.min_stateful_height_len(), + )) + .min(Height::from( + self.lth.metrics.realized.min_stateful_height_len(), + )) } /// Import state for all separate cohorts at or before given height. @@ -770,7 +759,11 @@ impl UTXOCohorts { /// Called during the block loop after separate cohorts' truncate_push but before reset. pub(crate) fn push_overlapping_realized_full(&mut self, height: Height) -> Result<()> { let Self { - all, sth, lth, age_range, .. + all, + sth, + lth, + age_range, + .. } = self; let sth_filter = &sth.metrics.filter; diff --git a/crates/brk_computer/src/distribution/metrics/activity/core.rs b/crates/brk_computer/src/distribution/metrics/activity/core.rs index 3550f277c..fafa6ac0a 100644 --- a/crates/brk_computer/src/distribution/metrics/activity/core.rs +++ b/crates/brk_computer/src/distribution/metrics/activity/core.rs @@ -64,7 +64,7 @@ impl ActivityCore { ) -> Result<()> { self.sent_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.sent.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/activity/full.rs b/crates/brk_computer/src/distribution/metrics/activity/full.rs index 3e533ceb3..eaca5f4a2 100644 --- a/crates/brk_computer/src/distribution/metrics/activity/full.rs +++ b/crates/brk_computer/src/distribution/metrics/activity/full.rs @@ -21,6 +21,7 @@ pub struct ActivityFull { pub coindays_destroyed_cumulative: ComputedFromHeight, pub coindays_destroyed_sum: RollingWindows, + #[traversable(rename = "sent_sum")] pub sent_sum_extended: RollingWindowsFrom1w, } @@ -72,7 +73,7 @@ impl ActivityFull { exit, )?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.coindays_destroyed_sum.compute_rolling_sum( starting_indexes.height, &window_starts, diff --git a/crates/brk_computer/src/distribution/metrics/cohort/all.rs b/crates/brk_computer/src/distribution/metrics/cohort/all.rs index a0eb55ec9..784a3d99d 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/all.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/all.rs @@ -35,7 +35,9 @@ pub struct AllCohortMetrics { pub dormancy: ComputedFromHeight, pub velocity: ComputedFromHeight, + #[traversable(wrap = "supply", rename = "delta")] pub supply_delta_extended: RollingDeltaExcept1m, + #[traversable(wrap = "outputs", rename = "utxo_count_delta")] pub utxo_count_delta_extended: RollingDeltaExcept1m, } @@ -147,7 +149,7 @@ impl AllCohortMetrics { exit, )?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.supply_delta_extended.compute( starting_indexes.height, &window_starts, diff --git a/crates/brk_computer/src/distribution/metrics/cohort/extended.rs b/crates/brk_computer/src/distribution/metrics/cohort/extended.rs index 507371e70..a0253f9e6 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/extended.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/extended.rs @@ -32,7 +32,9 @@ pub struct ExtendedCohortMetrics { pub dormancy: ComputedFromHeight, pub velocity: ComputedFromHeight, + #[traversable(wrap = "supply", rename = "delta")] pub supply_delta_extended: RollingDeltaExcept1m, + #[traversable(wrap = "outputs", rename = "utxo_count_delta")] pub utxo_count_delta_extended: RollingDeltaExcept1m, } @@ -126,7 +128,7 @@ impl ExtendedCohortMetrics { exit, )?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.supply_delta_extended.compute( starting_indexes.height, &window_starts, diff --git a/crates/brk_computer/src/distribution/metrics/config.rs b/crates/brk_computer/src/distribution/metrics/config.rs index 0649f676d..a889aeb25 100644 --- a/crates/brk_computer/src/distribution/metrics/config.rs +++ b/crates/brk_computer/src/distribution/metrics/config.rs @@ -1,19 +1,17 @@ use brk_cohort::Filter; use brk_error::Result; -use brk_types::{ - BasisPoints16, BasisPoints32, BasisPointsSigned32, Cents, Height, Version, -}; +use brk_types::{BasisPoints16, BasisPoints32, BasisPointsSigned32, Cents, Height, Version}; use schemars::JsonSchema; use vecdb::{BytesVec, BytesVecValue, Database, ImportableVec}; use crate::{ indexes, internal::{ - CentsType, ComputedFromHeight, ComputedFromHeightCumulative, - ComputedFromHeightCumulativeSum, ComputedFromHeightRatio, FiatFromHeight, - FiatRollingDelta1m, FiatRollingDeltaExcept1m, NumericValue, PercentFromHeight, - PercentRollingWindows, Price, RollingDelta1m, RollingDeltaExcept1m, RollingWindow24h, - RollingWindows, RollingWindowsFrom1w, ValueFromHeight, ValueFromHeightCumulative, + AmountFromHeight, AmountFromHeightCumulative, CentsType, ComputedFromHeight, + ComputedFromHeightCumulative, ComputedFromHeightCumulativeSum, ComputedFromHeightRatio, + FiatFromHeight, FiatRollingDelta1m, FiatRollingDeltaExcept1m, NumericValue, + PercentFromHeight, PercentRollingWindows, Price, RollingDelta1m, RollingDeltaExcept1m, + RollingWindow24h, RollingWindows, RollingWindowsFrom1w, }, }; @@ -37,8 +35,8 @@ macro_rules! impl_config_import { // Non-generic types impl_config_import!( - ValueFromHeight, - ValueFromHeightCumulative, + AmountFromHeight, + AmountFromHeightCumulative, ComputedFromHeightRatio, PercentFromHeight, PercentFromHeight, @@ -83,7 +81,8 @@ impl ConfigImport for FiatFromHeight { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } -impl ConfigImport for RollingDelta1m +impl ConfigImport + for RollingDelta1m { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) @@ -136,11 +135,7 @@ impl<'a> ImportConfig<'a> { } } - pub(crate) fn import( - &self, - suffix: &str, - offset: Version, - ) -> Result { + pub(crate) fn import(&self, suffix: &str, offset: Version) -> Result { T::config_import(self, suffix, offset) } } diff --git a/crates/brk_computer/src/distribution/metrics/outputs.rs b/crates/brk_computer/src/distribution/metrics/outputs.rs index 4d2280650..7894ea7d5 100644 --- a/crates/brk_computer/src/distribution/metrics/outputs.rs +++ b/crates/brk_computer/src/distribution/metrics/outputs.rs @@ -67,7 +67,7 @@ impl OutputsMetrics { ) -> Result<()> { self.utxo_count_delta.compute( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, &self.utxo_count.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs index 8791f17a7..62c406a4e 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs @@ -12,23 +12,21 @@ use crate::distribution::metrics::ImportConfig; #[derive(Traversable)] pub struct RealizedAdjusted { - pub adjusted_value_created: ComputedFromHeight, - pub adjusted_value_destroyed: ComputedFromHeight, - - pub adjusted_value_created_sum: RollingWindows, - pub adjusted_value_destroyed_sum: RollingWindows, - - pub adjusted_sopr: RollingWindows, + pub value_created: ComputedFromHeight, + pub value_destroyed: ComputedFromHeight, + pub value_created_sum: RollingWindows, + pub value_destroyed_sum: RollingWindows, + pub sopr: RollingWindows, } impl RealizedAdjusted { pub(crate) fn forced_import(cfg: &ImportConfig) -> Result { Ok(RealizedAdjusted { - adjusted_value_created: cfg.import("adjusted_value_created", Version::ZERO)?, - adjusted_value_destroyed: cfg.import("adjusted_value_destroyed", Version::ZERO)?, - adjusted_value_created_sum: cfg.import("adjusted_value_created", Version::ONE)?, - adjusted_value_destroyed_sum: cfg.import("adjusted_value_destroyed", Version::ONE)?, - adjusted_sopr: cfg.import("adjusted_sopr", Version::ONE)?, + value_created: cfg.import("adjusted_value_created", Version::ZERO)?, + value_destroyed: cfg.import("adjusted_value_destroyed", Version::ZERO)?, + value_created_sum: cfg.import("adjusted_value_created", Version::ONE)?, + value_destroyed_sum: cfg.import("adjusted_value_destroyed", Version::ONE)?, + sopr: cfg.import("adjusted_sopr", Version::ONE)?, }) } @@ -43,14 +41,14 @@ impl RealizedAdjusted { up_to_1h_value_destroyed: &impl ReadableVec, exit: &Exit, ) -> Result<()> { - // Compute adjusted_value_created = base.value_created - up_to_1h.value_created - self.adjusted_value_created.height.compute_subtract( + // Compute value_created = base.value_created - up_to_1h.value_created + self.value_created.height.compute_subtract( starting_indexes.height, base_value_created, up_to_1h_value_created, exit, )?; - self.adjusted_value_destroyed.height.compute_subtract( + self.value_destroyed.height.compute_subtract( starting_indexes.height, base_value_destroyed, up_to_1h_value_destroyed, @@ -58,27 +56,27 @@ impl RealizedAdjusted { )?; // Adjusted value created/destroyed rolling sums - let window_starts = blocks.count.window_starts(); - self.adjusted_value_created_sum.compute_rolling_sum( + let window_starts = blocks.lookback.window_starts(); + self.value_created_sum.compute_rolling_sum( starting_indexes.height, &window_starts, - &self.adjusted_value_created.height, + &self.value_created.height, exit, )?; - self.adjusted_value_destroyed_sum.compute_rolling_sum( + self.value_destroyed_sum.compute_rolling_sum( starting_indexes.height, &window_starts, - &self.adjusted_value_destroyed.height, + &self.value_destroyed.height, exit, )?; // SOPR ratios from rolling sums for ((sopr, vc), vd) in self - .adjusted_sopr + .sopr .as_mut_array() .into_iter() - .zip(self.adjusted_value_created_sum.as_array()) - .zip(self.adjusted_value_destroyed_sum.as_array()) + .zip(self.value_created_sum.as_array()) + .zip(self.value_destroyed_sum.as_array()) { sopr.compute_binary::( starting_indexes.height, diff --git a/crates/brk_computer/src/distribution/metrics/realized/base.rs b/crates/brk_computer/src/distribution/metrics/realized/base.rs index ed6013a90..014bed5b4 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/base.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/base.rs @@ -91,13 +91,13 @@ impl RealizedBase { self.core.compute_rest_part1(blocks, starting_indexes, exit)?; self.sent_in_profit_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.sent_in_profit.height, exit, )?; self.sent_in_loss_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.sent_in_loss.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/core.rs b/crates/brk_computer/src/distribution/metrics/realized/core.rs index ccfee9217..69e30ece9 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/core.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/core.rs @@ -155,27 +155,27 @@ impl RealizedCore { self.realized_cap_delta.compute( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, &self.minimal.realized_cap_cents.height, exit, )?; self.net_realized_pnl_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.net_realized_pnl.height, exit, )?; self.value_created_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.value_created.height, exit, )?; self.value_destroyed_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.value_destroyed.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/full.rs b/crates/brk_computer/src/distribution/metrics/realized/full.rs index 616f5c93c..4f03c2c5e 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/full.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/full.rs @@ -57,13 +57,16 @@ pub struct RealizedFull { pub gross_pnl_sum: RollingWindows, pub net_realized_pnl_cumulative: ComputedFromHeight, + #[traversable(rename = "net_realized_pnl_sum")] pub net_realized_pnl_sum_extended: RollingWindowsFrom1w, pub net_pnl_delta: FiatRollingDelta1m, + #[traversable(rename = "net_pnl_delta")] pub net_pnl_delta_extended: FiatRollingDeltaExcept1m, pub net_pnl_change_1m_rel_to_realized_cap: PercentFromHeight, pub net_pnl_change_1m_rel_to_market_cap: PercentFromHeight, + #[traversable(rename = "realized_cap_delta")] pub realized_cap_delta_extended: FiatRollingDeltaExcept1m, pub investor_price: Price>, @@ -82,15 +85,22 @@ pub struct RealizedFull { pub realized_cap_rel_to_own_market_cap: PercentFromHeight, + #[traversable(rename = "realized_profit_sum")] pub realized_profit_sum_extended: RollingWindowsFrom1w, + #[traversable(rename = "realized_loss_sum")] pub realized_loss_sum_extended: RollingWindowsFrom1w, pub realized_profit_to_loss_ratio: RollingWindows, + #[traversable(rename = "value_created_sum")] pub value_created_sum_extended: RollingWindowsFrom1w, + #[traversable(rename = "value_destroyed_sum")] pub value_destroyed_sum_extended: RollingWindowsFrom1w, + #[traversable(rename = "sopr")] pub sopr_extended: RollingWindowsFrom1w, + #[traversable(rename = "sent_in_profit_sum")] pub sent_in_profit_sum_extended: RollingWindowsFrom1w, + #[traversable(rename = "sent_in_loss_sum")] pub sent_in_loss_sum_extended: RollingWindowsFrom1w, pub realized_price_ratio_percentiles: ComputedFromHeightRatioPercentiles, @@ -377,7 +387,7 @@ impl RealizedFull { exit, )?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); // Extended rolling sum (1w, 1m, 1y) for net_realized_pnl self.net_realized_pnl_sum_extended.compute_rolling_sum( @@ -496,7 +506,7 @@ impl RealizedFull { // Net PnL delta (1m base + 24h/1w/1y extended) self.net_pnl_delta.compute( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, &self.net_realized_pnl_cumulative.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/minimal.rs b/crates/brk_computer/src/distribution/metrics/realized/minimal.rs index c7cc0e3f1..ac0e2dc58 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/minimal.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/minimal.rs @@ -127,13 +127,13 @@ impl RealizedMinimal { .compute_rest(starting_indexes.height, exit)?; self.realized_profit_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.realized_profit.height, exit, )?; self.realized_loss_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, + &blocks.lookback.height_24h_ago, &self.realized_loss.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/supply.rs b/crates/brk_computer/src/distribution/metrics/supply.rs index 0460c70f5..78bbc4079 100644 --- a/crates/brk_computer/src/distribution/metrics/supply.rs +++ b/crates/brk_computer/src/distribution/metrics/supply.rs @@ -6,8 +6,8 @@ use crate::{blocks, prices}; use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec}; use crate::internal::{ - HalveCents, HalveDollars, HalveSats, HalveSatsToBitcoin, LazyValueFromHeight, - RollingDelta1m, ValueFromHeight, + AmountFromHeight, HalveCents, HalveDollars, HalveSats, HalveSatsToBitcoin, + LazyAmountFromHeight, RollingDelta1m, }; use super::ImportConfig; @@ -15,8 +15,8 @@ use super::ImportConfig; /// Supply metrics for a cohort. #[derive(Traversable)] pub struct SupplyMetrics { - pub total: ValueFromHeight, - pub halved: LazyValueFromHeight, + pub total: AmountFromHeight, + pub halved: LazyAmountFromHeight, pub delta: RollingDelta1m, } @@ -25,7 +25,7 @@ impl SupplyMetrics { pub(crate) fn forced_import(cfg: &ImportConfig) -> Result { let supply = cfg.import("supply", Version::ZERO)?; - let supply_halved = LazyValueFromHeight::from_block_source::< + let supply_halved = LazyAmountFromHeight::from_block_source::< HalveSats, HalveSatsToBitcoin, HalveCents, @@ -54,8 +54,8 @@ impl SupplyMetrics { pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> { vec![ - &mut self.total.base.sats.height as &mut dyn AnyStoredVec, - &mut self.total.base.cents.height as &mut dyn AnyStoredVec, + &mut self.total.sats.height as &mut dyn AnyStoredVec, + &mut self.total.cents.height as &mut dyn AnyStoredVec, ] } @@ -102,7 +102,7 @@ impl SupplyMetrics { ) -> Result<()> { self.delta.compute( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, &self.total.sats.height, exit, ) diff --git a/crates/brk_computer/src/distribution/metrics/unrealized/minimal.rs b/crates/brk_computer/src/distribution/metrics/unrealized/minimal.rs index b776741a1..c60b06850 100644 --- a/crates/brk_computer/src/distribution/metrics/unrealized/minimal.rs +++ b/crates/brk_computer/src/distribution/metrics/unrealized/minimal.rs @@ -5,15 +5,15 @@ use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec}; use crate::prices; -use crate::internal::ValueFromHeight; +use crate::internal::AmountFromHeight; use crate::distribution::{metrics::ImportConfig, state::UnrealizedState}; /// Minimal unrealized metrics: supply in profit/loss only. #[derive(Traversable)] pub struct UnrealizedMinimal { - pub supply_in_profit: ValueFromHeight, - pub supply_in_loss: ValueFromHeight, + pub supply_in_profit: AmountFromHeight, + pub supply_in_loss: AmountFromHeight, } impl UnrealizedMinimal { @@ -46,10 +46,10 @@ impl UnrealizedMinimal { pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> { vec![ - &mut self.supply_in_profit.base.sats.height as &mut dyn AnyStoredVec, - &mut self.supply_in_profit.base.cents.height as &mut dyn AnyStoredVec, - &mut self.supply_in_loss.base.sats.height as &mut dyn AnyStoredVec, - &mut self.supply_in_loss.base.cents.height as &mut dyn AnyStoredVec, + &mut self.supply_in_profit.sats.height as &mut dyn AnyStoredVec, + &mut self.supply_in_profit.cents.height as &mut dyn AnyStoredVec, + &mut self.supply_in_loss.sats.height as &mut dyn AnyStoredVec, + &mut self.supply_in_loss.cents.height as &mut dyn AnyStoredVec, ] } @@ -59,8 +59,8 @@ impl UnrealizedMinimal { others: &[&Self], exit: &Exit, ) -> Result<()> { - sum_others!(self, starting_indexes, others, exit; supply_in_profit.base.sats.height); - sum_others!(self, starting_indexes, others, exit; supply_in_loss.base.sats.height); + sum_others!(self, starting_indexes, others, exit; supply_in_profit.sats.height); + sum_others!(self, starting_indexes, others, exit; supply_in_loss.sats.height); Ok(()) } diff --git a/crates/brk_computer/src/distribution/vecs.rs b/crates/brk_computer/src/distribution/vecs.rs index 7b962a6b0..6f21ac949 100644 --- a/crates/brk_computer/src/distribution/vecs.rs +++ b/crates/brk_computer/src/distribution/vecs.rs @@ -414,7 +414,7 @@ impl Vecs { exit, )?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.address_activity .compute_rest(starting_indexes.height, &window_starts, exit)?; diff --git a/crates/brk_computer/src/inputs/count/compute.rs b/crates/brk_computer/src/inputs/count/compute.rs index 52f6bdfe3..c0cce6046 100644 --- a/crates/brk_computer/src/inputs/count/compute.rs +++ b/crates/brk_computer/src/inputs/count/compute.rs @@ -15,7 +15,7 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.0 .compute(starting_indexes.height, &window_starts, exit, |full| { full.compute_with_skip( diff --git a/crates/brk_computer/src/internal/value/base.rs b/crates/brk_computer/src/internal/amount/base.rs similarity index 78% rename from crates/brk_computer/src/internal/value/base.rs rename to crates/brk_computer/src/internal/amount/base.rs index b040548e4..124282164 100644 --- a/crates/brk_computer/src/internal/value/base.rs +++ b/crates/brk_computer/src/internal/amount/base.rs @@ -1,4 +1,4 @@ -//! Value type with height-level data only (no period-derived views). +//! Amount type with height-level data only (no period-derived views). //! //! Stores sats and cents per index, plus lazy btc and usd transforms. //! Use when period views are unnecessary (e.g., rolling windows provide windowed data). @@ -16,26 +16,23 @@ use crate::{ prices, }; -const VERSION: Version = Version::TWO; // Match ValueFromHeight versioning +const VERSION: Version = Version::TWO; // Match AmountFromHeight versioning #[derive(Traversable)] -pub struct Value { +pub struct Amount { pub sats: M::Stored>>, pub btc: LazyVecFrom1, pub cents: M::Stored>>, pub usd: LazyVecFrom1, } -impl Value { +impl Amount { pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result { let v = version + VERSION; - let sats: EagerVec> = EagerVec::forced_import(db, name, v)?; - let btc = LazyVecFrom1::transformed::( - &format!("{name}_btc"), - v, - sats.read_only_boxed_clone(), - ); + let sats: EagerVec> = + EagerVec::forced_import(db, &format!("{name}_sats"), v)?; + let btc = LazyVecFrom1::transformed::(name, v, sats.read_only_boxed_clone()); let cents: EagerVec> = EagerVec::forced_import(db, &format!("{name}_cents"), v)?; let usd = LazyVecFrom1::transformed::( diff --git a/crates/brk_computer/src/internal/value/lazy.rs b/crates/brk_computer/src/internal/amount/lazy.rs similarity index 91% rename from crates/brk_computer/src/internal/value/lazy.rs rename to crates/brk_computer/src/internal/amount/lazy.rs index f56b4f0f5..87676bef8 100644 --- a/crates/brk_computer/src/internal/value/lazy.rs +++ b/crates/brk_computer/src/internal/amount/lazy.rs @@ -2,20 +2,20 @@ use brk_traversable::Traversable; use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version}; use vecdb::{LazyVecFrom1, ReadableCloneableVec, UnaryTransform, VecIndex}; -use crate::internal::ValueFromHeight; +use crate::internal::AmountFromHeight; /// Fully lazy value type at height level. /// /// All fields are lazy transforms from existing sources - no storage. #[derive(Clone, Traversable)] -pub struct LazyValue { +pub struct LazyAmount { pub sats: LazyVecFrom1, pub btc: LazyVecFrom1, pub cents: LazyVecFrom1, pub usd: LazyVecFrom1, } -impl LazyValue { +impl LazyAmount { pub(crate) fn from_block_source< SatsTransform, BitcoinTransform, @@ -23,7 +23,7 @@ impl LazyValue { DollarsTransform, >( name: &str, - source: &ValueFromHeight, + source: &AmountFromHeight, version: Version, ) -> Self where @@ -33,13 +33,13 @@ impl LazyValue { DollarsTransform: UnaryTransform, { let sats = LazyVecFrom1::transformed::( - name, + &format!("{name}_sats"), version, source.sats.height.read_only_boxed_clone(), ); let btc = LazyVecFrom1::transformed::( - &format!("{name}_btc"), + name, version, source.sats.height.read_only_boxed_clone(), ); diff --git a/crates/brk_computer/src/internal/value/mod.rs b/crates/brk_computer/src/internal/amount/mod.rs similarity index 100% rename from crates/brk_computer/src/internal/value/mod.rs rename to crates/brk_computer/src/internal/amount/mod.rs diff --git a/crates/brk_computer/src/internal/containers/distribution_stats.rs b/crates/brk_computer/src/internal/containers/distribution_stats.rs index 0efde5961..a485bcdcf 100644 --- a/crates/brk_computer/src/internal/containers/distribution_stats.rs +++ b/crates/brk_computer/src/internal/containers/distribution_stats.rs @@ -14,7 +14,7 @@ pub struct DistributionStats { impl DistributionStats { pub const SUFFIXES: [&'static str; 8] = [ - "average", "min", "max", "p10", "p25", "median", "p75", "p90", + "average", "min", "max", "pct10", "pct25", "median", "pct75", "pct90", ]; pub fn try_from_fn( diff --git a/crates/brk_computer/src/internal/derived/lazy_value.rs b/crates/brk_computer/src/internal/derived/lazy_amount.rs similarity index 88% rename from crates/brk_computer/src/internal/derived/lazy_value.rs rename to crates/brk_computer/src/internal/derived/lazy_amount.rs index 799b9e243..e1337c041 100644 --- a/crates/brk_computer/src/internal/derived/lazy_value.rs +++ b/crates/brk_computer/src/internal/derived/lazy_amount.rs @@ -2,17 +2,17 @@ use brk_traversable::Traversable; use brk_types::{Bitcoin, Cents, Dollars, Sats, Version}; use vecdb::UnaryTransform; -use crate::internal::{LazyHeightDerived, ValueFromHeight}; +use crate::internal::{AmountFromHeight, LazyHeightDerived}; #[derive(Clone, Traversable)] -pub struct LazyValueHeightDerived { +pub struct LazyAmountHeightDerived { pub sats: LazyHeightDerived, pub btc: LazyHeightDerived, pub cents: LazyHeightDerived, pub usd: LazyHeightDerived, } -impl LazyValueHeightDerived { +impl LazyAmountHeightDerived { pub(crate) fn from_block_source< SatsTransform, BitcoinTransform, @@ -20,7 +20,7 @@ impl LazyValueHeightDerived { DollarsTransform, >( name: &str, - source: &ValueFromHeight, + source: &AmountFromHeight, version: Version, ) -> Self where @@ -30,13 +30,13 @@ impl LazyValueHeightDerived { DollarsTransform: UnaryTransform, { let sats = LazyHeightDerived::from_derived_computed::( - name, + &format!("{name}_sats"), version, &source.sats.rest, ); let btc = LazyHeightDerived::from_derived_computed::( - &format!("{name}_btc"), + name, version, &source.sats.rest, ); diff --git a/crates/brk_computer/src/internal/derived/mod.rs b/crates/brk_computer/src/internal/derived/mod.rs index 4b08ef6d2..edb1120e5 100644 --- a/crates/brk_computer/src/internal/derived/mod.rs +++ b/crates/brk_computer/src/internal/derived/mod.rs @@ -1,13 +1,13 @@ mod full; mod last; mod lazy_last; -mod lazy_value; +mod lazy_amount; mod map_option; mod transform_last; pub use full::*; pub use last::*; pub use lazy_last::*; -pub use lazy_value::*; +pub use lazy_amount::*; pub use map_option::*; pub use transform_last::*; diff --git a/crates/brk_computer/src/internal/from_height/value/cumulative.rs b/crates/brk_computer/src/internal/from_height/amount/cumulative.rs similarity index 75% rename from crates/brk_computer/src/internal/from_height/value/cumulative.rs rename to crates/brk_computer/src/internal/from_height/amount/cumulative.rs index 9c4a02e71..159279eb9 100644 --- a/crates/brk_computer/src/internal/from_height/value/cumulative.rs +++ b/crates/brk_computer/src/internal/from_height/amount/cumulative.rs @@ -5,19 +5,19 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, - internal::{ByUnit, SatsToCents}, + internal::{AmountFromHeight, SatsToCents}, prices, }; #[derive(Traversable)] -pub struct ValueFromHeightCumulative { - pub base: ByUnit, - pub cumulative: ByUnit, +pub struct AmountFromHeightCumulative { + pub base: AmountFromHeight, + pub cumulative: AmountFromHeight, } const VERSION: Version = Version::ONE; -impl ValueFromHeightCumulative { +impl AmountFromHeightCumulative { pub(crate) fn forced_import( db: &Database, name: &str, @@ -27,8 +27,13 @@ impl ValueFromHeightCumulative { let v = version + VERSION; Ok(Self { - base: ByUnit::forced_import(db, name, v, indexes)?, - cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?, + base: AmountFromHeight::forced_import(db, name, v, indexes)?, + cumulative: AmountFromHeight::forced_import( + db, + &format!("{name}_cumulative"), + v, + indexes, + )?, }) } diff --git a/crates/brk_computer/src/internal/from_height/value/cumulative_sum.rs b/crates/brk_computer/src/internal/from_height/amount/cumulative_sum.rs similarity index 70% rename from crates/brk_computer/src/internal/from_height/value/cumulative_sum.rs rename to crates/brk_computer/src/internal/from_height/amount/cumulative_sum.rs index faab7a7b2..bb4ca5b78 100644 --- a/crates/brk_computer/src/internal/from_height/value/cumulative_sum.rs +++ b/crates/brk_computer/src/internal/from_height/amount/cumulative_sum.rs @@ -5,20 +5,20 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, - internal::{ByUnit, RollingSumByUnit, SatsToCents, WindowStarts}, + internal::{AmountFromHeight, RollingSumAmountFromHeight, SatsToCents, WindowStarts}, prices, }; #[derive(Traversable)] -pub struct ValueFromHeightCumulativeSum { - pub base: ByUnit, - pub cumulative: ByUnit, - pub sum: RollingSumByUnit, +pub struct AmountFromHeightCumulativeSum { + pub base: AmountFromHeight, + pub cumulative: AmountFromHeight, + pub sum: RollingSumAmountFromHeight, } const VERSION: Version = Version::TWO; -impl ValueFromHeightCumulativeSum { +impl AmountFromHeightCumulativeSum { pub(crate) fn forced_import( db: &Database, name: &str, @@ -28,9 +28,14 @@ impl ValueFromHeightCumulativeSum { let v = version + VERSION; Ok(Self { - base: ByUnit::forced_import(db, name, v, indexes)?, - cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?, - sum: RollingSumByUnit::forced_import(db, name, v, indexes)?, + base: AmountFromHeight::forced_import(db, name, v, indexes)?, + cumulative: AmountFromHeight::forced_import( + db, + &format!("{name}_cumulative"), + v, + indexes, + )?, + sum: RollingSumAmountFromHeight::forced_import(db, name, v, indexes)?, }) } diff --git a/crates/brk_computer/src/internal/from_height/value/full.rs b/crates/brk_computer/src/internal/from_height/amount/full.rs similarity index 71% rename from crates/brk_computer/src/internal/from_height/value/full.rs rename to crates/brk_computer/src/internal/from_height/amount/full.rs index c0ce0288c..7bd783b3d 100644 --- a/crates/brk_computer/src/internal/from_height/value/full.rs +++ b/crates/brk_computer/src/internal/from_height/amount/full.rs @@ -5,21 +5,21 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, - internal::{ByUnit, RollingFullByUnit, SatsToCents, WindowStarts}, + internal::{AmountFromHeight, RollingFullAmountFromHeight, SatsToCents, WindowStarts}, prices, }; #[derive(Traversable)] -pub struct ValueFromHeightFull { - pub base: ByUnit, - pub cumulative: ByUnit, +pub struct AmountFromHeightFull { + pub base: AmountFromHeight, + pub cumulative: AmountFromHeight, #[traversable(flatten)] - pub rolling: RollingFullByUnit, + pub rolling: RollingFullAmountFromHeight, } const VERSION: Version = Version::TWO; -impl ValueFromHeightFull { +impl AmountFromHeightFull { pub(crate) fn forced_import( db: &Database, name: &str, @@ -29,9 +29,14 @@ impl ValueFromHeightFull { let v = version + VERSION; Ok(Self { - base: ByUnit::forced_import(db, name, v, indexes)?, - cumulative: ByUnit::forced_import(db, &format!("{name}_cumulative"), v, indexes)?, - rolling: RollingFullByUnit::forced_import(db, name, v, indexes)?, + base: AmountFromHeight::forced_import(db, name, v, indexes)?, + cumulative: AmountFromHeight::forced_import( + db, + &format!("{name}_cumulative"), + v, + indexes, + )?, + rolling: RollingFullAmountFromHeight::forced_import(db, name, v, indexes)?, }) } diff --git a/crates/brk_computer/src/internal/from_height/value/lazy.rs b/crates/brk_computer/src/internal/from_height/amount/lazy.rs similarity index 72% rename from crates/brk_computer/src/internal/from_height/value/lazy.rs rename to crates/brk_computer/src/internal/from_height/amount/lazy.rs index 79be366f0..82b0827a2 100644 --- a/crates/brk_computer/src/internal/from_height/value/lazy.rs +++ b/crates/brk_computer/src/internal/from_height/amount/lazy.rs @@ -1,25 +1,25 @@ -//! Lazy value wrapper for ValueFromHeight - all transforms are lazy. +//! Lazy value wrapper for AmountFromHeight - all transforms are lazy. use brk_traversable::Traversable; use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version}; use derive_more::{Deref, DerefMut}; use vecdb::UnaryTransform; -use crate::internal::{LazyValue, LazyValueHeightDerived, ValueFromHeight}; +use crate::internal::{AmountFromHeight, LazyAmount, LazyAmountHeightDerived}; -/// Lazy value wrapper with height + all derived last transforms from ValueFromHeight. +/// Lazy value wrapper with height + all derived last transforms from AmountFromHeight. #[derive(Clone, Deref, DerefMut, Traversable)] #[traversable(merge)] -pub struct LazyValueFromHeight { +pub struct LazyAmountFromHeight { #[traversable(flatten)] - pub height: LazyValue, + pub height: LazyAmount, #[deref] #[deref_mut] #[traversable(flatten)] - pub rest: Box, + pub rest: Box, } -impl LazyValueFromHeight { +impl LazyAmountFromHeight { pub(crate) fn from_block_source< SatsTransform, BitcoinTransform, @@ -27,7 +27,7 @@ impl LazyValueFromHeight { DollarsTransform, >( name: &str, - source: &ValueFromHeight, + source: &AmountFromHeight, version: Version, ) -> Self where @@ -36,14 +36,14 @@ impl LazyValueFromHeight { CentsTransform: UnaryTransform, DollarsTransform: UnaryTransform, { - let height = LazyValue::from_block_source::< + let height = LazyAmount::from_block_source::< SatsTransform, BitcoinTransform, CentsTransform, DollarsTransform, >(name, source, version); - let rest = LazyValueHeightDerived::from_block_source::< + let rest = LazyAmountHeightDerived::from_block_source::< SatsTransform, BitcoinTransform, CentsTransform, diff --git a/crates/brk_computer/src/internal/from_height/amount/mod.rs b/crates/brk_computer/src/internal/from_height/amount/mod.rs new file mode 100644 index 000000000..933d338aa --- /dev/null +++ b/crates/brk_computer/src/internal/from_height/amount/mod.rs @@ -0,0 +1,122 @@ +mod cumulative; +mod cumulative_sum; +mod full; +mod lazy; +mod rolling; +mod rolling_full; +mod rolling_sum; + +use brk_error::Result; +use brk_traversable::Traversable; +use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version}; +use vecdb::{AnyVec, Database, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode}; + +use crate::{ + indexes, + internal::{ + CentsUnsignedToDollars, ComputedFromHeight, LazyFromHeight, SatsToBitcoin, SatsToCents, + Windows, + }, + prices, +}; + +pub use cumulative::*; +pub use cumulative_sum::*; +pub use full::*; +pub use lazy::*; +pub use rolling::*; +pub use rolling_full::*; +pub use rolling_sum::*; + +#[derive(Traversable)] +pub struct AmountFromHeight { + pub sats: ComputedFromHeight, + pub btc: LazyFromHeight, + pub cents: ComputedFromHeight, + pub usd: LazyFromHeight, +} + +impl AmountFromHeight { + pub(crate) fn forced_import( + db: &Database, + name: &str, + version: Version, + indexes: &indexes::Vecs, + ) -> Result { + let sats = + ComputedFromHeight::forced_import(db, &format!("{name}_sats"), version, indexes)?; + + let btc = LazyFromHeight::from_computed::( + name, + version, + sats.height.read_only_boxed_clone(), + &sats, + ); + + let cents = + ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?; + + let usd = LazyFromHeight::from_computed::( + &format!("{name}_usd"), + version, + cents.height.read_only_boxed_clone(), + ¢s, + ); + + Ok(Self { + sats, + btc, + cents, + usd, + }) + } + + pub(crate) fn min_stateful_len(&self) -> usize { + self.sats.height.len() + } + + pub(crate) fn compute( + &mut self, + prices: &prices::Vecs, + max_from: Height, + exit: &Exit, + ) -> Result<()> { + self.cents.compute_binary::( + max_from, + &self.sats.height, + &prices.price.cents.height, + exit, + )?; + Ok(()) + } + + pub(crate) fn compute_rolling_sum( + &mut self, + max_from: Height, + window_starts: &impl ReadableVec, + sats_source: &impl ReadableVec, + cents_source: &impl ReadableVec, + exit: &Exit, + ) -> Result<()> { + self.sats + .height + .compute_rolling_sum(max_from, window_starts, sats_source, exit)?; + self.cents + .height + .compute_rolling_sum(max_from, window_starts, cents_source, exit)?; + Ok(()) + } +} + +impl Windows { + pub(crate) fn forced_import( + db: &Database, + name: &str, + version: Version, + indexes: &indexes::Vecs, + ) -> Result { + Windows::try_from_fn(|suffix| { + AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) + }) + } +} diff --git a/crates/brk_computer/src/internal/from_height/value/rolling.rs b/crates/brk_computer/src/internal/from_height/amount/rolling.rs similarity index 65% rename from crates/brk_computer/src/internal/from_height/value/rolling.rs rename to crates/brk_computer/src/internal/from_height/amount/rolling.rs index 049eeb983..a315cbcd6 100644 --- a/crates/brk_computer/src/internal/from_height/value/rolling.rs +++ b/crates/brk_computer/src/internal/from_height/amount/rolling.rs @@ -1,7 +1,7 @@ //! Value type for Height + Rolling pattern. //! //! Combines Value (sats/btc/usd per height, no period views) with -//! ValueFromHeightWindows (rolling sums across 4 windows). +//! AmountFromHeightWindows (rolling sums across 4 windows). use brk_error::Result; use brk_traversable::Traversable; @@ -11,21 +11,21 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, - internal::{Value, ValueFromHeightWindows, WindowStarts}, + internal::{Amount, AmountFromHeightWindows, WindowStarts}, prices, }; #[derive(Deref, DerefMut, Traversable)] -pub struct ValueFromHeightRolling { +pub struct AmountFromHeightRolling { #[deref] #[deref_mut] #[traversable(flatten)] - pub value: Value, + pub amount: Amount, #[traversable(flatten)] - pub rolling: ValueFromHeightWindows, + pub rolling: AmountFromHeightWindows, } -impl ValueFromHeightRolling { +impl AmountFromHeightRolling { pub(crate) fn forced_import( db: &Database, name: &str, @@ -33,8 +33,8 @@ impl ValueFromHeightRolling { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - value: Value::forced_import(db, name, version)?, - rolling: ValueFromHeightWindows::forced_import(db, name, version, indexes)?, + amount: Amount::forced_import(db, name, version)?, + rolling: AmountFromHeightWindows::forced_import(db, name, version, indexes)?, }) } @@ -47,13 +47,13 @@ impl ValueFromHeightRolling { exit: &Exit, compute_sats: impl FnOnce(&mut EagerVec>) -> Result<()>, ) -> Result<()> { - compute_sats(&mut self.value.sats)?; - self.value.compute_cents(prices, max_from, exit)?; + compute_sats(&mut self.amount.sats)?; + self.amount.compute_cents(prices, max_from, exit)?; self.rolling.compute_rolling_sum( max_from, windows, - &self.value.sats, - &self.value.cents, + &self.amount.sats, + &self.amount.cents, exit, )?; Ok(()) diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs b/crates/brk_computer/src/internal/from_height/amount/rolling_full.rs similarity index 83% rename from crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs rename to crates/brk_computer/src/internal/from_height/amount/rolling_full.rs index fe47822fe..62f2b3896 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs +++ b/crates/brk_computer/src/internal/from_height/amount/rolling_full.rs @@ -7,18 +7,18 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; use crate::{ indexes, internal::{ - ByUnit, DistributionStats, WindowStarts, Windows, compute_rolling_distribution_from_starts, + AmountFromHeight, DistributionStats, WindowStarts, Windows, compute_rolling_distribution_from_starts, }, }; -/// One window slot: sum + 8 distribution stats, each a ByUnit. +/// One window slot: sum + 8 distribution stats, each a AmountFromHeight. /// /// Tree: `sum.sats.height`, `average.sats.height`, etc. #[derive(Traversable)] pub struct RollingFullSlot { - pub sum: ByUnit, + pub sum: AmountFromHeight, #[traversable(flatten)] - pub distribution: DistributionStats>, + pub distribution: DistributionStats>, } impl RollingFullSlot { @@ -29,9 +29,9 @@ impl RollingFullSlot { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - sum: ByUnit::forced_import(db, &format!("{name}_sum"), version, indexes)?, + sum: AmountFromHeight::forced_import(db, &format!("{name}_sum"), version, indexes)?, distribution: DistributionStats::try_from_fn(|suffix| { - ByUnit::forced_import(db, &format!("{name}_{suffix}"), version, indexes) + AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?, }) } @@ -85,9 +85,9 @@ impl RollingFullSlot { /// Tree: `_24h.sum.sats.height`, `_24h.average.sats.height`, etc. #[derive(Deref, DerefMut, Traversable)] #[traversable(transparent)] -pub struct RollingFullByUnit(pub Windows>); +pub struct RollingFullAmountFromHeight(pub Windows>); -impl RollingFullByUnit { +impl RollingFullAmountFromHeight { pub(crate) fn forced_import( db: &Database, name: &str, diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs b/crates/brk_computer/src/internal/from_height/amount/rolling_sum.rs similarity index 84% rename from crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs rename to crates/brk_computer/src/internal/from_height/amount/rolling_sum.rs index 186a42b10..b0db48985 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs +++ b/crates/brk_computer/src/internal/from_height/amount/rolling_sum.rs @@ -6,7 +6,7 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; use crate::{ indexes, - internal::{ByUnit, WindowStarts, Windows}, + internal::{AmountFromHeight, WindowStarts, Windows}, }; /// Rolling sum only, window-first then unit. @@ -14,16 +14,16 @@ use crate::{ /// Tree: `_24h.sats.height`, `_24h.btc.height`, etc. #[derive(Deref, DerefMut, Traversable)] #[traversable(transparent)] -pub struct RollingSumByUnit(pub Windows>); +pub struct RollingSumAmountFromHeight(pub Windows>); -impl RollingSumByUnit { +impl RollingSumAmountFromHeight { pub(crate) fn forced_import( db: &Database, name: &str, version: Version, indexes: &indexes::Vecs, ) -> Result { - Ok(Self(Windows::::forced_import( + Ok(Self(Windows::::forced_import( db, &format!("{name}_sum"), version, diff --git a/crates/brk_computer/src/internal/from_height/by_unit/mod.rs b/crates/brk_computer/src/internal/from_height/by_unit/mod.rs deleted file mode 100644 index 8a69f338e..000000000 --- a/crates/brk_computer/src/internal/from_height/by_unit/mod.rs +++ /dev/null @@ -1,77 +0,0 @@ -mod rolling_full; -mod rolling_sum; - -use brk_error::Result; -use brk_traversable::Traversable; -use brk_types::{Bitcoin, Cents, Dollars, Sats, Version}; -use vecdb::{AnyVec, Database, ReadableCloneableVec, Rw, StorageMode}; - -use crate::{ - indexes, - internal::{ - CentsUnsignedToDollars, ComputedFromHeight, LazyFromHeight, SatsToBitcoin, Windows, - }, -}; - -pub use rolling_full::*; -pub use rolling_sum::*; - -#[derive(Traversable)] -pub struct ByUnit { - pub sats: ComputedFromHeight, - pub btc: LazyFromHeight, - pub cents: ComputedFromHeight, - pub usd: LazyFromHeight, -} - -impl ByUnit { - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - let sats = ComputedFromHeight::forced_import(db, name, version, indexes)?; - - let btc = LazyFromHeight::from_computed::( - &format!("{name}_btc"), - version, - sats.height.read_only_boxed_clone(), - &sats, - ); - - let cents = - ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?; - - let usd = LazyFromHeight::from_computed::( - &format!("{name}_usd"), - version, - cents.height.read_only_boxed_clone(), - ¢s, - ); - - Ok(Self { - sats, - btc, - cents, - usd, - }) - } - - pub(crate) fn min_stateful_len(&self) -> usize { - self.sats.height.len() - } -} - -impl Windows { - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - Windows::try_from_fn(|suffix| { - ByUnit::forced_import(db, &format!("{name}_{suffix}"), version, indexes) - }) - } -} diff --git a/crates/brk_computer/src/internal/from_height/fiat.rs b/crates/brk_computer/src/internal/from_height/fiat.rs index a7ada88c3..f707e68ee 100644 --- a/crates/brk_computer/src/internal/from_height/fiat.rs +++ b/crates/brk_computer/src/internal/from_height/fiat.rs @@ -41,7 +41,7 @@ impl FiatFromHeight { let cents = ComputedFromHeight::forced_import(db, &format!("{name}_cents"), version, indexes)?; let usd = LazyFromHeight::from_computed::( - &format!("{name}_usd"), + name, version, cents.height.read_only_boxed_clone(), ¢s, diff --git a/crates/brk_computer/src/internal/from_height/lazy_fiat.rs b/crates/brk_computer/src/internal/from_height/lazy_fiat.rs index 8faebd434..d1eb217f3 100644 --- a/crates/brk_computer/src/internal/from_height/lazy_fiat.rs +++ b/crates/brk_computer/src/internal/from_height/lazy_fiat.rs @@ -31,7 +31,7 @@ impl LazyFiatFromHeight { source, ); let usd = LazyFromHeight::from_computed::( - &format!("{name}_usd"), + name, version, source.height.read_only_boxed_clone(), source, diff --git a/crates/brk_computer/src/internal/from_height/mod.rs b/crates/brk_computer/src/internal/from_height/mod.rs index 449ea8098..3a97df3e8 100644 --- a/crates/brk_computer/src/internal/from_height/mod.rs +++ b/crates/brk_computer/src/internal/from_height/mod.rs @@ -1,5 +1,5 @@ mod base; -mod by_unit; +mod amount; mod computed; mod constant; mod fiat; @@ -10,10 +10,9 @@ mod percentiles; mod price; mod ratio; mod stddev; -mod value; pub use base::*; -pub use by_unit::*; +pub use amount::*; pub use computed::*; pub use constant::*; pub use fiat::*; @@ -24,4 +23,3 @@ pub use percentiles::*; pub use price::*; pub use ratio::*; pub use stddev::*; -pub use value::*; diff --git a/crates/brk_computer/src/internal/from_height/ratio/percentiles.rs b/crates/brk_computer/src/internal/from_height/ratio/percentiles.rs index 1f8ea1016..c8affef01 100644 --- a/crates/brk_computer/src/internal/from_height/ratio/percentiles.rs +++ b/crates/brk_computer/src/internal/from_height/ratio/percentiles.rs @@ -91,14 +91,14 @@ impl ComputedFromHeightRatioPercentiles { ) -> Result<()> { self.ratio_sma_1w.bps.height.compute_rolling_average( starting_indexes.height, - &blocks.count.height_1w_ago, + &blocks.lookback.height_1w_ago, ratio_source, exit, )?; self.ratio_sma_1m.bps.height.compute_rolling_average( starting_indexes.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, ratio_source, exit, )?; diff --git a/crates/brk_computer/src/internal/from_height/stddev/mod.rs b/crates/brk_computer/src/internal/from_height/stddev/mod.rs index c816ca2cb..d69028b33 100644 --- a/crates/brk_computer/src/internal/from_height/stddev/mod.rs +++ b/crates/brk_computer/src/internal/from_height/stddev/mod.rs @@ -65,7 +65,7 @@ impl ComputedFromHeightStdDev { return Ok(()); } - let window_starts = blocks.count.start_vec(self.days); + let window_starts = blocks.lookback.start_vec(self.days); self.sma.height.compute_rolling_average( starting_indexes.height, diff --git a/crates/brk_computer/src/internal/from_height/value/base.rs b/crates/brk_computer/src/internal/from_height/value/base.rs deleted file mode 100644 index 57f1e702b..000000000 --- a/crates/brk_computer/src/internal/from_height/value/base.rs +++ /dev/null @@ -1,69 +0,0 @@ -use brk_error::Result; -use brk_traversable::Traversable; -use brk_types::{Cents, Height, Sats, Version}; -use derive_more::{Deref, DerefMut}; -use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; - -use crate::{ - indexes, - internal::{ByUnit, SatsToCents}, - prices, -}; - -#[derive(Deref, DerefMut, Traversable)] -#[traversable(transparent)] -pub struct ValueFromHeight { - #[deref] - #[deref_mut] - pub base: ByUnit, -} - -const VERSION: Version = Version::TWO; - -impl ValueFromHeight { - pub(crate) fn forced_import( - db: &Database, - name: &str, - version: Version, - indexes: &indexes::Vecs, - ) -> Result { - let v = version + VERSION; - Ok(Self { - base: ByUnit::forced_import(db, name, v, indexes)?, - }) - } - - pub(crate) fn compute( - &mut self, - prices: &prices::Vecs, - max_from: Height, - exit: &Exit, - ) -> Result<()> { - self.base.cents.compute_binary::( - max_from, - &self.base.sats.height, - &prices.price.cents.height, - exit, - )?; - Ok(()) - } - - pub(crate) fn compute_rolling_sum( - &mut self, - max_from: Height, - window_starts: &impl ReadableVec, - sats_source: &impl ReadableVec, - cents_source: &impl ReadableVec, - exit: &Exit, - ) -> Result<()> { - self.base - .sats - .height - .compute_rolling_sum(max_from, window_starts, sats_source, exit)?; - self.base - .cents - .height - .compute_rolling_sum(max_from, window_starts, cents_source, exit)?; - Ok(()) - } -} diff --git a/crates/brk_computer/src/internal/from_height/value/mod.rs b/crates/brk_computer/src/internal/from_height/value/mod.rs deleted file mode 100644 index 7d7c37f4e..000000000 --- a/crates/brk_computer/src/internal/from_height/value/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod base; -mod cumulative; -mod cumulative_sum; -mod full; -mod lazy; -mod rolling; - -pub use base::*; -pub use cumulative::*; -pub use cumulative_sum::*; -pub use full::*; -pub use lazy::*; -pub use rolling::*; diff --git a/crates/brk_computer/src/internal/mod.rs b/crates/brk_computer/src/internal/mod.rs index bfbcf0415..96bf3278c 100644 --- a/crates/brk_computer/src/internal/mod.rs +++ b/crates/brk_computer/src/internal/mod.rs @@ -9,7 +9,7 @@ mod indexes; mod rolling; mod traits; mod transform; -mod value; +mod amount; pub(crate) use aggregate::*; pub(crate) use algo::*; @@ -22,4 +22,4 @@ pub(crate) use indexes::*; pub(crate) use rolling::*; pub(crate) use traits::*; pub use transform::*; -pub(crate) use value::*; +pub(crate) use amount::*; diff --git a/crates/brk_computer/src/internal/rolling/value_windows.rs b/crates/brk_computer/src/internal/rolling/amount_windows.rs similarity index 75% rename from crates/brk_computer/src/internal/rolling/value_windows.rs rename to crates/brk_computer/src/internal/rolling/amount_windows.rs index d9bfdb6b2..02b8800c4 100644 --- a/crates/brk_computer/src/internal/rolling/value_windows.rs +++ b/crates/brk_computer/src/internal/rolling/amount_windows.rs @@ -1,4 +1,4 @@ -//! ValueFromHeightWindows - window-first ordering. +//! AmountFromHeightWindows - window-first ordering. //! //! Access pattern: `coinbase_sum._24h.sats.height` //! Each window (24h, 7d, 30d, 1y) contains sats (stored) + btc (lazy) + usd (stored). @@ -14,17 +14,17 @@ use brk_types::{Cents, Sats}; use crate::{ indexes, - internal::{ValueFromHeight, WindowStarts, Windows}, + internal::{AmountFromHeight, WindowStarts, Windows}, }; /// Value rolling windows — window-first, currency-last. /// -/// Each window contains `ValueFromHeight` (sats + btc lazy + usd). +/// Each window contains `AmountFromHeight` (sats + btc lazy + usd). #[derive(Deref, DerefMut, Traversable)] #[traversable(transparent)] -pub struct ValueFromHeightWindows(pub Windows>); +pub struct AmountFromHeightWindows(pub Windows>); -impl ValueFromHeightWindows { +impl AmountFromHeightWindows { pub(crate) fn forced_import( db: &Database, name: &str, @@ -32,7 +32,7 @@ impl ValueFromHeightWindows { indexes: &indexes::Vecs, ) -> Result { Ok(Self(Windows::try_from_fn(|suffix| { - ValueFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) + AmountFromHeight::forced_import(db, &format!("{name}_{suffix}"), version, indexes) })?)) } diff --git a/crates/brk_computer/src/internal/rolling/mod.rs b/crates/brk_computer/src/internal/rolling/mod.rs index 74cef4cc3..7a50a60a0 100644 --- a/crates/brk_computer/src/internal/rolling/mod.rs +++ b/crates/brk_computer/src/internal/rolling/mod.rs @@ -1,11 +1,11 @@ mod distribution; mod full; mod percent_windows; -mod value_windows; +mod amount_windows; mod windows; pub use distribution::*; pub use full::*; pub use percent_windows::*; -pub use value_windows::*; +pub use amount_windows::*; pub use windows::*; diff --git a/crates/brk_computer/src/market/dca/compute.rs b/crates/brk_computer/src/market/dca/compute.rs index 57a845a78..96c296168 100644 --- a/crates/brk_computer/src/market/dca/compute.rs +++ b/crates/brk_computer/src/market/dca/compute.rs @@ -51,7 +51,7 @@ impl Vecs { // DCA by period - stack (rolling sum via _start vecs) for (stack, days) in self.period_stack.iter_mut_with_days() { - let window_starts = blocks.count.start_vec(days as usize); + let window_starts = blocks.lookback.start_vec(days as usize); stack.sats.height.compute_rolling_sum( starting_indexes.height, window_starts, diff --git a/crates/brk_computer/src/market/dca/import.rs b/crates/brk_computer/src/market/dca/import.rs index 448233fb0..55a26281e 100644 --- a/crates/brk_computer/src/market/dca/import.rs +++ b/crates/brk_computer/src/market/dca/import.rs @@ -5,7 +5,7 @@ use vecdb::{Database, ImportableVec}; use super::{ByDcaCagr, ByDcaClass, ByDcaPeriod, Vecs}; use crate::{ indexes, - internal::{PercentFromHeight, Price, ValueFromHeight}, + internal::{AmountFromHeight, PercentFromHeight, Price}, }; impl Vecs { @@ -15,7 +15,7 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { let period_stack = ByDcaPeriod::try_new(|name, _days| { - ValueFromHeight::forced_import(db, &format!("dca_stack_{name}"), version, indexes) + AmountFromHeight::forced_import(db, &format!("dca_stack_{name}"), version, indexes) })?; let period_cost_basis = ByDcaPeriod::try_new(|name, _days| { @@ -31,7 +31,7 @@ impl Vecs { })?; let period_lump_sum_stack = ByDcaPeriod::try_new(|name, _days| { - ValueFromHeight::forced_import(db, &format!("lump_sum_stack_{name}"), version, indexes) + AmountFromHeight::forced_import(db, &format!("lump_sum_stack_{name}"), version, indexes) })?; let period_lump_sum_return = ByDcaPeriod::try_new(|name, _days| { @@ -44,7 +44,7 @@ impl Vecs { })?; let class_stack = ByDcaClass::try_new(|name, _year, _day1| { - ValueFromHeight::forced_import(db, &format!("dca_stack_{name}"), version, indexes) + AmountFromHeight::forced_import(db, &format!("dca_stack_{name}"), version, indexes) })?; let class_cost_basis = ByDcaClass::try_new(|name, _year, _day1| { diff --git a/crates/brk_computer/src/market/dca/vecs.rs b/crates/brk_computer/src/market/dca/vecs.rs index 0703320c2..012f61975 100644 --- a/crates/brk_computer/src/market/dca/vecs.rs +++ b/crates/brk_computer/src/market/dca/vecs.rs @@ -3,7 +3,7 @@ use brk_types::{BasisPointsSigned32, Cents, Height, Sats}; use vecdb::{EagerVec, PcoVec, Rw, StorageMode}; use super::{ByDcaCagr, ByDcaClass, ByDcaPeriod}; -use crate::internal::{ComputedFromHeight, PercentFromHeight, Price, ValueFromHeight}; +use crate::internal::{AmountFromHeight, ComputedFromHeight, PercentFromHeight, Price}; #[derive(Traversable)] pub struct Vecs { /// Per-height DCA sats contribution: sats_from_dca(close) on day boundaries, 0 otherwise. @@ -11,17 +11,17 @@ pub struct Vecs { pub dca_sats_per_day: M::Stored>>, // DCA by period - pub period_stack: ByDcaPeriod>, + pub period_stack: ByDcaPeriod>, pub period_cost_basis: ByDcaPeriod>>, pub period_return: ByDcaPeriod>, pub period_cagr: ByDcaCagr>, // Lump sum by period (for comparison with DCA) - pub period_lump_sum_stack: ByDcaPeriod>, + pub period_lump_sum_stack: ByDcaPeriod>, pub period_lump_sum_return: ByDcaPeriod>, // DCA by year class - pub class_stack: ByDcaClass>, + pub class_stack: ByDcaClass>, pub class_cost_basis: ByDcaClass>>, pub class_return: ByDcaClass>, } diff --git a/crates/brk_computer/src/market/indicators/compute.rs b/crates/brk_computer/src/market/indicators/compute.rs index a39494db8..239178bd9 100644 --- a/crates/brk_computer/src/market/indicators/compute.rs +++ b/crates/brk_computer/src/market/indicators/compute.rs @@ -56,7 +56,7 @@ impl Vecs { self.stoch_d.bps.height.compute_rolling_average( starting_indexes.height, - &blocks.count.height_3d_ago, + &blocks.lookback.height_3d_ago, &self.stoch_k.bps.height, exit, )?; diff --git a/crates/brk_computer/src/market/indicators/macd.rs b/crates/brk_computer/src/market/indicators/macd.rs index 053a6257e..a9728e5d3 100644 --- a/crates/brk_computer/src/market/indicators/macd.rs +++ b/crates/brk_computer/src/market/indicators/macd.rs @@ -17,9 +17,9 @@ pub(super) fn compute( exit: &Exit, ) -> Result<()> { let close = &prices.price.usd.height; - let ws_fast = blocks.count.start_vec(fast_days); - let ws_slow = blocks.count.start_vec(slow_days); - let ws_signal = blocks.count.start_vec(signal_days); + let ws_fast = blocks.lookback.start_vec(fast_days); + let ws_slow = blocks.lookback.start_vec(slow_days); + let ws_signal = blocks.lookback.start_vec(signal_days); chain .ema_fast diff --git a/crates/brk_computer/src/market/indicators/rsi.rs b/crates/brk_computer/src/market/indicators/rsi.rs index 6837ed062..6fc92b53b 100644 --- a/crates/brk_computer/src/market/indicators/rsi.rs +++ b/crates/brk_computer/src/market/indicators/rsi.rs @@ -14,8 +14,8 @@ pub(super) fn compute( starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let ws_rma = blocks.count.start_vec(rma_days); - let ws_sma = blocks.count.start_vec(stoch_sma_days); + let ws_rma = blocks.lookback.start_vec(rma_days); + let ws_sma = blocks.lookback.start_vec(stoch_sma_days); // Gains = max(return, 0) chain.gains.height.compute_transform( diff --git a/crates/brk_computer/src/market/lookback/compute.rs b/crates/brk_computer/src/market/lookback/compute.rs index e4ec3ff85..1a8496f38 100644 --- a/crates/brk_computer/src/market/lookback/compute.rs +++ b/crates/brk_computer/src/market/lookback/compute.rs @@ -16,7 +16,7 @@ impl Vecs { let price = &prices.price.cents.height; for (price_lookback, days) in self.price_lookback.iter_mut_with_days() { - let window_starts = blocks.count.start_vec(days as usize); + let window_starts = blocks.lookback.start_vec(days as usize); price_lookback.cents.height.compute_lookback( starting_indexes.height, window_starts, diff --git a/crates/brk_computer/src/market/moving_average/compute.rs b/crates/brk_computer/src/market/moving_average/compute.rs index e8c8223fc..bb38a974e 100644 --- a/crates/brk_computer/src/market/moving_average/compute.rs +++ b/crates/brk_computer/src/market/moving_average/compute.rs @@ -33,7 +33,7 @@ impl Vecs { (&mut self.price_sma_200w, 200 * 7), (&mut self.price_sma_4y, 4 * 365), ] { - let window_starts = blocks.count.start_vec(period); + let window_starts = blocks.lookback.start_vec(period); sma.compute_all(prices, starting_indexes, exit, |v| { v.compute_rolling_average(starting_indexes.height, window_starts, close, exit)?; Ok(()) @@ -58,7 +58,7 @@ impl Vecs { (&mut self.price_ema_200w, 200 * 7), (&mut self.price_ema_4y, 4 * 365), ] { - let window_starts = blocks.count.start_vec(period); + let window_starts = blocks.lookback.start_vec(period); ema.compute_all(prices, starting_indexes, exit, |v| { v.compute_rolling_ema(starting_indexes.height, window_starts, close, exit)?; Ok(()) diff --git a/crates/brk_computer/src/market/range/compute.rs b/crates/brk_computer/src/market/range/compute.rs index e44c1fb92..307812f65 100644 --- a/crates/brk_computer/src/market/range/compute.rs +++ b/crates/brk_computer/src/market/range/compute.rs @@ -19,22 +19,22 @@ impl Vecs { ( &mut self.price_min_1w.cents.height, &mut self.price_max_1w.cents.height, - &blocks.count.height_1w_ago, + &blocks.lookback.height_1w_ago, ), ( &mut self.price_min_2w.cents.height, &mut self.price_max_2w.cents.height, - &blocks.count.height_2w_ago, + &blocks.lookback.height_2w_ago, ), ( &mut self.price_min_1m.cents.height, &mut self.price_max_1m.cents.height, - &blocks.count.height_1m_ago, + &blocks.lookback.height_1m_ago, ), ( &mut self.price_min_1y.cents.height, &mut self.price_max_1y.cents.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, ), ] { min_vec.compute_rolling_min_from_starts( @@ -75,7 +75,7 @@ impl Vecs { // 2w rolling sum of true range self.price_true_range_sum_2w.height.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_2w_ago, + &blocks.lookback.height_2w_ago, &self.price_true_range.height, exit, )?; @@ -88,7 +88,7 @@ impl Vecs { &self.price_true_range_sum_2w.height, &self.price_max_2w.cents.height, &self.price_min_2w.cents.height, - &blocks.count.height_2w_ago, + &blocks.lookback.height_2w_ago, |(h, tr_sum, max, min, window_start, ..)| { let range = f64::from(max) - f64::from(min); let n = (h.to_usize() - window_start.to_usize() + 1) as f32; diff --git a/crates/brk_computer/src/mining/compute.rs b/crates/brk_computer/src/mining/compute.rs index 423ae01fc..76e5114ba 100644 --- a/crates/brk_computer/src/mining/compute.rs +++ b/crates/brk_computer/src/mining/compute.rs @@ -22,7 +22,7 @@ impl Vecs { self.rewards.compute( indexer, indexes, - &blocks.count, + &blocks.lookback, &transactions.fees, prices, starting_indexes, @@ -31,6 +31,7 @@ impl Vecs { self.hashrate.compute( &blocks.count, + &blocks.lookback, &blocks.difficulty, &self.rewards.coinbase.sum._24h.sats.height, &self.rewards.coinbase.sum._24h.usd.height, diff --git a/crates/brk_computer/src/mining/hashrate/compute.rs b/crates/brk_computer/src/mining/hashrate/compute.rs index 2f2cc8425..8091f213e 100644 --- a/crates/brk_computer/src/mining/hashrate/compute.rs +++ b/crates/brk_computer/src/mining/hashrate/compute.rs @@ -9,9 +9,11 @@ use crate::{ }; impl Vecs { + #[allow(clippy::too_many_arguments)] pub(crate) fn compute( &mut self, count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, difficulty_vecs: &blocks::DifficultyVecs, coinbase_sats_24h_sum: &impl ReadableVec, coinbase_usd_24h_sum: &impl ReadableVec, @@ -20,7 +22,7 @@ impl Vecs { ) -> Result<()> { self.hash_rate.height.compute_transform2( starting_indexes.height, - &count_vecs.block_count_sum._24h.height, + &count_vecs.block_count.sum._24h.height, &difficulty_vecs.as_hash.height, |(i, block_count_sum, difficulty_as_hash, ..)| { ( @@ -36,10 +38,10 @@ impl Vecs { let hash_rate = &self.hash_rate.height; for (sma, window) in [ - (&mut self.hash_rate_sma_1w.height, &count_vecs.height_1w_ago), - (&mut self.hash_rate_sma_1m.height, &count_vecs.height_1m_ago), - (&mut self.hash_rate_sma_2m.height, &count_vecs.height_2m_ago), - (&mut self.hash_rate_sma_1y.height, &count_vecs.height_1y_ago), + (&mut self.hash_rate_sma_1w.height, &lookback.height_1w_ago), + (&mut self.hash_rate_sma_1m.height, &lookback.height_1m_ago), + (&mut self.hash_rate_sma_2m.height, &lookback.height_2m_ago), + (&mut self.hash_rate_sma_1y.height, &lookback.height_1y_ago), ] { sma.compute_rolling_average(starting_indexes.height, window, hash_rate, exit)?; } diff --git a/crates/brk_computer/src/mining/rewards/compute.rs b/crates/brk_computer/src/mining/rewards/compute.rs index a72cf76cf..9418ffddc 100644 --- a/crates/brk_computer/src/mining/rewards/compute.rs +++ b/crates/brk_computer/src/mining/rewards/compute.rs @@ -12,13 +12,13 @@ impl Vecs { &mut self, indexer: &Indexer, indexes: &indexes::Vecs, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, transactions_fees: &transactions::FeesVecs, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); self.coinbase.compute( starting_indexes.height, @@ -163,7 +163,7 @@ impl Vecs { self.subsidy_sma_1y.cents.height.compute_rolling_average( starting_indexes.height, - &count_vecs.height_1y_ago, + &lookback.height_1y_ago, &self.subsidy.base.cents.height, exit, )?; diff --git a/crates/brk_computer/src/mining/rewards/import.rs b/crates/brk_computer/src/mining/rewards/import.rs index 5df1fc10a..e64bb059a 100644 --- a/crates/brk_computer/src/mining/rewards/import.rs +++ b/crates/brk_computer/src/mining/rewards/import.rs @@ -6,8 +6,8 @@ use super::Vecs; use crate::{ indexes, internal::{ - FiatFromHeight, PercentFromHeight, PercentRollingWindows, ValueFromHeightCumulative, - ValueFromHeightCumulativeSum, ValueFromHeightFull, + AmountFromHeightCumulative, AmountFromHeightCumulativeSum, AmountFromHeightFull, + FiatFromHeight, PercentFromHeight, PercentRollingWindows, }, }; @@ -18,10 +18,12 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - coinbase: ValueFromHeightCumulativeSum::forced_import(db, "coinbase", version, indexes)?, - subsidy: ValueFromHeightCumulative::forced_import(db, "subsidy", version, indexes)?, - fees: ValueFromHeightFull::forced_import(db, "fees", version, indexes)?, - unclaimed_rewards: ValueFromHeightCumulativeSum::forced_import( + coinbase: AmountFromHeightCumulativeSum::forced_import( + db, "coinbase", version, indexes, + )?, + subsidy: AmountFromHeightCumulative::forced_import(db, "subsidy", version, indexes)?, + fees: AmountFromHeightFull::forced_import(db, "fees", version, indexes)?, + unclaimed_rewards: AmountFromHeightCumulativeSum::forced_import( db, "unclaimed_rewards", version, diff --git a/crates/brk_computer/src/mining/rewards/vecs.rs b/crates/brk_computer/src/mining/rewards/vecs.rs index 9e8d15d43..6f7870806 100644 --- a/crates/brk_computer/src/mining/rewards/vecs.rs +++ b/crates/brk_computer/src/mining/rewards/vecs.rs @@ -3,16 +3,16 @@ use brk_types::{BasisPoints16, Cents}; use vecdb::{Rw, StorageMode}; use crate::internal::{ - FiatFromHeight, PercentFromHeight, PercentRollingWindows, ValueFromHeightCumulative, - ValueFromHeightCumulativeSum, ValueFromHeightFull, + AmountFromHeightCumulative, AmountFromHeightCumulativeSum, AmountFromHeightFull, + FiatFromHeight, PercentFromHeight, PercentRollingWindows, }; #[derive(Traversable)] pub struct Vecs { - pub coinbase: ValueFromHeightCumulativeSum, - pub subsidy: ValueFromHeightCumulative, - pub fees: ValueFromHeightFull, - pub unclaimed_rewards: ValueFromHeightCumulativeSum, + pub coinbase: AmountFromHeightCumulativeSum, + pub subsidy: AmountFromHeightCumulative, + pub fees: AmountFromHeightFull, + pub unclaimed_rewards: AmountFromHeightCumulativeSum, pub fee_dominance: PercentFromHeight, pub fee_dominance_rolling: PercentRollingWindows, pub subsidy_dominance: PercentFromHeight, diff --git a/crates/brk_computer/src/outputs/count/compute.rs b/crates/brk_computer/src/outputs/count/compute.rs index 68cf7d35c..115c351d6 100644 --- a/crates/brk_computer/src/outputs/count/compute.rs +++ b/crates/brk_computer/src/outputs/count/compute.rs @@ -18,7 +18,7 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.total_count .compute(starting_indexes.height, &window_starts, exit, |full| { full.compute_with_skip( diff --git a/crates/brk_computer/src/pools/major.rs b/crates/brk_computer/src/pools/major.rs index 7eeeb06ae..0cce4fcc6 100644 --- a/crates/brk_computer/src/pools/major.rs +++ b/crates/brk_computer/src/pools/major.rs @@ -7,8 +7,8 @@ use vecdb::{BinaryTransform, Database, Exit, ReadableVec, Rw, StorageMode, Versi use crate::{ blocks, indexes, internal::{ - MaskSats, PercentRollingWindows, RatioU32Bp16, RollingWindows, - ValueFromHeightCumulativeSum, + AmountFromHeightCumulativeSum, MaskSats, PercentRollingWindows, RatioU32Bp16, + RollingWindows, }, mining, prices, }; @@ -25,7 +25,7 @@ pub struct Vecs { #[traversable(wrap = "blocks_mined", rename = "sum")] pub blocks_mined_sum: RollingWindows, - pub rewards: ValueFromHeightCumulativeSum, + pub rewards: AmountFromHeightCumulativeSum, pub dominance_rolling: PercentRollingWindows, } @@ -44,7 +44,7 @@ impl Vecs { RollingWindows::forced_import(db, &suffix("blocks_mined"), version, indexes)?; let rewards = - ValueFromHeightCumulativeSum::forced_import(db, &suffix("rewards"), version, indexes)?; + AmountFromHeightCumulativeSum::forced_import(db, &suffix("rewards"), version, indexes)?; let dominance_rolling = PercentRollingWindows::forced_import(db, &suffix("dominance"), version, indexes)?; @@ -70,7 +70,7 @@ impl Vecs { self.base .compute(starting_indexes, height_to_pool, blocks, exit)?; - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.blocks_mined_sum.compute_rolling_sum( starting_indexes.height, @@ -79,17 +79,12 @@ impl Vecs { exit, )?; - for (dom, (mined, total)) in self - .dominance_rolling - .as_mut_array() - .into_iter() - .zip( - self.blocks_mined_sum - .as_array() - .into_iter() - .zip(blocks.count.block_count_sum.as_array()), - ) - { + for (dom, (mined, total)) in self.dominance_rolling.as_mut_array().into_iter().zip( + self.blocks_mined_sum + .as_array() + .into_iter() + .zip(blocks.count.block_count.sum.as_array()), + ) { dom.compute_binary::( starting_indexes.height, &mined.height, diff --git a/crates/brk_computer/src/scripts/compute.rs b/crates/brk_computer/src/scripts/compute.rs index 3707c5093..2a71f94ce 100644 --- a/crates/brk_computer/src/scripts/compute.rs +++ b/crates/brk_computer/src/scripts/compute.rs @@ -18,7 +18,7 @@ impl Vecs { exit: &Exit, ) -> Result<()> { self.count - .compute(indexer, &blocks.count, starting_indexes, exit)?; + .compute(indexer, &blocks.lookback, starting_indexes, exit)?; self.value .compute(indexer, prices, starting_indexes, exit)?; diff --git a/crates/brk_computer/src/scripts/count/compute.rs b/crates/brk_computer/src/scripts/count/compute.rs index 917fa19f0..2dbe024b6 100644 --- a/crates/brk_computer/src/scripts/count/compute.rs +++ b/crates/brk_computer/src/scripts/count/compute.rs @@ -10,11 +10,11 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); self.p2a .compute(starting_indexes.height, &window_starts, exit, |v| { diff --git a/crates/brk_computer/src/scripts/value/import.rs b/crates/brk_computer/src/scripts/value/import.rs index e37a88849..d7aa70865 100644 --- a/crates/brk_computer/src/scripts/value/import.rs +++ b/crates/brk_computer/src/scripts/value/import.rs @@ -3,7 +3,7 @@ use brk_types::Version; use vecdb::Database; use super::Vecs; -use crate::{indexes, internal::ValueFromHeightCumulative}; +use crate::{indexes, internal::AmountFromHeightCumulative}; impl Vecs { pub(crate) fn forced_import( @@ -12,7 +12,7 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - opreturn: ValueFromHeightCumulative::forced_import( + opreturn: AmountFromHeightCumulative::forced_import( db, "opreturn_value", version, diff --git a/crates/brk_computer/src/scripts/value/vecs.rs b/crates/brk_computer/src/scripts/value/vecs.rs index 76602cf7e..3fed2eccd 100644 --- a/crates/brk_computer/src/scripts/value/vecs.rs +++ b/crates/brk_computer/src/scripts/value/vecs.rs @@ -1,9 +1,9 @@ use brk_traversable::Traversable; use vecdb::{Rw, StorageMode}; -use crate::internal::ValueFromHeightCumulative; +use crate::internal::AmountFromHeightCumulative; #[derive(Traversable)] pub struct Vecs { - pub opreturn: ValueFromHeightCumulative, + pub opreturn: AmountFromHeightCumulative, } diff --git a/crates/brk_computer/src/supply/burned/compute.rs b/crates/brk_computer/src/supply/burned/compute.rs index 991d194d4..08ee78c53 100644 --- a/crates/brk_computer/src/supply/burned/compute.rs +++ b/crates/brk_computer/src/supply/burned/compute.rs @@ -10,12 +10,12 @@ impl Vecs { &mut self, scripts: &scripts::Vecs, mining: &mining::Vecs, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); // 1. Compute opreturn supply - copy per-block opreturn values from scripts self.opreturn.compute( diff --git a/crates/brk_computer/src/supply/burned/import.rs b/crates/brk_computer/src/supply/burned/import.rs index d884c5795..e96a03669 100644 --- a/crates/brk_computer/src/supply/burned/import.rs +++ b/crates/brk_computer/src/supply/burned/import.rs @@ -3,7 +3,7 @@ use brk_types::Version; use vecdb::Database; use super::Vecs; -use crate::{indexes, internal::ValueFromHeightCumulativeSum}; +use crate::{indexes, internal::AmountFromHeightCumulativeSum}; impl Vecs { pub(crate) fn forced_import( @@ -12,13 +12,13 @@ impl Vecs { indexes: &indexes::Vecs, ) -> Result { Ok(Self { - opreturn: ValueFromHeightCumulativeSum::forced_import( + opreturn: AmountFromHeightCumulativeSum::forced_import( db, "opreturn_supply", version, indexes, )?, - unspendable: ValueFromHeightCumulativeSum::forced_import( + unspendable: AmountFromHeightCumulativeSum::forced_import( db, "unspendable_supply", version, diff --git a/crates/brk_computer/src/supply/burned/vecs.rs b/crates/brk_computer/src/supply/burned/vecs.rs index acb0a43ec..b055f639d 100644 --- a/crates/brk_computer/src/supply/burned/vecs.rs +++ b/crates/brk_computer/src/supply/burned/vecs.rs @@ -1,10 +1,10 @@ use brk_traversable::Traversable; use vecdb::{Rw, StorageMode}; -use crate::internal::ValueFromHeightCumulativeSum; +use crate::internal::AmountFromHeightCumulativeSum; #[derive(Traversable)] pub struct Vecs { - pub opreturn: ValueFromHeightCumulativeSum, - pub unspendable: ValueFromHeightCumulativeSum, + pub opreturn: AmountFromHeightCumulativeSum, + pub unspendable: AmountFromHeightCumulativeSum, } diff --git a/crates/brk_computer/src/supply/compute.rs b/crates/brk_computer/src/supply/compute.rs index 61b750658..03a237f4f 100644 --- a/crates/brk_computer/src/supply/compute.rs +++ b/crates/brk_computer/src/supply/compute.rs @@ -22,7 +22,7 @@ impl Vecs { self.burned.compute( scripts, mining, - &blocks.count, + &blocks.lookback, prices, starting_indexes, exit, @@ -35,7 +35,7 @@ impl Vecs { .height .compute_rolling_ratio_change( starting_indexes.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, &circulating_supply.height, exit, )?; @@ -45,7 +45,7 @@ impl Vecs { .compute(blocks, transactions, distribution, starting_indexes, exit)?; // 4. Compute market cap delta (change + rate across 4 windows) - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.market_cap_delta.compute( starting_indexes.height, diff --git a/crates/brk_computer/src/supply/import.rs b/crates/brk_computer/src/supply/import.rs index c0702cd1d..9c538a017 100644 --- a/crates/brk_computer/src/supply/import.rs +++ b/crates/brk_computer/src/supply/import.rs @@ -6,7 +6,7 @@ use brk_types::{Cents, Dollars, Sats, Version}; use crate::{ distribution, indexes, internal::{ - FiatRollingDelta, Identity, LazyFiatFromHeight, LazyValueFromHeight, PercentFromHeight, + FiatRollingDelta, Identity, LazyFiatFromHeight, LazyAmountFromHeight, PercentFromHeight, RollingWindows, SatsToBitcoin, finalize_db, open_db, }, }; @@ -28,7 +28,7 @@ impl Vecs { let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply; // Circulating supply - lazy refs to distribution - let circulating = LazyValueFromHeight::from_block_source::< + let circulating = LazyAmountFromHeight::from_block_source::< Identity, SatsToBitcoin, Identity, diff --git a/crates/brk_computer/src/supply/vecs.rs b/crates/brk_computer/src/supply/vecs.rs index 1de061832..a32ab25cb 100644 --- a/crates/brk_computer/src/supply/vecs.rs +++ b/crates/brk_computer/src/supply/vecs.rs @@ -4,7 +4,7 @@ use vecdb::{Database, Rw, StorageMode}; use super::{burned, velocity}; use crate::internal::{ - FiatRollingDelta, LazyFiatFromHeight, LazyValueFromHeight, PercentFromHeight, RollingWindows, + FiatRollingDelta, LazyFiatFromHeight, LazyAmountFromHeight, PercentFromHeight, RollingWindows, }; #[derive(Traversable)] @@ -12,7 +12,7 @@ pub struct Vecs { #[traversable(skip)] pub(crate) db: Database, - pub circulating: LazyValueFromHeight, + pub circulating: LazyAmountFromHeight, pub burned: burned::Vecs, pub inflation_rate: PercentFromHeight, pub velocity: velocity::Vecs, diff --git a/crates/brk_computer/src/supply/velocity/compute.rs b/crates/brk_computer/src/supply/velocity/compute.rs index 255bd5173..a9d4cdf69 100644 --- a/crates/brk_computer/src/supply/velocity/compute.rs +++ b/crates/brk_computer/src/supply/velocity/compute.rs @@ -20,7 +20,7 @@ impl Vecs { // BTC velocity at height level self.btc.height.compute_rolling_ratio( starting_indexes.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, &transactions.volume.sent_sum.sats, &circulating_supply.sats.height, exit, @@ -29,7 +29,7 @@ impl Vecs { // USD velocity at height level self.usd.height.compute_rolling_ratio( starting_indexes.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, &transactions.volume.sent_sum.usd, &circulating_supply.usd.height, exit, diff --git a/crates/brk_computer/src/transactions/compute.rs b/crates/brk_computer/src/transactions/compute.rs index 8e48fe9d1..de455c4b9 100644 --- a/crates/brk_computer/src/transactions/compute.rs +++ b/crates/brk_computer/src/transactions/compute.rs @@ -22,11 +22,11 @@ impl Vecs { ) -> Result<()> { // Count computes first self.count - .compute(indexer, &blocks.count, starting_indexes, exit)?; + .compute(indexer, &blocks.lookback, starting_indexes, exit)?; // Versions depends on count self.versions - .compute(indexer, &blocks.count, starting_indexes, exit)?; + .compute(indexer, &blocks.lookback, starting_indexes, exit)?; // Size computes next (uses 6-block rolling window) self.size diff --git a/crates/brk_computer/src/transactions/count/compute.rs b/crates/brk_computer/src/transactions/count/compute.rs index de713c993..f7dfd1a7b 100644 --- a/crates/brk_computer/src/transactions/count/compute.rs +++ b/crates/brk_computer/src/transactions/count/compute.rs @@ -10,11 +10,11 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); self.tx_count .compute(starting_indexes.height, &window_starts, exit, |height| { Ok(height.compute_count_from_indexes( diff --git a/crates/brk_computer/src/transactions/versions/compute.rs b/crates/brk_computer/src/transactions/versions/compute.rs index ed2220976..890608ff8 100644 --- a/crates/brk_computer/src/transactions/versions/compute.rs +++ b/crates/brk_computer/src/transactions/versions/compute.rs @@ -10,11 +10,11 @@ impl Vecs { pub(crate) fn compute( &mut self, indexer: &Indexer, - count_vecs: &blocks::CountVecs, + lookback: &blocks::LookbackVecs, starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = count_vecs.window_starts(); + let window_starts = lookback.window_starts(); let tx_vany = |tx_vany: &mut ComputedFromHeightCumulativeSum, txversion: TxVersion| { diff --git a/crates/brk_computer/src/transactions/volume/compute.rs b/crates/brk_computer/src/transactions/volume/compute.rs index b9df488b2..9228210c3 100644 --- a/crates/brk_computer/src/transactions/volume/compute.rs +++ b/crates/brk_computer/src/transactions/volume/compute.rs @@ -22,7 +22,7 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let window_starts = blocks.count.window_starts(); + let window_starts = blocks.lookback.window_starts(); self.sent_sum.compute( starting_indexes.height, @@ -60,7 +60,7 @@ impl Vecs { // Annualized volume: rolling 1y sum of per-block sent volume self.annualized_volume.sats.height.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_1y_ago, + &blocks.lookback.height_1y_ago, &self.sent_sum.sats, exit, )?; diff --git a/crates/brk_computer/src/transactions/volume/import.rs b/crates/brk_computer/src/transactions/volume/import.rs index 96306bba8..f09f483b6 100644 --- a/crates/brk_computer/src/transactions/volume/import.rs +++ b/crates/brk_computer/src/transactions/volume/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{ComputedFromHeight, ValueFromHeight, ValueFromHeightRolling}, + internal::{AmountFromHeight, AmountFromHeightRolling, ComputedFromHeight}, }; impl Vecs { @@ -16,14 +16,14 @@ impl Vecs { ) -> Result { let v2 = Version::TWO; Ok(Self { - sent_sum: ValueFromHeightRolling::forced_import(db, "sent_sum", version, indexes)?, - received_sum: ValueFromHeightRolling::forced_import( + sent_sum: AmountFromHeightRolling::forced_import(db, "sent_sum", version, indexes)?, + received_sum: AmountFromHeightRolling::forced_import( db, "received_sum", version, indexes, )?, - annualized_volume: ValueFromHeight::forced_import( + annualized_volume: AmountFromHeight::forced_import( db, "annualized_volume", version, diff --git a/crates/brk_computer/src/transactions/volume/vecs.rs b/crates/brk_computer/src/transactions/volume/vecs.rs index 586a4d6df..582034b0b 100644 --- a/crates/brk_computer/src/transactions/volume/vecs.rs +++ b/crates/brk_computer/src/transactions/volume/vecs.rs @@ -2,13 +2,13 @@ use brk_traversable::Traversable; use brk_types::StoredF32; use vecdb::{Rw, StorageMode}; -use crate::internal::{ComputedFromHeight, ValueFromHeight, ValueFromHeightRolling}; +use crate::internal::{AmountFromHeight, AmountFromHeightRolling, ComputedFromHeight}; #[derive(Traversable)] pub struct Vecs { - pub sent_sum: ValueFromHeightRolling, - pub received_sum: ValueFromHeightRolling, - pub annualized_volume: ValueFromHeight, + pub sent_sum: AmountFromHeightRolling, + pub received_sum: AmountFromHeightRolling, + pub annualized_volume: AmountFromHeight, pub tx_per_sec: ComputedFromHeight, pub outputs_per_sec: ComputedFromHeight, pub inputs_per_sec: ComputedFromHeight, diff --git a/crates/brk_indexer/src/vecs/blocks.rs b/crates/brk_indexer/src/vecs/blocks.rs index 3235bfa0c..61ad62928 100644 --- a/crates/brk_indexer/src/vecs/blocks.rs +++ b/crates/brk_indexer/src/vecs/blocks.rs @@ -16,6 +16,7 @@ pub struct BlocksVecs { /// Doesn't guarantee continuity due to possible reorgs and more generally the nature of mining #[traversable(wrap = "time")] pub timestamp: M::Stored>, + #[traversable(wrap = "size")] pub total_size: M::Stored>, pub weight: M::Stored>, }