mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Indexes, Sats, StoredF64, Version};
|
||||
use brk_types::{Bitcoin, Indexes, StoredF64, Version};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ActivityCore<M: StorageMode = Rw> {
|
||||
pub transfer_volume: PerBlockCumulativeWithSums<Sats, Sats, M>,
|
||||
pub transfer_volume: AmountPerBlockCumulativeWithSums<M>,
|
||||
pub coindays_destroyed: PerBlockCumulativeWithSums<StoredF64, StoredF64, M>,
|
||||
#[traversable(wrap = "transfer_volume", rename = "in_profit")]
|
||||
pub transfer_volume_in_profit: AmountPerBlockCumulativeWithSums<M>,
|
||||
@@ -33,6 +33,7 @@ impl ActivityCore {
|
||||
pub(crate) fn min_len(&self) -> usize {
|
||||
self.transfer_volume
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.len()
|
||||
.min(self.coindays_destroyed.base.height.len())
|
||||
@@ -45,7 +46,7 @@ impl ActivityCore {
|
||||
&mut self,
|
||||
state: &CohortState<impl RealizedOps, impl CostBasisOps>,
|
||||
) {
|
||||
self.transfer_volume.base.height.push(state.sent);
|
||||
self.transfer_volume.base.sats.height.push(state.sent);
|
||||
self.coindays_destroyed.base.height.push(
|
||||
StoredF64::from(Bitcoin::from(state.satdays_destroyed)),
|
||||
);
|
||||
@@ -63,7 +64,8 @@ impl ActivityCore {
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.transfer_volume.base.height as &mut dyn AnyStoredVec,
|
||||
&mut self.transfer_volume.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.transfer_volume.base.cents.height,
|
||||
&mut self.coindays_destroyed.base.height,
|
||||
&mut self.transfer_volume_in_profit.base.sats.height,
|
||||
&mut self.transfer_volume_in_profit.base.cents.height,
|
||||
@@ -82,11 +84,11 @@ impl ActivityCore {
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.transfer_volume.base.height.compute_sum_of_others(
|
||||
self.transfer_volume.base.sats.height.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others
|
||||
.iter()
|
||||
.map(|v| &v.transfer_volume.base.height)
|
||||
.map(|v| &v.transfer_volume.base.sats.height)
|
||||
.collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
@@ -100,11 +102,12 @@ impl ActivityCore {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.transfer_volume
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
.compute_rest(starting_indexes.height, prices, exit)?;
|
||||
self.coindays_destroyed
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
Ok(())
|
||||
|
||||
@@ -6,7 +6,10 @@ use vecdb::{AnyStoredVec, Exit, ReadableCloneableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{Identity, LazyPerBlock, PerBlock};
|
||||
|
||||
use crate::distribution::{metrics::ImportConfig, state::{CohortState, CostBasisOps, RealizedOps}};
|
||||
use crate::{
|
||||
distribution::{metrics::ImportConfig, state::{CohortState, CostBasisOps, RealizedOps}},
|
||||
prices,
|
||||
};
|
||||
|
||||
use super::ActivityCore;
|
||||
|
||||
@@ -71,10 +74,11 @@ impl ActivityFull {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.inner.compute_rest_part1(starting_indexes, exit)
|
||||
self.inner.compute_rest_part1(prices, starting_indexes, exit)
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part2(
|
||||
@@ -85,7 +89,7 @@ impl ActivityFull {
|
||||
self.dormancy.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.inner.coindays_destroyed.base.height,
|
||||
&self.inner.transfer_volume.base.height,
|
||||
&self.inner.transfer_volume.base.sats.height,
|
||||
|(i, cdd, sent_sats, ..)| {
|
||||
let sent_btc = f64::from(Bitcoin::from(sent_sats));
|
||||
if sent_btc == 0.0 {
|
||||
|
||||
@@ -8,7 +8,10 @@ use brk_error::Result;
|
||||
use brk_types::{Indexes, Version};
|
||||
use vecdb::Exit;
|
||||
|
||||
use crate::distribution::state::{CohortState, CostBasisOps, RealizedOps};
|
||||
use crate::{
|
||||
distribution::state::{CohortState, CostBasisOps, RealizedOps},
|
||||
prices,
|
||||
};
|
||||
|
||||
pub trait ActivityLike: Send + Sync {
|
||||
fn as_core(&self) -> &ActivityCore;
|
||||
@@ -27,6 +30,7 @@ pub trait ActivityLike: Send + Sync {
|
||||
) -> Result<()>;
|
||||
fn compute_rest_part1(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()>;
|
||||
@@ -45,8 +49,8 @@ impl ActivityLike for ActivityCore {
|
||||
fn compute_from_stateful(&mut self, starting_indexes: &Indexes, others: &[&ActivityCore], exit: &Exit) -> Result<()> {
|
||||
self.compute_from_stateful(starting_indexes, others, exit)
|
||||
}
|
||||
fn compute_rest_part1(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(starting_indexes, exit)
|
||||
fn compute_rest_part1(&mut self, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(prices, starting_indexes, exit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +67,7 @@ impl ActivityLike for ActivityFull {
|
||||
fn compute_from_stateful(&mut self, starting_indexes: &Indexes, others: &[&ActivityCore], exit: &Exit) -> Result<()> {
|
||||
self.compute_from_stateful(starting_indexes, others, exit)
|
||||
}
|
||||
fn compute_rest_part1(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(starting_indexes, exit)
|
||||
fn compute_rest_part1(&mut self, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(prices, starting_indexes, exit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ impl CoreCohortMetrics {
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
|
||||
self.activity
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
.compute_rest_part1(prices, starting_indexes, exit)?;
|
||||
self.activity
|
||||
.compute_sent_profitability(prices, starting_indexes, exit)?;
|
||||
|
||||
|
||||
@@ -223,7 +223,7 @@ pub trait CohortMetricsBase:
|
||||
self.supply_mut()
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.activity_mut()
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
.compute_rest_part1(prices, starting_indexes, exit)?;
|
||||
self.activity_core_mut()
|
||||
.compute_sent_profitability(prices, starting_indexes, exit)?;
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ pub struct Computer<M: StorageMode = Rw> {
|
||||
pub mining: Box<mining::Vecs<M>>,
|
||||
pub transactions: Box<transactions::Vecs<M>>,
|
||||
pub scripts: Box<scripts::Vecs<M>>,
|
||||
#[traversable(hidden)]
|
||||
pub positions: Box<positions::Vecs<M>>,
|
||||
pub cointime: Box<cointime::Vecs<M>>,
|
||||
pub constants: Box<constants::Vecs>,
|
||||
@@ -493,10 +492,10 @@ impl Computer {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_iter_named_exportable {
|
||||
macro_rules! impl_iter_named {
|
||||
($($field:ident),+ $(,)?) => {
|
||||
impl_iter_named_exportable!(@mode Ro, $($field),+);
|
||||
impl_iter_named_exportable!(@mode Rw, $($field),+);
|
||||
impl_iter_named!(@mode Ro, $($field),+);
|
||||
impl_iter_named!(@mode Rw, $($field),+);
|
||||
};
|
||||
(@mode $mode:ty, $($field:ident),+) => {
|
||||
impl Computer<$mode> {
|
||||
@@ -507,11 +506,19 @@ macro_rules! impl_iter_named_exportable {
|
||||
std::iter::empty()
|
||||
$(.chain(self.$field.iter_any_exportable().map(|v| ($field::DB_NAME, v))))+
|
||||
}
|
||||
|
||||
pub fn iter_named_visible(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&'static str, &dyn AnyExportableVec)> {
|
||||
use brk_traversable::Traversable;
|
||||
std::iter::empty()
|
||||
$(.chain(self.$field.iter_any_visible().map(|v| ($field::DB_NAME, v))))+
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_iter_named_exportable!(blocks, mining, transactions, scripts, positions, cointime,
|
||||
impl_iter_named!(blocks, mining, transactions, scripts, positions, cointime,
|
||||
constants, indicators, indexes, market, pools, prices, distribution, supply, inputs, outputs);
|
||||
|
||||
fn timed<T>(label: &str, f: impl FnOnce() -> T) -> T {
|
||||
|
||||
@@ -25,7 +25,7 @@ impl Vecs {
|
||||
)?,
|
||||
unspent: PerBlock::forced_import(
|
||||
db,
|
||||
"exact_utxo_count",
|
||||
"utxo_count_bis",
|
||||
version,
|
||||
indexes,
|
||||
)?,
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::internal::db_utils::{finalize_db, open_db};
|
||||
pub const DB_NAME: &str = "positions";
|
||||
|
||||
#[derive(Traversable)]
|
||||
#[traversable(hidden)]
|
||||
pub struct Vecs<M: StorageMode = Rw> {
|
||||
db: Database,
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ impl Vecs {
|
||||
Ok(Self {
|
||||
transfer_volume: AmountPerBlockCumulativeWithSums::forced_import(
|
||||
db,
|
||||
"exact_transfer_volume",
|
||||
"transfer_volume_bis",
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
|
||||
Reference in New Issue
Block a user