From 330cf928c62655be88705a3027705cb3e0438331 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Wed, 4 Oct 2023 00:53:14 +0400 Subject: [PATCH] fix peer test failure --- waku/v2/peermanager/peer_manager.go | 12 +++++++++++- waku/v2/peermanager/peer_manager_test.go | 15 +++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/waku/v2/peermanager/peer_manager.go b/waku/v2/peermanager/peer_manager.go index 0ae8719d4..40e4ffb57 100644 --- a/waku/v2/peermanager/peer_manager.go +++ b/waku/v2/peermanager/peer_manager.go @@ -3,6 +3,7 @@ package peermanager import ( "context" "errors" + "fmt" "math/rand" "sync" "time" @@ -537,11 +538,16 @@ type pingResult struct { // to maintain the RTT as part of peer-scoring and just select based on that. func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (peer.ID, error) { var peers peer.IDSlice + var err error if criteria.Ctx == nil { criteria.Ctx = context.Background() } - peers, err := pm.FilterPeersByProto(criteria.SpecificPeers, criteria.Proto) + if criteria.PubsubTopic != "" { + peers = pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopic(criteria.PubsubTopic, criteria.SpecificPeers...) + } + + peers, err = pm.FilterPeersByProto(peers, criteria.Proto) if err != nil { return "", err } @@ -563,6 +569,8 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) ( p: p, rtt: result.RTT, } + } else { + fmt.Println("Error in Ping", result) } }(p) } @@ -575,6 +583,7 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) ( case <-waitCh: var min *pingResult for p := range pingCh { + fmt.Println("ping result", p) if min == nil { min = &p } else { @@ -584,6 +593,7 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) ( } } if min == nil { + pm.logger.Info("Could not find min") return "", ErrNoPeersAvailable } diff --git a/waku/v2/peermanager/peer_manager_test.go b/waku/v2/peermanager/peer_manager_test.go index 4a17a3b40..a23fdc623 100644 --- a/waku/v2/peermanager/peer_manager_test.go +++ b/waku/v2/peermanager/peer_manager_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "fmt" + "strings" "testing" "time" @@ -20,16 +21,22 @@ import ( func getAddr(h host.Host) multiaddr.Multiaddr { id, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().Pretty())) - return h.Network().ListenAddresses()[0].Encapsulate(id) + var selectedAddr multiaddr.Multiaddr + //For now skipping circuit relay addresses as libp2p seems to be returning empty p2p-circuit addresses. + for _, addr := range h.Network().ListenAddresses() { + if strings.Contains(addr.String(), "p2p-circuit") { + continue + } + selectedAddr = addr + } + return selectedAddr.Encapsulate(id) } func initTest(t *testing.T) (context.Context, *PeerManager, func()) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) // hosts h1, err := tests.MakeHost(ctx, 0, rand.Reader) require.NoError(t, err) - defer h1.Close() // host 1 is used by peer manager pm := NewPeerManager(10, 20, utils.Logger())