Skip to content

Commit

Permalink
Make block rewards contract address a param (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jul 13, 2024
1 parent e067a3c commit c48bfc0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
33 changes: 28 additions & 5 deletions src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ethereum::{EthEvmExecutor, EthExecuteOutput};
use crate::gnosis::{apply_block_rewards_contract_call, apply_withdrawals_contract_call};
use eyre::eyre;
use reth::providers::ExecutionOutcome;
use reth::{
api::ConfigureEvm,
Expand All @@ -26,15 +27,28 @@ use std::{collections::HashMap, sync::Arc};
pub struct GnosisExecutorProvider<EvmConfig: Clone> {
chain_spec: Arc<ChainSpec>,
evm_config: EvmConfig,
/// AuRa BlockRewards contract address for its system call
block_rewards_contract: Address,
}

impl<EvmConfig: Clone> GnosisExecutorProvider<EvmConfig> {
/// Creates a new executor provider.
pub fn new(chain_spec: Arc<ChainSpec>, evm_config: EvmConfig) -> Self {
Self {
pub fn new(chain_spec: Arc<ChainSpec>, evm_config: EvmConfig) -> eyre::Result<Self> {
let block_rewards_contract = chain_spec
.genesis()
.config
.extra_fields
.get("blockRewardsContract")
.ok_or(eyre!("blockRewardsContract not defined"))?;
let block_rewards_contract: Address =
serde_json::from_value(block_rewards_contract.clone())
.map_err(|e| BlockExecutionError::Other(Box::new(e)))?;

Ok(Self {
chain_spec,
evm_config,
}
block_rewards_contract,
})
}
}

Expand All @@ -48,6 +62,7 @@ where
DB: Database<Error: Into<ProviderError> + Display>,
{
GnosisBlockExecutor::new(
self.block_rewards_contract,
self.chain_spec.clone(),
self.evm_config.clone(),
State::builder()
Expand Down Expand Up @@ -98,18 +113,26 @@ pub struct GnosisBlockExecutor<EvmConfig, DB> {
executor: EthEvmExecutor<EvmConfig>,
/// The state to use for execution
state: State<DB>,
/// AuRa BlockRewards contract address for its system call
block_rewards_contract: Address,
}

// [Gnosis/fork] Copy paste code from crates/ethereum/evm/src/execute.rs::EthBlockExecutor
impl<EvmConfig, DB> GnosisBlockExecutor<EvmConfig, DB> {
/// Creates a new Ethereum block executor.
pub fn new(chain_spec: Arc<ChainSpec>, evm_config: EvmConfig, state: State<DB>) -> Self {
pub fn new(
block_rewards_contract: Address,
chain_spec: Arc<ChainSpec>,
evm_config: EvmConfig,
state: State<DB>,
) -> Self {
Self {
executor: EthEvmExecutor {
chain_spec,
evm_config,
},
state,
block_rewards_contract,
}
}

Expand Down Expand Up @@ -225,7 +248,7 @@ where
let mut evm = self.executor.evm_config.evm_with_env(&mut self.state, env);

apply_block_rewards_contract_call(
&chain_spec,
self.block_rewards_contract,
block.timestamp,
block.beneficiary,
&mut evm,
Expand Down
7 changes: 2 additions & 5 deletions src/gnosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ use reth_evm::execute::BlockExecutionError;

pub const SYSTEM_ADDRESS: Address = address!("fffffffffffffffffffffffffffffffffffffffe");

// TODO: customize from genesis or somewhere
pub const BLOCK_REWARDS_CONTRACT: Address = address!("481c034c6d9441db23ea48de68bcae812c5d39ba");

// Codegen from https://github.com/gnosischain/specs/blob/master/execution/withdrawals.md
sol!(
function executeSystemWithdrawals(
Expand Down Expand Up @@ -108,7 +105,7 @@ where
/// Ref: <https://github.com/gnosischain/specs/blob/master/execution/posdao-post-merge.md>
#[inline]
pub fn apply_block_rewards_contract_call<EXT, DB: Database + DatabaseCommit>(
_chain_spec: &ChainSpec,
block_rewards_contract: Address,
_block_timestamp: u64,
coinbase: Address,
evm: &mut Evm<'_, EXT, DB>,
Expand All @@ -123,7 +120,7 @@ where
fill_tx_env_with_system_contract_call(
&mut evm.context.evm.env,
SYSTEM_ADDRESS,
BLOCK_REWARDS_CONTRACT,
block_rewards_contract,
rewardCall {
benefactors: vec![coinbase],
// Type 0 = RewardAuthor
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
let evm_config = GnosisEvmConfig {
collector_address: serde_json::from_value(collector_address.clone())?,
};
let executor = GnosisExecutorProvider::new(chain_spec, evm_config);
let executor = GnosisExecutorProvider::new(chain_spec, evm_config)?;

Ok((evm_config, executor))
}
Expand Down

0 comments on commit c48bfc0

Please sign in to comment.