forked from TTCECO/gttc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_test.go
1147 lines (1099 loc) · 41.3 KB
/
snapshot_test.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
// Copyright 2018 The gttc Authors
// This file is part of the gttc library.
//
// The gttc 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 gttc 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 gttc library. If not, see <http://www.gnu.org/licenses/>.
// Package alien implements the delegated-proof-of-stake consensus engine.
package alien
import (
"bytes"
"crypto/ecdsa"
"math/big"
"testing"
"github.com/TTCECO/gttc/common"
"github.com/TTCECO/gttc/core"
"github.com/TTCECO/gttc/core/rawdb"
"github.com/TTCECO/gttc/core/types"
"github.com/TTCECO/gttc/crypto"
"github.com/TTCECO/gttc/ethdb"
"github.com/TTCECO/gttc/params"
)
type testerTransaction struct {
from string // name of from address
to string // name of to address
balance int // balance address in snap.voter
isVote bool // "ufo:1:event:vote"
isProposal bool // "ufo:1:event:proposal:..."
proposalType uint64 // proposalTypeCandidateAdd or proposalTypeCandidateRemove
isDeclare bool // "ufo:1:event:declare:..."
candidate string // name of candidate in proposal
txHash string // hash of tx
decision bool // decision of declare
}
type testerSingleHeader struct {
txs []testerTransaction
}
type testerSelfVoter struct {
voter string // name of self voter address in genesis block
balance int // balance
}
type testerVote struct {
voter string
candidate string
stake int
}
type testerSnapshot struct {
Signers []string
Votes map[string]*testerVote
Tally map[string]int
Voters map[string]int
}
// testerAccountPool is a pool to maintain currently active tester accounts,
// mapped from textual names used in the tests below to actual Ethereum private
// keys capable of signing transactions.
type testerAccountPool struct {
accounts map[string]*ecdsa.PrivateKey
}
func newTesterAccountPool() *testerAccountPool {
return &testerAccountPool{
accounts: make(map[string]*ecdsa.PrivateKey),
}
}
func (ap *testerAccountPool) sign(header *types.Header, signer string) {
// Ensure we have a persistent key for the signer
if ap.accounts[signer] == nil {
ap.accounts[signer], _ = crypto.GenerateKey()
}
// Sign the header and embed the signature in extra data
headerSigHash, _ := sigHash(header)
sig, _ := crypto.Sign(headerSigHash.Bytes(), ap.accounts[signer])
copy(header.Extra[len(header.Extra)-65:], sig)
}
func (ap *testerAccountPool) address(account string) common.Address {
// Ensure we have a persistent key for the account
if ap.accounts[account] == nil {
ap.accounts[account], _ = crypto.GenerateKey()
}
// Resolve and return the Ethereum address
return crypto.PubkeyToAddress(ap.accounts[account].PublicKey)
}
func (ap *testerAccountPool) name(address common.Address) string {
for name, v := range ap.accounts {
if crypto.PubkeyToAddress(v.PublicKey) == address {
return name
}
}
return ""
}
// testerChainReader implements consensus.ChainReader to access the genesis
// block. All other methods and requests will panic.
type testerChainReader struct {
db ethdb.Database
}
func (r *testerChainReader) Config() *params.ChainConfig { return params.AllAlienProtocolChanges }
func (r *testerChainReader) CurrentHeader() *types.Header { panic("not supported") }
func (r *testerChainReader) GetHeader(common.Hash, uint64) *types.Header { panic("not supported") }
func (r *testerChainReader) GetBlock(common.Hash, uint64) *types.Block { panic("not supported") }
func (r *testerChainReader) GetHeaderByHash(common.Hash) *types.Header { panic("not supported") }
func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
if number == 0 {
return rawdb.ReadHeader(r.db, rawdb.ReadCanonicalHash(r.db, 0), 0)
}
panic("not supported")
}
// Tests that voting is evaluated correctly for various simple and complex scenarios.
func TestVoting(t *testing.T) {
// Define the various voting scenarios to test
tests := []struct {
addrNames []string // accounts used in this case
candidateNeedPD bool // candidate from POA
period uint64 // default 3
epoch uint64 // default 30000
maxSignerCount uint64 // default 5 for test
minVoterBalance int // default 50
genesisTimestamp uint64 // default time.now() - period + 1
lcrs uint64 // loop count to recreate signers from top tally
selfVoters []testerSelfVoter //
txHeaders []testerSingleHeader //
result testerSnapshot // the result of current snapshot
vlCnt uint64
}{
{
/* Case 0:
* Just two self vote address A B in genesis
* No votes or transactions through blocks
*/
addrNames: []string{"A", "B"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200},
Voters: map[string]int{"A": 0, "B": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
},
},
},
{
/* Case 1:
* Two self vote address A B in genesis
* C vote D to be signer in block 3
* But current loop do not finish, so D is not signer,
* the vote info already in Tally, Voters and Votes
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(7),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200, "D": 200},
Voters: map[string]int{"A": 0, "B": 0, "C": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 200},
},
},
},
{
/* Case 2:
* Two self vote address in genesis
* C vote D to be signer in block 2
* But balance of C is lower than minVoterBalance,
* so this vote not processed, D is not signer
* the vote info is dropped .
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 20, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200},
Voters: map[string]int{"A": 0, "B": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
},
},
},
{
/* Case 3:
* Two self vote address A B in genesis
* C vote D to be signer in block 3
* balance of C is higher than minVoterBalance
* D is signer in next loop
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "D"},
Tally: map[string]int{"A": 100, "B": 200, "D": 200},
Voters: map[string]int{"A": 0, "B": 0, "C": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 200},
},
},
},
{
/* Case 4:
* Two self vote address A B in genesis
* C vote D to be signer in block 2
* C vote B to be signer in block 3
* balance of C is higher minVoterBalance
* the first vote from C is dropped
* the signers are still A and B
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{{from: "C", to: "B", balance: 180, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 380},
Voters: map[string]int{"A": 0, "B": 0, "C": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "B", 180},
},
},
},
{
/* Case 5:
* Two self vote address A B in genesis
* C vote D to be signer in block 2
* C transaction to E 20 in block 3
* In Voters, the vote block number of C is still 2, not 4
*/
addrNames: []string{"A", "B", "C", "D", "E"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 100, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "E", balance: 20, isVote: false}}}, // when C transaction to E, the balance of C is 20
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "D"},
Tally: map[string]int{"A": 100, "B": 200, "D": 20},
Voters: map[string]int{"A": 0, "B": 0, "C": 2},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 20},
},
},
},
{
/* Case 6:
* Two self vote address A B in genesis
* C vote D , J vote K, H vote I to be signer in block 2
* E vote F in block 3
* The signers in the next loop is A,B,D,F,I but not K
* K is not top 5(maxsigercount) in Tally
*/
addrNames: []string{"A", "B", "C", "D", "E", "F", "H", "I", "J", "K"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 110, isVote: true}, {from: "J", to: "K", balance: 80, isVote: true}, {from: "H", to: "I", balance: 160, isVote: true}}},
{[]testerTransaction{{from: "E", to: "F", balance: 130, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "D", "F", "I"},
Tally: map[string]int{"A": 100, "B": 200, "D": 110, "I": 160, "F": 130, "K": 80},
Voters: map[string]int{"A": 0, "B": 0, "C": 2, "H": 2, "J": 2, "E": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 110},
"J": {"J", "K", 80},
"H": {"H", "I", 160},
"E": {"E", "F", 130},
},
},
},
{
/* Case 7:
* one self vote address A in genesis
* C vote D , J vote K, H vote I to be signer in block 3
* E vote F in block 4
* B vote B in block 5
* The signers in the next loop is A, B, D,F,I,K
* current number - The block number of vote for A > epoch expired
*
*/
addrNames: []string{"A", "B", "C", "D", "E", "F", "H", "I", "J", "K"},
period: uint64(3),
epoch: uint64(8),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 110, isVote: true}, {from: "J", to: "K", balance: 80, isVote: true}, {from: "H", to: "I", balance: 160, isVote: true}}},
{[]testerTransaction{{from: "E", to: "F", balance: 130, isVote: true}}},
{[]testerTransaction{{from: "B", to: "B", balance: 200, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"B", "D", "F", "I", "K"},
Tally: map[string]int{"B": 200, "D": 110, "I": 160, "F": 130, "K": 80},
Voters: map[string]int{"B": 14, "C": 12, "H": 12, "J": 12, "E": 13},
Votes: map[string]*testerVote{
"B": {"B", "B", 200},
"C": {"C", "D", 110},
"J": {"J", "K", 80},
"H": {"H", "I", 160},
"E": {"E", "F", 130},
},
},
},
{
/* Case 8:
* Two self vote address A,B in genesis
* C vote D , D vote C to be signer in block 3
*/
addrNames: []string{"A", "B", "C", "D", "E"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 110, isVote: true}, {from: "D", to: "C", balance: 80, isVote: true}, {from: "C", to: "E", balance: 110, isVote: false}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"B", "A", "C", "D"},
Tally: map[string]int{"B": 200, "D": 110, "A": 100, "C": 80},
Voters: map[string]int{"B": 0, "C": 3, "D": 3, "A": 0},
Votes: map[string]*testerVote{
"B": {"B", "B", 200},
"A": {"A", "A", 100},
"C": {"C", "D", 110},
"D": {"D", "C", 80},
},
},
},
{
/* Case 9:
* Two self vote address A B in genesis
* C vote D to be signer in block 3
* lcrs is 2, so the signers will recalculate after 5 *2 block
* D is still not signer
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 2,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200, "D": 200},
Voters: map[string]int{"A": 0, "B": 0, "C": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 200},
},
},
},
{
/* Case 10:
* Two self vote address A B in genesis
* C vote D to be signer in block 3
* lcrs is 2, so the signers will recalculate after 5 *2 block
* D is signer
*/
addrNames: []string{"A", "B", "C", "D"},
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 2,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "D"},
Tally: map[string]int{"A": 100, "B": 200, "D": 200},
Voters: map[string]int{"A": 0, "B": 0, "C": 3},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 200},
},
},
},
{
/* Case 11:
* All self vote in genesis
* lcrs is 1, so the signers will recalculate after 5 block
* official 21 node test case
*/
addrNames: []string{"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10",
"A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20",
"A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "A29", "A30",
"A31", "A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A40"},
period: uint64(3),
epoch: uint64(300),
maxSignerCount: uint64(21),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A1", 5000}, {"A2", 5000}, {"A3", 5000}, {"A4", 5000}, {"A5", 5000},
{"A6", 5000}, {"A7", 5000}, {"A8", 5000}, {"A9", 5000}, {"A10", 5000},
{"A11", 4000}, {"A12", 4000}, {"A13", 4000}, {"A14", 4000}, {"A15", 4000},
{"A16", 4000}, {"A17", 4000}, {"A18", 4000}, {"A19", 4000}, {"A20", 4000},
{"A21", 3000}, {"A22", 3000}, {"A23", 3000}, {"A24", 3000}, {"A25", 3000},
{"A26", 3000}, {"A27", 3000}, {"A28", 3000}, {"A29", 3000}, {"A30", 3000},
{"A31", 2000}, {"A32", 2000}, {"A33", 2000}, {"A34", 2000}, {"A35", 2000},
{"A36", 2000}, {"A37", 2000}, {"A38", 2000}, {"A39", 2000}, {"A40", 2000}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}}, {[]testerTransaction{}},
{[]testerTransaction{}}, {[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{},
Tally: map[string]int{"A1": 5000, "A2": 5000, "A3": 5000, "A4": 5000, "A5": 5000, "A6": 5000, "A7": 5000, "A8": 5000, "A9": 5000, "A10": 5000,
"A11": 4000, "A12": 4000, "A13": 4000, "A14": 4000, "A15": 4000, "A16": 4000, "A17": 4000, "A18": 4000, "A19": 4000, "A20": 4000,
"A21": 3000, "A22": 3000, "A23": 3000, "A24": 3000, "A25": 3000, "A26": 3000, "A27": 3000, "A28": 3000, "A29": 3000, "A30": 3000,
"A31": 2000, "A32": 2000, "A33": 2000, "A34": 2000, "A35": 2000, "A36": 2000, "A37": 2000, "A38": 2000, "A39": 2000, "A40": 2000},
Voters: map[string]int{"A1": 0, "A2": 0, "A3": 0, "A4": 0, "A5": 0, "A6": 0, "A7": 0, "A8": 0, "A9": 0, "A10": 0,
"A11": 0, "A12": 0, "A13": 0, "A14": 0, "A15": 0, "A16": 0, "A17": 0, "A18": 0, "A19": 0, "A20": 0,
"A21": 0, "A22": 0, "A23": 0, "A24": 0, "A25": 0, "A26": 0, "A27": 0, "A28": 0, "A29": 0, "A30": 0,
"A31": 0, "A32": 0, "A33": 0, "A34": 0, "A35": 0, "A36": 0, "A37": 0, "A38": 0, "A39": 0, "A40": 0},
Votes: map[string]*testerVote{
"A1": {"A1", "A1", 5000},
"A2": {"A2", "A2", 5000},
"A3": {"A3", "A3", 5000},
"A4": {"A4", "A4", 5000},
"A5": {"A5", "A5", 5000},
"A6": {"A6", "A6", 5000},
"A7": {"A7", "A7", 5000},
"A8": {"A8", "A8", 5000},
"A9": {"A9", "A9", 5000},
"A10": {"A10", "A10", 5000},
"A11": {"A11", "A11", 4000},
"A12": {"A12", "A12", 4000},
"A13": {"A13", "A13", 4000},
"A14": {"A14", "A14", 4000},
"A15": {"A15", "A15", 4000},
"A16": {"A16", "A16", 4000},
"A17": {"A17", "A17", 4000},
"A18": {"A18", "A18", 4000},
"A19": {"A19", "A19", 4000},
"A20": {"A20", "A20", 4000},
"A21": {"A21", "A21", 3000},
"A22": {"A22", "A22", 3000},
"A23": {"A23", "A23", 3000},
"A24": {"A24", "A24", 3000},
"A25": {"A25", "A25", 3000},
"A26": {"A26", "A26", 3000},
"A27": {"A27", "A27", 3000},
"A28": {"A28", "A28", 3000},
"A29": {"A29", "A29", 3000},
"A30": {"A30", "A30", 3000},
"A31": {"A31", "A31", 2000},
"A32": {"A32", "A32", 2000},
"A33": {"A33", "A33", 2000},
"A34": {"A34", "A34", 2000},
"A35": {"A35", "A35", 2000},
"A36": {"A36", "A36", 2000},
"A37": {"A37", "A37", 2000},
"A38": {"A38", "A38", 2000},
"A39": {"A39", "A39", 2000},
"A40": {"A40", "A40", 2000},
},
},
},
{
/* Case 12:
* Candidate from Poa is enable
* Two self vote address A B in genesis
* C vote D to be signer in block 3, but D is not in candidates ,so this vote not valid
*/
addrNames: []string{"A", "B", "C", "D"},
candidateNeedPD: true,
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 200, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200},
Voters: map[string]int{"A": 0, "B": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
},
},
},
{
/* Case 13:
* Candidate from Poa is enable
* Two self vote address A B in genesis
* A proposal D to candidates, B declare agree to this proposal ,but not pass 2/3 * all stake, so fail
* C vote D to be signer in block 3, but D is not in candidates ,so this vote not valid
*/
addrNames: []string{"A", "B", "C", "D"},
candidateNeedPD: true,
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "A", to: "A", isProposal: true, candidate: "D", txHash: "a", proposalType: proposalTypeCandidateAdd}}},
{[]testerTransaction{{from: "B", to: "B", isDeclare: true, txHash: "a", decision: true}}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 250, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B"},
Tally: map[string]int{"A": 100, "B": 200},
Voters: map[string]int{"A": 0, "B": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
},
},
},
{
/* Case 14:
* Candidate from Poa is enable
* Two self vote address A B in genesis
* A proposal D to candidates, and A,B declare agree to this proposal, so D is in candidates
* C vote D to be signer in block 5
*/
addrNames: []string{"A", "B", "C", "D"},
candidateNeedPD: true,
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "A", to: "A", isProposal: true, candidate: "D", txHash: "a", proposalType: proposalTypeCandidateAdd}}},
{[]testerTransaction{{from: "A", to: "A", isDeclare: true, txHash: "a", decision: true}, {from: "B", to: "B", isDeclare: true, txHash: "a", decision: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 250, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "D"},
Tally: map[string]int{"A": 100, "B": 200, "D": 250},
Voters: map[string]int{"A": 0, "B": 0, "C": 11},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"C": {"C", "D", 250},
},
},
},
{
/* Case 15:
* Candidate from Poa is enable
* Two self vote address A B E F in genesis
* A proposal D to candidates, and A,B,F declare agree to this proposal,
* but the sum stake of A B F is less than 2/3 of all stake, so D is not in candidates
* C vote D to be signer in block 5
*/
addrNames: []string{"A", "B", "C", "D", "E", "F"},
candidateNeedPD: true,
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}, {"E", 2000}, {"F", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "A", to: "A", isProposal: true, candidate: "D", txHash: "a", proposalType: proposalTypeCandidateAdd}}},
{[]testerTransaction{{from: "A", to: "A", isDeclare: true, txHash: "a", decision: true}, {from: "B", to: "B", isDeclare: true, txHash: "a", decision: true}, {from: "F", to: "F", isDeclare: true, txHash: "a", decision: true}}},
{[]testerTransaction{}},
{[]testerTransaction{{from: "C", to: "D", balance: 250, isVote: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "B", "E", "F"},
Tally: map[string]int{"A": 100, "B": 200, "E": 2000, "F": 200},
Voters: map[string]int{"A": 0, "B": 0, "E": 0, "F": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"E": {"E", "E", 2000},
"F": {"F", "F", 200},
},
},
},
{
/* Case 16:
* Candidate from Poa is enable
* Two self vote address A B E F in genesis
* A proposal B remove from candidates, and A, E ,F declare agree to this proposal,
* the sum stake of A E F is more than 2/3 of all stake, so B is not in candidates
* Now do not change the vote automatically,
*/
addrNames: []string{"A", "B", "C", "D", "E", "F"},
candidateNeedPD: true,
period: uint64(3),
epoch: uint64(31),
maxSignerCount: uint64(5),
minVoterBalance: 50,
lcrs: 1,
genesisTimestamp: uint64(0),
selfVoters: []testerSelfVoter{{"A", 100}, {"B", 200}, {"E", 2000}, {"F", 200}},
txHeaders: []testerSingleHeader{
{[]testerTransaction{}},
{[]testerTransaction{{from: "A", to: "A", isProposal: true, candidate: "B", txHash: "a", proposalType: proposalTypeCandidateRemove}}},
{[]testerTransaction{{from: "A", to: "A", isDeclare: true, txHash: "a", decision: true}, {from: "E", to: "E", isDeclare: true, txHash: "a", decision: true}, {from: "F", to: "F", isDeclare: true, txHash: "a", decision: true}}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
{[]testerTransaction{}},
},
result: testerSnapshot{
Signers: []string{"A", "E", "F"},
Tally: map[string]int{"A": 100, "B": 200, "E": 2000, "F": 200},
Voters: map[string]int{"A": 0, "B": 0, "E": 0, "F": 0},
Votes: map[string]*testerVote{
"A": {"A", "A", 100},
"B": {"B", "B", 200},
"E": {"E", "E", 2000},
"F": {"F", "F", 200},
},
},
},
}
// Run through the scenarios and test them
for i, tt := range tests {
candidateNeedPD = tt.candidateNeedPD
if tt.vlCnt == 0 {
tt.vlCnt = 1
}
// Create the account pool and generate the initial set of all address in addrNames
accounts := newTesterAccountPool()
addrNames := make([]common.Address, len(tt.addrNames))
for j, signer := range tt.addrNames {
addrNames[j] = accounts.address(signer)
}
for j := 0; j < len(addrNames); j++ {
for k := j + 1; k < len(addrNames); k++ {
if bytes.Compare(addrNames[j][:], addrNames[k][:]) > 0 {
addrNames[j], addrNames[k] = addrNames[k], addrNames[j]
}
}
}
var snap *Snapshot
// Prepare data for the genesis block
var genesisVotes []*Vote // for create the new snapshot of genesis block
var selfVoteSigners []common.UnprefixedAddress // for header extra
alreadyVote := make(map[common.Address]struct{})
for _, voter := range tt.selfVoters {
if _, ok := alreadyVote[accounts.address(voter.voter)]; !ok {
genesisVotes = append(genesisVotes, &Vote{
Voter: accounts.address(voter.voter),
Candidate: accounts.address(voter.voter),
Stake: big.NewInt(int64(voter.balance)),
})
selfVoteSigners = append(selfVoteSigners, common.UnprefixedAddress(accounts.address(voter.voter)))
alreadyVote[accounts.address(voter.voter)] = struct{}{}
}
}
// extend length of extra, so address of CoinBase can keep signature .
genesis := &core.Genesis{
ExtraData: make([]byte, extraVanity+extraSeal),
}
// Create a pristine blockchain with the genesis injected
db := ethdb.NewMemDatabase()
genesis.Commit(db)
// Create new alien
alien := New(¶ms.AlienConfig{
Period: tt.period,
Epoch: tt.epoch,
MinVoterBalance: big.NewInt(int64(tt.minVoterBalance)),
MaxSignerCount: tt.maxSignerCount,
SelfVoteSigners: selfVoteSigners,
}, db)
// Assemble a chain of headers from the cast votes
headers := make([]*types.Header, len(tt.txHeaders))
for j, header := range tt.txHeaders {
var currentBlockVotes []Vote
var currentBlockProposals []Proposal
var currentBlockDeclares []Declare
var modifyPredecessorVotes []Vote
for _, trans := range header.txs {
if trans.isVote {
if trans.balance >= tt.minVoterBalance && (!candidateNeedPD || snap.isCandidate(accounts.address(trans.to))) {
// vote event
currentBlockVotes = append(currentBlockVotes, Vote{
Voter: accounts.address(trans.from),
Candidate: accounts.address(trans.to),
Stake: big.NewInt(int64(trans.balance)),
})
}
} else if trans.isProposal {
if snap.isCandidate(accounts.address(trans.from)) {
currentBlockProposals = append(currentBlockProposals, Proposal{
Hash: common.HexToHash(trans.txHash),
ReceivedNumber: big.NewInt(int64(j)),
CurrentDeposit: proposalDeposit,
ValidationLoopCnt: tt.vlCnt,
ProposalType: trans.proposalType,
Proposer: accounts.address(trans.from),
TargetAddress: accounts.address(trans.candidate),
MinerRewardPerThousand: minerRewardPerThousand,
SCHash: common.Hash{},
SCBlockCountPerPeriod: 1,
SCBlockRewardPerPeriod: 0,
Declares: []*Declare{},
MinVoterBalance: new(big.Int).Div(minVoterBalance, big.NewInt(1e+18)).Uint64(),
ProposalDeposit: new(big.Int).Div(proposalDeposit, big.NewInt(1e+18)).Uint64(),
SCRentFee: 0,
SCRentRate: 1,
SCRentLength: defaultSCRentLength,
})
}
} else if trans.isDeclare {
if snap.isCandidate(accounts.address(trans.from)) {
currentBlockDeclares = append(currentBlockDeclares, Declare{
ProposalHash: common.HexToHash(trans.txHash),
Declarer: accounts.address(trans.from),
Decision: trans.decision,
})
}
} else {
// modify balance
// modifyPredecessorVotes
// only consider the voter
modifyPredecessorVotes = append(modifyPredecessorVotes, Vote{
Voter: accounts.address(trans.from),
Stake: big.NewInt(int64(trans.balance)),
})
}
}
currentHeaderExtra := HeaderExtra{}
signer := common.Address{}
// (j==0) means (header.Number==1)
if j == 0 {
for k := 0; k < int(tt.maxSignerCount); k++ {
currentHeaderExtra.SignerQueue = append(currentHeaderExtra.SignerQueue, common.Address(selfVoteSigners[k%len(selfVoteSigners)]))
}
currentHeaderExtra.LoopStartTime = tt.genesisTimestamp // here should be parent genesisTimestamp
signer = common.Address(selfVoteSigners[0])
} else {
// decode parent header.extra
//rlp.DecodeBytes(headers[j-1].Extra[extraVanity:len(headers[j-1].Extra)-extraSeal], ¤tHeaderExtra)
decodeHeaderExtra(alien.config, headers[j-1].Number, headers[j-1].Extra[extraVanity:len(headers[j-1].Extra)-extraSeal], ¤tHeaderExtra)
signer = currentHeaderExtra.SignerQueue[uint64(j)%tt.maxSignerCount]
// means header.Number % tt.maxSignerCount == 0
if (j+1)%int(tt.maxSignerCount) == 0 {
snap, err := alien.snapshot(&testerChainReader{db: db}, headers[j-1].Number.Uint64(), headers[j-1].Hash(), headers, nil, uint64(tt.lcrs))
if err != nil {
t.Errorf("test %d: failed to create voting snapshot: %v", i, err)
continue
}
currentHeaderExtra.SignerQueue = []common.Address{}
newSignerQueue, err := snap.createSignerQueue()
if err != nil {
t.Errorf("test %d: failed to create signer queue: %v", i, err)
}
currentHeaderExtra.SignerQueue = newSignerQueue
currentHeaderExtra.LoopStartTime = currentHeaderExtra.LoopStartTime + tt.period*tt.maxSignerCount
} else {
}
}
currentHeaderExtra.CurrentBlockVotes = currentBlockVotes
currentHeaderExtra.ModifyPredecessorVotes = modifyPredecessorVotes
currentHeaderExtra.CurrentBlockProposals = currentBlockProposals