computer: store part 2

This commit is contained in:
nym21
2025-06-27 19:38:44 +02:00
parent e73daa6214
commit 8ea13544de
16 changed files with 393 additions and 118 deletions

View File

@@ -1,11 +1,11 @@
use byteview::ByteView;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
use crate::{Dollars, Sats};
use crate::{CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C, packed)]
#[derive(Debug, Default, Clone, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
pub struct AddressData {
pub sent: Sats,
pub received: Sats,
@@ -17,6 +17,54 @@ impl AddressData {
pub fn amount(&self) -> Sats {
(u64::from(self.received) - u64::from(self.sent)).into()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
if self.amount() == Sats::ZERO {
if self.outputs_len != 0 {
unreachable!();
}
true
} else {
false
}
}
pub fn receive(&mut self, amount: Sats, price: Dollars) {
self.received += amount;
self.outputs_len += 1;
self.realized_cap += price * amount;
}
pub fn send(&mut self, amount: Sats, previous_price: Dollars) -> Result<()> {
if self.amount() < amount {
return Err(Error::String("Previous_amount smaller than sent amount"));
}
self.sent += amount;
self.outputs_len -= 1;
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
Ok(())
}
}
impl From<EmptyAddressData> for AddressData {
fn from(value: EmptyAddressData) -> Self {
Self::from(&value)
}
}
impl From<&EmptyAddressData> for AddressData {
fn from(value: &EmptyAddressData) -> Self {
Self {
sent: value.transfered,
received: value.transfered,
realized_cap: Dollars::ZERO,
outputs_len: 0,
}
}
}
impl From<ByteView> for AddressData {
@@ -31,6 +79,14 @@ impl From<AddressData> for ByteView {
}
impl From<&AddressData> for ByteView {
fn from(value: &AddressData) -> Self {
Self::new(value.as_bytes())
Self::new(
&[
value.sent.as_bytes(),
value.received.as_bytes(),
value.realized_cap.as_bytes(),
value.outputs_len.as_bytes(),
]
.concat(),
)
}
}

View File

@@ -2,14 +2,30 @@ use byteview::ByteView;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::Sats;
use crate::{AddressData, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
pub struct EmptyAddressData {
pub transfered: Sats,
}
impl EmptyAddressData {}
impl From<AddressData> for EmptyAddressData {
fn from(value: AddressData) -> Self {
Self::from(&value)
}
}
impl From<&AddressData> for EmptyAddressData {
fn from(value: &AddressData) -> Self {
if value.sent != value.received {
dbg!(&value);
panic!("Trying to convert not empty wallet to empty !");
}
Self {
transfered: value.sent,
}
}
}
impl From<ByteView> for EmptyAddressData {
fn from(value: ByteView) -> Self {

View File

@@ -11,6 +11,15 @@ pub struct P2AAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2AAddressIndex, OutputIndex)> for P2AAddressIndexOutputindex {
fn from(value: (P2AAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2AAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2PK33AddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2PK33AddressIndex, OutputIndex)> for P2PK33AddressIndexOutputindex {
fn from(value: (P2PK33AddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2PK33AddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2PK65AddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2PK65AddressIndex, OutputIndex)> for P2PK65AddressIndexOutputindex {
fn from(value: (P2PK65AddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2PK65AddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2PKHAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2PKHAddressIndex, OutputIndex)> for P2PKHAddressIndexOutputindex {
fn from(value: (P2PKHAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2PKHAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2SHAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2SHAddressIndex, OutputIndex)> for P2SHAddressIndexOutputindex {
fn from(value: (P2SHAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2SHAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2TRAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2TRAddressIndex, OutputIndex)> for P2TRAddressIndexOutputindex {
fn from(value: (P2TRAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2TRAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2WPKHAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2WPKHAddressIndex, OutputIndex)> for P2WPKHAddressIndexOutputindex {
fn from(value: (P2WPKHAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2WPKHAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =

View File

@@ -11,6 +11,15 @@ pub struct P2WSHAddressIndexOutputindex {
outputindex: OutputIndex,
}
impl From<(P2WSHAddressIndex, OutputIndex)> for P2WSHAddressIndexOutputindex {
fn from(value: (P2WSHAddressIndex, OutputIndex)) -> Self {
Self {
addressindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for P2WSHAddressIndexOutputindex {
fn from(value: ByteView) -> Self {
let addressindex =