Replace ValidIpAddress by IpAddress

This commit is contained in:
Arnaud 2024-12-18 17:17:31 +01:00
parent 83b4943c17
commit daadffe30a
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
4 changed files with 33 additions and 19 deletions

View File

@ -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)):

View File

@ -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:

View File

@ -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,

View File

@ -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