Skip to content

Commit

Permalink
refactor: reduce number of args for post_block_balance_increments (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jun 28, 2024
1 parent e81c079 commit 87cdfb1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
14 changes: 3 additions & 11 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use reth_evm::{
};
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{
BlockNumber, BlockWithSenders, EthereumHardfork, Header, Receipt, Request, Withdrawals, U256,
BlockNumber, BlockWithSenders, EthereumHardfork, Header, Receipt, Request, U256,
};
use reth_prune_types::PruneModes;
use reth_revm::{
Expand Down Expand Up @@ -328,16 +328,8 @@ where
block: &BlockWithSenders,
total_difficulty: U256,
) -> Result<(), BlockExecutionError> {
let mut balance_increments = post_block_balance_increments(
self.chain_spec(),
block.number,
block.difficulty,
block.beneficiary,
block.timestamp,
total_difficulty,
&block.ommers,
block.withdrawals.as_ref().map(Withdrawals::as_ref),
);
let mut balance_increments =
post_block_balance_increments(self.chain_spec(), block, total_difficulty);

// Irregular state change at Ethereum DAO hardfork
if self.chain_spec().fork(EthereumHardfork::Dao).transitions_at_block(block.number) {
Expand Down
16 changes: 3 additions & 13 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use reth_evm::{
};
use reth_execution_types::ExecutionOutcome;
use reth_optimism_consensus::validate_block_post_execution;
use reth_primitives::{
BlockNumber, BlockWithSenders, Header, Receipt, Receipts, TxType, Withdrawals, U256,
};
use reth_primitives::{BlockNumber, BlockWithSenders, Header, Receipt, Receipts, TxType, U256};
use reth_prune_types::PruneModes;
use reth_revm::{
batch::{BlockBatchRecord, BlockExecutorStats},
Expand Down Expand Up @@ -324,16 +322,8 @@ where
block: &BlockWithSenders,
total_difficulty: U256,
) -> Result<(), BlockExecutionError> {
let balance_increments = post_block_balance_increments(
self.chain_spec(),
block.number,
block.difficulty,
block.beneficiary,
block.timestamp,
total_difficulty,
&block.ommers,
block.withdrawals.as_ref().map(Withdrawals::as_ref),
);
let balance_increments =
post_block_balance_increments(self.chain_spec(), block, total_difficulty);
// increment balances
self.state
.increment_balances(balance_increments)
Expand Down
24 changes: 9 additions & 15 deletions crates/revm/src/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use reth_primitives::{
fill_tx_env_with_beacon_root_contract_call,
fill_tx_env_with_withdrawal_requests_contract_call,
},
Address, Header, Request, Withdrawal, B256, U256,
Address, Block, Request, Withdrawal, Withdrawals, B256, U256,
};
use reth_storage_errors::provider::ProviderError;
use revm::{
Expand All @@ -36,40 +36,34 @@ use std::collections::HashMap;
///
/// Balance changes might include the block reward, uncle rewards, withdrawals, or irregular
/// state changes (DAO fork).
#[allow(clippy::too_many_arguments)]
#[inline]
pub fn post_block_balance_increments(
chain_spec: &ChainSpec,
block_number: u64,
block_difficulty: U256,
beneficiary: Address,
block_timestamp: u64,
block: &Block,
total_difficulty: U256,
ommers: &[Header],
withdrawals: Option<&[Withdrawal]>,
) -> HashMap<Address, u128> {
let mut balance_increments = HashMap::new();

// Add block rewards if they are enabled.
if let Some(base_block_reward) =
calc::base_block_reward(chain_spec, block_number, block_difficulty, total_difficulty)
calc::base_block_reward(chain_spec, block.number, block.difficulty, total_difficulty)
{
// Ommer rewards
for ommer in ommers {
for ommer in &block.ommers {
*balance_increments.entry(ommer.beneficiary).or_default() +=
calc::ommer_reward(base_block_reward, block_number, ommer.number);
calc::ommer_reward(base_block_reward, block.number, ommer.number);
}

// Full block reward
*balance_increments.entry(beneficiary).or_default() +=
calc::block_reward(base_block_reward, ommers.len());
*balance_increments.entry(block.beneficiary).or_default() +=
calc::block_reward(base_block_reward, block.ommers.len());
}

// process withdrawals
insert_post_block_withdrawals_balance_increments(
chain_spec,
block_timestamp,
withdrawals,
block.timestamp,
block.withdrawals.as_ref().map(Withdrawals::as_ref),
&mut balance_increments,
);

Expand Down

0 comments on commit 87cdfb1

Please sign in to comment.