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.ip == none(array[4, byte])
record.tcp == none(uint16)
record.udp == none(uint16)
record.ip6 == some(enrIpAddr.address_v6)
record.tcp6 == none(uint16)
record.udp6 == some(enrUdpPort.uint16)
record.udp == some(enrUdpPort.uint16)

View File

@ -7,15 +7,27 @@ import
eth/p2p/discoveryv5/enr,
libp2p/crypto/crypto as libp2p_crypto
import ./typed_record
## Builder
type EnrBuilder* = object
seqNumber: uint64
privateKey: eth_keys.PrivateKey
ipAddress: Opt[IpAddress]
tcpPort: Opt[Port]
udpPort: Opt[Port]
fields: seq[FieldPair]
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 =
# TODO: Inconvenient runtime assertion. Move this assertion to compile time
@ -41,9 +53,9 @@ proc build*(builder: EnrBuilder): EnrResult[enr.Record] =
enr.Record.init(
seqNum = builder.seqNumber,
pk = builder.privateKey,
ip = Opt.none(IpAddress),
tcpPort = Opt.none(Port),
udpPort = Opt.none(Port),
ip = builder.ipAddress,
tcpPort = builder.tcpPort,
udpPort = builder.udpPort,
extraFields = builder.fields,
)
@ -52,38 +64,14 @@ proc build*(builder: EnrBuilder): EnrResult[enr.Record] =
proc addAddressAndPorts(
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
let isV6 = ip.family == IPv6
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)
builder.ipAddress = Opt.some(ip)
builder.tcpPort = tcpPort.toOpt()
builder.udpPort = udpPort.toOpt()
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
if tcp.isSome():
let tcpPort = tcp.get()
builder.addFieldPair("tcp", tcpPort.uint16)
if udp.isSome():
let udpPort = udp.get()
builder.addFieldPair("udp", udpPort.uint16)
builder.tcpPort = tcp.toOpt()
builder.udpPort = udp.toOpt()
proc withIpAddressAndPorts*(
builder: var EnrBuilder,

View File

@ -84,10 +84,16 @@ func tcp*(record: TypedRecord): Option[uint16] =
record.tryGet("tcp", 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] =
record.tryGet("udp", 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
agentString* {.
defaultValue: "nwaku-" & git_version,
defaultValue: "nwaku-" & external_config.git_version,
desc: "Node agent string which is used as identifier in network",
name: "agent-string"
.}: string