Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use correct exception in parseCmdArg #614

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions tools/dcli.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Copyright (c) 2020-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

Copy link
Member

@arnetheduck arnetheduck Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably have slapped push raises: [] on top here

import
std/[options, strutils, tables, sets],
confutils, confutils/std/net, chronicles, chronicles/topics_registry,
Expand Down Expand Up @@ -104,35 +112,35 @@ func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress =
func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress =
(static ValidIpAddress.init("127.0.0.1"))

proc parseCmdArg*(T: type enr.Record, p: string): T =
proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} =
if not fromURI(result, p):
raise newException(ConfigurationError, "Invalid ENR")
raise newException(ValueError, "Invalid ENR")

proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
return @[]

proc parseCmdArg*(T: type Node, p: string): T =
proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} =
var record: enr.Record
if not fromURI(record, p):
raise newException(ConfigurationError, "Invalid ENR")
raise newException(ValueError, "Invalid ENR")

let n = newNode(record)
if n.isErr:
raise newException(ConfigurationError, $n.error)
raise newException(ValueError, $n.error)

if n[].address.isNone():
raise newException(ConfigurationError, "ENR without address")
raise newException(ValueError, "ENR without address")

n[]

proc completeCmdArg*(T: type Node, val: string): seq[string] =
return @[]

proc parseCmdArg*(T: type PrivateKey, p: string): T =
proc parseCmdArg*(T: type PrivateKey, p: string): T {.raises: [ValueError].} =
try:
result = PrivateKey.fromHex(string(p)).tryGet()
except CatchableError:
raise newException(ConfigurationError, "Invalid private key")
raise newException(ValueError, "Invalid private key")

proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] =
return @[]
Expand Down Expand Up @@ -168,7 +176,7 @@ proc discover(d: discv5_protocol.Protocol, psFile: string) {.async.} =
bits.inc(countOnes(byt.uint))

let str = "$#,$#,$#,$#,$#,$#\n"
let newLine = str % [pubkey.get().toHex, dNode.id.toHex, forkDigest[0..3].toHex, $dNode.address.get(), attnets.get().toHex, $bits]
let newLine = str % [pubkey.get().toHex, dNode.id.toHex, forkDigest[0..3].toHex, $dNode.address.get(), attnets.get().toHex, $bits]

ps.write(newLine)
await sleepAsync(1000) # 1 sec of delay
Expand Down
Loading