Skip to content

Commit aec5b0f

Browse files
committed
chore: update deps and remove protons
1 parent 12626e8 commit aec5b0f

File tree

9 files changed

+2457
-202
lines changed

9 files changed

+2457
-202
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ts/message/rpc*

.github/workflows/main.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v2
15-
- run: yarn
16-
- run: yarn lint
17-
- run: yarn prebuild
18-
- run: yarn aegir dep-check
15+
- run: npm install
16+
- run: npm run lint
17+
- run: npm run prebuild
18+
- run: npx aegir dep-check
1919
test-node:
2020
needs: check
2121
runs-on: ${{ matrix.os }}
@@ -29,23 +29,23 @@ jobs:
2929
- uses: actions/setup-node@v1
3030
with:
3131
node-version: ${{ matrix.node }}
32-
- run: yarn
33-
- run: yarn prebuild
32+
- run: npm install
33+
- run: npm run prebuild
3434
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
3535
- uses: codecov/codecov-action@v1
3636
test-chrome:
3737
needs: check
3838
runs-on: ubuntu-latest
3939
steps:
4040
- uses: actions/checkout@v2
41-
- run: yarn
42-
- run: yarn prebuild
41+
- run: npm install
42+
- run: npm run prebuild
4343
- run: npx aegir test -t browser -t webworker --bail
4444
test-firefox:
4545
needs: check
4646
runs-on: ubuntu-latest
4747
steps:
4848
- uses: actions/checkout@v2
49-
- run: yarn
50-
- run: yarn prebuild
49+
- run: npm install
50+
- run: npm run prebuild
5151
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
"types": "src/index.d.ts",
1212
"scripts": {
1313
"lint": "eslint --ext .ts ts",
14-
"release": "aegir release",
15-
"prebuild": "tsc",
16-
"build": "aegir build",
14+
"release": "aegir release --no-types",
15+
"prebuild": "tsc && cp -R ts/message src",
16+
"build": "npm run build:proto && npm run build:proto-types && npm run build:types",
17+
"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",
18+
"build:proto-types": "pbts -o ts/message/rpc.d.ts ts/message/rpc.js",
19+
"build:types": "aegir build --no-types",
1720
"pretest": "tsc",
1821
"test": "aegir test",
1922
"test:node": "aegir test --target node",
@@ -40,22 +43,22 @@
4043
"denque": "^1.4.1",
4144
"err-code": "^2.0.0",
4245
"it-pipe": "^1.0.1",
43-
"libp2p-interfaces": "^0.8.0",
46+
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/update-pubsub-interface",
4447
"peer-id": "^0.14.0",
45-
"protons": "^2.0.0",
48+
"protobufjs": "^6.10.2",
4649
"time-cache": "^0.3.0",
47-
"uint8arrays": "^1.1.0"
50+
"uint8arrays": "^2.1.4"
4851
},
4952
"devDependencies": {
5053
"@types/chai": "^4.2.3",
51-
"@types/mocha": "^7.0.2",
54+
"@types/mocha": "^8.2.2",
5255
"@typescript-eslint/eslint-plugin": "^3.0.2",
5356
"@typescript-eslint/parser": "^3.0.2",
54-
"aegir": "^28.0.0",
57+
"aegir": "^33.0.0",
5558
"benchmark": "^2.1.4",
5659
"chai": "^4.2.0",
5760
"chai-spies": "^1.0.0",
58-
"delay": "^4.3.0",
61+
"delay": "^5.0.0",
5962
"detect-node": "^2.0.4",
6063
"dirty-chai": "^2.0.1",
6164
"eslint": "^7.1.0",
@@ -98,4 +101,4 @@
98101
"Patrick Michot <[email protected]>",
99102
"Hugo Dias <[email protected]>"
100103
]
101-
}
104+
}

