Replace deprecated ENR functions (#6419)

This commit is contained in:
Kim De Mey 2024-07-12 17:18:24 +02:00 committed by GitHub
parent 738bc0e98f
commit ca15c4e433
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 16 deletions

View File

@ -1269,8 +1269,11 @@ func completeCmdArg*(T: type WalletName, 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
func completeCmdArg*(T: type enr.Record, val: string): seq[string] = func completeCmdArg*(T: type enr.Record, val: string): seq[string] =
return @[] return @[]

View File

@ -25,13 +25,13 @@ type
Eth2DiscoveryId* = NodeId Eth2DiscoveryId* = NodeId
func parseBootstrapAddress*(address: string): func parseBootstrapAddress*(address: string):
Result[enr.Record, cstring] = Result[enr.Record, string] =
let lowerCaseAddress = toLowerAscii(address) let lowerCaseAddress = toLowerAscii(address)
if lowerCaseAddress.startsWith("enr:"): if lowerCaseAddress.startsWith("enr:"):
var enrRec: enr.Record let res = enr.Record.fromURI(address)
if enrRec.fromURI(address): if res.isOk():
return ok enrRec return ok res.value
return err "Invalid ENR bootstrap record" return err "Invalid bootstrap ENR: " & $res.error
elif lowerCaseAddress.startsWith("enode:"): elif lowerCaseAddress.startsWith("enode:"):
return err "ENode bootstrap addresses are not supported" return err "ENode bootstrap addresses are not supported"
else: else:

View File

@ -1391,7 +1391,7 @@ proc connectWorker(node: Eth2Node, index: int) {.async: (raises: [CancelledError
node.connTable.excl(remotePeerAddr.peerId) node.connTable.excl(remotePeerAddr.peerId)
proc toPeerAddr(node: Node): Result[PeerAddr, cstring] = proc toPeerAddr(node: Node): Result[PeerAddr, cstring] =
let nodeRecord = ? node.record.toTypedRecord() let nodeRecord = TypedRecord.fromRecord(node.record)
let peerAddr = ? nodeRecord.toPeerAddr(tcpProtocol) let peerAddr = ? nodeRecord.toPeerAddr(tcpProtocol)
ok(peerAddr) ok(peerAddr)
@ -1883,11 +1883,9 @@ proc start*(node: Eth2Node) {.async: (raises: [CancelledError]).} =
notice "Discovery disabled; trying bootstrap nodes", notice "Discovery disabled; trying bootstrap nodes",
nodes = node.discovery.bootstrapRecords.len nodes = node.discovery.bootstrapRecords.len
for enr in node.discovery.bootstrapRecords: for enr in node.discovery.bootstrapRecords:
let tr = enr.toTypedRecord() let pa = TypedRecord.fromRecord(enr).toPeerAddr(tcpProtocol)
if tr.isOk(): if pa.isOk():
let pa = tr.get().toPeerAddr(tcpProtocol) await node.connQueue.addLast(pa.get())
if pa.isOk():
await node.connQueue.addLast(pa.get())
node.peerPingerHeartbeatFut = node.peerPingerHeartbeat() node.peerPingerHeartbeatFut = node.peerPingerHeartbeat()
node.peerTrimmerHeartbeatFut = node.peerTrimmerHeartbeat() node.peerTrimmerHeartbeatFut = node.peerTrimmerHeartbeat()
@ -2279,7 +2277,10 @@ proc createEth2Node*(rng: ref HmacDrbgContext,
let (peerId, address) = let (peerId, address) =
if s.startsWith("enr:"): if s.startsWith("enr:"):
let let
typedEnr = parseBootstrapAddress(s).get().toTypedRecord().get() enr = parseBootstrapAddress(s).valueOr:
fatal "Failed to parse bootstrap address", enr=s
quit 1
typedEnr = TypedRecord.fromRecord(enr)
peerAddress = toPeerAddr(typedEnr, tcpProtocol).get() peerAddress = toPeerAddr(typedEnr, tcpProtocol).get()
(peerAddress.peerId, peerAddress.addrs[0]) (peerAddress.peerId, peerAddress.addrs[0])
elif s.startsWith("/"): elif s.startsWith("/"):

View File

@ -108,8 +108,7 @@ proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
"" ""
proc getDiscoveryAddresses(node: BeaconNode): seq[string] = proc getDiscoveryAddresses(node: BeaconNode): seq[string] =
let let
typedRec = node.network.enrRecord().toTypedRecord().valueOr: typedRec = TypedRecord.fromRecord(node.network.enrRecord())
return default(seq[string])
peerAddr = typedRec.toPeerAddr(udpProtocol).valueOr: peerAddr = typedRec.toPeerAddr(udpProtocol).valueOr:
return default(seq[string]) return default(seq[string])
maddress = MultiAddress.init(multiCodec("p2p"), peerAddr.peerId).valueOr: maddress = MultiAddress.init(multiCodec("p2p"), peerAddr.peerId).valueOr: