Add envelope valid and dropped metrics for Waku/Whisper

This commit is contained in:
kdeme 2019-12-17 23:57:34 +01:00 committed by zah
parent 94f53e6924
commit ac30d7f589
3 changed files with 62 additions and 6 deletions

View File

@ -35,7 +35,7 @@
## else there will be no peers to send and receive messages from.
import
options, tables, times, chronos, chronicles,
options, tables, times, chronos, chronicles, metrics,
eth/[keys, async_utils, p2p], whisper/whisper_types, eth/trie/trie_defs
export
@ -44,6 +44,21 @@ export
logScope:
topics = "waku"
declareCounter valid_envelopes,
"Received & posted valid envelopes"
declareCounter dropped_low_pow_envelopes,
"Dropped envelopes because of too low PoW"
declareCounter dropped_too_large_envelopes,
"Dropped envelopes because larger than maximum allowed size"
declareCounter dropped_bloom_filter_mismatch_envelopes,
"Dropped envelopes because not matching with bloom filter"
declareCounter dropped_topic_mismatch_envelopes,
"Dropped envelopes because of not matching topics"
declareCounter dropped_benign_duplicate_envelopes,
"Dropped benign duplicate envelopes"
declareCounter dropped_malicious_duplicate_envelopes,
"Dropped malicious duplicate envelopes"
const
defaultQueueCapacity = 256
wakuVersion* = 0 ## Waku version.
@ -108,19 +123,23 @@ proc allowed*(msg: Message, config: WakuConfig): bool =
# Check max msg size, already happens in RLPx but there is a specific waku
# max msg size which should always be < RLPx max msg size
if msg.size > config.maxMsgSize:
dropped_too_large_envelopes.inc()
warn "Message size too large", size = msg.size
return false
if msg.pow < config.powRequirement:
dropped_low_pow_envelopes.inc()
warn "Message PoW too low", pow = msg.pow, minPow = config.powRequirement
return false
if not bloomFilterMatch(config.bloom, msg.bloom):
dropped_bloom_filter_mismatch_envelopes.inc()
warn "Message does not match node bloom filter"
return false
if config.wakuMode == WakuChan:
if msg.env.topic notin config.topics:
dropped_topic_mismatch_envelopes.inc()
warn "Message topic does not match Waku topic list"
return false
@ -241,6 +260,7 @@ p2pProtocol Waku(version = wakuVersion,
# (see above comment). If we want to seperate these cases (e.g. when peer
# rating), then we have to add a "peer.state.send" HashSet.
if peer.state.received.containsOrIncl(msg):
dropped_malicious_duplicate_envelopes.inc()
debug "Peer sending duplicate messages", peer, hash = msg.hash
# await peer.disconnect(SubprotocolReason)
continue
@ -248,8 +268,11 @@ p2pProtocol Waku(version = wakuVersion,
# This can still be a duplicate message, but from another peer than
# the peer who send the message.
if peer.networkState.queue[].add(msg):
valid_envelopes.inc()
# notify filters of this message
peer.networkState.filters.notify(msg)
else:
dropped_benign_duplicate_envelopes.inc()
proc powRequirement(peer: Peer, value: uint64) =
if not peer.state.initialized:
@ -391,6 +414,7 @@ proc queueMessage(node: EthereumNode, msg: Message): bool =
trace "Adding message to queue"
if wakuNet.queue[].add(msg):
valid_envelopes.inc()
# Also notify our own filters of the message we are sending,
# e.g. msg from local Dapp to Dapp
wakuNet.filters.notify(msg)

View File

@ -10,13 +10,18 @@
import
algorithm, bitops, math, options, strutils, tables, times, chronicles, hashes,
stew/[byteutils, endians2],
stew/[byteutils, endians2], metrics,
nimcrypto/[bcmode, hash, keccak, rijndael, sysrand],
eth/[keys, rlp, p2p], eth/p2p/ecies
logScope:
topics = "whisper_types"
declarePublicCounter dropped_expired_envelopes,
"Dropped envelopes because expired"
declarePublicCounter dropped_from_future_envelopes,
"Dropped envelopes because of future timestamp"
const
flagsLen = 1 ## payload flags field length, bytes
gcmIVLen = 12 ## Length of IV (seed) used for AES
@ -400,11 +405,17 @@ proc decode*(data: openarray[byte], dst = none[PrivateKey](),
# Envelopes --------------------------------------------------------------------
proc valid*(self: Envelope, now = epochTime()): bool =
if self.expiry.float64 < now: return false # expired
if self.ttl <= 0: return false # this would invalidate pow calculation
if self.expiry.float64 < now: # expired
dropped_expired_envelopes.inc()
return false
if self.ttl <= 0: # this would invalidate pow calculation
dropped_expired_envelopes.inc()
return false
let created = self.expiry - self.ttl
if created.float64 > (now + 2.0): return false # created in the future
if created.float64 > (now + 2.0): # created in the future
dropped_from_future_envelopes.inc()
return false
return true

View File

@ -33,7 +33,7 @@
## else there will be no peers to send and receive messages from.
import
options, tables, times, chronos, chronicles,
options, tables, times, chronos, chronicles, metrics,
eth/[keys, async_utils, p2p], whisper/whisper_types
export
@ -42,6 +42,19 @@ export
logScope:
topics = "whisper"
declareCounter valid_envelopes,
"Received & posted valid envelopes"
declareCounter dropped_low_pow_envelopes,
"Dropped envelopes because of too low PoW"
declareCounter dropped_too_large_envelopes,
"Dropped envelopes because larger than maximum allowed size"
declareCounter dropped_bloom_filter_mismatch_envelopes,
"Dropped envelopes because not matching with bloom filter"
declareCounter dropped_benign_duplicate_envelopes,
"Dropped benign duplicate envelopes"
declareCounter dropped_malicious_duplicate_envelopes,
"Dropped malicious duplicate envelopes"
const
defaultQueueCapacity = 256
whisperVersion* = 6 ## Whisper version.
@ -78,14 +91,17 @@ proc allowed*(msg: Message, config: WhisperConfig): bool =
# Check max msg size, already happens in RLPx but there is a specific shh
# max msg size which should always be < RLPx max msg size
if msg.size > config.maxMsgSize:
dropped_too_large_envelopes.inc()
warn "Message size too large", size = msg.size
return false
if msg.pow < config.powRequirement:
dropped_low_pow_envelopes.inc()
warn "Message PoW too low", pow = msg.pow, minPow = config.powRequirement
return false
if not bloomFilterMatch(config.bloom, msg.bloom):
dropped_bloom_filter_mismatch_envelopes.inc()
warn "Message does not match node bloom filter"
return false
@ -187,6 +203,7 @@ p2pProtocol Whisper(version = whisperVersion,
# (see above comment). If we want to seperate these cases (e.g. when peer
# rating), then we have to add a "peer.state.send" HashSet.
if peer.state.received.containsOrIncl(msg):
dropped_malicious_duplicate_envelopes.inc()
debug "Peer sending duplicate messages", peer, hash = msg.hash
# await peer.disconnect(SubprotocolReason)
continue
@ -194,8 +211,11 @@ p2pProtocol Whisper(version = whisperVersion,
# This can still be a duplicate message, but from another peer than
# the peer who send the message.
if peer.networkState.queue[].add(msg):
valid_envelopes.inc()
# notify filters of this message
peer.networkState.filters.notify(msg)
else:
dropped_benign_duplicate_envelopes.inc()
proc powRequirement(peer: Peer, value: uint64) =
if not peer.state.initialized:
@ -317,6 +337,7 @@ proc queueMessage(node: EthereumNode, msg: Message): bool =
trace "Adding message to queue"
if whisperNet.queue[].add(msg):
valid_envelopes.inc()
# Also notify our own filters of the message we are sending,
# e.g. msg from local Dapp to Dapp
whisperNet.filters.notify(msg)