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

Commit

Permalink
feat: implement bitswap.wantlist peerid and bitswap.unwant (#1349)
Browse files Browse the repository at this point in the history
* feat(bitswap.unwant) expose bitswap.unwant to cli and http api

* feat(tests) move some bitswap tests to interface

Will require including the version of interface-ipfs-core that
they are moved to

* feat(tests) move some bitswap tests to interface

Will require including the version of interface-ipfs-core that
they are moved to

* feat(bitswap.wantlist) add peer parameter

* fix(test) fix cli tests for both bitswap.unwant and bitswap.wantlist peerId

* fix: pick peer property from querystring, fix tests

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* chore: update interface-ipfs-core dependency

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* fix: don't add newline to cli block.get output

* chore: increase timeout for bitswap cli test setup

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* chore: update interface-ipfs-core dependency

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
wraithgar authored and alanshaw committed Jun 20, 2018
1 parent 2a5cc5e commit 45b705d
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 144 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"expose-loader": "~0.7.5",
"form-data": "^2.3.2",
"hat": "0.0.3",
"interface-ipfs-core": "~0.66.2",
"interface-ipfs-core": "~0.68.2",
"ipfsd-ctl": "~0.37.3",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
Expand Down
7 changes: 1 addition & 6 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
Expand All @@ -17,11 +16,7 @@ module.exports = {
}

stats.wantlist = stats.wantlist || []
stats.wantlist = stats.wantlist.map((entry) => {
const buf = Buffer.from(entry.cid.hash.data)
const cid = new CID(entry.cid.version, entry.cid.codec, buf)
return cid.toBaseEncodedString()
})
stats.wantlist = stats.wantlist.map(entry => entry['/'])
stats.peers = stats.peers || []

print(`bitswap status
Expand Down
18 changes: 16 additions & 2 deletions src/cli/commands/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'unwant <key>',

describe: 'Remove a given block from your wantlist.',
describe: 'Removes a given block from your wantlist.',

builder: {
key: {
alias: 'k',
describe: 'Key to remove from your wantlist',
type: 'string'
}
},
handler (argv) {
throw new Error('Not implemented yet')
argv.ipfs.bitswap.unwant(argv.key, (err) => {
if (err) {
throw err
}
print(`Key ${argv.key} removed from wantlist`)
})
}
}
9 changes: 4 additions & 5 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const print = require('../../utils').print

module.exports = {
command: 'wantlist',
command: 'wantlist [peer]',

describe: 'Print out all blocks currently on the bitswap wantlist for the local peer.',

Expand All @@ -16,13 +16,12 @@ 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
}
res.Keys.forEach((cidStr) => {
print(cidStr)
res.Keys.forEach((cid) => {
print(cid['/'])
})
})
}
Expand Down
7 changes: 6 additions & 1 deletion src/cli/commands/block/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
command: 'get <key>',
Expand All @@ -17,7 +18,11 @@ module.exports = {
throw err
}

process.stdout.write(block.data)
if (block) {
print(block.data, false)
} else {
print('Block was unwanted before it could be remotely retrieved')
}
})
}
}
50 changes: 40 additions & 10 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,42 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
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])
return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() }))
}

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

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

const list = self._bitswap.getWantlist()
return formatWantlist(list)
},
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)
return setImmediate(() => callback(null, { Keys: list }))
}),

stat: promisify((callback) => {
if (!self.isOnline()) {
Expand All @@ -40,12 +61,21 @@ module.exports = function bitswap (self) {
})
}),

unwant: (key) => {
unwant: promisify((keys, callback) => {
if (!self.isOnline()) {
throw new Error(OFFLINE_ERROR)
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

// TODO: implement when https://github.com/ipfs/js-ipfs-bitswap/pull/10 is merged
}
if (!Array.isArray(keys)) {
keys = [keys]
}
keys = keys.map((key) => {
if (CID.isCID(key)) {
return key
}
return new CID(key)
})
return setImmediate(() => callback(null, self._bitswap.unwant(keys)))
})
}
}
78 changes: 43 additions & 35 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,58 @@ const parseKey = require('./block').parseKey

exports = module.exports

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

reply({
Keys: 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 = {
// uses common parseKey method that returns a `key`
// uses common parseKey method that assigns a `key` to request.pre.args
parseArgs: parseKey,

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
reply(boom.badRequest(new Error('Not implemented yet')))
const key = request.pre.args.key
const ipfs = request.server.app.ipfs
ipfs.bitswap.unwant(key, (err) => {
if (err) {
return reply(boom.badRequest(err))
}
reply({ key: key.toBaseEncodedString() })
})
}
}
8 changes: 7 additions & 1 deletion src/http/api/resources/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ exports.get = {
}).code(500)
}

return reply(block.data)
if (block) {
return reply(block.data)
}
return reply({
Message: 'Block was unwanted before it could be remotely retrieved',
Code: 0
}).code(404)
})
}
}
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
27 changes: 23 additions & 4 deletions test/cli/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@

const expect = require('chai').expect
const runOn = require('../utils/on-and-off').on
const PeerId = require('peer-id')

describe('bitswap', () => runOn((thing) => {
let ipfs
let peerId
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'

before((done) => {
before(function (done) {
this.timeout(60 * 1000)
ipfs = thing.ipfs
ipfs('block get ' + key)
.then(() => {})
.catch(() => {})
setTimeout(done, 800)
PeerId.create((err, peer) => {
expect(err).to.not.exist()
peerId = peer.toB58String()
done()
})
})

it('wantlist', function () {
Expand All @@ -23,8 +30,14 @@ describe('bitswap', () => runOn((thing) => {
})
})

// TODO @hacdias fix this with https://github.com/ipfs/js-ipfs/pull/1198
it.skip('stat', function () {
it('wantlist peerid', function () {
this.timeout(20 * 1000)
return ipfs('bitswap wantlist ' + peerId).then((out) => {
expect(out).to.eql('')
})
})

it('stat', function () {
this.timeout(20 * 1000)

return ipfs('bitswap stat').then((out) => {
Expand All @@ -40,4 +53,10 @@ describe('bitswap', () => runOn((thing) => {
].join('\n') + '\n')
})
})

it('unwant', function () {
return ipfs('bitswap unwant ' + key).then((out) => {
expect(out).to.eql(`Key ${key} removed from wantlist\n`)
})
})
}))
Loading

0 comments on commit 45b705d

Please sign in to comment.