computer: percentiles snapshot

This commit is contained in:
nym21
2025-12-02 19:40:47 +01:00
parent b6ec133368
commit 24248215e9
29 changed files with 2535 additions and 2425 deletions

View File

@@ -314,7 +314,7 @@ impl Vecs {
stateful: &stateful::Vecs,
exit: &Exit,
) -> Result<()> {
let circulating_supply = &stateful.utxo_cohorts.all.1.height_to_supply;
let circulating_supply = &stateful.utxo_cohorts.all.inner.height_to_supply;
self.indexes_to_coinblocks_created
.compute_all(indexes, starting_indexes, exit, |vec| {
@@ -328,7 +328,7 @@ impl Vecs {
})?;
let indexes_to_coinblocks_destroyed =
&stateful.utxo_cohorts.all.1.indexes_to_coinblocks_destroyed;
&stateful.utxo_cohorts.all.inner.indexes_to_coinblocks_destroyed;
self.indexes_to_coinblocks_stored
.compute_all(indexes, starting_indexes, exit, |vec| {
@@ -454,7 +454,7 @@ impl Vecs {
let realized_cap = stateful
.utxo_cohorts
.all
.1
.inner
.height_to_realized_cap
.as_ref()
.unwrap();
@@ -462,7 +462,7 @@ impl Vecs {
let realized_price = stateful
.utxo_cohorts
.all
.1
.inner
.indexes_to_realized_price
.as_ref()
.unwrap()

View File

@@ -1,14 +1,14 @@
use brk_error::Result;
use brk_traversable::{Traversable, TreeNode};
use brk_types::{Dollars, Height, Version};
use vecdb::{AnyExportableVec, Database, EagerVec, Exit, PcoVec};
use vecdb::{AnyExportableVec, Database, EagerVec, Exit, GenericStoredVec, PcoVec};
use crate::{indexes, Indexes};
use crate::{Indexes, indexes};
use super::{ComputedVecsFromHeight, Source, VecBuilderOptions};
pub const PERCENTILES: [u8; 21] = [
0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
pub const PERCENTILES: [u8; 19] = [
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
];
pub const PERCENTILES_LEN: usize = PERCENTILES.len();
@@ -51,7 +51,10 @@ impl PricePercentiles {
) -> Result<()> {
for (i, vec) in self.vecs.iter_mut().enumerate() {
if let Some(v) = vec {
v.height.as_mut().unwrap().truncate_push(height, percentile_prices[i])?;
v.height
.as_mut()
.unwrap()
.truncate_push(height, percentile_prices[i])?;
}
}
Ok(())
@@ -64,7 +67,12 @@ impl PricePercentiles {
exit: &Exit,
) -> Result<()> {
for vec in self.vecs.iter_mut().flatten() {
vec.compute_rest(indexes, starting_indexes, exit, None::<&EagerVec<PcoVec<Height, Dollars>>>)?;
vec.compute_rest(
indexes,
starting_indexes,
exit,
None::<&EagerVec<PcoVec<Height, Dollars>>>,
)?;
}
Ok(())
}
@@ -89,6 +97,9 @@ impl Traversable for PricePercentiles {
}
fn iter_any_exportable(&self) -> impl Iterator<Item = &dyn AnyExportableVec> {
self.vecs.iter().flatten().flat_map(|p| p.iter_any_exportable())
self.vecs
.iter()
.flatten()
.flat_map(|p| p.iter_any_exportable())
}
}

View File

@@ -239,8 +239,6 @@ impl Computer {
exit,
)?;
return Ok(());
info!("Computing cointime...");
self.cointime.compute(
&self.indexes,

View File

@@ -1,6 +1,7 @@
use std::path::Path;
use brk_error::Result;
use brk_grouper::{CohortContext, Filter, Filtered};
use brk_traversable::Traversable;
use brk_types::{Bitcoin, DateIndex, Dollars, Height, StoredU64, Version};
use vecdb::{
@@ -39,7 +40,7 @@ impl Vecs {
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
db: &Database,
cohort_name: Option<&str>,
filter: Filter,
version: Version,
indexes: &indexes::Vecs,
price: Option<&price::Vecs>,
@@ -48,16 +49,19 @@ impl Vecs {
) -> Result<Self> {
let compute_dollars = price.is_some();
let suffix = |s: &str| cohort_name.map_or(s.to_string(), |name| format!("{name}_{s}"));
let full_name = filter.to_full_name(CohortContext::Address);
let suffix = |s: &str| {
if full_name.is_empty() {
s.to_string()
} else {
format!("{full_name}_{s}")
}
};
Ok(Self {
starting_height: None,
state: states_path.map(|states_path| {
AddressCohortState::new(
states_path,
cohort_name.unwrap_or_default(),
compute_dollars,
)
AddressCohortState::new(states_path, &full_name, compute_dollars)
}),
height_to_addr_count: EagerVec::forced_import(
db,
@@ -74,7 +78,8 @@ impl Vecs {
)?,
inner: common::Vecs::forced_import(
db,
cohort_name,
filter,
CohortContext::Address,
version,
indexes,
price,
@@ -230,3 +235,9 @@ impl CohortVecs for Vecs {
)
}
}
impl Filtered for Vecs {
fn filter(&self) -> &Filter {
&self.inner.filter
}
}

View File

@@ -1,7 +1,7 @@
use std::path::Path;
use brk_error::Result;
use brk_grouper::{AddressGroups, ByAmountRange, ByGreatEqualAmount, ByLowerThanAmount, Filtered};
use brk_grouper::{AddressGroups, AmountFilter, Filter, Filtered};
use brk_traversable::Traversable;
use brk_types::{Bitcoin, DateIndex, Dollars, Height, Version};
use derive_deref::{Deref, DerefMut};
@@ -18,7 +18,7 @@ use crate::{
const VERSION: Version = Version::new(0);
#[derive(Clone, Deref, DerefMut, Traversable)]
pub struct Vecs(AddressGroups<Filtered<address_cohort::Vecs>>);
pub struct Vecs(AddressGroups<address_cohort::Vecs>);
impl Vecs {
pub fn forced_import(
@@ -28,386 +28,23 @@ impl Vecs {
price: Option<&price::Vecs>,
states_path: &Path,
) -> Result<Self> {
Ok(Self(
AddressGroups {
amount_range: ByAmountRange {
_0sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_with_0sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_1sat_to_10sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1sat_under_10sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_10sats_to_100sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10sats_under_100sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_100sats_to_1k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100sats_under_1k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_1k_sats_to_10k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1k_sats_under_10k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_10k_sats_to_100k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10k_sats_under_100k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_100k_sats_to_1m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100k_sats_under_1m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_1m_sats_to_10m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1m_sats_under_10m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_10m_sats_to_1btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10m_sats_under_1btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_1btc_to_10btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1btc_under_10btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_10btc_to_100btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10btc_under_100btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_100btc_to_1k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100btc_under_1k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_1k_btc_to_10k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1k_btc_under_10k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_10k_btc_to_100k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10k_btc_under_100k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
_100k_btc_or_more: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
Some(states_path),
true,
)?,
},
lt_amount: ByLowerThanAmount {
_10sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_10sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_100sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_1k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_10k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_100k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_1m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_10m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_1btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_10btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_100btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_1k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_10k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_under_100k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
},
ge_amount: ByGreatEqualAmount {
_1sat: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1sat"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100k_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100k_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10m_sats: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10m_sats"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_100btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_100btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_1k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_1k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
_10k_btc: address_cohort::Vecs::forced_import(
db,
Some("addrs_above_10k_btc"),
version + VERSION + Version::ZERO,
indexes,
price,
None,
true,
)?,
},
}
.into(),
))
Ok(Self(AddressGroups::new(|filter| {
let states_path = match &filter {
Filter::Amount(AmountFilter::Range(_)) => Some(states_path),
_ => None,
};
address_cohort::Vecs::forced_import(
db,
filter,
version + VERSION + Version::ZERO,
indexes,
price,
states_path,
true,
)
.unwrap()
})))
}
pub fn compute_overlapping_vecs(
@@ -421,13 +58,13 @@ impl Vecs {
self.0
.ge_amount
.iter_mut()
.map(|Filtered(filter, vecs)| {
.map(|vecs| {
let filter = vecs.filter().clone();
(
vecs,
by_size_range
.iter()
.filter(|Filtered(other, _)| filter.includes(other))
.map(Filtered::t)
.filter(|other| filter.includes(other.filter()))
.collect::<Vec<_>>(),
)
})
@@ -435,13 +72,13 @@ impl Vecs {
self.0
.lt_amount
.iter_mut()
.map(|Filtered(filter, vecs)| {
.map(|vecs| {
let filter = vecs.filter().clone();
(
vecs,
by_size_range
.iter()
.filter(|Filtered(other, _)| filter.includes(other))
.map(Filtered::t)
.filter(|other| filter.includes(other.filter()))
.collect::<Vec<_>>(),
)
})
@@ -462,7 +99,6 @@ impl Vecs {
exit: &Exit,
) -> Result<()> {
self.iter_mut()
.map(Filtered::mut_t)
.try_for_each(|v| v.compute_rest_part1(indexes, price, starting_indexes, exit))
}
@@ -480,7 +116,7 @@ impl Vecs {
dateindex_to_realized_cap: Option<&impl IterableVec<DateIndex, Dollars>>,
exit: &Exit,
) -> Result<()> {
self.0.iter_mut().map(Filtered::mut_t).try_for_each(|v| {
self.0.iter_mut().try_for_each(|v| {
v.compute_rest_part2(
indexes,
price,
@@ -498,7 +134,6 @@ impl Vecs {
pub fn safe_flush_stateful_vecs(&mut self, height: Height, exit: &Exit) -> Result<()> {
self.iter_separate_mut()
.map(Filtered::mut_t)
.try_for_each(|v| v.safe_flush_stateful_vecs(height, exit))
}
}

View File

@@ -1,4 +1,5 @@
use brk_error::{Error, Result};
use brk_grouper::{CohortContext, Filter};
use brk_traversable::Traversable;
use brk_types::{
Bitcoin, DateIndex, Dollars, Height, Sats, StoredF32, StoredF64, StoredU64, Version,
@@ -21,6 +22,9 @@ use crate::{
#[derive(Clone, Traversable)]
pub struct Vecs {
#[traversable(skip)]
pub filter: Filter,
// Cumulative
pub height_to_realized_cap: Option<EagerVec<PcoVec<Height, Dollars>>>,
pub height_to_supply: EagerVec<PcoVec<Height, Sats>>,
@@ -167,7 +171,8 @@ impl Vecs {
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
db: &Database,
cohort_name: Option<&str>,
filter: Filter,
context: CohortContext,
parent_version: Version,
indexes: &indexes::Vecs,
price: Option<&price::Vecs>,
@@ -179,9 +184,14 @@ impl Vecs {
let version = parent_version + Version::ZERO;
// let prefix = |s: &str| cohort_name.map_or(s.to_string(), |name| format!("{s}_{name}"));
let suffix = |s: &str| cohort_name.map_or(s.to_string(), |name| format!("{name}_{s}"));
let name_prefix = filter.to_full_name(context);
let suffix = |s: &str| {
if name_prefix.is_empty() {
s.to_string()
} else {
format!("{name_prefix}_{s}")
}
};
let dateindex_to_supply_in_profit = compute_dollars.then(|| {
EagerVec::forced_import(db, &suffix("supply_in_profit"), version + Version::ZERO)
@@ -203,6 +213,8 @@ impl Vecs {
});
Ok(Self {
filter,
height_to_supply_in_profit: compute_dollars.then(|| {
EagerVec::forced_import(db, &suffix("supply_in_profit"), version + Version::ZERO)
.unwrap()
@@ -1592,6 +1604,12 @@ impl Vecs {
.unwrap()
.truncate_push(dateindex, date_unrealized_state.unrealized_loss)?;
}
// Compute and push price percentiles
if let Some(price_percentiles) = self.price_percentiles.as_mut() {
let percentile_prices = state.compute_percentile_prices();
price_percentiles.truncate_push(height, &percentile_prices)?;
}
}
Ok(())

View File

@@ -182,7 +182,7 @@ impl Vecs {
version + VERSION + Version::ONE,
utxo_cohorts
.all
.1
.inner
.height_to_supply_value
.dollars
.as_ref()
@@ -604,10 +604,10 @@ impl Vecs {
separate_utxo_vecs
.par_iter_mut()
.try_for_each(|Filtered(_, v)| v.validate_computed_versions(base_version))?;
.try_for_each(|v| v.validate_computed_versions(base_version))?;
separate_address_vecs
.par_iter_mut()
.try_for_each(|Filtered(_, v)| v.validate_computed_versions(base_version))?;
.try_for_each(|v| v.validate_computed_versions(base_version))?;
self.height_to_unspendable_supply
.validate_computed_version_or_reset(
base_version + self.height_to_unspendable_supply.inner_version(),
@@ -620,13 +620,13 @@ impl Vecs {
let mut chain_state_starting_height = Height::from(self.chain_state.len());
let stateful_starting_height = match separate_utxo_vecs
.par_iter_mut()
.map(|Filtered(_, v)| Height::from(v.min_height_vecs_len()))
.map(|v| Height::from(v.min_height_vecs_len()))
.min()
.unwrap_or_default()
.min(
separate_address_vecs
.par_iter_mut()
.map(|Filtered(_, v)| Height::from(v.min_height_vecs_len()))
.map(|v| Height::from(v.min_height_vecs_len()))
.min()
.unwrap_or_default(),
)
@@ -683,7 +683,7 @@ impl Vecs {
let starting_height = if starting_height.is_not_zero()
&& separate_utxo_vecs
.iter_mut()
.map(|Filtered(_, v)| v.import_state(starting_height).unwrap_or_default())
.map(|v| v.import_state(starting_height).unwrap_or_default())
.all(|h| h == starting_height)
{
starting_height
@@ -695,7 +695,7 @@ impl Vecs {
let starting_height = if starting_height.is_not_zero()
&& separate_address_vecs
.iter_mut()
.map(|Filtered(_, v)| v.import_state(starting_height).unwrap_or_default())
.map(|v| v.import_state(starting_height).unwrap_or_default())
.all(|h| h == starting_height)
{
starting_height
@@ -703,6 +703,7 @@ impl Vecs {
Height::ZERO
};
// info!("starting_height = {starting_height}");
let mut chain_state: Vec<BlockState>;
@@ -737,14 +738,17 @@ impl Vecs {
separate_utxo_vecs
.par_iter_mut()
.try_for_each(|Filtered(_, v)| {
.try_for_each(|v| {
v.reset_state_starting_height();
v.state.as_mut().unwrap().reset_price_to_amount_if_needed()
})?;
// Reset aggregate cohorts' price_to_amount
self.utxo_cohorts.reset_aggregate_price_to_amount()?;
separate_address_vecs
.par_iter_mut()
.try_for_each(|Filtered(_, v)| {
.try_for_each(|v| {
v.reset_state_starting_height();
v.state.as_mut().unwrap().reset_price_to_amount_if_needed()
})?;
@@ -839,13 +843,13 @@ impl Vecs {
self.utxo_cohorts
.iter_separate_mut()
.for_each(|Filtered(_, v)| {
.for_each(|v| {
v.state.as_mut().unwrap().reset_single_iteration_values()
});
self.address_cohorts
.iter_separate_mut()
.for_each(|Filtered(_, v)| {
.for_each(|v| {
v.state.as_mut().unwrap().reset_single_iteration_values()
});
@@ -1328,11 +1332,11 @@ impl Vecs {
self.utxo_cohorts
.par_iter_separate_mut()
.map(|Filtered(_, v)| v as &mut dyn DynCohortVecs)
.map(|v| v as &mut dyn DynCohortVecs)
.chain(
self.address_cohorts
.par_iter_separate_mut()
.map(|Filtered(_, v)| v as &mut dyn DynCohortVecs),
.map(|v| v as &mut dyn DynCohortVecs),
)
.try_for_each(|v| {
v.truncate_push(height)?;
@@ -1341,6 +1345,10 @@ impl Vecs {
)
})?;
// Compute and push percentiles for aggregate cohorts (all, sth, lth)
self.utxo_cohorts
.truncate_push_aggregate_percentiles(height)?;
if height != last_height
&& height != Height::ZERO
&& height.to_usize() % 10_000 == 0
@@ -1454,7 +1462,7 @@ impl Vecs {
starting_indexes.dateindex,
self.utxo_cohorts
.all
.1
.inner
.indexes_to_supply
.dollars
.as_ref()
@@ -1474,14 +1482,14 @@ impl Vecs {
let height_to_supply = &self
.utxo_cohorts
.all
.1
.inner
.height_to_supply_value
.bitcoin
.clone();
let dateindex_to_supply = self
.utxo_cohorts
.all
.1
.inner
.indexes_to_supply
.bitcoin
.dateindex
@@ -1491,11 +1499,11 @@ impl Vecs {
.indexes_to_market_cap
.as_ref()
.map(|v| v.dateindex.as_ref().unwrap().clone());
let height_to_realized_cap = self.utxo_cohorts.all.1.height_to_realized_cap.clone();
let height_to_realized_cap = self.utxo_cohorts.all.inner.height_to_realized_cap.clone();
let dateindex_to_realized_cap = self
.utxo_cohorts
.all
.1
.inner
.indexes_to_realized_cap
.as_ref()
.map(|v| v.dateindex.unwrap_last().clone());
@@ -1615,10 +1623,10 @@ impl Vecs {
self.utxo_cohorts
.par_iter_separate_mut()
.try_for_each(|Filtered(_, v)| v.safe_flush_stateful_vecs(height, exit))?;
.try_for_each(|v| v.safe_flush_stateful_vecs(height, exit))?;
self.address_cohorts
.par_iter_separate_mut()
.try_for_each(|Filtered(_, v)| v.safe_flush_stateful_vecs(height, exit))?;
.try_for_each(|v| v.safe_flush_stateful_vecs(height, exit))?;
self.height_to_unspendable_supply.safe_flush(exit)?;
self.height_to_opreturn_supply.safe_flush(exit)?;
self.addresstype_to_height_to_addr_count
@@ -1816,13 +1824,12 @@ impl AddressTypeToVec<(TypeIndex, Sats)> {
if is_new
|| from_any_empty
|| vecs.amount_range.get_mut(amount).0.clone()
!= vecs.amount_range.get_mut(prev_amount).0.clone()
|| vecs.amount_range.get_mut(amount).filter().clone()
!= vecs.amount_range.get_mut(prev_amount).filter().clone()
{
if !is_new && !from_any_empty {
vecs.amount_range
.get_mut(prev_amount)
.1
.state
.as_mut()
.unwrap()
@@ -1833,7 +1840,6 @@ impl AddressTypeToVec<(TypeIndex, Sats)> {
vecs.amount_range
.get_mut(amount)
.1
.state
.as_mut()
.unwrap()
@@ -1841,7 +1847,6 @@ impl AddressTypeToVec<(TypeIndex, Sats)> {
} else {
vecs.amount_range
.get_mut(amount)
.1
.state
.as_mut()
.unwrap()
@@ -1913,12 +1918,11 @@ impl HeightToAddressTypeToVec<(TypeIndex, Sats)> {
let will_be_empty = addressdata.has_1_utxos();
if will_be_empty
|| vecs.amount_range.get_mut(amount).0.clone()
!= vecs.amount_range.get_mut(prev_amount).0.clone()
|| vecs.amount_range.get_mut(amount).filter().clone()
!= vecs.amount_range.get_mut(prev_amount).filter().clone()
{
vecs.amount_range
.get_mut(prev_amount)
.1
.state
.as_mut()
.unwrap()
@@ -1944,7 +1948,6 @@ impl HeightToAddressTypeToVec<(TypeIndex, Sats)> {
} else {
vecs.amount_range
.get_mut(amount)
.1
.state
.as_mut()
.unwrap()
@@ -1953,7 +1956,6 @@ impl HeightToAddressTypeToVec<(TypeIndex, Sats)> {
} else {
vecs.amount_range
.get_mut(amount)
.1
.state
.as_mut()
.unwrap()

View File

@@ -1,12 +1,15 @@
use std::{ops::Deref, path::Path};
use brk_error::Result;
use brk_grouper::{CohortContext, Filter, Filtered, StateLevel};
use brk_traversable::Traversable;
use brk_types::{Bitcoin, DateIndex, Dollars, Height, Version};
use brk_types::{Bitcoin, DateIndex, Dollars, Height, Sats, Version};
use vecdb::{Database, Exit, IterableVec};
use crate::{
Indexes, UTXOCohortState, indexes, price,
Indexes, PriceToAmount, UTXOCohortState,
grouped::PERCENTILES_LEN,
indexes, price,
stateful::{
common,
r#trait::{CohortVecs, DynCohortVecs},
@@ -20,6 +23,10 @@ pub struct Vecs {
#[traversable(skip)]
pub state: Option<UTXOCohortState>,
/// For aggregate cohorts (all, sth, lth) that only need price_to_amount for percentiles
#[traversable(skip)]
pub price_to_amount: Option<PriceToAmount>,
#[traversable(flatten)]
pub inner: common::Vecs,
}
@@ -28,31 +35,39 @@ impl Vecs {
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
db: &Database,
cohort_name: Option<&str>,
filter: Filter,
version: Version,
indexes: &indexes::Vecs,
price: Option<&price::Vecs>,
states_path: Option<&Path>,
states_path: &Path,
state_level: StateLevel,
extended: bool,
compute_rel_to_all: bool,
compute_adjusted: bool,
) -> Result<Self> {
let compute_dollars = price.is_some();
let full_name = filter.to_full_name(CohortContext::Utxo);
Ok(Self {
state_starting_height: None,
state: states_path.map(|states_path| {
UTXOCohortState::new(
states_path,
cohort_name.unwrap_or_default(),
compute_dollars,
)
}),
state: if state_level.is_full() {
Some(UTXOCohortState::new(states_path, &full_name, compute_dollars))
} else {
None
},
price_to_amount: if state_level.is_price_only() && compute_dollars {
Some(PriceToAmount::create(states_path, &full_name))
} else {
None
},
inner: common::Vecs::forced_import(
db,
cohort_name,
filter,
CohortContext::Utxo,
version,
indexes,
price,
@@ -173,9 +188,55 @@ impl CohortVecs for Vecs {
}
}
impl Vecs {
/// Compute percentile prices for aggregate cohorts that have standalone price_to_amount.
/// Returns NaN array if price_to_amount is None or empty.
pub fn compute_percentile_prices_from_standalone(&self, supply: Sats) -> [Dollars; PERCENTILES_LEN] {
use crate::grouped::PERCENTILES;
let mut result = [Dollars::NAN; PERCENTILES_LEN];
let price_to_amount = match self.price_to_amount.as_ref() {
Some(p) => p,
None => return result,
};
if price_to_amount.is_empty() || supply == Sats::ZERO {
return result;
}
let total = u64::from(supply);
let targets = PERCENTILES.map(|p| total * u64::from(p) / 100);
let mut accumulated = 0u64;
let mut pct_idx = 0;
for (&price, &sats) in price_to_amount.iter() {
accumulated += u64::from(sats);
while pct_idx < PERCENTILES_LEN && accumulated >= targets[pct_idx] {
result[pct_idx] = price;
pct_idx += 1;
}
if pct_idx >= PERCENTILES_LEN {
break;
}
}
result
}
}
impl Deref for Vecs {
type Target = common::Vecs;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl Filtered for Vecs {
fn filter(&self) -> &Filter {
&self.inner.filter
}
}

File diff suppressed because it is too large Load Diff