diff options
-rw-r--r-- | core/block_cache.go | 17 | ||||
-rw-r--r-- | core/block_cache_test.go | 12 |
2 files changed, 29 insertions, 0 deletions
diff --git a/core/block_cache.go b/core/block_cache.go index ea39e78e8..768d3bf19 100644 --- a/core/block_cache.go +++ b/core/block_cache.go @@ -56,6 +56,23 @@ func (bc *BlockCache) Push(block *types.Block) { bc.hashes[len(bc.hashes)-1] = hash } +func (bc *BlockCache) Delete(hash common.Hash) { + bc.mu.Lock() + defer bc.mu.Unlock() + + if _, ok := bc.blocks[hash]; ok { + delete(bc.blocks, hash) + for i, h := range bc.hashes { + if hash == h { + bc.hashes = bc.hashes[:i+copy(bc.hashes[i:], bc.hashes[i+1:])] + // or ? => bc.hashes = append(bc.hashes[:i], bc.hashes[i+1]...) + + break + } + } + } +} + func (bc *BlockCache) Get(hash common.Hash) *types.Block { bc.mu.RLock() defer bc.mu.RUnlock() diff --git a/core/block_cache_test.go b/core/block_cache_test.go index d4f610b71..434b97792 100644 --- a/core/block_cache_test.go +++ b/core/block_cache_test.go @@ -46,3 +46,15 @@ func TestInclusion(t *testing.T) { } } } + +func TestDeletion(t *testing.T) { + chain := newChain(3) + cache := NewBlockCache(3) + insertChainCache(cache, chain) + + cache.Delete(chain[1].Hash()) + + if cache.Has(chain[1].Hash()) { + t.Errorf("expected %x not to be included") + } +} |