Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Nov 28, 2023
1 parent 7580a6a commit 7d0f2f7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
36 changes: 21 additions & 15 deletions tests/test_peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -259,33 +266,32 @@ 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
)

node3.mountMetadata(0).expect("Mounted Waku Metadata")

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()])

Expand Down
8 changes: 2 additions & 6 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion waku/node/peer_manager/waku_peer_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 9 additions & 5 deletions waku/waku_core/peers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 7d0f2f7

Please sign in to comment.