Skip to content

Commit e5a7a47

Browse files
authored
refactor: move EthereumAPI functions into Execution/Consensus API (#1842)
1 parent adfa490 commit e5a7a47

File tree

7 files changed

+172
-197
lines changed

7 files changed

+172
-197
lines changed

bin/e2hs-writer/src/subcommands/head_generator/e2hs_builder.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::path::PathBuf;
22

3-
use anyhow::ensure;
3+
use anyhow::{bail, ensure};
44
use e2store::e2hs::{E2HSWriter, BLOCKS_PER_E2HS};
5+
use ethereum_rpc_client::{consensus::ConsensusApi, execution::ExecutionApi};
56
use ethportal_api::{
67
consensus::{
78
beacon_block::BeaconBlockElectra, beacon_state::HistoricalBatch,
@@ -14,7 +15,6 @@ use tempfile::TempDir;
1415
use tracing::info;
1516
use trin_validation::header_validator::HeaderValidator;
1617

17-
use super::ethereum_api::EthereumApi;
1818
use crate::{cli::HeadGeneratorConfig, subcommands::full_block::FullBlock};
1919

2020
struct ProvingAnchors {
@@ -41,7 +41,8 @@ impl ProvingAnchors {
4141
}
4242

4343
pub struct E2HSBuilder {
44-
pub ethereum_api: EthereumApi,
44+
pub consensus_api: ConsensusApi,
45+
pub execution_api: ExecutionApi,
4546
pub index: u64,
4647
slot_for_next_execution_number: u64,
4748
proving_anchors: ProvingAnchors,
@@ -50,20 +51,31 @@ pub struct E2HSBuilder {
5051

5152
impl E2HSBuilder {
5253
pub async fn new(config: HeadGeneratorConfig, index: u64) -> anyhow::Result<Self> {
53-
let ethereum_api = EthereumApi::new(
54-
config.cl_provider,
55-
config.el_provider,
54+
let consensus_api = ConsensusApi::new(
55+
config.cl_provider.clone(),
56+
config.cl_provider.clone(),
57+
config.request_timeout,
58+
)
59+
.await?;
60+
61+
let execution_api = ExecutionApi::new(
62+
config.el_provider.clone(),
63+
config.el_provider.clone(),
5664
config.request_timeout,
5765
)
5866
.await?;
5967

6068
let next_execution_block_number = index * BLOCKS_PER_E2HS as u64;
61-
let slot_for_next_execution_number = ethereum_api
62-
.find_slot_for_execution_block_number(next_execution_block_number)
63-
.await?;
69+
let slot_for_next_execution_number = find_slot_for_execution_block_number(
70+
&execution_api,
71+
&consensus_api,
72+
next_execution_block_number,
73+
)
74+
.await?;
6475

6576
Ok(Self {
66-
ethereum_api,
77+
consensus_api,
78+
execution_api,
6779
index,
6880
slot_for_next_execution_number,
6981
proving_anchors: ProvingAnchors::new(),
@@ -81,8 +93,8 @@ impl E2HSBuilder {
8193

8294
for block_number in starting_block..ending_block {
8395
let beacon_block = self
84-
.ethereum_api
85-
.fetch_beacon_block_retry(self.slot_for_next_execution_number)
96+
.consensus_api
97+
.find_first_beacon_block(self.slot_for_next_execution_number)
8698
.await?
8799
.message;
88100
self.update_proving_anchors(beacon_block.slot).await?;
@@ -119,7 +131,7 @@ impl E2HSBuilder {
119131
}
120132

121133
let state = self
122-
.ethereum_api
134+
.consensus_api
123135
.get_state_for_start_of_next_period(slot)
124136
.await?;
125137

@@ -147,7 +159,7 @@ impl E2HSBuilder {
147159
beacon_block,
148160
&self.proving_anchors.current_historical_batch,
149161
)?;
150-
let receipts = self.ethereum_api.get_receipts(block_number).await?;
162+
let receipts = self.execution_api.get_receipts(block_number).await?;
151163

152164
Ok(FullBlock {
153165
header_with_proof,
@@ -156,3 +168,22 @@ impl E2HSBuilder {
156168
})
157169
}
158170
}
171+
172+
pub async fn find_slot_for_execution_block_number(
173+
execution_api: &ExecutionApi,
174+
consensus_api: &ConsensusApi,
175+
execution_block_number: u64,
176+
) -> anyhow::Result<u64> {
177+
// The `parent_beacon_block_root` refers to the block root of the *previous* beacon block.
178+
// To fetch the corresponding beacon block, we must query using the *next* execution block
179+
// number (i.e., `execution_block_number + 1`), since that's when the parent beacon
180+
// block is referenced.
181+
let block = execution_api.get_block(execution_block_number + 1).await?;
182+
let Some(block_root) = block.header.parent_beacon_block_root else {
183+
bail!("We should only be able to backfill blocks which contain block root's {execution_block_number}")
184+
};
185+
let beacon_block = consensus_api
186+
.get_beacon_block(block_root.to_string())
187+
.await?;
188+
Ok(beacon_block.message.slot)
189+
}

bin/e2hs-writer/src/subcommands/head_generator/ethereum_api.rs

Lines changed: 0 additions & 141 deletions
This file was deleted.

bin/e2hs-writer/src/subcommands/head_generator/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod e2hs_builder;
2-
mod ethereum_api;
32
mod s3_bucket;
43

54
use std::{
@@ -11,7 +10,6 @@ use std::{
1110
use anyhow::{bail, ensure};
1211
use e2hs_builder::E2HSBuilder;
1312
use e2store::e2hs::BLOCKS_PER_E2HS;
14-
use ethereum_api::first_slot_in_a_period;
1513
use ethportal_api::consensus::constants::SLOTS_PER_EPOCH;
1614
use humanize_duration::{prelude::DurationExt, Truncate};
1715
use s3_bucket::S3Bucket;
@@ -95,22 +93,11 @@ impl HeadGenerator {
9593
}
9694

9795
async fn amount_of_blocks_that_can_be_backfilled(&self) -> anyhow::Result<u64> {
98-
let latest_finalized_slot = self
99-
.e2hs_builder
100-
.ethereum_api
101-
.consensus_api
102-
.get_beacon_block("finalized")
103-
.await?
104-
.message
105-
.slot;
106-
107-
let latest_provable_slot = first_slot_in_a_period(latest_finalized_slot);
10896
let first_execution_number_after_latest_finalized_period = self
10997
.e2hs_builder
110-
.ethereum_api
111-
.fetch_beacon_block_retry(latest_provable_slot)
98+
.consensus_api
99+
.first_block_in_latest_finalized_period()
112100
.await?
113-
.message
114101
.body
115102
.execution_payload
116103
.block_number;

bin/e2hs-writer/src/subcommands/single_generator/e2hs_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl E2HSBuilder {
7878
let receipts_handle = tokio::spawn(async move {
7979
if receipts_required {
8080
execution_api
81-
.get_receipts(starting_block..ending_block)
81+
.get_receipts_range(starting_block..ending_block)
8282
.await
8383
} else {
8484
Ok(HashMap::new())

0 commit comments

Comments
 (0)