Skip to content

Commit 44dfcb5

Browse files
authored
fix: await network start and stop (#415)
Starting/stopping network operations needs to be async
1 parent 2d8e20f commit 44dfcb5

File tree

8 files changed

+54
-52
lines changed

8 files changed

+54
-52
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
"iso-random-stream": "^2.0.0",
200200
"it-all": "^1.0.5",
201201
"it-drain": "^1.0.4",
202-
"libp2p": "libp2p/js-libp2p#feat/async-peerstore",
202+
"libp2p": "next",
203203
"libp2p-kad-dht": "^0.28.4",
204204
"libp2p-mplex": "^0.10.2",
205205
"libp2p-tcp": "^0.17.1",

src/bitswap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,20 @@ export class Bitswap extends BaseBlockstore {
427427
/**
428428
* Start the bitswap node
429429
*/
430-
start () {
430+
async start () {
431431
this.wm.start()
432-
this.network.start()
432+
await this.network.start()
433433
this.engine.start()
434434
this.started = true
435435
}
436436

437437
/**
438438
* Stop the bitswap node
439439
*/
440-
stop () {
440+
async stop () {
441441
this._stats.stop()
442442
this.wm.stop()
443-
this.network.stop()
443+
await this.network.stop()
444444
this.engine.stop()
445445
this.started = false
446446
}

test/bitswap-mock-internals.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('bitswap with mocks', function () {
5959
describe('receive message', () => {
6060
it('simple block message', async () => {
6161
const bs = new Bitswap(mockLibp2pNode(), blockstore)
62-
bs.start()
62+
await bs.start()
6363

6464
const other = ids[1]
6565

@@ -93,12 +93,12 @@ describe('bitswap with mocks', function () {
9393
expect(ledger.recv).to.equal(96)
9494
expect(ledger.exchanged).to.equal(2)
9595

96-
bs.stop()
96+
await bs.stop()
9797
})
9898

9999
it('simple want message', async () => {
100100
const bs = new Bitswap(mockLibp2pNode(), blockstore)
101-
bs.start()
101+
await bs.start()
102102

103103
const other = ids[1]
104104
const b1 = blocks[0]
@@ -116,14 +116,14 @@ describe('bitswap with mocks', function () {
116116
expect(wl.has(b1.cid.toString(base58btc))).to.eql(true)
117117
expect(wl.has(b2.cid.toString(base58btc))).to.eql(true)
118118

119-
bs.stop()
119+
await bs.stop()
120120
})
121121

122122
it('multi peer', async function () {
123123
this.timeout(80 * 1000)
124124
const bs = new Bitswap(mockLibp2pNode(), blockstore)
125125

126-
bs.start()
126+
await bs.start()
127127

128128
const others = await makePeerIds(5)
129129
const blocks = await makeBlocks(10)
@@ -147,12 +147,12 @@ describe('bitswap with mocks', function () {
147147
await storeHasBlocks(msg, blockstore)
148148
}
149149

150-
bs.stop()
150+
await bs.stop()
151151
})
152152

153153
it('ignore unwanted blocks', async () => {
154154
const bs = new Bitswap(mockLibp2pNode(), blockstore)
155-
bs.start()
155+
await bs.start()
156156

157157
const other = ids[1]
158158

@@ -192,7 +192,7 @@ describe('bitswap with mocks', function () {
192192
expect(ledger.recv).to.equal(144)
193193
expect(ledger.exchanged).to.equal(3)
194194

195-
bs.stop()
195+
await bs.stop()
196196
})
197197
})
198198

@@ -256,7 +256,7 @@ describe('bitswap with mocks', function () {
256256
bs.network = net
257257
bs.wm.network = net
258258
bs.engine.network = net
259-
bs.start()
259+
await bs.start()
260260
const get = bs.get(block.cid)
261261

262262
setTimeout(() => {
@@ -269,7 +269,7 @@ describe('bitswap with mocks', function () {
269269
finish(2)
270270

271271
finish.assert()
272-
bs.stop()
272+
await bs.stop()
273273
})
274274

275275
it('block is sent after local add', async () => {
@@ -348,12 +348,12 @@ describe('bitswap with mocks', function () {
348348
// Create and start bs1
349349
const bs1 = new Bitswap(mockLibp2pNode(), blockstore)
350350
applyNetwork(bs1, n1)
351-
bs1.start()
351+
await bs1.start()
352352

353353
// Create and start bs2
354354
const bs2 = new Bitswap(mockLibp2pNode(), new MemoryBlockstore())
355355
applyNetwork(bs2, n2)
356-
bs2.start()
356+
await bs2.start()
357357

358358
bs1._onPeerConnected(other)
359359
bs2._onPeerConnected(me)
@@ -365,8 +365,8 @@ describe('bitswap with mocks', function () {
365365
const b1 = await p1
366366
expect(b1).to.equalBytes(block.data)
367367

368-
bs1.stop()
369-
bs2.stop()
368+
await bs1.stop()
369+
await bs2.stop()
370370
})
371371

372372
it('double get', async () => {
@@ -472,7 +472,7 @@ describe('bitswap with mocks', function () {
472472
describe('unwant', () => {
473473
it('removes blocks that are wanted multiple times', async () => {
474474
const bs = new Bitswap(mockLibp2pNode(), blockstore)
475-
bs.start()
475+
await bs.start()
476476

477477
const b = blocks[12]
478478
const p = Promise.all([
@@ -484,7 +484,7 @@ describe('bitswap with mocks', function () {
484484

485485
await expect(p).to.eventually.be.rejected()
486486

487-
bs.stop()
487+
await bs.stop()
488488
})
489489
})
490490

test/bitswap-stats.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('bitswap stats', () => {
6969
bs.wm.wantBlocks(blocks.map(b => b.cid))
7070

7171
// start the first bitswap
72-
bs.start()
72+
await bs.start()
7373
})
7474

7575
after(async () => {
@@ -170,7 +170,7 @@ describe('bitswap stats', () => {
170170

171171
before(async () => {
172172
bs2 = bitswaps[1]
173-
bs2.start()
173+
await bs2.start()
174174

175175
const ma = `${libp2pNodes[1].multiaddrs[0]}/p2p/${libp2pNodes[1].peerId.toB58String()}`
176176
await libp2pNodes[0].dial(ma)
@@ -180,8 +180,8 @@ describe('bitswap stats', () => {
180180
await bs.put(block.cid, block.data)
181181
})
182182

183-
after(() => {
184-
bs2.stop()
183+
after(async () => {
184+
await bs2.stop()
185185
})
186186

187187
it('updates stats on transfer', async () => {

test/bitswap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function createThing (dht) {
3030
}
3131
})
3232
const bitswap = new Bitswap(libp2pNode, new MemoryBlockstore())
33-
bitswap.start()
33+
await bitswap.start()
3434
return { libp2pNode, bitswap }
3535
}
3636

@@ -43,8 +43,8 @@ describe('start/stop', () => {
4343
register: () => {}
4444
},
4545
peerStore: {
46-
peers: {
47-
values: () => []
46+
getPeers: async function * () {
47+
yield * []
4848
}
4949
}
5050
}
@@ -53,11 +53,11 @@ describe('start/stop', () => {
5353

5454
expect(bitswap.isStarted()).to.be.false()
5555

56-
bitswap.start()
56+
await bitswap.start()
5757

5858
expect(bitswap.isStarted()).to.be.true()
5959

60-
bitswap.stop()
60+
await bitswap.stop()
6161

6262
expect(bitswap.isStarted()).to.be.false()
6363
})

test/network/gen-bitswap-network.node.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('gen Bitswap network', function () {
2121
}))
2222
expect(res).to.have.length(blocks.length)
2323

24-
node.bitswap.stop()
24+
await node.bitswap.stop()
2525
await node.libp2p.stop()
2626
})
2727

@@ -33,9 +33,9 @@ describe('gen Bitswap network', function () {
3333

3434
// -- actual test
3535
await exchangeBlocks(nodeArr, blocksPerNode)
36-
await Promise.all(nodeArr.map(node => {
37-
node.bitswap.stop()
38-
return node.libp2p.stop()
36+
await Promise.all(nodeArr.map(async node => {
37+
await node.bitswap.stop()
38+
await node.libp2p.stop()
3939
}))
4040
})
4141
})
@@ -53,7 +53,7 @@ async function exchangeBlocks (nodes, blocksPerNode = 10) {
5353

5454
// put blocksPerNode amount of blocks per node
5555
await Promise.all(nodes.map(async (node, i) => {
56-
node.bitswap.start()
56+
await node.bitswap.start()
5757

5858
const data = new Array(blocksPerNode).fill(0).map((_, j) => {
5959
const index = i * blocksPerNode + j

test/network/network.node.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ describe('network', () => {
7676
// @ts-expect-error {} is not a real libp2p
7777
networkC = new Network(p2pC, bitswapMockC, new Stats({}), { b100Only: true })
7878

79-
networkA.start()
80-
networkB.start()
81-
networkC.start()
79+
await networkA.start()
80+
await networkB.start()
81+
await networkC.start()
8282
})
8383

84-
afterEach(() => {
85-
p2pA.stop()
86-
p2pB.stop()
87-
p2pC.stop()
84+
afterEach(async () => {
85+
await p2pA.stop()
86+
await p2pB.stop()
87+
await p2pC.stop()
8888
})
8989

9090
it('connectTo fail', async () => {
@@ -146,14 +146,14 @@ describe('network', () => {
146146
p2pBConnected
147147
])
148148

149-
networkA.stop()
150-
networkB.stop()
149+
await networkA.stop()
150+
await networkB.stop()
151151

152152
p2pAConnected = pDefer()
153153
p2pBConnected = pDefer()
154154

155-
networkA.start()
156-
networkB.start()
155+
await networkA.start()
156+
await networkB.start()
157157

158158
await Promise.all([
159159
p2pAConnected,
@@ -278,8 +278,8 @@ describe('network', () => {
278278
networkB = new Network(p2pB, bitswapMockB, new Stats({}))
279279
networkB._protocols = ['/ipfs/bitswap/1.2.0']
280280

281-
networkA.start()
282-
networkB.start()
281+
await networkA.start()
282+
await networkB.start()
283283

284284
// In a real network scenario, peers will be discovered and their addresses
285285
// will be added to the addressBook before bitswap kicks in
@@ -310,9 +310,11 @@ describe('network', () => {
310310
registrar: {
311311
register: sinon.stub()
312312
},
313+
// @ts-expect-error incomplete implementation
313314
peerStore: {
314-
// @ts-expect-error {} incomplete implementation
315-
peers: new Map()
315+
getPeers: async function * () {
316+
yield * []
317+
}
316318
},
317319
dial: mockDial,
318320
handle: sinon.stub()
@@ -336,7 +338,7 @@ describe('network', () => {
336338
mockDial.withArgs(provider1.id).returns(Promise.reject(new Error('Could not dial')))
337339
mockDial.withArgs(provider2.id).returns(Promise.resolve())
338340

339-
network.start()
341+
await network.start()
340342

341343
await network.findAndConnect(cid)
342344

test/utils/create-bitswap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export const createBitswap = async () => {
1212
}
1313
})
1414
const bitswap = new Bitswap(libp2pNode, new MemoryBlockstore())
15-
bitswap.start()
15+
await bitswap.start()
1616
return { bitswap, libp2pNode }
1717
}

0 commit comments

Comments
 (0)