mirror of https://github.com/waku-org/nwaku.git
feat(enr): added enr builder waku capabilities extension
This commit is contained in:
parent
1995bbec6c
commit
075815b072
|
@ -1,7 +1,7 @@
|
||||||
{.used.}
|
{.used.}
|
||||||
|
|
||||||
import
|
import
|
||||||
std/options ,
|
std/options,
|
||||||
stew/results,
|
stew/results,
|
||||||
stew/shims/net,
|
stew/shims/net,
|
||||||
testutils/unittests
|
testutils/unittests
|
||||||
|
|
|
@ -37,7 +37,31 @@ suite "Waku ENR - Capabilities bitfield":
|
||||||
check:
|
check:
|
||||||
caps == @[Capabilities.Relay, Capabilities.Filter, Capabilities.Lightpush]
|
caps == @[Capabilities.Relay, Capabilities.Filter, Capabilities.Lightpush]
|
||||||
|
|
||||||
test "encode and extract capabilities from record":
|
test "encode and extract capabilities from record (EnrBuilder ext)":
|
||||||
|
## Given
|
||||||
|
let
|
||||||
|
enrSeqNum = 1u64
|
||||||
|
enrPrivKey = generatesecp256k1key()
|
||||||
|
|
||||||
|
## When
|
||||||
|
var builder = EnrBuilder.init(enrPrivKey, seqNum = enrSeqNum)
|
||||||
|
builder.withWakuCapabilities(Capabilities.Relay, Capabilities.Store)
|
||||||
|
|
||||||
|
let recordRes = builder.build()
|
||||||
|
|
||||||
|
## Then
|
||||||
|
check recordRes.isOk()
|
||||||
|
let record = recordRes.tryGet()
|
||||||
|
|
||||||
|
let bitfieldRes = record.getCapabilitiesField()
|
||||||
|
check bitfieldRes.isOk()
|
||||||
|
|
||||||
|
let bitfield = bitfieldRes.tryGet()
|
||||||
|
check:
|
||||||
|
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
||||||
|
|
||||||
|
test "encode and extract capabilities from record (deprecated)":
|
||||||
|
# TODO: Remove after removing the `Record.init()` proc
|
||||||
## Given
|
## Given
|
||||||
let enrkey = generatesecp256k1key()
|
let enrkey = generatesecp256k1key()
|
||||||
let caps = CapabilitiesBitfield.init(Capabilities.Relay, Capabilities.Store)
|
let caps = CapabilitiesBitfield.init(Capabilities.Relay, Capabilities.Store)
|
||||||
|
@ -56,8 +80,11 @@ suite "Waku ENR - Capabilities bitfield":
|
||||||
|
|
||||||
test "cannot extract capabilities from record":
|
test "cannot extract capabilities from record":
|
||||||
## Given
|
## Given
|
||||||
let enrkey = generatesecp256k1key()
|
let
|
||||||
let record = Record.init(1, enrkey, wakuFlags=none(CapabilitiesBitfield))
|
enrSeqNum = 1u64
|
||||||
|
enrPrivKey = generatesecp256k1key()
|
||||||
|
|
||||||
|
let record = EnrBuilder.init(enrPrivKey, enrSeqNum).build().tryGet()
|
||||||
|
|
||||||
## When
|
## When
|
||||||
let bitfieldRes = record.getCapabilitiesField()
|
let bitfieldRes = record.getCapabilitiesField()
|
||||||
|
|
|
@ -12,9 +12,10 @@ import
|
||||||
stew/[endians2, results],
|
stew/[endians2, results],
|
||||||
stew/shims/net,
|
stew/shims/net,
|
||||||
eth/keys,
|
eth/keys,
|
||||||
eth/p2p/discoveryv5/enr,
|
|
||||||
libp2p/[multiaddress, multicodec],
|
libp2p/[multiaddress, multicodec],
|
||||||
libp2p/crypto/crypto
|
libp2p/crypto/crypto
|
||||||
|
import
|
||||||
|
../../common/enr
|
||||||
|
|
||||||
export enr, crypto, multiaddress, net
|
export enr, crypto, multiaddress, net
|
||||||
|
|
||||||
|
@ -23,16 +24,17 @@ const
|
||||||
CapabilitiesEnrField* = "waku2"
|
CapabilitiesEnrField* = "waku2"
|
||||||
|
|
||||||
|
|
||||||
## Capabilities
|
## Node capabilities
|
||||||
|
|
||||||
type
|
type
|
||||||
## 8-bit flag field to indicate Waku capabilities.
|
## 8-bit flag field to indicate Waku node capabilities.
|
||||||
## Only the 4 LSBs are currently defined according
|
## Only the 4 LSBs are currently defined according
|
||||||
## to RFC31 (https://rfc.vac.dev/spec/31/).
|
## to RFC31 (https://rfc.vac.dev/spec/31/).
|
||||||
CapabilitiesBitfield* = distinct uint8
|
CapabilitiesBitfield* = distinct uint8
|
||||||
|
|
||||||
## See: https://rfc.vac.dev/spec/31/#waku2-enr-key
|
## See: https://rfc.vac.dev/spec/31/#waku2-enr-key
|
||||||
## each enum numbers maps to a bit (where 0 is the LSB)
|
## each enum numbers maps to a bit (where 0 is the LSB)
|
||||||
|
# TODO: Make this enum {.pure.}
|
||||||
Capabilities* = enum
|
Capabilities* = enum
|
||||||
Relay = 0,
|
Relay = 0,
|
||||||
Store = 1,
|
Store = 1,
|
||||||
|
@ -66,9 +68,19 @@ func toCapabilities*(bitfield: CapabilitiesBitfield): seq[Capabilities] =
|
||||||
toSeq(Capabilities.low..Capabilities.high).filterIt(supportsCapability(bitfield, it))
|
toSeq(Capabilities.low..Capabilities.high).filterIt(supportsCapability(bitfield, it))
|
||||||
|
|
||||||
|
|
||||||
## TODO: Turn into an EnrBuilder extension
|
# ENR builder extension
|
||||||
func toFieldPair*(caps: CapabilitiesBitfield): FieldPair =
|
|
||||||
toFieldPair(CapabilitiesEnrField, @[caps.uint8])
|
proc withWakuCapabilities*(builder: var EnrBuilder, caps: CapabilitiesBitfield) =
|
||||||
|
builder.addFieldPair(CapabilitiesEnrField, @[caps.uint8])
|
||||||
|
|
||||||
|
proc withWakuCapabilities*(builder: var EnrBuilder, caps: varargs[Capabilities]) =
|
||||||
|
withWakuCapabilities(builder, CapabilitiesBitfield.init(caps))
|
||||||
|
|
||||||
|
proc withWakuCapabilities*(builder: var EnrBuilder, caps: openArray[Capabilities]) =
|
||||||
|
withWakuCapabilities(builder, CapabilitiesBitfield.init(@caps))
|
||||||
|
|
||||||
|
|
||||||
|
# ENR record accessors (e.g., Record, TypedRecord, etc.)
|
||||||
|
|
||||||
proc getCapabilitiesField*(r: Record): EnrResult[CapabilitiesBitfield] =
|
proc getCapabilitiesField*(r: Record): EnrResult[CapabilitiesBitfield] =
|
||||||
let field = ?r.get(CapabilitiesEnrField, seq[uint8])
|
let field = ?r.get(CapabilitiesEnrField, seq[uint8])
|
||||||
|
@ -201,7 +213,7 @@ func init*(T: type enr.Record,
|
||||||
|
|
||||||
# `waku2` field
|
# `waku2` field
|
||||||
if wakuFlags.isSome():
|
if wakuFlags.isSome():
|
||||||
wakuEnrFields.add(toFieldPair(wakuFlags.get()))
|
wakuEnrFields.add(toFieldPair(CapabilitiesEnrField, @[wakuFlags.get().uint8]))
|
||||||
|
|
||||||
# `multiaddrs` field
|
# `multiaddrs` field
|
||||||
if multiaddrs.len > 0:
|
if multiaddrs.len > 0:
|
||||||
|
|
Loading…
Reference in New Issue