Skip to content

Commit f83007d

Browse files
committed
chore: use libp2p 0.28.x
1 parent 7c7dfa9 commit f83007d

10 files changed

+89
-74
lines changed

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"ipfs-repo": "^2.0.0",
5454
"ipfs-utils": "^2.2.0",
5555
"iso-random-stream": "^1.1.1",
56-
"libp2p": "^0.27.0",
57-
"libp2p-kad-dht": "^0.18.3",
56+
"libp2p": "libp2p/js-libp2p#0.28.x",
57+
"libp2p-kad-dht": "^0.19.1",
5858
"libp2p-mplex": "^0.9.2",
5959
"libp2p-secio": "^0.12.1",
6060
"libp2p-tcp": "^0.14.2",
@@ -66,9 +66,7 @@
6666
"p-defer": "^3.0.0",
6767
"p-event": "^4.1.0",
6868
"p-wait-for": "^3.1.0",
69-
"peer-book": "~0.9.0",
7069
"peer-id": "^0.13.5",
71-
"peer-info": "^0.17.0",
7270
"promisify-es6": "^1.0.3",
7371
"rimraf": "^3.0.0",
7472
"stats-lite": "^2.2.0",

src/index.js

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

4040
this._options = Object.assign({}, defaultOptions, options)
4141

@@ -52,16 +52,16 @@ class Bitswap {
5252
// local database
5353
this.blockstore = blockstore
5454

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

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

60-
this.notifications = new Notifications(this.peerInfo.id)
60+
this.notifications = new Notifications(this.peerId)
6161
}
6262

63-
get peerInfo () {
64-
return this._libp2p.peerInfo
63+
get peerId () {
64+
return this._libp2p.peerId
6565
}
6666

6767
// 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
/**
@@ -168,7 +168,7 @@ class Network {
168168
/**
169169
* Connects to another peer
170170
*
171-
* @param {PeerInfo|PeerId|Multiaddr} peer
171+
* @param {PeerId|Multiaddr} peer
172172
* @returns {Promise.<Connection>}
173173
*/
174174
async connectTo (peer) { // eslint-disable-line require-await

test/bitswap-stats.js

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

174-
await libp2pNodes[0].dial(libp2pNodes[1].peerInfo)
174+
libp2pNodes[0].peerStore.addressBook.set(libp2pNodes[1].peerId, libp2pNodes[1].multiaddrs)
175+
await libp2pNodes[0].dial(libp2pNodes[1].peerId)
175176

176177
block = await makeBlock()
177178

@@ -214,7 +215,7 @@ describe('bitswap stats', () => {
214215
})
215216

216217
it('has peer stats', async () => {
217-
const peerStats = bs2.stat().forPeer(libp2pNodes[0].peerInfo.id)
218+
const peerStats = bs2.stat().forPeer(libp2pNodes[0].peerId)
218219
expect(peerStats).to.exist()
219220

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

test/bitswap.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ describe('bitswap without DHT', function () {
3737
])
3838

3939
// connect 0 -> 1 && 1 -> 2
40+
nodes[0].libp2pNode.peerStore.addressBook.set(nodes[1].libp2pNode.peerId, nodes[1].libp2pNode.multiaddrs)
41+
nodes[1].libp2pNode.peerStore.addressBook.set(nodes[2].libp2pNode.peerId, nodes[2].libp2pNode.multiaddrs)
42+
4043
await Promise.all([
41-
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerInfo),
42-
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerInfo)
44+
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerId),
45+
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerId)
4346
])
4447
})
4548

@@ -85,9 +88,12 @@ describe('bitswap with DHT', function () {
8588
])
8689

