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 yield stripped
proc addBootstrapNode(bootstrapAddr: string, bootstrapEnrs: var seq[Record]) = proc addBootstrapNode(bootstrapAddr: string, bootstrapEnrs: var seq[Record]) =
var enrRec: enr.Record let res = enr.Record.fromURI(bootstrapAddr)
if enrRec.fromURI(bootstrapAddr): if res.isOk():
bootstrapEnrs.add enrRec bootstrapEnrs.add res.value
else: else:
warn "Ignoring invalid bootstrap ENR", bootstrapAddr warn "Ignoring invalid bootstrap ENR", bootstrapAddr, error = $res.error
proc loadBootstrapFile*(bootstrapFile: string, bootstrapEnrs: var seq[Record]) = proc loadBootstrapFile*(bootstrapFile: string, bootstrapEnrs: var seq[Record]) =
if bootstrapFile.len == 0: if bootstrapFile.len == 0:
@ -109,11 +109,12 @@ proc getPersistentEnr*(enrFilePath: string): Opt[enr.Record] =
var record: enr.Record var record: enr.Record
# TODO: This old API of var passing is very error prone and should be # TODO: This old API of var passing is very error prone and should be
# changed in nim-eth. # 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" warn "Could not decode ENR from ENR file"
return Opt.none(enr.Record) Opt.none(enr.Record)
else: else:
return Opt.some(record) Opt.some(res.value)
else: else:
warn "Could not find ENR file. Was it manually deleted?" 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 @[] return @[]
proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} = proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} =
if not fromURI(result, p): let res = enr.Record.fromURI(p)
raise newException(ValueError, "Invalid ENR") if res.isErr():
raise newException(ValueError, "Invalid ENR: " & $res.error)
res.value
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] = proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
return @[] return @[]
proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} = proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} =
var record: enr.Record let res = enr.Record.fromURI(p)
if not fromURI(record, p): if res.isErr():
raise newException(ValueError, "Invalid ENR") raise newException(ValueError, "Invalid ENR: " & $res.error)
let n = newNode(record) let n = Node.fromRecord(res.value)
if n.isErr: if n.address.isNone():
raise newException(ValueError, $n.error)
if n[].address.isNone():
raise newException(ValueError, "ENR without address") raise newException(ValueError, "ENR without address")
n[] n
proc completeCmdArg*(T: type Node, val: string): seq[string] = proc completeCmdArg*(T: type Node, val: string): seq[string] =
return @[] return @[]

View File

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

View File

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

View File

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

View File

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

View File

@ -53,11 +53,7 @@ func getRoutingTableInfo*(r: RoutingTable): RoutingTableInfo =
info info
func toNodeWithAddress*(enr: Record): Node {.raises: [ValueError].} = func toNodeWithAddress*(enr: Record): Node {.raises: [ValueError].} =
let nodeRes = newNode(enr) let node = Node.fromRecord(enr)
if nodeRes.isErr():
raise newException(ValueError, $nodeRes.error)
let node = nodeRes.get()
if node.address.isNone(): if node.address.isNone():
raise newException(ValueError, "ENR without address") raise newException(ValueError, "ENR without address")
else: else:
@ -69,7 +65,7 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: Record) {.gcsafe, raises: [IOEr
proc readValue*( proc readValue*(
r: var JsonReader[JrpcConv], val: var Record r: var JsonReader[JrpcConv], val: var Record
) {.gcsafe, raises: [IOError, JsonReaderError].} = ) {.gcsafe, raises: [IOError, JsonReaderError].} =
if not fromURI(val, r.parseString()): val = Record.fromURI(r.parseString()).valueOr:
r.raiseUnexpectedValue("Invalid ENR") r.raiseUnexpectedValue("Invalid ENR")
proc writeValue*(w: var JsonWriter[JrpcConv], v: NodeId) {.gcsafe, raises: [IOError].} = 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 message.nodes.enrs.len() == 0
test "Nodes Response - enrs": test "Nodes Response - enrs":
var e1, e2: Record let
check: res1 = Record.fromURI(
e1.fromURI(
"enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg" "enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg"
) )
e2.fromURI( res2 = Record.fromURI(
"enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU" "enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU"
) )
check:
res1.isOk()
res2.isOk()
let let
e1 = res1.value
e2 = res2.value
total = 0x1'u8 total = 0x1'u8
n = NodesMessage( n = NodesMessage(
total: total, enrs: List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)]) 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 message.content.content == content
test "Content Response - enrs": test "Content Response - enrs":
var e1, e2: Record let
check: res1 = Record.fromURI(
e1.fromURI(
"enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg" "enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg"
) )
e2.fromURI( res2 = Record.fromURI(
"enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU" "enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU"
) )
check:
res1.isOk()
res2.isOk()
let let
e1 = res1.value
e2 = res2.value
enrs = List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)]) enrs = List[ByteList, 32](@[ByteList(e1.raw), ByteList(e2.raw)])
c = ContentMessage(contentMessageType: enrsType, enrs: enrs) c = ContentMessage(contentMessageType: enrsType, enrs: enrs)

View File

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

View File

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

2
vendor/nim-eth vendored

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