"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
- dbm "github.com/vapor/database/leveldb"
- "github.com/vapor/database/storage"
- "github.com/vapor/errors"
- "github.com/vapor/protocol"
- "github.com/vapor/protocol/bc"
- "github.com/vapor/protocol/bc/types"
- "github.com/vapor/protocol/state"
+ dbm "github.com/bytom/vapor/database/leveldb"
+ "github.com/bytom/vapor/database/storage"
+ "github.com/bytom/vapor/errors"
+ "github.com/bytom/vapor/protocol"
+ "github.com/bytom/vapor/protocol/bc"
+ "github.com/bytom/vapor/protocol/bc/types"
+ "github.com/bytom/vapor/protocol/state"
)
const (
// methods for querying current data.
type Store struct {
db dbm.DB
- cache cache
+ cache *cache
}
func calcMainChainIndexPrefix(height uint64) []byte {
// 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
}
- for _, result := range consensusResults {
+ var clearCacheFuncs []func()
+ for _, consensusResult := range consensusResults {
+ result := consensusResult
bytes, err := json.Marshal(result)
if err != nil {
return err
}
batch.Set(calcConsensusResultKey(result.Seq), bytes)
- s.cache.removeConsensusResult(result)
+ clearCacheFuncs = append(clearCacheFuncs, func() {
+ s.cache.removeConsensusResult(result)
+ })
}
blockHash := blockHeader.Hash()
batch.Set([]byte{blockStore}, bytes)
// save main chain blockHeaders
- for _, bh := range mainBlockHeaders {
+ for _, blockHeader := range mainBlockHeaders {
+ bh := blockHeader
blockHash := bh.Hash()
binaryBlockHash, err := blockHash.MarshalText()
if err != nil {
}
batch.Set(calcMainChainIndexPrefix(bh.Height), binaryBlockHash)
- s.cache.removeMainChainHash(bh.Height)
+ clearCacheFuncs = append(clearCacheFuncs, func() {
+ s.cache.removeMainChainHash(bh.Height)
+ })
+ }
+
+ if currentStatus != nil {
+ for i := blockHeader.Height + 1; i <= currentStatus.Height; i++ {
+ index := i
+ batch.Delete(calcMainChainIndexPrefix(index))
+ clearCacheFuncs = append(clearCacheFuncs, func() {
+ s.cache.removeMainChainHash(index)
+ })
+ }
}
batch.Write()
+
+ for _, clearCacheFunc := range clearCacheFuncs {
+ clearCacheFunc()
+ }
return nil
}