Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockcache: fix datarace in blockcache_test #7029

Merged
Merged
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
50 changes: 31 additions & 19 deletions blockcache/blockcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ type mockChainBackend struct {
sync.RWMutex
}

func (m *mockChainBackend) addBlock(block *wire.MsgBlock, nonce uint32) {
m.Lock()
defer m.Unlock()
block.Header.Nonce = nonce
hash := block.Header.BlockHash()
m.blocks[hash] = block
func newMockChain() *mockChainBackend {
return &mockChainBackend{
blocks: make(map[chainhash.Hash]*wire.MsgBlock),
}
}

// GetBlock is a mock implementation of block fetching that tracks the number
// of backend calls and returns the block found for the given hash or an error.
func (m *mockChainBackend) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

m.chainCallCount++

block, ok := m.blocks[*blockHash]
Expand All @@ -40,15 +42,25 @@ func (m *mockChainBackend) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock,
return block, nil
}

func newMockChain() *mockChainBackend {
return &mockChainBackend{
blocks: make(map[chainhash.Hash]*wire.MsgBlock),
}
func (m *mockChainBackend) getChainCallCount() int {
m.RLock()
defer m.RUnlock()

return m.chainCallCount
}

func (m *mockChainBackend) addBlock(block *wire.MsgBlock, nonce uint32) {
m.Lock()
defer m.Unlock()
MStreet3 marked this conversation as resolved.
Show resolved Hide resolved

block.Header.Nonce = nonce
hash := block.Header.BlockHash()
m.blocks[hash] = block
}

func (m *mockChainBackend) resetChainCallCount() {
m.RLock()
defer m.RUnlock()
m.Lock()
defer m.Unlock()

m.chainCallCount = 0
}
Expand Down Expand Up @@ -90,7 +102,7 @@ func TestBlockCacheGetBlock(t *testing.T) {
_, err := bc.GetBlock(&blockhash1, getBlockImpl)
require.NoError(t, err)
require.Equal(t, 1, bc.Cache.Len())
require.Equal(t, 1, mc.chainCallCount)
require.Equal(t, 1, mc.getChainCallCount())
mc.resetChainCallCount()

_, err = bc.Cache.Get(*inv1)
Expand All @@ -102,7 +114,7 @@ func TestBlockCacheGetBlock(t *testing.T) {
_, err = bc.GetBlock(&blockhash2, getBlockImpl)
require.NoError(t, err)
require.Equal(t, 2, bc.Cache.Len())
require.Equal(t, 1, mc.chainCallCount)
require.Equal(t, 1, mc.getChainCallCount())
mc.resetChainCallCount()

_, err = bc.Cache.Get(*inv1)
Expand All @@ -117,7 +129,7 @@ func TestBlockCacheGetBlock(t *testing.T) {
_, err = bc.GetBlock(&blockhash1, getBlockImpl)
require.NoError(t, err)
require.Equal(t, 2, bc.Cache.Len())
require.Equal(t, 0, mc.chainCallCount)
require.Equal(t, 0, mc.getChainCallCount())
mc.resetChainCallCount()

// Since the Cache is now at its max capacity, it is expected that when
Expand All @@ -128,7 +140,7 @@ func TestBlockCacheGetBlock(t *testing.T) {
_, err = bc.GetBlock(&blockhash3, getBlockImpl)
require.NoError(t, err)
require.Equal(t, 2, bc.Cache.Len())
require.Equal(t, 1, mc.chainCallCount)
require.Equal(t, 1, mc.getChainCallCount())
mc.resetChainCallCount()

_, err = bc.Cache.Get(*inv1)
Expand Down Expand Up @@ -183,5 +195,5 @@ func TestBlockCacheMutexes(t *testing.T) {
}

wg.Wait()
require.Equal(t, 2, mc.chainCallCount)
require.Equal(t, 2, mc.getChainCallCount())
}
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.16.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ crash](https://github.com/lightningnetwork/lnd/pull/7019).
* [Fixed potential data race on funding manager
restart](https://github.com/lightningnetwork/lnd/pull/6929).

* [Fixed a flake in the TestBlockCacheMutexes unit
test](https://github.com/lightningnetwork/lnd/pull/7029).

## `lncli`
* [Add an `insecure` flag to skip tls auth as well as a `metadata` string slice
flag](https://github.com/lightningnetwork/lnd/pull/6818) that allows the
Expand Down Expand Up @@ -125,6 +128,7 @@ crash](https://github.com/lightningnetwork/lnd/pull/7019).
* hieblmi
* Jesse de Wit
* Matt Morehouse
* Michael Street
* Olaoluwa Osuntokun
* Oliver Gugger
* Priyansh Rastogi