Skip to content

Commit

Permalink
fix: initial implement for peer-exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Jul 9, 2024
1 parent 8bfad3a commit 01ec8d5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ proc connectRelay*(
waku_node_conns_initiated.inc(labelValues = [source])

pm.peerStore[NumberFailedConnBook][peerId] = 0
pm.peerStore[LastSuccessfulConnBook][peerId] = Moment.init(getTime().toUnix, Second)

return true

Expand Down
10 changes: 8 additions & 2 deletions waku/node/peer_manager/waku_peer_store.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.push raises: [].}

import
std/[tables, sequtils, sets, options, strutils],
std/[tables, sequtils, sets, options, strutils, times, random, math],
chronos,
eth/p2p/discoveryv5/enr,
libp2p/builders,
Expand All @@ -23,6 +23,9 @@ type
# Last failed connection attemp timestamp
LastFailedConnBook* = ref object of PeerBook[Moment]

# Last Successful connection attemp timestamp
LastSuccessfulConnBook* = ref object of PeerBook[Moment]

# Failed connection attempts
NumberFailedConnBook* = ref object of PeerBook[int]

Expand Down Expand Up @@ -68,6 +71,7 @@ proc get*(peerStore: PeerStore, peerId: PeerID): RemotePeerInfo =
origin: peerStore[SourceBook][peerId],
direction: peerStore[DirectionBook][peerId],
lastFailedConn: peerStore[LastFailedConnBook][peerId],
lastSuccessfulConn: peerStore[LastSuccessfulConnBook][peerId],
numberFailedConn: peerStore[NumberFailedConnBook][peerId],
)

Expand Down Expand Up @@ -140,8 +144,10 @@ proc getPeersByProtocol*(peerStore: PeerStore, proto: string): seq[RemotePeerInf
return peerStore.peers.filterIt(it.protocols.contains(proto))

proc getReachablePeers*(peerStore: PeerStore): seq[RemotePeerInfo] =
let threshold = Moment.fromNow(initDuration(hours = 3))
return peerStore.peers.filterIt(
it.connectedness == CanConnect or it.connectedness == Connected
(it.connectedness == CanConnect or it.connectedness == Connected) and
(it.lastSuccessfulConn < threshold)
)

proc getPeersByShard*(
Expand Down
1 change: 1 addition & 0 deletions waku/waku_core/peers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type RemotePeerInfo* = ref object
origin*: PeerOrigin
direction*: PeerDirection
lastFailedConn*: Moment
lastSuccessfulConn*: Moment
numberFailedConn*: int

func `$`*(remotePeerInfo: RemotePeerInfo): string =
Expand Down
1 change: 1 addition & 0 deletions waku/waku_peer_exchange/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ proc respond(
proc getEnrsFromCache(
wpx: WakuPeerExchange, numPeers: uint64
): seq[enr.Record] {.gcsafe.} =
wpx.updatePxEnrCache()
if wpx.enrCache.len() == 0:
debug "peer exchange ENR cache is empty"
return @[]
Expand Down

0 comments on commit 01ec8d5

Please sign in to comment.