-
Notifications
You must be signed in to change notification settings - Fork 84
/
slot_leader_selection.go
841 lines (719 loc) · 24.9 KB
/
slot_leader_selection.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
package slotleader
import (
"bytes"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/wanchain/go-wanchain/common"
"github.com/wanchain/go-wanchain/common/hexutil"
"github.com/wanchain/go-wanchain/pos/epochLeader"
"github.com/wanchain/go-wanchain/consensus"
"github.com/wanchain/go-wanchain/accounts/keystore"
"github.com/wanchain/go-wanchain/core"
"github.com/wanchain/go-wanchain/core/state"
"github.com/wanchain/go-wanchain/core/types"
"github.com/wanchain/go-wanchain/core/vm"
"github.com/wanchain/go-wanchain/functrace"
"github.com/wanchain/go-wanchain/log"
"github.com/wanchain/go-wanchain/pos/posconfig"
"github.com/wanchain/go-wanchain/pos/posdb"
"github.com/wanchain/go-wanchain/pos/util/convert"
lru "github.com/hashicorp/golang-lru"
"github.com/wanchain/go-wanchain/rpc"
"github.com/wanchain/go-wanchain/crypto"
"github.com/wanchain/go-wanchain/pos/uleaderselection"
"github.com/wanchain/go-wanchain/pos/util"
"github.com/wanchain/go-wanchain/rlp"
)
const lengthPublicKeyBytes = 65
const (
StageTwoProofCount = 2
EpochLeaders = "epochLeaders"
SecurityMsg = "securityMsg"
CR = "cr"
SlotLeader = "slotLeader"
)
const (
slotLeaderSelectionInit = iota + 1 //1
//Ready to start slot leader selection stage1
slotLeaderSelectionStage1 = iota + 1 //2
//Slot leader selection stage1 finish
slotLeaderSelectionStage2 = iota + 1 //3
slotLeaderSelectionStage3 = iota + 1 //4
slotLeaderSelectionStageFinished = iota + 1 //5
)
var (
errorRetry = 3
)
type SLS struct {
workingEpochID uint64
workStage int
rc *rpc.Client
key *keystore.Key
stateDbTest *state.StateDB
epochLeadersArray []string // len(pki)=65 hex.EncodeToString
epochLeadersMap map[string][]uint64 // key: pki value: []uint64 the indexes of this pki. hex.EncodeToString
slotLeadersPtrArray [posconfig.SlotCount]*ecdsa.PublicKey
defaultSlotLeadersPtrArray [posconfig.SlotCount]*ecdsa.PublicKey
slotLeadersIndex [posconfig.SlotCount]uint64
epochLeadersPtrArray [posconfig.EpochLeaderCount]*ecdsa.PublicKey
// true: can be used to slot leader false: can not be used to slot leader
validEpochLeadersIndex [posconfig.EpochLeaderCount]bool
stageOneMi [posconfig.EpochLeaderCount]*ecdsa.PublicKey
stageTwoAlphaPKi [posconfig.EpochLeaderCount][posconfig.EpochLeaderCount]*ecdsa.PublicKey
stageTwoProof [posconfig.EpochLeaderCount][StageTwoProofCount]*big.Int //[0]: e; [1]:Z
slotCreateStatus map[uint64]bool
slotCreateStatusLockCh chan int
blockChain *core.BlockChain
epochLeadersPtrArrayGenesis [posconfig.EpochLeaderCount]*ecdsa.PublicKey
stageOneMiGenesis [posconfig.EpochLeaderCount]*ecdsa.PublicKey
stageTwoAlphaPKiGenesis [posconfig.EpochLeaderCount][posconfig.EpochLeaderCount]*ecdsa.PublicKey
stageTwoProofGenesis [posconfig.EpochLeaderCount][StageTwoProofCount]*big.Int //[0]: e; [1]:Z
randomGenesis *big.Int
smaGenesis [posconfig.EpochLeaderCount]*ecdsa.PublicKey
sendTransactionFn SendTxFn
}
var slotLeaderSelection *SLS
var APkiCache *lru.ARCCache
var RndCache *lru.ARCCache
type Pack struct {
Proof [][]byte
ProofMeg [][]byte
}
func GetSlotLeaderSelection() *SLS {
return slotLeaderSelection
}
func (s *SLS) GetLocalPublicKey() (*ecdsa.PublicKey, error) {
return s.getLocalPublicKey()
}
func (s *SLS) GetEpochLeadersPK(epochID uint64) []*ecdsa.PublicKey {
return s.getEpochLeadersPK(epochID)
}
func (s *SLS) GetSlotCreateStatusByEpochID(epochID uint64) bool {
s.slotCreateStatusLockCh <- 1
_, ok := s.slotCreateStatus[epochID]
<-s.slotCreateStatusLockCh
return ok
}
func (s *SLS) GetSlotLeader(epochID uint64, slotID uint64) (slotLeader *ecdsa.PublicKey, err error) {
if slotID >= posconfig.SlotCount {
return nil, vm.ErrSlotIDOutOfRange
}
if epochID <= posconfig.FirstEpochId+2 {
if s.getDefaultSlotLeader(slotID) != nil {
log.Info("GetSlotLeader:getDefaultSlotLeader",
"epochID", epochID,
"slotID", slotID,
"slot leader", hex.EncodeToString(crypto.FromECDSAPub(s.getDefaultSlotLeader(slotID))))
} else {
log.Warn("GetSlotLeader slot leader is nil")
}
return s.getDefaultSlotLeader(slotID), nil
}
_, isGenesis, _ := s.getSMAPieces(epochID)
log.Info("getSMAPieces", "isGenesis", isGenesis, "epochID", epochID)
if isGenesis {
if epochID < posconfig.Cfg().MarsEpochId || epochID <= posconfig.FirstEpochId+2 {
log.Info("GetSlotLeader use getDefaultSlotLeader", "isGenesis", isGenesis)
if s.getDefaultSlotLeader(slotID) != nil {
log.Info("GetSlotLeader:getDefaultSlotLeader",
"epochID", epochID,
"slotID", slotID,
"slot leader", hex.EncodeToString(crypto.FromECDSAPub(s.getDefaultSlotLeader(slotID))))
} else {
log.Warn("GetSlotLeader slot leader is nil")
}
return s.getDefaultSlotLeader(slotID), nil
}
log.Info("GetSlotLeader use getDefaultSlotLeader Mars")
epRecovery := GetRecoveryEpochID(epochID)
return s.GetSlotLeader(epRecovery, slotID)
}
return s.getSlotLeader(epochID, slotID)
}
func (s *SLS) getDefaultSlotLeader(slotID uint64) (slotLeader *ecdsa.PublicKey) {
return s.defaultSlotLeadersPtrArray[slotID]
}
func (s *SLS) getSlotLeader(epochID uint64, slotID uint64) (slotLeader *ecdsa.PublicKey, err error) {
if slotID >= posconfig.SlotCount {
return nil, vm.ErrSlotIDOutOfRange
}
epochIDGet := epochID
epochLeadersPtrArray, isDefault := s.GetPreEpochLeadersPK(epochIDGet)
if isDefault && epochID > posconfig.FirstEpochId+2 {
log.Info("generateSlotLeadsGroup use default epochLeader", "epochID", epochID)
epochIDGet = 0
}
if !s.IsLocalPkInEpochLeaders(epochLeadersPtrArray) {
log.Debug("Local node is not in pre epoch leaders at generateSlotLeadsGroup", "epochID", epochID)
return nil, uleaderselection.ErrNoInPreEPLS
}
piecesPtr, isGenesis, _ := s.getSMAPieces(epochIDGet)
if isGenesis {
log.Warn("Can not find pre epoch SMA or not in Pre epoch leaders, use the first epoch.", "curEpochID", epochID,
"preEpochID", epochID-1)
epochIDGet = 0
}
random, err := s.getRandom(nil, epochIDGet)
if err != nil {
return nil, vm.ErrInvalidRandom
}
if len(epochLeadersPtrArray) != posconfig.EpochLeaderCount {
log.Error("SLS", "Fail to get epoch leader", epochIDGet)
return nil, fmt.Errorf("fail to get epochLeader:%d", epochIDGet)
}
slotLeadersPtr, err := uleaderselection.GenerateSlotLeaderSeqOne(piecesPtr[:],
epochLeadersPtrArray[:], random.Bytes(), slotID, epochID)
if err != nil {
log.SyslogAlert("generateSlotLeadsGroup", "epochid", epochID, "error", err.Error())
return nil, err
}
if slotLeadersPtr != nil {
log.Info("getSlotLeader",
"epochID", epochID,
"slotID", slotID,
"slot leader", hex.EncodeToString(crypto.FromECDSAPub(slotLeadersPtr)))
} else {
log.Warn("getSlotLeader slot leader is nil")
}
return slotLeadersPtr, nil
}
func (s *SLS) GetSma(epochID uint64) (ret []*ecdsa.PublicKey, isGenesis bool, err error) {
return s.getSMAPieces(epochID)
}
func SlsInit() {
var err error
APkiCache, err = lru.NewARC(1000)
if err != nil || APkiCache == nil {
log.SyslogErr("APkiCache failed")
}
RndCache, err = lru.NewARC(10)
if err != nil || RndCache == nil {
log.SyslogErr("RndCache failed")
}
slotLeaderSelection = &SLS{}
slotLeaderSelection.epochLeadersMap = make(map[string][]uint64)
slotLeaderSelection.epochLeadersArray = make([]string, 0)
slotLeaderSelection.slotCreateStatus = make(map[uint64]bool)
slotLeaderSelection.slotCreateStatusLockCh = make(chan int, 1)
}
func (s *SLS) getSlotLeaderStage2TxIndexes(epochID uint64) (indexesSentTran []bool, err error) {
var ret [posconfig.EpochLeaderCount]bool
stateDb, err := s.getCurrentStateDb()
if err != nil {
return ret[:], err
}
slotLeaderPrecompileAddr := vm.GetSlotLeaderSCAddress()
keyHash := vm.GetSlotLeaderStage2IndexesKeyHash(convert.Uint64ToBytes(epochID))
log.Debug(fmt.Sprintf("getSlotLeaderStage2TxIndexes:try to get stateDB addr:%s, key:%s",
slotLeaderPrecompileAddr.Hex(), keyHash.Hex()))
data := stateDb.GetStateByteArray(slotLeaderPrecompileAddr, keyHash)
if data == nil {
return ret[:], vm.ErrNoTx2TransInDB
}
err = rlp.DecodeBytes(data, &ret)
if err != nil {
return ret[:], vm.ErrNoTx2TransInDB
}
return ret[:], nil
}
func (s *SLS) getAlpha(epochID uint64, selfIndex uint64) (*big.Int, error) {
if posconfig.SelfTestMode {
ret := big.NewInt(123)
return ret, nil
}
buf, err := posdb.GetDb().GetWithIndex(epochID, selfIndex, "alpha")
if err != nil {
return nil, err
}
var alpha = big.NewInt(0).SetBytes(buf)
return alpha, nil
}
func (s *SLS) getLocalPublicKey() (*ecdsa.PublicKey, error) {
if s.key == nil || s.key.PrivateKey == nil {
log.SyslogErr("SLS", "getLocalPublicKey", vm.ErrInvalidLocalPublicKey.Error())
return nil, vm.ErrInvalidLocalPublicKey
}
return &s.key.PrivateKey.PublicKey, nil
}
func (s *SLS) getLocalPrivateKey() (*ecdsa.PrivateKey, error) {
return s.key.PrivateKey, nil
}
func (s *SLS) getEpochLeaders(epochID uint64) [][]byte {
//test := false
if posconfig.SelfTestMode {
//test: generate test publicKey
epochLeaderAllBytes, err := posdb.GetDb().Get(epochID, EpochLeaders)
if err != nil {
return nil
}
piecesCount := len(epochLeaderAllBytes) / lengthPublicKeyBytes
ret := make([][]byte, 0)
var pubKeyByte []byte
for i := 0; i < piecesCount; i++ {
if i < piecesCount-1 {
pubKeyByte = epochLeaderAllBytes[i*lengthPublicKeyBytes : (i+1)*lengthPublicKeyBytes]
} else {
pubKeyByte = epochLeaderAllBytes[i*lengthPublicKeyBytes:]
}
ret = append(ret, pubKeyByte)
}
return ret
} else {
type epoch interface {
GetEpochLeaders(epochID uint64) [][]byte
}
selector := util.GetEpocherInst() //TODO:CHECK INIT
if selector == nil {
return nil
}
epochLeaders := selector.GetEpochLeaders(epochID)
if epochLeaders != nil {
log.Debug(fmt.Sprintf("getEpochLeaders called return len(epochLeaders):%d", len(epochLeaders)))
}
return epochLeaders
}
}
func (s *SLS) getEpochLeadersPK(epochID uint64) []*ecdsa.PublicKey {
bufs := s.getEpochLeaders(epochID)
pks := make([]*ecdsa.PublicKey, len(bufs))
for i := 0; i < len(bufs); i++ {
pks[i] = crypto.ToECDSAPub(bufs[i])
}
return pks
}
func (s *SLS) GetPreEpochLeadersPK(epochID uint64) (pks []*ecdsa.PublicKey, isDefault bool) {
if epochID <= posconfig.FirstEpochId+2 {
return s.GetEpochDefaultLeadersPK(0), true
}
pks = s.getEpochLeadersPK(epochID - 1)
if len(pks) == 0 {
log.Warn("Can not found pre epoch leaders return epoch default", "epochIDPre", epochID-1)
return s.GetEpochDefaultLeadersPK(0), true
}
return pks, false
}
func (s *SLS) GetEpochDefaultLeadersPK(epochID uint64) []*ecdsa.PublicKey {
pks := make([]*ecdsa.PublicKey, posconfig.EpochLeaderCount)
if posconfig.SelfTestMode {
var initPksStr []*ecdsa.PublicKey
for _, value := range posconfig.WhiteListOrig {
b := hexutil.MustDecode(value)
initPksStr = append(initPksStr, crypto.ToECDSAPub(b))
}
for i := 0; i < posconfig.EpochLeaderCount; i++ {
pks[i] = initPksStr[i%len(initPksStr)]
}
} else {
selector := epochLeader.GetEpocher()
initPksStr, err := selector.GetWhiteByEpochId(epochID)
if err != nil {
log.SyslogErr("GetEpochDefaultLeadersPK error", "err", err)
}
for i := 0; i < posconfig.EpochLeaderCount; i++ {
pkBuf := common.FromHex(initPksStr[i%len(initPksStr)])
pks[i] = crypto.ToECDSAPub(pkBuf)
}
}
return pks
}
func (s *SLS) IsLocalPkInEpochLeaders(pks []*ecdsa.PublicKey) bool {
localPk, err := s.getLocalPublicKey()
log.Info("IsLocalPkInEpochLeaders", "localPk", hex.EncodeToString(crypto.FromECDSAPub(localPk)))
if err != nil {
log.Error("IsLocalPkInEpochLeaders", "error", err)
return false
}
for _, value := range pks {
if util.PkEqual(localPk, value) {
return true
}
}
return false
}
func (s *SLS) isLocalPkInCurrentEpochLeaders() bool {
selfPublicKey, _ := s.getLocalPublicKey()
locakPk := crypto.FromECDSAPub(selfPublicKey)
epochID, _ := util.GetEpochSlotID()
pks := s.getEpochLeadersPK(epochID)
if len(pks) == 0 {
pks = s.getEpochLeadersPK(0)
}
for i := 0; i < len(pks); i++ {
if hex.EncodeToString(locakPk) == hex.EncodeToString(crypto.FromECDSAPub(pks[i])) {
return true
}
}
return false
}
func (s *SLS) clearData() {
s.epochLeadersArray = make([]string, 0)
s.epochLeadersMap = make(map[string][]uint64)
s.slotCreateStatusLockCh <- 1
s.slotCreateStatus = make(map[uint64]bool)
<-s.slotCreateStatusLockCh
for i := 0; i < posconfig.EpochLeaderCount; i++ {
s.epochLeadersPtrArray[i] = nil
s.validEpochLeadersIndex[i] = true
s.stageOneMi[i] = nil
for j := 0; j < posconfig.EpochLeaderCount; j++ {
s.stageTwoAlphaPKi[i][j] = nil
}
for k := 0; k < StageTwoProofCount; k++ {
s.stageTwoProof[i][k] = nil
}
}
for i := 0; i < posconfig.SlotCount; i++ {
s.slotLeadersPtrArray[i] = nil
}
for i := 0; i < posconfig.SlotCount; i++ {
s.slotLeadersIndex[i] = 0
}
}
func (s *SLS) dumpData() {
s.dumpPreEpochLeaders()
s.dumpCurrentEpochLeaders()
s.dumpLocalPublicKey()
s.dumpLocalPublicKeyIndex()
}
func (s *SLS) dumpPreEpochLeaders() {
log.Debug("\n")
currentEpochID := s.getWorkingEpochID()
log.Debug("dumpPreEpochLeaders", "currentEpochID", currentEpochID)
if currentEpochID == posconfig.FirstEpochId {
return
}
preEpochLeaders := s.getEpochLeaders(currentEpochID - 1)
for i := 0; i < len(preEpochLeaders); i++ {
log.Debug("dumpPreEpochLeaders", "index", i, "preEpochLeader", hex.EncodeToString(preEpochLeaders[i]))
}
log.Debug("\n")
}
func (s *SLS) dumpCurrentEpochLeaders() {
log.Debug("\n")
currentEpochID := s.getWorkingEpochID()
log.Debug("dumpCurrentEpochLeaders", "currentEpochID", currentEpochID)
if currentEpochID == posconfig.FirstEpochId {
return
}
for index, value := range s.epochLeadersPtrArray {
log.Debug("dumpCurrentEpochLeaders", "index", index, "curEpochLeader",
hex.EncodeToString(crypto.FromECDSAPub(value)))
}
}
func (s *SLS) dumpSlotLeaders() {
log.Debug("\n")
currentEpochID := s.getWorkingEpochID()
log.Debug("dumpSlotLeaders", "currentEpochID", currentEpochID)
if currentEpochID == posconfig.FirstEpochId {
return
}
for index, value := range s.slotLeadersPtrArray {
log.Debug("dumpSlotLeaders", "index", s.slotLeadersIndex[index], "curSlotLeader",
hex.EncodeToString(crypto.FromECDSAPub(value)))
}
}
func (s *SLS) dumpLocalPublicKey() {
log.Debug("\n")
localPublicKey, _ := s.getLocalPublicKey()
log.Debug("dumpLocalPublicKey", "current Local publickey", hex.EncodeToString(crypto.FromECDSAPub(localPublicKey)))
}
func (s *SLS) dumpLocalPublicKeyIndex() {
log.Debug("\n")
localPublicKey, _ := s.getLocalPublicKey()
localPublicKeyByte := crypto.FromECDSAPub(localPublicKey)
log.Debug("current Local publickey", "indexes in current epochLeaders",
s.epochLeadersMap[hex.EncodeToString(localPublicKeyByte)])
}
func (s *SLS) buildEpochLeaderGroup(epochID uint64) {
functrace.Enter()
// build Array and map
data := s.getEpochLeaders(epochID)
if data == nil {
log.SyslogErr("SLS", "buildEpochLeaderGroup", "no epoch leaders", "epochID", epochID)
// no epoch leaders, it leads that no one send SMA stage1 and stage2 transaction.
// comment panic, because let node live to used for others node synchronization.
//panic("No epoch leaders")
return
}
for index, value := range data {
s.epochLeadersArray = append(s.epochLeadersArray, hex.EncodeToString(value))
s.epochLeadersMap[hex.EncodeToString(value)] = append(s.epochLeadersMap[hex.EncodeToString(value)],
uint64(index))
s.epochLeadersPtrArray[index] = crypto.ToECDSAPub(value)
}
functrace.Exit()
}
func (s *SLS) isEpochLeaderMapReady() bool {
if s.epochLeadersMap == nil || len(s.epochLeadersMap) == 0 {
return false
}
return true
}
func (s *SLS) getRandom(block *types.Block, epochID uint64) (ret *big.Int, err error) {
rnd, ok := RndCache.Get(epochID)
if ok {
return rnd.(*big.Int), nil
}
// If db is nil, use current stateDB
var db *state.StateDB
if block == nil {
db, err = s.getCurrentStateDb()
if err != nil {
log.SyslogErr("SLS.getRandom getStateDb return error, use a default value", "epochID", epochID)
rb := posconfig.GetRandomGenesis()
RndCache.Add(epochID, rb)
return rb, nil
}
} else {
//db, err = s.blockChain.StateAt(s.blockChain.GetBlockByHash(block.ParentHash()).Root())
db, err = s.blockChain.StateAt(s.blockChain.GetHeaderByHash(block.ParentHash()).Root)
if err != nil {
log.SyslogErr("Update stateDb error in SLS.updateToLastStateDb", "error", err.Error())
rb := posconfig.GetRandomGenesis()
RndCache.Add(epochID, rb)
return rb, nil
}
}
rb := vm.GetR(db, epochID)
if rb == nil {
log.SyslogErr("vm.GetR return nil, use a default value", "epochID", epochID)
rb = posconfig.GetRandomGenesis()
}
RndCache.Add(epochID, rb)
return rb, nil
}
// getSMAPieces can get the SMA info generate in pre epoch.
// It had been +1 when save into db, so do not -1 in get.
func (s *SLS) getSMAPieces(epochID uint64) (ret []*ecdsa.PublicKey, isGenesis bool, err error) {
piecesPtr := make([]*ecdsa.PublicKey, 0)
if epochID <= posconfig.FirstEpochId+2 {
return s.smaGenesis[:], true, nil
} else {
// pieces: alpha[1]*G, alpha[2]*G, .....
pieces, err := posdb.GetDb().Get(epochID, SecurityMsg)
if err != nil {
if epochID > posconfig.FirstEpochId+2 {
log.Warn("getSMAPieces error use the first epoch SMA", "epochID", epochID, "SecurityMsg", SecurityMsg)
}
return s.smaGenesis[:], true, nil
}
piecesCount := len(pieces) / lengthPublicKeyBytes
var pubKeyByte []byte
for i := 0; i < piecesCount; i++ {
if i < piecesCount-1 {
pubKeyByte = pieces[i*lengthPublicKeyBytes : (i+1)*lengthPublicKeyBytes]
} else {
pubKeyByte = pieces[i*lengthPublicKeyBytes:]
}
piecesPtr = append(piecesPtr, crypto.ToECDSAPub(pubKeyByte))
}
return piecesPtr, false, nil
}
}
//func (s *SLS) generateSlotLeadsGroup(epochID uint64) error {
// epochIDGet := epochID
// epochLeadersPtrArray, isDefault := s.GetPreEpochLeadersPK(epochIDGet)
// if isDefault && epochID > posconfig.FirstEpochId+2 {
// log.Info("generateSlotLeadsGroup use default epochLeader", "epochID", epochID)
// epochIDGet = 0
// }
// if !s.IsLocalPkInEpochLeaders(epochLeadersPtrArray) {
// log.Debug("Local node is not in pre epoch leaders at generateSlotLeadsGroup", "epochID", epochID)
// return nil
// }
//
// // get pre sma
// piecesPtr, isGenesis, _ := s.getSMAPieces(epochIDGet)
// if isGenesis {
// log.Warn("Can not find pre epoch SMA or not in Pre epoch leaders, use the first epoch.", "curEpochID", epochID,
// "preEpochID", epochID-1)
// //epochIDGet = posconfig.FirstEpochId
// epochIDGet = 0
// }
// // get random
// random, err := s.getRandom(nil, epochIDGet)
// if err != nil {
// return vm.ErrInvalidRandom
// }
// log.Debug("generateSlotLeadsGroup", "Random got", hex.EncodeToString(random.Bytes()))
//
// // return slot leaders pointers.
// slotLeadersPtr := make([]*ecdsa.PublicKey, 0)
// if len(epochLeadersPtrArray) != posconfig.EpochLeaderCount {
// log.Error("SLS", "Fail to get epoch leader", epochIDGet)
// return fmt.Errorf("fail to get epochLeader:%d", epochIDGet)
// }
//
// slotLeadersPtr, _, slotLeadersIndex, err := uleaderselection.GenerateSlotLeaderSeqAndIndex(piecesPtr[:],
// epochLeadersPtrArray[:], random.Bytes(), posconfig.SlotCount, epochID)
// if err != nil {
// log.SyslogAlert("generateSlotLeadsGroup", "epochid", epochID, "error", err.Error())
// return err
// }
//
// // insert slot address to local DB
// for index, val := range slotLeadersPtr {
// _, err = posdb.GetDb().PutWithIndex(uint64(epochID), uint64(index), SlotLeader, crypto.FromECDSAPub(val))
// if err != nil {
// log.SyslogAlert("generateSlotLeadsGroup:PutWithIndex", "epochid", epochID, "error", err.Error())
// return err
// }
// }
//
// for index, val := range slotLeadersPtr {
// s.slotLeadersPtrArray[index] = val
// }
//
// for index, value := range slotLeadersIndex {
// s.slotLeadersIndex[index] = value
// }
//
// s.slotCreateStatusLockCh <- 1
// s.slotCreateStatus[epochID] = true
// <-s.slotCreateStatusLockCh
// log.SyslogInfo("generateSlotLeadsGroup success")
//
// s.dumpData()
// return nil
//}
// create alpha1*pki,alpha1*PKi,alphaN*PKi,...
// used to create security message.
func (s *SLS) buildSecurityPieces(epochID uint64) (pieces []*ecdsa.PublicKey, err error) {
selfPk, err := s.getLocalPublicKey()
if err != nil {
return nil, err
}
indexes, exist := s.epochLeadersMap[hex.EncodeToString(crypto.FromECDSAPub(selfPk))]
if exist == false {
log.Warn(fmt.Sprintf("%v not in epoch leaders", hex.EncodeToString(crypto.FromECDSAPub(selfPk))))
return nil, nil
}
selfPkReceivePiecesMap := make(map[uint64][]*ecdsa.PublicKey, 0)
for _, selfIndex := range indexes {
for i := 0; i < len(s.epochLeadersArray); i++ {
if (s.stageTwoAlphaPKi[i][selfIndex] != nil) && (s.validEpochLeadersIndex[i]) {
selfPkReceivePiecesMap[selfIndex] = append(selfPkReceivePiecesMap[selfIndex],
s.stageTwoAlphaPKi[i][selfIndex])
}
}
}
piece := make([]*ecdsa.PublicKey, 0)
piece = selfPkReceivePiecesMap[indexes[0]]
// the value in selfPk Received Pieces Map should be same,so we can return the first one.
return piece, nil
}
func (s *SLS) collectStagesData(epochID uint64) (err error) {
indexesSentTran, err := s.getSlotLeaderStage2TxIndexes(epochID)
log.Debug("collectStagesData", "indexesSentTran", indexesSentTran)
if err != nil {
log.SyslogErr("collectStagesData", "indexesSentTran", vm.ErrCollectTxData.Error())
return vm.ErrCollectTxData
}
for i := 0; i < posconfig.EpochLeaderCount; i++ {
if !indexesSentTran[i] {
s.validEpochLeadersIndex[i] = false
continue
}
// no need get current stateDB, because in getSlotLeaderStage2TxIndexes, have got the current db in stateDb.
statedb, _ := s.getCurrentStateDb()
alphaPki, proof, err := vm.GetStage2TxAlphaPki(statedb, epochID, uint64(i))
if err != nil {
log.SyslogErr("GetStage2TxAlphaPki", "error", err.Error(), "index", i)
s.validEpochLeadersIndex[i] = false
continue
}
if (len(alphaPki) != posconfig.EpochLeaderCount) || (len(proof) != StageTwoProofCount) {
log.SyslogErr("GetStage2TxAlphaPki", "error", "len(alphaPkis) or len(proofs) is wrong.", "index", i)
s.validEpochLeadersIndex[i] = false
} else {
for j := 0; j < posconfig.EpochLeaderCount; j++ {
s.stageTwoAlphaPKi[i][j] = alphaPki[j]
}
for j := 0; j < StageTwoProofCount; j++ {
s.stageTwoProof[i][j] = proof[j]
}
}
}
return nil
}
func (s *SLS) generateSecurityMsg(epochID uint64, PrivateKey *ecdsa.PrivateKey) error {
if !s.isLocalPkInCurrentEpochLeaders() {
log.Debug("generateSecurityMsg", "input public key",
hex.EncodeToString(crypto.FromECDSAPub(&PrivateKey.PublicKey)))
return vm.ErrPkNotInCurrentEpochLeadersGroup
}
// collect data
err := s.collectStagesData(epochID)
if err != nil {
return vm.ErrCollectTxData
}
// build security self pieces. alpha1*pki, alpha2*pk2, alpha3*pk3....
ArrayPiece, err := s.buildSecurityPieces(epochID)
if err != nil {
log.Warn("generateSecurityMsg:buildSecurityPieces", "error", err.Error())
return err
}
smasPtr := make([]*ecdsa.PublicKey, 0)
var smasBytes bytes.Buffer
smasPtr, err = uleaderselection.GenerateSMA(PrivateKey, ArrayPiece)
if err != nil {
log.Error("generateSecurityMsg:GenerateSMA", "error", err.Error())
return err
}
for _, value := range smasPtr {
smasBytes.Write(crypto.FromECDSAPub(value))
log.Debug(fmt.Sprintf("epochID+1 = %d set security message is %v\n", epochID+1,
hex.EncodeToString(crypto.FromECDSAPub(value))))
}
_, err = posdb.GetDb().Put(uint64(epochID+1), SecurityMsg, smasBytes.Bytes())
if err != nil {
log.SyslogCrit("generateSecurityMsg:Put", "epochid", epochID, "error", err.Error())
return err
}
return nil
}
// used for stage2 payload
// stage2 tx payload 1(alpha * Pk1, alpha * Pk2, ..., alpha * Pkn)
// stage2 tx payload 2 proof pai[i]
// []*ecdsa : payload1 []*big.Int payload2
func (s *SLS) buildArrayPiece(epochID uint64, selfIndex uint64) ([]*ecdsa.PublicKey, []*big.Int, error) {
// get alpha
alpha, err := s.getAlpha(epochID, selfIndex)
if err != nil {
return nil, nil, err
}
publicKeys := s.epochLeadersPtrArray[:]
for i := 0; i < len(publicKeys); i++ {
if publicKeys[i] == nil {
log.SyslogErr("epochLeader is not ready")
return nil, nil, errors.New("epochLeader is not ready")
}
}
_, ArrayPiece, proof, err := uleaderselection.GenerateArrayPiece(publicKeys, alpha)
return ArrayPiece, proof, err
}
func (s *SLS) buildStage2TxPayload(epochID uint64, selfIndex uint64) ([]byte, error) {
var selfPk *ecdsa.PublicKey
var err error
if posconfig.SelfTestMode {
selfPk = s.epochLeadersPtrArray[selfIndex]
} else {
selfPk, err = s.getLocalPublicKey()
if err != nil {
return nil, err
}
}
alphaPki, proof, err := s.buildArrayPiece(epochID, selfIndex)
if err != nil {
return nil, err
}
buf, err := vm.RlpPackStage2DataForTx(epochID, selfIndex, selfPk, alphaPki, proof, vm.GetSlotLeaderScAbiString())
return buf, err
}
// GetChainReader can get a simple reader interface of blockchain
func (s *SLS) GetChainReader() consensus.ChainReader {
return s.blockChain
}