diff --git a/src/index.ts b/src/index.ts index 197380db..77397989 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2727,6 +2727,7 @@ export class GossipSub extends EventEmitter implements Initiali private onScrapeMetrics(metrics: Metrics): void { /* Data structure sizes */ metrics.mcacheSize.set(this.mcache.size) + metrics.mcacheNotValidatedCount.set(this.mcache.notValidatedCount) // Arbitrary size metrics.cacheSize.set({ cache: 'direct' }, this.direct.size) metrics.cacheSize.set({ cache: 'seenCache' }, this.seenCache.size) diff --git a/src/message-cache.ts b/src/message-cache.ts index b33fc79e..6db09404 100644 --- a/src/message-cache.ts +++ b/src/message-cache.ts @@ -28,6 +28,9 @@ export class MessageCache { history: CacheEntry[][] = [] + /** Track with accounting of messages in the mcache that are not yet validated */ + notValidatedCount = 0 + /** * Holds history of messages in timebounded history arrays */ @@ -71,6 +74,10 @@ export class MessageCache { this.history[0].push({ ...messageId, topic: msg.topic }) + if (!validated) { + this.notValidatedCount++ + } + return true } @@ -143,6 +150,10 @@ export class MessageCache { return null } + if (!entry.validated) { + this.notValidatedCount-- + } + const { message, originatingPeers } = entry entry.validated = true // Clear the known peers list (after a message is validated, it is forwarded and we no @@ -155,9 +166,15 @@ export class MessageCache { * Shifts the current window, discarding messages older than this.history.length of the cache */ shift(): void { - const last = this.history[this.history.length - 1] - last.forEach((entry) => { - this.msgs.delete(entry.msgIdStr) + const lastCacheEntries = this.history[this.history.length - 1] + lastCacheEntries.forEach((cacheEntry) => { + const entry = this.msgs.get(cacheEntry.msgIdStr) + if (entry) { + this.msgs.delete(cacheEntry.msgIdStr) + if (!entry.validated) { + this.notValidatedCount-- + } + } }) this.history.pop() diff --git a/src/metrics.ts b/src/metrics.ts index 0d47f8ea..a09ebf1b 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -500,6 +500,10 @@ export function getMetrics( name: 'gossipsub_mcache_size', help: 'Current mcache msg count' }), + mcacheNotValidatedCount: register.gauge({ + name: 'gossipsub_mcache_not_validated_count', + help: 'Current mcache msg count not validated' + }), topicStrToLabel: topicStrToLabel,