Skip to content

Commit a5115e9

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: migrate to new ipfs-block and repo
1 parent 92961f0 commit a5115e9

File tree

24 files changed

+748
-1021
lines changed

24 files changed

+748
-1021
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ node_modules
2929
dist
3030

3131
test/test-repo-for*
32+
docs
33+
34+
test/test-repo/datastore

README.md

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
2424
- [Use in a browser using a script tag](#use-in-a-browser-using-a-script-tag)
2525
- [Usage](#usage)
26+
- [API](#api)
2627
- [Contribute](#contribute)
2728
- [License](#license)
2829

2930
## Install
3031

3132
### npm
3233

33-
```sh
34-
> npm install ipfs-bitswap --save
34+
```bash
35+
> npm install ipfs-bitswap
3536
```
3637

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

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

45-
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.
46-
4746
```js
4847
const Bitswap = require('ipfs-bitswap')
4948
```
@@ -60,84 +59,11 @@ Loading this module through a script tag will make the `IpfsBitswap` object avai
6059

6160
## Usage
6261

63-
For the documentation see [API.md](API.md).
64-
65-
### API
66-
67-
#### `new Bitswap(libp2p, blockstore)`
68-
69-
- `libp2p: Libp2p`, instance of the local network stack.
70-
- `blockstore: Blockstore`, instance of the local database (`IpfsRepo.blockstore`)
71-
72-
Create a new instance.
73-
74-
#### `getStream(cid)`
75-
76-
- `cid: CID|Array`
77-
78-
Returns a source `pull-stream`. Values emitted are the received blocks.
79-
80-
Example:
81-
82-
```js
83-
// Single block
84-
pull(
85-
bitswap.getStream(cid),
86-
pull.collect((err, blocks) => {
87-
// blocks === [block]
88-
})
89-
)
90-
91-
// Many blocks
92-
pull(
93-
bitswap.getStream([cid1, cid2, cid3]),
94-
pull.collect((err, blocks) => {
95-
// blocks === [block1, block2, block3]
96-
})
97-
)
98-
```
99-
100-
> Note: This is safe guarded so that the network is not asked
101-
> for blocks that are in the local `datastore`.
102-
103-
#### `unwant(cids)`
104-
105-
- `cids: CID|[]CID`
106-
107-
Cancel previously requested cids, forcefully. That means they are removed from the
108-
wantlist independent of how many other resources requested these cids. Callbacks
109-
attached to `getBlock` are errored with `Error('manual unwant: cid)`.
110-
111-
#### `cancelWants(cids)`
112-
113-
- `cid: CID|[]CID`
114-
115-
Cancel previously requested cids.
116-
117-
#### `putStream()`
118-
119-
Returns a duplex `pull-stream` that emits an object `{cid: CID}` for every written block when it was stored.
120-
Objects passed into here should be of the form `{data: Buffer, cid: CID}`
121-
122-
#### `put(blockAndCid, callback)`
123-
124-
- `blockAndCid: {data: Buffer, cid: CID}`
125-
- `callback: Function`
126-
127-
Announce that the current node now has the block containing `data`. This will store it
128-
in the local database and attempt to serve it to all peers that are known
129-
to have requested it. The callback is called when we are sure that the block
130-
is stored.
131-
132-
#### `wantlistForPeer(peerId)`
133-
134-
- `peerId: PeerId`
135-
136-
Get the wantlist for a given peer.
62+
See https://ipfs.github.io/js-ipfs-bitswap
13763

138-
#### `stat()`
64+
## API
13965

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

14268
## Development
14369

benchmarks/index.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const mapSeries = require('async/mapSeries')
88
const each = require('async/each')
99
const _ = require('lodash')
1010
const Block = require('ipfs-block')
11-
const pull = require('pull-stream')
1211
const assert = require('assert')
1312
const crypto = require('crypto')
1413
const CID = require('cids')
14+
const multihashing = require('multihashing-async')
1515

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

@@ -50,12 +50,11 @@ function shutdown (nodeArr, cb) {
5050
}
5151

5252
function round (nodeArr, blockFactor, n, cb) {
53-
const blocks = createBlocks(n, blockFactor)
54-
map(blocks, (b, cb) => b.key(cb), (err, keys) => {
53+
createBlocks(n, blockFactor, (err, blocks) => {
5554
if (err) {
5655
return cb(err)
5756
}
58-
const cids = keys.map((k) => new CID(k))
57+
const cids = blocks.map((b) => b.cid)
5958
let d
6059
series([
6160
// put blockFactor amount of blocks per node
@@ -64,10 +63,7 @@ function round (nodeArr, blockFactor, n, cb) {
6463

6564
const data = _.map(_.range(blockFactor), (j) => {
6665
const index = i * blockFactor + j
67-
return {
68-
block: blocks[index],
69-
cid: cids[index]
70-
}
66+
return blocks[index]
7167
})
7268
each(
7369
data,
@@ -81,17 +77,14 @@ function round (nodeArr, blockFactor, n, cb) {
8177
},
8278
// fetch all blocks on every node
8379
(cb) => parallel(_.map(nodeArr, (node, i) => (callback) => {
84-
pull(
85-
node.bitswap.getStream(cids),
86-
pull.collect((err, res) => {
87-
if (err) {
88-
return callback(err)
89-
}
80+
map(cids, (cid, cb) => node.bitswap.get(cid, cb), (err, res) => {
81+
if (err) {
82+
return callback(err)
83+
}
9084

91-
assert(res.length === blocks.length)
92-
callback()
93-
})
94-
)
85+
assert(res.length === blocks.length)
86+
callback()
87+
})
9588
}), cb)
9689
], (err) => {
9790
if (err) {
@@ -103,8 +96,14 @@ function round (nodeArr, blockFactor, n, cb) {
10396
})
10497
}
10598

106-
function createBlocks (n, blockFactor) {
107-
return _.map(_.range(n * blockFactor), () => {
108-
return new Block(crypto.randomBytes(n * blockFactor))
109-
})
99+
function createBlocks (n, blockFactor, callback) {
100+
map(_.range(n * blockFactor), (i, cb) => {
101+
const data = crypto.randomBytes(n * blockFactor)
102+
multihashing(data, 'sha2-256', (err, hash) => {
103+
if (err) {
104+
return cb(err)
105+
}
106+
cb(null, new Block(data, new CID(hash)))
107+
})
108+
}, callback)
110109
}

benchmarks/put-get.js

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
12
'use strict'
23

34
const Benchmark = require('benchmark')
45
const _ = require('lodash')
56
const Block = require('ipfs-block')
67
const assert = require('assert')
7-
const pull = require('pull-stream')
88
const series = require('async/series')
9+
const map = require('async/map')
910
const crypto = require('crypto')
1011
const CID = require('cids')
12+
const multihashing = require('multihashing-async')
1113

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

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

2628
blockCounts.forEach((n) => blockSizes.forEach((k) => {
2729
suite.add(`put-get ${n} blocks of size ${k}`, (defer) => {
28-
const blocks = createBlocks(n, k)
29-
series([
30-
(cb) => put(blocks, bitswap, cb),
31-
(cb) => get(blocks, bitswap, cb)
32-
], (err) => {
30+
createBlocks(n, k, (err, blocks) => {
3331
if (err) {
3432
throw err
3533
}
36-
defer.resolve()
34+
series([
35+
(cb) => bitswap.putMany(blocks, cb),
36+
(cb) => get(blocks, bitswap, cb)
37+
], (err) => {
38+
if (err) {
39+
throw err
40+
}
41+
defer.resolve()
42+
})
3743
})
3844
}, {
3945
defer: true
@@ -52,40 +58,27 @@ utils.genBitswapNetwork(1, (err, nodes) => {
5258
})
5359
})
5460

55-
function createBlocks (n, k) {
56-
return _.map(_.range(n), () => {
57-
return new Block(crypto.randomBytes(k))
58-
})
59-
}
60-
61-
function put (blocks, bs, callback) {
62-
pull(
63-
pull.values(blocks),
64-
pull.asyncMap((b, cb) => {
65-
b.key((err, key) => {
66-
if (err) {
67-
return cb(err)
68-
}
69-
cb(null, {cid: new CID(key), block: b})
70-
})
71-
}),
72-
bs.putStream(),
73-
pull.onEnd(callback)
74-
)
75-
}
76-
77-
function get (blocks, bs, callback) {
78-
pull(
79-
pull.values(blocks),
80-
pull.asyncMap((b, cb) => b.key(cb)),
81-
pull.map((k) => bs.getStream(new CID(k))),
82-
pull.flatten(),
83-
pull.collect((err, res) => {
61+
function createBlocks (n, k, callback) {
62+
map(_.range(n), (i, cb) => {
63+
const data = crypto.randomBytes(k)
64+
multihashing(data, 'sha2-256', (err, hash) => {
8465
if (err) {
85-
return callback(err)
66+
return cb(err)
8667
}
87-
assert(res.length === blocks.length)
88-
callback()
68+
cb(null, new Block(data, new CID(hash)))
8969
})
90-
)
70+
}, callback)
71+
}
72+
73+
function get (blocks, bs, callback) {
74+
map(blocks, (b, cb) => {
75+
bs.get(b.cid, cb)
76+
}, (err, res) => {
77+
if (err) {
78+
return callback(err)
79+
}
80+
81+
assert(res.length === blocks.length)
82+
callback()
83+
})
9184
}

package.json

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
"test:browser": "aegir-test browser",
1212
"test:node": "aegir-test node",
1313
"lint": "aegir-lint",
14-
"release": "aegir-release",
15-
"release-minor": "aegir-release --type minor",
16-
"release-major": "aegir-release --type major",
14+
"release": "aegir-release --docs",
15+
"release-minor": "aegir-release --type minor --docs",
16+
"release-major": "aegir-release --type major --docs",
1717
"bench": "node benchmarks/index",
1818
"build": "aegir-build",
1919
"coverage": "aegir-coverage",
20-
"coverage-publish": "aegir-coverage publish"
20+
"coverage-publish": "aegir-coverage publish",
21+
"docs": "aegir-docs"
2122
},
2223
"repository": {
2324
"type": "git",
@@ -36,42 +37,39 @@
3637
},
3738
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
3839
"devDependencies": {
39-
"aegir": "^10.0.0",
40+
"aegir": "^11.0.0",
4041
"benchmark": "^2.1.3",
41-
"buffer-loader": "0.0.1",
4242
"chai": "^3.5.0",
43-
"fs-pull-blob-store": "~0.4.1",
44-
"idb-pull-blob-store": "~0.5.1",
45-
"interface-pull-blob-store": "~0.6.0",
46-
"ipfs-repo": "~0.11.3",
43+
"dirty-chai": "^1.2.2",
44+
"ipfs-repo": "~0.12.0",
4745
"libp2p-ipfs-nodejs": "~0.19.0",
4846
"lodash": "^4.17.4",
49-
"multiaddr": "^2.2.1",
47+
"multiaddr": "^2.2.2",
5048
"ncp": "^2.0.0",
5149
"peer-book": "~0.3.1",
52-
"peer-id": "~0.8.2",
53-
"peer-info": "~0.8.3",
50+
"peer-id": "~0.8.4",
51+
"peer-info": "~0.8.4",
5452
"rimraf": "^2.6.1",
5553
"safe-buffer": "^5.0.1"
5654
},
5755
"dependencies": {
5856
"async": "^2.1.5",
59-
"cids": "~0.4.1",
60-
"debug": "^2.6.2",
61-
"heap": "^0.2.6",
62-
"ipfs-block": "~0.5.5",
57+
"cids": "~0.4.2",
58+
"debug": "^2.6.3",
59+
"ipfs-block": "~0.6.0",
6360
"lodash.debounce": "^4.0.8",
6461
"lodash.find": "^4.6.0",
6562
"lodash.groupby": "^4.6.0",
6663
"lodash.isequalwith": "^4.4.0",
6764
"lodash.isundefined": "^3.0.1",
6865
"lodash.pullallwith": "^4.7.0",
66+
"lodash.sortby": "^4.7.0",
6967
"lodash.uniqwith": "^4.5.0",
7068
"lodash.values": "^4.3.0",
69+
"multihashing-async": "^0.4.4",
7170
"protocol-buffers": "^3.2.1",
7271
"pull-defer": "^0.2.2",
7372
"pull-length-prefixed": "^1.2.0",
74-
"pull-paramap": "^1.2.1",
7573
"pull-pushable": "^2.0.1",
7674
"pull-stream": "^3.5.0",
7775
"varint-decoder": "^0.1.1"
@@ -87,4 +85,4 @@
8785
"greenkeeperio-bot <[email protected]>",
8886
"npmcdn-to-unpkg-bot <[email protected]>"
8987
]
90-
}
88+
}

0 commit comments

Comments
 (0)