Skip to content
Open
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
11 changes: 5 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
alloy-consensus = "1.6.3"
alloy-eips = "1.6.3"
alloy-eips = "1.8.3"
alloy-evm = "0.27.2"
alloy-genesis = "1.6.3"
alloy-network = "1.6.3"
Expand Down
21 changes: 21 additions & 0 deletions src/chainspec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ impl BerachainChainSpec {
self.pol_contract_address
}

/// PoL distributor address when Prague1 is active at the given timestamp.
pub fn active_pol_distributor_at_timestamp(&self, timestamp: u64) -> Option<Address> {
if !self.is_prague1_active_at_timestamp(timestamp) {
return None;
}
let address = self.pol_contract();
(!address.is_zero()).then_some(address)
}

/// Get blocked addresses for Prague3 if the hardfork is active
pub fn prague3_blocked_addresses_at_timestamp(&self, timestamp: u64) -> Option<&[Address]> {
if self.is_prague3_active_at_timestamp(timestamp) {
Expand Down Expand Up @@ -817,6 +826,18 @@ mod tests {
assert_eq!(bepolia.deposit_contract().map(|c| c.address), Some(expected));
}

#[test]
fn test_builtin_genesis_pol_distributor() {
let expected = address!("D2f19a79b026Fb636A7c300bF5947df113940761");
let mainnet = BerachainChainSpecParser::parse(BERACHAIN_MAINNET).unwrap();
let bepolia = BerachainChainSpecParser::parse(BERACHAIN_BEPOLIA).unwrap();

assert_eq!(mainnet.active_pol_distributor_at_timestamp(1_756_915_199), None);
assert_eq!(bepolia.active_pol_distributor_at_timestamp(1_754_495_999), None);
assert_eq!(mainnet.active_pol_distributor_at_timestamp(1_756_915_200), Some(expected));
assert_eq!(bepolia.active_pol_distributor_at_timestamp(1_754_496_000), Some(expected));
}

#[test]
fn test_from_genesis() {
let mut genesis = Genesis::default();
Expand Down
101 changes: 92 additions & 9 deletions src/rpc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use reth_rpc_eth_types::EthApiError;

use std::collections::BTreeMap;

/// EIP-7910 `systemContracts` key for the PoL distributor (BRIP-0004 / Prague1).
const POL_DISTRIBUTOR_SYSTEM_CONTRACT: &str = "POL_DISTRIBUTOR_ADDRESS";

/// Berachain `eth_config` RPC handler implementing EIP-7910.
#[derive(Debug, Clone)]
pub struct BerachainConfigHandler<Provider> {
Expand All @@ -40,31 +43,38 @@ where
/// Builds fork config for timestamp, returns None if no blob params exist.
fn build_fork_config_at(
&self,
timestamp: u64,
fork_timestamp: u64,
precompiles: BTreeMap<String, Address>,
system_contracts_eval_timestamp: u64,
) -> Option<EthForkConfig> {
let chain_spec = self.provider.chain_spec();

let mut system_contracts = BTreeMap::<SystemContract, Address>::default();

if chain_spec.is_cancun_active_at_timestamp(timestamp) {
if chain_spec.is_cancun_active_at_timestamp(fork_timestamp) {
system_contracts.extend(SystemContract::cancun());
}

if chain_spec.is_prague_active_at_timestamp(timestamp) {
if chain_spec.is_prague_active_at_timestamp(fork_timestamp) {
system_contracts
.extend(SystemContract::prague(chain_spec.deposit_contract().map(|c| c.address)));
}

insert_berachain_system_contracts(
&chain_spec,
system_contracts_eval_timestamp,
&mut system_contracts,
);

let fork_id = chain_spec
.fork_id(&Head { timestamp, number: u64::MAX, ..Default::default() })
.fork_id(&Head { timestamp: fork_timestamp, number: u64::MAX, ..Default::default() })
.hash
.0
.into();

Some(EthForkConfig {
activation_time: timestamp,
blob_schedule: chain_spec.blob_params_at_timestamp(timestamp)?,
activation_time: fork_timestamp,
blob_schedule: chain_spec.blob_params_at_timestamp(fork_timestamp)?,
chain_id: chain_spec.chain().id(),
fork_id,
precompiles,
Expand Down Expand Up @@ -98,7 +108,7 @@ where
.ok_or_else(|| RethError::msg("no active timestamp fork found"))?;

let current = self
.build_fork_config_at(current_fork_timestamp, current_precompiles)
.build_fork_config_at(current_fork_timestamp, current_precompiles, latest.timestamp)
.ok_or_else(|| RethError::msg("no fork config for current fork"))?;

let mut config = EthConfig { current, next: None, last: None };
Expand All @@ -115,7 +125,11 @@ where
.map_err(RethError::other)?,
);

config.next = self.build_fork_config_at(next_fork_timestamp, next_precompiles);
config.next = self.build_fork_config_at(
next_fork_timestamp,
next_precompiles,
next_fork_timestamp,
);
} else {
// If there is no fork scheduled, there is no "last" or "final" fork scheduled.
return Ok(config);
Expand All @@ -133,7 +147,8 @@ where
.map_err(RethError::other)?,
);

config.last = self.build_fork_config_at(last_fork_timestamp, last_precompiles);
config.last =
self.build_fork_config_at(last_fork_timestamp, last_precompiles, last_fork_timestamp);

Ok(config)
}
Expand All @@ -150,6 +165,18 @@ where
}
}

fn insert_berachain_system_contracts(
chain_spec: &BerachainChainSpec,
eval_timestamp: u64,
system_contracts: &mut BTreeMap<SystemContract, Address>,
) {
// BRIP-0004: PoL distributor is a Prague1 system contract, not part of Ethereum Prague.
if let Some(address) = chain_spec.active_pol_distributor_at_timestamp(eval_timestamp) {
system_contracts
.insert(SystemContract::Other(POL_DISTRIBUTOR_SYSTEM_CONTRACT.into()), address);
}
}

fn evm_to_precompiles_map(
evm: impl Evm<Precompiles = PrecompilesMap>,
) -> BTreeMap<String, Address> {
Expand All @@ -161,3 +188,59 @@ fn evm_to_precompiles_map(
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{chainspec::BerachainChainSpecParser, hardforks::BerachainHardforks};
use alloy_primitives::address;
use reth_cli::chainspec::ChainSpecParser;

const BERACHAIN_BEPOLIA: &str = "bepolia";

#[test]
fn test_insert_berachain_system_contracts_bepolia_boundary() {
let chain_spec = BerachainChainSpecParser::parse(BERACHAIN_BEPOLIA).unwrap();
let expected = address!("D2f19a79b026Fb636A7c300bF5947df113940761");

let mut before_prague1 = BTreeMap::default();
insert_berachain_system_contracts(&chain_spec, 1_754_495_999, &mut before_prague1);
assert!(before_prague1.is_empty());

let mut after_prague1 = BTreeMap::default();
insert_berachain_system_contracts(&chain_spec, 1_754_496_000, &mut after_prague1);
assert_eq!(
after_prague1.get(&SystemContract::Other(POL_DISTRIBUTOR_SYSTEM_CONTRACT.into())),
Some(&expected),
);
}

#[test]
fn test_pol_distributor_uses_head_timestamp_not_prague_fork_timestamp() {
let chain_spec = BerachainChainSpecParser::parse(BERACHAIN_BEPOLIA).unwrap();
let expected = address!("D2f19a79b026Fb636A7c300bF5947df113940761");
let prague_fork_timestamp = 1_746_633_600;
let post_prague1_head_timestamp = 1_755_000_000;

assert!(!chain_spec.is_prague1_active_at_timestamp(prague_fork_timestamp));
assert!(chain_spec.is_prague1_active_at_timestamp(post_prague1_head_timestamp));

let mut at_prague_fork = BTreeMap::default();
insert_berachain_system_contracts(&chain_spec, prague_fork_timestamp, &mut at_prague_fork);
assert!(at_prague_fork.is_empty());

let mut at_head = BTreeMap::default();
insert_berachain_system_contracts(&chain_spec, post_prague1_head_timestamp, &mut at_head);
assert_eq!(
at_head.get(&SystemContract::Other(POL_DISTRIBUTOR_SYSTEM_CONTRACT.into())),
Some(&expected),
);
}

#[test]
fn test_pol_distributor_system_contract_serde() {
let contract = SystemContract::Other(POL_DISTRIBUTOR_SYSTEM_CONTRACT.into());
let value = serde_json::to_value(contract).unwrap();
assert_eq!(value, "POL_DISTRIBUTOR_ADDRESS");
}
}