mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 07:09:59 -07:00
parser: compress empty_address_data
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user