mirror of https://github.com/status-im/nim-eth.git
Merge pull request #249 from status-im/whisper-impr
Whisper improvements
This commit is contained in:
commit
be9a87848e
|
@ -17,18 +17,10 @@ import
|
||||||
logScope:
|
logScope:
|
||||||
topics = "whisper_types"
|
topics = "whisper_types"
|
||||||
|
|
||||||
declarePublicCounter valid_envelopes,
|
declarePublicCounter envelopes_valid,
|
||||||
"Received & posted valid envelopes"
|
"Received & posted valid envelopes"
|
||||||
declarePublicCounter dropped_benign_duplicate_envelopes,
|
declarePublicCounter envelopes_dropped,
|
||||||
"Dropped benign duplicate envelopes"
|
"Dropped envelopes", labels = ["reason"]
|
||||||
declarePublicCounter dropped_expired_envelopes,
|
|
||||||
"Dropped envelopes because expired"
|
|
||||||
declarePublicCounter dropped_from_future_envelopes,
|
|
||||||
"Dropped envelopes because of future timestamp"
|
|
||||||
declarePublicCounter dropped_full_queue_new_envelopes,
|
|
||||||
"New valid envelopes dropped because of full queue"
|
|
||||||
declarePublicCounter dropped_full_queue_old_envelopes,
|
|
||||||
"Old valid envelopes dropped because of full queue"
|
|
||||||
|
|
||||||
const
|
const
|
||||||
flagsLen = 1 ## payload flags field length, bytes
|
flagsLen = 1 ## payload flags field length, bytes
|
||||||
|
@ -207,6 +199,7 @@ proc encryptAesGcm(plain: openarray[byte], key: SymKey,
|
||||||
gcm.encrypt(plain, result)
|
gcm.encrypt(plain, result)
|
||||||
var tag: array[gcmTagLen, byte]
|
var tag: array[gcmTagLen, byte]
|
||||||
gcm.getTag(tag)
|
gcm.getTag(tag)
|
||||||
|
gcm.clear()
|
||||||
result.add tag
|
result.add tag
|
||||||
result.add iv
|
result.add iv
|
||||||
|
|
||||||
|
@ -225,6 +218,7 @@ proc decryptAesGcm(cipher: openarray[byte], key: SymKey): Option[seq[byte]] =
|
||||||
gcm.decrypt(cipher[0 ..< ^(gcmIVLen + gcmTagLen)], res)
|
gcm.decrypt(cipher[0 ..< ^(gcmIVLen + gcmTagLen)], res)
|
||||||
var tag2: array[gcmTagLen, byte]
|
var tag2: array[gcmTagLen, byte]
|
||||||
gcm.getTag(tag2)
|
gcm.getTag(tag2)
|
||||||
|
gcm.clear()
|
||||||
|
|
||||||
if tag != tag2:
|
if tag != tag2:
|
||||||
debug "cipher tag mismatch", len = cipher.len, tag, tag2
|
debug "cipher tag mismatch", len = cipher.len, tag, tag2
|
||||||
|
@ -411,15 +405,15 @@ proc decode*(data: openarray[byte], dst = none[PrivateKey](),
|
||||||
|
|
||||||
proc valid*(self: Envelope, now = epochTime()): bool =
|
proc valid*(self: Envelope, now = epochTime()): bool =
|
||||||
if self.expiry.float64 < now: # expired
|
if self.expiry.float64 < now: # expired
|
||||||
dropped_expired_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["expired"])
|
||||||
return false
|
return false
|
||||||
if self.ttl <= 0: # this would invalidate pow calculation
|
if self.ttl <= 0: # this would invalidate pow calculation
|
||||||
dropped_expired_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["expired"])
|
||||||
return false
|
return false
|
||||||
|
|
||||||
let created = self.expiry - self.ttl
|
let created = self.expiry - self.ttl
|
||||||
if created.float64 > (now + 2.0): # created in the future
|
if created.float64 > (now + 2.0): # created in the future
|
||||||
dropped_from_future_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["future_timestamp"])
|
||||||
return false
|
return false
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -556,10 +550,10 @@ proc add*(self: var Queue, msg: Message): bool =
|
||||||
|
|
||||||
# check for duplicate before pruning
|
# check for duplicate before pruning
|
||||||
if self.itemHashes.contains(msg.hash):
|
if self.itemHashes.contains(msg.hash):
|
||||||
dropped_benign_duplicate_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["benign_duplicate"])
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
valid_envelopes.inc()
|
envelopes_valid.inc()
|
||||||
if self.items.len >= self.capacity:
|
if self.items.len >= self.capacity:
|
||||||
self.prune() # Only prune if needed
|
self.prune() # Only prune if needed
|
||||||
|
|
||||||
|
@ -570,12 +564,12 @@ proc add*(self: var Queue, msg: Message): bool =
|
||||||
if last.pow > msg.pow or
|
if last.pow > msg.pow or
|
||||||
(last.pow == msg.pow and last.env.expiry > msg.env.expiry):
|
(last.pow == msg.pow and last.env.expiry > msg.env.expiry):
|
||||||
# The new message has less pow or will expire earlier - drop it
|
# The new message has less pow or will expire earlier - drop it
|
||||||
dropped_full_queue_new_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["full_queue_new"])
|
||||||
return false
|
return false
|
||||||
|
|
||||||
self.items.del(self.items.len() - 1)
|
self.items.del(self.items.len() - 1)
|
||||||
self.itemHashes.excl(last.hash)
|
self.itemHashes.excl(last.hash)
|
||||||
dropped_full_queue_old_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["full_queue_old"])
|
||||||
|
|
||||||
self.itemHashes.incl(msg.hash)
|
self.itemHashes.incl(msg.hash)
|
||||||
self.items.insert(msg, self.items.lowerBound(msg, cmpPow))
|
self.items.insert(msg, self.items.lowerBound(msg, cmpPow))
|
||||||
|
|
|
@ -42,15 +42,6 @@ export
|
||||||
logScope:
|
logScope:
|
||||||
topics = "whisper"
|
topics = "whisper"
|
||||||
|
|
||||||
declarePublicCounter dropped_low_pow_envelopes,
|
|
||||||
"Dropped envelopes because of too low PoW"
|
|
||||||
declarePublicCounter dropped_too_large_envelopes,
|
|
||||||
"Dropped envelopes because larger than maximum allowed size"
|
|
||||||
declarePublicCounter dropped_bloom_filter_mismatch_envelopes,
|
|
||||||
"Dropped envelopes because not matching with bloom filter"
|
|
||||||
declarePublicCounter dropped_duplicate_envelopes,
|
|
||||||
"Dropped duplicate envelopes"
|
|
||||||
|
|
||||||
const
|
const
|
||||||
defaultQueueCapacity = 2048
|
defaultQueueCapacity = 2048
|
||||||
whisperVersion* = 6 ## Whisper version.
|
whisperVersion* = 6 ## Whisper version.
|
||||||
|
@ -87,17 +78,17 @@ proc allowed*(msg: Message, config: WhisperConfig): bool =
|
||||||
# Check max msg size, already happens in RLPx but there is a specific shh
|
# 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
|
# max msg size which should always be < RLPx max msg size
|
||||||
if msg.size > config.maxMsgSize:
|
if msg.size > config.maxMsgSize:
|
||||||
dropped_too_large_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["too_large"])
|
||||||
warn "Message size too large", size = msg.size
|
warn "Message size too large", size = msg.size
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if msg.pow < config.powRequirement:
|
if msg.pow < config.powRequirement:
|
||||||
dropped_low_pow_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["low_pow"])
|
||||||
warn "Message PoW too low", pow = msg.pow, minPow = config.powRequirement
|
warn "Message PoW too low", pow = msg.pow, minPow = config.powRequirement
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if not bloomFilterMatch(config.bloom, msg.bloom):
|
if not bloomFilterMatch(config.bloom, msg.bloom):
|
||||||
dropped_bloom_filter_mismatch_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["bloom_filter_mismatch"])
|
||||||
warn "Message does not match node bloom filter"
|
warn "Message does not match node bloom filter"
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
@ -202,7 +193,7 @@ p2pProtocol Whisper(version = whisperVersion,
|
||||||
# this node to a peer and that same message arriving from that peer (after
|
# this node to a peer and that same message arriving from that peer (after
|
||||||
# it was received from another peer) here.
|
# it was received from another peer) here.
|
||||||
if peer.state.received.containsOrIncl(msg.hash):
|
if peer.state.received.containsOrIncl(msg.hash):
|
||||||
dropped_duplicate_envelopes.inc()
|
envelopes_dropped.inc(labelValues = ["duplicate"])
|
||||||
trace "Peer sending duplicate messages", peer, hash = $msg.hash
|
trace "Peer sending duplicate messages", peer, hash = $msg.hash
|
||||||
# await peer.disconnect(SubprotocolReason)
|
# await peer.disconnect(SubprotocolReason)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue