This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathibft.go
1069 lines (888 loc) · 28.3 KB
/
ibft.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 ibft
import (
"crypto/ecdsa"
"fmt"
"math"
"reflect"
"time"
"github.com/0xPolygon/polygon-sdk/consensus"
"github.com/0xPolygon/polygon-sdk/consensus/ibft/proto"
"github.com/0xPolygon/polygon-sdk/crypto"
"github.com/0xPolygon/polygon-sdk/helper/hex"
"github.com/0xPolygon/polygon-sdk/network"
"github.com/0xPolygon/polygon-sdk/protocol"
"github.com/0xPolygon/polygon-sdk/secrets"
"github.com/0xPolygon/polygon-sdk/state"
"github.com/0xPolygon/polygon-sdk/types"
"github.com/hashicorp/go-hclog"
any "google.golang.org/protobuf/types/known/anypb"
)
const (
DefaultEpochSize = 100000
)
type blockchainInterface interface {
Header() *types.Header
GetHeaderByNumber(i uint64) (*types.Header, bool)
WriteBlocks(blocks []*types.Block) error
CalculateGasLimit(number uint64) (uint64, error)
}
type transactionPoolInterface interface {
ResetWithHeader(h *types.Header)
Pop() (*types.Transaction, func())
DecreaseAccountNonce(tx *types.Transaction)
Length() uint64
}
// Ibft represents the IBFT consensus mechanism object
type Ibft struct {
sealing bool // Flag indicating if the node is a sealer
logger hclog.Logger // Output logger
config *consensus.Config // Consensus configuration
state *currentState // Reference to the current state
blockchain blockchainInterface // Interface exposed by the blockchain layer
executor *state.Executor // Reference to the state executor
closeCh chan struct{} // Channel for closing
validatorKey *ecdsa.PrivateKey // Private key for the validator
validatorKeyAddr types.Address
txpool transactionPoolInterface // Reference to the transaction pool
store *snapshotStore // Snapshot store that keeps track of all snapshots
epochSize uint64
msgQueue *msgQueue // Structure containing different message queues
updateCh chan struct{} // Update channel
syncer *protocol.Syncer // Reference to the sync protocol
syncNotifyCh chan bool // Sync protocol notification channel
network *network.Server // Reference to the networking layer
transport transport // Reference to the transport protocol
operator *operator
// aux test methods
forceTimeoutCh bool
metrics *consensus.Metrics
secretsManager secrets.SecretsManager
}
// Factory implements the base consensus Factory method
func Factory(
params *consensus.ConsensusParams,
) (consensus.Consensus, error) {
p := &Ibft{
logger: params.Logger.Named("ibft"),
config: params.Config,
blockchain: params.Blockchain,
executor: params.Executor,
closeCh: make(chan struct{}),
txpool: params.Txpool,
state: ¤tState{},
network: params.Network,
epochSize: DefaultEpochSize,
syncNotifyCh: make(chan bool),
sealing: params.Seal,
metrics: params.Metrics,
secretsManager: params.SecretsManager,
}
// Istanbul requires a different header hash function
types.HeaderHash = istanbulHeaderHash
p.syncer = protocol.NewSyncer(params.Logger, params.Network, params.Blockchain)
// register the grpc operator
p.operator = &operator{ibft: p}
proto.RegisterIbftOperatorServer(params.Grpc, p.operator)
// Set up the node's validator key
if err := p.createKey(); err != nil {
return nil, err
}
p.logger.Info("validator key", "addr", p.validatorKeyAddr.String())
// start the transport protocol
if err := p.setupTransport(); err != nil {
return nil, err
}
return p, nil
}
// Start starts the IBFT consensus
func (i *Ibft) Start() error {
// Start the syncer
i.syncer.Start()
// Set up the snapshots
if err := i.setupSnapshot(); err != nil {
return err
}
// Start the actual IBFT protocol
go i.start()
return nil
}
type transport interface {
Gossip(msg *proto.MessageReq) error
}
// Define the IBFT libp2p protocol
var ibftProto = "/ibft/0.1"
type gossipTransport struct {
topic *network.Topic
}
// Gossip publishes a new message to the topic
func (g *gossipTransport) Gossip(msg *proto.MessageReq) error {
return g.topic.Publish(msg)
}
// setupTransport sets up the gossip transport protocol
func (i *Ibft) setupTransport() error {
// Define a new topic
topic, err := i.network.NewTopic(ibftProto, &proto.MessageReq{})
if err != nil {
return err
}
// Subscribe to the newly created topic
err = topic.Subscribe(func(obj interface{}) {
msg := obj.(*proto.MessageReq)
if !i.isSealing() {
// if we are not sealing we do not care about the messages
// but we need to subscribe to propagate the messages
return
}
// decode sender
if err := validateMsg(msg); err != nil {
i.logger.Error("failed to validate msg", "err", err)
return
}
if msg.From == i.validatorKeyAddr.String() {
// we are the sender, skip this message since we already
// relay our own messages internally.
return
}
i.pushMessage(msg)
})
if err != nil {
return err
}
i.transport = &gossipTransport{topic: topic}
return nil
}
// createKey sets the validator's private key from the secrets manager
func (i *Ibft) createKey() error {
i.msgQueue = newMsgQueue()
i.closeCh = make(chan struct{})
i.updateCh = make(chan struct{})
if i.validatorKey == nil {
// Check if the validator key is initialized
var key *ecdsa.PrivateKey
if i.secretsManager.HasSecret(secrets.ValidatorKey) {
// The validator key is present in the secrets manager, load it
validatorKey, readErr := crypto.ReadConsensusKey(i.secretsManager)
if readErr != nil {
return fmt.Errorf("unable to read validator key from Secrets Manager, %v", readErr)
}
key = validatorKey
} else {
// The validator key is not present in the secrets manager, generate it
validatorKey, validatorKeyEncoded, genErr := crypto.GenerateAndEncodePrivateKey()
if genErr != nil {
return fmt.Errorf("unable to generate validator key for Secrets Manager, %v", genErr)
}
// Save the key to the secrets manager
saveErr := i.secretsManager.SetSecret(secrets.ValidatorKey, validatorKeyEncoded)
if saveErr != nil {
return fmt.Errorf("unable to save validator key to Secrets Manager, %v", saveErr)
}
key = validatorKey
}
i.validatorKey = key
i.validatorKeyAddr = crypto.PubKeyToAddress(&key.PublicKey)
}
return nil
}
const IbftKeyName = "validator.key"
// start starts the IBFT consensus state machine
func (i *Ibft) start() {
// consensus always starts in SyncState mode in case it needs
// to sync with other nodes.
i.setState(SyncState)
// Grab the latest header
header := i.blockchain.Header()
i.logger.Debug("current sequence", "sequence", header.Number+1)
for {
select {
case <-i.closeCh:
return
default: // Default is here because we would block until we receive something in the closeCh
}
// Start the state machine loop
i.runCycle()
}
}
// runCycle represents the IBFT state machine loop
func (i *Ibft) runCycle() {
// Log to the console
if i.state.view != nil {
i.logger.Debug("cycle", "state", i.getState(), "sequence", i.state.view.Sequence, "round", i.state.view.Round)
}
// Based on the current state, execute the corresponding section
switch i.getState() {
case AcceptState:
i.runAcceptState()
case ValidateState:
i.runValidateState()
case RoundChangeState:
i.runRoundChangeState()
case SyncState:
i.runSyncState()
}
}
// isValidSnapshot checks if the current node is in the validator set for the latest snapshot
func (i *Ibft) isValidSnapshot() bool {
if !i.isSealing() {
return false
}
// check if we are a validator and enabled
header := i.blockchain.Header()
snap, err := i.getSnapshot(header.Number)
if err != nil {
return false
}
if snap.Set.Includes(i.validatorKeyAddr) {
i.state.view = &proto.View{
Sequence: header.Number + 1,
Round: 0,
}
return true
}
return false
}
// runSyncState implements the Sync state loop.
//
// It fetches fresh data from the blockchain. Checks if the current node is a validator and resolves any pending blocks
func (i *Ibft) runSyncState() {
for i.isState(SyncState) {
// try to sync with some target peer
p := i.syncer.BestPeer()
if p == nil {
// if we do not have any peers and we have been a validator
// we can start now. In case we start on another fork this will be
// reverted later
if i.isValidSnapshot() {
// initialize the round and sequence
header := i.blockchain.Header()
i.state.view = &proto.View{
Round: 0,
Sequence: header.Number + 1,
}
//Set the round metric
i.metrics.Rounds.Set(float64(i.state.view.Round))
i.setState(AcceptState)
} else {
time.Sleep(1 * time.Second)
}
continue
}
if err := i.syncer.BulkSyncWithPeer(p); err != nil {
i.logger.Error("failed to bulk sync", "err", err)
continue
}
// if we are a validator we do not even want to wait here
// we can just move ahead
if i.isValidSnapshot() {
i.setState(AcceptState)
continue
}
// start watch mode
var isValidator bool
i.syncer.WatchSyncWithPeer(p, func(b *types.Block) bool {
i.syncer.Broadcast(b)
isValidator = i.isValidSnapshot()
return isValidator
})
if isValidator {
// at this point, we are in sync with the latest chain we know of
// and we are a validator of that chain so we need to change to AcceptState
// so that we can start to do some stuff there
i.setState(AcceptState)
}
}
}
var defaultBlockPeriod = 2 * time.Second
// buildBlock builds the block, based on the passed in snapshot and parent header
func (i *Ibft) buildBlock(snap *Snapshot, parent *types.Header) (*types.Block, error) {
header := &types.Header{
ParentHash: parent.Hash,
Number: parent.Number + 1,
Miner: types.Address{},
Nonce: types.Nonce{},
MixHash: IstanbulDigest,
Difficulty: parent.Number + 1, // we need to do this because blockchain needs difficulty to organize blocks and forks
StateRoot: types.EmptyRootHash, // this avoids needing state for now
Sha3Uncles: types.EmptyUncleHash,
GasLimit: parent.GasLimit, // Inherit from parent for now, will need to adjust dynamically later.
}
// calculate gas limit based on parent header
gasLimit, err := i.blockchain.CalculateGasLimit(header.Number)
if err != nil {
return nil, err
}
header.GasLimit = gasLimit
// try to pick a candidate
if candidate := i.operator.getNextCandidate(snap); candidate != nil {
header.Miner = types.StringToAddress(candidate.Address)
if candidate.Auth {
header.Nonce = nonceAuthVote
} else {
header.Nonce = nonceDropVote
}
}
// set the timestamp
parentTime := time.Unix(int64(parent.Timestamp), 0)
headerTime := parentTime.Add(defaultBlockPeriod)
if headerTime.Before(time.Now()) {
headerTime = time.Now()
}
header.Timestamp = uint64(headerTime.Unix())
// we need to include in the extra field the current set of validators
putIbftExtraValidators(header, snap.Set)
transition, err := i.executor.BeginTxn(parent.StateRoot, header, i.validatorKeyAddr)
if err != nil {
return nil, err
}
txns := i.writeTransactions(gasLimit, transition)
_, root := transition.Commit()
header.StateRoot = root
header.GasUsed = transition.TotalGas()
// build the block
block := consensus.BuildBlock(consensus.BuildBlockParams{
Header: header,
Txns: txns,
Receipts: transition.Receipts(),
})
// write the seal of the block after all the fields are completed
header, err = writeSeal(i.validatorKey, block.Header)
if err != nil {
return nil, err
}
block.Header = header
// compute the hash, this is only a provisional hash since the final one
// is sealed after all the committed seals
block.Header.ComputeHash()
i.logger.Info("build block", "number", header.Number, "txns", len(txns))
return block, nil
}
type transitionInterface interface {
Write(txn *types.Transaction) error
}
// writeTransactions writes transactions from the txpool to the transition object
// and returns transactions that were included in the transition (new block)
func (i *Ibft) writeTransactions(gasLimit uint64, transition transitionInterface) []*types.Transaction {
txns := []*types.Transaction{}
returnTxnFuncs := []func(){}
for {
txn, retTxnFn := i.txpool.Pop()
if txn == nil {
break
}
if txn.ExceedsBlockGasLimit(gasLimit) {
i.logger.Error(fmt.Sprintf("failed to write transaction: %v", state.ErrBlockLimitExceeded))
i.txpool.DecreaseAccountNonce(txn)
continue
}
if err := transition.Write(txn); err != nil {
if _, ok := err.(*state.GasLimitReachedTransitionApplicationError); ok {
returnTxnFuncs = append(returnTxnFuncs, retTxnFn)
break
} else if appErr, ok := err.(*state.TransitionApplicationError); ok && appErr.IsRecoverable {
returnTxnFuncs = append(returnTxnFuncs, retTxnFn)
} else {
i.txpool.DecreaseAccountNonce(txn)
}
continue
}
txns = append(txns, txn)
}
// we return recoverable txns that were popped from the txpool after the above for loop breaks,
// since we don't want to return the tx to the pool just to pop the same txn in the next loop iteration
for _, retFunc := range returnTxnFuncs {
retFunc()
}
i.logger.Info("picked out txns from pool", "num", len(txns), "remaining", i.txpool.Length())
return txns
}
// runAcceptState runs the Accept state loop
//
// The Accept state always checks the snapshot, and the validator set. If the current node is not in the validators set,
// it moves back to the Sync state. On the other hand, if the node is a validator, it calculates the proposer.
// If it turns out that the current node is the proposer, it builds a block, and sends preprepare and then prepare messages.
func (i *Ibft) runAcceptState() { // start new round
logger := i.logger.Named("acceptState")
logger.Info("Accept state", "sequence", i.state.view.Sequence)
// This is the state in which we either propose a block or wait for the pre-prepare message
parent := i.blockchain.Header()
number := parent.Number + 1
if number != i.state.view.Sequence {
i.logger.Error("sequence not correct", "parent", parent.Number, "sequence", i.state.view.Sequence)
i.setState(SyncState)
return
}
snap, err := i.getSnapshot(parent.Number)
if err != nil {
i.logger.Error("cannot find snapshot", "num", parent.Number)
i.setState(SyncState)
return
}
if !snap.Set.Includes(i.validatorKeyAddr) {
// we are not a validator anymore, move back to sync state
i.logger.Info("we are not a validator anymore")
i.setState(SyncState)
return
}
i.logger.Info("current snapshot", "validators", len(snap.Set), "votes", len(snap.Votes))
i.state.validators = snap.Set
//Update the No.of validator metric
i.metrics.Validators.Set(float64(len(snap.Set)))
// reset round messages
i.state.resetRoundMsgs()
// select the proposer of the block
var lastProposer types.Address
if parent.Number != 0 {
lastProposer, _ = ecrecoverFromHeader(parent)
}
i.state.CalcProposer(lastProposer)
if i.state.proposer == i.validatorKeyAddr {
logger.Info("we are the proposer", "block", number)
if !i.state.locked {
// since the state is not locked, we need to build a new block
i.state.block, err = i.buildBlock(snap, parent)
if err != nil {
i.logger.Error("failed to build block", "err", err)
i.setState(RoundChangeState)
return
}
// calculate how much time do we have to wait to mine the block
delay := time.Until(time.Unix(int64(i.state.block.Header.Timestamp), 0))
select {
case <-time.After(delay):
case <-i.closeCh:
return
}
}
// send the preprepare message as an RLP encoded block
i.sendPreprepareMsg()
// send the prepare message since we are ready to move the state
i.sendPrepareMsg()
// move to validation state for new prepare messages
i.setState(ValidateState)
return
}
i.logger.Info("proposer calculated", "proposer", i.state.proposer, "block", number)
// we are NOT a proposer for the block. Then, we have to wait
// for a pre-prepare message from the proposer
timeout := i.randomTimeout()
for i.getState() == AcceptState {
msg, ok := i.getNextMessage(timeout)
if !ok {
return
}
if msg == nil {
i.setState(RoundChangeState)
continue
}
if msg.From != i.state.proposer.String() {
i.logger.Error("msg received from wrong proposer")
continue
}
// retrieve the block proposal
block := &types.Block{}
if err := block.UnmarshalRLP(msg.Proposal.Value); err != nil {
i.logger.Error("failed to unmarshal block", "err", err)
i.setState(RoundChangeState)
return
}
if i.state.locked {
// the state is locked, we need to receive the same block
if block.Hash() == i.state.block.Hash() {
// fast-track and send a commit message and wait for validations
i.sendCommitMsg()
i.setState(ValidateState)
} else {
i.handleStateErr(errIncorrectBlockLocked)
}
} else {
// since its a new block, we have to verify it first
if err := i.verifyHeaderImpl(snap, parent, block.Header); err != nil {
i.logger.Error("block verification failed", "err", err)
i.handleStateErr(errBlockVerificationFailed)
} else {
i.state.block = block
// send prepare message and wait for validations
i.sendPrepareMsg()
i.setState(ValidateState)
}
}
}
}
// runValidateState implements the Validate state loop.
//
// The Validate state is rather simple - all nodes do in this state is read messages and add them to their local snapshot state
func (i *Ibft) runValidateState() {
hasCommitted := false
sendCommit := func() {
// at this point either we have enough prepare messages
// or commit messages so we can lock the block
i.state.lock()
if !hasCommitted {
// send the commit message
i.sendCommitMsg()
hasCommitted = true
}
}
timeout := i.randomTimeout()
for i.getState() == ValidateState {
msg, ok := i.getNextMessage(timeout)
if !ok {
// closing
return
}
if msg == nil {
i.setState(RoundChangeState)
continue
}
switch msg.Type {
case proto.MessageReq_Prepare:
i.state.addPrepared(msg)
case proto.MessageReq_Commit:
i.state.addCommitted(msg)
default:
panic(fmt.Sprintf("BUG: %s", reflect.TypeOf(msg.Type)))
}
if i.state.numPrepared() > i.state.NumValid() {
// we have received enough pre-prepare messages
sendCommit()
}
if i.state.numCommitted() > i.state.NumValid() {
// we have received enough commit messages
sendCommit()
// try to commit the block (TODO: just to get out of the loop)
i.setState(CommitState)
}
}
if i.getState() == CommitState {
// at this point either if it works or not we need to unlock
block := i.state.block
i.state.unlock()
if err := i.insertBlock(block); err != nil {
// start a new round with the state unlocked since we need to
// be able to propose/validate a different block
i.logger.Error("failed to insert block", "err", err)
i.handleStateErr(errFailedToInsertBlock)
} else {
// update metrics
i.updateMetrics(block)
// move ahead to the next block
i.setState(AcceptState)
}
}
}
// updateMetrics will update various metrics based on the given block
// currently we capture No.of Txs and block interval metrics using this function
func (i *Ibft) updateMetrics(block *types.Block) {
prvHeader, _ := i.blockchain.GetHeaderByNumber(block.Number() - 1)
parentTime := time.Unix(int64(prvHeader.Timestamp), 0)
headerTime := time.Unix(int64(block.Header.Timestamp), 0)
//Update the block interval metric
if block.Number() > 1 {
i.metrics.BlockInterval.Observe(
headerTime.Sub(parentTime).Seconds(),
)
}
//Update the Number of transactions in the block metric
i.metrics.NumTxs.Set(float64(len(block.Body().Transactions)))
}
func (i *Ibft) insertBlock(block *types.Block) error {
committedSeals := [][]byte{}
for _, commit := range i.state.committed {
// no need to check the format of seal here because writeCommittedSeals will check
committedSeals = append(committedSeals, hex.MustDecodeHex(commit.Seal))
}
header, err := writeCommittedSeals(block.Header, committedSeals)
if err != nil {
return err
}
// we need to recompute the hash since we have change extra-data
block.Header = header
block.Header.ComputeHash()
if err := i.blockchain.WriteBlocks([]*types.Block{block}); err != nil {
return err
}
i.logger.Info(
"block committed",
"sequence", i.state.view.Sequence,
"hash", block.Hash(),
"validators", len(i.state.validators),
"rounds", i.state.view.Round+1,
"committed", i.state.numCommitted(),
)
// increase the sequence number and reset the round if any
i.state.view = &proto.View{
Sequence: header.Number + 1,
Round: 0,
}
// broadcast the new block
i.syncer.Broadcast(block)
// after the block has been written we reset the txpool so that
// the old transactions are removed
i.txpool.ResetWithHeader(block.Header)
return nil
}
var (
errIncorrectBlockLocked = fmt.Errorf("block locked is incorrect")
errBlockVerificationFailed = fmt.Errorf("block verification failed")
errFailedToInsertBlock = fmt.Errorf("failed to insert block")
)
func (i *Ibft) handleStateErr(err error) {
i.state.err = err
i.setState(RoundChangeState)
}
func (i *Ibft) runRoundChangeState() {
sendRoundChange := func(round uint64) {
i.logger.Debug("local round change", "round", round)
// set the new round and update the round metric
i.state.view.Round = round
i.metrics.Rounds.Set(float64(round))
// clean the round
i.state.cleanRound(round)
// send the round change message
i.sendRoundChange()
}
sendNextRoundChange := func() {
sendRoundChange(i.state.view.Round + 1)
}
checkTimeout := func() {
// check if there is any peer that is really advanced and we might need to sync with it first
if i.syncer != nil {
bestPeer := i.syncer.BestPeer()
if bestPeer != nil {
lastProposal := i.blockchain.Header()
if bestPeer.Number() > lastProposal.Number {
i.logger.Debug("it has found a better peer to connect", "local", lastProposal.Number, "remote", bestPeer.Number())
// we need to catch up with the last sequence
i.setState(SyncState)
return
}
}
}
// otherwise, it seems that we are in sync
// and we should start a new round
sendNextRoundChange()
}
// if the round was triggered due to an error, we send our own
// next round change
if err := i.state.getErr(); err != nil {
i.logger.Debug("round change handle err", "err", err)
sendNextRoundChange()
} else {
// otherwise, it is due to a timeout in any stage
// First, we try to sync up with any max round already available
if maxRound, ok := i.state.maxRound(); ok {
i.logger.Debug("round change set max round", "round", maxRound)
sendRoundChange(maxRound)
} else {
// otherwise, do your best to sync up
checkTimeout()
}
}
// create a timer for the round change
timeout := i.randomTimeout()
for i.getState() == RoundChangeState {
msg, ok := i.getNextMessage(timeout)
if !ok {
// closing
return
}
if msg == nil {
i.logger.Debug("round change timeout")
checkTimeout()
//update the timeout duration
timeout = i.randomTimeout()
continue
}
// we only expect RoundChange messages right now
num := i.state.AddRoundMessage(msg)
if num == i.state.NumValid() {
// start a new round inmediatly
i.state.view.Round = msg.View.Round
i.setState(AcceptState)
} else if num == i.state.validators.MaxFaultyNodes()+1 {
// weak certificate, try to catch up if our round number is smaller
if i.state.view.Round < msg.View.Round {
// update timer
timeout = i.randomTimeout()
sendRoundChange(msg.View.Round)
}
}
}
}
// --- com wrappers ---
func (i *Ibft) sendRoundChange() {
i.gossip(proto.MessageReq_RoundChange)
}
func (i *Ibft) sendPreprepareMsg() {
i.gossip(proto.MessageReq_Preprepare)
}
func (i *Ibft) sendPrepareMsg() {
i.gossip(proto.MessageReq_Prepare)
}
func (i *Ibft) sendCommitMsg() {
i.gossip(proto.MessageReq_Commit)
}
func (i *Ibft) gossip(typ proto.MessageReq_Type) {
msg := &proto.MessageReq{
Type: typ,
}
// add View
msg.View = i.state.view.Copy()
// if we are sending a preprepare message we need to include the proposed block
if msg.Type == proto.MessageReq_Preprepare {
msg.Proposal = &any.Any{
Value: i.state.block.MarshalRLP(),
}
}
// if the message is commit, we need to add the committed seal
if msg.Type == proto.MessageReq_Commit {
seal, err := writeCommittedSeal(i.validatorKey, i.state.block.Header)
if err != nil {
i.logger.Error("failed to commit seal", "err", err)
return
}
msg.Seal = hex.EncodeToHex(seal)
}
if msg.Type != proto.MessageReq_Preprepare {
// send a copy to ourselves so that we can process this message as well
msg2 := msg.Copy()
msg2.From = i.validatorKeyAddr.String()
i.pushMessage(msg2)
}
if err := signMsg(i.validatorKey, msg); err != nil {
i.logger.Error("failed to sign message", "err", err)
return
}
if err := i.transport.Gossip(msg); err != nil {
i.logger.Error("failed to gossip", "err", err)
}
}
// getState returns the current IBFT state
func (i *Ibft) getState() IbftState {
return i.state.getState()
}
// isState checks if the node is in the passed in state
func (i *Ibft) isState(s IbftState) bool {
return i.state.getState() == s
}
// setState sets the IBFT state
func (i *Ibft) setState(s IbftState) {
i.logger.Debug("state change", "new", s)
i.state.setState(s)
}
// forceTimeout sets the forceTimeoutCh flag to true
func (i *Ibft) forceTimeout() {
i.forceTimeoutCh = true
}
// randomTimeout calculates the timeout duration depending on the current round
func (i *Ibft) randomTimeout() time.Duration {
timeout := time.Duration(10000) * time.Millisecond
round := i.state.view.Round
if round > 0 {
timeout += time.Duration(math.Pow(2, float64(round))) * time.Second
}
return timeout
}
// isSealing checks if the current node is sealing blocks
func (i *Ibft) isSealing() bool {
return i.sealing
}
// verifyHeaderImpl implements the actual header verification logic
func (i *Ibft) verifyHeaderImpl(snap *Snapshot, parent, header *types.Header) error {
// ensure the extra data is correctly formatted
if _, err := getIbftExtra(header); err != nil {
return err
}
// Because you must specify either AUTH or DROP vote, it is confusing how to have a block without any votes.
// This is achieved by specifying the miner field to zeroes,
// because then the value in the Nonce will not be taken into consideration.
if header.Nonce != nonceDropVote && header.Nonce != nonceAuthVote {
return fmt.Errorf("invalid nonce")
}
if header.MixHash != IstanbulDigest {
return fmt.Errorf("invalid mixhash")
}
if header.Sha3Uncles != types.EmptyUncleHash {
return fmt.Errorf("invalid sha3 uncles")
}
// difficulty has to match number
if header.Difficulty != header.Number {
return fmt.Errorf("wrong difficulty")
}
// verify the sealer
if err := verifySigner(snap, header); err != nil {
return err
}
return nil
}
// VerifyHeader wrapper for verifying headers
func (i *Ibft) VerifyHeader(parent, header *types.Header) error {
snap, err := i.getSnapshot(parent.Number)
if err != nil {
return err
}
// verify all the header fields + seal
if err := i.verifyHeaderImpl(snap, parent, header); err != nil {
return err
}
// verify the commited seals
if err := verifyCommitedFields(snap, header); err != nil {
return err