Skip to content

Commit 4842680

Browse files
authored
chore: replace err-code with CodeError (#397)
* deps: bump @libp2p/interfaces * deps: remove err-code * chore: replace err-code with CodeError
1 parent e7c88ac commit 4842680

4 files changed

Lines changed: 45 additions & 68 deletions

File tree

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"@libp2p/interface-peer-store": "^1.2.2",
7575
"@libp2p/interface-pubsub": "^3.0.0",
7676
"@libp2p/interface-registrar": "^2.0.3",
77-
"@libp2p/interfaces": "^3.0.3",
77+
"@libp2p/interfaces": "^3.2.0",
7878
"@libp2p/logger": "^2.0.0",
7979
"@libp2p/peer-id": "^2.0.0",
8080
"@libp2p/peer-record": "^5.0.0",
@@ -83,7 +83,6 @@
8383
"@multiformats/multiaddr": "^11.0.0",
8484
"abortable-iterator": "^4.0.2",
8585
"denque": "^1.5.0",
86-
"err-code": "^3.0.1",
8786
"it-length-prefixed": "^8.0.2",
8887
"it-pipe": "^2.0.4",
8988
"it-pushable": "^3.1.0",

src/score/peer-score-params.ts

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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 {
259256
export 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
}

src/score/peer-score-thresholds.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ERR_INVALID_PEER_SCORE_THRESHOLDS } from './constants.js'
2-
import errcode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33

44
// This file defines PeerScoreThresholds interface
55
// as well as a constructor, default constructor, and validation function
@@ -54,27 +54,24 @@ export function createPeerScoreThresholds(p: Partial<PeerScoreThresholds> = {}):
5454

5555
export function validatePeerScoreThresholds(p: PeerScoreThresholds): void {
5656
if (p.gossipThreshold > 0) {
57-
throw errcode(new Error('invalid gossip threshold; it must be <= 0'), ERR_INVALID_PEER_SCORE_THRESHOLDS)
57+
throw new CodeError('invalid gossip threshold; it must be <= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
5858
}
5959
if (p.publishThreshold > 0 || p.publishThreshold > p.gossipThreshold) {
60-
throw errcode(
61-
new Error('invalid publish threshold; it must be <= 0 and <= gossip threshold'),
60+
throw new CodeError(
61+
'invalid publish threshold; it must be <= 0 and <= gossip threshold',
6262
ERR_INVALID_PEER_SCORE_THRESHOLDS
6363
)
6464
}
6565
if (p.graylistThreshold > 0 || p.graylistThreshold > p.publishThreshold) {
66-
throw errcode(
67-
new Error('invalid graylist threshold; it must be <= 0 and <= publish threshold'),
66+
throw new CodeError(
67+
'invalid graylist threshold; it must be <= 0 and <= publish threshold',
6868
ERR_INVALID_PEER_SCORE_THRESHOLDS
6969
)
7070
}
7171
if (p.acceptPXThreshold < 0) {
72-
throw errcode(new Error('invalid accept PX threshold; it must be >= 0'), ERR_INVALID_PEER_SCORE_THRESHOLDS)
72+
throw new CodeError('invalid accept PX threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
7373
}
7474
if (p.opportunisticGraftThreshold < 0) {
75-
throw errcode(
76-
new Error('invalid opportunistic grafting threshold; it must be >= 0'),
77-
ERR_INVALID_PEER_SCORE_THRESHOLDS
78-
)
75+
throw new CodeError('invalid opportunistic grafting threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
7976
}
8077
}

0 commit comments

Comments
 (0)