Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5953a59

Browse files
committed
feat(bitswap.wantlist) add peer parameter
1 parent de5a1d9 commit 5953a59

File tree

5 files changed

+59
-34
lines changed

5 files changed

+59
-34
lines changed

src/cli/commands/bitswap/wantlist.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ module.exports = {
1616
},
1717

1818
handler (argv) {
19-
// TODO: handle argv.peer
20-
argv.ipfs.bitswap.wantlist((err, res) => {
19+
argv.ipfs.bitswap.wantlist(argv.peer, (err, res) => {
2120
if (err) {
2221
throw err
2322
}

src/core/components/bitswap.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,40 @@ const promisify = require('promisify-es6')
55
const setImmediate = require('async/setImmediate')
66
const Big = require('big.js')
77
const CID = require('cids')
8+
const PeerId = require('peer-id')
89

910
function formatWantlist (list) {
1011
return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() }))
1112
}
1213

1314
module.exports = function bitswap (self) {
1415
return {
15-
wantlist: promisify((callback) => {
16+
wantlist: promisify((peerId, callback) => {
17+
if (!callback) {
18+
callback = peerId
19+
peerId = undefined
20+
}
21+
1622
if (!self.isOnline()) {
1723
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
1824
}
1925

20-
let list = self._bitswap.getWantlist()
26+
let list
27+
if (peerId) {
28+
try {
29+
peerId = PeerId.createFromB58String(peerId)
30+
} catch (e) {
31+
peerId = null
32+
}
33+
if (!peerId) {
34+
return setImmediate(() => callback(new Error('Invalid peerId')))
35+
}
36+
list = self._bitswap.wantlistForPeer(peerId)
37+
} else {
38+
list = self._bitswap.getWantlist()
39+
}
2140
list = formatWantlist(list)
22-
callback(null, { Keys: list })
41+
return setImmediate(() => callback(null, { Keys: list }))
2342
}),
2443

2544
stat: promisify((callback) => {
@@ -56,7 +75,7 @@ module.exports = function bitswap (self) {
5675
}
5776
return new CID(key)
5877
})
59-
callback(null, self._bitswap.unwant(keys))
78+
return setImmediate(() => callback(null, self._bitswap.unwant(keys)))
6079
})
6180
}
6281
}

src/http/api/resources/bitswap.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,48 @@ const parseKey = require('./block').parseKey
66

77
exports = module.exports
88

9-
exports.wantlist = (request, reply) => {
10-
request.server.app.ipfs.bitswap.wantlist((err, list) => {
11-
if (err) {
9+
exports.wantlist = {
10+
handler: (request, reply) => {
11+
const peerId = request.query.arg
12+
let list
13+
try {
14+
list = request.server.app.ipfs.bitswap.wantlist(peerId)
15+
list = list.map((entry) => entry.cid.toBaseEncodedString())
16+
} catch (err) {
1217
return reply(boom.badRequest(err))
1318
}
14-
list = list.map((entry) => entry.cid.toBaseEncodedString())
19+
1520
reply({
1621
Keys: list
1722
})
18-
})
23+
}
1924
}
2025

21-
exports.stat = (request, reply) => {
22-
const ipfs = request.server.app.ipfs
26+
exports.stat = {
27+
handler: (request, reply) => {
28+
const ipfs = request.server.app.ipfs
2329

24-
ipfs.bitswap.stat((err, stats) => {
25-
if (err) {
26-
return reply({
27-
Message: err.toString(),
28-
Code: 0
29-
}).code(500)
30-
}
30+
ipfs.bitswap.stat((err, stats) => {
31+
if (err) {
32+
return reply({
33+
Message: err.toString(),
34+
Code: 0
35+
}).code(500)
36+
}
3137

32-
reply({
33-
ProvideBufLen: stats.provideBufLen,
34-
BlocksReceived: stats.blocksReceived,
35-
Wantlist: stats.wantlist,
36-
Peers: stats.peers,
37-
DupBlksReceived: stats.dupBlksReceived,
38-
DupDataReceived: stats.dupDataReceived,
39-
DataReceived: stats.dataReceived,
40-
BlocksSent: stats.blocksSent,
41-
DataSent: stats.dataSent
38+
reply({
39+
ProvideBufLen: stats.provideBufLen,
40+
BlocksReceived: stats.blocksReceived,
41+
Wantlist: stats.wantlist,
42+
Peers: stats.peers,
43+
DupBlksReceived: stats.dupBlksReceived,
44+
DupDataReceived: stats.dupDataReceived,
45+
DataReceived: stats.dataReceived,
46+
BlocksSent: stats.blocksSent,
47+
DataSent: stats.dataSent
48+
})
4249
})
43-
})
50+
}
4451
}
4552

4653
exports.unwant = {

src/http/api/routes/bitswap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ module.exports = (server) => {
99
method: '*',
1010
path: '/api/v0/bitswap/wantlist',
1111
config: {
12-
handler: resources.bitswap.wantlist
12+
handler: resources.bitswap.wantlist.handler
1313
}
1414
})
1515

1616
api.route({
1717
method: '*',
1818
path: '/api/v0/bitswap/stat',
1919
config: {
20-
handler: resources.bitswap.stat
20+
handler: resources.bitswap.stat.handler
2121
}
2222
})
2323

src/http/api/routes/stats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = (server) => {
99
method: '*',
1010
path: '/api/v0/stats/bitswap',
1111
config: {
12-
handler: resources.stats.bitswap
12+
handler: resources.stats.bitswap.handler
1313
}
1414
})
1515

0 commit comments

Comments
 (0)