Skip to content

Commit

Permalink
Merge pull request #77 from ChainSafe/typescript
Browse files Browse the repository at this point in the history
Refactor to use Typescript
  • Loading branch information
wemeetagain authored Jun 3, 2020
2 parents 7d31804 + 666099e commit 49dc8fd
Show file tree
Hide file tree
Showing 27 changed files with 458 additions and 277 deletions.
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// from https://dev.to/itmayziii/typescript-eslint-and-standardjs-5hmd
module.exports = {
'parser': '@typescript-eslint/parser',
'parserOptions': {
'project': './tsconfig.json', // Required to have rules that rely on Types.
'tsconfigRootDir': './'
},
'extends': [
'plugin:@typescript-eslint/recommended', // Out of the box Typescript rules
'standard' // Out of the box StandardJS rules
],
'plugins': [
'@typescript-eslint' // Let's us override rules below.
],
'rules': {
'@typescript-eslint/member-delimiter-style': ['error', { // Prevents us from using any delimiter for interface properties.
'multiline': {
'delimiter': 'none',
'requireLast': false
},
'singleline': {
'delimiter': 'comma',
'requireLast': false
}
}],
'@typescript-eslint/indent': 'off', // This is the job of StandardJS, they are competing rules so we turn off the Typescript one.
'node/no-unsupported-features/es-syntax': 'off', // Allows us to use Import and Export keywords.
'@typescript-eslint/no-non-null-assertion': 'off',
'no-mixed-operators': 'off'
}
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ os:
- linux
- osx

before_script: npm run prebuild
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

Expand All @@ -22,6 +23,7 @@ jobs:

- stage: check
script:
- npm run prebuild
- npx aegir dep-check
- npm run lint

Expand All @@ -30,6 +32,7 @@ jobs:
addons:
chrome: stable
script:
- npm run prebuild
- npx aegir test -t browser
- npx aegir test -t webworker

Expand All @@ -38,6 +41,7 @@ jobs:
addons:
firefox: latest
script:
- npm run prebuild
- npx aegir test -t browser -- --browsers FirefoxHeadless
- npx aegir test -t webworker -- --browsers FirefoxHeadless

Expand Down
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "libp2p-gossipsub",
"version": "0.4.2",
"description": "A javascript implementation of gossipsub",
"description": "A typescript implementation of gossipsub",
"leadMaintainer": "Cayman Nava <[email protected]>",
"main": "src/index.js",
"files": [
Expand All @@ -10,9 +10,11 @@
],
"types": "src/index.d.ts",
"scripts": {
"lint": "aegir lint",
"lint": "eslint --ext .ts ts",
"release": "aegir release",
"prebuild": "tsc",
"build": "aegir build",
"pretest": "tsc",
"test": "aegir test",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser"
Expand Down Expand Up @@ -41,27 +43,36 @@
"it-pipe": "^1.0.1",
"libp2p-pubsub": "^0.5.0",
"p-map": "^4.0.0",
"peer-id": "~0.13.3",
"peer-id": "~0.13.12",
"protons": "^1.0.1",
"time-cache": "^0.3.0"
},
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/mocha": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"aegir": "^21.10.2",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"eslint": "^7.1.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"it-pair": "^1.0.0",
"libp2p-floodsub": "^0.21.0",
"lodash": "^4.17.15",
"mocha": "^7.1.1",
"p-times": "^2.1.0",
"p-wait-for": "^3.1.0",
"promisify-es6": "^1.0.3",
"sinon": "^9.0.2"
"sinon": "^9.0.2",
"typescript": "^3.9.3"
},
"contributors": [
"Cayman <[email protected]>",
Expand Down
24 changes: 0 additions & 24 deletions src/constants.js

This file was deleted.

44 changes: 0 additions & 44 deletions src/index.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/message/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/utils/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/2-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
const expect = chai.expect

const { GossipSubID: multicodec } = require('../src/constants')
const { GossipsubID: multicodec } = require('../src/constants')

const {
createGossipsub,
Expand Down
2 changes: 1 addition & 1 deletion test/gossip-incoming.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
const expect = chai.expect

const { GossipSubID: multicodec } = require('../src/constants')
const { GossipsubID: multicodec } = require('../src/constants')
const { createGossipsubConnectedNodes } = require('./utils')

const shouldNotHappen = (msg) => expect.fail()
Expand Down
4 changes: 2 additions & 2 deletions test/gossip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { Buffer } = require('buffer')
const { expect } = require('chai')
const sinon = require('sinon')

const { GossipSubID: multicodec, GossipSubDhi } = require('../src/constants')
const { GossipsubID: multicodec, GossipsubDhi } = require('../src/constants')
const {
first,
createGossipsubNodes,
Expand All @@ -20,7 +20,7 @@ describe('gossip', () => {
({
nodes,
registrarRecords
} = await createGossipsubNodes(GossipSubDhi + 2, true))
} = await createGossipsubNodes(GossipsubDhi + 2, true))
})

afterEach(() => Promise.all(nodes.map((n) => n.stop())))
Expand Down
4 changes: 2 additions & 2 deletions test/heartbeat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { expect } = require('chai')

const Gossipsub = require('../src')
const { GossipSubHeartbeatInterval } = require('../src/constants')
const { GossipsubHeartbeatInterval } = require('../src/constants')
const { createPeerId, mockRegistrar } = require('./utils')

describe('heartbeat', () => {
Expand All @@ -25,6 +25,6 @@ describe('heartbeat', () => {
await new Promise((resolve) => gossipsub.once('gossipsub:heartbeat', resolve))
const t2 = Date.now()
const safeDelta = 100 // ms
expect(t2 - t1).to.be.lt(GossipSubHeartbeatInterval + safeDelta)
expect(t2 - t1).to.be.lt(GossipsubHeartbeatInterval + safeDelta)
})
})
6 changes: 3 additions & 3 deletions test/mesh.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const { expect } = require('chai')

const { GossipSubDhi, GossipSubID: multicodec } = require('../src/constants')
const { GossipsubDhi, GossipsubID: multicodec } = require('../src/constants')
const {
createGossipsubNodes,
ConnectionPair
Expand All @@ -17,7 +17,7 @@ describe('mesh overlay', () => {
({
nodes,
registrarRecords
} = await createGossipsubNodes(GossipSubDhi + 2, true))
} = await createGossipsubNodes(GossipsubDhi + 2, true))
})

