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

chore: peer exchange tests coverage improvement #1046

Merged
merged 9 commits into from
Mar 11, 2024
154 changes: 154 additions & 0 deletions waku/v2/protocol/peer_exchange/waku_peer_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ package peer_exchange

import (
"context"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/multiformats/go-multiaddr"
"github.com/waku-org/go-waku/waku/v2/peermanager"
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"testing"
"time"

Expand All @@ -17,6 +25,26 @@ import (
"github.com/waku-org/go-waku/waku/v2/utils"
)

func createHostWithDiscv5(t *testing.T, bootnode ...*enode.Node) (host.Host, *discv5.DiscoveryV5) {
ps, err := pstoremem.NewPeerstore()
require.NoError(t, err)
wakuPeerStore := wps.NewWakuPeerstore(ps)

host, _, prvKey := tests.CreateHost(t, libp2p.Peerstore(wakuPeerStore))

port, err := tests.FindFreePort(t, "127.0.0.1", 3)
require.NoError(t, err)
ip, _ := tests.ExtractIP(host.Addrs()[0])
l, err := tests.NewLocalnode(prvKey, ip, port, wenr.NewWakuEnrBitfield(false, false, false, true), nil, utils.Logger())
require.NoError(t, err)
discv5PeerConn1 := discv5.NewTestPeerDiscoverer()
d, err := discv5.NewDiscoveryV5(prvKey, l, discv5PeerConn1, prometheus.DefaultRegisterer, utils.Logger(), discv5.WithUDPPort(uint(port)), discv5.WithBootnodes(bootnode))
require.NoError(t, err)
d.SetHost(host)

return host, d
}

func TestRetrieveProvidePeerExchangePeers(t *testing.T) {
// H1
host1, _, prvKey1 := tests.CreateHost(t)
Expand Down Expand Up @@ -185,3 +213,129 @@ func TestRetrieveFilteredPeerExchangePeers(t *testing.T) {
px1.Stop()
px3.Stop()
}

func TestPeerExchangeOptions(t *testing.T) {

// Prepare host1
host1, _, prvKey1 := tests.CreateHost(t)
udpPort1, err := tests.FindFreePort(t, "127.0.0.1", 3)
require.NoError(t, err)
ip1, _ := tests.ExtractIP(host1.Addrs()[0])
l1, err := tests.NewLocalnode(prvKey1, ip1, udpPort1, wenr.NewWakuEnrBitfield(false, false, false, true), nil, utils.Logger())
require.NoError(t, err)
discv5PeerConn1 := discv5.NewTestPeerDiscoverer()
d1, err := discv5.NewDiscoveryV5(prvKey1, l1, discv5PeerConn1, prometheus.DefaultRegisterer, utils.Logger(), discv5.WithUDPPort(uint(udpPort1)))
require.NoError(t, err)
d1.SetHost(host1)

// Mount peer exchange
pxPeerConn1 := discv5.NewTestPeerDiscoverer()
px1, err := NewWakuPeerExchange(d1, pxPeerConn1, nil, prometheus.DefaultRegisterer, utils.Logger())
require.NoError(t, err)
px1.SetHost(host1)

// Test WithPeerAddr()
params := new(PeerExchangeParameters)
params.host = px1.h
params.log = px1.log
params.pm = px1.pm

optList := DefaultOptions(px1.h)
optList = append(optList, WithPeerAddr(host1.Addrs()[0]))
for _, opt := range optList {
err := opt(params)
require.NoError(t, err)
}

require.Equal(t, host1.Addrs()[0], params.peerAddr)

// Test WithFastestPeerSelection()
optList = DefaultOptions(px1.h)
optList = append(optList, WithFastestPeerSelection(host1.ID()))
for _, opt := range optList {
err := opt(params)
require.NoError(t, err)
}

require.Equal(t, peermanager.LowestRTT, params.peerSelectionType)
require.Equal(t, host1.ID(), params.preferredPeers[0])

}

func TestRetrieveProvidePeerExchangeWithPMAndPeerAddr(t *testing.T) {
log := utils.Logger()

// H1 + H2 with discovery on
host1, d1 := createHostWithDiscv5(t)
host2, d2 := createHostWithDiscv5(t, d1.Node())

// H3
ps3, err := pstoremem.NewPeerstore()
require.NoError(t, err)
wakuPeerStore3 := wps.NewWakuPeerstore(ps3)
host3, _, _ := tests.CreateHost(t, libp2p.Peerstore(wakuPeerStore3))

defer d1.Stop()
defer d2.Stop()
defer host1.Close()
defer host2.Close()
defer host3.Close()

err = d1.Start(context.Background())
require.NoError(t, err)

err = d2.Start(context.Background())
require.NoError(t, err)

// Prepare peer manager for host3
pm3 := peermanager.NewPeerManager(10, 20, log)
pm3.SetHost(host3)
pxPeerConn3, err := peermanager.NewPeerConnectionStrategy(pm3, 30*time.Second, utils.Logger())
require.NoError(t, err)
pm3.SetPeerConnector(pxPeerConn3)
romanzac marked this conversation as resolved.
Show resolved Hide resolved
pm3.Start(context.Background())
romanzac marked this conversation as resolved.
Show resolved Hide resolved

// mount peer exchange
pxPeerConn1 := discv5.NewTestPeerDiscoverer()
px1, err := NewWakuPeerExchange(d1, pxPeerConn1, nil, prometheus.DefaultRegisterer, utils.Logger())
require.NoError(t, err)
px1.SetHost(host1)

//pxPeerConn3 := discv5.NewTestPeerDiscoverer()
romanzac marked this conversation as resolved.
Show resolved Hide resolved
px3, err := NewWakuPeerExchange(nil, pxPeerConn3, pm3, prometheus.DefaultRegisterer, utils.Logger())
require.NoError(t, err)
px3.SetHost(host3)

err = px1.Start(context.Background())
require.NoError(t, err)

err = px3.Start(context.Background())
romanzac marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
romanzac marked this conversation as resolved.
Show resolved Hide resolved

time.Sleep(30 * time.Second)
romanzac marked this conversation as resolved.
Show resolved Hide resolved

log.Info("Host1 is", zap.String("peer", host1.ID().String()))
log.Info("Host2 is", zap.String("peer", host2.ID().String()))
log.Info("Host3 is", zap.String("peer", host3.ID().String()))

for _, peer := range host3.Peerstore().Peers() {
log.Info("Host3 knows before", zap.String("peer", peer.String()))
}
require.False(t, slices.Contains(host3.Peerstore().Peers(), host2.ID()))

// Construct multi address like example "/ip4/0.0.0.0/tcp/30304/p2p/16Uiu2HAmBu5zRFzBGAzzMAuGWhaxN2EwcbW7CzibELQELzisf192"
host1MultiAddr, err := multiaddr.NewMultiaddr(host1.Addrs()[0].String() + "/p2p/" + host1.ID().String())
romanzac marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

log.Info("Connecting to peer", zap.String(host1MultiAddr.String(), "to provide 1 peer"))
err = px3.Request(context.Background(), 1, WithPeerAddr(host1MultiAddr))
require.NoError(t, err)

time.Sleep(30 * time.Second)
romanzac marked this conversation as resolved.
Show resolved Hide resolved

for _, peer := range host3.Peerstore().Peers() {
log.Info("Host3 knows after", zap.String("peer", peer.String()))
}
require.True(t, slices.Contains(host3.Peerstore().Peers(), host2.ID()))

}
Loading