Skip to content

Commit

Permalink
Merge pull request #288 from jenyasd209/jenyasd209/fix-race-conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan authored Dec 23, 2021
2 parents 06cc49f + f68b864 commit 6035222
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Month, DD, YYYY
- [go package] (Link to PR) Description @username

- [header] Added missing `err` value in ErrorW logging calls. @jbowen93
- [service/block, node/p2p] [Fix race conditions in TestExtendedHeaderBroadcast and TestFull_P2P_Streams.](https://github.com/celestiaorg/celestia-node/pull/288) @jenyasd209
10 changes: 6 additions & 4 deletions node/p2p/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ func AddrsFactory(announce []string, noAnnounce []string) func() (_ p2pconfig.Ad
maNoAnnounce[string(maddr.Bytes())] = true
}

return func(maListen []ma.Multiaddr) (out []ma.Multiaddr) {
// combine maListen and maAnnounce addresses
maAnnounce = append(maAnnounce, maListen...)
return func(maListen []ma.Multiaddr) []ma.Multiaddr {
// copy maAnnounce to out
out := make([]ma.Multiaddr, 0, len(maAnnounce)+len(maListen))
out = append(out, maAnnounce...)

// filter out unneeded
for _, maddr := range maListen {
ok := maNoAnnounce[string(maddr.Bytes())]
if !ok {
out = append(out, maddr)
}
}
return
return out
}, nil
}
}
25 changes: 22 additions & 3 deletions service/block/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package block

import (
"context"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -58,7 +59,9 @@ func TestExtendedHeaderBroadcast(t *testing.T) {

mockFetcher := &mockFetcher{
suite: suite,
commitsLock: sync.Mutex{},
commits: make(map[int64]*core.Commit),
valSetsLock: sync.Mutex{},
valSets: make(map[int64]*core.ValidatorSet),
mockNewBlockCh: make(chan *RawBlock),
}
Expand Down Expand Up @@ -106,9 +109,14 @@ func TestExtendedHeaderBroadcast(t *testing.T) {

// mockFetcher mocks away the `Fetcher` interface.
type mockFetcher struct {
suite *header.TestSuite
valSets map[int64]*core.ValidatorSet
commits map[int64]*core.Commit
suite *header.TestSuite

valSetsLock sync.Mutex
valSets map[int64]*core.ValidatorSet

commitsLock sync.Mutex
commits map[int64]*core.Commit

mockNewBlockCh chan *RawBlock
}

Expand All @@ -117,10 +125,16 @@ func (m *mockFetcher) GetBlock(ctx context.Context, height *int64) (*RawBlock, e
}

func (m *mockFetcher) Commit(ctx context.Context, height *int64) (*core.Commit, error) {
m.commitsLock.Lock()
defer m.commitsLock.Unlock()

return m.commits[*height], nil
}

func (m *mockFetcher) ValidatorSet(ctx context.Context, height *int64) (*core.ValidatorSet, error) {
m.valSetsLock.Lock()
defer m.valSetsLock.Unlock()

return m.valSets[*height], nil
}

Expand Down Expand Up @@ -149,8 +163,13 @@ func (m *mockFetcher) generateBlocksWithValidHeaders(t *testing.T, num int) []*R

rawBlocks[i] = b
// store commit and valset at height
m.commitsLock.Lock()
m.commits[b.Height] = eh.Commit
m.commitsLock.Unlock()

m.valSetsLock.Lock()
m.valSets[b.Height] = eh.ValidatorSet
m.valSetsLock.Unlock()

m.mockNewBlockCh <- b
prevEH = eh
Expand Down

0 comments on commit 6035222

Please sign in to comment.