Skip to content

Upgrade to new datastore and repo interface #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ node_modules
dist

test/test-repo-for*
docs

test/test-repo/datastore
86 changes: 6 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
- [Use in a browser using a script tag](#use-in-a-browser-using-a-script-tag)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)

## Install

### npm

```sh
> npm install ipfs-bitswap --save
```bash
> npm install ipfs-bitswap
```

### Use in Node.js
Expand All @@ -42,8 +43,6 @@ const Bitswap = require('ipfs-bitswap')

### Use in a browser with browserify, webpack or any other bundler

The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

```js
const Bitswap = require('ipfs-bitswap')
```
Expand All @@ -60,84 +59,11 @@ Loading this module through a script tag will make the `IpfsBitswap` object avai

## Usage

For the documentation see [API.md](API.md).

### API

#### `new Bitswap(libp2p, blockstore)`

- `libp2p: Libp2p`, instance of the local network stack.
- `blockstore: Blockstore`, instance of the local database (`IpfsRepo.blockstore`)

Create a new instance.

#### `getStream(cid)`

- `cid: CID|Array`

Returns a source `pull-stream`. Values emitted are the received blocks.

Example:

```js
// Single block
pull(
bitswap.getStream(cid),
pull.collect((err, blocks) => {
// blocks === [block]
})
)

// Many blocks
pull(
bitswap.getStream([cid1, cid2, cid3]),
pull.collect((err, blocks) => {
// blocks === [block1, block2, block3]
})
)
```

> Note: This is safe guarded so that the network is not asked
> for blocks that are in the local `datastore`.

#### `unwant(cids)`

- `cids: CID|[]CID`

Cancel previously requested cids, forcefully. That means they are removed from the
wantlist independent of how many other resources requested these cids. Callbacks
attached to `getBlock` are errored with `Error('manual unwant: cid)`.

#### `cancelWants(cids)`

- `cid: CID|[]CID`

Cancel previously requested cids.

#### `putStream()`

Returns a duplex `pull-stream` that emits an object `{cid: CID}` for every written block when it was stored.
Objects passed into here should be of the form `{data: Buffer, cid: CID}`

#### `put(blockAndCid, callback)`

- `blockAndCid: {data: Buffer, cid: CID}`
- `callback: Function`

Announce that the current node now has the block containing `data`. This will store it
in the local database and attempt to serve it to all peers that are known
to have requested it. The callback is called when we are sure that the block
is stored.

#### `wantlistForPeer(peerId)`

- `peerId: PeerId`

Get the wantlist for a given peer.
See https://ipfs.github.io/js-ipfs-bitswap

#### `stat()`
## API

Get stats about about the current state of the bitswap instance.
See https://ipfs.github.io/js-ipfs-bitswap

## Development

Expand Down
43 changes: 21 additions & 22 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const mapSeries = require('async/mapSeries')
const each = require('async/each')
const _ = require('lodash')
const Block = require('ipfs-block')
const pull = require('pull-stream')
const assert = require('assert')
const crypto = require('crypto')
const CID = require('cids')
const multihashing = require('multihashing-async')

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

Expand Down Expand Up @@ -50,12 +50,11 @@ function shutdown (nodeArr, cb) {
}

function round (nodeArr, blockFactor, n, cb) {
const blocks = createBlocks(n, blockFactor)
map(blocks, (b, cb) => b.key(cb), (err, keys) => {
createBlocks(n, blockFactor, (err, blocks) => {
if (err) {
return cb(err)
}
const cids = keys.map((k) => new CID(k))
const cids = blocks.map((b) => b.cid)
let d
series([
// put blockFactor amount of blocks per node
Expand All @@ -64,10 +63,7 @@ function round (nodeArr, blockFactor, n, cb) {

const data = _.map(_.range(blockFactor), (j) => {
const index = i * blockFactor + j
return {
block: blocks[index],
cid: cids[index]
}
return blocks[index]
})
each(
data,
Expand All @@ -81,17 +77,14 @@ function round (nodeArr, blockFactor, n, cb) {
},
// fetch all blocks on every node
(cb) => parallel(_.map(nodeArr, (node, i) => (callback) => {
pull(
node.bitswap.getStream(cids),
pull.collect((err, res) => {
if (err) {
return callback(err)
}
map(cids, (cid, cb) => node.bitswap.get(cid, cb), (err, res) => {
if (err) {
return callback(err)
}

assert(res.length === blocks.length)
callback()
})
)
assert(res.length === blocks.length)
callback()
})
}), cb)
], (err) => {
if (err) {
Expand All @@ -103,8 +96,14 @@ function round (nodeArr, blockFactor, n, cb) {
})
}

function createBlocks (n, blockFactor) {
return _.map(_.range(n * blockFactor), () => {
return new Block(crypto.randomBytes(n * blockFactor))
})
function createBlocks (n, blockFactor, callback) {
map(_.range(n * blockFactor), (i, cb) => {
const data = crypto.randomBytes(n * blockFactor)
multihashing(data, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
}
cb(null, new Block(data, new CID(hash)))
})
}, callback)
}
73 changes: 33 additions & 40 deletions benchmarks/put-get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const Benchmark = require('benchmark')
const _ = require('lodash')
const Block = require('ipfs-block')
const assert = require('assert')
const pull = require('pull-stream')
const series = require('async/series')
const map = require('async/map')
const crypto = require('crypto')
const CID = require('cids')
const multihashing = require('multihashing-async')

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

Expand All @@ -25,15 +27,19 @@ utils.genBitswapNetwork(1, (err, nodes) => {

blockCounts.forEach((n) => blockSizes.forEach((k) => {
suite.add(`put-get ${n} blocks of size ${k}`, (defer) => {
const blocks = createBlocks(n, k)
series([
(cb) => put(blocks, bitswap, cb),
(cb) => get(blocks, bitswap, cb)
], (err) => {
createBlocks(n, k, (err, blocks) => {
if (err) {
throw err
}
defer.resolve()
series([
(cb) => bitswap.putMany(blocks, cb),
(cb) => get(blocks, bitswap, cb)
], (err) => {
if (err) {
throw err
}
defer.resolve()
})
})
}, {
defer: true
Expand All @@ -52,40 +58,27 @@ utils.genBitswapNetwork(1, (err, nodes) => {
})
})

function createBlocks (n, k) {
return _.map(_.range(n), () => {
return new Block(crypto.randomBytes(k))
})
}

function put (blocks, bs, callback) {
pull(
pull.values(blocks),
pull.asyncMap((b, cb) => {
b.key((err, key) => {
if (err) {
return cb(err)
}
cb(null, {cid: new CID(key), block: b})
})
}),
bs.putStream(),
pull.onEnd(callback)
)
}

function get (blocks, bs, callback) {
pull(
pull.values(blocks),
pull.asyncMap((b, cb) => b.key(cb)),
pull.map((k) => bs.getStream(new CID(k))),
pull.flatten(),
pull.collect((err, res) => {
function createBlocks (n, k, callback) {
map(_.range(n), (i, cb) => {
const data = crypto.randomBytes(k)
multihashing(data, 'sha2-256', (err, hash) => {
if (err) {
return callback(err)
return cb(err)
}
assert(res.length === blocks.length)
callback()
cb(null, new Block(data, new CID(hash)))
})
)
}, callback)
}

function get (blocks, bs, callback) {
map(blocks, (b, cb) => {
bs.get(b.cid, cb)
}, (err, res) => {
if (err) {
return callback(err)
}

assert(res.length === blocks.length)
callback()
})
}
36 changes: 17 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"test:browser": "aegir-test browser",
"test:node": "aegir-test node",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"bench": "node benchmarks/index",
"build": "aegir-build",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
"coverage-publish": "aegir-coverage publish",
"docs": "aegir-docs"
},
"repository": {
"type": "git",
Expand All @@ -36,42 +37,39 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
"devDependencies": {
"aegir": "^10.0.0",
"aegir": "^11.0.0",
"benchmark": "^2.1.3",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"fs-pull-blob-store": "~0.4.1",
"idb-pull-blob-store": "~0.5.1",
"interface-pull-blob-store": "~0.6.0",
"ipfs-repo": "~0.11.3",
"dirty-chai": "^1.2.2",
"ipfs-repo": "~0.12.0",
"libp2p-ipfs-nodejs": "~0.19.0",
"lodash": "^4.17.4",
"multiaddr": "^2.2.1",
"multiaddr": "^2.2.2",
"ncp": "^2.0.0",
"peer-book": "~0.3.1",
"peer-id": "~0.8.2",
"peer-info": "~0.8.3",
"peer-id": "~0.8.4",
"peer-info": "~0.8.4",
"rimraf": "^2.6.1",
"safe-buffer": "^5.0.1"
},
"dependencies": {
"async": "^2.1.5",
"cids": "~0.4.1",
"debug": "^2.6.2",
"heap": "^0.2.6",
"ipfs-block": "~0.5.5",
"cids": "~0.4.2",
"debug": "^2.6.3",
"ipfs-block": "~0.6.0",
"lodash.debounce": "^4.0.8",
"lodash.find": "^4.6.0",
"lodash.groupby": "^4.6.0",
"lodash.isequalwith": "^4.4.0",
"lodash.isundefined": "^3.0.1",
"lodash.pullallwith": "^4.7.0",
"lodash.sortby": "^4.7.0",
"lodash.uniqwith": "^4.5.0",
"lodash.values": "^4.3.0",
"multihashing-async": "^0.4.4",
"protocol-buffers": "^3.2.1",
"pull-defer": "^0.2.2",
"pull-length-prefixed": "^1.2.0",
"pull-paramap": "^1.2.1",
"pull-pushable": "^2.0.1",
"pull-stream": "^3.5.0",
"varint-decoder": "^0.1.1"
Expand All @@ -87,4 +85,4 @@
"greenkeeperio-bot <[email protected]>",
"npmcdn-to-unpkg-bot <[email protected]>"
]
}
}
Loading