@@ -103,16 +103,16 @@ func DefaultTrieOption() FactoryOption {
103
103
return func (sf * factory , cfg * config.Config ) error {
104
104
dbPath := cfg .Chain .TrieDBPath
105
105
if len (dbPath ) == 0 {
106
- return errors .New ("Invalid empty trie DB path" )
106
+ return errors .New ("Invalid empty trie db path" )
107
107
}
108
108
trieDB := db .NewBoltDB (dbPath , nil )
109
109
if err := trieDB .Start (context .Background ()); err != nil {
110
- return errors .Wrap (err , "failed to start trie DB " )
110
+ return errors .Wrap (err , "failed to start trie db " )
111
111
}
112
112
// create account trie
113
113
accountTrieRoot , err := sf .getRoot (trieDB , trie .AccountKVNameSpace , AccountTrieRootKey )
114
114
if err != nil {
115
- return errors .Wrap (err , "failed to get accountTrie's root hash from DB " )
115
+ return errors .Wrap (err , "failed to get accountTrie's root hash from underlying db " )
116
116
}
117
117
tr , err := trie .NewTrie (trieDB , trie .AccountKVNameSpace , accountTrieRoot )
118
118
if err != nil {
@@ -129,12 +129,12 @@ func InMemTrieOption() FactoryOption {
129
129
return func (sf * factory , cfg * config.Config ) error {
130
130
trieDB := db .NewMemKVStore ()
131
131
if err := trieDB .Start (context .Background ()); err != nil {
132
- return errors .Wrap (err , "failed to start trie DB " )
132
+ return errors .Wrap (err , "failed to start trie db " )
133
133
}
134
134
// create account trie
135
135
accountTrieRoot , err := sf .getRoot (trieDB , trie .AccountKVNameSpace , AccountTrieRootKey )
136
136
if err != nil {
137
- return errors .Wrap (err , "failed to get accountTrie's root hash from DB " )
137
+ return errors .Wrap (err , "failed to get accountTrie's root hash from underlying db " )
138
138
}
139
139
tr , err := trie .NewTrie (trieDB , trie .AccountKVNameSpace , accountTrieRoot )
140
140
if err != nil {
@@ -256,7 +256,7 @@ func (sf *factory) RootHash() hash.Hash32B {
256
256
func (sf * factory ) Height () (uint64 , error ) {
257
257
height , err := sf .accountTrie .TrieDB ().Get (trie .AccountKVNameSpace , []byte (CurrentHeightKey ))
258
258
if err != nil {
259
- return 0 , errors .Wrap (err , "failed to get factory's height from DB " )
259
+ return 0 , errors .Wrap (err , "failed to get factory's height from underlying db " )
260
260
}
261
261
return byteutil .BytesToUint64 (height ), nil
262
262
}
@@ -337,15 +337,24 @@ func (sf *factory) CommitStateChanges(blockHeight uint64, tsf []*action.Transfer
337
337
// Persist accountTrie's root hash
338
338
accountRootHash := sf .RootHash ()
339
339
if err := trieDB .Put (trie .AccountKVNameSpace , []byte (AccountTrieRootKey ), accountRootHash [:]); err != nil {
340
- return errors .Wrap (err , "failed to update accountTrie's root hash in DB " )
340
+ return errors .Wrap (err , "failed to update accountTrie's root hash in underlying db " )
341
341
}
342
342
343
- // Persist new list of candidates to DB
344
- if err := sf .putCandidates (blockHeight ); err != nil {
345
- return err
343
+ // Persist new list of candidates to underlying db
344
+ candidates , err := MapToCandidates (sf .cachedCandidates )
345
+ if err != nil {
346
+ return errors .Wrap (err , "failed to convert map of cached candidates to candidate list" )
347
+ }
348
+ sort .Sort (candidates )
349
+ candidatesBytes , err := Serialize (candidates )
350
+ if err != nil {
351
+ return errors .Wrap (err , "failed to serialize candidates" )
352
+ }
353
+ if err := trieDB .Put (trie .CandidateKVNameSpace , byteutil .Uint64ToBytes (blockHeight ), candidatesBytes ); err != nil {
354
+ return errors .Wrapf (err , "failed to store candidates on height %d into underlying db" , blockHeight )
346
355
}
347
356
348
- // Set current chain height and persist it to DB
357
+ // Set current chain height and persist it to db
349
358
sf .currentChainHeight = blockHeight
350
359
return trieDB .Put (trie .AccountKVNameSpace , []byte (CurrentHeightKey ), byteutil .Uint64ToBytes (blockHeight ))
351
360
}
@@ -432,7 +441,7 @@ func (sf *factory) Candidates() (uint64, []*Candidate) {
432
441
433
442
// CandidatesByHeight returns array of candidates in candidate pool of a given height
434
443
func (sf * factory ) CandidatesByHeight (height uint64 ) ([]* Candidate , error ) {
435
- // Load candidates on the given height from DB
444
+ // Load candidates on the given height from underlying db
436
445
candidates , err := sf .getCandidates (height )
437
446
if err != nil {
438
447
return []* Candidate {}, errors .Wrapf (err , "failed to get candidates on height %d" , height )
@@ -521,41 +530,13 @@ func (sf *factory) updateCandidate(pkHash hash.AddrHash, totalWeight *big.Int, b
521
530
}
522
531
523
532
func (sf * factory ) getCandidates (height uint64 ) (CandidateList , error ) {
524
- trieDB := sf .accountTrie .TrieDB ()
525
- candHash , err := trieDB .Get (trie .CandidateKVNameSpace , byteutil .Uint64ToBytes (height ))
526
- if err != nil {
527
- return []* Candidate {}, errors .Wrapf (err , "failed to get candidates hash on height %d" , height )
528
- }
529
- candidatesBytes , err := trieDB .Get (trie .CandidateKVNameSpace , candHash [:])
533
+ candidatesBytes , err := sf .accountTrie .TrieDB ().Get (trie .CandidateKVNameSpace , byteutil .Uint64ToBytes (height ))
530
534
if err != nil {
531
535
return []* Candidate {}, errors .Wrapf (err , "failed to get candidates on height %d" , height )
532
536
}
533
537
return Deserialize (candidatesBytes )
534
538
}
535
539
536
- func (sf * factory ) putCandidates (height uint64 ) error {
537
- candidates , err := MapToCandidates (sf .cachedCandidates )
538
- if err != nil {
539
- return errors .Wrap (err , "failed to convert map of cached candidates to candidate list" )
540
- }
541
- sort .Sort (candidates )
542
- candidatesBytes , err := Serialize (candidates )
543
- if err != nil {
544
- return errors .Wrap (err , "failed to serialize candidates" )
545
- }
546
- trieDB := sf .accountTrie .TrieDB ()
547
- h := hash .Hash160b (candidatesBytes )
548
- if err := trieDB .Put (trie .CandidateKVNameSpace , byteutil .Uint64ToBytes (height ), h [:]); err != nil {
549
- return errors .Wrapf (err , "failed to put candidates hash on height %d" , height )
550
- }
551
- if _ , err := trieDB .Get (trie .CandidateKVNameSpace , h [:]); err == nil {
552
- // candidate list already exist
553
- return err
554
- }
555
- // store new candidate list into DB
556
- return trieDB .Put (trie .CandidateKVNameSpace , h [:], candidatesBytes )
557
- }
558
-
559
540
//======================================
560
541
// private transfer/vote functions
561
542
//======================================
@@ -685,7 +666,7 @@ func (sf *factory) getRoot(trieDB db.KVStore, nameSpace string, key string) (has
685
666
case bolt .ErrBucketNotFound :
686
667
trieRoot = trie .EmptyRoot
687
668
default :
688
- return hash .ZeroHash32B , errors .Wrap (err , "failed to get trie's root hash from DB " )
669
+ return hash .ZeroHash32B , errors .Wrap (err , "failed to get trie's root hash from underlying db " )
689
670
}
690
671
return trieRoot , nil
691
672
}
0 commit comments