OSDN Git Service

fix cache (#463)
[bytom/vapor.git] / database / store.go
index 6abd41f..bff69c6 100644 (file)
@@ -53,7 +53,7 @@ func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState {
 // methods for querying current data.
 type Store struct {
        db    dbm.DB
-       cache cache
+       cache *cache
 }
 
 func calcMainChainIndexPrefix(height uint64) []byte {
@@ -325,6 +325,7 @@ func (s *Store) SaveBlockHeader(blockHeader *types.BlockHeader) error {
 
 // SaveChainStatus save the core's newest status && delete old status
 func (s *Store) SaveChainStatus(blockHeader, irrBlockHeader *types.BlockHeader, mainBlockHeaders []*types.BlockHeader, view *state.UtxoViewpoint, consensusResults []*state.ConsensusResult) error {
+       currentStatus := loadBlockStoreStateJSON(s.db)
        batch := s.db.NewBatch()
        if err := saveUtxoView(batch, view); err != nil {
                return err
@@ -364,6 +365,13 @@ func (s *Store) SaveChainStatus(blockHeader, irrBlockHeader *types.BlockHeader,
                batch.Set(calcMainChainIndexPrefix(bh.Height), binaryBlockHash)
                s.cache.removeMainChainHash(bh.Height)
        }
+
+       if currentStatus != nil {
+               for i := blockHeader.Height + 1; i <= currentStatus.Height; i++ {
+                       batch.Delete(calcMainChainIndexPrefix(i))
+                       s.cache.removeMainChainHash(i)
+               }
+       }
        batch.Write()
        return nil
 }