mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-09 13:56:23 +00:00
Use setupAddress for better IP and ports configuration
This commit is contained in:
parent
d47f53cd9d
commit
5f750f84b4
@ -5,7 +5,7 @@ import
|
|||||||
chronicles, chronicles/options as chroniclesOptions,
|
chronicles, chronicles/options as chroniclesOptions,
|
||||||
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
|
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
|
||||||
stew/io2, unicodedb/properties, normalize,
|
stew/io2, unicodedb/properties, normalize,
|
||||||
eth/common/eth_types as commonEthTypes,
|
eth/common/eth_types as commonEthTypes, eth/net/nat,
|
||||||
eth/p2p/discoveryv5/enr,
|
eth/p2p/discoveryv5/enr,
|
||||||
json_serialization, web3/[ethtypes, confutils_defs],
|
json_serialization, web3/[ethtypes, confutils_defs],
|
||||||
spec/[crypto, keystore, digest, datatypes, network],
|
spec/[crypto, keystore, digest, datatypes, network],
|
||||||
@ -170,7 +170,8 @@ type
|
|||||||
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>"
|
||||||
defaultValue: "any" }: string
|
defaultValue: NatConfig(hasExtIp: false, nat: NatAny)
|
||||||
|
name: "nat" .}: NatConfig
|
||||||
|
|
||||||
enrAutoUpdate* {.
|
enrAutoUpdate* {.
|
||||||
defaultValue: false
|
defaultValue: false
|
||||||
|
@ -75,7 +75,7 @@ proc loadBootstrapFile*(bootstrapFile: string,
|
|||||||
|
|
||||||
proc new*(T: type Eth2DiscoveryProtocol,
|
proc new*(T: type Eth2DiscoveryProtocol,
|
||||||
config: BeaconNodeConf,
|
config: BeaconNodeConf,
|
||||||
ip: Option[ValidIpAddress], tcpPort, udpPort: Port,
|
enrIp: Option[ValidIpAddress], enrTcpPort, enrUdpPort: Option[Port],
|
||||||
pk: PrivateKey,
|
pk: PrivateKey,
|
||||||
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
|
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
|
||||||
T {.raises: [Exception, Defect].} =
|
T {.raises: [Exception, Defect].} =
|
||||||
@ -92,6 +92,6 @@ proc new*(T: type Eth2DiscoveryProtocol,
|
|||||||
if fileExists(persistentBootstrapFile):
|
if fileExists(persistentBootstrapFile):
|
||||||
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
||||||
|
|
||||||
newProtocol(pk, ip, tcpPort, udpPort, enrFields, bootstrapEnrs,
|
newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields, bootstrapEnrs,
|
||||||
bindIp = config.listenAddress, enrAutoUpdate = config.enrAutoUpdate,
|
bindPort = config.udpPort, bindIp = config.listenAddress,
|
||||||
rng = rng)
|
enrAutoUpdate = config.enrAutoUpdate, rng = rng)
|
||||||
|
@ -1062,7 +1062,7 @@ proc onConnEvent(node: Eth2Node, peerId: PeerID, event: ConnEvent) {.async.} =
|
|||||||
|
|
||||||
proc new*(T: type Eth2Node, config: BeaconNodeConf, enrForkId: ENRForkID,
|
proc new*(T: type Eth2Node, config: BeaconNodeConf, enrForkId: ENRForkID,
|
||||||
switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
|
switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
|
||||||
tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool,
|
tcpPort, udpPort: Option[Port], privKey: keys.PrivateKey, discovery: bool,
|
||||||
rng: ref BrHmacDrbgContext): T =
|
rng: ref BrHmacDrbgContext): T =
|
||||||
let
|
let
|
||||||
metadata = getPersistentNetMetadata(config)
|
metadata = getPersistentNetMetadata(config)
|
||||||
@ -1315,49 +1315,6 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend =
|
|||||||
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
|
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
|
||||||
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
|
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
|
||||||
|
|
||||||
proc setupNat(config: BeaconNodeConf): tuple[ip: Option[ValidIpAddress],
|
|
||||||
tcpPort: Port,
|
|
||||||
udpPort: Port] =
|
|
||||||
# defaults
|
|
||||||
result.tcpPort = config.tcpPort
|
|
||||||
result.udpPort = config.udpPort
|
|
||||||
|
|
||||||
var nat: NatStrategy
|
|
||||||
case config.nat.toLowerAscii:
|
|
||||||
of "any":
|
|
||||||
nat = NatAny
|
|
||||||
of "none":
|
|
||||||
nat = NatNone
|
|
||||||
of "upnp":
|
|
||||||
nat = NatUpnp
|
|
||||||
of "pmp":
|
|
||||||
nat = NatPmp
|
|
||||||
else:
|
|
||||||
if config.nat.startsWith("extip:"):
|
|
||||||
try:
|
|
||||||
# any required port redirection is assumed to be done by hand
|
|
||||||
result.ip = some(ValidIpAddress.init(config.nat[6..^1]))
|
|
||||||
nat = NatNone
|
|
||||||
except ValueError:
|
|
||||||
error "nor a valid IP address", address = config.nat[6..^1]
|
|
||||||
quit QuitFailure
|
|
||||||
else:
|
|
||||||
error "not a valid NAT mechanism", value = config.nat
|
|
||||||
quit QuitFailure
|
|
||||||
|
|
||||||
if nat != NatNone:
|
|
||||||
let extIp = getExternalIP(nat)
|
|
||||||
if extIP.isSome:
|
|
||||||
result.ip = some(ValidIpAddress.init extIp.get)
|
|
||||||
# TODO redirectPorts in considered a gcsafety violation
|
|
||||||
# because it obtains the address of a non-gcsafe proc?
|
|
||||||
let extPorts = ({.gcsafe.}:
|
|
||||||
redirectPorts(tcpPort = result.tcpPort,
|
|
||||||
udpPort = result.udpPort,
|
|
||||||
description = clientId))
|
|
||||||
if extPorts.isSome:
|
|
||||||
(result.tcpPort, result.udpPort) = extPorts.get()
|
|
||||||
|
|
||||||
func asLibp2pKey*(key: keys.PublicKey): PublicKey =
|
func asLibp2pKey*(key: keys.PublicKey): PublicKey =
|
||||||
PublicKey(scheme: Secp256k1, skkey: secp.SkPublicKey(key))
|
PublicKey(scheme: Secp256k1, skkey: secp.SkPublicKey(key))
|
||||||
|
|
||||||
@ -1540,10 +1497,11 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
|
|||||||
netKeys: KeyPair,
|
netKeys: KeyPair,
|
||||||
enrForkId: ENRForkID): Eth2Node =
|
enrForkId: ENRForkID): Eth2Node =
|
||||||
var
|
var
|
||||||
(extIp, extTcpPort, extUdpPort) = setupNat(config)
|
(extIp, extTcpPort, extUdpPort) = setupAddress(config.nat,
|
||||||
|
config.listenAddress, config.tcpPort, config.udpPort, clientId)
|
||||||
hostAddress = tcpEndPoint(config.listenAddress, config.tcpPort)
|
hostAddress = tcpEndPoint(config.listenAddress, config.tcpPort)
|
||||||
announcedAddresses = if extIp.isNone(): @[]
|
announcedAddresses = if extIp.isNone() or extTcpPort.isNone(): @[]
|
||||||
else: @[tcpEndPoint(extIp.get(), extTcpPort)]
|
else: @[tcpEndPoint(extIp.get(), extTcpPort.get())]
|
||||||
|
|
||||||
debug "Initializing networking", hostAddress,
|
debug "Initializing networking", hostAddress,
|
||||||
network_public_key = netKeys.pubkey,
|
network_public_key = netKeys.pubkey,
|
||||||
|
@ -17,7 +17,7 @@ import
|
|||||||
chronicles, bearssl, blscurve,
|
chronicles, bearssl, blscurve,
|
||||||
json_serialization/std/[options, sets, net], serialization/errors,
|
json_serialization/std/[options, sets, net], serialization/errors,
|
||||||
|
|
||||||
eth/[keys, async_utils],
|
eth/[keys, async_utils], eth/net/nat,
|
||||||
eth/db/[kvstore, kvstore_sqlite3],
|
eth/db/[kvstore, kvstore_sqlite3],
|
||||||
eth/p2p/enode, eth/p2p/discoveryv5/[protocol, enr, random2],
|
eth/p2p/enode, eth/p2p/discoveryv5/[protocol, enr, random2],
|
||||||
|
|
||||||
|
@ -412,9 +412,11 @@ proc bootstrapDiscovery(conf: InspectorConf,
|
|||||||
if enrFields.isSome():
|
if enrFields.isSome():
|
||||||
let fields = enrFields.get()
|
let fields = enrFields.get()
|
||||||
let pairs = {"eth2": fields.eth2, "attnets": fields.attnets}
|
let pairs = {"eth2": fields.eth2, "attnets": fields.attnets}
|
||||||
result = newProtocol(pk, host, tcpPort, udpPort, pairs, bootnodes)
|
result = newProtocol(pk, host, some(tcpPort), some(udpPort), pairs,
|
||||||
|
bootnodes, bindPort = udpPort)
|
||||||
else:
|
else:
|
||||||
result = newProtocol(pk, host, tcpPort, udpPort, [], bootnodes)
|
result = newProtocol(pk, host, some(tcpPort), some(udpPort), [],
|
||||||
|
bootnodes, bindPort = udpPort)
|
||||||
result.open()
|
result.open()
|
||||||
result.start()
|
result.start()
|
||||||
|
|
||||||
|
2
vendor/nim-chronos
vendored
2
vendor/nim-chronos
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 25688cd0aa51f5e28c1e55dca1b3a9a73cb3dafa
|
Subproject commit 03707426e43d03cccc1de2e7284de168b79f7bf6
|
2
vendor/nim-eth
vendored
2
vendor/nim-eth
vendored
@ -1 +1 @@
|
|||||||
Subproject commit e8c9691b35f4875d9a97193cb965fbf1a956dd7e
|
Subproject commit be5e088b21e06a85cac4826454412db8459ed4f1
|
Loading…
x
Reference in New Issue
Block a user