Skip to content

Commit

Permalink
chore: update deps and remove protons
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 7, 2021
1 parent 12626e8 commit aec5b0f
Show file tree
Hide file tree
Showing 9 changed files with 2,457 additions and 202 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ts/message/rpc*
20 changes: 10 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: yarn lint
- run: yarn prebuild
- run: yarn aegir dep-check
- run: npm install
- run: npm run lint
- run: npm run prebuild
- run: npx aegir dep-check
test-node:
needs: check
runs-on: ${{ matrix.os }}
Expand All @@ -29,23 +29,23 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: yarn
- run: yarn prebuild
- run: npm install
- run: npm run prebuild
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- uses: codecov/codecov-action@v1
test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: yarn prebuild
- run: npm install
- run: npm run prebuild
- run: npx aegir test -t browser -t webworker --bail
test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: yarn prebuild
- run: npm install
- run: npm run prebuild
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"types": "src/index.d.ts",
"scripts": {
"lint": "eslint --ext .ts ts",
"release": "aegir release",
"prebuild": "tsc",
"build": "aegir build",
"release": "aegir release --no-types",
"prebuild": "tsc && cp -R ts/message src",
"build": "npm run build:proto && npm run build:proto-types && npm run build:types",
"build:proto": "pbjs -t static-module --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o ts/message/rpc.js ./ts/message/rpc.proto",
"build:proto-types": "pbts -o ts/message/rpc.d.ts ts/message/rpc.js",
"build:types": "aegir build --no-types",
"pretest": "tsc",
"test": "aegir test",
"test:node": "aegir test --target node",
Expand All @@ -40,22 +43,22 @@
"denque": "^1.4.1",
"err-code": "^2.0.0",
"it-pipe": "^1.0.1",
"libp2p-interfaces": "^0.8.0",
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/update-pubsub-interface",
"peer-id": "^0.14.0",
"protons": "^2.0.0",
"protobufjs": "^6.10.2",
"time-cache": "^0.3.0",
"uint8arrays": "^1.1.0"
"uint8arrays": "^2.1.4"
},
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/mocha": "^7.0.2",
"@types/mocha": "^8.2.2",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"aegir": "^28.0.0",
"aegir": "^33.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"delay": "^4.3.0",
"delay": "^5.0.0",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"eslint": "^7.1.0",
Expand Down Expand Up @@ -98,4 +101,4 @@
"Patrick Michot <[email protected]>",
"Hugo Dias <[email protected]>"
]
}
}
86 changes: 41 additions & 45 deletions ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Pubsub, { InMessage, utils } from 'libp2p-interfaces/src/pubsub'
import { MessageCache } from './message-cache'
import {
RPCCodec,
RPC, Message,
ControlMessage, ControlIHave, ControlGraft, ControlIWant, ControlPrune, PeerInfo
} from './message'
import { RPC, IRPC } from './message/rpc'
import * as constants from './constants'
import { Heartbeat } from './heartbeat'
import { getGossipPeers } from './get-gossip-peers'
Expand Down Expand Up @@ -76,8 +72,8 @@ class Gossipsub extends Pubsub {
mesh: Map<string, Set<string>>
fanout: Map<string, Set<string>>
lastpub: Map<string, number>
gossip: Map<string, ControlIHave[]>
control: Map<string, ControlMessage>
gossip: Map<string, RPC.IControlIHave[]>
control: Map<string, RPC.IControlMessage>
peerhave:Map<string, number>
iasked:Map<string, number>
backoff: Map<string, Map<string, number>>
Expand All @@ -96,7 +92,7 @@ class Gossipsub extends Pubsub {
_libp2p: Libp2p
_options: GossipOptions
_directPeerInitial: NodeJS.Timeout
log: Debugger
log: Debugger & { err: Debugger }
// eslint-disable-next-line @typescript-eslint/ban-types
emit: (event: string | symbol, ...args: any[]) => boolean

Expand Down Expand Up @@ -201,15 +197,15 @@ class Gossipsub extends Pubsub {
* Map of pending messages to gossip
* peer id => control messages
*
* @type {Map<string, Array<ControlIHave object>> }
* @type {Map<string, Array<RPC.IControlIHave object>> }
*/
this.gossip = new Map()

/**
* Map of control messages
* peer id => control message
*
* @type {Map<string, ControlMessage object>}
* @type {Map<string, RPC.IControlMessage object>}
*/
this.control = new Map()

Expand Down Expand Up @@ -279,7 +275,7 @@ class Gossipsub extends Pubsub {
* @returns {RPC}
*/
_decodeRpc (bytes: Uint8Array) {
return RPCCodec.decode(bytes)
return RPC.decode(bytes)
}

/**
Expand All @@ -290,7 +286,7 @@ class Gossipsub extends Pubsub {
* @returns {Uint8Array}
*/
_encodeRpc (rpc: RPC) {
return RPCCodec.encode(rpc)
return RPC.encode(rpc).finish()
}

/**
Expand Down Expand Up @@ -378,18 +374,18 @@ class Gossipsub extends Pubsub {
/**
* Handles an rpc control message from a peer
* @param {string} id peer id
* @param {ControlMessage} controlMsg
* @param {RPC.IControlMessage} controlMsg
* @returns {void}
*/
_processRpcControlMessage (id: string, controlMsg: ControlMessage): void {
_processRpcControlMessage (id: string, controlMsg: RPC.IControlMessage): void {
if (!controlMsg) {
return
}

const iwant = this._handleIHave(id, controlMsg.ihave)
const ihave = this._handleIWant(id, controlMsg.iwant)
const prune = this._handleGraft(id, controlMsg.graft)
this._handlePrune(id, controlMsg.prune)
const iwant = controlMsg.ihave ? this._handleIHave(id, controlMsg.ihave) : []
const ihave = controlMsg.iwant ? this._handleIWant(id, controlMsg.iwant) : []
const prune = controlMsg.graft ? this._handleGraft(id, controlMsg.graft) : []
controlMsg.prune && this._handlePrune(id, controlMsg.prune)

if (!iwant.length && !ihave.length && !prune.length) {
return
Expand Down Expand Up @@ -450,10 +446,10 @@ class Gossipsub extends Pubsub {
/**
* Handles IHAVE messages
* @param {string} id peer id
* @param {Array<ControlIHave>} ihave
* @returns {ControlIWant}
* @param {Array<RPC.IControlIHave>} ihave
* @returns {RPC.IControlIWant}
*/
_handleIHave (id: string, ihave: ControlIHave[]): ControlIWant[] {
_handleIHave (id: string, ihave: RPC.IControlIHave[]): RPC.IControlIWant[] {
if (!ihave.length) {
return []
}
Expand Down Expand Up @@ -491,7 +487,7 @@ class Gossipsub extends Pubsub {
const iwant = new Map<string, Uint8Array>()

ihave.forEach(({ topicID, messageIDs }) => {
if (!topicID || !this.mesh.has(topicID)) {
if (!topicID || !messageIDs || !this.mesh.has(topicID)) {
return
}

Expand Down Expand Up @@ -537,10 +533,10 @@ class Gossipsub extends Pubsub {
* Handles IWANT messages
* Returns messages to send back to peer
* @param {string} id peer id
* @param {Array<ControlIWant>} iwant
* @returns {Array<Message>}
* @param {Array<RPC.IControlIWant>} iwant
* @returns {Array<RPC.IMessage>}
*/
_handleIWant (id: string, iwant: ControlIWant[]): Message[] {
_handleIWant (id: string, iwant: RPC.IControlIWant[]): RPC.IMessage[] {
if (!iwant.length) {
return []
}
Expand All @@ -557,7 +553,7 @@ class Gossipsub extends Pubsub {
const ihave = new Map<string, InMessage>()

iwant.forEach(({ messageIDs }) => {
messageIDs.forEach((msgID) => {
messageIDs && messageIDs.forEach((msgID) => {
const [msg, count] = this.messageCache.getForPeer(msgID, id)
if (!msg) {
return
Expand Down Expand Up @@ -586,10 +582,10 @@ class Gossipsub extends Pubsub {
/**
* Handles Graft messages
* @param {string} id peer id
* @param {Array<ControlGraft>} graft
* @return {Array<ControlPrune>}
* @param {Array<RPC.IControlGraft>} graft
* @return {Array<RPC.IControlPrune>}
*/
_handleGraft (id: string, graft: ControlGraft[]): ControlPrune[] {
_handleGraft (id: string, graft: RPC.IControlGraft[]): RPC.IControlPrune[] {
const prune: string[] = []
const score = this.score.score(id)
const now = this._now()
Expand Down Expand Up @@ -682,10 +678,10 @@ class Gossipsub extends Pubsub {
/**
* Handles Prune messages
* @param {string} id peer id
* @param {Array<ControlPrune>} prune
* @param {Array<RPC.IControlPrune>} prune
* @returns {void}
*/
_handlePrune (id: string, prune: ControlPrune[]): void {
_handlePrune (id: string, prune: RPC.IControlPrune[]): void {
const score = this.score.score(id)
prune.forEach(({ topicID, backoff, peers }) => {
if (!topicID) {
Expand Down Expand Up @@ -811,10 +807,10 @@ class Gossipsub extends Pubsub {

/**
* Maybe attempt connection given signed peer records
* @param {PeerInfo[]} peers
* @param {RPC.IPeerInfo[]} peers
* @returns {Promise<void>}
*/
async _pxConnect (peers: PeerInfo[]): Promise<void> {
async _pxConnect (peers: RPC.IPeerInfo[]): Promise<void> {
if (peers.length > constants.GossipsubPrunePeers) {
shuffle(peers)
peers = peers.slice(0, constants.GossipsubPrunePeers)
Expand Down Expand Up @@ -1137,7 +1133,7 @@ class Gossipsub extends Pubsub {
/**
* @override
*/
_sendRpc (id: string, outRpc: RPC): void {
_sendRpc (id: string, outRpc: IRPC): void {
const peerStreams = this.peers.get(id)
if (!peerStreams || !peerStreams.isWritable) {
return
Expand All @@ -1157,10 +1153,10 @@ class Gossipsub extends Pubsub {
this.gossip.delete(id)
}

peerStreams.write(RPCCodec.encode(outRpc))
peerStreams.write(RPC.encode(outRpc).finish())
}

_piggybackControl (id: string, outRpc: RPC, ctrl: ControlMessage): void {
_piggybackControl (id: string, outRpc: IRPC, ctrl: RPC.IControlMessage): void {
const tograft = (ctrl.graft || [])
.filter(({ topicID }) => (topicID && this.mesh.get(topicID) || new Set()).has(id))
const toprune = (ctrl.prune || [])
Expand All @@ -1171,14 +1167,14 @@ class Gossipsub extends Pubsub {
}

if (outRpc.control) {
outRpc.control.graft = outRpc.control.graft.concat(tograft)
outRpc.control.prune = outRpc.control.prune.concat(toprune)
outRpc.control.graft = outRpc.control.graft && outRpc.control.graft.concat(tograft)
outRpc.control.prune = outRpc.control.prune && outRpc.control.prune.concat(toprune)
} else {
outRpc.control = { ihave: [], iwant: [], graft: tograft, prune: toprune }
}
}

_piggybackGossip (id: string, outRpc: RPC, ihave: ControlIHave[]): void {
_piggybackGossip (id: string, outRpc: IRPC, ihave: RPC.IControlIHave[]): void {
if (!outRpc.control) {
outRpc.control = { ihave: [], iwant: [], graft: [], prune: [] }
}
Expand All @@ -1194,7 +1190,7 @@ class Gossipsub extends Pubsub {
const doPX = this._options.doPX
for (const [id, topics] of tograft) {
const graft = topics.map((topicID) => ({ topicID }))
let prune: ControlPrune[] = []
let prune: RPC.IControlPrune[] = []
// If a peer also has prunes, process them now
const pruning = toprune.get(id)
if (pruning) {
Expand Down Expand Up @@ -1305,10 +1301,10 @@ class Gossipsub extends Pubsub {
/**
* Adds new IHAVE messages to pending gossip
* @param {PeerStreams} peerStreams
* @param {Array<ControlIHave>} controlIHaveMsgs
* @param {Array<RPC.IControlIHave>} controlIHaveMsgs
* @returns {void}
*/
_pushGossip (id: string, controlIHaveMsgs: ControlIHave): void {
_pushGossip (id: string, controlIHaveMsgs: RPC.IControlIHave): void {
this.log('Add gossip to %s', id)
const gossip = this.gossip.get(id) || []
this.gossip.set(id, gossip.concat(controlIHaveMsgs))
Expand All @@ -1327,9 +1323,9 @@ class Gossipsub extends Pubsub {
* @param {string} id
* @param {string} topic
* @param {boolean} doPX
* @returns {ControlPrune}
* @returns {RPC.IControlPrune}
*/
_makePrune (id: string, topic: string, doPX: boolean): ControlPrune {
_makePrune (id: string, topic: string, doPX: boolean): RPC.IControlPrune {
if (this.peers.get(id)!.protocol === constants.GossipsubIDv10) {
// Gossipsub v1.0 -- no backoff, the peer won't be able to parse it anyway
return {
Expand All @@ -1340,7 +1336,7 @@ class Gossipsub extends Pubsub {
// backoff is measured in seconds
// GossipsubPruneBackoff is measured in milliseconds
const backoff = constants.GossipsubPruneBackoff / 1000
const px: PeerInfo[] = []
const px: RPC.IPeerInfo[] = []
if (doPX) {
// select peers for Peer eXchange
const peers = getGossipPeers(this, topic, constants.GossipsubPrunePeers, (xid: string): boolean => {
Expand Down
Loading

0 comments on commit aec5b0f

Please sign in to comment.