Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 153fd24

Browse files
authored
refactor: return peer ids as strings (#581)
As part of the async iterator refactor some peer IDs were being returned as CIDs - the conversion was done in IPFS but not in libp2p, which meant that where IPFS exposes libp2p directly, peer IDs were being returned as strings, e.g. pubsub topic subscribers, message senders, etc. The aim is to convert these to CIDs in the long run but such a change needs to be driven from inside libp2p instead of piecemeal at the IPFS layer. This PR changes the `ipfs.id().id` and `ipfs.swarm.peers().[].peer` properties to strings so we can do the CID conversion in one go. Also adds more in-depth tests for `ipfs.id` and documents the output from that method. BREAKING CHANGE: Where `PeerID`s were previously [CID]s, now they are Strings - `ipfs.bitswap.stat().peers[n]` is now a String (was a CID) - `ipfs.dht.findPeer().id` is now a String (was a CID) - `ipfs.dht.findProvs()[n].id` is now a String (was a CID) - `ipfs.dht.provide()[n].id` is now a String (was a CID) - `ipfs.dht.put()[n].id` is now a String (was a CID) - `ipfs.dht.query()[n].id` is now a String (was a CID) - `ipfs.id().id` is now a String (was a CID) - `ipfs.id().addresses[n]` are now [Multiaddr]s (were Strings) [CID]: https://www.npmjs.com/package/cids [Multiaddr]: https://www.npmjs.com/package/multiaddr
1 parent ced5ea1 commit 153fd24

File tree

12 files changed

+54
-30
lines changed

12 files changed

+54
-30
lines changed

SPEC/BITSWAP.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The returned object contains the following keys:
4747

4848
- `provideBufLen` is an integer.
4949
- `wantlist` (array of [CID][cid]s)
50-
- `peers` (array of peer IDs as [CID][cid] instances)
50+
- `peers` (array of peer IDs as Strings)
5151
- `blocksReceived` is a [BigNumber Int][1]
5252
- `dataReceived` is a [BigNumber Int][1]
5353
- `blocksSent` is a [BigNumber Int][1]
@@ -64,9 +64,9 @@ console.log(stats)
6464
// provideBufLen: 0,
6565
// wantlist: [ CID('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM') ],
6666
// peers:
67-
// [ CID('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM'),
68-
// CID('QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu'),
69-
// CID('QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd') ],
67+
// [ 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
68+
// 'QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
69+
// 'QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd' ],
7070
// blocksReceived: 0,
7171
// dataReceived: 0,
7272
// blocksSent: 0,

SPEC/DHT.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ Where `peerId` is a Peer ID in `String`, [`CID`](https://github.com/multiformats
1919

2020
| Type | Description |
2121
| -------- | -------- |
22-
| `Promise<{ id: CID, addrs: Multiaddr[] }>` | A promise that resolves to an object with `id` and `addrs`. `id` is a [`CID`](https://github.com/multiformats/js-cid) - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
22+
| `Promise<{ id: String, addrs: Multiaddr[] }>` | A promise that resolves to an object with `id` and `addrs`. `id` is a String - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
2323

2424
**Example:**
2525

2626
```JavaScript
2727
const info = await ipfs.dht.findPeer('QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt')
2828

29-
console.log(info.id.toString())
29+
console.log(info.id)
3030
/*
3131
QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
3232
*/
@@ -60,7 +60,7 @@ Note that if `options.numProviders` are not found an error will be thrown.
6060

6161
| Type | Description |
6262
| -------- | -------- |
63-
| `AsyncIterable<{ id: CID, addrs: Multiaddr[] }>` | A async iterable that yields objects with `id` and `addrs`. `id` is a [`CID`](https://github.com/multiformats/js-cid) - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
63+
| `AsyncIterable<{ id: String, addrs: Multiaddr[] }>` | A async iterable that yields objects with `id` and `addrs`. `id` is a String - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
6464

6565
**Example:**
6666

@@ -127,15 +127,15 @@ Prints objects like:
127127
128128
{
129129
extra: 'dial backoff',
130-
id: CID(QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z),
130+
id: 'QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z',
131131
responses: [
132132
{
133133
addrs: [
134134
Multiaddr(/ip4/127.0.0.1/tcp/4001),
135135
Multiaddr(/ip4/172.20.0.3/tcp/4001),
136136
Multiaddr(/ip4/35.178.190.196/tcp/1024)
137137
],
138-
id: CID(QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8)
138+
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
139139
}
140140
],
141141
type: 1
@@ -181,15 +181,15 @@ Prints objects like:
181181
182182
{
183183
extra: 'dial backoff',
184-
id: CID(QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z),
184+
id: 'QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z',
185185
responses: [
186186
{
187187
addrs: [
188188
Multiaddr(/ip4/127.0.0.1/tcp/4001),
189189
Multiaddr(/ip4/172.20.0.3/tcp/4001),
190190
Multiaddr(/ip4/35.178.190.196/tcp/1024)
191191
],
192-
id: CID(QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8)
192+
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
193193
}
194194
],
195195
type: 1
@@ -235,15 +235,15 @@ Prints objects like:
235235
236236
{
237237
extra: 'dial backoff',
238-
id: CID(QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z),
238+
id: 'QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z',
239239
responses: [
240240
{
241241
addrs: [
242242
Multiaddr(/ip4/127.0.0.1/tcp/4001),
243243
Multiaddr(/ip4/172.20.0.3/tcp/4001),
244244
Multiaddr(/ip4/35.178.190.196/tcp/1024)
245245
],
246-
id: CID(QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8)
246+
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
247247
}
248248
],
249249
type: 1

SPEC/MISCELLANEOUS.md

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
| -------- | -------- |
2020
| `Promise<Object>` | An object with the Peer identity |
2121

22+
The Peer identity has the following properties:
23+
24+
- `id: String` - the Peer ID
25+
- `publicKey: String` - the public key of the peer as a base64 encoded string
26+
- `addresses: Multiaddr[]` - A list of multiaddrs this node is listening on
27+
- `agentVersion: String` - The agent version
28+
- `protocolVersion: String` - The supported protocol version
29+
2230
**Example:**
2331

2432
```JavaScript

SPEC/SWARM.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
| Type | Description |
2020
| -------- | -------- |
21-
| `Promise<{ id: CID, addrs: Multiaddr[] }>` | A promise that resolves to an object with `id` and `addrs`. `id` is a [`CID`](https://github.com/multiformats/js-cid) - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
21+
| `Promise<{ id: String, addrs: Multiaddr[] }>` | A promise that resolves to an object with `id` and `addrs`. `id` is a String - the peer's ID and `addrs` is an array of [Multiaddr](https://github.com/multiformats/js-multiaddr/) - addresses for the peer. |
2222

2323
**Example:**
2424

2525
```JavaScript
2626
const peerInfos = await ipfs.swarm.addrs()
2727

2828
peerInfos.forEach(info => {
29-
console.log(info.id.toString())
29+
console.log(info.id)
3030
/*
3131
QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
3232
*/
@@ -131,7 +131,7 @@ A great source of [examples][] can be found in the tests for this API.
131131
The returned array has the following form:
132132

133133
- `addr: Multiaddr`
134-
- `peer: CID`
134+
- `peer: String`
135135
- `latency: String` - Only if `verbose: true` was passed
136136
- `muxer: String` - The type of stream muxer the peer is usng
137137
- `streams: string[]` - Only if `verbose: true`, a list of currently open streams

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"dependencies": {
4141
"chai": "^4.2.0",
4242
"chai-as-promised": "^7.1.1",
43+
"chai-things": "^0.2.0",
4344
"cids": "~0.7.1",
4445
"delay": "^4.3.0",
4546
"dirty-chai": "^2.0.1",

src/dht/find-peer.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ module.exports = (common, options) => {
2727
after(() => common.clean())
2828

2929
it('should find other peers', async () => {
30-
const res = await nodeA.dht.findPeer(nodeB.peerId.id)
31-
30+
const nodeBId = await nodeB.id()
31+
const res = await nodeA.dht.findPeer(nodeBId.id)
3232
const id = res.id.toString()
33-
const nodeAddresses = nodeB.peerId.addresses.map((addr) => addr.split('/ipfs/')[0]) // remove '/ipfs/'
34-
const peerAddresses = res.addrs.map(ma => ma.toString().split('/ipfs/')[0])
33+
34+
const nodeAddresses = nodeBId.addresses.map((addr) => addr.nodeAddress())
35+
const peerAddresses = res.addrs.map(ma => ma.nodeAddress())
3536

3637
expect(id).to.be.eql(nodeB.peerId.id)
37-
expect(nodeAddresses).to.include(peerAddresses[0])
38+
expect(peerAddresses).to.deep.include(nodeAddresses[0])
3839
})
3940

4041
it('should fail to find other peer if peer does not exist', () => {

src/miscellaneous/id.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
'use strict'
33

44
const { getDescribe, getIt, expect } = require('../utils/mocha')
5+
const Multiaddr = require('multiaddr')
6+
const CID = require('cids')
57

68
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
79
/**
@@ -24,8 +26,12 @@ module.exports = (common, options) => {
2426

2527
it('should get the node ID', async () => {
2628
const res = await ipfs.id()
27-
expect(res).to.have.a.property('id')
29+
expect(res).to.have.a.property('id').that.is.a('string')
30+
expect(CID.isCID(new CID(res.id))).to.equal(true)
2831
expect(res).to.have.a.property('publicKey')
32+
expect(res).to.have.a.property('addresses').that.is.an('array').and.all.satisfy(ma => Multiaddr.isMultiaddr(ma))
33+
expect(res).to.have.a.property('agentVersion').that.is.a('string')
34+
expect(res).to.have.a.property('protocolVersion').that.is.a('string')
2935
})
3036
})
3137
}

src/pubsub/peers.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ module.exports = (common, options) => {
2626
ipfs2 = (await common.spawn({ type: 'go' })).api
2727
ipfs3 = (await common.spawn({ type: 'go' })).api
2828

29-
const ipfs2Addr = ipfs2.peerId.addresses.find((a) => a.includes('127.0.0.1'))
30-
const ipfs3Addr = ipfs3.peerId.addresses.find((a) => a.includes('127.0.0.1'))
29+
const ipfs2Addr = ipfs2.peerId.addresses
30+
.find(ma => ma.nodeAddress().address === '127.0.0.1')
31+
const ipfs3Addr = ipfs3.peerId.addresses
32+
.find(ma => ma.nodeAddress().address === '127.0.0.1')
3133

3234
await ipfs1.swarm.connect(ipfs2Addr)
3335
await ipfs1.swarm.connect(ipfs3Addr)

src/pubsub/subscribe.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ module.exports = (common, options) => {
153153
ipfs2.pubsub.setMaxListeners(100)
154154
}
155155

156-
const ipfs2Addr = ipfs2.peerId.addresses.find((a) => a.includes('127.0.0.1'))
156+
const ipfs2Addr = ipfs2.peerId.addresses
157+
.find(ma => ma.nodeAddress().address === '127.0.0.1')
158+
157159
return ipfs1.swarm.connect(ipfs2Addr)
158160
})
159161

src/swarm/addrs.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ module.exports = (common, options) => {
3232
const peerInfos = await ipfsA.swarm.addrs()
3333
expect(peerInfos).to.not.be.empty()
3434
expect(peerInfos).to.be.an('array')
35-
peerInfos.forEach(m => {
36-
expect(CID.isCID(m.id)).to.be.true()
37-
m.addrs.forEach(addr => expect(Multiaddr.isMultiaddr(addr)).to.be.true())
35+
36+
expect(peerInfos).to.all.satisfy(peerInfo => {
37+
expect(CID.isCID(new CID(peerInfo.id))).to.be.true()
38+
expect(peerInfo).to.have.a.property('addrs').that.is.an('array').and.all.satisfy(ma => Multiaddr.isMultiaddr(ma))
39+
40+
return true
3841
})
3942
})
4043
})

src/swarm/peers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ module.exports = (common, options) => {
4141

4242
expect(peer).to.have.a.property('addr')
4343
expect(multiaddr.isMultiaddr(peer.addr)).to.equal(true)
44-
expect(peer).to.have.a.property('peer')
45-
expect(CID.isCID(peer.peer)).to.equal(true)
44+
expect(peer).to.have.a.property('peer').that.is.a('string')
45+
expect(CID.isCID(new CID(peer.peer))).to.equal(true)
4646
expect(peer).to.not.have.a.property('latency')
4747

4848
/* TODO: These assertions must be uncommented as soon as

src/utils/mocha.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chai = require('chai')
66
// Do not reorder these statements - https://github.com/chaijs/chai/issues/1298
77
chai.use(require('chai-as-promised'))
88
chai.use(require('dirty-chai'))
9+
chai.use(require('chai-things'))
910

1011
module.exports.expect = chai.expect
1112

0 commit comments

Comments
 (0)