OSDN Git Service

update vote result cache (#185)
authorChengcheng Zhang <943420582@qq.com>
Mon, 17 Jun 2019 10:44:50 +0000 (18:44 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 17 Jun 2019 10:44:50 +0000 (18:44 +0800)
* update

* update

database/store.go

index e5e4134..3af6338 100644 (file)
@@ -316,8 +316,16 @@ func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *s
                return err
        }
 
-       if err := saveVoteResult(batch, voteResults); err != nil {
-               return err
+       for _, vote := range voteResults {
+               bytes, err := json.Marshal(vote)
+               if err != nil {
+                       return err
+               }
+
+               batch.Set(calcVoteResultKey(vote.Seq), bytes)
+               if _, ok := s.cache.getVoteResult(vote.Seq); ok {
+                       s.cache.addVoteResult(vote)
+               }
        }
 
        bytes, err := json.Marshal(protocol.BlockStoreState{
@@ -334,16 +342,3 @@ func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *s
        batch.Write()
        return nil
 }
-
-// saveVoteResult update the voting results generated by each irreversible block
-func saveVoteResult(batch dbm.Batch, voteResults []*state.VoteResult) error {
-       for _, vote := range voteResults {
-               bytes, err := json.Marshal(vote)
-               if err != nil {
-                       return err
-               }
-
-               batch.Set(calcVoteResultKey(vote.Seq), bytes)
-       }
-       return nil
-}