8790
// connect 0 -> 1 && 1 -> 2
91+
nodes[0].libp2pNode.peerStore.addressBook.set(nodes[1].libp2pNode.peerId, nodes[1].libp2pNode.multiaddrs)
92+
nodes[1].libp2pNode.peerStore.addressBook.set(nodes[2].libp2pNode.peerId, nodes[2].libp2pNode.multiaddrs)
93+
8894
await Promise.all([
89-
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerInfo),
90-
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerInfo)
95+
nodes[0].libp2pNode.dial(nodes[1].libp2pNode.peerId),
96+
nodes[1].libp2pNode.dial(nodes[2].libp2pNode.peerId)
9197
])
9298
})
9399

@@ -101,7 +107,6 @@ describe('bitswap with DHT', function () {
101107

102108
it('put a block in 2, get it in 0', async () => {
103109
const block = await makeBlock()
104-
nodes[2].bitswap.put(block)
105110

106111
await nodes[2].bitswap.put(block)
107112

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

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

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

test/network/network.node.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('network', () => {
7878

7979
it('connectTo fail', async () => {
8080
try {
81-
await networkA.connectTo(p2pB.peerInfo.id)
81+
await networkA.connectTo(p2pB.peerId)
8282
chai.assert.fail()
8383
} catch (err) {
8484
expect(err).to.exist()
@@ -89,24 +89,26 @@ describe('network', () => {
8989
var counter = 0
9090

9191
bitswapMockA._onPeerConnected = (peerId) => {
92-
expect(peerId.toB58String()).to.equal(p2pB.peerInfo.id.toB58String())
92+
expect(peerId.toB58String()).to.equal(p2pB.peerId.toB58String())
9393
counter++
9494
}
9595

9696
bitswapMockB._onPeerConnected = (peerId) => {
97-
expect(peerId.toB58String()).to.equal(p2pA.peerInfo.id.toB58String())
97+
expect(peerId.toB58String()).to.equal(p2pA.peerId.toB58String())
9898
counter++
9999
}
100100

101-
await p2pA.dial(p2pB.peerInfo)
101+
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
102+
await p2pA.dial(p2pB.peerId)
102103

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

108109
it('connectTo success', async () => {
109-
await networkA.connectTo(p2pB.peerInfo)
110+
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
111+
await networkA.connectTo(p2pB.peerId)
110112
})
111113

112114
const versions = [{
@@ -136,7 +138,8 @@ describe('network', () => {
136138

137139
bitswapMockB._receiveError = (err) => deferred.reject(err)
138140

139-
const { stream } = await p2pA.dialProtocol(p2pB.peerInfo, '/ipfs/bitswap/' + version.num)
141+
// TODO: set addr
142+
const { stream } = await p2pA.dialProtocol(p2pB.peerId, '/ipfs/bitswap/' + version.num)
140143
await pipe(
141144
[version.serialize(msg)],
142145
lp.encode(),
@@ -167,11 +170,12 @@ describe('network', () => {
167170

168171
bitswapMockB._receiveError = deferred.reject
169172

170-
await networkA.sendMessage(p2pB.peerInfo.id, msg)
173+
await networkA.sendMessage(p2pB.peerId, msg)
171174
})
172175

173176
it('dial to peer on Bitswap 1.0.0', async () => {
174-
const { protocol } = await p2pA.dialProtocol(p2pC.peerInfo, ['/ipfs/bitswap/1.1.0', '/ipfs/bitswap/1.0.0'])
177+
p2pA.peerStore.addressBook.set(p2pC.peerId, p2pC.multiaddrs)
178+
const { protocol } = await p2pA.dialProtocol(p2pC.peerId, ['/ipfs/bitswap/1.1.0', '/ipfs/bitswap/1.0.0'])
175179

176180
expect(protocol).to.equal('/ipfs/bitswap/1.0.0')
177181
})
@@ -196,7 +200,7 @@ describe('network', () => {
196200

197201
bitswapMockC._receiveError = deferred.reject
198202

199-
await networkA.sendMessage(p2pC.peerInfo.id, msg)
203+
await networkA.sendMessage(p2pC.peerId, msg)
200204
await deferred.promise
201205
})
202206
})

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)