parser: compress empty_address_data

This commit is contained in:
k
2024-10-09 00:33:14 +02:00
parent f5d427a04f
commit db60d4e453
4 changed files with 63 additions and 230 deletions

View File

@@ -79,11 +79,14 @@ impl AddressData {
}
pub fn from_empty(empty: &EmptyAddressData) -> Self {
let address_type = empty.address_type();
let transfered = empty.transfered();
Self {
address_type: empty.address_type,
address_type,
amount: Amount::ZERO,
sent: empty.transfered,
received: empty.transfered,
sent: transfered,
received: transfered,
realized_cap: Price::ZERO,
outputs_len: 0,
}

View File

@@ -19,3 +19,24 @@ pub enum AddressType {
P2WSH,
P2TR,
}
impl TryFrom<u64> for AddressType {
type Error = ();
fn try_from(u: u64) -> Result<Self, Self::Error> {
match u {
x if x == AddressType::Empty as u64 => Ok(AddressType::Empty),
x if x == AddressType::OpReturn as u64 => Ok(AddressType::OpReturn),
x if x == AddressType::PushOnly as u64 => Ok(AddressType::PushOnly),
x if x == AddressType::Unknown as u64 => Ok(AddressType::Unknown),
x if x == AddressType::MultiSig as u64 => Ok(AddressType::MultiSig),
x if x == AddressType::P2PK as u64 => Ok(AddressType::P2PK),
x if x == AddressType::P2PKH as u64 => Ok(AddressType::P2PKH),
x if x == AddressType::P2SH as u64 => Ok(AddressType::P2SH),
x if x == AddressType::P2WPKH as u64 => Ok(AddressType::P2WPKH),
x if x == AddressType::P2WSH as u64 => Ok(AddressType::P2WSH),
x if x == AddressType::P2TR as u64 => Ok(AddressType::P2TR),
_ => Err(()),
}
}
}

View File

@@ -4,12 +4,13 @@ use sanakirja::{direct_repr, Storable, UnsizedStorable};
use super::{AddressData, AddressType, Amount};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Allocative)]
pub struct EmptyAddressData {
pub address_type: AddressType,
pub transfered: Amount,
}
pub struct EmptyAddressData(u64);
direct_repr!(EmptyAddressData);
const SHIFT: u64 = 5;
const AND: u64 = (1 << SHIFT) - 1;
const MAX: u64 = (u64::MAX - 1) >> 5;
impl EmptyAddressData {
pub fn from_non_empty(non_empty: &AddressData) -> Self {
if non_empty.sent != non_empty.received {
@@ -17,9 +18,20 @@ impl EmptyAddressData {
panic!("Trying to convert not empty wallet to empty !");
}
Self {
address_type: non_empty.address_type,
transfered: non_empty.sent,
let transfered = non_empty.sent.to_sat();
if transfered >= MAX {
panic!("Too large !");
}
Self((transfered << SHIFT) + (non_empty.address_type as u64))
}
pub fn address_type(&self) -> AddressType {
(self.0 & AND).try_into().unwrap()
}
pub fn transfered(&self) -> Amount {
Amount::from_sat(self.0 >> SHIFT)
}
}