From 36d139930679e30eb32cc643bfe70872e8161a67 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 24 Nov 2025 16:46:38 +0800 Subject: [PATCH 1/5] update minimum gas used --- crates/evm/src/eth/block.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/evm/src/eth/block.rs b/crates/evm/src/eth/block.rs index 4a846bd5..ac77ca18 100644 --- a/crates/evm/src/eth/block.rs +++ b/crates/evm/src/eth/block.rs @@ -134,7 +134,16 @@ where self.system_caller.on_state(StateChangeSource::Transaction(self.receipts.len()), &state); - let gas_used = result.gas_used(); + let raw_gas_used = result.gas_used(); + + // Ensure each transaction uses at least 80% of its gas limit + let tx_gas_limit = tx.tx().gas_limit(); + let min_gas_used = (tx_gas_limit * 4) / 5; // 80% of tx gas_limit + let gas_used = if raw_gas_used < min_gas_used { + min_gas_used + } else { + raw_gas_used + }; // append gas used self.gas_used += gas_used; From 919fbd259188f69678a02b78f005ac9557c112f9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 4 Dec 2025 10:19:52 +0800 Subject: [PATCH 2/5] fix gas used deduction --- crates/evm/src/eth/block.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/evm/src/eth/block.rs b/crates/evm/src/eth/block.rs index ac77ca18..45bd3f54 100644 --- a/crates/evm/src/eth/block.rs +++ b/crates/evm/src/eth/block.rs @@ -130,7 +130,7 @@ where output: ResultAndState<::HaltReason>, tx: impl ExecutableTx, ) -> Result { - let ResultAndState { result, state } = output; + let ResultAndState { result, mut state } = output; self.system_caller.on_state(StateChangeSource::Transaction(self.receipts.len()), &state); @@ -145,6 +145,21 @@ where raw_gas_used }; + // If actual gas used is less than 80%, deduct additional balance from sender + if raw_gas_used < min_gas_used { + let extra_gas = min_gas_used - raw_gas_used; + // Calculate effective gas price + let base_fee = self.evm.block().basefee; + let effective_gas_price = tx.tx().effective_gas_price(Some(base_fee as u64)); + let extra_cost = alloy_primitives::U256::from(extra_gas) * alloy_primitives::U256::from(effective_gas_price); + + // Deduct extra cost from sender's balance in state + let sender = *tx.signer(); + if let Some(account) = state.get_mut(&sender) { + account.info.balance = account.info.balance.saturating_sub(extra_cost); + } + } + // append gas used self.gas_used += gas_used; From e29507f19eb2eb6e10e1a26fa2da91a8778a8870 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 4 Dec 2025 16:04:54 +0800 Subject: [PATCH 3/5] update --- crates/evm/src/eth/block.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/evm/src/eth/block.rs b/crates/evm/src/eth/block.rs index 45bd3f54..29c96e72 100644 --- a/crates/evm/src/eth/block.rs +++ b/crates/evm/src/eth/block.rs @@ -146,18 +146,29 @@ where }; // If actual gas used is less than 80%, deduct additional balance from sender + // and add additional tip to coinbase (miner) if raw_gas_used < min_gas_used { let extra_gas = min_gas_used - raw_gas_used; - // Calculate effective gas price let base_fee = self.evm.block().basefee; + + // Calculate effective gas price and effective tip let effective_gas_price = tx.tx().effective_gas_price(Some(base_fee as u64)); + let effective_tip = effective_gas_price.saturating_sub(base_fee as u128); + let extra_cost = alloy_primitives::U256::from(extra_gas) * alloy_primitives::U256::from(effective_gas_price); + let extra_tip = alloy_primitives::U256::from(extra_gas) * alloy_primitives::U256::from(effective_tip); // Deduct extra cost from sender's balance in state let sender = *tx.signer(); if let Some(account) = state.get_mut(&sender) { account.info.balance = account.info.balance.saturating_sub(extra_cost); } + + // Add extra tip to coinbase (miner) balance in state + let coinbase = self.evm.block().beneficiary; + if let Some(account) = state.get_mut(&coinbase) { + account.info.balance = account.info.balance.saturating_add(extra_tip); + } } // append gas used From 7b56e1fc4f681e50d611748740e9cf5b5a6b4a1d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 5 Dec 2025 14:13:23 +0800 Subject: [PATCH 4/5] fix --- crates/evm/src/eth/block.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/evm/src/eth/block.rs b/crates/evm/src/eth/block.rs index 29c96e72..2be18d2b 100644 --- a/crates/evm/src/eth/block.rs +++ b/crates/evm/src/eth/block.rs @@ -159,15 +159,28 @@ where let extra_tip = alloy_primitives::U256::from(extra_gas) * alloy_primitives::U256::from(effective_tip); // Deduct extra cost from sender's balance in state + // Sender should always be in state after EVM execution (gas was deducted) let sender = *tx.signer(); if let Some(account) = state.get_mut(&sender) { account.info.balance = account.info.balance.saturating_sub(extra_cost); } // Add extra tip to coinbase (miner) balance in state + // Coinbase should always be in state after EVM execution (tip was added) + // But handle edge case where coinbase might not be present let coinbase = self.evm.block().beneficiary; - if let Some(account) = state.get_mut(&coinbase) { - account.info.balance = account.info.balance.saturating_add(extra_tip); + match state.get_mut(&coinbase) { + Some(account) => { + account.info.balance = account.info.balance.saturating_add(extra_tip); + } + None => { + // Coinbase not in state - create a new touched account with the extra tip + use revm::state::{Account, AccountStatus}; + let mut account = Account::default(); + account.info.balance = extra_tip; + account.status = AccountStatus::Touched; + state.insert(coinbase, account); + } } } From 888b6a80c571727db57e8c9a287743ae9b62f93b Mon Sep 17 00:00:00 2001 From: pinardZ Date: Thu, 4 Dec 2025 10:46:18 +0800 Subject: [PATCH 5/5] feat: apply staking distribution --- crates/evm/src/eth/block.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/evm/src/eth/block.rs b/crates/evm/src/eth/block.rs index 2be18d2b..b3b1c38d 100644 --- a/crates/evm/src/eth/block.rs +++ b/crates/evm/src/eth/block.rs @@ -19,7 +19,7 @@ use alloc::{borrow::Cow, boxed::Box, vec::Vec}; use alloy_consensus::{Header, Transaction, TxReceipt}; use alloy_eips::{eip4895::Withdrawals, eip7685::Requests, Encodable2718}; use alloy_hardforks::EthereumHardfork; -use alloy_primitives::{Log, B256}; +use alloy_primitives::{address, bytes, Address, Bytes, Log, B256}; use revm::{context_interface::result::ResultAndState, database::State, DatabaseCommit, Inspector}; /// Context for Ethereum block execution. @@ -257,6 +257,28 @@ where .increment_balances(balance_increments.clone()) .map_err(|_| BlockValidationError::IncrementBalanceFailed)?; + if let Some(withdrawals) = self.ctx.withdrawals.as_deref() { + if withdrawals.len() > 1 && withdrawals[0].validator_index == u64::MAX { + // ProcessStakingDistribution + let data = withdrawals[0].amount_wei().to_be_bytes::<32>(); + // Bytes::from(value) + match self.evm.transact_system_call( + address!("fffffffffffffffffffffffffffffffffffffffe"), + withdrawals[0].address, + Bytes::from(data), + ) { + Ok(res) => { + self.evm.db_mut().commit(res.state); + }, + Err(e) => { + print!("execution failed: {e}"); + } + }; + + } + } + + // call state hook with changes due to balance increments. self.system_caller.try_on_state_with(|| { balance_increment_state(&balance_increments, self.evm.db_mut()).map(|state| {