Skip to content
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
13 changes: 13 additions & 0 deletions network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
ID() peer.ID // return peer.ID for self
IDSigner() *PeerIDChallengeSigner
AddrInfo() peer.AddrInfo // return addrInfo for self
NetworkNotify(network.Notifiee)
NetworkStopNotify(network.Notifiee)

DialPeersUntilTargetCount(targetConnCount int)
ClosePeer(peer.ID) error
Expand Down Expand Up @@ -227,6 +229,7 @@

// Close shuts down the P2P service
func (s *serviceImpl) Close() error {
s.host.Network().StopNotify(s.streams)

Check warning on line 232 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L232

Added line #L232 was not covered by tests
return s.host.Close()
}

Expand Down Expand Up @@ -424,3 +427,13 @@
func (s *serviceImpl) GetHTTPClient(addrInfo *peer.AddrInfo, connTimeStore limitcaller.ConnectionTimeStore, queueingTimeout time.Duration) (*http.Client, error) {
return makeHTTPClientWithRateLimit(addrInfo, s, connTimeStore, queueingTimeout)
}

// NetworkNotify registers a notifiee with the host's network
func (s *serviceImpl) NetworkNotify(n network.Notifiee) {
s.host.Network().Notify(n)

Check warning on line 433 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L432-L433

Added lines #L432 - L433 were not covered by tests
}

// NetworkStopNotify unregisters a notifiee with the host's network
func (s *serviceImpl) NetworkStopNotify(n network.Notifiee) {
s.host.Network().StopNotify(n)

Check warning on line 438 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L437-L438

Added lines #L437 - L438 were not covered by tests
}
25 changes: 24 additions & 1 deletion network/p2pNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ func (s *mockService) GetHTTPClient(addrInfo *peer.AddrInfo, connTimeStore limit
return nil, nil
}

func (s *mockService) NetworkNotify(notifiee network.Notifiee) {
}

func (s *mockService) NetworkStopNotify(notifiee network.Notifiee) {
}

func makeMockService(id peer.ID, addrs []ma.Multiaddr) *mockService {
return &mockService{
id: id,
Expand Down Expand Up @@ -1357,8 +1363,23 @@ func TestP2PEnableGossipService_BothDisable(t *testing.T) {
relayCfg := cfg
relayCfg.NetAddress = "127.0.0.1:0"

var netAConnected atomic.Bool
var netBConnected atomic.Bool
notifiee1 := &network.NotifyBundle{
ConnectedF: func(n network.Network, c network.Conn) {
netAConnected.Store(true)
},
}
notifiee2 := &network.NotifyBundle{
ConnectedF: func(n network.Network, c network.Conn) {
netBConnected.Store(true)
},
}

netA, err := NewP2PNetwork(log.With("net", "netA"), relayCfg, "", nil, genesisID, config.Devtestnet, &nopeNodeInfo{}, nil)
require.NoError(t, err)
netA.service.NetworkNotify(notifiee1)
defer netA.service.NetworkStopNotify(notifiee1)
netA.Start()
defer netA.Stop()

Expand All @@ -1374,11 +1395,13 @@ func TestP2PEnableGossipService_BothDisable(t *testing.T) {

netB, err := NewP2PNetwork(log.With("net", "netB"), nodeCfg, "", phoneBookAddresses, genesisID, config.Devtestnet, &nopeNodeInfo{}, nil)
require.NoError(t, err)
netB.service.NetworkNotify(notifiee2)
defer netB.service.NetworkStopNotify(notifiee2)
netB.Start()
defer netB.Stop()

require.Eventually(t, func() bool {
return len(netA.service.Conns()) > 0 && len(netB.service.Conns()) > 0
return netAConnected.Load() && netBConnected.Load()
}, 1*time.Second, 50*time.Millisecond)

require.False(t, netA.hasPeers())
Expand Down
Loading