Bump nim-eth for ENR changes and related clean-up (#2422)

This commit is contained in:
Kim De Mey 2024-06-27 17:59:08 +02:00 committed by GitHub
parent 61bbf40014
commit ae094692fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 81 additions and 95 deletions

View File

@ -28,11 +28,11 @@ iterator strippedLines(filename: string): string {.raises: [ref IOError].} =
yield stripped
proc addBootstrapNode(bootstrapAddr: string, bootstrapEnrs: var seq[Record]) =
var enrRec: enr.Record
if enrRec.fromURI(bootstrapAddr):
bootstrapEnrs.add enrRec
let res = enr.Record.fromURI(bootstrapAddr)
if res.isOk():
bootstrapEnrs.add res.value
else:
warn "Ignoring invalid bootstrap ENR", bootstrapAddr
warn "Ignoring invalid bootstrap ENR", bootstrapAddr, error = $res.error
proc loadBootstrapFile*(bootstrapFile: string, bootstrapEnrs: var seq[Record]) =
if bootstrapFile.len == 0:
@ -109,11 +109,12 @@ proc getPersistentEnr*(enrFilePath: string): Opt[enr.Record] =
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):
let res = enr.Record.fromURI(enrUri)
if res.isErr():
warn "Could not decode ENR from ENR file"
return Opt.none(enr.Record)
Opt.none(enr.Record)
else:
return Opt.some(record)
Opt.some(res.value)
else:
warn "Could not find ENR file. Was it manually deleted?"
return Opt.none(enr.Record)
Opt.none(enr.Record)

View File

@ -316,25 +316,24 @@ func completeCmdArg*(T: type TrustedDigest, input: string): seq[string] =
return @[]
proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} =
if not fromURI(result, p):
raise newException(ValueError, "Invalid ENR")
let res = enr.Record.fromURI(p)
if res.isErr():
raise newException(ValueError, "Invalid ENR: " & $res.error)
res.value
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
return @[]
proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} =
var record: enr.Record
if not fromURI(record, p):
raise newException(ValueError, "Invalid ENR")
let res = enr.Record.fromURI(p)
if res.isErr():
raise newException(ValueError, "Invalid ENR: " & $res.error)
let n = newNode(record)
if n.isErr:
raise newException(ValueError, $n.error)
if n[].address.isNone():
let n = Node.fromRecord(res.value)
if n.address.isNone():
raise newException(ValueError, "ENR without address")
n[]
n
proc completeCmdArg*(T: type Node, val: string): seq[string] =
return @[]

View File

@ -128,14 +128,14 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
discard # don't connect to any network bootstrap nodes
of PortalNetwork.mainnet:
for enrURI in mainnetBootstrapNodes:
var record: Record
if fromURI(record, enrURI):
bootstrapRecords.add(record)
let res = enr.Record.fromURI(enrURI)
if res.isOk():
bootstrapRecords.add(res.value)
of PortalNetwork.angelfood:
for enrURI in angelfoodBootstrapNodes:
var record: Record
if fromURI(record, enrURI):
bootstrapRecords.add(record)
let res = enr.Record.fromURI(enrURI)
if res.isOk():
bootstrapRecords.add(res.value)
let
discoveryConfig =

View File

