Load and use the stored ENR to allow incr of seqNum on updates (#1763)

This commit is contained in:
Kim De Mey 2023-09-25 21:08:10 +02:00 committed by GitHub
parent a07247b547
commit dd074d4df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 8 deletions

View File

@ -50,8 +50,8 @@ proc loadBootstrapFile*(bootstrapFile: string,
# However that would require the pull the keystore.nim and parts of
# keystore_management.nim out of nimbus-eth2.
proc getPersistentNetKey*(
rng: var HmacDrbgContext, keyFilePath: string, dataDir: string):
PrivateKey =
rng: var HmacDrbgContext, keyFilePath: string):
tuple[key: PrivateKey, newNetKey: bool] =
logScope:
key_file = keyFilePath
@ -69,7 +69,7 @@ proc getPersistentNetKey*(
let netKey = PrivateKey.fromHex(netKeyInHex)
if netKey.isOk():
info "Network key was successfully read"
netKey.get()
(netKey.get(), false)
else:
fatal "Invalid private key from file", error = netKey.error
quit QuitFailure
@ -88,4 +88,32 @@ proc getPersistentNetKey*(
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)

View File

@ -118,15 +118,22 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
# TODO: allow for no TCP port mapping!
(extIp, _, extUdpPort) =
try: setupAddress(config.nat,
config.listenAddress, udpPort, udpPort, "dcli")
config.listenAddress, udpPort, udpPort, "fluffy")
except CatchableError as exc: raise exc
# TODO: Ideally we don't have the Exception here
except Exception as exc: raiseAssert exc.msg
netkey =
(netkey, newNetKey) =
if config.networkKey.isSome():
config.networkKey.get()
(config.networkKey.get(), true)
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]
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapRecords)
@ -152,6 +159,11 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
# Might make this into a, default off, cli option.
localEnrFields = {"c": enrClientInfoShort},
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,
enrAutoUpdate = config.enrAutoUpdate,
config = discoveryConfig,