Skip to content

Commit

Permalink
refactor: wrap peer store (#3051)
Browse files Browse the repository at this point in the history
Encapsulate peerstore with wakupeerstore
  • Loading branch information
darshankabariya authored Sep 27, 2024
1 parent b358c90 commit 729e63f
Show file tree
Hide file tree
Showing 17 changed files with 570 additions and 452 deletions.
6 changes: 3 additions & 3 deletions apps/liteprotocoltester/diagnose_connections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ logScope:
proc logSelfPeersLoop(pm: PeerManager, interval: Duration) {.async.} =
trace "Starting logSelfPeersLoop diagnosis loop"
while true:
let selfLighpushPeers = pm.peerStore.getPeersByProtocol(WakuLightPushCodec)
let selfRelayPeers = pm.peerStore.getPeersByProtocol(WakuRelayCodec)
let selfFilterPeers = pm.peerStore.getPeersByProtocol(WakuFilterSubscribeCodec)
let selfLighpushPeers = pm.wakuPeerStore.getPeersByProtocol(WakuLightPushCodec)
let selfRelayPeers = pm.wakuPeerStore.getPeersByProtocol(WakuRelayCodec)
let selfFilterPeers = pm.wakuPeerStore.getPeersByProtocol(WakuFilterSubscribeCodec)

let printable = catch:
"""*------------------------------------------------------------------------------------------*
Expand Down
2 changes: 1 addition & 1 deletion apps/wakucanary/wakucanary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ proc main(rng: ref HmacDrbgContext): Future[int] {.async.} =
return 1

let lp2pPeerStore = node.switch.peerStore
let conStatus = node.peerManager.peerStore[ConnectionBook][peer.peerId]
let conStatus = node.peerManager.wakuPeerStore[ConnectionBook][peer.peerId]

if conf.ping:
discard await pingFut
Expand Down
5 changes: 3 additions & 2 deletions examples/publisher.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ proc setupAndPublish(rng: ref HmacDrbgContext) {.async.} =

# wait for a minimum of peers to be connected, otherwise messages wont be gossiped
while true:
let numConnectedPeers =
node.peerManager.peerStore[ConnectionBook].book.values().countIt(it == Connected)
let numConnectedPeers = node.peerManager.wakuPeerStore[ConnectionBook].book
.values()
.countIt(it == Connected)
if numConnectedPeers >= 6:
notice "publisher is ready", connectedPeers = numConnectedPeers, required = 6
break
Expand Down
5 changes: 3 additions & 2 deletions examples/subscriber.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ proc setupAndSubscribe(rng: ref HmacDrbgContext) {.async.} =

# wait for a minimum of peers to be connected, otherwise messages wont be gossiped
while true:
let numConnectedPeers =
node.peerManager.peerStore[ConnectionBook].book.values().countIt(it == Connected)
let numConnectedPeers = node.peerManager.wakuPeerStore[ConnectionBook].book
.values()
.countIt(it == Connected)
if numConnectedPeers >= 6:
notice "subscriber is ready", connectedPeers = numConnectedPeers, required = 6
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ proc process*(
return err(ret.error)
of GET_ALL_PEER_IDS:
## returns a comma-separated string of peerIDs
let peerIDs = waku.node.peerManager.peerStore.peers().mapIt($it.peerId).join(",")
let peerIDs =
waku.node.peerManager.wakuPeerStore.peers().mapIt($it.peerId).join(",")
return ok(peerIDs)
of GET_PEER_IDS_BY_PROTOCOL:
## returns a comma-separated string of peerIDs that mount the given protocol
Expand Down
26 changes: 13 additions & 13 deletions tests/node/test_wakunode_peer_exchange.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ suite "Waku Peer Exchange":

# Then no peers are fetched
check:
node.peerManager.peerStore.peers.len == 0
node.peerManager.wakuPeerStore.peers.len == 0
res.error.status_code == SERVICE_UNAVAILABLE
res.error.status_desc == some("PeerExchange is not mounted")

Expand All @@ -98,12 +98,12 @@ suite "Waku Peer Exchange":
res.error.status_desc == some("peer_not_found_failure")

# Then no peers are fetched
check node.peerManager.peerStore.peers.len == 0
check node.peerManager.wakuPeerStore.peers.len == 0

asyncTest "Node succesfully exchanges px peers with faked discv5":
# Given both nodes mount peer exchange
await allFutures([node.mountPeerExchange(), node2.mountPeerExchange()])
check node.peerManager.peerStore.peers.len == 0
check node.peerManager.wakuPeerStore.peers.len == 0

# Mock that we discovered a node (to avoid running discv5)
var enr = enr.Record()
Expand All @@ -124,8 +124,8 @@ suite "Waku Peer Exchange":
# Check that the peer ended up in the peerstore
let rpInfo = enr.toRemotePeerInfo.get()
check:
node.peerManager.peerStore.peers.anyIt(it.peerId == rpInfo.peerId)
node.peerManager.peerStore.peers.anyIt(it.addrs == rpInfo.addrs)
node.peerManager.wakuPeerStore.peers.anyIt(it.peerId == rpInfo.peerId)
node.peerManager.wakuPeerStore.peers.anyIt(it.addrs == rpInfo.addrs)

suite "setPeerExchangePeer":
var node2 {.threadvar.}: WakuNode
Expand All @@ -142,7 +142,7 @@ suite "Waku Peer Exchange":
asyncTest "peer set successfully":
# Given a node with peer exchange mounted
await node.mountPeerExchange()
let initialPeers = node.peerManager.peerStore.peers.len
let initialPeers = node.peerManager.wakuPeerStore.peers.len

# And a valid peer info
let remotePeerInfo2 = node2.peerInfo.toRemotePeerInfo()
Expand All @@ -152,12 +152,12 @@ suite "Waku Peer Exchange":

# Then the peer is added to the peer store
check:
node.peerManager.peerStore.peers.len == (initialPeers + 1)
node.peerManager.wakuPeerStore.peers.len == (initialPeers + 1)

asyncTest "peer exchange not mounted":
# Given a node without peer exchange mounted
check node.wakuPeerExchange == nil
let initialPeers = node.peerManager.peerStore.peers.len
let initialPeers = node.peerManager.wakuPeerStore.peers.len

# And a valid peer info
let invalidMultiAddress = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
Expand All @@ -167,12 +167,12 @@ suite "Waku Peer Exchange":

# Then no peer is added to the peer store
check:
node.peerManager.peerStore.peers.len == initialPeers
node.peerManager.wakuPeerStore.peers.len == initialPeers

asyncTest "peer info parse error":
# Given a node with peer exchange mounted
await node.mountPeerExchange()
let initialPeers = node.peerManager.peerStore.peers.len
let initialPeers = node.peerManager.wakuPeerStore.peers.len

# And given a peer info with an invalid peer id
var remotePeerInfo2 = node2.peerInfo.toRemotePeerInfo()
Expand All @@ -183,7 +183,7 @@ suite "Waku Peer Exchange":

# Then no peer is added to the peer store
check:
node.peerManager.peerStore.peers.len == initialPeers
node.peerManager.wakuPeerStore.peers.len == initialPeers

suite "Waku Peer Exchange with discv5":
asyncTest "Node successfully exchanges px peers with real discv5":
Expand Down Expand Up @@ -286,13 +286,13 @@ suite "Waku Peer Exchange with discv5":

let
requestPeers = 1
currentPeers = node3.peerManager.peerStore.peers.len
currentPeers = node3.peerManager.wakuPeerStore.peers.len
let res = await node3.fetchPeerExchangePeers(1)
check res.tryGet() == 1

# Then node3 has received 1 peer from node1
check:
node3.peerManager.peerStore.peers.len == currentPeers + requestPeers
node3.peerManager.wakuPeerStore.peers.len == currentPeers + requestPeers

await allFutures(
[node1.stop(), node2.stop(), node3.stop(), disc1.stop(), disc2.stop()]
Expand Down
Loading

0 comments on commit 729e63f

Please sign in to comment.