diff --git a/tests/test_peer_manager.nim b/tests/test_peer_manager.nim index 5a3e3b0844..d1f8347d03 100644 --- a/tests/test_peer_manager.nim +++ b/tests/test_peer_manager.nim @@ -227,13 +227,12 @@ procSuite "Peer Manager": storage = WakuPeerStorage.new(database)[] node1 = newTestWakuNode( generateSecp256k1Key(), - ValidIpAddress.init("0.0.0.0"), - Port(0), + ValidIpAddress.init("127.0.0.1"), + Port(44048), peerStorage = storage ) - node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(0)) - peerInfo2 = node2.switch.peerInfo - + node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("127.0.0.1"), Port(34023)) + node1.mountMetadata(0).expect("Mounted Waku Metadata") node2.mountMetadata(0).expect("Mounted Waku Metadata") @@ -243,12 +242,20 @@ procSuite "Peer Manager": await node1.mountRelay() await node2.mountRelay() - # Setup sharding info + let peerInfo2 = node2.switch.peerInfo var remotePeerInfo2 = peerInfo2.toRemotePeerInfo() remotePeerInfo2.enr = some(node2.enr) - let isConnected = await node1.peerManager.connectRelay(remotePeerInfo2) - assert isConnected == true, "Node 1 and 2 not connected" + let is12Connected = await node1.peerManager.connectRelay(remotePeerInfo2) + assert is12Connected == true, "Node 1 and 2 not connected" + + # When node use 0.0.0.0 and port 0 + # After connecting the peer store is updated with the wrong address + check: + node1.peerManager.peerStore[AddressBook][remotePeerInfo2.peerId] == remotePeerInfo2.addrs + + # wait for the peer store update + await sleepAsync(chronos.milliseconds(500)) check: # Currently connected to node2 @@ -259,8 +266,8 @@ procSuite "Peer Manager": # Simulate restart by initialising a new node using the same storage let node3 = newTestWakuNode( generateSecp256k1Key(), - ValidIpAddress.init("0.0.0.0"), - Port(0), + ValidIpAddress.init("127.0.0.1"), + Port(56037), peerStorage = storage ) @@ -268,24 +275,23 @@ procSuite "Peer Manager": await node3.start() - await node3.mountRelay() - check: # Node2 has been loaded after "restart", but we have not yet reconnected node3.peerManager.peerStore.peers().len == 1 node3.peerManager.peerStore.peers().anyIt(it.peerId == peerInfo2.peerId) node3.peerManager.peerStore.connectedness(peerInfo2.peerId) == NotConnected + await node3.mountRelay() + await node3.peerManager.manageRelayPeers() - # Can't work because .manageRelayPeers() require sharding information - # but the ENR is not save in storage + await sleepAsync(chronos.milliseconds(500)) check: # Reconnected to node2 after "restart" node3.peerManager.peerStore.peers().len == 1 node3.peerManager.peerStore.peers().anyIt(it.peerId == peerInfo2.peerId) - node3.peerManager.peerStore.connectedness(peerInfo2.peerId) == Connected + node3.peerManager.peerStore.connectedness(peerInfo2.peerId) == Connected await allFutures([node1.stop(), node2.stop(), node3.stop()]) diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index 1cd6858a3d..04503233df 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -115,12 +115,8 @@ proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, origin = UnknownO # Do not attempt to manage our unmanageable self return - # ...public key - var publicKey: PublicKey - discard remotePeerInfo.peerId.extractPublicKey(publicKey) - if pm.peerStore[AddressBook][remotePeerInfo.peerId] == remotePeerInfo.addrs and - pm.peerStore[KeyBook][remotePeerInfo.peerId] == publicKey and + pm.peerStore[KeyBook][remotePeerInfo.peerId] == remotePeerInfo.publicKey and pm.peerStore[ENRBook][remotePeerInfo.peerId].raw.len > 0: # Peer already managed and ENR info is already saved return @@ -133,7 +129,7 @@ proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, origin = UnknownO enr = remotePeerInfo.enr pm.peerStore[AddressBook][remotePeerInfo.peerId] = remotePeerInfo.addrs - pm.peerStore[KeyBook][remotePeerInfo.peerId] = publicKey + pm.peerStore[KeyBook][remotePeerInfo.peerId] = remotePeerInfo.publicKey pm.peerStore[SourceBook][remotePeerInfo.peerId] = origin if remotePeerInfo.protocols.len > 0: diff --git a/waku/node/peer_manager/waku_peer_store.nim b/waku/node/peer_manager/waku_peer_store.nim index 6c426fce48..44dbb75dbc 100644 --- a/waku/node/peer_manager/waku_peer_store.nim +++ b/waku/node/peer_manager/waku_peer_store.nim @@ -4,7 +4,7 @@ else: {.push raises: [].} import - std/[tables, sequtils, sets, options, strutils], + std/[tables, sequtils, sets, times, options, strutils], chronos, eth/p2p/discoveryv5/enr, libp2p/builders, diff --git a/waku/waku_core/peers.nim b/waku/waku_core/peers.nim index 941b3c55d7..9acfd315c3 100644 --- a/waku/waku_core/peers.nim +++ b/waku/waku_core/peers.nim @@ -218,11 +218,15 @@ converter toRemotePeerInfo*(peerInfo: PeerInfo): RemotePeerInfo = ## Converts the local peerInfo to dialable RemotePeerInfo. ## Useful for testing or internal connections. ## Result in a RemotePeerInfo without ENR. - RemotePeerInfo.init( - peerInfo.peerId, - peerInfo.listenAddrs, - none(enr.Record), - peerInfo.protocols + RemotePeerInfo( + peerId: peerInfo.peerId, + addrs: peerInfo.listenAddrs, + enr: none(Record), + protocols: peerInfo.protocols, + + agent: peerInfo.agentVersion, + protoVersion: peerInfo.protoVersion, + publicKey: peerInfo.publicKey, ) proc hasProtocol*(ma: MultiAddress, proto: string): bool =