Merge pull request #155 from status-im/waku-123

Waku/Whisper small adjustments + queue ordering fix
This commit is contained in:
kdeme 2020-01-14 12:07:52 -08:00 committed by GitHub
commit b7ebf8ed54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 47 deletions

View File

@ -47,23 +47,23 @@ export
logScope:
topics = "waku"
declareCounter valid_envelopes,
declarePublicCounter valid_envelopes,
"Received & posted valid envelopes"
declareCounter dropped_low_pow_envelopes,
declarePublicCounter dropped_low_pow_envelopes,
"Dropped envelopes because of too low PoW"
declareCounter dropped_too_large_envelopes,
declarePublicCounter dropped_too_large_envelopes,
"Dropped envelopes because larger than maximum allowed size"
declareCounter dropped_bloom_filter_mismatch_envelopes,
declarePublicCounter dropped_bloom_filter_mismatch_envelopes,
"Dropped envelopes because not matching with bloom filter"
declareCounter dropped_topic_mismatch_envelopes,
declarePublicCounter dropped_topic_mismatch_envelopes,
"Dropped envelopes because of not matching topics"
declareCounter dropped_benign_duplicate_envelopes,
declarePublicCounter dropped_benign_duplicate_envelopes,
"Dropped benign duplicate envelopes"
declareCounter dropped_malicious_duplicate_envelopes,
declarePublicCounter dropped_malicious_duplicate_envelopes,
"Dropped malicious duplicate envelopes"
const
defaultQueueCapacity = 256
defaultQueueCapacity = 2048
wakuVersion* = 0 ## Waku version.
wakuVersionStr* = $wakuVersion ## Waku version.
defaultMinPow* = 0.2'f64 ## The default minimum PoW requirement for this node.
@ -373,17 +373,18 @@ proc processQueue(peer: Peer) =
continue
if message.pow < wakuPeer.powRequirement:
debug "Message PoW too low for peer", pow = message.pow,
trace "Message PoW too low for peer", pow = message.pow,
powReq = wakuPeer.powRequirement
continue
if not bloomFilterMatch(wakuPeer.bloom, message.bloom):
debug "Message does not match peer bloom filter"
trace "Message does not match peer bloom filter"
continue
if wakuNet.config.wakuMode == WakuSan and
wakuPeer.wakuMode == WakuChan:
if message.env.topic notin wakuPeer.topics:
trace "Message does not match topics list"
continue
trace "Adding envelope"

View File

