Skip to content

Commit 2949a4f

Browse files
committed
chore: remove CodeError
1 parent a993667 commit 2949a4f

File tree

10 files changed

+3260
-2023
lines changed

10 files changed

+3260
-2023
lines changed

package-lock.json

Lines changed: 3170 additions & 1935 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"it-pipe": "^3.0.1",
8585
"it-pushable": "^3.2.3",
8686
"multiformats": "^13.0.1",
87-
"protons-runtime": "5.4.0",
87+
"protons-runtime": "^5.5.0",
8888
"uint8arraylist": "^2.4.8",
8989
"uint8arrays": "^5.0.1"
9090
},
@@ -100,8 +100,8 @@
100100
"aegir": "^42.2.2",
101101
"datastore-core": "^9.2.7",
102102
"delay": "^6.0.0",
103-
"mkdirp": "^3.0.1",
104103
"it-all": "^3.0.6",
104+
"mkdirp": "^3.0.1",
105105
"p-defer": "^4.0.0",
106106
"p-event": "^6.0.0",
107107
"p-retry": "^6.2.0",

src/errors.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class InvalidPeerScoreParamsError extends Error {
2+
static name = 'InvalidPeerScoreParamsError'
3+
4+
constructor (message = 'Invalid peer score params') {
5+
super(message)
6+
this.name = 'InvalidPeerScoreParamsError'
7+
}
8+
}
9+
10+
export class InvalidPeerScoreThresholdsError extends Error {
11+
static name = 'InvalidPeerScoreThresholdsError'
12+
13+
constructor (message = 'Invalid peer score thresholds') {
14+
super(message)
15+
this.name = 'InvalidPeerScoreThresholdsError'
16+
}
17+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2613,7 +2613,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
26132613
try {
26142614
peerInfo = await this.components.peerStore.get(id)
26152615
} catch (err: any) {
2616-
if (err.code !== 'ERR_NOT_FOUND') {
2616+
if (err.name !== 'NotFoundError') {
26172617
throw err
26182618
}
26192619
}

src/message/rpc.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
55
/* eslint-disable @typescript-eslint/no-empty-interface */
66

7-
import { type Codec, CodeError, decodeMessage, type DecodeOptions, encodeMessage, message } from 'protons-runtime'
7+
import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, MaxLengthError, message } from 'protons-runtime'
88
import type { Uint8ArrayList } from 'uint8arraylist'
99

1010
export interface RPC {
@@ -256,34 +256,42 @@ export namespace RPC {
256256
switch (tag >>> 3) {
257257
case 1: {
258258
if (opts.limits?.ihave != null && obj.ihave.length === opts.limits.ihave) {
259-
throw new CodeError('decode error - map field "ihave" had too many elements', 'ERR_MAX_LENGTH')
259+
throw new MaxLengthError('Decode error - map field "ihave" had too many elements')
260260
}
261261

262-
obj.ihave.push(RPC.ControlIHave.codec().decode(reader, reader.uint32()))
262+
obj.ihave.push(RPC.ControlIHave.codec().decode(reader, reader.uint32(), {
263+
limits: opts.limits?.ihave$
264+
}))
263265
break
264266
}
265267
case 2: {
266268
if (opts.limits?.iwant != null && obj.iwant.length === opts.limits.iwant) {
267-
throw new CodeError('decode error - map field "iwant" had too many elements', 'ERR_MAX_LENGTH')
269+
throw new MaxLengthError('Decode error - map field "iwant" had too many elements')
268270
}
269271

270-
obj.iwant.push(RPC.ControlIWant.codec().decode(reader, reader.uint32()))
272+
obj.iwant.push(RPC.ControlIWant.codec().decode(reader, reader.uint32(), {
273+
limits: opts.limits?.iwant$
274+
}))
271275
break
272276
}
273277
case 3: {
274278
if (opts.limits?.graft != null && obj.graft.length === opts.limits.graft) {
275-
throw new CodeError('decode error - map field "graft" had too many elements', 'ERR_MAX_LENGTH')
279+
throw new MaxLengthError('Decode error - map field "graft" had too many elements')
276280
}
277281

278-
obj.graft.push(RPC.ControlGraft.codec().decode(reader, reader.uint32()))
282+
obj.graft.push(RPC.ControlGraft.codec().decode(reader, reader.uint32(), {
283+
limits: opts.limits?.graft$
284+
}))
279285
break
280286
}
281287
case 4: {
282288
if (opts.limits?.prune != null && obj.prune.length === opts.limits.prune) {
283-
throw new CodeError('decode error - map field "prune" had too many elements', 'ERR_MAX_LENGTH')
289+
throw new MaxLengthError('Decode error - map field "prune" had too many elements')
284290
}
285291

286-
obj.prune.push(RPC.ControlPrune.codec().decode(reader, reader.uint32()))
292+
obj.prune.push(RPC.ControlPrune.codec().decode(reader, reader.uint32(), {
293+
limits: opts.limits?.prune$
294+
}))
287295
break
288296
}
289297
default: {
@@ -356,7 +364,7 @@ export namespace RPC {
356364
}
357365
case 2: {
358366
if (opts.limits?.messageIDs != null && obj.messageIDs.length === opts.limits.messageIDs) {
359-
throw new CodeError('decode error - map field "messageIDs" had too many elements', 'ERR_MAX_LENGTH')
367+
throw new MaxLengthError('Decode error - map field "messageIDs" had too many elements')
360368
}
361369

362370
obj.messageIDs.push(reader.bytes())
@@ -422,7 +430,7 @@ export namespace RPC {
422430
switch (tag >>> 3) {
423431
case 1: {
424432
if (opts.limits?.messageIDs != null && obj.messageIDs.length === opts.limits.messageIDs) {
425-
throw new CodeError('decode error - map field "messageIDs" had too many elements', 'ERR_MAX_LENGTH')
433+
throw new MaxLengthError('Decode error - map field "messageIDs" had too many elements')
426434
}
427435

428436
obj.messageIDs.push(reader.bytes())
@@ -562,10 +570,12 @@ export namespace RPC {
562570
}
563571
case 2: {
564572
if (opts.limits?.peers != null && obj.peers.length === opts.limits.peers) {
565-
throw new CodeError('decode error - map field "peers" had too many elements', 'ERR_MAX_LENGTH')
573+
throw new MaxLengthError('Decode error - map field "peers" had too many elements')
566574
}
567575

568-
obj.peers.push(RPC.PeerInfo.codec().decode(reader, reader.uint32()))
576+
obj.peers.push(RPC.PeerInfo.codec().decode(reader, reader.uint32(), {
577+
limits: opts.limits?.peers$
578+
}))
569579
break
570580
}
571581
case 3: {
@@ -708,22 +718,28 @@ export namespace RPC {
708718
switch (tag >>> 3) {
709719
case 1: {
710720
if (opts.limits?.subscriptions != null && obj.subscriptions.length === opts.limits.subscriptions) {
711-
throw new CodeError('decode error - map field "subscriptions" had too many elements', 'ERR_MAX_LENGTH')
721+
throw new MaxLengthError('Decode error - map field "subscriptions" had too many elements')
712722
}
713723

714-
obj.subscriptions.push(RPC.SubOpts.codec().decode(reader, reader.uint32()))
724+
obj.subscriptions.push(RPC.SubOpts.codec().decode(reader, reader.uint32(), {
725+
limits: opts.limits?.subscriptions$
726+
}))
715727
break
716728
}
717729
case 2: {
718730
if (opts.limits?.messages != null && obj.messages.length === opts.limits.messages) {
719-
throw new CodeError('decode error - map field "messages" had too many elements', 'ERR_MAX_LENGTH')
731+
throw new MaxLengthError('Decode error - map field "messages" had too many elements')
720732
}
721733

722-
obj.messages.push(RPC.Message.codec().decode(reader, reader.uint32()))
734+
obj.messages.push(RPC.Message.codec().decode(reader, reader.uint32(), {
735+
limits: opts.limits?.messages$
736+
}))
723737
break
724738
}
725739
case 3: {
726-
obj.control = RPC.ControlMessage.codec().decode(reader, reader.uint32())
740+
obj.control = RPC.ControlMessage.codec().decode(reader, reader.uint32(), {
741+
limits: opts.limits?.control
742+
})
727743
break
728744
}
729745
default: {

src/score/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/score/peer-score-params.ts

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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 {
257247
export 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
}

src/score/peer-score-thresholds.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { CodeError } from '@libp2p/interface'
2-
import { ERR_INVALID_PEER_SCORE_THRESHOLDS } from './constants.js'
1+
import { InvalidPeerScoreThresholdsError } from '../errors.js'
32

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

5554
export function validatePeerScoreThresholds (p: PeerScoreThresholds): void {
5655
if (p.gossipThreshold > 0) {
57-
throw new CodeError('invalid gossip threshold; it must be <= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
56+
throw new InvalidPeerScoreThresholdsError('invalid gossip threshold; it must be <= 0')
5857
}
5958
if (p.publishThreshold > 0 || p.publishThreshold > p.gossipThreshold) {
60-
throw new CodeError(
61-
'invalid publish threshold; it must be <= 0 and <= gossip threshold',
62-
ERR_INVALID_PEER_SCORE_THRESHOLDS
63-
)
59+
throw new InvalidPeerScoreThresholdsError('invalid publish threshold; it must be <= 0 and <= gossip threshold')
6460
}
6561
if (p.graylistThreshold > 0 || p.graylistThreshold > p.publishThreshold) {
66-
throw new CodeError(
67-
'invalid graylist threshold; it must be <= 0 and <= publish threshold',
68-
ERR_INVALID_PEER_SCORE_THRESHOLDS
69-
)
62+
throw new InvalidPeerScoreThresholdsError('invalid graylist threshold; it must be <= 0 and <= publish threshold')
7063
}
7164
if (p.acceptPXThreshold < 0) {
72-
throw new CodeError('invalid accept PX threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
65+
throw new InvalidPeerScoreThresholdsError('invalid accept PX threshold; it must be >= 0')
7366
}
7467
if (p.opportunisticGraftThreshold < 0) {
75-
throw new CodeError('invalid opportunistic grafting threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
68+
throw new InvalidPeerScoreThresholdsError('invalid opportunistic grafting threshold; it must be >= 0')
7669
}
7770
}

test/floodsub.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('gossipsub fallbacks to floodsub', () => {
8080
await connectPubsubNodes(nodeGs, nodeFs)
8181
expect.fail('Dial should not have succeed')
8282
} catch (err) {
83-
expect((err as { code: string }).code).to.be.equal('ERR_UNSUPPORTED_PROTOCOL')
83+
expect(err).to.have.property('name', 'UnsupportedProtocolError')
8484
}
8585
})
8686
})

test/gossip.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('gossip', () => {
191191
await nodeA.pubsub.publish(topic, new Uint8Array(5000000))
192192
await pEvent(nodeA.pubsub, 'gossipsub:heartbeat')
193193
const expectedError = nodeBSpy.handlePeerReadStreamError.getCalls()[0]?.args[0]
194-
expect(expectedError !== undefined && (expectedError as unknown as { code: string }).code, 'ERR_MSG_DATA_TOO_LONG')
194+
expect(expectedError).to.have.property('name', 'InvalidDataLengthError')
195195

196196
// unset spy
197197
nodeBSpy.handlePeerReadStreamError.restore()

0 commit comments

Comments
 (0)