computer: store part 9

This commit is contained in:
nym21
2025-07-03 19:15:02 +02:00
parent be4e693a27
commit 6d35c26b3f
4 changed files with 30 additions and 16 deletions
@@ -98,14 +98,20 @@ impl AddressCohortState {
pub fn add(&mut self, addressdata: &AddressData) {
self.address_count += 1;
self.inner
.increment_(&addressdata.into(), addressdata.realized_cap);
self.inner.increment_(
&addressdata.into(),
addressdata.realized_cap,
addressdata.realized_price(),
);
}
pub fn subtract(&mut self, addressdata: &AddressData) {
self.address_count = self.address_count.checked_sub(1).unwrap();
self.inner
.decrement_(&addressdata.into(), addressdata.realized_cap);
self.inner.decrement_(
&addressdata.into(),
addressdata.realized_cap,
addressdata.realized_price(),
);
}
pub fn commit(&mut self, height: Height) -> Result<()> {
@@ -1,6 +1,6 @@
use std::{cmp::Ordering, path::Path};
use brk_core::{Bitcoin, CheckedSub, Dollars, Height, Result, Sats};
use brk_core::{CheckedSub, Dollars, Height, Result, Sats};
use crate::{PriceToAmount, RealizedState, SupplyState, UnrealizedState};
@@ -61,16 +61,18 @@ impl CohortState {
}
}
pub fn increment_(&mut self, supply_state: &SupplyState, realized_cap: Dollars) {
pub fn increment_(
&mut self,
supply_state: &SupplyState,
realized_cap: Dollars,
realized_price: Dollars,
) {
self.supply += supply_state;
if supply_state.value > Sats::ZERO {
if let Some(realized) = self.realized.as_mut() {
realized.increment_(realized_cap);
self.price_to_amount.increment(
realized_cap / Bitcoin::from(supply_state.value),
supply_state,
);
self.price_to_amount.increment(realized_price, supply_state);
}
}
}
@@ -87,16 +89,18 @@ impl CohortState {
}
}
pub fn decrement_(&mut self, supply_state: &SupplyState, realized_cap: Dollars) {
pub fn decrement_(
&mut self,
supply_state: &SupplyState,
realized_cap: Dollars,
realized_price: Dollars,
) {
self.supply -= supply_state;
if supply_state.value > Sats::ZERO {
if let Some(realized) = self.realized.as_mut() {
realized.decrement_(realized_cap);
self.price_to_amount.decrement(
(realized_cap / Bitcoin::from(supply_state.value)).round_nearest_cent(),
supply_state,
);
self.price_to_amount.decrement(realized_price, supply_state);
}
}
}
+1 -1
View File
@@ -38,7 +38,7 @@ pub use addresstype_to_typeindex_vec::*;
use r#trait::CohortVecs;
pub use withaddressdatasource::WithAddressDataSource;
const VERSION: Version = Version::new(9);
const VERSION: Version = Version::new(11);
#[derive(Clone)]
pub struct Vecs {
+4
View File
@@ -95,6 +95,10 @@ impl CheckedSub<usize> for Sats {
impl SubAssign for Sats {
fn sub_assign(&mut self, rhs: Self) {
*self = self.checked_sub(rhs).unwrap();
// .unwrap_or_else(|| {
// dbg!((*self, rhs));
// unreachable!();
// });
}
}