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

Commit bc6d3d1

Browse files
authored
Merge pull request #267 from ipfs/bitswap_wantlist_peerid
test: Bitswap wantlist peerid
2 parents b9e5a21 + fcc834c commit bc6d3d1

File tree

3 files changed

+209
-4
lines changed

3 files changed

+209
-4
lines changed

SPEC/BITSWAP.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,58 @@
44
* [bitswap.unwant](#bitswapunwant)
55
* [bitswap.stat](#bitswapstat)
66

7-
#### `bitswap.wantlist`
7+
#### `bitswap.unwant`
88

9-
(not spec'ed yet)
9+
> Removes a given block from your wantlist
1010
11-
#### `bitswap.unwant`
11+
##### `Go` **WIP**
12+
13+
##### `JavaScript` - ipfs.bitswap.unwant(cid, [callback])
14+
15+
`cid` is a [cid][cid] which can be passed as:
16+
17+
- CID, a CID instance
18+
- String, the base58 encoded version of the multihash
19+
20+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
21+
22+
**Example:**
23+
24+
```JavaScript
25+
ipfs.bitswap.unwant('QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', (err) => {
26+
if (err) throw err
27+
console.log('Done')
28+
})
29+
```
30+
31+
##### `Go` **WIP**
32+
33+
### `bitswap.wantlist`
34+
35+
> Returns the wantlist, optionally filtered by peer ID
1236
13-
(not spec'ed yet)
37+
#### `Go` **WIP**
38+
39+
#### `JavaScript` - ipfs.bitswap.wantlist([peerId], [callback])
40+
41+
`callback` must follow `function (err, list) {}` signature, where `err` is an error if the operation was not successful. `list` is an Object containing the following keys:
42+
43+
- `Keys` An array of objects containing the following keys:
44+
- `/` A string multihash
45+
46+
If no `callback` is passed, a promise is returned.
47+
48+
**Example:**
49+
50+
```JavaScript
51+
ipfs.bitswap.wantlist((err, list) => console.log(list))
52+
53+
// { Keys: [{ '/': 'QmHash' }] }
54+
55+
ipfs.bitswap.wantlist(peerId, (err, list) => console.log(list))
56+
57+
// { Keys: [{ '/': 'QmHash' }] }
58+
```
1459

1560
#### `bitswap.stat`
1661

js/src/bitswap.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const series = require('async/series')
7+
const expect = chai.expect
8+
const statsTests = require('./utils/stats')
9+
const spawn = require('./utils/spawn')
10+
chai.use(dirtyChai)
11+
const CID = require('cids')
12+
13+
module.exports = (common) => {
14+
describe('.bitswap online', () => {
15+
let ipfsA
16+
let ipfsB
17+
let withGo
18+
let ipfsBId
19+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
20+
21+
before(function (done) {
22+
// CI takes longer to instantiate the daemon, so we need to increase the
23+
// timeout for the before step
24+
this.timeout(60 * 250)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
series([
29+
(cb) => spawn.spawnNodeWithId(factory, (err, node) => {
30+
expect(err).to.not.exist()
31+
ipfsA = node
32+
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
33+
cb()
34+
}),
35+
(cb) => spawn.spawnNodeWithId(factory, (err, node) => {
36+
expect(err).to.not.exist()
37+
ipfsB = node
38+
ipfsBId = node.peerId
39+
ipfsB.block.get(new CID(key))
40+
.then(() => {})
41+
.catch(() => {})
42+
ipfsA.swarm.connect(ipfsBId.addresses[0], (err) => {
43+
expect(err).to.not.exist()
44+
setTimeout(cb, 350)
45+
})
46+
})
47+
], done)
48+
})
49+
})
50+
51+
after((done) => common.teardown(done))
52+
53+
it('.stat', (done) => {
54+
ipfsB.bitswap.stat((err, stats) => {
55+
expect(err).to.not.exist()
56+
statsTests.expectIsBitswap(err, stats)
57+
done()
58+
})
59+
})
60+
61+
it('.wantlist', (done) => {
62+
ipfsB.bitswap.wantlist((err, list) => {
63+
expect(err).to.not.exist()
64+
expect(list.Keys).to.have.length(1)
65+
expect(list.Keys[0]['/']).to.equal(key)
66+
done()
67+
})
68+
})
69+
70+
it('.wantlist peerid', (done) => {
71+
ipfsA.bitswap.wantlist(ipfsBId.id, (err, list) => {
72+
expect(err).to.not.exist()
73+
expect(list.Keys[0]['/']).to.equal(key)
74+
done()
75+
})
76+
})
77+
78+
it('.unwant', function (done) {
79+
if (withGo) {
80+
this.skip()
81+
}
82+
ipfsB.bitswap.unwant(key, (err) => {
83+
expect(err).to.not.exist()
84+
ipfsB.bitswap.wantlist((err, list) => {
85+
expect(err).to.not.exist()
86+
expect(list.Keys).to.be.empty()
87+
done()
88+
})
89+
})
90+
})
91+
})
92+
93+
describe('.bitswap offline', () => {
94+
let ipfs
95+
96+
before(function (done) {
97+
// CI takes longer to instantiate the daemon, so we need to increase the
98+
// timeout for the before step
99+
this.timeout(60 * 1000)
100+
101+
common.setup((err, factory) => {
102+
expect(err).to.not.exist()
103+
factory.spawnNode((err, node) => {
104+
expect(err).to.not.exist()
105+
ipfs = node
106+
ipfs.id((err, id) => {
107+
expect(err).to.not.exist()
108+
ipfs.stop((err) => {
109+
// TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078
110+
if (!id.agentVersion.startsWith('go-ipfs')) {
111+
expect(err).to.not.exist()
112+
}
113+
done()
114+
})
115+
})
116+
})
117+
})
118+
})
119+
120+
it('.stat gives error while offline', (done) => {
121+
ipfs.bitswap.stat((err, stats) => {
122+
expect(err).to.exist()
123+
// When run against core we get our expected error, when run
124+
// as part of the http tests we get a connection refused
125+
if (err.code !== 'ECONNREFUSED') {
126+
expect(err).to.match(/online mode/)
127+
}
128+
expect(stats).to.not.exist()
129+
done()
130+
})
131+
})
132+
133+
it('.wantlist gives error if offline', (done) => {
134+
ipfs.bitswap.wantlist((err, list) => {
135+
expect(err).to.exist()
136+
// When run against core we get our expected error, when run
137+
// as part of the http tests we get a connection refused
138+
if (err.code !== 'ECONNREFUSED') {
139+
expect(err).to.match(/online mode/)
140+
}
141+
expect(list).to.not.exist()
142+
done()
143+
})
144+
})
145+
146+
it('.unwant gives error if offline', (done) => {
147+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
148+
ipfs.bitswap.unwant(key, (err) => {
149+
expect(err).to.exist()
150+
// When run against core we get our expected error, when run
151+
// as part of the http tests we get a connection refused
152+
if (err.code !== 'ECONNREFUSED') {
153+
expect(err).to.match(/online mode/)
154+
}
155+
done()
156+
})
157+
})
158+
})
159+
}

js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ exports.repo = require('./repo')
1919
exports.bootstrap = require('./bootstrap')
2020
exports.types = require('./types')
2121
exports.util = require('./util')
22+
exports.bitswap = require('./bitswap')

0 commit comments

Comments
 (0)