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: split score params/thresholds files
  • Loading branch information
wemeetagain committed Jun 7, 2020
commit 2293dc4884966b3e18c0ab2949393ae5b4ba9fb6
53 changes: 2 additions & 51 deletions test/scoreParams.spec.js → test/peerScoreParams.spec.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,8 @@
const { expect } = require('chai')
const {
createPeerScoreThresholds, validatePeerScoreThresholds,
createTopicScoreParams, validateTopicScoreParams,
createPeerScoreParams, validatePeerScoreParams
} = require('../src/score/scoreParams')

describe('PeerScoreThresholds validation', () => {
it('should throw on invalid PeerScoreThresholds', () => {
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: 1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
publishThreshold: 1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: 0
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: -2
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
acceptPXThreshold: -1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
opportunisticGraftThreshold: -1
})
)).to.throw
})
it('should not throw on valid PeerScoreThresholds', () => {
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: -2,
graylistThreshold: -3,
acceptPXThreshold: 1,
opportunisticGraftThreshold: 2
})
)).to.not.throw
})
})
} = require('../src/score')

describe('TopicScoreParams validation', () => {
it('should throw on invalid TopicScoreParams', () => {
Expand Down Expand Up @@ -416,4 +367,4 @@ describe('PeerScoreParams validation', () => {
})
)).to.not.throw
})
})
})
53 changes: 53 additions & 0 deletions test/peerScoreThresholds.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { expect } = require('chai')
const {
createPeerScoreThresholds, validatePeerScoreThresholds,
} = require('../src/score')

describe('PeerScoreThresholds validation', () => {
it('should throw on invalid PeerScoreThresholds', () => {
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: 1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
publishThreshold: 1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: 0
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: -2
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
acceptPXThreshold: -1
})
)).to.throw
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
opportunisticGraftThreshold: -1
})
)).to.throw
})
it('should not throw on valid PeerScoreThresholds', () => {
expect(() => validatePeerScoreThresholds(
createPeerScoreThresholds({
gossipThreshold: -1,
publishThreshold: -2,
graylistThreshold: -3,
acceptPXThreshold: 1,
opportunisticGraftThreshold: 2
})
)).to.not.throw
})
})

2 changes: 1 addition & 1 deletion ts/score/computeScore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PeerStats } from './peerStats'
import { PeerScoreParams } from './scoreParams'
import { PeerScoreParams } from './peerScoreParams'
import PeerId = require('peer-id')

export function computeScore (
Expand Down
4 changes: 2 additions & 2 deletions ts/score/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './scoreParams'
export * from './peerStats'
export * from './peerScoreParams'
export * from './peerScoreThresholds'
export * from './peerScore'
2 changes: 1 addition & 1 deletion ts/score/peerScore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message } from '../message'
import { PeerScoreParams, validatePeerScoreParams } from './scoreParams'
import { PeerScoreParams, validatePeerScoreParams } from './peerScoreParams'
import { PeerStats, createPeerStats, ensureTopicStats } from './peerStats'
import { computeScore } from './computeScore'
import { MessageDeliveries, DeliveryRecordStatus } from './messageDeliveries'
Expand Down
82 changes: 0 additions & 82 deletions ts/score/scoreParams.ts → ts/score/peerScoreParams.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,5 @@
import PeerId = require('peer-id')

export interface PeerScoreThresholds {
/**
* gossipThreshold is the score threshold below which gossip propagation is supressed;
* should be negative.
*/
gossipThreshold: number

/**
* publishThreshold is the score threshold below which we shouldn't publish when using flood
* publishing (also applies to fanout and floodsub peers); should be negative and <= GossipThreshold.
*/
publishThreshold: number

/**
* graylistThreshold is the score threshold below which message processing is supressed altogether,
* implementing an effective graylist according to peer score; should be negative and <= PublisThreshold.
*/
graylistThreshold: number

/**
* acceptPXThreshold is the score threshold below which PX will be ignored; this should be positive
* and limited to scores attainable by bootstrappers and other trusted nodes.
*/
acceptPXThreshold: number

/**
* opportunisticGraftThreshold is the median mesh score threshold before triggering opportunistic
* grafting; this should have a small positive value.
*/
opportunisticGraftThreshold: number
}

export function createPeerScoreThresholds (p: Partial<PeerScoreThresholds>): PeerScoreThresholds {
return {
gossipThreshold: 0,
publishThreshold: 0,
graylistThreshold: 0,
acceptPXThreshold: 0,
opportunisticGraftThreshold: 0,
...p
}
}

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

