@@ -17,6 +17,13 @@ const {
17
17
18
18
const log = debug ( 'libp2p:gossipsub:score' )
19
19
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
+
20
27
export class PeerScore {
21
28
/**
22
29
* The score parameters
@@ -33,11 +40,7 @@ export class PeerScore {
33
40
/**
34
41
* Cache score up to decayInterval if topic stats are unchanged.
35
42
*/
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 >
41
44
/**
42
45
* Recent message delivery timing/participants
43
46
*/
@@ -56,7 +59,6 @@ export class PeerScore {
56
59
this . peerStats = new Map ( )
57
60
this . peerIPs = new Map ( )
58
61
this . scoreCache = new Map ( )
59
- this . scoreCacheUntil = new Map ( )
60
62
this . deliveryRecords = new MessageDeliveries ( )
61
63
this . msgId = msgId
62
64
}
@@ -164,7 +166,7 @@ export class PeerScore {
164
166
pstats . behaviourPenalty = 0
165
167
}
166
168
167
- this . scoreCacheUntil . set ( id , 0 )
169
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
168
170
} )
169
171
}
170
172
@@ -180,17 +182,19 @@ export class PeerScore {
180
182
}
181
183
182
184
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 )
187
189
}
188
190
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 )
190
195
// 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
194
198
}
195
199
196
200
/**
@@ -205,7 +209,7 @@ export class PeerScore {
205
209
return
206
210
}
207
211
pstats . behaviourPenalty += penalty
208
- this . scoreCacheUntil . set ( id , 0 )
212
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
209
213
}
210
214
211
215
/**
@@ -246,7 +250,6 @@ export class PeerScore {
246
250
247
251
// delete score cache
248
252
this . scoreCache . delete ( id )
249
- this . scoreCacheUntil . delete ( id )
250
253
251
254
// furthermore, when we decide to retain the score, the firstMessageDelivery counters are
252
255
// reset to 0 and mesh delivery penalties applied.
@@ -286,7 +289,7 @@ export class PeerScore {
286
289
tstats . graftTime = Date . now ( )
287
290
tstats . meshTime = 0
288
291
tstats . meshMessageDeliveriesActive = false
289
- this . scoreCacheUntil . set ( id , 0 )
292
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
290
293
}
291
294
292
295
/**
@@ -312,7 +315,7 @@ export class PeerScore {
312
315
tstats . meshFailurePenalty += deficit * deficit
313
316
}
314
317
tstats . inMesh = false
315
- this . scoreCacheUntil . set ( id , 0 )
318
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
316
319
}
317
320
318
321
/**
@@ -447,7 +450,7 @@ export class PeerScore {
447
450
448
451
tstats . invalidMessageDeliveries += 1
449
452
} )
450
- this . scoreCacheUntil . set ( id , 0 )
453
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
451
454
}
452
455
453
456
/**
@@ -485,7 +488,7 @@ export class PeerScore {
485
488
tstats . meshMessageDeliveries = cap
486
489
}
487
490
} )
488
- this . scoreCacheUntil . set ( id , 0 )
491
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
489
492
}
490
493
491
494
/**
@@ -529,7 +532,7 @@ export class PeerScore {
529
532
tstats . meshMessageDeliveries = cap
530
533
}
531
534
} )
532
- this . scoreCacheUntil . set ( id , 0 )
535
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
533
536
}
534
537
535
538
/**
@@ -591,7 +594,7 @@ export class PeerScore {
591
594
}
592
595
}
593
596
594
- this . scoreCacheUntil . set ( id , 0 )
597
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
595
598
}
596
599
597
600
/**
@@ -613,7 +616,7 @@ export class PeerScore {
613
616
}
614
617
} )
615
618
616
- this . scoreCacheUntil . set ( id , 0 )
619
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
617
620
}
618
621
619
622
/**
@@ -625,7 +628,7 @@ export class PeerScore {
625
628
const newIPs = this . _getIPs ( id )
626
629
this . _setIPs ( id , newIPs , pstats . ips )
627
630
pstats . ips = newIPs
628
- this . scoreCacheUntil . set ( id , 0 )
631
+ this . scoreCache . set ( id , { score : null , cacheUntil : 0 } )
629
632
} )
630
633
}
631
634
}
0 commit comments