Add discoveryv5 session metrics (#454)

This commit is contained in:
Kim De Mey 2021-12-21 15:09:46 +01:00 committed by GitHub
parent 8b7d41f596
commit 42e257796f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -15,7 +15,7 @@
import import
std/[tables, options, hashes, net], std/[tables, options, hashes, net],
nimcrypto, stint, chronicles, bearssl, stew/[results, byteutils], nimcrypto, stint, chronicles, bearssl, stew/[results, byteutils], metrics,
".."/../[rlp, keys], ".."/../[rlp, keys],
"."/[messages, node, enr, hkdf, sessions] "."/[messages, node, enr, hkdf, sessions]
@ -23,6 +23,10 @@ from stew/objects import checkedEnumAssign
export keys export keys
declareCounter discovery_session_lru_cache_hits, "Session LRU cache hits"
declareCounter discovery_session_lru_cache_misses, "Session LRU cache misses"
declareCounter discovery_session_decrypt_failures, "Session decrypt failures"
logScope: logScope:
topics = "discv5" topics = "discv5"
@ -206,6 +210,7 @@ proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
var initiatorKey, recipientKey: AesKey var initiatorKey, recipientKey: AesKey
if c.sessions.load(toId, toAddr, recipientKey, initiatorKey): if c.sessions.load(toId, toAddr, recipientKey, initiatorKey):
messageEncrypted = encryptGCM(initiatorKey, nonce, message, @iv & header) messageEncrypted = encryptGCM(initiatorKey, nonce, message, @iv & header)
discovery_session_lru_cache_hits.inc()
else: else:
# We might not have the node's keys if the handshake hasn't been performed # We might not have the node's keys if the handshake hasn't been performed
# yet. That's fine, we send a random-packet and we will be responded with # yet. That's fine, we send a random-packet and we will be responded with
@ -217,6 +222,7 @@ proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
var randomData: array[gcmTagSize + 4, byte] var randomData: array[gcmTagSize + 4, byte]
brHmacDrbgGenerate(rng, randomData) brHmacDrbgGenerate(rng, randomData)
messageEncrypted.add(randomData) messageEncrypted.add(randomData)
discovery_session_lru_cache_misses.inc()
let maskedHeader = encryptHeader(toId, iv, header) let maskedHeader = encryptHeader(toId, iv, header)
@ -423,15 +429,19 @@ proc decodeMessagePacket(c: var Codec, fromAddr: Address, nonce: AESGCMNonce,
# Don't consider this an error, simply haven't done a handshake yet or # Don't consider this an error, simply haven't done a handshake yet or
# the session got removed. # the session got removed.
trace "Decrypting failed (no keys)" trace "Decrypting failed (no keys)"
discovery_session_lru_cache_misses.inc()
return ok(Packet(flag: Flag.OrdinaryMessage, requestNonce: nonce, return ok(Packet(flag: Flag.OrdinaryMessage, requestNonce: nonce,
srcId: srcId)) srcId: srcId))
discovery_session_lru_cache_hits.inc()
let pt = decryptGCM(recipientKey, nonce, ct, @iv & @header) let pt = decryptGCM(recipientKey, nonce, ct, @iv & @header)
if pt.isNone(): if pt.isNone():
# Don't consider this an error, the session got probably removed at the # Don't consider this an error, the session got probably removed at the
# peer's side and a random message is send. # peer's side and a random message is send.
trace "Decrypting failed (invalid keys)" trace "Decrypting failed (invalid keys)"
c.sessions.del(srcId, fromAddr) c.sessions.del(srcId, fromAddr)
discovery_session_decrypt_failures.inc()
return ok(Packet(flag: Flag.OrdinaryMessage, requestNonce: nonce, return ok(Packet(flag: Flag.OrdinaryMessage, requestNonce: nonce,
srcId: srcId)) srcId: srcId))