From 1b52f649e9cf0c256495ee1c639f31caa357b0d6 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Tue, 1 Apr 2025 10:08:17 +0200 Subject: [PATCH] EVM Kernel: Add cumulative execution gas to EVM BIPs --- etherlink/CHANGES_KERNEL.md | 1 + etherlink/kernel_latest/kernel/src/apply.rs | 2 ++ etherlink/kernel_latest/kernel/src/block.rs | 2 ++ .../kernel/src/block_in_progress.rs | 26 +++++++++++++++---- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/etherlink/CHANGES_KERNEL.md b/etherlink/CHANGES_KERNEL.md index f0ba9da4465b..74ba24436209 100644 --- a/etherlink/CHANGES_KERNEL.md +++ b/etherlink/CHANGES_KERNEL.md @@ -31,6 +31,7 @@ - The EVM version is now written in the durable storage at `/evm/evm_version`. (!17202) - Replaced the custom ABI encoder/decoder with `alloy`'s encoder/decoder for improved maintainability. (!16670) +- Keeps track of execution gas in addition to estimated ticks. (!17507) ## Calypso (6046630) diff --git a/etherlink/kernel_latest/kernel/src/apply.rs b/etherlink/kernel_latest/kernel/src/apply.rs index 00f686919883..f9b5bd0280d4 100644 --- a/etherlink/kernel_latest/kernel/src/apply.rs +++ b/etherlink/kernel_latest/kernel/src/apply.rs @@ -509,6 +509,7 @@ pub struct ExecutionInfo { pub receipt_info: TransactionReceiptInfo, pub object_info: TransactionObjectInfo, pub estimated_ticks_used: u64, + pub execution_gas_used: U256, } pub enum ExecutionResult { @@ -592,6 +593,7 @@ pub fn handle_transaction_result( receipt_info, object_info, estimated_ticks_used: ticks_used, + execution_gas_used: gas_used, }) } diff --git a/etherlink/kernel_latest/kernel/src/block.rs b/etherlink/kernel_latest/kernel/src/block.rs index 837562a727ac..797b333505f1 100644 --- a/etherlink/kernel_latest/kernel/src/block.rs +++ b/etherlink/kernel_latest/kernel/src/block.rs @@ -204,6 +204,7 @@ fn compute( receipt_info, object_info, estimated_ticks_used, + execution_gas_used, }) => { if transaction.is_delayed() { block_in_progress.register_delayed_transaction(transaction.tx_hash); @@ -214,6 +215,7 @@ fn compute( object_info, receipt_info, estimated_ticks_used, + execution_gas_used, host, )?; log!( diff --git a/etherlink/kernel_latest/kernel/src/block_in_progress.rs b/etherlink/kernel_latest/kernel/src/block_in_progress.rs index cd075b025a89..c608b9d5d04a 100644 --- a/etherlink/kernel_latest/kernel/src/block_in_progress.rs +++ b/etherlink/kernel_latest/kernel/src/block_in_progress.rs @@ -65,6 +65,8 @@ pub struct EthBlockInProgress { pub previous_receipts_root: OwnedHash, /// Transactions root pub previous_transactions_root: OwnedHash, + // Unit of gas used to execute transactions + pub cumulative_execution_gas: U256, } impl Encodable for EthBlockInProgress { @@ -85,8 +87,9 @@ impl Encodable for EthBlockInProgress { base_fee_per_gas, previous_receipts_root, previous_transactions_root, + cumulative_execution_gas, } = self; - stream.begin_list(14); + stream.begin_list(15); stream.append(number); append_queue(stream, tx_queue); append_txs(stream, valid_txs); @@ -101,6 +104,7 @@ impl Encodable for EthBlockInProgress { stream.append(base_fee_per_gas); stream.append(previous_receipts_root); stream.append(previous_transactions_root); + stream.append(cumulative_execution_gas); } } @@ -123,7 +127,7 @@ impl Decodable for EthBlockInProgress { if !decoder.is_list() { return Err(DecoderError::RlpExpectedToBeList); } - if decoder.item_count()? != 14 { + if decoder.item_count()? != 15 { return Err(DecoderError::RlpIncorrectListLen); } @@ -145,6 +149,8 @@ impl Decodable for EthBlockInProgress { decode_field(&next(&mut it)?, "previous_receipts_root")?; let previous_transactions_root: OwnedHash = decode_field(&next(&mut it)?, "previous_transactions_root")?; + let cumulative_execution_gas: U256 = + decode_field(&next(&mut it)?, "cumulative_execution_gas")?; let bip = Self { number, @@ -162,6 +168,7 @@ impl Decodable for EthBlockInProgress { base_fee_per_gas, previous_receipts_root, previous_transactions_root, + cumulative_execution_gas, }; Ok(bip) } @@ -225,6 +232,7 @@ impl EthBlockInProgress { base_fee_per_gas, previous_receipts_root, previous_transactions_root, + cumulative_execution_gas: U256::zero(), } } @@ -324,6 +332,7 @@ impl EthBlockInProgress { object_info: TransactionObjectInfo, receipt_info: TransactionReceiptInfo, ticks_used: u64, + execution_gas_used: U256, host: &mut Host, ) -> Result<(), anyhow::Error> { // account for gas @@ -372,6 +381,9 @@ impl EthBlockInProgress { receipt_bloom_size, )); + // keep track of execution gas used + self.cumulative_execution_gas += execution_gas_used; + Ok(()) } @@ -654,10 +666,11 @@ mod tests { base_fee_per_gas: U256::from(21000u64), previous_receipts_root: vec![0; 32], previous_transactions_root: vec![0; 32], + cumulative_execution_gas: U256::from(1), }; let encoded = bip.rlp_bytes(); - let expected = "f902a42af8e6f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f871a00808080808080808080808080808080808080808080808080808080808080808f84e01b84bf84908080880088034a00808080808080808080808080808080808080808080808080808080808080808a00808080808080808080808080808080808080808080808080808080808080808f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; + let expected = "f902a52af8e6f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f871a00808080808080808080808080808080808080808080808080808080808080808f84e01b84bf84908080880088034a00808080808080808080808080808080808080808080808080808080808080808a00808080808080808080808080808080808080808080808080808080808080808f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000001"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); @@ -692,10 +705,11 @@ mod tests { base_fee_per_gas: U256::from(21000u64), previous_receipts_root: vec![0; 32], previous_transactions_root: vec![0; 32], + cumulative_execution_gas: U256::from(1), }; let encoded = bip.rlp_bytes(); - let expected = "f9025b2af87cf83ca00101010101010101010101010101010101010101010101010101010101010101da02d8019401010101010101010101010101010101010101010180f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909e1a002020202020202020202020202020202020202020202020202020202020202020304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; + let expected = "f9025c2af87cf83ca00101010101010101010101010101010101010101010101010101010101010101da02d8019401010101010101010101010101010101010101010180f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909e1a002020202020202020202020202020202020202020202020202020202020202020304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000001"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); @@ -730,10 +744,12 @@ mod tests { base_fee_per_gas: U256::from(21000u64), previous_receipts_root: vec![0; 32], previous_transactions_root: vec![0; 32], + cumulative_execution_gas: U256::from(1), }; let encoded = bip.rlp_bytes(); - let expected = "f9026f2af8b1f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; + let expected = + "f902702af8b1f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000001"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); -- GitLab