From 8e6259b2587b28344cc0ad6a93bb59ea93555fde Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 26 Sep 2019 11:31:06 -0700 Subject: [PATCH] Add cache invalidation for reorg'd blocks --- common/cache.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/cache.go b/common/cache.go index 93ad21a..00825bb 100644 --- a/common/cache.go +++ b/common/cache.go @@ -39,6 +39,16 @@ func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error { c.LastBlock = height - 1 } + // If we're adding a block in the middle of the cache, remove all + // blocks after it, since this might be a reorg, and we don't want + // Any outdated blocks returned + if height >= c.FirstBlock && height <= c.LastBlock { + for i := height; i <= c.LastBlock; i++ { + delete(c.m, i) + } + c.LastBlock = height - 1 + } + // Don't allow out-of-order blocks. This is more of a sanity check than anything // If there is a reorg, then the ingestor needs to handle it. if c.m[height-1] != nil && !bytes.Equal(block.PrevHash, c.m[height-1].Hash) {