@@ -22,6 +22,15 @@ import (
22
22
)
23
23
24
24
func TestLightClient_NewLightClientOptimisticUpdateFromBeaconState (t * testing.T ) {
25
+ params .SetupTestConfigCleanup (t )
26
+ cfg := params .BeaconConfig ()
27
+ cfg .AltairForkEpoch = 1
28
+ cfg .BellatrixForkEpoch = 2
29
+ cfg .CapellaForkEpoch = 3
30
+ cfg .DenebForkEpoch = 4
31
+ cfg .ElectraForkEpoch = 5
32
+ params .OverrideBeaconConfig (cfg )
33
+
25
34
t .Run ("Altair" , func (t * testing.T ) {
26
35
l := util .NewTestLightClient (t ).SetupTestAltair ()
27
36
@@ -59,9 +68,31 @@ func TestLightClient_NewLightClientOptimisticUpdateFromBeaconState(t *testing.T)
59
68
l .CheckSyncAggregate (update .SyncAggregate ())
60
69
l .CheckAttestedHeader (update .AttestedHeader ())
61
70
})
71
+
72
+ t .Run ("Electra" , func (t * testing.T ) {
73
+ l := util .NewTestLightClient (t ).SetupTestElectra (false )
74
+
75
+ update , err := lightClient .NewLightClientOptimisticUpdateFromBeaconState (l .Ctx , l .State .Slot (), l .State , l .Block , l .AttestedState , l .AttestedBlock )
76
+ require .NoError (t , err )
77
+ require .NotNil (t , update , "update is nil" )
78
+
79
+ require .Equal (t , l .Block .Block ().Slot (), update .SignatureSlot (), "Signature slot is not equal" )
80
+
81
+ l .CheckSyncAggregate (update .SyncAggregate ())
82
+ l .CheckAttestedHeader (update .AttestedHeader ())
83
+ })
62
84
}
63
85
64
86
func TestLightClient_NewLightClientFinalityUpdateFromBeaconState (t * testing.T ) {
87
+ params .SetupTestConfigCleanup (t )
88
+ cfg := params .BeaconConfig ()
89
+ cfg .AltairForkEpoch = 1
90
+ cfg .BellatrixForkEpoch = 2
91
+ cfg .CapellaForkEpoch = 3
92
+ cfg .DenebForkEpoch = 4
93
+ cfg .ElectraForkEpoch = 5
94
+ params .OverrideBeaconConfig (cfg )
95
+
65
96
t .Run ("Altair" , func (t * testing.T ) {
66
97
l := util .NewTestLightClient (t ).SetupTestAltair ()
67
98
@@ -356,6 +387,157 @@ func TestLightClient_NewLightClientFinalityUpdateFromBeaconState(t *testing.T) {
356
387
require .DeepSSZEqual (t , execution , updateExecution .Proto (), "Finalized Block Execution is not equal" )
357
388
})
358
389
})
390
+
391
+ t .Run ("Electra" , func (t * testing.T ) {
392
+ t .Run ("FinalizedBlock Not Nil" , func (t * testing.T ) {
393
+ l := util .NewTestLightClient (t ).SetupTestElectra (false )
394
+
395
+ update , err := lightClient .NewLightClientFinalityUpdateFromBeaconState (l .Ctx , l .State .Slot (), l .State , l .Block , l .AttestedState , l .AttestedBlock , l .FinalizedBlock )
396
+ require .NoError (t , err )
397
+ require .NotNil (t , update , "update is nil" )
398
+
399
+ require .Equal (t , l .Block .Block ().Slot (), update .SignatureSlot (), "Signature slot is not equal" )
400
+
401
+ l .CheckSyncAggregate (update .SyncAggregate ())
402
+ l .CheckAttestedHeader (update .AttestedHeader ())
403
+
404
+ //zeroHash := params.BeaconConfig().ZeroHash[:]
405
+ finalizedBlockHeader , err := l .FinalizedBlock .Header ()
406
+ require .NoError (t , err )
407
+ require .NotNil (t , update .FinalizedHeader (), "Finalized header is nil" )
408
+ updateFinalizedHeaderBeacon := update .FinalizedHeader ().Beacon ()
409
+ require .Equal (t , finalizedBlockHeader .Header .Slot , updateFinalizedHeaderBeacon .Slot , "Finalized header slot is not equal" )
410
+ require .Equal (t , finalizedBlockHeader .Header .ProposerIndex , updateFinalizedHeaderBeacon .ProposerIndex , "Finalized header proposer index is not equal" )
411
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .ParentRoot , updateFinalizedHeaderBeacon .ParentRoot , "Finalized header parent root is not equal" )
412
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .StateRoot , updateFinalizedHeaderBeacon .StateRoot , "Finalized header state root is not equal" )
413
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .BodyRoot , updateFinalizedHeaderBeacon .BodyRoot , "Finalized header body root is not equal" )
414
+ fb , err := update .FinalityBranchElectra ()
415
+ require .NoError (t , err )
416
+ proof , err := l .AttestedState .FinalizedRootProof (l .Ctx )
417
+ require .NoError (t , err )
418
+ for i , leaf := range fb {
419
+ require .DeepSSZEqual (t , proof [i ], leaf [:], "Leaf is not equal" )
420
+ }
421
+
422
+ // Check Execution BlockHash
423
+ payloadInterface , err := l .FinalizedBlock .Block ().Body ().Execution ()
424
+ require .NoError (t , err )
425
+ transactionsRoot , err := payloadInterface .TransactionsRoot ()
426
+ if errors .Is (err , consensustypes .ErrUnsupportedField ) {
427
+ transactions , err := payloadInterface .Transactions ()
428
+ require .NoError (t , err )
429
+ transactionsRootArray , err := ssz .TransactionsRoot (transactions )
430
+ require .NoError (t , err )
431
+ transactionsRoot = transactionsRootArray [:]
432
+ } else {
433
+ require .NoError (t , err )
434
+ }
435
+ withdrawalsRoot , err := payloadInterface .WithdrawalsRoot ()
436
+ if errors .Is (err , consensustypes .ErrUnsupportedField ) {
437
+ withdrawals , err := payloadInterface .Withdrawals ()
438
+ require .NoError (t , err )
439
+ withdrawalsRootArray , err := ssz .WithdrawalSliceRoot (withdrawals , fieldparams .MaxWithdrawalsPerPayload )
440
+ require .NoError (t , err )
441
+ withdrawalsRoot = withdrawalsRootArray [:]
442
+ } else {
443
+ require .NoError (t , err )
444
+ }
445
+ execution := & v11.ExecutionPayloadHeaderDeneb {
446
+ ParentHash : payloadInterface .ParentHash (),
447
+ FeeRecipient : payloadInterface .FeeRecipient (),
448
+ StateRoot : payloadInterface .StateRoot (),
449
+ ReceiptsRoot : payloadInterface .ReceiptsRoot (),
450
+ LogsBloom : payloadInterface .LogsBloom (),
451
+ PrevRandao : payloadInterface .PrevRandao (),
452
+ BlockNumber : payloadInterface .BlockNumber (),
453
+ GasLimit : payloadInterface .GasLimit (),
454
+ GasUsed : payloadInterface .GasUsed (),
455
+ Timestamp : payloadInterface .Timestamp (),
456
+ ExtraData : payloadInterface .ExtraData (),
457
+ BaseFeePerGas : payloadInterface .BaseFeePerGas (),
458
+ BlockHash : payloadInterface .BlockHash (),
459
+ TransactionsRoot : transactionsRoot ,
460
+ WithdrawalsRoot : withdrawalsRoot ,
461
+ }
462
+ updateExecution , err := update .FinalizedHeader ().Execution ()
463
+ require .NoError (t , err )
464
+ require .DeepSSZEqual (t , execution , updateExecution .Proto (), "Finalized Block Execution is not equal" )
465
+ })
466
+
467
+ t .Run ("FinalizedBlock In Previous Fork" , func (t * testing.T ) {
468
+ l := util .NewTestLightClient (t ).SetupTestElectraFinalizedBlockDeneb (false )
469
+
470
+ update , err := lightClient .NewLightClientFinalityUpdateFromBeaconState (l .Ctx , l .State .Slot (), l .State , l .Block , l .AttestedState , l .AttestedBlock , l .FinalizedBlock )
471
+ require .NoError (t , err )
472
+ require .NotNil (t , update , "update is nil" )
473
+
474
+ require .Equal (t , l .Block .Block ().Slot (), update .SignatureSlot (), "Signature slot is not equal" )
475
+
476
+ l .CheckSyncAggregate (update .SyncAggregate ())
477
+ l .CheckAttestedHeader (update .AttestedHeader ())
478
+
479
+ finalizedBlockHeader , err := l .FinalizedBlock .Header ()
480
+ require .NoError (t , err )
481
+ require .NotNil (t , update .FinalizedHeader (), "Finalized header is nil" )
482
+ updateFinalizedHeaderBeacon := update .FinalizedHeader ().Beacon ()
483
+ require .Equal (t , finalizedBlockHeader .Header .Slot , updateFinalizedHeaderBeacon .Slot , "Finalized header slot is not equal" )
484
+ require .Equal (t , finalizedBlockHeader .Header .ProposerIndex , updateFinalizedHeaderBeacon .ProposerIndex , "Finalized header proposer index is not equal" )
485
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .ParentRoot , updateFinalizedHeaderBeacon .ParentRoot , "Finalized header parent root is not equal" )
486
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .StateRoot , updateFinalizedHeaderBeacon .StateRoot , "Finalized header state root is not equal" )
487
+ require .DeepSSZEqual (t , finalizedBlockHeader .Header .BodyRoot , updateFinalizedHeaderBeacon .BodyRoot , "Finalized header body root is not equal" )
488
+ fb , err := update .FinalityBranchElectra ()
489
+ require .NoError (t , err )
490
+ proof , err := l .AttestedState .FinalizedRootProof (l .Ctx )
491
+ require .NoError (t , err )
492
+ for i , leaf := range fb {
493
+ require .DeepSSZEqual (t , proof [i ], leaf [:], "Leaf is not equal" )
494
+ }
495
+
496
+ // Check Execution BlockHash
497
+ payloadInterface , err := l .FinalizedBlock .Block ().Body ().Execution ()
498
+ require .NoError (t , err )
499
+ transactionsRoot , err := payloadInterface .TransactionsRoot ()
500
+ if errors .Is (err , consensustypes .ErrUnsupportedField ) {
501
+ transactions , err := payloadInterface .Transactions ()
502
+ require .NoError (t , err )
503
+ transactionsRootArray , err := ssz .TransactionsRoot (transactions )
504
+ require .NoError (t , err )
505
+ transactionsRoot = transactionsRootArray [:]
506
+ } else {
507
+ require .NoError (t , err )
508
+ }
509
+ withdrawalsRoot , err := payloadInterface .WithdrawalsRoot ()
510
+ if errors .Is (err , consensustypes .ErrUnsupportedField ) {
511
+ withdrawals , err := payloadInterface .Withdrawals ()
512
+ require .NoError (t , err )
513
+ withdrawalsRootArray , err := ssz .WithdrawalSliceRoot (withdrawals , fieldparams .MaxWithdrawalsPerPayload )
514
+ require .NoError (t , err )
515
+ withdrawalsRoot = withdrawalsRootArray [:]
516
+ } else {
517
+ require .NoError (t , err )
518
+ }
519
+ execution := & v11.ExecutionPayloadHeaderDeneb {
520
+ ParentHash : payloadInterface .ParentHash (),
521
+ FeeRecipient : payloadInterface .FeeRecipient (),
522
+ StateRoot : payloadInterface .StateRoot (),
523
+ ReceiptsRoot : payloadInterface .ReceiptsRoot (),
524
+ LogsBloom : payloadInterface .LogsBloom (),
525
+ PrevRandao : payloadInterface .PrevRandao (),
526
+ BlockNumber : payloadInterface .BlockNumber (),
527
+ GasLimit : payloadInterface .GasLimit (),
528
+ GasUsed : payloadInterface .GasUsed (),
529
+ Timestamp : payloadInterface .Timestamp (),
530
+ ExtraData : payloadInterface .ExtraData (),
531
+ BaseFeePerGas : payloadInterface .BaseFeePerGas (),
532
+ BlockHash : payloadInterface .BlockHash (),
533
+ TransactionsRoot : transactionsRoot ,
534
+ WithdrawalsRoot : withdrawalsRoot ,
535
+ }
536
+ updateExecution , err := update .FinalizedHeader ().Execution ()
537
+ require .NoError (t , err )
538
+ require .DeepSSZEqual (t , execution , updateExecution .Proto (), "Finalized Block Execution is not equal" )
539
+ })
540
+ })
359
541
}
360
542
361
543
func TestLightClient_BlockToLightClientHeader (t * testing.T ) {
0 commit comments