From 35df8d99dc5ac98b7e35e783ec7f1cd26d07a5d2 Mon Sep 17 00:00:00 2001 From: nym21 Date: Tue, 3 Mar 2026 00:23:19 +0100 Subject: [PATCH] global: snapshot --- .../brk_computer/src/blocks/count/compute.rs | 4 +- .../distribution/metrics/realized/adjusted.rs | 204 +++---------- .../src/distribution/metrics/realized/base.rs | 283 ++++-------------- .../distribution/metrics/realized/extended.rs | 134 ++------- .../from_height/by_unit/rolling_full.rs | 11 +- .../from_height/by_unit/rolling_sum.rs | 4 +- .../internal/from_height/by_unit/windows.rs | 7 +- .../src/internal/rolling/value_windows.rs | 31 +- .../src/internal/rolling/windows.rs | 26 +- crates/brk_computer/src/internal/windows.rs | 17 ++ .../src/mining/rewards/compute.rs | 75 ++--- .../brk_computer/src/mining/rewards/import.rs | 46 +-- .../brk_computer/src/mining/rewards/vecs.rs | 12 +- crates/brk_computer/src/pools/vecs.rs | 124 ++------ 14 files changed, 224 insertions(+), 754 deletions(-) diff --git a/crates/brk_computer/src/blocks/count/compute.rs b/crates/brk_computer/src/blocks/count/compute.rs index 3e352681e..39be456eb 100644 --- a/crates/brk_computer/src/blocks/count/compute.rs +++ b/crates/brk_computer/src/blocks/count/compute.rs @@ -3,7 +3,7 @@ use brk_indexer::Indexer; use brk_types::{Height, StoredU32, Timestamp}; use vecdb::{AnyVec, Cursor, EagerVec, Exit, PcoVec, ReadableVec, VecIndex}; -use crate::ComputeIndexes; +use crate::{internal::WindowStarts, ComputeIndexes}; use super::{super::time, Vecs}; @@ -160,7 +160,7 @@ impl Vecs { })?; // Compute rolling window block counts (both block_count's own rolling + separate block_count_sum) - let ws = crate::internal::WindowStarts { + let ws = WindowStarts { _24h: &self.height_24h_ago, _7d: &self.height_1w_ago, _30d: &self.height_1m_ago, diff --git a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs index 2f15fa292..e7d0ba3d0 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs @@ -5,7 +5,7 @@ use vecdb::{Exit, Ident, ReadableCloneableVec, ReadableVec, Rw, StorageMode}; use crate::{ ComputeIndexes, blocks, - internal::{ComputedFromHeight, LazyFromHeight, RatioCents64}, + internal::{ComputedFromHeight, LazyFromHeight, RatioCents64, RollingWindows}, }; use crate::distribution::metrics::ImportConfig; @@ -18,21 +18,11 @@ pub struct RealizedAdjusted { pub adjusted_value_destroyed: ComputedFromHeight, // === Adjusted Value Created/Destroyed Rolling Sums === - pub adjusted_value_created_24h: ComputedFromHeight, - pub adjusted_value_created_7d: ComputedFromHeight, - pub adjusted_value_created_30d: ComputedFromHeight, - pub adjusted_value_created_1y: ComputedFromHeight, - pub adjusted_value_destroyed_24h: ComputedFromHeight, - pub adjusted_value_destroyed_7d: ComputedFromHeight, - pub adjusted_value_destroyed_30d: ComputedFromHeight, - pub adjusted_value_destroyed_1y: ComputedFromHeight, + pub adjusted_value_created_sum: RollingWindows, + pub adjusted_value_destroyed_sum: RollingWindows, // === Adjusted SOPR (rolling window ratios) === - pub adjusted_sopr: LazyFromHeight, - pub adjusted_sopr_24h: ComputedFromHeight, - pub adjusted_sopr_7d: ComputedFromHeight, - pub adjusted_sopr_30d: ComputedFromHeight, - pub adjusted_sopr_1y: ComputedFromHeight, + pub adjusted_sopr: RollingWindows, pub adjusted_sopr_24h_7d_ema: ComputedFromHeight, pub adjusted_sopr_7d_ema: LazyFromHeight, pub adjusted_sopr_24h_30d_ema: ComputedFromHeight, @@ -43,17 +33,6 @@ impl RealizedAdjusted { pub(crate) fn forced_import(cfg: &ImportConfig) -> Result { let v1 = Version::ONE; - macro_rules! import_rolling { - ($name:expr) => { - ComputedFromHeight::forced_import( - cfg.db, - &cfg.name($name), - cfg.version + v1, - cfg.indexes, - )? - }; - } - let adjusted_value_created = ComputedFromHeight::forced_import( cfg.db, &cfg.name("adjusted_value_created"), @@ -67,54 +46,35 @@ impl RealizedAdjusted { cfg.indexes, )?; - let adjusted_value_created_24h = import_rolling!("adjusted_value_created_24h"); - let adjusted_value_created_7d = import_rolling!("adjusted_value_created_7d"); - let adjusted_value_created_30d = import_rolling!("adjusted_value_created_30d"); - let adjusted_value_created_1y = import_rolling!("adjusted_value_created_1y"); - let adjusted_value_destroyed_24h = import_rolling!("adjusted_value_destroyed_24h"); - let adjusted_value_destroyed_7d = import_rolling!("adjusted_value_destroyed_7d"); - let adjusted_value_destroyed_30d = import_rolling!("adjusted_value_destroyed_30d"); - let adjusted_value_destroyed_1y = import_rolling!("adjusted_value_destroyed_1y"); + let adjusted_value_created_sum = RollingWindows::forced_import( + cfg.db, &cfg.name("adjusted_value_created"), cfg.version + v1, cfg.indexes, + )?; + let adjusted_value_destroyed_sum = RollingWindows::forced_import( + cfg.db, &cfg.name("adjusted_value_destroyed"), cfg.version + v1, cfg.indexes, + )?; + let adjusted_sopr = RollingWindows::forced_import( + cfg.db, &cfg.name("adjusted_sopr"), cfg.version + v1, cfg.indexes, + )?; - let adjusted_sopr_24h = ComputedFromHeight::forced_import( - cfg.db, - &cfg.name("adjusted_sopr_24h"), - cfg.version + v1, - cfg.indexes, - )?; - let adjusted_sopr_7d = ComputedFromHeight::forced_import( - cfg.db, - &cfg.name("adjusted_sopr_7d"), - cfg.version + v1, - cfg.indexes, - )?; - let adjusted_sopr_30d = ComputedFromHeight::forced_import( - cfg.db, - &cfg.name("adjusted_sopr_30d"), - cfg.version + v1, - cfg.indexes, - )?; - let adjusted_sopr_1y = ComputedFromHeight::forced_import( - cfg.db, - &cfg.name("adjusted_sopr_1y"), - cfg.version + v1, - cfg.indexes, - )?; - let adjusted_sopr = LazyFromHeight::from_computed::( - &cfg.name("adjusted_sopr"), - cfg.version + v1, - adjusted_sopr_24h.height.read_only_boxed_clone(), - &adjusted_sopr_24h, - ); + macro_rules! import_computed { + ($name:expr) => { + ComputedFromHeight::forced_import( + cfg.db, + &cfg.name($name), + cfg.version + v1, + cfg.indexes, + )? + }; + } - let adjusted_sopr_24h_7d_ema = import_rolling!("adjusted_sopr_24h_7d_ema"); + let adjusted_sopr_24h_7d_ema = import_computed!("adjusted_sopr_24h_7d_ema"); let adjusted_sopr_7d_ema = LazyFromHeight::from_computed::( &cfg.name("adjusted_sopr_7d_ema"), cfg.version + v1, adjusted_sopr_24h_7d_ema.height.read_only_boxed_clone(), &adjusted_sopr_24h_7d_ema, ); - let adjusted_sopr_24h_30d_ema = import_rolling!("adjusted_sopr_24h_30d_ema"); + let adjusted_sopr_24h_30d_ema = import_computed!("adjusted_sopr_24h_30d_ema"); let adjusted_sopr_30d_ema = LazyFromHeight::from_computed::( &cfg.name("adjusted_sopr_30d_ema"), cfg.version + v1, @@ -125,19 +85,9 @@ impl RealizedAdjusted { Ok(RealizedAdjusted { adjusted_value_created, adjusted_value_destroyed, - adjusted_value_created_24h, - adjusted_value_created_7d, - adjusted_value_created_30d, - adjusted_value_created_1y, - adjusted_value_destroyed_24h, - adjusted_value_destroyed_7d, - adjusted_value_destroyed_30d, - adjusted_value_destroyed_1y, + adjusted_value_created_sum, + adjusted_value_destroyed_sum, adjusted_sopr, - adjusted_sopr_24h, - adjusted_sopr_7d, - adjusted_sopr_30d, - adjusted_sopr_1y, adjusted_sopr_24h_7d_ema, adjusted_sopr_7d_ema, adjusted_sopr_24h_30d_ema, @@ -171,101 +121,31 @@ impl RealizedAdjusted { )?; // Adjusted value created/destroyed rolling sums - self.adjusted_value_created_24h.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_24h_ago, - &self.adjusted_value_created.height, - exit, + let window_starts = blocks.count.window_starts(); + self.adjusted_value_created_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &self.adjusted_value_created.height, exit, )?; - self.adjusted_value_created_7d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1w_ago, - &self.adjusted_value_created.height, - exit, + self.adjusted_value_destroyed_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &self.adjusted_value_destroyed.height, exit, )?; - self.adjusted_value_created_30d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1m_ago, - &self.adjusted_value_created.height, - exit, - )?; - self.adjusted_value_created_1y.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1y_ago, - &self.adjusted_value_created.height, - exit, - )?; - - self.adjusted_value_destroyed_24h - .height - .compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_24h_ago, - &self.adjusted_value_destroyed.height, - exit, - )?; - self.adjusted_value_destroyed_7d - .height - .compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1w_ago, - &self.adjusted_value_destroyed.height, - exit, - )?; - self.adjusted_value_destroyed_30d - .height - .compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1m_ago, - &self.adjusted_value_destroyed.height, - exit, - )?; - self.adjusted_value_destroyed_1y - .height - .compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1y_ago, - &self.adjusted_value_destroyed.height, - exit, - )?; // SOPR ratios from rolling sums - self.adjusted_sopr_24h - .compute_binary::( - starting_indexes.height, - &self.adjusted_value_created_24h.height, - &self.adjusted_value_destroyed_24h.height, - exit, - )?; - self.adjusted_sopr_7d - .compute_binary::( - starting_indexes.height, - &self.adjusted_value_created_7d.height, - &self.adjusted_value_destroyed_7d.height, - exit, - )?; - self.adjusted_sopr_30d - .compute_binary::( - starting_indexes.height, - &self.adjusted_value_created_30d.height, - &self.adjusted_value_destroyed_30d.height, - exit, - )?; - self.adjusted_sopr_1y - .compute_binary::( - starting_indexes.height, - &self.adjusted_value_created_1y.height, - &self.adjusted_value_destroyed_1y.height, - exit, + for ((sopr, vc), vd) in self.adjusted_sopr.as_mut_array().into_iter() + .zip(self.adjusted_value_created_sum.as_array()) + .zip(self.adjusted_value_destroyed_sum.as_array()) + { + sopr.compute_binary::( + starting_indexes.height, &vc.height, &vd.height, exit, )?; + } - // Adjusted SOPR EMAs + // Adjusted SOPR EMAs (based on 24h window) self.adjusted_sopr_24h_7d_ema .height .compute_rolling_ema( starting_indexes.height, &blocks.count.height_1w_ago, - &self.adjusted_sopr.height, + &self.adjusted_sopr._24h.height, exit, )?; self.adjusted_sopr_24h_30d_ema @@ -273,7 +153,7 @@ impl RealizedAdjusted { .compute_rolling_ema( starting_indexes.height, &blocks.count.height_1m_ago, - &self.adjusted_sopr.height, + &self.adjusted_sopr._24h.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/base.rs b/crates/brk_computer/src/distribution/metrics/realized/base.rs index 373aa96c2..1cf774bed 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/base.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/base.rs @@ -15,7 +15,7 @@ use crate::{ CentsPlus, CentsUnsignedToDollars, ComputedFromHeightCumulative, ComputedFromHeight, ComputedFromHeightRatio, NegCentsUnsignedToDollars, ValueFromHeightCumulative, LazyFromHeight, PercentageCentsF32, PercentageCentsSignedCentsF32, PercentageCentsSignedDollarsF32, Price, RatioCents64, - Identity, ValueFromHeight, + RollingWindows, Identity, ValueFromHeight, }, prices, }; @@ -80,38 +80,19 @@ pub struct RealizedBase { pub profit_flow: LazyFromHeight, // === Value Created/Destroyed Rolling Sums === - pub value_created_24h: ComputedFromHeight, - pub value_created_7d: ComputedFromHeight, - pub value_created_30d: ComputedFromHeight, - pub value_created_1y: ComputedFromHeight, - pub value_destroyed_24h: ComputedFromHeight, - pub value_destroyed_7d: ComputedFromHeight, - pub value_destroyed_30d: ComputedFromHeight, - pub value_destroyed_1y: ComputedFromHeight, + pub value_created_sum: RollingWindows, + pub value_destroyed_sum: RollingWindows, // === SOPR (rolling window ratios) === - pub sopr: LazyFromHeight, - pub sopr_24h: ComputedFromHeight, - pub sopr_7d: ComputedFromHeight, - pub sopr_30d: ComputedFromHeight, - pub sopr_1y: ComputedFromHeight, + pub sopr: RollingWindows, pub sopr_24h_7d_ema: ComputedFromHeight, pub sopr_7d_ema: LazyFromHeight, pub sopr_24h_30d_ema: ComputedFromHeight, pub sopr_30d_ema: LazyFromHeight, - // === Sell Side Risk Rolling Sum Intermediates === - pub realized_value_24h: ComputedFromHeight, - pub realized_value_7d: ComputedFromHeight, - pub realized_value_30d: ComputedFromHeight, - pub realized_value_1y: ComputedFromHeight, - - // === Sell Side Risk (rolling window ratios) === - pub sell_side_risk_ratio: LazyFromHeight, - pub sell_side_risk_ratio_24h: ComputedFromHeight, - pub sell_side_risk_ratio_7d: ComputedFromHeight, - pub sell_side_risk_ratio_30d: ComputedFromHeight, - pub sell_side_risk_ratio_1y: ComputedFromHeight, + // === Sell Side Risk === + pub realized_value_sum: RollingWindows, + pub sell_side_risk_ratio: RollingWindows, pub sell_side_risk_ratio_24h_7d_ema: ComputedFromHeight, pub sell_side_risk_ratio_7d_ema: LazyFromHeight, pub sell_side_risk_ratio_24h_30d_ema: ComputedFromHeight, @@ -351,8 +332,25 @@ impl RealizedBase { &realized_price_extra.ratio, ); - // === Rolling sum intermediates === - macro_rules! import_rolling { + // === Rolling windows === + let value_created_sum = RollingWindows::forced_import( + cfg.db, &cfg.name("value_created"), cfg.version + v1, cfg.indexes, + )?; + let value_destroyed_sum = RollingWindows::forced_import( + cfg.db, &cfg.name("value_destroyed"), cfg.version + v1, cfg.indexes, + )?; + let realized_value_sum = RollingWindows::forced_import( + cfg.db, &cfg.name("realized_value"), cfg.version + v1, cfg.indexes, + )?; + let sopr = RollingWindows::forced_import( + cfg.db, &cfg.name("sopr"), cfg.version + v1, cfg.indexes, + )?; + let sell_side_risk_ratio = RollingWindows::forced_import( + cfg.db, &cfg.name("sell_side_risk_ratio"), cfg.version + v1, cfg.indexes, + )?; + + // === EMA imports + identity aliases === + macro_rules! import_computed { ($name:expr) => { ComputedFromHeight::forced_import( cfg.db, @@ -363,52 +361,14 @@ impl RealizedBase { }; } - let value_created_24h = import_rolling!("value_created_24h"); - let value_created_7d = import_rolling!("value_created_7d"); - let value_created_30d = import_rolling!("value_created_30d"); - let value_created_1y = import_rolling!("value_created_1y"); - let value_destroyed_24h = import_rolling!("value_destroyed_24h"); - let value_destroyed_7d = import_rolling!("value_destroyed_7d"); - let value_destroyed_30d = import_rolling!("value_destroyed_30d"); - let value_destroyed_1y = import_rolling!("value_destroyed_1y"); - - let realized_value_24h = import_rolling!("realized_value_24h"); - let realized_value_7d = import_rolling!("realized_value_7d"); - let realized_value_30d = import_rolling!("realized_value_30d"); - let realized_value_1y = import_rolling!("realized_value_1y"); - - // === Rolling window stored ratios === - let sopr_24h = import_rolling!("sopr_24h"); - let sopr_7d = import_rolling!("sopr_7d"); - let sopr_30d = import_rolling!("sopr_30d"); - let sopr_1y = import_rolling!("sopr_1y"); - let sopr = LazyFromHeight::from_computed::( - &cfg.name("sopr"), - cfg.version + v1, - sopr_24h.height.read_only_boxed_clone(), - &sopr_24h, - ); - - let sell_side_risk_ratio_24h = import_rolling!("sell_side_risk_ratio_24h"); - let sell_side_risk_ratio_7d = import_rolling!("sell_side_risk_ratio_7d"); - let sell_side_risk_ratio_30d = import_rolling!("sell_side_risk_ratio_30d"); - let sell_side_risk_ratio_1y = import_rolling!("sell_side_risk_ratio_1y"); - let sell_side_risk_ratio = LazyFromHeight::from_computed::( - &cfg.name("sell_side_risk_ratio"), - cfg.version + v1, - sell_side_risk_ratio_24h.height.read_only_boxed_clone(), - &sell_side_risk_ratio_24h, - ); - - // === EMA imports + identity aliases === - let sopr_24h_7d_ema = import_rolling!("sopr_24h_7d_ema"); + let sopr_24h_7d_ema = import_computed!("sopr_24h_7d_ema"); let sopr_7d_ema = LazyFromHeight::from_computed::( &cfg.name("sopr_7d_ema"), cfg.version + v1, sopr_24h_7d_ema.height.read_only_boxed_clone(), &sopr_24h_7d_ema, ); - let sopr_24h_30d_ema = import_rolling!("sopr_24h_30d_ema"); + let sopr_24h_30d_ema = import_computed!("sopr_24h_30d_ema"); let sopr_30d_ema = LazyFromHeight::from_computed::( &cfg.name("sopr_30d_ema"), cfg.version + v1, @@ -416,7 +376,7 @@ impl RealizedBase { &sopr_24h_30d_ema, ); - let sell_side_risk_ratio_24h_7d_ema = import_rolling!("sell_side_risk_ratio_24h_7d_ema"); + let sell_side_risk_ratio_24h_7d_ema = import_computed!("sell_side_risk_ratio_24h_7d_ema"); let sell_side_risk_ratio_7d_ema = LazyFromHeight::from_computed::( &cfg.name("sell_side_risk_ratio_7d_ema"), cfg.version + v1, @@ -425,7 +385,7 @@ impl RealizedBase { .read_only_boxed_clone(), &sell_side_risk_ratio_24h_7d_ema, ); - let sell_side_risk_ratio_24h_30d_ema = import_rolling!("sell_side_risk_ratio_24h_30d_ema"); + let sell_side_risk_ratio_24h_30d_ema = import_computed!("sell_side_risk_ratio_24h_30d_ema"); let sell_side_risk_ratio_30d_ema = LazyFromHeight::from_computed::( &cfg.name("sell_side_risk_ratio_30d_ema"), cfg.version + v1, @@ -480,32 +440,15 @@ impl RealizedBase { value_destroyed, capitulation_flow, profit_flow, - value_created_24h, - value_created_7d, - value_created_30d, - value_created_1y, - value_destroyed_24h, - value_destroyed_7d, - value_destroyed_30d, - value_destroyed_1y, + value_created_sum, + value_destroyed_sum, sopr, - sopr_24h, - sopr_7d, - sopr_30d, - sopr_1y, sopr_24h_7d_ema, sopr_7d_ema, sopr_24h_30d_ema, sopr_30d_ema, - realized_value_24h, - realized_value_7d, - realized_value_30d, - realized_value_1y, + realized_value_sum, sell_side_risk_ratio, - sell_side_risk_ratio_24h, - sell_side_risk_ratio_7d, - sell_side_risk_ratio_30d, - sell_side_risk_ratio_1y, sell_side_risk_ratio_24h_7d_ema, sell_side_risk_ratio_7d_ema, sell_side_risk_ratio_24h_30d_ema, @@ -927,135 +870,35 @@ impl RealizedBase { )?; // === Rolling sum intermediates === - macro_rules! rolling_sum { - ($target:expr, $window:expr, $source:expr) => { - $target.height.compute_rolling_sum( - starting_indexes.height, - $window, - $source, - exit, - )? - }; - } - - rolling_sum!( - self.value_created_24h, - &blocks.count.height_24h_ago, - &self.value_created.height - ); - rolling_sum!( - self.value_created_7d, - &blocks.count.height_1w_ago, - &self.value_created.height - ); - rolling_sum!( - self.value_created_30d, - &blocks.count.height_1m_ago, - &self.value_created.height - ); - rolling_sum!( - self.value_created_1y, - &blocks.count.height_1y_ago, - &self.value_created.height - ); - rolling_sum!( - self.value_destroyed_24h, - &blocks.count.height_24h_ago, - &self.value_destroyed.height - ); - rolling_sum!( - self.value_destroyed_7d, - &blocks.count.height_1w_ago, - &self.value_destroyed.height - ); - rolling_sum!( - self.value_destroyed_30d, - &blocks.count.height_1m_ago, - &self.value_destroyed.height - ); - rolling_sum!( - self.value_destroyed_1y, - &blocks.count.height_1y_ago, - &self.value_destroyed.height - ); - - // Realized value rolling sums - rolling_sum!( - self.realized_value_24h, - &blocks.count.height_24h_ago, - &self.realized_value.height - ); - rolling_sum!( - self.realized_value_7d, - &blocks.count.height_1w_ago, - &self.realized_value.height - ); - rolling_sum!( - self.realized_value_30d, - &blocks.count.height_1m_ago, - &self.realized_value.height - ); - rolling_sum!( - self.realized_value_1y, - &blocks.count.height_1y_ago, - &self.realized_value.height - ); + let window_starts = blocks.count.window_starts(); + self.value_created_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &self.value_created.height, exit, + )?; + self.value_destroyed_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &self.value_destroyed.height, exit, + )?; + self.realized_value_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &self.realized_value.height, exit, + )?; // Compute SOPR from rolling sums - self.sopr_24h.compute_binary::( - starting_indexes.height, - &self.value_created_24h.height, - &self.value_destroyed_24h.height, - exit, - )?; - self.sopr_7d.compute_binary::( - starting_indexes.height, - &self.value_created_7d.height, - &self.value_destroyed_7d.height, - exit, - )?; - self.sopr_30d.compute_binary::( - starting_indexes.height, - &self.value_created_30d.height, - &self.value_destroyed_30d.height, - exit, - )?; - self.sopr_1y.compute_binary::( - starting_indexes.height, - &self.value_created_1y.height, - &self.value_destroyed_1y.height, - exit, - )?; + for ((sopr, vc), vd) in self.sopr.as_mut_array().into_iter() + .zip(self.value_created_sum.as_array()) + .zip(self.value_destroyed_sum.as_array()) + { + sopr.compute_binary::( + starting_indexes.height, &vc.height, &vd.height, exit, + )?; + } // Compute sell-side risk ratios - self.sell_side_risk_ratio_24h - .compute_binary::( - starting_indexes.height, - &self.realized_value_24h.height, - &self.realized_cap_cents.height, - exit, - )?; - self.sell_side_risk_ratio_7d - .compute_binary::( - starting_indexes.height, - &self.realized_value_7d.height, - &self.realized_cap_cents.height, - exit, - )?; - self.sell_side_risk_ratio_30d - .compute_binary::( - starting_indexes.height, - &self.realized_value_30d.height, - &self.realized_cap_cents.height, - exit, - )?; - self.sell_side_risk_ratio_1y - .compute_binary::( - starting_indexes.height, - &self.realized_value_1y.height, - &self.realized_cap_cents.height, - exit, + for (ssrr, rv) in self.sell_side_risk_ratio.as_mut_array().into_iter() + .zip(self.realized_value_sum.as_array()) + { + ssrr.compute_binary::( + starting_indexes.height, &rv.height, &self.realized_cap_cents.height, exit, )?; + } // 7d EMAs self.realized_profit_7d_ema.height.compute_rolling_ema( @@ -1095,27 +938,27 @@ impl RealizedBase { exit, )?; - // SOPR EMAs + // SOPR EMAs (based on 24h window) self.sopr_24h_7d_ema.height.compute_rolling_ema( starting_indexes.height, &blocks.count.height_1w_ago, - &self.sopr.height, + &self.sopr._24h.height, exit, )?; self.sopr_24h_30d_ema.height.compute_rolling_ema( starting_indexes.height, &blocks.count.height_1m_ago, - &self.sopr.height, + &self.sopr._24h.height, exit, )?; - // Sell side risk EMAs + // Sell side risk EMAs (based on 24h window) self.sell_side_risk_ratio_24h_7d_ema .height .compute_rolling_ema( starting_indexes.height, &blocks.count.height_1w_ago, - &self.sell_side_risk_ratio.height, + &self.sell_side_risk_ratio._24h.height, exit, )?; self.sell_side_risk_ratio_24h_30d_ema @@ -1123,7 +966,7 @@ impl RealizedBase { .compute_rolling_ema( starting_indexes.height, &blocks.count.height_1m_ago, - &self.sell_side_risk_ratio.height, + &self.sell_side_risk_ratio._24h.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/realized/extended.rs b/crates/brk_computer/src/distribution/metrics/realized/extended.rs index e3c39a991..c42f3f308 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/extended.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/extended.rs @@ -5,7 +5,7 @@ use vecdb::{Exit, ReadableVec, Rw, StorageMode}; use crate::{ ComputeIndexes, blocks, - internal::{ComputedFromHeight, ComputedFromHeightRatioExtension, RatioCents64}, + internal::{ComputedFromHeight, ComputedFromHeightRatioExtension, RatioCents64, RollingWindows}, }; use crate::distribution::metrics::ImportConfig; @@ -18,20 +18,11 @@ pub struct RealizedExtended { pub realized_cap_rel_to_own_market_cap: ComputedFromHeight, // === Realized Profit/Loss Rolling Sums === - pub realized_profit_24h: ComputedFromHeight, - pub realized_profit_7d: ComputedFromHeight, - pub realized_profit_30d: ComputedFromHeight, - pub realized_profit_1y: ComputedFromHeight, - pub realized_loss_24h: ComputedFromHeight, - pub realized_loss_7d: ComputedFromHeight, - pub realized_loss_30d: ComputedFromHeight, - pub realized_loss_1y: ComputedFromHeight, + pub realized_profit_sum: RollingWindows, + pub realized_loss_sum: RollingWindows, // === Realized Profit to Loss Ratio (from rolling sums) === - pub realized_profit_to_loss_ratio_24h: ComputedFromHeight, - pub realized_profit_to_loss_ratio_7d: ComputedFromHeight, - pub realized_profit_to_loss_ratio_30d: ComputedFromHeight, - pub realized_profit_to_loss_ratio_1y: ComputedFromHeight, + pub realized_profit_to_loss_ratio: RollingWindows, // === Extended ratio metrics for realized/investor price === pub realized_price_ratio_ext: ComputedFromHeightRatioExtension, @@ -42,17 +33,6 @@ impl RealizedExtended { pub(crate) fn forced_import(cfg: &ImportConfig) -> Result { let v1 = Version::ONE; - macro_rules! import_rolling { - ($name:expr) => { - ComputedFromHeight::forced_import( - cfg.db, - &cfg.name($name), - cfg.version + v1, - cfg.indexes, - )? - }; - } - Ok(RealizedExtended { realized_cap_rel_to_own_market_cap: ComputedFromHeight::forced_import( cfg.db, @@ -60,18 +40,15 @@ impl RealizedExtended { cfg.version, cfg.indexes, )?, - realized_profit_24h: import_rolling!("realized_profit_24h"), - realized_profit_7d: import_rolling!("realized_profit_7d"), - realized_profit_30d: import_rolling!("realized_profit_30d"), - realized_profit_1y: import_rolling!("realized_profit_1y"), - realized_loss_24h: import_rolling!("realized_loss_24h"), - realized_loss_7d: import_rolling!("realized_loss_7d"), - realized_loss_30d: import_rolling!("realized_loss_30d"), - realized_loss_1y: import_rolling!("realized_loss_1y"), - realized_profit_to_loss_ratio_24h: import_rolling!("realized_profit_to_loss_ratio_24h"), - realized_profit_to_loss_ratio_7d: import_rolling!("realized_profit_to_loss_ratio_7d"), - realized_profit_to_loss_ratio_30d: import_rolling!("realized_profit_to_loss_ratio_30d"), - realized_profit_to_loss_ratio_1y: import_rolling!("realized_profit_to_loss_ratio_1y"), + realized_profit_sum: RollingWindows::forced_import( + cfg.db, &cfg.name("realized_profit"), cfg.version + v1, cfg.indexes, + )?, + realized_loss_sum: RollingWindows::forced_import( + cfg.db, &cfg.name("realized_loss"), cfg.version + v1, cfg.indexes, + )?, + realized_profit_to_loss_ratio: RollingWindows::forced_import( + cfg.db, &cfg.name("realized_profit_to_loss_ratio"), cfg.version + v1, cfg.indexes, + )?, realized_price_ratio_ext: ComputedFromHeightRatioExtension::forced_import( cfg.db, &cfg.name("realized_price"), @@ -97,53 +74,12 @@ impl RealizedExtended { exit: &Exit, ) -> Result<()> { // Realized profit/loss rolling sums - self.realized_profit_24h.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_24h_ago, - &base.realized_profit.height, - exit, + let window_starts = blocks.count.window_starts(); + self.realized_profit_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &base.realized_profit.height, exit, )?; - self.realized_profit_7d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1w_ago, - &base.realized_profit.height, - exit, - )?; - self.realized_profit_30d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1m_ago, - &base.realized_profit.height, - exit, - )?; - self.realized_profit_1y.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1y_ago, - &base.realized_profit.height, - exit, - )?; - self.realized_loss_24h.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_24h_ago, - &base.realized_loss.height, - exit, - )?; - self.realized_loss_7d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1w_ago, - &base.realized_loss.height, - exit, - )?; - self.realized_loss_30d.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1m_ago, - &base.realized_loss.height, - exit, - )?; - self.realized_loss_1y.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1y_ago, - &base.realized_loss.height, - exit, + self.realized_loss_sum.compute_rolling_sum( + starting_indexes.height, &window_starts, &base.realized_loss.height, exit, )?; // Realized cap relative to own market cap @@ -157,34 +93,14 @@ impl RealizedExtended { )?; // Realized profit to loss ratios - self.realized_profit_to_loss_ratio_24h - .compute_binary::( - starting_indexes.height, - &self.realized_profit_24h.height, - &self.realized_loss_24h.height, - exit, - )?; - self.realized_profit_to_loss_ratio_7d - .compute_binary::( - starting_indexes.height, - &self.realized_profit_7d.height, - &self.realized_loss_7d.height, - exit, - )?; - self.realized_profit_to_loss_ratio_30d - .compute_binary::( - starting_indexes.height, - &self.realized_profit_30d.height, - &self.realized_loss_30d.height, - exit, - )?; - self.realized_profit_to_loss_ratio_1y - .compute_binary::( - starting_indexes.height, - &self.realized_profit_1y.height, - &self.realized_loss_1y.height, - exit, + for ((ratio, profit), loss) in self.realized_profit_to_loss_ratio.as_mut_array().into_iter() + .zip(self.realized_profit_sum.as_array()) + .zip(self.realized_loss_sum.as_array()) + { + ratio.compute_binary::( + starting_indexes.height, &profit.height, &loss.height, exit, )?; + } // Extended ratio metrics self.realized_price_ratio_ext.compute_rest( diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs b/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs index 848f55861..06197f398 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs +++ b/crates/brk_computer/src/internal/from_height/by_unit/rolling_full.rs @@ -92,12 +92,9 @@ impl RollingFullByUnit { indexes: &indexes::Vecs, ) -> Result { let v = version + VERSION; - Ok(Self(Windows { - _24h: RollingFullSlot::forced_import(db, &format!("{name}_24h"), v, indexes)?, - _7d: RollingFullSlot::forced_import(db, &format!("{name}_7d"), v, indexes)?, - _30d: RollingFullSlot::forced_import(db, &format!("{name}_30d"), v, indexes)?, - _1y: RollingFullSlot::forced_import(db, &format!("{name}_1y"), v, indexes)?, - })) + Ok(Self(Windows::try_from_fn(|suffix| { + RollingFullSlot::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + })?)) } pub(crate) fn compute( @@ -109,7 +106,7 @@ impl RollingFullByUnit { exit: &Exit, ) -> Result<()> { for (slot, starts) in self.0.as_mut_array().into_iter().zip(windows.as_array()) { - slot.compute(max_from, starts, sats_source, cents_source, exit)?; + slot.compute(max_from, *starts, sats_source, cents_source, exit)?; } Ok(()) } diff --git a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs b/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs index 01acc103a..821b4372c 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs +++ b/crates/brk_computer/src/internal/from_height/by_unit/rolling_sum.rs @@ -38,8 +38,8 @@ impl RollingSumByUnit { exit: &Exit, ) -> Result<()> { for (w, starts) in self.0.as_mut_array().into_iter().zip(windows.as_array()) { - w.sats.height.compute_rolling_sum(max_from, starts, sats_source, exit)?; - w.cents.height.compute_rolling_sum(max_from, starts, cents_source, exit)?; + w.sats.height.compute_rolling_sum(max_from, *starts, sats_source, exit)?; + w.cents.height.compute_rolling_sum(max_from, *starts, cents_source, exit)?; } Ok(()) } diff --git a/crates/brk_computer/src/internal/from_height/by_unit/windows.rs b/crates/brk_computer/src/internal/from_height/by_unit/windows.rs index 4c2a142fc..f317c5d4d 100644 --- a/crates/brk_computer/src/internal/from_height/by_unit/windows.rs +++ b/crates/brk_computer/src/internal/from_height/by_unit/windows.rs @@ -14,11 +14,8 @@ impl Windows { version: Version, indexes: &indexes::Vecs, ) -> Result { - Ok(Self { - _24h: ByUnit::forced_import(db, &format!("{name}_24h"), version, indexes)?, - _7d: ByUnit::forced_import(db, &format!("{name}_7d"), version, indexes)?, - _30d: ByUnit::forced_import(db, &format!("{name}_30d"), version, indexes)?, - _1y: ByUnit::forced_import(db, &format!("{name}_1y"), version, indexes)?, + Windows::try_from_fn(|suffix| { + ByUnit::forced_import(db, &format!("{name}_{suffix}"), version, indexes) }) } } diff --git a/crates/brk_computer/src/internal/rolling/value_windows.rs b/crates/brk_computer/src/internal/rolling/value_windows.rs index 91c21251f..eb7dc0c39 100644 --- a/crates/brk_computer/src/internal/rolling/value_windows.rs +++ b/crates/brk_computer/src/internal/rolling/value_windows.rs @@ -36,32 +36,9 @@ impl ValueFromHeightWindows { indexes: &indexes::Vecs, ) -> Result { let v = version + VERSION; - Ok(Self(Windows { - _24h: ValueFromHeight::forced_import( - db, - &format!("{name}_24h"), - v, - indexes, - )?, - _7d: ValueFromHeight::forced_import( - db, - &format!("{name}_7d"), - v, - indexes, - )?, - _30d: ValueFromHeight::forced_import( - db, - &format!("{name}_30d"), - v, - indexes, - )?, - _1y: ValueFromHeight::forced_import( - db, - &format!("{name}_1y"), - v, - indexes, - )?, - })) + Ok(Self(Windows::try_from_fn(|suffix| { + ValueFromHeight::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + })?)) } pub(crate) fn compute_rolling_sum( @@ -73,7 +50,7 @@ impl ValueFromHeightWindows { exit: &Exit, ) -> Result<()> { for (w, starts) in self.0.as_mut_array().into_iter().zip(windows.as_array()) { - w.compute_rolling_sum(max_from, starts, sats_source, cents_source, exit)?; + w.compute_rolling_sum(max_from, *starts, sats_source, cents_source, exit)?; } Ok(()) } diff --git a/crates/brk_computer/src/internal/rolling/windows.rs b/crates/brk_computer/src/internal/rolling/windows.rs index 1beb5535c..a16fd76c1 100644 --- a/crates/brk_computer/src/internal/rolling/windows.rs +++ b/crates/brk_computer/src/internal/rolling/windows.rs @@ -18,19 +18,8 @@ use crate::{ internal::{ComputedFromHeight, ComputedVecValue, NumericValue, Windows}, }; -/// Rolling window start heights — references to the 4 height-ago vecs. -pub struct WindowStarts<'a> { - pub _24h: &'a EagerVec>, - pub _7d: &'a EagerVec>, - pub _30d: &'a EagerVec>, - pub _1y: &'a EagerVec>, -} - -impl<'a> WindowStarts<'a> { - pub fn as_array(&self) -> [&'a EagerVec>; 4] { - [self._24h, self._7d, self._30d, self._1y] - } -} +/// Rolling window start heights — the 4 height-ago vecs (24h, 7d, 30d, 1y). +pub type WindowStarts<'a> = Windows<&'a EagerVec>>; /// 4 rolling window vecs (24h, 7d, 30d, 1y), each with height data + all 17 index views. #[derive(Deref, DerefMut, Traversable)] @@ -52,12 +41,9 @@ where indexes: &indexes::Vecs, ) -> Result { let v = version + VERSION; - Ok(Self(Windows { - _24h: ComputedFromHeight::forced_import(db, &format!("{name}_24h"), v, indexes)?, - _7d: ComputedFromHeight::forced_import(db, &format!("{name}_7d"), v, indexes)?, - _30d: ComputedFromHeight::forced_import(db, &format!("{name}_30d"), v, indexes)?, - _1y: ComputedFromHeight::forced_import(db, &format!("{name}_1y"), v, indexes)?, - })) + Ok(Self(Windows::try_from_fn(|suffix| { + ComputedFromHeight::forced_import(db, &format!("{name}_{suffix}"), v, indexes) + })?)) } pub(crate) fn compute_rolling_sum( @@ -71,7 +57,7 @@ where T: Default + SubAssign, { for (w, starts) in self.0.as_mut_array().into_iter().zip(windows.as_array()) { - w.height.compute_rolling_sum(max_from, starts, source, exit)?; + w.height.compute_rolling_sum(max_from, *starts, source, exit)?; } Ok(()) } diff --git a/crates/brk_computer/src/internal/windows.rs b/crates/brk_computer/src/internal/windows.rs index 3cca1d858..7d7ebd546 100644 --- a/crates/brk_computer/src/internal/windows.rs +++ b/crates/brk_computer/src/internal/windows.rs @@ -17,6 +17,23 @@ pub struct Windows { } impl Windows { + pub const SUFFIXES: [&'static str; 4] = ["24h", "7d", "30d", "1y"]; + + pub fn try_from_fn( + mut f: impl FnMut(&str) -> std::result::Result, + ) -> std::result::Result { + Ok(Self { + _24h: f(Self::SUFFIXES[0])?, + _7d: f(Self::SUFFIXES[1])?, + _30d: f(Self::SUFFIXES[2])?, + _1y: f(Self::SUFFIXES[3])?, + }) + } + + pub fn as_array(&self) -> [&A; 4] { + [&self._24h, &self._7d, &self._30d, &self._1y] + } + pub fn as_mut_array(&mut self) -> [&mut A; 4] { [&mut self._24h, &mut self._7d, &mut self._30d, &mut self._1y] } diff --git a/crates/brk_computer/src/mining/rewards/compute.rs b/crates/brk_computer/src/mining/rewards/compute.rs index d11cfeac1..d32fe4441 100644 --- a/crates/brk_computer/src/mining/rewards/compute.rs +++ b/crates/brk_computer/src/mining/rewards/compute.rs @@ -130,30 +130,20 @@ impl Vecs { )?; // Rolling fee dominance = sum(fees) / sum(coinbase) * 100 - self.fee_dominance_24h.height.compute_percentage( - starting_indexes.height, - &self.fees.rolling._24h.sum.sats.height, - &self.coinbase.rolling._24h.sum.sats.height, - exit, - )?; - self.fee_dominance_7d.height.compute_percentage( - starting_indexes.height, - &self.fees.rolling._7d.sum.sats.height, - &self.coinbase.rolling._7d.sum.sats.height, - exit, - )?; - self.fee_dominance_30d.height.compute_percentage( - starting_indexes.height, - &self.fees.rolling._30d.sum.sats.height, - &self.coinbase.rolling._30d.sum.sats.height, - exit, - )?; - self.fee_dominance_1y.height.compute_percentage( - starting_indexes.height, - &self.fees.rolling._1y.sum.sats.height, - &self.coinbase.rolling._1y.sum.sats.height, - exit, - )?; + for ((fee_dom, fees_w), coinbase_w) in self + .fee_dominance_rolling + .as_mut_array() + .into_iter() + .zip(self.fees.rolling.as_array()) + .zip(self.coinbase.rolling.as_array()) + { + fee_dom.height.compute_percentage( + starting_indexes.height, + &fees_w.sum.sats.height, + &coinbase_w.sum.sats.height, + exit, + )?; + } // All-time cumulative subsidy dominance self.subsidy_dominance.height.compute_percentage( @@ -165,30 +155,19 @@ impl Vecs { // Rolling subsidy dominance = 100 - fee_dominance let hundred = StoredF32::from(100u8); - self.subsidy_dominance_24h.height.compute_transform( - starting_indexes.height, - &self.fee_dominance_24h.height, - |(height, fee_dom, _)| (height, hundred - fee_dom), - exit, - )?; - self.subsidy_dominance_7d.height.compute_transform( - starting_indexes.height, - &self.fee_dominance_7d.height, - |(height, fee_dom, _)| (height, hundred - fee_dom), - exit, - )?; - self.subsidy_dominance_30d.height.compute_transform( - starting_indexes.height, - &self.fee_dominance_30d.height, - |(height, fee_dom, _)| (height, hundred - fee_dom), - exit, - )?; - self.subsidy_dominance_1y.height.compute_transform( - starting_indexes.height, - &self.fee_dominance_1y.height, - |(height, fee_dom, _)| (height, hundred - fee_dom), - exit, - )?; + for (sub_dom, fee_dom) in self + .subsidy_dominance_rolling + .as_mut_array() + .into_iter() + .zip(self.fee_dominance_rolling.as_array()) + { + sub_dom.height.compute_transform( + starting_indexes.height, + &fee_dom.height, + |(height, fee_dom, _)| (height, hundred - fee_dom), + exit, + )?; + } self.subsidy_usd_1y_sma.cents.height.compute_rolling_average( starting_indexes.height, diff --git a/crates/brk_computer/src/mining/rewards/import.rs b/crates/brk_computer/src/mining/rewards/import.rs index 4322b222f..510094571 100644 --- a/crates/brk_computer/src/mining/rewards/import.rs +++ b/crates/brk_computer/src/mining/rewards/import.rs @@ -6,7 +6,7 @@ use super::Vecs; use crate::{ indexes, internal::{ - ComputedFromHeight, FiatFromHeight, ValueFromHeightFull, + ComputedFromHeight, FiatFromHeight, RollingWindows, ValueFromHeightFull, ValueFromHeightCumulativeSum, }, }; @@ -33,27 +33,9 @@ impl Vecs { version, indexes, )?, - fee_dominance_24h: ComputedFromHeight::forced_import( + fee_dominance_rolling: RollingWindows::forced_import( db, - "fee_dominance_24h", - version, - indexes, - )?, - fee_dominance_7d: ComputedFromHeight::forced_import( - db, - "fee_dominance_7d", - version, - indexes, - )?, - fee_dominance_30d: ComputedFromHeight::forced_import( - db, - "fee_dominance_30d", - version, - indexes, - )?, - fee_dominance_1y: ComputedFromHeight::forced_import( - db, - "fee_dominance_1y", + "fee_dominance", version, indexes, )?, @@ -63,27 +45,9 @@ impl Vecs { version, indexes, )?, - subsidy_dominance_24h: ComputedFromHeight::forced_import( + subsidy_dominance_rolling: RollingWindows::forced_import( db, - "subsidy_dominance_24h", - version, - indexes, - )?, - subsidy_dominance_7d: ComputedFromHeight::forced_import( - db, - "subsidy_dominance_7d", - version, - indexes, - )?, - subsidy_dominance_30d: ComputedFromHeight::forced_import( - db, - "subsidy_dominance_30d", - version, - indexes, - )?, - subsidy_dominance_1y: ComputedFromHeight::forced_import( - db, - "subsidy_dominance_1y", + "subsidy_dominance", version, indexes, )?, diff --git a/crates/brk_computer/src/mining/rewards/vecs.rs b/crates/brk_computer/src/mining/rewards/vecs.rs index 3f204919d..c47aabf9a 100644 --- a/crates/brk_computer/src/mining/rewards/vecs.rs +++ b/crates/brk_computer/src/mining/rewards/vecs.rs @@ -3,7 +3,7 @@ use brk_types::{Cents, StoredF32}; use vecdb::{Rw, StorageMode}; use crate::internal::{ - ComputedFromHeight, FiatFromHeight, ValueFromHeightFull, + ComputedFromHeight, FiatFromHeight, RollingWindows, ValueFromHeightFull, ValueFromHeightCumulativeSum, }; @@ -15,14 +15,8 @@ pub struct Vecs { pub fees: ValueFromHeightFull, pub unclaimed_rewards: ValueFromHeightCumulativeSum, pub fee_dominance: ComputedFromHeight, - pub fee_dominance_24h: ComputedFromHeight, - pub fee_dominance_7d: ComputedFromHeight, - pub fee_dominance_30d: ComputedFromHeight, - pub fee_dominance_1y: ComputedFromHeight, + pub fee_dominance_rolling: RollingWindows, pub subsidy_dominance: ComputedFromHeight, - pub subsidy_dominance_24h: ComputedFromHeight, - pub subsidy_dominance_7d: ComputedFromHeight, - pub subsidy_dominance_30d: ComputedFromHeight, - pub subsidy_dominance_1y: ComputedFromHeight, + pub subsidy_dominance_rolling: RollingWindows, pub subsidy_usd_1y_sma: FiatFromHeight, } diff --git a/crates/brk_computer/src/pools/vecs.rs b/crates/brk_computer/src/pools/vecs.rs index f64244e26..c40d683e4 100644 --- a/crates/brk_computer/src/pools/vecs.rs +++ b/crates/brk_computer/src/pools/vecs.rs @@ -8,7 +8,7 @@ use crate::{ indexes::{self, ComputeIndexes}, internal::{ ComputedFromHeightCumulativeSum, ComputedFromHeight, MaskSats, PercentageU32F32, - ValueFromHeightCumulativeSum, + RollingWindows, ValueFromHeightCumulativeSum, }, mining, prices, }; @@ -18,19 +18,12 @@ pub struct Vecs { slug: PoolSlug, pub blocks_mined: ComputedFromHeightCumulativeSum, - pub blocks_mined_24h_sum: ComputedFromHeight, - pub blocks_mined_1w_sum: ComputedFromHeight, - pub blocks_mined_1m_sum: ComputedFromHeight, - pub blocks_mined_1y_sum: ComputedFromHeight, + pub blocks_mined_sum: RollingWindows, pub subsidy: ValueFromHeightCumulativeSum, pub fee: ValueFromHeightCumulativeSum, pub coinbase: ValueFromHeightCumulativeSum, pub dominance: ComputedFromHeight, - - pub dominance_24h: ComputedFromHeight, - pub dominance_1w: ComputedFromHeight, - pub dominance_1m: ComputedFromHeight, - pub dominance_1y: ComputedFromHeight, + pub dominance_rolling: RollingWindows, pub blocks_since_block: ComputedFromHeight, pub days_since_block: ComputedFromHeight, } @@ -52,30 +45,8 @@ impl Vecs { indexes, )?; - let blocks_mined_24h_sum = ComputedFromHeight::forced_import( - db, - &suffix("blocks_mined_24h_sum"), - version, - indexes, - )?; - let blocks_mined_1w_sum = ComputedFromHeight::forced_import( - db, - &suffix("blocks_mined_1w_sum"), - version, - indexes, - )?; - let blocks_mined_1m_sum = ComputedFromHeight::forced_import( - db, - &suffix("blocks_mined_1m_sum"), - version, - indexes, - )?; - let blocks_mined_1y_sum = ComputedFromHeight::forced_import( - db, - &suffix("blocks_mined_1y_sum"), - version, - indexes, - )?; + let blocks_mined_sum = + RollingWindows::forced_import(db, &suffix("blocks_mined_sum"), version, indexes)?; let subsidy = ValueFromHeightCumulativeSum::forced_import(db, &suffix("subsidy"), version, indexes)?; @@ -88,27 +59,15 @@ impl Vecs { let dominance = ComputedFromHeight::forced_import(db, &suffix("dominance"), version, indexes)?; - let dominance_24h = - ComputedFromHeight::forced_import(db, &suffix("dominance_24h"), version, indexes)?; - let dominance_1w = - ComputedFromHeight::forced_import(db, &suffix("dominance_1w"), version, indexes)?; - let dominance_1m = - ComputedFromHeight::forced_import(db, &suffix("dominance_1m"), version, indexes)?; - let dominance_1y = - ComputedFromHeight::forced_import(db, &suffix("dominance_1y"), version, indexes)?; + let dominance_rolling = + RollingWindows::forced_import(db, &suffix("dominance"), version, indexes)?; Ok(Self { dominance, - dominance_24h, - dominance_1w, - dominance_1m, - dominance_1y, + dominance_rolling, slug, blocks_mined, - blocks_mined_24h_sum, - blocks_mined_1w_sum, - blocks_mined_1m_sum, - blocks_mined_1y_sum, + blocks_mined_sum, coinbase, subsidy, fee, @@ -159,31 +118,9 @@ impl Vecs { Ok(()) })?; - // Compute rolling window blocks mined using the start heights from blocks.count - self.blocks_mined_24h_sum.height.compute_rolling_sum( + self.blocks_mined_sum.compute_rolling_sum( starting_indexes.height, - &blocks.count.height_24h_ago, - &self.blocks_mined.height, - exit, - )?; - - self.blocks_mined_1w_sum.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1w_ago, - &self.blocks_mined.height, - exit, - )?; - - self.blocks_mined_1m_sum.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1m_ago, - &self.blocks_mined.height, - exit, - )?; - - self.blocks_mined_1y_sum.height.compute_rolling_sum( - starting_indexes.height, - &blocks.count.height_1y_ago, + &window_starts, &self.blocks_mined.height, exit, )?; @@ -196,37 +133,20 @@ impl Vecs { exit, )?; - self.dominance_24h - .compute_binary::( + for ((dom, mined), total) in self + .dominance_rolling + .as_mut_array() + .into_iter() + .zip(self.blocks_mined_sum.as_array()) + .zip(blocks.count.block_count_sum.as_array()) + { + dom.compute_binary::( starting_indexes.height, - &self.blocks_mined_24h_sum.height, - &blocks.count.block_count_sum._24h.height, - exit, - )?; - - self.dominance_1w - .compute_binary::( - starting_indexes.height, - &self.blocks_mined_1w_sum.height, - &blocks.count.block_count_sum._7d.height, - exit, - )?; - - self.dominance_1m - .compute_binary::( - starting_indexes.height, - &self.blocks_mined_1m_sum.height, - &blocks.count.block_count_sum._30d.height, - exit, - )?; - - self.dominance_1y - .compute_binary::( - starting_indexes.height, - &self.blocks_mined_1y_sum.height, - &blocks.count.block_count_sum._1y.height, + &mined.height, + &total.height, exit, )?; + } self.subsidy.compute( starting_indexes.height,