From daadffe30a10d9509f98d807d385592297afa13d Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 18 Dec 2024 17:17:31 +0100 Subject: [PATCH] Replace ValidIpAddress by IpAddress --- codex.nim | 4 ++-- codex/conf.nim | 40 ++++++++++++++++++++++++++------------- codex/discovery.nim | 6 +++--- codex/utils/addrutils.nim | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/codex.nim b/codex.nim index 4ef01299..5faf33a0 100644 --- a/codex.nim +++ b/codex.nim @@ -55,11 +55,11 @@ when isMainModule: config.setupLogging() config.setupMetrics() - if config.nat == ValidIpAddress.init(IPv4_any()): + if config.nat == IPv4_any(): error "`--nat` cannot be set to the any (`0.0.0.0`) address" quit QuitFailure - if config.nat == ValidIpAddress.init("127.0.0.1"): + if config.nat == static parseIpAddress("127.0.0.1"): warn "`--nat` is set to loopback, your node wont properly announce over the DHT" if not(checkAndCreateDataDir((config.dataDir).string)): diff --git a/codex/conf.nim b/codex/conf.nim index d59b347e..d4a4ec5d 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -114,9 +114,9 @@ type metricsAddress* {. desc: "Listening address of the metrics server" - defaultValue: ValidIpAddress.init("127.0.0.1") + defaultValue: defaultAddress(config) defaultValueDesc: "127.0.0.1" - name: "metrics-address" }: ValidIpAddress + name: "metrics-address" }: IpAddress metricsPort* {. desc: "Listening HTTP port of the metrics server" @@ -142,17 +142,17 @@ type # TODO: change this once we integrate nat support nat* {. desc: "IP Addresses to announce behind a NAT" - defaultValue: ValidIpAddress.init("127.0.0.1") + defaultValue: defaultAddress(config) defaultValueDesc: "127.0.0.1" abbr: "a" - name: "nat" }: ValidIpAddress + name: "nat" }: IpAddress discoveryIp* {. desc: "Discovery listen address" - defaultValue: ValidIpAddress.init(IPv4_any()) + defaultValue: IPv4_any() defaultValueDesc: "0.0.0.0" abbr: "e" - name: "disc-ip" }: ValidIpAddress + name: "disc-ip" }: IpAddress discoveryPort* {. desc: "Discovery (UDP) port" @@ -413,6 +413,9 @@ type logutils.formatIt(LogFormat.textLines, EthAddress): it.short0xHexLog logutils.formatIt(LogFormat.json, EthAddress): %it +func defaultAddress*(conf: CodexConf): IpAddress = + result = static parseIpAddress("127.0.0.1") + func persistence*(self: CodexConf): bool = self.cmd == StartUpCmd.persistence @@ -445,13 +448,17 @@ const proc parseCmdArg*(T: typedesc[MultiAddress], input: string): MultiAddress - {.upraises: [ValueError, LPError].} = + {.upraises: [ValueError] .} = var ma: MultiAddress - let res = MultiAddress.init(input) - if res.isOk: - ma = res.get() - else: - warn "Invalid MultiAddress", input=input, error = res.error() + try: + let res = MultiAddress.init(input) + if res.isOk: + ma = res.get() + else: + warn "Invalid MultiAddress", input=input, error = res.error() + quit QuitFailure + except LPError as exc: + warn "Invalid MultiAddress uri", uri = input, error = exc.msg quit QuitFailure ma @@ -461,6 +468,9 @@ proc parseCmdArg*(T: type SignedPeerRecord, uri: string): T = if not res.fromURI(uri): warn "Invalid SignedPeerRecord uri", uri = uri quit QuitFailure + except LPError as exc: + warn "Invalid SignedPeerRecord uri", uri = uri, error = exc.msg + quit QuitFailure except CatchableError as exc: warn "Invalid SignedPeerRecord uri", uri = uri, error = exc.msg quit QuitFailure @@ -494,7 +504,11 @@ proc readValue*(r: var TomlReader, val: var SignedPeerRecord) = error "invalid SignedPeerRecord configuration value", error = err.msg quit QuitFailure - val = SignedPeerRecord.parseCmdArg(uri) + try: + val = SignedPeerRecord.parseCmdArg(uri) + except LPError as err: + warn "Invalid SignedPeerRecord uri", uri = uri, error = err.msg + quit QuitFailure proc readValue*(r: var TomlReader, val: var MultiAddress) = without input =? r.readValue(string).catch, err: diff --git a/codex/discovery.nim b/codex/discovery.nim index 74ed0f6f..d79aee75 100644 --- a/codex/discovery.nim +++ b/codex/discovery.nim @@ -147,7 +147,7 @@ proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) = d.protocol.updateRecord(d.providerRecord) .expect("Should update SPR") -proc updateDhtRecord*(d: Discovery, ip: ValidIpAddress, port: Port) = +proc updateDhtRecord*(d: Discovery, ip: IpAddress, port: Port) = ## Update providers record ## @@ -173,7 +173,7 @@ proc stop*(d: Discovery) {.async.} = proc new*( T: type Discovery, key: PrivateKey, - bindIp = ValidIpAddress.init(IPv4_any()), + bindIp = IPv4_any(), bindPort = 0.Port, announceAddrs: openArray[MultiAddress], bootstrapNodes: openArray[SignedPeerRecord] = [], @@ -203,7 +203,7 @@ proc new*( self.protocol = newProtocol( key, - bindIp = bindIp.toNormalIp, + bindIp = bindIp, bindPort = bindPort, record = self.providerRecord.get, bootstrapRecords = bootstrapNodes, diff --git a/codex/utils/addrutils.nim b/codex/utils/addrutils.nim index f044581a..0b2c56f0 100644 --- a/codex/utils/addrutils.nim +++ b/codex/utils/addrutils.nim @@ -18,7 +18,7 @@ import pkg/stew/shims/net func remapAddr*( address: MultiAddress, - ip: Option[ValidIpAddress] = ValidIpAddress.none, + ip: Option[IpAddress] = IpAddress.none, port: Option[Port] = Port.none ): MultiAddress = ## Remap addresses to new IP and/or Port