Skip to content

Commit 6abb64d

Browse files
committed
chore: use libp2p 0.28.x
1 parent bd676f0 commit 6abb64d

11 files changed

+103
-78
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
2-
cache: npm
2+
cache: false
33

44
stages:
55
- check

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
"iso-random-stream": "^1.1.1",
5353
"it-all": "^1.0.2",
5454
"it-drain": "^1.0.1",
55-
"libp2p": "^0.27.0",
56-
"libp2p-kad-dht": "^0.18.3",
55+
"libp2p": "libp2p/js-libp2p#0.28.x",
56+
"libp2p-kad-dht": "^0.19.1",
5757
"libp2p-mplex": "^0.9.2",
5858
"libp2p-secio": "^0.12.1",
5959
"libp2p-tcp": "^0.14.2",
@@ -65,9 +65,7 @@
6565
"p-defer": "^3.0.0",
6666
"p-event": "^4.1.0",
6767
"p-wait-for": "^3.1.0",
68-
"peer-book": "~0.9.0",
6968
"peer-id": "^0.13.5",
70-
"peer-info": "^0.17.0",
7169
"promisify-es6": "^1.0.3",
7270
"rimraf": "^3.0.0",
7371
"sinon": "^9.0.0",

src/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const statsKeys = [
3737
class Bitswap {
3838
constructor (libp2p, blockstore, options) {
3939
this._libp2p = libp2p
40-
this._log = logger(this.peerInfo.id)
40+
this._log = logger(this.peerId)
4141

4242
this._options = Object.assign({}, defaultOptions, options)
4343

@@ -54,16 +54,16 @@ class Bitswap {
5454
// local database
5555
this.blockstore = blockstore
5656

57-
this.engine = new DecisionEngine(this.peerInfo.id, blockstore, this.network, this._stats)
57+
this.engine = new DecisionEngine(this.peerId, blockstore, this.network, this._stats)
5858

5959
// handle message sending
60-
this.wm = new WantManager(this.peerInfo.id, this.network, this._stats)
60+
this.wm = new WantManager(this.peerId, this.network, this._stats)
6161

62-
this.notifications = new Notifications(this.peerInfo.id)
62+
this.notifications = new Notifications(this.peerId)
6363
}
6464

65-
get peerInfo () {
66-
return this._libp2p.peerInfo
65+
get peerId () {
66+
return this._libp2p.peerId
6767
}
6868

6969
// handle messages received through the network

src/network.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const BITSWAP120 = '/ipfs/bitswap/1.2.0'
1313

1414
class Network {
1515
constructor (libp2p, bitswap, options, stats) {
16-
this._log = logger(libp2p.peerInfo.id, 'network')
16+
this._log = logger(libp2p.peerId, 'network')
1717
options = options || {}
1818
this.libp2p = libp2p
1919
this.bitswap = bitswap
@@ -37,14 +37,14 @@ class Network {
3737
this._running = true
3838
this.libp2p.handle(this.protocols, this._onConnection)
3939

40-
this.libp2p.on('peer:connect', this._onPeerConnect)
41-
this.libp2p.on('peer:disconnect', this._onPeerDisconnect)
40+
this.libp2p.connectionManager.on('peer:connect', this._onPeerConnect)
41+
this.libp2p.connectionManager.on('peer:disconnect', this._onPeerDisconnect)
4242

4343
// All existing connections are like new ones for us
4444
for (const peer of this.libp2p.peerStore.peers.values()) {
45-
if (this.libp2p.registrar.getConnection(peer)) {
46-
this._onPeerConnect(peer)
47-
}
45+
const conn = this.libp2p.connectionManager.get(peer.id)
46+
47+
conn && this._onPeerConnect(conn)
4848
}
4949
}
5050

@@ -54,8 +54,8 @@ class Network {
5454
// Unhandle both, libp2p doesn't care if it's not already handled
5555
this.libp2p.unhandle(this.protocols)
5656

57-
this.libp2p.removeListener('peer:connect', this._onPeerConnect)
58-
this.libp2p.removeListener('peer:disconnect', this._onPeerDisconnect)
57+
this.libp2p.connectionManager.removeListener('peer:connect', this._onPeerConnect)
58+
this.libp2p.connectionManager.removeListener('peer:disconnect', this._onPeerDisconnect)
5959
}
6060

6161
/**
@@ -92,12 +92,12 @@ class Network {
9292
}
9393
}
9494

95-
_onPeerConnect (peerInfo) {
96-
this.bitswap._onPeerConnected(peerInfo.id)
95+
_onPeerConnect (connection) {
96+
this.bitswap._onPeerConnected(connection.remotePeer)
9797
}
9898

99-
_onPeerDisconnect (peerInfo) {
100-
this.bitswap._onPeerDisconnected(peerInfo.id)
99+
_onPeerDisconnect (connection) {
100+
this.bitswap._onPeerDisconnected(connection.remotePeer)
101101
}
102102

103103
/**
@@ -181,7 +181,7 @@ class Network {
181181
/**
182182
* Connects to another peer
183183
*
184-
* @param {PeerInfo|PeerId|Multiaddr} peer
184+
* @param {PeerId|Multiaddr} peer
185185
* @param {Object} options
186186
* @param {AbortSignal} options.abortSignal
187187
* @returns {Promise<Connection>}

test/bitswap-stats.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ describe('bitswap stats', () => {
169169
bs2 = bitswaps[1]
170170
bs2.start()
171171

172-
await libp2pNodes[0].dial(libp2pNodes[1].peerInfo)
172+
libp2pNodes[0].peerStore.addressBook.set(libp2pNodes[1].peerId, libp2pNodes[1].multiaddrs)
173+
await libp2pNodes[0].dial(libp2pNodes[1].peerId)
173174

174175
block = await makeBlock()
175176

@@ -212,7 +213,7 @@ describe('bitswap stats', () => {
212213
})
213214

214215
it('has peer stats', async () => {
215-
const peerStats = bs2.stat().forPeer(libp2pNodes[0].peerInfo.id)
216+
const peerStats = bs2.stat().forPeer(libp2pNodes[0].peerId)
216217
expect(peerStats).to.exist()
217218

218219
const stats = await pEvent(peerStats, 'update')

test/bitswap.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { expect } = require('aegir/utils/chai')
55
const delay = require('delay')
66
const PeerId = require('peer-id')
77
const sinon = require('sinon')
8+
const pWaitFor = require('p-wait-for')
89

910
const Bitswap = require('../src')
1011

@@ -38,9 +39,12 @@ describe('bitswap without DHT', function () {
3839
])
3940

4041
// connect 0 -> 1 && 1 -> 2
42+
nodes[0].libp2pNode.peerStore.addressBook.set(nodes[1].libp2pNode.peerId, nodes[1].libp2pNode.multiaddrs)
43+
nodes[1].libp2pNode.peerStore.addressBook.set(nodes[2].libp2pNode.peerId, nodes[2].libp2pNode.multiaddrs)
44+
4145
await Promise.all([
42-
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerInfo),
43-
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerInfo)
46+
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerId),
47+
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerId)
4448
])
4549
})
4650

@@ -132,10 +136,22 @@ describe('bitswap with DHT', function () {
132136
])
133137

134138
// connect 0 -> 1 && 1 -> 2
139+
nodes[0].libp2pNode.peerStore.addressBook.set(nodes[1].libp2pNode.peerId, nodes[1].libp2pNode.multiaddrs)
140+
nodes[1].libp2pNode.peerStore.addressBook.set(nodes[2].libp2pNode.peerId, nodes[2].libp2pNode.multiaddrs)
141+
142+
await Promise.all([
143+
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerId),
144+
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerId)
145+
])
146+
147+
// await dht routing table are updated
135148
await Promise.all([
136-
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerInfo),
137-
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerInfo)
149+
pWaitFor(() => nodes[0].libp2pNode._dht.routingTable.size >= 1),
150+
pWaitFor(() => nodes[1].libp2pNode._dht.routingTable.size >= 1)
138151
])
152+
153+
// Give time to process
154+
await delay(300)
139155
})
140156

141157
after(async () => {
@@ -151,7 +167,7 @@ describe('bitswap with DHT', function () {
151167
await nodes[2].bitswap.put(block)
152168

153169
// Give put time to process
154-
await delay(100)
170+
await delay(300)
155171

156172
const blockRetrieved = await nodes[0].bitswap.get(block.cid)
157173
expect(block.data).to.eql(blockRetrieved.data)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async function exchangeBlocks (nodes, blocksPerNode = 10) {
7979
const d = Date.now()
8080

8181
// fetch all blocks on every node
82-
await Promise.all(nodes.map(async (node) => {
82+
await Promise.all(nodes.map(async (node, index) => {
8383
const bs = await Promise.all(cids.map((cid) => node.bitswap.get(cid)))
8484
expect(bs).to.deep.equal(blocks)
8585
}))

test/network/network.node.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('network', () => {
7676

7777
it('connectTo fail', async () => {
7878
try {
79-
await networkA.connectTo(p2pB.peerInfo.id)
79+
await networkA.connectTo(p2pB.peerId)
8080
assert.fail()
8181
} catch (err) {
8282
expect(err).to.exist()
@@ -87,24 +87,26 @@ describe('network', () => {
8787
var counter = 0
8888

8989
bitswapMockA._onPeerConnected = (peerId) => {
90-
expect(peerId.toB58String()).to.equal(p2pB.peerInfo.id.toB58String())
90+
expect(peerId.toB58String()).to.equal(p2pB.peerId.toB58String())
9191
counter++
9292
}
9393

9494
bitswapMockB._onPeerConnected = (peerId) => {
95-
expect(peerId.toB58String()).to.equal(p2pA.peerInfo.id.toB58String())
95+
expect(peerId.toB58String()).to.equal(p2pA.peerId.toB58String())
9696
counter++
9797
}
9898

99-
await p2pA.dial(p2pB.peerInfo)
99+
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
100+
await p2pA.dial(p2pB.peerId)
100101

101102
await pWaitFor(() => counter >= 2)
102103
bitswapMockA._onPeerConnected = () => {}
103104
bitswapMockB._onPeerConnected = () => {}
104105
})
105106

106107
it('connectTo success', async () => {
107-
await networkA.connectTo(p2pB.peerInfo)
108+
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
109+
await networkA.connectTo(p2pB.peerId)
108110
})
109111

110112
const versions = [{
@@ -134,7 +136,8 @@ describe('network', () => {
134136

135137
bitswapMockB._receiveError = (err) => deferred.reject(err)
136138

137-
const { stream } = await p2pA.dialProtocol(p2pB.peerInfo, '/ipfs/bitswap/' + version.num)
139+
// TODO: set addr
140+
const { stream } = await p2pA.dialProtocol(p2pB.peerId, '/ipfs/bitswap/' + version.num)
138141
await pipe(
139142
[version.serialize(msg)],
140143
lp.encode(),
@@ -165,11 +168,12 @@ describe('network', () => {
165168

166169
bitswapMockB._receiveError = deferred.reject
167170

168-
await networkA.sendMessage(p2pB.peerInfo.id, msg)
171+
await networkA.sendMessage(p2pB.peerId, msg)
169172
})
170173

171174
it('dial to peer on Bitswap 1.0.0', async () => {
172-
const { protocol } = await p2pA.dialProtocol(p2pC.peerInfo, ['/ipfs/bitswap/1.1.0', '/ipfs/bitswap/1.0.0'])
175+
p2pA.peerStore.addressBook.set(p2pC.peerId, p2pC.multiaddrs)
176+
const { protocol } = await p2pA.dialProtocol(p2pC.peerId, ['/ipfs/bitswap/1.1.0', '/ipfs/bitswap/1.0.0'])
173177

174178
expect(protocol).to.equal('/ipfs/bitswap/1.0.0')
175179
})
@@ -194,7 +198,7 @@ describe('network', () => {
194198

195199
bitswapMockC._receiveError = deferred.reject
196200

197-
await networkA.sendMessage(p2pC.peerInfo.id, msg)
201+
await networkA.sendMessage(p2pC.peerId, msg)
198202
await deferred.promise
199203
})
200204

@@ -208,16 +212,15 @@ describe('network', () => {
208212
networkA.start()
209213
networkB.start()
210214

211-
// FIXME: have to already be connected as sendMessage only accepts a peer id, not a PeerInfo
212-
await p2pA.dial(p2pB.peerInfo)
215+
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
213216

214217
const deferred = pDefer()
215218

216219
bitswapMockB._receiveMessage = () => {
217220
deferred.resolve()
218221
}
219222

220-
await networkA.sendMessage(p2pB.peerInfo.id, new Message(true))
223+
await networkA.sendMessage(p2pB.peerId, new Message(true))
221224

222225
return deferred
223226
})

test/utils/connect-all.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const without = require('lodash.without')
55
module.exports = async (nodes) => {
66
for (const node of nodes) {
77
for (const otherNode of without(nodes, node)) {
8-
await node.libp2pNode.dial(otherNode.bitswap.peerInfo)
8+
// TODO: set addrs
9+
await node.libp2pNode.dial(otherNode.bitswap.peerId)
910
}
1011
}
1112
}

test/utils/create-libp2p-node.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const MPLEX = require('libp2p-mplex')
55
const SECIO = require('libp2p-secio')
66
const libp2p = require('libp2p')
77
const KadDHT = require('libp2p-kad-dht')
8-
const PeerInfo = require('peer-info')
98
const PeerId = require('peer-id')
9+
1010
const defaultsDeep = require('@nodeutils/defaults-deep')
1111

1212
class Node extends libp2p {
@@ -38,10 +38,13 @@ class Node extends libp2p {
3838

3939
async function createLibp2pNode (options = {}) {
4040
const id = await PeerId.create({ bits: 512 })
41-
const peerInfo = new PeerInfo(id)
42-
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
43-
options.peerInfo = peerInfo
44-
const node = new Node(options)
41+
const node = new Node({
42+
peerId: id,
43+
addresses: {
44+
listen: ['/ip4/0.0.0.0/tcp/0']
45+
},
46+
...options
47+
})
4548
await node.start()
4649

4750
return node

0 commit comments

Comments
 (0)