Skip to content

Commit 2029715

Browse files
committed
Unify to a single score cache map
1 parent f395258 commit 2029715

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

ts/score/peer-score.ts

+28-25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ const {
1717

1818
const log = debug('libp2p:gossipsub:score')
1919

20+
interface ScoreCacheEntry {
21+
/** The cached score, null if not cached */
22+
score: number | null
23+
/** Unix timestamp in miliseconds, the time after which the cached score for a peer is no longer valid */
24+
cacheUntil: number
25+
}
26+
2027
export class PeerScore {
2128
/**
2229
* The score parameters
@@ -33,11 +40,7 @@ export class PeerScore {
3340
/**
3441
* Cache score up to decayInterval if topic stats are unchanged.
3542
*/
36-
scoreCache: Map<string, number>
37-
/**
38-
* The time after which the cached score for a peer is no longer valid.
39-
*/
40-
scoreCacheUntil: Map<string, number>
43+
scoreCache: Map<string, ScoreCacheEntry>
4144
/**
4245
* Recent message delivery timing/participants
4346
*/
@@ -56,7 +59,6 @@ export class PeerScore {
5659
this.peerStats = new Map()
5760
this.peerIPs = new Map()
5861
this.scoreCache = new Map()
59-
this.scoreCacheUntil = new Map()
6062
this.deliveryRecords = new MessageDeliveries()
6163
this.msgId = msgId
6264
}
@@ -164,7 +166,7 @@ export class PeerScore {
164166
pstats.behaviourPenalty = 0
165167
}
166168

167-
this.scoreCacheUntil.set(id, 0)
169+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
168170
})
169171
}
170172

@@ -180,17 +182,19 @@ export class PeerScore {
180182
}
181183

182184
const now = Date.now()
183-
const cacheUntil = this.scoreCacheUntil.get(id)
184-
if (cacheUntil !== undefined && cacheUntil > now) {
185-
const score = this.scoreCache.get(id)
186-
if (score !== undefined) return score
185+
let cacheEntry = this.scoreCache.get(id)
186+
if (cacheEntry === undefined) {
187+
cacheEntry = { score: null, cacheUntil: 0 }
188+
this.scoreCache.set(id, cacheEntry)
187189
}
188190

189-
const score = computeScore(id, pstats, this.params, this.peerIPs)
191+
const { score, cacheUntil } = cacheEntry
192+
if (cacheUntil > now && score !== null) return score
193+
194+
cacheEntry.score = computeScore(id, pstats, this.params, this.peerIPs)
190195
// decayInterval is used to refresh score so we don't want to cache more than that
191-
this.scoreCacheUntil.set(id, now + this.params.decayInterval)
192-
this.scoreCache.set(id, score)
193-
return score
196+
cacheEntry.cacheUntil = now + this.params.decayInterval
197+
return cacheEntry.score
194198
}
195199

196200
/**
@@ -205,7 +209,7 @@ export class PeerScore {
205209
return
206210
}
207211
pstats.behaviourPenalty += penalty
208-
this.scoreCacheUntil.set(id, 0)
212+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
209213
}
210214

211215
/**
@@ -246,7 +250,6 @@ export class PeerScore {
246250

247251
// delete score cache
248252
this.scoreCache.delete(id)
249-
this.scoreCacheUntil.delete(id)
250253

251254
// furthermore, when we decide to retain the score, the firstMessageDelivery counters are
252255
// reset to 0 and mesh delivery penalties applied.
@@ -286,7 +289,7 @@ export class PeerScore {
286289
tstats.graftTime = Date.now()
287290
tstats.meshTime = 0
288291
tstats.meshMessageDeliveriesActive = false
289-
this.scoreCacheUntil.set(id, 0)
292+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
290293
}
291294

292295
/**
@@ -312,7 +315,7 @@ export class PeerScore {
312315
tstats.meshFailurePenalty += deficit * deficit
313316
}
314317
tstats.inMesh = false
315-
this.scoreCacheUntil.set(id, 0)
318+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
316319
}
317320

318321
/**
@@ -447,7 +450,7 @@ export class PeerScore {
447450

448451
tstats.invalidMessageDeliveries += 1
449452
})
450-
this.scoreCacheUntil.set(id, 0)
453+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
451454
}
452455

453456
/**
@@ -485,7 +488,7 @@ export class PeerScore {
485488
tstats.meshMessageDeliveries = cap
486489
}
487490
})
488-
this.scoreCacheUntil.set(id, 0)
491+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
489492
}
490493

491494
/**
@@ -529,7 +532,7 @@ export class PeerScore {
529532
tstats.meshMessageDeliveries = cap
530533
}
531534
})
532-
this.scoreCacheUntil.set(id, 0)
535+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
533536
}
534537

535538
/**
@@ -591,7 +594,7 @@ export class PeerScore {
591594
}
592595
}
593596

594-
this.scoreCacheUntil.set(id, 0)
597+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
595598
}
596599

597600
/**
@@ -613,7 +616,7 @@ export class PeerScore {
613616
}
614617
})
615618

616-
this.scoreCacheUntil.set(id, 0)
619+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
617620
}
618621

619622
/**
@@ -625,7 +628,7 @@ export class PeerScore {
625628
const newIPs = this._getIPs(id)
626629
this._setIPs(id, newIPs, pstats.ips)
627630
pstats.ips = newIPs
628-
this.scoreCacheUntil.set(id, 0)
631+
this.scoreCache.set(id, { score: null, cacheUntil: 0 })
629632
})
630633
}
631634
}

0 commit comments

Comments
 (0)