From ae094692fbccbdfd10f2fd5145f0d6c74855f4fe Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Thu, 27 Jun 2024 17:59:08 +0200 Subject: [PATCH] Bump nim-eth for ENR changes and related clean-up (#2422) --- fluffy/common/common_utils.nim | 17 +++++++------ fluffy/conf.nim | 21 ++++++++-------- fluffy/fluffy.nim | 12 ++++----- fluffy/network/wire/portal_protocol.nim | 13 +++------- fluffy/rpc/rpc_discovery_api.nim | 19 ++++++-------- fluffy/rpc/rpc_portal_api.nim | 13 ++++------ fluffy/rpc/rpc_types.nim | 8 ++---- .../mainnet/test_portal_wire_encoding.nim | 24 ++++++++++++------ fluffy/tools/portalcli.nim | 22 ++++++++-------- fluffy/tools/utp_testing/utp_test_app.nim | 25 ++++++++----------- vendor/nim-eth | 2 +- 11 files changed, 81 insertions(+), 95 deletions(-) diff --git a/fluffy/common/common_utils.nim b/fluffy/common/common_utils.nim index 77ffbd95c..924ea451a 100644 --- a/fluffy/common/common_utils.nim +++ b/fluffy/common/common_utils.nim @@ -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) diff --git a/fluffy/conf.nim b/fluffy/conf.nim index 45d3e76e6..9790e111b 100644 --- a/fluffy/conf.nim +++ b/fluffy/conf.nim @@ -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 @[] diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index 36d48cd8c..c0a54223f 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -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 = diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index 10dcbe5ed..bf0aac959 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -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*( diff --git a/fluffy/rpc/rpc_discovery_api.nim b/fluffy/rpc/rpc_discovery_api.nim index 84544cc98..6c5c68e77 100644 --- a/fluffy/rpc/rpc_discovery_api.nim +++ b/fluffy/rpc/rpc_discovery_api.nim @@ -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 diff --git a/fluffy/rpc/rpc_portal_api.nim b/fluffy/rpc/rpc_portal_api.nim index 0426563e9..84768fe38 100644 --- a/fluffy/rpc/rpc_portal_api.nim +++ b/fluffy/rpc/rpc_portal_api.nim @@ -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 diff --git a/fluffy/rpc/rpc_types.nim b/fluffy/rpc/rpc_types.nim index ab43f3b84..ea9cf542b 100644 --- a/fluffy/rpc/rpc_types.nim +++ b/fluffy/rpc/rpc_types.nim @@ -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].} = diff --git a/fluffy/tests/portal_spec_tests/mainnet/test_portal_wire_encoding.nim b/fluffy/tests/portal_spec_tests/mainnet/test_portal_wire_encoding.nim index 1231f2a93..755521af8 100644 --- a/fluffy/tests/portal_spec_tests/mainnet/test_portal_wire_encoding.nim +++ b/fluffy/tests/portal_spec_tests/mainnet/test_portal_wire_encoding.nim @@ -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) diff --git a/fluffy/tools/portalcli.nim b/fluffy/tools/portalcli.nim index a62812f3d..2d7363aec 100644 --- a/fluffy/tools/portalcli.nim +++ b/fluffy/tools/portalcli.nim @@ -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 @[] diff --git a/fluffy/tools/utp_testing/utp_test_app.nim b/fluffy/tools/utp_testing/utp_test_app.nim index 783f30be2..3a15632c9 100644 --- a/fluffy/tools/utp_testing/utp_test_app.nim +++ b/fluffy/tools/utp_testing/utp_test_app.nim @@ -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) diff --git a/vendor/nim-eth b/vendor/nim-eth index 26212c881..8088fe72d 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit 26212c881b464ed64cac20442fb45144d3ecd3b3 +Subproject commit 8088fe72d77702dad83bda31d54485376c63cb61