From f690b1bc118d06d8db6e1c103ce94e7ff6378609 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Sun, 12 Nov 2023 22:35:57 +0100 Subject: [PATCH] Derive packable for ManaAllotment --- sdk/src/types/block/mana/allotment.rs | 39 ++++++++------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/sdk/src/types/block/mana/allotment.rs b/sdk/src/types/block/mana/allotment.rs index 16ad2ff509..fbb319f4d2 100644 --- a/sdk/src/types/block/mana/allotment.rs +++ b/sdk/src/types/block/mana/allotment.rs @@ -1,20 +1,18 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -use packable::{ - error::{UnpackError, UnpackErrorExt}, - packer::Packer, - unpacker::Unpacker, - Packable, -}; +use packable::Packable; use crate::types::block::{output::AccountId, protocol::ProtocolParameters, Error}; /// An allotment of Mana which will be added upon commitment of the slot in which the containing transaction was issued, /// in the form of Block Issuance Credits to the account. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Packable)] +#[packable(unpack_error = Error)] +#[packable(unpack_visitor = ProtocolParameters)] pub struct ManaAllotment { pub(crate) account_id: AccountId, + #[packable(verify_with = verify_mana)] pub(crate) mana: u64, } @@ -32,9 +30,8 @@ impl Ord for ManaAllotment { impl ManaAllotment { pub fn new(account_id: AccountId, mana: u64, protocol_params: &ProtocolParameters) -> Result { - if mana > protocol_params.mana_parameters().max_mana() { - return Err(Error::InvalidManaValue(mana)); - } + verify_mana::(&mana, protocol_params)?; + Ok(Self { account_id, mana }) } @@ -47,26 +44,12 @@ impl ManaAllotment { } } -impl Packable for ManaAllotment { - type UnpackError = Error; - type UnpackVisitor = ProtocolParameters; - - fn pack(&self, packer: &mut P) -> Result<(), P::Error> { - self.account_id.pack(packer)?; - self.mana.pack(packer)?; - - Ok(()) +fn verify_mana(mana: &u64, params: &ProtocolParameters) -> Result<(), Error> { + if VERIFY && *mana > params.mana_parameters().max_mana() { + return Err(Error::InvalidManaValue(*mana)); } - fn unpack( - unpacker: &mut U, - visitor: &Self::UnpackVisitor, - ) -> Result> { - let account_id = AccountId::unpack::<_, VERIFY>(unpacker, &()).coerce()?; - let mana = u64::unpack::<_, VERIFY>(unpacker, &()).coerce()?; - - Self::new(account_id, mana, visitor).map_err(UnpackError::Packable) - } + Ok(()) } #[cfg(feature = "serde")]