mirror of https://github.com/status-im/nim-eth.git
add some peer convenience funcs to PeerPool and EthNode
This commit is contained in:
parent
9f1d5ef1a0
commit
4f0155e626
18
eth/p2p.nim
18
eth/p2p.nim
|
@ -176,3 +176,21 @@ proc getPeer*(node: EthereumNode, peerId: NodeId, Protocol: type): Option[Peer]
|
||||||
for peer in node.peers(Protocol):
|
for peer in node.peers(Protocol):
|
||||||
if peer.remote.id == peerId:
|
if peer.remote.id == peerId:
|
||||||
return some(peer)
|
return some(peer)
|
||||||
|
|
||||||
|
proc connectToNode*(node: EthereumNode, n: Node) {.async.} =
|
||||||
|
await node.peerPool.connectToNode(n)
|
||||||
|
|
||||||
|
proc connectToNode*(node: EthereumNode, n: ENode) {.async.} =
|
||||||
|
await node.peerPool.connectToNode(n)
|
||||||
|
|
||||||
|
func numPeers*(node: EthereumNode): int =
|
||||||
|
node.peerPool.numPeers
|
||||||
|
|
||||||
|
func hasPeer*(node: EthereumNode, n: ENode): bool =
|
||||||
|
n in node.peerPool
|
||||||
|
|
||||||
|
func hasPeer*(node: EthereumNode, n: Node): bool =
|
||||||
|
n in node.peerPool
|
||||||
|
|
||||||
|
func hasPeer*(node: EthereumNode, n: Peer): bool =
|
||||||
|
n in node.peerPool
|
||||||
|
|
|
@ -14,7 +14,7 @@ import
|
||||||
std/[os, tables, times, random, sequtils, options],
|
std/[os, tables, times, random, sequtils, options],
|
||||||
chronos, chronicles,
|
chronos, chronicles,
|
||||||
".."/[rlp, keys, common],
|
".."/[rlp, keys, common],
|
||||||
./private/p2p_types, "."/[discovery, kademlia, rlpx]
|
./private/p2p_types, "."/[discovery, kademlia, rlpx, enode]
|
||||||
|
|
||||||
const
|
const
|
||||||
lookupInterval = 5
|
lookupInterval = 5
|
||||||
|
@ -36,7 +36,7 @@ proc newPeerPool*(
|
||||||
proc nodesToConnect(p: PeerPool): seq[Node] =
|
proc nodesToConnect(p: PeerPool): seq[Node] =
|
||||||
p.discovery.randomNodes(p.minPeers).filterIt(it notin p.discovery.bootstrapNodes)
|
p.discovery.randomNodes(p.minPeers).filterIt(it notin p.discovery.bootstrapNodes)
|
||||||
|
|
||||||
proc addObserver(p: PeerPool, observerId: int, observer: PeerObserver) =
|
proc addObserver*(p: PeerPool, observerId: int, observer: PeerObserver) =
|
||||||
doAssert(observerId notin p.observers)
|
doAssert(observerId notin p.observers)
|
||||||
p.observers[observerId] = observer
|
p.observers[observerId] = observer
|
||||||
if not observer.onPeerConnected.isNil:
|
if not observer.onPeerConnected.isNil:
|
||||||
|
@ -44,7 +44,7 @@ proc addObserver(p: PeerPool, observerId: int, observer: PeerObserver) =
|
||||||
if observer.protocol.isNil or peer.supports(observer.protocol):
|
if observer.protocol.isNil or peer.supports(observer.protocol):
|
||||||
observer.onPeerConnected(peer)
|
observer.onPeerConnected(peer)
|
||||||
|
|
||||||
proc delObserver(p: PeerPool, observerId: int) =
|
proc delObserver*(p: PeerPool, observerId: int) =
|
||||||
p.observers.del(observerId)
|
p.observers.del(observerId)
|
||||||
|
|
||||||
proc addObserver*(p: PeerPool, observerId: ref, observer: PeerObserver) =
|
proc addObserver*(p: PeerPool, observerId: ref, observer: PeerObserver) =
|
||||||
|
@ -123,9 +123,12 @@ proc connectToNode*(p: PeerPool, n: Node) {.async.} =
|
||||||
trace "Connection established (outgoing)", peer
|
trace "Connection established (outgoing)", peer
|
||||||
p.addPeer(peer)
|
p.addPeer(peer)
|
||||||
|
|
||||||
|
proc connectToNode*(p: PeerPool, n: ENode) {.async.} =
|
||||||
|
await p.connectToNode(newNode(n))
|
||||||
|
|
||||||
proc connectToNodes(p: PeerPool, nodes: seq[Node]) {.async.} =
|
proc connectToNodes(p: PeerPool, nodes: seq[Node]) {.async.} =
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
discard p.connectToNode(node)
|
await p.connectToNode(node)
|
||||||
|
|
||||||
# # TODO: Consider changing connect() to raise an exception instead of
|
# # TODO: Consider changing connect() to raise an exception instead of
|
||||||
# # returning None, as discussed in
|
# # returning None, as discussed in
|
||||||
|
@ -213,3 +216,16 @@ iterator peers*(p: PeerPool, Protocol: type): Peer =
|
||||||
if peer.supports(Protocol):
|
if peer.supports(Protocol):
|
||||||
yield peer
|
yield peer
|
||||||
|
|
||||||
|
func numPeers*(p: PeerPool): int =
|
||||||
|
p.connectedNodes.len
|
||||||
|
|
||||||
|
func contains*(p: PeerPool, n: ENode): bool =
|
||||||
|
for remote, _ in p.connectedNodes:
|
||||||
|
if remote.node == n:
|
||||||
|
return true
|
||||||
|
|
||||||
|
func contains*(p: PeerPool, n: Node): bool =
|
||||||
|
n in p.connectedNodes
|
||||||
|
|
||||||
|
func contains*(p: PeerPool, n: Peer): bool =
|
||||||
|
n.remote in p.connectedNodes
|
||||||
|
|
Loading…
Reference in New Issue