mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-18 18:42:35 +00:00
Wire-up the peer dialing loop and introduce --max-peers option
This commit is contained in:
parent
52878405b7
commit
07d329acb8
@ -105,6 +105,11 @@ type
|
|||||||
desc: "UDP listening port."
|
desc: "UDP listening port."
|
||||||
name: "udp-port" }: int
|
name: "udp-port" }: int
|
||||||
|
|
||||||
|
maxPeers* {.
|
||||||
|
defaultValue: 10
|
||||||
|
desc: "The maximum number of peers to connect to"
|
||||||
|
name: "max-peers" }: int
|
||||||
|
|
||||||
nat* {.
|
nat* {.
|
||||||
desc: "Specify method to use for determining public address. " &
|
desc: "Specify method to use for determining public address. " &
|
||||||
"Must be one of: any, none, upnp, pmp, extip:<IP>."
|
"Must be one of: any, none, upnp, pmp, extip:<IP>."
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import
|
import
|
||||||
algorithm, typetraits,
|
algorithm, typetraits, net,
|
||||||
stew/[varints,base58], stew/shims/[macros, tables], chronos, chronicles,
|
stew/[varints,base58], stew/shims/[macros, tables], chronos, chronicles,
|
||||||
faststreams/output_stream, serialization,
|
faststreams/output_stream, serialization,
|
||||||
json_serialization/std/options, eth/p2p/p2p_protocol_dsl,
|
json_serialization/std/options, eth/p2p/p2p_protocol_dsl,
|
||||||
@ -99,6 +99,9 @@ type
|
|||||||
|
|
||||||
TransmissionError* = object of CatchableError
|
TransmissionError* = object of CatchableError
|
||||||
|
|
||||||
|
const
|
||||||
|
TCP = net.Protocol.IPPROTO_TCP
|
||||||
|
|
||||||
template `$`*(peer: Peer): string = id(peer.info)
|
template `$`*(peer: Peer): string = id(peer.info)
|
||||||
chronicles.formatIt(Peer): $it
|
chronicles.formatIt(Peer): $it
|
||||||
|
|
||||||
@ -147,9 +150,41 @@ include eth/p2p/p2p_backends_helpers
|
|||||||
include eth/p2p/p2p_tracing
|
include eth/p2p/p2p_tracing
|
||||||
include libp2p_backends_common
|
include libp2p_backends_common
|
||||||
|
|
||||||
proc dialPeer*(node: Eth2Node, enr: enr.Record) {.async.} =
|
proc toPeerInfo*(r: enr.TypedRecord): PeerInfo =
|
||||||
|
if r.secp256k1.isSome:
|
||||||
|
var peerId = PeerID.init r.secp256k1.get
|
||||||
|
var addresses = newSeq[MultiAddress]()
|
||||||
|
|
||||||
|
if r.ip.isSome and r.tcp.isSome:
|
||||||
|
let ip = IpAddress(family: IpAddressFamily.IPv4,
|
||||||
|
address_v4: r.ip.get)
|
||||||
|
addresses.add MultiAddress.init(ip, TCP, Port r.tcp.get)
|
||||||
|
|
||||||
|
if r.ip6.isSome:
|
||||||
|
let ip = IpAddress(family: IpAddressFamily.IPv6,
|
||||||
|
address_v6: r.ip6.get)
|
||||||
|
if r.tcp6.isSome:
|
||||||
|
addresses.add MultiAddress.init(ip, TCP, Port r.tcp6.get)
|
||||||
|
elif r.tcp.isSome:
|
||||||
|
addresses.add MultiAddress.init(ip, TCP, Port r.tcp.get)
|
||||||
|
else:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
if addresses.len > 0:
|
||||||
|
return PeerInfo.init(peerId, addresses)
|
||||||
|
|
||||||
|
proc toPeerInfo(r: Option[enr.TypedRecord]): PeerInfo =
|
||||||
|
if r.isSome:
|
||||||
|
return r.get.toPeerInfo
|
||||||
|
|
||||||
|
proc dialPeer*(node: Eth2Node, enr: enr.Record) {.async.} =
|
||||||
|
let peerInfo = enr.toTypedRecord.toPeerInfo
|
||||||
|
if peerInfo != nil:
|
||||||
|
discard await node.switch.dial(peerInfo)
|
||||||
|
var peer = node.getPeer(peerInfo)
|
||||||
|
peer.wasDialed = true
|
||||||
|
await initializeConnection(peer)
|
||||||
|
|
||||||
proc runDiscoveryLoop(node: Eth2Node) {.async.} =
|
proc runDiscoveryLoop(node: Eth2Node) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
if node.peersByDiscoveryId.len < node.wantedPeers:
|
if node.peersByDiscoveryId.len < node.wantedPeers:
|
||||||
@ -167,6 +202,7 @@ proc init*(T: type Eth2Node, conf: BeaconNodeConf,
|
|||||||
result.switch = switch
|
result.switch = switch
|
||||||
result.peers = initTable[PeerID, Peer]()
|
result.peers = initTable[PeerID, Peer]()
|
||||||
result.discovery = Eth2DiscoveryProtocol.new(conf, privKey.getBytes)
|
result.discovery = Eth2DiscoveryProtocol.new(conf, privKey.getBytes)
|
||||||
|
result.wantedPeers = conf.maxPeers
|
||||||
|
|
||||||
newSeq result.protocolStates, allProtocols.len
|
newSeq result.protocolStates, allProtocols.len
|
||||||
for proto in allProtocols:
|
for proto in allProtocols:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user