computer: store part 8

This commit is contained in:
nym21
2025-07-03 18:19:36 +02:00
parent 5810276156
commit be4e693a27
29 changed files with 745 additions and 364 deletions
@@ -20,31 +20,80 @@ impl AddressCohortState {
})
}
pub fn height(&self) -> Option<Height> {
self.inner.height()
}
pub fn reset_price_to_amount(&mut self) -> Result<()> {
self.inner.reset_price_to_amount()
}
pub fn reset_single_iteration_values(&mut self) {
self.inner.reset_single_iteration_values();
}
#[allow(clippy::too_many_arguments)]
pub fn send(
&mut self,
addressdata: &mut AddressData,
value: Sats,
current_price: Option<Dollars>,
prev_price: Option<Dollars>,
blocks_old: usize,
days_old: f64,
older_than_hour: bool,
) {
self.inner.send(
) -> Result<()> {
let compute_price = current_price.is_some();
let prev_realized_price = compute_price.then(|| addressdata.realized_price());
let prev_supply_state = SupplyState {
utxos: addressdata.outputs_len as usize,
value: addressdata.amount(),
};
addressdata.send(value, prev_price)?;
let supply_state = SupplyState {
utxos: addressdata.outputs_len as usize,
value: addressdata.amount(),
};
self.inner.send_(
&SupplyState { utxos: 1, value },
current_price,
prev_price,
blocks_old,
days_old,
older_than_hour,
compute_price.then(|| (addressdata.realized_price(), &supply_state)),
prev_realized_price.map(|prev_price| (prev_price, &prev_supply_state)),
);
Ok(())
}
pub fn receive(&mut self, value: Sats, price: Option<Dollars>) {
self.inner.receive(&SupplyState { utxos: 1, value }, price);
pub fn receive(&mut self, address_data: &mut AddressData, value: Sats, price: Option<Dollars>) {
let compute_price = price.is_some();
let prev_realized_price = compute_price.then(|| address_data.realized_price());
let prev_supply_state = SupplyState {
utxos: address_data.outputs_len as usize,
value: address_data.amount(),
};
address_data.receive(value, price);
let supply_state = SupplyState {
utxos: address_data.outputs_len as usize,
value: address_data.amount(),
};
self.inner.receive_(
&SupplyState { utxos: 1, value },
price,
compute_price.then(|| (address_data.realized_price(), &supply_state)),
prev_realized_price.map(|prev_price| (prev_price, &prev_supply_state)),
);
}
pub fn add(&mut self, addressdata: &AddressData) {
@@ -54,7 +103,7 @@ impl AddressCohortState {
}
pub fn subtract(&mut self, addressdata: &AddressData) {
self.address_count.checked_sub(1).unwrap();
self.address_count = self.address_count.checked_sub(1).unwrap();
self.inner
.decrement_(&addressdata.into(), addressdata.realized_cap);
}
@@ -63,39 +112,3 @@ impl AddressCohortState {
self.inner.commit(height)
}
}
// fn decrement(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
// self.inner.decrement(supply_state, price);
// }
// fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) {
// self.inner.decrement_price_to_amount(supply_state, price);
// }
// fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
// self.inner.receive(supply_state, price);
// }
// fn compute_unrealized_states(
// &self,
// height_price: Dollars,
// date_price: Option<Dollars>,
// ) -> (UnrealizedState, Option<UnrealizedState>) {
// self.inner
// .compute_unrealized_states(height_price, date_price)
// }
// }
// impl Deref for AddressCohortState {
// type Target = CohortState;
// fn deref(&self) -> &Self::Target {
// &self.inner
// }
// }
// impl DerefMut for AddressCohortState {
// fn deref_mut(&mut self) -> &mut Self::Target {
// &mut self.inner
// }
// }
@@ -10,7 +10,8 @@ pub struct CohortState {
pub realized: Option<RealizedState>,
pub satblocks_destroyed: Sats,
pub satdays_destroyed: Sats,
pub price_to_amount: PriceToAmount,
price_to_amount: PriceToAmount,
}
impl CohortState {
@@ -24,6 +25,22 @@ impl CohortState {
})
}
pub fn height(&self) -> Option<Height> {
self.price_to_amount.height()
}
pub fn reset_price_to_amount(&mut self) -> Result<()> {
self.price_to_amount.reset()
}
pub fn price_to_amount_first_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.price_to_amount.first_key_value()
}
pub fn price_to_amount_last_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.price_to_amount.last_key_value()
}
pub fn reset_single_iteration_values(&mut self) {
self.satdays_destroyed = Sats::ZERO;
self.satblocks_destroyed = Sats::ZERO;
@@ -39,7 +56,7 @@ impl CohortState {
if let Some(realized) = self.realized.as_mut() {
let price = price.unwrap();
realized.increment(supply_state, price);
*self.price_to_amount.entry(price).or_default() += supply_state.value;
self.price_to_amount.increment(price, supply_state);
}
}
}
@@ -50,10 +67,10 @@ impl CohortState {
if supply_state.value > Sats::ZERO {
if let Some(realized) = self.realized.as_mut() {
realized.increment_(realized_cap);
*self
.price_to_amount
.entry(realized_cap / Bitcoin::from(supply_state.value))
.or_default() += supply_state.value;
self.price_to_amount.increment(
realized_cap / Bitcoin::from(supply_state.value),
supply_state,
);
}
}
}
@@ -65,7 +82,7 @@ impl CohortState {
if let Some(realized) = self.realized.as_mut() {
let price = price.unwrap();
realized.decrement(supply_state, price);
self.decrement_price_to_amount(supply_state, price);
self.price_to_amount.decrement(price, supply_state);
}
}
}
@@ -76,30 +93,47 @@ impl CohortState {
if supply_state.value > Sats::ZERO {
if let Some(realized) = self.realized.as_mut() {
realized.decrement_(realized_cap);
self.decrement_price_to_amount(
self.price_to_amount.decrement(
(realized_cap / Bitcoin::from(supply_state.value)).round_nearest_cent(),
supply_state,
realized_cap / Bitcoin::from(supply_state.value),
);
}
}
}
fn decrement_price_to_amount(&mut self, supply_state: &SupplyState, price: Dollars) {
let amount = self.price_to_amount.get_mut(&price).unwrap();
*amount -= supply_state.value;
if *amount == Sats::ZERO {
self.price_to_amount.remove(&price);
}
pub fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
self.receive_(
supply_state,
price,
price.map(|price| (price, supply_state)),
None,
);
}
pub fn receive(&mut self, supply_state: &SupplyState, price: Option<Dollars>) {
pub fn receive_(
&mut self,
supply_state: &SupplyState,
price: Option<Dollars>,
price_to_amount_increment: Option<(Dollars, &SupplyState)>,
price_to_amount_decrement: Option<(Dollars, &SupplyState)>,
) {
self.supply += supply_state;
if supply_state.value > Sats::ZERO {
if let Some(realized) = self.realized.as_mut() {
let price = price.unwrap();
realized.receive(supply_state, price);
*self.price_to_amount.entry(price).or_default() += supply_state.value;
if let Some((price, supply)) = price_to_amount_increment
&& supply.value.is_not_zero()
{
self.price_to_amount.increment(price, supply);
}
if let Some((price, supply)) = price_to_amount_decrement
&& supply.value.is_not_zero()
{
self.price_to_amount.decrement(price, supply);
}
}
}
}
@@ -112,6 +146,30 @@ impl CohortState {
blocks_old: usize,
days_old: f64,
older_than_hour: bool,
) {
self.send_(
supply_state,
current_price,
prev_price,
blocks_old,
days_old,
older_than_hour,
None,
prev_price.map(|prev_price| (prev_price, supply_state)),
);
}
#[allow(clippy::too_many_arguments)]
pub fn send_(
&mut self,
supply_state: &SupplyState,
current_price: Option<Dollars>,
prev_price: Option<Dollars>,
blocks_old: usize,
days_old: f64,
older_than_hour: bool,
price_to_amount_increment: Option<(Dollars, &SupplyState)>,
price_to_amount_decrement: Option<(Dollars, &SupplyState)>,
) {
if supply_state.utxos == 0 {
return;
@@ -129,7 +187,16 @@ impl CohortState {
let current_price = current_price.unwrap();
let prev_price = prev_price.unwrap();
realized.send(supply_state, current_price, prev_price, older_than_hour);
self.decrement_price_to_amount(supply_state, prev_price);
if let Some((price, supply)) = price_to_amount_increment
&& supply.value.is_not_zero()
{
self.price_to_amount.increment(price, supply);
}
if let Some((price, supply)) = price_to_amount_decrement
&& supply.value.is_not_zero()
{
self.price_to_amount.decrement(price, supply);
}
}
}
}
@@ -1,6 +1,6 @@
use std::path::Path;
use brk_core::Result;
use brk_core::{Height, Result};
use derive_deref::{Deref, DerefMut};
use super::CohortState;
@@ -16,4 +16,12 @@ impl UTXOCohortState {
compute_dollars,
)?))
}
pub fn height(&self) -> Option<Height> {
self.0.height()
}
pub fn reset_price_to_amount(&mut self) -> Result<()> {
self.0.reset_price_to_amount()
}
}
@@ -2,7 +2,6 @@ use std::{
collections::BTreeMap,
fs::{self, File},
io::{BufReader, BufWriter},
ops::{Deref, DerefMut},
path::{Path, PathBuf},
};
@@ -11,6 +10,8 @@ use brk_core::{Dollars, Height, Result, Sats};
use derive_deref::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
use crate::states::SupplyState;
#[derive(Clone, Debug)]
pub struct PriceToAmount {
pathbuf: PathBuf,
@@ -46,8 +47,36 @@ impl PriceToAmount {
})
}
pub fn iter(&self) -> impl Iterator<Item = (&Dollars, &Sats)> {
self.state.iter()
}
pub fn is_empty(&self) -> bool {
self.state.is_empty()
}
pub fn first_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.state.first_key_value()
}
pub fn last_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.state.last_key_value()
}
pub fn increment(&mut self, price: Dollars, supply_state: &SupplyState) {
*self.state.entry(price).or_default() += supply_state.value;
}
pub fn decrement(&mut self, price: Dollars, supply_state: &SupplyState) {
let amount = self.state.get_mut(&price).unwrap();
*amount -= supply_state.value;
if *amount == Sats::ZERO {
self.state.remove(&price);
}
}
pub fn reset(&mut self) -> Result<()> {
self.clear();
self.state.clear();
self.height = None;
fs::remove_dir_all(&self.pathbuf)?;
fs::create_dir_all(&self.pathbuf)?;
@@ -89,16 +118,3 @@ impl PriceToAmount {
path.join("height")
}
}
impl Deref for PriceToAmount {
type Target = BTreeMap<Dollars, Sats>;
fn deref(&self) -> &Self::Target {
&self.state
}
}
impl DerefMut for PriceToAmount {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.state
}
}