-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconsensus.go
1182 lines (1015 loc) · 35.7 KB
/
consensus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package consensus
///NOTE: only these imports are required others are in package already
// uses rpc
// uses config for networks
// uses common for datatypes
import (
"bytes"
"encoding/hex"
"fmt"
"log"
"math/big"
"os"
"sync"
"time"
"github.com/BlocSoc-iitr/selene/common"
"github.com/BlocSoc-iitr/selene/config"
"github.com/BlocSoc-iitr/selene/config/checkpoints"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/consensus/rpc"
"github.com/BlocSoc-iitr/selene/utils"
beacon "github.com/ethereum/go-ethereum/beacon/types"
geth "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
"github.com/pkg/errors"
bls "github.com/protolambda/bls12-381-util"
)
// Error definitions
var (
ErrIncorrectRpcNetwork = errors.New("incorrect RPC network")
ErrPayloadNotFound = errors.New("payload not found")
ErrInvalidHeaderHash = errors.New("invalid header hash")
ErrCheckpointTooOld = errors.New("checkpoint too old")
ErrBootstrapFetchFailed = errors.New("could not fetch bootstrap")
ErrInvalidUpdate = errors.New("invalid update")
ErrInsufficientParticipation = errors.New("insufficient participation")
ErrInvalidTimestamp = errors.New("invalid timestamp")
ErrInvalidPeriod = errors.New("invalid period")
ErrNotRelevant = errors.New("update not relevant")
ErrInvalidFinalityProof = errors.New("invalid finality proof")
ErrInvalidNextSyncCommitteeProof = errors.New("invalid next sync committee proof")
ErrInvalidSignature = errors.New("invalid signature")
)
const MAX_REQUEST_LIGHT_CLIENT_UPDATES = 128
type GenericUpdate struct {
AttestedHeader consensus_core.Header
SyncAggregate consensus_core.SyncAggregate
SignatureSlot uint64
NextSyncCommittee *consensus_core.SyncCommittee
NextSyncCommitteeBranch *[]consensus_core.Bytes32
FinalizedHeader consensus_core.Header
FinalityBranch []consensus_core.Bytes32
}
// The consensus client fields are updated because state takes chan *common.Block,
// not common.Block
type ConsensusClient struct {
BlockRecv chan *common.Block
FinalizedBlockRecv chan *common.Block
CheckpointRecv *[]byte
genesisTime uint64
db Database
}
type Inner struct {
RPC rpc.ConsensusRpc
Store LightClientStore
lastCheckpoint *[]byte
blockSend chan *common.Block
finalizedBlockSend chan *common.Block
checkpointSend chan *[]byte
Config *config.Config
}
type LightClientStore struct {
FinalizedHeader consensus_core.Header
CurrentSyncCommitee consensus_core.SyncCommittee
NextSyncCommitee *consensus_core.SyncCommittee
OptimisticHeader consensus_core.Header
PreviousMaxActiveParticipants uint64
CurrentMaxActiveParticipants uint64
}
func (con ConsensusClient) New(rpc *string, config config.Config) ConsensusClient {
blockSend := make(chan *common.Block, 256)
finalizedBlockSend := make(chan *common.Block)
checkpointSend := make(chan *[]byte)
// Here the initialisation of database was wrong as it was pointing to an interface,
// not a types that implements the interface. This resulted in an invalid address error
_db, err := (&ConfigDB{}).New(&config)
if err != nil {
panic(err)
}
// Doing this makes the db variable a concrete type (ConfigDB here)
// It can also be switched to FileDB if needed
db, ok := _db.(*ConfigDB)
if !ok {
panic(errors.New("Expected ConfigDB instance"))
}
var initialCheckpoint [32]byte
// There was a problem in this assignment initially as it was setting the checkpoint
// to db.LoadCheckpoint() when the user inputed a checkpoint. This updated one is
// according to Helios.
if config.Checkpoint != nil {
initialCheckpoint = *config.Checkpoint
} else {
initialNewCheckpoint, errorWhileLoadingCheckpoint := db.LoadCheckpoint()
copy(initialCheckpoint[:], initialNewCheckpoint)
if errorWhileLoadingCheckpoint != nil {
initialCheckpoint = config.DefaultCheckpoint
}
}
if initialCheckpoint == [32]byte{} {
panic("No checkpoint found")
}
In := &Inner{}
inner := In.New(*rpc, blockSend, finalizedBlockSend, checkpointSend, &config)
go func() {
err := inner.sync(initialCheckpoint)
if err != nil {
if inner.Config.LoadExternalFallback {
err = sync_all_fallback(inner, inner.Config.Chain.ChainID)
if err != nil {
log.Printf("sync failed: %v", err)
os.Exit(1)
}
} else if inner.Config.Fallback != nil {
err = sync_fallback(inner, inner.Config.Fallback)
if err != nil {
log.Printf("sync failed: %v", err)
os.Exit(1)
}
} else {
log.Printf("sync failed: %v", err)
os.Exit(1)
}
}
_ = inner.send_blocks()
for {
time.Sleep(inner.duration_until_next_update())
err := inner.advance()
if err != nil {
log.Printf("advance error: %v", err)
continue
}
err = inner.send_blocks()
if err != nil {
log.Printf("send error: %v", err)
continue
}
}
}()
checkpointsReceived := <-checkpointSend
return ConsensusClient{
BlockRecv: blockSend,
FinalizedBlockRecv: finalizedBlockSend,
CheckpointRecv: checkpointsReceived,
genesisTime: config.Chain.GenesisTime,
db: db,
}
}
func (con ConsensusClient) Shutdown() error {
checkpoint := con.CheckpointRecv
if checkpoint != nil {
err := con.db.SaveCheckpoint(*checkpoint)
if err != nil {
return err
}
}
return nil
}
func (con ConsensusClient) Expected_current_slot() uint64 {
now := time.Now().Unix()
// Assuming SLOT_DURATION is the duration of each slot in seconds
const SLOT_DURATION uint64 = 12
return (uint64(now) - con.genesisTime) / SLOT_DURATION
}
func sync_fallback(inner *Inner, fallback *string) error {
// Create a buffered channel to receive any errors from the goroutine
errorChan := make(chan error, 1)
go func() {
// Attempt to fetch the latest checkpoint from the API
cf, err := (&checkpoints.CheckpointFallback{}).FetchLatestCheckpointFromApi(*fallback)
if err != nil {
errorChan <- err
return
}
if err := inner.sync(cf); err != nil {
errorChan <- err
return
}
errorChan <- nil
}()
return <-errorChan
}
func sync_all_fallback(inner *Inner, chainID uint64) error {
var n config.Network
network, err := n.ChainID(chainID)
if err != nil {
return err
}
errorChan := make(chan error, 1)
go func() {
ch := checkpoints.CheckpointFallback{}
checkpointFallback, errWhileCheckpoint := ch.Build()
if errWhileCheckpoint != nil {
errorChan <- errWhileCheckpoint
return
}
chainId := network.Chain.ChainID
var networkName config.Network
if chainId == 1 {
networkName = config.MAINNET
} else if chainId == 5 {
networkName = config.GOERLI
} else if chainId == 11155111 {
networkName = config.SEPOLIA
} else {
errorChan <- errors.New("chain id not recognized")
return
}
// Fetch the latest checkpoint from the network
checkpoint := checkpointFallback.FetchLatestCheckpoint(networkName)
// Sync using the inner struct's sync method
if err := inner.sync(checkpoint); err != nil {
errorChan <- err
}
errorChan <- nil
}()
return <-errorChan
}
func (in *Inner) New(rpcURL string, blockSend chan *common.Block, finalizedBlockSend chan *common.Block, checkpointSend chan *[]byte, config *config.Config) *Inner {
rpcClient := rpc.NewConsensusRpc(rpcURL)
return &Inner{
RPC: rpcClient,
Store: LightClientStore{},
lastCheckpoint: nil, // No checkpoint initially
blockSend: blockSend,
finalizedBlockSend: finalizedBlockSend,
checkpointSend: checkpointSend,
Config: config,
}
}
func (in *Inner) Check_rpc() error {
errorChan := make(chan error, 1)
go func() {
chainID, err := in.RPC.ChainId()
if err != nil {
errorChan <- err
return
}
if chainID != in.Config.Chain.ChainID {
errorChan <- ErrIncorrectRpcNetwork
return
}
errorChan <- nil
}()
return <-errorChan
}
func (in *Inner) get_execution_payload(slot *uint64) (*consensus_core.ExecutionPayload, error) {
errorChan := make(chan error, 1)
blockChan := make(chan consensus_core.BeaconBlock, 1)
go func() {
var err error
block, err := in.RPC.GetBlock(*slot)
if err != nil {
errorChan <- err
}
errorChan <- nil
blockChan <- block
}()
if err := <-errorChan; err != nil {
return nil, err
}
block := <-blockChan
Gethblock, err := beacon.BlockFromJSON("capella", block.Hash)
if err != nil {
return nil, err
}
blockHash := Gethblock.Root()
latestSlot := in.Store.OptimisticHeader.Slot
finalizedSlot := in.Store.FinalizedHeader.Slot
var verifiedBlockHash geth.Hash
if *slot == latestSlot {
verifiedBlockHash = toGethHeader(&in.Store.OptimisticHeader).Hash()
} else if *slot == finalizedSlot {
verifiedBlockHash = toGethHeader(&in.Store.FinalizedHeader).Hash()
} else {
return nil, ErrPayloadNotFound
}
// Compare the hashes
if !bytes.Equal(verifiedBlockHash[:], blockHash.Bytes()) {
return nil, fmt.Errorf("%w: expected %v but got %v", ErrInvalidHeaderHash, verifiedBlockHash, blockHash)
}
payload := block.Body.ExecutionPayload
return &payload, nil
}
func (in *Inner) Get_payloads(startSlot, endSlot uint64) ([]interface{}, error) {
var payloads []interface{}
// Fetch the block at endSlot to get the initial parent hash
endBlock, err := in.RPC.GetBlock(endSlot)
if err != nil {
return nil, err
}
endPayload := endBlock.Body.ExecutionPayload
prevParentHash := endPayload.ParentHash
// Create a wait group to manage concurrent fetching
var wg sync.WaitGroup
payloadsChan := make(chan interface{}, endSlot-startSlot+1)
errorChan := make(chan error, 1) // Buffer for one error
// Fetch blocks in parallel
for slot := endSlot; slot >= startSlot; slot-- {
wg.Add(1)
go func(slot uint64) {
defer wg.Done()
block, err := in.RPC.GetBlock(slot)
if err != nil {
errorChan <- err
return
}
payload := block.Body.ExecutionPayload
if payload.ParentHash != prevParentHash {
log.Printf("Error while backfilling blocks: expected block hash %v but got %v", prevParentHash, payload.ParentHash)
return
}
prevParentHash = payload.ParentHash
payloadsChan <- payload
}(slot)
}
// Close channels after all fetches are complete
go func() {
wg.Wait()
close(payloadsChan)
close(errorChan)
}()
// Collect results and check for errors
for {
select {
case payload, ok := <-payloadsChan:
if !ok {
return nil, errors.New("payloads channel closed unexpectedly")
}
payloads = append(payloads, payload)
return payloads, nil
case err := <-errorChan:
return nil, err
}
}
}
func (in *Inner) advance() error {
ErrorChan := make(chan error, 1)
finalityChan := make(chan consensus_core.FinalityUpdate, 1)
go func() {
finalityUpdate, err := in.RPC.GetFinalityUpdate()
if err != nil {
ErrorChan <- err
return
}
finalityChan <- finalityUpdate
ErrorChan <- nil
}()
if ErrorChan != nil {
return <-ErrorChan
}
finalityUpdate := <-finalityChan
if err := in.verify_finality_update(&finalityUpdate); err != nil {
return err
}
in.apply_finality_update(&finalityUpdate)
// Fetch and apply optimistic update
optimisticUpdate, err := in.RPC.GetOptimisticUpdate()
if err != nil {
return err
}
if err := in.verify_optimistic_update(&optimisticUpdate); err != nil {
return err
}
in.apply_optimistic_update(&optimisticUpdate)
// Check for sync committee update if it's not set
if in.Store.NextSyncCommitee == nil {
log.Printf("checking for sync committee update")
currentPeriod := utils.CalcSyncPeriod(in.Store.FinalizedHeader.Slot)
updates, err := in.RPC.GetUpdates(currentPeriod, 1)
if err != nil {
return err
}
if len(updates) == 1 {
update := updates[0]
if err := in.verify_update(&update); err == nil {
log.Printf("updating sync committee")
in.apply_update(&update)
}
}
}
return nil
}
func (in *Inner) sync(checkpoint [32]byte) error {
// Reset store and checkpoint
in.Store = LightClientStore{}
in.lastCheckpoint = nil
// Perform bootstrap with the given checkpoint
in.bootstrap(checkpoint)
currentPeriod := utils.CalcSyncPeriod(in.Store.FinalizedHeader.Slot)
errorChan := make(chan error, 1)
var updates []consensus_core.Update
var err error
go func() {
updates, err = in.RPC.GetUpdates(currentPeriod, MAX_REQUEST_LIGHT_CLIENT_UPDATES)
if err != nil {
errorChan <- err
}
// Apply updates
for _, update := range updates {
if err := in.verify_update(&update); err != nil {
errorChan <- err
return
}
in.apply_update(&update)
}
finalityUpdate, err := in.RPC.GetFinalityUpdate()
if err != nil {
errorChan <- err
return
}
if err := in.verify_finality_update(&finalityUpdate); err != nil {
errorChan <- err
return
}
in.apply_finality_update(&finalityUpdate)
// Fetch and apply optimistic update
optimisticUpdate, err := in.RPC.GetOptimisticUpdate()
if err != nil {
errorChan <- err
return
}
if err := in.verify_optimistic_update(&optimisticUpdate); err != nil {
errorChan <- err
return
}
in.apply_optimistic_update(&optimisticUpdate)
errorChan <- nil
log.Printf("consensus client in sync with checkpoint: 0x%s", hex.EncodeToString(checkpoint[:]))
}()
if err := <-errorChan; err != nil {
return err
}
// Log the success message
return nil
}
func (in *Inner) send_blocks() error {
// Get slot from the optimistic header
slot := in.Store.OptimisticHeader.Slot
payload, err := in.get_execution_payload(&slot)
if err != nil {
return err
}
// Get finalized slot from the finalized header
finalizedSlot := in.Store.FinalizedHeader.Slot
finalizedPayload, err := in.get_execution_payload(&finalizedSlot)
if err != nil {
return err
}
// Send payload converted to block over the BlockSend channel
go func() {
block, err := PayloadToBlock(payload)
if err != nil {
log.Printf("Error converting payload to block: %v", err)
return
}
in.blockSend <- block
}()
go func() {
block, err := PayloadToBlock(finalizedPayload)
if err != nil {
log.Printf("Error converting finalized payload to block: %v", err)
return
}
in.finalizedBlockSend <- block
}()
// Send checkpoint over the CheckpointSend channel
go func() {
in.checkpointSend <- in.lastCheckpoint
}()
return nil
}
// / Gets the duration until the next update
// / Updates are scheduled for 4 seconds into each slot
func (in *Inner) duration_until_next_update() time.Duration {
currentSlot := in.expected_current_slot()
nextSlot := currentSlot + 1
nextSlotTimestamp := nextSlot*12 + in.Config.Chain.GenesisTime
now := uint64(time.Now().Unix())
timeToNextSlot := int64(nextSlotTimestamp - now)
nextUpdate := timeToNextSlot + 4
return time.Duration(nextUpdate) * time.Second
}
func (in *Inner) bootstrap(checkpoint [32]byte) {
errorChan := make(chan error, 1)
bootstrapChan := make(chan consensus_core.Bootstrap, 1)
go func() {
bootstrap, errInBootstrap := in.RPC.GetBootstrap(checkpoint)
if errInBootstrap != nil {
log.Printf("failed to fetch bootstrap: %v", errInBootstrap)
errorChan <- errInBootstrap
return
}
bootstrapChan <- bootstrap
errorChan <- nil
}()
if err := <-errorChan; err != nil {
return
}
bootstrap := <-bootstrapChan
isValid := in.is_valid_checkpoint(bootstrap.Header.Slot)
if !isValid {
if in.Config.StrictCheckpointAge {
panic("checkpoint too old, consider using a more recent checkpoint")
} else {
log.Printf("checkpoint too old, consider using a more recent checkpoint")
}
}
verify_bootstrap(checkpoint, bootstrap)
apply_bootstrap(&in.Store, bootstrap)
}
func verify_bootstrap(checkpoint [32]byte, bootstrap consensus_core.Bootstrap) {
isCommitteValid := isCurrentCommitteeProofValid(&bootstrap.Header, &bootstrap.CurrentSyncCommittee, bootstrap.CurrentSyncCommitteeBranch)
if !isCommitteValid {
log.Println("invalid current sync committee proof")
return
}
headerHash := toGethHeader(&bootstrap.Header).Hash()
HeaderValid := bytes.Equal(headerHash[:], checkpoint[:])
if !HeaderValid {
log.Println("invalid header hash")
return
}
}
func apply_bootstrap(store *LightClientStore, bootstrap consensus_core.Bootstrap) {
store.FinalizedHeader = bootstrap.Header
store.CurrentSyncCommitee = bootstrap.CurrentSyncCommittee
store.NextSyncCommitee = nil
store.OptimisticHeader = bootstrap.Header
store.PreviousMaxActiveParticipants = 0
store.CurrentMaxActiveParticipants = 0
}
func (in *Inner) verify_generic_update(update *GenericUpdate, expectedCurrentSlot uint64, store *LightClientStore, genesisRoots []byte, forks consensus_core.Forks) error {
{
bits := getBits(update.SyncAggregate.SyncCommitteeBits[:])
if bits == 0 {
return ErrInsufficientParticipation
}
updateFinalizedSlot := update.FinalizedHeader.Slot
validTime := expectedCurrentSlot >= update.SignatureSlot &&
update.SignatureSlot > update.AttestedHeader.Slot &&
update.AttestedHeader.Slot >= updateFinalizedSlot
if !validTime {
return ErrInvalidTimestamp
}
storePeriod := utils.CalcSyncPeriod(store.FinalizedHeader.Slot)
updateSigPeriod := utils.CalcSyncPeriod(update.SignatureSlot)
var validPeriod bool
if store.NextSyncCommitee != nil {
validPeriod = updateSigPeriod == storePeriod || updateSigPeriod == storePeriod+1
} else {
validPeriod = updateSigPeriod == storePeriod
}
if !validPeriod {
return ErrInvalidPeriod
}
updateAttestedPeriod := utils.CalcSyncPeriod(update.AttestedHeader.Slot)
updateHasNextCommittee := store.NextSyncCommitee == nil && update.NextSyncCommittee != nil && updateAttestedPeriod == storePeriod
if update.AttestedHeader.Slot <= store.FinalizedHeader.Slot && !updateHasNextCommittee {
return ErrNotRelevant
}
// Validate finalized header and finality branch
if update.FinalizedHeader != (consensus_core.Header{}) && update.FinalityBranch != nil {
if !isFinalityProofValid(&update.AttestedHeader, &update.FinalizedHeader, update.FinalityBranch) {
return ErrInvalidFinalityProof
}
} else if (update.FinalizedHeader != (consensus_core.Header{}) && update.FinalityBranch == nil) || (update.FinalizedHeader == (consensus_core.Header{}) && update.FinalityBranch != nil) {
return ErrInvalidFinalityProof
}
if update.NextSyncCommittee != nil && update.NextSyncCommitteeBranch != nil {
if !isNextCommitteeProofValid(&update.AttestedHeader, update.NextSyncCommittee, *update.NextSyncCommitteeBranch) {
return ErrInvalidNextSyncCommitteeProof
}
} else if (update.NextSyncCommittee != nil && update.NextSyncCommitteeBranch == nil) || (update.NextSyncCommittee == nil && update.NextSyncCommitteeBranch != nil) {
return ErrInvalidNextSyncCommitteeProof
}
// Set sync committee based on updateSigPeriod
var syncCommittee *consensus_core.SyncCommittee
if updateSigPeriod == storePeriod {
syncCommittee = &in.Store.CurrentSyncCommitee
} else {
syncCommittee = in.Store.NextSyncCommitee
}
_, err := utils.GetParticipatingKeys(syncCommittee, [64]byte(update.SyncAggregate.SyncCommitteeBits))
if err != nil {
return fmt.Errorf("failed to get participating keys: %w", err)
}
forkVersion := utils.CalculateForkVersion(&forks, update.SignatureSlot)
forkDataRoot := utils.ComputeForkDataRoot(forkVersion, consensus_core.Bytes32(genesisRoots))
if !verifySyncCommitteeSignature(syncCommittee.Pubkeys, &update.AttestedHeader, &update.SyncAggregate, forkDataRoot) {
return ErrInvalidSignature
}
return nil
}
}
func (in *Inner) verify_update(update *consensus_core.Update) error {
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
NextSyncCommittee: &update.NextSyncCommittee,
NextSyncCommitteeBranch: &update.NextSyncCommitteeBranch,
FinalizedHeader: update.FinalizedHeader,
FinalityBranch: update.FinalityBranch,
}
return in.verify_generic_update(&genUpdate, in.expected_current_slot(), &in.Store, in.Config.Chain.GenesisRoot, in.Config.Forks)
}
func (in *Inner) verify_finality_update(update *consensus_core.FinalityUpdate) error {
if update == nil {
return ErrInvalidUpdate
}
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
FinalizedHeader: update.FinalizedHeader,
FinalityBranch: update.FinalityBranch,
}
return in.verify_generic_update(&genUpdate, in.expected_current_slot(), &in.Store, in.Config.Chain.GenesisRoot, in.Config.Forks)
}
func (in *Inner) verify_optimistic_update(update *consensus_core.OptimisticUpdate) error {
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
}
return in.verify_generic_update(&genUpdate, in.expected_current_slot(), &in.Store, in.Config.Chain.GenesisRoot, in.Config.Forks)
}
func (in *Inner) apply_generic_update(store *LightClientStore, update *GenericUpdate) *[]byte {
committeeBits := getBits(update.SyncAggregate.SyncCommitteeBits[:])
// Update max active participants
if committeeBits > store.CurrentMaxActiveParticipants {
store.CurrentMaxActiveParticipants = committeeBits
}
// Determine if we should update the optimistic header
shouldUpdateOptimistic := committeeBits > in.safety_threshold() &&
update.AttestedHeader.Slot > store.OptimisticHeader.Slot
if shouldUpdateOptimistic {
store.OptimisticHeader = update.AttestedHeader
}
updateAttestedPeriod := utils.CalcSyncPeriod(update.AttestedHeader.Slot)
updateFinalizedSlot := uint64(0)
if update.FinalizedHeader != (consensus_core.Header{}) {
updateFinalizedSlot = update.FinalizedHeader.Slot
}
updateFinalizedPeriod := utils.CalcSyncPeriod(updateFinalizedSlot)
updateHasFinalizedNextCommittee := in.Store.NextSyncCommitee == nil &&
in.has_sync_update(update) && in.has_finality_update(update) &&
updateFinalizedPeriod == updateAttestedPeriod
// Determine if we should apply the update
hasMajority := committeeBits*3 >= 512*2
if !hasMajority {
log.Println("skipping block with low vote count")
}
updateIsNewer := updateFinalizedSlot > store.FinalizedHeader.Slot
goodUpdate := updateIsNewer || updateHasFinalizedNextCommittee
shouldApplyUpdate := hasMajority && goodUpdate
// Apply the update if conditions are met
if shouldApplyUpdate {
storePeriod := utils.CalcSyncPeriod(store.FinalizedHeader.Slot)
// Sync committee update logic
if store.NextSyncCommitee == nil {
store.NextSyncCommitee = update.NextSyncCommittee
} else if updateFinalizedPeriod == storePeriod+1 {
log.Println("sync committee updated")
store.CurrentSyncCommitee = *store.NextSyncCommitee
store.NextSyncCommitee = update.NextSyncCommittee
store.PreviousMaxActiveParticipants = store.CurrentMaxActiveParticipants
store.CurrentMaxActiveParticipants = 0
}
// Update finalized header
if updateFinalizedSlot > store.FinalizedHeader.Slot {
store.FinalizedHeader = update.FinalizedHeader
if store.FinalizedHeader.Slot > store.OptimisticHeader.Slot {
store.OptimisticHeader = store.FinalizedHeader
}
if store.FinalizedHeader.Slot%32 == 0 {
checkpoint := toGethHeader(&store.FinalizedHeader).Hash()
checkpointBytes := checkpoint.Bytes()
return &checkpointBytes
}
}
}
return nil
}
func (in *Inner) apply_update(update *consensus_core.Update) {
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
NextSyncCommittee: &update.NextSyncCommittee,
NextSyncCommitteeBranch: &update.NextSyncCommitteeBranch,
FinalizedHeader: update.FinalizedHeader,
FinalityBranch: update.FinalityBranch,
}
checkpoint := in.apply_generic_update(&in.Store, &genUpdate)
if checkpoint != nil {
in.lastCheckpoint = checkpoint
}
}
func (in *Inner) apply_finality_update(update *consensus_core.FinalityUpdate) {
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
FinalizedHeader: update.FinalizedHeader,
FinalityBranch: update.FinalityBranch,
}
checkpoint := in.apply_generic_update(&in.Store, &genUpdate)
if checkpoint != nil {
in.lastCheckpoint = checkpoint
}
}
func (in *Inner) apply_optimistic_update(update *consensus_core.OptimisticUpdate) {
genUpdate := GenericUpdate{
AttestedHeader: update.AttestedHeader,
SyncAggregate: update.SyncAggregate,
SignatureSlot: update.SignatureSlot,
}
checkpoint := in.apply_generic_update(&in.Store, &genUpdate)
if checkpoint != nil {
in.lastCheckpoint = checkpoint
}
}
func (in *Inner) Log_finality_update(update *consensus_core.FinalityUpdate) {
participation := float32(getBits(update.SyncAggregate.SyncCommitteeBits[:])) / 512.0 * 100.0
decimals := 2
if participation == 100.0 {
decimals = 1
}
age := in.Age(in.Store.FinalizedHeader.Slot)
days := age.Hours() / 24
hours := int(age.Hours()) % 24
minutes := int(age.Minutes()) % 60
seconds := int(age.Seconds()) % 60
log.Printf(
"finalized slot slot=%d confidence=%.*f%% age=%02d:%02d:%02d:%02d",
in.Store.FinalizedHeader.Slot, decimals, participation, int(days), hours, minutes, seconds,
)
}
func (in *Inner) Log_optimistic_update(update *consensus_core.OptimisticUpdate) {
participation := float32(getBits(update.SyncAggregate.SyncCommitteeBits[:])) / 512.0 * 100.0
decimals := 2
if participation == 100.0 {
decimals = 1
}
age := in.Age(in.Store.OptimisticHeader.Slot)
days := age.Hours() / 24
hours := int(age.Hours()) % 24
minutes := int(age.Minutes()) % 60
seconds := int(age.Seconds()) % 60
log.Printf(
"updated head slot=%d confidence=%.*f%% age=%02d:%02d:%02d:%02d",
in.Store.OptimisticHeader.Slot, decimals, participation, int(days), hours, minutes, seconds,
)
}
func (in *Inner) has_finality_update(update *GenericUpdate) bool {
return update.FinalizedHeader != (consensus_core.Header{}) && update.FinalityBranch != nil
}
func (in *Inner) has_sync_update(update *GenericUpdate) bool {
return update.NextSyncCommittee != nil && update.NextSyncCommitteeBranch != nil
}
func (in *Inner) safety_threshold() uint64 {
return max(in.Store.CurrentMaxActiveParticipants, in.Store.PreviousMaxActiveParticipants) / 2
}
// verifySyncCommitteeSignature verifies the sync committee signature.
func verifySyncCommitteeSignature(
pks []consensus_core.BLSPubKey, // Public keys slice
attestedHeader *consensus_core.Header, // Attested header
signature *consensus_core.SyncAggregate, // Signature bytes
forkDataRoot consensus_core.Bytes32, // Fork data root
) bool {
if signature == nil {
fmt.Println("no signature")
return false
}
// Create a slice to hold the collected public keys.
collectedPks := make([]*bls.Pubkey, 0, len(pks))
for i := range pks {
var pksinBytes [48]byte = [48]byte(pks[i])
dkey := new(bls.Pubkey)
err := dkey.Deserialize(&pksinBytes)
if err != nil {
fmt.Println("error deserializing public key:", err)
return false
}
// Include the public key only if the corresponding SyncCommitteeBits bit is set.
if signature.SyncCommitteeBits[i/8]&(byte(1)<<(i%8)) != 0 {
collectedPks = append(collectedPks, dkey)
}
}
// Compute signingRoot.
signingRoot := ComputeCommitteeSignRoot(toGethHeader(attestedHeader), forkDataRoot)
var sig bls.Signature
signatureForUnmarshalling := [96]byte(signature.SyncCommitteeSignature)
// Deserialize the signature.
if err := sig.Deserialize(&signatureForUnmarshalling); err != nil {
fmt.Println("error deserializing signature:", err)
return false
}
// Check if we have collected any public keys before proceeding.
if len(collectedPks) == 0 {
fmt.Println("no valid public keys collected")
return false
}
return utils.FastAggregateVerify(collectedPks, signingRoot[:], &sig)
}
func ComputeCommitteeSignRoot(header *beacon.Header, fork consensus_core.Bytes32) consensus_core.Bytes32 {
// Domain type for the sync committee
domainType := [4]byte{7, 0, 0, 0}
// Compute the domain
domain := utils.ComputeDomain(domainType, fork)
// Compute and return the signing root
return utils.ComputeSigningRoot(header, domain)
}
func (in *Inner) Age(slot uint64) time.Duration {
expectedTime := slot*12 + in.Config.Chain.GenesisTime
now := time.Now().Unix()
return time.Duration(uint64(now)-expectedTime) * time.Second
}
func (in *Inner) expected_current_slot() uint64 {
const SLOT_DURATION = 12
now := time.Now().Unix()
sinceGenesis := now - int64(in.Config.Chain.GenesisTime)
return uint64(sinceGenesis) / SLOT_DURATION
}
func (in *Inner) is_valid_checkpoint(blockHashSlot uint64) bool {
const SLOT_DURATION = 12
currentSlot := in.expected_current_slot()
currentSlotTimestamp := int64(in.Config.Chain.GenesisTime) + int64(currentSlot)*SLOT_DURATION
blockhashSlotTimestamp := int64(in.Config.Chain.GenesisTime) + int64(blockHashSlot)*SLOT_DURATION
slotAge := currentSlotTimestamp - blockhashSlotTimestamp
return uint64(slotAge) < in.Config.MaxCheckpointAge
}
func isFinalityProofValid(attestedHeader *consensus_core.Header, finalizedHeader *consensus_core.Header, finalityBranch []consensus_core.Bytes32) bool {
return utils.IsProofValid(attestedHeader, toGethHeader(finalizedHeader).Hash(), finalityBranch, 6, 105)
}
func isCurrentCommitteeProofValid(attestedHeader *consensus_core.Header, currentCommittee *consensus_core.SyncCommittee, currentCommitteeBranch []consensus_core.Bytes32) bool {
return utils.IsProofValid(attestedHeader, toGethSyncCommittee(currentCommittee).Root(), currentCommitteeBranch, 5, 54)
}
func isNextCommitteeProofValid(attestedHeader *consensus_core.Header, nextCommittee *consensus_core.SyncCommittee, nextCommitteeBranch []consensus_core.Bytes32) bool {
return utils.IsProofValid(attestedHeader, toGethSyncCommittee(nextCommittee).Root(), nextCommitteeBranch, 5, 55)
}
func PayloadToBlock(value *consensus_core.ExecutionPayload) (*common.Block, error) {
emptyNonce := "0x0000000000000000"
emptyUncleHash := geth.HexToHash("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
// Allocate txs on the heap by using make with a predefined capacity