global: snapshot

This commit is contained in:
nym21
2026-03-04 17:10:15 +01:00
parent 891f0dad9e
commit 9e23de4ba1
313 changed files with 9087 additions and 4918 deletions

View File

@@ -18,8 +18,7 @@ impl AddressCohortState {
pub(crate) fn new(path: &Path, name: &str) -> Self {
Self {
addr_count: 0,
inner: CohortState::new(path, name)
.with_price_rounding(COST_BASIS_PRICE_DIGITS),
inner: CohortState::new(path, name).with_price_rounding(COST_BASIS_PRICE_DIGITS),
}
}
@@ -136,5 +135,4 @@ impl AddressCohortState {
self.inner.decrement_snapshot(&snapshot);
}
}

View File

@@ -1,7 +1,7 @@
use std::path::Path;
use brk_error::Result;
use brk_types::{Age, CentsSats, Cents, CostBasisSnapshot, Height, Sats, SupplyState};
use brk_types::{Age, Cents, CentsSats, CostBasisSnapshot, Height, Sats, SupplyState};
use super::super::cost_basis::{CostBasisData, Percentiles, RealizedState, UnrealizedState};
@@ -256,8 +256,7 @@ impl CohortState {
}
pub(crate) fn compute_unrealized_state(&mut self, height_price: Cents) -> UnrealizedState {
self.cost_basis_data
.compute_unrealized_state(height_price)
self.cost_basis_data.compute_unrealized_state(height_price)
}
pub(crate) fn write(&mut self, height: Height, cleanup: bool) -> Result<()> {

View File

@@ -6,7 +6,7 @@ use std::{
use brk_error::{Error, Result};
use brk_types::{
CentsCompact, CentsSats, CentsSquaredSats, Cents, CostBasisDistribution, Height, Sats,
Cents, CentsCompact, CentsSats, CentsSquaredSats, CostBasisDistribution, Height, Sats,
};
use rustc_hash::FxHashMap;
use vecdb::Bytes;
@@ -95,7 +95,13 @@ impl CostBasisData {
pub(crate) fn iter(&self) -> impl Iterator<Item = (CentsCompact, &Sats)> {
self.assert_pending_empty();
self.state.as_ref().unwrap().base.map.iter().map(|(&k, v)| (k, v))
self.state
.as_ref()
.unwrap()
.base
.map
.iter()
.map(|(&k, v)| (k, v))
}
pub(crate) fn is_empty(&self) -> bool {
@@ -105,7 +111,8 @@ impl CostBasisData {
pub(crate) fn first_key_value(&self) -> Option<(CentsCompact, &Sats)> {
self.assert_pending_empty();
self.state
.as_ref().unwrap()
.as_ref()
.unwrap()
.base
.map
.first_key_value()
@@ -115,7 +122,8 @@ impl CostBasisData {
pub(crate) fn last_key_value(&self) -> Option<(CentsCompact, &Sats)> {
self.assert_pending_empty();
self.state
.as_ref().unwrap()
.as_ref()
.unwrap()
.base
.map
.last_key_value()
@@ -179,7 +187,14 @@ impl CostBasisData {
self.percentiles_dirty = true;
}
for (cents, (inc, dec)) in self.pending.drain() {
let entry = self.state.as_mut().unwrap().base.map.entry(cents).or_default();
let entry = self
.state
.as_mut()
.unwrap()
.base
.map
.entry(cents)
.or_default();
*entry += inc;
if *entry < dec {
panic!(
@@ -322,7 +337,10 @@ impl CostBasisData {
}
}
fs::write(self.path_state(height), self.state.as_ref().unwrap().serialize()?)?;
fs::write(
self.path_state(height),
self.state.as_ref().unwrap().serialize()?,
)?;
Ok(())
}

View File

@@ -1,6 +1,6 @@
use std::cmp::Ordering;
use brk_types::{CentsSats, CentsSquaredSats, Cents, Sats};
use brk_types::{Cents, CentsSats, CentsSquaredSats, Sats};
/// Realized state using u128 for raw cent*sat values internally.
/// This avoids overflow and defers division to output time for efficiency.
@@ -156,14 +156,22 @@ impl RealizedState {
/// Increment using pre-computed snapshot values (for address path)
#[inline]
pub(crate) fn increment_snapshot(&mut self, price_sats: CentsSats, investor_cap: CentsSquaredSats) {
pub(crate) fn increment_snapshot(
&mut self,
price_sats: CentsSats,
investor_cap: CentsSquaredSats,
) {
self.cap_raw += price_sats.as_u128();
self.investor_cap_raw += investor_cap;
}
/// Decrement using pre-computed snapshot values (for address path)
#[inline]
pub(crate) fn decrement_snapshot(&mut self, price_sats: CentsSats, investor_cap: CentsSquaredSats) {
pub(crate) fn decrement_snapshot(
&mut self,
price_sats: CentsSats,
investor_cap: CentsSquaredSats,
) {
self.cap_raw -= price_sats.as_u128();
self.investor_cap_raw -= investor_cap;
}

View File

@@ -35,7 +35,6 @@ impl UnrealizedState {
invested_capital_in_profit_raw: 0,
invested_capital_in_loss_raw: 0,
};
}
/// Internal cache state using u128 for raw cent*sat values.
@@ -279,5 +278,4 @@ impl CachedUnrealizedState {
state
}
}