Skip to content

Commit

Permalink
add rollback for headerCache, headerIndex and blockCache
Browse files Browse the repository at this point in the history
1. rollback headerCache, headerIndex and blockCache after rollbacking DB.

Signed-off-by: NoelBright <[email protected]>
  • Loading branch information
NoelBright authored and yilunzhang committed Sep 28, 2018
1 parent 845185f commit a63047a
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion db/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ func (cs *ChainStore) Rollback(b *ledger.Block) error {
return err
}

return cs.st.BatchCommit()
if err := cs.st.BatchCommit(); err != nil {
return err
}

return cs.rollbackCached(b)
}

func (cs *ChainStore) rollbackHeader(b *ledger.Block) error {
Expand Down Expand Up @@ -375,3 +379,25 @@ func (cs *ChainStore) rollbackPrepaidAndWithdraw(b *ledger.Block) error {

return nil
}

func (cs *ChainStore) rollbackCached(b *ledger.Block) error {
cs.mu.Lock()
defer cs.mu.Unlock()

hash := b.Hash()
if h, ok := cs.headerIndex[b.Header.Height]; ok {
if h.CompareTo(hash) == 0 {
delete(cs.headerIndex, b.Header.Height)
}
}

if _, ok := cs.blockCache[hash]; ok {
delete(cs.blockCache, hash)
}

if _, ok := cs.headerCache[hash]; ok {
delete(cs.headerCache, hash)
}

return nil
}

0 comments on commit a63047a

Please sign in to comment.