Skip to content

Commit

Permalink
chore: replace err-code with CodeError (#397)
Browse files Browse the repository at this point in the history
* deps: bump @libp2p/interfaces

* deps: remove err-code

* chore: replace err-code with CodeError
  • Loading branch information
tabcat committed Jan 16, 2023
1 parent e7c88ac commit 4842680
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 68 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@libp2p/interface-peer-store": "^1.2.2",
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/interface-registrar": "^2.0.3",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/peer-record": "^5.0.0",
Expand All @@ -83,7 +83,6 @@
"@multiformats/multiaddr": "^11.0.0",
"abortable-iterator": "^4.0.2",
"denque": "^1.5.0",
"err-code": "^3.0.1",
"it-length-prefixed": "^8.0.2",
"it-pipe": "^2.0.4",
"it-pushable": "^3.1.0",
Expand Down
88 changes: 35 additions & 53 deletions src/score/peer-score-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ERR_INVALID_PEER_SCORE_PARAMS } from './constants.js'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'

// This file defines PeerScoreParams and TopicScoreParams interfaces
// as well as constructors, default constructors, and validation functions
Expand Down Expand Up @@ -203,54 +203,51 @@ export function validatePeerScoreParams(p: PeerScoreParams): void {
try {
validateTopicScoreParams(params)
} catch (e) {
throw errcode(
new Error(`invalid score parameters for topic ${topic}: ${(e as Error).message}`),
throw new CodeError(
`invalid score parameters for topic ${topic}: ${(e as Error).message}`,
ERR_INVALID_PEER_SCORE_PARAMS
)
}
}

// check that the topic score is 0 or something positive
if (p.topicScoreCap < 0) {
throw errcode(
new Error('invalid topic score cap; must be positive (or 0 for no cap)'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid topic score cap; must be positive (or 0 for no cap)', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check that we have an app specific score; the weight can be anything (but expected positive)
if (p.appSpecificScore === null || p.appSpecificScore === undefined) {
throw errcode(new Error('missing application specific score function'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('missing application specific score function', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check the IP colocation factor
if (p.IPColocationFactorWeight > 0) {
throw errcode(
new Error('invalid IPColocationFactorWeight; must be negative (or 0 to disable)'),
throw new CodeError(
'invalid IPColocationFactorWeight; must be negative (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.IPColocationFactorWeight !== 0 && p.IPColocationFactorThreshold < 1) {
throw errcode(new Error('invalid IPColocationFactorThreshold; must be at least 1'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid IPColocationFactorThreshold; must be at least 1', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check the behaviour penalty
if (p.behaviourPenaltyWeight > 0) {
throw errcode(
new Error('invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)'),
throw new CodeError(
'invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.behaviourPenaltyWeight !== 0 && (p.behaviourPenaltyDecay <= 0 || p.behaviourPenaltyDecay >= 1)) {
throw errcode(new Error('invalid BehaviourPenaltyDecay; must be between 0 and 1'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid BehaviourPenaltyDecay; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check the decay parameters
if (p.decayInterval < 1000) {
throw errcode(new Error('invalid DecayInterval; must be at least 1s'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid DecayInterval; must be at least 1s', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.decayToZero <= 0 || p.decayToZero >= 1) {
throw errcode(new Error('invalid DecayToZero; must be between 0 and 1'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid DecayToZero; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}

// no need to check the score retention; a value of 0 means that we don't retain scores
Expand All @@ -259,97 +256,82 @@ export function validatePeerScoreParams(p: PeerScoreParams): void {
export function validateTopicScoreParams(p: TopicScoreParams): void {
// make sure we have a sane topic weight
if (p.topicWeight < 0) {
throw errcode(new Error('invalid topic weight; must be >= 0'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid topic weight; must be >= 0', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check P1
if (p.timeInMeshQuantum === 0) {
throw errcode(new Error('invalid TimeInMeshQuantum; must be non zero'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid TimeInMeshQuantum; must be non zero', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.timeInMeshWeight < 0) {
throw errcode(
new Error('invalid TimeInMeshWeight; must be positive (or 0 to disable)'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid TimeInMeshWeight; must be positive (or 0 to disable)', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.timeInMeshWeight !== 0 && p.timeInMeshQuantum <= 0) {
throw errcode(new Error('invalid TimeInMeshQuantum; must be positive'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid TimeInMeshQuantum; must be positive', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.timeInMeshWeight !== 0 && p.timeInMeshCap <= 0) {
throw errcode(new Error('invalid TimeInMeshCap; must be positive'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid TimeInMeshCap; must be positive', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check P2
if (p.firstMessageDeliveriesWeight < 0) {
throw errcode(
new Error('invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)'),
throw new CodeError(
'invallid FirstMessageDeliveriesWeight; must be positive (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (
p.firstMessageDeliveriesWeight !== 0 &&
(p.firstMessageDeliveriesDecay <= 0 || p.firstMessageDeliveriesDecay >= 1)
) {
throw errcode(
new Error('invalid FirstMessageDeliveriesDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid FirstMessageDeliveriesDecay; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.firstMessageDeliveriesWeight !== 0 && p.firstMessageDeliveriesCap <= 0) {
throw errcode(new Error('invalid FirstMessageDeliveriesCap; must be positive'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid FirstMessageDeliveriesCap; must be positive', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check P3
if (p.meshMessageDeliveriesWeight > 0) {
throw errcode(
new Error('invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)'),
throw new CodeError(
'invalid MeshMessageDeliveriesWeight; must be negative (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.meshMessageDeliveriesWeight !== 0 && (p.meshMessageDeliveriesDecay <= 0 || p.meshMessageDeliveriesDecay >= 1)) {
throw errcode(
new Error('invalid MeshMessageDeliveriesDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid MeshMessageDeliveriesDecay; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.meshMessageDeliveriesWeight !== 0 && p.meshMessageDeliveriesCap <= 0) {
throw errcode(new Error('invalid MeshMessageDeliveriesCap; must be positive'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid MeshMessageDeliveriesCap; must be positive', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.meshMessageDeliveriesWeight !== 0 && p.meshMessageDeliveriesThreshold <= 0) {
throw errcode(new Error('invalid MeshMessageDeliveriesThreshold; must be positive'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid MeshMessageDeliveriesThreshold; must be positive', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.meshMessageDeliveriesWindow < 0) {
throw errcode(new Error('invalid MeshMessageDeliveriesWindow; must be non-negative'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid MeshMessageDeliveriesWindow; must be non-negative', ERR_INVALID_PEER_SCORE_PARAMS)
}
if (p.meshMessageDeliveriesWeight !== 0 && p.meshMessageDeliveriesActivation < 1000) {
throw errcode(
new Error('invalid MeshMessageDeliveriesActivation; must be at least 1s'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid MeshMessageDeliveriesActivation; must be at least 1s', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check P3b
if (p.meshFailurePenaltyWeight > 0) {
throw errcode(
new Error('invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)'),
throw new CodeError(
'invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.meshFailurePenaltyWeight !== 0 && (p.meshFailurePenaltyDecay <= 0 || p.meshFailurePenaltyDecay >= 1)) {
throw errcode(new Error('invalid MeshFailurePenaltyDecay; must be between 0 and 1'), ERR_INVALID_PEER_SCORE_PARAMS)
throw new CodeError('invalid MeshFailurePenaltyDecay; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}

// check P4
if (p.invalidMessageDeliveriesWeight > 0) {
throw errcode(
new Error('invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)'),
throw new CodeError(
'invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)',
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.invalidMessageDeliveriesDecay <= 0 || p.invalidMessageDeliveriesDecay >= 1) {
throw errcode(
new Error('invalid InvalidMessageDeliveriesDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
throw new CodeError('invalid InvalidMessageDeliveriesDecay; must be between 0 and 1', ERR_INVALID_PEER_SCORE_PARAMS)
}
}
19 changes: 8 additions & 11 deletions src/score/peer-score-thresholds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ERR_INVALID_PEER_SCORE_THRESHOLDS } from './constants.js'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'

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

export function validatePeerScoreThresholds(p: PeerScoreThresholds): void {
if (p.gossipThreshold > 0) {
throw errcode(new Error('invalid gossip threshold; it must be <= 0'), ERR_INVALID_PEER_SCORE_THRESHOLDS)
throw new CodeError('invalid gossip threshold; it must be <= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
}
if (p.publishThreshold > 0 || p.publishThreshold > p.gossipThreshold) {
throw errcode(
new Error('invalid publish threshold; it must be <= 0 and <= gossip threshold'),
throw new CodeError(
'invalid publish threshold; it must be <= 0 and <= gossip threshold',
ERR_INVALID_PEER_SCORE_THRESHOLDS
)
}
if (p.graylistThreshold > 0 || p.graylistThreshold > p.publishThreshold) {
throw errcode(
new Error('invalid graylist threshold; it must be <= 0 and <= publish threshold'),
throw new CodeError(
'invalid graylist threshold; it must be <= 0 and <= publish threshold',
ERR_INVALID_PEER_SCORE_THRESHOLDS
)
}
if (p.acceptPXThreshold < 0) {
throw errcode(new Error('invalid accept PX threshold; it must be >= 0'), ERR_INVALID_PEER_SCORE_THRESHOLDS)
throw new CodeError('invalid accept PX threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
}
if (p.opportunisticGraftThreshold < 0) {
throw errcode(
new Error('invalid opportunistic grafting threshold; it must be >= 0'),
ERR_INVALID_PEER_SCORE_THRESHOLDS
)
throw new CodeError('invalid opportunistic grafting threshold; it must be >= 0', ERR_INVALID_PEER_SCORE_THRESHOLDS)
}
}

0 comments on commit 4842680

Please sign in to comment.