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

feat(networking): add relay connectivity loop #1482

Merged
merged 4 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit tests
  • Loading branch information
alrevuelta committed Jan 18, 2023
commit 903ff4b23590275b2395625133761289d6508799
1 change: 1 addition & 0 deletions tests/all_tests_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import
./v2/test_waku_filter,
./v2/test_wakunode_filter,
./v2/test_waku_peer_exchange,
./v2/test_peer_store_extended,
./v2/test_waku_payload,
./v2/test_waku_swap,
./v2/test_utils_peers,
Expand Down
28 changes: 27 additions & 1 deletion tests/v2/test_peer_store_extended.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ suite "Extended nim-libp2p Peer Store":
peerStore[ConnectionBook][p1] = Connected
peerStore[DisconnectBook][p1] = 0
peerStore[SourceBook][p1] = Discv5
peerStore[DirectionBook][p1] = Inbound

# Peer2: Connected
peerStore[AddressBook][p2] = @[MultiAddress.init("/ip4/127.0.0.1/tcp/2").tryGet()]
Expand All @@ -54,6 +55,7 @@ suite "Extended nim-libp2p Peer Store":
peerStore[ConnectionBook][p2] = Connected
peerStore[DisconnectBook][p2] = 0
peerStore[SourceBook][p2] = Discv5
peerStore[DirectionBook][p2] = Inbound

# Peer3: Connected
peerStore[AddressBook][p3] = @[MultiAddress.init("/ip4/127.0.0.1/tcp/3").tryGet()]
Expand All @@ -64,6 +66,7 @@ suite "Extended nim-libp2p Peer Store":
peerStore[ConnectionBook][p3] = Connected
peerStore[DisconnectBook][p3] = 0
peerStore[SourceBook][p3] = Discv5
peerStore[DirectionBook][p3] = Inbound

# Peer4: Added but never connected
peerStore[AddressBook][p4] = @[MultiAddress.init("/ip4/127.0.0.1/tcp/4").tryGet()]
Expand All @@ -74,6 +77,7 @@ suite "Extended nim-libp2p Peer Store":
peerStore[ConnectionBook][p4] = NotConnected
peerStore[DisconnectBook][p4] = 0
peerStore[SourceBook][p4] = Discv5
peerStore[DirectionBook][p4] = Inbound

# Peer5: Connecteed in the past
peerStore[AddressBook][p5] = @[MultiAddress.init("/ip4/127.0.0.1/tcp/5").tryGet()]
Expand All @@ -84,6 +88,7 @@ suite "Extended nim-libp2p Peer Store":
peerStore[ConnectionBook][p5] = CanConnect
peerStore[DisconnectBook][p5] = 1000
peerStore[SourceBook][p5] = Discv5
peerStore[DirectionBook][p5] = Outbound

test "get() returns the correct StoredInfo for a given PeerId":
# When
Expand Down Expand Up @@ -113,7 +118,7 @@ suite "Extended nim-libp2p Peer Store":
storedInfoPeer6.protoVersion == ""
storedInfoPeer6.connectedness == NotConnected
storedInfoPeer6.disconnectTime == 0
storedInfoPeer6.origin == Unknown
storedInfoPeer6.origin == UnknownOrigin

test "peers() returns all StoredInfo of the PeerStore":
# When
Expand Down Expand Up @@ -254,3 +259,24 @@ suite "Extended nim-libp2p Peer Store":
swapPeer.isSome()
swapPeer.get().peerId == p5
swapPeer.get().protocols == @["/vac/waku/swap/2.0.0", "/vac/waku/store/2.0.0-beta2"]

test "getPeersByDirection()":
# When
let inPeers = peerStore.getPeersByDirection(Inbound)
let outPeers = peerStore.getPeersByDirection(Outbound)

# Then
check:
inPeers.len == 4
outPeers.len == 1

test "getDisconnectedPeers()":
# When
let disconnedtedPeers = peerStore.getDisconnectedPeers()

# Then
check:
disconnedtedPeers.len == 2
disconnedtedPeers.anyIt(it.peerId == p4)
disconnedtedPeers.anyIt(it.peerId == p5)
not disconnedtedPeers.anyIt(it.connectedness == Connected)
2 changes: 1 addition & 1 deletion waku/v2/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, proto: string) =
# Peer already managed
return

info "Adding peer to manager", peerId = remotePeerInfo.peerId, addresses = remotePeerInfo.addrs, proto = proto
debug "Adding peer to manager", peerId = remotePeerInfo.peerId, addresses = remotePeerInfo.addrs, proto = proto

pm.peerStore[AddressBook][remotePeerInfo.peerId] = remotePeerInfo.addrs
pm.peerStore[KeyBook][remotePeerInfo.peerId] = publicKey
Expand Down
1 change: 0 additions & 1 deletion waku/v2/node/peer_manager/waku_peer_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,5 @@ proc selectPeer*(peerStore: PeerStore, proto: string): Option[RemotePeerInfo] =
proc getPeersByDirection*(peerStore: PeerStore, direction: Direction): seq[StoredInfo] =
return peerStore.peers().filterIt(it.direction == direction)

# TODO: unit test.
proc getDisconnectedPeers*(peerStore: PeerStore): seq[StoredInfo] =
return peerStore.peers().filterIt(it.connectedness != Connected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You can remove the parenthesis from the peers property, as it is a noun.

Suggested change
return peerStore.peers().filterIt(it.connectedness != Connected)
return peerStore.peers.filterIt(it.connectedness != Connected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure fixed.