-
Notifications
You must be signed in to change notification settings - Fork 126
/
cosi.go
876 lines (794 loc) · 25.4 KB
/
cosi.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
package kernel
import (
"crypto/rand"
"fmt"
"sync"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
)
const (
CosiActionSelfEmpty = iota
CosiActionSelfCommitment
CosiActionSelfResponse
CosiActionExternalAnnouncement
CosiActionExternalChallenge
CosiActionFinalization
)
type CosiAction struct {
Action int
PeerId crypto.Hash
SnapshotHash crypto.Hash
Snapshot *common.Snapshot
Commitment *crypto.Key
Signature *crypto.CosiSignature
Response *[32]byte
Transaction *common.VersionedTransaction
WantTx bool
}
type CosiAggregator struct {
Snapshot *common.Snapshot
Transaction *common.VersionedTransaction
WantTxs map[crypto.Hash]bool
Commitments map[int]*crypto.Key
Responses map[int]*[32]byte
committed map[crypto.Hash]bool
responsed map[crypto.Hash]bool
}
type CosiVerifier struct {
Snapshot *common.Snapshot
random *crypto.Key
}
func (node *Node) CosiLoop() error {
defer close(node.clc)
for {
select {
case <-node.done:
return nil
case m := <-node.cosiActionsChan:
err := node.cosiHandleAction(m)
if err != nil {
return err
}
}
}
}
func (node *Node) cosiHandleAction(m *CosiAction) error {
defer node.Graph.UpdateFinalCache(node.IdForNetwork)
switch m.Action {
case CosiActionSelfEmpty:
return node.cosiSendAnnouncement(m)
case CosiActionSelfCommitment:
return node.cosiHandleCommitment(m)
case CosiActionSelfResponse:
return node.cosiHandleResponse(m)
case CosiActionExternalAnnouncement:
return node.cosiHandleAnnouncement(m)
case CosiActionExternalChallenge:
return node.cosiHandleChallenge(m)
case CosiActionFinalization:
return node.handleFinalization(m)
}
return nil
}
func (node *Node) cosiSendAnnouncement(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiSendAnnouncement %v\n", m.Snapshot)
s := m.Snapshot
if s.NodeId != node.IdForNetwork || s.NodeId != m.PeerId {
panic("should never be here")
}
if s.Version != common.SnapshotVersion || s.Signature != nil || s.Timestamp != 0 {
return nil
}
if !node.CheckCatchUpWithPeers() && !node.checkInitialAcceptSnapshotWeak(m.Snapshot) {
logger.Verbosef("CosiLoop cosiHandleAction cosiSendAnnouncement CheckCatchUpWithPeers\n")
return nil
}
tx, finalized, err := node.checkCacheSnapshotTransaction(s)
if err != nil || finalized || tx == nil {
return nil
}
agg := &CosiAggregator{
Snapshot: s,
Transaction: tx,
WantTxs: make(map[crypto.Hash]bool),
Commitments: make(map[int]*crypto.Key),
Responses: make(map[int]*[32]byte),
committed: make(map[crypto.Hash]bool),
responsed: make(map[crypto.Hash]bool),
}
if node.checkInitialAcceptSnapshot(s, tx) {
s.Timestamp = uint64(clock.Now().UnixNano())
s.Hash = s.PayloadHash()
v := &CosiVerifier{Snapshot: s, random: crypto.CosiCommit(rand.Reader)}
R := v.random.Public()
node.CosiVerifiers[s.Hash] = v
agg.Commitments[len(node.SortedConsensusNodes)] = &R
node.CosiAggregators.Set(s.Hash, agg)
for peerId, _ := range node.ConsensusNodes {
err := node.Peer.SendSnapshotAnnouncementMessage(peerId, s, R)
if err != nil {
return err
}
}
return nil
}
if node.ConsensusIndex < 0 || node.Graph.FinalRound[s.NodeId] == nil {
return nil
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
final := node.Graph.FinalRound[s.NodeId].Copy()
if len(cache.Snapshots) == 0 && !node.CheckBroadcastedToPeers() {
return node.clearAndQueueSnapshotOrPanic(s)
}
for {
s.Timestamp = uint64(clock.Now().UnixNano())
if s.Timestamp > cache.Timestamp {
break
}
time.Sleep(100 * time.Millisecond)
}
if len(cache.Snapshots) == 0 {
external, err := node.persistStore.ReadRound(cache.References.External)
if err != nil {
return err
}
best := node.determinBestRound(s.NodeId, s.Timestamp)
threshold := external.Timestamp + config.SnapshotReferenceThreshold*config.SnapshotRoundGap*36
if best != nil && best.NodeId != final.NodeId && threshold < best.Start {
logger.Verbosef("CosiLoop cosiHandleAction cosiSendAnnouncement new best external %s:%d:%d => %s:%d:%d\n", external.NodeId, external.Number, external.Timestamp, best.NodeId, best.Number, best.Start)
link, err := node.persistStore.ReadLink(cache.NodeId, best.NodeId)
if err != nil {
return err
}
if best.Number <= link {
return node.clearAndQueueSnapshotOrPanic(s)
}
cache.References = &common.RoundLink{
Self: final.Hash,
External: best.Hash,
}
err = node.persistStore.UpdateEmptyHeadRound(cache.NodeId, cache.Number, cache.References)
if err != nil {
panic(err)
}
node.assignNewGraphRound(final, cache)
return node.clearAndQueueSnapshotOrPanic(s)
}
} else if start, _ := cache.Gap(); s.Timestamp >= start+config.SnapshotRoundGap {
best := node.determinBestRound(s.NodeId, s.Timestamp)
if best == nil {
logger.Verbosef("CosiLoop cosiHandleAction cosiSendAnnouncement no best available\n")
return node.clearAndQueueSnapshotOrPanic(s)
}
if best.NodeId == final.NodeId {
panic("should never be here")
}
final = cache.asFinal()
cache = &CacheRound{
NodeId: s.NodeId,
Number: final.Number + 1,
References: &common.RoundLink{
Self: final.Hash,
External: best.Hash,
},
}
err := node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
panic(err)
}
}
cache.Timestamp = s.Timestamp
if len(cache.Snapshots) > 0 && s.Timestamp > cache.Snapshots[0].Timestamp+uint64(config.SnapshotRoundGap*4/5) {
return node.clearAndQueueSnapshotOrPanic(s)
}
s.RoundNumber = cache.Number
s.References = cache.References
s.Hash = s.PayloadHash()
v := &CosiVerifier{Snapshot: s, random: crypto.CosiCommit(rand.Reader)}
R := v.random.Public()
node.CosiVerifiers[s.Hash] = v
agg.Commitments[node.ConsensusIndex] = &R
node.assignNewGraphRound(final, cache)
node.CosiAggregators.Set(s.Hash, agg)
for peerId, _ := range node.ConsensusNodes {
err := node.Peer.SendSnapshotAnnouncementMessage(peerId, m.Snapshot, R)
if err != nil {
return err
}
}
return nil
}
func (node *Node) cosiHandleAnnouncement(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleAnnouncement %s %v\n", m.PeerId, m.Snapshot)
if node.ConsensusIndex < 0 || !node.CheckCatchUpWithPeers() {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleAnnouncement CheckCatchUpWithPeers\n")
return nil
}
cn := node.getPeerConsensusNode(m.PeerId)
if cn == nil {
return nil
}
if cn.Timestamp+uint64(config.KernelNodeAcceptPeriodMinimum) >= m.Snapshot.Timestamp && !node.genesisNodesMap[cn.IdForNetwork(node.networkId)] {
return nil
}
s := m.Snapshot
if s.NodeId == node.IdForNetwork || s.NodeId != m.PeerId {
panic(fmt.Errorf("should never be here %s %s %s", node.IdForNetwork, s.NodeId, s.Signature))
}
if s.Version != common.SnapshotVersion || s.Signature != nil || s.Timestamp == 0 {
return nil
}
threshold := config.SnapshotRoundGap * config.SnapshotReferenceThreshold
if s.Timestamp > uint64(clock.Now().UnixNano())+threshold {
return nil
}
if s.Timestamp+threshold*2 < node.Graph.GraphTimestamp {
return nil
}
tx, finalized, err := node.checkCacheSnapshotTransaction(s)
if err != nil || finalized {
return nil
}
v := &CosiVerifier{Snapshot: s, random: crypto.CosiCommit(rand.Reader)}
if node.checkInitialAcceptSnapshotWeak(s) {
node.CosiVerifiers[s.Hash] = v
return node.Peer.SendSnapshotCommitmentMessage(s.NodeId, s.Hash, v.random.Public(), tx == nil)
}
if s.RoundNumber == 0 || node.Graph.FinalRound[s.NodeId] == nil {
return nil
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
final := node.Graph.FinalRound[s.NodeId].Copy()
if s.RoundNumber < cache.Number {
return nil
}
if s.RoundNumber > cache.Number+1 {
return node.queueSnapshotOrPanic(m.PeerId, s)
}
if s.Timestamp <= final.Start+config.SnapshotRoundGap {
return nil
}
if s.RoundNumber == cache.Number && !s.References.Equal(cache.References) {
if len(cache.Snapshots) > 0 {
return nil
}
if s.References.Self != cache.References.Self {
return nil
}
external, err := node.persistStore.ReadRound(s.References.External)
if err != nil || external == nil {
return err
}
link, err := node.persistStore.ReadLink(cache.NodeId, external.NodeId)
if err != nil {
return err
}
if external.Number < link {
return nil
}
cache.References = &common.RoundLink{
Self: s.References.Self,
External: s.References.External,
}
err = node.persistStore.UpdateEmptyHeadRound(cache.NodeId, cache.Number, cache.References)
if err != nil {
panic(err)
}
node.assignNewGraphRound(final, cache)
return node.queueSnapshotOrPanic(m.PeerId, s)
}
if s.RoundNumber == cache.Number+1 {
round, _, err := node.startNewRound(s, cache, false)
if err != nil {
logger.Verbosef("ERROR verifyExternalSnapshot %s %d %s %s\n", s.NodeId, s.RoundNumber, s.Transaction, err.Error())
return node.queueSnapshotOrPanic(m.PeerId, s)
} else if round == nil {
return nil
} else {
final = round
}
cache = &CacheRound{
NodeId: s.NodeId,
Number: s.RoundNumber,
Timestamp: s.Timestamp,
References: s.References,
}
err = node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
panic(err)
}
}
node.assignNewGraphRound(final, cache)
if err := cache.ValidateSnapshot(s, false); err != nil {
return nil
}
node.CosiVerifiers[s.Hash] = v
return node.Peer.SendSnapshotCommitmentMessage(s.NodeId, s.Hash, v.random.Public(), tx == nil)
}
func (node *Node) cosiHandleCommitment(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleCommitment %v\n", m)
cn := node.ConsensusNodes[m.PeerId]
if cn == nil {
return nil
}
ann := node.CosiAggregators.Get(m.SnapshotHash)
if ann == nil || ann.Snapshot.Hash != m.SnapshotHash {
return nil
}
if ann.committed[m.PeerId] {
return nil
}
if !node.CheckCatchUpWithPeers() && !node.checkInitialAcceptSnapshotWeak(ann.Snapshot) {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleCommitment CheckCatchUpWithPeers\n")
return nil
}
if cn.Timestamp+uint64(config.KernelNodeAcceptPeriodMinimum) >= ann.Snapshot.Timestamp && !node.genesisNodesMap[cn.IdForNetwork(node.networkId)] {
return nil
}
ann.committed[m.PeerId] = true
base := node.ConsensusThreshold(ann.Snapshot.Timestamp)
if len(ann.Commitments) >= base {
return nil
}
for i, id := range node.SortedConsensusNodes {
if id == m.PeerId {
ann.Commitments[i] = m.Commitment
ann.WantTxs[m.PeerId] = m.WantTx
break
}
}
if len(ann.Commitments) < base {
return nil
}
tx, finalized, err := node.checkCacheSnapshotTransaction(ann.Snapshot)
if err != nil || finalized || tx == nil {
return nil
}
cosi, err := crypto.CosiAggregateCommitment(ann.Commitments)
if err != nil {
return err
}
ann.Snapshot.Signature = cosi
v := node.CosiVerifiers[m.SnapshotHash]
priv := node.Signer.PrivateSpendKey
publics := node.ConsensusKeys(ann.Snapshot.Timestamp)
if node.checkInitialAcceptSnapshot(ann.Snapshot, tx) {
publics = append(publics, &node.ConsensusPledging.Signer.PublicSpendKey)
}
response, err := cosi.Response(&priv, v.random, publics, m.SnapshotHash[:])
if err != nil {
return err
}
if node.checkInitialAcceptSnapshot(ann.Snapshot, tx) {
ann.Responses[len(node.SortedConsensusNodes)] = &response
} else {
ann.Responses[node.ConsensusIndex] = &response
}
copy(cosi.Signature[32:], response[:])
for id, _ := range node.ConsensusNodes {
if wantTx, found := ann.WantTxs[id]; !found {
continue
} else if wantTx {
err = node.Peer.SendTransactionChallengeMessage(id, m.SnapshotHash, cosi, tx)
} else {
err = node.Peer.SendTransactionChallengeMessage(id, m.SnapshotHash, cosi, nil)
}
if err != nil {
return err
}
}
return nil
}
func (node *Node) cosiHandleChallenge(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleChallenge %v\n", m)
if node.ConsensusIndex < 0 || !node.CheckCatchUpWithPeers() {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleChallenge CheckCatchUpWithPeers\n")
return nil
}
if node.getPeerConsensusNode(m.PeerId) == nil {
return nil
}
v := node.CosiVerifiers[m.SnapshotHash]
if v == nil || v.Snapshot.Hash != m.SnapshotHash {
return nil
}
if m.Transaction != nil {
err := node.CachePutTransaction(m.PeerId, m.Transaction)
if err != nil {
return err
}
}
s := v.Snapshot
threshold := config.SnapshotRoundGap * config.SnapshotReferenceThreshold
if s.Timestamp > uint64(clock.Now().UnixNano())+threshold {
return nil
}
if s.Timestamp+threshold*2 < node.Graph.GraphTimestamp {
return nil
}
tx, finalized, err := node.checkCacheSnapshotTransaction(s)
if err != nil || finalized || tx == nil {
return nil
}
var sig crypto.Signature
copy(sig[:], s.Commitment[:])
copy(sig[32:], m.Signature.Signature[32:])
pub := node.getPeerConsensusNode(s.NodeId).Signer.PublicSpendKey
publics := node.ConsensusKeys(s.Timestamp)
if node.checkInitialAcceptSnapshot(s, tx) {
publics = append(publics, &node.ConsensusPledging.Signer.PublicSpendKey)
}
challenge, err := m.Signature.Challenge(publics, m.SnapshotHash[:])
if err != nil {
return nil
}
if !pub.VerifyWithChallenge(m.SnapshotHash[:], sig, challenge) {
return nil
}
priv := node.Signer.PrivateSpendKey
response, err := m.Signature.Response(&priv, v.random, publics, m.SnapshotHash[:])
if err != nil {
return err
}
return node.Peer.SendSnapshotResponseMessage(m.PeerId, m.SnapshotHash, response)
}
func (node *Node) cosiHandleResponse(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleResponse %v\n", m)
if node.ConsensusNodes[m.PeerId] == nil {
return nil
}
agg := node.CosiAggregators.Get(m.SnapshotHash)
if agg == nil || agg.Snapshot.Hash != m.SnapshotHash {
return nil
}
if agg.responsed[m.PeerId] {
return nil
}
if !node.CheckCatchUpWithPeers() && !node.checkInitialAcceptSnapshotWeak(agg.Snapshot) {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleResponse CheckCatchUpWithPeers\n")
return nil
}
agg.responsed[m.PeerId] = true
if len(agg.Responses) >= len(agg.Commitments) {
return nil
}
base := node.ConsensusThreshold(agg.Snapshot.Timestamp)
if len(agg.Commitments) < base {
return nil
}
s := agg.Snapshot
tx, finalized, err := node.checkCacheSnapshotTransaction(s)
if err != nil || finalized || tx == nil {
return nil
}
for i, id := range node.SortedConsensusNodes {
if id == m.PeerId {
agg.Responses[i] = m.Response
break
}
}
if len(agg.Responses) != len(agg.Commitments) {
return nil
}
publics := node.ConsensusKeys(s.Timestamp)
if node.checkInitialAcceptSnapshot(s, tx) {
publics = append(publics, &node.ConsensusPledging.Signer.PublicSpendKey)
}
s.Signature.AggregateResponse(publics, agg.Responses, m.SnapshotHash[:], false)
if !node.CacheVerifyCosi(m.SnapshotHash, s.Signature, publics, base) {
return nil
}
if node.checkInitialAcceptSnapshot(s, tx) {
err := node.finalizeNodeAcceptSnapshot(s)
if err != nil {
return err
}
for id, _ := range node.ConsensusNodes {
err := node.Peer.SendSnapshotFinalizationMessage(id, s)
if err != nil {
return err
}
}
return node.reloadConsensusNodesList(s, tx)
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
if s.RoundNumber > cache.Number {
panic(fmt.Sprintf("should never be here %d %d", cache.Number, s.RoundNumber))
}
if s.RoundNumber < cache.Number {
return node.clearAndQueueSnapshotOrPanic(s)
}
if !s.References.Equal(cache.References) {
return node.clearAndQueueSnapshotOrPanic(s)
}
if err := cache.ValidateSnapshot(s, false); err != nil {
return node.clearAndQueueSnapshotOrPanic(s)
}
topo := &common.SnapshotWithTopologicalOrder{
Snapshot: *s,
TopologicalOrder: node.TopoCounter.Next(),
}
err = node.persistStore.WriteSnapshot(topo)
if err != nil {
panic(err)
}
if err := cache.ValidateSnapshot(s, true); err != nil {
panic("should never be here")
}
node.Graph.CacheRound[s.NodeId] = cache
for id, _ := range node.ConsensusNodes {
if !agg.responsed[id] {
err := node.SendTransactionToPeer(id, agg.Snapshot.Transaction)
if err != nil {
return err
}
}
err := node.Peer.SendSnapshotFinalizationMessage(id, agg.Snapshot)
if err != nil {
return err
}
}
return node.reloadConsensusNodesList(s, tx)
}
func (node *Node) cosiHandleFinalization(m *CosiAction) error {
logger.Verbosef("CosiLoop cosiHandleAction cosiHandleFinalization %s %v\n", m.PeerId, m.Snapshot)
s, tx := m.Snapshot, m.Transaction
if node.checkInitialAcceptSnapshot(s, tx) {
err := node.finalizeNodeAcceptSnapshot(s)
if err != nil {
return err
}
return node.reloadConsensusNodesList(s, tx)
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
final := node.Graph.FinalRound[s.NodeId].Copy()
if s.RoundNumber < cache.Number {
logger.Verbosef("ERROR cosiHandleFinalization expired round %s %s %d %d\n", m.PeerId, s.Hash, s.RoundNumber, cache.Number)
return nil
}
if s.RoundNumber > cache.Number+1 {
return node.QueueAppendSnapshot(m.PeerId, s, true)
}
if s.RoundNumber == cache.Number && !s.References.Equal(cache.References) {
if len(cache.Snapshots) != 0 {
logger.Verbosef("ERROR cosiHandleFinalization malformated head round references not empty %s %v %d\n", m.PeerId, s, len(cache.Snapshots))
return nil
}
if s.References.Self != cache.References.Self {
logger.Verbosef("ERROR cosiHandleFinalization malformated head round references self diff %s %v %v\n", m.PeerId, s, cache.References)
return nil
}
external, err := node.persistStore.ReadRound(s.References.External)
if err != nil {
return err
}
if external == nil {
logger.Verbosef("ERROR cosiHandleFinalization head round references external not ready yet %s %v %v\n", m.PeerId, s, cache.References)
return node.QueueAppendSnapshot(m.PeerId, s, true)
}
err = node.persistStore.UpdateEmptyHeadRound(cache.NodeId, cache.Number, s.References)
if err != nil {
panic(err)
}
cache.References = s.References
node.assignNewGraphRound(final, cache)
return node.QueueAppendSnapshot(m.PeerId, s, true)
}
if s.RoundNumber == cache.Number+1 {
if round, _, err := node.startNewRound(s, cache, false); err != nil {
return node.QueueAppendSnapshot(m.PeerId, s, true)
} else if round == nil {
logger.Verbosef("ERROR cosiHandleFinalization startNewRound empty %s %v\n", m.PeerId, s)
return nil
} else {
final = round
}
cache = &CacheRound{
NodeId: s.NodeId,
Number: s.RoundNumber,
Timestamp: s.Timestamp,
References: s.References,
}
err := node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
panic(err)
}
}
node.assignNewGraphRound(final, cache)
if err := cache.ValidateSnapshot(s, false); err != nil {
logger.Verbosef("ERROR cosiHandleFinalization ValidateSnapshot %s %v %s\n", m.PeerId, s, err.Error())
return nil
}
topo := &common.SnapshotWithTopologicalOrder{
Snapshot: *s,
TopologicalOrder: node.TopoCounter.Next(),
}
err := node.persistStore.WriteSnapshot(topo)
if err != nil {
panic(err)
}
if err := cache.ValidateSnapshot(s, true); err != nil {
panic("should never be here")
}
node.assignNewGraphRound(final, cache)
return node.reloadConsensusNodesList(s, tx)
}
func (node *Node) handleFinalization(m *CosiAction) error {
logger.Debugf("CosiLoop cosiHandleAction handleFinalization %s %v\n", m.PeerId, m.Snapshot)
s := m.Snapshot
s.Hash = s.PayloadHash()
if !node.verifyFinalization(s) {
logger.Verbosef("ERROR handleFinalization verifyFinalization %s %v %d %t\n", m.PeerId, s, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil)
return nil
}
if cache := node.Graph.CacheRound[s.NodeId]; cache != nil {
if s.RoundNumber < cache.Number {
logger.Verbosef("ERROR handleFinalization expired round %s %s %d %d\n", m.PeerId, s.Hash, s.RoundNumber, cache.Number)
return nil
}
if s.RoundNumber > cache.Number+1 {
return node.QueueAppendSnapshot(m.PeerId, s, true)
}
}
dummy, err := node.tryToStartNewRound(s)
if err != nil {
logger.Verbosef("ERROR handleFinalization tryToStartNewRound %s %s %d %t %s\n", m.PeerId, s.Hash, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil, err.Error())
return node.QueueAppendSnapshot(m.PeerId, s, true)
} else if dummy {
logger.Verbosef("ERROR handleFinalization tryToStartNewRound DUMMY %s %s %d %t\n", m.PeerId, s.Hash, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil)
return node.QueueAppendSnapshot(m.PeerId, s, true)
}
tx, err := node.checkFinalSnapshotTransaction(s)
if err != nil {
logger.Verbosef("ERROR handleFinalization checkFinalSnapshotTransaction %s %s %d %t %s\n", m.PeerId, s.Hash, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil, err.Error())
return node.QueueAppendSnapshot(m.PeerId, s, true)
} else if tx == nil {
logger.Verbosef("ERROR handleFinalization checkFinalSnapshotTransaction %s %s %d %t %s\n", m.PeerId, s.Hash, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil, "tx empty")
return nil
}
if s.RoundNumber == 0 && tx.TransactionType() != common.TransactionTypeNodeAccept {
return fmt.Errorf("invalid initial transaction type %d", tx.TransactionType())
}
m.Transaction = tx
return node.cosiHandleFinalization(m)
}
func (node *Node) CosiQueueExternalAnnouncement(peerId crypto.Hash, s *common.Snapshot, commitment *crypto.Key) error {
if node.getPeerConsensusNode(peerId) == nil {
return nil
}
if s.Version != common.SnapshotVersion {
return nil
}
if s.NodeId == node.IdForNetwork || s.NodeId != peerId {
return nil
}
if s.Signature != nil || s.Timestamp == 0 || commitment == nil {
return nil
}
s.Hash = s.PayloadHash()
s.Commitment = commitment
return node.QueueAppendSnapshot(peerId, s, false)
}
func (node *Node) CosiAggregateSelfCommitments(peerId crypto.Hash, snap crypto.Hash, commitment *crypto.Key, wantTx bool) error {
if node.ConsensusNodes[peerId] == nil {
return nil
}
m := &CosiAction{
PeerId: peerId,
Action: CosiActionSelfCommitment,
SnapshotHash: snap,
Commitment: commitment,
WantTx: wantTx,
}
node.cosiActionsChan <- m
return nil
}
func (node *Node) CosiQueueExternalChallenge(peerId crypto.Hash, snap crypto.Hash, cosi *crypto.CosiSignature, ver *common.VersionedTransaction) error {
if node.getPeerConsensusNode(peerId) == nil {
return nil
}
m := &CosiAction{
PeerId: peerId,
Action: CosiActionExternalChallenge,
SnapshotHash: snap,
Signature: cosi,
Transaction: ver,
}
node.cosiActionsChan <- m
return nil
}
func (node *Node) CosiAggregateSelfResponses(peerId crypto.Hash, snap crypto.Hash, response *[32]byte) error {
if node.ConsensusNodes[peerId] == nil {
return nil
}
agg := node.CosiAggregators.Get(snap)
if agg == nil {
return nil
}
s := agg.Snapshot
tx, finalized, err := node.checkCacheSnapshotTransaction(s)
if err != nil || finalized || tx == nil {
return nil
}
index := -1
for i, id := range node.SortedConsensusNodes {
if id == peerId {
index = i
break
}
}
if index < 0 {
return nil
}
publics := node.ConsensusKeys(s.Timestamp)
if node.checkInitialAcceptSnapshotWeak(s) {
publics = append(publics, &node.ConsensusPledging.Signer.PublicSpendKey)
}
err = s.Signature.VerifyResponse(publics, index, response, snap[:])
if err != nil {
return nil
}
m := &CosiAction{
PeerId: peerId,
Action: CosiActionSelfResponse,
SnapshotHash: snap,
Response: response,
}
node.cosiActionsChan <- m
return nil
}
func (node *Node) VerifyAndQueueAppendSnapshotFinalization(peerId crypto.Hash, s *common.Snapshot) error {
s.Hash = s.PayloadHash()
logger.Debugf("VerifyAndQueueAppendSnapshotFinalization(%s, %s)\n", peerId, s.Hash)
if node.custom.Node.ConsensusOnly && node.getPeerConsensusNode(peerId) == nil {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) invalid consensus peer\n", peerId, s.Hash)
return nil
}
node.Peer.ConfirmSnapshotForPeer(peerId, s.Hash)
err := node.Peer.SendSnapshotConfirmMessage(peerId, s.Hash)
if err != nil {
return err
}
inNode, err := node.persistStore.CheckTransactionInNode(s.NodeId, s.Transaction)
if err != nil || inNode {
logger.Verbosef("VerifyAndQueueAppendSnapshotFinalization(%s, %s) already finalized %t %v\n", peerId, s.Hash, inNode, err)
return err
}
if s.Version == 0 {
return node.legacyAppendFinalization(peerId, s)
}
if !node.verifyFinalization(s) {
logger.Verbosef("ERROR VerifyAndQueueAppendSnapshotFinalization %s %v %d %t\n", peerId, s, node.ConsensusThreshold(s.Timestamp), node.ConsensusRemovedRecently(s.Timestamp) != nil)
return nil
}
return node.QueueAppendSnapshot(peerId, s, true)
}
func (node *Node) getPeerConsensusNode(peerId crypto.Hash) *common.Node {
if n := node.ConsensusPledging; n != nil && n.IdForNetwork(node.networkId) == peerId {
return n
}
return node.ConsensusNodes[peerId]
}
type aggregatorMap struct {
mutex *sync.RWMutex
m map[crypto.Hash]*CosiAggregator
}
func (s *aggregatorMap) Set(k crypto.Hash, p *CosiAggregator) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.m[k] = p
}
func (s *aggregatorMap) Get(k crypto.Hash) *CosiAggregator {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.m[k]
}
func (s *aggregatorMap) Delete(k crypto.Hash) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.m, k)
}