Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: add err-code
  • Loading branch information
wemeetagain committed Jun 16, 2020
commit a6c4de4965a97457969b96e006d05773974be200
2 changes: 2 additions & 0 deletions ts/score/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ERR_INVALID_PEER_SCORE_PARAMS = 'ERR_INVALID_PEER_SCORE_PARAMS'
export const ERR_INVALID_PEER_SCORE_THRESHOLDS = 'ERR_INVALID_PEER_SCORE_THRESHOLDS'
140 changes: 113 additions & 27 deletions ts/score/peerScoreParams.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { ERR_INVALID_PEER_SCORE_PARAMS } from './constants'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import errcode = require('err-code')

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

// check that the topic score is 0 or something positive
if (p.topicScoreCap < 0) {
throw new Error('invalid topic score cap; must be positive (or 0 for no cap)')
throw errcode(
new Error('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 new Error('missing application specific score function')
throw errcode(
new Error('missing application specific score function'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}

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

// check the behaviour penalty
if (p.behaviourPenaltyWeight > 0) {
throw new Error('invalid BehaviourPenaltyWeight; must be negative (or 0 to disable)')
throw errcode(
new Error('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 new Error('invalid BehaviourPenaltyDecay; must be between 0 and 1')
throw errcode(
new Error('invalid BehaviourPenaltyDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}

// check the decay parameters
if (p.decayInterval < 1000) {
throw new Error('invalid DecayInterval; must be at least 1s')
throw errcode(
new Error('invalid DecayInterval; must be at least 1s'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.decayToZero <= 0 || p.decayToZero >= 1) {
throw new Error('invalid DecayToZero; must be between 0 and 1')
throw errcode(
new Error('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 @@ -243,67 +275,121 @@ 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 new Error('invalid topic weight; must be >= 0')
throw errcode(
new Error('invalid topic weight; must be >= 0'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}

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

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

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

// check P3b
if (p.meshFailurePenaltyWeight > 0) {
throw new Error('invalid MeshFailurePenaltyWeight; must be negative (or 0 to disable)')
throw errcode(
new Error('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 new Error('invalid MeshFailurePenaltyDecay; must be between 0 and 1')
throw errcode(
new Error('invalid MeshFailurePenaltyDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}

// check P4
if (p.invalidMessageDeliveriesWeight > 0) {
throw new Error('invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)')
throw errcode(
new Error('invalid InvalidMessageDeliveriesWeight; must be negative (or 0 to disable)'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}
if (p.invalidMessageDeliveriesDecay <= 0 || p.invalidMessageDeliveriesDecay >= 1) {
throw new Error('invalid InvalidMessageDeliveriesDecay; must be between 0 and 1')
throw errcode(
new Error('invalid InvalidMessageDeliveriesDecay; must be between 0 and 1'),
ERR_INVALID_PEER_SCORE_PARAMS
)
}
}
30 changes: 25 additions & 5 deletions ts/score/peerScoreThresholds.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { ERR_INVALID_PEER_SCORE_THRESHOLDS } from './constants'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import errcode = require('err-code')

// This file defines PeerScoreThresholds interface
// as well as a constructor, default constructor, and validation function
// for this interface
Expand Down Expand Up @@ -51,18 +56,33 @@ export function createPeerScoreThresholds (p: Partial<PeerScoreThresholds> = {})

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