export interface PeerScoreParams {
/**
* Score parameters per topic.
Expand Down Expand Up @@ -353,24 +292,3 @@ export function validateTopicScoreParams (p: TopicScoreParams): void {
throw new Error('invalid InvalidMessageDeliveriesDecay; must be between 0 and 1')
}
}

const DefaultDecayInterval = 1000
const DefaultDecayToZero = 0.01

/**
* ScoreParameterDecay computes the decay factor for a parameter, assuming the DecayInterval is 1s
* and that the value decays to zero if it drops below 0.01
*/
export function scoreParameterDecay (decay: number): number {
return scoreParameterDecayWithBase(decay, DefaultDecayInterval, DefaultDecayToZero)
}

/**
* ScoreParameterDecay computes the decay factor for a parameter using base as the DecayInterval
*/
export function scoreParameterDecayWithBase (decay: number, base: number, decayToZero: number): number {
// the decay is linear, so after n ticks the value is factor^n
// so factor^n = decayToZero => factor = decayToZero^(1/n)
const ticks = decay / base
return decayToZero ** (1 / ticks)
}
60 changes: 60 additions & 0 deletions ts/score/peerScoreThresholds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export interface PeerScoreThresholds {
/**
* gossipThreshold is the score threshold below which gossip propagation is supressed;
* should be negative.
*/
gossipThreshold: number

/**
* publishThreshold is the score threshold below which we shouldn't publish when using flood
* publishing (also applies to fanout and floodsub peers); should be negative and <= GossipThreshold.
*/
publishThreshold: number

/**
* graylistThreshold is the score threshold below which message processing is supressed altogether,
* implementing an effective graylist according to peer score; should be negative and <= PublisThreshold.
*/
graylistThreshold: number

/**
* acceptPXThreshold is the score threshold below which PX will be ignored; this should be positive
* and limited to scores attainable by bootstrappers and other trusted nodes.
*/
acceptPXThreshold: number

/**
* opportunisticGraftThreshold is the median mesh score threshold before triggering opportunistic
* grafting; this should have a small positive value.
*/
opportunisticGraftThreshold: number
}

export function createPeerScoreThresholds (p: Partial<PeerScoreThresholds>): PeerScoreThresholds {
return {
gossipThreshold: 0,
publishThreshold: 0,
graylistThreshold: 0,
acceptPXThreshold: 0,
opportunisticGraftThreshold: 0,
...p
}
}

export function validatePeerScoreThresholds (p: PeerScoreThresholds): void {
if (p.gossipThreshold > 0) {
throw new Error('invalid gossip threshold; it must be <= 0')
}
if (p.publishThreshold > 0 || p.publishThreshold > p.gossipThreshold) {
throw new Error('invalid publish threshold; it must be <= 0 and <= gossip threshold')
}
if (p.graylistThreshold > 0 || p.graylistThreshold > p.publishThreshold) {
throw new Error('invalid graylist threshold; it must be <= 0 and <= publish threshold')
}
if (p.acceptPXThreshold < 0) {
throw new Error('invalid accept PX threshold; it must be >= 0')
}
if (p.opportunisticGraftThreshold < 0) {
throw new Error('invalid opportunistic grafting threshold; it must be >= 0')
}
}
2 changes: 1 addition & 1 deletion ts/score/peerStats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PeerScoreParams } from './scoreParams'
import { PeerScoreParams } from './peerScoreParams'

export interface PeerStats {
/**
Expand Down
20 changes: 20 additions & 0 deletions ts/score/scoreParamDecay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const DefaultDecayInterval = 1000
const DefaultDecayToZero = 0.01

/**
* ScoreParameterDecay computes the decay factor for a parameter, assuming the DecayInterval is 1s
* and that the value decays to zero if it drops below 0.01
*/
export function scoreParameterDecay (decay: number): number {
return scoreParameterDecayWithBase(decay, DefaultDecayInterval, DefaultDecayToZero)
}

/**
* ScoreParameterDecay computes the decay factor for a parameter using base as the DecayInterval
*/
export function scoreParameterDecayWithBase (decay: number, base: number, decayToZero: number): number {
// the decay is linear, so after n ticks the value is factor^n
// so factor^n = decayToZero => factor = decayToZero^(1/n)
const ticks = decay / base
return decayToZero ** (1 / ticks)
}