11import { ERR_INVALID_PEER_SCORE_PARAMS } from './constants.js'
2- import errcode from 'err-code '
2+ import { CodeError } from '@libp2p/interfaces/errors '
33
44// This file defines PeerScoreParams and TopicScoreParams interfaces
55// as well as constructors, default constructors, and validation functions
@@ -203,54 +203,51 @@ export function validatePeerScoreParams(p: PeerScoreParams): void {
203203 try {
204204 validateTopicScoreParams ( params )
205205 } catch ( e ) {
206- throw errcode (
207- new Error ( `invalid score parameters for topic ${ topic } : ${ ( e as Error ) . message } ` ) ,
206+ throw new CodeError (
207+ `invalid score parameters for topic ${ topic } : ${ ( e as Error ) . message } ` ,
208208 ERR_INVALID_PEER_SCORE_PARAMS
209209 )
210210 }
211211 }
212212
213213 // check that the topic score is 0 or something positive
214214 if ( p . topicScoreCap < 0 ) {
215- throw errcode (
216- new Error ( 'invalid topic score cap; must be positive (or 0 for no cap)' ) ,
217- ERR_INVALID_PEER_SCORE_PARAMS
218- )
215+ throw new CodeError ( 'invalid topic score cap; must be positive (or 0 for no cap)' , ERR_INVALID_PEER_SCORE_PARAMS )
219216 }
220217
221218 // check that we have an app specific score; the weight can be anything (but expected positive)
222219 if ( p . appSpecificScore === null || p . appSpecificScore === undefined ) {
223- throw errcode ( new Error ( 'missing application specific score function' ) , ERR_INVALID_PEER_SCORE_PARAMS )
220+ throw new CodeError ( 'missing application specific score function' , ERR_INVALID_PEER_SCORE_PARAMS )
224221 }
225222
226223 // check the IP colocation factor
227224 if ( p . IPColocationFactorWeight > 0 ) {
228- throw errcode (
229- new Error ( 'invalid IPColocationFactorWeight; must be negative (or 0 to disable)' ) ,
225+ throw new CodeError (
226+ 'invalid IPColocationFactorWeight; must be negative (or 0 to disable)' ,
230227 ERR_INVALID_PEER_SCORE_PARAMS
231228 )
232229 }
233230 if ( p . IPColocationFactorWeight !== 0 && p . IPColocationFactorThreshold < 1 ) {
234- throw errcode ( new Error ( 'invalid IPColocationFactorThreshold; must be at least 1' ) , ERR_INVALID_PEER_SCORE_PARAMS )
231+ throw new CodeError ( 'invalid IPColocationFactorThreshold; must be at least 1' , ERR_INVALID_PEER_SCORE_PARAMS )
235232 }
236233
237234 // check the behaviour penalty
238235 if ( p . behaviourPenaltyWeight > 0 ) {
239- throw errcode (
240- new Error ( 'invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)' ) ,
236+ throw new CodeError (
237+ 'invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)' ,
241238 ERR_INVALID_PEER_SCORE_PARAMS
242239 )
243240 }
244241 if ( p . behaviourPenaltyWeight !== 0 && ( p . behaviourPenaltyDecay <= 0 || p . behaviourPenaltyDecay >= 1 ) ) {
245- throw errcode ( new Error ( 'invalid BehaviourPenaltyDecay; must be between 0 and 1' ) , ERR_INVALID_PEER_SCORE_PARAMS )
242+ throw new CodeError ( 'invalid BehaviourPenaltyDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
246243 }
247244
248245 // check the decay parameters
249246 if ( p . decayInterval < 1000 ) {
250- throw errcode ( new Error ( 'invalid DecayInterval; must be at least 1s' ) , ERR_INVALID_PEER_SCORE_PARAMS )
247+ throw new CodeError ( 'invalid DecayInterval; must be at least 1s' , ERR_INVALID_PEER_SCORE_PARAMS )
251248 }
252249 if ( p . decayToZero <= 0 || p . decayToZero >= 1 ) {
253- throw errcode ( new Error ( 'invalid DecayToZero; must be between 0 and 1' ) , ERR_INVALID_PEER_SCORE_PARAMS )
250+ throw new CodeError ( 'invalid DecayToZero; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
254251 }
255252
256253 // no need to check the score retention; a value of 0 means that we don't retain scores
@@ -259,97 +256,82 @@ export function validatePeerScoreParams(p: PeerScoreParams): void {
259256export function validateTopicScoreParams ( p : TopicScoreParams ) : void {
260257 // make sure we have a sane topic weight
261258 if ( p . topicWeight < 0 ) {
262- throw errcode ( new Error ( 'invalid topic weight; must be >= 0' ) , ERR_INVALID_PEER_SCORE_PARAMS )
259+ throw new CodeError ( 'invalid topic weight; must be >= 0' , ERR_INVALID_PEER_SCORE_PARAMS )
263260 }
264261
265262 // check P1
266263 if ( p . timeInMeshQuantum === 0 ) {
267- throw errcode ( new Error ( 'invalid TimeInMeshQuantum; must be non zero' ) , ERR_INVALID_PEER_SCORE_PARAMS )
264+ throw new CodeError ( 'invalid TimeInMeshQuantum; must be non zero' , ERR_INVALID_PEER_SCORE_PARAMS )
268265 }
269266 if ( p . timeInMeshWeight < 0 ) {
270- throw errcode (
271- new Error ( 'invalid TimeInMeshWeight; must be positive (or 0 to disable)' ) ,
272- ERR_INVALID_PEER_SCORE_PARAMS
273- )
267+ throw new CodeError ( 'invalid TimeInMeshWeight; must be positive (or 0 to disable)' , ERR_INVALID_PEER_SCORE_PARAMS )
274268 }
275269 if ( p . timeInMeshWeight !== 0 && p . timeInMeshQuantum <= 0 ) {
276- throw errcode ( new Error ( 'invalid TimeInMeshQuantum; must be positive' ) , ERR_INVALID_PEER_SCORE_PARAMS )
270+ throw new CodeError ( 'invalid TimeInMeshQuantum; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
277271 }
278272 if ( p . timeInMeshWeight !== 0 && p . timeInMeshCap <= 0 ) {
279- throw errcode ( new Error ( 'invalid TimeInMeshCap; must be positive' ) , ERR_INVALID_PEER_SCORE_PARAMS )
273+ throw new CodeError ( 'invalid TimeInMeshCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
280274 }
281275
282276 // check P2
283277 if ( p . firstMessageDeliveriesWeight < 0 ) {
284- throw errcode (
285- new Error ( 'invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)' ) ,
278+ throw new CodeError (
279+ 'invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)' ,
286280 ERR_INVALID_PEER_SCORE_PARAMS
287281 )
288282 }
289283 if (
290284 p . firstMessageDeliveriesWeight !== 0 &&
291285 ( p . firstMessageDeliveriesDecay <= 0 || p . firstMessageDeliveriesDecay >= 1 )
292286 ) {
293- throw errcode (
294- new Error ( 'invalid FirstMessageDeliveriesDecay; must be between 0 and 1' ) ,
295- ERR_INVALID_PEER_SCORE_PARAMS
296- )
287+ throw new CodeError ( 'invalid FirstMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
297288 }
298289 if ( p . firstMessageDeliveriesWeight !== 0 && p . firstMessageDeliveriesCap <= 0 ) {
299- throw errcode ( new Error ( 'invalid FirstMessageDeliveriesCap; must be positive' ) , ERR_INVALID_PEER_SCORE_PARAMS )
290+ throw new CodeError ( 'invalid FirstMessageDeliveriesCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
300291 }
301292
302293 // check P3
303294 if ( p . meshMessageDeliveriesWeight > 0 ) {
304- throw errcode (
305- new Error ( 'invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)' ) ,
295+ throw new CodeError (
296+ 'invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)' ,
306297 ERR_INVALID_PEER_SCORE_PARAMS
307298 )
308299 }
309300 if ( p . meshMessageDeliveriesWeight !== 0 && ( p . meshMessageDeliveriesDecay <= 0 || p . meshMessageDeliveriesDecay >= 1 ) ) {
310- throw errcode (
311- new Error ( 'invalid MeshMessageDeliveriesDecay; must be between 0 and 1' ) ,
312- ERR_INVALID_PEER_SCORE_PARAMS
313- )
301+ throw new CodeError ( 'invalid MeshMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
314302 }
315303 if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesCap <= 0 ) {
316- throw errcode ( new Error ( 'invalid MeshMessageDeliveriesCap; must be positive' ) , ERR_INVALID_PEER_SCORE_PARAMS )
304+ throw new CodeError ( 'invalid MeshMessageDeliveriesCap; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
317305 }
318306 if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesThreshold <= 0 ) {
319- throw errcode ( new Error ( 'invalid MeshMessageDeliveriesThreshold; must be positive' ) , ERR_INVALID_PEER_SCORE_PARAMS )
307+ throw new CodeError ( 'invalid MeshMessageDeliveriesThreshold; must be positive' , ERR_INVALID_PEER_SCORE_PARAMS )
320308 }
321309 if ( p . meshMessageDeliveriesWindow < 0 ) {
322- throw errcode ( new Error ( 'invalid MeshMessageDeliveriesWindow; must be non-negative' ) , ERR_INVALID_PEER_SCORE_PARAMS )
310+ throw new CodeError ( 'invalid MeshMessageDeliveriesWindow; must be non-negative' , ERR_INVALID_PEER_SCORE_PARAMS )
323311 }
324312 if ( p . meshMessageDeliveriesWeight !== 0 && p . meshMessageDeliveriesActivation < 1000 ) {
325- throw errcode (
326- new Error ( 'invalid MeshMessageDeliveriesActivation; must be at least 1s' ) ,
327- ERR_INVALID_PEER_SCORE_PARAMS
328- )
313+ throw new CodeError ( 'invalid MeshMessageDeliveriesActivation; must be at least 1s' , ERR_INVALID_PEER_SCORE_PARAMS )
329314 }
330315
331316 // check P3b
332317 if ( p . meshFailurePenaltyWeight > 0 ) {
333- throw errcode (
334- new Error ( 'invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)' ) ,
318+ throw new CodeError (
319+ 'invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)' ,
335320 ERR_INVALID_PEER_SCORE_PARAMS
336321 )
337322 }
338323 if ( p . meshFailurePenaltyWeight !== 0 && ( p . meshFailurePenaltyDecay <= 0 || p . meshFailurePenaltyDecay >= 1 ) ) {
339- throw errcode ( new Error ( 'invalid MeshFailurePenaltyDecay; must be between 0 and 1' ) , ERR_INVALID_PEER_SCORE_PARAMS )
324+ throw new CodeError ( 'invalid MeshFailurePenaltyDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
340325 }
341326
342327 // check P4
343328 if ( p . invalidMessageDeliveriesWeight > 0 ) {
344- throw errcode (
345- new Error ( 'invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)' ) ,
329+ throw new CodeError (
330+ 'invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)' ,
346331 ERR_INVALID_PEER_SCORE_PARAMS
347332 )
348333 }
349334 if ( p . invalidMessageDeliveriesDecay <= 0 || p . invalidMessageDeliveriesDecay >= 1 ) {
350- throw errcode (
351- new Error ( 'invalid InvalidMessageDeliveriesDecay; must be between 0 and 1' ) ,
352- ERR_INVALID_PEER_SCORE_PARAMS
353- )
335+ throw new CodeError ( 'invalid InvalidMessageDeliveriesDecay; must be between 0 and 1' , ERR_INVALID_PEER_SCORE_PARAMS )
354336 }
355337}
0 commit comments