Skip to content

Commit

Permalink
Get rid of ethcontractresultdetails
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Oct 17, 2024
1 parent 565a367 commit 39d1fd4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
2 changes: 1 addition & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3066,7 +3066,7 @@ impl_runtime_apis! {
storage_deposit_limit.unwrap_or(u128::MAX),
pallet_revive::DebugInfo::UnsafeDebug,
pallet_revive::CollectEvents::UnsafeCollect,
).map(pallet_transaction_payment::Pallet::<Runtime>::compute_fee)
)
}

fn call(
Expand Down
48 changes: 36 additions & 12 deletions substrate/frame/revive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub mod evm;
pub mod test_utils;
pub mod weights;

pub use crate::exec::MomentOf;
use crate::{
exec::{AccountIdOf, ExecError, Executable, Ext, Key, Origin, Stack as ExecStack},
gas::GasMeter,
Expand All @@ -68,7 +67,7 @@ use frame_system::{
pallet_prelude::{BlockNumberFor, OriginFor},
EventRecord, Pallet as System,
};
pub use primitives::*;
use pallet_transaction_payment::OnChargeTransaction;
use scale_info::TypeInfo;
use sp_core::{H160, H256, U256};
use sp_runtime::{
Expand All @@ -79,8 +78,10 @@ use sp_runtime::{
pub use crate::{
address::{create1, create2, AddressMapper, DefaultAddressMapper},
debug::Tracing,
exec::MomentOf,
pallet::*,
};
pub use primitives::*;
pub use weights::WeightInfo;

#[cfg(doc)]
Expand All @@ -89,6 +90,7 @@ pub use crate::wasm::SyscallDoc;
type TrieId = BoundedVec<u8, ConstU32<128>>;
type BalanceOf<T> =
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
type OnChargeTransactionBalanceOf<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<T>>::Balance;
type CodeVec = BoundedVec<u8, ConstU32<{ limits::code::BLOB_BYTES }>>;
type EventRecordOf<T> =
EventRecord<<T as frame_system::Config>::RuntimeEvent, <T as frame_system::Config>::Hash>;
Expand Down Expand Up @@ -1149,10 +1151,14 @@ where
storage_deposit_limit: BalanceOf<T>,
debug: DebugInfo,
collect_events: CollectEvents,
) -> EthContractResultDetails<BalanceOf<T>>
) -> EthContractResult<BalanceOf<T>>
where
T: pallet_transaction_payment::Config,
<T as frame_system::Config>::RuntimeCall:
Dispatchable<Info = frame_support::dispatch::DispatchInfo>,
<T as Config>::RuntimeCall: From<crate::Call<T>>,
<T as Config>::RuntimeCall: Encode,
OnChargeTransactionBalanceOf<T>: Into<BalanceOf<T>>,
T::Nonce: Into<U256>,
{
use crate::evm::TransactionLegacyUnsigned;
Expand Down Expand Up @@ -1191,12 +1197,18 @@ where
}
.into();

EthContractResultDetails {
dispatch_info: dispatch_call.get_dispatch_info(),
len: dispatch_call.encode().len() as u32,
let dispatch_info = dispatch_call.get_dispatch_info();
let len = dispatch_call.encode().len() as u32;
EthContractResult {
gas_limit: result.gas_required,
storage_deposit: result.storage_deposit.charge_or_zero(),
result: result.result.map(|v| v.data),
fee: pallet_transaction_payment::Pallet::<T>::compute_fee(
len as u32,
&dispatch_info,
0u32.into(),
)
.into(),
}
} else {
let tx = TransactionLegacyUnsigned {
Expand Down Expand Up @@ -1227,12 +1239,18 @@ where
storage_deposit_limit: 0u32.into(),
};

return EthContractResultDetails {
dispatch_info: dispatch_call.get_dispatch_info(),
let dispatch_info = dispatch_call.get_dispatch_info();
let len = dispatch_call.encode().len() as u32;
return EthContractResult {
gas_limit: Default::default(),
storage_deposit: Default::default(),
len: dispatch_call.encode().len() as u32,
result: Err(<Error<T>>::DecodingFailed.into()),
fee: pallet_transaction_payment::Pallet::<T>::compute_fee(
len as u32,
&dispatch_info,
0u32.into(),
)
.into(),
}
};

Expand All @@ -1255,12 +1273,18 @@ where
}
.into();

EthContractResultDetails {
dispatch_info: dispatch_call.get_dispatch_info(),
len: dispatch_call.encode().len() as u32,
let dispatch_info = dispatch_call.get_dispatch_info();
let len = dispatch_call.encode().len() as u32;
EthContractResult {
gas_limit: result.gas_required,
storage_deposit: result.storage_deposit.charge_or_zero(),
result: result.result.map(|v| v.result.data),
fee: pallet_transaction_payment::Pallet::<T>::compute_fee(
len as u32,
&dispatch_info,
0u32.into(),
)
.into(),
}
}
}
Expand Down
36 changes: 1 addition & 35 deletions substrate/frame/revive/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::H160;
use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchInfo, weights::Weight};
use frame_support::weights::Weight;
use pallet_revive_uapi::ReturnFlags;
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -83,40 +83,6 @@ pub struct ContractResult<R, Balance, EventRecord> {
}

/// The result of the execution of a `eth_transact` call.
pub struct EthContractResultDetails<Balance> {
/// The call's dispatch info.
pub dispatch_info: DispatchInfo,
/// Length of the encoded call.
pub len: u32,
/// Gas limit of the transaction.
pub gas_limit: Weight,
/// Storage deposit charged.
pub storage_deposit: Balance,
/// The execution result.
pub result: Result<Vec<u8>, DispatchError>,
}

impl<Balance: From<u32>> EthContractResultDetails<Balance> {
/// Map to a EthContractResult, using the provided compute_fee function.
///
/// # Parameters
///
/// - `compute_fee`: A function that takes the length of the encoded call, the dispatch info and
/// the tip and returns the fee.
pub fn map(
self,
compute_fee: impl FnOnce(u32, &DispatchInfo, Balance) -> Balance,
) -> EthContractResult<Balance> {
EthContractResult {
result: self.result,
gas_limit: self.gas_limit,
storage_deposit: self.storage_deposit,
fee: compute_fee(self.len, &self.dispatch_info, 0.into()),
}
}
}

/// Similar to `EthContractResultDetails` but with the fee instead of dispatch info and len.
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct EthContractResult<Balance> {
/// The fee charged for the execution.
Expand Down

0 comments on commit 39d1fd4

Please sign in to comment.