afterEach(() => Promise.all(nodes.map((n) => n.stop())))
Expand Down Expand Up @@ -76,6 +76,6 @@ describe('mesh overlay', () => {
await new Promise((resolve) => setTimeout(resolve, 500))
// await mesh rebalancing
await new Promise((resolve) => node0.once('gossipsub:heartbeat', resolve))
expect(node0.mesh.get(topic).size).to.be.lte(GossipSubDhi)
expect(node0.mesh.get(topic).size).to.be.lte(GossipsubDhi)
})
})
2 changes: 1 addition & 1 deletion test/multiple-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const promisify = require('promisify-es6')

const { GossipSubID: multicodec } = require('../src/constants')
const { GossipsubID: multicodec } = require('../src/constants')
const {
createGossipsubNodes,
expectSet,
Expand Down
4 changes: 2 additions & 2 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const pTimes = require('p-times')
const FloodSub = require('libp2p-floodsub')
const PeerId = require('peer-id')

const GossipSub = require('../../src')
const Gossipsub = require('../../src')

exports.first = (map) => map.values().next().value

Expand All @@ -26,7 +26,7 @@ exports.createPeerId = createPeerId

const createGossipsub = async (registrar, shouldStart = false, options) => {
const peerId = await createPeerId()
const gs = new GossipSub(peerId, registrar, options)
const gs = new Gossipsub(peerId, registrar, options)

if (shouldStart) {
await gs.start()
Expand Down
24 changes: 24 additions & 0 deletions ts/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const second = exports.second = 1000
const minute = exports.minute = 60 * second

// Protocol identifiers
export const FloodSubID = '/floodsub/1.0.0'
export const GossipsubID = '/meshsub/1.0.0'

// Overlay parameters
export const GossipsubD = 6
export const GossipsubDlo = 4
export const GossipsubDhi = 12

// Gossip parameters
export const GossipsubHistoryLength = 5
export const GossipsubHistoryGossip = 3

// Heartbeat interval
export const GossipsubHeartbeatInitialDelay = 100 / second
export const GossipsubHeartbeatInterval = second

// Fanout ttl
export const GossipsubFanoutTTL = minute
14 changes: 7 additions & 7 deletions src/getGossipPeers.js → ts/getGossipPeers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const constants = require('./constants')
const { shuffle } = require('./utils')
import * as constants from './constants'
import { shuffle } from './utils'
import { Peer } from './peer'
import Gossipsub = require('./index')

/**
* Given a topic, returns up to count peers subscribed to that topic
Expand All @@ -12,16 +12,16 @@ const { shuffle } = require('./utils')
* @returns {Set<Peer>}
*
*/
module.exports = function getGossipPeers (router, topic, count) {
export function getGossipPeers (router: Gossipsub, topic: string, count: number): Set<Peer> {
const peersInTopic = router.topics.get(topic)
if (!peersInTopic) {
return new Set()
}

// Adds all peers using our protocol
let peers = []
let peers: Peer[] = []
peersInTopic.forEach((peer) => {
if (peer.protocols.includes(constants.GossipSubID)) {
if (peer.protocols.includes(constants.GossipsubID)) {
peers.push(peer)
}
})
Expand Down
Loading

0 comments on commit 49dc8fd

Please sign in to comment.