1
- import { CodeError } from '@libp2p/interface'
2
- import { ERR_INVALID_PEER_SCORE_PARAMS } from './constants.js'
1
+ import { InvalidPeerScoreParamsError } from '../errors.js'
3
2
4
3
// This file defines PeerScoreParams and TopicScoreParams interfaces
5
4
// as well as constructors, default constructors, and validation functions
@@ -203,51 +202,42 @@ export function validatePeerScoreParams (p: PeerScoreParams): void {
203
202
try {
204
203
validateTopicScoreParams ( params )
205
204
} catch ( e ) {
206
- throw new CodeError (
207
- `invalid score parameters for topic ${ topic } : ${ ( e as Error ) . message } ` ,
208
- ERR_INVALID_PEER_SCORE_PARAMS
209
- )
205
+ throw new InvalidPeerScoreParamsError ( `invalid score parameters for topic ${ topic } : ${ ( e as Error ) . message } ` )
210
206
}
211
207
}
212
208
213
209
// check that the topic score is 0 or something positive
214
210
if ( p . topicScoreCap < 0 ) {
215
- throw new CodeError ( 'invalid topic score cap; must be positive (or 0 for no cap)' , ERR_INVALID_PEER_SCORE_PARAMS )
211
+ throw new InvalidPeerScoreParamsError ( 'invalid topic score cap; must be positive (or 0 for no cap)' )
216
212
}
217
213
218
214
// check that we have an app specific score; the weight can be anything (but expected positive)
219
215
if ( p . appSpecificScore === null || p . appSpecificScore === undefined ) {
220
- throw new CodeError ( 'missing application specific score function' , ERR_INVALID_PEER_SCORE_PARAMS )
216
+ throw new InvalidPeerScoreParamsError ( 'missing application specific score function' )
221
217
}
222
218
223
219
// check the IP colocation factor
224
220
if ( p . IPColocationFactorWeight > 0 ) {
225
- throw new CodeError (
226
- 'invalid IPColocationFactorWeight; must be negative (or 0 to disable)' ,
227
- ERR_INVALID_PEER_SCORE_PARAMS
228
- )
221
+ throw new InvalidPeerScoreParamsError ( 'invalid IPColocationFactorWeight; must be negative (or 0 to disable)' )
229
222
}
230
223
if ( p . IPColocationFactorWeight !== 0 && p . IPColocationFactorThreshold < 1 ) {
231
- throw new CodeError ( 'invalid IPColocationFactorThreshold; must be at least 1' , ERR_INVALID_PEER_SCORE_PARAMS )
224
+ throw new InvalidPeerScoreParamsError ( 'invalid IPColocationFactorThreshold; must be at least 1' )
232
225
}
233
226
234
227
// check the behaviour penalty
235
228
if ( p . behaviourPenaltyWeight > 0 ) {
236
- throw new CodeError (
237
- 'invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)' ,
238
- ERR_INVALID_PEER_SCORE_PARAMS
239
- )
229
+ throw new InvalidPeerScoreParamsError ( 'invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)' )
240
230
}
241
231
if ( p . behaviourPenaltyWeight !== 0 && ( p . behaviourPenaltyDecay <= 0 || p . behaviourPenaltyDecay >= 1 ) ) {
242
- throw new CodeError ( 'invalid BehaviourPenaltyDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
232
+ throw new InvalidPeerScoreParamsError ( 'invalid BehaviourPenaltyDecay; must be between 0 and 1' )
243
233
}
244
234
245
235
// check the decay parameters
246
236
if ( p . decayInterval < 1000 ) {
247
- throw new CodeError ( 'invalid DecayInterval; must be at least 1s' , ERR_INVALID_PEER_SCORE_PARAMS )
237
+ throw new InvalidPeerScoreParamsError ( 'invalid DecayInterval; must be at least 1s' )
248
238
}
249
239
if ( p . decayToZero <= 0 || p . decayToZero >= 1 ) {
250
- throw new CodeError ( 'invalid DecayToZero; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
240
+ throw new InvalidPeerScoreParamsError ( 'invalid DecayToZero; must be between 0 and 1' )
251
241
}
252
242
253
243
// no need to check the score retention; a value of 0 means that we don't retain scores
@@ -257,82 +247,70 @@ export function validatePeerScoreParams (p: PeerScoreParams): void {
257
247
export function validateTopicScoreParams ( p : TopicScoreParams ) : void {
258
248
// make sure we have a sane topic weight
259
249
if ( p . topicWeight < 0 ) {
260
- throw new CodeError ( 'invalid topic weight; must be >= 0' , ERR_INVALID_PEER_SCORE_PARAMS )
250
+ throw new InvalidPeerScoreParamsError ( 'invalid topic weight; must be >= 0' )
261
251
}
262
252
263
253
// check P1
264
254
if ( p . timeInMeshQuantum === 0 ) {
265
- throw new CodeError ( 'invalid TimeInMeshQuantum; must be non zero' , ERR_INVALID_PEER_SCORE_PARAMS )
255
+ throw new InvalidPeerScoreParamsError ( 'invalid TimeInMeshQuantum; must be non zero' )
266
256
}
267
257
if ( p . timeInMeshWeight < 0 ) {
268
- throw new CodeError ( 'invalid TimeInMeshWeight; must be positive (or 0 to disable)' , ERR_INVALID_PEER_SCORE_PARAMS )
258
+ throw new InvalidPeerScoreParamsError ( 'invalid TimeInMeshWeight; must be positive (or 0 to disable)' )
269
259
}
270
260
if ( p . timeInMeshWeight !== 0 && p . timeInMeshQuantum <= 0 ) {
271
- throw new CodeError ( 'invalid TimeInMeshQuantum; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
261
+ throw new InvalidPeerScoreParamsError ( 'invalid TimeInMeshQuantum; must be positive' )
272
262
}
273
263
if ( p . timeInMeshWeight !== 0 && p . timeInMeshCap <= 0 ) {
274
- throw new CodeError ( 'invalid TimeInMeshCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
264
+ throw new InvalidPeerScoreParamsError ( 'invalid TimeInMeshCap; must be positive' )
275
265
}
276
266
277
267
// check P2
278
268
if ( p . firstMessageDeliveriesWeight < 0 ) {
279
- throw new CodeError (
280
- 'invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)' ,
281
- ERR_INVALID_PEER_SCORE_PARAMS
282
- )
269
+ throw new InvalidPeerScoreParamsError ( 'invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)' )
283
270
}
284
271
if (
285
272
p . firstMessageDeliveriesWeight !== 0 &&
286
273
( p . firstMessageDeliveriesDecay <= 0 || p . firstMessageDeliveriesDecay >= 1 )
287
274
) {
288
- throw new CodeError ( 'invalid FirstMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
275
+ throw new InvalidPeerScoreParamsError ( 'invalid FirstMessageDeliveriesDecay; must be between 0 and 1' )
289
276
}
290
277
if ( p . firstMessageDeliveriesWeight !== 0 && p . firstMessageDeliveriesCap <= 0 ) {
291
- throw new CodeError ( 'invalid FirstMessageDeliveriesCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
278
+ throw new InvalidPeerScoreParamsError ( 'invalid FirstMessageDeliveriesCap; must be positive' )
292
279
}
293
280
294
281
// check P3
295
282
if ( p . meshMessageDeliveriesWeight > 0 ) {
296
- throw new CodeError (
297
- 'invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)' ,
298
- ERR_INVALID_PEER_SCORE_PARAMS
299
- )
283
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)' )
300
284
}
301
285
if ( p . meshMessageDeliveriesWeight !== 0 && ( p . meshMessageDeliveriesDecay <= 0 || p . meshMessageDeliveriesDecay >= 1 ) ) {
302
- throw new CodeError ( 'invalid MeshMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
286
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesDecay; must be between 0 and 1' )
303
287
}
304
288
if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesCap <= 0 ) {
305
- throw new CodeError ( 'invalid MeshMessageDeliveriesCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
289
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesCap; must be positive' )
306
290
}
307
291
if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesThreshold <= 0 ) {
308
- throw new CodeError ( 'invalid MeshMessageDeliveriesThreshold; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
292
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesThreshold; must be positive' )
309
293
}
310
294
if ( p . meshMessageDeliveriesWindow < 0 ) {
311
- throw new CodeError ( 'invalid MeshMessageDeliveriesWindow; must be non-negative' , ERR_INVALID_PEER_SCORE_PARAMS )
295
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesWindow; must be non-negative' )
312
296
}
313
297
if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesActivation < 1000 ) {
314
- throw new CodeError ( 'invalid MeshMessageDeliveriesActivation; must be at least 1s' , ERR_INVALID_PEER_SCORE_PARAMS )
298
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshMessageDeliveriesActivation; must be at least 1s' )
315
299
}
316
300
317
301
// check P3b
318
302
if ( p . meshFailurePenaltyWeight > 0 ) {
319
- throw new CodeError (
320
- 'invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)' ,
321
- ERR_INVALID_PEER_SCORE_PARAMS
322
- )
303
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)' )
323
304
}
324
305
if ( p . meshFailurePenaltyWeight !== 0 && ( p . meshFailurePenaltyDecay <= 0 || p . meshFailurePenaltyDecay >= 1 ) ) {
325
- throw new CodeError ( 'invalid MeshFailurePenaltyDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
306
+ throw new InvalidPeerScoreParamsError ( 'invalid MeshFailurePenaltyDecay; must be between 0 and 1' )
326
307
}
327
308
328
309
// check P4
329
310
if ( p . invalidMessageDeliveriesWeight > 0 ) {
330
- throw new CodeError (
331
- 'invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)' ,
332
- ERR_INVALID_PEER_SCORE_PARAMS
333
- )
311
+ throw new InvalidPeerScoreParamsError ( 'invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)' )
334
312
}
335
313
if ( p . invalidMessageDeliveriesDecay <= 0 || p . invalidMessageDeliveriesDecay >= 1 ) {
336
- throw new CodeError ( 'invalid InvalidMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
314
+ throw new InvalidPeerScoreParamsError ( 'invalid InvalidMessageDeliveriesDecay; must be between 0 and 1' )
337
315
}
338
316
}
0 commit comments