@@ -103,16 +103,16 @@ func DefaultTrieOption() FactoryOption {
103103 return func (sf * factory , cfg * config.Config ) error {
104104 dbPath := cfg .Chain .TrieDBPath
105105 if len (dbPath ) == 0 {
106- return errors .New ("Invalid empty trie DB path" )
106+ return errors .New ("Invalid empty trie db path" )
107107 }
108108 trieDB := db .NewBoltDB (dbPath , nil )
109109 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 " )
111111 }
112112 // create account trie
113113 accountTrieRoot , err := sf .getRoot (trieDB , trie .AccountKVNameSpace , AccountTrieRootKey )
114114 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 " )
116116 }
117117 tr , err := trie .NewTrie (trieDB , trie .AccountKVNameSpace , accountTrieRoot )
118118 if err != nil {
@@ -129,12 +129,12 @@ func InMemTrieOption() FactoryOption {
129129 return func (sf * factory , cfg * config.Config ) error {
130130 trieDB := db .NewMemKVStore ()
131131 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 " )
133133 }
134134 // create account trie
135135 accountTrieRoot , err := sf .getRoot (trieDB , trie .AccountKVNameSpace , AccountTrieRootKey )
136136 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 " )
138138 }
139139 tr , err := trie .NewTrie (trieDB , trie .AccountKVNameSpace , accountTrieRoot )
140140 if err != nil {
@@ -256,7 +256,7 @@ func (sf *factory) RootHash() hash.Hash32B {
256256func (sf * factory ) Height () (uint64 , error ) {
257257 height , err := sf .accountTrie .TrieDB ().Get (trie .AccountKVNameSpace , []byte (CurrentHeightKey ))
258258 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 " )
260260 }
261261 return byteutil .BytesToUint64 (height ), nil
262262}
@@ -337,15 +337,24 @@ func (sf *factory) CommitStateChanges(blockHeight uint64, tsf []*action.Transfer
337337 // Persist accountTrie's root hash
338338 accountRootHash := sf .RootHash ()
339339 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 " )
341341 }
342342
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 )
346355 }
347356
348- // Set current chain height and persist it to DB
357+ // Set current chain height and persist it to db
349358 sf .currentChainHeight = blockHeight
350359 return trieDB .Put (trie .AccountKVNameSpace , []byte (CurrentHeightKey ), byteutil .Uint64ToBytes (blockHeight ))
351360}
@@ -432,7 +441,7 @@ func (sf *factory) Candidates() (uint64, []*Candidate) {
432441
433442// CandidatesByHeight returns array of candidates in candidate pool of a given height
434443func (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
436445 candidates , err := sf .getCandidates (height )
437446 if err != nil {
438447 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
521530}
522531
523532func (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 ))
530534 if err != nil {
531535 return []* Candidate {}, errors .Wrapf (err , "failed to get candidates on height %d" , height )
532536 }
533537 return Deserialize (candidatesBytes )
534538}
535539
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-
559540//======================================
560541// private transfer/vote functions
561542//======================================
@@ -685,7 +666,7 @@ func (sf *factory) getRoot(trieDB db.KVStore, nameSpace string, key string) (has
685666 case bolt .ErrBucketNotFound :
686667 trieRoot = trie .EmptyRoot
687668 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 " )
689670 }
690671 return trieRoot , nil
691672}
0 commit comments