Skip to content

Commit

Permalink
Add genesis param to customize collector address
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jun 12, 2024
1 parent d60fcc9 commit fb342b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ clap = { version = "4.5.6", features = ["derive"] }
alloy-sol-macro = "0.7.6"
alloy-sol-types = "0.7.6"
revm = "9.0.0"
serde_json = "1.0.117"

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { version = "0.5.0", optional = true }
Expand Down
31 changes: 18 additions & 13 deletions src/evm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ use reth_evm_ethereum::EthEvmConfig;
use revm::handler::mainnet::reward_beneficiary as reward_beneficiary_mainnet;
use std::sync::Arc;

// TODO: Define a per-network collector address configurable via genesis file
const COLLECTOR_ADDRESS: Address = address!("6BBe78ee9e474842Dbd4AB4987b3CeFE88426A92");

/// Reward beneficiary with gas fee.
#[inline]
pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
gas: &Gas,
collector_address: Address,
) -> Result<(), EVMError<DB::Error>> {
reward_beneficiary_mainnet::<SPEC, EXT, DB>(context, gas)?;
if SPEC::enabled(SpecId::LONDON) {
mint_basefee_to_collector_address::<EXT, DB>(context, gas)?;
mint_basefee_to_collector_address::<EXT, DB>(context, gas, collector_address)?;
}
Ok(())
}
Expand All @@ -33,6 +31,7 @@ pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(
pub fn mint_basefee_to_collector_address<EXT, DB: Database>(
context: &mut Context<EXT, DB>,
gas: &Gas,
collector_address: Address,
) -> Result<(), EVMError<DB::Error>> {
// TODO: Define a per-network collector address configurable via genesis file
let base_fee = context.evm.env.block.basefee;
Expand All @@ -42,7 +41,7 @@ pub fn mint_basefee_to_collector_address<EXT, DB: Database>(
.evm
.inner
.journaled_state
.load_account(COLLECTOR_ADDRESS, &mut context.evm.inner.db)?;
.load_account(collector_address, &mut context.evm.inner.db)?;

collector_account.mark_touch();
collector_account.info.balance = collector_account
Expand All @@ -54,19 +53,23 @@ pub fn mint_basefee_to_collector_address<EXT, DB: Database>(
}

/// Custom EVM configuration
#[derive(Debug, Clone, Copy, Default)]
pub struct GnosisEvmConfig;
#[derive(Debug, Clone, Copy)]
pub struct GnosisEvmConfig {
pub collector_address: Address,
}

impl ConfigureEvm for GnosisEvmConfig {
type DefaultExternalContext<'a> = ();

fn evm<'a, DB: Database + 'a>(&self, db: DB) -> Evm<'a, Self::DefaultExternalContext<'a>, DB> {
let collector_address = self.collector_address;
EvmBuilder::default()
.with_db(db)
.append_handler_register_box(Box::new(|h| {
.append_handler_register_box(Box::new(move |h| {
spec_to_generic!(h.spec_id(), {
h.post_execution.reward_beneficiary =
Arc::new(reward_beneficiary::<SPEC, (), DB>);
h.post_execution.reward_beneficiary = Arc::new(move |context, gas| {
reward_beneficiary::<SPEC, (), DB>(context, gas, collector_address)
});
});
}))
.build()
Expand All @@ -77,13 +80,15 @@ impl ConfigureEvm for GnosisEvmConfig {
DB: Database + 'a,
I: GetInspector<DB>,
{
let collector_address = self.collector_address;
EvmBuilder::default()
.with_db(db)
.with_external_context(inspector)
.append_handler_register_box(Box::new(|h| {
.append_handler_register_box(Box::new(move |h| {
spec_to_generic!(h.spec_id(), {
h.post_execution.reward_beneficiary =
Arc::new(reward_beneficiary::<SPEC, I, DB>);
h.post_execution.reward_beneficiary = Arc::new(move |context, gas| {
reward_beneficiary::<SPEC, I, DB>(context, gas, collector_address)
});
});
}))
.append_handler_register(inspector_handle_register)
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use evm_config::GnosisEvmConfig;
use execute::GnosisExecutorProvider;
use eyre::eyre;
use reth::{
api::NodeTypes,
builder::{
Expand Down Expand Up @@ -98,7 +99,18 @@ where
ctx: &BuilderContext<Node>,
) -> eyre::Result<(Self::EVM, Self::Executor)> {
let chain_spec = ctx.chain_spec();
let evm_config = GnosisEvmConfig;
let collector_address = ctx
.config()
.chain
.genesis()
.config
.extra_fields
.get("eip1559collector")
.ok_or(eyre!("no eip1559collector field"))?;

let evm_config = GnosisEvmConfig {
collector_address: serde_json::from_value(collector_address.clone())?,
};
let executor = GnosisExecutorProvider::new(chain_spec, evm_config);

Ok((evm_config, executor))
Expand Down

0 comments on commit fb342b0

Please sign in to comment.