Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mint basefee gas to collector address #3

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ eyre = "0.6.12"
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
112 changes: 112 additions & 0 deletions src/evm_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use reth::{
primitives::{Address, ChainSpec, Header, TransactionSigned, U256},
revm::{
inspector_handle_register,
interpreter::Gas,
primitives::{spec_to_generic, CfgEnvWithHandlerCfg, EVMError, Spec, SpecId, TxEnv},
Context, Database, Evm, EvmBuilder, GetInspector,
},
};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_evm_ethereum::EthEvmConfig;
use revm::handler::mainnet::reward_beneficiary as reward_beneficiary_mainnet;
use std::sync::Arc;

/// 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, collector_address)?;
}
Ok(())
}

/// Mint basefee to eip1559 collector
#[inline]
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;
let gas_used = U256::from(gas.spent() - gas.refunded() as u64);

let (collector_account, _) = context
.evm
.inner
.journaled_state
.load_account(collector_address, &mut context.evm.inner.db)?;

collector_account.mark_touch();
collector_account.info.balance = collector_account
.info
.balance
.saturating_add(base_fee * gas_used);

Ok(())
}

/// Custom EVM configuration
#[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(move |h| {
spec_to_generic!(h.spec_id(), {
h.post_execution.reward_beneficiary = Arc::new(move |context, gas| {
reward_beneficiary::<SPEC, (), DB>(context, gas, collector_address)
});
});
}))
.build()
}

fn evm_with_inspector<'a, DB, I>(&self, db: DB, inspector: I) -> Evm<'a, I, DB>
where
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(move |h| {
spec_to_generic!(h.spec_id(), {
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)
.build()
}
}

impl ConfigureEvmEnv for GnosisEvmConfig {
fn fill_tx_env(tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
EthEvmConfig::fill_tx_env(tx_env, transaction, sender)
}

fn fill_cfg_env(
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
total_difficulty: U256,
) {
EthEvmConfig::fill_cfg_env(cfg_env, chain_spec, header, total_difficulty);
}
}
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use evm_config::GnosisEvmConfig;
use execute::GnosisExecutorProvider;
use eyre::eyre;
use reth::{
api::NodeTypes,
builder::{
Expand All @@ -7,13 +9,13 @@ use reth::{
BuilderContext, Node,
},
};
use reth_evm_ethereum::EthEvmConfig;
use reth_node_ethereum::{
node::{EthereumNetworkBuilder, EthereumPayloadBuilder, EthereumPoolBuilder},
EthEngineTypes, EthereumNode,
};

mod ethereum;
mod evm_config;
mod execute;
mod gnosis;

Expand Down Expand Up @@ -88,7 +90,7 @@ where
Node: FullNodeTypes,
{
// Must implement ConfigureEvm;
type EVM = EthEvmConfig;
type EVM = GnosisEvmConfig;
// Must implement BlockExecutorProvider;
type Executor = GnosisExecutorProvider<Self::EVM>;

Expand All @@ -97,7 +99,18 @@ where
ctx: &BuilderContext<Node>,
) -> eyre::Result<(Self::EVM, Self::Executor)> {
let chain_spec = ctx.chain_spec();
let evm_config = EthEvmConfig::default();
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
Loading