Adapt ENR Record handling to new interface of nim-eth

This commit is contained in:
NagyZoltanPeter 2024-10-21 15:33:50 +02:00
parent a5189d0566
commit 0a91ab6eb5
No known key found for this signature in database
GPG Key ID: 16EADB9673B65368
4 changed files with 31 additions and 37 deletions

View File

@ -120,7 +120,7 @@ suite "nim-eth ENR - Ext: IP address and TCP/UDP ports":
@(record.secp256k1.get()) == expectedPubKey @(record.secp256k1.get()) == expectedPubKey
record.ip == none(array[4, byte]) record.ip == none(array[4, byte])
record.tcp == none(uint16) record.tcp == none(uint16)
record.udp == none(uint16)
record.ip6 == some(enrIpAddr.address_v6) record.ip6 == some(enrIpAddr.address_v6)
record.tcp6 == none(uint16) record.tcp6 == none(uint16)
record.udp6 == some(enrUdpPort.uint16) record.udp6 == some(enrUdpPort.uint16)
record.udp == some(enrUdpPort.uint16)

View File

@ -7,15 +7,27 @@ import
eth/p2p/discoveryv5/enr, eth/p2p/discoveryv5/enr,
libp2p/crypto/crypto as libp2p_crypto libp2p/crypto/crypto as libp2p_crypto
import ./typed_record
## Builder ## Builder
type EnrBuilder* = object type EnrBuilder* = object
seqNumber: uint64 seqNumber: uint64
privateKey: eth_keys.PrivateKey privateKey: eth_keys.PrivateKey
ipAddress: Opt[IpAddress]
tcpPort: Opt[Port]
udpPort: Opt[Port]
fields: seq[FieldPair] fields: seq[FieldPair]
proc init*(T: type EnrBuilder, key: eth_keys.PrivateKey, seqNum: uint64 = 1): T = proc init*(T: type EnrBuilder, key: eth_keys.PrivateKey, seqNum: uint64 = 1): T =
EnrBuilder(seqNumber: seqNum, privateKey: key, fields: newSeq[FieldPair]()) EnrBuilder(
seqNumber: seqNum,
privateKey: key,
ipAddress: Opt.none(IpAddress),
tcpPort: Opt.none(Port),
udpPort: Opt.none(Port),
fields: newSeq[FieldPair](),
)
proc init*(T: type EnrBuilder, key: libp2p_crypto.PrivateKey, seqNum: uint64 = 1): T = proc init*(T: type EnrBuilder, key: libp2p_crypto.PrivateKey, seqNum: uint64 = 1): T =
# TODO: Inconvenient runtime assertion. Move this assertion to compile time # TODO: Inconvenient runtime assertion. Move this assertion to compile time
@ -41,9 +53,9 @@ proc build*(builder: EnrBuilder): EnrResult[enr.Record] =
enr.Record.init( enr.Record.init(
seqNum = builder.seqNumber, seqNum = builder.seqNumber,
pk = builder.privateKey, pk = builder.privateKey,
ip = Opt.none(IpAddress), ip = builder.ipAddress,
tcpPort = Opt.none(Port), tcpPort = builder.tcpPort,
udpPort = Opt.none(Port), udpPort = builder.udpPort,
extraFields = builder.fields, extraFields = builder.fields,
) )
@ -52,38 +64,14 @@ proc build*(builder: EnrBuilder): EnrResult[enr.Record] =
proc addAddressAndPorts( proc addAddressAndPorts(
builder: var EnrBuilder, ip: IpAddress, tcpPort, udpPort: Option[Port] builder: var EnrBuilder, ip: IpAddress, tcpPort, udpPort: Option[Port]
) = ) =
# Based on: https://github.com/status-im/nim-eth/blob/4b22fcd/eth/p2p/discoveryv5/enr.nim#L166 builder.ipAddress = Opt.some(ip)
let isV6 = ip.family == IPv6 builder.tcpPort = tcpPort.toOpt()
builder.udpPort = udpPort.toOpt()
let ipField =
if isV6:
toFieldPair("ip6", ip.address_v6)
else:
toFieldPair("ip", ip.address_v4)
builder.addFieldPair(ipField)
if tcpPort.isSome():
let
tcpPortFieldKey = if isV6: "tcp6" else: "tcp"
tcpPortFieldValue = tcpPort.get()
builder.addFieldPair(tcpPortFieldKey, tcpPortFieldValue.uint16)
if udpPort.isSome():
let
udpPortFieldKey = if isV6: "udp6" else: "udp"
udpPortFieldValue = udpPort.get()
builder.addFieldPair(udpPortFieldKey, udpPortFieldValue.uint16)
proc addPorts(builder: var EnrBuilder, tcp, udp: Option[Port]) = proc addPorts(builder: var EnrBuilder, tcp, udp: Option[Port]) =
# Based on: https://github.com/status-im/nim-eth/blob/4b22fcd/eth/p2p/discoveryv5/enr.nim#L166 # Based on: https://github.com/status-im/nim-eth/blob/4b22fcd/eth/p2p/discoveryv5/enr.nim#L166
builder.tcpPort = tcp.toOpt()
if tcp.isSome(): builder.udpPort = udp.toOpt()
let tcpPort = tcp.get()
builder.addFieldPair("tcp", tcpPort.uint16)
if udp.isSome():
let udpPort = udp.get()
builder.addFieldPair("udp", udpPort.uint16)
proc withIpAddressAndPorts*( proc withIpAddressAndPorts*(
builder: var EnrBuilder, builder: var EnrBuilder,

View File

@ -84,10 +84,16 @@ func tcp*(record: TypedRecord): Option[uint16] =
record.tryGet("tcp", uint16) record.tryGet("tcp", uint16)
func tcp6*(record: TypedRecord): Option[uint16] = func tcp6*(record: TypedRecord): Option[uint16] =
record.tryGet("tcp6", uint16) let port = record.tryGet("tcp6", uint16)
if port.isNone():
return record.tcp()
return port
func udp*(record: TypedRecord): Option[uint16] = func udp*(record: TypedRecord): Option[uint16] =
record.tryGet("udp", uint16) record.tryGet("udp", uint16)
func udp6*(record: TypedRecord): Option[uint16] = func udp6*(record: TypedRecord): Option[uint16] =
record.tryGet("udp6", uint16) let port = record.tryGet("udp6", uint16)
if port.isNone():
return record.udp()
return port

View File

@ -160,7 +160,7 @@ type WakuNodeConf* = object
.}: uint16 .}: uint16
agentString* {. agentString* {.
defaultValue: "nwaku-" & git_version, defaultValue: "nwaku-" & external_config.git_version,
desc: "Node agent string which is used as identifier in network", desc: "Node agent string which is used as identifier in network",
name: "agent-string" name: "agent-string"
.}: string .}: string