computer: convert stores to vecs part 2

This commit is contained in:
nym21
2025-07-16 16:23:40 +02:00
parent 1505454793
commit a0cfc1be2b
23 changed files with 749 additions and 340 deletions

View File

@@ -1,95 +0,0 @@
use std::ops::Add;
use byteview::ByteView;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{CheckedSub, Printable, TypeIndex};
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct AnyAddressDataIndex(TypeIndex);
// impl From<TypeIndex> for AddressDataIndex {
// fn from(value: TypeIndex) -> Self {
// Self(value)
// }
// }
// impl From<AddressDataIndex> for TypeIndex {
// fn from(value: AddressDataIndex) -> Self {
// value.0
// }
// }
// impl From<AddressDataIndex> for u32 {
// fn from(value: AddressDataIndex) -> Self {
// Self::from(*value)
// }
// }
// impl From<u32> for AddressDataIndex {
// fn from(value: u32) -> Self {
// Self(TypeIndex::from(value))
// }
// }
// impl From<AddressDataIndex> for usize {
// fn from(value: AddressDataIndex) -> Self {
// Self::from(*value)
// }
// }
// impl From<usize> for AddressDataIndex {
// fn from(value: usize) -> Self {
// Self(TypeIndex::from(value))
// }
// }
// impl Add<usize> for AddressDataIndex {
// type Output = Self;
// fn add(self, rhs: usize) -> Self::Output {
// Self(*self + rhs)
// }
// }
// impl CheckedSub<AddressDataIndex> for AddressDataIndex {
// fn checked_sub(self, rhs: Self) -> Option<Self> {
// self.0.checked_sub(rhs.0).map(Self)
// }
// }
// impl Printable for AddressDataIndex {
// fn to_string() -> &'static str {
// "p2pk33addressindex"
// }
// fn to_possible_strings() -> &'static [&'static str] {
// &["addr", "p2pk33addr", "p2pk33addressindex"]
// }
// }
// impl From<ByteView> for AddressDataIndex {
// fn from(value: ByteView) -> Self {
// Self::read_from_bytes(&value).unwrap()
// }
// }
// impl From<AddressDataIndex> for ByteView {
// fn from(value: AddressDataIndex) -> Self {
// Self::from(&value)
// }
// }
// impl From<&AddressDataIndex> for ByteView {
// fn from(value: &AddressDataIndex) -> Self {
// Self::new(value.as_bytes())
// }
// }

View File

@@ -0,0 +1,48 @@
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{
TypeIndex,
structs::{EmptyAddressIndex, LoadedAddressIndex},
};
const MIN_EMPTY_INDEX: u32 = u32::MAX - 4_000_000_000;
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout,
)]
pub struct AnyAddressIndex(TypeIndex);
impl AnyAddressIndex {
pub fn to_enum(&self) -> AnyAddressDataIndexEnum {
AnyAddressDataIndexEnum::from(*self)
}
}
impl Serialize for AnyAddressIndex {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_enum().serialize(serializer)
}
}
#[derive(Debug, Serialize)]
pub enum AnyAddressDataIndexEnum {
Loaded(LoadedAddressIndex),
Empty(EmptyAddressIndex),
}
impl From<AnyAddressIndex> for AnyAddressDataIndexEnum {
fn from(value: AnyAddressIndex) -> Self {
let uvalue = u32::from(value.0);
if uvalue >= MIN_EMPTY_INDEX {
Self::Empty(EmptyAddressIndex::from(TypeIndex::from(
uvalue - MIN_EMPTY_INDEX,
)))
} else {
Self::Loaded(LoadedAddressIndex::from(value.0))
}
}
}

View File

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

View File

@@ -0,0 +1,62 @@
use std::ops::Add;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{CheckedSub, Printable, TypeIndex};
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct EmptyAddressIndex(TypeIndex);
impl From<TypeIndex> for EmptyAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for EmptyAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<EmptyAddressIndex> for usize {
fn from(value: EmptyAddressIndex) -> Self {
usize::from(value.0)
}
}
impl Add<usize> for EmptyAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyAddressIndex> for EmptyAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for EmptyAddressIndex {
fn to_string() -> &'static str {
"emptyaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["emptyaddr", "emptyaddressindex"]
}
}

View File

@@ -31,7 +31,7 @@ impl From<TypeIndex> for EmptyOutputIndex {
}
impl From<EmptyOutputIndex> for usize {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(*value)
Self::from(value.0)
}
}
impl From<usize> for EmptyOutputIndex {
@@ -42,7 +42,7 @@ impl From<usize> for EmptyOutputIndex {
impl Add<usize> for EmptyOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyOutputIndex> for EmptyOutputIndex {

View File

@@ -1,18 +1,21 @@
use byteview::ByteView;
use serde::Serialize;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct AddressData {
pub struct LoadedAddressData {
pub sent: Sats,
pub received: Sats,
pub realized_cap: Dollars,
pub outputs_len: u32,
padding: u32,
}
impl AddressData {
impl LoadedAddressData {
pub fn amount(&self) -> Sats {
(u64::from(self.received) - u64::from(self.sent)).into()
}
@@ -55,23 +58,24 @@ impl AddressData {
}
}
impl From<EmptyAddressData> for AddressData {
impl From<EmptyAddressData> for LoadedAddressData {
fn from(value: EmptyAddressData) -> Self {
Self::from(&value)
}
}
impl From<&EmptyAddressData> for AddressData {
impl From<&EmptyAddressData> for LoadedAddressData {
fn from(value: &EmptyAddressData) -> Self {
Self {
sent: value.transfered,
received: value.transfered,
realized_cap: Dollars::ZERO,
outputs_len: 0,
padding: 0,
}
}
}
impl From<ByteView> for AddressData {
impl From<ByteView> for LoadedAddressData {
fn from(value: ByteView) -> Self {
Self {
// MUST be same order as impl From<&AddressData> for ByteView
@@ -79,16 +83,17 @@ impl From<ByteView> for AddressData {
received: Sats::read_from_bytes(&value[8..16]).unwrap(),
realized_cap: Dollars::read_from_bytes(&value[16..24]).unwrap(),
outputs_len: u32::read_from_bytes(&value[24..]).unwrap(),
padding: 0,
}
}
}
impl From<AddressData> for ByteView {
fn from(value: AddressData) -> Self {
impl From<LoadedAddressData> for ByteView {
fn from(value: LoadedAddressData) -> Self {
Self::from(&value)
}
}
impl From<&AddressData> for ByteView {
fn from(value: &AddressData) -> Self {
impl From<&LoadedAddressData> for ByteView {
fn from(value: &LoadedAddressData) -> Self {
Self::new(
&[
value.sent.as_bytes(),

View File

@@ -0,0 +1,60 @@
use std::ops::Add;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{CheckedSub, Printable, TypeIndex};
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct LoadedAddressIndex(TypeIndex);
impl From<TypeIndex> for LoadedAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for LoadedAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<LoadedAddressIndex> for usize {
fn from(value: LoadedAddressIndex) -> Self {
usize::from(value.0)
}
}
impl Add<usize> for LoadedAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<LoadedAddressIndex> for LoadedAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for LoadedAddressIndex {
fn to_string() -> &'static str {
"loadedaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["loadedaddr", "loadedaddressindex"]
}
}

View File

@@ -1,7 +1,6 @@
mod addressbytes;
mod addressbyteshash;
mod addressdata;
mod anyaddressdataindex;
mod anyaddressindex;
mod bitcoin;
mod blockhash;
mod blockhashprefix;
@@ -12,11 +11,14 @@ mod decadeindex;
mod difficultyepoch;
mod dollars;
mod emptyaddressdata;
mod emptyaddressindex;
mod emptyoutputindex;
mod feerate;
mod halvingepoch;
mod height;
mod inputindex;
mod loadedaddressdata;
mod loadedaddressindex;
mod monthindex;
mod ohlc;
mod opreturnindex;
@@ -59,8 +61,7 @@ mod yearindex;
pub use addressbytes::*;
pub use addressbyteshash::*;
pub use addressdata::*;
pub use anyaddressdataindex::*;
pub use anyaddressindex::*;
pub use bitcoin::*;
pub use blockhash::*;
pub use blockhashprefix::*;
@@ -71,11 +72,14 @@ pub use decadeindex::*;
pub use difficultyepoch::*;
pub use dollars::*;
pub use emptyaddressdata::*;
pub use emptyaddressindex::*;
pub use emptyoutputindex::*;
pub use feerate::*;
pub use halvingepoch::*;
pub use height::*;
pub use inputindex::*;
pub use loadedaddressdata::*;
pub use loadedaddressindex::*;
pub use monthindex::*;
pub use ohlc::*;
pub use opreturnindex::*;