mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot part 5
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
|
||||
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, StoredF32, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{DeltaAvg, LazyDeltaVec, LazyVecFrom1, ReadableCloneableVec};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
CachedWindowStarts, CentsUnsignedToDollars, DerivedResolutions, LazyPerBlock,
|
||||
LazyRollingAvgFromHeight, Resolutions, SatsToBitcoin, Windows,
|
||||
CachedWindowStarts, DerivedResolutions, AvgCentsToUsd, AvgSatsToBtc, LazyPerBlock,
|
||||
LazyRollingAvgFromHeight, Resolutions, Windows,
|
||||
},
|
||||
};
|
||||
|
||||
/// Single window slot: lazy rolling average for Amount (sats + btc + cents + usd).
|
||||
/// Single window slot: lazy rolling average for Amount (sats + btc + cents + usd), all as f64.
|
||||
#[derive(Clone, Traversable)]
|
||||
pub struct LazyRollingAvgAmountFromHeight {
|
||||
pub btc: LazyPerBlock<Bitcoin, Sats>,
|
||||
pub btc: LazyPerBlock<Bitcoin, StoredF32>,
|
||||
pub sats: LazyRollingAvgFromHeight<Sats>,
|
||||
pub usd: LazyPerBlock<Dollars, Cents>,
|
||||
pub usd: LazyPerBlock<Dollars, StoredF32>,
|
||||
pub cents: LazyRollingAvgFromHeight<Cents>,
|
||||
}
|
||||
|
||||
/// Lazy rolling averages for all 4 windows, for Amount (sats + btc + cents + usd).
|
||||
/// Lazy rolling averages for all 4 windows, for Amount (sats + btc + cents + usd), all as f64.
|
||||
#[derive(Clone, Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct LazyRollingAvgsAmountFromHeight(pub Windows<LazyRollingAvgAmountFromHeight>);
|
||||
@@ -42,8 +42,8 @@ impl LazyRollingAvgsAmountFromHeight {
|
||||
let cached = cached_start.clone();
|
||||
let starts_version = cached.version();
|
||||
|
||||
// Sats lazy rolling avg
|
||||
let sats_avg = LazyDeltaVec::<Height, Sats, Sats, DeltaAvg>::new(
|
||||
// Sats lazy rolling avg → f64
|
||||
let sats_avg = LazyDeltaVec::<Height, Sats, StoredF32, DeltaAvg>::new(
|
||||
&format!("{full_name}_sats"),
|
||||
version,
|
||||
cum_sats.clone(),
|
||||
@@ -64,22 +64,22 @@ impl LazyRollingAvgsAmountFromHeight {
|
||||
resolutions: Box::new(sats_resolutions),
|
||||
};
|
||||
|
||||
// Btc lazy from sats
|
||||
// Btc: f64 sats avg / 1e8
|
||||
let btc = LazyPerBlock {
|
||||
height: LazyVecFrom1::transformed::<SatsToBitcoin>(
|
||||
height: LazyVecFrom1::transformed::<AvgSatsToBtc>(
|
||||
&full_name,
|
||||
version,
|
||||
sats.height.read_only_boxed_clone(),
|
||||
),
|
||||
resolutions: Box::new(DerivedResolutions::from_derived_computed::<SatsToBitcoin>(
|
||||
resolutions: Box::new(DerivedResolutions::from_derived_computed::<AvgSatsToBtc>(
|
||||
&full_name,
|
||||
version,
|
||||
&sats.resolutions,
|
||||
)),
|
||||
};
|
||||
|
||||
// Cents rolling avg
|
||||
let cents_avg = LazyDeltaVec::<Height, Cents, Cents, DeltaAvg>::new(
|
||||
// Cents lazy rolling avg → f64
|
||||
let cents_avg = LazyDeltaVec::<Height, Cents, StoredF32, DeltaAvg>::new(
|
||||
&format!("{full_name}_cents"),
|
||||
version,
|
||||
cum_cents.clone(),
|
||||
@@ -97,17 +97,17 @@ impl LazyRollingAvgsAmountFromHeight {
|
||||
resolutions: Box::new(cents_resolutions),
|
||||
};
|
||||
|
||||
// Usd lazy from cents
|
||||
// Usd: f64 cents avg / 100
|
||||
let usd = LazyPerBlock {
|
||||
height: LazyVecFrom1::transformed::<CentsUnsignedToDollars>(
|
||||
height: LazyVecFrom1::transformed::<AvgCentsToUsd>(
|
||||
&format!("{full_name}_usd"),
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
),
|
||||
resolutions: Box::new(DerivedResolutions::from_derived_computed::<
|
||||
CentsUnsignedToDollars,
|
||||
>(
|
||||
&format!("{full_name}_usd"), version, ¢s.resolutions
|
||||
resolutions: Box::new(DerivedResolutions::from_derived_computed::<AvgCentsToUsd>(
|
||||
&format!("{full_name}_usd"),
|
||||
version,
|
||||
¢s.resolutions,
|
||||
)),
|
||||
};
|
||||
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::Height;
|
||||
use brk_types::{Height, StoredF32};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{DeltaAvg, LazyDeltaVec};
|
||||
|
||||
use crate::internal::{NumericValue, Resolutions};
|
||||
|
||||
/// A single lazy rolling-average slot from height: the lazy delta vec + its resolution views.
|
||||
/// Output is always StoredF32 regardless of input type T.
|
||||
#[derive(Clone, Traversable)]
|
||||
#[traversable(merge)]
|
||||
pub struct LazyRollingAvgFromHeight<T>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
{
|
||||
pub height: LazyDeltaVec<Height, T, T, DeltaAvg>,
|
||||
pub height: LazyDeltaVec<Height, T, StoredF32, DeltaAvg>,
|
||||
#[traversable(flatten)]
|
||||
pub resolutions: Box<Resolutions<T>>,
|
||||
pub resolutions: Box<Resolutions<StoredF32>>,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Version};
|
||||
use brk_types::{Height, StoredF32, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{DeltaAvg, LazyDeltaVec, ReadableCloneableVec};
|
||||
@@ -15,8 +15,7 @@ use super::LazyRollingAvgFromHeight;
|
||||
/// derived from a cumulative vec + cached window starts.
|
||||
///
|
||||
/// Nothing is stored on disk — all values are computed on-the-fly via
|
||||
/// `LazyDeltaVec<Height, T, T, DeltaAvg>`: `(cum[h] - cum[start-1]) / (h - start + 1)`.
|
||||
/// T is converted to f64 internally for division, then back to T.
|
||||
/// `LazyDeltaVec<Height, T, f64, DeltaAvg>`: `(cum[h] - cum[start-1]) / (h - start + 1)`.
|
||||
#[derive(Clone, Deref, DerefMut, Traversable)]
|
||||
#[traversable(transparent)]
|
||||
pub struct LazyRollingAvgsFromHeight<T>(pub Windows<LazyRollingAvgFromHeight<T>>)
|
||||
@@ -40,7 +39,7 @@ where
|
||||
let full_name = format!("{name}_{suffix}");
|
||||
let cached = cached_start.clone();
|
||||
let starts_version = cached.version();
|
||||
let avg = LazyDeltaVec::<Height, T, T, DeltaAvg>::new(
|
||||
let avg = LazyDeltaVec::<Height, T, StoredF32, DeltaAvg>::new(
|
||||
&full_name,
|
||||
version,
|
||||
cum_source.clone(),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use brk_types::{Bitcoin, Cents, CentsSigned, Dollars, Sats, SatsFract};
|
||||
use brk_types::{Bitcoin, Cents, CentsSigned, Dollars, Sats, SatsFract, StoredF32};
|
||||
use vecdb::{BinaryTransform, UnaryTransform};
|
||||
|
||||
pub struct SatsToBitcoin;
|
||||
@@ -10,6 +10,24 @@ impl UnaryTransform<Sats, Bitcoin> for SatsToBitcoin {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AvgSatsToBtc;
|
||||
|
||||
impl UnaryTransform<StoredF32, Bitcoin> for AvgSatsToBtc {
|
||||
#[inline(always)]
|
||||
fn apply(sats: StoredF32) -> Bitcoin {
|
||||
Bitcoin::from(f64::from(sats) / Sats::ONE_BTC_U128 as f64)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AvgCentsToUsd;
|
||||
|
||||
impl UnaryTransform<StoredF32, Dollars> for AvgCentsToUsd {
|
||||
#[inline(always)]
|
||||
fn apply(cents: StoredF32) -> Dollars {
|
||||
Dollars::from(f64::from(cents) / 100.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SatsToCents;
|
||||
|
||||
impl BinaryTransform<Sats, Cents, Cents> for SatsToCents {
|
||||
|
||||
@@ -16,7 +16,8 @@ pub use bps::{
|
||||
};
|
||||
pub use currency::{
|
||||
CentsSignedToDollars, CentsSubtractToCentsSigned, CentsTimesTenths, CentsUnsignedToDollars,
|
||||
CentsUnsignedToSats, DollarsToSatsFract, NegCentsUnsignedToDollars, SatsToBitcoin, SatsToCents,
|
||||
CentsUnsignedToSats, DollarsToSatsFract, AvgCentsToUsd, AvgSatsToBtc,
|
||||
NegCentsUnsignedToDollars, SatsToBitcoin, SatsToCents,
|
||||
};
|
||||
pub use derived::{
|
||||
Days1, Days7, Days30, Days365, DaysToYears, PriceTimesRatioBp32Cents, PriceTimesRatioCents,
|
||||
|
||||
Reference in New Issue
Block a user