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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.
package core
import (
"errors"
"fmt"
"math"
"sort"
"sync"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core/types"
)
const (
infinity uint64 = math.MaxUint64
)
const (
// TotalOrderingModeError returns mode error.
TotalOrderingModeError uint32 = iota
// TotalOrderingModeNormal returns mode normal.
TotalOrderingModeNormal
// TotalOrderingModeEarly returns mode early.
TotalOrderingModeEarly
// TotalOrderingModeFlush returns mode flush.
TotalOrderingModeFlush
)
var (
// ErrInvalidDAG is reported when block subbmitted to totalOrdering
// didn't form a DAG.
ErrInvalidDAG = errors.New("invalid dag")
// ErrFutureRoundDelivered means some blocks from later rounds are
// delivered, this means program error.
ErrFutureRoundDelivered = errors.New("future round delivered")
// ErrBlockFromPastRound means we receive some block from past round.
ErrBlockFromPastRound = errors.New("block from past round")
// ErrTotalOrderingHangs means total ordering hangs somewhere.
ErrTotalOrderingHangs = errors.New("total ordering hangs")
// ErrForwardAck means a block acking some blocks from newer round.
ErrForwardAck = errors.New("forward ack")
// ErrUnexpected means general (I'm lazy) errors.
ErrUnexpected = errors.New("unexpected")
// ErrTotalOrderingPhiRatio means invalid phi ratio
ErrTotalOrderingPhiRatio = errors.New("invalid total ordering phi ratio")
)
// totalOrderingConfig is the configuration for total ordering.
type totalOrderingConfig struct {
roundBasedConfig
// k represents the k in 'k-level total ordering'.
// In short, only block height equals to (global minimum height + k)
// would be taken into consideration.
k uint64
// phi is a const to control how strong the leading preceding block
// should be.
phi uint64
numChains uint32
isFlushRequired bool
}
func (config *totalOrderingConfig) fromConfig(round uint64, cfg *types.Config) {
config.k = uint64(cfg.K)
config.numChains = cfg.NumChains
config.phi = uint64(float32(cfg.NumChains-1)*cfg.PhiRatio + 1)
config.setupRoundBasedFields(round, cfg)
}
func newTotalOrderingConfig(
prev *totalOrderingConfig, cur *types.Config) *totalOrderingConfig {
c := &totalOrderingConfig{}
c.fromConfig(prev.roundID+1, cur)
c.setRoundBeginTime(prev.roundEndTime)
prev.isFlushRequired = c.k != prev.k ||
c.phi != prev.phi ||
c.numChains != prev.numChains
return c
}
// totalOrderingWinRecord caches the comparison of candidates calculated by
// their height vector.
type totalOrderingWinRecord struct {
wins []int8
count uint
}
func (rec *totalOrderingWinRecord) reset() {
rec.count = 0
for idx := range rec.wins {
rec.wins[idx] = 0
}
}
func newTotalOrderingWinRecord(numChains uint32) *totalOrderingWinRecord {
return &totalOrderingWinRecord{
wins: make([]int8, numChains),
count: 0,
}
}
// grade implements the 'grade' potential function in algorithm.
func (rec *totalOrderingWinRecord) grade(
numChains uint32, phi uint64, globalAnsLength uint64) int {
if uint64(rec.count) >= phi {
return 1
} else if uint64(rec.count) < phi-uint64(numChains)+globalAnsLength {
return 0
} else {
return -1
}
}
// totalOrderingHeightRecord records:
// - the minimum height of block which acks this block.
// - the count of blocks acking this block.
type totalOrderingHeightRecord struct{ minHeight, count uint64 }
// totalOrderingCache caches objects for reuse and not being colloected by GC.
// Each cached target has "get-" and "put-" functions for getting and reusing
// of objects.
type totalOrderingCache struct {
ackedStatus [][]*totalOrderingHeightRecord
heightVectors [][]uint64
winRecords [][]*totalOrderingWinRecord
winRecordPool sync.Pool
ackedVectors []map[common.Hash]struct{}
numChains uint32
}
// newTotalOrderingObjectCache constructs an totalOrderingCache instance.
func newTotalOrderingObjectCache(numChains uint32) *totalOrderingCache {
return &totalOrderingCache{
winRecordPool: sync.Pool{
New: func() interface{} {
return newTotalOrderingWinRecord(numChains)
},
},
numChains: numChains,
}
}
// resize makes sure internal storage of totalOrdering instance can handle
// maximum possible numChains in future configs.
func (cache *totalOrderingCache) resize(numChains uint32) {
// Basically, everything in cache needs to be cleaned.
if cache.numChains >= numChains {
return
}
cache.ackedStatus = nil
cache.heightVectors = nil
cache.winRecords = nil
cache.ackedVectors = nil
cache.numChains = numChains
cache.winRecordPool = sync.Pool{
New: func() interface{} {
return newTotalOrderingWinRecord(numChains)
},
}
}
func (cache *totalOrderingCache) getAckedStatus() (
acked []*totalOrderingHeightRecord) {
if len(cache.ackedStatus) == 0 {
acked = make([]*totalOrderingHeightRecord, cache.numChains)
for idx := range acked {
acked[idx] = &totalOrderingHeightRecord{count: 0}
}
} else {
acked = cache.ackedStatus[len(cache.ackedStatus)-1]
cache.ackedStatus = cache.ackedStatus[:len(cache.ackedStatus)-1]
// Reset acked status.
for idx := range acked {
acked[idx].count = 0
}
}
return
}
func (cache *totalOrderingCache) putAckedStatus(
acked []*totalOrderingHeightRecord) {
// If the recycled objects supports lower numChains than we required,
// don't recycle it.
if uint32(len(acked)) != cache.numChains {
return
}
cache.ackedStatus = append(cache.ackedStatus, acked)
}
func (cache *totalOrderingCache) getWinRecord() (
win *totalOrderingWinRecord) {
win = cache.winRecordPool.Get().(*totalOrderingWinRecord)
win.reset()
return
}
func (cache *totalOrderingCache) putWinRecord(win *totalOrderingWinRecord) {
if win == nil {
return
}
// If the recycled objects supports lower numChains than we required,
// don't recycle it.
if uint32(len(win.wins)) != cache.numChains {
return
}
cache.winRecordPool.Put(win)
}
func (cache *totalOrderingCache) getHeightVector() (hv []uint64) {
if len(cache.heightVectors) == 0 {
hv = make([]uint64, cache.numChains)
} else {
hv = cache.heightVectors[len(cache.heightVectors)-1]
cache.heightVectors = cache.heightVectors[:len(cache.heightVectors)-1]
}
for idx := range hv {
hv[idx] = infinity
}
return
}
func (cache *totalOrderingCache) putHeightVector(hv []uint64) {
if uint32(len(hv)) != cache.numChains {
return
}
cache.heightVectors = append(cache.heightVectors, hv)
}
func (cache *totalOrderingCache) getWinRecords() (w []*totalOrderingWinRecord) {
if len(cache.winRecords) == 0 {
w = make([]*totalOrderingWinRecord, cache.numChains)
} else {
w = cache.winRecords[len(cache.winRecords)-1]
cache.winRecords = cache.winRecords[:len(cache.winRecords)-1]
for idx := range w {
w[idx] = nil
}
}
return
}
func (cache *totalOrderingCache) putWinRecords(w []*totalOrderingWinRecord) {
if uint32(len(w)) != cache.numChains {
return
}
cache.winRecords = append(cache.winRecords, w)
}
func (cache *totalOrderingCache) getAckedVector() (
acked map[common.Hash]struct{}) {
if len(cache.ackedVectors) == 0 {
acked = make(map[common.Hash]struct{})
} else {
acked, cache.ackedVectors =
cache.ackedVectors[len(cache.ackedVectors)-1],
cache.ackedVectors[:len(cache.ackedVectors)-1]
for k := range acked {
delete(acked, k)
}
}
return
}
func (cache *totalOrderingCache) putAckedVector(
acked map[common.Hash]struct{}) {
if acked != nil {
cache.ackedVectors = append(cache.ackedVectors, acked)
}
}
// totalOrderingCandidateInfo stores proceding status for a candidate including
// - acked status as height records, which keeps the number of blocks from other
// chains acking this candidate.
// - cached height vector, which valids height based on K-level used for
// comparison in 'grade' function.
// - cached result of grade function to other candidates.
//
// Height Record:
// When block A acks block B, all blocks proposed from the same proposer as
// block A with higher height also acks block B. Thus records below is needed
// - the minimum height of acking block from that proposer
// - count of acking blocks from that proposer
// to repsent the acking status for block A.
type totalOrderingCandidateInfo struct {
ackedStatus []*totalOrderingHeightRecord
cachedHeightVector []uint64
winRecords []*totalOrderingWinRecord
hash common.Hash
}
// newTotalOrderingCandidateInfo creates an totalOrderingCandidateInfo instance.
func newTotalOrderingCandidateInfo(
candidateHash common.Hash,
objCache *totalOrderingCache) *totalOrderingCandidateInfo {
return &totalOrderingCandidateInfo{
ackedStatus: objCache.getAckedStatus(),
winRecords: objCache.getWinRecords(),
hash: candidateHash,
}
}
// clean clears information related to another candidate, which should be called
// when that candidate is selected in deliver set.
func (v *totalOrderingCandidateInfo) clean(otherCandidateChainID uint32) {
v.winRecords[otherCandidateChainID] = nil
}
// recycle recycles objects for later usage, this eases GC's work.
func (v *totalOrderingCandidateInfo) recycle(objCache *totalOrderingCache) {
if v.winRecords != nil {
for _, win := range v.winRecords {
objCache.putWinRecord(win)
}
objCache.putWinRecords(v.winRecords)
}
if v.cachedHeightVector != nil {
objCache.putHeightVector(v.cachedHeightVector)
}
objCache.putAckedStatus(v.ackedStatus)
}
// addBlock would update totalOrderingCandidateInfo, it's caller's duty
// to make sure the input block acutally acking the target block.
func (v *totalOrderingCandidateInfo) addBlock(b *types.Block) error {
rec := v.ackedStatus[b.Position.ChainID]
if rec.count == 0 {
rec.minHeight = b.Position.Height
rec.count = 1
} else {
if b.Position.Height <= rec.minHeight {
return ErrInvalidDAG
}
rec.count++
}
return nil
}
// getAckingNodeSetLength returns the size of acking node set. Only heights
// larger than "global minimum height + k" are counted. For example, global
// minimum acking height is 1 and k is 1, only block heights which is larger or
// equal to 2 are added into acking node set.
func (v *totalOrderingCandidateInfo) getAckingNodeSetLength(
global *totalOrderingCandidateInfo,
k uint64,
numChains uint32) (count uint64) {
var rec *totalOrderingHeightRecord
for idx, gRec := range global.ackedStatus[:numChains] {
if gRec.count == 0 {
continue
}
rec = v.ackedStatus[idx]
if rec.count == 0 {
continue
}
if rec.minHeight+rec.count-1 >= gRec.minHeight+k {
count++
}
}
return
}
// updateAckingHeightVector would cached acking height vector.
//
// Only block height equals to (global minimum block height + k) would be
// taken into consideration.
func (v *totalOrderingCandidateInfo) updateAckingHeightVector(
global *totalOrderingCandidateInfo,
k uint64,
dirtyChainIDs []int,
objCache *totalOrderingCache) {
var (
idx int
gRec, rec *totalOrderingHeightRecord
)
// The reason for not merging two loops is that the performance impact of map
// iteration is large if the size is large. Iteration of dirty chains is
// faster the map.
// TODO(mission): merge the code in this if/else if the performance won't be
// downgraded when adding a function for the shared part.
if v.cachedHeightVector == nil {
// Generate height vector from scratch.
v.cachedHeightVector = objCache.getHeightVector()
for idx, gRec = range global.ackedStatus {
if gRec.count <= k {
continue
}
rec = v.ackedStatus[idx]
if rec.count == 0 {
v.cachedHeightVector[idx] = infinity
} else if rec.minHeight <= gRec.minHeight+k {
// This check is sufficient to make sure the block height:
//
// gRec.minHeight + k
//
// would be included in this totalOrderingCandidateInfo.
v.cachedHeightVector[idx] = gRec.minHeight + k
} else {
v.cachedHeightVector[idx] = infinity
}
}
} else {
// Return the cached one, only update dirty fields.
for _, idx = range dirtyChainIDs {
gRec = global.ackedStatus[idx]
if gRec.count == 0 || gRec.count <= k {
v.cachedHeightVector[idx] = infinity
continue
}
rec = v.ackedStatus[idx]
if rec.count == 0 {
v.cachedHeightVector[idx] = infinity
} else if rec.minHeight <= gRec.minHeight+k {
v.cachedHeightVector[idx] = gRec.minHeight + k
} else {
v.cachedHeightVector[idx] = infinity
}
}
}
return
}
// updateWinRecord setups win records from two candidates.
func (v *totalOrderingCandidateInfo) updateWinRecord(
otherChainID uint32,
other *totalOrderingCandidateInfo,
dirtyChainIDs []int,
objCache *totalOrderingCache,
numChains uint32) {
var (
idx int
height uint64
)
// The reason not to merge two loops is that the iteration of map is
// expensive when chain count is large, iterating of dirty chains is cheaper.
// TODO(mission): merge the code in this if/else if adding a function won't
// affect the performance.
win := v.winRecords[otherChainID]
if win == nil {
win = objCache.getWinRecord()
v.winRecords[otherChainID] = win
for idx, height = range v.cachedHeightVector[:numChains] {
if height == infinity {
continue
}
if other.cachedHeightVector[idx] == infinity {
win.wins[idx] = 1
win.count++
}
}
} else {
for _, idx = range dirtyChainIDs {
if v.cachedHeightVector[idx] == infinity {
if win.wins[idx] == 1 {
win.wins[idx] = 0
win.count--
}
continue
}
if other.cachedHeightVector[idx] == infinity {
if win.wins[idx] == 0 {
win.wins[idx] = 1
win.count++
}
} else {
if win.wins[idx] == 1 {
win.wins[idx] = 0
win.count--
}
}
}
}
}
// totalOrderingBreakpoint is a record of height discontinuity on a chain
type totalOrderingBreakpoint struct {
roundID uint64
// height of last block.
lastHeight uint64
}
// totalOrderingGroupVector keeps global status of current pending set.
type totalOrderingGlobalVector struct {
// blocks stores all blocks grouped by their proposers and sorted by height.
// TODO(mission): slice used here reallocates frequently.
blocks [][]*types.Block
// breakpoints stores rounds for chains that blocks' height on them are
// not consecutive, for example in chain i
// Round Height
// 0 0
// 0 1
// 1 2
// 1 3
// 1 4
// 2 - <- a config change of chain number occured
// 2 -
// 3 -
// 3 -
// 4 0 <- a breakpoint for round 3 is cached here
// 5 -
// 5 -
// 6 0 <- breakpoint again
// breakpoints[i][0] == &totalOrderingBreakpoint{roundID: 4, lastHeight: 4}
// breakpoints[i][1] == &totalOrderingBreakpoint{roundID: 6, lastHeight: 0}
breakpoints [][]*totalOrderingBreakpoint
// curRound stores the last round ID used for purging breakpoints.
curRound uint64
// tips records the last seen block for each chain.
tips []*types.Block
// Only ackedStatus in cachedCandidateInfo is used.
cachedCandidateInfo *totalOrderingCandidateInfo
}
func newTotalOrderingGlobalVector(
initRound uint64, numChains uint32) *totalOrderingGlobalVector {
return &totalOrderingGlobalVector{
blocks: make([][]*types.Block, numChains),
tips: make([]*types.Block, numChains),
breakpoints: make([][]*totalOrderingBreakpoint, numChains),
curRound: initRound,
}
}
func (global *totalOrderingGlobalVector) resize(numChains uint32) {
if len(global.blocks) >= int(numChains) {
return
}
// Resize blocks.
newBlocks := make([][]*types.Block, numChains)
copy(newBlocks, global.blocks)
global.blocks = newBlocks
// Resize breakpoints.
newBreakPoints := make([][]*totalOrderingBreakpoint, numChains)
copy(newBreakPoints, global.breakpoints)
global.breakpoints = newBreakPoints
// Resize tips.
newTips := make([]*types.Block, numChains)
copy(newTips, global.tips)
global.tips = newTips
}
func (global *totalOrderingGlobalVector) switchRound(roundID uint64) {
if global.curRound+1 != roundID {
panic(ErrUnexpected)
}
global.curRound = roundID
for chainID, bs := range global.breakpoints {
if len(bs) == 0 {
continue
}
if bs[0].roundID == roundID {
global.breakpoints[chainID] = bs[1:]
}
}
}
func (global *totalOrderingGlobalVector) prepareHeightRecord(
candidate *types.Block,
info *totalOrderingCandidateInfo,
acked map[common.Hash]struct{}) {
var (
chainID = candidate.Position.ChainID
breakpoints = global.breakpoints[chainID]
breakpoint *totalOrderingBreakpoint
rec *totalOrderingHeightRecord
)
// Setup height record for own chain.
rec = &totalOrderingHeightRecord{
minHeight: candidate.Position.Height,
}
if len(breakpoints) == 0 {
// If no breakpoint, count is the amount of blocks.
rec.count = uint64(len(global.blocks[chainID]))
} else {
// If there are breakpoints, only the first counts.
rec.count = breakpoints[0].lastHeight - candidate.Position.Height + 1
}
info.ackedStatus[chainID] = rec
if acked == nil {
return
}
for idx, blocks := range global.blocks {
if idx == int(chainID) {
continue
}
breakpoint = nil
if len(global.breakpoints[idx]) > 0 {
breakpoint = global.breakpoints[idx][0]
}
for i, b := range blocks {
if breakpoint != nil && b.Position.Round >= breakpoint.roundID {
break
}
if _, acked := acked[b.Hash]; !acked {
continue
}
// If this block acks the candidate, all newer blocks from the same chain
// also 'indirectly' acks the candidate.
rec = info.ackedStatus[idx]
rec.minHeight = b.Position.Height
if breakpoint == nil {
rec.count = uint64(len(blocks) - i)
} else {
rec.count = breakpoint.lastHeight - b.Position.Height + 1
}
break
}
}
}
func (global *totalOrderingGlobalVector) addBlock(
b *types.Block) (isOldest bool, pending bool, err error) {
// isOldest implies the block is the oldest in global vector
chainID := b.Position.ChainID
tip := global.tips[chainID]
isOldest = len(global.blocks[chainID]) == 0
if tip != nil {
// Perform light weight sanity check based on tip.
if tip.Position.Round > b.Position.Round {
err = ErrInvalidDAG
return
}
if DiffUint64(tip.Position.Round, b.Position.Round) > 1 {
if b.Position.Height != 0 {
err = ErrInvalidDAG
return
}
// Add breakpoint.
global.breakpoints[chainID] = append(
global.breakpoints[chainID],
&totalOrderingBreakpoint{
roundID: b.Position.Round,
lastHeight: tip.Position.Height,
})
} else {
if b.Position.Height != tip.Position.Height+1 {
err = ErrInvalidDAG
return
}
}
} else {
if b.Position.Round < global.curRound {
err = ErrBlockFromPastRound
return
}
if b.Position.Round > global.curRound {
// Add breakpoint.
bp := &totalOrderingBreakpoint{
roundID: b.Position.Round,
lastHeight: 0,
}
global.breakpoints[chainID] = append(global.breakpoints[chainID], bp)
}
}
bps := global.breakpoints[chainID]
pending = len(bps) > 0 && bps[0].roundID <= b.Position.Round
global.blocks[chainID] = append(global.blocks[chainID], b)
global.tips[chainID] = b
return
}
// updateCandidateInfo udpates cached candidate info.
func (global *totalOrderingGlobalVector) updateCandidateInfo(
dirtyChainIDs []int, objCache *totalOrderingCache) {
var (
idx int
blocks []*types.Block
block *types.Block
info *totalOrderingCandidateInfo
rec *totalOrderingHeightRecord
breakpoint *totalOrderingBreakpoint
)
if global.cachedCandidateInfo == nil {
info = newTotalOrderingCandidateInfo(common.Hash{}, objCache)
for idx, blocks = range global.blocks {
if len(blocks) == 0 {
continue
}
rec = info.ackedStatus[idx]
if len(global.breakpoints[idx]) > 0 {
breakpoint = global.breakpoints[idx][0]
block = blocks[0]
if block.Position.Round >= breakpoint.roundID {
continue
}
rec.minHeight = block.Position.Height
rec.count = breakpoint.lastHeight - block.Position.Height + 1
} else {
rec.minHeight = blocks[0].Position.Height
rec.count = uint64(len(blocks))
}
}
global.cachedCandidateInfo = info
} else {
info = global.cachedCandidateInfo
for _, idx = range dirtyChainIDs {
blocks = global.blocks[idx]
if len(blocks) == 0 {
info.ackedStatus[idx].count = 0
continue
}
rec = info.ackedStatus[idx]
if len(global.breakpoints[idx]) > 0 {
breakpoint = global.breakpoints[idx][0]
block = blocks[0]
if block.Position.Round >= breakpoint.roundID {
continue
}
rec.minHeight = block.Position.Height
rec.count = breakpoint.lastHeight - block.Position.Height + 1
} else {
rec.minHeight = blocks[0].Position.Height
rec.count = uint64(len(blocks))
}
}
}
return
}
// totalOrdering represent a process unit to handle total ordering for blocks.
type totalOrdering struct {
// pendings stores blocks awaiting to be ordered.
pendings map[common.Hash]*types.Block
// The round of config used when performing total ordering.
curRound uint64
// duringFlush is a flag to switch the flush mode and normal mode.
duringFlush bool
// flushReadyChains checks if the last block of that chain arrived. Once
// last blocks from all chains in current config are arrived, we can
// perform flush.
flushReadyChains map[uint32]struct{}
// flushed is a map of flushed blocks.
flushed map[uint32]struct{}
// globalVector group all pending blocks by proposers and
// sort them by block height. This structure is helpful when:
//
// - build global height vector
// - picking candidates next round
globalVector *totalOrderingGlobalVector
// candidates caches result of potential function during generating preceding
// set.
candidates []*totalOrderingCandidateInfo
// acked stores the 'block A acked by block B' by acked[A.Hash][B.Hash]
acked map[common.Hash]map[common.Hash]struct{}
// dirtyChainIDs stores chainIDs that is "dirty", i.e. needed updating all
// cached statuses (win record, acking status).
dirtyChainIDs []int
// objCache caches allocated objects, like map.
objCache *totalOrderingCache
// candidateChainMapping keeps a mapping from candidate's hash to
// their chain IDs.
candidateChainMapping map[uint32]common.Hash
// candidateChainIDs records chain ID of all candidates.
candidateChainIDs []uint32
// configs keeps configuration for each round in continuous way.
configs []*totalOrderingConfig
}
// newTotalOrdering constructs an totalOrdering instance.
func newTotalOrdering(
dMoment time.Time, round uint64, cfg *types.Config) *totalOrdering {
config := &totalOrderingConfig{}
config.fromConfig(round, cfg)
config.setRoundBeginTime(dMoment)
candidates := make([]*totalOrderingCandidateInfo, config.numChains)
to := &totalOrdering{
pendings: make(map[common.Hash]*types.Block),
dirtyChainIDs: make([]int, 0, config.numChains),
acked: make(map[common.Hash]map[common.Hash]struct{}),
objCache: newTotalOrderingObjectCache(config.numChains),
candidateChainMapping: make(map[uint32]common.Hash),
candidates: candidates,
candidateChainIDs: make([]uint32, 0, config.numChains),
curRound: config.roundID,
globalVector: newTotalOrderingGlobalVector(
config.roundID, config.numChains),
}
to.configs = []*totalOrderingConfig{config}
return to
}
// appendConfig add new configs for upcoming rounds. If you add a config for
// round R, next time you can only add the config for round R+1.
func (to *totalOrdering) appendConfig(
round uint64, config *types.Config) error {
if round != uint64(len(to.configs))+to.configs[0].roundID {
return ErrRoundNotIncreasing
}
if config.PhiRatio < 0.5 || config.PhiRatio > 1.0 {
return ErrTotalOrderingPhiRatio
}
to.configs = append(
to.configs,
newTotalOrderingConfig(to.configs[len(to.configs)-1], config))
// Resize internal structures.
to.globalVector.resize(config.NumChains)
to.objCache.resize(config.NumChains)
if int(config.NumChains) > len(to.candidates) {
newCandidates := make([]*totalOrderingCandidateInfo, config.NumChains)
copy(newCandidates, to.candidates)
to.candidates = newCandidates
}
return nil
}
func (to *totalOrdering) switchRound() {
to.curRound++
to.globalVector.switchRound(to.curRound)
}
// buildBlockRelation update all its indirect acks recursively.
func (to *totalOrdering) buildBlockRelation(b *types.Block) {
var (
curBlock, nextBlock *types.Block
ack common.Hash
acked map[common.Hash]struct{}
exists, alreadyPopulated bool
toCheck = []*types.Block{b}
)
for len(toCheck) != 0 {
curBlock, toCheck = toCheck[len(toCheck)-1], toCheck[:len(toCheck)-1]
if curBlock.Position.Round > b.Position.Round {
// It's illegal for a block to ack some blocks in future round.
panic(ErrForwardAck)
}
for _, ack = range curBlock.Acks {
if acked, exists = to.acked[ack]; !exists {
acked = to.objCache.getAckedVector()
to.acked[ack] = acked
}
// Check if the block is handled.
if _, alreadyPopulated = acked[b.Hash]; alreadyPopulated {
continue
}
acked[b.Hash] = struct{}{}
// See if we need to do this recursively.
if nextBlock, exists = to.pendings[ack]; exists {
toCheck = append(toCheck, nextBlock)
}
}
}
}
// clean a block from working set. This behaviour would prevent
// our memory usage growing infinity.
func (to *totalOrdering) clean(b *types.Block) {
var (
h = b.Hash
chainID = b.Position.ChainID
)
to.objCache.putAckedVector(to.acked[h])
delete(to.acked, h)
delete(to.pendings, h)
to.candidates[chainID].recycle(to.objCache)
to.candidates[chainID] = nil
delete(to.candidateChainMapping, chainID)
// Remove this candidate from candidate IDs.
to.candidateChainIDs =
removeFromSortedUint32Slice(to.candidateChainIDs, chainID)
// Clear records of this candidate from other candidates.
for _, idx := range to.candidateChainIDs {
to.candidates[idx].clean(chainID)
}
}
// updateVectors is a helper function to update all cached vectors.
func (to *totalOrdering) updateVectors(
b *types.Block) (isOldest bool, err error) {
var (
candidateHash common.Hash
chainID uint32
acked bool
pending bool
)
// Update global height vector
if isOldest, pending, err = to.globalVector.addBlock(b); err != nil {
return
}
if to.duringFlush {
// It makes no sense to calculate potential functions of total ordering
// when flushing would be happened.
return
}
if pending {
// The chain of this block contains breakpoints, which means their
// height are not continuous. This implementation of DEXON total
// ordering algorithm assumes the height of blocks in working set should
// be continuous.
//
// To workaround this issue, when block arrived after breakpoints,
// their information would not be contributed to current working set.
// This mechanism works because we switch rounds by flushing and
// reset the whole working set.
// This works because forward acking blocks are rejected.
return
}
// Update candidates' acking status.
for chainID, candidateHash = range to.candidateChainMapping {
if _, acked = to.acked[candidateHash][b.Hash]; !acked {
continue
}
if err = to.candidates[chainID].addBlock(b); err != nil {
return
}
}
return
}
// prepareCandidate builds totalOrderingCandidateInfo for a new candidate.
func (to *totalOrdering) prepareCandidate(b *types.Block) {
var (
info = newTotalOrderingCandidateInfo(b.Hash, to.objCache)
chainID = b.Position.ChainID
)
to.candidates[chainID] = info
to.candidateChainMapping[chainID] = b.Hash
// Add index to slot to allocated list, make sure the modified list is sorted.
to.candidateChainIDs = append(to.candidateChainIDs, chainID)
sort.Slice(to.candidateChainIDs, func(i, j int) bool {
return to.candidateChainIDs[i] < to.candidateChainIDs[j]
})
to.globalVector.prepareHeightRecord(b, info, to.acked[b.Hash])
return
}
// isCandidate checks if a block only contains acks to delivered blocks.
func (to *totalOrdering) isCandidate(b *types.Block) bool {
for _, ack := range b.Acks {
if _, exists := to.pendings[ack]; exists {
return false
}
}
return true
}
// output finishes the delivery of preceding set.
func (to *totalOrdering) output(
precedings map[common.Hash]struct{},
numChains uint32) (ret []*types.Block) {
for p := range precedings {
// Remove the first element from corresponding blockVector.
b := to.pendings[p]
chainID := b.Position.ChainID
// TODO(mission): frequent reallocation here.
to.globalVector.blocks[chainID] = to.globalVector.blocks[chainID][1:]
ret = append(ret, b)
// Remove block relations.
to.clean(b)
to.dirtyChainIDs = append(to.dirtyChainIDs, int(chainID))
}
sort.Sort(types.ByHash(ret))
// Find new candidates from global vector's tips.
// The complexity here is O(N^2logN).
// TODO(mission): only tips which acking some blocks in the devliered set
// should be checked. This improvement related to the latency introduced by K.
for chainID, blocks := range to.globalVector.blocks[:numChains] {
if len(blocks) == 0 {
continue
}
if _, picked := to.candidateChainMapping[uint32(chainID)]; picked {
continue
}
if !to.isCandidate(blocks[0]) {
continue
}
// Build totalOrderingCandidateInfo for new candidate.
to.prepareCandidate(blocks[0])
}
return
}
// generateDeliverSet generates preceding set and checks if the preceding set
// is deliverable by potential function.
func (to *totalOrdering) generateDeliverSet() (
delivered map[common.Hash]struct{}, mode uint32) {
var (
chainID, otherChainID uint32
info, otherInfo *totalOrderingCandidateInfo
precedings = make(map[uint32]struct{})
cfg = to.getCurrentConfig()
)
mode = TotalOrderingModeNormal
to.globalVector.updateCandidateInfo(to.dirtyChainIDs, to.objCache)
globalInfo := to.globalVector.cachedCandidateInfo
for _, chainID = range to.candidateChainIDs {
to.candidates[chainID].updateAckingHeightVector(
globalInfo, cfg.k, to.dirtyChainIDs, to.objCache)
}
// Update winning records for each candidate.
// TODO(mission): It's not reasonable to request one routine for each
// candidate, the context switch rate would be high.
var wg sync.WaitGroup
wg.Add(len(to.candidateChainIDs))
for _, chainID := range to.candidateChainIDs {
info = to.candidates[chainID]
go func(can uint32, canInfo *totalOrderingCandidateInfo) {
defer wg.Done()
for _, otherChainID := range to.candidateChainIDs {
if can == otherChainID {
continue
}
canInfo.updateWinRecord(
otherChainID,
to.candidates[otherChainID],
to.dirtyChainIDs,
to.objCache,
cfg.numChains)
}
}(chainID, info)
}
wg.Wait()
// Reset dirty chains.
to.dirtyChainIDs = to.dirtyChainIDs[:0]
// TODO(mission): ANS should be bounded by current numChains.
globalAnsLength := globalInfo.getAckingNodeSetLength(
globalInfo, cfg.k, cfg.numChains)
CheckNextCandidateLoop:
for _, chainID = range to.candidateChainIDs {
info = to.candidates[chainID]
for _, otherChainID = range to.candidateChainIDs {
if chainID == otherChainID {
continue
}
otherInfo = to.candidates[otherChainID]
// TODO(mission): grade should be bounded by current numChains.
if otherInfo.winRecords[chainID].grade(
cfg.numChains, cfg.phi, globalAnsLength) != 0 {
continue CheckNextCandidateLoop
}
}
precedings[chainID] = struct{}{}
}
if len(precedings) == 0 {
return
}
// internal is a helper function to verify internal stability.
internal := func() bool {
var (
isPreceding, beaten bool
p uint32
)
for _, chainID = range to.candidateChainIDs {
if _, isPreceding = precedings[chainID]; isPreceding {
continue
}
beaten = false
for p = range precedings {
// TODO(mission): grade should be bound by current numChains.
if beaten = to.candidates[p].winRecords[chainID].grade(
cfg.numChains, cfg.phi, globalAnsLength) == 1; beaten {
break
}
}
if !beaten {
return false
}
}
return true
}
// checkAHV is a helper function to verify external stability.
// It would make sure some preceding block is strong enough
// to lead the whole preceding set.
checkAHV := func() bool {
var (
height, count uint64
p uint32
)
for p = range precedings {
count = 0
info = to.candidates[p]
for _, height = range info.cachedHeightVector {
if height != infinity {
count++
if count > cfg.phi {
return true
}
}
}
}
return false
}
// checkANS is a helper function to verify external stability.
// It would make sure all preceding blocks are strong enough
// to be delivered.
checkANS := func() bool {
var chainAnsLength uint64
for p := range precedings {
// TODO(mission): ANS should be bound by current numChains.
chainAnsLength = to.candidates[p].getAckingNodeSetLength(
globalInfo, cfg.k, cfg.numChains)
if uint64(chainAnsLength) < uint64(cfg.numChains)-cfg.phi {
return false
}
}
return true
}
// If all chains propose enough blocks, we should force
// to deliver since the whole picture of the DAG is revealed.
if globalAnsLength != uint64(cfg.numChains) {
// Check internal stability first.
if !internal() {
return
}
// The whole picture is not ready, we need to check if
// exteranl stability is met, and we can deliver earlier.
if checkAHV() && checkANS() {
mode = TotalOrderingModeEarly
} else {
return
}
}
delivered = make(map[common.Hash]struct{})
for p := range precedings {
delivered[to.candidates[p].hash] = struct{}{}
}
return
}
// flushBlocks flushes blocks.
func (to *totalOrdering) flushBlocks() (
flushed []*types.Block, mode uint32, err error) {
mode = TotalOrderingModeFlush
cfg := to.getCurrentConfig()
// Flush blocks until last blocks from all chains appeared.
if len(to.flushReadyChains) < int(cfg.numChains) {
return
}
if len(to.flushReadyChains) > int(cfg.numChains) {
// This case should never be occured.
err = ErrFutureRoundDelivered
return
}
// Dump all blocks in this round.
for len(to.flushed) != int(cfg.numChains) {
// Dump all candidates without checking potential function.
flushedHashes := make(map[common.Hash]struct{})
for _, chainID := range to.candidateChainIDs {
candidateBlock := to.pendings[to.candidates[chainID].hash]
if candidateBlock.Position.Round > to.curRound {
continue
}
flushedHashes[candidateBlock.Hash] = struct{}{}
}
if len(flushedHashes) == 0 {
err = ErrTotalOrderingHangs
return
}
flushedBlocks := to.output(flushedHashes, cfg.numChains)
for _, b := range flushedBlocks {
if cfg.isLastBlock(b) {
to.flushed[b.Position.ChainID] = struct{}{}
}
}
flushed = append(flushed, flushedBlocks...)
}
// Switch back to non-flushing mode.
to.duringFlush = false
to.flushed = make(map[uint32]struct{})
to.flushReadyChains = make(map[uint32]struct{})
// Clean all cached intermediate stats.
for idx := range to.candidates {
if to.candidates[idx] == nil {
continue
}
to.candidates[idx].recycle(to.objCache)
to.candidates[idx] = nil
}
to.dirtyChainIDs = nil
to.candidateChainMapping = make(map[uint32]common.Hash)
to.candidateChainIDs = nil
to.globalVector.cachedCandidateInfo = nil
to.switchRound()
// Force picking new candidates.
numChains := to.getCurrentConfig().numChains
to.output(map[common.Hash]struct{}{}, numChains)
return
}
// deliverBlocks delivers blocks by DEXON total ordering algorithm.
func (to *totalOrdering) deliverBlocks() (
delivered []*types.Block, mode uint32, err error) {
hashes, mode := to.generateDeliverSet()
cfg := to.getCurrentConfig()
// Output precedings.
delivered = to.output(hashes, cfg.numChains)
// Check if any block in delivered set is the last block in this round, if
// there is, perform flush or round-switch.
for _, b := range delivered {
if b.Position.Round > to.curRound {
err = ErrFutureRoundDelivered
return
}
if !cfg.isLastBlock(b) {
continue
}
// Code reaches here if a last block is processed. This triggers
// "duringFlush" mode if config changes.
if cfg.isFlushRequired {
// Switch to flush mode.
to.duringFlush = true
to.flushReadyChains = make(map[uint32]struct{})
to.flushed = make(map[uint32]struct{})
} else {
// Switch round directly.
to.switchRound()
}
break
}
if to.duringFlush {
// Collect last blocks until all last blocks appears and function
// flushBlocks will be called.
for _, b := range delivered {
if cfg.isLastBlock(b) {
to.flushed[b.Position.ChainID] = struct{}{}
}
}
// Some last blocks for the round to be flushed might not be delivered
// yet.
for _, tip := range to.globalVector.tips[:cfg.numChains] {
if tip.Position.Round > to.curRound || cfg.isLastBlock(tip) {
to.flushReadyChains[tip.Position.ChainID] = struct{}{}
}
}
}
return
}
func (to *totalOrdering) getCurrentConfig() *totalOrderingConfig {
cfgIdx := to.curRound - to.configs[0].roundID
if cfgIdx >= uint64(len(to.configs)) {
panic(fmt.Errorf("total ordering config is not ready: %v, %v, %v",
to.curRound, to.configs[0].roundID, len(to.configs)))
}
return to.configs[cfgIdx]
}
// addBlock adds a block to the working set of total ordering module.
func (to *totalOrdering) addBlock(b *types.Block) error {
// NOTE: Block b is assumed to be in topologically sorted, i.e., all its
// acking blocks are during or after total ordering stage.
cfg := to.getCurrentConfig()
to.pendings[b.Hash] = b
to.buildBlockRelation(b)
isOldest, err := to.updateVectors(b)
if err != nil {
return err
}
// Mark the proposer of incoming block as dirty.
if b.Position.ChainID < cfg.numChains {
to.dirtyChainIDs = append(to.dirtyChainIDs, int(b.Position.ChainID))
_, exists := to.candidateChainMapping[b.Position.ChainID]
if isOldest && !exists && to.isCandidate(b) {
// isOldest means b is the oldest block in global vector, and isCandidate
// is still needed here due to round change. For example:
// o o o <- genesis block for round change, isCandidate returns true
// | | but isOldest is false
// o o
// | |
// o o o <- isOldest is true but isCandidate returns false
// | | /
// o o
to.prepareCandidate(b)
}
}
if to.duringFlush && cfg.isLastBlock(b) {
to.flushReadyChains[b.Position.ChainID] = struct{}{}
}
return nil
}
// extractBlocks check if there is any deliverable set.
func (to *totalOrdering) extractBlocks() ([]*types.Block, uint32, error) {
if to.duringFlush {
return to.flushBlocks()
}
return to.deliverBlocks()
}
|