Skip to content

Commit 55b0ab7

Browse files
acuddshulyak
authored andcommitted
swarm/network: fix data race warning on TestBzzHandshakeLightNode (ethereum#18459)
(cherry picked from commit 81e26d5)
1 parent 42013c3 commit 55b0ab7

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

swarm/network/protocol.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (b *Bzz) APIs() []rpc.API {
168168
func (b *Bzz) RunProtocol(spec *protocols.Spec, run func(*BzzPeer) error) func(*p2p.Peer, p2p.MsgReadWriter) error {
169169
return func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
170170
// wait for the bzz protocol to perform the handshake
171-
handshake, _ := b.GetHandshake(p.ID())
171+
handshake, _ := b.GetOrCreateHandshake(p.ID())
172172
defer b.removeHandshake(p.ID())
173173
select {
174174
case <-handshake.done:
@@ -213,7 +213,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
213213
// runBzz is the p2p protocol run function for the bzz base protocol
214214
// that negotiates the bzz handshake
215215
func (b *Bzz) runBzz(p *p2p.Peer, rw p2p.MsgReadWriter) error {
216-
handshake, _ := b.GetHandshake(p.ID())
216+
handshake, _ := b.GetOrCreateHandshake(p.ID())
217217
if !<-handshake.init {
218218
return fmt.Errorf("%08x: bzz already started on peer %08x", b.localAddr.Over()[:4], p.ID().Bytes()[:4])
219219
}
@@ -303,7 +303,7 @@ func (b *Bzz) removeHandshake(peerID enode.ID) {
303303
}
304304

305305
// GetHandshake returns the bzz handhake that the remote peer with peerID sent
306-
func (b *Bzz) GetHandshake(peerID enode.ID) (*HandshakeMsg, bool) {
306+
func (b *Bzz) GetOrCreateHandshake(peerID enode.ID) (*HandshakeMsg, bool) {
307307
b.mtx.Lock()
308308
defer b.mtx.Unlock()
309309
handshake, found := b.handshakes[peerID]

swarm/network/protocol_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"testing"
24+
"time"
2425

2526
"github.com/ethereum/go-ethereum/log"
2627
"github.com/ethereum/go-ethereum/p2p"
@@ -224,7 +225,7 @@ func TestBzzHandshakeLightNode(t *testing.T) {
224225
for _, test := range lightNodeTests {
225226
t.Run(test.name, func(t *testing.T) {
226227
randomAddr := RandomAddr()
227-
pt := newBzzHandshakeTester(t, 1, randomAddr, false)
228+
pt := newBzzHandshakeTester(nil, 1, randomAddr, false) // TODO change signature - t is not used anywhere
228229
node := pt.Nodes[0]
229230
addr := NewAddr(node)
230231

@@ -237,8 +238,14 @@ func TestBzzHandshakeLightNode(t *testing.T) {
237238
t.Fatal(err)
238239
}
239240

240-
if pt.bzz.handshakes[node.ID()].LightNode != test.lightNode {
241-
t.Fatalf("peer LightNode flag is %v, should be %v", pt.bzz.handshakes[node.ID()].LightNode, test.lightNode)
241+
select {
242+
243+
case <-pt.bzz.handshakes[node.ID()].done:
244+
if pt.bzz.handshakes[node.ID()].LightNode != test.lightNode {
245+
t.Fatalf("peer LightNode flag is %v, should be %v", pt.bzz.handshakes[node.ID()].LightNode, test.lightNode)
246+
}
247+
case <-time.After(10 * time.Second):
248+
t.Fatal("test timeout")
242249
}
243250
})
244251
}

0 commit comments

Comments
 (0)