global: refactor

This commit is contained in:
nym21
2026-04-22 22:23:39 +02:00
parent c5b16e7048
commit 84e924b77e
63 changed files with 726 additions and 257 deletions
+2 -2
View File
@@ -1,5 +1,4 @@
pub(crate) mod algo;
mod amount;
mod block_walker;
mod cache_budget;
mod containers;
@@ -9,9 +8,9 @@ mod per_block;
mod per_tx;
mod traits;
mod transform;
mod value;
mod with_addr_types;
pub(crate) use amount::*;
pub(crate) use block_walker::*;
pub(crate) use cache_budget::*;
pub(crate) use containers::*;
@@ -20,4 +19,5 @@ pub(crate) use per_block::*;
pub(crate) use per_tx::*;
pub(crate) use traits::*;
pub use transform::*;
pub(crate) use value::*;
pub(crate) use with_addr_types::*;
@@ -12,27 +12,27 @@ use crate::{
};
/// Trait that associates a cents type with its transform to Dollars.
pub trait CentsType: NumericValue + JsonSchema {
pub trait FiatType: NumericValue + JsonSchema {
type ToDollars: UnaryTransform<Self, Dollars>;
}
impl CentsType for Cents {
impl FiatType for Cents {
type ToDollars = CentsUnsignedToDollars;
}
impl CentsType for CentsSigned {
impl FiatType for CentsSigned {
type ToDollars = CentsSignedToDollars;
}
/// Height-indexed fiat monetary value: cents (eager, integer) + usd (lazy, float).
/// Generic over `C` to support both `Cents` (unsigned) and `CentsSigned` (signed).
#[derive(Traversable)]
pub struct FiatPerBlock<C: CentsType, M: StorageMode = Rw> {
pub struct FiatPerBlock<C: FiatType, M: StorageMode = Rw> {
pub usd: LazyPerBlock<Dollars, C>,
pub cents: PerBlock<C, M>,
}
impl<C: CentsType> FiatPerBlock<C> {
impl<C: FiatType> FiatPerBlock<C> {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -5,16 +5,16 @@ use vecdb::{
Database, EagerVec, ImportableVec, LazyVecFrom1, PcoVec, ReadableCloneableVec, Rw, StorageMode,
};
use super::CentsType;
use super::FiatType;
/// Raw per-block fiat data: cents (stored) + usd (lazy), no resolutions.
#[derive(Traversable)]
pub struct FiatBlock<C: CentsType, M: StorageMode = Rw> {
pub struct FiatBlock<C: FiatType, M: StorageMode = Rw> {
pub usd: LazyVecFrom1<Height, Dollars, Height, C>,
pub cents: M::Stored<EagerVec<PcoVec<Height, C>>>,
}
impl<C: CentsType> FiatBlock<C> {
impl<C: FiatType> FiatBlock<C> {
pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result<Self> {
let cents: EagerVec<PcoVec<Height, C>> =
EagerVec::forced_import(db, &format!("{name}_cents"), version)?;
@@ -6,18 +6,18 @@ use vecdb::{Database, Exit, Rw, StorageMode};
use crate::{
indexes,
internal::{
CentsType, FiatBlock, FiatPerBlock, LazyRollingSumsFiatFromHeight, WindowStartVec, Windows,
FiatType, FiatBlock, FiatPerBlock, LazyRollingSumsFiatFromHeight, WindowStartVec, Windows,
},
};
#[derive(Traversable)]
pub struct FiatPerBlockCumulativeWithSums<C: CentsType, M: StorageMode = Rw> {
pub struct FiatPerBlockCumulativeWithSums<C: FiatType, M: StorageMode = Rw> {
pub block: FiatBlock<C, M>,
pub cumulative: FiatPerBlock<C, M>,
pub sum: LazyRollingSumsFiatFromHeight<C>,
}
impl<C: CentsType> FiatPerBlockCumulativeWithSums<C> {
impl<C: FiatType> FiatPerBlockCumulativeWithSums<C> {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -9,13 +9,13 @@ use crate::{
internal::{BpsType, LazyRollingDeltasFiatFromHeight, WindowStartVec, Windows},
};
use super::{CentsType, FiatPerBlockCumulativeWithSums};
use super::{FiatType, FiatPerBlockCumulativeWithSums};
#[derive(Deref, DerefMut, Traversable)]
pub struct FiatPerBlockCumulativeWithSumsAndDeltas<C, CS, B, M: StorageMode = Rw>
where
C: CentsType + Into<f64>,
CS: CentsType + From<f64>,
C: FiatType + Into<f64>,
CS: FiatType + From<f64>,
B: BpsType + From<f64>,
{
#[deref]
@@ -27,8 +27,8 @@ where
impl<C, CS, B> FiatPerBlockCumulativeWithSumsAndDeltas<C, CS, B>
where
C: CentsType + Into<f64>,
CS: CentsType + From<f64>,
C: FiatType + Into<f64>,
CS: FiatType + From<f64>,
B: BpsType + From<f64>,
{
pub(crate) fn forced_import(
@@ -2,17 +2,17 @@ use brk_traversable::Traversable;
use brk_types::{Dollars, Version};
use vecdb::ReadableCloneableVec;
use crate::internal::{CentsType, Identity, LazyPerBlock, NumericValue, PerBlock};
use crate::internal::{FiatType, Identity, LazyPerBlock, NumericValue, PerBlock};
/// Lazy fiat: both cents and usd are lazy views of a stored source.
/// Zero extra stored vecs.
#[derive(Clone, Traversable)]
pub struct LazyFiatPerBlock<C: CentsType> {
pub struct LazyFiatPerBlock<C: FiatType> {
pub usd: LazyPerBlock<Dollars, C>,
pub cents: LazyPerBlock<C, C>,
}
impl<C: CentsType> LazyFiatPerBlock<C> {
impl<C: FiatType> LazyFiatPerBlock<C> {
pub(crate) fn from_computed(name: &str, version: Version, source: &PerBlock<C>) -> Self
where
C: NumericValue,
@@ -6,24 +6,24 @@ use vecdb::{DeltaSub, LazyDeltaVec, LazyVecFrom1, ReadOnlyClone, ReadableCloneab
use crate::{
indexes,
internal::{
CentsType, DerivedResolutions, LazyPerBlock, LazyRollingSumFromHeight, Resolutions,
FiatType, DerivedResolutions, LazyPerBlock, LazyRollingSumFromHeight, Resolutions,
WindowStartVec, Windows,
},
};
#[derive(Clone, Traversable)]
pub struct LazyRollingSumFiatFromHeight<C: CentsType> {
pub struct LazyRollingSumFiatFromHeight<C: FiatType> {
pub usd: LazyPerBlock<Dollars, C>,
pub cents: LazyRollingSumFromHeight<C>,
}
#[derive(Clone, Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct LazyRollingSumsFiatFromHeight<C: CentsType>(
pub struct LazyRollingSumsFiatFromHeight<C: FiatType>(
pub Windows<LazyRollingSumFiatFromHeight<C>>,
);
impl<C: CentsType> LazyRollingSumsFiatFromHeight<C> {
impl<C: FiatType> LazyRollingSumsFiatFromHeight<C> {
pub fn new(
name: &str,
version: Version,
@@ -10,13 +10,13 @@ use crate::{
internal::{BpsType, LazyRollingDeltasFiatFromHeight, WindowStartVec, Windows},
};
use super::{CentsType, FiatPerBlock};
use super::{FiatType, FiatPerBlock};
#[derive(Deref, DerefMut, Traversable)]
pub struct FiatPerBlockWithDeltas<C, CS, B, M: StorageMode = Rw>
where
C: CentsType + Into<f64>,
CS: CentsType + From<f64>,
C: FiatType + Into<f64>,
CS: FiatType + From<f64>,
B: BpsType + From<f64>,
{
#[deref]
@@ -28,8 +28,8 @@ where
impl<C, CS, B> FiatPerBlockWithDeltas<C, CS, B>
where
C: CentsType + JsonSchema + Into<f64>,
CS: CentsType + From<f64>,
C: FiatType + JsonSchema + Into<f64>,
CS: FiatType + From<f64>,
B: BpsType + From<f64>,
{
pub(crate) fn forced_import(
@@ -1,4 +1,3 @@
mod amount;
mod computed;
mod fiat;
mod lazy;
@@ -8,8 +7,8 @@ mod price;
mod ratio;
mod rolling;
mod stddev;
mod value;
pub use amount::*;
pub use computed::*;
pub use fiat::*;
pub use lazy::*;
@@ -19,3 +18,4 @@ pub use price::*;
pub use ratio::*;
pub use rolling::*;
pub use stddev::*;
pub use value::*;
@@ -1,5 +1,5 @@
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, StoredF32, Version};
use brk_types::{Bitcoin, Dollars, Height, StoredF32, Version};
use derive_more::{Deref, DerefMut};
use schemars::JsonSchema;
use vecdb::{
@@ -10,8 +10,8 @@ use vecdb::{
use crate::{
indexes,
internal::{
BpsType, CentsType, DerivedResolutions, LazyPerBlock, NumericValue, Percent, Resolutions,
WindowStartVec, Windows,
AmountType, BpsType, DerivedResolutions, FiatType, LazyPerBlock, NumericValue, Percent,
Resolutions, WindowStartVec, Windows,
},
};
@@ -160,6 +160,152 @@ where
}
}
// ---------------------------------------------------------------------------
// Amount delta types (sats change + lazy BTC + rate)
// ---------------------------------------------------------------------------
/// Single-slot amount delta change: sats delta + lazy BTC.
#[derive(Clone, Traversable)]
pub struct LazyDeltaAmountFromHeight<S, C>
where
S: VecValue,
C: AmountType,
{
pub btc: LazyPerBlock<Bitcoin, C>,
pub sats: LazyDeltaFromHeight<S, C, DeltaChange>,
}
/// Lazy amount rolling deltas for all 4 windows.
///
/// Tree shape: `absolute._24h.{sats,btc}/...`, `rate._24h/...` — mirrors
/// `LazyRollingDeltasFiatFromHeight` but stores sats instead of cents and
/// derives a lazy BTC view alongside (free, since sats → btc is a scalar
/// transform).
#[derive(Clone, Traversable)]
pub struct LazyRollingDeltasAmountFromHeight<S, C, B>
where
S: VecValue,
C: AmountType,
B: BpsType,
{
pub absolute: Windows<LazyDeltaAmountFromHeight<S, C>>,
pub rate: Windows<LazyDeltaPercentFromHeight<S, B>>,
}
impl<S, C, B> LazyRollingDeltasAmountFromHeight<S, C, B>
where
S: VecValue + Into<f64>,
C: AmountType + From<f64>,
B: BpsType + From<f64>,
{
pub fn new(
name: &str,
version: Version,
source: &(impl ReadableCloneableVec<Height, S> + 'static),
cached_starts: &Windows<&WindowStartVec>,
indexes: &indexes::Vecs,
) -> Self {
let src = source.read_only_boxed_clone();
let make_slot = |suffix: &str, cached_start: &&WindowStartVec| {
let full_name = format!("{name}_{suffix}");
let cached = cached_start.read_only_clone();
let starts_version = cached.version();
// Absolute change (sats): source[h] - source[ago] as C (via f64)
let sats_name = format!("{full_name}_sats");
let change_vec = LazyDeltaVec::<Height, S, C, DeltaChange>::new(
&sats_name,
version,
src.clone(),
starts_version,
{
let cached = cached.clone();
move || cached.cached()
},
);
let change_resolutions =
Resolutions::forced_import(&sats_name, change_vec.clone(), version, indexes);
let sats = LazyDeltaFromHeight {
height: change_vec,
resolutions: Box::new(change_resolutions),
};
// Absolute change (btc): lazy from sats delta
let btc = LazyPerBlock {
height: LazyVecFrom1::transformed::<C::ToBitcoin>(
&full_name,
version,
sats.height.read_only_boxed_clone(),
),
resolutions: Box::new(DerivedResolutions::from_derived_computed::<C::ToBitcoin>(
&full_name,
version,
&sats.resolutions,
)),
};
let absolute = LazyDeltaAmountFromHeight { btc, sats };
// Rate BPS: (source[h] - source[ago]) / source[ago] as B (via f64)
let rate_bps_name = format!("{full_name}_rate_bps");
let rate_vec = LazyDeltaVec::<Height, S, B, DeltaRate>::new(
&rate_bps_name,
version,
src.clone(),
starts_version,
move || cached.cached(),
);
let rate_resolutions =
Resolutions::forced_import(&rate_bps_name, rate_vec.clone(), version, indexes);
let bps = LazyDeltaFromHeight {
height: rate_vec,
resolutions: Box::new(rate_resolutions),
};
let rate_ratio_name = format!("{full_name}_rate_ratio");
let ratio = LazyPerBlock {
height: LazyVecFrom1::transformed::<B::ToRatio>(
&rate_ratio_name,
version,
bps.height.read_only_boxed_clone(),
),
resolutions: Box::new(DerivedResolutions::from_derived_computed::<B::ToRatio>(
&rate_ratio_name,
version,
&bps.resolutions,
)),
};
let rate_name = format!("{full_name}_rate");
let percent = LazyPerBlock {
height: LazyVecFrom1::transformed::<B::ToPercent>(
&rate_name,
version,
bps.height.read_only_boxed_clone(),
),
resolutions: Box::new(DerivedResolutions::from_derived_computed::<B::ToPercent>(
&rate_name,
version,
&bps.resolutions,
)),
};
let rate = LazyDeltaPercentFromHeight(Percent {
bps,
ratio,
percent,
});
(absolute, rate)
};
let (absolute, rate) = cached_starts.map_with_suffix(make_slot).unzip();
Self { absolute, rate }
}
}
// ---------------------------------------------------------------------------
// Fiat delta types (cents change + lazy USD + rate)
// ---------------------------------------------------------------------------
@@ -169,7 +315,7 @@ where
pub struct LazyDeltaFiatFromHeight<S, C>
where
S: VecValue,
C: CentsType,
C: FiatType,
{
pub usd: LazyPerBlock<Dollars, C>,
pub cents: LazyDeltaFromHeight<S, C, DeltaChange>,
@@ -184,7 +330,7 @@ where
pub struct LazyRollingDeltasFiatFromHeight<S, C, B>
where
S: VecValue,
C: CentsType,
C: FiatType,
B: BpsType,
{
pub absolute: Windows<LazyDeltaFiatFromHeight<S, C>>,
@@ -194,7 +340,7 @@ where
impl<S, C, B> LazyRollingDeltasFiatFromHeight<S, C, B>
where
S: VecValue + Into<f64>,
C: CentsType + From<f64>,
C: FiatType + From<f64>,
B: BpsType + From<f64>,
{
pub fn new(
@@ -1,23 +1,40 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
use vecdb::{Database, Exit, ReadableCloneableVec, Rw, StorageMode};
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, SatsSigned, Version};
use schemars::JsonSchema;
use vecdb::{Database, Exit, ReadableCloneableVec, Rw, StorageMode, UnaryTransform};
use crate::{
indexes,
internal::{CentsUnsignedToDollars, LazyPerBlock, PerBlock, SatsToBitcoin, SatsToCents},
internal::{
CentsUnsignedToDollars, LazyPerBlock, NumericValue, PerBlock, SatsSignedToBitcoin,
SatsToBitcoin, SatsToCents,
},
prices,
};
/// Trait that associates a sats type with its transform to Bitcoin.
pub trait AmountType: NumericValue + JsonSchema {
type ToBitcoin: UnaryTransform<Self, Bitcoin>;
}
impl AmountType for Sats {
type ToBitcoin = SatsToBitcoin;
}
impl AmountType for SatsSigned {
type ToBitcoin = SatsSignedToBitcoin;
}
#[derive(Traversable)]
pub struct AmountPerBlock<M: StorageMode = Rw> {
pub struct ValuePerBlock<M: StorageMode = Rw> {
pub btc: LazyPerBlock<Bitcoin, Sats>,
pub sats: PerBlock<Sats, M>,
pub usd: LazyPerBlock<Dollars, Cents>,
pub cents: PerBlock<Cents, M>,
}
impl AmountPerBlock {
impl ValuePerBlock {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -13,14 +13,14 @@ use crate::{
/// Raw per-block amount data: sats + cents (stored), btc + usd (lazy), no resolutions.
#[derive(Traversable)]
pub struct AmountBlock<M: StorageMode = Rw> {
pub struct ValueBlock<M: StorageMode = Rw> {
pub btc: LazyVecFrom1<Height, Bitcoin, Height, Sats>,
pub sats: M::Stored<EagerVec<PcoVec<Height, Sats>>>,
pub usd: LazyVecFrom1<Height, Dollars, Height, Cents>,
pub cents: M::Stored<EagerVec<PcoVec<Height, Cents>>>,
}
impl AmountBlock {
impl ValueBlock {
pub(crate) fn forced_import(db: &Database, name: &str, version: Version) -> Result<Self> {
let sats: EagerVec<PcoVec<Height, Sats>> =
EagerVec::forced_import(db, &format!("{name}_sats"), version)?;
@@ -5,19 +5,19 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{AmountBlock, AmountPerBlock},
internal::{ValueBlock, ValuePerBlock},
prices,
};
#[derive(Traversable)]
pub struct AmountPerBlockCumulative<M: StorageMode = Rw> {
pub block: AmountBlock<M>,
pub cumulative: AmountPerBlock<M>,
pub struct ValuePerBlockCumulative<M: StorageMode = Rw> {
pub block: ValueBlock<M>,
pub cumulative: ValuePerBlock<M>,
}
const VERSION: Version = Version::ONE;
impl AmountPerBlockCumulative {
impl ValuePerBlockCumulative {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -27,8 +27,8 @@ impl AmountPerBlockCumulative {
let v = version + VERSION;
Ok(Self {
block: AmountBlock::forced_import(db, name, v)?,
cumulative: AmountPerBlock::forced_import(
block: ValueBlock::forced_import(db, name, v)?,
cumulative: ValuePerBlock::forced_import(
db,
&format!("{name}_cumulative"),
v,
@@ -7,25 +7,25 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{
AmountPerBlockCumulative, LazyRollingAvgsAmountFromHeight, LazyRollingSumsAmountFromHeight,
ValuePerBlockCumulative, LazyRollingAvgsAmountFromHeight, LazyRollingSumsAmountFromHeight,
WindowStartVec, Windows,
},
prices,
};
#[derive(Deref, DerefMut, Traversable)]
pub struct AmountPerBlockCumulativeRolling<M: StorageMode = Rw> {
pub struct ValuePerBlockCumulativeRolling<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub inner: AmountPerBlockCumulative<M>,
pub inner: ValuePerBlockCumulative<M>,
pub sum: LazyRollingSumsAmountFromHeight,
pub average: LazyRollingAvgsAmountFromHeight,
}
const VERSION: Version = Version::TWO;
impl AmountPerBlockCumulativeRolling {
impl ValuePerBlockCumulativeRolling {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -35,7 +35,7 @@ impl AmountPerBlockCumulativeRolling {
) -> Result<Self> {
let v = version + VERSION;
let inner = AmountPerBlockCumulative::forced_import(db, name, v, indexes)?;
let inner = ValuePerBlockCumulative::forced_import(db, name, v, indexes)?;
let sum = LazyRollingSumsAmountFromHeight::new(
&format!("{name}_sum"),
v,
@@ -7,25 +7,25 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{
AmountPerBlockCumulativeRolling, RollingDistributionAmountPerBlock, WindowStartVec,
ValuePerBlockCumulativeRolling, RollingDistributionValuePerBlock, WindowStartVec,
WindowStarts, Windows,
},
prices,
};
#[derive(Deref, DerefMut, Traversable)]
pub struct AmountPerBlockFull<M: StorageMode = Rw> {
pub struct ValuePerBlockFull<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub inner: AmountPerBlockCumulativeRolling<M>,
pub inner: ValuePerBlockCumulativeRolling<M>,
#[traversable(flatten)]
pub distribution: RollingDistributionAmountPerBlock<M>,
pub distribution: RollingDistributionValuePerBlock<M>,
}
const VERSION: Version = Version::TWO;
impl AmountPerBlockFull {
impl ValuePerBlockFull {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -36,8 +36,8 @@ impl AmountPerBlockFull {
let v = version + VERSION;
let inner =
AmountPerBlockCumulativeRolling::forced_import(db, name, v, indexes, cached_starts)?;
let distribution = RollingDistributionAmountPerBlock::forced_import(db, name, v, indexes)?;
ValuePerBlockCumulativeRolling::forced_import(db, name, v, indexes, cached_starts)?;
let distribution = RollingDistributionValuePerBlock::forced_import(db, name, v, indexes)?;
Ok(Self {
inner,
@@ -1,4 +1,4 @@
//! Lazy value wrapper for AmountPerBlock - all transforms are lazy.
//! Lazy value wrapper for ValuePerBlock - all transforms are lazy.
use brk_traversable::Traversable;
use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version};
@@ -6,22 +6,22 @@ use derive_more::{Deref, DerefMut};
use vecdb::UnaryTransform;
use crate::internal::{
AmountPerBlock, Identity, LazyAmount, LazyAmountDerivedResolutions, SatsToBitcoin,
ValuePerBlock, Identity, LazyValue, LazyValueDerivedResolutions, SatsToBitcoin,
};
/// Lazy value wrapper with height + all derived last transforms from AmountPerBlock.
/// Lazy value wrapper with height + all derived last transforms from ValuePerBlock.
#[derive(Clone, Deref, DerefMut, Traversable)]
#[traversable(merge)]
pub struct LazyAmountPerBlock {
pub struct LazyValuePerBlock {
#[traversable(flatten)]
pub height: LazyAmount<Height>,
pub height: LazyValue<Height>,
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub resolutions: Box<LazyAmountDerivedResolutions>,
pub resolutions: Box<LazyValueDerivedResolutions>,
}
impl LazyAmountPerBlock {
impl LazyValuePerBlock {
pub(crate) fn from_block_source<
SatsTransform,
BitcoinTransform,
@@ -29,7 +29,7 @@ impl LazyAmountPerBlock {
DollarsTransform,
>(
name: &str,
source: &AmountPerBlock,
source: &ValuePerBlock,
version: Version,
) -> Self
where
@@ -38,14 +38,14 @@ impl LazyAmountPerBlock {
CentsTransform: UnaryTransform<Cents, Cents>,
DollarsTransform: UnaryTransform<Dollars, Dollars>,
{
let height = LazyAmount::from_block_source::<
let height = LazyValue::from_block_source::<
SatsTransform,
BitcoinTransform,
CentsTransform,
DollarsTransform,
>(name, source, version);
let resolutions = LazyAmountDerivedResolutions::from_block_source::<
let resolutions = LazyValueDerivedResolutions::from_block_source::<
SatsTransform,
BitcoinTransform,
CentsTransform,
@@ -58,7 +58,7 @@ impl LazyAmountPerBlock {
}
}
pub(crate) fn identity(name: &str, source: &AmountPerBlock, version: Version) -> Self {
pub(crate) fn identity(name: &str, source: &ValuePerBlock, version: Version) -> Self {
Self::from_block_source::<Identity<Sats>, SatsToBitcoin, Identity<Cents>, Identity<Dollars>>(
name, source, version,
)
@@ -2,17 +2,17 @@ use brk_traversable::Traversable;
use brk_types::{Bitcoin, Cents, Dollars, Sats, Version};
use vecdb::UnaryTransform;
use crate::internal::{AmountPerBlock, DerivedResolutions};
use crate::internal::{ValuePerBlock, DerivedResolutions};
#[derive(Clone, Traversable)]
pub struct LazyAmountDerivedResolutions {
pub struct LazyValueDerivedResolutions {
pub btc: DerivedResolutions<Bitcoin, Sats>,
pub sats: DerivedResolutions<Sats, Sats>,
pub usd: DerivedResolutions<Dollars, Dollars>,
pub cents: DerivedResolutions<Cents, Cents>,
}
impl LazyAmountDerivedResolutions {
impl LazyValueDerivedResolutions {
pub(crate) fn from_block_source<
SatsTransform,
BitcoinTransform,
@@ -20,7 +20,7 @@ impl LazyAmountDerivedResolutions {
DollarsTransform,
>(
name: &str,
source: &AmountPerBlock,
source: &ValuePerBlock,
version: Version,
) -> Self
where
@@ -7,7 +7,7 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode};
use crate::{
indexes,
internal::{
AmountPerBlock, DistributionStats, WindowStarts, Windows,
ValuePerBlock, DistributionStats, WindowStarts, Windows,
algo::compute_rolling_distribution_from_starts,
},
};
@@ -18,11 +18,11 @@ use crate::{
/// Series: `{name}_average_24h`, `{name}_max_24h`, etc.
#[derive(Deref, DerefMut, Traversable)]
#[traversable(transparent)]
pub struct RollingDistributionAmountPerBlock<M: StorageMode = Rw>(
pub DistributionStats<Windows<AmountPerBlock<M>>>,
pub struct RollingDistributionValuePerBlock<M: StorageMode = Rw>(
pub DistributionStats<Windows<ValuePerBlock<M>>>,
);
impl RollingDistributionAmountPerBlock {
impl RollingDistributionValuePerBlock {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -31,7 +31,7 @@ impl RollingDistributionAmountPerBlock {
) -> Result<Self> {
Ok(Self(DistributionStats::try_from_fn(|stat_suffix| {
Windows::try_from_fn(|window_suffix| {
AmountPerBlock::forced_import(
ValuePerBlock::forced_import(
db,
&format!("{name}_{stat_suffix}_{window_suffix}"),
version,
@@ -6,19 +6,19 @@ use vecdb::{Database, Rw, StorageMode};
use crate::{
indexes,
internal::{AmountPerBlock, LazyRollingDeltasFromHeight, WindowStartVec, Windows},
internal::{LazyRollingDeltasAmountFromHeight, ValuePerBlock, WindowStartVec, Windows},
};
#[derive(Deref, DerefMut, Traversable)]
pub struct AmountPerBlockWithDeltas<M: StorageMode = Rw> {
pub struct ValuePerBlockWithDeltas<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub inner: AmountPerBlock<M>,
pub delta: LazyRollingDeltasFromHeight<Sats, SatsSigned, BasisPointsSigned32>,
pub inner: ValuePerBlock<M>,
pub delta: LazyRollingDeltasAmountFromHeight<Sats, SatsSigned, BasisPointsSigned32>,
}
impl AmountPerBlockWithDeltas {
impl ValuePerBlockWithDeltas {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -26,9 +26,9 @@ impl AmountPerBlockWithDeltas {
indexes: &indexes::Vecs,
cached_starts: &Windows<&WindowStartVec>,
) -> Result<Self> {
let inner = AmountPerBlock::forced_import(db, name, version, indexes)?;
let inner = ValuePerBlock::forced_import(db, name, version, indexes)?;
let delta = LazyRollingDeltasFromHeight::new(
let delta = LazyRollingDeltasAmountFromHeight::new(
&format!("{name}_delta"),
version + Version::ONE,
&inner.sats.height,
@@ -17,7 +17,7 @@ pub use bps::{
pub use currency::{
AvgCentsToUsd, AvgSatsToBtc, CentsSignedToDollars, CentsSubtractToCentsSigned,
CentsTimesTenths, CentsUnsignedToDollars, CentsUnsignedToSats, DollarsToSatsFract,
NegCentsUnsignedToDollars, SatsToBitcoin, SatsToCents,
NegCentsUnsignedToDollars, SatsSignedToBitcoin, SatsToBitcoin, SatsToCents,
};
pub use derived::{
Days1, Days7, Days30, Days365, DaysToYears, PriceTimesRatioBp32Cents, PriceTimesRatioCents,
@@ -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::AmountPerBlock;
use crate::internal::ValuePerBlock;
/// Fully lazy value type at height level.
///
/// All fields are lazy transforms from existing sources - no storage.
#[derive(Clone, Traversable)]
pub struct LazyAmount<I: VecIndex> {
pub struct LazyValue<I: VecIndex> {
pub btc: LazyVecFrom1<I, Bitcoin, I, Sats>,
pub sats: LazyVecFrom1<I, Sats, I, Sats>,
pub usd: LazyVecFrom1<I, Dollars, I, Dollars>,
pub cents: LazyVecFrom1<I, Cents, I, Cents>,
}
impl LazyAmount<Height> {
impl LazyValue<Height> {
pub(crate) fn from_block_source<
SatsTransform,
BitcoinTransform,
@@ -23,7 +23,7 @@ impl LazyAmount<Height> {
DollarsTransform,
>(
name: &str,
source: &AmountPerBlock,
source: &ValuePerBlock,
version: Version,
) -> Self
where
@@ -13,7 +13,7 @@ use vecdb::{AnyStoredVec, AnyVec, Database, EagerVec, Exit, PcoVec, WritableVec}
use crate::{indexes, prices};
use super::{
AmountPerBlock, BpsType, NumericValue, PerBlock, PerBlockCumulativeRolling, PercentPerBlock,
ValuePerBlock, BpsType, NumericValue, PerBlock, PerBlockCumulativeRolling, PercentPerBlock,
WindowStartVec, Windows,
};
@@ -176,16 +176,16 @@ where
}
}
impl WithAddrTypes<AmountPerBlock> {
impl WithAddrTypes<ValuePerBlock> {
pub(crate) fn forced_import(
db: &Database,
name: &str,
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let all = AmountPerBlock::forced_import(db, name, version, indexes)?;
let all = ValuePerBlock::forced_import(db, name, version, indexes)?;
let by_addr_type = ByAddrType::new_with_name(|type_name| {
AmountPerBlock::forced_import(db, &format!("{type_name}_{name}"), version, indexes)
ValuePerBlock::forced_import(db, &format!("{type_name}_{name}"), version, indexes)
})?;
Ok(Self { all, by_addr_type })
}