good morning!!!!

Skip to content
Snippets Groups Projects
Commit 29f12020 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke
Browse files

Added block cache delete method

parent 0d1a9ce6
No related branches found
No related tags found
No related merge requests found
...@@ -56,6 +56,23 @@ func (bc *BlockCache) Push(block *types.Block) { ...@@ -56,6 +56,23 @@ func (bc *BlockCache) Push(block *types.Block) {
bc.hashes[len(bc.hashes)-1] = hash 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 { func (bc *BlockCache) Get(hash common.Hash) *types.Block {
bc.mu.RLock() bc.mu.RLock()
defer bc.mu.RUnlock() defer bc.mu.RUnlock()
......
...@@ -46,3 +46,15 @@ func TestInclusion(t *testing.T) { ...@@ -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")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment