diff --git a/fluffy/common/common_utils.nim b/fluffy/common/common_utils.nim index 33c810c55..f7443c44a 100644 --- a/fluffy/common/common_utils.nim +++ b/fluffy/common/common_utils.nim @@ -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) diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index f22d546bb..7ee4120fc 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -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,