11use std:: path:: PathBuf ;
22
3- use anyhow:: ensure;
3+ use anyhow:: { bail , ensure} ;
44use e2store:: e2hs:: { E2HSWriter , BLOCKS_PER_E2HS } ;
5+ use ethereum_rpc_client:: { consensus:: ConsensusApi , execution:: ExecutionApi } ;
56use ethportal_api:: {
67 consensus:: {
78 beacon_block:: BeaconBlockElectra , beacon_state:: HistoricalBatch ,
@@ -14,7 +15,6 @@ use tempfile::TempDir;
1415use tracing:: info;
1516use trin_validation:: header_validator:: HeaderValidator ;
1617
17- use super :: ethereum_api:: EthereumApi ;
1818use crate :: { cli:: HeadGeneratorConfig , subcommands:: full_block:: FullBlock } ;
1919
2020struct ProvingAnchors {
@@ -41,7 +41,8 @@ impl ProvingAnchors {
4141}
4242
4343pub 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
5152impl 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+ }
0 commit comments