diff --git a/crates/brk_computer/src/states/supply.rs b/crates/brk_computer/src/states/supply.rs index 310362350..4109e76cd 100644 --- a/crates/brk_computer/src/states/supply.rs +++ b/crates/brk_computer/src/states/supply.rs @@ -75,8 +75,8 @@ impl Bytes for SupplyState { fn from_bytes(bytes: &[u8]) -> vecdb::Result { 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])?, }) } } diff --git a/crates/brk_error/src/lib.rs b/crates/brk_error/src/lib.rs index 713edb4ce..7bba379c7 100644 --- a/crates/brk_error/src/lib.rs +++ b/crates/brk_error/src/lib.rs @@ -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!( diff --git a/crates/brk_types/src/addressbytes.rs b/crates/brk_types/src/addressbytes.rs index 467b032c4..8be3378a6 100644 --- a/crates/brk_types/src/addressbytes.rs +++ b/crates/brk_types/src/addressbytes.rs @@ -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)))) diff --git a/crates/brk_types/src/emptyaddressdata.rs b/crates/brk_types/src/emptyaddressdata.rs index 697675b39..89a8e8457 100644 --- a/crates/brk_types/src/emptyaddressdata.rs +++ b/crates/brk_types/src/emptyaddressdata.rs @@ -67,9 +67,9 @@ impl Bytes for EmptyAddressData { fn from_bytes(bytes: &[u8]) -> vecdb::Result { 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])?, }) } } diff --git a/crates/brk_types/src/loadedaddressdata.rs b/crates/brk_types/src/loadedaddressdata.rs index 963bc4dfa..97ec8d6fe 100644 --- a/crates/brk_types/src/loadedaddressdata.rs +++ b/crates/brk_types/src/loadedaddressdata.rs @@ -163,13 +163,13 @@ impl Bytes for LoadedAddressData { fn from_bytes(bytes: &[u8]) -> vecdb::Result { 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])?, }) } } diff --git a/crates/brk_types/src/ohlc.rs b/crates/brk_types/src/ohlc.rs index 8b86f9524..ae667908f 100644 --- a/crates/brk_types/src/ohlc.rs +++ b/crates/brk_types/src/ohlc.rs @@ -90,10 +90,10 @@ impl Bytes for OHLCCents { fn from_bytes(bytes: &[u8]) -> vecdb::Result { Ok(Self { - open: Open::::from_bytes(&bytes[0..])?, - high: High::::from_bytes(&bytes[8..])?, - low: Low::::from_bytes(&bytes[16..])?, - close: Close::::from_bytes(&bytes[24..])?, + open: Open::::from_bytes(&bytes[0..8])?, + high: High::::from_bytes(&bytes[8..16])?, + low: Low::::from_bytes(&bytes[16..24])?, + close: Close::::from_bytes(&bytes[24..32])?, }) } } @@ -202,10 +202,10 @@ impl Bytes for OHLCDollars { fn from_bytes(bytes: &[u8]) -> vecdb::Result { Ok(Self { - open: Open::::from_bytes(&bytes[0..])?, - high: High::::from_bytes(&bytes[8..])?, - low: Low::::from_bytes(&bytes[16..])?, - close: Close::::from_bytes(&bytes[24..])?, + open: Open::::from_bytes(&bytes[0..8])?, + high: High::::from_bytes(&bytes[8..16])?, + low: Low::::from_bytes(&bytes[16..24])?, + close: Close::::from_bytes(&bytes[24..32])?, }) } } @@ -295,10 +295,10 @@ impl Bytes for OHLCSats { fn from_bytes(bytes: &[u8]) -> vecdb::Result { Ok(Self { - open: Open::::from_bytes(&bytes[0..])?, - high: High::::from_bytes(&bytes[8..])?, - low: Low::::from_bytes(&bytes[16..])?, - close: Close::::from_bytes(&bytes[24..])?, + open: Open::::from_bytes(&bytes[0..8])?, + high: High::::from_bytes(&bytes[8..16])?, + low: Low::::from_bytes(&bytes[16..24])?, + close: Close::::from_bytes(&bytes[24..32])?, }) } } diff --git a/crates/brk_types/src/outputtype.rs b/crates/brk_types/src/outputtype.rs index 9fb15ea50..edbf0ded6 100644 --- a/crates/brk_types/src/outputtype.rs +++ b/crates/brk_types/src/outputtype.rs @@ -911,9 +911,12 @@ impl Bytes for OutputType { #[inline] fn from_bytes(bytes: &[u8]) -> vecdb::Result { - if bytes.is_empty() { - return Err(vecdb::Error::WrongLength); - } + if bytes.len() != size_of::() { + return Err(vecdb::Error::WrongLength { + expected: size_of::(), + 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]) }; diff --git a/crates/brk_types/src/poolid.rs b/crates/brk_types/src/poolid.rs index 5f5744990..ca1c59a44 100644 --- a/crates/brk_types/src/poolid.rs +++ b/crates/brk_types/src/poolid.rs @@ -300,9 +300,12 @@ impl Bytes for PoolId { #[inline] fn from_bytes(bytes: &[u8]) -> vecdb::Result { - if bytes.is_empty() { - return Err(vecdb::Error::WrongLength); - } + if bytes.len() != size_of::() { + return Err(vecdb::Error::WrongLength { + expected: size_of::(), + 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]) };