mirror of
https://github.com/waku-org/nwaku.git
synced 2025-02-26 14:00:57 +00:00
refactor(enr): added waku2 capabilities accessor
This commit is contained in:
parent
d920b973b1
commit
157724d9b9
@ -53,10 +53,13 @@ suite "Waku ENR - Capabilities bitfield":
|
|||||||
check recordRes.isOk()
|
check recordRes.isOk()
|
||||||
let record = recordRes.tryGet()
|
let record = recordRes.tryGet()
|
||||||
|
|
||||||
let bitfieldRes = record.getCapabilitiesField()
|
let typedRecord = record.toTyped()
|
||||||
check bitfieldRes.isOk()
|
require typedRecord.isOk()
|
||||||
|
|
||||||
let bitfield = bitfieldRes.tryGet()
|
let bitfieldOpt = typedRecord.value.waku2
|
||||||
|
check bitfieldOpt.isSome()
|
||||||
|
|
||||||
|
let bitfield = bitfieldOpt.get()
|
||||||
check:
|
check:
|
||||||
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
||||||
|
|
||||||
@ -69,12 +72,15 @@ suite "Waku ENR - Capabilities bitfield":
|
|||||||
let record = Record.init(1, enrkey, wakuFlags=some(caps))
|
let record = Record.init(1, enrkey, wakuFlags=some(caps))
|
||||||
|
|
||||||
## When
|
## When
|
||||||
let bitfieldRes = record.getCapabilitiesField()
|
let typedRecord = record.toTyped()
|
||||||
|
require typedRecord.isOk()
|
||||||
|
|
||||||
|
let bitfieldOpt = typedRecord.value.waku2
|
||||||
|
|
||||||
## Then
|
## Then
|
||||||
check bitfieldRes.isOk()
|
check bitfieldOpt.isSome()
|
||||||
|
|
||||||
let bitfield = bitfieldRes.tryGet()
|
let bitfield = bitfieldOpt.get()
|
||||||
check:
|
check:
|
||||||
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
||||||
|
|
||||||
@ -87,14 +93,13 @@ suite "Waku ENR - Capabilities bitfield":
|
|||||||
let record = EnrBuilder.init(enrPrivKey, enrSeqNum).build().tryGet()
|
let record = EnrBuilder.init(enrPrivKey, enrSeqNum).build().tryGet()
|
||||||
|
|
||||||
## When
|
## When
|
||||||
let bitfieldRes = record.getCapabilitiesField()
|
let typedRecord = record.toTyped()
|
||||||
|
require typedRecord.isOk()
|
||||||
|
|
||||||
|
let bitfieldOpt = typedRecord.value.waku2
|
||||||
|
|
||||||
## Then
|
## Then
|
||||||
check bitfieldRes.isErr()
|
check bitfieldOpt.isNone()
|
||||||
|
|
||||||
let err = bitfieldRes.tryError()
|
|
||||||
check:
|
|
||||||
err == "Key not found in ENR"
|
|
||||||
|
|
||||||
test "check capabilities on a waku node record":
|
test "check capabilities on a waku node record":
|
||||||
## Given
|
## Given
|
||||||
@ -107,12 +112,19 @@ suite "Waku ENR - Capabilities bitfield":
|
|||||||
require waku_enr.fromBase64(record, wakuRecord)
|
require waku_enr.fromBase64(record, wakuRecord)
|
||||||
|
|
||||||
## Then
|
## Then
|
||||||
|
let typedRecordRes = record.toTyped()
|
||||||
|
require typedRecordRes.isOk()
|
||||||
|
|
||||||
|
let bitfieldOpt = typedRecordRes.value.waku2
|
||||||
|
require bitfieldOpt.isSome()
|
||||||
|
|
||||||
|
let bitfield = bitfieldOpt.get()
|
||||||
check:
|
check:
|
||||||
record.supportsCapability(Relay) == true
|
bitfield.supportsCapability(Capabilities.Relay) == true
|
||||||
record.supportsCapability(Store) == true
|
bitfield.supportsCapability(Capabilities.Store) == true
|
||||||
record.supportsCapability(Filter) == false
|
bitfield.supportsCapability(Capabilities.Filter) == false
|
||||||
record.supportsCapability(Lightpush) == false
|
bitfield.supportsCapability(Capabilities.Lightpush) == false
|
||||||
record.getCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
bitfield.toCapabilities() == @[Capabilities.Relay, Capabilities.Store]
|
||||||
|
|
||||||
test "check capabilities on a non-waku node record":
|
test "check capabilities on a non-waku node record":
|
||||||
## Given
|
## Given
|
||||||
@ -122,16 +134,22 @@ suite "Waku ENR - Capabilities bitfield":
|
|||||||
"Y3AyNTZrMaEDhpehBDbZjM_L9ek699Y7vhUJ-eAdMyQW_Fil522Y0fODdGNwgiMog3VkcIIjKA"
|
"Y3AyNTZrMaEDhpehBDbZjM_L9ek699Y7vhUJ-eAdMyQW_Fil522Y0fODdGNwgiMog3VkcIIjKA"
|
||||||
|
|
||||||
## When
|
## When
|
||||||
var nonWakuEnrRecord: Record
|
var record: Record
|
||||||
require waku_enr.fromURI(nonWakuEnrRecord, nonWakuEnr)
|
require waku_enr.fromURI(record, nonWakuEnr)
|
||||||
|
|
||||||
## Then
|
## Then
|
||||||
|
let typedRecordRes = record.toTyped()
|
||||||
|
require typedRecordRes.isOk()
|
||||||
|
|
||||||
|
let bitfieldOpt = typedRecordRes.value.waku2
|
||||||
|
check bitfieldOpt.isNone()
|
||||||
|
|
||||||
check:
|
check:
|
||||||
nonWakuEnrRecord.getCapabilities() == []
|
record.getCapabilities() == []
|
||||||
nonWakuEnrRecord.supportsCapability(Relay) == false
|
record.supportsCapability(Capabilities.Relay) == false
|
||||||
nonWakuEnrRecord.supportsCapability(Store) == false
|
record.supportsCapability(Capabilities.Store) == false
|
||||||
nonWakuEnrRecord.supportsCapability(Filter) == false
|
record.supportsCapability(Capabilities.Filter) == false
|
||||||
nonWakuEnrRecord.supportsCapability(Lightpush) == false
|
record.supportsCapability(Capabilities.Lightpush) == false
|
||||||
|
|
||||||
|
|
||||||
suite "Waku ENR - Multiaddresses":
|
suite "Waku ENR - Multiaddresses":
|
||||||
|
@ -34,8 +34,7 @@ type
|
|||||||
|
|
||||||
## 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*{.pure.} = enum
|
||||||
Capabilities* = enum
|
|
||||||
Relay = 0,
|
Relay = 0,
|
||||||
Store = 1,
|
Store = 1,
|
||||||
Filter = 2,
|
Filter = 2,
|
||||||
@ -82,24 +81,36 @@ proc withWakuCapabilities*(builder: var EnrBuilder, caps: openArray[Capabilities
|
|||||||
|
|
||||||
# ENR record accessors (e.g., Record, TypedRecord, etc.)
|
# ENR record accessors (e.g., Record, TypedRecord, etc.)
|
||||||
|
|
||||||
proc getCapabilitiesField*(r: Record): EnrResult[CapabilitiesBitfield] =
|
func waku2*(record: TypedRecord): Option[CapabilitiesBitfield] =
|
||||||
let field = ?r.get(CapabilitiesEnrField, seq[uint8])
|
let field = record.tryGet(CapabilitiesEnrField, seq[uint8])
|
||||||
ok(CapabilitiesBitfield(field[0]))
|
if field.isNone():
|
||||||
|
return none(CapabilitiesBitfield)
|
||||||
|
|
||||||
|
some(CapabilitiesBitfield(field.get()[0]))
|
||||||
|
|
||||||
proc supportsCapability*(r: Record, cap: Capabilities): bool =
|
proc supportsCapability*(r: Record, cap: Capabilities): bool =
|
||||||
let bitfield = getCapabilitiesField(r)
|
let recordRes = r.toTyped()
|
||||||
if bitfield.isErr():
|
if recordRes.isErr():
|
||||||
return false
|
return false
|
||||||
|
|
||||||
bitfield.value.supportsCapability(cap)
|
let bitfieldOpt = recordRes.value.waku2
|
||||||
|
if bitfieldOpt.isNone():
|
||||||
|
return false
|
||||||
|
|
||||||
|
let bitfield = bitfieldOpt.get()
|
||||||
|
bitfield.supportsCapability(cap)
|
||||||
|
|
||||||
proc getCapabilities*(r: Record): seq[Capabilities] =
|
proc getCapabilities*(r: Record): seq[Capabilities] =
|
||||||
let bitfield = getCapabilitiesField(r)
|
let recordRes = r.toTyped()
|
||||||
if bitfield.isErr():
|
if recordRes.isErr():
|
||||||
return @[]
|
return @[]
|
||||||
|
|
||||||
bitfield.value.toCapabilities()
|
let bitfieldOpt = recordRes.value.waku2
|
||||||
|
if bitfieldOpt.isNone():
|
||||||
|
return @[]
|
||||||
|
|
||||||
|
let bitfield = bitfieldOpt.get()
|
||||||
|
bitfield.toCapabilities()
|
||||||
|
|
||||||
|
|
||||||
## Multiaddress
|
## Multiaddress
|
||||||
|
Loading…
x
Reference in New Issue
Block a user