Skip to content

Commit a30fa9c

Browse files
authored
feat(anvil): add /eth/v1/beacon/genesis endpoint to Beacon API (#12503)
- Introduced `anvil_getGenesisTime` request to retrieve the genesis time of the chain. - Implemented `anvil_get_genesis_time` function in the backend to return the genesis timestamp. - Added a new handler for the `/eth/v1/beacon/genesis` endpoint to respond with genesis details. - Created `GenesisDetails` data struct for Beacon API response
1 parent cca5d6f commit a30fa9c

File tree

7 files changed

+55
-2
lines changed

7 files changed

+55
-2
lines changed

crates/anvil/core/src/eth/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ pub enum EthRequest {
202202
#[serde(rename = "anvil_getBlobSidecarsByBlockId", with = "sequence")]
203203
GetBlobSidecarsByBlockId(BlockId),
204204

205+
/// Returns the genesis time for the chain
206+
#[serde(rename = "anvil_getGenesisTime", with = "empty_params")]
207+
GetGenesisTime(()),
208+
205209
#[serde(rename = "eth_getTransactionByBlockHashAndIndex")]
206210
EthGetTransactionByBlockHashAndIndex(TxHash, Index),
207211

crates/anvil/src/eth/api.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ impl EthApi {
291291
EthRequest::GetBlobSidecarsByBlockId(block_id) => {
292292
self.anvil_get_blob_sidecars_by_block_id(block_id).to_rpc_result()
293293
}
294+
EthRequest::GetGenesisTime(()) => self.anvil_get_genesis_time().to_rpc_result(),
294295
EthRequest::EthGetRawTransactionByBlockHashAndIndex(hash, index) => {
295296
self.raw_transaction_by_block_hash_and_index(hash, index).await.to_rpc_result()
296297
}
@@ -1394,6 +1395,14 @@ impl EthApi {
13941395
Ok(self.backend.get_blob_sidecars_by_block_id(block_id)?)
13951396
}
13961397

1398+
/// Returns the genesis time for the Beacon chain
1399+
///
1400+
/// Handler for Beacon API call: `GET /eth/v1/beacon/genesis`
1401+
pub fn anvil_get_genesis_time(&self) -> Result<u64> {
1402+
node_info!("anvil_getGenesisTime");
1403+
Ok(self.backend.genesis_time())
1404+
}
1405+
13971406
/// Get transaction by its hash.
13981407
///
13991408
/// This will check the storage for a matching transaction, if no transaction exists in storage

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ impl Backend {
730730
self.env.write().evm_env.cfg_env.chain_id = chain_id;
731731
}
732732

733+
/// Returns the genesis data for the Beacon API.
734+
pub fn genesis_time(&self) -> u64 {
735+
self.genesis.timestamp
736+
}
737+
733738
/// Returns balance of the given account.
734739
pub async fn current_balance(&self, address: Address) -> DatabaseResult<U256> {
735740
Ok(self.get_account(address).await?.balance)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Beacon data structures for the Beacon API responses
2+
3+
use alloy_primitives::{B256, aliases::B32};
4+
use serde::{Deserialize, Serialize};
5+
6+
/// Ethereum Beacon chain genesis details
7+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8+
pub struct GenesisDetails {
9+
/// The genesis_time configured for the beacon chain
10+
pub genesis_time: u64,
11+
/// The genesis validators root
12+
pub genesis_validators_root: B256,
13+
/// The genesis fork version, as used in the beacon chain
14+
pub genesis_fork_version: B32,
15+
}

crates/anvil/src/eth/beacon/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
//! This module provides types and utilities for implementing Beacon API endpoints
44
//! in Anvil, allowing testing of blob-based transactions with standard beacon chain APIs.
55
6+
pub mod data;
67
pub mod error;
78
pub mod response;
89

10+
pub use data::GenesisDetails;
911
pub use error::BeaconError;
1012
pub use response::BeaconResponse;

crates/anvil/src/server/beacon_handler.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::eth::{
22
EthApi,
3-
beacon::{BeaconError, BeaconResponse},
3+
beacon::{BeaconError, BeaconResponse, GenesisDetails},
44
};
55
use alloy_eips::BlockId;
6-
use alloy_primitives::B256;
6+
use alloy_primitives::{B256, aliases::B32};
77
use alloy_rpc_types_beacon::{
88
header::Header,
99
sidecar::{BlobData, GetBlobsResponse},
@@ -91,3 +91,20 @@ pub async fn handle_get_blobs(
9191
Err(_) => BeaconError::internal_error().into_response(),
9292
}
9393
}
94+
95+
/// Handles incoming Beacon API requests for genesis details
96+
///
97+
/// Only returns the `genesis_time`, other fields are set to zero.
98+
///
99+
/// GET /eth/v1/beacon/genesis
100+
pub async fn handle_get_genesis(State(api): State<EthApi>) -> Response {
101+
match api.anvil_get_genesis_time() {
102+
Ok(genesis_time) => BeaconResponse::new(GenesisDetails {
103+
genesis_time,
104+
genesis_validators_root: B256::ZERO,
105+
genesis_fork_version: B32::ZERO,
106+
})
107+
.into_response(),
108+
Err(_) => BeaconError::internal_error().into_response(),
109+
}
110+
}

crates/anvil/src/server/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn beacon_router(api: EthApi) -> Router {
5858
get(beacon_handler::handle_get_blob_sidecars),
5959
)
6060
.route("/eth/v1/beacon/blobs/{block_id}", get(beacon_handler::handle_get_blobs))
61+
.route("/eth/v1/beacon/genesis", get(beacon_handler::handle_get_genesis))
6162
.with_state(api)
6263
}
6364

0 commit comments

Comments
 (0)