global: snapshot

This commit is contained in:
nym21
2026-02-12 22:52:57 +01:00
parent 3bc20a0a46
commit b779edc0d6
60 changed files with 8720 additions and 1504 deletions
@@ -1,20 +0,0 @@
use brk_types::Version;
use super::Vecs;
use crate::{
distribution,
internal::{DollarsIdentity, LazyValueFromHeightLast, SatsIdentity},
};
impl Vecs {
pub fn import(version: Version, distribution: &distribution::Vecs) -> Self {
let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply;
Self(LazyValueFromHeightLast::from_block_source::<
SatsIdentity,
DollarsIdentity,
>(
"circulating_supply", &supply_metrics.total, version)
)
}
}
@@ -1,4 +0,0 @@
mod import;
mod vecs;
pub use vecs::Vecs;
@@ -1,8 +0,0 @@
use brk_traversable::Traversable;
use derive_more::{Deref, DerefMut};
use crate::internal::LazyValueFromHeightLast;
/// Circulating supply - lazy references to distribution's actual supply (KISS)
#[derive(Clone, Deref, DerefMut, Traversable)]
pub struct Vecs(pub LazyValueFromHeightLast);
+50 -5
View File
@@ -2,7 +2,7 @@ use brk_error::Result;
use vecdb::Exit;
use super::Vecs;
use crate::{blocks, distribution, indexes, scripts, transactions, ComputeIndexes};
use crate::{ComputeIndexes, blocks, distribution, indexes, scripts, transactions};
impl Vecs {
#[allow(clippy::too_many_arguments)]
@@ -20,15 +20,60 @@ impl Vecs {
self.burned
.compute(indexes, scripts, blocks, starting_indexes, exit)?;
// 2. Compute inflation rate
self.inflation
.compute(blocks, distribution, starting_indexes, exit)?;
// 2. Compute inflation rate: daily_subsidy / circulating_supply * 365 * 100
let circulating_supply = &distribution.utxo_cohorts.all.metrics.supply.total.sats;
self.inflation.compute_all(starting_indexes, exit, |v| {
v.compute_transform2(
starting_indexes.dateindex,
&blocks.rewards.subsidy.sats.dateindex.sum_cum.sum.0,
&circulating_supply.dateindex.0,
|(i, subsidy_1d_sum, supply, ..)| {
let inflation = if *supply > 0 {
365.0 * *subsidy_1d_sum as f64 / *supply as f64 * 100.0
} else {
0.0
};
(i, inflation.into())
},
exit,
)?;
Ok(())
})?;
// 3. Compute velocity
self.velocity
.compute(transactions, distribution, starting_indexes, exit)?;
// Note: circulating and market_cap are lazy - no compute needed
// 4. Compute cap growth rates
if let Some(market_cap) = self.market_cap.as_ref() {
let mcap_dateindex = &market_cap.dateindex.0;
self.market_cap_growth_rate
.compute_all(starting_indexes, exit, |vec| {
vec.compute_percentage_change(
starting_indexes.dateindex,
mcap_dateindex,
30,
exit,
)?;
Ok(())
})?;
}
if let Some(realized) = distribution.utxo_cohorts.all.metrics.realized.as_ref() {
let rcap_dateindex = &realized.realized_cap.dateindex.0;
self.realized_cap_growth_rate
.compute_all(starting_indexes, exit, |vec| {
vec.compute_percentage_change(
starting_indexes.dateindex,
rcap_dateindex,
30,
exit,
)?;
Ok(())
})?;
}
// Note: circulating, market_cap, cap_growth_rate_diff are lazy
let _lock = exit.lock();
self.db.compact()?;
+42 -7
View File
@@ -3,12 +3,18 @@ use std::path::Path;
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::Version;
use vecdb::{Database, PAGE_SIZE};
use vecdb::{Database, IterableCloneableVec, LazyVecFrom2, PAGE_SIZE};
use super::Vecs;
use crate::{distribution, indexes, price};
use crate::{
distribution, indexes, price,
internal::{
ComputedFromDateAverage, ComputedFromDateLast, DifferenceF32, DollarsIdentity,
LazyFromHeightLast, LazyValueFromHeightLast, SatsIdentity,
},
};
const VERSION: Version = Version::ZERO;
const VERSION: Version = Version::ONE;
impl Vecs {
pub fn forced_import(
@@ -24,21 +30,47 @@ impl Vecs {
let version = parent_version + VERSION;
let compute_dollars = price.is_some();
let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply;
// Circulating supply - lazy refs to distribution
let circulating = super::circulating::Vecs::import(version, distribution);
let circulating = LazyValueFromHeightLast::from_block_source::<SatsIdentity, DollarsIdentity>(
"circulating_supply",
&supply_metrics.total,
version,
);
// Burned/unspendable supply - computed from scripts
let burned = super::burned::Vecs::forced_import(&db, version, indexes, price)?;
// Inflation rate
let inflation = super::inflation::Vecs::forced_import(&db, version, indexes)?;
let inflation =
ComputedFromDateAverage::forced_import(&db, "inflation_rate", version, indexes)?;
// Velocity
let velocity =
super::velocity::Vecs::forced_import(&db, version, indexes, compute_dollars)?;
// Market cap - lazy refs to supply in USD
let market_cap = super::market_cap::Vecs::import(version, distribution);
// Market cap - lazy identity from distribution supply in USD
let market_cap = supply_metrics.total.dollars.as_ref().map(|d| {
LazyFromHeightLast::from_lazy_binary_computed::<DollarsIdentity, _, _>(
"market_cap",
version,
d.height.boxed_clone(),
d,
)
});
// Growth rates
let market_cap_growth_rate =
ComputedFromDateLast::forced_import(&db, "market_cap_growth_rate", version, indexes)?;
let realized_cap_growth_rate =
ComputedFromDateLast::forced_import(&db, "realized_cap_growth_rate", version, indexes)?;
let cap_growth_rate_diff = LazyVecFrom2::transformed::<DifferenceF32>(
"cap_growth_rate_diff",
version,
market_cap_growth_rate.dateindex.boxed_clone(),
realized_cap_growth_rate.dateindex.boxed_clone(),
);
let this = Self {
db,
@@ -47,6 +79,9 @@ impl Vecs {
inflation,
velocity,
market_cap,
market_cap_growth_rate,
realized_cap_growth_rate,
cap_growth_rate_diff,
};
this.db.retain_regions(
@@ -1,38 +0,0 @@
use brk_error::Result;
use vecdb::Exit;
use super::Vecs;
use crate::{ComputeIndexes, blocks, distribution};
impl Vecs {
pub fn compute(
&mut self,
blocks: &blocks::Vecs,
distribution: &distribution::Vecs,
starting_indexes: &ComputeIndexes,
exit: &Exit,
) -> Result<()> {
// inflation = daily_subsidy / circulating_supply * 365 * 100
let circulating_supply = &distribution.utxo_cohorts.all.metrics.supply.total.sats;
self.compute_all(starting_indexes, exit, |v| {
v.compute_transform2(
starting_indexes.dateindex,
&blocks.rewards.subsidy.sats.dateindex.sum_cum.sum.0,
&circulating_supply.dateindex.0,
|(i, subsidy_1d_sum, supply, ..)| {
let inflation = if *supply > 0 {
365.0 * *subsidy_1d_sum as f64 / *supply as f64 * 100.0
} else {
0.0
};
(i, inflation.into())
},
exit,
)?;
Ok(())
})?;
Ok(())
}
}
@@ -1,17 +0,0 @@
use brk_error::Result;
use brk_types::Version;
use vecdb::Database;
use super::Vecs;
use crate::{indexes, internal::ComputedFromDateAverage};
impl Vecs {
pub fn forced_import(db: &Database, version: Version, indexes: &indexes::Vecs) -> Result<Self> {
Ok(Self(ComputedFromDateAverage::forced_import(
db,
"inflation_rate",
version,
indexes,
)?))
}
}
@@ -1,5 +0,0 @@
mod compute;
mod import;
mod vecs;
pub use vecs::Vecs;
@@ -1,10 +0,0 @@
use brk_traversable::Traversable;
use brk_types::StoredF32;
use derive_more::{Deref, DerefMut};
use crate::internal::ComputedFromDateAverage;
/// Inflation rate metrics
#[derive(Clone, Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct Vecs(pub ComputedFromDateAverage<StoredF32>);
@@ -1,23 +0,0 @@
use brk_types::Version;
use vecdb::IterableCloneableVec;
use super::Vecs;
use crate::{
distribution,
internal::{DollarsIdentity, LazyFromHeightLast},
};
impl Vecs {
pub fn import(version: Version, distribution: &distribution::Vecs) -> Option<Self> {
let supply_metrics = &distribution.utxo_cohorts.all.metrics.supply;
supply_metrics.total.dollars.as_ref().map(|d| {
Self(LazyFromHeightLast::from_lazy_binary_computed::<DollarsIdentity, _, _>(
"market_cap",
version,
d.height.boxed_clone(),
d,
))
})
}
}
@@ -1,4 +0,0 @@
mod import;
mod vecs;
pub use vecs::Vecs;
@@ -1,8 +0,0 @@
use brk_traversable::Traversable;
use brk_types::Dollars;
use derive_more::{Deref, DerefMut};
use crate::internal::LazyFromHeightLast;
#[derive(Clone, Deref, DerefMut, Traversable)]
pub struct Vecs(pub LazyFromHeightLast<Dollars>);
-3
View File
@@ -1,7 +1,4 @@
pub mod burned;
pub mod circulating;
pub mod inflation;
pub mod market_cap;
pub mod velocity;
mod compute;
+13 -13
View File
@@ -1,24 +1,24 @@
use brk_traversable::Traversable;
use vecdb::Database;
use brk_types::{DateIndex, Dollars, StoredF32};
use vecdb::{Database, LazyVecFrom2};
use super::{burned, circulating, inflation, market_cap, velocity};
use super::{burned, velocity};
use crate::internal::{
ComputedFromDateAverage, ComputedFromDateLast, LazyFromHeightLast, LazyValueFromHeightLast,
};
/// Supply metrics module
///
/// This module owns all supply-related metrics:
/// - circulating: Lazy references to distribution's actual circulating supply
/// - burned: Cumulative opreturn and unspendable supply
/// - inflation: Inflation rate derived from supply
/// - velocity: BTC and USD velocity metrics
/// - market_cap: Lazy references to supply in USD (circulating * price)
#[derive(Clone, Traversable)]
pub struct Vecs {
#[traversable(skip)]
pub(crate) db: Database,
pub circulating: circulating::Vecs,
pub circulating: LazyValueFromHeightLast,
pub burned: burned::Vecs,
pub inflation: inflation::Vecs,
pub inflation: ComputedFromDateAverage<StoredF32>,
pub velocity: velocity::Vecs,
pub market_cap: Option<market_cap::Vecs>,
pub market_cap: Option<LazyFromHeightLast<Dollars>>,
pub market_cap_growth_rate: ComputedFromDateLast<StoredF32>,
pub realized_cap_growth_rate: ComputedFromDateLast<StoredF32>,
pub cap_growth_rate_diff:
LazyVecFrom2<DateIndex, StoredF32, DateIndex, StoredF32, DateIndex, StoredF32>,
}