1- import { CodeError } from '@libp2p/interface'
2- import { ERR_INVALID_PEER_SCORE_PARAMS } from './constants.js'
1+ import { InvalidPeerScoreParamsError } from '../errors.js'
32
43// This file defines PeerScoreParams and TopicScoreParams interfaces
54// as well as constructors, default constructors, and validation functions
@@ -203,51 +202,42 @@ export function validatePeerScoreParams (p: PeerScoreParams): void {
203202 try {
204203 validateTopicScoreParams ( params )
205204 } 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 } ` )
210206 }
211207 }
212208
213209 // check that the topic score is 0 or something positive
214210 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)' )
216212 }
217213
218214 // check that we have an app specific score; the weight can be anything (but expected positive)
219215 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' )
221217 }
222218
223219 // check the IP colocation factor
224220 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)' )
229222 }
230223 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' )
232225 }
233226
234227 // check the behaviour penalty
235228 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)' )
240230 }
241231 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' )
243233 }
244234
245235 // check the decay parameters
246236 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' )
248238 }
249239 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' )
251241 }
252242
253243 // 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 {
257247export function validateTopicScoreParams ( p : TopicScoreParams ) : void {
258248 // make sure we have a sane topic weight
259249 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' )
261251 }
262252
263253 // check P1
264254 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' )
266256 }
267257 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)' )
269259 }
270260 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' )
272262 }
273263 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' )
275265 }
276266
277267 // check P2
278268 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)' )
283270 }
284271 if (
285272 p . firstMessageDeliveriesWeight !== 0 &&
286273 ( p . firstMessageDeliveriesDecay <= 0 || p . firstMessageDeliveriesDecay >= 1 )
287274 ) {
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' )
289276 }
290277 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' )
292279 }
293280
294281 // check P3
295282 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)' )
300284 }
301285 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' )
303287 }
304288 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' )
306290 }
307291 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' )
309293 }
310294 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' )
312296 }
313297 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' )
315299 }
316300
317301 // check P3b
318302 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)' )
323304 }
324305 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' )
326307 }
327308
328309 // check P4
329310 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)' )
334312 }
335313 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' )
337315 }
338316}
0 commit comments