Skip to content

Commit

Permalink
Don't use a fixed size of serialized ValueBalance (#8732)
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn authored Jul 31, 2024
1 parent 8b8a7a4 commit 45261a2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion zebra-chain/src/value_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl ValueBalance<NonNegative> {

/// From byte array
#[allow(clippy::unwrap_in_result)]
pub fn from_bytes(bytes: [u8; 32]) -> Result<ValueBalance<NonNegative>, ValueBalanceError> {
pub fn from_bytes(bytes: &[u8]) -> Result<ValueBalance<NonNegative>, ValueBalanceError> {
let transparent = Amount::from_bytes(
bytes[0..8]
.try_into()
Expand Down
8 changes: 3 additions & 5 deletions zebra-chain/src/value_balance/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ proptest! {
fn value_balance_serialization(value_balance in any::<ValueBalance<NonNegative>>()) {
let _init_guard = zebra_test::init();

let bytes = value_balance.to_bytes();
let serialized_value_balance = ValueBalance::from_bytes(bytes)?;
let serialized_value_balance = ValueBalance::from_bytes(&value_balance.to_bytes())?;

prop_assert_eq!(value_balance, serialized_value_balance);
}
Expand All @@ -119,9 +118,8 @@ proptest! {
fn value_balance_deserialization(bytes in any::<[u8; 32]>()) {
let _init_guard = zebra_test::init();

if let Ok(deserialized) = ValueBalance::<NonNegative>::from_bytes(bytes) {
let bytes2 = deserialized.to_bytes();
prop_assert_eq!(bytes, bytes2);
if let Ok(deserialized) = ValueBalance::<NonNegative>::from_bytes(&bytes) {
prop_assert_eq!(bytes, deserialized.to_bytes());
}
}
}
3 changes: 1 addition & 2 deletions zebra-state/src/service/finalized_state/disk_format/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl IntoDisk for ValueBalance<NonNegative> {

impl FromDisk for ValueBalance<NonNegative> {
fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
let array = bytes.as_ref().try_into().unwrap();
ValueBalance::from_bytes(array).unwrap()
ValueBalance::from_bytes(bytes.as_ref()).expect("ValueBalance should be parsable")
}
}

Expand Down

0 comments on commit 45261a2

Please sign in to comment.