Skip to content

Commit

Permalink
Deduplicate storage fee calculation code
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Mar 5, 2025
1 parent 5a72b05 commit e415afe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
37 changes: 23 additions & 14 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ pub type Executive = domain_pallet_executive::Executive<
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
>;

/// Returns the storage fee for `len` bytes, or an overflow error.
fn consensus_storage_fee(len: impl TryInto<Balance>) -> Result<Balance, TransactionValidityError> {
// 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::<Balance>::into(len))
.ok_or(TransactionValidityError::Invalid(
InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW),
))
}

impl fp_self_contained::SelfContainedCall for RuntimeCall {
type SignedInfo = H160;

Expand Down Expand Up @@ -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 = <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<
Runtime,
Expand Down Expand Up @@ -232,10 +246,9 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
RuntimeCall::Ethereum(call) => {
// 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 <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<Runtime>>::withdraw_fee(
info,
Expand Down Expand Up @@ -465,9 +478,7 @@ impl domain_pallet_executive::ExtrinsicStorageFees<Runtime> 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
{
Expand Down Expand Up @@ -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 <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<Runtime>>::withdraw_fee(
&account_id,
consensus_storage_fee.into(),
Expand Down
38 changes: 24 additions & 14 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Balance>) -> Result<Balance, TransactionValidityError> {
// 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::<Balance>::into(len))
.ok_or(TransactionValidityError::Invalid(
InvalidTransaction::Custom(ERR_BALANCE_OVERFLOW),
))
}

impl fp_self_contained::SelfContainedCall for RuntimeCall {
type SignedInfo = H160;

Expand Down Expand Up @@ -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 = <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<
Runtime,
Expand Down Expand Up @@ -283,10 +297,9 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
RuntimeCall::Ethereum(call) => {
// 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 <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<Runtime>>::withdraw_fee(
info,
Expand Down Expand Up @@ -504,9 +517,7 @@ impl domain_pallet_executive::ExtrinsicStorageFees<Runtime> 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
{
Expand Down Expand Up @@ -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 <InnerEVMCurrencyAdapter as pallet_evm::OnChargeEVMTransaction<Runtime>>::withdraw_fee(
&account_id,
consensus_storage_fee.into(),
Expand Down

0 comments on commit e415afe

Please sign in to comment.