2023-08-30 14:33:57 +00:00
|
|
|
package peermanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2023-08-30 14:33:57 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
2024-01-26 08:45:15 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-08-30 14:33:57 +00:00
|
|
|
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-08-30 14:33:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/waku-org/go-waku/tests"
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/discv5"
|
2024-06-25 15:28:04 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/onlinechecker"
|
2023-08-30 14:33:57 +00:00
|
|
|
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
2023-09-29 05:13:25 +00:00
|
|
|
wakuproto "github.com/waku-org/go-waku/waku/v2/protocol"
|
2023-11-07 17:13:19 +00:00
|
|
|
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
|
2023-09-14 08:36:08 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-08-30 14:33:57 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initTest(t *testing.T) (context.Context, *PeerManager, func()) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
// hosts
|
|
|
|
h1, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2023-09-07 09:31:31 +00:00
|
|
|
|
2023-08-30 14:33:57 +00:00
|
|
|
// host 1 is used by peer manager
|
2024-03-14 14:21:47 +00:00
|
|
|
pm := NewPeerManager(10, 20, nil, utils.Logger())
|
2023-08-30 14:33:57 +00:00
|
|
|
pm.SetHost(h1)
|
2023-09-07 09:31:31 +00:00
|
|
|
|
2023-08-30 14:33:57 +00:00
|
|
|
return ctx, pm, func() {
|
|
|
|
cancel()
|
|
|
|
h1.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceSlots(t *testing.T) {
|
|
|
|
ctx, pm, deferFn := initTest(t)
|
|
|
|
defer deferFn()
|
|
|
|
|
|
|
|
h2, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h2.Close()
|
|
|
|
|
|
|
|
h3, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h3.Close()
|
|
|
|
// protocols
|
|
|
|
protocol := libp2pProtocol.ID("test/protocol")
|
|
|
|
protocol1 := libp2pProtocol.ID("test/protocol1")
|
|
|
|
|
|
|
|
// add h2 peer to peer manager
|
|
|
|
t.Log(h2.ID())
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h2), wps.Static, []string{""}, libp2pProtocol.ID(protocol))
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// getting peer for protocol
|
|
|
|
///////////////
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
var peerID peer.ID
|
2023-08-30 14:33:57 +00:00
|
|
|
// select peer from pm, currently only h2 is set in pm
|
2024-01-26 08:45:15 +00:00
|
|
|
peers, err := pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol})
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, h2.ID(), peers[0])
|
2023-08-30 14:33:57 +00:00
|
|
|
|
|
|
|
// add h3 peer to peer manager
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h3), wps.Static, []string{""}, libp2pProtocol.ID(protocol))
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that returned peer is h2 or h3 peer
|
2024-01-26 08:45:15 +00:00
|
|
|
peers, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol})
|
|
|
|
peerID = peers[0]
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if peerID == h2.ID() || peerID == h3.ID() {
|
|
|
|
//Test success
|
|
|
|
t.Log("Random peer selection per protocol successful")
|
|
|
|
} else {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// getting peer for protocol1
|
|
|
|
///////////////
|
|
|
|
h4, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h4.Close()
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol1})
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Error(t, err, ErrNoPeersAvailable)
|
2023-08-30 14:33:57 +00:00
|
|
|
|
|
|
|
// add h4 peer for protocol1
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h4), wps.Static, []string{""}, libp2pProtocol.ID(protocol1))
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//Test peer selection for protocol1
|
2024-01-26 08:45:15 +00:00
|
|
|
peers, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol1})
|
|
|
|
peerID = peers[0]
|
|
|
|
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, peerID, h4.ID())
|
|
|
|
|
2023-11-13 22:52:46 +00:00
|
|
|
_, err = pm.SelectPeerByContentTopics(protocol1, []string{""})
|
2023-09-29 05:13:25 +00:00
|
|
|
require.Error(t, wakuproto.ErrInvalidFormat, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerSelection(t *testing.T) {
|
|
|
|
ctx, pm, deferFn := initTest(t)
|
|
|
|
defer deferFn()
|
|
|
|
|
|
|
|
h2, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h2.Close()
|
|
|
|
|
|
|
|
h3, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h3.Close()
|
|
|
|
|
|
|
|
protocol := libp2pProtocol.ID("test/protocol")
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h2), wps.Static, []string{"/waku/2/rs/2/1", "/waku/2/rs/2/2"}, libp2pProtocol.ID(protocol))
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h3), wps.Static, []string{"/waku/2/rs/2/1"}, libp2pProtocol.ID(protocol))
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol})
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
peerIDs, err := pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/2"}})
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, h2.ID(), peerIDs[0])
|
2023-09-29 05:13:25 +00:00
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/3"}})
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Error(t, ErrNoPeersAvailable, err)
|
2023-09-29 05:13:25 +00:00
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/1"}})
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-16 16:42:01 +00:00
|
|
|
//Test for selectWithLowestRTT
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: LowestRTT, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/1"}})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
peerIDs, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/1"}, MaxPeers: 2})
|
|
|
|
require.Equal(t, 2, peerIDs.Len())
|
2023-10-16 16:42:01 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
|
|
|
|
peerIDs, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/1"}, MaxPeers: 3})
|
|
|
|
require.Equal(t, 2, peerIDs.Len())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
h4, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h4.Close()
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h4), wps.Static, []string{"/waku/2/rs/2/1"}, libp2pProtocol.ID(protocol))
|
2024-01-26 08:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
peerIDs, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol, PubsubTopics: []string{"/waku/2/rs/2/1"}, MaxPeers: 3})
|
|
|
|
require.Equal(t, 3, peerIDs.Len())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-30 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultProtocol(t *testing.T) {
|
|
|
|
ctx, pm, deferFn := initTest(t)
|
|
|
|
defer deferFn()
|
|
|
|
///////////////
|
|
|
|
// check peer for default protocol
|
|
|
|
///////////////
|
|
|
|
//Test empty peer selection for relay protocol
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err := pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: relay.WakuRelayID_v200})
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Error(t, err, ErrNoPeersAvailable)
|
2023-08-30 14:33:57 +00:00
|
|
|
|
|
|
|
///////////////
|
|
|
|
// getting peer for default protocol
|
|
|
|
///////////////
|
|
|
|
h5, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h5.Close()
|
|
|
|
|
|
|
|
//Test peer selection for relay protocol from peer store
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h5), wps.Static, []string{""}, relay.WakuRelayID_v200)
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// since we are not passing peerList, selectPeer fn using filterByProto checks in PeerStore for peers with same protocol.
|
2024-01-26 08:45:15 +00:00
|
|
|
peerIDs, err := pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: relay.WakuRelayID_v200})
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, h5.ID(), peerIDs[0])
|
2023-08-30 14:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdditionAndRemovalOfPeer(t *testing.T) {
|
|
|
|
ctx, pm, deferFn := initTest(t)
|
|
|
|
defer deferFn()
|
|
|
|
///////////////
|
|
|
|
// set h6 peer for protocol2 and remove that peer and check again
|
|
|
|
///////////////
|
|
|
|
//Test random peer selection
|
|
|
|
protocol2 := libp2pProtocol.ID("test/protocol2")
|
|
|
|
h6, err := tests.MakeHost(ctx, 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer h6.Close()
|
|
|
|
|
2024-05-22 06:15:53 +00:00
|
|
|
_, err = pm.AddPeer(tests.GetAddr(h6), wps.Static, []string{""}, protocol2)
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
peers, err := pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol2})
|
2023-08-30 14:33:57 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, peers[0], h6.ID())
|
2023-08-30 14:33:57 +00:00
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
pm.RemovePeer(peers[0])
|
|
|
|
_, err = pm.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: protocol2})
|
2023-10-16 16:42:01 +00:00
|
|
|
require.Error(t, err, ErrNoPeersAvailable)
|
2023-08-30 14:33:57 +00:00
|
|
|
}
|
2023-09-07 06:36:43 +00:00
|
|
|
|
|
|
|
func TestConnectToRelayPeers(t *testing.T) {
|
|
|
|
|
2023-09-07 09:31:31 +00:00
|
|
|
ctx, pm, deferFn := initTest(t)
|
2024-06-25 15:28:04 +00:00
|
|
|
pc, err := NewPeerConnectionStrategy(pm, onlinechecker.NewDefaultOnlineChecker(true), 120*time.Second, pm.logger)
|
2023-09-07 09:31:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = pc.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
pm.Start(ctx)
|
|
|
|
|
2023-09-07 06:36:43 +00:00
|
|
|
defer deferFn()
|
|
|
|
|
|
|
|
pm.connectToRelayPeers()
|
|
|
|
|
|
|
|
}
|
2023-11-07 17:13:19 +00:00
|
|
|
|
|
|
|
func createHostWithDiscv5AndPM(t *testing.T, hostName string, topic string, enrField uint8, bootnode ...*enode.Node) (host.Host, *PeerManager, *discv5.DiscoveryV5) {
|
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
require.NoError(t, err)
|
|
|
|
wakuPeerStore := wps.NewWakuPeerstore(ps)
|
|
|
|
|
|
|
|
host, _, prvKey1 := tests.CreateHost(t, libp2p.Peerstore(wakuPeerStore))
|
|
|
|
|
|
|
|
logger := utils.Logger().Named(hostName)
|
|
|
|
|
|
|
|
udpPort, err := tests.FindFreeUDPPort(t, "127.0.0.1", 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ip1, _ := tests.ExtractIP(host.Addrs()[0])
|
|
|
|
localNode, err := tests.NewLocalnode(prvKey1, ip1, udpPort, enrField, nil, logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rs, err := wakuproto.TopicsToRelayShards(topic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-06-20 07:20:15 +00:00
|
|
|
err = wenr.Update(utils.Logger(), localNode, wenr.WithWakuRelaySharding(rs[0]))
|
2023-11-07 17:13:19 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-14 14:21:47 +00:00
|
|
|
pm := NewPeerManager(10, 20, nil, logger)
|
2023-11-07 17:13:19 +00:00
|
|
|
pm.SetHost(host)
|
2024-06-25 15:28:04 +00:00
|
|
|
peerconn, err := NewPeerConnectionStrategy(pm, onlinechecker.NewDefaultOnlineChecker(true), 30*time.Second, logger)
|
2023-11-07 17:13:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
discv5, err := discv5.NewDiscoveryV5(prvKey1, localNode, peerconn, prometheus.DefaultRegisterer, logger, discv5.WithUDPPort(uint(udpPort)), discv5.WithBootnodes(bootnode))
|
|
|
|
require.NoError(t, err)
|
|
|
|
discv5.SetHost(host)
|
|
|
|
pm.SetDiscv5(discv5)
|
|
|
|
pm.SetPeerConnector(peerconn)
|
|
|
|
|
|
|
|
return host, pm, discv5
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnDemandPeerDiscovery(t *testing.T) {
|
|
|
|
topic := "/waku/2/rs/1/1"
|
|
|
|
|
|
|
|
// Host1 <-> Host2 <-> Host3
|
|
|
|
host1, _, d1 := createHostWithDiscv5AndPM(t, "host1", topic, wenr.NewWakuEnrBitfield(true, true, false, true))
|
|
|
|
|
|
|
|
host2, _, d2 := createHostWithDiscv5AndPM(t, "host2", topic, wenr.NewWakuEnrBitfield(false, true, true, true), d1.Node())
|
|
|
|
host3, pm3, d3 := createHostWithDiscv5AndPM(t, "host3", topic, wenr.NewWakuEnrBitfield(true, true, true, true), d2.Node())
|
|
|
|
|
|
|
|
defer d1.Stop()
|
|
|
|
defer d2.Stop()
|
|
|
|
defer d3.Stop()
|
|
|
|
|
|
|
|
defer host1.Close()
|
|
|
|
defer host2.Close()
|
|
|
|
defer host3.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err := d1.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = d2.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = d3.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//Discovery should fail for non-waku protocol
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm3.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, PubsubTopics: []string{topic}, Proto: "/test"})
|
2023-11-07 17:13:19 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2024-01-26 08:45:15 +00:00
|
|
|
_, err = pm3.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, Proto: "/test"})
|
2023-11-07 17:13:19 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var enrField uint8
|
|
|
|
enrField |= (1 << 1)
|
|
|
|
pm3.RegisterWakuProtocol("/vac/waku/store/2.0.0-beta4", enrField)
|
2024-01-26 08:45:15 +00:00
|
|
|
peerIDs, err := pm3.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, PubsubTopics: []string{topic}, Proto: "/vac/waku/store/2.0.0-beta4", Ctx: ctx})
|
2023-11-07 17:13:19 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, host2.ID(), peerIDs[0])
|
2023-11-07 17:13:19 +00:00
|
|
|
|
|
|
|
var enrField1 uint8
|
|
|
|
|
|
|
|
enrField1 |= (1 << 3)
|
|
|
|
pm3.RegisterWakuProtocol("/vac/waku/lightpush/2.0.0-beta1", enrField1)
|
2024-01-26 08:45:15 +00:00
|
|
|
peerIDs, err = pm3.SelectPeers(PeerSelectionCriteria{SelectionType: Automatic, PubsubTopics: []string{topic}, Proto: "/vac/waku/lightpush/2.0.0-beta1", Ctx: ctx})
|
2023-11-07 17:13:19 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 08:45:15 +00:00
|
|
|
require.Equal(t, host1.ID(), peerIDs[0])
|
2023-11-07 17:13:19 +00:00
|
|
|
|
|
|
|
}
|