Skip to content

Commit

Permalink
chore(networkmonitor): refactor + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Oct 26, 2022
1 parent a0723ef commit 99d06c6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
87 changes: 61 additions & 26 deletions tests/v2/test_enr_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,40 +126,25 @@ procSuite "ENR utils":
for knownMultiaddr in knownMultiaddrs:
check decodedAddrs.contains(knownMultiaddr)

asyncTest "Supports capabilities encoded in the ENR":
asyncTest "Supports specific capabilities encoded in the ENR":
let
enrIp = ValidIpAddress.init("127.0.0.1")
enrTcpPort, enrUdpPort = Port(60000)
enrKey = wakuenr.crypto.PrivateKey.random(Secp256k1, rng[])[]
multiaddrs = @[MultiAddress.init("/ip4/127.0.0.1/tcp/442/ws")[]]

# TODO: ugly, refactor
records = @[initWakuFlags(false, false, false, false),
initWakuFlags(false, false, false, true),
initWakuFlags(false, false, true, false),
initWakuFlags(false, false, true, true),
initWakuFlags(false, true, false, false),
initWakuFlags(false, true, false, true),
initWakuFlags(false, true, true, false),
initWakuFlags(false, true, true, true),
initWakuFlags(true, false, false, false),
initWakuFlags(true, false, false, true),
initWakuFlags(true, false, true, false),
initWakuFlags(true, false, true, true),
initWakuFlags(true, true, false, false),
initWakuFlags(true, true, false, true),
initWakuFlags(true, true, true, false),
initWakuFlags(true, true, true, true)]
.mapIt(initEnr(enrKey,
some(enrIp),
some(enrTcpPort),
some(enrUdpPort),
some(it),
multiaddrs))
# TODO: Refactor initEnr, provide enums as inputs initEnr(capabilites=[Store,Filter])
# TODO: safer than a util function and directly using the bits
# test all flag combinations 2^4 = 16 (b0000-b1111)
records = toSeq(0b0000_0000'u8..0b0000_1111'u8)
.mapIt(initEnr(enrKey,
some(enrIp),
some(enrTcpPort),
some(enrUdpPort),
some(uint8(it)),
multiaddrs))

# pitty the wakuFlags arguments can't be unwrapped
# same order:  lightpush | filter| store | relay
# TODO: Refactor after changing initWakuFlags. ask team
expectedCapabilities = @[[false, false, false, false],
[false, false, false, true],
[false, false, true, false],
Expand All @@ -180,3 +165,53 @@ procSuite "ENR utils":
for i, record in records:
for j, capability in @[Lightpush, Filter, Store, Relay]:
check expectedCapabilities[i][j] == record.supportsCapability(capability)

asyncTest "Get all supported capabilities encoded in the ENR":
let
enrIp = ValidIpAddress.init("127.0.0.1")
enrTcpPort, enrUdpPort = Port(60000)
enrKey = wakuenr.crypto.PrivateKey.random(Secp256k1, rng[])[]
multiaddrs = @[MultiAddress.init("/ip4/127.0.0.1/tcp/442/ws")[]]

records = @[0b0000_0000'u8,
0b0000_1111'u8,
0b0000_1001'u8,
0b0000_1110'u8,
0b0000_1000'u8,]
.mapIt(initEnr(enrKey,
some(enrIp),
some(enrTcpPort),
some(enrUdpPort),
some(uint8(it)),
multiaddrs))

# expected capabilities, ordered LSB to MSB
expectedCapabilities: seq[seq[Capabilities]] = @[
#[0b0000_0000]# @[],
#[0b0000_1111]# @[Relay, Store, Filter, Lightpush],
#[0b0000_1001]# @[Relay, Lightpush],
#[0b0000_1110]# @[Store, Filter, Lightpush],
#[0b0000_1000]# @[Lightpush]]

for i, actualExpetedTuple in zip(records, expectedCapabilities):
check actualExpetedTuple[0].getCapabilities() == actualExpetedTuple[1]

asyncTest "Get supported capabilities of a non waku node":

# non waku enr, i.e. Ethereum one
let nonWakuEnr = "enr:-KG4QOtcP9X1FbIMOe17QNMKqDxCpm14jcX5tiOE4_TyMrFqbmhPZHK_ZPG2G"&
"xb1GE2xdtodOfx9-cgvNtxnRyHEmC0ghGV0aDKQ9aX9QgAAAAD__________4JpZIJ2NIJpcIQDE8KdiXNl"&
"Y3AyNTZrMaEDhpehBDbZjM_L9ek699Y7vhUJ-eAdMyQW_Fil522Y0fODdGNwgiMog3VkcIIjKA"

var nonWakuEnrRecord: Record

check:
nonWakuEnrRecord.fromURI(nonWakuEnr)

# check that it doesn't support any capability and it doesnt't break
check:
nonWakuEnrRecord.getCapabilities() == []
nonWakuEnrRecord.supportsCapability(Relay) == false
nonWakuEnrRecord.supportsCapability(Store) == false
nonWakuEnrRecord.supportsCapability(Filter) == false
nonWakuEnrRecord.supportsCapability(Lightpush) == false
2 changes: 1 addition & 1 deletion tools/networkmonitor/networkmonitor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ proc main() {.async.} =
for node in flatNodes: discovered_peers_list.set(int64(0),
labelValues = [node.record.toURI(), #enr
$node.record.toTypedRecord().get().ip.get(), # TODO: Error handling. or what happens?
node.record.supportedCapabilites().join(",")])
node.record.getCapabilites().join(",")])
# TODO: Some debug prints
echo "total nodes: ", totalNodes
echo "seen nodes: ", seenNodes
Expand Down
15 changes: 6 additions & 9 deletions waku/v2/utils/wakuenr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,11 @@ func initEnr*(privateKey: crypto.PrivateKey,

return enr

# TODO: use isWakuNode?
proc supportsCapability*(r: Record, capability: Capabilities): bool =
# TODO: error handling, this is not safe
let enrCapabilities = r.get(WAKU_ENR_FIELD, seq[byte])[]
return testBit(enrCapabilities[0], capability.ord)

# TODO: Add tests for this
proc supportedCapabilites*(r: Record): seq[Capabilities] =
# TODO: Error handling
let enrCapabilities = r.get(WAKU_ENR_FIELD, seq[byte])[]
let enrCapabilities = r.get(WAKU_ENR_FIELD, seq[byte])
if enrCapabilities.isOk():
return testBit(enrCapabilities.get()[0], capability.ord)
return false

proc getCapabilities*(r: Record): seq[Capabilities] =
return toSeq(Capabilities.low..Capabilities.high).filterIt(r.supportsCapability(it))

0 comments on commit 99d06c6

Please sign in to comment.