use crate::{
evm::api::{GenericTransaction, TransactionSigned},
AccountIdOf, AddressMapper, BalanceOf, MomentOf, Weight, LOG_TARGET,
};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, GetDispatchInfo},
traits::{ExtrinsicCall, InherentBuilder, SignedTransactionBuilder},
};
use pallet_transaction_payment::OnChargeTransaction;
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_arithmetic::Percent;
use sp_core::{Get, H256, U256};
use sp_runtime::{
generic::{self, CheckedExtrinsic, ExtrinsicFormat},
traits::{
self, Checkable, Dispatchable, ExtrinsicLike, ExtrinsicMetadata, IdentifyAccount, Member,
TransactionExtension,
},
transaction_validity::{InvalidTransaction, TransactionValidityError},
OpaqueExtrinsic, RuntimeDebug, Saturating,
};
use alloc::vec::Vec;
type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
pub const GAS_PRICE: u32 = 1u32;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct UncheckedExtrinsic<Address, Signature, E: EthExtra>(
pub generic::UncheckedExtrinsic<Address, CallOf<E::Config>, Signature, E::Extension>,
);
impl<Address, Signature, E: EthExtra> TypeInfo for UncheckedExtrinsic<Address, Signature, E>
where
Address: StaticTypeInfo,
Signature: StaticTypeInfo,
E::Extension: StaticTypeInfo,
{
type Identity =
generic::UncheckedExtrinsic<Address, CallOf<E::Config>, Signature, E::Extension>;
fn type_info() -> scale_info::Type {
generic::UncheckedExtrinsic::<Address, CallOf<E::Config>, Signature, E::Extension>::type_info()
}
}
impl<Address, Signature, E: EthExtra>
From<generic::UncheckedExtrinsic<Address, CallOf<E::Config>, Signature, E::Extension>>
for UncheckedExtrinsic<Address, Signature, E>
{
fn from(
utx: generic::UncheckedExtrinsic<Address, CallOf<E::Config>, Signature, E::Extension>,
) -> Self {
Self(utx)
}
}
impl<Address: TypeInfo, Signature: TypeInfo, E: EthExtra> ExtrinsicLike
for UncheckedExtrinsic<Address, Signature, E>
{
fn is_bare(&self) -> bool {
ExtrinsicLike::is_bare(&self.0)
}
}
impl<Address, Signature, E: EthExtra> ExtrinsicMetadata
for UncheckedExtrinsic<Address, Signature, E>
{
const VERSION: u8 =
generic::UncheckedExtrinsic::<Address, CallOf<E::Config>, Signature, E::Extension>::VERSION;
type TransactionExtensions = E::Extension;
}
impl<Address: TypeInfo, Signature: TypeInfo, E: EthExtra> ExtrinsicCall
for UncheckedExtrinsic<Address, Signature, E>
{
type Call = CallOf<E::Config>;
fn call(&self) -> &Self::Call {
self.0.call()
}
}
use sp_runtime::traits::MaybeDisplay;
type OnChargeTransactionBalanceOf<T> = <<T as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<T>>::Balance;
impl<LookupSource, Signature, E, Lookup> Checkable<Lookup>
for UncheckedExtrinsic<LookupSource, Signature, E>
where
E: EthExtra,
Self: Encode,
<E::Config as frame_system::Config>::Nonce: TryFrom<U256>,
<E::Config as frame_system::Config>::RuntimeCall: Dispatchable<Info = DispatchInfo>,
OnChargeTransactionBalanceOf<E::Config>: Into<BalanceOf<E::Config>>,
BalanceOf<E::Config>: Into<U256> + TryFrom<U256>,
MomentOf<E::Config>: Into<U256>,
CallOf<E::Config>: From<crate::Call<E::Config>> + TryInto<crate::Call<E::Config>>,
<E::Config as frame_system::Config>::Hash: frame_support::traits::IsType<H256>,
LookupSource: Member + MaybeDisplay,
CallOf<E::Config>: Encode + Member + Dispatchable,
Signature: Member + traits::Verify,
<Signature as traits::Verify>::Signer: IdentifyAccount<AccountId = AccountIdOf<E::Config>>,
E::Extension: Encode + TransactionExtension<CallOf<E::Config>>,
Lookup: traits::Lookup<Source = LookupSource, Target = AccountIdOf<E::Config>>,
{
type Checked = CheckedExtrinsic<AccountIdOf<E::Config>, CallOf<E::Config>, E::Extension>;
fn check(self, lookup: &Lookup) -> Result<Self::Checked, TransactionValidityError> {
if !self.0.is_signed() {
if let Ok(call) = self.0.function.clone().try_into() {
if let crate::Call::eth_transact { payload, gas_limit, storage_deposit_limit } =
call
{
let checked = E::try_into_checked_extrinsic(
payload,
gas_limit,
storage_deposit_limit,
self.encoded_size(),
)?;
return Ok(checked)
};
}
}
self.0.check(lookup)
}
#[cfg(feature = "try-runtime")]
fn unchecked_into_checked_i_know_what_i_am_doing(
self,
lookup: &Lookup,
) -> Result<Self::Checked, TransactionValidityError> {
self.0.unchecked_into_checked_i_know_what_i_am_doing(lookup)
}
}
impl<Address, Signature, E: EthExtra> GetDispatchInfo for UncheckedExtrinsic<Address, Signature, E>
where
CallOf<E::Config>: GetDispatchInfo + Dispatchable,
{
fn get_dispatch_info(&self) -> DispatchInfo {
self.0.get_dispatch_info()
}
}
impl<Address: Encode, Signature: Encode, E: EthExtra> serde::Serialize
for UncheckedExtrinsic<Address, Signature, E>
{
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
self.0.serialize(seq)
}
}
impl<'a, Address: Decode, Signature: Decode, E: EthExtra> serde::Deserialize<'a>
for UncheckedExtrinsic<Address, Signature, E>
{
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
let r = sp_core::bytes::deserialize(de)?;
Decode::decode(&mut &r[..])
.map_err(|e| serde::de::Error::custom(alloc::format!("Decode error: {}", e)))
}
}
impl<Address, Signature, E: EthExtra> SignedTransactionBuilder
for UncheckedExtrinsic<Address, Signature, E>
where
Address: TypeInfo,
CallOf<E::Config>: TypeInfo,
Signature: TypeInfo,
E::Extension: TypeInfo,
{
type Address = Address;
type Signature = Signature;
type Extension = E::Extension;
fn new_signed_transaction(
call: Self::Call,
signed: Address,
signature: Signature,
tx_ext: E::Extension,
) -> Self {
generic::UncheckedExtrinsic::new_signed(call, signed, signature, tx_ext).into()
}
}
impl<Address, Signature, E: EthExtra> InherentBuilder for UncheckedExtrinsic<Address, Signature, E>
where
Address: TypeInfo,
CallOf<E::Config>: TypeInfo,
Signature: TypeInfo,
E::Extension: TypeInfo,
{
fn new_inherent(call: Self::Call) -> Self {
generic::UncheckedExtrinsic::new_bare(call).into()
}
}
impl<Address, Signature, E: EthExtra> From<UncheckedExtrinsic<Address, Signature, E>>
for OpaqueExtrinsic
where
Address: Encode,
Signature: Encode,
CallOf<E::Config>: Encode,
E::Extension: Encode,
{
fn from(extrinsic: UncheckedExtrinsic<Address, Signature, E>) -> Self {
Self::from_bytes(extrinsic.encode().as_slice()).expect(
"both OpaqueExtrinsic and UncheckedExtrinsic have encoding that is compatible with \
raw Vec<u8> encoding; qed",
)
}
}
pub trait EthExtra {
type Config: crate::Config + pallet_transaction_payment::Config;
type Extension: TransactionExtension<CallOf<Self::Config>>;
fn get_eth_extension(
nonce: <Self::Config as frame_system::Config>::Nonce,
tip: BalanceOf<Self::Config>,
) -> Self::Extension;
fn try_into_checked_extrinsic(
payload: Vec<u8>,
gas_limit: Weight,
storage_deposit_limit: BalanceOf<Self::Config>,
encoded_len: usize,
) -> Result<
CheckedExtrinsic<AccountIdOf<Self::Config>, CallOf<Self::Config>, Self::Extension>,
InvalidTransaction,
>
where
<Self::Config as frame_system::Config>::Nonce: TryFrom<U256>,
BalanceOf<Self::Config>: Into<U256> + TryFrom<U256>,
MomentOf<Self::Config>: Into<U256>,
<Self::Config as frame_system::Config>::RuntimeCall: Dispatchable<Info = DispatchInfo>,
OnChargeTransactionBalanceOf<Self::Config>: Into<BalanceOf<Self::Config>>,
CallOf<Self::Config>: From<crate::Call<Self::Config>>,
<Self::Config as frame_system::Config>::Hash: frame_support::traits::IsType<H256>,
{
let tx = TransactionSigned::decode(&payload).map_err(|err| {
log::debug!(target: LOG_TARGET, "Failed to decode transaction: {err:?}");
InvalidTransaction::Call
})?;
let signer = tx.recover_eth_address().map_err(|err| {
log::debug!(target: LOG_TARGET, "Failed to recover signer: {err:?}");
InvalidTransaction::BadProof
})?;
let signer =
<Self::Config as crate::Config>::AddressMapper::to_fallback_account_id(&signer);
let GenericTransaction { nonce, chain_id, to, value, input, gas, gas_price, .. } =
GenericTransaction::from_signed(tx, None);
if chain_id.unwrap_or_default() != <Self::Config as crate::Config>::ChainId::get().into() {
log::debug!(target: LOG_TARGET, "Invalid chain_id {chain_id:?}");
return Err(InvalidTransaction::Call);
}
let value = crate::Pallet::<Self::Config>::convert_evm_to_native(value.unwrap_or_default())
.map_err(|err| {
log::debug!(target: LOG_TARGET, "Failed to convert value to native: {err:?}");
InvalidTransaction::Call
})?;
let data = input.unwrap_or_default().0;
let call = if let Some(dest) = to {
crate::Call::call::<Self::Config> {
dest,
value,
gas_limit,
storage_deposit_limit,
data,
}
} else {
let blob = match polkavm::ProgramBlob::blob_length(&data) {
Some(blob_len) =>
blob_len.try_into().ok().and_then(|blob_len| (data.split_at_checked(blob_len))),
_ => None,
};
let Some((code, data)) = blob else {
log::debug!(target: LOG_TARGET, "Failed to extract polkavm code & data");
return Err(InvalidTransaction::Call);
};
crate::Call::instantiate_with_code::<Self::Config> {
value,
gas_limit,
storage_deposit_limit,
code: code.to_vec(),
data: data.to_vec(),
salt: None,
}
};
let nonce = nonce.unwrap_or_default().try_into().map_err(|_| InvalidTransaction::Call)?;
let eth_fee_no_tip = U256::from(GAS_PRICE)
.saturating_mul(gas.unwrap_or_default())
.try_into()
.map_err(|_| InvalidTransaction::Call)?;
let eth_fee: BalanceOf<Self::Config> = U256::from(gas_price.unwrap_or_default())
.saturating_mul(gas.unwrap_or_default())
.try_into()
.map_err(|_| InvalidTransaction::Call)?;
let info = call.get_dispatch_info();
let function: CallOf<Self::Config> = call.into();
let actual_fee: BalanceOf<Self::Config> =
pallet_transaction_payment::Pallet::<Self::Config>::compute_fee(
encoded_len as u32,
&info,
Default::default(),
)
.into();
log::trace!(target: LOG_TARGET, "try_into_checked_extrinsic: encoded_len: {encoded_len:?} actual_fee: {actual_fee:?} eth_fee: {eth_fee:?}");
if eth_fee < actual_fee {
log::debug!(target: LOG_TARGET, "fees {eth_fee:?} too low for the extrinsic {actual_fee:?}");
return Err(InvalidTransaction::Payment.into())
}
let min = actual_fee.min(eth_fee_no_tip);
let max = actual_fee.max(eth_fee_no_tip);
let diff = Percent::from_rational(max - min, min);
if diff > Percent::from_percent(10) {
log::trace!(target: LOG_TARGET, "Difference between the extrinsic fees {actual_fee:?} and the Ethereum gas fees {eth_fee_no_tip:?} should be no more than 10% got {diff:?}");
return Err(InvalidTransaction::Call.into())
} else {
log::trace!(target: LOG_TARGET, "Difference between the extrinsic fees {actual_fee:?} and the Ethereum gas fees {eth_fee_no_tip:?}: {diff:?}");
}
let tip = eth_fee.saturating_sub(eth_fee_no_tip);
log::debug!(target: LOG_TARGET, "Created checked Ethereum transaction with nonce {nonce:?} and tip: {tip:?}");
Ok(CheckedExtrinsic {
format: ExtrinsicFormat::Signed(signer.into(), Self::get_eth_extension(nonce, tip)),
function,
})
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
evm::*,
test_utils::*,
tests::{ExtBuilder, RuntimeCall, RuntimeOrigin, Test},
};
use frame_support::{error::LookupError, traits::fungible::Mutate};
use pallet_revive_fixtures::compile_module;
use sp_runtime::{
traits::{Checkable, DispatchTransaction},
MultiAddress, MultiSignature,
};
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Extra;
type SignedExtra = (frame_system::CheckNonce<Test>, ChargeTransactionPayment<Test>);
use pallet_transaction_payment::ChargeTransactionPayment;
impl EthExtra for Extra {
type Config = Test;
type Extension = SignedExtra;
fn get_eth_extension(nonce: u32, tip: BalanceOf<Test>) -> Self::Extension {
(frame_system::CheckNonce::from(nonce), ChargeTransactionPayment::from(tip))
}
}
type Ex = UncheckedExtrinsic<MultiAddress<AccountId32, u32>, MultiSignature, Extra>;
struct TestContext;
impl traits::Lookup for TestContext {
type Source = MultiAddress<AccountId32, u32>;
type Target = AccountIdOf<Test>;
fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError> {
match s {
MultiAddress::Id(id) => Ok(id),
_ => Err(LookupError),
}
}
}
#[derive(Clone)]
struct UncheckedExtrinsicBuilder {
tx: TransactionLegacyUnsigned,
gas_limit: Weight,
storage_deposit_limit: BalanceOf<Test>,
}
impl UncheckedExtrinsicBuilder {
fn new() -> Self {
Self {
tx: TransactionLegacyUnsigned {
chain_id: Some(<Test as crate::Config>::ChainId::get().into()),
gas_price: U256::from(GAS_PRICE),
..Default::default()
},
gas_limit: Weight::zero(),
storage_deposit_limit: 0,
}
}
fn estimate_gas(&mut self) {
let dry_run = crate::Pallet::<Test>::bare_eth_transact(
Account::default().substrate_account(),
self.tx.to,
self.tx.value.try_into().unwrap(),
self.tx.input.clone().0,
Weight::MAX,
u64::MAX,
|call| {
let call = RuntimeCall::Contracts(call);
let uxt: Ex = sp_runtime::generic::UncheckedExtrinsic::new_bare(call).into();
uxt.encoded_size() as u32
},
crate::DebugInfo::Skip,
crate::CollectEvents::Skip,
);
self.tx.gas = ((dry_run.fee + GAS_PRICE as u64) / (GAS_PRICE as u64)).into();
}
fn call_with(dest: H160) -> Self {
let mut builder = Self::new();
builder.tx.to = Some(dest);
builder.estimate_gas();
builder
}
fn instantiate_with(code: Vec<u8>, data: Vec<u8>) -> Self {
let mut builder = Self::new();
builder.tx.input = Bytes(code.into_iter().chain(data.into_iter()).collect());
builder.estimate_gas();
builder
}
fn update(mut self, f: impl FnOnce(&mut TransactionLegacyUnsigned) -> ()) -> Self {
f(&mut self.tx);
self
}
fn check(&self) -> Result<(RuntimeCall, SignedExtra), TransactionValidityError> {
let UncheckedExtrinsicBuilder { tx, gas_limit, storage_deposit_limit } = self.clone();
let account = Account::default();
let _ = <Test as crate::Config>::Currency::set_balance(
&account.substrate_account(),
100_000_000_000_000,
);
let payload = account.sign_transaction(tx.into()).signed_payload();
let call = RuntimeCall::Contracts(crate::Call::eth_transact {
payload,
gas_limit,
storage_deposit_limit,
});
let encoded_len = call.encoded_size();
let uxt: Ex = generic::UncheckedExtrinsic::new_bare(call).into();
let result: CheckedExtrinsic<_, _, _> = uxt.check(&TestContext {})?;
let (account_id, extra): (AccountId32, SignedExtra) = match result.format {
ExtrinsicFormat::Signed(signer, extra) => (signer, extra),
_ => unreachable!(),
};
extra.clone().validate_and_prepare(
RuntimeOrigin::signed(account_id),
&result.function,
&result.function.get_dispatch_info(),
encoded_len,
0,
)?;
Ok((result.function, extra))
}
}
#[test]
fn check_eth_transact_call_works() {
ExtBuilder::default().build().execute_with(|| {
let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
assert_eq!(
builder.check().unwrap().0,
crate::Call::call::<Test> {
dest: builder.tx.to.unwrap(),
value: builder.tx.value.as_u64(),
gas_limit: builder.gas_limit,
storage_deposit_limit: builder.storage_deposit_limit,
data: builder.tx.input.0
}
.into()
);
});
}
#[test]
fn check_eth_transact_instantiate_works() {
ExtBuilder::default().build().execute_with(|| {
let (code, _) = compile_module("dummy").unwrap();
let data = vec![];
let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone());
assert_eq!(
builder.check().unwrap().0,
crate::Call::instantiate_with_code::<Test> {
value: builder.tx.value.as_u64(),
gas_limit: builder.gas_limit,
storage_deposit_limit: builder.storage_deposit_limit,
code,
data,
salt: None
}
.into()
);
});
}
#[test]
fn check_eth_transact_nonce_works() {
ExtBuilder::default().build().execute_with(|| {
let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]))
.update(|tx| tx.nonce = 1u32.into());
assert_eq!(
builder.check(),
Err(TransactionValidityError::Invalid(InvalidTransaction::Future))
);
<crate::System<Test>>::inc_account_nonce(Account::default().substrate_account());
let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
assert_eq!(
builder.check(),
Err(TransactionValidityError::Invalid(InvalidTransaction::Stale))
);
});
}
#[test]
fn check_eth_transact_chain_id_works() {
ExtBuilder::default().build().execute_with(|| {
let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]))
.update(|tx| tx.chain_id = Some(42.into()));
assert_eq!(
builder.check(),
Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
);
});
}
#[test]
fn check_instantiate_data() {
ExtBuilder::default().build().execute_with(|| {
let code = b"invalid code".to_vec();
let data = vec![1];
let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone());
assert_eq!(
builder.clone().update(|tx| tx.input = Bytes(vec![1, 2, 3])).check(),
Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
);
});
}
#[test]
fn check_transaction_fees() {
ExtBuilder::default().build().execute_with(|| {
let scenarios: [(_, Box<dyn FnOnce(&mut TransactionLegacyUnsigned)>, _); 5] = [
("Eth fees too low", Box::new(|tx| tx.gas_price /= 2), InvalidTransaction::Payment),
("Gas fees too high", Box::new(|tx| tx.gas *= 2), InvalidTransaction::Call),
("Gas fees too low", Box::new(|tx| tx.gas *= 2), InvalidTransaction::Call),
(
"Diff > 10%",
Box::new(|tx| tx.gas = tx.gas * 111 / 100),
InvalidTransaction::Call,
),
(
"Diff < 10%",
Box::new(|tx| {
tx.gas_price *= 2;
tx.gas = tx.gas * 89 / 100
}),
InvalidTransaction::Call,
),
];
for (msg, update_tx, err) in scenarios {
let builder =
UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])).update(update_tx);
assert_eq!(builder.check(), Err(TransactionValidityError::Invalid(err)), "{}", msg);
}
});
}
#[test]
fn check_transaction_tip() {
ExtBuilder::default().build().execute_with(|| {
let (code, _) = compile_module("dummy").unwrap();
let data = vec![];
let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone())
.update(|tx| tx.gas_price = tx.gas_price * 103 / 100);
let tx = &builder.tx;
let expected_tip = tx.gas_price * tx.gas - U256::from(GAS_PRICE) * tx.gas;
let (_, extra) = builder.check().unwrap();
assert_eq!(U256::from(extra.1.tip()), expected_tip);
});
}
}