Merge branch 'discv5-ip-checks' into bump

This commit is contained in:
Ștefan Talpalaru 2020-04-01 22:48:17 +02:00
commit 3ccb2dcb88
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
4 changed files with 21 additions and 29 deletions

View File

@ -1273,15 +1273,12 @@ when isMainModule:
if bootstrapFile.len > 0: if bootstrapFile.len > 0:
let let
networkKeys = getPersistentNetKeys(config) networkKeys = getPersistentNetKeys(config)
bootstrapAddress = enode.Address(
ip: config.bootstrapAddress,
tcpPort: config.bootstrapPort,
udpPort: config.bootstrapPort)
bootstrapEnr = enr.Record.init( bootstrapEnr = enr.Record.init(
1, # sequence number 1, # sequence number
networkKeys.seckey.asEthKey, networkKeys.seckey.asEthKey,
some(bootstrapAddress)) some(config.bootstrapAddress),
config.bootstrapPort,
config.bootstrapPort)
writeFile(bootstrapFile, bootstrapEnr.toURI) writeFile(bootstrapFile, bootstrapEnr.toURI)
echo "Wrote ", bootstrapFile echo "Wrote ", bootstrapFile

View File

@ -154,7 +154,8 @@ proc loadBootstrapFile*(bootstrapFile: string,
proc new*(T: type Eth2DiscoveryProtocol, proc new*(T: type Eth2DiscoveryProtocol,
conf: BeaconNodeConf, conf: BeaconNodeConf,
ip: IpAddress, rawPrivKeyBytes: openarray[byte]): T = ip: Option[IpAddress], tcpPort, udpPort: Port,
rawPrivKeyBytes: openarray[byte]): T =
# TODO # TODO
# Implement more configuration options: # Implement more configuration options:
# * for setting up a specific key # * for setting up a specific key
@ -174,4 +175,4 @@ proc new*(T: type Eth2DiscoveryProtocol,
if fileExists(persistentBootstrapFile): if fileExists(persistentBootstrapFile):
loadBootstrapFile(persistentBootstrapFile, bootNodes, bootEnrs, ourPubKey) loadBootstrapFile(persistentBootstrapFile, bootNodes, bootEnrs, ourPubKey)
newProtocol(pk, db, ip, conf.tcpPort, conf.udpPort, bootEnrs) newProtocol(pk, db, ip, tcpPort, udpPort, bootEnrs)

View File

@ -134,9 +134,6 @@ const
readTimeoutErrorMsg = "Exceeded read timeout for a request" readTimeoutErrorMsg = "Exceeded read timeout for a request"
let
globalListeningAddr = parseIpAddress("0.0.0.0")
# Metrics for tracking attestation and beacon block loss # Metrics for tracking attestation and beacon block loss
declareCounter gossip_messages_sent, declareCounter gossip_messages_sent,
"Number of gossip messages sent by this peer" "Number of gossip messages sent by this peer"
@ -681,10 +678,11 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
await sleepAsync seconds(1) await sleepAsync seconds(1)
proc init*(T: type Eth2Node, conf: BeaconNodeConf, proc init*(T: type Eth2Node, conf: BeaconNodeConf,
switch: Switch, ip: IpAddress, privKey: keys.PrivateKey): T = switch: Switch, ip: Option[IpAddress], tcpPort, udpPort: Port,
privKey: keys.PrivateKey): T =
new result new result
result.switch = switch result.switch = switch
result.discovery = Eth2DiscoveryProtocol.new(conf, ip, privKey.data) result.discovery = Eth2DiscoveryProtocol.new(conf, ip, tcpPort, udpPort, privKey.data)
result.wantedPeers = conf.maxPeers result.wantedPeers = conf.maxPeers
result.peerPool = newPeerPool[Peer, PeerID](maxPeers = conf.maxPeers) result.peerPool = newPeerPool[Peer, PeerID](maxPeers = conf.maxPeers)
@ -829,11 +827,10 @@ 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(conf: BeaconNodeConf): tuple[ip: IpAddress, proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[IpAddress],
tcpPort: Port, tcpPort: Port,
udpPort: Port] {.gcsafe.} = udpPort: Port] {.gcsafe.} =
# defaults # defaults
result.ip = globalListeningAddr
result.tcpPort = conf.tcpPort result.tcpPort = conf.tcpPort
result.udpPort = conf.udpPort result.udpPort = conf.udpPort
@ -850,16 +847,15 @@ proc setupNat(conf: BeaconNodeConf): tuple[ip: IpAddress,
else: else:
if conf.nat.startsWith("extip:") and isIpAddress(conf.nat[6..^1]): if conf.nat.startsWith("extip:") and isIpAddress(conf.nat[6..^1]):
# any required port redirection is assumed to be done by hand # any required port redirection is assumed to be done by hand
result.ip = parseIpAddress(conf.nat[6..^1]) result.ip = some(parseIpAddress(conf.nat[6..^1]))
nat = NatNone nat = NatNone
else: else:
error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat
quit(QuitFailure) quit(QuitFailure)
if nat != NatNone: if nat != NatNone:
let extIP = getExternalIP(nat) result.ip = getExternalIP(nat)
if extIP.isSome: if result.ip.isSome:
result.ip = extIP.get()
# TODO redirectPorts in considered a gcsafety violation # TODO redirectPorts in considered a gcsafety violation
# because it obtains the address of a non-gcsafe proc? # because it obtains the address of a non-gcsafe proc?
let extPorts = ({.gcsafe.}: let extPorts = ({.gcsafe.}:
@ -901,10 +897,10 @@ proc getPersistentNetKeys*(conf: BeaconNodeConf): KeyPair =
proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async, gcsafe.} = proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async, gcsafe.} =
var var
(extIp, extTcpPort, _) = setupNat(conf) (extIp, extTcpPort, extUdpPort) = setupNat(conf)
hostAddress = tcpEndPoint(conf.libp2pAddress, conf.tcpPort) hostAddress = tcpEndPoint(conf.libp2pAddress, conf.tcpPort)
announcedAddresses = if extIp == globalListeningAddr: @[] announcedAddresses = if extIp.isNone(): @[]
else: @[tcpEndPoint(extIp, extTcpPort)] else: @[tcpEndPoint(extIp.get(), extTcpPort)]
info "Initializing networking", hostAddress, info "Initializing networking", hostAddress,
announcedAddresses announcedAddresses
@ -915,17 +911,15 @@ proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async, gcsafe.} =
# are running behind a NAT). # are running behind a NAT).
var switch = newStandardSwitch(some keys.seckey, hostAddress, var switch = newStandardSwitch(some keys.seckey, hostAddress,
triggerSelf = true, gossip = true) triggerSelf = true, gossip = true)
result = Eth2Node.init(conf, switch, extIp, keys.seckey.asEthKey) result = Eth2Node.init(conf, switch, extIp, extTcpPort, extUdpPort,
keys.seckey.asEthKey)
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf, proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
ip: IpAddress, port: Port): enr.Record = ip: IpAddress, port: Port): enr.Record =
let let pair = getPersistentNetKeys(conf)
pair = getPersistentNetKeys(conf)
enodeAddress = Address(ip: ip, udpPort: port)
return enr.Record.init(1'u64, # sequence number return enr.Record.init(1'u64, # sequence number
pair.seckey.asEthKey, pair.seckey.asEthKey,
some(enodeAddress)) some(ip), port, port)
proc announcedENR*(node: Eth2Node): enr.Record = proc announcedENR*(node: Eth2Node): enr.Record =
doAssert node.discovery != nil, "The Eth2Node must be initialized" doAssert node.discovery != nil, "The Eth2Node must be initialized"

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit c3f23e5912efff98fc6c8181db579037e5a19a2c Subproject commit fe6df94a1956509e77ff533d9d00dd35b403ea22