Skip to main content

pallet_revive/evm/
runtime.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//! Runtime types for integrating `pallet-revive` with the EVM.
18use crate::{
19	AccountIdOf, AddressMapper, BalanceOf, CallOf, Config, LOG_TARGET, Pallet, Zero,
20	evm::{
21		CreateCallMode,
22		api::{GenericTransaction, TransactionSigned},
23		fees::InfoT,
24	},
25};
26use codec::{Decode, DecodeWithMemTracking, Encode};
27use frame_support::{
28	dispatch::{DispatchInfo, GetDispatchInfo},
29	traits::{
30		InherentBuilder, IsSubType, SignedTransactionBuilder,
31		fungible::Balanced,
32		tokens::{Fortitude, Precision, Preservation},
33	},
34};
35use pallet_transaction_payment::Config as TxConfig;
36use scale_info::{StaticTypeInfo, TypeInfo};
37use sp_core::U256;
38use sp_runtime::{
39	Debug, OpaqueExtrinsic, Weight,
40	generic::{self, CheckedExtrinsic, ExtrinsicFormat},
41	traits::{
42		Checkable, ExtrinsicCall, ExtrinsicLike, ExtrinsicMetadata, LazyExtrinsic, Pipeline,
43		TransactionExtension,
44	},
45	transaction_validity::{InvalidTransaction, TransactionValidityError},
46};
47
48/// Used to set the weight limit argument of a `eth_call` or `eth_instantiate_with_code` call.
49pub trait SetWeightLimit {
50	/// Set the weight limit of this call.
51	///
52	/// Returns the replaced weight.
53	fn set_weight_limit(&mut self, weight_limit: Weight) -> Weight;
54}
55
56/// Wraps [`generic::UncheckedExtrinsic`] to support checking unsigned
57/// [`crate::Call::eth_transact`] extrinsic.
58#[derive(Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug)]
59pub struct UncheckedExtrinsic<Address, Signature, E: EthExtra>(
60	pub  generic::UncheckedExtrinsic<
61		Address,
62		CallOf<E::Config>,
63		Signature,
64		E::ExtensionV0,
65		E::ExtensionOtherVersions,
66	>,
67);
68
69impl<Address, Signature, E: EthExtra> TypeInfo for UncheckedExtrinsic<Address, Signature, E>
70where
71	Address: StaticTypeInfo,
72	Signature: StaticTypeInfo,
73	E::ExtensionV0: StaticTypeInfo,
74{
75	type Identity = generic::UncheckedExtrinsic<
76		Address,
77		CallOf<E::Config>,
78		Signature,
79		E::ExtensionV0,
80		E::ExtensionOtherVersions,
81	>;
82	fn type_info() -> scale_info::Type {
83		generic::UncheckedExtrinsic::<
84			Address,
85			CallOf<E::Config>,
86			Signature,
87			E::ExtensionV0,
88			E::ExtensionOtherVersions,
89		>::type_info()
90	}
91}
92
93impl<Address, Signature, E: EthExtra>
94	From<
95		generic::UncheckedExtrinsic<
96			Address,
97			CallOf<E::Config>,
98			Signature,
99			E::ExtensionV0,
100			E::ExtensionOtherVersions,
101		>,
102	> for UncheckedExtrinsic<Address, Signature, E>
103{
104	fn from(
105		utx: generic::UncheckedExtrinsic<
106			Address,
107			CallOf<E::Config>,
108			Signature,
109			E::ExtensionV0,
110			E::ExtensionOtherVersions,
111		>,
112	) -> Self {
113		Self(utx)
114	}
115}
116
117impl<Address: TypeInfo, Signature: TypeInfo, E: EthExtra> ExtrinsicLike
118	for UncheckedExtrinsic<Address, Signature, E>
119{
120	fn is_bare(&self) -> bool {
121		ExtrinsicLike::is_bare(&self.0)
122	}
123}
124
125impl<Address, Signature, E: EthExtra> ExtrinsicMetadata
126	for UncheckedExtrinsic<Address, Signature, E>
127{
128	const VERSIONS: &'static [u8] = generic::UncheckedExtrinsic::<
129		Address,
130		CallOf<E::Config>,
131		Signature,
132		E::ExtensionV0,
133		E::ExtensionOtherVersions,
134	>::VERSIONS;
135	type TransactionExtensionPipelines = <generic::UncheckedExtrinsic<
136		Address,
137		CallOf<E::Config>,
138		Signature,
139		E::ExtensionV0,
140		E::ExtensionOtherVersions,
141	> as ExtrinsicMetadata>::TransactionExtensionPipelines;
142}
143
144impl<Address: TypeInfo, Signature: TypeInfo, E: EthExtra> ExtrinsicCall
145	for UncheckedExtrinsic<Address, Signature, E>
146{
147	type Call = CallOf<E::Config>;
148
149	fn call(&self) -> &Self::Call {
150		self.0.call()
151	}
152
153	fn into_call(self) -> Self::Call {
154		self.0.into_call()
155	}
156}
157
158impl<LookupSource, Signature, E, Lookup> Checkable<Lookup>
159	for UncheckedExtrinsic<LookupSource, Signature, E>
160where
161	E: EthExtra,
162	Self: Encode,
163	<E::Config as frame_system::Config>::Nonce: TryFrom<U256>,
164	CallOf<E::Config>: SetWeightLimit,
165	// required by Checkable for `generic::UncheckedExtrinsic`
166	generic::UncheckedExtrinsic<
167		LookupSource,
168		CallOf<E::Config>,
169		Signature,
170		E::ExtensionV0,
171		E::ExtensionOtherVersions,
172	>: Checkable<
173			Lookup,
174			Checked = CheckedExtrinsic<
175				AccountIdOf<E::Config>,
176				CallOf<E::Config>,
177				E::ExtensionV0,
178				E::ExtensionOtherVersions,
179			>,
180		>,
181{
182	type Checked = CheckedExtrinsic<
183		AccountIdOf<E::Config>,
184		CallOf<E::Config>,
185		E::ExtensionV0,
186		E::ExtensionOtherVersions,
187	>;
188
189	fn check(self, lookup: &Lookup) -> Result<Self::Checked, TransactionValidityError> {
190		if !self.0.is_signed() {
191			if let Some(crate::Call::eth_transact { payload }) = self.0.function.is_sub_type() {
192				log::trace!(
193					target: LOG_TARGET,
194					"eth_transact substrate tx hash: 0x{}",
195					sp_core::hexdisplay::HexDisplay::from(&sp_core::hashing::blake2_256(&self.encode())),
196				);
197				let checked = E::try_into_checked_extrinsic(payload, self.encoded_size())?;
198				return Ok(checked);
199			};
200		}
201		self.0.check(lookup)
202	}
203
204	#[cfg(feature = "try-runtime")]
205	fn unchecked_into_checked_i_know_what_i_am_doing(
206		self,
207		lookup: &Lookup,
208	) -> Result<Self::Checked, TransactionValidityError> {
209		self.0.unchecked_into_checked_i_know_what_i_am_doing(lookup)
210	}
211}
212
213impl<Address, Signature, E: EthExtra> GetDispatchInfo
214	for UncheckedExtrinsic<Address, Signature, E>
215{
216	fn get_dispatch_info(&self) -> DispatchInfo {
217		self.0.get_dispatch_info()
218	}
219}
220
221impl<Address: Encode, Signature: Encode, E: EthExtra> serde::Serialize
222	for UncheckedExtrinsic<Address, Signature, E>
223{
224	fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
225	where
226		S: ::serde::Serializer,
227	{
228		self.0.serialize(seq)
229	}
230}
231
232impl<'a, Address: DecodeWithMemTracking, Signature: DecodeWithMemTracking, E: EthExtra>
233	serde::Deserialize<'a> for UncheckedExtrinsic<Address, Signature, E>
234{
235	fn deserialize<D>(de: D) -> Result<Self, D::Error>
236	where
237		D: serde::Deserializer<'a>,
238	{
239		let r = sp_core::bytes::deserialize(de)?;
240		Decode::decode(&mut &r[..])
241			.map_err(|e| serde::de::Error::custom(alloc::format!("Decode error: {}", e)))
242	}
243}
244
245impl<Address, Signature, E: EthExtra> SignedTransactionBuilder
246	for UncheckedExtrinsic<Address, Signature, E>
247where
248	Address: TypeInfo,
249	Signature: TypeInfo,
250	E::ExtensionV0: TypeInfo,
251{
252	type Address = Address;
253	type Signature = Signature;
254	type Extension = E::ExtensionV0;
255
256	fn new_signed_transaction(
257		call: Self::Call,
258		signed: Address,
259		signature: Signature,
260		tx_ext: E::ExtensionV0,
261	) -> Self {
262		generic::UncheckedExtrinsic::new_signed(call, signed, signature, tx_ext).into()
263	}
264}
265
266impl<Address, Signature, E: EthExtra> InherentBuilder for UncheckedExtrinsic<Address, Signature, E>
267where
268	Address: TypeInfo,
269	Signature: TypeInfo,
270	E::ExtensionV0: TypeInfo,
271{
272	fn new_inherent(call: Self::Call) -> Self {
273		generic::UncheckedExtrinsic::new_bare(call).into()
274	}
275}
276
277impl<Address, Signature, E: EthExtra> From<UncheckedExtrinsic<Address, Signature, E>>
278	for OpaqueExtrinsic
279where
280	Address: Encode,
281	Signature: Encode,
282	E::ExtensionV0: Encode,
283{
284	fn from(extrinsic: UncheckedExtrinsic<Address, Signature, E>) -> Self {
285		extrinsic.0.into()
286	}
287}
288
289impl<Address, Signature, E: EthExtra> LazyExtrinsic for UncheckedExtrinsic<Address, Signature, E>
290where
291	generic::UncheckedExtrinsic<
292		Address,
293		CallOf<E::Config>,
294		Signature,
295		E::ExtensionV0,
296		E::ExtensionOtherVersions,
297	>: LazyExtrinsic,
298{
299	fn decode_unprefixed(data: &[u8]) -> Result<Self, codec::Error> {
300		Ok(Self(LazyExtrinsic::decode_unprefixed(data)?))
301	}
302}
303
304/// EthExtra convert an unsigned [`crate::Call::eth_transact`] into a [`CheckedExtrinsic`].
305pub trait EthExtra {
306	/// The Runtime configuration.
307	type Config: Config + TxConfig;
308
309	/// The Runtime's transaction extension version 0.
310	/// It should include at least:
311	/// - [`frame_system::CheckNonce`] to ensure that the nonce from the Ethereum transaction is
312	///   correct.
313	type ExtensionV0: TransactionExtension<CallOf<Self::Config>>;
314
315	/// The Runtime's transaction extension versions other than 0.
316	///
317	/// Use [`sp_runtime::traits::InvalidVersion`] if no other versions should be supported.
318	type ExtensionOtherVersions: Pipeline<CallOf<Self::Config>>;
319
320	/// Get the transaction extension to apply to an unsigned [`crate::Call::eth_transact`]
321	/// extrinsic.
322	///
323	/// # Parameters
324	/// - `nonce`: The nonce extracted from the Ethereum transaction.
325	/// - `tip`: The transaction tip calculated from the Ethereum transaction.
326	fn get_eth_extension(
327		nonce: <Self::Config as frame_system::Config>::Nonce,
328		tip: BalanceOf<Self::Config>,
329	) -> Self::ExtensionV0;
330
331	/// Convert the unsigned [`crate::Call::eth_transact`] into a [`CheckedExtrinsic`].
332	/// and ensure that the fees from the Ethereum transaction correspond to the fees computed from
333	/// the encoded_len and the injected weight_limit.
334	///
335	/// # Parameters
336	/// - `payload`: The RLP-encoded Ethereum transaction.
337	/// - `encoded_len`: The encoded length of the extrinsic.
338	fn try_into_checked_extrinsic(
339		payload: &[u8],
340		encoded_len: usize,
341	) -> Result<
342		CheckedExtrinsic<
343			AccountIdOf<Self::Config>,
344			CallOf<Self::Config>,
345			Self::ExtensionV0,
346			Self::ExtensionOtherVersions,
347		>,
348		InvalidTransaction,
349	>
350	where
351		<Self::Config as frame_system::Config>::Nonce: TryFrom<U256>,
352		CallOf<Self::Config>: SetWeightLimit,
353	{
354		let tx = TransactionSigned::decode(&payload).map_err(|err| {
355			log::debug!(target: LOG_TARGET, "Failed to decode transaction: {err:?}");
356			InvalidTransaction::Call
357		})?;
358
359		// Check transaction type and reject unsupported transaction types
360		match &tx {
361			crate::evm::api::TransactionSigned::Transaction1559Signed(_) |
362			crate::evm::api::TransactionSigned::Transaction2930Signed(_) |
363			crate::evm::api::TransactionSigned::TransactionLegacySigned(_) => {
364				// Supported transaction types, continue processing
365			},
366			crate::evm::api::TransactionSigned::Transaction7702Signed(_) => {
367				log::debug!(target: LOG_TARGET, "EIP-7702 transactions are not supported");
368				return Err(InvalidTransaction::Call);
369			},
370			crate::evm::api::TransactionSigned::Transaction4844Signed(_) => {
371				log::debug!(target: LOG_TARGET, "EIP-4844 transactions are not supported");
372				return Err(InvalidTransaction::Call);
373			},
374		}
375
376		let signer_addr = tx.recover_eth_address().map_err(|err| {
377			log::debug!(target: LOG_TARGET, "Failed to recover signer: {err:?}");
378			InvalidTransaction::BadProof
379		})?;
380
381		let signer = <Self::Config as Config>::AddressMapper::to_fallback_account_id(&signer_addr);
382		let base_fee = <Pallet<Self::Config>>::evm_base_fee();
383		let tx = GenericTransaction::from_signed(tx, base_fee, None);
384		let nonce = tx.nonce.unwrap_or_default().try_into().map_err(|_| {
385			log::debug!(target: LOG_TARGET, "Failed to convert nonce");
386			InvalidTransaction::Call
387		})?;
388
389		log::debug!(target: LOG_TARGET, "Decoded Ethereum transaction with signer: {signer_addr:?} nonce: {nonce:?}");
390		log::trace!(target: LOG_TARGET, "Decoded Ethereum transaction was: {tx:?}");
391		let call_info = tx.into_call::<Self::Config>(CreateCallMode::ExtrinsicExecution(
392			encoded_len as u32,
393			payload.to_vec(),
394		))?;
395		let storage_credit = <Self::Config as Config>::Currency::withdraw(
396			&signer,
397			call_info.storage_deposit,
398			Precision::Exact,
399			Preservation::Preserve,
400			Fortitude::Polite,
401		).map_err(|_| {
402			log::debug!(target: LOG_TARGET, "Not enough balance to hold additional storage deposit of {:?}", call_info.storage_deposit);
403			InvalidTransaction::Payment
404		})?;
405		<Self::Config as Config>::FeeInfo::deposit_txfee(storage_credit);
406
407		crate::tracing::if_tracing(|tracer| {
408			tracer.watch_address(&Pallet::<Self::Config>::block_author());
409			tracer.watch_address(&signer_addr);
410		});
411
412		log::debug!(target: LOG_TARGET, "\
413			Created checked Ethereum transaction with: \
414			from={signer_addr:?} \
415			eth_gas={} \
416			encoded_len={encoded_len} \
417			tx_fee={:?} \
418			storage_deposit={:?} \
419			weight_limit={} \
420			nonce={nonce:?}\
421			",
422			call_info.eth_gas_limit,
423			call_info.tx_fee,
424			call_info.storage_deposit,
425			call_info.weight_limit,
426		);
427
428		// We can't calculate a tip because it needs to be based on the actual gas used which we
429		// cannot know pre-dispatch. Hence we never supply a tip here or it would be way too high.
430		Ok(CheckedExtrinsic {
431			format: ExtrinsicFormat::Signed(
432				signer.into(),
433				Self::get_eth_extension(nonce, Zero::zero()),
434			),
435			function: call_info.call,
436		})
437	}
438}
439
440#[cfg(test)]
441mod test {
442	use super::*;
443	use crate::{
444		EthTransactInfo, RUNTIME_PALLETS_ADDR, Weight,
445		evm::*,
446		test_utils::*,
447		tests::{
448			Address, ExtBuilder, RuntimeCall, RuntimeOrigin, SignedExtra, Test, UncheckedExtrinsic,
449		},
450	};
451	use frame_support::{error::LookupError, traits::fungible::Mutate};
452	use pallet_revive_fixtures::compile_module;
453	use sp_runtime::traits::{self, Checkable, DispatchTransaction};
454
455	type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
456
457	struct TestContext;
458
459	impl traits::Lookup for TestContext {
460		type Source = Address;
461		type Target = AccountIdOf<Test>;
462		fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError> {
463			match s {
464				Self::Source::Id(id) => Ok(id),
465				_ => Err(LookupError),
466			}
467		}
468	}
469
470	/// A builder for creating an unchecked extrinsic, and test that the check function works.
471	#[derive(Clone)]
472	struct UncheckedExtrinsicBuilder {
473		tx: GenericTransaction,
474		before_validate: Option<std::sync::Arc<dyn Fn() + Send + Sync>>,
475		dry_run: Option<EthTransactInfo<BalanceOf<Test>>>,
476	}
477
478	impl UncheckedExtrinsicBuilder {
479		/// Create a new builder with default values.
480		fn new() -> Self {
481			Self {
482				tx: GenericTransaction {
483					from: Some(Account::default().address()),
484					chain_id: Some(<Test as Config>::ChainId::get().into()),
485					..Default::default()
486				},
487				before_validate: None,
488				dry_run: None,
489			}
490		}
491
492		fn data(mut self, data: Vec<u8>) -> Self {
493			self.tx.input = Bytes(data).into();
494			self
495		}
496
497		fn fund_account(account: &Account) {
498			let _ = <Test as Config>::Currency::set_balance(
499				&account.substrate_account(),
500				100_000_000_000_000,
501			);
502		}
503
504		fn estimate_gas(&mut self) {
505			let account = Account::default();
506			Self::fund_account(&account);
507
508			let dry_run =
509				crate::Pallet::<Test>::dry_run_eth_transact(self.tx.clone(), Default::default());
510			self.tx.gas_price = Some(<Pallet<Test>>::evm_base_fee());
511
512			match dry_run {
513				Ok(dry_run) => {
514					self.tx.gas = Some(dry_run.eth_gas);
515					self.dry_run = Some(dry_run);
516				},
517				Err(err) => {
518					log::debug!(target: LOG_TARGET, "Failed to estimate gas: {:?}", err);
519				},
520			}
521		}
522
523		/// Create a new builder with a call to the given address.
524		fn call_with(dest: H160) -> Self {
525			let mut builder = Self::new();
526			builder.tx.to = Some(dest);
527			builder
528		}
529
530		/// Create a new builder with an instantiate call.
531		fn instantiate_with(code: Vec<u8>, data: Vec<u8>) -> Self {
532			let mut builder = Self::new();
533			builder.tx.input = Bytes(code.into_iter().chain(data.into_iter()).collect()).into();
534			builder
535		}
536
537		/// Set before_validate function.
538		fn before_validate(mut self, f: impl Fn() + Send + Sync + 'static) -> Self {
539			self.before_validate = Some(std::sync::Arc::new(f));
540			self
541		}
542
543		fn check(
544			self,
545		) -> Result<
546			(u32, RuntimeCall, SignedExtra, GenericTransaction, Weight, TransactionSigned),
547			TransactionValidityError,
548		> {
549			self.mutate_estimate_and_check(Box::new(|_| ()))
550		}
551
552		/// Call `check` on the unchecked extrinsic, and `pre_dispatch` on the signed extension.
553		fn mutate_estimate_and_check(
554			mut self,
555			f: Box<dyn FnOnce(&mut GenericTransaction) -> ()>,
556		) -> Result<
557			(u32, RuntimeCall, SignedExtra, GenericTransaction, Weight, TransactionSigned),
558			TransactionValidityError,
559		> {
560			ExtBuilder::default().build().execute_with(|| self.estimate_gas());
561			ExtBuilder::default().build().execute_with(|| {
562				f(&mut self.tx);
563				let UncheckedExtrinsicBuilder { tx, before_validate, .. } = self.clone();
564
565				// Fund the account.
566				let account = Account::default();
567				Self::fund_account(&account);
568
569				let signed_transaction =
570					account.sign_transaction(tx.clone().try_into_unsigned().unwrap());
571				let call = RuntimeCall::Contracts(crate::Call::eth_transact {
572					payload: signed_transaction.signed_payload().clone(),
573				});
574
575				let uxt: UncheckedExtrinsic = generic::UncheckedExtrinsic::new_bare(call).into();
576				let encoded_len = uxt.encoded_size();
577				let result: CheckedExtrinsic<_, _, _> = uxt.check(&TestContext {})?;
578				let (account_id, extra): (AccountId32, SignedExtra) = match result.format {
579					ExtrinsicFormat::Signed(signer, extra) => (signer, extra),
580					_ => unreachable!(),
581				};
582
583				before_validate.map(|f| f());
584				extra.clone().validate_and_prepare(
585					RuntimeOrigin::signed(account_id),
586					&result.function,
587					&result.function.get_dispatch_info(),
588					encoded_len,
589					0,
590				)?;
591
592				Ok((
593					encoded_len as u32,
594					result.function,
595					extra,
596					tx,
597					self.dry_run.unwrap().weight_required,
598					signed_transaction,
599				))
600			})
601		}
602	}
603
604	#[test]
605	fn check_eth_transact_call_works() {
606		let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
607		let (expected_encoded_len, call, _, tx, weight_required, signed_transaction) =
608			builder.check().unwrap();
609		let expected_effective_gas_price =
610			ExtBuilder::default().build().execute_with(|| Pallet::<Test>::evm_base_fee());
611
612		match call {
613			RuntimeCall::Contracts(crate::Call::eth_call::<Test> {
614				dest,
615				value,
616				weight_limit,
617				data,
618				transaction_encoded,
619				effective_gas_price,
620				encoded_len,
621				..
622			}) if dest == tx.to.unwrap() &&
623				value == tx.value.unwrap_or_default().as_u64().into() &&
624				data == tx.input.to_vec() &&
625				transaction_encoded == signed_transaction.signed_payload() &&
626				effective_gas_price == expected_effective_gas_price =>
627			{
628				assert_eq!(encoded_len, expected_encoded_len);
629				assert!(
630					weight_limit.all_gte(weight_required),
631					"Assert failed: weight_limit={weight_limit:?} >= weight_required={weight_required:?}"
632				);
633			},
634			_ => panic!("Call does not match."),
635		}
636	}
637
638	#[test]
639	fn check_eth_transact_instantiate_works() {
640		let (expected_code, _) = compile_module("dummy").unwrap();
641		let expected_data = vec![];
642		let builder = UncheckedExtrinsicBuilder::instantiate_with(
643			expected_code.clone(),
644			expected_data.clone(),
645		);
646		let (expected_encoded_len, call, _, tx, weight_required, signed_transaction) =
647			builder.check().unwrap();
648		let expected_effective_gas_price =
649			ExtBuilder::default().build().execute_with(|| Pallet::<Test>::evm_base_fee());
650		let expected_value = tx.value.unwrap_or_default().as_u64().into();
651
652		match call {
653			RuntimeCall::Contracts(crate::Call::eth_instantiate_with_code::<Test> {
654				value,
655				weight_limit,
656				code,
657				data,
658				transaction_encoded,
659				effective_gas_price,
660				encoded_len,
661				..
662			}) if value == expected_value &&
663				code == expected_code &&
664				data == expected_data &&
665				transaction_encoded == signed_transaction.signed_payload() &&
666				effective_gas_price == expected_effective_gas_price =>
667			{
668				assert_eq!(encoded_len, expected_encoded_len);
669				assert!(
670					weight_limit.all_gte(weight_required),
671					"Assert failed: weight_limit={weight_limit:?} >= weight_required={weight_required:?}"
672				);
673			},
674			_ => panic!("Call does not match."),
675		}
676	}
677
678	#[test]
679	fn check_eth_transact_nonce_works() {
680		let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
681
682		assert_eq!(
683			builder.mutate_estimate_and_check(Box::new(|tx| tx.nonce = Some(1u32.into()))),
684			Err(TransactionValidityError::Invalid(InvalidTransaction::Future))
685		);
686
687		let builder =
688			UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])).before_validate(|| {
689				<crate::System<Test>>::inc_account_nonce(Account::default().substrate_account());
690			});
691
692		assert_eq!(
693			builder.check(),
694			Err(TransactionValidityError::Invalid(InvalidTransaction::Stale))
695		);
696	}
697
698	#[test]
699	fn check_eth_transact_chain_id_works() {
700		let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
701
702		assert_eq!(
703			builder.mutate_estimate_and_check(Box::new(|tx| tx.chain_id = Some(42.into()))),
704			Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
705		);
706	}
707
708	#[test]
709	fn check_instantiate_data() {
710		let code: Vec<u8> = polkavm_common::program::BLOB_MAGIC
711			.into_iter()
712			.chain(b"invalid code".iter().cloned())
713			.collect();
714		let data = vec![1];
715
716		let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone());
717
718		// Fail because the tx input fail to get the blob length
719		assert_eq!(
720			builder.check(),
721			Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
722		);
723	}
724
725	#[test]
726	fn check_transaction_fees() {
727		let scenarios: Vec<(_, Box<dyn FnOnce(&mut GenericTransaction)>, _)> = vec![
728			(
729				"Eth fees too low",
730				Box::new(|tx| {
731					tx.gas_price = Some(100u64.into());
732				}),
733				InvalidTransaction::Payment,
734			),
735			(
736				"Gas fees too low",
737				Box::new(|tx| {
738					tx.gas = Some(tx.gas.unwrap() / 2);
739				}),
740				InvalidTransaction::Payment,
741			),
742		];
743
744		for (msg, update_tx, err) in scenarios {
745			let res = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]))
746				.mutate_estimate_and_check(update_tx);
747
748			assert_eq!(res, Err(TransactionValidityError::Invalid(err)), "{}", msg);
749		}
750	}
751
752	#[test]
753	fn eth_pre_dispatch_weight_matches_check_weight_booking() {
754		let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20]));
755		let (encoded_len, call, _, _, _, signed_transaction) = builder.check().unwrap();
756
757		ExtBuilder::default().build().execute_with(|| {
758			let reported =
759				Pallet::<Test>::eth_pre_dispatch_weight(signed_transaction.signed_payload())
760					.unwrap();
761			let info = <Test as Config>::FeeInfo::dispatch_info(&call);
762			let expected = frame_system::calculate_consumed_extrinsic_weight::<CallOf<Test>>(
763				&<Test as frame_system::Config>::BlockWeights::get(),
764				&info,
765				encoded_len as usize,
766			);
767
768			assert_eq!(reported, expected);
769		});
770	}
771
772	#[test]
773	fn check_transaction_tip() {
774		let (code, _) = compile_module("dummy").unwrap();
775		// create some dummy data to increase the gas fee
776		let data = vec![42u8; crate::limits::CALLDATA_BYTES as usize];
777		let (_, _, extra, _tx, _gas_required, _) =
778			UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone())
779				.mutate_estimate_and_check(Box::new(|tx| {
780					tx.gas_price = Some(tx.gas_price.unwrap() * 103 / 100);
781					log::debug!(target: LOG_TARGET, "Gas price: {:?}", tx.gas_price);
782				}))
783				.unwrap();
784
785		assert_eq!(U256::from(extra.1.tip()), 0u32.into());
786	}
787
788	#[test]
789	fn check_runtime_pallets_addr_works() {
790		let remark: CallOf<Test> =
791			frame_system::Call::remark { remark: b"Hello, world!".to_vec() }.into();
792
793		let builder =
794			UncheckedExtrinsicBuilder::call_with(RUNTIME_PALLETS_ADDR).data(remark.encode());
795		let (_, call, _, _, _, _) = builder.check().unwrap();
796
797		match call {
798			RuntimeCall::Contracts(crate::Call::eth_substrate_call {
799				call: inner_call, ..
800			}) => {
801				assert_eq!(*inner_call, remark);
802			},
803			_ => panic!("Expected the RuntimeCall::Contracts variant, got: {:?}", call),
804		}
805	}
806
807	/// The raw bytes seen in this test is of a deployment transaction from [eip-2470] which publish
808	/// a contract at a predicable address on any chain that it's run on. We use these bytes to test
809	/// that if we were to run this transaction on pallet-revive that it would run and also produce
810	/// a contract at the address described in the EIP.
811	///
812	/// Note: the linked EIP is not an EIP for Nick's method, it's just an EIP that makes use of
813	/// Nick's method.
814	///
815	/// [eip-2470]: https://eips.ethereum.org/EIPS/eip-2470
816	#[test]
817	fn contract_deployment_with_nick_method_works() {
818		// Arrange
819		let raw_transaction_bytes = alloy_core::hex!(
820			"0xf9016c8085174876e8008303c4d88080b90154608060405234801561001057600080fd5b50610134806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634af63f0214602d575b600080fd5b60cf60048036036040811015604157600080fd5b810190602081018135640100000000811115605b57600080fd5b820183602082011115606c57600080fd5b80359060200191846001830284011164010000000083111715608d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550509135925060eb915050565b604080516001600160a01b039092168252519081900360200190f35b6000818351602085016000f5939250505056fea26469706673582212206b44f8a82cb6b156bfcc3dc6aadd6df4eefd204bc928a4397fd15dacf6d5320564736f6c634300060200331b83247000822470"
821		);
822
823		let mut signed_transaction = TransactionSigned::decode(raw_transaction_bytes.as_slice())
824			.expect("Invalid raw transaction bytes");
825		if let TransactionSigned::TransactionLegacySigned(ref mut legacy_transaction) =
826			signed_transaction
827		{
828			legacy_transaction.transaction_legacy_unsigned.gas =
829				U256::from_dec_str("3750815700000").unwrap();
830		}
831		let generic_transaction = GenericTransaction::from_signed(
832			signed_transaction.clone(),
833			ExtBuilder::default().build().execute_with(|| Pallet::<Test>::evm_base_fee()),
834			None,
835		);
836
837		let unchecked_extrinsic_builder = UncheckedExtrinsicBuilder {
838			tx: generic_transaction,
839			before_validate: None,
840			dry_run: None,
841		};
842
843		// Act
844		let eth_transact_result = unchecked_extrinsic_builder.check();
845
846		// Assert
847		let (
848			_encoded_len,
849			_function,
850			_extra,
851			generic_transaction,
852			_gas_required,
853			_signed_transaction,
854		) = eth_transact_result.expect("eth_transact failed");
855		assert!(
856			generic_transaction.chain_id.is_none(),
857			"Chain Id in the generic transaction is not None"
858		);
859	}
860}