Load and use the stored ENR to allow incr of seqNum on updates (#1763)
This commit is contained in:
parent
a07247b547
commit
dd074d4df8
|
@ -50,8 +50,8 @@ proc loadBootstrapFile*(bootstrapFile: string,
|
||||||
# However that would require the pull the keystore.nim and parts of
|
# However that would require the pull the keystore.nim and parts of
|
||||||
# keystore_management.nim out of nimbus-eth2.
|
# keystore_management.nim out of nimbus-eth2.
|
||||||
proc getPersistentNetKey*(
|
proc getPersistentNetKey*(
|
||||||
rng: var HmacDrbgContext, keyFilePath: string, dataDir: string):
|
rng: var HmacDrbgContext, keyFilePath: string):
|
||||||
PrivateKey =
|
tuple[key: PrivateKey, newNetKey: bool] =
|
||||||
logScope:
|
logScope:
|
||||||
key_file = keyFilePath
|
key_file = keyFilePath
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ proc getPersistentNetKey*(
|
||||||
let netKey = PrivateKey.fromHex(netKeyInHex)
|
let netKey = PrivateKey.fromHex(netKeyInHex)
|
||||||
if netKey.isOk():
|
if netKey.isOk():
|
||||||
info "Network key was successfully read"
|
info "Network key was successfully read"
|
||||||
netKey.get()
|
(netKey.get(), false)
|
||||||
else:
|
else:
|
||||||
fatal "Invalid private key from file", error = netKey.error
|
fatal "Invalid private key from file", error = netKey.error
|
||||||
quit QuitFailure
|
quit QuitFailure
|
||||||
|
@ -88,4 +88,32 @@ proc getPersistentNetKey*(
|
||||||
|
|
||||||
info "New network key file was created"
|
info "New network key file was created"
|
||||||
|
|
||||||
key
|
(key, true)
|
||||||
|
|
||||||
|
proc getPersistentEnr*(enrFilePath: string): Opt[enr.Record] =
|
||||||
|
logScope:
|
||||||
|
enr_file = enrFilePath
|
||||||
|
|
||||||
|
if fileAccessible(enrFilePath, {AccessFlags.Find}):
|
||||||
|
info "ENR file is present, reading ENR"
|
||||||
|
|
||||||
|
let readResult = readAllChars(enrFilePath)
|
||||||
|
if readResult.isErr():
|
||||||
|
warn "Could not load ENR file",
|
||||||
|
error = ioErrorMsg(readResult.error)
|
||||||
|
return Opt.none(enr.Record)
|
||||||
|
|
||||||
|
let enrUri = readResult.get()
|
||||||
|
|
||||||
|
var record: enr.Record
|
||||||
|
# TODO: This old API of var passing is very error prone and should be
|
||||||
|
# changed in nim-eth.
|
||||||
|
if not record.fromURI(enrUri):
|
||||||
|
warn "Could not decode ENR from ENR file"
|
||||||
|
return Opt.none(enr.Record)
|
||||||
|
else:
|
||||||
|
return Opt.some(record)
|
||||||
|
|
||||||
|
else:
|
||||||
|
warn "Could not find ENR file. Was it manually deleted?"
|
||||||
|
return Opt.none(enr.Record)
|
||||||
|
|
|
@ -118,15 +118,22 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
|
||||||
# TODO: allow for no TCP port mapping!
|
# TODO: allow for no TCP port mapping!
|
||||||
(extIp, _, extUdpPort) =
|
(extIp, _, extUdpPort) =
|
||||||
try: setupAddress(config.nat,
|
try: setupAddress(config.nat,
|
||||||
config.listenAddress, udpPort, udpPort, "dcli")
|
config.listenAddress, udpPort, udpPort, "fluffy")
|
||||||
except CatchableError as exc: raise exc
|
except CatchableError as exc: raise exc
|
||||||
# TODO: Ideally we don't have the Exception here
|
# TODO: Ideally we don't have the Exception here
|
||||||
except Exception as exc: raiseAssert exc.msg
|
except Exception as exc: raiseAssert exc.msg
|
||||||
netkey =
|
(netkey, newNetKey) =
|
||||||
if config.networkKey.isSome():
|
if config.networkKey.isSome():
|
||||||
config.networkKey.get()
|
(config.networkKey.get(), true)
|
||||||
else:
|
else:
|
||||||
getPersistentNetKey(rng[], config.networkKeyFile, config.dataDir.string)
|
getPersistentNetKey(rng[], config.networkKeyFile)
|
||||||
|
|
||||||
|
enrFilePath = config.dataDir / "fluffy_node.enr"
|
||||||
|
previousEnr =
|
||||||
|
if not newNetKey:
|
||||||
|
getPersistentEnr(enrFilePath)
|
||||||
|
else:
|
||||||
|
Opt.none(enr.Record)
|
||||||
|
|
||||||
var bootstrapRecords: seq[Record]
|
var bootstrapRecords: seq[Record]
|
||||||
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapRecords)
|
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapRecords)
|
||||||
|
@ -152,6 +159,11 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
|
||||||
# Might make this into a, default off, cli option.
|
# Might make this into a, default off, cli option.
|
||||||
localEnrFields = {"c": enrClientInfoShort},
|
localEnrFields = {"c": enrClientInfoShort},
|
||||||
bootstrapRecords = bootstrapRecords,
|
bootstrapRecords = bootstrapRecords,
|
||||||
|
previousRecord = # TODO: discv5/enr code still uses Option, to be changed.
|
||||||
|
if previousEnr.isSome():
|
||||||
|
some(previousEnr.get())
|
||||||
|
else:
|
||||||
|
none(enr.Record),
|
||||||
bindIp = bindIp, bindPort = udpPort,
|
bindIp = bindIp, bindPort = udpPort,
|
||||||
enrAutoUpdate = config.enrAutoUpdate,
|
enrAutoUpdate = config.enrAutoUpdate,
|
||||||
config = discoveryConfig,
|
config = discoveryConfig,
|
||||||
|
|
Loading…
Reference in New Issue