Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"math/big"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -2309,6 +2310,9 @@ func (bc *BlockChain) UpdateBlocksHashCache(block *types.Block) []common.Hash {
cached, ok := bc.blocksHashCache.Get(blockNumber)

if ok {
if slices.Contains(cached, block.Hash()) {
return cached
}
hashArr := cached
hashArr = append(hashArr, block.Hash())
bc.blocksHashCache.Remove(blockNumber)
Expand Down
16 changes: 16 additions & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,22 @@ func TestBlocksHashCacheUpdate(t *testing.T) {
t.Error("BlocksHashCache doesn't work when inserting block solely")
}
})

t.Run("Expect repeated cache update to keep one entry per hash", func(t *testing.T) {
head := chain.CurrentBlock()
chain.UpdateBlocksHashCache(types.NewBlockWithHeader(head))
chain.UpdateBlocksHashCache(types.NewBlockWithHeader(head))
cached, _ := chain.blocksHashCache.Get(head.Number.Uint64())
count := 0
for _, hash := range cached {
if hash == head.Hash() {
count++
}
}
if count != 1 {
t.Errorf("BlocksHashCache has %d entries for head hash, want 1: %v", count, cached)
}
})
}

// TestAreTwoBlocksSamePath tests are two blocks same path.
Expand Down