global: fixes

This commit is contained in:
nym21
2025-11-29 23:33:48 +01:00
parent 25c697cca1
commit 35e567cfb6
8 changed files with 51 additions and 37 deletions

View File

@@ -75,8 +75,8 @@ impl Bytes for SupplyState {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
utxo_count: u64::from_bytes(&bytes[0..])?,
value: Sats::from_bytes(&bytes[8..])?,
utxo_count: u64::from_bytes(&bytes[0..8])?,
value: Sats::from_bytes(&bytes[8..16])?,
})
}
}

View File

@@ -28,7 +28,7 @@ pub enum Error {
ZeroCopyError,
Vecs(vecdb::Error),
WrongLength,
WrongLength { expected: usize, received: usize },
WrongAddressType,
UnindexableDate,
QuickCacheError,
@@ -184,8 +184,10 @@ impl fmt::Display for Error {
Error::VecDB(error) => Display::fmt(&error, f),
Error::Vecs(error) => Display::fmt(&error, f),
Error::ZeroCopyError => write!(f, "ZeroCopy error"),
Error::WrongLength => write!(f, "Wrong length"),
Error::WrongLength { expected, received } => write!(
f,
"Wrong length, expected: {expected}, received: {received}"
),
Error::QuickCacheError => write!(f, "Quick cache error"),
Error::WrongAddressType => write!(f, "Wrong address type"),
Error::UnindexableDate => write!(

View File

@@ -54,9 +54,12 @@ impl TryFrom<(&ScriptBuf, OutputType)> for AddressBytes {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
67 => &bytes[1..66],
_ => {
len => {
dbg!(bytes);
return Err(Error::WrongLength);
return Err(Error::WrongLength {
expected: 67,
received: len,
});
}
};
Ok(Self::P2PK65(Box::new(P2PK65Bytes::from(bytes))))
@@ -65,9 +68,12 @@ impl TryFrom<(&ScriptBuf, OutputType)> for AddressBytes {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
35 => &bytes[1..34],
_ => {
len => {
dbg!(bytes);
return Err(Error::WrongLength);
return Err(Error::WrongLength {
expected: 35,
received: len,
});
}
};
Ok(Self::P2PK33(Box::new(P2PK33Bytes::from(bytes))))

View File

@@ -67,9 +67,9 @@ impl Bytes for EmptyAddressData {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
tx_count: u32::from_bytes(&bytes[0..])?,
funded_txo_count: u32::from_bytes(&bytes[4..])?,
transfered: Sats::from_bytes(&bytes[8..])?,
tx_count: u32::from_bytes(&bytes[0..4])?,
funded_txo_count: u32::from_bytes(&bytes[4..8])?,
transfered: Sats::from_bytes(&bytes[8..16])?,
})
}
}

View File

@@ -163,13 +163,13 @@ impl Bytes for LoadedAddressData {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
tx_count: u32::from_bytes(&bytes[0..])?,
funded_txo_count: u32::from_bytes(&bytes[4..])?,
spent_txo_count: u32::from_bytes(&bytes[8..])?,
padding: u32::from_bytes(&bytes[12..])?,
received: Sats::from_bytes(&bytes[16..])?,
sent: Sats::from_bytes(&bytes[24..])?,
realized_cap: Dollars::from_bytes(&bytes[32..])?,
tx_count: u32::from_bytes(&bytes[0..4])?,
funded_txo_count: u32::from_bytes(&bytes[4..8])?,
spent_txo_count: u32::from_bytes(&bytes[8..12])?,
padding: u32::from_bytes(&bytes[12..16])?,
received: Sats::from_bytes(&bytes[16..24])?,
sent: Sats::from_bytes(&bytes[24..32])?,
realized_cap: Dollars::from_bytes(&bytes[32..40])?,
})
}
}

View File

@@ -90,10 +90,10 @@ impl Bytes for OHLCCents {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
open: Open::<Cents>::from_bytes(&bytes[0..])?,
high: High::<Cents>::from_bytes(&bytes[8..])?,
low: Low::<Cents>::from_bytes(&bytes[16..])?,
close: Close::<Cents>::from_bytes(&bytes[24..])?,
open: Open::<Cents>::from_bytes(&bytes[0..8])?,
high: High::<Cents>::from_bytes(&bytes[8..16])?,
low: Low::<Cents>::from_bytes(&bytes[16..24])?,
close: Close::<Cents>::from_bytes(&bytes[24..32])?,
})
}
}
@@ -202,10 +202,10 @@ impl Bytes for OHLCDollars {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
open: Open::<Dollars>::from_bytes(&bytes[0..])?,
high: High::<Dollars>::from_bytes(&bytes[8..])?,
low: Low::<Dollars>::from_bytes(&bytes[16..])?,
close: Close::<Dollars>::from_bytes(&bytes[24..])?,
open: Open::<Dollars>::from_bytes(&bytes[0..8])?,
high: High::<Dollars>::from_bytes(&bytes[8..16])?,
low: Low::<Dollars>::from_bytes(&bytes[16..24])?,
close: Close::<Dollars>::from_bytes(&bytes[24..32])?,
})
}
}
@@ -295,10 +295,10 @@ impl Bytes for OHLCSats {
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
Ok(Self {
open: Open::<Sats>::from_bytes(&bytes[0..])?,
high: High::<Sats>::from_bytes(&bytes[8..])?,
low: Low::<Sats>::from_bytes(&bytes[16..])?,
close: Close::<Sats>::from_bytes(&bytes[24..])?,
open: Open::<Sats>::from_bytes(&bytes[0..8])?,
high: High::<Sats>::from_bytes(&bytes[8..16])?,
low: Low::<Sats>::from_bytes(&bytes[16..24])?,
close: Close::<Sats>::from_bytes(&bytes[24..32])?,
})
}
}

View File

@@ -911,9 +911,12 @@ impl Bytes for OutputType {
#[inline]
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
if bytes.is_empty() {
return Err(vecdb::Error::WrongLength);
}
if bytes.len() != size_of::<Self>() {
return Err(vecdb::Error::WrongLength {
expected: size_of::<Self>(),
received: bytes.len(),
});
};
// SAFETY: OutputType is repr(u8) and we're transmuting from u8
// All values 0-255 are valid (includes dummy variants)
let s: Self = unsafe { std::mem::transmute(bytes[0]) };

View File

@@ -300,9 +300,12 @@ impl Bytes for PoolId {
#[inline]
fn from_bytes(bytes: &[u8]) -> vecdb::Result<Self> {
if bytes.is_empty() {
return Err(vecdb::Error::WrongLength);
}
if bytes.len() != size_of::<Self>() {
return Err(vecdb::Error::WrongLength {
expected: size_of::<Self>(),
received: bytes.len(),
});
};
// SAFETY: PoolId is repr(u8) and we're transmuting from u8
// All values 0-255 are valid (includes dummy variants)
let s: Self = unsafe { std::mem::transmute(bytes[0]) };