@@ -30,6 +30,15 @@ export class PeerScore {
30
30
* IP colocation tracking; maps IP => set of peers.
31
31
*/
32
32
peerIPs : Map < string , Set < string > >
33
+ scoreCache : Map < string , number >
34
+ /**
35
+ * Flag to mark a peer score cache valid or not.
36
+ */
37
+ scoreCacheValid : Map < string , boolean >
38
+ /**
39
+ * The last time the score for a peer was cached.
40
+ */
41
+ scoreCacheTime : Map < string , number >
33
42
/**
34
43
* Recent message delivery timing/participants
35
44
*/
@@ -47,6 +56,9 @@ export class PeerScore {
47
56
this . _connectionManager = connectionManager
48
57
this . peerStats = new Map ( )
49
58
this . peerIPs = new Map ( )
59
+ this . scoreCache = new Map ( )
60
+ this . scoreCacheValid = new Map ( )
61
+ this . scoreCacheTime = new Map ( )
50
62
this . deliveryRecords = new MessageDeliveries ( )
51
63
this . msgId = msgId
52
64
}
@@ -153,6 +165,8 @@ export class PeerScore {
153
165
if ( pstats . behaviourPenalty < decayToZero ) {
154
166
pstats . behaviourPenalty = 0
155
167
}
168
+
169
+ this . scoreCacheValid . set ( id , false )
156
170
} )
157
171
}
158
172
@@ -166,7 +180,18 @@ export class PeerScore {
166
180
if ( ! pstats ) {
167
181
return 0
168
182
}
169
- return computeScore ( id , pstats , this . params , this . peerIPs )
183
+
184
+ const now = Date . now ( )
185
+ if ( this . scoreCacheValid . get ( id ) && now - ( this . scoreCacheTime . get ( id ) ?? 0 ) < this . params . decayInterval ) {
186
+ const score = this . scoreCache . get ( id )
187
+ if ( score !== undefined ) return score
188
+ }
189
+
190
+ const score = computeScore ( id , pstats , this . params , this . peerIPs )
191
+ this . scoreCacheValid . set ( id , true )
192
+ this . scoreCacheTime . set ( id , now )
193
+ this . scoreCache . set ( id , score )
194
+ return score
170
195
}
171
196
172
197
/**
@@ -181,6 +206,7 @@ export class PeerScore {
181
206
return
182
207
}
183
208
pstats . behaviourPenalty += penalty
209
+ this . scoreCacheValid . set ( id , false )
184
210
}
185
211
186
212
/**
@@ -199,6 +225,10 @@ export class PeerScore {
199
225
const ips = this . _getIPs ( id )
200
226
this . _setIPs ( id , ips , pstats . ips )
201
227
pstats . ips = ips
228
+
229
+ // initialize score cache
230
+ this . scoreCacheTime . set ( id , 0 )
231
+ this . scoreCacheValid . set ( id , false )
202
232
}
203
233
204
234
/**
@@ -219,6 +249,11 @@ export class PeerScore {
219
249
return
220
250
}
221
251
252
+ // delete score cache
253
+ this . scoreCache . delete ( id )
254
+ this . scoreCacheTime . delete ( id )
255
+ this . scoreCacheValid . delete ( id )
256
+
222
257
// furthermore, when we decide to retain the score, the firstMessageDelivery counters are
223
258
// reset to 0 and mesh delivery penalties applied.
224
259
Object . entries ( pstats . topics ) . forEach ( ( [ topic , tstats ] ) => {
@@ -257,6 +292,7 @@ export class PeerScore {
257
292
tstats . graftTime = Date . now ( )
258
293
tstats . meshTime = 0
259
294
tstats . meshMessageDeliveriesActive = false
295
+ this . scoreCacheValid . set ( id , false )
260
296
}
261
297
262
298
/**
@@ -282,6 +318,7 @@ export class PeerScore {
282
318
tstats . meshFailurePenalty += deficit * deficit
283
319
}
284
320
tstats . inMesh = false
321
+ this . scoreCacheValid . set ( id , false )
285
322
}
286
323
287
324
/**
@@ -416,6 +453,7 @@ export class PeerScore {
416
453
417
454
tstats . invalidMessageDeliveries += 1
418
455
} )
456
+ this . scoreCacheValid . set ( id , false )
419
457
}
420
458
421
459
/**
@@ -453,6 +491,7 @@ export class PeerScore {
453
491
tstats . meshMessageDeliveries = cap
454
492
}
455
493
} )
494
+ this . scoreCacheValid . set ( id , false )
456
495
}
457
496
458
497
/**
@@ -496,6 +535,7 @@ export class PeerScore {
496
535
tstats . meshMessageDeliveries = cap
497
536
}
498
537
} )
538
+ this . scoreCacheValid . set ( id , false )
499
539
}
500
540
501
541
/**
@@ -556,6 +596,8 @@ export class PeerScore {
556
596
this . peerIPs . delete ( ip )
557
597
}
558
598
}
599
+
600
+ this . scoreCacheValid . set ( id , false )
559
601
}
560
602
561
603
/**
@@ -576,6 +618,8 @@ export class PeerScore {
576
618
this . peerIPs . delete ( ip )
577
619
}
578
620
} )
621
+
622
+ this . scoreCacheValid . set ( id , false )
579
623
}
580
624
581
625
/**
@@ -587,6 +631,7 @@ export class PeerScore {
587
631
const newIPs = this . _getIPs ( id )
588
632
this . _setIPs ( id , newIPs , pstats . ips )
589
633
pstats . ips = newIPs
634
+ this . scoreCacheValid . set ( id , false )
590
635
} )
591
636
}
592
637
}
0 commit comments