@ -296,11 +296,7 @@ proc addNode*(p: PortalProtocol, node: Node): NodeStatus =
p.routingTable.addNode(node)
proc addNode*(p: PortalProtocol, r: Record): bool =
let node = newNode(r)
if node.isOk():
p.addNode(node[]) == Added
else:
false
p.addNode(Node.fromRecord(r)) == Added
func getNode*(p: PortalProtocol, id: NodeId): Opt[Node] =
p.routingTable.getNode(id)
@ -682,14 +678,13 @@ proc offerImpl*(
proc recordsFromBytes*(rawRecords: List[ByteList, 32]): PortalResult[seq[Record]] =
var records: seq[Record]
for r in rawRecords.asSeq():
var record: Record
if record.fromBytes(r.asSeq()):
records.add(record)
else:
let record = enr.Record.fromBytes(r.asSeq()).valueOr:
# If any of the ENRs is invalid, fail immediatly. This is similar as what
# is done on the discovery v5 layer.
return err("Deserialization of an ENR failed")
records.add(record)
ok(records)
proc ping*(

View File

@ -49,25 +49,20 @@ proc installDiscoveryApiHandlers*(
return getRoutingTableInfo(d.routingTable)
rpcServer.rpc("discv5_addEnr") do(enr: Record) -> bool:
let nodeRes = newNode(enr)
if nodeRes.isOk():
let node = nodeRes.get()
let res = d.addNode(node)
let node = Node.fromRecord(enr)
let res = d.addNode(node)
if res:
d.routingTable.setJustSeen(node)
return res
else:
raise newException(ValueError, "Failed creating Node from ENR")
return res
rpcServer.rpc("discv5_addEnrs") do(enrs: seq[Record]) -> bool:
# Note: unspecified RPC, but useful for our local testnet test
# TODO: We could also adjust the API of addNode & newNode to accept a seen
# TODO: We could also adjust the API of addNode & fromRecord to accept a seen
# parameter, but perhaps only if that makes sense on other locations in
# discv5/portal that are not testing/debug related.
for enr in enrs:
let nodeRes = newNode(enr)
if nodeRes.isOk():
let node = nodeRes.get()
discard d.addNode(node)
let node = Node.fromRecord(enr)
if d.addNode(node):
d.routingTable.setJustSeen(node)
return true

View File

@ -56,20 +56,17 @@ proc installPortalApiHandlers*(
return getRoutingTableInfo(p.routingTable)
rpcServer.rpc("portal_" & network & "AddEnr") do(enr: Record) -> bool:
let node = newNode(enr).valueOr:
raise newException(ValueError, "Failed creating Node from ENR")
let node = Node.fromRecord(enr)
let addResult = p.addNode(node)
p.routingTable.setJustSeen(node)
if addResult == Added:
p.routingTable.setJustSeen(node)
return addResult == Added
rpcServer.rpc("portal_" & network & "AddEnrs") do(enrs: seq[Record]) -> bool:
# Note: unspecified RPC, but useful for our local testnet test
for enr in enrs:
let nodeRes = newNode(enr)
if nodeRes.isOk():
let node = nodeRes.get()
discard p.addNode(node)
let node = Node.fromRecord(enr)
if p.addNode(node) == Added:
p.routingTable.setJustSeen(node)
return true

View File

@ -53,11 +53,7 @@ func getRoutingTableInfo*(r: RoutingTable): RoutingTableInfo =
info
func toNodeWithAddress*(enr: Record): Node {.raises: [ValueError].} =
let nodeRes = newNode(enr)
if nodeRes.isErr():
raise newException(ValueError, $nodeRes.error)
let node = nodeRes.get()
let node = Node.fromRecord(enr)
if node.address.isNone():
raise newException(ValueError, "ENR without address")
else:
@ -69,7 +65,7 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: Record) {.gcsafe, raises: [IOEr
proc readValue*(
r: var JsonReader[JrpcConv], val: var Record
) {.gcsafe, raises: [IOError, JsonReaderError].} =
if not fromURI(val, r.parseString()):
val = Record.fromURI(r.parseString()).valueOr:
r.raiseUnexpectedValue("Invalid ENR")
proc writeValue*(w: var JsonWriter[JrpcConv], v: NodeId) {.gcsafe, raises: [IOError].} =

View File

@ -93,16 +93,20 @@ suite "Portal Wire Protocol Message Encodings":
message.nodes.enrs.len() == 0
test "Nodes Response - enrs":
var e1, e2: Record
check:
e1.fromURI(
let
res1 = Record.fromURI(
"enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg"
)
e2.fromURI(
res2 = Record.fromURI(
"enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU"
)
check:
res1.isOk()
res2.isOk()
let
e1 = res1.value
e2 = res2.value
total = 0x1'u8
n = NodesMessage(
total: total, enrs: List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)])
@ -177,16 +181,20 @@ suite "Portal Wire Protocol Message Encodings":
message.content.content == content
test "Content Response - enrs":
var e1, e2: Record
check:
e1.fromURI(
let
res1 = Record.fromURI(
"enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg"
)
e2.fromURI(
res2 = Record.fromURI(
"enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU"
)
check:
res1.isOk()
res2.isOk()
let
e1 = res1.value
e2 = res2.value
enrs = List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)])
c = ContentMessage(contentMessageType: enrsType, enrs: enrs)

View File

@ -160,25 +160,25 @@ type
.}: Node
proc parseCmdArg*(T: type enr.Record, p: string): T =
if not fromURI(result, p):
raise newException(ValueError, "Invalid ENR")
let res = enr.Record.fromURI(p)
if res.isErr:
raise newException(ValueError, "Invalid ENR: " & $res.error)
res.value
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
return @[]
proc parseCmdArg*(T: type Node, p: string): T =
var record: enr.Record
if not fromURI(record, p):
raise newException(ValueError, "Invalid ENR")
let res = enr.Record.fromURI(p)
if res.isErr:
raise newException(ValueError, "Invalid ENR: " & $res.error)
let n = newNode(record)
if n.isErr:
raise newException(ValueError, $n.error)
if n[].address.isNone():
let n = Node.fromRecord(res.value)
if n.address.isNone():
raise newException(ValueError, "ENR without address")
n[]
n
proc completeCmdArg*(T: type Node, val: string): seq[string] =
return @[]

View File

@ -56,22 +56,17 @@ proc installUtpHandlers(
t: ref Table[SKey, UtpSocket[NodeAddress]],
) {.raises: [].} =
srv.rpc("utp_connect") do(r: enr.Record) -> SKey:
let nodeRes = newNode(r)
if nodeRes.isOk():
let node = nodeRes.get()
let nodeAddress = NodeAddress.init(node).unsafeGet()
discard d.addNode(node)
let connResult = await s.connectTo(nodeAddress)
if (connResult.isOk()):
let socket = connResult.get()
let sKey = socket.socketKey.toSKey()
t[sKey] = socket
return sKey
else:
raise newException(ValueError, "Connection to node Failed.")
let node = Node.fromRecord(r)
let nodeAddress = NodeAddress.init(node).unsafeGet()
discard d.addNode(node)
let connResult = await s.connectTo(nodeAddress)
if (connResult.isOk()):
let socket = connResult.get()
let sKey = socket.socketKey.toSKey()
t[sKey] = socket
return sKey
else:
raise newException(ValueError, "Bad enr")
raise newException(ValueError, "Connection to node Failed.")
srv.rpc("utp_write") do(k: SKey, b: string) -> bool:
let sock = t.getOrDefault(k)

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 26212c881b464ed64cac20442fb45144d3ecd3b3
Subproject commit 8088fe72d77702dad83bda31d54485376c63cb61