referrerpolicy=no-referrer-when-downgrade

pallet_revive/
benchmarking.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
18//! Benchmarks for the revive pallet.
19
20#![cfg(feature = "runtime-benchmarks")]
21use crate::{
22	Pallet as Contracts,
23	access_list::{AccessEntry, AccessList, MAX_ACCESS_LIST_ENTRIES, StorageOp, Warmth},
24	call_builder::{CallSetup, Contract, VmBinaryModule, caller_funding, default_deposit_limit},
25	evm::{
26		TransactionLegacyUnsigned, TransactionSigned, TransactionUnsigned,
27		block_hash::EthereumBlockBuilder, block_storage,
28	},
29	exec::{Ext, Key, Origin as ExecOrigin, PrecompileExt},
30	limits::{self, CALLDATA_BYTES, EVM_MEMORY_BYTES, EVM_STACK_LIMIT},
31	precompiles::{
32		self, BenchmarkStorage, BenchmarkSystem, BuiltinPrecompile,
33		alloy::sol_types::{
34			SolType,
35			sol_data::{Bool, Bytes, FixedBytes, Uint},
36		},
37		run::builtin as run_builtin_precompile,
38	},
39	storage::WriteOutcome,
40	vm::{
41		BENCH_MIN_EVM_INIT_CODE_LEN, evm,
42		evm::{EvmOpcodeCosts, ExtBytecode, Halt, Interpreter, instructions},
43		pvm,
44	},
45	*,
46};
47use alloc::{vec, vec::Vec};
48use alloy_core::sol_types::{SolInterface, SolValue};
49use codec::{Encode, MaxEncodedLen};
50use core::ops::ControlFlow;
51use frame_benchmarking::v2::*;
52use frame_support::{
53	self, assert_ok,
54	migrations::SteppedMigration,
55	storage::child,
56	traits::{Hooks, fungible::InspectHold},
57	weights::{Weight, WeightMeter},
58};
59use frame_system::RawOrigin;
60use k256::ecdsa::SigningKey;
61use pallet_revive_uapi::{
62	CallFlags, ReturnErrorCode, StorageFlags, pack_hi_lo,
63	precompiles::{storage::IStorage, system::ISystem},
64};
65use rand::{Rng, SeedableRng, seq::SliceRandom};
66use rand_pcg::Pcg64;
67use revm::{
68	bytecode::{Bytecode, opcode::*},
69	primitives::{eip170::MAX_CODE_SIZE, eip3860::MAX_INITCODE_SIZE},
70};
71use sp_consensus_aura::AURA_ENGINE_ID;
72use sp_consensus_babe::{
73	BABE_ENGINE_ID,
74	digests::{PreDigest, PrimaryPreDigest},
75};
76use sp_consensus_slots::Slot;
77use sp_runtime::{SaturatedConversion, generic::DigestItem, traits::Zero};
78
79/// How many runs we do per API benchmark.
80///
81/// This is picked more or less arbitrary. We experimented with different numbers until
82/// the results appeared to be stable. Reducing the number would speed up the benchmarks
83/// but might make the results less precise.
84const API_BENCHMARK_RUNS: u32 = 1600;
85
86/// How far after its source the `evm_mcopy_per_byte` benchmark puts its destination.
87///
88/// It's just under a page, so every word the copy loads has the same lowest 12 address bits as the
89/// word it stored just before. It's also misaligned with the source, so `memmove` has to shift
90/// everything it copies.
91const MCOPY_DESTINATION_DISTANCE: u32 = 4091;
92
93macro_rules! memory(
94	($($bytes:expr,)*) => {{
95		vec![].iter()$(.chain($bytes.iter()))*.cloned().collect::<Vec<_>>()
96	}};
97);
98
99macro_rules! build_runtime(
100	($runtime:ident, $memory:ident: [$($segment:expr,)*]) => {
101		build_runtime!($runtime, _contract, $memory: [$($segment,)*]);
102	};
103	($runtime:ident, $contract:ident, $memory:ident: [$($bytes:expr,)*]) => {
104		build_runtime!($runtime, $contract);
105		let mut $memory = memory!($($bytes,)*);
106	};
107	($runtime:ident, $contract:ident) => {
108		let mut setup = CallSetup::<T>::default();
109		let $contract = setup.contract();
110		let input = setup.data();
111		let (mut ext, _) = setup.ext();
112		let mut $runtime = $crate::vm::pvm::Runtime::<_, [u8]>::new(&mut ext, input);
113	};
114);
115
116/// Get the pallet account and whitelist it for benchmarking.
117/// The account is warmed up `on_initialize` so read should not impact the PoV.
118fn whitelisted_pallet_account<T: Config>() -> T::AccountId {
119	let pallet_account = Pallet::<T>::account_id();
120	whitelist_account!(pallet_account);
121	pallet_account
122}
123
124/// Asserts that `account` is not whitelisted, so the benchmark measures its storage accesses.
125///
126/// Effective only in benchmark runs (e.g. `frame-omni-bencher`): the externalities backend used
127/// by `cargo test` does not track the whitelist.
128fn assert_account_not_whitelisted<T: Config>(account: &T::AccountId) {
129	let key = frame_system::Account::<T>::hashed_key_for(account);
130	let whitelisted = frame_benchmarking::benchmarking::get_whitelist()
131		.iter()
132		.any(|tracked| tracked.child_trie_key.is_none() && tracked.key == key);
133	assert!(!whitelisted, "the account is whitelisted, so the benchmark does not measure it");
134}
135
136/// Delegate `address` to `target` (EIP-7702) without going through signature recovery.
137///
138/// Returns the account id the `seal_call` family expects in guest memory: `address` is never
139/// mapped, so it resolves to its fallback account whose first 20 encoded bytes are `address`.
140fn delegated_eoa<T: Config>(address: H160, target: H160) -> Result<T::AccountId, BenchmarkError> {
141	let account_id = T::AddressMapper::to_fallback_account_id(&address);
142	AccountInfo::<T>::set_delegation(&address, Some(target), &account_id)
143		.map_err(|_| "set_delegation failed")?;
144	Ok(account_id)
145}
146
147/// Queues `contract` for deletion as a terminated contract that left dust on its account.
148///
149/// With no contract live at the address, the deletion queue reads the `AccountInfoOf` entry and
150/// runs a `clear_prefix` on `NativeDepositOf`, even when there are no rows. In ref time this
151/// branch is the worst case per entry. With a live contract the entry is 125 bytes larger (the
152/// encoded `ContractInfo` with a 32-byte trie id and `u128` balances, against `EOA` and its
153/// dust), but the `clear_prefix` is skipped. `deletion_queue_per_entry` charges the
154/// `AccountInfoOf` read at its max encoded length, so its proof size covers either branch. The
155/// branch is also taken when the address has no entry at all; `dust: 1` keeps a stored entry so
156/// that the measured read decodes a value.
157fn queue_terminated_for_deletion<T: Config>(contract: &Contract<T>) -> Result<(), BenchmarkError> {
158	let info = contract.info()?;
159	AccountInfoOf::<T>::insert(&contract.address, AccountInfo { dust: 1, ..Default::default() });
160	ContractInfo::<T>::queue_for_deletion(info.trie_id, contract.account_id.clone());
161	Ok(())
162}
163
164/// Sets the dust of `address` so that receiving `dust` makes the transfer mint a plank.
165fn set_dust_so_transfer_mints<T: Config>(address: &H160, dust: u32) {
166	AccountInfoOf::<T>::mutate(address, |info| {
167		info.get_or_insert_default().dust = T::NativeToEthRatio::get() - dust;
168	});
169}
170
171/// Pushes the data a benchmark set up out of the L1 and L2 caches by writing unrelated memory.
172///
173/// Writes 8 MiB, which is larger than the L2 cache of the reference hardware.
174fn evict_caches() {
175	const EVICTION_SIZE: usize = 8 * 1024 * 1024;
176	core::hint::black_box(vec![1u8; EVICTION_SIZE]);
177}
178
179/// The most iterations of the `instr_load_chain_<m>` benchmarks.
180///
181/// An iteration makes `LOAD_CHAIN_GAS - 3` loads, so the largest `r` goes around the cycle of
182/// [`VmBinaryModule::LOAD_CHAIN_SLOTS`] slots several times.
183const LOAD_CHAIN_ITERATIONS: u32 = 20;
184
185/// The most laps of the `instr_indirect_jump` benchmark.
186///
187/// A lap makes `INDIRECT_JUMP_GAS - 2` indirect jumps, so the largest `r` makes about 400000.
188const INDIRECT_JUMP_LAPS: u32 = 400_000 / (crate::metering::INDIRECT_JUMP_GAS - 2);
189
190/// Prepares the call of the `instr_load_chain_<m>` benchmark for variant `m` and `r` iterations.
191///
192/// Each iteration runs [`crate::metering::LOAD_CHAIN_GAS`] gas: the loop condition, a basic block
193/// of dependent 8 byte loads, the counter increment and the jump back (see
194/// [`VmBinaryModule::with_load_chain`]). The loads follow a random cycle through the
195/// [`VmBinaryModule::LOAD_CHAIN_SLOTS`] slots of the contract's read-write data, and `r` may go
196/// around the cycle several times. Every value is at `LOAD_CHAIN_SLOT_OFFSET - 8 * m` in its
197/// slot, so in one of the variants every load spans two host cache lines. This assumes that the
198/// eight variants, which run the same setup, get the same address modulo 64 for the read-write
199/// data (see the comment on `LOAD_CHAIN_SLOT_OFFSET` below). Writing the chain brings it into the
200/// CPU caches, where it stays in the L2 but not in the L1 data cache of the reference hardware (see
201/// [`VmBinaryModule::LOAD_CHAIN_SLOTS`]). Every load therefore waits for the previous one to
202/// come from the L2 cache, which is where the cost per gas assumes contract memory to be.
203///
204/// `r` starts at 1: the interpreter compiles the loop body when execution first enters it, which
205/// `r = 0` never does. With every `r` running at least one iteration, compiling the loop is in
206/// the intercept and the slope is one iteration over compiled code.
207///
208/// [`crate::metering::EngineMeter::ref_time_per_fuel`] divides the slope of every variant by
209/// [`crate::metering::LOAD_CHAIN_GAS`] and takes the largest.
210fn prepare_load_chain<E: Ext>(prepared: &mut pvm::PreparedCall<E>, m: u32, r: u32) {
211	/// The largest offset of the 8 byte value in a slot.
212	///
213	/// Variant `m` puts the value of every slot at `LOAD_CHAIN_SLOT_OFFSET - 8 * m`.
214	///
215	/// An 8 byte load spans two 64 byte cache lines when it starts in the last 7 bytes of a line.
216	/// The interpreter keeps the read-write data in a byte vector, whose host address is only
217	/// known to be 8 byte aligned. No offset makes a load span two lines for every such address.
218	/// The 8 offsets start 4 bytes past an 8 byte boundary and take all 8 such positions modulo
219	/// 64. The slots are a multiple of 64 bytes apart, so for any 8 byte aligned host address
220	/// exactly one variant has every load span two cache lines. This assumes that all 8 variants
221	/// get the same host address modulo 64 for the read-write data, since they run the same setup
222	/// with the same allocations. Every value ends within its slot, so values of different slots
223	/// do not overlap.
224	const LOAD_CHAIN_SLOT_OFFSET: u32 = 60;
225	const {
226		let slot_bytes = VmBinaryModule::LOAD_CHAIN_SLOT_BYTES;
227		assert!(slot_bytes.is_multiple_of(64));
228		assert!(LOAD_CHAIN_SLOT_OFFSET % 8 == 4 && LOAD_CHAIN_SLOT_OFFSET >= 8 * 7);
229		assert!(LOAD_CHAIN_SLOT_OFFSET + 8 <= slot_bytes);
230	}
231
232	let slots = VmBinaryModule::LOAD_CHAIN_SLOTS;
233	let slot_bytes = VmBinaryModule::LOAD_CHAIN_SLOT_BYTES;
234	let base = prepared.memory_map().rw_data_address();
235	let offset = LOAD_CHAIN_SLOT_OFFSET - 8 * m;
236	let address = |slot: u32| base + slot * slot_bytes + offset;
237	let mut order: Vec<u32> = (0..slots).collect();
238	order.shuffle(&mut Pcg64::seed_from_u64(1337));
239	// Only the chain's values are written, so the chain is all the memory the setup brings into
240	// the caches right before the call.
241	for (k, &slot) in order.iter().enumerate() {
242		let next = order[(k + 1) % order.len()];
243		prepared
244			.write_memory(address(slot), &u64::from(address(next)).to_le_bytes())
245			.expect("every value ends within its slot of the read-write data; qed");
246	}
247	prepared.set_reg(polkavm::Reg::A0, address(order[0]).into());
248	prepared.set_reg(polkavm::Reg::A1, r.into());
249}
250
251/// Pushes `values` onto the interpreter's stack in order, so the last value ends up on top.
252fn setup_stack<E: Ext>(interpreter: &mut Interpreter<E>, values: impl IntoIterator<Item = U256>) {
253	for value in values {
254		interpreter.stack.push(value).continue_value().unwrap();
255	}
256}
257
258/// Returns the operands of `r` pairs of `SLT` or `SGT` and `POP` op-codes, in the order they are
259/// pushed onto the stack.
260fn signed_comparison_operands(r: u32) -> Vec<U256> {
261	// Every limb is one, so adding one to a limb makes the operands first differ at that limb.
262	const POSITIVE: U256 = U256([1, 1, 1, 1]);
263	const NEGATIVE: U256 = U256([1, 1, 1, (1 << 63) + 1]);
264
265	fn same_sign_operands(rng: &mut Pcg64, base: U256) -> [U256; 2] {
266		match [3, 2, 1, 0].into_iter().find(|_| rng.gen_bool(0.5)) {
267			Some(limb) => [base, base + (U256::one() << (64 * limb))],
268			None => [base, base],
269		}
270	}
271
272	let mut rng = Pcg64::seed_from_u64(1337);
273	(0..r)
274		.flat_map(|_| {
275			let mut operands = if rng.gen_bool(0.5) {
276				same_sign_operands(&mut rng, NEGATIVE)
277			} else if rng.gen_bool(0.5) {
278				[U256::zero(), POSITIVE]
279			} else {
280				same_sign_operands(&mut rng, POSITIVE)
281			};
282			operands.shuffle(&mut rng);
283			operands
284		})
285		.collect()
286}
287
288enum Outcome {
289	DifferInLimb3,
290	DifferInLimb2,
291	DifferInLimb1,
292	DifferInLimb0,
293	Equal,
294}
295
296impl Outcome {
297	const ALL: [Self; 5] = [
298		Self::DifferInLimb3,
299		Self::DifferInLimb2,
300		Self::DifferInLimb1,
301		Self::DifferInLimb0,
302		Self::Equal,
303	];
304}
305
306/// The code of the `evm_dispatch_mix` benchmark: a pseudo-random, stack-neutral mix of cheap
307/// op-codes, built in blocks that each contain the op-codes of [`EvmOpcodeCosts::DISPATCH_MIX`]
308/// the number of times it lists.
309struct EvmMixedOpcodes;
310
311impl EvmMixedOpcodes {
312	/// The number of op-codes in a block.
313	const CODE_BLOCK_SIZE: usize = {
314		let mut size = 0;
315		let mut index = 0;
316		while index < EvmOpcodeCosts::DISPATCH_MIX.len() {
317			size += EvmOpcodeCosts::DISPATCH_MIX[index].repetitions as usize;
318			index += 1;
319		}
320		size
321	};
322
323	/// The height of the stack at the start and the end of every block.
324	const STACK_INITIAL_HEIGHT: usize = 16;
325
326	/// The highest the stack gets, within its initial capacity so pushing never reallocates it.
327	const STACK_MAX_HEIGHT: usize = 32;
328
329	/// Returns `r` blocks of op-codes, each in its own pseudo-random order.
330	fn generate_code(rng: &mut impl Rng, r: u32) -> Vec<u8> {
331		let mut code = Vec::with_capacity(r as usize * Self::CODE_BLOCK_SIZE);
332		for _ in 0..r {
333			Self::generate_block(rng, &mut code);
334		}
335		code
336	}
337
338	/// Appends one block to `code` in a pseudo-random order that keeps the stack between empty and
339	/// `STACK_MAX_HEIGHT`.
340	fn generate_block(rng: &mut impl Rng, code: &mut Vec<u8>) {
341		use alloc::collections::VecDeque;
342
343		let mut remaining = EvmOpcodeCosts::DISPATCH_MIX
344			.into_iter()
345			.flat_map(|mix_opcode| {
346				let info = OpCode::info_by_op(mix_opcode.opcode)
347					.expect("every op-code of the dispatch mix is defined; qed");
348				let (pops, pushes) = (usize::from(info.inputs()), usize::from(info.outputs()));
349				vec![(mix_opcode.opcode, pops, pushes); mix_opcode.repetitions as usize]
350			})
351			.collect::<VecDeque<_>>();
352		remaining.make_contiguous().shuffle(rng);
353		let mut height = Self::STACK_INITIAL_HEIGHT;
354		while let Some(instruction @ (op_code, pops, pushes)) = remaining.pop_front() {
355			if pops <= height && height - pops + pushes <= Self::STACK_MAX_HEIGHT {
356				code.push(op_code);
357				height = height - pops + pushes;
358			} else {
359				remaining.push_back(instruction);
360			}
361		}
362	}
363}
364
365/// Pairs a pseudo-randomly chosen denominator with the shared `2^255` numerator.
366fn unsigned_division_operands(rng: &mut impl Rng, denominators: &[U256]) -> [U256; 2] {
367	let denominator = denominators.choose(rng).copied().unwrap();
368	[denominator, knuth_division_operands::NUMERATOR]
369}
370
371/// Like [`unsigned_division_operands`], but negates the divisor pseudo-randomly.
372fn signed_division_operands(rng: &mut impl Rng, denominators: &[U256]) -> [U256; 2] {
373	let [magnitude, numerator] = unsigned_division_operands(rng, denominators);
374	let divisor =
375		if rng.gen_bool(0.5) { magnitude } else { U256::zero().overflowing_sub(magnitude).0 };
376	[divisor, numerator]
377}
378
379/// Operands covering all 17 correction patterns possible when dividing `2^255` by two-limb
380/// divisors.
381///
382/// Knuth's Algorithm D can correct the highest quotient limb zero or one time and each lower limb
383/// up to twice. The pattern `1, 0, 2` is impossible for this numerator. The counts were checked
384/// against `uint` 0.10's division. None of these operands needs the rarer correction that adds
385/// the divisor back after subtracting.
386///
387/// Picking a denominator pseudo-randomly varies the correction branches across divisions. Each
388/// denominator also makes the `u128` divisions behind the estimates take as many steps as any we
389/// found with its pattern. Correcting a limb twice needs a second denominator limb above one, which
390/// shortens the division behind the first estimate.
391///
392/// The fixed divisors are the denominators that measured slowest for each op-code. The slowest one
393/// depends on the op-code: `SDIV` and `SMOD` measured slowest with different patterns than `DIV`
394/// and `MOD`. After updating `uint` or the Rust toolchain, search for the denominators again and
395/// measure which one is slowest for each op-code.
396mod knuth_division_operands {
397	use super::{Rng, U256};
398
399	pub fn fixed_signed_operands(rng: &mut impl Rng, magnitude: U256) -> [U256; 2] {
400		// Consume the divisor draw to match the baseline's pseudo-random sign sequence.
401		let _ = rng.gen_range(0..DENOMINATORS.len() as u32);
402		let divisor =
403			if rng.gen_bool(0.5) { magnitude } else { U256::zero().overflowing_sub(magnitude).0 };
404		[divisor, NUMERATOR]
405	}
406
407	/// 2^255 as an unsigned value, or -2^255 as a signed one, so signed and unsigned divisions
408	/// divide the same magnitudes.
409	pub const NUMERATOR: U256 = U256([0, 0, 0, 1 << 63]);
410	/// The denominator that measured slowest for `DIV`.
411	pub const DIV_FIXED_DIVISOR: U256 = U256([0x05f0_ac84_84d2_a8f7, 0x1, 0, 0]);
412	/// The denominator that measured slowest for `SDIV`.
413	pub const SDIV_FIXED_DIVISOR: U256 = U256([0x16d5_ec4a_0e7b_8cdb, 0x1, 0, 0]);
414	/// The denominator that measured slowest for `MOD`.
415	pub const MOD_FIXED_DIVISOR: U256 = U256([0x0007_f84c_6624_4131, 0x1, 0, 0]);
416	/// The denominator that measured slowest for `SMOD`.
417	pub const SMOD_FIXED_DIVISOR: U256 = U256([0x5141_1d54_0d50_9ec1, 0x1, 0, 0]);
418	/// `ADDMOD` reuses the divisor of `MOD`.
419	pub const ADDMOD_FIXED_DIVISOR: U256 = MOD_FIXED_DIVISOR;
420
421	/// Two distinct divisors per pattern, with correction counts listed in limb order 2, 1, 0.
422	pub const DENOMINATORS: [U256; 34] = [
423		// Corrections: 0, 0, 0.
424		U256([0x1ac6_6dea_3270_0980, 0x1, 0, 0]),
425		U256([0x1150_77a4_12e2_ccc0, 0x1, 0, 0]),
426		// Corrections: 0, 0, 1.
427		U256([0x3c9b_ee44_0b3f_dba7, 0x1, 0, 0]),
428		U256([0x62aa_fc46_0845_aad9, 0x1, 0, 0]),
429		// Corrections: 0, 1, 0.
430		U256([0x2806_4b94_70ef_f3d9, 0x1, 0, 0]),
431		U256([0x61d1_c4c6_195a_2d43, 0x1, 0, 0]),
432		// Corrections: 0, 1, 1.
433		SDIV_FIXED_DIVISOR,
434		U256([0x3ef8_7550_1dce_eb99, 0x1, 0, 0]),
435		// Corrections: 1, 0, 0.
436		U256([0xce1e_8ac2_290c_265f, 0x1, 0, 0]),
437		U256([0xd736_05ee_66e1_a76b, 0x1, 0, 0]),
438		// Corrections: 1, 0, 1.
439		U256([0xeb2b_fb00_8fa9_e0f7, 0x1, 0, 0]),
440		SMOD_FIXED_DIVISOR,
441		// Corrections: 1, 1, 0.
442		U256([0x500c_237e_4578_d88b, 0x1, 0, 0]),
443		U256([0x0164_a34e_0b1c_a4fd, 0x1, 0, 0]),
444		// Corrections: 1, 1, 1.
445		MOD_FIXED_DIVISOR,
446		DIV_FIXED_DIVISOR,
447		// Corrections: 0, 0, 2.
448		U256([0x3db2_354e_f8e8_6ca5, 0x96aa_3b25, 0, 0]),
449		U256([0x2848_08be_ea22_56a3, 0x9339_ddd9, 0, 0]),
450		// Corrections: 0, 1, 2.
451		U256([0x2534_a796_ffdf_b85e, 0xc250_8262, 0, 0]),
452		U256([0x4726_e939_da3c_08fe, 0xc198_a4ce, 0, 0]),
453		// Corrections: 0, 2, 0.
454		U256([0x5d5b_2f53_f03d_bec8, 0x909d_ad3b, 0, 0]),
455		U256([0x8e31_4add_736d_ee08, 0x40ce_7f0c, 0, 0]),
456		// Corrections: 0, 2, 1.
457		U256([0x0c62_030f_fabf_0fc7, 0xb51d_e6cb, 0, 0]),
458		U256([0x0332_58df_e522_494c, 0xb4db_915d, 0, 0]),
459		// Corrections: 0, 2, 2.
460		U256([0x0b07_4c7e_fb00_0c1f, 0xa5b9_8f9a, 0, 0]),
461		U256([0x26a5_a3c7_fce7_abd6, 0xa4a5_d36a, 0, 0]),
462		// Corrections: 1, 1, 2.
463		U256([0x6654_d104_1c8e_6943, 0x2, 0, 0]),
464		U256([0x64b6_7f9c_0cf1_dbff, 0x2, 0, 0]),
465		// Corrections: 1, 2, 0.
466		U256([0xeff9_3d22_eeac_89cf, 0x87, 0, 0]),
467		U256([0xadb2_0f65_e0ba_c8df, 0x12, 0, 0]),
468		// Corrections: 1, 2, 1.
469		U256([0xf486_a407_463b_dfde, 0x253, 0, 0]),
470		U256([0xaea8_360c_3043_f5f9, 0x1b4, 0, 0]),
471		// Corrections: 1, 2, 2.
472		U256([0xecbe_4a28_bb14_dbf0, 0x43, 0, 0]),
473		U256([0x64e2_acbc_9934_b1f2, 0x43, 0, 0]),
474	];
475}
476
477/// Operands covering all eight correction patterns in the one-limb division path.
478///
479/// `div_mod_small` divides one numerator limb at a time. The highest limb cannot require a
480/// correction. Each remaining limb can cause one correction in the 128-bit division helper. The
481/// patterns below are ordered by numerator limbs 2, 1, 0. The counts were checked with a
482/// source-level trace of Rust 1.93 `compiler_builtins` using the shared `2^255` numerator.
483///
484/// Each pattern has two distinct full-width divisors. Selecting a divisor pseudo-randomly varies
485/// the correction branches while preserving the signed minimum-numerator case.
486mod one_limb_division_operands {
487	use super::U256;
488
489	/// Two distinct divisors per pattern, with correction counts listed in limb order 2, 1, 0.
490	pub const DENOMINATORS: [U256; 16] = [
491		// Corrections: 0, 0, 0.
492		U256([0xfff9_9928_4b39_14e9, 0, 0, 0]),
493		U256([0xfff7_2ef6_07d7_0402, 0, 0, 0]),
494		// Corrections: 0, 0, 1.
495		U256([0xfffc_adff_2ef2_7bf4, 0, 0, 0]),
496		U256([0xffc7_1287_6d60_c1f0, 0, 0, 0]),
497		// Corrections: 0, 1, 0.
498		U256([0xfff8_74b0_648f_da8a, 0, 0, 0]),
499		U256([0xfff7_1d06_39ab_3877, 0, 0, 0]),
500		// Corrections: 0, 1, 1.
501		U256([0xf974_5f46_74c9_5adf, 0, 0, 0]),
502		U256([0xf7e1_3204_7bd0_15af, 0, 0, 0]),
503		// Corrections: 1, 0, 0.
504		U256([0xfff7_ac60_75f1_501f, 0, 0, 0]),
505		U256([0xffe8_555d_5f54_a346, 0, 0, 0]),
506		// Corrections: 1, 0, 1.
507		U256([0xfe7a_ca58_4afc_ed70, 0, 0, 0]),
508		U256([0xfd2b_4905_64bd_9797, 0, 0, 0]),
509		// Corrections: 1, 1, 0.
510		U256([0xfcd0_0e3d_ccbd_7da4, 0, 0, 0]),
511		U256([0xee06_4d5d_bfab_b524, 0, 0, 0]),
512		// Corrections: 1, 1, 1.
513		U256([0xf42a_21ef_e080_fc89, 0, 0, 0]),
514		U256([0xf095_aea7_fc96_fbde, 0, 0, 0]),
515	];
516}
517
518/// Operands covering the seven patterns we found when dividing `2^255` by three-limb divisors.
519///
520/// Knuth's Algorithm D estimates two quotient limbs for these divisors. We found the higher limb
521/// corrected zero or one time and the lower limb up to twice. Unlike with two-limb divisors, an
522/// estimate can still be one too large after its corrections, and the division then adds the
523/// divisor back after subtracting. We only found that on the higher limb without corrections. The
524/// counts were checked with a source-level trace of `uint` 0.10 and Rust 1.93
525/// `compiler_builtins`.
526///
527/// Each pattern has two distinct divisors. Each denominator also makes the `u128` divisions behind
528/// the estimates take as many steps as any we found with its pattern. We only found the lower limb
529/// corrected twice without the higher one with a highest denominator limb above one, which shortens
530/// the division behind the first estimate.
531mod three_limb_division_operands {
532	use super::U256;
533
534	/// Two distinct divisors per pattern, with correction counts listed in limb order 1, 0.
535	pub const DENOMINATORS: [U256; 14] = [
536		// Corrections: 0, 0.
537		U256([0x5953_c831_477d_af4d, 0xb3aa_ba8e_016a_4a5b, 0x1, 0]),
538		U256([0xc435_0788_b8fa_4738, 0xfce4_f4ce_07f9_5693, 0x1, 0]),
539		// Corrections: 0, 1.
540		U256([0x5bea_4bdb_1b8f_828a, 0x5bee_2b88_29a3_5779, 0x1, 0]),
541		U256([0x113a_2875_4d21_7cac, 0x5915_1f88_0b93_b2e5, 0x1, 0]),
542		// Corrections: 0, 2.
543		U256([0x58d3_080e_a261_0b2c, 0x06c1_6099_c37a_fb70, 0x25d, 0]),
544		U256([0x5648_021c_ce54_ef66, 0xf709_e780_1fa7_e46e, 0x0103_59f5, 0]),
545		// Corrections: 1, 0.
546		U256([0x64c3_be1d_246d_7821, 0x00f5_75ee_0862_2f50, 0x1, 0]),
547		U256([0x5239_dfd9_4c93_12cc, 0x4de1_94ac_3a49_1826, 0x1, 0]),
548		// Corrections: 1, 1.
549		U256([0xc9e1_b256_93a4_a2cc, 0xedb3_e9e8_d16e_a87f, 0x1, 0]),
550		U256([0x5ebe_d4f9_e6f8_b9e8, 0xe261_7082_463f_8395, 0x1, 0]),
551		// Corrections: 1, 2.
552		U256([0xd0ad_3019_7337_1a13, 0x0a1b_d7f0_0fa5_0c21, 0x1, 0]),
553		U256([0xd4c3_a565_664e_aa66, 0x0ca2_823c_292b_9317, 0x1, 0]),
554		// Corrections: 0, 0, with the divisor added back to the higher limb.
555		U256([0x0e78_2bf3_fa0e_13ab, 0x386d_d8b2_091a_6756, 0x1, 0]),
556		U256([0x0c44_ac2c_7ddc_8ca9, 0x0280_e278_312b_2362, 0x1, 0]),
557	];
558}
559
560/// Randomly chooses between the Knuth and one-limb correction-pattern operand sets.
561mod mixed_division_operands {
562	use super::{
563		Rng, U256, knuth_division_operands, one_limb_division_operands, signed_division_operands,
564		unsigned_division_operands,
565	};
566
567	pub fn unsigned_operands(rng: &mut impl Rng) -> [U256; 2] {
568		let denominators = denominators(rng);
569		unsigned_division_operands(rng, denominators)
570	}
571
572	pub fn signed_operands(rng: &mut impl Rng) -> [U256; 2] {
573		let denominators = denominators(rng);
574		signed_division_operands(rng, denominators)
575	}
576
577	fn denominators(rng: &mut impl Rng) -> &'static [U256] {
578		if rng.gen_bool(0.5) {
579			&knuth_division_operands::DENOMINATORS
580		} else {
581			&one_limb_division_operands::DENOMINATORS
582		}
583	}
584}
585
586/// # Subtraction Safety
587///
588/// Some EVM op-codes are charged as the weight of their benchmark minus the weight of another one:
589/// `JUMP` and `JUMPI` subtract `evm_jumpdest_opcode`, and the op-codes benchmarked in pairs with a
590/// `POP` subtract `evm_pop_opcode`. A benchmark is subtraction safe when subtracting it can never
591/// undercharge the op-code that remains.
592///
593/// A benchmark is subtraction safe when it underestimates its op-code: the op-code has no operands
594/// or branches that could make it more expensive, and the benchmark runs it in the cheapest
595/// setting, straight line code whose dispatch is always predicted. Subtracting an underestimate can
596/// only overcharge the op-code that remains.
597///
598/// A benchmark is not subtraction safe when it measures the worst case we have found for its
599/// op-code. The op-code can cost less inside another benchmark than in its own worst case, so
600/// subtracting that worst case can undercharge the op-code that remains.
601#[benchmarks(
602	where
603		T: Config,
604		<T as Config>::RuntimeCall: From<frame_system::Call<T>>,
605		<T as frame_system::Config>::Hash: frame_support::traits::IsType<H256>,
606		OriginFor<T>: From<Origin<T>>,
607)]
608mod benchmarks {
609	use super::*;
610
611	/// The base weight consumed on processing contracts deletion queue.
612	#[benchmark(pov_mode = Measured)]
613	fn deletion_queue_batch() {
614		#[block]
615		{
616			ContractInfo::<T>::process_deletion_queue_batch(&mut WeightMeter::new())
617		}
618	}
619
620	// Benchmark for processing N EIP-7702 authorizations with empty accounts
621	// This measures the overhead of processing the authorization list
622	// Parameter `n`: number of authorizations to process
623	#[benchmark(pov_mode = Measured)]
624	fn process_new_account_authorization(n: Linear<0, 255>) -> Result<(), BenchmarkError> {
625		use crate::evm::eip7702;
626		use sp_io::hashing::keccak_256;
627
628		let caller: T::AccountId = whitelisted_caller();
629		T::Currency::set_balance(&caller, caller_funding::<T>());
630		<T as Config>::FeeInfo::deposit_txfee(
631			<T as Config>::Currency::issue(caller_funding::<T>()),
632		);
633		let chain_id = U256::from(T::ChainId::get());
634		let exec_config = ExecConfig::new_eth_tx(U256::from(1), 0, Weight::MAX);
635
636		// Worst case: every authorization targets a *distinct* contract with *distinct* code
637		// so neither `AccountInfoOf<target>` nor `CodeInfoOf<code_hash>` reads can be cached
638		// across the loop. `dummy_unique(i)` produces a unique blob per index; the contract is
639		// deployed at a unique salt-index so addresses also differ.
640		let mut authorization_list = vec![];
641		for i in 0..n {
642			let target_contract =
643				Contract::<T>::with_index(i + 1, VmBinaryModule::dummy_unique(i), vec![])?;
644			let target = target_contract.address;
645
646			let key_material = keccak_256(&i.to_le_bytes());
647			let key = SigningKey::from_bytes(&key_material.into()).expect("valid key; qed");
648			let signed_auth = eip7702::sign_authorization(&key, chain_id, target, U256::zero());
649			authorization_list.push(signed_auth);
650		}
651
652		let auth_result;
653		#[block]
654		{
655			auth_result =
656				eip7702::process_authorizations::<T>(&authorization_list, &caller, &exec_config);
657		}
658
659		assert_eq!(auth_result.new_accounts, n as u32, "All authorizations should be new");
660		Ok(())
661	}
662
663	// Benchmark for processing N EIP-7702 authorizations with existing accounts.
664	//
665	// Worst case: each authority is *already* delegated to a unique target whose code is
666	// referenced only by that delegation, with the deposit paid by an account other than the
667	// bench's `caller`. Re-delegating to a fresh target then exercises the most expensive paths
668	// per auth:
669	//   - payer-change: full refund to `setup_payer` + full charge from `caller`
670	//   - old-code refcount → 0: `decrement_refcount` removes `CodeInfoOf` + `PristineCode`
671	//   - new-code refcount 0 → 1: `increment_refcount`
672	//
673	// To produce `refcount(old_code) == 1` going into the bench, we directly decrement the
674	// deployment's contribution after setup. This is equivalent to having terminated the old
675	// target (which would also drop the deployment's ref) — `#[block]` only reads the
676	// delegation snapshot's `code_hash`, never `AccountInfoOf[old_target]`, so it doesn't
677	// matter that the latter is still present.
678	//
679	// Parameter `n`: number of authorizations to process
680	#[benchmark(pov_mode = Measured)]
681	fn process_existing_account_authorization(n: Linear<0, 255>) -> Result<(), BenchmarkError> {
682		use crate::evm::eip7702;
683		use sp_io::hashing::keccak_256;
684
685		let caller: T::AccountId = whitelisted_caller();
686		T::Currency::set_balance(&caller, caller_funding::<T>());
687		<T as Config>::FeeInfo::deposit_txfee(
688			<T as Config>::Currency::issue(caller_funding::<T>()),
689		);
690
691		// Distinct payer (≠ `caller`) so the bench takes the payer-change branch in
692		// `process_authorizations` (full refund + full charge per auth) rather than the
693		// same-payer net-diff fast path.
694		let setup_payer: T::AccountId = account("setup_payer", 0, 0);
695		T::Currency::set_balance(&setup_payer, caller_funding::<T>());
696		<T as Config>::FeeInfo::deposit_txfee(
697			<T as Config>::Currency::issue(caller_funding::<T>()),
698		);
699
700		let chain_id = U256::from(T::ChainId::get());
701		let exec_config = ExecConfig::new_eth_tx(U256::from(1), 0, Weight::MAX);
702
703		let mut authorization_list = vec![];
704		for i in 0..n {
705			// Old delegation target with unique code (so each gets its own `CodeInfoOf` entry).
706			let old_target =
707				Contract::<T>::with_index(2 * i + 1, VmBinaryModule::dummy_unique(2 * i), vec![])?;
708			let old_code_hash = <AccountInfoOf<T>>::get(&old_target.address)
709				.and_then(|info| match info.account_type {
710					AccountType::Contract(c) => Some(c.code_hash),
711					_ => None,
712				})
713				.ok_or("old_target should be a Contract")?;
714
715			// Pre-existing delegation paid by `setup_payer`. Bumps the authority's nonce to 1
716			// and brings `refcount(old_code)` to 2 (deployment + delegation snapshot).
717			let key_material = keccak_256(&i.to_le_bytes());
718			let key = SigningKey::from_bytes(&key_material.into()).expect("valid key; qed");
719			let setup_auth =
720				eip7702::sign_authorization(&key, chain_id, old_target.address, U256::zero());
721			let _ = eip7702::process_authorizations::<T>(&[setup_auth], &setup_payer, &exec_config);
722
723			// Drop the deployment's ref so the delegation snapshot is the sole holder. This
724			// mirrors the post-termination storage state without spending setup time on real
725			// contract calls (the bench measures `#[block]`, not setup).
726			let _ =
727				CodeInfo::<T>::decrement_refcount(old_code_hash).map_err(|_| "decrement failed")?;
728
729			// New target (also unique code) that the authority re-delegates to.
730			let new_target = Contract::<T>::with_index(
731				2 * i + 2,
732				VmBinaryModule::dummy_unique(2 * i + 1),
733				vec![],
734			)?;
735
736			// Authority's nonce is 1 after setup, so the re-delegation auth signs with nonce=1.
737			let signed_auth =
738				eip7702::sign_authorization(&key, chain_id, new_target.address, U256::one());
739			authorization_list.push(signed_auth);
740		}
741
742		let auth_result;
743		#[block]
744		{
745			auth_result =
746				eip7702::process_authorizations::<T>(&authorization_list, &caller, &exec_config);
747		}
748
749		assert_eq!(auth_result.new_accounts, 0u32);
750		assert_eq!(auth_result.existing_accounts, n as u32);
751		Ok(())
752	}
753
754	// Measures the per-tuple cost of an authorization that runs through chain_id check
755	// and ecdsa_recover but then fails validation (here: nonce mismatch) — captures the
756	// sig-recovery cost without any account creation/update work.
757	#[benchmark(pov_mode = Measured)]
758	fn process_invalid_authorization(n: Linear<0, 255>) -> Result<(), BenchmarkError> {
759		use crate::evm::eip7702;
760		use sp_io::hashing::keccak_256;
761
762		let chain_id = U256::from(T::ChainId::get());
763		let target_contract = Contract::<T>::with_index(0, VmBinaryModule::dummy(), vec![])?;
764		let target = target_contract.address;
765		let caller: T::AccountId = whitelisted_caller();
766		T::Currency::set_balance(&caller, caller_funding::<T>());
767		<T as Config>::FeeInfo::deposit_txfee(
768			<T as Config>::Currency::issue(caller_funding::<T>()),
769		);
770		let exec_config = ExecConfig::new_eth_tx(U256::from(1), 0, Weight::MAX);
771
772		let mut authorization_list = vec![];
773		for i in 0..n {
774			let key_material = keccak_256(&(i as u32).to_le_bytes());
775			let key = SigningKey::from_bytes(&key_material.into()).expect("valid key; qed");
776			// Force nonce mismatch: signer's nonce is 0, but we sign nonce=1.
777			let signed_auth = eip7702::sign_authorization(&key, chain_id, target, U256::one());
778			authorization_list.push(signed_auth);
779		}
780
781		let auth_result;
782		#[block]
783		{
784			auth_result =
785				eip7702::process_authorizations::<T>(&authorization_list, &caller, &exec_config);
786		}
787
788		assert_eq!(auth_result.new_accounts, 0u32);
789		assert_eq!(auth_result.existing_accounts, 0u32);
790		Ok(())
791	}
792
793	/// Measures the per-entry cost of `process_deletion_queue_batch`.
794	///
795	/// This is one `DeletionQueue` read, the `AccountInfoOf` read that checks for a live contract
796	/// at the address, plus the `DeletionQueue` + `DeletionQueueCounter` writes done by
797	/// `entry.remove()`.
798	///
799	/// The `AccountInfoOf` read is charged at its max encoded length, so the proof covers a
800	/// live contract's entry as well as the plain account entry this benchmark reads.
801	#[benchmark(pov_mode = Measured {
802		Revive::AccountInfoOf: MaxEncodedLen,
803	})]
804	fn deletion_queue_per_entry() -> Result<(), BenchmarkError> {
805		let instance = Contract::<T>::with_storage(VmBinaryModule::dummy(), 0, 0)?;
806		queue_terminated_for_deletion(&instance)?;
807
808		#[block]
809		{
810			ContractInfo::<T>::process_deletion_queue_batch(&mut WeightMeter::new())
811		}
812
813		assert!(<DeletionQueue<T>>::iter().next().is_none(), "deletion queue should be drained",);
814		Ok(())
815	}
816
817	#[benchmark(skip_meta, pov_mode = Measured)]
818	fn deletion_queue_per_trie_key(k: Linear<0, 1024>) -> Result<(), BenchmarkError> {
819		let instance =
820			Contract::<T>::with_storage(VmBinaryModule::dummy(), k, limits::STORAGE_BYTES)?;
821		queue_terminated_for_deletion(&instance)?;
822
823		#[block]
824		{
825			ContractInfo::<T>::process_deletion_queue_batch(&mut WeightMeter::new())
826		}
827
828		assert!(<DeletionQueue<T>>::iter().next().is_none(), "deletion queue should be drained",);
829		Ok(())
830	}
831
832	/// Measures the cost of clearing one [`NativeDepositOf`] row during
833	/// [`ContractInfo::process_deletion_queue_batch`].
834	///
835	/// Pre-populates the contract with `k` per-payer rows and queues it for deletion as a
836	/// terminated contract with an empty trie. The deletion queue then drains all rows in one go.
837	#[benchmark(skip_meta, pov_mode = Measured)]
838	fn deletion_queue_per_native_deposit_key(k: Linear<0, 1024>) -> Result<(), BenchmarkError> {
839		use frame_benchmarking::v2::account;
840
841		// Empty trie: zero items, zero bytes; we only want to measure native-deposit cleanup.
842		let instance = Contract::<T>::with_storage(VmBinaryModule::dummy(), 0, 0)?;
843		for i in 0..k {
844			let payer: T::AccountId = account("payer", i, 0);
845			NativeDepositOf::<T>::insert(&instance.account_id, &payer, BalanceOf::<T>::default());
846		}
847		queue_terminated_for_deletion(&instance)?;
848
849		#[block]
850		{
851			ContractInfo::<T>::process_deletion_queue_batch(&mut WeightMeter::new())
852		}
853
854		assert!(<DeletionQueue<T>>::iter().next().is_none(), "deletion queue should be drained",);
855		assert!(
856			NativeDepositOf::<T>::iter_prefix(&instance.account_id).next().is_none(),
857			"native deposit rows should be cleared",
858		);
859		Ok(())
860	}
861
862	/// Measures loading PVM code of `c` bytes from storage into the execution engine.
863	///
864	/// The slope per byte of the code blob is charged to the caller for loading PVM code. It
865	/// includes the callee's first allocation of its map of compiled blocks, which the
866	/// interpreter charges as gas against a credit of the same size (see
867	/// `ContractBlob::prepare_call`). The entry block of `VmBinaryModule::sized(c)` grows with `c`
868	/// for small `c`, so part of its compilation is in the slope, and the interpreter charges it
869	/// as well. The called code returns right away; its execution is charged by the gas meter.
870	///
871	/// The proof size is the main result of this benchmark.
872	#[benchmark(pov_mode = Measured)]
873	fn call_with_pvm_code_per_byte(c: Linear<0, { 100 * 1024 }>) -> Result<(), BenchmarkError> {
874		let instance =
875			Contract::<T>::with_caller(whitelisted_caller(), VmBinaryModule::sized(c), vec![])?;
876		let value = Pallet::<T>::min_balance();
877		let storage_deposit = default_deposit_limit::<T>();
878
879		#[extrinsic_call]
880		call(
881			RawOrigin::Signed(instance.caller.clone()),
882			instance.address,
883			value,
884			Weight::MAX,
885			storage_deposit,
886			vec![],
887		);
888
889		Ok(())
890	}
891
892	// This benchmarks the overhead of loading a code of size `c` byte from storage and into
893	// the execution engine.
894	/// This is similar to `call_with_pvm_code_per_byte` but for EVM bytecode.
895	#[benchmark(pov_mode = Measured)]
896	fn call_with_evm_code_per_byte(
897		c: Linear<1, { MAX_CODE_SIZE as u32 }>,
898	) -> Result<(), BenchmarkError> {
899		let instance = Contract::<T>::with_caller(
900			whitelisted_caller(),
901			VmBinaryModule::evm_init_code_for_runtime_size(c),
902			vec![],
903		)?;
904		let value = Pallet::<T>::min_balance();
905		let storage_deposit = default_deposit_limit::<T>();
906
907		let code_len = PristineCode::<T>::get(instance.info()?.code_hash)
908			.expect("code should be stored")
909			.len();
910		assert_eq!(
911			code_len, c as usize,
912			"runtime bytecode should be exactly {c} bytes, got {code_len}"
913		);
914
915		#[extrinsic_call]
916		call(
917			RawOrigin::Signed(instance.caller.clone()),
918			instance.address,
919			value,
920			Weight::MAX,
921			storage_deposit,
922			vec![],
923		);
924
925		Ok(())
926	}
927
928	/// Measures the time the interpreter needs to compile one instruction of a basic block.
929	///
930	/// The interpreter compiles a basic block when execution first enters it. The called
931	/// contract stops at the first instruction of its entry block, which has `n` instructions
932	/// (see [`VmBinaryModule::with_entry_block`]). The code has the same size for every `n`, so
933	/// the call reads the same storage items of the same size and the slope is the time to
934	/// compile one more instruction of the entry block, including its gas cost calculation.
935	///
936	/// Only the ref time slope is used, for the gas the interpreter charges per compiled
937	/// instruction (see [`crate::metering::EngineMeter::interpreter_compilation_cost`]). Loading
938	/// the code is charged by `call_with_pvm_code_per_byte`.
939	#[benchmark(pov_mode = Measured)]
940	fn interpreter_compile_per_instruction(
941		n: Linear<2, { limits::code::BASIC_BLOCK_SIZE }>,
942	) -> Result<(), BenchmarkError> {
943		let instance = Contract::<T>::with_caller(
944			whitelisted_caller(),
945			VmBinaryModule::with_entry_block(n),
946			vec![],
947		)?;
948		let value = Pallet::<T>::min_balance();
949		let storage_deposit = default_deposit_limit::<T>();
950
951		#[block]
952		{
953			Pallet::<T>::call(
954				RawOrigin::Signed(instance.caller.clone()).into(),
955				instance.address,
956				value,
957				Weight::MAX,
958				storage_deposit,
959				vec![],
960			)?;
961		}
962
963		Ok(())
964	}
965
966	/// Measures the time the interpreter needs to compile and enter a basic block of one
967	/// instruction.
968	///
969	/// The called contract runs through `b` blocks that each hold one taken branch before it
970	/// stops (see [`VmBinaryModule::with_branch_blocks`]). The code has the same size for every
971	/// `b`. The slope is the time to compile one block of one instruction and to execute that
972	/// instruction. The per block cost of compilation is this slope minus the slope of
973	/// `interpreter_compile_per_instruction` minus one gas of execution.
974	///
975	/// Only the ref time slope is used, for the gas the interpreter charges per compiled block
976	/// (see [`crate::metering::EngineMeter::interpreter_compilation_cost`]). Loading the code is
977	/// charged by `call_with_pvm_code_per_byte`.
978	#[benchmark(pov_mode = Measured)]
979	fn interpreter_compile_per_block(
980		b: Linear<0, { VmBinaryModule::BRANCH_BLOCKS }>,
981	) -> Result<(), BenchmarkError> {
982		let instance = Contract::<T>::with_caller(
983			whitelisted_caller(),
984			VmBinaryModule::with_branch_blocks(b),
985			vec![],
986		)?;
987		let value = Pallet::<T>::min_balance();
988		let storage_deposit = default_deposit_limit::<T>();
989
990		#[block]
991		{
992			Pallet::<T>::call(
993				RawOrigin::Signed(instance.caller.clone()).into(),
994				instance.address,
995				value,
996				Weight::MAX,
997				storage_deposit,
998				vec![],
999			)?;
1000		}
1001
1002		Ok(())
1003	}
1004
1005	/// Measures allocating the interpreter's map of compiled blocks per byte of code section.
1006	///
1007	/// The map has one entry for every byte of the code section. The measured block resets the
1008	/// interpreter cache of a freshly prepared instance, which has no map yet, and runs it. The
1009	/// first compilation allocates the map for the `c` bytes of the code section and charges it.
1010	/// Every later reset, such as the one when a PVM frame starts during a call or instantiation
1011	/// the contract makes, forces the same allocation again. The entry block of two instructions is
1012	/// the same for every `c` (see [`VmBinaryModule::with_code_bytes`]) and is in the intercept,
1013	/// so the slope is the code map per byte of code.
1014	///
1015	/// The interpreter charges the slope per byte of code section whenever it allocates or
1016	/// rewrites the map, see [`crate::metering::EngineMeter::interpreter_compilation_cost`].
1017	#[benchmark(pov_mode = Ignored)]
1018	fn interpreter_code_map_per_byte(c: Linear<0, { VmBinaryModule::MAX_CODE_BYTES }>) {
1019		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_code_bytes(c));
1020		let (mut ext, module) = setup.ext();
1021		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
1022
1023		#[block]
1024		{
1025			prepared.reset_interpreter_cache().unwrap();
1026			prepared.call().unwrap();
1027		}
1028	}
1029
1030	// `c`: Size of the code in bytes.
1031	// `i`: Size of the input in bytes.
1032	#[benchmark(pov_mode = Measured)]
1033	fn instantiate_with_code(
1034		c: Linear<0, { 100 * 1024 }>,
1035		i: Linear<0, { limits::CALLDATA_BYTES }>,
1036	) {
1037		let pallet_account = whitelisted_pallet_account::<T>();
1038		let input = vec![42u8; i as usize];
1039		let salt = [42u8; 32];
1040		let value = Pallet::<T>::min_balance();
1041		let caller = whitelisted_caller();
1042		T::Currency::set_balance(&caller, caller_funding::<T>());
1043		let VmBinaryModule { code, .. } = VmBinaryModule::sized(c);
1044		let origin = RawOrigin::Signed(caller.clone());
1045		if !T::AddressMapper::is_mapped(&caller) {
1046			T::AddressMapper::map(&caller).unwrap();
1047		}
1048		let deployer = T::AddressMapper::to_address(&caller);
1049		let addr = crate::address::create2(&deployer, &code, &input, &salt);
1050		let account_id = T::AddressMapper::to_fallback_account_id(&addr);
1051		let storage_deposit = default_deposit_limit::<T>();
1052		#[extrinsic_call]
1053		_(origin, value, Weight::MAX, storage_deposit, code, input, Some(salt));
1054
1055		let deposit =
1056			T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &account_id);
1057		// uploading the code reserves some balance in the pallet's account
1058		let code_deposit = T::Currency::balance_on_hold(
1059			&HoldReason::CodeUploadDepositReserve.into(),
1060			&pallet_account,
1061		);
1062		let mapping_deposit =
1063			T::Currency::balance_on_hold(&HoldReason::AddressMapping.into(), &caller);
1064		assert_eq!(
1065			T::Currency::balance(&caller),
1066			caller_funding::<T>() - value - deposit - code_deposit - mapping_deposit,
1067		);
1068		// contract has the full value
1069		assert_eq!(T::Currency::balance(&account_id), value + Pallet::<T>::min_balance());
1070	}
1071
1072	// `c`: Size of the code in bytes.
1073	// `i`: Size of the input in bytes.
1074	// `d`: with or without dust value to transfer
1075	#[benchmark(pov_mode = Measured)]
1076	fn eth_instantiate_with_code(
1077		c: Linear<0, { 100 * 1024 }>,
1078		i: Linear<0, { limits::CALLDATA_BYTES }>,
1079		d: Linear<0, 1>,
1080	) -> Result<(), BenchmarkError> {
1081		let input = vec![42u8; i as usize];
1082
1083		// Use an `effective_gas_price` that is not a multiple of `T::NativeToEthRatio`
1084		// to hit the code that charge the rounding error so that tx_cost == effective_gas_price *
1085		// gas_used
1086		let effective_gas_price = Pallet::<T>::evm_base_fee() + 1;
1087		let value = Pallet::<T>::min_balance();
1088		let dust = 42u32 * d;
1089		let evm_value =
1090			Pallet::<T>::convert_native_to_evm(BalanceWithDust::new_unchecked::<T>(value, dust));
1091		let caller = whitelisted_caller();
1092		T::Currency::set_balance(&caller, caller_funding::<T>());
1093		let VmBinaryModule { code, .. } = VmBinaryModule::sized(c);
1094		let origin = Origin::EthTransaction(caller.clone());
1095		if !T::AddressMapper::is_mapped(&caller) {
1096			T::AddressMapper::map(&caller).unwrap();
1097		}
1098		let deployer = T::AddressMapper::to_address(&caller);
1099		let nonce = System::<T>::account_nonce(&caller).try_into().unwrap_or_default();
1100		let addr = crate::address::create1(&deployer, nonce);
1101
1102		assert!(AccountInfoOf::<T>::get(&deployer).is_none());
1103
1104		<T as Config>::FeeInfo::deposit_txfee(
1105			<T as Config>::Currency::issue(caller_funding::<T>()),
1106		);
1107
1108		#[extrinsic_call]
1109		_(
1110			origin,
1111			evm_value,
1112			Weight::MAX,
1113			U256::MAX,
1114			code,
1115			input,
1116			TransactionSigned::default().signed_payload(),
1117			effective_gas_price,
1118			0,
1119		);
1120
1121		// contract has the full value
1122		assert_eq!(Pallet::<T>::evm_balance(&addr), evm_value);
1123		Ok(())
1124	}
1125
1126	#[benchmark(pov_mode = Measured)]
1127	fn deposit_eth_extrinsic_revert_event() {
1128		#[block]
1129		{
1130			Pallet::<T>::deposit_event(Event::<T>::EthExtrinsicRevert {
1131				dispatch_error: crate::Error::<T>::BenchmarkingError.into(),
1132			});
1133		}
1134	}
1135
1136	// `i`: Size of the input in bytes.
1137	// `s`: Size of e salt in bytes.
1138	#[benchmark(pov_mode = Measured)]
1139	fn instantiate(i: Linear<0, { limits::CALLDATA_BYTES }>) -> Result<(), BenchmarkError> {
1140		let pallet_account = whitelisted_pallet_account::<T>();
1141		let input = vec![42u8; i as usize];
1142		let salt = [42u8; 32];
1143		let value = Pallet::<T>::min_balance();
1144		let caller = whitelisted_caller();
1145		T::Currency::set_balance(&caller, caller_funding::<T>());
1146		let origin = RawOrigin::Signed(caller.clone());
1147		if !T::AddressMapper::is_mapped(&caller) {
1148			T::AddressMapper::map(&caller).unwrap();
1149		}
1150		let VmBinaryModule { code, .. } = VmBinaryModule::dummy();
1151		let storage_deposit = default_deposit_limit::<T>();
1152		let deployer = T::AddressMapper::to_address(&caller);
1153		let addr = crate::address::create2(&deployer, &code, &input, &salt);
1154		let hash = Contracts::<T>::bare_upload_code(origin.clone().into(), code, storage_deposit)?
1155			.code_hash;
1156		let account_id = T::AddressMapper::to_fallback_account_id(&addr);
1157
1158		#[extrinsic_call]
1159		_(origin, value, Weight::MAX, storage_deposit, hash, input, Some(salt));
1160
1161		let deposit =
1162			T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &account_id);
1163		let code_deposit = T::Currency::balance_on_hold(
1164			&HoldReason::CodeUploadDepositReserve.into(),
1165			&pallet_account,
1166		);
1167		let mapping_deposit =
1168			T::Currency::balance_on_hold(&HoldReason::AddressMapping.into(), &account_id);
1169		// value was removed from the caller
1170		assert_eq!(
1171			T::Currency::total_balance(&caller),
1172			caller_funding::<T>() - value - deposit - code_deposit - mapping_deposit,
1173		);
1174		// contract has the full value
1175		assert_eq!(T::Currency::balance(&account_id), value + Pallet::<T>::min_balance());
1176
1177		Ok(())
1178	}
1179
1180	// We just call a dummy contract to measure the overhead of the call extrinsic.
1181	// The size of the data has no influence on the costs of this extrinsic as long as the contract
1182	// won't call `seal_call_data_copy` in its constructor to copy the data to contract memory.
1183	// The dummy contract used here does not do this. The costs for the data copy is billed as
1184	// part of `seal_call_data_copy`. The costs for invoking a contract of a specific size are not
1185	// part of this benchmark because we cannot know the size of the contract when issuing a call
1186	// transaction. See `call_with_pvm_code_per_byte` for this.
1187	#[benchmark(pov_mode = Measured)]
1188	fn call() -> Result<(), BenchmarkError> {
1189		let pallet_account = whitelisted_pallet_account::<T>();
1190		let data = vec![42u8; 1024];
1191		let instance =
1192			Contract::<T>::with_caller(whitelisted_caller(), VmBinaryModule::dummy(), vec![])?;
1193		let value = Pallet::<T>::min_balance();
1194		let origin = RawOrigin::Signed(instance.caller.clone());
1195		let before = T::Currency::balance(&instance.account_id);
1196		let storage_deposit = default_deposit_limit::<T>();
1197		#[extrinsic_call]
1198		_(origin, instance.address, value, Weight::MAX, storage_deposit, data);
1199		let deposit = T::Currency::balance_on_hold(
1200			&HoldReason::StorageDepositReserve.into(),
1201			&instance.account_id,
1202		);
1203		let code_deposit = T::Currency::balance_on_hold(
1204			&HoldReason::CodeUploadDepositReserve.into(),
1205			&pallet_account,
1206		);
1207		let mapping_deposit =
1208			T::Currency::balance_on_hold(&HoldReason::AddressMapping.into(), &instance.caller);
1209		// value and value transferred via call should be removed from the caller
1210		assert_eq!(
1211			T::Currency::balance(&instance.caller),
1212			caller_funding::<T>() - value - deposit - code_deposit - mapping_deposit,
1213		);
1214		// contract should have received the value
1215		assert_eq!(T::Currency::balance(&instance.account_id), before + value);
1216		// contract should still exist
1217		instance.info()?;
1218
1219		Ok(())
1220	}
1221
1222	// `d`: with or without dust value to transfer
1223	#[benchmark(pov_mode = Measured)]
1224	fn eth_call(d: Linear<0, 1>) -> Result<(), BenchmarkError> {
1225		let data = vec![42u8; 1024];
1226		let instance =
1227			Contract::<T>::with_caller(whitelisted_caller(), VmBinaryModule::dummy(), vec![])?;
1228
1229		// Use an `effective_gas_price` that is not a multiple of `T::NativeToEthRatio`
1230		// to hit the code that charge the rounding error so that tx_cost == effective_gas_price *
1231		// gas_used
1232		let effective_gas_price = Pallet::<T>::evm_base_fee() + 1;
1233		let value = Pallet::<T>::min_balance();
1234		let dust = 42u32 * d;
1235		let evm_value =
1236			Pallet::<T>::convert_native_to_evm(BalanceWithDust::new_unchecked::<T>(value, dust));
1237
1238		// need to pass the overdraw check
1239		<T as Config>::FeeInfo::deposit_txfee(
1240			<T as Config>::Currency::issue(caller_funding::<T>()),
1241		);
1242
1243		let origin = Origin::EthTransaction(instance.caller.clone());
1244		let before = Pallet::<T>::evm_balance(&instance.address);
1245
1246		#[extrinsic_call]
1247		_(
1248			origin,
1249			instance.address,
1250			evm_value,
1251			Weight::MAX,
1252			U256::MAX,
1253			data,
1254			TransactionSigned::default().signed_payload(),
1255			effective_gas_price,
1256			0,
1257			vec![],
1258		);
1259
1260		// contract should have received the value
1261		assert_eq!(Pallet::<T>::evm_balance(&instance.address), before + evm_value);
1262		// contract should still exist
1263		instance.info()?;
1264
1265		Ok(())
1266	}
1267
1268	// `c`: Size of the RLP encoded Ethereum transaction in bytes.
1269	#[benchmark(pov_mode = Measured)]
1270	fn eth_substrate_call(c: Linear<0, { 100 * 1024 }>) -> Result<(), BenchmarkError> {
1271		let caller = whitelisted_caller();
1272		T::Currency::set_balance(&caller, caller_funding::<T>());
1273		let origin = Origin::EthTransaction(caller);
1274		let dispatchable = frame_system::Call::remark { remark: vec![] }.into();
1275		#[extrinsic_call]
1276		_(origin, Box::new(dispatchable), vec![42u8; c as usize]);
1277		Ok(())
1278	}
1279
1280	// This constructs a contract that is maximal expensive to instrument.
1281	// It creates a maximum number of metering blocks per byte.
1282	// `c`: Size of the code in bytes.
1283	#[benchmark(pov_mode = Measured)]
1284	fn upload_code(c: Linear<0, { 100 * 1024 }>) {
1285		let caller = whitelisted_caller();
1286		let pallet_account = whitelisted_pallet_account::<T>();
1287		T::Currency::set_balance(&caller, caller_funding::<T>());
1288		let VmBinaryModule { code, hash, .. } = VmBinaryModule::sized(c);
1289		let origin = RawOrigin::Signed(caller.clone());
1290		let storage_deposit = default_deposit_limit::<T>();
1291		#[extrinsic_call]
1292		_(origin, code, storage_deposit);
1293		// uploading the code reserves some balance in the pallet's account
1294		assert!(T::Currency::total_balance_on_hold(&pallet_account) > 0u32.into());
1295		assert!(<Contract<T>>::code_exists(&hash));
1296	}
1297
1298	// Removing code does not depend on the size of the contract because all the information
1299	// needed to verify the removal claim (refcount, owner) is stored in a separate storage
1300	// item (`CodeInfoOf`).
1301	#[benchmark(pov_mode = Measured)]
1302	fn remove_code() -> Result<(), BenchmarkError> {
1303		let caller = whitelisted_caller();
1304		let pallet_account = whitelisted_pallet_account::<T>();
1305		T::Currency::set_balance(&caller, caller_funding::<T>());
1306		let VmBinaryModule { code, hash, .. } = VmBinaryModule::dummy();
1307		let origin = RawOrigin::Signed(caller.clone());
1308		let storage_deposit = default_deposit_limit::<T>();
1309		let uploaded =
1310			<Contracts<T>>::bare_upload_code(origin.clone().into(), code, storage_deposit)?;
1311		assert_eq!(uploaded.code_hash, hash);
1312		assert_eq!(uploaded.deposit, T::Currency::total_balance_on_hold(&pallet_account));
1313		assert!(<Contract<T>>::code_exists(&hash));
1314		#[extrinsic_call]
1315		_(origin, hash);
1316		// removing the code should have unreserved the deposit
1317		assert_eq!(T::Currency::total_balance_on_hold(&pallet_account), 0u32.into());
1318		assert!(<Contract<T>>::code_removed(&hash));
1319		Ok(())
1320	}
1321
1322	#[benchmark(pov_mode = Measured)]
1323	fn set_code() -> Result<(), BenchmarkError> {
1324		let instance =
1325			<Contract<T>>::with_caller(whitelisted_caller(), VmBinaryModule::dummy(), vec![])?;
1326		// we just add some bytes so that the code hash is different
1327		let VmBinaryModule { code, .. } = VmBinaryModule::dummy_unique(128);
1328		let origin = RawOrigin::Signed(instance.caller.clone());
1329		let storage_deposit = default_deposit_limit::<T>();
1330		let hash =
1331			<Contracts<T>>::bare_upload_code(origin.into(), code, storage_deposit)?.code_hash;
1332		assert_ne!(instance.info()?.code_hash, hash);
1333		#[extrinsic_call]
1334		_(RawOrigin::Root, instance.address, hash);
1335		assert_eq!(instance.info()?.code_hash, hash);
1336		Ok(())
1337	}
1338
1339	#[benchmark(pov_mode = Measured)]
1340	fn map_account() {
1341		let caller = whitelisted_caller();
1342		T::Currency::set_balance(&caller, caller_funding::<T>());
1343		let origin = RawOrigin::Signed(caller.clone());
1344		if T::AddressMapper::is_mapped(&caller) {
1345			T::AddressMapper::unmap(&caller).unwrap();
1346		}
1347		assert!(!T::AddressMapper::is_mapped(&caller));
1348		#[extrinsic_call]
1349		_(origin);
1350		assert!(T::AddressMapper::is_mapped(&caller));
1351	}
1352
1353	#[benchmark(pov_mode = Measured)]
1354	fn unmap_account() {
1355		let caller = whitelisted_caller();
1356		T::Currency::set_balance(&caller, caller_funding::<T>());
1357		let origin = RawOrigin::Signed(caller.clone());
1358		if !T::AddressMapper::is_mapped(&caller) {
1359			T::AddressMapper::map(&caller).unwrap();
1360		}
1361		assert!(T::AddressMapper::is_mapped(&caller));
1362		#[extrinsic_call]
1363		_(origin);
1364		assert!(!T::AddressMapper::is_mapped(&caller));
1365	}
1366
1367	/// Worst case: every input account is not eth-derived, not yet mapped, and
1368	/// already carries an [`HoldReason::AddressMapping`] hold. The per-account
1369	/// loop body in `batch_map_accounts` then both inserts the [`OriginalAccount`]
1370	/// entry via `map_no_deposit_unchecked` *and* releases the existing hold.
1371	#[benchmark(pov_mode = Measured)]
1372	fn batch_map_accounts(a: Linear<0, 1024>) -> Result<(), BenchmarkError> {
1373		use frame_benchmarking::v2::account;
1374
1375		let caller: T::AccountId = whitelisted_caller();
1376		T::Currency::set_balance(&caller, caller_funding::<T>());
1377
1378		// Matches the deposit that `AccountId32Mapper::map` would normally take.
1379		let deposit = T::DepositPerByte::get()
1380			.saturating_mul(52u32.into())
1381			.saturating_add(T::DepositPerItem::get());
1382
1383		let mut accounts = Vec::with_capacity(a as usize);
1384		for i in 0..a {
1385			let account_id: T::AccountId = account("to_map", i, 0);
1386			T::Currency::set_balance(&account_id, caller_funding::<T>());
1387			T::Currency::hold(&HoldReason::AddressMapping.into(), &account_id, deposit)?;
1388			accounts.push(account_id);
1389		}
1390
1391		#[extrinsic_call]
1392		_(RawOrigin::Signed(caller), accounts.clone());
1393
1394		for account_id in &accounts {
1395			assert!(T::AddressMapper::is_mapped(account_id));
1396			assert_eq!(
1397				T::Currency::balance_on_hold(&HoldReason::AddressMapping.into(), account_id),
1398				0u32.into(),
1399			);
1400		}
1401
1402		Ok(())
1403	}
1404
1405	#[benchmark(pov_mode = Measured)]
1406	fn dispatch_as_fallback_account() {
1407		let caller = whitelisted_caller();
1408		T::Currency::set_balance(&caller, caller_funding::<T>());
1409		let origin = RawOrigin::Signed(caller.clone());
1410		let dispatchable = frame_system::Call::remark { remark: vec![] }.into();
1411		#[extrinsic_call]
1412		_(origin, Box::new(dispatchable));
1413	}
1414
1415	#[benchmark(pov_mode = Measured)]
1416	fn noop_host_fn(r: Linear<0, API_BENCHMARK_RUNS>) {
1417		let mut setup = CallSetup::<T>::new(VmBinaryModule::noop());
1418		let (mut ext, module) = setup.ext();
1419		let prepared = CallSetup::<T>::prepare_call(&mut ext, module, r.encode());
1420		#[block]
1421		{
1422			prepared.call().unwrap();
1423		}
1424	}
1425
1426	#[benchmark(pov_mode = Measured)]
1427	fn seal_caller() {
1428		let len = H160::len_bytes();
1429		build_runtime!(runtime, memory: [vec![0u8; len as _], ]);
1430
1431		let result;
1432		#[block]
1433		{
1434			result = runtime.bench_caller(memory.as_mut_slice(), 0);
1435		}
1436
1437		assert_ok!(result);
1438		assert_eq!(
1439			<H160 as Decode>::decode(&mut &memory[..]).unwrap(),
1440			T::AddressMapper::to_address(&runtime.ext().caller().account_id().unwrap())
1441		);
1442	}
1443
1444	#[benchmark(pov_mode = Measured)]
1445	fn seal_origin() {
1446		let len = H160::len_bytes();
1447		build_runtime!(runtime, memory: [vec![0u8; len as _], ]);
1448
1449		let result;
1450		#[block]
1451		{
1452			result = runtime.bench_origin(memory.as_mut_slice(), 0);
1453		}
1454
1455		assert_ok!(result);
1456		assert_eq!(
1457			<H160 as Decode>::decode(&mut &memory[..]).unwrap(),
1458			T::AddressMapper::to_address(&runtime.ext().origin().account_id().unwrap())
1459		);
1460	}
1461
1462	#[benchmark(pov_mode = Measured)]
1463	fn to_account_id() {
1464		// use a mapped address for the benchmark, to ensure that we bench the worst
1465		// case (and not the fallback case).
1466		let account_id = account("precompile_to_account_id", 0, 0);
1467		let address = {
1468			T::Currency::set_balance(&account_id, caller_funding::<T>());
1469			if !T::AddressMapper::is_mapped(&account_id) {
1470				T::AddressMapper::map(&account_id).unwrap();
1471			}
1472			T::AddressMapper::to_address(&account_id)
1473		};
1474
1475		let input_bytes = ISystem::ISystemCalls::toAccountId(ISystem::toAccountIdCall {
1476			input: address.0.into(),
1477		})
1478		.abi_encode();
1479
1480		let mut call_setup = CallSetup::<T>::default();
1481		let (mut ext, _) = call_setup.ext();
1482
1483		let result;
1484		#[block]
1485		{
1486			result = run_builtin_precompile(
1487				&mut ext,
1488				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1489				input_bytes,
1490			);
1491		}
1492		let raw_data = result.unwrap().data;
1493		let data = Bytes::abi_decode(&raw_data).expect("decoding failed");
1494		assert_ne!(
1495			data.0.as_ref()[20..32],
1496			[0xEE; 12],
1497			"fallback suffix found where none should be"
1498		);
1499		assert_eq!(T::AccountId::decode(&mut data.as_ref()), Ok(account_id),);
1500	}
1501
1502	#[benchmark(pov_mode = Measured)]
1503	fn seal_code_hash() {
1504		let contract = Contract::<T>::with_index(1, VmBinaryModule::dummy(), vec![]).unwrap();
1505		let len = <sp_core::H256 as MaxEncodedLen>::max_encoded_len() as u32;
1506		build_runtime!(runtime, memory: [vec![0u8; len as _], contract.account_id.encode(), ]);
1507
1508		let result;
1509		#[block]
1510		{
1511			result = runtime.bench_code_hash(memory.as_mut_slice(), len, 0);
1512		}
1513
1514		assert_ok!(result);
1515		assert_eq!(
1516			<sp_core::H256 as Decode>::decode(&mut &memory[..]).unwrap(),
1517			contract.info().unwrap().code_hash
1518		);
1519	}
1520
1521	#[benchmark(pov_mode = Measured)]
1522	fn own_code_hash() {
1523		let input_bytes =
1524			ISystem::ISystemCalls::ownCodeHash(ISystem::ownCodeHashCall {}).abi_encode();
1525		let mut call_setup = CallSetup::<T>::default();
1526		let contract_acc = call_setup.contract().account_id.clone();
1527		let caller = call_setup.contract().address;
1528		call_setup.set_origin(ExecOrigin::from_account_id(contract_acc));
1529		let (mut ext, _) = call_setup.ext();
1530
1531		let result;
1532		#[block]
1533		{
1534			result = run_builtin_precompile(
1535				&mut ext,
1536				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1537				input_bytes,
1538			);
1539		}
1540		assert!(result.is_ok());
1541		let caller_code_hash = ext.code_hash(&caller);
1542		assert_eq!(caller_code_hash.0.to_vec(), result.unwrap().data);
1543	}
1544
1545	#[benchmark(pov_mode = Measured)]
1546	fn seal_code_size() {
1547		let contract = Contract::<T>::with_index(1, VmBinaryModule::dummy(), vec![]).unwrap();
1548		build_runtime!(runtime, memory: [contract.address.encode(),]);
1549
1550		let result;
1551		#[block]
1552		{
1553			result = runtime.bench_code_size(memory.as_mut_slice(), 0);
1554		}
1555
1556		assert_eq!(result.unwrap(), VmBinaryModule::dummy().code.len() as u64);
1557	}
1558
1559	#[benchmark(pov_mode = Measured)]
1560	fn caller_is_origin() {
1561		let input_bytes =
1562			ISystem::ISystemCalls::callerIsOrigin(ISystem::callerIsOriginCall {}).abi_encode();
1563
1564		let mut call_setup = CallSetup::<T>::default();
1565		let (mut ext, _) = call_setup.ext();
1566
1567		let result;
1568		#[block]
1569		{
1570			result = run_builtin_precompile(
1571				&mut ext,
1572				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1573				input_bytes,
1574			);
1575		}
1576		let raw_data = result.unwrap().data;
1577		let is_origin = Bool::abi_decode(&raw_data[..]).expect("decoding failed");
1578		assert!(is_origin);
1579	}
1580
1581	#[benchmark(pov_mode = Measured)]
1582	fn caller_is_root() {
1583		let input_bytes =
1584			ISystem::ISystemCalls::callerIsRoot(ISystem::callerIsRootCall {}).abi_encode();
1585
1586		let mut setup = CallSetup::<T>::default();
1587		setup.set_origin(ExecOrigin::Root);
1588		let (mut ext, _) = setup.ext();
1589
1590		let result;
1591		#[block]
1592		{
1593			result = run_builtin_precompile(
1594				&mut ext,
1595				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1596				input_bytes,
1597			);
1598		}
1599		let raw_data = result.unwrap().data;
1600		let is_root = Bool::abi_decode(&raw_data).expect("decoding failed");
1601		assert!(is_root);
1602	}
1603
1604	#[benchmark(pov_mode = Measured)]
1605	fn origin_is_root() {
1606		let input_bytes =
1607			ISystem::ISystemCalls::originIsRoot(ISystem::originIsRootCall {}).abi_encode();
1608
1609		let mut setup = CallSetup::<T>::default();
1610		setup.set_origin(ExecOrigin::Root);
1611		let (mut ext, _) = setup.ext();
1612
1613		let result;
1614		#[block]
1615		{
1616			result = run_builtin_precompile(
1617				&mut ext,
1618				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1619				input_bytes,
1620			);
1621		}
1622		let raw_data = result.unwrap().data;
1623		let is_root = Bool::abi_decode(&raw_data).expect("decoding failed");
1624		assert!(is_root);
1625	}
1626
1627	#[benchmark(pov_mode = Measured)]
1628	fn seal_address() {
1629		let len = H160::len_bytes();
1630		build_runtime!(runtime, memory: [vec![0u8; len as _], ]);
1631
1632		let result;
1633		#[block]
1634		{
1635			result = runtime.bench_address(memory.as_mut_slice(), 0);
1636		}
1637		assert_ok!(result);
1638		assert_eq!(<H160 as Decode>::decode(&mut &memory[..]).unwrap(), runtime.ext().address());
1639	}
1640
1641	#[benchmark(pov_mode = Measured)]
1642	fn weight_left() {
1643		let input_bytes =
1644			ISystem::ISystemCalls::weightLeft(ISystem::weightLeftCall {}).abi_encode();
1645
1646		let mut call_setup = CallSetup::<T>::default();
1647		let (mut ext, _) = call_setup.ext();
1648
1649		let weight_left_before = ext.frame_meter().weight_left().unwrap();
1650		let result;
1651		#[block]
1652		{
1653			result = run_builtin_precompile(
1654				&mut ext,
1655				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1656				input_bytes,
1657			);
1658		}
1659		let weight_left_after = ext.frame_meter().weight_left().unwrap();
1660		assert_ne!(weight_left_after.ref_time(), 0);
1661		assert!(weight_left_before.ref_time() > weight_left_after.ref_time());
1662
1663		let raw_data = result.unwrap().data;
1664		type MyTy = (Uint<64>, Uint<64>);
1665		let foo = MyTy::abi_decode(&raw_data[..]).unwrap();
1666		assert_eq!(weight_left_after.ref_time(), foo.0);
1667	}
1668
1669	#[benchmark(pov_mode = Measured)]
1670	fn seal_ref_time_left() {
1671		build_runtime!(runtime, memory: [vec![], ]);
1672
1673		let result;
1674		#[block]
1675		{
1676			result = runtime.bench_ref_time_left(memory.as_mut_slice());
1677		}
1678		assert_eq!(result.unwrap(), runtime.ext().gas_left());
1679	}
1680
1681	#[benchmark(pov_mode = Measured)]
1682	fn seal_balance() {
1683		build_runtime!(runtime, contract, memory: [[0u8;32], ]);
1684		contract.set_balance(BalanceWithDust::new_unchecked::<T>(
1685			Pallet::<T>::min_balance() * 2u32.into(),
1686			42u32,
1687		));
1688		// A `transfer` or `send` receiver runs on a stipend too small for a storage read, and must
1689		// still be able to read its own balance. In a contract called without value by another
1690		// contract, the read is unpaid until #12673 removes this whitelist.
1691		let account_id = &contract.account_id;
1692		whitelist_account!(account_id);
1693
1694		let result;
1695		#[block]
1696		{
1697			result = runtime.bench_balance(memory.as_mut_slice(), 0);
1698		}
1699		assert_ok!(result);
1700		assert_eq!(
1701			U256::from_little_endian(&memory[..]),
1702			Pallet::<T>::convert_native_to_evm(BalanceWithDust::new_unchecked::<T>(
1703				Pallet::<T>::min_balance(),
1704				42
1705			))
1706		);
1707	}
1708
1709	#[benchmark(pov_mode = Measured)]
1710	fn seal_balance_of() {
1711		let len = <sp_core::U256 as MaxEncodedLen>::max_encoded_len();
1712		let account = account::<T::AccountId>("target", 0, 0);
1713		<T as Config>::AddressMapper::map_no_deposit_unchecked(&account).unwrap();
1714
1715		let address = T::AddressMapper::to_address(&account);
1716		let balance = Pallet::<T>::min_balance() * 2u32.into();
1717		T::Currency::set_balance(&account, balance);
1718		AccountInfoOf::<T>::insert(&address, AccountInfo { dust: 42, ..Default::default() });
1719
1720		build_runtime!(runtime, memory: [vec![0u8; len], address.0, ]);
1721
1722		let result;
1723		#[block]
1724		{
1725			result = runtime.bench_balance_of(memory.as_mut_slice(), len as u32, 0);
1726		}
1727
1728		assert_ok!(result);
1729		assert_eq!(
1730			U256::from_little_endian(&memory[..len]),
1731			Pallet::<T>::convert_native_to_evm(BalanceWithDust::new_unchecked::<T>(
1732				Pallet::<T>::min_balance(),
1733				42
1734			))
1735		);
1736	}
1737
1738	#[benchmark(pov_mode = Measured)]
1739	fn seal_get_immutable_data(n: Linear<1, { limits::IMMUTABLE_BYTES }>) {
1740		let len = n as usize;
1741		let immutable_data = vec![1u8; len];
1742
1743		build_runtime!(runtime, contract, memory: [(len as u32).encode(), vec![0u8; len],]);
1744
1745		<ImmutableDataOf<T>>::insert::<_, BoundedVec<_, _>>(
1746			contract.address,
1747			immutable_data.clone().try_into().unwrap(),
1748		);
1749
1750		let result;
1751		#[block]
1752		{
1753			result = runtime.bench_get_immutable_data(memory.as_mut_slice(), 4, 0 as u32);
1754		}
1755
1756		assert_ok!(result);
1757		assert_eq!(&memory[0..4], (len as u32).encode());
1758		assert_eq!(&memory[4..len + 4], &immutable_data);
1759	}
1760
1761	#[benchmark(pov_mode = Measured)]
1762	fn seal_set_immutable_data(n: Linear<1, { limits::IMMUTABLE_BYTES }>) {
1763		let len = n as usize;
1764		let mut memory = vec![1u8; len];
1765		let mut setup = CallSetup::<T>::default();
1766		let input = setup.data();
1767		let (mut ext, _) = setup.ext();
1768		ext.override_export(crate::exec::ExportedFunction::Constructor);
1769
1770		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, input);
1771
1772		let result;
1773		#[block]
1774		{
1775			result = runtime.bench_set_immutable_data(memory.as_mut_slice(), 0, n);
1776		}
1777
1778		assert_ok!(result);
1779		assert_eq!(&memory[..], &<ImmutableDataOf<T>>::get(setup.contract().address).unwrap()[..]);
1780	}
1781
1782	#[benchmark(pov_mode = Measured)]
1783	fn seal_value_transferred() {
1784		build_runtime!(runtime, memory: [[0u8;32], ]);
1785		let result;
1786		#[block]
1787		{
1788			result = runtime.bench_value_transferred(memory.as_mut_slice(), 0);
1789		}
1790		assert_ok!(result);
1791		assert_eq!(U256::from_little_endian(&memory[..]), runtime.ext().value_transferred());
1792	}
1793
1794	#[benchmark(pov_mode = Measured)]
1795	fn minimum_balance() {
1796		let input_bytes =
1797			ISystem::ISystemCalls::minimumBalance(ISystem::minimumBalanceCall {}).abi_encode();
1798
1799		let mut call_setup = CallSetup::<T>::default();
1800		let (mut ext, _) = call_setup.ext();
1801
1802		let result;
1803		#[block]
1804		{
1805			result = run_builtin_precompile(
1806				&mut ext,
1807				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
1808				input_bytes,
1809			);
1810		}
1811		let min: U256 = crate::Pallet::<T>::convert_native_to_evm(T::Currency::minimum_balance());
1812		let min =
1813			crate::precompiles::alloy::primitives::aliases::U256::abi_decode(&min.to_big_endian())
1814				.unwrap();
1815
1816		let raw_data = result.unwrap().data;
1817		let returned_min =
1818			crate::precompiles::alloy::primitives::aliases::U256::abi_decode(&raw_data)
1819				.expect("decoding failed");
1820		assert_eq!(returned_min, min);
1821	}
1822
1823	#[benchmark(pov_mode = Measured)]
1824	fn seal_return_data_size() {
1825		let mut setup = CallSetup::<T>::default();
1826		let (mut ext, _) = setup.ext();
1827		let mut runtime = pvm::Runtime::new(&mut ext, vec![]);
1828		let mut memory = memory!(vec![],);
1829		*runtime.ext().last_frame_output_mut() =
1830			ExecReturnValue { data: vec![42; 256], ..Default::default() };
1831		let result;
1832		#[block]
1833		{
1834			result = runtime.bench_return_data_size(memory.as_mut_slice());
1835		}
1836		assert_eq!(result.unwrap(), 256);
1837	}
1838
1839	#[benchmark(pov_mode = Measured)]
1840	fn seal_call_data_size() {
1841		let mut setup = CallSetup::<T>::default();
1842		let (mut ext, _) = setup.ext();
1843		let mut runtime = pvm::Runtime::new(&mut ext, vec![42u8; 128 as usize]);
1844		let mut memory = memory!(vec![0u8; 4],);
1845		let result;
1846		#[block]
1847		{
1848			result = runtime.bench_call_data_size(memory.as_mut_slice());
1849		}
1850		assert_eq!(result.unwrap(), 128);
1851	}
1852
1853	#[benchmark(pov_mode = Measured)]
1854	fn seal_gas_limit() {
1855		build_runtime!(runtime, memory: []);
1856		let result;
1857		#[block]
1858		{
1859			result = runtime.bench_gas_limit(&mut memory);
1860		}
1861		assert_eq!(U256::from(result.unwrap()), <Pallet<T>>::evm_block_gas_limit());
1862	}
1863
1864	#[benchmark(pov_mode = Measured)]
1865	fn seal_gas_price() {
1866		build_runtime!(runtime, memory: []);
1867		let result;
1868		#[block]
1869		{
1870			result = runtime.bench_gas_price(memory.as_mut_slice());
1871		}
1872		assert_eq!(U256::from(result.unwrap()), <Pallet<T>>::evm_base_fee());
1873	}
1874
1875	#[benchmark(pov_mode = Measured)]
1876	fn seal_base_fee() {
1877		build_runtime!(runtime, memory: [[1u8;32], ]);
1878		let result;
1879		#[block]
1880		{
1881			result = runtime.bench_base_fee(memory.as_mut_slice(), 0);
1882		}
1883		assert_ok!(result);
1884		assert_eq!(U256::from_little_endian(&memory[..]), <crate::Pallet<T>>::evm_base_fee());
1885	}
1886
1887	#[benchmark(pov_mode = Measured)]
1888	fn seal_block_number() {
1889		build_runtime!(runtime, memory: [[0u8;32], ]);
1890		let result;
1891		#[block]
1892		{
1893			result = runtime.bench_block_number(memory.as_mut_slice(), 0);
1894		}
1895		assert_ok!(result);
1896		assert_eq!(U256::from_little_endian(&memory[..]), runtime.ext().block_number());
1897	}
1898
1899	#[benchmark(pov_mode = Measured)]
1900	fn seal_block_author() {
1901		build_runtime!(runtime, memory: [[123u8; 20], ]);
1902
1903		// The pre-runtime digest log is unbounded; usually around 3 items but it can vary.
1904		// To get safe benchmark results despite that, populate it with a bunch of random logs to
1905		// ensure iteration over many items (we just overestimate the cost of the API).
1906		for i in 0..16 {
1907			frame_system::Pallet::<T>::deposit_log(DigestItem::PreRuntime(
1908				[i, i, i, i],
1909				vec![i; 128],
1910			));
1911			frame_system::Pallet::<T>::deposit_log(DigestItem::Consensus(
1912				[i, i, i, i],
1913				vec![i; 128],
1914			));
1915			frame_system::Pallet::<T>::deposit_log(DigestItem::Seal([i, i, i, i], vec![i; 128]));
1916			frame_system::Pallet::<T>::deposit_log(DigestItem::Other(vec![i; 128]));
1917		}
1918
1919		// The content of the pre-runtime digest log depends on the configured consensus.
1920		// However, mismatching logs are simply ignored. Thus we construct fixtures which will
1921		// let the API to return a value in both BABE and AURA consensus.
1922
1923		// Construct a `Digest` log fixture returning some value in BABE
1924		let primary_pre_digest = vec![0; <PrimaryPreDigest as MaxEncodedLen>::max_encoded_len()];
1925		let pre_digest =
1926			PreDigest::Primary(PrimaryPreDigest::decode(&mut &primary_pre_digest[..]).unwrap());
1927		frame_system::Pallet::<T>::deposit_log(DigestItem::PreRuntime(
1928			BABE_ENGINE_ID,
1929			pre_digest.encode(),
1930		));
1931		frame_system::Pallet::<T>::deposit_log(DigestItem::Seal(
1932			BABE_ENGINE_ID,
1933			pre_digest.encode(),
1934		));
1935
1936		// Construct a `Digest` log fixture returning some value in AURA
1937		let slot = Slot::default();
1938		frame_system::Pallet::<T>::deposit_log(DigestItem::PreRuntime(
1939			AURA_ENGINE_ID,
1940			slot.encode(),
1941		));
1942		frame_system::Pallet::<T>::deposit_log(DigestItem::Seal(AURA_ENGINE_ID, slot.encode()));
1943
1944		let result;
1945		#[block]
1946		{
1947			result = runtime.bench_block_author(memory.as_mut_slice(), 0);
1948		}
1949		assert_ok!(result);
1950
1951		let block_author = runtime.ext().block_author();
1952		assert_eq!(&memory[..], block_author.as_bytes());
1953	}
1954
1955	#[benchmark(pov_mode = Measured)]
1956	fn seal_block_hash() {
1957		let mut memory = vec![0u8; 64];
1958		let mut setup = CallSetup::<T>::default();
1959		let input = setup.data();
1960		let (mut ext, _) = setup.ext();
1961		ext.set_block_number(BlockNumberFor::<T>::from(1u32));
1962
1963		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, input);
1964
1965		let block_hash = H256::from([1; 32]);
1966
1967		// Store block hash in pallet-revive BlockHash mapping
1968		crate::BlockHash::<T>::insert(crate::BlockNumberFor::<T>::from(0u32), block_hash);
1969
1970		let result;
1971		#[block]
1972		{
1973			result = runtime.bench_block_hash(memory.as_mut_slice(), 32, 0);
1974		}
1975		assert_ok!(result);
1976		assert_eq!(&memory[..32], &block_hash.0);
1977	}
1978
1979	#[benchmark(pov_mode = Measured)]
1980	fn seal_now() {
1981		build_runtime!(runtime, memory: [[0u8;32], ]);
1982		let result;
1983		#[block]
1984		{
1985			result = runtime.bench_now(memory.as_mut_slice(), 0);
1986		}
1987		assert_ok!(result);
1988		assert_eq!(U256::from_little_endian(&memory[..]), runtime.ext().now());
1989	}
1990
1991	#[benchmark(pov_mode = Measured)]
1992	fn seal_copy_to_contract(n: Linear<0, { limits::code::BLOB_BYTES - 4 }>) {
1993		let mut setup = CallSetup::<T>::default();
1994		let (mut ext, _) = setup.ext();
1995		let mut runtime = pvm::Runtime::new(&mut ext, vec![]);
1996		let mut memory = memory!(n.encode(), vec![0u8; n as usize],);
1997		let result;
1998		#[block]
1999		{
2000			result = runtime.write_sandbox_output(
2001				memory.as_mut_slice(),
2002				4,
2003				0,
2004				&vec![42u8; n as usize],
2005				false,
2006				|_| None,
2007			);
2008		}
2009		assert_ok!(result);
2010		assert_eq!(&memory[..4], &n.encode());
2011		assert_eq!(&memory[4..], &vec![42u8; n as usize]);
2012	}
2013
2014	#[benchmark(pov_mode = Measured)]
2015	fn seal_call_data_load() {
2016		let mut setup = CallSetup::<T>::default();
2017		let (mut ext, _) = setup.ext();
2018		let mut runtime = pvm::Runtime::new(&mut ext, vec![42u8; 32]);
2019		let mut memory = memory!(vec![0u8; 32],);
2020		let result;
2021		#[block]
2022		{
2023			result = runtime.bench_call_data_load(memory.as_mut_slice(), 0, 0);
2024		}
2025		assert_ok!(result);
2026		assert_eq!(&memory[..], &vec![42u8; 32]);
2027	}
2028
2029	#[benchmark(pov_mode = Measured)]
2030	fn seal_call_data_copy(n: Linear<0, { limits::code::BLOB_BYTES }>) {
2031		let mut setup = CallSetup::<T>::default();
2032		let (mut ext, _) = setup.ext();
2033		let mut runtime = pvm::Runtime::new(&mut ext, vec![42u8; n as usize]);
2034		let mut memory = memory!(vec![0u8; n as usize],);
2035		let result;
2036		#[block]
2037		{
2038			result = runtime.bench_call_data_copy(memory.as_mut_slice(), 0, n, 0);
2039		}
2040		assert_ok!(result);
2041		assert_eq!(&memory[..], &vec![42u8; n as usize]);
2042	}
2043
2044	#[benchmark(pov_mode = Measured)]
2045	fn seal_return(n: Linear<0, { limits::CALLDATA_BYTES }>) {
2046		build_runtime!(runtime, memory: [n.to_le_bytes(), vec![42u8; n as usize], ]);
2047
2048		let result;
2049		#[block]
2050		{
2051			result = runtime.bench_seal_return(memory.as_mut_slice(), 0, 0, n);
2052		}
2053
2054		assert!(matches!(
2055			result,
2056			Err(crate::vm::pvm::TrapReason::Return(crate::vm::pvm::ReturnData { .. }))
2057		));
2058	}
2059
2060	/// Benchmark the ocst of terminating a contract.
2061	///
2062	/// `r`: whether the old code will be removed as a result of this operation. (1: yes, 0: no)
2063	#[benchmark(pov_mode = Measured)]
2064	fn seal_terminate(r: Linear<0, 1>) -> Result<(), BenchmarkError> {
2065		let delete_code = r == 1;
2066		let beneficiary = account::<T::AccountId>("beneficiary", 0, 0);
2067
2068		build_runtime!(runtime, instance, memory: [beneficiary.encode(),]);
2069		let code_hash = instance.info()?.code_hash;
2070
2071		// Increment the refcount of the code hash so that it does not get deleted
2072		if !delete_code {
2073			<CodeInfo<T>>::increment_refcount(code_hash).unwrap();
2074		}
2075		assert_account_not_whitelisted::<T>(&instance.account_id);
2076
2077		let result;
2078		#[block]
2079		{
2080			result = runtime.bench_terminate(memory.as_mut_slice(), 0);
2081		}
2082
2083		assert!(matches!(result, Err(crate::vm::pvm::TrapReason::Termination)));
2084
2085		Ok(())
2086	}
2087
2088	/// The teardown of a terminated contract at the end of the call stack.
2089	#[benchmark(pov_mode = Measured)]
2090	fn seal_terminate_logic() -> Result<(), BenchmarkError> {
2091		let caller = whitelisted_caller();
2092		let beneficiary = account::<T::AccountId>("beneficiary", 0, 0);
2093		T::AddressMapper::map_no_deposit_unchecked(&beneficiary)?;
2094
2095		build_runtime!(_runtime, instance, _memory: [vec![0u8; 0], ]);
2096		let code_hash = instance.info()?.code_hash;
2097
2098		assert!(PristineCode::<T>::get(code_hash).is_some());
2099
2100		T::Currency::set_balance(&instance.account_id, Pallet::<T>::min_balance() * 10u32.into());
2101
2102		let storage_deposit = T::Currency::balance_on_hold(
2103			&HoldReason::StorageDepositReserve.into(),
2104			&instance.account_id,
2105		);
2106		NativeDepositOf::<T>::insert(&instance.account_id, &caller, storage_deposit);
2107
2108		let mut transaction_meter = TransactionMeter::new(TransactionLimits::WeightAndDeposit {
2109			weight_limit: Default::default(),
2110			deposit_limit: BalanceOf::<T>::max_value(),
2111		})
2112		.unwrap();
2113		let exec_config = ExecConfig::new_substrate_tx();
2114		let contract_account = &instance.account_id;
2115		let origin = &ExecOrigin::from_account_id(caller);
2116		assert_account_not_whitelisted::<T>(contract_account);
2117
2118		#[block]
2119		{
2120			crate::exec::bench_do_terminate::<T>(
2121				&mut transaction_meter,
2122				&exec_config,
2123				contract_account,
2124				&origin,
2125				&beneficiary,
2126			);
2127		}
2128
2129		// Check that the contract is removed
2130		assert!(PristineCode::<T>::get(code_hash).is_none());
2131
2132		// Check that the balance has been transferred away
2133		let balance = <T as Config>::Currency::total_balance(&instance.account_id);
2134		assert_eq!(balance, 0u32.into());
2135
2136		// Check that the beneficiary received the balance
2137		let balance = <T as Config>::Currency::balance(&beneficiary);
2138		assert_eq!(balance, Pallet::<T>::min_balance() + Pallet::<T>::min_balance() * 9u32.into());
2139
2140		Ok(())
2141	}
2142
2143	// Benchmark the overhead that topics generate.
2144	// `t`: Number of topics
2145	// `n`: Size of event payload in bytes
2146	#[benchmark(pov_mode = Measured)]
2147	fn seal_deposit_event(
2148		t: Linear<0, { limits::NUM_EVENT_TOPICS as u32 }>,
2149		n: Linear<0, { limits::EVENT_BYTES }>,
2150	) {
2151		let num_topic = t as u32;
2152		let topics = (0..t).map(|i| H256::repeat_byte(i as u8)).collect::<Vec<_>>();
2153		let topics_data =
2154			topics.iter().flat_map(|hash| hash.as_bytes().to_vec()).collect::<Vec<u8>>();
2155		let data = vec![42u8; n as _];
2156		build_runtime!(runtime, instance, memory: [ topics_data, data, ]);
2157
2158		// Inside an ethereum transaction, the worst case: the log is also captured into the open
2159		// receipt, which encodes it, accrues the bloom and writes what the transaction has
2160		// committed so far to storage.
2161		let result;
2162		#[block]
2163		{
2164			result = block_storage::bench_with_ethereum_context(|| {
2165				runtime.bench_deposit_event(
2166					memory.as_mut_slice(),
2167					0, // topics_ptr
2168					num_topic,
2169					topics_data.len() as u32, // data_ptr
2170					n,                        // data_len
2171				)
2172			});
2173		}
2174		assert_ok!(result);
2175
2176		let events = System::<T>::events();
2177		let record = &events[events.len() - 1];
2178
2179		assert_eq!(
2180			record.event,
2181			crate::Event::ContractEmitted { contract: instance.address, data, topics }.into(),
2182		);
2183	}
2184
2185	enum TrieFill {
2186		Empty,
2187		Full,
2188	}
2189
2190	enum SlotAccess {
2191		Cold,
2192		Hot,
2193	}
2194
2195	fn build_storage_contract<T: Config>(
2196		op: StorageOp,
2197		fill: TrieFill,
2198	) -> Result<(ContractInfo<T>, Vec<u8>, Vec<u8>), BenchmarkError> {
2199		let key = vec![0u8; limits::STORAGE_KEY_BYTES as usize];
2200		let value = vec![1u8; limits::STORAGE_BYTES as usize];
2201		let initial_value = match op {
2202			StorageOp::Read => value.clone(),
2203			StorageOp::Write => vec![42u8; limits::STORAGE_BYTES as usize],
2204		};
2205
2206		let instance = match fill {
2207			TrieFill::Full => {
2208				Contract::<T>::with_unbalanced_storage_trie(VmBinaryModule::dummy(), &key)?
2209			},
2210			TrieFill::Empty => Contract::<T>::new(VmBinaryModule::dummy(), vec![])?,
2211		};
2212		let info = instance.info()?;
2213		info.bench_write_raw(&key, Some(initial_value), false)
2214			.map_err(|_| "Failed to write to storage during setup.")?;
2215		Ok((info, key, value))
2216	}
2217
2218	enum StorageCall {
2219		Clear,
2220		Contains,
2221		Take,
2222	}
2223
2224	fn setup_precompile_bench<T: Config>(
2225		op: StorageCall,
2226		key_byte: u8,
2227		access: SlotAccess,
2228	) -> Result<(CallSetup<T>, Key, Vec<u8>), BenchmarkError> {
2229		let max_key_len = limits::STORAGE_KEY_BYTES;
2230		let key = Key::try_from_var(vec![key_byte; max_key_len as usize])
2231			.map_err(|_| "Key has wrong length")?;
2232		let raw_key = vec![key_byte; max_key_len as usize].into();
2233		let input_bytes = match op {
2234			StorageCall::Clear => {
2235				IStorage::IStorageCalls::clearStorage(IStorage::clearStorageCall {
2236					flags: StorageFlags::empty().bits(),
2237					key: raw_key,
2238					isFixedKey: false,
2239				})
2240			},
2241			StorageCall::Contains => {
2242				IStorage::IStorageCalls::containsStorage(IStorage::containsStorageCall {
2243					flags: StorageFlags::empty().bits(),
2244					key: raw_key,
2245					isFixedKey: false,
2246				})
2247			},
2248			StorageCall::Take => IStorage::IStorageCalls::takeStorage(IStorage::takeStorageCall {
2249				flags: StorageFlags::empty().bits(),
2250				key: raw_key,
2251				isFixedKey: false,
2252			}),
2253		}
2254		.abi_encode();
2255
2256		let call_setup = CallSetup::<T>::default();
2257		if matches!(access, SlotAccess::Hot) {
2258			let info = call_setup.contract().info()?;
2259			frame_benchmarking::add_to_whitelist_child(
2260				info.child_trie_info().storage_key().to_vec(),
2261				key.hash(),
2262			);
2263		}
2264		Ok((call_setup, key, input_bytes))
2265	}
2266
2267	#[benchmark(skip_meta, pov_mode = Measured)]
2268	fn get_storage_empty() -> Result<(), BenchmarkError> {
2269		let (info, key, value) = build_storage_contract::<T>(StorageOp::Read, TrieFill::Empty)?;
2270		let child_trie_info = info.child_trie_info();
2271
2272		let result;
2273		#[block]
2274		{
2275			result = child::get_raw(&child_trie_info, &key);
2276		}
2277
2278		assert_eq!(result, Some(value));
2279		Ok(())
2280	}
2281
2282	#[benchmark(skip_meta, pov_mode = Measured)]
2283	fn get_storage_full() -> Result<(), BenchmarkError> {
2284		let (info, key, value) = build_storage_contract::<T>(StorageOp::Read, TrieFill::Full)?;
2285		let child_trie_info = info.child_trie_info();
2286
2287		let result;
2288		#[block]
2289		{
2290			result = child::get_raw(&child_trie_info, &key);
2291		}
2292
2293		assert_eq!(result, Some(value));
2294		Ok(())
2295	}
2296
2297	#[benchmark(skip_meta, pov_mode = Measured)]
2298	fn set_storage_empty() -> Result<(), BenchmarkError> {
2299		let (info, key, value) = build_storage_contract::<T>(StorageOp::Write, TrieFill::Empty)?;
2300
2301		let val = Some(value.clone());
2302		let result;
2303		#[block]
2304		{
2305			result = info.bench_write_raw(&key, val, true);
2306		}
2307
2308		assert_ok!(result);
2309		assert_eq!(child::get_raw(&info.child_trie_info(), &key).unwrap(), value);
2310		Ok(())
2311	}
2312
2313	#[benchmark(skip_meta, pov_mode = Measured)]
2314	fn set_storage_full() -> Result<(), BenchmarkError> {
2315		let (info, key, value) = build_storage_contract::<T>(StorageOp::Write, TrieFill::Full)?;
2316
2317		let val = Some(value.clone());
2318		let result;
2319		#[block]
2320		{
2321			result = info.bench_write_raw(&key, val, true);
2322		}
2323
2324		assert_ok!(result);
2325		assert_eq!(child::get_raw(&info.child_trie_info(), &key).unwrap(), value);
2326		Ok(())
2327	}
2328
2329	/// Worst-case lookup keys: differing only at the tail, comparisons walk the whole key.
2330	fn shared_prefix_keys(count: usize, suffix_of: impl Fn(u64) -> u64) -> Vec<Vec<u8>> {
2331		(0..count as u64)
2332			.map(|i| {
2333				let mut key = vec![0u8; limits::STORAGE_KEY_BYTES as usize];
2334				key[limits::STORAGE_KEY_BYTES as usize - 8..]
2335					.copy_from_slice(&suffix_of(i).to_be_bytes());
2336				key
2337			})
2338			.collect()
2339	}
2340
2341	/// Probed keys must exist, or reads measure a proof-of-absence walk instead.
2342	/// Whitelisting keeps them out of the DB counters and pre-warms the cache.
2343	fn setup_stored_keys<T: Config>(
2344		count: usize,
2345		value_byte: u8,
2346		suffix_of: impl Fn(u64) -> u64,
2347	) -> Result<(ContractInfo<T>, Vec<Vec<u8>>), BenchmarkError> {
2348		let instance = Contract::<T>::new(VmBinaryModule::dummy(), vec![])?;
2349		let info = instance.info()?;
2350		let child_trie_info = info.child_trie_info();
2351		let value = vec![value_byte; limits::STORAGE_BYTES as usize];
2352		let stored_keys = shared_prefix_keys(count, suffix_of);
2353		for key in &stored_keys {
2354			info.bench_write_raw(key, Some(value.clone()), false)
2355				.map_err(|_| "Failed to write to storage during setup.")?;
2356			frame_benchmarking::add_to_whitelist_child(
2357				child_trie_info.storage_key().to_vec(),
2358				key.clone(),
2359			);
2360		}
2361		Ok((info, stored_keys))
2362	}
2363
2364	#[benchmark(skip_meta, pov_mode = Measured)]
2365	fn overlay_probe_full(
2366		n: Linear<0, { MAX_ACCESS_LIST_ENTRIES as u32 }>,
2367	) -> Result<(), BenchmarkError> {
2368		let value_byte = 42;
2369		// One stored key per probe; odd suffixes, so the even fill never overwrites them.
2370		let (info, stored_keys) =
2371			setup_stored_keys::<T>(MAX_ACCESS_LIST_ENTRIES, value_byte, |i| i * 2 + 1)?;
2372		let child_trie_info = info.child_trie_info();
2373		// The block's PoV budget bounds the overlay entries like the access list cap.
2374		let fill_keys = shared_prefix_keys(MAX_ACCESS_LIST_ENTRIES, |i| (i + 1) * 2);
2375
2376		let mut result = None;
2377		#[block]
2378		{
2379			// The benchmark framework drains the overlay right before the block, so fill here.
2380			for key in &fill_keys {
2381				child::put_raw(&child_trie_info, key, &[0u8]);
2382			}
2383			for i in 0..n {
2384				let index = i as usize % stored_keys.len();
2385				result = child::get_raw(&child_trie_info, &stored_keys[index]);
2386			}
2387		}
2388
2389		if n > 0 {
2390			let expected = vec![value_byte; limits::STORAGE_BYTES as usize];
2391			assert_eq!(result, Some(expected), "the stored value must be read back");
2392		}
2393		Ok(())
2394	}
2395
2396	#[benchmark(skip_meta, pov_mode = Measured)]
2397	fn overlay_probe_empty(
2398		n: Linear<0, { MAX_ACCESS_LIST_ENTRIES as u32 }>,
2399	) -> Result<(), BenchmarkError> {
2400		let value_byte = 42;
2401		// Reads must cost the same as in `overlay_probe_full`, so their shared cost cancels.
2402		let (info, stored_keys) =
2403			setup_stored_keys::<T>(MAX_ACCESS_LIST_ENTRIES, value_byte, |i| i * 2 + 1)?;
2404		let child_trie_info = info.child_trie_info();
2405
2406		let mut result = None;
2407		#[block]
2408		{
2409			for i in 0..n {
2410				let index = i as usize % stored_keys.len();
2411				result = child::get_raw(&child_trie_info, &stored_keys[index]);
2412			}
2413		}
2414
2415		if n > 0 {
2416			let expected = vec![value_byte; limits::STORAGE_BYTES as usize];
2417			assert_eq!(result, Some(expected), "the stored value must be read back");
2418		}
2419		Ok(())
2420	}
2421
2422	// n: new byte size
2423	// o: old byte size
2424	#[benchmark(skip_meta, pov_mode = Measured)]
2425	fn seal_set_storage(
2426		n: Linear<0, { limits::STORAGE_BYTES }>,
2427		o: Linear<0, { limits::STORAGE_BYTES }>,
2428	) -> Result<(), BenchmarkError> {
2429		let max_key_len = limits::STORAGE_KEY_BYTES;
2430		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2431			.map_err(|_| "Key has wrong length")?;
2432		let value = vec![1u8; n as usize];
2433
2434		build_runtime!(runtime, instance, memory: [ key.unhashed(), value.clone(), ]);
2435		let info = instance.info()?;
2436
2437		info.write(&key, Some(vec![42u8; o as usize]), None, false)
2438			.map_err(|_| "Failed to write to storage during setup.")?;
2439
2440		let result;
2441		#[block]
2442		{
2443			result = runtime.bench_set_storage(
2444				memory.as_mut_slice(),
2445				StorageFlags::empty().bits(),
2446				0,           // key_ptr
2447				max_key_len, // key_len
2448				max_key_len, // value_ptr
2449				n,           // value_len
2450			);
2451		}
2452
2453		assert_ok!(result);
2454		assert_eq!(info.read(&key).unwrap(), value);
2455		Ok(())
2456	}
2457
2458	#[benchmark(skip_meta, pov_mode = Measured)]
2459	fn seal_set_storage_hot(
2460		n: Linear<0, { limits::STORAGE_BYTES }>,
2461		o: Linear<0, { limits::STORAGE_BYTES }>,
2462	) -> Result<(), BenchmarkError> {
2463		let max_key_len = limits::STORAGE_KEY_BYTES;
2464		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2465			.map_err(|_| "Key has wrong length")?;
2466		let value = vec![1u8; n as usize];
2467
2468		build_runtime!(runtime, instance, memory: [ key.unhashed(), value.clone(), ]);
2469		let info = instance.info()?;
2470
2471		info.write(&key, Some(vec![42u8; o as usize]), None, false)
2472			.map_err(|_| "Failed to write to storage during setup.")?;
2473
2474		frame_benchmarking::add_to_whitelist_child(
2475			info.child_trie_info().storage_key().to_vec(),
2476			key.hash(),
2477		);
2478
2479		// Add the key to access list so the op's touch is hot.
2480		runtime.ext().touch_storage_access(&key, StorageOp::Write);
2481
2482		let result;
2483		#[block]
2484		{
2485			result = runtime.bench_set_storage(
2486				memory.as_mut_slice(),
2487				StorageFlags::empty().bits(),
2488				0,           // key_ptr
2489				max_key_len, // key_len
2490				max_key_len, // value_ptr
2491				n,           // value_len
2492			);
2493		}
2494
2495		assert_ok!(result);
2496		assert_eq!(info.read(&key).unwrap(), value);
2497		Ok(())
2498	}
2499
2500	#[benchmark(skip_meta, pov_mode = Measured)]
2501	fn clear_storage(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2502		let key_byte = 0;
2503		let (mut call_setup, key, input_bytes) =
2504			setup_precompile_bench::<T>(StorageCall::Clear, key_byte, SlotAccess::Cold)?;
2505		let (mut ext, _) = call_setup.ext();
2506		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2507			.map_err(|_| "Failed to write to storage during setup.")?;
2508
2509		let result;
2510		#[block]
2511		{
2512			result = run_builtin_precompile(
2513				&mut ext,
2514				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2515				input_bytes,
2516			);
2517		}
2518		assert_ok!(result);
2519		assert!(ext.get_storage(&key).is_none());
2520
2521		Ok(())
2522	}
2523
2524	#[benchmark(skip_meta, pov_mode = Measured)]
2525	fn clear_storage_hot(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2526		let key_byte = 0;
2527		let (mut call_setup, key, input_bytes) =
2528			setup_precompile_bench::<T>(StorageCall::Clear, key_byte, SlotAccess::Hot)?;
2529		let (mut ext, _) = call_setup.ext();
2530		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2531			.map_err(|_| "Failed to write to storage during setup.")?;
2532
2533		ext.touch_storage_access(&key, StorageOp::Write);
2534
2535		let result;
2536		#[block]
2537		{
2538			result = run_builtin_precompile(
2539				&mut ext,
2540				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2541				input_bytes,
2542			);
2543		}
2544		assert_ok!(result);
2545		assert!(ext.get_storage(&key).is_none());
2546
2547		Ok(())
2548	}
2549
2550	#[benchmark(skip_meta, pov_mode = Measured)]
2551	fn seal_get_storage(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2552		let max_key_len = limits::STORAGE_KEY_BYTES;
2553		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2554			.map_err(|_| "Key has wrong length")?;
2555		build_runtime!(runtime, instance, memory: [ key.unhashed(), n.to_le_bytes(), vec![0u8; n as _], ]);
2556		let info = instance.info()?;
2557
2558		info.write(&key, Some(vec![42u8; n as usize]), None, false)
2559			.map_err(|_| "Failed to write to storage during setup.")?;
2560
2561		let out_ptr = max_key_len + 4;
2562		let result;
2563		#[block]
2564		{
2565			result = runtime.bench_get_storage(
2566				memory.as_mut_slice(),
2567				StorageFlags::empty().bits(),
2568				0,           // key_ptr
2569				max_key_len, // key_len
2570				out_ptr,     // out_ptr
2571				max_key_len, // out_len_ptr
2572			);
2573		}
2574
2575		assert_ok!(result);
2576		assert_eq!(&info.read(&key).unwrap(), &memory[out_ptr as usize..]);
2577		Ok(())
2578	}
2579
2580	#[benchmark(skip_meta, pov_mode = Measured)]
2581	fn seal_get_storage_hot(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2582		let max_key_len = limits::STORAGE_KEY_BYTES;
2583		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2584			.map_err(|_| "Key has wrong length")?;
2585		build_runtime!(runtime, instance, memory: [ key.unhashed(), n.to_le_bytes(), vec![0u8; n as _], ]);
2586		let info = instance.info()?;
2587
2588		info.write(&key, Some(vec![42u8; n as usize]), None, false)
2589			.map_err(|_| "Failed to write to storage during setup.")?;
2590
2591		frame_benchmarking::add_to_whitelist_child(
2592			info.child_trie_info().storage_key().to_vec(),
2593			key.hash(),
2594		);
2595
2596		runtime.ext().touch_storage_access(&key, StorageOp::Read);
2597
2598		let out_ptr = max_key_len + 4;
2599		let result;
2600		#[block]
2601		{
2602			result = runtime.bench_get_storage(
2603				memory.as_mut_slice(),
2604				StorageFlags::empty().bits(),
2605				0,           // key_ptr
2606				max_key_len, // key_len
2607				out_ptr,     // out_ptr
2608				max_key_len, // out_len_ptr
2609			);
2610		}
2611
2612		assert_ok!(result);
2613		assert_eq!(&info.read(&key).unwrap(), &memory[out_ptr as usize..]);
2614		Ok(())
2615	}
2616
2617	#[benchmark(skip_meta, pov_mode = Measured)]
2618	fn contains_storage(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2619		let key_byte = 0;
2620		let (mut call_setup, key, input_bytes) =
2621			setup_precompile_bench::<T>(StorageCall::Contains, key_byte, SlotAccess::Cold)?;
2622		let (mut ext, _) = call_setup.ext();
2623		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2624			.map_err(|_| "Failed to write to storage during setup.")?;
2625
2626		let result;
2627		#[block]
2628		{
2629			result = run_builtin_precompile(
2630				&mut ext,
2631				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2632				input_bytes,
2633			);
2634		}
2635		assert_ok!(result);
2636		assert!(ext.get_storage(&key).is_some());
2637
2638		Ok(())
2639	}
2640
2641	#[benchmark(skip_meta, pov_mode = Measured)]
2642	fn contains_storage_hot(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2643		let key_byte = 0;
2644		let (mut call_setup, key, input_bytes) =
2645			setup_precompile_bench::<T>(StorageCall::Contains, key_byte, SlotAccess::Hot)?;
2646		let (mut ext, _) = call_setup.ext();
2647		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2648			.map_err(|_| "Failed to write to storage during setup.")?;
2649
2650		ext.touch_storage_access(&key, StorageOp::Read);
2651
2652		let result;
2653		#[block]
2654		{
2655			result = run_builtin_precompile(
2656				&mut ext,
2657				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2658				input_bytes,
2659			);
2660		}
2661		assert_ok!(result);
2662		assert!(ext.get_storage(&key).is_some());
2663
2664		Ok(())
2665	}
2666
2667	#[benchmark(skip_meta, pov_mode = Measured)]
2668	fn take_storage(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2669		let key_byte = 3;
2670		let (mut call_setup, key, input_bytes) =
2671			setup_precompile_bench::<T>(StorageCall::Take, key_byte, SlotAccess::Cold)?;
2672		let (mut ext, _) = call_setup.ext();
2673		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2674			.map_err(|_| "Failed to write to storage during setup.")?;
2675
2676		let result;
2677		#[block]
2678		{
2679			result = run_builtin_precompile(
2680				&mut ext,
2681				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2682				input_bytes,
2683			);
2684		}
2685		assert_ok!(result);
2686		assert!(ext.get_storage(&key).is_none());
2687
2688		Ok(())
2689	}
2690
2691	#[benchmark(skip_meta, pov_mode = Measured)]
2692	fn take_storage_hot(n: Linear<0, { limits::STORAGE_BYTES }>) -> Result<(), BenchmarkError> {
2693		let key_byte = 3;
2694		let (mut call_setup, key, input_bytes) =
2695			setup_precompile_bench::<T>(StorageCall::Take, key_byte, SlotAccess::Hot)?;
2696		let (mut ext, _) = call_setup.ext();
2697		ext.set_storage(&key, Some(vec![42u8; n as usize]), false)
2698			.map_err(|_| "Failed to write to storage during setup.")?;
2699
2700		ext.touch_storage_access(&key, StorageOp::Write);
2701
2702		let result;
2703		#[block]
2704		{
2705			result = run_builtin_precompile(
2706				&mut ext,
2707				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
2708				input_bytes,
2709			);
2710		}
2711		assert_ok!(result);
2712		assert!(ext.get_storage(&key).is_none());
2713
2714		Ok(())
2715	}
2716
2717	fn worst_case_slot() -> crate::access_list::Slot {
2718		let key = Key::try_from_var(vec![0xFFu8; limits::STORAGE_KEY_BYTES as usize])
2719			.expect("key fits STORAGE_KEY_BYTES bound; qed");
2720		crate::access_list::Slot::from(&key)
2721	}
2722
2723	fn near_full_access_list() -> crate::access_list::AccessList {
2724		let mut al = AccessList::new();
2725		for i in 0..(MAX_ACCESS_LIST_ENTRIES - 1) {
2726			al.touch(
2727				AccessEntry { slot: worst_case_slot(), address: H160::from_low_u64_be(i as u64) },
2728				StorageOp::Read,
2729			);
2730		}
2731		al
2732	}
2733
2734	#[benchmark(pov_mode = Ignored)]
2735	fn access_list_touch_cold_full() -> Result<(), BenchmarkError> {
2736		let mut al = near_full_access_list();
2737		// Insert a new entry (u64::MAX is past the fill range, so the touch is cold).
2738		let entry =
2739			AccessEntry { slot: worst_case_slot(), address: H160::from_low_u64_be(u64::MAX) };
2740		let outcome;
2741		#[block]
2742		{
2743			outcome = al.touch(entry, StorageOp::Read);
2744		}
2745		assert!(outcome.is_cold());
2746		Ok(())
2747	}
2748
2749	#[benchmark(pov_mode = Ignored)]
2750	fn access_list_touch_hot_full() -> Result<(), BenchmarkError> {
2751		let mut al = near_full_access_list();
2752		// Worst-case hot touch: the rightmost key and the write upgrades the read-paid entry.
2753		let entry = AccessEntry {
2754			slot: worst_case_slot(),
2755			address: H160::from_low_u64_be(MAX_ACCESS_LIST_ENTRIES as u64 - 2),
2756		};
2757
2758		let touched = entry.clone();
2759		let outcome;
2760		#[block]
2761		{
2762			outcome = al.touch(touched, StorageOp::Write);
2763		}
2764		assert_eq!(
2765			outcome,
2766			Warmth::Hot { charged: StorageOp::Read },
2767			"the fill seeded this entry read-paid"
2768		);
2769		assert_eq!(
2770			al.peek(&entry),
2771			Warmth::Hot { charged: StorageOp::Write },
2772			"the write upgraded the entry"
2773		);
2774		Ok(())
2775	}
2776
2777	#[benchmark(pov_mode = Ignored)]
2778	fn access_list_touch_cold_empty() -> Result<(), BenchmarkError> {
2779		let mut al = AccessList::new();
2780		let entry =
2781			AccessEntry { slot: worst_case_slot(), address: H160::from_low_u64_be(u64::MAX) };
2782		let outcome;
2783		#[block]
2784		{
2785			outcome = al.touch(entry, StorageOp::Read);
2786		}
2787		assert!(outcome.is_cold());
2788		Ok(())
2789	}
2790
2791	#[benchmark(pov_mode = Ignored)]
2792	fn access_list_touch_hot_single_element() -> Result<(), BenchmarkError> {
2793		let mut al = AccessList::new();
2794		let entry =
2795			AccessEntry { slot: worst_case_slot(), address: H160::from_low_u64_be(u64::MAX) };
2796		al.touch(entry.clone(), StorageOp::Read);
2797		let outcome;
2798		#[block]
2799		{
2800			outcome = al.touch(entry, StorageOp::Read);
2801		}
2802		assert!(!outcome.is_cold());
2803		Ok(())
2804	}
2805
2806	// Per-entry rollback cost, prepaid by every cold touch since a frame revert
2807	// can't charge gas itself. Isolated by reverting a frame with exactly one
2808	// journaled entry on top of a near-full `AccessList`.
2809	#[benchmark(pov_mode = Ignored)]
2810	fn access_list_rollback_amortization() -> Result<(), BenchmarkError> {
2811		let mut al = near_full_access_list();
2812		al.enter_frame();
2813		al.touch(
2814			AccessEntry { slot: worst_case_slot(), address: H160::from_low_u64_be(u64::MAX) },
2815			StorageOp::Read,
2816		);
2817		#[block]
2818		{
2819			al.rollback_frame();
2820		}
2821		Ok(())
2822	}
2823
2824	// We use both full and empty benchmarks here instead of benchmarking transient_storage
2825	// (BTreeMap) directly. This approach is necessary because benchmarking this BTreeMap is very
2826	// slow. Additionally, we use linear regression for our benchmarks, and the BTreeMap's log(n)
2827	// complexity can introduce approximation errors.
2828	#[benchmark(pov_mode = Ignored)]
2829	fn set_transient_storage_empty() -> Result<(), BenchmarkError> {
2830		let max_value_len = limits::STORAGE_BYTES;
2831		let max_key_len = limits::STORAGE_KEY_BYTES;
2832		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2833			.map_err(|_| "Key has wrong length")?;
2834		let value = Some(vec![42u8; max_value_len as _]);
2835		let mut setup = CallSetup::<T>::default();
2836		let (mut ext, _) = setup.ext();
2837		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
2838		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2839		let result;
2840		#[block]
2841		{
2842			result = runtime.ext().set_transient_storage(&key, value, false);
2843		}
2844
2845		assert_eq!(result, Ok(WriteOutcome::New));
2846		assert_eq!(runtime.ext().get_transient_storage(&key), Some(vec![42u8; max_value_len as _]));
2847		Ok(())
2848	}
2849
2850	#[benchmark(pov_mode = Ignored)]
2851	fn set_transient_storage_full() -> Result<(), BenchmarkError> {
2852		let max_value_len = limits::STORAGE_BYTES;
2853		let max_key_len = limits::STORAGE_KEY_BYTES;
2854		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2855			.map_err(|_| "Key has wrong length")?;
2856		let value = Some(vec![42u8; max_value_len as _]);
2857		let mut setup = CallSetup::<T>::default();
2858		setup.set_transient_storage_size(limits::TRANSIENT_STORAGE_BYTES);
2859		let (mut ext, _) = setup.ext();
2860		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
2861		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2862		let result;
2863		#[block]
2864		{
2865			result = runtime.ext().set_transient_storage(&key, value, false);
2866		}
2867
2868		assert_eq!(result, Ok(WriteOutcome::New));
2869		assert_eq!(runtime.ext().get_transient_storage(&key), Some(vec![42u8; max_value_len as _]));
2870		Ok(())
2871	}
2872
2873	#[benchmark(pov_mode = Ignored)]
2874	fn get_transient_storage_empty() -> Result<(), BenchmarkError> {
2875		let max_value_len = limits::STORAGE_BYTES;
2876		let max_key_len = limits::STORAGE_KEY_BYTES;
2877		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2878			.map_err(|_| "Key has wrong length")?;
2879
2880		let mut setup = CallSetup::<T>::default();
2881		let (mut ext, _) = setup.ext();
2882		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
2883		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2884		runtime
2885			.ext()
2886			.set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false)
2887			.map_err(|_| "Failed to write to transient storage during setup.")?;
2888		let result;
2889		#[block]
2890		{
2891			result = runtime.ext().get_transient_storage(&key);
2892		}
2893
2894		assert_eq!(result, Some(vec![42u8; max_value_len as _]));
2895		Ok(())
2896	}
2897
2898	#[benchmark(pov_mode = Ignored)]
2899	fn get_transient_storage_full() -> Result<(), BenchmarkError> {
2900		let max_value_len = limits::STORAGE_BYTES;
2901		let max_key_len = limits::STORAGE_KEY_BYTES;
2902		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2903			.map_err(|_| "Key has wrong length")?;
2904
2905		let mut setup = CallSetup::<T>::default();
2906		setup.set_transient_storage_size(limits::TRANSIENT_STORAGE_BYTES);
2907		let (mut ext, _) = setup.ext();
2908		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
2909		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2910		runtime
2911			.ext()
2912			.set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false)
2913			.map_err(|_| "Failed to write to transient storage during setup.")?;
2914		let result;
2915		#[block]
2916		{
2917			result = runtime.ext().get_transient_storage(&key);
2918		}
2919
2920		assert_eq!(result, Some(vec![42u8; max_value_len as _]));
2921		Ok(())
2922	}
2923
2924	// The weight of journal rollbacks should be taken into account when setting storage.
2925	#[benchmark(pov_mode = Ignored)]
2926	fn rollback_transient_storage() -> Result<(), BenchmarkError> {
2927		let max_value_len = limits::STORAGE_BYTES;
2928		let max_key_len = limits::STORAGE_KEY_BYTES;
2929		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2930			.map_err(|_| "Key has wrong length")?;
2931
2932		let mut setup = CallSetup::<T>::default();
2933		setup.set_transient_storage_size(limits::TRANSIENT_STORAGE_BYTES);
2934		let (mut ext, _) = setup.ext();
2935		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
2936		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2937		runtime.ext().transient_storage().start_transaction();
2938		runtime
2939			.ext()
2940			.set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false)
2941			.map_err(|_| "Failed to write to transient storage during setup.")?;
2942		#[block]
2943		{
2944			runtime.ext().transient_storage().rollback_transaction();
2945		}
2946
2947		assert_eq!(runtime.ext().get_transient_storage(&key), None);
2948		Ok(())
2949	}
2950
2951	// n: new byte size
2952	// o: old byte size
2953	#[benchmark(pov_mode = Measured)]
2954	fn seal_set_transient_storage(
2955		n: Linear<0, { limits::STORAGE_BYTES }>,
2956		o: Linear<0, { limits::STORAGE_BYTES }>,
2957	) -> Result<(), BenchmarkError> {
2958		let max_key_len = limits::STORAGE_KEY_BYTES;
2959		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2960			.map_err(|_| "Key has wrong length")?;
2961		let value = vec![1u8; n as usize];
2962		build_runtime!(runtime, memory: [ key.unhashed(), value.clone(), ]);
2963		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
2964		runtime
2965			.ext()
2966			.set_transient_storage(&key, Some(vec![42u8; o as usize]), false)
2967			.map_err(|_| "Failed to write to transient storage during setup.")?;
2968
2969		let result;
2970		#[block]
2971		{
2972			result = runtime.bench_set_storage(
2973				memory.as_mut_slice(),
2974				StorageFlags::TRANSIENT.bits(),
2975				0,           // key_ptr
2976				max_key_len, // key_len
2977				max_key_len, // value_ptr
2978				n,           // value_len
2979			);
2980		}
2981
2982		assert_ok!(result);
2983		assert_eq!(runtime.ext().get_transient_storage(&key).unwrap(), value);
2984		Ok(())
2985	}
2986
2987	#[benchmark(pov_mode = Measured)]
2988	fn seal_clear_transient_storage(
2989		n: Linear<0, { limits::STORAGE_BYTES }>,
2990	) -> Result<(), BenchmarkError> {
2991		let max_key_len = limits::STORAGE_KEY_BYTES;
2992		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
2993			.map_err(|_| "Key has wrong length")?;
2994		let input_bytes = IStorage::IStorageCalls::clearStorage(IStorage::clearStorageCall {
2995			flags: StorageFlags::TRANSIENT.bits(),
2996			key: vec![0u8; max_key_len as usize].into(),
2997			isFixedKey: false,
2998		})
2999		.abi_encode();
3000
3001		let mut call_setup = CallSetup::<T>::default();
3002		let (mut ext, _) = call_setup.ext();
3003		ext.set_transient_storage(&key, Some(vec![42u8; n as usize]), false)
3004			.map_err(|_| "Failed to write to transient storage during setup.")?;
3005
3006		let result;
3007		#[block]
3008		{
3009			result = run_builtin_precompile(
3010				&mut ext,
3011				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
3012				input_bytes,
3013			);
3014		}
3015		assert_ok!(result);
3016		assert!(ext.get_transient_storage(&key).is_none());
3017
3018		Ok(())
3019	}
3020
3021	#[benchmark(pov_mode = Measured)]
3022	fn seal_get_transient_storage(
3023		n: Linear<0, { limits::STORAGE_BYTES }>,
3024	) -> Result<(), BenchmarkError> {
3025		let max_key_len = limits::STORAGE_KEY_BYTES;
3026		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
3027			.map_err(|_| "Key has wrong length")?;
3028		build_runtime!(runtime, memory: [ key.unhashed(), n.to_le_bytes(), vec![0u8; n as _], ]);
3029		runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX;
3030		runtime
3031			.ext()
3032			.set_transient_storage(&key, Some(vec![42u8; n as usize]), false)
3033			.map_err(|_| "Failed to write to transient storage during setup.")?;
3034
3035		let out_ptr = max_key_len + 4;
3036		let result;
3037		#[block]
3038		{
3039			result = runtime.bench_get_storage(
3040				memory.as_mut_slice(),
3041				StorageFlags::TRANSIENT.bits(),
3042				0,           // key_ptr
3043				max_key_len, // key_len
3044				out_ptr,     // out_ptr
3045				max_key_len, // out_len_ptr
3046			);
3047		}
3048
3049		assert_ok!(result);
3050		assert_eq!(
3051			&runtime.ext().get_transient_storage(&key).unwrap(),
3052			&memory[out_ptr as usize..]
3053		);
3054		Ok(())
3055	}
3056
3057	#[benchmark(pov_mode = Measured)]
3058	fn seal_contains_transient_storage(
3059		n: Linear<0, { limits::STORAGE_BYTES }>,
3060	) -> Result<(), BenchmarkError> {
3061		let max_key_len = limits::STORAGE_KEY_BYTES;
3062		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
3063			.map_err(|_| "Key has wrong length")?;
3064
3065		let input_bytes = IStorage::IStorageCalls::containsStorage(IStorage::containsStorageCall {
3066			flags: StorageFlags::TRANSIENT.bits(),
3067			key: vec![0u8; max_key_len as usize].into(),
3068			isFixedKey: false,
3069		})
3070		.abi_encode();
3071
3072		let mut call_setup = CallSetup::<T>::default();
3073		let (mut ext, _) = call_setup.ext();
3074		ext.set_transient_storage(&key, Some(vec![42u8; n as usize]), false)
3075			.map_err(|_| "Failed to write to transient storage during setup.")?;
3076
3077		let result;
3078		#[block]
3079		{
3080			result = run_builtin_precompile(
3081				&mut ext,
3082				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
3083				input_bytes,
3084			);
3085		}
3086		assert!(result.is_ok());
3087		assert!(ext.get_transient_storage(&key).is_some());
3088
3089		Ok(())
3090	}
3091
3092	#[benchmark(pov_mode = Measured)]
3093	fn seal_take_transient_storage(
3094		n: Linear<0, { limits::STORAGE_BYTES }>,
3095	) -> Result<(), BenchmarkError> {
3096		let n = limits::STORAGE_BYTES;
3097		let value = vec![42u8; n as usize];
3098		let max_key_len = limits::STORAGE_KEY_BYTES;
3099		let key = Key::try_from_var(vec![0u8; max_key_len as usize])
3100			.map_err(|_| "Key has wrong length")?;
3101
3102		let input_bytes = IStorage::IStorageCalls::takeStorage(IStorage::takeStorageCall {
3103			flags: StorageFlags::TRANSIENT.bits(),
3104			key: vec![0u8; max_key_len as usize].into(),
3105			isFixedKey: false,
3106		})
3107		.abi_encode();
3108
3109		let mut call_setup = CallSetup::<T>::default();
3110		let (mut ext, _) = call_setup.ext();
3111		ext.set_transient_storage(&key, Some(value), false)
3112			.map_err(|_| "Failed to write to transient storage during setup.")?;
3113
3114		let result;
3115		#[block]
3116		{
3117			result = run_builtin_precompile(
3118				&mut ext,
3119				H160(BenchmarkStorage::<T>::MATCHER.base_address()).as_fixed_bytes(),
3120				input_bytes,
3121			);
3122		}
3123		assert!(result.is_ok());
3124		assert!(ext.get_transient_storage(&key).is_none());
3125
3126		Ok(())
3127	}
3128
3129	// Sets up a call that sends `$value` plus `$dust` and clones an input of `$input_len` bytes.
3130	// The callee's account is funded up front if `$callee_exists`; otherwise the transfer creates
3131	// it. Defines `$do_call` to make the call and `$assert_transferred` to check that the value
3132	// arrived.
3133	macro_rules! call_setup {
3134		(
3135			$do_call:ident, $assert_transferred:ident,
3136			value: $value:expr, dust: $dust:expr, input_len: $input_len:expr,
3137			callee_exists: $callee_exists:expr
3138		) => {
3139			// An EIP-7702 delegated callee is the worst case for the account resolution.
3140			let target = Contract::<T>::with_index(1, VmBinaryModule::dummy(), vec![])?;
3141			let callee_addr = H160([0x42; 20]);
3142			let callee = delegated_eoa::<T>(callee_addr, target.address)?;
3143			if $callee_exists {
3144				T::Currency::set_balance(&callee, Pallet::<T>::min_balance());
3145			}
3146			assert_eq!(System::<T>::account_exists(&callee), $callee_exists);
3147			if $dust != 0 {
3148				set_dust_so_transfer_mints::<T>(&callee_addr, $dust);
3149			}
3150			let callee_balance_before = Pallet::<T>::evm_balance(&callee_addr);
3151
3152			let callee_bytes = callee.encode();
3153			let callee_len = callee_bytes.len() as u32;
3154
3155			let value: BalanceOf<T> = $value.into();
3156			let evm_value = Pallet::<T>::convert_native_to_evm(
3157				BalanceWithDust::new_unchecked::<T>(value, $dust),
3158			);
3159			let value_bytes = evm_value.encode();
3160
3161			let deposit = default_deposit_limit::<T>();
3162			let deposit_bytes = Into::<U256>::into(deposit).encode();
3163			let deposit_len = deposit_bytes.len() as u32;
3164
3165			let mut setup = CallSetup::<T>::default();
3166			setup.set_origin(ExecOrigin::from_account_id(setup.contract().account_id.clone()));
3167			setup.set_balance(caller_funding::<T>());
3168			let caller_account = setup.contract().account_id.clone();
3169			let caller_addr = setup.contract().address;
3170			let caller_balance_before = Pallet::<T>::evm_balance(&caller_addr);
3171			let callee_ed_paid = if $callee_exists {
3172				U256::zero()
3173			} else {
3174				Pallet::<T>::convert_native_to_evm(Pallet::<T>::min_balance())
3175			};
3176
3177			let (mut ext, _) = setup.ext();
3178			// `CLONE_INPUT` clones the runtime's own input, so the input goes here.
3179			let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![42; $input_len as usize]);
3180			let mut memory = memory!(callee_bytes, deposit_bytes, value_bytes,);
3181
3182			let mut $do_call = || {
3183				runtime.bench_call(
3184					memory.as_mut_slice(),
3185					pack_hi_lo(CallFlags::CLONE_INPUT.bits(), 0), // flags + callee
3186					u64::MAX,                                     // ref_time_limit
3187					u64::MAX,                                     // proof_size_limit
3188					pack_hi_lo(callee_len, callee_len + deposit_len), // deposit_ptr + value_pr
3189					pack_hi_lo(0, 0),                             // input len + data ptr
3190					pack_hi_lo(0, SENTINEL),                      // output len + data ptr
3191				)
3192			};
3193
3194			let $assert_transferred = || {
3195				assert!(System::<T>::account_exists(&callee), "{callee_addr:?} should exist");
3196				assert_eq!(
3197					Pallet::<T>::evm_balance(&callee_addr),
3198					callee_balance_before + evm_value,
3199					"{callee_addr:?} balance should grow by {evm_value:?}"
3200				);
3201				assert_eq!(AccountInfoOf::<T>::get(&callee_addr).map(|info| info.dust), Some(0));
3202				assert_eq!(
3203					Pallet::<T>::evm_balance(&caller_addr),
3204					caller_balance_before - evm_value - callee_ed_paid,
3205					"{caller_addr:?} should pay only the value and the callee's ED"
3206				);
3207			};
3208			assert_account_not_whitelisted::<T>(&caller_account);
3209		};
3210	}
3211
3212	// i: size of the input the call clones
3213	#[benchmark(pov_mode = Measured)]
3214	fn seal_call(i: Linear<0, { limits::CALLDATA_BYTES }>) -> Result<(), BenchmarkError> {
3215		call_setup!(
3216			do_call,
3217			assert_transferred,
3218			value: 0u32,
3219			dust: 0u32,
3220			input_len: i,
3221			callee_exists: true
3222		);
3223
3224		// Inside an ethereum transaction, the worst case: the callee's successful exit also checks
3225		// the open receipt, see `block_storage::ensure_receipt_within_limit`.
3226		let result;
3227		#[block]
3228		{
3229			result = block_storage::bench_with_ethereum_context(|| do_call());
3230		}
3231
3232		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3233		assert_transferred();
3234		Ok(())
3235	}
3236
3237	// d: with or without dust value to transfer
3238	#[benchmark(pov_mode = Measured)]
3239	fn seal_call_transfer(d: Linear<0, 1>) -> Result<(), BenchmarkError> {
3240		call_setup!(
3241			do_call,
3242			assert_transferred,
3243			value: 1_000_000u32,
3244			dust: 100u32 * d,
3245			input_len: 0u32,
3246			callee_exists: false
3247		);
3248
3249		// Inside an ethereum transaction, the worst case: the callee's successful exit also checks
3250		// the open receipt, see `block_storage::ensure_receipt_within_limit`.
3251		let result;
3252		#[block]
3253		{
3254			result = block_storage::bench_with_ethereum_context(|| do_call());
3255		}
3256
3257		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3258		assert_transferred();
3259		Ok(())
3260	}
3261
3262	// d: with or without dust value to transfer
3263	#[benchmark(pov_mode = Measured)]
3264	fn seal_call_transfer_to_existing(d: Linear<0, 1>) -> Result<(), BenchmarkError> {
3265		call_setup!(
3266			do_call,
3267			assert_transferred,
3268			value: 1_000_000u32,
3269			dust: 100u32 * d,
3270			input_len: 0u32,
3271			callee_exists: true
3272		);
3273
3274		// Inside an ethereum transaction, the worst case: the callee's successful exit also checks
3275		// the open receipt, see `block_storage::ensure_receipt_within_limit`.
3276		let result;
3277		#[block]
3278		{
3279			result = block_storage::bench_with_ethereum_context(|| do_call());
3280		}
3281
3282		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3283		assert_transferred();
3284		Ok(())
3285	}
3286
3287	// d: 1 if the associated pre-compile has a contract info that needs to be loaded
3288	// i: size of the input data
3289	#[benchmark(pov_mode = Measured)]
3290	fn seal_call_precompile(d: Linear<0, 1>, i: Linear<0, { limits::CALLDATA_BYTES - 100 }>) {
3291		use alloy_core::sol_types::SolInterface;
3292		use precompiles::{BenchmarkNoInfo, BenchmarkWithInfo, BuiltinPrecompile, IBenchmarking};
3293
3294		let callee_bytes = if d == 1 {
3295			BenchmarkWithInfo::<T>::MATCHER.base_address().to_vec()
3296		} else {
3297			BenchmarkNoInfo::<T>::MATCHER.base_address().to_vec()
3298		};
3299		let callee_len = callee_bytes.len() as u32;
3300
3301		let deposit: BalanceOf<T> = (u32::MAX - 100).into();
3302		let deposit_bytes = Into::<U256>::into(deposit).encode();
3303		let deposit_len = deposit_bytes.len() as u32;
3304
3305		let value: BalanceOf<T> = Zero::zero();
3306		let value_bytes = Into::<U256>::into(value).encode();
3307		let value_len = value_bytes.len() as u32;
3308
3309		let input_bytes = IBenchmarking::IBenchmarkingCalls::bench(IBenchmarking::benchCall {
3310			input: vec![42_u8; i as usize].into(),
3311		})
3312		.abi_encode();
3313		let input_len = input_bytes.len() as u32;
3314
3315		let mut setup = CallSetup::<T>::default();
3316		setup.set_storage_deposit_limit(deposit);
3317
3318		let (mut ext, _) = setup.ext();
3319		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
3320		let mut memory = memory!(callee_bytes, deposit_bytes, value_bytes, input_bytes,);
3321
3322		let mut do_benchmark = || {
3323			runtime.bench_call(
3324				memory.as_mut_slice(),
3325				pack_hi_lo(0, 0), // flags + callee
3326				u64::MAX,         // ref_time_limit
3327				u64::MAX,         // proof_size_limit
3328				pack_hi_lo(callee_len, callee_len + deposit_len), /* deposit_ptr +
3329				                   * value_pr */
3330				pack_hi_lo(input_len, callee_len + deposit_len + value_len), /* input len +
3331				                                                              * input ptr */
3332				pack_hi_lo(0, SENTINEL), // output len + output ptr
3333			)
3334		};
3335
3336		// first call of the pre-compile will create its contract info and account
3337		// so we make sure to create it
3338		assert_eq!(do_benchmark().unwrap(), ReturnErrorCode::Success);
3339
3340		// Inside an ethereum transaction, the worst case: the pre-compile's successful exit also
3341		// checks the open receipt, see `block_storage::ensure_receipt_within_limit`.
3342		let result;
3343		#[block]
3344		{
3345			result = block_storage::bench_with_ethereum_context(|| do_benchmark());
3346		}
3347
3348		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3349	}
3350
3351	#[benchmark(pov_mode = Measured)]
3352	fn seal_delegate_call() -> Result<(), BenchmarkError> {
3353		// Delegated code source: worst case for the callee resolution in `new_frame`.
3354		let target = Contract::<T>::with_index(1, VmBinaryModule::dummy(), vec![])?;
3355		let address = delegated_eoa::<T>(H160([0x43; 20]), target.address)?;
3356
3357		let address_bytes = address.encode();
3358		let address_len = address_bytes.len() as u32;
3359
3360		let deposit: BalanceOf<T> = (u32::MAX - 100).into();
3361		let deposit_bytes = Into::<U256>::into(deposit).encode();
3362
3363		let mut setup = CallSetup::<T>::default();
3364		setup.set_storage_deposit_limit(deposit);
3365		setup.set_origin(ExecOrigin::from_account_id(setup.contract().account_id.clone()));
3366
3367		let (mut ext, _) = setup.ext();
3368		let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
3369		let mut memory = memory!(address_bytes, deposit_bytes,);
3370
3371		// Inside an ethereum transaction, the worst case: the callee's successful exit also checks
3372		// the open receipt, see `block_storage::ensure_receipt_within_limit`.
3373		let result;
3374		#[block]
3375		{
3376			result = block_storage::bench_with_ethereum_context(|| {
3377				runtime.bench_delegate_call(
3378					memory.as_mut_slice(),
3379					pack_hi_lo(0, 0),        // flags + address ptr
3380					u64::MAX,                // ref_time_limit
3381					u64::MAX,                // proof_size_limit
3382					address_len,             // deposit_ptr
3383					pack_hi_lo(0, 0),        // input len + data ptr
3384					pack_hi_lo(0, SENTINEL), // output len + ptr
3385				)
3386			});
3387		}
3388
3389		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3390		Ok(())
3391	}
3392
3393	// Sets up an instantiate that sends `$value` plus `$dust` with an input of `$input_len` bytes.
3394	// Defines `$do_instantiate` to run it and `$assert_instantiated` to check that the value
3395	// arrived.
3396	macro_rules! seal_instantiate_setup {
3397		(
3398			$do_instantiate:ident, $assert_instantiated:ident,
3399			value: $value:expr, dust: $dust:expr, input_len: $input_len:expr
3400		) => {
3401			let code = VmBinaryModule::dummy();
3402			let hash =
3403				Contract::<T>::with_index(1, VmBinaryModule::dummy(), vec![])?.info()?.code_hash;
3404			let hash_bytes = hash.encode();
3405
3406			let value: BalanceOf<T> = $value.into();
3407			let evm_value = Pallet::<T>::convert_native_to_evm(
3408				BalanceWithDust::new_unchecked::<T>(value, $dust),
3409			);
3410			let value_bytes = evm_value.encode();
3411			let value_len = value_bytes.len() as u32;
3412
3413			let deposit: BalanceOf<T> = BalanceOf::<T>::max_value();
3414			let deposit_bytes = Into::<U256>::into(deposit).encode();
3415			let deposit_len = deposit_bytes.len() as u32;
3416
3417			let mut setup = CallSetup::<T>::default();
3418			setup.set_origin(ExecOrigin::from_account_id(setup.contract().account_id.clone()));
3419			setup.set_balance(caller_funding::<T>());
3420
3421			let account_id = &setup.contract().account_id.clone();
3422			let (mut ext, _) = setup.ext();
3423			let mut runtime = pvm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
3424
3425			let input = vec![42u8; $input_len as usize];
3426			let input_len = hash_bytes.len() as u32 + input.len() as u32;
3427			let salt = [42u8; 32];
3428			let deployer = T::AddressMapper::to_address(&account_id);
3429			let deployer_balance_before = Pallet::<T>::evm_balance(&deployer);
3430			let new_contract_addr = crate::address::create2(&deployer, &code.code, &input, &salt);
3431			let mut memory = memory!(hash_bytes, input, deposit_bytes, value_bytes, salt,);
3432			let deposit_ptr = input_len;
3433			let value_ptr = deposit_ptr + deposit_len;
3434			let salt_ptr = value_ptr + value_len;
3435
3436			assert!(AccountInfoOf::<T>::get(&new_contract_addr).is_none());
3437			if $dust != 0 {
3438				set_dust_so_transfer_mints::<T>(&new_contract_addr, $dust);
3439			}
3440			let new_contract_balance_before = Pallet::<T>::evm_balance(&new_contract_addr);
3441
3442			let mut $do_instantiate = || {
3443				runtime.bench_instantiate(
3444					memory.as_mut_slice(),
3445					u64::MAX,                           // ref_time_limit
3446					u64::MAX,                           // proof_size_limit
3447					pack_hi_lo(deposit_ptr, value_ptr), // deposit_ptr + value_ptr
3448					pack_hi_lo(input_len, 0),           // input_data_len + input_data
3449					pack_hi_lo(0, SENTINEL),            // output_len_ptr + output_ptr
3450					pack_hi_lo(SENTINEL, salt_ptr),     // address_ptr + salt_ptr
3451				)
3452			};
3453			let $assert_instantiated = || {
3454				assert!(AccountInfo::<T>::load_contract(&new_contract_addr).is_some());
3455				assert_eq!(
3456					Pallet::<T>::evm_balance(&new_contract_addr),
3457					new_contract_balance_before + evm_value,
3458					"{new_contract_addr:?} balance should grow by {evm_value:?}"
3459				);
3460				assert_eq!(
3461					AccountInfoOf::<T>::get(&new_contract_addr).map(|info| info.dust),
3462					Some(0)
3463				);
3464				assert_eq!(
3465					Pallet::<T>::evm_balance(&deployer),
3466					deployer_balance_before - evm_value,
3467					"{deployer:?} should pay only the value"
3468				);
3469			};
3470			assert_account_not_whitelisted::<T>(account_id);
3471		};
3472	}
3473
3474	// i: size of the input data
3475	#[benchmark(pov_mode = Measured)]
3476	fn seal_instantiate(i: Linear<0, { limits::CALLDATA_BYTES }>) -> Result<(), BenchmarkError> {
3477		seal_instantiate_setup!(
3478			do_instantiate,
3479			assert_instantiated,
3480			value: 0u32,
3481			dust: 0u32,
3482			input_len: i
3483		);
3484
3485		// Inside an ethereum transaction, the worst case: the constructor's successful exit also
3486		// checks the open receipt, see `block_storage::ensure_receipt_within_limit`.
3487		let result;
3488		#[block]
3489		{
3490			result = block_storage::bench_with_ethereum_context(|| do_instantiate());
3491		}
3492
3493		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3494		assert_instantiated();
3495		Ok(())
3496	}
3497
3498	// d: with or without dust value to transfer
3499	#[benchmark(pov_mode = Measured)]
3500	fn seal_instantiate_transfer(d: Linear<0, 1>) -> Result<(), BenchmarkError> {
3501		seal_instantiate_setup!(
3502			do_instantiate,
3503			assert_instantiated,
3504			value: 1_000_000u32,
3505			dust: 100u32 * d,
3506			input_len: 0u32
3507		);
3508
3509		// Inside an ethereum transaction, the worst case: the constructor's successful exit also
3510		// checks the open receipt, see `block_storage::ensure_receipt_within_limit`.
3511		let result;
3512		#[block]
3513		{
3514			result = block_storage::bench_with_ethereum_context(|| do_instantiate());
3515		}
3516
3517		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3518		assert_instantiated();
3519		Ok(())
3520	}
3521
3522	// Sets up an instantiate that sends `$value` plus `$dust` with `$init_code_len` bytes of code.
3523	// Defines `$do_instantiate` to run it and `$assert_instantiated` to check that the value
3524	// arrived.
3525	macro_rules! evm_instantiate_setup {
3526		(
3527			$do_instantiate:ident, $assert_instantiated:ident,
3528			value: $value:expr, dust: $dust:expr, init_code_len: $init_code_len:expr
3529		) => {
3530			use crate::vm::evm::instructions::BENCH_INIT_CODE;
3531			let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_init_code_for_runtime_size(0));
3532			setup.set_origin(ExecOrigin::from_account_id(setup.contract().account_id.clone()));
3533			setup.set_balance(caller_funding::<T>());
3534			let deployer_account = setup.contract().account_id.clone();
3535			let deployer = setup.contract().address;
3536			let deployer_balance_before = Pallet::<T>::evm_balance(&deployer);
3537
3538			let (mut ext, _) = setup.ext();
3539			let mut interpreter =
3540				Interpreter::new(Default::default(), Default::default(), &mut ext);
3541
3542			let value: BalanceOf<T> = $value.into();
3543			let evm_value = Pallet::<T>::convert_native_to_evm(
3544				BalanceWithDust::new_unchecked::<T>(value, $dust),
3545			);
3546
3547			let init_code = vec![BENCH_INIT_CODE; $init_code_len as usize];
3548			interpreter
3549				.memory
3550				.resize(0, init_code.len(), interpreter.ext)
3551				.continue_value()
3552				.expect("the init code fits the memory limit and the setup weight budget; qed");
3553			let salt = U256::from(42u64);
3554			interpreter.memory.set_data(0, 0, init_code.len(), &init_code);
3555			let new_contract_addr =
3556				crate::address::create2(&deployer, &init_code, &[], &salt.to_big_endian());
3557			if $dust != 0 {
3558				set_dust_so_transfer_mints::<T>(&new_contract_addr, $dust);
3559			}
3560			let new_contract_balance_before = Pallet::<T>::evm_balance(&new_contract_addr);
3561
3562			// Setup stack for create instruction [value, offset, size, salt]
3563			let _ = interpreter.stack.push(salt);
3564			let _ = interpreter.stack.push(U256::from(init_code.len()));
3565			let _ = interpreter.stack.push(U256::zero());
3566			let _ = interpreter.stack.push(evm_value);
3567
3568			let mut $do_instantiate =
3569				|| instructions::contract::create::<true, _>(&mut interpreter);
3570			let $assert_instantiated = || {
3571				let new_contract = AccountInfo::<T>::load_contract(&new_contract_addr)
3572					.expect("the create should deploy a contract");
3573				assert_eq!(Pallet::<T>::code(&new_contract_addr).len(), MAX_CODE_SIZE);
3574				assert_eq!(
3575					Pallet::<T>::evm_balance(&new_contract_addr),
3576					new_contract_balance_before + evm_value,
3577					"{new_contract_addr:?} balance should grow by {evm_value:?}"
3578				);
3579				assert_eq!(
3580					AccountInfoOf::<T>::get(&new_contract_addr).map(|info| info.dust),
3581					Some(0)
3582				);
3583				let code_deposit = Pallet::<T>::convert_native_to_evm(
3584					CodeInfoOf::<T>::get(new_contract.code_hash)
3585						.expect("the create should store its runtime code")
3586						.deposit(),
3587				);
3588				assert_eq!(
3589					Pallet::<T>::evm_balance(&deployer),
3590					deployer_balance_before - evm_value - code_deposit,
3591					"{deployer:?} should pay only the value and the code deposit"
3592				);
3593			};
3594			assert_account_not_whitelisted::<T>(&deployer_account);
3595		};
3596	}
3597
3598	// i: size of the init code
3599	#[benchmark(pov_mode = Measured)]
3600	fn evm_instantiate(
3601		i: Linear<{ BENCH_MIN_EVM_INIT_CODE_LEN }, { MAX_INITCODE_SIZE as u32 }>,
3602	) -> Result<(), BenchmarkError> {
3603		evm_instantiate_setup!(
3604			do_instantiate,
3605			assert_instantiated,
3606			value: 0u32,
3607			dust: 0u32,
3608			init_code_len: i
3609		);
3610
3611		// Inside an ethereum transaction, the worst case: the constructor's successful exit also
3612		// checks the open receipt, see `block_storage::ensure_receipt_within_limit`.
3613		let result;
3614		#[block]
3615		{
3616			result = block_storage::bench_with_ethereum_context(|| do_instantiate());
3617		}
3618
3619		assert!(result.is_continue());
3620		assert_instantiated();
3621		Ok(())
3622	}
3623
3624	// d: with or without dust value to transfer
3625	#[benchmark(pov_mode = Measured)]
3626	fn evm_instantiate_transfer(d: Linear<0, 1>) -> Result<(), BenchmarkError> {
3627		evm_instantiate_setup!(
3628			do_instantiate,
3629			assert_instantiated,
3630			value: 1_000_000u32,
3631			dust: 100u32 * d,
3632			init_code_len: BENCH_MIN_EVM_INIT_CODE_LEN
3633		);
3634
3635		// Inside an ethereum transaction, the worst case: the constructor's successful exit also
3636		// checks the open receipt, see `block_storage::ensure_receipt_within_limit`.
3637		let result;
3638		#[block]
3639		{
3640			result = block_storage::bench_with_ethereum_context(|| do_instantiate());
3641		}
3642
3643		assert!(result.is_continue());
3644		assert_instantiated();
3645		Ok(())
3646	}
3647
3648	// `n`: Input to hash in bytes
3649	#[benchmark(pov_mode = Measured)]
3650	fn sha2_256(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3651		let input = vec![0u8; n as usize];
3652		let mut call_setup = CallSetup::<T>::default();
3653		let (mut ext, _) = call_setup.ext();
3654
3655		let result;
3656		#[block]
3657		{
3658			result = run_builtin_precompile(
3659				&mut ext,
3660				H160::from_low_u64_be(2).as_fixed_bytes(),
3661				input.clone(),
3662			);
3663		}
3664		assert_eq!(sp_io::hashing::sha2_256(&input).to_vec(), result.unwrap().data);
3665	}
3666
3667	#[benchmark(pov_mode = Measured)]
3668	fn identity(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3669		let input = vec![0u8; n as usize];
3670		let mut call_setup = CallSetup::<T>::default();
3671		let (mut ext, _) = call_setup.ext();
3672
3673		let result;
3674		#[block]
3675		{
3676			result = run_builtin_precompile(
3677				&mut ext,
3678				H160::from_low_u64_be(4).as_fixed_bytes(),
3679				input.clone(),
3680			);
3681		}
3682		assert_eq!(input, result.unwrap().data);
3683	}
3684
3685	// `n`: Input to hash in bytes
3686	#[benchmark(pov_mode = Measured)]
3687	fn ripemd_160(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3688		use ripemd::Digest;
3689		let input = vec![0u8; n as usize];
3690		let mut call_setup = CallSetup::<T>::default();
3691		let (mut ext, _) = call_setup.ext();
3692
3693		let result;
3694		#[block]
3695		{
3696			result = run_builtin_precompile(
3697				&mut ext,
3698				H160::from_low_u64_be(3).as_fixed_bytes(),
3699				input.clone(),
3700			);
3701		}
3702		let mut expected = [0u8; 32];
3703		expected[12..32].copy_from_slice(&ripemd::Ripemd160::digest(input));
3704
3705		assert_eq!(expected.to_vec(), result.unwrap().data);
3706	}
3707
3708	// `n`: Input to hash in bytes
3709	#[benchmark(pov_mode = Measured)]
3710	fn seal_hash_keccak_256(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3711		build_runtime!(runtime, memory: [[0u8; 32], vec![0u8; n as usize], ]);
3712
3713		let result;
3714		#[block]
3715		{
3716			result = runtime.bench_hash_keccak_256(memory.as_mut_slice(), 32, n, 0);
3717		}
3718		assert_eq!(sp_io::hashing::keccak_256(&memory[32..]), &memory[0..32]);
3719		assert_ok!(result);
3720	}
3721
3722	// `n`: Input to hash in bytes
3723	#[benchmark(pov_mode = Measured)]
3724	fn hash_blake2_256(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3725		let input = vec![0u8; n as usize];
3726		let input_bytes = ISystem::ISystemCalls::hashBlake256(ISystem::hashBlake256Call {
3727			input: input.clone().into(),
3728		})
3729		.abi_encode();
3730
3731		let mut call_setup = CallSetup::<T>::default();
3732		let (mut ext, _) = call_setup.ext();
3733
3734		let result;
3735		#[block]
3736		{
3737			result = run_builtin_precompile(
3738				&mut ext,
3739				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
3740				input_bytes,
3741			);
3742		}
3743		let truth: [u8; 32] = sp_io::hashing::blake2_256(&input);
3744		let truth = FixedBytes::<32>::abi_encode(&truth);
3745		let truth = FixedBytes::<32>::abi_decode(&truth[..]).expect("decoding failed");
3746
3747		let raw_data = result.unwrap().data;
3748		let ret_hash = FixedBytes::<32>::abi_decode(&raw_data[..]).expect("decoding failed");
3749		assert_eq!(truth, ret_hash);
3750	}
3751
3752	// `n`: Input to hash in bytes
3753	#[benchmark(pov_mode = Measured)]
3754	fn hash_blake2_128(n: Linear<0, { limits::code::BLOB_BYTES }>) {
3755		let input = vec![0u8; n as usize];
3756		let input_bytes = ISystem::ISystemCalls::hashBlake128(ISystem::hashBlake128Call {
3757			input: input.clone().into(),
3758		})
3759		.abi_encode();
3760
3761		let mut call_setup = CallSetup::<T>::default();
3762		let (mut ext, _) = call_setup.ext();
3763
3764		let result;
3765		#[block]
3766		{
3767			result = run_builtin_precompile(
3768				&mut ext,
3769				H160(BenchmarkSystem::<T>::MATCHER.base_address()).as_fixed_bytes(),
3770				input_bytes,
3771			);
3772		}
3773		let truth: [u8; 16] = sp_io::hashing::blake2_128(&input);
3774		let truth = FixedBytes::<16>::abi_encode(&truth);
3775		let truth = FixedBytes::<16>::abi_decode(&truth[..]).expect("decoding failed");
3776
3777		let raw_data = result.unwrap().data;
3778		let ret_hash = FixedBytes::<16>::abi_decode(&raw_data[..]).expect("decoding failed");
3779		assert_eq!(truth, ret_hash);
3780	}
3781
3782	// `n`: Message input length to verify in bytes.
3783	// need some buffer so the code size does not exceed the max code size.
3784	#[benchmark(pov_mode = Measured)]
3785	fn seal_sr25519_verify(n: Linear<0, { limits::code::BLOB_BYTES - 255 }>) {
3786		let message = (0..n).zip((32u8..127u8).cycle()).map(|(_, c)| c).collect::<Vec<_>>();
3787		let message_len = message.len() as u32;
3788
3789		let key_type = sp_core::crypto::KeyTypeId(*b"code");
3790		let pub_key = sp_io::crypto::sr25519_generate(key_type, None);
3791		let sig =
3792			sp_io::crypto::sr25519_sign(key_type, &pub_key, &message).expect("Generates signature");
3793		let sig = AsRef::<[u8; 64]>::as_ref(&sig).to_vec();
3794		let sig_len = sig.len() as u32;
3795
3796		build_runtime!(runtime, memory: [sig, pub_key.to_vec(), message, ]);
3797
3798		let result;
3799		#[block]
3800		{
3801			result = runtime.bench_sr25519_verify(
3802				memory.as_mut_slice(),
3803				0,                              // signature_ptr
3804				sig_len,                        // pub_key_ptr
3805				message_len,                    // message_len
3806				sig_len + pub_key.len() as u32, // message_ptr
3807			);
3808		}
3809
3810		assert_eq!(result.unwrap(), ReturnErrorCode::Success);
3811	}
3812
3813	#[benchmark(pov_mode = Measured)]
3814	fn ecdsa_recover() {
3815		use hex_literal::hex;
3816		let input = hex!("18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c000000000000000000000000000000000000000000000000000000000000001c73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75feeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549").to_vec();
3817		let expected = hex!("000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b");
3818		let mut call_setup = CallSetup::<T>::default();
3819		let (mut ext, _) = call_setup.ext();
3820
3821		let result;
3822
3823		#[block]
3824		{
3825			result =
3826				run_builtin_precompile(&mut ext, H160::from_low_u64_be(1).as_fixed_bytes(), input);
3827		}
3828
3829		assert_eq!(result.unwrap().data, expected);
3830	}
3831
3832	#[benchmark(pov_mode = Measured)]
3833	fn p256_verify() {
3834		use hex_literal::hex;
3835		let input = hex!("4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e").to_vec();
3836		let expected = U256::one().to_big_endian();
3837		let mut call_setup = CallSetup::<T>::default();
3838		let (mut ext, _) = call_setup.ext();
3839
3840		let result;
3841
3842		#[block]
3843		{
3844			result = run_builtin_precompile(
3845				&mut ext,
3846				H160::from_low_u64_be(0x100).as_fixed_bytes(),
3847				input,
3848			);
3849		}
3850
3851		assert_eq!(result.unwrap().data, expected);
3852	}
3853
3854	#[benchmark(pov_mode = Measured)]
3855	fn bn128_add() {
3856		use hex_literal::hex;
3857		let input = hex!("089142debb13c461f61523586a60732d8b69c5b38a3380a74da7b2961d867dbf2d5fc7bbc013c16d7945f190b232eacc25da675c0eb093fe6b9f1b4b4e107b3625f8c89ea3437f44f8fc8b6bfbb6312074dc6f983809a5e809ff4e1d076dd5850b38c7ced6e4daef9c4347f370d6d8b58f4b1d8dc61a3c59d651a0644a2a27cf").to_vec();
3858		let expected = hex!(
3859			"0a6678fd675aa4d8f0d03a1feb921a27f38ebdcb860cc083653519655acd6d79172fd5b3b2bfdd44e43bcec3eace9347608f9f0a16f1e184cb3f52e6f259cbeb"
3860		);
3861		let mut call_setup = CallSetup::<T>::default();
3862		let (mut ext, _) = call_setup.ext();
3863
3864		let result;
3865		#[block]
3866		{
3867			result =
3868				run_builtin_precompile(&mut ext, H160::from_low_u64_be(6).as_fixed_bytes(), input);
3869		}
3870
3871		assert_eq!(result.unwrap().data, expected);
3872	}
3873
3874	#[benchmark(pov_mode = Measured)]
3875	fn bn128_mul() {
3876		use hex_literal::hex;
3877		let input = hex!("089142debb13c461f61523586a60732d8b69c5b38a3380a74da7b2961d867dbf2d5fc7bbc013c16d7945f190b232eacc25da675c0eb093fe6b9f1b4b4e107b36ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").to_vec();
3878		let expected = hex!(
3879			"0bf982b98a2757878c051bfe7eee228b12bc69274b918f08d9fcb21e9184ddc10b17c77cbf3c19d5d27e18cbd4a8c336afb488d0e92c18d56e64dd4ea5c437e6"
3880		);
3881		let mut call_setup = CallSetup::<T>::default();
3882		let (mut ext, _) = call_setup.ext();
3883
3884		let result;
3885		#[block]
3886		{
3887			result =
3888				run_builtin_precompile(&mut ext, H160::from_low_u64_be(7).as_fixed_bytes(), input);
3889		}
3890
3891		assert_eq!(result.unwrap().data, expected);
3892	}
3893
3894	// `n`: pairings to perform
3895	#[benchmark(pov_mode = Measured)]
3896	fn bn128_pairing(n: Linear<0, { 20 }>) {
3897		fn generate_random_ecpairs(n: usize) -> Vec<u8> {
3898			use bn::{AffineG1, AffineG2, Fr, G1, G2, Group};
3899			use rand::SeedableRng;
3900			use rand_pcg::Pcg64;
3901			let mut rng = Pcg64::seed_from_u64(1);
3902
3903			let mut buffer = vec![0u8; n * 192];
3904
3905			let mut write = |element: &bn::Fq, offset: &mut usize| {
3906				element.to_big_endian(&mut buffer[*offset..*offset + 32]).unwrap();
3907				*offset += 32
3908			};
3909
3910			for i in 0..n {
3911				let mut offset = i * 192;
3912				let scalar = Fr::random(&mut rng);
3913
3914				let g1 = G1::one() * scalar;
3915				let g2 = G2::one() * scalar;
3916				let a = AffineG1::from_jacobian(g1).expect("G1 point should be on curve");
3917				let b = AffineG2::from_jacobian(g2).expect("G2 point should be on curve");
3918
3919				write(&a.x(), &mut offset);
3920				write(&a.y(), &mut offset);
3921				write(&b.x().imaginary(), &mut offset);
3922				write(&b.x().real(), &mut offset);
3923				write(&b.y().imaginary(), &mut offset);
3924				write(&b.y().real(), &mut offset);
3925			}
3926
3927			buffer
3928		}
3929
3930		let input = generate_random_ecpairs(n as usize);
3931		let mut call_setup = CallSetup::<T>::default();
3932		let (mut ext, _) = call_setup.ext();
3933
3934		let result;
3935		#[block]
3936		{
3937			result =
3938				run_builtin_precompile(&mut ext, H160::from_low_u64_be(8).as_fixed_bytes(), input);
3939		}
3940		assert_ok!(result);
3941	}
3942
3943	// `n`: number of rounds to perform
3944	#[benchmark(pov_mode = Measured)]
3945	fn blake2f(n: Linear<0, 1200>) {
3946		use hex_literal::hex;
3947		let input = hex!(
3948			"48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"
3949		);
3950		let input = n.to_be_bytes().to_vec().into_iter().chain(input.to_vec()).collect::<Vec<_>>();
3951		let mut call_setup = CallSetup::<T>::default();
3952		let (mut ext, _) = call_setup.ext();
3953
3954		let result;
3955		#[block]
3956		{
3957			result =
3958				run_builtin_precompile(&mut ext, H160::from_low_u64_be(9).as_fixed_bytes(), input);
3959		}
3960		assert_ok!(result);
3961	}
3962
3963	// Only calling the function itself for the list of
3964	// generated different ECDSA keys.
3965	// This is a slow call: We reduce the number of runs.
3966	#[benchmark(pov_mode = Measured)]
3967	fn seal_ecdsa_to_eth_address() {
3968		let key_type = sp_core::crypto::KeyTypeId(*b"code");
3969		let pub_key_bytes = sp_io::crypto::ecdsa_generate(key_type, None).0;
3970		build_runtime!(runtime, memory: [[0u8; 20], pub_key_bytes,]);
3971
3972		let result;
3973		#[block]
3974		{
3975			result = runtime.bench_ecdsa_to_eth_address(
3976				memory.as_mut_slice(),
3977				20, // key_ptr
3978				0,  // output_ptr
3979			);
3980		}
3981
3982		assert_ok!(result);
3983		assert_eq!(&memory[..20], runtime.ext().ecdsa_to_eth_address(&pub_key_bytes).unwrap());
3984	}
3985
3986	/// Benchmarks `r` EVM `JUMPDEST` op-codes.
3987	///
3988	/// # Subtraction Safety
3989	///
3990	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
3991	#[benchmark(pov_mode = Measured)]
3992	fn evm_jumpdest_opcode(
3993		r: Linear<0, { MAX_INITCODE_SIZE as u32 }>,
3994	) -> Result<(), BenchmarkError> {
3995		let module = VmBinaryModule::evm_noop(r);
3996		let inputs = vec![];
3997
3998		let code = Bytecode::new_raw(revm::primitives::Bytes::from(module.code.clone()));
3999		let mut setup = CallSetup::<T>::new(module);
4000		let (mut ext, _) = setup.ext();
4001
4002		evict_caches();
4003		let result;
4004		#[block]
4005		{
4006			result = evm::call(code, &mut ext, inputs);
4007		}
4008
4009		assert!(result.is_ok());
4010		Ok(())
4011	}
4012
4013	/// Benchmarks `r` EVM `JUMP` op-codes.
4014	///
4015	/// # Added Overheads
4016	///
4017	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4018	/// * A cold destination read on each jump, since each destination is on a different 4 KiB page
4019	///   from the entry point and every other destination.
4020	/// * No help from the pre-fetcher, since the destinations are visited in a pseudo-random order.
4021	///
4022	/// # Subtraction Safety
4023	///
4024	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4025	#[benchmark(pov_mode = Measured)]
4026	fn evm_jump_opcode(r: Linear<1, { MAX_INITCODE_SIZE as u32 / 4096 - 1 }>) {
4027		const PAGE_SIZE: usize = 4096;
4028		const PAGE_COUNT: usize = MAX_INITCODE_SIZE / PAGE_SIZE - 1;
4029
4030		let code = Bytecode::new_raw([JUMP, JUMPDEST].repeat(MAX_INITCODE_SIZE / 2).into());
4031		let last_jumpdest_offset = MAX_INITCODE_SIZE - 1;
4032
4033		// Analysis can reallocate the code, so use its final address. Skip pages that entry
4034		// fallthrough can touch and reserve the last page for the terminal destination.
4035		let address = code.bytecode_ptr() as usize;
4036		let first_page_start = (address + 2 * PAGE_COUNT).next_multiple_of(PAGE_SIZE) - address;
4037		let mut targets = (0..PAGE_COUNT - 1)
4038			.map(|page_index| page_index * PAGE_SIZE + first_page_start)
4039			// Odd offsets are JUMPDESTs; placing them mid-page keeps the next JUMP on that page.
4040			.map(|page_start| (page_start + PAGE_SIZE / 2) | 1)
4041			.collect::<Vec<_>>();
4042		targets.shuffle(&mut Pcg64::seed_from_u64(1337));
4043		targets.truncate(r as usize - 1);
4044		targets.push(last_jumpdest_offset);
4045
4046		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4047		let (mut ext, _) = setup.ext();
4048		let bytecode = ExtBytecode::new(code);
4049		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
4050		setup_stack(&mut interpreter, targets.into_iter().rev().map(U256::from));
4051
4052		evict_caches();
4053		let result;
4054		#[block]
4055		{
4056			result = evm::run_plain(&mut interpreter);
4057		}
4058
4059		let ControlFlow::Break(halt) = result;
4060		assert!(matches!(halt, Halt::Stop));
4061		assert_eq!(interpreter.stack.len(), 0);
4062		assert_eq!(interpreter.bytecode.pc(), last_jumpdest_offset + 2);
4063	}
4064
4065	/// Benchmarks `r` EVM `JUMPI` op-codes.
4066	///
4067	/// # Added Overheads
4068	///
4069	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4070	/// * A branch misprediction on whether each jump is taken, since condition is pseudo-random.
4071	/// * A cold destination read on each taken jump, since each destination is on a different 4 KiB
4072	///   page from the entry point and every other destination.
4073	/// * No help from the pre-fetcher, since the destinations are visited in a pseudo-random order.
4074	/// * No early exit from the zero check of a true condition, since its only nonzero word is the
4075	///   last one checked.
4076	///
4077	/// # Subtraction Safety
4078	///
4079	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4080	#[benchmark(pov_mode = Measured)]
4081	fn evm_jumpi_opcode(r: Linear<1, { MAX_INITCODE_SIZE as u32 / 4096 - 1 }>) {
4082		const PAGE_SIZE: usize = 4096;
4083		const PAGE_COUNT: usize = MAX_INITCODE_SIZE / PAGE_SIZE - 1;
4084
4085		let mut rng = Pcg64::seed_from_u64(1337);
4086
4087		let code = Bytecode::new_raw([JUMPI, JUMPDEST].repeat(MAX_INITCODE_SIZE / 2).into());
4088		let last_jumpdest_offset = MAX_INITCODE_SIZE - 1;
4089
4090		// Analysis can reallocate the code, so use its final address. Skip pages that entry
4091		// fallthrough can touch and reserve the last page for the terminal destination.
4092		let address = code.bytecode_ptr() as usize;
4093		let first_page_start = (address + 2 * PAGE_COUNT).next_multiple_of(PAGE_SIZE) - address;
4094		let mut targets = (0..PAGE_COUNT - 1)
4095			.map(|page_index| page_index * PAGE_SIZE + first_page_start)
4096			// Odd offsets are JUMPDESTs; placing them mid-page keeps fallthrough on that page.
4097			.map(|page_start| (page_start + PAGE_SIZE / 2) | 1)
4098			.collect::<Vec<_>>();
4099		targets.shuffle(&mut rng);
4100		targets.truncate(r as usize - 1);
4101		targets.push(last_jumpdest_offset);
4102
4103		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4104		let (mut ext, _) = setup.ext();
4105		let bytecode = ExtBytecode::new(code);
4106		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
4107		let operands = targets.into_iter().rev().flat_map(|target| {
4108			// The last jump is always taken so that the code ends on the last `JUMPDEST`.
4109			let condition = if target == last_jumpdest_offset || rng.gen_bool(0.5) {
4110				// Constructing a truthy value which requires us to check all of the limbs of the
4111				// 256-bit word.
4112				U256([0, 0, 0, rng.gen_range(1u64..=u64::MAX)])
4113			} else {
4114				U256::zero()
4115			};
4116			[condition, U256::from(target)]
4117		});
4118		setup_stack(&mut interpreter, operands);
4119
4120		evict_caches();
4121		let result;
4122		#[block]
4123		{
4124			result = evm::run_plain(&mut interpreter);
4125		}
4126
4127		let ControlFlow::Break(halt) = result;
4128		assert!(matches!(halt, Halt::Stop));
4129		assert_eq!(interpreter.stack.len(), 0);
4130		assert_eq!(interpreter.bytecode.pc(), last_jumpdest_offset + 2);
4131	}
4132
4133	/// Benchmarks `r` EVM `PC` op-codes.
4134	///
4135	/// # Subtraction Safety
4136	///
4137	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4138	#[benchmark(pov_mode = Measured)]
4139	fn evm_pc_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4140		let code = Bytecode::new_raw(vec![PC; r as usize].into());
4141		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4142		let (mut ext, _) = setup.ext();
4143		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4144
4145		evict_caches();
4146		let result;
4147		#[block]
4148		{
4149			result = evm::run_plain(&mut interpreter);
4150		}
4151
4152		let ControlFlow::Break(halt) = result;
4153		assert!(matches!(halt, Halt::Stop));
4154		assert_eq!(interpreter.stack.len(), r as usize);
4155		assert_eq!(interpreter.stack.top(), r.checked_sub(1).map(U256::from).as_ref());
4156	}
4157
4158	/// Benchmarks `r` EVM `PUSH0` to `PUSH32` op-codes.
4159	///
4160	/// # Added Overheads
4161	///
4162	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4163	/// * A misprediction of the handler the interpreter dispatches to, since the push op-codes are
4164	///   pseudo-random.
4165	///
4166	/// # Subtraction Safety
4167	///
4168	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4169	#[benchmark(pov_mode = Measured)]
4170	fn evm_push_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4171		let mut rng = Pcg64::seed_from_u64(1337);
4172
4173		let code = (0..r)
4174			.flat_map(|_| {
4175				let width = rng.gen_range(0..=32u8);
4176				core::iter::once(PUSH0 + width)
4177					.chain(core::iter::repeat_n(u8::MAX, usize::from(width)))
4178			})
4179			.collect::<Vec<u8>>();
4180
4181		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4182		let (mut ext, _) = setup.ext();
4183		let bytecode = ExtBytecode::new(Bytecode::new_raw(code.into()));
4184		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
4185
4186		evict_caches();
4187		let result;
4188		#[block]
4189		{
4190			result = evm::run_plain(&mut interpreter);
4191		}
4192
4193		let ControlFlow::Break(halt) = result;
4194		assert!(matches!(halt, Halt::Stop));
4195		assert_eq!(interpreter.stack.len(), r as usize);
4196	}
4197
4198	/// Benchmarks `r` EVM `POP` op-codes.
4199	///
4200	/// # Subtraction Safety
4201	///
4202	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4203	#[benchmark(pov_mode = Measured)]
4204	fn evm_pop_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4205		let code = Bytecode::new_raw(vec![POP; r as usize].into());
4206		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4207		let (mut ext, _) = setup.ext();
4208		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4209		setup_stack(&mut interpreter, vec![U256::MAX; r as usize]);
4210
4211		evict_caches();
4212		let result;
4213		#[block]
4214		{
4215			result = evm::run_plain(&mut interpreter);
4216		}
4217
4218		let ControlFlow::Break(halt) = result;
4219		assert!(matches!(halt, Halt::Stop));
4220		assert_eq!(interpreter.stack.len(), 0);
4221	}
4222
4223	/// Benchmarks `r` EVM `DUP1` to `DUP16` op-codes.
4224	///
4225	/// # Added Overheads
4226	///
4227	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4228	/// * A misprediction of the handler the interpreter dispatches to, since the duplication
4229	///   op-codes are pseudo-random.
4230	///
4231	/// # Subtraction Safety
4232	///
4233	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4234	#[benchmark(pov_mode = Measured)]
4235	fn evm_dup_opcode(r: Linear<0, { EVM_STACK_LIMIT - 16 }>) {
4236		let mut rng = Pcg64::seed_from_u64(1337);
4237
4238		let code = (0..r).map(|_| rng.gen_range(DUP1..=DUP16)).collect::<Vec<u8>>();
4239
4240		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4241		let (mut ext, _) = setup.ext();
4242		let bytecode = ExtBytecode::new(Bytecode::new_raw(code.into()));
4243		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
4244		setup_stack(&mut interpreter, vec![U256::MAX; 16]);
4245
4246		evict_caches();
4247		let result;
4248		#[block]
4249		{
4250			result = evm::run_plain(&mut interpreter);
4251		}
4252
4253		let ControlFlow::Break(halt) = result;
4254		assert!(matches!(halt, Halt::Stop));
4255		assert_eq!(interpreter.stack.len(), 16 + r as usize);
4256	}
4257
4258	/// Benchmarks `r` EVM `SWAP1` to `SWAP16` op-codes.
4259	///
4260	/// # Added Overheads
4261	///
4262	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4263	/// * A misprediction of the handler the interpreter dispatches to, since the swap op-codes are
4264	///   pseudo-random.
4265	///
4266	/// # Subtraction Safety
4267	///
4268	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4269	#[benchmark(pov_mode = Measured)]
4270	fn evm_swap_opcode(r: Linear<0, { MAX_INITCODE_SIZE as u32 }>) {
4271		let mut rng = Pcg64::seed_from_u64(1337);
4272
4273		let code = (0..r).map(|_| rng.gen_range(SWAP1..=SWAP16)).collect::<Vec<u8>>();
4274
4275		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4276		let (mut ext, _) = setup.ext();
4277		let bytecode = ExtBytecode::new(Bytecode::new_raw(code.into()));
4278		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
4279		setup_stack(&mut interpreter, vec![U256::MAX; 17]);
4280
4281		evict_caches();
4282		let result;
4283		#[block]
4284		{
4285			result = evm::run_plain(&mut interpreter);
4286		}
4287
4288		let ControlFlow::Break(halt) = result;
4289		assert!(matches!(halt, Halt::Stop));
4290		assert_eq!(interpreter.stack.len(), 17);
4291	}
4292
4293	/// Benchmarks `r` EVM `CHAINID` op-codes.
4294	///
4295	/// # Subtraction Safety
4296	///
4297	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4298	#[benchmark(pov_mode = Measured)]
4299	fn evm_chainid_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4300		let code = Bytecode::new_raw(vec![CHAINID; r as usize].into());
4301		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4302		let (mut ext, _) = setup.ext();
4303		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4304
4305		evict_caches();
4306		let result;
4307		#[block]
4308		{
4309			result = evm::run_plain(&mut interpreter);
4310		}
4311
4312		let ControlFlow::Break(halt) = result;
4313		assert!(matches!(halt, Halt::Stop));
4314		assert_eq!(interpreter.stack.len(), r as usize);
4315		let chain_id = U256::from(T::ChainId::get());
4316		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&chain_id));
4317	}
4318
4319	/// Benchmarks `r` EVM `DIFFICULTY` op-codes.
4320	///
4321	/// # Subtraction Safety
4322	///
4323	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4324	#[benchmark(pov_mode = Measured)]
4325	fn evm_difficulty_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4326		let code = Bytecode::new_raw(vec![DIFFICULTY; r as usize].into());
4327		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4328		let (mut ext, _) = setup.ext();
4329		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4330
4331		evict_caches();
4332		let result;
4333		#[block]
4334		{
4335			result = evm::run_plain(&mut interpreter);
4336		}
4337
4338		let ControlFlow::Break(halt) = result;
4339		assert!(matches!(halt, Halt::Stop));
4340		assert_eq!(interpreter.stack.len(), r as usize);
4341		let difficulty = U256::from(evm::DIFFICULTY);
4342		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&difficulty));
4343	}
4344
4345	/// Benchmarks `r` EVM `CODESIZE` op-codes.
4346	///
4347	/// # Subtraction Safety
4348	///
4349	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4350	#[benchmark(pov_mode = Measured)]
4351	fn evm_codesize_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4352		let code = Bytecode::new_raw(vec![CODESIZE; r as usize].into());
4353		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4354		let (mut ext, _) = setup.ext();
4355		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4356
4357		evict_caches();
4358		let result;
4359		#[block]
4360		{
4361			result = evm::run_plain(&mut interpreter);
4362		}
4363
4364		let ControlFlow::Break(halt) = result;
4365		assert!(matches!(halt, Halt::Stop));
4366		assert_eq!(interpreter.stack.len(), r as usize);
4367		let code_size = U256::from(r);
4368		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&code_size));
4369	}
4370
4371	/// Benchmarks `r` EVM `CALLDATASIZE` op-codes.
4372	///
4373	/// # Subtraction Safety
4374	///
4375	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4376	#[benchmark(pov_mode = Measured)]
4377	fn evm_calldatasize_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4378		let code = Bytecode::new_raw(vec![CALLDATASIZE; r as usize].into());
4379		let input = vec![0u8; CALLDATA_BYTES as usize];
4380		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4381		let (mut ext, _) = setup.ext();
4382		let mut interpreter = Interpreter::new(ExtBytecode::new(code), input, &mut ext);
4383
4384		evict_caches();
4385		let result;
4386		#[block]
4387		{
4388			result = evm::run_plain(&mut interpreter);
4389		}
4390
4391		let ControlFlow::Break(halt) = result;
4392		assert!(matches!(halt, Halt::Stop));
4393		assert_eq!(interpreter.stack.len(), r as usize);
4394		let calldata_size = U256::from(CALLDATA_BYTES);
4395		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&calldata_size));
4396	}
4397
4398	/// Benchmarks `r` EVM `RETURNDATASIZE` op-codes.
4399	///
4400	/// # Subtraction Safety
4401	///
4402	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4403	#[benchmark(pov_mode = Measured)]
4404	fn evm_returndatasize_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4405		let code = Bytecode::new_raw(vec![RETURNDATASIZE; r as usize].into());
4406		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4407		let (mut ext, _) = setup.ext();
4408		*ext.last_frame_output_mut() =
4409			ExecReturnValue { data: vec![42; CALLDATA_BYTES as usize], ..Default::default() };
4410		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4411
4412		evict_caches();
4413		let result;
4414		#[block]
4415		{
4416			result = evm::run_plain(&mut interpreter);
4417		}
4418
4419		let ControlFlow::Break(halt) = result;
4420		assert!(matches!(halt, Halt::Stop));
4421		assert_eq!(interpreter.stack.len(), r as usize);
4422		let return_data_size = U256::from(CALLDATA_BYTES);
4423		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&return_data_size));
4424	}
4425
4426	/// Benchmarks `r` EVM `CALLDATALOAD` op-codes.
4427	///
4428	/// # Added Overheads
4429	///
4430	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4431	/// * An L1 and L2 cache miss on each load, since each word is on a cache line that hasn't been
4432	///   read since the eviction.
4433	/// * A second cache line read on each load, since each word crosses into the next line.
4434	/// * A TLB miss on each load, since each word is on a page that hasn't been read since the
4435	///   eviction.
4436	/// * No help from the pre-fetcher, since the pages are visited in a pseudo-random order.
4437	/// * The runtime's `memcpy` rebuilding everything it copies with shifts, since each word starts
4438	///   at a misaligned address.
4439	///
4440	/// # Subtraction Safety
4441	///
4442	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4443	#[benchmark(pov_mode = Measured)]
4444	fn evm_calldataload_opcode(r: Linear<0, { CALLDATA_BYTES / 4096 - 1 }>) {
4445		const CALLDATA_SIZE: usize = CALLDATA_BYTES as usize;
4446		const PAGE_SIZE: usize = 4096;
4447		const WORD_SIZE: usize = 32;
4448		const PAGE_COUNT: usize = CALLDATA_SIZE / PAGE_SIZE - 1;
4449		// Mid-page and not 4-byte aligned, so the runtime's `memcpy` copies it with shifts.
4450		const OFFSET_IN_PAGE: usize = PAGE_SIZE / 2 + 49;
4451		const END_OF_WALK: U256 = U256::MAX;
4452
4453		let mut rng = Pcg64::seed_from_u64(1337);
4454
4455		let load_count = r as usize;
4456		let mut calldata = vec![0u8; CALLDATA_SIZE];
4457
4458		// The allocator only aligns to 8 bytes, so the calldata can start anywhere within a page.
4459		// Words are placed relative to real page boundaries, not to the start of the calldata.
4460		let address = calldata.as_ptr() as usize;
4461		let first_page_start = address.next_multiple_of(PAGE_SIZE) - address;
4462
4463		let mut offsets = (0..PAGE_COUNT)
4464			.map(|page_index| page_index * PAGE_SIZE)
4465			.map(|page_offset| page_offset + first_page_start)
4466			.map(|page_start| page_start + OFFSET_IN_PAGE)
4467			.collect::<Vec<_>>();
4468		offsets.shuffle(&mut rng);
4469		let walk = &offsets[..load_count];
4470
4471		// Each word in the walk stores the offset of the next one, and the last one stores
4472		// END_OF_WALK.
4473		let next_values = walk.iter().skip(1).map(|&next| U256::from(next)).chain([END_OF_WALK]);
4474		for (&offset, value) in walk.iter().zip(next_values) {
4475			calldata[offset..offset + WORD_SIZE].copy_from_slice(&value.to_big_endian());
4476		}
4477
4478		// With zero loads nothing replaces the stack item, so it has to start as END_OF_WALK for
4479		// the final assertion to hold.
4480		let initial_stack_value = walk.first().map_or(END_OF_WALK, |&first| U256::from(first));
4481
4482		let code = Bytecode::new_raw(vec![CALLDATALOAD; load_count].into());
4483		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4484		let (mut ext, _) = setup.ext();
4485		let mut interpreter = Interpreter::new(ExtBytecode::new(code), calldata, &mut ext);
4486		setup_stack(&mut interpreter, [initial_stack_value]);
4487
4488		evict_caches();
4489		let result;
4490		#[block]
4491		{
4492			result = evm::run_plain(&mut interpreter);
4493		}
4494
4495		let ControlFlow::Break(halt) = result;
4496		assert!(matches!(halt, Halt::Stop));
4497		assert_eq!(interpreter.stack.len(), 1);
4498		assert_eq!(interpreter.stack.top(), Some(&END_OF_WALK));
4499	}
4500
4501	/// Benchmarks `r` EVM `CALLDATACOPY` op-codes that copy one byte into expanded memory.
4502	///
4503	/// This is the fixed cost of a `CALLDATACOPY`, which it pays at any length.
4504	/// The non-zero copy includes the offset conversions and memory bounds check.
4505	///
4506	/// # Subtraction Safety
4507	///
4508	/// Not safe to subtract: a zero-length copy skips part of the measured work.
4509	/// See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4510	#[benchmark(pov_mode = Measured)]
4511	fn evm_calldatacopy_opcode(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
4512		let code = Bytecode::new_raw(vec![CALLDATACOPY; r as usize].into());
4513		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4514		let (mut ext, _) = setup.ext();
4515		let calldata = vec![42; CALLDATA_BYTES as usize];
4516		let mut interpreter = Interpreter::new(ExtBytecode::new(code), calldata, &mut ext);
4517		interpreter
4518			.memory
4519			.resize(0, 32, interpreter.ext)
4520			.continue_value()
4521			.expect("the benchmark memory fits within the limit");
4522		interpreter.memory.slice_mut(0, 32).fill(0xa5);
4523		// Each copy writes one byte from call data to the last byte of expanded memory.
4524		setup_stack(
4525			&mut interpreter,
4526			(0..r).flat_map(|_| [U256::one(), U256::zero(), U256::from(31)]),
4527		);
4528
4529		evict_caches();
4530		let result;
4531		#[block]
4532		{
4533			result = evm::run_plain(&mut interpreter);
4534		}
4535
4536		let ControlFlow::Break(halt) = result;
4537		assert!(matches!(halt, Halt::Stop));
4538		assert_eq!(interpreter.stack.len(), 0);
4539		assert_eq!(interpreter.memory.size(), 32);
4540		let mut expected = [0xa5; 32];
4541		if r > 0 {
4542			expected[31] = 42;
4543		}
4544		assert_eq!(interpreter.memory.slice(0..32), &expected);
4545	}
4546
4547	/// Benchmarks `r` EVM `CODECOPY` op-codes that copy one byte into expanded memory.
4548	///
4549	/// This is the fixed cost of a `CODECOPY`, which it pays at any length.
4550	/// The non-zero copy includes the offset conversions and memory bounds check.
4551	///
4552	/// # Subtraction Safety
4553	///
4554	/// Not safe to subtract: a zero-length copy skips part of the measured work.
4555	/// See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4556	#[benchmark(pov_mode = Measured)]
4557	fn evm_codecopy_opcode(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
4558		let mut code = vec![CODECOPY; r as usize];
4559		code.extend_from_slice(&[STOP, 42]);
4560		let code = Bytecode::new_raw(code.into());
4561		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4562		let (mut ext, _) = setup.ext();
4563		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4564		interpreter
4565			.memory
4566			.resize(0, 32, interpreter.ext)
4567			.continue_value()
4568			.expect("the benchmark memory fits within the limit");
4569		interpreter.memory.slice_mut(0, 32).fill(0xa5);
4570		// The source byte follows STOP, so it is copied without being executed.
4571		setup_stack(
4572			&mut interpreter,
4573			(0..r).flat_map(|_| [U256::one(), U256::from(r + 1), U256::from(31)]),
4574		);
4575
4576		evict_caches();
4577		let result;
4578		#[block]
4579		{
4580			result = evm::run_plain(&mut interpreter);
4581		}
4582
4583		let ControlFlow::Break(halt) = result;
4584		assert!(matches!(halt, Halt::Stop));
4585		assert_eq!(interpreter.stack.len(), 0);
4586		assert_eq!(interpreter.memory.size(), 32);
4587		let mut expected = [0xa5; 32];
4588		if r > 0 {
4589			expected[31] = 42;
4590		}
4591		assert_eq!(interpreter.memory.slice(0..32), &expected);
4592	}
4593
4594	/// Benchmarks `r` EVM `RETURNDATACOPY` op-codes that copy one byte into expanded memory.
4595	///
4596	/// This is the fixed cost of a `RETURNDATACOPY`, which it pays at any length.
4597	/// The non-zero copy includes the return data bounds check and offset conversions.
4598	///
4599	/// # Subtraction Safety
4600	///
4601	/// Not safe to subtract: a zero-length copy skips part of the measured work.
4602	/// See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4603	#[benchmark(pov_mode = Measured)]
4604	fn evm_returndatacopy_opcode(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
4605		let code = Bytecode::new_raw(vec![RETURNDATACOPY; r as usize].into());
4606		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4607		let (mut ext, _) = setup.ext();
4608		*ext.last_frame_output_mut() =
4609			ExecReturnValue { data: vec![42; CALLDATA_BYTES as usize], ..Default::default() };
4610		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4611		interpreter
4612			.memory
4613			.resize(0, 32, interpreter.ext)
4614			.continue_value()
4615			.expect("the benchmark memory fits within the limit");
4616		interpreter.memory.slice_mut(0, 32).fill(0xa5);
4617		setup_stack(
4618			&mut interpreter,
4619			(0..r).flat_map(|_| [U256::one(), U256::zero(), U256::from(31)]),
4620		);
4621
4622		evict_caches();
4623		let result;
4624		#[block]
4625		{
4626			result = evm::run_plain(&mut interpreter);
4627		}
4628
4629		let ControlFlow::Break(halt) = result;
4630		assert!(matches!(halt, Halt::Stop));
4631		assert_eq!(interpreter.stack.len(), 0);
4632		assert_eq!(interpreter.memory.size(), 32);
4633		let mut expected = [0xa5; 32];
4634		if r > 0 {
4635			expected[31] = 42;
4636		}
4637		assert_eq!(interpreter.memory.slice(0..32), &expected);
4638	}
4639
4640	/// Benchmarks `Memory::grow`, which only zeroes newly exposed bytes.
4641	///
4642	/// Setup grows past half the memory limit, obtaining the final capacity through the normal
4643	/// resize path. `n` counts added 32-byte words; the measured growth cannot reallocate.
4644	#[benchmark(pov_mode = Measured)]
4645	fn evm_memory_grow(n: Linear<1, { EVM_MEMORY_BYTES / 64 - 1 }>) {
4646		const INITIAL_WORDS: u32 = EVM_MEMORY_BYTES / 64 + 1;
4647		let initial_len = INITIAL_WORDS as usize * 32;
4648		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4649		let (mut ext, _) = setup.ext();
4650		let mut interpreter = Interpreter::new(Default::default(), Vec::new(), &mut ext);
4651		let memory = &mut interpreter.memory;
4652		memory.resize(0, initial_len, interpreter.ext).continue_value().unwrap();
4653		memory.slice_mut(0, initial_len).fill(0xa5);
4654		let target_words = INITIAL_WORDS + n;
4655		let target_len = target_words as usize * 32;
4656
4657		evict_caches();
4658		let result;
4659		#[block]
4660		{
4661			result = core::hint::black_box(&mut *memory).bench_grow(target_words);
4662		}
4663
4664		assert!(result.is_continue());
4665		assert_eq!(memory.size(), target_len);
4666		assert!(memory.slice(0..initial_len).iter().all(|&byte| byte == 0xa5));
4667		assert!(memory.slice(initial_len..target_len).iter().all(|&byte| byte == 0));
4668	}
4669
4670	/// Benchmarks `Memory::reallocate`: allocation, copying live bytes, and freeing the old buffer.
4671	///
4672	/// `n` counts requested capacity in 32-byte words. Production capacity at least doubles, so
4673	/// setup fills half the replacement capacity to cover the largest production copy. The
4674	/// measured operation preserves the logical size and does not zero newly exposed memory.
4675	#[benchmark(pov_mode = Measured)]
4676	fn evm_memory_reallocate(n: Linear<256, { EVM_MEMORY_BYTES / 32 }>) {
4677		let initial_len = (n / 2) as usize * 32;
4678		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4679		let (mut ext, _) = setup.ext();
4680		let mut interpreter = Interpreter::new(Default::default(), Vec::new(), &mut ext);
4681		let memory = &mut interpreter.memory;
4682		memory.resize(0, initial_len, interpreter.ext).continue_value().unwrap();
4683		memory.slice_mut(0, initial_len).fill(0xa5);
4684
4685		evict_caches();
4686		let result;
4687		#[block]
4688		{
4689			result = core::hint::black_box(&mut *memory).bench_reallocate(n);
4690		}
4691
4692		assert!(result.is_continue());
4693		assert_eq!(memory.size(), initial_len);
4694		assert!(memory.slice(0..initial_len).iter().all(|&byte| byte == 0xa5));
4695	}
4696
4697	/// Benchmarks allocating and copying `n` bytes out of EVM memory for RETURN and REVERT.
4698	///
4699	/// Setup fills the entire memory limit with seeded pseudo-random bytes. Growth and data
4700	/// initialization are outside the measured block; the output buffer is retained afterwards.
4701	#[benchmark(pov_mode = Measured)]
4702	fn evm_memory_copy_out(n: Linear<0, { EVM_MEMORY_BYTES }>) {
4703		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4704		let (mut ext, _) = setup.ext();
4705		let mut interpreter = Interpreter::new(Default::default(), Vec::new(), &mut ext);
4706		let memory = &mut interpreter.memory;
4707		memory
4708			.resize(0, EVM_MEMORY_BYTES as usize, interpreter.ext)
4709			.continue_value()
4710			.expect("the memory limit fits the setup weight budget; qed");
4711		Pcg64::seed_from_u64(1337).fill(memory.slice_mut(0, EVM_MEMORY_BYTES as usize));
4712
4713		evict_caches();
4714		let result;
4715		#[block]
4716		{
4717			result = core::hint::black_box(&*memory).bench_copy_out(0, n as usize);
4718		}
4719
4720		assert_eq!(result.len(), n as usize);
4721		assert_eq!(result.as_slice(), memory.slice_len(0, n as usize));
4722	}
4723
4724	/// Benchmarks `r` EVM `MLOAD` op-codes.
4725	///
4726	/// This uses a linked-list scheme similar to the one in [`evm_calldataload_opcode`].
4727	///
4728	/// # Added Overheads
4729	///
4730	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4731	/// * An L1 and L2 cache miss on each load, since each word is on a cache line that hasn't been
4732	///   read since the eviction.
4733	/// * A second cache line read on each load, since each word crosses into the next line.
4734	/// * A TLB miss on each load, since each word is on a page that hasn't been read since the
4735	///   eviction.
4736	/// * No help from the pre-fetcher, since the pages are visited in a pseudo-random order.
4737	///
4738	/// # Subtraction Safety
4739	///
4740	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4741	#[benchmark(pov_mode = Measured)]
4742	fn evm_mload_opcode(r: Linear<0, { EVM_MEMORY_BYTES / 4096 - 1 }>) {
4743		const MEMORY_SIZE: usize = EVM_MEMORY_BYTES as usize;
4744		const PAGE_SIZE: usize = 4096;
4745		const WORD_SIZE: usize = 32;
4746		const PAGE_COUNT: usize = MEMORY_SIZE / PAGE_SIZE - 1;
4747		// Mid-page and 36 bytes into a cache line, so the word runs into the next line.
4748		const OFFSET_IN_PAGE: usize = PAGE_SIZE / 2 + 36;
4749		const END_OF_WALK: U256 = U256::MAX;
4750
4751		let mut rng = Pcg64::seed_from_u64(1337);
4752
4753		let load_count = r as usize;
4754		let code = Bytecode::new_raw(vec![MLOAD; load_count].into());
4755		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4756		let (mut ext, _) = setup.ext();
4757		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4758		interpreter
4759			.memory
4760			.resize(0, MEMORY_SIZE, interpreter.ext)
4761			.continue_value()
4762			.unwrap();
4763		let memory = interpreter.memory.slice_mut(0, MEMORY_SIZE);
4764
4765		// The allocator only aligns to 8 bytes, so memory can start anywhere within a page. Words
4766		// are placed relative to real page boundaries, not to the start of memory.
4767		let address = memory.as_ptr() as usize;
4768		let first_page_start = address.next_multiple_of(PAGE_SIZE) - address;
4769
4770		let mut offsets = (0..PAGE_COUNT)
4771			.map(|page_index| page_index * PAGE_SIZE)
4772			.map(|page_offset| page_offset + first_page_start)
4773			.map(|page_start| page_start + OFFSET_IN_PAGE)
4774			.collect::<Vec<_>>();
4775		offsets.shuffle(&mut rng);
4776		let walk = &offsets[..load_count];
4777
4778		// Each word in the walk stores the offset of the next one, and the last one stores
4779		// END_OF_WALK.
4780		let next_values = walk.iter().skip(1).map(|&next| U256::from(next)).chain([END_OF_WALK]);
4781		for (&offset, value) in walk.iter().zip(next_values) {
4782			memory[offset..offset + WORD_SIZE].copy_from_slice(&value.to_big_endian());
4783		}
4784
4785		// With zero loads nothing replaces the stack item, so it has to start as END_OF_WALK for
4786		// the final assertion to hold.
4787		let initial_stack_value = walk.first().map_or(END_OF_WALK, |&first| U256::from(first));
4788		setup_stack(&mut interpreter, [initial_stack_value]);
4789
4790		evict_caches();
4791		let result;
4792		#[block]
4793		{
4794			result = evm::run_plain(&mut interpreter);
4795		}
4796
4797		let ControlFlow::Break(halt) = result;
4798		assert!(matches!(halt, Halt::Stop));
4799		assert_eq!(interpreter.stack.len(), 1);
4800		assert_eq!(interpreter.stack.top(), Some(&END_OF_WALK));
4801		assert_eq!(interpreter.memory.size(), MEMORY_SIZE);
4802	}
4803
4804	/// Benchmarks `r` EVM `MSIZE` op-codes.
4805	///
4806	/// # Subtraction Safety
4807	///
4808	/// Safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4809	#[benchmark(pov_mode = Measured)]
4810	fn evm_msize_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
4811		let code = Bytecode::new_raw(vec![MSIZE; r as usize].into());
4812		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4813		let (mut ext, _) = setup.ext();
4814		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4815		interpreter
4816			.memory
4817			.resize(0, EVM_MEMORY_BYTES as usize, interpreter.ext)
4818			.continue_value()
4819			.unwrap();
4820
4821		evict_caches();
4822		let result;
4823		#[block]
4824		{
4825			result = evm::run_plain(&mut interpreter);
4826		}
4827
4828		let ControlFlow::Break(halt) = result;
4829		assert!(matches!(halt, Halt::Stop));
4830		assert_eq!(interpreter.stack.len(), r as usize);
4831		let memory_size = U256::from(EVM_MEMORY_BYTES);
4832		assert_eq!(interpreter.stack.top(), (r > 0).then_some(&memory_size));
4833	}
4834
4835	/// Benchmarks `r` EVM `MSTORE` op-codes.
4836	///
4837	/// # Added Overheads
4838	///
4839	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4840	/// * An L1 and L2 cache miss on each store, since each word is on a cache line that hasn't been
4841	///   accessed since the eviction.
4842	/// * A second cache line written on each store, since each word crosses into the next line.
4843	/// * A TLB miss on each store, since each word is on a page that hasn't been accessed since the
4844	///   eviction.
4845	/// * No help from the pre-fetcher, since the pages are visited in a pseudo-random order.
4846	///
4847	/// # Subtraction Safety
4848	///
4849	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4850	#[benchmark(pov_mode = Measured)]
4851	fn evm_mstore_opcode(r: Linear<0, { EVM_MEMORY_BYTES / 4096 - 1 }>) {
4852		const MEMORY_SIZE: usize = EVM_MEMORY_BYTES as usize;
4853		const PAGE_SIZE: usize = 4096;
4854		const WORD_SIZE: usize = 32;
4855		const PAGE_COUNT: usize = MEMORY_SIZE / PAGE_SIZE - 1;
4856		// Mid-page and 36 bytes into a cache line, so the word runs into the next line.
4857		const OFFSET_IN_PAGE: usize = PAGE_SIZE / 2 + 36;
4858
4859		let mut rng = Pcg64::seed_from_u64(1337);
4860
4861		let code = Bytecode::new_raw(vec![MSTORE; r as usize].into());
4862		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4863		let (mut ext, _) = setup.ext();
4864		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4865		interpreter
4866			.memory
4867			.resize(0, MEMORY_SIZE, interpreter.ext)
4868			.continue_value()
4869			.unwrap();
4870
4871		// The allocator only aligns to 8 bytes, so memory can start anywhere within a page. Words
4872		// are placed relative to real page boundaries, not to the start of memory.
4873		let address = interpreter.memory.slice(0..MEMORY_SIZE).as_ptr() as usize;
4874		let first_page_start = address.next_multiple_of(PAGE_SIZE) - address;
4875
4876		let mut offsets = (0..PAGE_COUNT)
4877			.map(|page_index| page_index * PAGE_SIZE)
4878			.map(|page_offset| page_offset + first_page_start)
4879			.map(|page_start| page_start + OFFSET_IN_PAGE)
4880			.collect::<Vec<_>>();
4881		offsets.shuffle(&mut rng);
4882		let stores = &offsets[..r as usize];
4883		setup_stack(
4884			&mut interpreter,
4885			stores.iter().flat_map(|&offset| [U256::MAX, U256::from(offset)]),
4886		);
4887
4888		evict_caches();
4889		let result;
4890		#[block]
4891		{
4892			result = evm::run_plain(&mut interpreter);
4893		}
4894
4895		let ControlFlow::Break(halt) = result;
4896		assert!(matches!(halt, Halt::Stop));
4897		assert_eq!(interpreter.stack.len(), 0);
4898		assert_eq!(interpreter.memory.size(), MEMORY_SIZE);
4899		for &offset in stores {
4900			assert_eq!(interpreter.memory.slice_len(offset, WORD_SIZE), &[0xff; WORD_SIZE]);
4901			assert_eq!(interpreter.memory.slice_len(offset - 1, 1), &[0]);
4902			assert_eq!(interpreter.memory.slice_len(offset + WORD_SIZE, 1), &[0]);
4903		}
4904	}
4905
4906	/// Benchmarks `r` EVM `MSTORE8` op-codes.
4907	///
4908	/// # Added Overheads
4909	///
4910	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4911	/// * An L1 and L2 cache miss on each store, since each byte is on a cache line that hasn't been
4912	///   accessed since the eviction.
4913	/// * A TLB miss on each store, since each byte is on a page that hasn't been accessed since the
4914	///   eviction.
4915	/// * No help from the pre-fetcher, since the pages are visited in a pseudo-random order.
4916	///
4917	/// # Subtraction Safety
4918	///
4919	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
4920	#[benchmark(pov_mode = Measured)]
4921	fn evm_mstore8_opcode(r: Linear<0, { EVM_MEMORY_BYTES / 4096 - 1 }>) {
4922		const MEMORY_SIZE: usize = EVM_MEMORY_BYTES as usize;
4923		const PAGE_SIZE: usize = 4096;
4924		const PAGE_COUNT: usize = MEMORY_SIZE / PAGE_SIZE - 1;
4925		// A single byte can't straddle a cache line, so where it sits in the page doesn't matter.
4926		const OFFSET_IN_PAGE: usize = PAGE_SIZE / 2;
4927
4928		let mut rng = Pcg64::seed_from_u64(1337);
4929
4930		let code = Bytecode::new_raw(vec![MSTORE8; r as usize].into());
4931		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
4932		let (mut ext, _) = setup.ext();
4933		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
4934		interpreter
4935			.memory
4936			.resize(0, MEMORY_SIZE, interpreter.ext)
4937			.continue_value()
4938			.unwrap();
4939
4940		// The allocator only aligns to 8 bytes, so memory can start anywhere within a page. Bytes
4941		// are placed relative to real page boundaries, not to the start of memory.
4942		let address = interpreter.memory.slice(0..MEMORY_SIZE).as_ptr() as usize;
4943		let first_page_start = address.next_multiple_of(PAGE_SIZE) - address;
4944
4945		let mut offsets = (0..PAGE_COUNT)
4946			.map(|page_index| page_index * PAGE_SIZE)
4947			.map(|page_offset| page_offset + first_page_start)
4948			.map(|page_start| page_start + OFFSET_IN_PAGE)
4949			.collect::<Vec<_>>();
4950		offsets.shuffle(&mut rng);
4951		let stores = &offsets[..r as usize];
4952		setup_stack(
4953			&mut interpreter,
4954			stores.iter().flat_map(|&offset| [U256::MAX, U256::from(offset)]),
4955		);
4956
4957		evict_caches();
4958		let result;
4959		#[block]
4960		{
4961			result = evm::run_plain(&mut interpreter);
4962		}
4963
4964		let ControlFlow::Break(halt) = result;
4965		assert!(matches!(halt, Halt::Stop));
4966		assert_eq!(interpreter.stack.len(), 0);
4967		assert_eq!(interpreter.memory.size(), MEMORY_SIZE);
4968		for &offset in stores {
4969			assert_eq!(interpreter.memory.slice_len(offset, 1), &[0xff]);
4970			assert_eq!(interpreter.memory.slice_len(offset - 1, 1), &[0]);
4971			assert_eq!(interpreter.memory.slice_len(offset + 1, 1), &[0]);
4972		}
4973	}
4974
4975	/// Benchmarks `r` EVM `MCOPY` op-codes that copy up to 64 bytes each.
4976	///
4977	/// This is the fixed cost of an `MCOPY`, which covers copying its first 64 bytes. Longer copies
4978	/// pay for every extra byte on top of it.
4979	///
4980	/// # Added Overheads
4981	///
4982	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
4983	/// * An L1 and L2 cache miss on the source and on the destination of each copy, since each of
4984	///   them is on a cache line that hasn't been accessed since the eviction.
4985	/// * A second cache line accessed by the source and by the destination of each copy, since each
4986	///   of them crosses into the next line.
4987	/// * A TLB miss on the source and on the destination of each copy, since each of them is on a
4988	///   page that hasn't been accessed since the eviction.
4989	/// * A second TLB miss on the source and on the destination of each copy, since each of them
4990	///   crosses into the next page.
4991	/// * A read of each destination line before the copy writes to it, since the line isn't in the
4992	///   cache.
4993	/// * No help from the pre-fetcher, since the pages are visited in a pseudo-random order.
4994	/// * The path of the runtime's `memmove` that rebuilds everything it copies with shifts, since
4995	///   the source and the destination are misaligned with each other.
4996	/// * A branch misprediction on how `memmove` copies the bytes before and after its words, since
4997	///   the alignments of the source and the destination and the length are pseudo-random.
4998	///
4999	/// # Subtraction Safety
5000	///
5001	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5002	#[benchmark(pov_mode = Measured)]
5003	fn evm_mcopy_opcode(r: Linear<0, { EVM_MEMORY_BYTES / 16384 - 1 }>) {
5004		const MEMORY_SIZE: usize = EVM_MEMORY_BYTES as usize;
5005		const PAGE_SIZE: usize = 4096;
5006		const MAX_COPY_BYTES: usize = 64;
5007		const PAIR_SIZE: usize = 2 * PAGE_SIZE;
5008		const PAIR_COUNT: usize = MEMORY_SIZE / PAIR_SIZE - 1;
5009		// Sources and destinations start up to three bytes after this, so each of them crosses the
5010		// boundary between the two pages of its pair.
5011		const REGION_IN_PAIR: usize = PAGE_SIZE - MAX_COPY_BYTES / 2;
5012
5013		let mut rng = Pcg64::seed_from_u64(1337);
5014
5015		let code = Bytecode::new_raw(vec![MCOPY; r as usize].into());
5016		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5017		let (mut ext, _) = setup.ext();
5018		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5019		interpreter
5020			.memory
5021			.resize(0, MEMORY_SIZE, interpreter.ext)
5022			.continue_value()
5023			.unwrap();
5024		let initial = (0u8..=255).cycle().take(MEMORY_SIZE).collect::<Vec<_>>();
5025		interpreter.memory.set(0, &initial);
5026
5027		// The allocator only aligns to 8 bytes, so memory can start anywhere within a page. Copies
5028		// are placed relative to real page boundaries, not to the start of memory.
5029		let address = interpreter.memory.slice(0..MEMORY_SIZE).as_ptr() as usize;
5030		let first_page_start = address.next_multiple_of(PAGE_SIZE) - address;
5031
5032		let mut pair_starts = (0..PAIR_COUNT)
5033			.map(|pair_index| pair_index * PAIR_SIZE)
5034			.map(|pair_offset| pair_offset + first_page_start)
5035			.collect::<Vec<_>>();
5036		pair_starts.shuffle(&mut rng);
5037		// Each copy's source and destination are in different pairs, with different alignments.
5038		let copies = pair_starts
5039			.chunks_exact(2)
5040			.take(r as usize)
5041			.map(|pairs| {
5042				let source_alignment = rng.gen_range(0..4);
5043				let destination_alignment = (source_alignment + rng.gen_range(1..4)) % 4;
5044				let len = rng.gen_range(MAX_COPY_BYTES - 3..=MAX_COPY_BYTES);
5045				let source = pairs[0] + REGION_IN_PAIR + source_alignment;
5046				let destination = pairs[1] + REGION_IN_PAIR + destination_alignment;
5047				[len, source, destination]
5048			})
5049			.collect::<Vec<_>>();
5050		setup_stack(&mut interpreter, copies.iter().flat_map(|copy| copy.map(U256::from)));
5051		let mut expected = initial.clone();
5052		for &[len, source, destination] in &copies {
5053			expected[destination..destination + len]
5054				.copy_from_slice(&initial[source..source + len]);
5055		}
5056
5057		evict_caches();
5058		let result;
5059		#[block]
5060		{
5061			result = evm::run_plain(&mut interpreter);
5062		}
5063
5064		let ControlFlow::Break(halt) = result;
5065		assert!(matches!(halt, Halt::Stop));
5066		assert_eq!(interpreter.stack.len(), 0);
5067		assert_eq!(interpreter.memory.size(), MEMORY_SIZE);
5068		assert_eq!(interpreter.memory.slice_len(0, MEMORY_SIZE), expected);
5069	}
5070
5071	/// Benchmarks one EVM `MCOPY` op-code that copies `64 + n` bytes.
5072	///
5073	/// The first 64 bytes are covered by [`evm_mcopy_opcode`], so the slope of this benchmark is
5074	/// the cost of every extra byte. `n` starts where the copy overlaps its destination, since
5075	/// `memmove` copies shorter ones forward.
5076	///
5077	/// # Added Overheads
5078	///
5079	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5080	/// * The slowest path of the runtime's `memmove`, which copies backward and rebuilds everything
5081	///   it copies with shifts, since the destination starts after the source, overlaps it and is
5082	///   misaligned with it.
5083	/// * A false conflict between every load and the word stored just before it, since the
5084	///   destination is just under a page after the source, so their addresses share the lowest 12
5085	///   bits.
5086	///
5087	/// # Subtraction Safety
5088	///
5089	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5090	#[benchmark(pov_mode = Measured)]
5091	fn evm_mcopy_per_byte(
5092		n: Linear<
5093			{ MCOPY_DESTINATION_DISTANCE - 63 },
5094			{ EVM_MEMORY_BYTES - MCOPY_DESTINATION_DISTANCE - 64 },
5095		>,
5096	) {
5097		const MEMORY_SIZE: usize = EVM_MEMORY_BYTES as usize;
5098		const BASE_COPY_BYTES: usize = 64;
5099		const DESTINATION: usize = MCOPY_DESTINATION_DISTANCE as usize;
5100
5101		let len = n as usize + BASE_COPY_BYTES;
5102		let code = Bytecode::new_raw(vec![MCOPY].into());
5103		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5104		let (mut ext, _) = setup.ext();
5105		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5106		interpreter
5107			.memory
5108			.resize(0, MEMORY_SIZE, interpreter.ext)
5109			.continue_value()
5110			.unwrap();
5111		let initial = (0u8..=255).cycle().take(MEMORY_SIZE).collect::<Vec<_>>();
5112		interpreter.memory.set(0, &initial);
5113		setup_stack(&mut interpreter, [U256::from(len), U256::zero(), U256::from(DESTINATION)]);
5114
5115		evict_caches();
5116		let result;
5117		#[block]
5118		{
5119			result = evm::run_plain(&mut interpreter);
5120		}
5121
5122		let ControlFlow::Break(halt) = result;
5123		assert!(matches!(halt, Halt::Stop));
5124		assert_eq!(interpreter.stack.len(), 0);
5125		assert_eq!(interpreter.memory.size(), MEMORY_SIZE);
5126		assert_eq!(interpreter.memory.slice_len(0, DESTINATION), &initial[..DESTINATION]);
5127		assert_eq!(interpreter.memory.slice_len(DESTINATION, len), &initial[..len]);
5128		assert_eq!(
5129			interpreter.memory.slice(DESTINATION + len..MEMORY_SIZE),
5130			&initial[DESTINATION + len..]
5131		);
5132	}
5133
5134	/// Benchmarks `r` EVM `LT` op-codes.
5135	///
5136	/// # Added Overheads
5137	///
5138	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5139	/// * A branch misprediction on the limb where each comparison stops, since it's pseudo-random.
5140	///
5141	/// # Subtraction Safety
5142	///
5143	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5144	#[benchmark(pov_mode = Measured)]
5145	fn evm_lt_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5146		let mut rng = Pcg64::seed_from_u64(1337);
5147		let start = U256::zero();
5148		let mut previous_result = start;
5149		let operands = (0..r)
5150			.map(|_| {
5151				let operand = match Outcome::ALL.choose(&mut rng).unwrap() {
5152					Outcome::DifferInLimb3 => previous_result + (U256::from(2) << 192),
5153					Outcome::DifferInLimb2 => previous_result + (U256::from(2) << 128),
5154					Outcome::DifferInLimb1 => previous_result + (U256::from(2) << 64),
5155					Outcome::DifferInLimb0 => previous_result + U256::from(2),
5156					Outcome::Equal => previous_result,
5157				};
5158				previous_result =
5159					if previous_result < operand { U256::one() } else { U256::zero() };
5160				operand
5161			})
5162			.collect::<Vec<_>>();
5163
5164		let code = Bytecode::new_raw(vec![LT; r as usize].into());
5165		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5166		let (mut ext, _) = setup.ext();
5167		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5168		setup_stack(&mut interpreter, operands.into_iter().rev().chain([start]));
5169
5170		evict_caches();
5171		let result;
5172		#[block]
5173		{
5174			result = evm::run_plain(&mut interpreter);
5175		}
5176
5177		let ControlFlow::Break(halt) = result;
5178		assert!(matches!(halt, Halt::Stop));
5179		assert_eq!(interpreter.stack.len(), 1);
5180		assert_eq!(interpreter.stack.top(), Some(&previous_result));
5181	}
5182
5183	/// Benchmarks `r` EVM `GT` op-codes.
5184	///
5185	/// # Added Overheads
5186	///
5187	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5188	/// * A branch misprediction on the limb where each comparison stops, since it's pseudo-random.
5189	///
5190	/// # Subtraction Safety
5191	///
5192	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5193	#[benchmark(pov_mode = Measured)]
5194	fn evm_gt_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5195		let mut rng = Pcg64::seed_from_u64(1337);
5196		let operands = (0..r)
5197			.map(|_| match Outcome::ALL.choose(&mut rng).unwrap() {
5198				Outcome::DifferInLimb3 => U256::from(2) << 192,
5199				Outcome::DifferInLimb2 => U256::from(2) << 128,
5200				Outcome::DifferInLimb1 => U256::from(2) << 64,
5201				Outcome::DifferInLimb0 => U256::from(2),
5202				Outcome::Equal => U256::zero(),
5203			})
5204			.collect::<Vec<_>>();
5205
5206		let code = Bytecode::new_raw(vec![GT; r as usize].into());
5207		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5208		let (mut ext, _) = setup.ext();
5209		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5210		setup_stack(&mut interpreter, operands.into_iter().rev().chain([U256::zero()]));
5211
5212		evict_caches();
5213		let result;
5214		#[block]
5215		{
5216			result = evm::run_plain(&mut interpreter);
5217		}
5218
5219		let ControlFlow::Break(halt) = result;
5220		assert!(matches!(halt, Halt::Stop));
5221		assert_eq!(interpreter.stack.len(), 1);
5222		assert_eq!(interpreter.stack.top(), Some(&U256::zero()));
5223	}
5224
5225	/// Benchmarks `r` EVM `EQ` op-codes.
5226	///
5227	/// # Added Overheads
5228	///
5229	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5230	/// * A branch misprediction on the byte where each comparison stops, since it's pseudo-random.
5231	/// * No early exit before the last bytes of each comparison, since the operands only ever
5232	///   differ in those bytes.
5233	///
5234	/// # Subtraction Safety
5235	///
5236	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5237	#[benchmark(pov_mode = Measured)]
5238	fn evm_eq_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5239		let mut rng = Pcg64::seed_from_u64(1337);
5240		let start = U256::zero();
5241		let mut previous_result = start;
5242		let operands = (0..r)
5243			.map(|_| {
5244				let differing_byte =
5245					[Some(29), Some(30), Some(31), None].choose(&mut rng).copied().unwrap();
5246				let operand = match differing_byte {
5247					Some(byte) => previous_result ^ (U256::one() << (8 * byte)),
5248					None => previous_result,
5249				};
5250				previous_result =
5251					if previous_result == operand { U256::one() } else { U256::zero() };
5252				operand
5253			})
5254			.collect::<Vec<_>>();
5255
5256		let code = Bytecode::new_raw(vec![EQ; r as usize].into());
5257		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5258		let (mut ext, _) = setup.ext();
5259		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5260		setup_stack(&mut interpreter, operands.into_iter().rev().chain([start]));
5261
5262		evict_caches();
5263		let result;
5264		#[block]
5265		{
5266			result = evm::run_plain(&mut interpreter);
5267		}
5268
5269		let ControlFlow::Break(halt) = result;
5270		assert!(matches!(halt, Halt::Stop));
5271		assert_eq!(interpreter.stack.len(), 1);
5272		assert_eq!(interpreter.stack.top(), Some(&previous_result));
5273	}
5274
5275	/// Benchmarks `r` EVM `ISZERO` op-codes.
5276	///
5277	/// # Added Overheads
5278	///
5279	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5280	/// * A branch misprediction on the word where each zero check stops, since it's pseudo-random.
5281	///
5282	/// # Subtraction Safety
5283	///
5284	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5285	#[benchmark(pov_mode = Measured)]
5286	fn evm_iszero_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
5287		let mut rng = Pcg64::seed_from_u64(1337);
5288		// Each operand is zero or nonzero in only one of words 0, 1 and 2. In the compiled runtime,
5289		// the zero check branches on words 0, 1 and 2 to stop at the first nonzero one, but tests
5290		// word 3 without a branch. An operand that is nonzero only in word 3 therefore takes the
5291		// same path as zero, and adding it would only make that path more likely, and so easier to
5292		// predict.
5293		let operands = (0..r).map(|_| {
5294			match [Some(0), Some(1), Some(2), None].choose(&mut rng).copied().unwrap() {
5295				Some(word) => U256::one() << (64 * word),
5296				None => U256::zero(),
5297			}
5298		});
5299
5300		let code = Bytecode::new_raw([ISZERO, POP].repeat(r as usize).into());
5301		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5302		let (mut ext, _) = setup.ext();
5303		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5304		setup_stack(&mut interpreter, operands);
5305
5306		evict_caches();
5307		let result;
5308		#[block]
5309		{
5310			result = evm::run_plain(&mut interpreter);
5311		}
5312
5313		let ControlFlow::Break(halt) = result;
5314		assert!(matches!(halt, Halt::Stop));
5315		assert_eq!(interpreter.stack.len(), 0);
5316	}
5317
5318	/// Benchmarks `r` EVM `AND` op-codes.
5319	///
5320	/// # Added Overheads
5321	///
5322	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5323	///
5324	/// # Subtraction Safety
5325	///
5326	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5327	#[benchmark(pov_mode = Measured)]
5328	fn evm_and_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5329		let code = Bytecode::new_raw(vec![AND; r as usize].into());
5330		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5331		let (mut ext, _) = setup.ext();
5332		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5333		setup_stack(&mut interpreter, vec![U256::MAX; r as usize + 1]);
5334
5335		evict_caches();
5336		let result;
5337		#[block]
5338		{
5339			result = evm::run_plain(&mut interpreter);
5340		}
5341
5342		let ControlFlow::Break(halt) = result;
5343		assert!(matches!(halt, Halt::Stop));
5344		assert_eq!(interpreter.stack.len(), 1);
5345		assert_eq!(interpreter.stack.top(), Some(&U256::MAX));
5346	}
5347
5348	/// Benchmarks `r` EVM `OR` op-codes.
5349	///
5350	/// # Added Overheads
5351	///
5352	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5353	///
5354	/// # Subtraction Safety
5355	///
5356	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5357	#[benchmark(pov_mode = Measured)]
5358	fn evm_or_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5359		let code = Bytecode::new_raw(vec![OR; r as usize].into());
5360		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5361		let (mut ext, _) = setup.ext();
5362		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5363		setup_stack(&mut interpreter, vec![U256::MAX; r as usize + 1]);
5364
5365		evict_caches();
5366		let result;
5367		#[block]
5368		{
5369			result = evm::run_plain(&mut interpreter);
5370		}
5371
5372		let ControlFlow::Break(halt) = result;
5373		assert!(matches!(halt, Halt::Stop));
5374		assert_eq!(interpreter.stack.len(), 1);
5375		assert_eq!(interpreter.stack.top(), Some(&U256::MAX));
5376	}
5377
5378	/// Benchmarks `r` EVM `XOR` op-codes.
5379	///
5380	/// # Added Overheads
5381	///
5382	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5383	///
5384	/// # Subtraction Safety
5385	///
5386	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5387	#[benchmark(pov_mode = Measured)]
5388	fn evm_xor_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
5389		let code = Bytecode::new_raw(vec![XOR; r as usize].into());
5390		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5391		let (mut ext, _) = setup.ext();
5392		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5393		setup_stack(&mut interpreter, vec![U256::MAX; r as usize + 1]);
5394
5395		evict_caches();
5396		let result;
5397		#[block]
5398		{
5399			result = evm::run_plain(&mut interpreter);
5400		}
5401
5402		let ControlFlow::Break(halt) = result;
5403		assert!(matches!(halt, Halt::Stop));
5404		assert_eq!(interpreter.stack.len(), 1);
5405		let expected = if r.is_multiple_of(2) { U256::MAX } else { U256::zero() };
5406		assert_eq!(interpreter.stack.top(), Some(&expected));
5407	}
5408
5409	/// Benchmarks `r` EVM `NOT` op-codes.
5410	///
5411	/// # Added Overheads
5412	///
5413	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5414	///
5415	/// # Subtraction Safety
5416	///
5417	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5418	#[benchmark(pov_mode = Measured)]
5419	fn evm_not_opcode(r: Linear<0, { MAX_INITCODE_SIZE as u32 }>) {
5420		let code = Bytecode::new_raw(vec![NOT; r as usize].into());
5421		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5422		let (mut ext, _) = setup.ext();
5423		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5424		setup_stack(&mut interpreter, [U256::zero()]);
5425
5426		evict_caches();
5427		let result;
5428		#[block]
5429		{
5430			result = evm::run_plain(&mut interpreter);
5431		}
5432
5433		let ControlFlow::Break(halt) = result;
5434		assert!(matches!(halt, Halt::Stop));
5435		assert_eq!(interpreter.stack.len(), 1);
5436		let expected = if r.is_multiple_of(2) { U256::zero() } else { U256::MAX };
5437		assert_eq!(interpreter.stack.top(), Some(&expected));
5438	}
5439
5440	/// Benchmarks `r` EVM `BYTE` op-codes.
5441	///
5442	/// # Added Overheads
5443	///
5444	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5445	/// * A branch misprediction on whether each index is in range, since it's pseudo-random.
5446	///
5447	/// # Subtraction Safety
5448	///
5449	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5450	#[benchmark(pov_mode = Measured)]
5451	fn evm_byte_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5452		let mut rng = Pcg64::seed_from_u64(1337);
5453		let operands = (0..r).flat_map(|_| {
5454			let index = if rng.gen_bool(0.5) { U256::from(31) } else { U256::from(32) };
5455			[U256::MAX, index]
5456		});
5457
5458		let code = Bytecode::new_raw([BYTE, POP].repeat(r as usize).into());
5459		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5460		let (mut ext, _) = setup.ext();
5461		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5462		setup_stack(&mut interpreter, operands);
5463
5464		evict_caches();
5465		let result;
5466		#[block]
5467		{
5468			result = evm::run_plain(&mut interpreter);
5469		}
5470
5471		let ControlFlow::Break(halt) = result;
5472		assert!(matches!(halt, Halt::Stop));
5473		assert_eq!(interpreter.stack.len(), 0);
5474	}
5475
5476	/// Benchmarks `r` EVM `CLZ` op-codes.
5477	///
5478	/// # Added Overheads
5479	///
5480	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5481	/// * A branch misprediction on the limb where each count stops, since it's pseudo-random.
5482	///
5483	/// # Subtraction Safety
5484	///
5485	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5486	#[benchmark(pov_mode = Measured)]
5487	fn evm_clz_opcode(r: Linear<0, EVM_STACK_LIMIT>) {
5488		let mut rng = Pcg64::seed_from_u64(1337);
5489		let operands = (0..r).map(|_| {
5490			match [Some(3), Some(2), Some(1), Some(0), None].choose(&mut rng).copied().unwrap() {
5491				Some(limb) => U256::one() << (64 * limb),
5492				None => U256::zero(),
5493			}
5494		});
5495
5496		let code = Bytecode::new_raw([CLZ, POP].repeat(r as usize).into());
5497		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5498		let (mut ext, _) = setup.ext();
5499		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5500		setup_stack(&mut interpreter, operands);
5501
5502		evict_caches();
5503		let result;
5504		#[block]
5505		{
5506			result = evm::run_plain(&mut interpreter);
5507		}
5508
5509		let ControlFlow::Break(halt) = result;
5510		assert!(matches!(halt, Halt::Stop));
5511		assert_eq!(interpreter.stack.len(), 0);
5512	}
5513
5514	/// Benchmarks `r` EVM `SLT` op-codes.
5515	///
5516	/// # Added Overheads
5517	///
5518	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5519	/// * A branch misprediction on the signs of the operands, since they're pseudo-random.
5520	/// * A branch misprediction on the limb where each comparison stops, since it's pseudo-random.
5521	/// * A branch misprediction on which operand is smaller, since their order is pseudo-random.
5522	///
5523	/// # Subtraction Safety
5524	///
5525	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5526	#[benchmark(pov_mode = Measured)]
5527	fn evm_slt_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5528		let code = Bytecode::new_raw([SLT, POP].repeat(r as usize).into());
5529		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5530		let (mut ext, _) = setup.ext();
5531		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5532		setup_stack(&mut interpreter, signed_comparison_operands(r));
5533
5534		evict_caches();
5535		let result;
5536		#[block]
5537		{
5538			result = evm::run_plain(&mut interpreter);
5539		}
5540
5541		let ControlFlow::Break(halt) = result;
5542		assert!(matches!(halt, Halt::Stop));
5543		assert_eq!(interpreter.stack.len(), 0);
5544	}
5545
5546	/// Benchmarks `r` EVM `SGT` op-codes.
5547	///
5548	/// # Added Overheads
5549	///
5550	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5551	/// * A branch misprediction on the signs of the operands, since they're pseudo-random.
5552	/// * A branch misprediction on the limb where each comparison stops, since it's pseudo-random.
5553	/// * A branch misprediction on which operand is greater, since their order is pseudo-random.
5554	///
5555	/// # Subtraction Safety
5556	///
5557	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5558	#[benchmark(pov_mode = Measured)]
5559	fn evm_sgt_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5560		let code = Bytecode::new_raw([SGT, POP].repeat(r as usize).into());
5561		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5562		let (mut ext, _) = setup.ext();
5563		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5564		setup_stack(&mut interpreter, signed_comparison_operands(r));
5565
5566		evict_caches();
5567		let result;
5568		#[block]
5569		{
5570			result = evm::run_plain(&mut interpreter);
5571		}
5572
5573		let ControlFlow::Break(halt) = result;
5574		assert!(matches!(halt, Halt::Stop));
5575		assert_eq!(interpreter.stack.len(), 0);
5576	}
5577
5578	/// Benchmarks `r` EVM `SHL` op-codes.
5579	///
5580	/// # Added Overheads
5581	///
5582	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5583	/// * A branch misprediction on how many whole words each shift moves, since it's pseudo-random.
5584	/// * A branch misprediction on whether each shift carries bits across words, since it's
5585	///   pseudo-random.
5586	///
5587	/// # Subtraction Safety
5588	///
5589	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5590	#[benchmark(pov_mode = Measured)]
5591	fn evm_shl_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5592		let mut rng = Pcg64::seed_from_u64(1337);
5593		let operands = (0..r).flat_map(|_| {
5594			let whole_words = rng.gen_range(0..=3u32);
5595			let extra_bits = rng.gen_range(0..=1u32);
5596			[U256::MAX, U256::from(64 * whole_words + extra_bits).max(U256::one())]
5597		});
5598
5599		let code = Bytecode::new_raw([SHL, POP].repeat(r as usize).into());
5600		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5601		let (mut ext, _) = setup.ext();
5602		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5603		setup_stack(&mut interpreter, operands);
5604
5605		evict_caches();
5606		let result;
5607		#[block]
5608		{
5609			result = evm::run_plain(&mut interpreter);
5610		}
5611
5612		let ControlFlow::Break(halt) = result;
5613		assert!(matches!(halt, Halt::Stop));
5614		assert_eq!(interpreter.stack.len(), 0);
5615	}
5616
5617	/// Benchmarks `r` EVM `SHR` op-codes.
5618	///
5619	/// # Added Overheads
5620	///
5621	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5622	/// * A branch misprediction on how many whole words each shift moves, since it's pseudo-random.
5623	/// * A branch misprediction on whether each shift carries bits across words, since it's
5624	///   pseudo-random.
5625	///
5626	/// # Subtraction Safety
5627	///
5628	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5629	#[benchmark(pov_mode = Measured)]
5630	fn evm_shr_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5631		let mut rng = Pcg64::seed_from_u64(1337);
5632		let operands = (0..r).flat_map(|_| {
5633			let whole_words = rng.gen_range(0..=3u32);
5634			let extra_bits = rng.gen_range(0..=1u32);
5635			[U256::MAX, U256::from(64 * whole_words + extra_bits).max(U256::one())]
5636		});
5637
5638		let code = Bytecode::new_raw([SHR, POP].repeat(r as usize).into());
5639		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5640		let (mut ext, _) = setup.ext();
5641		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5642		setup_stack(&mut interpreter, operands);
5643
5644		evict_caches();
5645		let result;
5646		#[block]
5647		{
5648			result = evm::run_plain(&mut interpreter);
5649		}
5650
5651		let ControlFlow::Break(halt) = result;
5652		assert!(matches!(halt, Halt::Stop));
5653		assert_eq!(interpreter.stack.len(), 0);
5654	}
5655
5656	/// Benchmarks `r` EVM `SAR` op-codes.
5657	///
5658	/// # Added Overheads
5659	///
5660	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5661	/// * A branch misprediction on how many whole words each shift moves, since it's pseudo-random.
5662	/// * A branch misprediction on whether each shift carries bits across words, since it's
5663	///   pseudo-random.
5664	/// * A second shift on every `SAR`, which fills the vacated bits with ones, since every value
5665	///   is negative.
5666	///
5667	/// # Subtraction Safety
5668	///
5669	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5670	#[benchmark(pov_mode = Measured)]
5671	fn evm_sar_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5672		let mut rng = Pcg64::seed_from_u64(1337);
5673		let operands = (0..r).flat_map(|_| {
5674			let whole_words = rng.gen_range(0..=3u32);
5675			let extra_bits = rng.gen_range(0..=1u32);
5676			[U256::MAX, U256::from(64 * whole_words + extra_bits).max(U256::one())]
5677		});
5678
5679		let code = Bytecode::new_raw([SAR, POP].repeat(r as usize).into());
5680		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5681		let (mut ext, _) = setup.ext();
5682		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5683		setup_stack(&mut interpreter, operands);
5684
5685		evict_caches();
5686		let result;
5687		#[block]
5688		{
5689			result = evm::run_plain(&mut interpreter);
5690		}
5691
5692		let ControlFlow::Break(halt) = result;
5693		assert!(matches!(halt, Halt::Stop));
5694		assert_eq!(interpreter.stack.len(), 0);
5695	}
5696
5697	/// Benchmarks `r` EVM `ADDMOD` op-codes.
5698	///
5699	/// # Added Overheads
5700	///
5701	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5702	/// * A division for each addend, since both are above the modulus.
5703	/// * A branch misprediction on how many times each quotient limb's estimate is corrected, since
5704	///   the modulus is picked at random from [`knuth_division_operands`].
5705	/// * A branch misprediction on whether the modulus is subtracted from the sum, since that
5706	///   depends on the modulus picked.
5707	///
5708	/// # Subtraction Safety
5709	///
5710	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5711	#[benchmark(pov_mode = Measured)]
5712	fn evm_addmod_opcode(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
5713		use knuth_division_operands::DENOMINATORS;
5714
5715		let mut rng = Pcg64::seed_from_u64(1337);
5716		let operands = (0..r).flat_map(|_| {
5717			let [modulus, numerator] = unsigned_division_operands(&mut rng, &DENOMINATORS);
5718			[modulus, numerator, numerator]
5719		});
5720
5721		let code = Bytecode::new_raw([ADDMOD, POP].repeat(r as usize).into());
5722		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5723		let (mut ext, _) = setup.ext();
5724		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5725		setup_stack(&mut interpreter, operands);
5726
5727		evict_caches();
5728		let result;
5729		#[block]
5730		{
5731			result = evm::run_plain(&mut interpreter);
5732		}
5733
5734		let ControlFlow::Break(halt) = result;
5735		assert!(matches!(halt, Halt::Stop));
5736		assert_eq!(interpreter.stack.len(), 0);
5737	}
5738
5739	/// Benchmarks `r` EVM `ADDMOD` op-codes with pseudo-random one-limb correction patterns.
5740	///
5741	/// # Subtraction Safety
5742	///
5743	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5744	#[benchmark(pov_mode = Measured)]
5745	fn evm_addmod_opcode_one_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
5746		use one_limb_division_operands::DENOMINATORS;
5747
5748		let mut rng = Pcg64::seed_from_u64(1337);
5749		let operands = (0..r).flat_map(|_| {
5750			let [modulus, numerator] = unsigned_division_operands(&mut rng, &DENOMINATORS);
5751			[modulus, numerator, numerator]
5752		});
5753
5754		let code = Bytecode::new_raw([ADDMOD, POP].repeat(r as usize).into());
5755		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5756		let (mut ext, _) = setup.ext();
5757		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5758		setup_stack(&mut interpreter, operands);
5759
5760		evict_caches();
5761		let result;
5762		#[block]
5763		{
5764			result = evm::run_plain(&mut interpreter);
5765		}
5766
5767		let ControlFlow::Break(halt) = result;
5768		assert!(matches!(halt, Halt::Stop));
5769		assert_eq!(interpreter.stack.len(), 0);
5770	}
5771
5772	/// Benchmarks `r` EVM `ADDMOD` op-codes with pseudo-random three-limb correction patterns.
5773	///
5774	/// # Subtraction Safety
5775	///
5776	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5777	#[benchmark(pov_mode = Measured)]
5778	fn evm_addmod_opcode_three_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
5779		use three_limb_division_operands::DENOMINATORS;
5780
5781		let mut rng = Pcg64::seed_from_u64(1337);
5782		let operands = (0..r).flat_map(|_| {
5783			let [modulus, numerator] = unsigned_division_operands(&mut rng, &DENOMINATORS);
5784			[modulus, numerator, numerator]
5785		});
5786
5787		let code = Bytecode::new_raw([ADDMOD, POP].repeat(r as usize).into());
5788		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5789		let (mut ext, _) = setup.ext();
5790		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5791		setup_stack(&mut interpreter, operands);
5792
5793		evict_caches();
5794		let result;
5795		#[block]
5796		{
5797			result = evm::run_plain(&mut interpreter);
5798		}
5799
5800		let ControlFlow::Break(halt) = result;
5801		assert!(matches!(halt, Halt::Stop));
5802		assert_eq!(interpreter.stack.len(), 0);
5803	}
5804
5805	/// Benchmarks `r` EVM `ADDMOD` op-codes with pseudo-random mixed one-limb and Knuth divisors.
5806	///
5807	/// # Subtraction Safety
5808	///
5809	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5810	#[benchmark(pov_mode = Measured)]
5811	fn evm_addmod_opcode_mixed_variant(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
5812		let mut rng = Pcg64::seed_from_u64(1337);
5813		let operands = (0..r).flat_map(|_| {
5814			let [modulus, numerator] = mixed_division_operands::unsigned_operands(&mut rng);
5815			[modulus, numerator, numerator]
5816		});
5817
5818		let code = Bytecode::new_raw([ADDMOD, POP].repeat(r as usize).into());
5819		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5820		let (mut ext, _) = setup.ext();
5821		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5822		setup_stack(&mut interpreter, operands);
5823
5824		evict_caches();
5825		let result;
5826		#[block]
5827		{
5828			result = evm::run_plain(&mut interpreter);
5829		}
5830
5831		let ControlFlow::Break(halt) = result;
5832		assert!(matches!(halt, Halt::Stop));
5833		assert_eq!(interpreter.stack.len(), 0);
5834	}
5835
5836	/// Benchmarks `r` EVM `ADDMOD` op-codes with the selected fixed Knuth divisor.
5837	///
5838	/// # Subtraction Safety
5839	///
5840	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5841	#[benchmark(pov_mode = Measured)]
5842	fn evm_addmod_opcode_fixed_knuth_variant(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
5843		use knuth_division_operands::{ADDMOD_FIXED_DIVISOR, NUMERATOR};
5844
5845		let operands = (0..r).flat_map(|_| [ADDMOD_FIXED_DIVISOR, NUMERATOR, NUMERATOR]);
5846		let code = Bytecode::new_raw([ADDMOD, POP].repeat(r as usize).into());
5847		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5848		let (mut ext, _) = setup.ext();
5849		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5850		setup_stack(&mut interpreter, operands);
5851
5852		evict_caches();
5853		let result;
5854		#[block]
5855		{
5856			result = evm::run_plain(&mut interpreter);
5857		}
5858
5859		let ControlFlow::Break(halt) = result;
5860		assert!(matches!(halt, Halt::Stop));
5861		assert_eq!(interpreter.stack.len(), 0);
5862	}
5863
5864	/// Benchmarks `r` EVM `DIV` op-codes.
5865	///
5866	/// # Added Overheads
5867	///
5868	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
5869	/// * A branch misprediction on how many times each quotient limb's estimate is corrected, since
5870	///   the divisor is picked at random from [`knuth_division_operands`].
5871	///
5872	/// # Subtraction Safety
5873	///
5874	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5875	#[benchmark(pov_mode = Measured)]
5876	fn evm_div_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5877		use knuth_division_operands::DENOMINATORS;
5878
5879		let mut rng = Pcg64::seed_from_u64(1337);
5880		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
5881
5882		let code = Bytecode::new_raw([DIV, POP].repeat(r as usize).into());
5883		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5884		let (mut ext, _) = setup.ext();
5885		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5886		setup_stack(&mut interpreter, operands);
5887
5888		evict_caches();
5889		let result;
5890		#[block]
5891		{
5892			result = evm::run_plain(&mut interpreter);
5893		}
5894
5895		let ControlFlow::Break(halt) = result;
5896		assert!(matches!(halt, Halt::Stop));
5897		assert_eq!(interpreter.stack.len(), 0);
5898	}
5899
5900	/// Benchmarks `r` EVM `DIV` op-codes with pseudo-random one-limb correction patterns.
5901	///
5902	/// # Subtraction Safety
5903	///
5904	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5905	#[benchmark(pov_mode = Measured)]
5906	fn evm_div_opcode_one_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5907		use one_limb_division_operands::DENOMINATORS;
5908
5909		let mut rng = Pcg64::seed_from_u64(1337);
5910		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
5911
5912		let code = Bytecode::new_raw([DIV, POP].repeat(r as usize).into());
5913		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5914		let (mut ext, _) = setup.ext();
5915		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5916		setup_stack(&mut interpreter, operands);
5917
5918		evict_caches();
5919		let result;
5920		#[block]
5921		{
5922			result = evm::run_plain(&mut interpreter);
5923		}
5924
5925		let ControlFlow::Break(halt) = result;
5926		assert!(matches!(halt, Halt::Stop));
5927		assert_eq!(interpreter.stack.len(), 0);
5928	}
5929
5930	/// Benchmarks `r` EVM `DIV` op-codes with pseudo-random three-limb correction patterns.
5931	///
5932	/// # Subtraction Safety
5933	///
5934	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5935	#[benchmark(pov_mode = Measured)]
5936	fn evm_div_opcode_three_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5937		use three_limb_division_operands::DENOMINATORS;
5938
5939		let mut rng = Pcg64::seed_from_u64(1337);
5940		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
5941
5942		let code = Bytecode::new_raw([DIV, POP].repeat(r as usize).into());
5943		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5944		let (mut ext, _) = setup.ext();
5945		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5946		setup_stack(&mut interpreter, operands);
5947
5948		evict_caches();
5949		let result;
5950		#[block]
5951		{
5952			result = evm::run_plain(&mut interpreter);
5953		}
5954
5955		let ControlFlow::Break(halt) = result;
5956		assert!(matches!(halt, Halt::Stop));
5957		assert_eq!(interpreter.stack.len(), 0);
5958	}
5959
5960	/// Benchmarks `r` EVM `DIV` op-codes with pseudo-random mixed one-limb and Knuth divisors.
5961	///
5962	/// # Subtraction Safety
5963	///
5964	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5965	#[benchmark(pov_mode = Measured)]
5966	fn evm_div_opcode_mixed_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5967		let mut rng = Pcg64::seed_from_u64(1337);
5968		let operands = (0..r).flat_map(|_| mixed_division_operands::unsigned_operands(&mut rng));
5969
5970		let code = Bytecode::new_raw([DIV, POP].repeat(r as usize).into());
5971		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
5972		let (mut ext, _) = setup.ext();
5973		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
5974		setup_stack(&mut interpreter, operands);
5975
5976		evict_caches();
5977		let result;
5978		#[block]
5979		{
5980			result = evm::run_plain(&mut interpreter);
5981		}
5982
5983		let ControlFlow::Break(halt) = result;
5984		assert!(matches!(halt, Halt::Stop));
5985		assert_eq!(interpreter.stack.len(), 0);
5986	}
5987
5988	/// Benchmarks `r` EVM `DIV` op-codes with the selected fixed Knuth divisor.
5989	///
5990	/// # Subtraction Safety
5991	///
5992	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
5993	#[benchmark(pov_mode = Measured)]
5994	fn evm_div_opcode_fixed_knuth_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
5995		use knuth_division_operands::{DIV_FIXED_DIVISOR, NUMERATOR};
5996
5997		let operands = (0..r).flat_map(|_| [DIV_FIXED_DIVISOR, NUMERATOR]);
5998		let code = Bytecode::new_raw([DIV, POP].repeat(r as usize).into());
5999		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6000		let (mut ext, _) = setup.ext();
6001		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6002		setup_stack(&mut interpreter, operands);
6003
6004		evict_caches();
6005		let result;
6006		#[block]
6007		{
6008			result = evm::run_plain(&mut interpreter);
6009		}
6010
6011		let ControlFlow::Break(halt) = result;
6012		assert!(matches!(halt, Halt::Stop));
6013		assert_eq!(interpreter.stack.len(), 0);
6014	}
6015
6016	/// Benchmarks `r` EVM `ADD` op-codes.
6017	///
6018	/// # Added Overheads
6019	///
6020	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6021	/// * A branch misprediction on whether each limb carries, since it's pseudo-random.
6022	///
6023	/// # Subtraction Safety
6024	///
6025	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6026	#[benchmark(pov_mode = Measured)]
6027	fn evm_add_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
6028		let mut rng = Pcg64::seed_from_u64(1337);
6029		let start = U256([u64::MAX / 2; 4]);
6030		let mut running_sum = start;
6031		let operands = (0..r)
6032			.map(|_| {
6033				// Nothing carries into the lowest limb.
6034				let mut carry_in = false;
6035				let limbs = running_sum.0.map(|limb| {
6036					let should_carry = rng.gen_bool(0.5);
6037					// The limb overflows exactly when a value larger than this is added to it.
6038					let largest_without_carry = u64::MAX - limb - u64::from(carry_in);
6039					let value = if should_carry {
6040						rng.gen_range(largest_without_carry + 1..=u64::MAX)
6041					} else {
6042						rng.gen_range(1..=largest_without_carry)
6043					};
6044					carry_in = should_carry;
6045					value
6046				});
6047				let operand = U256(limbs);
6048				running_sum = running_sum.overflowing_add(operand).0;
6049				operand
6050			})
6051			.collect::<Vec<_>>();
6052
6053		let code = Bytecode::new_raw(vec![ADD; r as usize].into());
6054		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6055		let (mut ext, _) = setup.ext();
6056		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6057		setup_stack(&mut interpreter, operands.into_iter().rev().chain([start]));
6058
6059		evict_caches();
6060		let result;
6061		#[block]
6062		{
6063			result = evm::run_plain(&mut interpreter);
6064		}
6065
6066		let ControlFlow::Break(halt) = result;
6067		assert!(matches!(halt, Halt::Stop));
6068		assert_eq!(interpreter.stack.len(), 1);
6069		assert_eq!(interpreter.stack.top(), Some(&running_sum));
6070	}
6071
6072	/// Benchmarks `r` EVM `SUB` op-codes.
6073	///
6074	/// # Added Overheads
6075	///
6076	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6077	/// * A branch misprediction on whether each limb borrows, since it's pseudo-random.
6078	///
6079	/// # Subtraction Safety
6080	///
6081	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6082	#[benchmark(pov_mode = Measured)]
6083	fn evm_sub_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
6084		let mut rng = Pcg64::seed_from_u64(1337);
6085		let start = U256([u64::MAX / 2; 4]);
6086		let mut running_difference = start;
6087		let operands = (0..r)
6088			.map(|_| {
6089				// Nothing borrows from below the lowest limb.
6090				let mut borrow_in = false;
6091				let limbs = running_difference.0.map(|limb| {
6092					let should_borrow = rng.gen_bool(0.5);
6093					// The limb borrows exactly when a value larger than this is subtracted.
6094					let largest_without_borrow = limb - u64::from(borrow_in);
6095					let value = if should_borrow {
6096						rng.gen_range(largest_without_borrow + 1..=u64::MAX)
6097					} else {
6098						rng.gen_range(1..=largest_without_borrow)
6099					};
6100					borrow_in = should_borrow;
6101					value
6102				});
6103				let operand = U256(limbs);
6104				running_difference = running_difference.overflowing_sub(operand).0;
6105				operand
6106			})
6107			.collect::<Vec<_>>();
6108
6109		let code = Bytecode::new_raw(vec![SUB; r as usize].into());
6110		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6111		let (mut ext, _) = setup.ext();
6112		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6113		setup_stack(&mut interpreter, operands.into_iter().rev().chain([start]));
6114
6115		evict_caches();
6116		let result;
6117		#[block]
6118		{
6119			result = evm::run_plain(&mut interpreter);
6120		}
6121
6122		let ControlFlow::Break(halt) = result;
6123		assert!(matches!(halt, Halt::Stop));
6124		assert_eq!(interpreter.stack.len(), 1);
6125		assert_eq!(interpreter.stack.top(), Some(&running_difference));
6126	}
6127
6128	/// Benchmarks `r` EVM `MUL` op-codes.
6129	///
6130	/// # Added Overheads
6131	///
6132	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6133	///
6134	/// # Subtraction Safety
6135	///
6136	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6137	#[benchmark(pov_mode = Measured)]
6138	fn evm_mul_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
6139		const START: U256 = U256([0x5555_5555_5555_5555; 4]);
6140
6141		let code = Bytecode::new_raw(vec![MUL; r as usize].into());
6142		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6143		let (mut ext, _) = setup.ext();
6144		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6145		setup_stack(&mut interpreter, vec![U256::MAX; r as usize]);
6146		setup_stack(&mut interpreter, [START]);
6147
6148		evict_caches();
6149		let result;
6150		#[block]
6151		{
6152			result = evm::run_plain(&mut interpreter);
6153		}
6154
6155		let ControlFlow::Break(halt) = result;
6156		assert!(matches!(halt, Halt::Stop));
6157		assert_eq!(interpreter.stack.len(), 1);
6158		let expected = if r.is_multiple_of(2) { START } else { START.overflowing_neg().0 };
6159		assert_eq!(interpreter.stack.top(), Some(&expected));
6160	}
6161
6162	/// Benchmarks `r` EVM `SDIV` op-codes.
6163	///
6164	/// # Added Overheads
6165	///
6166	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6167	/// * A carry through every limb to negate the numerator, since it's the most negative value.
6168	/// * No early exit from the check against the most negative value, since the numerator is that
6169	///   value.
6170	/// * A branch misprediction on how many times each quotient limb's estimate is corrected, since
6171	///   the divisor is picked at random from [`knuth_division_operands`].
6172	/// * A branch misprediction on whether the divisor and the quotient are negated, since the
6173	///   divisor's sign is pseudo-random.
6174	///
6175	/// # Subtraction Safety
6176	///
6177	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6178	#[benchmark(pov_mode = Measured)]
6179	fn evm_sdiv_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6180		use knuth_division_operands::DENOMINATORS;
6181
6182		let mut rng = Pcg64::seed_from_u64(1337);
6183		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6184
6185		let code = Bytecode::new_raw([SDIV, POP].repeat(r as usize).into());
6186		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6187		let (mut ext, _) = setup.ext();
6188		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6189		setup_stack(&mut interpreter, operands);
6190
6191		evict_caches();
6192		let result;
6193		#[block]
6194		{
6195			result = evm::run_plain(&mut interpreter);
6196		}
6197
6198		let ControlFlow::Break(halt) = result;
6199		assert!(matches!(halt, Halt::Stop));
6200		assert_eq!(interpreter.stack.len(), 0);
6201	}
6202
6203	/// Benchmarks `r` EVM `SDIV` op-codes with pseudo-random one-limb correction patterns.
6204	///
6205	/// # Subtraction Safety
6206	///
6207	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6208	#[benchmark(pov_mode = Measured)]
6209	fn evm_sdiv_opcode_one_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6210		use one_limb_division_operands::DENOMINATORS;
6211
6212		let mut rng = Pcg64::seed_from_u64(1337);
6213		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6214
6215		let code = Bytecode::new_raw([SDIV, POP].repeat(r as usize).into());
6216		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6217		let (mut ext, _) = setup.ext();
6218		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6219		setup_stack(&mut interpreter, operands);
6220
6221		evict_caches();
6222		let result;
6223		#[block]
6224		{
6225			result = evm::run_plain(&mut interpreter);
6226		}
6227
6228		let ControlFlow::Break(halt) = result;
6229		assert!(matches!(halt, Halt::Stop));
6230		assert_eq!(interpreter.stack.len(), 0);
6231	}
6232
6233	/// Benchmarks `r` EVM `SDIV` op-codes with pseudo-random three-limb correction patterns.
6234	///
6235	/// # Subtraction Safety
6236	///
6237	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6238	#[benchmark(pov_mode = Measured)]
6239	fn evm_sdiv_opcode_three_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6240		use three_limb_division_operands::DENOMINATORS;
6241
6242		let mut rng = Pcg64::seed_from_u64(1337);
6243		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6244
6245		let code = Bytecode::new_raw([SDIV, POP].repeat(r as usize).into());
6246		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6247		let (mut ext, _) = setup.ext();
6248		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6249		setup_stack(&mut interpreter, operands);
6250
6251		evict_caches();
6252		let result;
6253		#[block]
6254		{
6255			result = evm::run_plain(&mut interpreter);
6256		}
6257
6258		let ControlFlow::Break(halt) = result;
6259		assert!(matches!(halt, Halt::Stop));
6260		assert_eq!(interpreter.stack.len(), 0);
6261	}
6262
6263	/// Benchmarks `r` EVM `SDIV` op-codes with pseudo-random mixed one-limb and Knuth divisors.
6264	///
6265	/// # Subtraction Safety
6266	///
6267	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6268	#[benchmark(pov_mode = Measured)]
6269	fn evm_sdiv_opcode_mixed_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6270		let mut rng = Pcg64::seed_from_u64(1337);
6271		let operands = (0..r).flat_map(|_| mixed_division_operands::signed_operands(&mut rng));
6272
6273		let code = Bytecode::new_raw([SDIV, POP].repeat(r as usize).into());
6274		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6275		let (mut ext, _) = setup.ext();
6276		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6277		setup_stack(&mut interpreter, operands);
6278
6279		evict_caches();
6280		let result;
6281		#[block]
6282		{
6283			result = evm::run_plain(&mut interpreter);
6284		}
6285
6286		let ControlFlow::Break(halt) = result;
6287		assert!(matches!(halt, Halt::Stop));
6288		assert_eq!(interpreter.stack.len(), 0);
6289	}
6290
6291	/// Benchmarks `r` EVM `SDIV` op-codes with the selected fixed Knuth divisor.
6292	///
6293	/// # Subtraction Safety
6294	///
6295	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6296	#[benchmark(pov_mode = Measured)]
6297	fn evm_sdiv_opcode_fixed_knuth_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6298		use knuth_division_operands::{SDIV_FIXED_DIVISOR, fixed_signed_operands};
6299
6300		let mut rng = Pcg64::seed_from_u64(1337);
6301		let operands = (0..r).flat_map(|_| fixed_signed_operands(&mut rng, SDIV_FIXED_DIVISOR));
6302
6303		let code = Bytecode::new_raw([SDIV, POP].repeat(r as usize).into());
6304		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6305		let (mut ext, _) = setup.ext();
6306		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6307		setup_stack(&mut interpreter, operands);
6308
6309		evict_caches();
6310		let result;
6311		#[block]
6312		{
6313			result = evm::run_plain(&mut interpreter);
6314		}
6315
6316		let ControlFlow::Break(halt) = result;
6317		assert!(matches!(halt, Halt::Stop));
6318		assert_eq!(interpreter.stack.len(), 0);
6319	}
6320
6321	/// Benchmarks `r` EVM `MOD` op-codes.
6322	///
6323	/// # Added Overheads
6324	///
6325	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6326	/// * A branch misprediction on how many times each quotient limb's estimate is corrected, since
6327	///   the divisor is picked at random from [`knuth_division_operands`].
6328	///
6329	/// # Subtraction Safety
6330	///
6331	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6332	#[benchmark(pov_mode = Measured)]
6333	fn evm_mod_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6334		use knuth_division_operands::DENOMINATORS;
6335
6336		let mut rng = Pcg64::seed_from_u64(1337);
6337		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
6338
6339		let code = Bytecode::new_raw([MOD, POP].repeat(r as usize).into());
6340		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6341		let (mut ext, _) = setup.ext();
6342		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6343		setup_stack(&mut interpreter, operands);
6344
6345		evict_caches();
6346		let result;
6347		#[block]
6348		{
6349			result = evm::run_plain(&mut interpreter);
6350		}
6351
6352		let ControlFlow::Break(halt) = result;
6353		assert!(matches!(halt, Halt::Stop));
6354		assert_eq!(interpreter.stack.len(), 0);
6355	}
6356
6357	/// Benchmarks `r` EVM `MOD` op-codes with pseudo-random one-limb correction patterns.
6358	///
6359	/// # Subtraction Safety
6360	///
6361	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6362	#[benchmark(pov_mode = Measured)]
6363	fn evm_mod_opcode_one_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6364		use one_limb_division_operands::DENOMINATORS;
6365
6366		let mut rng = Pcg64::seed_from_u64(1337);
6367		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
6368
6369		let code = Bytecode::new_raw([MOD, POP].repeat(r as usize).into());
6370		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6371		let (mut ext, _) = setup.ext();
6372		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6373		setup_stack(&mut interpreter, operands);
6374
6375		evict_caches();
6376		let result;
6377		#[block]
6378		{
6379			result = evm::run_plain(&mut interpreter);
6380		}
6381
6382		let ControlFlow::Break(halt) = result;
6383		assert!(matches!(halt, Halt::Stop));
6384		assert_eq!(interpreter.stack.len(), 0);
6385	}
6386
6387	/// Benchmarks `r` EVM `MOD` op-codes with pseudo-random three-limb correction patterns.
6388	///
6389	/// # Subtraction Safety
6390	///
6391	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6392	#[benchmark(pov_mode = Measured)]
6393	fn evm_mod_opcode_three_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6394		use three_limb_division_operands::DENOMINATORS;
6395
6396		let mut rng = Pcg64::seed_from_u64(1337);
6397		let operands = (0..r).flat_map(|_| unsigned_division_operands(&mut rng, &DENOMINATORS));
6398
6399		let code = Bytecode::new_raw([MOD, POP].repeat(r as usize).into());
6400		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6401		let (mut ext, _) = setup.ext();
6402		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6403		setup_stack(&mut interpreter, operands);
6404
6405		evict_caches();
6406		let result;
6407		#[block]
6408		{
6409			result = evm::run_plain(&mut interpreter);
6410		}
6411
6412		let ControlFlow::Break(halt) = result;
6413		assert!(matches!(halt, Halt::Stop));
6414		assert_eq!(interpreter.stack.len(), 0);
6415	}
6416
6417	/// Benchmarks `r` EVM `MOD` op-codes with pseudo-random mixed one-limb and Knuth divisors.
6418	///
6419	/// # Subtraction Safety
6420	///
6421	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6422	#[benchmark(pov_mode = Measured)]
6423	fn evm_mod_opcode_mixed_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6424		let mut rng = Pcg64::seed_from_u64(1337);
6425		let operands = (0..r).flat_map(|_| mixed_division_operands::unsigned_operands(&mut rng));
6426
6427		let code = Bytecode::new_raw([MOD, POP].repeat(r as usize).into());
6428		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6429		let (mut ext, _) = setup.ext();
6430		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6431		setup_stack(&mut interpreter, operands);
6432
6433		evict_caches();
6434		let result;
6435		#[block]
6436		{
6437			result = evm::run_plain(&mut interpreter);
6438		}
6439
6440		let ControlFlow::Break(halt) = result;
6441		assert!(matches!(halt, Halt::Stop));
6442		assert_eq!(interpreter.stack.len(), 0);
6443	}
6444
6445	/// Benchmarks `r` EVM `MOD` op-codes with the selected fixed Knuth divisor.
6446	///
6447	/// # Subtraction Safety
6448	///
6449	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6450	#[benchmark(pov_mode = Measured)]
6451	fn evm_mod_opcode_fixed_knuth_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6452		use knuth_division_operands::{MOD_FIXED_DIVISOR, NUMERATOR};
6453
6454		let operands = (0..r).flat_map(|_| [MOD_FIXED_DIVISOR, NUMERATOR]);
6455		let code = Bytecode::new_raw([MOD, POP].repeat(r as usize).into());
6456		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6457		let (mut ext, _) = setup.ext();
6458		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6459		setup_stack(&mut interpreter, operands);
6460
6461		evict_caches();
6462		let result;
6463		#[block]
6464		{
6465			result = evm::run_plain(&mut interpreter);
6466		}
6467
6468		let ControlFlow::Break(halt) = result;
6469		assert!(matches!(halt, Halt::Stop));
6470		assert_eq!(interpreter.stack.len(), 0);
6471	}
6472
6473	/// Benchmarks `r` EVM `SMOD` op-codes.
6474	///
6475	/// # Added Overheads
6476	///
6477	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6478	/// * A carry through every limb to negate the numerator, since it's the most negative value.
6479	/// * A branch misprediction on how many times each quotient limb's estimate is corrected, since
6480	///   the divisor is picked at random from [`knuth_division_operands`].
6481	/// * A branch misprediction on whether the divisor is negated, since its sign is pseudo-random.
6482	///
6483	/// # Subtraction Safety
6484	///
6485	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6486	#[benchmark(pov_mode = Measured)]
6487	fn evm_smod_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6488		use knuth_division_operands::DENOMINATORS;
6489
6490		let mut rng = Pcg64::seed_from_u64(1337);
6491		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6492
6493		let code = Bytecode::new_raw([SMOD, POP].repeat(r as usize).into());
6494		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6495		let (mut ext, _) = setup.ext();
6496		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6497		setup_stack(&mut interpreter, operands);
6498
6499		evict_caches();
6500		let result;
6501		#[block]
6502		{
6503			result = evm::run_plain(&mut interpreter);
6504		}
6505
6506		let ControlFlow::Break(halt) = result;
6507		assert!(matches!(halt, Halt::Stop));
6508		assert_eq!(interpreter.stack.len(), 0);
6509	}
6510
6511	/// Benchmarks `r` EVM `SMOD` op-codes with pseudo-random one-limb correction patterns.
6512	///
6513	/// # Subtraction Safety
6514	///
6515	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6516	#[benchmark(pov_mode = Measured)]
6517	fn evm_smod_opcode_one_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6518		use one_limb_division_operands::DENOMINATORS;
6519
6520		let mut rng = Pcg64::seed_from_u64(1337);
6521		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6522
6523		let code = Bytecode::new_raw([SMOD, POP].repeat(r as usize).into());
6524		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6525		let (mut ext, _) = setup.ext();
6526		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6527		setup_stack(&mut interpreter, operands);
6528
6529		evict_caches();
6530		let result;
6531		#[block]
6532		{
6533			result = evm::run_plain(&mut interpreter);
6534		}
6535
6536		let ControlFlow::Break(halt) = result;
6537		assert!(matches!(halt, Halt::Stop));
6538		assert_eq!(interpreter.stack.len(), 0);
6539	}
6540
6541	/// Benchmarks `r` EVM `SMOD` op-codes with pseudo-random three-limb correction patterns.
6542	///
6543	/// # Subtraction Safety
6544	///
6545	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6546	#[benchmark(pov_mode = Measured)]
6547	fn evm_smod_opcode_three_limb_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6548		use three_limb_division_operands::DENOMINATORS;
6549
6550		let mut rng = Pcg64::seed_from_u64(1337);
6551		let operands = (0..r).flat_map(|_| signed_division_operands(&mut rng, &DENOMINATORS));
6552
6553		let code = Bytecode::new_raw([SMOD, POP].repeat(r as usize).into());
6554		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6555		let (mut ext, _) = setup.ext();
6556		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6557		setup_stack(&mut interpreter, operands);
6558
6559		evict_caches();
6560		let result;
6561		#[block]
6562		{
6563			result = evm::run_plain(&mut interpreter);
6564		}
6565
6566		let ControlFlow::Break(halt) = result;
6567		assert!(matches!(halt, Halt::Stop));
6568		assert_eq!(interpreter.stack.len(), 0);
6569	}
6570
6571	/// Benchmarks `r` EVM `SMOD` op-codes with pseudo-random mixed one-limb and Knuth divisors.
6572	///
6573	/// # Subtraction Safety
6574	///
6575	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6576	#[benchmark(pov_mode = Measured)]
6577	fn evm_smod_opcode_mixed_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6578		let mut rng = Pcg64::seed_from_u64(1337);
6579		let operands = (0..r).flat_map(|_| mixed_division_operands::signed_operands(&mut rng));
6580
6581		let code = Bytecode::new_raw([SMOD, POP].repeat(r as usize).into());
6582		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6583		let (mut ext, _) = setup.ext();
6584		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6585		setup_stack(&mut interpreter, operands);
6586
6587		evict_caches();
6588		let result;
6589		#[block]
6590		{
6591			result = evm::run_plain(&mut interpreter);
6592		}
6593
6594		let ControlFlow::Break(halt) = result;
6595		assert!(matches!(halt, Halt::Stop));
6596		assert_eq!(interpreter.stack.len(), 0);
6597	}
6598
6599	/// Benchmarks `r` EVM `SMOD` op-codes with the selected fixed Knuth divisor.
6600	///
6601	/// # Subtraction Safety
6602	///
6603	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6604	#[benchmark(pov_mode = Measured)]
6605	fn evm_smod_opcode_fixed_knuth_variant(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6606		use knuth_division_operands::{SMOD_FIXED_DIVISOR, fixed_signed_operands};
6607
6608		let mut rng = Pcg64::seed_from_u64(1337);
6609		let operands = (0..r).flat_map(|_| fixed_signed_operands(&mut rng, SMOD_FIXED_DIVISOR));
6610
6611		let code = Bytecode::new_raw([SMOD, POP].repeat(r as usize).into());
6612		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6613		let (mut ext, _) = setup.ext();
6614		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6615		setup_stack(&mut interpreter, operands);
6616
6617		evict_caches();
6618		let result;
6619		#[block]
6620		{
6621			result = evm::run_plain(&mut interpreter);
6622		}
6623
6624		let ControlFlow::Break(halt) = result;
6625		assert!(matches!(halt, Halt::Stop));
6626		assert_eq!(interpreter.stack.len(), 0);
6627	}
6628
6629	/// Benchmarks `r` EVM `MULMOD` op-codes.
6630	///
6631	/// # Added Overheads
6632	///
6633	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6634	/// * A product that fills every limb, since the top and lowest bits of both operands are set.
6635	/// * A multiply-subtract over all divisor limbs instead of all but the top two, since the top
6636	///   bits of the modulus are clear and division requires a nonzero normalization shift.
6637	/// * A branch misprediction on how many times the division loops, since the size of the modulus
6638	///   is pseudo-random.
6639	/// * A branch misprediction on each adjustment of the modulus's reciprocal, since whether it
6640	///   happens is pseudo-random.
6641	/// * A branch misprediction on the carry steps of the multiplication, since which limbs of one
6642	///   operand are zero is pseudo-random.
6643	///
6644	/// # Subtraction Safety
6645	///
6646	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6647	#[benchmark(pov_mode = Measured)]
6648	fn evm_mulmod_opcode(r: Linear<0, { EVM_STACK_LIMIT / 3 }>) {
6649		let top_bit = U256::one() << 255;
6650		// The division computes the reciprocal of the top two limbs of the shifted modulus from the
6651		// top limb first, and then adjusts it twice for the second limb. These say whether each
6652		// adjustment happens.
6653		let reciprocal_adjustments = |modulus: U256| {
6654			let top = (modulus << modulus.leading_zeros() as usize) >> 128;
6655			let (high, low) = (top.low_u128() >> 64, u128::from(top.low_u64()));
6656			[low > u128::MAX % high, (U256::MAX >> 64) / top < U256::from((u128::MAX - low) / high)]
6657		};
6658		let mut rng = Pcg64::seed_from_u64(1337);
6659		let operands = (0..r).flat_map(|_| {
6660			let limbs = if rng.gen_bool(0.5) { 3 } else { 4 };
6661			let adjustments = [rng.gen_bool(0.5), rng.gen_bool(0.5)];
6662			let modulus = loop {
6663				let modulus = (U256(rng.r#gen()) | top_bit) >> (2 + 64 * (4 - limbs));
6664				if reciprocal_adjustments(modulus) == adjustments {
6665					break modulus;
6666				}
6667			};
6668			let a = U256(rng.r#gen()) | top_bit | U256::one();
6669			let mut b = U256(rng.r#gen()) | top_bit | U256::one();
6670			for limb in 1..=2 {
6671				if rng.gen_bool(0.5) {
6672					b.0[limb] = 0;
6673				}
6674			}
6675			[modulus, b, a]
6676		});
6677
6678		let code = Bytecode::new_raw([MULMOD, POP].repeat(r as usize).into());
6679		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6680		let (mut ext, _) = setup.ext();
6681		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6682		setup_stack(&mut interpreter, operands);
6683
6684		evict_caches();
6685		let result;
6686		#[block]
6687		{
6688			result = evm::run_plain(&mut interpreter);
6689		}
6690
6691		let ControlFlow::Break(halt) = result;
6692		assert!(matches!(halt, Halt::Stop));
6693		assert_eq!(interpreter.stack.len(), 0);
6694	}
6695
6696	/// Benchmarks `r` EVM `EXP` op-codes whose exponents are zero or one.
6697	///
6698	/// # Added Overheads
6699	///
6700	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6701	/// * A branch misprediction on whether each exponent is zero, since it's pseudo-random.
6702	/// * A multiplication for every exponent of one, since the exponents are a pseudo-random mix of
6703	///   zero and one.
6704	///
6705	/// # Subtraction Safety
6706	///
6707	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6708	#[benchmark(pov_mode = Measured)]
6709	fn evm_exp_zero_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
6710		let mut rng = Pcg64::seed_from_u64(1337);
6711		let exponents = (0..r).map(|_| U256::from(u8::from(rng.gen_bool(0.5))));
6712
6713		let code = Bytecode::new_raw(vec![EXP; r as usize].into());
6714		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6715		let (mut ext, _) = setup.ext();
6716		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6717		setup_stack(&mut interpreter, exponents.chain([U256::MAX]));
6718
6719		evict_caches();
6720		let result;
6721		#[block]
6722		{
6723			result = evm::run_plain(&mut interpreter);
6724		}
6725
6726		let ControlFlow::Break(halt) = result;
6727		assert!(matches!(halt, Halt::Stop));
6728		assert_eq!(interpreter.stack.len(), 1);
6729	}
6730
6731	/// Benchmarks `r` EVM `EXP` op-codes whose exponents are one or three.
6732	///
6733	/// # Added Overheads
6734	///
6735	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6736	/// * A branch misprediction on whether the loop over the bits of each exponent runs, since the
6737	///   exponent is pseudo-random.
6738	/// * A squaring and a multiplication for every exponent of three, since the exponents are a
6739	///   pseudo-random mix of one and three.
6740	///
6741	/// # Subtraction Safety
6742	///
6743	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6744	#[benchmark(pov_mode = Measured)]
6745	fn evm_exp_opcode(r: Linear<0, { EVM_STACK_LIMIT - 1 }>) {
6746		let mut rng = Pcg64::seed_from_u64(1337);
6747		let exponents = (0..r).map(|_| U256::from(if rng.gen_bool(0.5) { 3 } else { 1 }));
6748
6749		let code = Bytecode::new_raw(vec![EXP; r as usize].into());
6750		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6751		let (mut ext, _) = setup.ext();
6752		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6753		setup_stack(&mut interpreter, exponents.chain([U256::MAX]));
6754
6755		evict_caches();
6756		let result;
6757		#[block]
6758		{
6759			result = evm::run_plain(&mut interpreter);
6760		}
6761
6762		let ControlFlow::Break(halt) = result;
6763		assert!(matches!(halt, Halt::Stop));
6764		assert_eq!(interpreter.stack.len(), 1);
6765	}
6766
6767	/// Benchmarks one EVM `EXP` op-code whose exponent has `b + 1` bits, all of them set.
6768	///
6769	/// # Added Overheads
6770	///
6771	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6772	/// * A multiplication for every bit of the exponent, since all of them are set.
6773	///
6774	/// # Subtraction Safety
6775	///
6776	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6777	#[benchmark(pov_mode = Measured)]
6778	fn evm_exp_per_bit(b: Linear<0, 255>) {
6779		let exponent = U256::MAX >> (255 - b as usize);
6780
6781		let code = Bytecode::new_raw(vec![EXP].into());
6782		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6783		let (mut ext, _) = setup.ext();
6784		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6785		setup_stack(&mut interpreter, [exponent, U256::MAX]);
6786
6787		evict_caches();
6788		let result;
6789		#[block]
6790		{
6791			result = evm::run_plain(&mut interpreter);
6792		}
6793
6794		let ControlFlow::Break(halt) = result;
6795		assert!(matches!(halt, Halt::Stop));
6796		assert_eq!(interpreter.stack.len(), 1);
6797	}
6798
6799	/// Benchmarks `r` EVM `SIGNEXTEND` op-codes.
6800	///
6801	/// # Added Overheads
6802	///
6803	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6804	/// * A branch misprediction on which limb holds the sign bit, since the index is pseudo-random.
6805	/// * A branch misprediction on how far the subtraction that builds the mask borrows, since the
6806	///   index is pseudo-random.
6807	/// * A branch misprediction on whether the value is extended with ones or zeros, since the
6808	///   value is pseudo-random.
6809	///
6810	/// # Subtraction Safety
6811	///
6812	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6813	#[benchmark(pov_mode = Measured)]
6814	fn evm_signextend_opcode(r: Linear<0, { EVM_STACK_LIMIT / 2 }>) {
6815		let mut rng = Pcg64::seed_from_u64(1337);
6816		let operands = (0..r).flat_map(|_| {
6817			let index = [27, 19, 11, 3].choose(&mut rng).copied().unwrap();
6818			[U256(rng.r#gen()), U256::from(index)]
6819		});
6820
6821		let code = Bytecode::new_raw([SIGNEXTEND, POP].repeat(r as usize).into());
6822		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6823		let (mut ext, _) = setup.ext();
6824		let mut interpreter = Interpreter::new(ExtBytecode::new(code), Vec::new(), &mut ext);
6825		setup_stack(&mut interpreter, operands);
6826
6827		evict_caches();
6828		let result;
6829		#[block]
6830		{
6831			result = evm::run_plain(&mut interpreter);
6832		}
6833
6834		let ControlFlow::Break(halt) = result;
6835		assert!(matches!(halt, Halt::Stop));
6836		assert_eq!(interpreter.stack.len(), 0);
6837	}
6838
6839	/// Benchmarks `r` blocks of a pseudo-random, stack-neutral mix of cheap EVM op-codes.
6840	///
6841	/// Every block runs the same op-codes the same number of times, so what the mix costs beyond
6842	/// the charges of its op-codes is the cost of dispatching to a handler the CPU can't predict.
6843	///
6844	/// # Added Overheads
6845	///
6846	/// * Cold reads, since the L1 and L2 caches are evicted before the benchmark runs.
6847	/// * A misprediction of the handler the interpreter dispatches to, since the op-codes are in a
6848	///   pseudo-random order.
6849	///
6850	/// # Subtraction Safety
6851	///
6852	/// Not safe to subtract. See [`benchmarks`](mod@benchmarks) for what subtraction safety means.
6853	#[benchmark(pov_mode = Measured)]
6854	fn evm_dispatch_mix(
6855		r: Linear<0, { (MAX_INITCODE_SIZE / EvmMixedOpcodes::CODE_BLOCK_SIZE) as u32 }>,
6856	) {
6857		let mut rng = Pcg64::seed_from_u64(1337);
6858		let code = EvmMixedOpcodes::generate_code(&mut rng, r);
6859
6860		let mut setup = CallSetup::<T>::new(VmBinaryModule::evm_noop(0));
6861		let (mut ext, _) = setup.ext();
6862		let bytecode = ExtBytecode::new(Bytecode::new_raw(code.into()));
6863		let mut interpreter = Interpreter::new(bytecode, Vec::new(), &mut ext);
6864		setup_stack(&mut interpreter, vec![U256::MAX; EvmMixedOpcodes::STACK_INITIAL_HEIGHT]);
6865
6866		evict_caches();
6867		let result;
6868		#[block]
6869		{
6870			result = evm::run_plain(&mut interpreter);
6871		}
6872
6873		let ControlFlow::Break(halt) = result;
6874		assert!(matches!(halt, Halt::Stop));
6875		assert_eq!(interpreter.stack.len(), EvmMixedOpcodes::STACK_INITIAL_HEIGHT);
6876	}
6877
6878	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `60` in
6879	/// its slot (see [`prepare_load_chain`]).
6880	#[benchmark(pov_mode = Ignored)]
6881	fn instr_load_chain_0(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6882		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6883		let (mut ext, module) = setup.ext();
6884		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6885		prepare_load_chain(&mut prepared, 0, r);
6886
6887		#[block]
6888		{
6889			prepared.call().unwrap();
6890		}
6891	}
6892
6893	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `52` in
6894	/// its slot (see [`prepare_load_chain`]).
6895	#[benchmark(pov_mode = Ignored)]
6896	fn instr_load_chain_1(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6897		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6898		let (mut ext, module) = setup.ext();
6899		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6900		prepare_load_chain(&mut prepared, 1, r);
6901
6902		#[block]
6903		{
6904			prepared.call().unwrap();
6905		}
6906	}
6907
6908	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `44` in
6909	/// its slot (see [`prepare_load_chain`]).
6910	#[benchmark(pov_mode = Ignored)]
6911	fn instr_load_chain_2(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6912		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6913		let (mut ext, module) = setup.ext();
6914		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6915		prepare_load_chain(&mut prepared, 2, r);
6916
6917		#[block]
6918		{
6919			prepared.call().unwrap();
6920		}
6921	}
6922
6923	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `36` in
6924	/// its slot (see [`prepare_load_chain`]).
6925	#[benchmark(pov_mode = Ignored)]
6926	fn instr_load_chain_3(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6927		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6928		let (mut ext, module) = setup.ext();
6929		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6930		prepare_load_chain(&mut prepared, 3, r);
6931
6932		#[block]
6933		{
6934			prepared.call().unwrap();
6935		}
6936	}
6937
6938	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `28` in
6939	/// its slot (see [`prepare_load_chain`]).
6940	#[benchmark(pov_mode = Ignored)]
6941	fn instr_load_chain_4(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6942		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6943		let (mut ext, module) = setup.ext();
6944		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6945		prepare_load_chain(&mut prepared, 4, r);
6946
6947		#[block]
6948		{
6949			prepared.call().unwrap();
6950		}
6951	}
6952
6953	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `20` in
6954	/// its slot (see [`prepare_load_chain`]).
6955	#[benchmark(pov_mode = Ignored)]
6956	fn instr_load_chain_5(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6957		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6958		let (mut ext, module) = setup.ext();
6959		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6960		prepare_load_chain(&mut prepared, 5, r);
6961
6962		#[block]
6963		{
6964			prepared.call().unwrap();
6965		}
6966	}
6967
6968	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `12` in
6969	/// its slot (see [`prepare_load_chain`]).
6970	#[benchmark(pov_mode = Ignored)]
6971	fn instr_load_chain_6(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6972		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6973		let (mut ext, module) = setup.ext();
6974		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6975		prepare_load_chain(&mut prepared, 6, r);
6976
6977		#[block]
6978		{
6979			prepared.call().unwrap();
6980		}
6981	}
6982
6983	/// Measures one gas of dependent loads from the L2 cache, with every value at offset `4` in
6984	/// its slot (see [`prepare_load_chain`]).
6985	#[benchmark(pov_mode = Ignored)]
6986	fn instr_load_chain_7(r: Linear<1, LOAD_CHAIN_ITERATIONS>) {
6987		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_load_chain());
6988		let (mut ext, module) = setup.ext();
6989		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
6990		prepare_load_chain(&mut prepared, 7, r);
6991
6992		#[block]
6993		{
6994			prepared.call().unwrap();
6995		}
6996	}
6997
6998	/// Measures the time of one gas spent on indirect jumps between basic blocks.
6999	///
7000	/// Each lap runs [`crate::metering::INDIRECT_JUMP_GAS`] gas: the loop condition, the
7001	/// counter increment and one indirect jump into and out of every block of a random cycle
7002	/// (see [`VmBinaryModule::with_indirect_jump_cycle`]). The interpreter data a lap reads stays
7003	/// in the L2 but not in the L1 data cache of the reference hardware, which is where the cost
7004	/// per gas assumes it to be. A warm-up before the first lap compiles every block of the cycle
7005	/// in code order, so the handlers a lap reads are scattered over the interpreter cache like
7006	/// its other data. The first lap compiles the counter increment. Every `r` runs the warm-up
7007	/// and at least one lap, so all compilation is in the intercept and the slope is one lap over
7008	/// compiled blocks. The contract compiles to 6018 cache entries of 24 bytes, 141 KiB of the
7009	/// 1 MiB interpreter cache, so the cache never resets and no block is compiled twice.
7010	///
7011	/// Laps over compiled blocks are the model for the cost per gas. The interpreter drops the
7012	/// compiled blocks of a contract when a PVM frame starts during a call the contract makes, and
7013	/// compiles a block again when execution next enters it. A block is therefore cold only right
7014	/// after its compilation, which `interpreter_compile_per_block` and
7015	/// `interpreter_compile_per_instruction` measure and the interpreter charges every time it
7016	/// compiles a block, and later jumps to it find it compiled.
7017	///
7018	/// [`crate::metering::EngineMeter::ref_time_per_fuel`] divides the slope by
7019	/// [`crate::metering::INDIRECT_JUMP_GAS`].
7020	#[benchmark(pov_mode = Ignored)]
7021	fn instr_indirect_jump(r: Linear<1, INDIRECT_JUMP_LAPS>) {
7022		let mut setup = CallSetup::<T>::new(VmBinaryModule::with_indirect_jump_cycle());
7023		let (mut ext, module) = setup.ext();
7024		let mut prepared = CallSetup::<T>::prepare_call(&mut ext, module, Vec::new());
7025		prepared.set_reg(polkavm::Reg::A1, r.into());
7026
7027		#[block]
7028		{
7029			prepared.call().unwrap();
7030		}
7031	}
7032
7033	#[benchmark(pov_mode = Measured)]
7034	fn extcodecopy(n: Linear<1_000, { 100 * 1024 }>) -> Result<(), BenchmarkError> {
7035		// The caller contract; `CallSetup` whitelists its `AccountInfoOf`.
7036		let mut setup = CallSetup::<T>::new(VmBinaryModule::dummy());
7037		// Copy a contract other than the caller, so its `AccountInfoOf` read is counted.
7038		let target = Contract::<T>::with_index(1, VmBinaryModule::sized(n), vec![])?;
7039
7040		let (mut ext, _) = setup.ext();
7041		let mut interpreter = Interpreter::new(Default::default(), Default::default(), &mut ext);
7042
7043		// Memory growth is charged separately from the copy.
7044		interpreter
7045			.memory
7046			.resize(0, n as usize, interpreter.ext)
7047			.continue_value()
7048			.unwrap();
7049
7050		// Setup stack for extcodecopy instruction: [address, dest_offset, offset, size]
7051		let _ = interpreter.stack.push(U256::from(n));
7052		let _ = interpreter.stack.push(U256::from(0u32));
7053		let _ = interpreter.stack.push(U256::from(0u32));
7054		let _ = interpreter.stack.push(target.address);
7055
7056		let result;
7057		#[block]
7058		{
7059			result = instructions::host::extcodecopy(&mut interpreter);
7060		}
7061
7062		assert!(result.is_continue());
7063		assert_eq!(
7064			*interpreter.memory.slice(0..n as usize),
7065			PristineCode::<T>::get(target.info()?.code_hash).unwrap()[0..n as usize],
7066			"Memory should contain the target contract's code after extcodecopy"
7067		);
7068
7069		Ok(())
7070	}
7071
7072	#[benchmark]
7073	fn v1_migration_step() {
7074		use crate::migrations::v1;
7075		let addr = H160::from([1u8; 20]);
7076		let contract_info = ContractInfo::new(&addr, 1u32.into(), Default::default()).unwrap();
7077
7078		v1::old::ContractInfoOf::<T>::insert(addr, contract_info.clone());
7079		let mut meter = WeightMeter::new();
7080		assert_eq!(AccountInfo::<T>::load_contract(&addr), None);
7081
7082		#[block]
7083		{
7084			v1::Migration::<T>::step(None, &mut meter).unwrap();
7085		}
7086
7087		assert_eq!(v1::old::ContractInfoOf::<T>::get(&addr), None);
7088		assert_eq!(AccountInfo::<T>::load_contract(&addr).unwrap(), contract_info);
7089
7090		// uses twice the weight once for migration and then for checking if there is another key.
7091		assert_eq!(meter.consumed(), <T as Config>::WeightInfo::v1_migration_step() * 2);
7092	}
7093
7094	#[benchmark]
7095	fn v2_migration_step() {
7096		use crate::migrations::v2;
7097		let code_hash = H256::from([0; 32]);
7098		let old_code_info = v2::Migration::<T>::create_old_code_info(
7099			whitelisted_caller(),
7100			1000u32.into(),
7101			1,
7102			100,
7103			0,
7104		);
7105		v2::Migration::<T>::insert_old_code_info(code_hash, old_code_info.clone());
7106		let mut meter = WeightMeter::new();
7107
7108		#[block]
7109		{
7110			v2::Migration::<T>::step(None, &mut meter).unwrap();
7111		}
7112
7113		v2::Migration::<T>::assert_migrated_code_info(code_hash, &old_code_info);
7114
7115		// uses twice the weight once for migration and then for checking if there is another key.
7116		assert_eq!(meter.consumed(), <T as Config>::WeightInfo::v2_migration_step() * 2);
7117	}
7118
7119	#[benchmark]
7120	fn v3_migration_step() {
7121		use crate::migrations::v3;
7122		// Remove all pre-existing accounts
7123		let _ = frame_system::Account::<T>::clear(u32::MAX, None);
7124
7125		let account = account::<T::AccountId>("target", 0, 0);
7126		T::Currency::mint_into(&account, Pallet::<T>::min_balance())
7127			.expect("should mint into account");
7128
7129		// clear the mapping so the migration has work to do
7130		let addr = T::AddressMapper::to_address(&account);
7131		crate::OriginalAccount::<T>::remove(addr);
7132
7133		assert!(!T::AddressMapper::is_mapped(&account));
7134		let mut meter = WeightMeter::new();
7135
7136		#[block]
7137		{
7138			v3::Migration::<T>::step(None, &mut meter).unwrap();
7139		}
7140
7141		assert!(T::AddressMapper::is_mapped(&account));
7142
7143		// uses twice the weight: once for migration and then for checking if there is another key.
7144		assert_eq!(meter.consumed(), <T as Config>::WeightInfo::v3_migration_step() * 2);
7145	}
7146
7147	/// One iteration of v4 phase 1: credit the uploader's [`NativeDepositOf`] bucket.
7148	///
7149	/// Seeds two codes and primes the cursor with the first stored entry so the benched
7150	/// iteration exercises the `iter_from` path that dominates phase 1 in production.
7151	#[benchmark]
7152	fn v4_code_upload_step() {
7153		use crate::migrations::v4;
7154
7155		let _ = CodeInfoOf::<T>::clear(u32::MAX, None);
7156
7157		let owner: T::AccountId = whitelisted_caller();
7158		let deposit: BalanceOf<T> = 1_000u32.into();
7159
7160		let pallet_account = Pallet::<T>::account_id();
7161		T::Currency::mint_into(&pallet_account, Pallet::<T>::min_balance()).unwrap();
7162		T::Currency::mint_into(&pallet_account, deposit).unwrap();
7163		T::Currency::hold(&HoldReason::CodeUploadDepositReserve.into(), &pallet_account, deposit)
7164			.unwrap();
7165
7166		CodeInfoOf::<T>::insert(
7167			H256::from([1u8; 32]),
7168			CodeInfo::<T>::new_with_deposit(owner.clone(), deposit),
7169		);
7170		CodeInfoOf::<T>::insert(
7171			H256::from([2u8; 32]),
7172			CodeInfo::<T>::new_with_deposit(owner.clone(), deposit),
7173		);
7174
7175		let first = match v4::Migration::<T>::step_once(None) {
7176			Some(v4::Cursor::CodeUpload(h)) => h,
7177			other => panic!("expected CodeUpload cursor, got {other:?}"),
7178		};
7179		let cursor = Some(v4::Cursor::CodeUpload(first));
7180
7181		#[block]
7182		{
7183			let _ = v4::Migration::<T>::step_once(cursor);
7184		}
7185
7186		assert_eq!(
7187			NativeDepositOf::<T>::get(&pallet_account, &owner),
7188			deposit + deposit,
7189			"both code uploads credited to owner",
7190		);
7191	}
7192
7193	/// One iteration of v4 phase 2: burn native hold, mint and hold PGAS for a single contract.
7194	///
7195	/// Seeds two contracts and primes the cursor with the first stored entry so the benched
7196	/// iteration exercises the `iter_from` path that dominates phase 2 in production.
7197	#[benchmark]
7198	fn v4_contract_step() {
7199		use crate::migrations::v4;
7200
7201		let _ = AccountInfoOf::<T>::clear(u32::MAX, None);
7202
7203		let code_hash = H256::from([0u8; 32]);
7204		let deposit: BalanceOf<T> = 1_000u32.into();
7205
7206		for byte in [0x41u8, 0x42u8] {
7207			let addr = H160::from([byte; 20]);
7208			let contract_account = T::AddressMapper::to_account_id(&addr);
7209			let info =
7210				ContractInfo::<T>::new(&addr, 1u32.into(), code_hash).expect("fresh contract info");
7211			AccountInfoOf::<T>::insert(
7212				addr,
7213				crate::storage::AccountInfo::<T> {
7214					account_type: crate::storage::AccountType::Contract(info),
7215					dust: 0,
7216				},
7217			);
7218			T::Currency::mint_into(&contract_account, Pallet::<T>::min_balance()).unwrap();
7219			T::Currency::mint_into(&contract_account, deposit).unwrap();
7220			T::Currency::hold(
7221				&HoldReason::StorageDepositReserve.into(),
7222				&contract_account,
7223				deposit,
7224			)
7225			.unwrap();
7226		}
7227
7228		let first = match v4::Migration::<T>::step_once(Some(v4::Cursor::Contract(None))) {
7229			Some(v4::Cursor::Contract(Some(addr))) => addr,
7230			other => panic!("expected Contract cursor, got {other:?}"),
7231		};
7232		let cursor = Some(v4::Cursor::Contract(Some(first)));
7233
7234		#[block]
7235		{
7236			let _ = v4::Migration::<T>::step_once(cursor);
7237		}
7238
7239		// `migrate_native_to_pgas` is a no-op for `Deposit = ()`, so the hold only clears on
7240		// PGAS-backed runtimes. On non-PGAS runtimes the benchmark still measures the iter cost.
7241		if T::Deposit::SUPPORTS_PGAS {
7242			for byte in [0x41u8, 0x42u8] {
7243				let addr = H160::from([byte; 20]);
7244				let contract_account = T::AddressMapper::to_account_id(&addr);
7245				assert_eq!(
7246					T::Currency::balance_on_hold(
7247						&HoldReason::StorageDepositReserve.into(),
7248						&contract_account,
7249					),
7250					0u32.into(),
7251					"native storage deposit burned for {addr:?}",
7252				);
7253			}
7254		}
7255	}
7256
7257	/// One iteration of v4 phase 3: rewrite a legacy [`v4::old::DeletionQueue`] entry into the
7258	/// new [`DeletionQueue`] format.
7259	///
7260	/// Seeds two legacy entries and primes the cursor with the first stored entry so the benched
7261	/// iteration exercises the `iter_from` path.
7262	#[benchmark]
7263	fn v4_deletion_queue_step() {
7264		use crate::migrations::v4;
7265
7266		let _ = v4::old::DeletionQueue::<T>::clear(u32::MAX, None);
7267
7268		let trie_a: TrieId = vec![0xAAu8; 16].try_into().unwrap();
7269		let trie_b: TrieId = vec![0xBBu8; 24].try_into().unwrap();
7270		v4::old::DeletionQueue::<T>::insert(0u32, trie_a);
7271		v4::old::DeletionQueue::<T>::insert(1u32, trie_b);
7272
7273		let first = match v4::Migration::<T>::step_once(Some(v4::Cursor::DeletionQueue(None))) {
7274			Some(v4::Cursor::DeletionQueue(Some(key))) => key,
7275			other => panic!("expected DeletionQueue cursor, got {other:?}"),
7276		};
7277		let cursor = Some(v4::Cursor::DeletionQueue(Some(first)));
7278
7279		#[block]
7280		{
7281			let _ = v4::Migration::<T>::step_once(cursor);
7282		}
7283
7284		assert!(
7285			DeletionQueue::<T>::get(0u32).is_some() && DeletionQueue::<T>::get(1u32).is_some(),
7286			"both legacy entries rewritten into the new format",
7287		);
7288	}
7289
7290	/// Helper function to create a test signer for finalize_block benchmark
7291	fn create_test_signer<T: Config>() -> (T::AccountId, SigningKey, H160) {
7292		use hex_literal::hex;
7293		// dev::alith()
7294		let signer_account_id = hex!("f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac");
7295		let signer_priv_key =
7296			hex!("5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133");
7297
7298		let signer_key = SigningKey::from_bytes(&signer_priv_key.into()).expect("valid key");
7299
7300		let signer_address = H160::from_slice(&signer_account_id);
7301		let signer_caller = T::AddressMapper::to_fallback_account_id(&signer_address);
7302
7303		(signer_caller, signer_key, signer_address)
7304	}
7305
7306	/// Helper function to create and sign a transaction for finalize_block benchmark
7307	fn create_signed_transaction<T: Config>(
7308		signer_key: &SigningKey,
7309		target_address: H160,
7310		value: U256,
7311		input_data: Vec<u8>,
7312	) -> Vec<u8> {
7313		let unsigned_tx: TransactionUnsigned = TransactionLegacyUnsigned {
7314			to: Some(target_address),
7315			value,
7316			chain_id: Some(T::ChainId::get().into()),
7317			input: input_data.into(),
7318			..Default::default()
7319		}
7320		.into();
7321
7322		let hashed_payload = sp_io::hashing::keccak_256(&unsigned_tx.unsigned_payload());
7323		let (signature, recovery_id) =
7324			signer_key.sign_prehash_recoverable(&hashed_payload).expect("signing success");
7325
7326		let mut sig_bytes = [0u8; 65];
7327		sig_bytes[..64].copy_from_slice(&signature.to_bytes());
7328		sig_bytes[64] = recovery_id.to_byte();
7329
7330		let signed_tx = unsigned_tx.with_signature(sig_bytes);
7331
7332		signed_tx.signed_payload()
7333	}
7334
7335	/// Helper function to generate common finalize_block benchmark setup
7336	fn setup_finalize_block_benchmark<T>()
7337	-> Result<(Contract<T>, BalanceOf<T>, U256, SigningKey, BlockNumberFor<T>), BenchmarkError>
7338	where
7339		BalanceOf<T>: Into<U256> + TryFrom<U256>,
7340		T: Config,
7341		MomentOf<T>: Into<U256>,
7342		<T as frame_system::Config>::Hash: frame_support::traits::IsType<H256>,
7343	{
7344		// Setup test signer
7345		let (signer_caller, signer_key, _signer_address) = create_test_signer::<T>();
7346		whitelist_account!(signer_caller);
7347
7348		// Setup contract instance
7349		let instance =
7350			Contract::<T>::with_caller(signer_caller.clone(), VmBinaryModule::dummy(), vec![])?;
7351		let storage_deposit = default_deposit_limit::<T>();
7352		let value = Pallet::<T>::min_balance();
7353		let evm_value =
7354			Pallet::<T>::convert_native_to_evm(BalanceWithDust::new_unchecked::<T>(value, 0));
7355
7356		// Setup block
7357		let current_block = BlockNumberFor::<T>::from(1u32);
7358		frame_system::Pallet::<T>::set_block_number(current_block);
7359
7360		// Where `Deposit` mints through `fungibles` and the runtime mirrors balance changes as
7361		// logs, creating the contract above buffers one. Drop it, so these benchmarks measure the
7362		// transactions and logs they set up themselves and no synthetic transaction on top.
7363		Pallet::<T>::clear_outside_frame_logs();
7364
7365		Ok((instance, storage_deposit, evm_value, signer_key, current_block))
7366	}
7367
7368	/// Benchmark the `on_finalize` hook scaling with number of transactions.
7369	///
7370	/// This benchmark measures the marginal computational cost of adding transactions
7371	/// to a block during finalization, with fixed payload size to isolate transaction
7372	/// count scaling effects.
7373	///
7374	/// ## Parameters:
7375	/// - `n`: Number of transactions in the block (0-200)
7376	///
7377	/// ## Test Setup:
7378	/// - Creates `n` transactions with fixed 100-byte payloads
7379	/// - Pre-populates block builder storage with test data
7380	///
7381	/// ## Usage:
7382	/// Use this with `on_finalize_per_byte` to calculate total cost:
7383	/// `total_cost = base + (n × per_tx_cost) + (total_bytes × per_byte_cost)`
7384	#[benchmark(pov_mode = Measured)]
7385	fn on_finalize_per_transaction(n: Linear<0, 200>) -> Result<(), BenchmarkError> {
7386		let (instance, _storage_deposit, evm_value, signer_key, current_block) =
7387			setup_finalize_block_benchmark::<T>()?;
7388
7389		// Fixed payload size to isolate transaction count effects
7390		let fixed_payload_size = 100usize;
7391
7392		// Pre-populate InflightTransactions with n transactions of fixed size
7393		if n > 0 {
7394			// Initialize block
7395			let _ = Pallet::<T>::on_initialize(current_block);
7396
7397			// Create input data of fixed size for consistent transaction payloads
7398			let input_data = vec![0x42u8; fixed_payload_size];
7399			let receipt_gas_info = ReceiptGasInfo {
7400				gas_used: U256::from(1_000_000),
7401				effective_gas_price: Pallet::<T>::evm_base_fee(),
7402			};
7403
7404			for _ in 0..n {
7405				// Create real signed transaction with fixed-size input data
7406				let signed_transaction = create_signed_transaction::<T>(
7407					&signer_key,
7408					instance.address,
7409					evm_value,
7410					input_data.clone(),
7411				);
7412
7413				// Store transaction
7414				let _ = block_storage::bench_with_ethereum_context(|| {
7415					let (encoded_logs, bloom) =
7416						block_storage::get_receipt_details::<T>().unwrap_or_default();
7417
7418					let block_builder_ir = EthBlockBuilderIR::<T>::get();
7419					let mut block_builder = EthereumBlockBuilder::<T>::from_ir(block_builder_ir);
7420
7421					block_builder.process_transaction(
7422						signed_transaction,
7423						true,
7424						receipt_gas_info.clone(),
7425						encoded_logs,
7426						bloom,
7427					);
7428
7429					EthBlockBuilderIR::<T>::put(block_builder.to_ir());
7430				});
7431			}
7432		}
7433
7434		#[block]
7435		{
7436			// Measure only the finalization cost with n transactions of fixed size
7437			let _ = Pallet::<T>::on_finalize(current_block);
7438		}
7439
7440		// Verify transaction count
7441		assert_eq!(Pallet::<T>::eth_block().transactions.len(), n as usize);
7442
7443		Ok(())
7444	}
7445
7446	/// Benchmark the `on_finalize` hook scaling with transaction payload size.
7447	///
7448	/// This benchmark measures the marginal computational cost of processing
7449	/// larger transaction payloads during finalization, with fixed transaction count
7450	/// to isolate payload size scaling effects.
7451	///
7452	/// ## Parameters:
7453	/// - `d`: Payload size per transaction in bytes (0-1000)
7454	///
7455	/// ## Test Setup:
7456	/// - Creates 10 transactions with payload size `d`
7457	/// - Pre-populates block builder storage with test data
7458	///
7459	/// ## Usage:
7460	/// Use this with `on_finalize_per_transaction` to calculate total cost:
7461	/// `total_cost = base + (n × per_tx_cost) + (total_bytes × per_byte_cost)`
7462	#[benchmark(pov_mode = Measured)]
7463	fn on_finalize_per_transaction_data(d: Linear<0, 1000>) -> Result<(), BenchmarkError> {
7464		let (instance, _storage_deposit, evm_value, signer_key, current_block) =
7465			setup_finalize_block_benchmark::<T>()?;
7466
7467		// Fixed transaction count to isolate payload size effects
7468		let fixed_tx_count = 10u32;
7469
7470		// Initialize block
7471		let _ = Pallet::<T>::on_initialize(current_block);
7472
7473		// Create input data of variable size p for realistic transaction payloads
7474		let input_data = vec![0x42u8; d as usize];
7475		let receipt_gas_info = ReceiptGasInfo {
7476			gas_used: U256::from(1_000_000),
7477			effective_gas_price: Pallet::<T>::evm_base_fee(),
7478		};
7479
7480		for _ in 0..fixed_tx_count {
7481			// Create real signed transaction with variable-size input data
7482			let signed_transaction = create_signed_transaction::<T>(
7483				&signer_key,
7484				instance.address,
7485				evm_value,
7486				input_data.clone(),
7487			);
7488
7489			// Store transaction
7490			let _ = block_storage::bench_with_ethereum_context(|| {
7491				let (encoded_logs, bloom) =
7492					block_storage::get_receipt_details::<T>().unwrap_or_default();
7493
7494				let block_builder_ir = EthBlockBuilderIR::<T>::get();
7495				let mut block_builder = EthereumBlockBuilder::<T>::from_ir(block_builder_ir);
7496
7497				block_builder.process_transaction(
7498					signed_transaction,
7499					true,
7500					receipt_gas_info.clone(),
7501					encoded_logs,
7502					bloom,
7503				);
7504
7505				EthBlockBuilderIR::<T>::put(block_builder.to_ir());
7506			});
7507		}
7508
7509		#[block]
7510		{
7511			// Measure only the finalization cost with fixed count, variable payload size
7512			let _ = Pallet::<T>::on_finalize(current_block);
7513		}
7514
7515		// Verify transaction count
7516		assert_eq!(Pallet::<T>::eth_block().transactions.len(), fixed_tx_count as usize);
7517
7518		Ok(())
7519	}
7520
7521	/// Benchmark the `on_finalize` per-event costs.
7522	///
7523	/// This benchmark measures the computational cost of processing events
7524	/// within the finalization process, isolating the overhead of event count.
7525	/// Uses a single transaction with varying numbers of minimal events.
7526	///
7527	/// ## Parameters:
7528	/// - `e`: Number of events per transaction
7529	///
7530	/// ## Test Setup:
7531	/// - Creates 1 transaction with `e` ContractEmitted events
7532	/// - Each event contains minimal data (no topics, empty data field)
7533	///
7534	/// ## Usage:
7535	/// Measures the per-event processing overhead during finalization
7536	/// - Fixed cost: `on_finalize_per_event(0)` - baseline finalization cost
7537	/// - Per event: `on_finalize_per_event(e)` - linear scaling with event count
7538	#[benchmark(pov_mode = Measured)]
7539	fn on_finalize_per_event(e: Linear<0, 100>) -> Result<(), BenchmarkError> {
7540		let (instance, _storage_deposit, evm_value, signer_key, current_block) =
7541			setup_finalize_block_benchmark::<T>()?;
7542
7543		// Create a single transaction with e events, each with minimal data
7544		let input_data = vec![0x42u8; 100];
7545		let signed_transaction = create_signed_transaction::<T>(
7546			&signer_key,
7547			instance.address,
7548			evm_value,
7549			input_data.clone(),
7550		);
7551
7552		let receipt_gas_info = ReceiptGasInfo {
7553			gas_used: U256::from(1_000_000),
7554			effective_gas_price: Pallet::<T>::evm_base_fee(),
7555		};
7556
7557		// Store transaction
7558		block_storage::bench_with_ethereum_context(|| -> Result<(), BenchmarkError> {
7559			// Captured inside the ethereum context, so each lands on the transaction's own receipt
7560			// — the path `on_finalize_block_per_event` is charged for on every `DepositEvent`. The
7561			// outside-of-frame drain is `outside_frame_log`'s, charged separately.
7562			for _ in 0..e {
7563				block_storage::capture_frame_log::<T>(&instance.address, &vec![], &vec![])?;
7564			}
7565
7566			let (encoded_logs, bloom) =
7567				block_storage::get_receipt_details::<T>().unwrap_or_default();
7568
7569			let block_builder_ir = EthBlockBuilderIR::<T>::get();
7570			let mut block_builder = EthereumBlockBuilder::<T>::from_ir(block_builder_ir);
7571
7572			block_builder.process_transaction(
7573				signed_transaction,
7574				true,
7575				receipt_gas_info.clone(),
7576				encoded_logs,
7577				bloom,
7578			);
7579
7580			EthBlockBuilderIR::<T>::put(block_builder.to_ir());
7581			Ok(())
7582		})?;
7583
7584		#[block]
7585		{
7586			// Initialize block
7587			let _ = Pallet::<T>::on_initialize(current_block);
7588
7589			// Measure the finalization cost with e events
7590			let _ = Pallet::<T>::on_finalize(current_block);
7591		}
7592
7593		// The real transaction only: its `e` logs are on its own receipt, so no synthetic
7594		// transaction is built.
7595		assert_eq!(Pallet::<T>::eth_block().transactions.len(), 1);
7596
7597		Ok(())
7598	}
7599
7600	/// ## Test Setup:
7601	/// - Creates 1 transaction with 1 ContractEmitted event
7602	/// - Event contains `d` total bytes of data across data field and topics, topics first: up to
7603	///   `limits::NUM_EVENT_TOPICS` full 32-byte topics, the remaining bytes in the data field
7604	///
7605	/// ## Usage:
7606	/// Measures the per-byte event data processing overhead during finalization
7607	/// - Fixed cost: `on_finalize_per_event_data(0)` - baseline cost with empty event
7608	/// - Per byte: `on_finalize_per_event_data(d)` - linear scaling with data size
7609	#[benchmark(pov_mode = Measured)]
7610	fn on_finalize_per_event_data(d: Linear<0, 16384>) -> Result<(), BenchmarkError> {
7611		let (instance, _storage_deposit, evm_value, signer_key, current_block) =
7612			setup_finalize_block_benchmark::<T>()?;
7613
7614		// Create a single transaction with one event containing d bytes of data
7615		let input_data = vec![0x42u8; 100];
7616		let signed_transaction = create_signed_transaction::<T>(
7617			&signer_key,
7618			instance.address,
7619			evm_value,
7620			input_data.clone(),
7621		);
7622
7623		let receipt_gas_info = ReceiptGasInfo {
7624			gas_used: U256::from(1_000_000),
7625			effective_gas_price: Pallet::<T>::evm_base_fee(),
7626		};
7627
7628		let d: usize = d.saturated_into();
7629		let max_topics: usize = limits::NUM_EVENT_TOPICS.saturated_into();
7630		let num_topics = max_topics.min(d / H256::len_bytes());
7631		let topics: Vec<H256> = (0..num_topics)
7632			.map(|index| H256::from_low_u64_be(index.saturated_into()))
7633			.collect();
7634		let event_data = vec![0x42u8; d - num_topics * H256::len_bytes()];
7635
7636		// Store transaction
7637		block_storage::bench_with_ethereum_context(|| -> Result<(), BenchmarkError> {
7638			// Captured inside the ethereum context, so the log lands on the transaction's own
7639			// receipt — the path `on_finalize_block_per_event` is charged for on every
7640			// `DepositEvent`. The outside-of-frame drain is `outside_frame_log`'s, charged
7641			// separately.
7642			block_storage::capture_frame_log::<T>(&instance.address, &event_data, &topics)?;
7643
7644			let (encoded_logs, bloom) =
7645				block_storage::get_receipt_details::<T>().unwrap_or_default();
7646
7647			let block_builder_ir = EthBlockBuilderIR::<T>::get();
7648			let mut block_builder = EthereumBlockBuilder::<T>::from_ir(block_builder_ir);
7649
7650			block_builder.process_transaction(
7651				signed_transaction,
7652				true,
7653				receipt_gas_info,
7654				encoded_logs,
7655				bloom,
7656			);
7657
7658			EthBlockBuilderIR::<T>::put(block_builder.to_ir());
7659			Ok(())
7660		})?;
7661
7662		#[block]
7663		{
7664			// Initialize block
7665			let _ = Pallet::<T>::on_initialize(current_block);
7666
7667			// Measure the finalization cost with d bytes of event data
7668			let _ = Pallet::<T>::on_finalize(current_block);
7669		}
7670
7671		// The real transaction only: its log is on its own receipt, so no synthetic transaction is
7672		// built.
7673		assert_eq!(Pallet::<T>::eth_block().transactions.len(), 1);
7674
7675		Ok(())
7676	}
7677
7678	/// Benchmark the `on_finalize` drain of `n` buffered outside-of-frame logs: taking the buffer
7679	/// and folding each log into the synthetic transaction's receipt (RLP + bloom).
7680	///
7681	/// `pov_mode = Measured` because the block builder's storage is unbounded. The buffer itself is
7682	/// whitelisted, so the marginal carries no proof size; a log's proof charge is its entry's
7683	/// bytes, registered where it is buffered. The append is in the emitting pallet's weight, see
7684	/// `buffer_outside_frame_log`.
7685	///
7686	/// Each log is empty (no topics, no data), so the slope is the per-log cost alone, as in
7687	/// `on_finalize_per_event`. The per-topic cost is `outside_frame_log_topics`'s and the per-byte
7688	/// cost `outside_frame_log_data`'s.
7689	#[benchmark(pov_mode = Measured)]
7690	fn outside_frame_log(n: Linear<0, 100>) -> Result<(), BenchmarkError> {
7691		let (instance, _storage_deposit, _evm_value, _signer_key, current_block) =
7692			setup_finalize_block_benchmark::<T>()?;
7693
7694		// Realistic order: `on_initialize` runs before the block's extrinsics buffer any logs.
7695		let _ = Pallet::<T>::on_initialize(current_block);
7696
7697		// Appended straight into the buffer, past the cap: the drain is what is measured, and a
7698		// runtime that keeps the buffer off still needs its weight.
7699		for _ in 0..n {
7700			Pallet::<T>::bench_buffer_outside_frame_log(instance.address, vec![], vec![]);
7701		}
7702
7703		#[block]
7704		{
7705			let _ = Pallet::<T>::on_finalize(current_block);
7706		}
7707
7708		// Only the synthetic transaction, and only when at least one log was buffered.
7709		assert_eq!(Pallet::<T>::eth_block().transactions.len(), (n > 0) as usize);
7710
7711		Ok(())
7712	}
7713
7714	/// Benchmark the `on_finalize` drain of one buffered outside-of-frame log with `t` topics and
7715	/// no data: the `OutsideFrameLogs::take` reads the topics back, the RLP encoding folds them
7716	/// into the synthetic transaction's receipt, and the bloom accrual hashes each of them once.
7717	///
7718	/// Topics and data are measured in separate benchmarks because a benchmark with both components
7719	/// varies one while holding the other at its maximum: the per-topic cost would be measured on
7720	/// top of the full data and fitted away. Pairs with `outside_frame_log` and
7721	/// `outside_frame_log_data`; together they give `OnFinalizeBlockParts::per_outside_frame_log`
7722	/// its per-log, per-topic and per-byte marginals.
7723	#[benchmark(pov_mode = Measured)]
7724	fn outside_frame_log_topics(
7725		t: Linear<0, { limits::NUM_EVENT_TOPICS }>,
7726	) -> Result<(), BenchmarkError> {
7727		let (instance, _storage_deposit, _evm_value, _signer_key, current_block) =
7728			setup_finalize_block_benchmark::<T>()?;
7729
7730		let _ = Pallet::<T>::on_initialize(current_block);
7731
7732		let topics = (0..t).map(|index| H256::from_low_u64_be(index.into())).collect();
7733		Pallet::<T>::bench_buffer_outside_frame_log(instance.address, topics, vec![]);
7734
7735		#[block]
7736		{
7737			let _ = Pallet::<T>::on_finalize(current_block);
7738		}
7739
7740		assert_eq!(Pallet::<T>::eth_block().transactions.len(), 1);
7741
7742		Ok(())
7743	}
7744
7745	/// Benchmark the `on_finalize` drain of one buffered outside-of-frame log with no topics and
7746	/// `d` bytes of data: the `OutsideFrameLogs::take` scales with the value it reads back, and so
7747	/// does the RLP encoding that folds it into the synthetic transaction's receipt.
7748	///
7749	/// `d` goes up to what a contract can emit. The per-topic cost is `outside_frame_log_topics`'s.
7750	#[benchmark(pov_mode = Measured)]
7751	fn outside_frame_log_data(d: Linear<0, { limits::EVENT_BYTES }>) -> Result<(), BenchmarkError> {
7752		let (instance, _storage_deposit, _evm_value, _signer_key, current_block) =
7753			setup_finalize_block_benchmark::<T>()?;
7754
7755		let _ = Pallet::<T>::on_initialize(current_block);
7756
7757		let data = vec![0x42u8; d.saturated_into()];
7758		Pallet::<T>::bench_buffer_outside_frame_log(instance.address, vec![], data);
7759
7760		#[block]
7761		{
7762			let _ = Pallet::<T>::on_finalize(current_block);
7763		}
7764
7765		assert_eq!(Pallet::<T>::eth_block().transactions.len(), 1);
7766
7767		Ok(())
7768	}
7769
7770	impl_benchmark_test_suite!(
7771		Contracts,
7772		crate::tests::ExtBuilder::default().build(),
7773		crate::tests::Test,
7774	);
7775}