computer: stateful: maybe got rollback to work, tbd

This commit is contained in:
nym21
2025-08-19 23:34:05 +02:00
parent 05036c682f
commit da1ff2cacc
20 changed files with 267 additions and 139 deletions

View File

@@ -24,20 +24,18 @@ use super::Dollars;
)]
pub struct Cents(i64);
const SIGNIFICANT_DIGITS: i32 = 4;
impl Cents {
pub const fn mint(value: i64) -> Self {
Self(value)
}
pub fn round_to_4_digits(self) -> Self {
pub fn round_to(self, digits: i32) -> Self {
let v = self.0;
let ilog10 = v.checked_ilog10().unwrap_or(0) as i32;
Self::from(if ilog10 >= SIGNIFICANT_DIGITS {
let log_diff = ilog10 - SIGNIFICANT_DIGITS + 1;
Self::from(if ilog10 >= digits {
let log_diff = ilog10 - digits + 1;
let pow = 10.0_f64.powi(log_diff);

View File

@@ -39,8 +39,12 @@ impl Dollars {
Dollars((self.0 * 100.0).round() / 100.0)
}
pub fn round_to_4_digits(self) -> Self {
Self::from(Cents::from(self).round_to_4_digits())
pub fn round_to(self, digits: i32) -> Self {
Self::from(Cents::from(self).round_to(digits))
}
pub fn is_negative(&self) -> bool {
self.0 < 0.0
}
}

View File

@@ -66,6 +66,10 @@ impl Height {
pub fn is_zero(self) -> bool {
self == Self::ZERO
}
pub fn is_not_zero(self) -> bool {
self != Self::ZERO
}
}
impl PartialEq<u64> for Height {

View File

@@ -21,7 +21,17 @@ impl LoadedAddressData {
}
pub fn realized_price(&self) -> Dollars {
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
let p = (self.realized_cap / Bitcoin::from(self.amount())).round_to(4);
if p.is_negative() {
dbg!((
self.realized_cap,
self.amount(),
Bitcoin::from(self.amount()),
p
));
panic!("");
}
p
}
#[inline]
@@ -38,7 +48,12 @@ impl LoadedAddressData {
self.received += amount;
self.outputs_len += 1;
if let Some(price) = price {
self.realized_cap += price * amount;
let added = price * amount;
self.realized_cap += added;
if added.is_negative() || self.realized_cap.is_negative() {
dbg!((self.realized_cap, price, amount, added));
panic!();
}
}
}
@@ -49,10 +64,20 @@ impl LoadedAddressData {
self.sent += amount;
self.outputs_len -= 1;
if let Some(previous_price) = previous_price {
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
let subtracted = previous_price * amount;
let realized_cap = self.realized_cap.checked_sub(subtracted).unwrap();
if self.realized_cap.is_negative() || realized_cap.is_negative() {
dbg!((
self,
realized_cap,
previous_price,
amount,
previous_price * amount,
subtracted
));
panic!();
}
self.realized_cap = realized_cap;
}
Ok(())
}