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

Commit

Permalink
feat(bitswap.wantlist) add peer parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jun 8, 2018
1 parent de5a1d9 commit 371bb56
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ module.exports = {
},

handler (argv) {
// TODO: handle argv.peer
argv.ipfs.bitswap.wantlist((err, res) => {
argv.ipfs.bitswap.wantlist(argv.peer, (err, res) => {
if (err) {
throw err
}
Expand Down
27 changes: 23 additions & 4 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@ const promisify = require('promisify-es6')
const setImmediate = require('async/setImmediate')
const Big = require('big.js')
const CID = require('cids')
const PeerId = require('peer-id')

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

module.exports = function bitswap (self) {
return {
wantlist: promisify((callback) => {
wantlist: promisify((peerId, callback) => {
if (!callback) {
callback = peerId
peerId = undefined
}

if (!self.isOnline()) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

let list = self._bitswap.getWantlist()
let list
if (peerId) {
try {
peerId = PeerId.createFromB58String(peerId)
} catch (e) {
peerId = null
}
if (!peerId) {
return setImmediate(() => callback(new Error('Invalid peerId')))
}
list = self._bitswap.wantlistForPeer(peerId)
} else {
list = self._bitswap.getWantlist()
}
list = formatWantlist(list)
callback(null, { Keys: list })
return setImmediate(() => callback(null, { Keys: list }))
}),

stat: promisify((callback) => {
Expand Down Expand Up @@ -56,7 +75,7 @@ module.exports = function bitswap (self) {
}
return new CID(key)
})
callback(null, self._bitswap.unwant(keys))
return setImmediate(() => callback(null, self._bitswap.unwant(keys)))
})
}
}
65 changes: 34 additions & 31 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,44 @@ const parseKey = require('./block').parseKey

exports = module.exports

exports.wantlist = (request, reply) => {
request.server.app.ipfs.bitswap.wantlist((err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
list = list.map((entry) => entry.cid.toBaseEncodedString())
reply({
Keys: list
exports.wantlist = {
handler: (request, reply) => {
const peerId = request.query.arg
let list
request.server.app.ipfs.bitswap.wantlist(peerId, (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
reply(list)
})
})
}
}

exports.stat = (request, reply) => {
const ipfs = request.server.app.ipfs

ipfs.bitswap.stat((err, stats) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

reply({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
exports.stat = {
handler: (request, reply) => {
const ipfs = request.server.app.ipfs

ipfs.bitswap.stat((err, stats) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

reply({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
})
})
})
}
}

exports.unwant = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/api/routes/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module.exports = (server) => {
method: '*',
path: '/api/v0/bitswap/wantlist',
config: {
handler: resources.bitswap.wantlist
handler: resources.bitswap.wantlist.handler
}
})

api.route({
method: '*',
path: '/api/v0/bitswap/stat',
config: {
handler: resources.bitswap.stat
handler: resources.bitswap.stat.handler
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/http/api/routes/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = (server) => {
method: '*',
path: '/api/v0/stats/bitswap',
config: {
handler: resources.stats.bitswap
handler: resources.stats.bitswap.handler
}
})

Expand Down

0 comments on commit 371bb56

Please sign in to comment.