@@ -60,6 +60,59 @@ func genesisPayloadHeader(
6060 }, nil
6161}
6262
63+ func genesisPayloadHeaderCapella (
64+ eth1GenesisBlock * types.Block ,
65+ spec * common.Spec ,
66+ ) (* capella.ExecutionPayloadHeader , error ) {
67+ extra := eth1GenesisBlock .Extra ()
68+ if len (extra ) > common .MAX_EXTRA_DATA_BYTES {
69+ return nil , fmt .Errorf (
70+ "extra data is %d bytes, max is %d" ,
71+ len (extra ),
72+ common .MAX_EXTRA_DATA_BYTES ,
73+ )
74+ }
75+ if len (eth1GenesisBlock .Transactions ()) != 0 {
76+ return nil , fmt .Errorf (
77+ "expected no transactions in genesis execution payload" ,
78+ )
79+ }
80+ if len (eth1GenesisBlock .Withdrawals ()) != 0 {
81+ return nil , fmt .Errorf (
82+ "expected no withdrawals in genesis execution payload" ,
83+ )
84+ }
85+
86+ baseFee , overflow := uint256 .FromBig (eth1GenesisBlock .BaseFee ())
87+ if overflow {
88+ return nil , fmt .Errorf ("basefee larger than 2^256-1" )
89+ }
90+
91+ return & capella.ExecutionPayloadHeader {
92+ ParentHash : common .Root (eth1GenesisBlock .ParentHash ()),
93+ FeeRecipient : common .Eth1Address (eth1GenesisBlock .Coinbase ()),
94+ StateRoot : common .Bytes32 (eth1GenesisBlock .Root ()),
95+ ReceiptsRoot : common .Bytes32 (eth1GenesisBlock .ReceiptHash ()),
96+ LogsBloom : common .LogsBloom (eth1GenesisBlock .Bloom ()),
97+ PrevRandao : common.Bytes32 {},
98+ BlockNumber : view .Uint64View (eth1GenesisBlock .NumberU64 ()),
99+ GasLimit : view .Uint64View (eth1GenesisBlock .GasLimit ()),
100+ GasUsed : view .Uint64View (eth1GenesisBlock .GasUsed ()),
101+ Timestamp : common .Timestamp (eth1GenesisBlock .Time ()),
102+ ExtraData : extra ,
103+ BaseFeePerGas : view .Uint256View (* baseFee ),
104+ BlockHash : common .Root (eth1GenesisBlock .Hash ()),
105+ // empty transactions root
106+ TransactionsRoot : common .PayloadTransactionsType (spec ).
107+ DefaultNode ().
108+ MerkleRoot (tree .GetHashFn ()),
109+ // empty withdrawals root
110+ WithdrawalsRoot : common .WithdrawalsType (spec ).
111+ DefaultNode ().
112+ MerkleRoot (tree .GetHashFn ()),
113+ }, nil
114+ }
115+
63116func createValidators (
64117 spec * common.Spec ,
65118 keys []* KeyDetails ,
@@ -267,10 +320,10 @@ func BuildBeaconState(
267320 }
268321 }
269322
270- if st , ok := state .(* bellatrix. BeaconStateView ); ok {
271- // did we hit the TTD at genesis block?
323+ switch st := state .(type ) {
324+ case * bellatrix. BeaconStateView :
272325 tdd := uint256 .Int (spec .TERMINAL_TOTAL_DIFFICULTY )
273- embedExecAtGenesis := tdd .ToBig ().Cmp (eth1Genesis .Difficulty ) < 0
326+ embedExecAtGenesis := tdd .ToBig ().Cmp (eth1Genesis .Difficulty ) <= 0
274327
275328 var execPayloadHeader * bellatrix.ExecutionPayloadHeader
276329 if embedExecAtGenesis {
@@ -287,6 +340,29 @@ func BuildBeaconState(
287340 execPayloadHeader = new (bellatrix.ExecutionPayloadHeader )
288341 }
289342
343+ if err := st .SetLatestExecutionPayloadHeader (execPayloadHeader ); err != nil {
344+ return nil , err
345+ }
346+ case * capella.BeaconStateView :
347+ // did we hit the TTD at genesis block?
348+ tdd := uint256 .Int (spec .TERMINAL_TOTAL_DIFFICULTY )
349+ embedExecAtGenesis := tdd .ToBig ().Cmp (eth1Genesis .Difficulty ) <= 0
350+
351+ var execPayloadHeader * capella.ExecutionPayloadHeader
352+ if embedExecAtGenesis {
353+ execPayloadHeader , err = genesisPayloadHeaderCapella (
354+ eth1GenesisBlock ,
355+ spec ,
356+ )
357+ if err != nil {
358+ return nil , err
359+ }
360+ } else {
361+ // we didn't build any on the eth1 chain though,
362+ // so we just put the genesis hash here (it could be any block from eth1 chain before TTD that is not ahead of eth2)
363+ execPayloadHeader = new (capella.ExecutionPayloadHeader )
364+ }
365+
290366 if err := st .SetLatestExecutionPayloadHeader (execPayloadHeader ); err != nil {
291367 return nil , err
292368 }
0 commit comments