diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index e1d33a0b76..1474e0c98f 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -160,6 +160,21 @@ pub type Executive = domain_pallet_executive::Executive< pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1, >; +/// Returns the storage fee for `len` bytes, or an overflow error. +fn consensus_storage_fee(len: impl TryInto) -> Result { + // This should never fail with the current types. + // But if converting to Balance would overflow, so would any multiplication. + let len = len.try_into().map_err(|_| { + TransactionValidityError::Invalid(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW)) + })?; + + BlockFees::consensus_chain_byte_fee() + .checked_mul(Into::::into(len)) + .ok_or(TransactionValidityError::Invalid( + InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW), + )) +} + impl fp_self_contained::SelfContainedCall for RuntimeCall { type SignedInfo = H160; @@ -195,10 +210,9 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { match self { RuntimeCall::Ethereum(call) => { // Ensure the caller can pay for the consensus chain storage fee - let Some(consensus_storage_fee) = - BlockFees::consensus_chain_byte_fee().checked_mul(Balance::from(len as u32)) - else { - return Some(Err(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW).into())); + let consensus_storage_fee = match consensus_storage_fee(len) { + Ok(fee) => fee, + Err(err) => return Some(Err(err)), }; let withdraw_res = { // Withdraw the consensus chain storage fee from the caller and record // it in the `BlockFees` - let Some(consensus_storage_fee) = - BlockFees::consensus_chain_byte_fee().checked_mul(Balance::from(len as u32)) - else { - return Some(Err(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW).into())); + let consensus_storage_fee = match consensus_storage_fee(len) { + Ok(fee) => fee, + Err(err) => return Some(Err(err)), }; match >::withdraw_fee( info, @@ -465,9 +478,7 @@ impl domain_pallet_executive::ExtrinsicStorageFees for ExtrinsicStorage charged_fees: Balance, tx_size: u32, ) -> Result<(), TransactionValidityError> { - let consensus_storage_fee = BlockFees::consensus_chain_byte_fee() - .checked_mul(Balance::from(tx_size)) - .ok_or(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW))?; + let consensus_storage_fee = consensus_storage_fee(tx_size)?; let (paid_consensus_storage_fee, paid_domain_fee) = if charged_fees <= consensus_storage_fee { @@ -1038,9 +1049,7 @@ fn pre_dispatch_evm_transaction( RuntimeCall::Ethereum(call) => { // Withdraw the consensus chain storage fee from the caller and record // it in the `BlockFees` - let consensus_storage_fee = BlockFees::consensus_chain_byte_fee() - .checked_mul(Balance::from(len as u32)) - .ok_or(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW))?; + let consensus_storage_fee = consensus_storage_fee(len)?; match >::withdraw_fee( &account_id, consensus_storage_fee.into(), diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index d56383a661..a452df7d7e 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -211,6 +211,21 @@ pub type Executive = domain_pallet_executive::Executive< AllPalletsWithSystem, >; +/// Returns the storage fee for `len` bytes, or an overflow error. +fn consensus_storage_fee(len: impl TryInto) -> Result { + // This should never fail with the current types. + // But if converting to Balance would overflow, so would any multiplication. + let len = len.try_into().map_err(|_| { + TransactionValidityError::Invalid(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW)) + })?; + + BlockFees::consensus_chain_byte_fee() + .checked_mul(Into::::into(len)) + .ok_or(TransactionValidityError::Invalid( + InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW), + )) +} + impl fp_self_contained::SelfContainedCall for RuntimeCall { type SignedInfo = H160; @@ -246,10 +261,9 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall { match self { RuntimeCall::Ethereum(call) => { // Ensure the caller can pay for the consensus chain storage fee - let Some(consensus_storage_fee) = - BlockFees::consensus_chain_byte_fee().checked_mul(Balance::from(len as u32)) - else { - return Some(Err(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW).into())); + let consensus_storage_fee = match consensus_storage_fee(len) { + Ok(fee) => fee, + Err(err) => return Some(Err(err)), }; let withdraw_res = { // Withdraw the consensus chain storage fee from the caller and record // it in the `BlockFees` - let Some(consensus_storage_fee) = - BlockFees::consensus_chain_byte_fee().checked_mul(Balance::from(len as u32)) - else { - return Some(Err(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW).into())); + let consensus_storage_fee = match consensus_storage_fee(len) { + Ok(fee) => fee, + Err(err) => return Some(Err(err)), }; match >::withdraw_fee( info, @@ -504,9 +517,7 @@ impl domain_pallet_executive::ExtrinsicStorageFees for ExtrinsicStorage charged_fees: Balance, tx_size: u32, ) -> Result<(), TransactionValidityError> { - let consensus_storage_fee = BlockFees::consensus_chain_byte_fee() - .checked_mul(Balance::from(tx_size)) - .ok_or(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW))?; + let consensus_storage_fee = consensus_storage_fee(tx_size)?; let (paid_consensus_storage_fee, paid_domain_fee) = if charged_fees <= consensus_storage_fee { @@ -1064,9 +1075,8 @@ fn pre_dispatch_evm_transaction( RuntimeCall::Ethereum(call) => { // Withdraw the consensus chain storage fee from the caller and record // it in the `BlockFees` - let consensus_storage_fee = BlockFees::consensus_chain_byte_fee() - .checked_mul(Balance::from(len as u32)) - .ok_or(InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW))?; + let consensus_storage_fee = consensus_storage_fee(len)?; + match >::withdraw_fee( &account_id, consensus_storage_fee.into(),