1
1
use std:: path:: PathBuf ;
2
2
3
- use anyhow:: ensure;
3
+ use anyhow:: { bail , ensure} ;
4
4
use e2store:: e2hs:: { E2HSWriter , BLOCKS_PER_E2HS } ;
5
+ use ethereum_rpc_client:: { consensus:: ConsensusApi , execution:: ExecutionApi } ;
5
6
use ethportal_api:: {
6
7
consensus:: {
7
8
beacon_block:: BeaconBlockElectra , beacon_state:: HistoricalBatch ,
@@ -14,7 +15,6 @@ use tempfile::TempDir;
14
15
use tracing:: info;
15
16
use trin_validation:: header_validator:: HeaderValidator ;
16
17
17
- use super :: ethereum_api:: EthereumApi ;
18
18
use crate :: { cli:: HeadGeneratorConfig , subcommands:: full_block:: FullBlock } ;
19
19
20
20
struct ProvingAnchors {
@@ -41,7 +41,8 @@ impl ProvingAnchors {
41
41
}
42
42
43
43
pub struct E2HSBuilder {
44
- pub ethereum_api : EthereumApi ,
44
+ pub consensus_api : ConsensusApi ,
45
+ pub execution_api : ExecutionApi ,
45
46
pub index : u64 ,
46
47
slot_for_next_execution_number : u64 ,
47
48
proving_anchors : ProvingAnchors ,
@@ -50,20 +51,31 @@ pub struct E2HSBuilder {
50
51
51
52
impl E2HSBuilder {
52
53
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 ( ) ,
56
64
config. request_timeout ,
57
65
)
58
66
. await ?;
59
67
60
68
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 ?;
64
75
65
76
Ok ( Self {
66
- ethereum_api,
77
+ consensus_api,
78
+ execution_api,
67
79
index,
68
80
slot_for_next_execution_number,
69
81
proving_anchors : ProvingAnchors :: new ( ) ,
@@ -81,8 +93,8 @@ impl E2HSBuilder {
81
93
82
94
for block_number in starting_block..ending_block {
83
95
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 )
86
98
. await ?
87
99
. message ;
88
100
self . update_proving_anchors ( beacon_block. slot ) . await ?;
@@ -119,7 +131,7 @@ impl E2HSBuilder {
119
131
}
120
132
121
133
let state = self
122
- . ethereum_api
134
+ . consensus_api
123
135
. get_state_for_start_of_next_period ( slot)
124
136
. await ?;
125
137
@@ -147,7 +159,7 @@ impl E2HSBuilder {
147
159
beacon_block,
148
160
& self . proving_anchors . current_historical_batch ,
149
161
) ?;
150
- let receipts = self . ethereum_api . get_receipts ( block_number) . await ?;
162
+ let receipts = self . execution_api . get_receipts ( block_number) . await ?;
151
163
152
164
Ok ( FullBlock {
153
165
header_with_proof,
@@ -156,3 +168,22 @@ impl E2HSBuilder {
156
168
} )
157
169
}
158
170
}
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