ts/index.ts

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Pubsub, { InMessage, utils } from 'libp2p-interfaces/src/pubsub'
22
import { MessageCache } from './message-cache'
3-
import {
4-
RPCCodec,
5-
RPC, Message,
6-
ControlMessage, ControlIHave, ControlGraft, ControlIWant, ControlPrune, PeerInfo
7-
} from './message'
3+
import { RPC, IRPC } from './message/rpc'
84
import * as constants from './constants'
95
import { Heartbeat } from './heartbeat'
106
import { getGossipPeers } from './get-gossip-peers'
@@ -76,8 +72,8 @@ class Gossipsub extends Pubsub {
7672
mesh: Map<string, Set<string>>
7773
fanout: Map<string, Set<string>>
7874
lastpub: Map<string, number>
79-
gossip: Map<string, ControlIHave[]>
80-
control: Map<string, ControlMessage>
75+
gossip: Map<string, RPC.IControlIHave[]>
76+
control: Map<string, RPC.IControlMessage>
8177
peerhave:Map<string, number>
8278
iasked:Map<string, number>
8379
backoff: Map<string, Map<string, number>>
@@ -96,7 +92,7 @@ class Gossipsub extends Pubsub {
9692
_libp2p: Libp2p
9793
_options: GossipOptions
9894
_directPeerInitial: NodeJS.Timeout
99-
log: Debugger
95+
log: Debugger & { err: Debugger }
10096
// eslint-disable-next-line @typescript-eslint/ban-types
10197
emit: (event: string | symbol, ...args: any[]) => boolean
10298

@@ -201,15 +197,15 @@ class Gossipsub extends Pubsub {
201197
* Map of pending messages to gossip
202198
* peer id => control messages
203199
*
204-
* @type {Map<string, Array<ControlIHave object>> }
200+
* @type {Map<string, Array<RPC.IControlIHave object>> }
205201
*/
206202
this.gossip = new Map()
207203

208204
/**
209205
* Map of control messages
210206
* peer id => control message
211207
*
212-
* @type {Map<string, ControlMessage object>}
208+
* @type {Map<string, RPC.IControlMessage object>}
213209
*/
214210
this.control = new Map()
215211

@@ -279,7 +275,7 @@ class Gossipsub extends Pubsub {
279275
* @returns {RPC}
280276
*/
281277
_decodeRpc (bytes: Uint8Array) {
282-
return RPCCodec.decode(bytes)
278+
return RPC.decode(bytes)
283279
}
284280

285281
/**
@@ -290,7 +286,7 @@ class Gossipsub extends Pubsub {
290286
* @returns {Uint8Array}
291287
*/
292288
_encodeRpc (rpc: RPC) {
293-
return RPCCodec.encode(rpc)
289+
return RPC.encode(rpc).finish()
294290
}
295291

296292
/**
@@ -378,18 +374,18 @@ class Gossipsub extends Pubsub {
378374
/**
379375
* Handles an rpc control message from a peer
380376
* @param {string} id peer id
381-
* @param {ControlMessage} controlMsg
377+
* @param {RPC.IControlMessage} controlMsg
382378
* @returns {void}
383379
*/
384-
_processRpcControlMessage (id: string, controlMsg: ControlMessage): void {
380+
_processRpcControlMessage (id: string, controlMsg: RPC.IControlMessage): void {
385381
if (!controlMsg) {
386382
return
387383
}
388384

389-
const iwant = this._handleIHave(id, controlMsg.ihave)
390-
const ihave = this._handleIWant(id, controlMsg.iwant)
391-
const prune = this._handleGraft(id, controlMsg.graft)
392-
this._handlePrune(id, controlMsg.prune)
385+
const iwant = controlMsg.ihave ? this._handleIHave(id, controlMsg.ihave) : []
386+
const ihave = controlMsg.iwant ? this._handleIWant(id, controlMsg.iwant) : []
387+
const prune = controlMsg.graft ? this._handleGraft(id, controlMsg.graft) : []
388+
controlMsg.prune && this._handlePrune(id, controlMsg.prune)
393389

394390
if (!iwant.length && !ihave.length && !prune.length) {
395391
return
@@ -450,10 +446,10 @@ class Gossipsub extends Pubsub {
450446
/**
451447
* Handles IHAVE messages
452448
* @param {string} id peer id
453-
* @param {Array<ControlIHave>} ihave
454-
* @returns {ControlIWant}
449+
* @param {Array<RPC.IControlIHave>} ihave
450+
* @returns {RPC.IControlIWant}
455451
*/
456-
_handleIHave (id: string, ihave: ControlIHave[]): ControlIWant[] {
452+
_handleIHave (id: string, ihave: RPC.IControlIHave[]): RPC.IControlIWant[] {
457453
if (!ihave.length) {
458454
return []
459455
}
@@ -491,7 +487,7 @@ class Gossipsub extends Pubsub {
491487
const iwant = new Map<string, Uint8Array>()
492488

493489
ihave.forEach(({ topicID, messageIDs }) => {
494-
if (!topicID || !this.mesh.has(topicID)) {
490+
if (!topicID || !messageIDs || !this.mesh.has(topicID)) {
495491
return
496492
}
497493

@@ -537,10 +533,10 @@ class Gossipsub extends Pubsub {
537533
* Handles IWANT messages
538534
* Returns messages to send back to peer
539535
* @param {string} id peer id
540-
* @param {Array<ControlIWant>} iwant
541-
* @returns {Array<Message>}
536+
* @param {Array<RPC.IControlIWant>} iwant
537+
* @returns {Array<RPC.IMessage>}
542538
*/
543-
_handleIWant (id: string, iwant: ControlIWant[]): Message[] {
539+
_handleIWant (id: string, iwant: RPC.IControlIWant[]): RPC.IMessage[] {
544540
if (!iwant.length) {
545541
return []
546542
}
@@ -557,7 +553,7 @@ class Gossipsub extends Pubsub {
557553
const ihave = new Map<string, InMessage>()
558554

559555
iwant.forEach(({ messageIDs }) => {
560-
messageIDs.forEach((msgID) => {
556+
messageIDs && messageIDs.forEach((msgID) => {
561557
const [msg, count] = this.messageCache.getForPeer(msgID, id)
562558
if (!msg) {
563559
return
@@ -586,10 +582,10 @@ class Gossipsub extends Pubsub {
586582
/**
587583
* Handles Graft messages
588584
* @param {string} id peer id
589-
* @param {Array<ControlGraft>} graft
590-
* @return {Array<ControlPrune>}
585+
* @param {Array<RPC.IControlGraft>} graft
586+
* @return {Array<RPC.IControlPrune>}
591587
*/
592-
_handleGraft (id: string, graft: ControlGraft[]): ControlPrune[] {
588+
_handleGraft (id: string, graft: RPC.IControlGraft[]): RPC.IControlPrune[] {
593589
const prune: string[] = []
594590
const score = this.score.score(id)
595591
const now = this._now()
@@ -682,10 +678,10 @@ class Gossipsub extends Pubsub {
682678
/**
683679
* Handles Prune messages
684680
* @param {string} id peer id
685-
* @param {Array<ControlPrune>} prune
681+
* @param {Array<RPC.IControlPrune>} prune
686682
* @returns {void}
687683
*/
688-
_handlePrune (id: string, prune: ControlPrune[]): void {
684+
_handlePrune (id: string, prune: RPC.IControlPrune[]): void {
689685
const score = this.score.score(id)
690686
prune.forEach(({ topicID, backoff, peers }) => {
691687
if (!topicID) {
@@ -811,10 +807,10 @@ class Gossipsub extends Pubsub {
811807

812808
/**
813809
* Maybe attempt connection given signed peer records
814-
* @param {PeerInfo[]} peers
810+
* @param {RPC.IPeerInfo[]} peers
815811
* @returns {Promise<void>}
816812
*/
817-
async _pxConnect (peers: PeerInfo[]): Promise<void> {
813+
async _pxConnect (peers: RPC.IPeerInfo[]): Promise<void> {
818814
if (peers.length > constants.GossipsubPrunePeers) {
819815
shuffle(peers)
820816
peers = peers.slice(0, constants.GossipsubPrunePeers)
@@ -1137,7 +1133,7 @@ class Gossipsub extends Pubsub {
11371133
/**
11381134
* @override
11391135
*/
1140-
_sendRpc (id: string, outRpc: RPC): void {
1136+
_sendRpc (id: string, outRpc: IRPC): void {
11411137
const peerStreams = this.peers.get(id)
11421138
if (!peerStreams || !peerStreams.isWritable) {
11431139
return
@@ -1157,10 +1153,10 @@ class Gossipsub extends Pubsub {
11571153
this.gossip.delete(id)
11581154
}
11591155

1160-
peerStreams.write(RPCCodec.encode(outRpc))
1156+
peerStreams.write(RPC.encode(outRpc).finish())
11611157
}
11621158

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

11731169
if (outRpc.control) {
1174-
outRpc.control.graft = outRpc.control.graft.concat(tograft)
1175-
outRpc.control.prune = outRpc.control.prune.concat(toprune)
1170+
outRpc.control.graft = outRpc.control.graft && outRpc.control.graft.concat(tograft)
1171+
outRpc.control.prune = outRpc.control.prune && outRpc.control.prune.concat(toprune)
11761172
} else {
11771173
outRpc.control = { ihave: [], iwant: [], graft: tograft, prune: toprune }
11781174
}
11791175
}
11801176

1181-
_piggybackGossip (id: string, outRpc: RPC, ihave: ControlIHave[]): void {
1177+
_piggybackGossip (id: string, outRpc: IRPC, ihave: RPC.IControlIHave[]): void {
11821178
if (!outRpc.control) {
11831179
outRpc.control = { ihave: [], iwant: [], graft: [], prune: [] }
11841180
}
@@ -1194,7 +1190,7 @@ class Gossipsub extends Pubsub {
11941190
const doPX = this._options.doPX
11951191
for (const [id, topics] of tograft) {
11961192
const graft = topics.map((topicID) => ({ topicID }))
1197-
let prune: ControlPrune[] = []
1193+
let prune: RPC.IControlPrune[] = []
11981194
// If a peer also has prunes, process them now
11991195
const pruning = toprune.get(id)
12001196
if (pruning) {
@@ -1305,10 +1301,10 @@ class Gossipsub extends Pubsub {
13051301
/**
13061302
* Adds new IHAVE messages to pending gossip
13071303
* @param {PeerStreams} peerStreams
1308-
* @param {Array<ControlIHave>} controlIHaveMsgs
1304+
* @param {Array<RPC.IControlIHave>} controlIHaveMsgs
13091305
* @returns {void}
13101306
*/
1311-
_pushGossip (id: string, controlIHaveMsgs: ControlIHave): void {
1307+
_pushGossip (id: string, controlIHaveMsgs: RPC.IControlIHave): void {
13121308
this.log('Add gossip to %s', id)
13131309
const gossip = this.gossip.get(id) || []
13141310
this.gossip.set(id, gossip.concat(controlIHaveMsgs))
@@ -1327,9 +1323,9 @@ class Gossipsub extends Pubsub {
13271323
* @param {string} id
13281324
* @param {string} topic
13291325
* @param {boolean} doPX
1330-
* @returns {ControlPrune}
1326+
* @returns {RPC.IControlPrune}
13311327
*/
1332-
_makePrune (id: string, topic: string, doPX: boolean): ControlPrune {
1328+
_makePrune (id: string, topic: string, doPX: boolean): RPC.IControlPrune {
13331329
if (this.peers.get(id)!.protocol === constants.GossipsubIDv10) {
13341330
// Gossipsub v1.0 -- no backoff, the peer won't be able to parse it anyway
13351331
return {
@@ -1340,7 +1336,7 @@ class Gossipsub extends Pubsub {
13401336
// backoff is measured in seconds
13411337
// GossipsubPruneBackoff is measured in milliseconds
13421338
const backoff = constants.GossipsubPruneBackoff / 1000
1343-
const px: PeerInfo[] = []
1339+
const px: RPC.IPeerInfo[] = []
13441340
if (doPX) {
13451341
// select peers for Peer eXchange
13461342
const peers = getGossipPeers(this, topic, constants.GossipsubPrunePeers, (xid: string): boolean => {

0 commit comments

Comments
 (0)