@ -473,7 +473,7 @@ proc calcPowHash*(self: Envelope): Hash =
proc cmpPow(a, b: Message): int =
## Biggest pow first, lowest at the end (for easy popping)
if a.pow > b.pow: 1
if a.pow < b.pow: 1
elif a.pow == b.pow: 0
else: -1
@ -572,7 +572,7 @@ proc add*(self: var Queue, msg: Message): bool =
return true
# Filters ----------------------------------------------------------------------
proc newFilter*(src = none[PublicKey](), privateKey = none[PrivateKey](),
proc initFilter*(src = none[PublicKey](), privateKey = none[PrivateKey](),
symKey = none[SymKey](), topics: seq[Topic] = @[],
powReq = 0.0, allowP2P = false): Filter =
# Zero topics will give an empty bloom filter which is fine as this bloom

View File

@ -42,21 +42,21 @@ export
logScope:
topics = "whisper"
declareCounter valid_envelopes,
declarePublicCounter valid_envelopes,
"Received & posted valid envelopes"
declareCounter dropped_low_pow_envelopes,
declarePublicCounter dropped_low_pow_envelopes,
"Dropped envelopes because of too low PoW"
declareCounter dropped_too_large_envelopes,
declarePublicCounter dropped_too_large_envelopes,
"Dropped envelopes because larger than maximum allowed size"
declareCounter dropped_bloom_filter_mismatch_envelopes,
declarePublicCounter dropped_bloom_filter_mismatch_envelopes,
"Dropped envelopes because not matching with bloom filter"
declareCounter dropped_benign_duplicate_envelopes,
declarePublicCounter dropped_benign_duplicate_envelopes,
"Dropped benign duplicate envelopes"
declareCounter dropped_malicious_duplicate_envelopes,
declarePublicCounter dropped_malicious_duplicate_envelopes,
"Dropped malicious duplicate envelopes"
const
defaultQueueCapacity = 256
defaultQueueCapacity = 2048
whisperVersion* = 6 ## Whisper version.
whisperVersionStr* = $whisperVersion ## Whisper version.
defaultMinPow* = 0.2'f64 ## The default minimum PoW requirement for this node.
@ -273,12 +273,12 @@ proc processQueue(peer: Peer) =
continue
if message.pow < whisperPeer.powRequirement:
debug "Message PoW too low for peer", pow = message.pow,
trace "Message PoW too low for peer", pow = message.pow,
powReq = whisperPeer.powRequirement
continue
if not bloomFilterMatch(whisperPeer.bloom, message.bloom):
debug "Message does not match peer bloom filter"
trace "Message does not match peer bloom filter"
continue
trace "Adding envelope"

View File

@ -117,20 +117,20 @@ if config.watch:
echo msg.decoded.payload.repr
# filter encrypted asym
discard node.subscribeFilter(newFilter(privateKey = some(encPrivateKey),
discard node.subscribeFilter(initFilter(privateKey = some(encPrivateKey),
topics = @[topic]),
handler)
# filter encrypted asym + signed
discard node.subscribeFilter(newFilter(some(signPublicKey),
discard node.subscribeFilter(initFilter(some(signPublicKey),
privateKey = some(encPrivateKey),
topics = @[topic]),
handler)
# filter encrypted sym
discard node.subscribeFilter(newFilter(symKey = some(symKey),
discard node.subscribeFilter(initFilter(symKey = some(symKey),
topics = @[topic]),
handler)
# filter encrypted sym + signed
discard node.subscribeFilter(newFilter(some(signPublicKey),
discard node.subscribeFilter(initFilter(some(signPublicKey),
symKey = some(symKey),
topics = @[topic]),
handler)

View File

@ -182,6 +182,9 @@ let
env1 = Envelope(
expiry:100000, ttl: 30, topic: [byte 0, 0, 0, 0],
data: repeat(byte 9, 256), nonce: 1010102)
env2 = Envelope(
expiry:100000, ttl: 30, topic: [byte 0, 0, 0, 0],
data: repeat(byte 9, 256), nonce: 1010103)
suite "Whisper envelope":
@ -247,6 +250,22 @@ suite "Whisper queue":
queue.items.len() == 2
test "check if order of queue is by decreasing PoW":
var queue = initQueue(3)
let msg0 = initMessage(env0)
let msg1 = initMessage(env1)
let msg2 = initMessage(env2)
discard queue.add(msg0)
discard queue.add(msg1)
discard queue.add(msg2)
check:
queue.items.len() == 3
queue.items[0].pow > queue.items[1].pow and
queue.items[1].pow > queue.items[2].pow
test "check field order against expected rlp order":
check rlp.encode(env0) ==
rlp.encodeList(env0.expiry, env0.ttl, env0.topic, env0.data, env0.nonce)
@ -269,7 +288,7 @@ suite "Whisper filter":
let msg = prepFilterTestMsg(symKey = some(symKey), topic = topic)
var filters = initTable[string, Filter]()
let filter = newFilter(symKey = some(symKey), topics = @[topic])
let filter = initFilter(symKey = some(symKey), topics = @[topic])
let filterId = filters.subscribeFilter(filter)
notify(filters, msg)
@ -287,7 +306,7 @@ suite "Whisper filter":
topic = topic)
var filters = initTable[string, Filter]()
let filter = newFilter(privateKey = some(privKey), topics = @[topic])
let filter = initFilter(privateKey = some(privKey), topics = @[topic])
let filterId = filters.subscribeFilter(filter)
notify(filters, msg)
@ -304,7 +323,7 @@ suite "Whisper filter":
let msg = prepFilterTestMsg(src = some(privKey), topic = topic)
var filters = initTable[string, Filter]()
let filter = newFilter(src = some(privKey.getPublicKey()),
let filter = initFilter(src = some(privKey.getPublicKey()),
topics = @[topic])
let filterId = filters.subscribeFilter(filter)
@ -328,9 +347,9 @@ suite "Whisper filter":
var filters = initTable[string, Filter]()
let
filterId1 = filters.subscribeFilter(
newFilter(topics = @[topic], powReq = 0.014492753623188406))
initFilter(topics = @[topic], powReq = 0.014492753623188406))
filterId2 = filters.subscribeFilter(
newFilter(topics = @[topic], powReq = 0.014492753623188407))
initFilter(topics = @[topic], powReq = 0.014492753623188407))
notify(filters, msg)
@ -347,8 +366,8 @@ suite "Whisper filter":
var filters = initTable[string, Filter]()
let
filterId1 = filters.subscribeFilter(newFilter(topics = @[topic1]))
filterId2 = filters.subscribeFilter(newFilter(topics = @[topic2]))
filterId1 = filters.subscribeFilter(initFilter(topics = @[topic1]))
filterId2 = filters.subscribeFilter(initFilter(topics = @[topic2]))
notify(filters, msg)

View File

@ -59,17 +59,17 @@ suite "Whisper connections":
# Filters
# filter for encrypted asym
filters.add(node1.subscribeFilter(newFilter(privateKey = some(encryptKeyPair.seckey),
filters.add(node1.subscribeFilter(initFilter(privateKey = some(encryptKeyPair.seckey),
topics = @[topic]), handler1))
# filter for encrypted asym + signed
filters.add(node1.subscribeFilter(newFilter(some(signKeyPair.pubkey),
filters.add(node1.subscribeFilter(initFilter(some(signKeyPair.pubkey),
privateKey = some(encryptKeyPair.seckey),
topics = @[topic]), handler2))
# filter for encrypted sym
filters.add(node1.subscribeFilter(newFilter(symKey = some(symKey),
filters.add(node1.subscribeFilter(initFilter(symKey = some(symKey),
topics = @[topic]), handler3))
# filter for encrypted sym + signed
filters.add(node1.subscribeFilter(newFilter(some(signKeyPair.pubkey),
filters.add(node1.subscribeFilter(initFilter(some(signKeyPair.pubkey),
symKey = some(symKey),
topics = @[topic]), handler4))
# Messages
@ -113,8 +113,8 @@ suite "Whisper connections":
check msg.decoded.payload == payloads[1]
futures[1].complete(1)
var filter1 = node1.subscribeFilter(newFilter(topics = @[topic1]), handler1)
var filter2 = node1.subscribeFilter(newFilter(topics = @[topic2]), handler2)
var filter1 = node1.subscribeFilter(initFilter(topics = @[topic1]), handler1)
var filter2 = node1.subscribeFilter(initFilter(topics = @[topic2]), handler2)
check:
node2.postMessage(ttl = safeTTL + 1, topic = topic1,
@ -142,9 +142,9 @@ suite "Whisper connections":
check msg.decoded.payload == payload
futures[1].complete(1)
var filter1 = node1.subscribeFilter(newFilter(topics = @[topic], powReq = 0),
var filter1 = node1.subscribeFilter(initFilter(topics = @[topic], powReq = 0),
handler1)
var filter2 = node1.subscribeFilter(newFilter(topics = @[topic],
var filter2 = node1.subscribeFilter(initFilter(topics = @[topic],
powReq = 1_000_000), handler2)
check:
@ -163,7 +163,7 @@ suite "Whisper connections":
let topic = [byte 0, 0, 0, 0]
let payload = repeat(byte 0, 10)
var filter = node1.subscribeFilter(newFilter(topics = @[topic]))
var filter = node1.subscribeFilter(initFilter(topics = @[topic]))
for i in countdown(10, 1):
check node2.postMessage(ttl = safeTTL, topic = topic,
payload = payload) == true
@ -179,7 +179,7 @@ suite "Whisper connections":
asyncTest "Local filter notify":
let topic = [byte 0, 0, 0, 0]
var filter = node1.subscribeFilter(newFilter(topics = @[topic]))
var filter = node1.subscribeFilter(initFilter(topics = @[topic]))
check:
node1.postMessage(ttl = safeTTL, topic = topic,
payload = repeat(byte 4, 10)) == true
@ -198,7 +198,7 @@ suite "Whisper connections":
proc handler(msg: ReceivedMessage) =
check msg.decoded.payload == payload
f.complete(1)
var filter = node1.subscribeFilter(newFilter(topics = filterTopics), handler)
var filter = node1.subscribeFilter(initFilter(topics = filterTopics), handler)
await node1.setBloomFilter(node1.filtersToBloom())
check:
@ -279,7 +279,7 @@ suite "Whisper connections":
check msg.decoded.payload == repeat(byte 4, 10)
f.complete(1)
var filter = node1.subscribeFilter(newFilter(topics = @[topic],
var filter = node1.subscribeFilter(initFilter(topics = @[topic],
allowP2P = true), handler)
check:
node1.setPeerTrusted(toNodeId(node2.keys.pubkey)) == true

View File

@ -54,9 +54,9 @@ suite "Waku - Whisper bridge tests":
futures[1].complete(1)
var filter1 = whisper.subscribeFilter(nodeWhisper,
whisper.newFilter(topics = @[topic1]), handler1)
whisper.initFilter(topics = @[topic1]), handler1)
var filter2 = waku.subscribeFilter(nodeWaku,
waku.newFilter(topics = @[topic2]), handler2)
waku.initFilter(topics = @[topic2]), handler2)
check:
# Message should also end up in the Whisper node its queue via the bridge

View File

@ -73,7 +73,7 @@ suite "Waku Mail Client":
f.complete(1)
let filter = subscribeFilter(client,
newFilter(topics = @[topic], allowP2P = true), handler)
initFilter(topics = @[topic], allowP2P = true), handler)
check:
client.setPeerTrusted(simpleServerNode.id)