@@ -9,7 +9,9 @@ use ethereum_hashing::hash;
9
9
use ssz:: Decode ;
10
10
use ssz:: Encode ;
11
11
use state_processing:: process_activations;
12
- use state_processing:: upgrade:: { upgrade_to_altair, upgrade_to_bellatrix} ;
12
+ use state_processing:: upgrade:: {
13
+ upgrade_to_altair, upgrade_to_bellatrix, upgrade_to_capella, upgrade_to_deneb,
14
+ } ;
13
15
use std:: fs:: File ;
14
16
use std:: io:: Read ;
15
17
use std:: path:: PathBuf ;
@@ -19,8 +21,8 @@ use types::ExecutionBlockHash;
19
21
use types:: {
20
22
test_utils:: generate_deterministic_keypairs, Address , BeaconState , ChainSpec , Config , Epoch ,
21
23
Eth1Data , EthSpec , ExecutionPayloadHeader , ExecutionPayloadHeaderCapella ,
22
- ExecutionPayloadHeaderDeneb , ExecutionPayloadHeaderMerge , ExecutionPayloadHeaderRefMut ,
23
- ForkName , Hash256 , Keypair , PublicKey , Validator ,
24
+ ExecutionPayloadHeaderDeneb , ExecutionPayloadHeaderMerge , ForkName , Hash256 , Keypair ,
25
+ PublicKey , Validator ,
24
26
} ;
25
27
26
28
pub fn run < T : EthSpec > ( testnet_dir_path : PathBuf , matches : & ArgMatches ) -> Result < ( ) , String > {
@@ -302,26 +304,47 @@ fn initialize_state_with_validators<T: EthSpec>(
302
304
state. fork_mut ( ) . previous_version = spec. bellatrix_fork_version ;
303
305
304
306
// Override latest execution payload header.
305
- // See https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/merge/beacon-chain.md#testing
306
-
307
- // Currently, we only support starting from a bellatrix state
308
- match state
309
- . latest_execution_payload_header_mut ( )
310
- . map_err ( |e| format ! ( "Failed to get execution payload header: {:?}" , e) ) ?
311
- {
312
- ExecutionPayloadHeaderRefMut :: Merge ( header_mut) => {
313
- if let ExecutionPayloadHeader :: Merge ( eph) = execution_payload_header {
314
- * header_mut = eph;
315
- } else {
316
- return Err ( "Execution payload header must be a bellatrix header" . to_string ( ) ) ;
317
- }
318
- }
319
- ExecutionPayloadHeaderRefMut :: Capella ( _) => {
320
- return Err ( "Cannot start genesis from a capella state" . to_string ( ) )
321
- }
322
- ExecutionPayloadHeaderRefMut :: Deneb ( _) => {
323
- return Err ( "Cannot start genesis from a deneb state" . to_string ( ) )
324
- }
307
+ // See https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/bellatrix/beacon-chain.md#testing
308
+ if let ExecutionPayloadHeader :: Merge ( ref header) = execution_payload_header {
309
+ * state
310
+ . latest_execution_payload_header_merge_mut ( )
311
+ . or ( Err ( "mismatched fork" . to_string ( ) ) ) ? = header. clone ( ) ;
312
+ }
313
+ }
314
+
315
+ if spec
316
+ . capella_fork_epoch
317
+ . map_or ( false , |fork_epoch| fork_epoch == T :: genesis_epoch ( ) )
318
+ {
319
+ upgrade_to_capella ( & mut state, spec) . unwrap ( ) ;
320
+
321
+ // Remove intermediate fork from `state.fork`.
322
+ state. fork_mut ( ) . previous_version = spec. capella_fork_version ;
323
+
324
+ // Override latest execution payload header.
325
+ // See https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/bellatrix/beacon-chain.md#testing
326
+ if let ExecutionPayloadHeader :: Capella ( ref header) = execution_payload_header {
327
+ * state
328
+ . latest_execution_payload_header_capella_mut ( )
329
+ . or ( Err ( "mismatched fork" . to_string ( ) ) ) ? = header. clone ( ) ;
330
+ }
331
+ }
332
+
333
+ if spec
334
+ . deneb_fork_epoch
335
+ . map_or ( false , |fork_epoch| fork_epoch == T :: genesis_epoch ( ) )
336
+ {
337
+ upgrade_to_deneb ( & mut state, spec) . unwrap ( ) ;
338
+
339
+ // Remove intermediate fork from `state.fork`.
340
+ state. fork_mut ( ) . previous_version = spec. deneb_fork_version ;
341
+
342
+ // Override latest execution payload header.
343
+ // See https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/bellatrix/beacon-chain.md#testing
344
+ if let ExecutionPayloadHeader :: Deneb ( ref header) = execution_payload_header {
345
+ * state
346
+ . latest_execution_payload_header_deneb_mut ( )
347
+ . or ( Err ( "mismatched fork" . to_string ( ) ) ) ? = header. clone ( ) ;
325
348
}
326
349
}
327
350
@@ -331,5 +354,10 @@ fn initialize_state_with_validators<T: EthSpec>(
331
354
// Set genesis validators root for domain separation and chain versioning
332
355
* state. genesis_validators_root_mut ( ) = state. update_validators_tree_hash_cache ( ) . unwrap ( ) ;
333
356
357
+ // Sanity check for state fork matching config fork.
358
+ state
359
+ . fork_name ( spec)
360
+ . map_err ( |e| format ! ( "state fork mismatch: {e:?}" ) ) ?;
361
+
334
362
Ok ( state)
335
363
}
0 commit comments