Skip to content

Commit 66e4467

Browse files
Merge pull request #41 from ipfs/webcrypto
Async Crypto Endeavour
2 parents 2fcc538 + 540af9f commit 66e4467

24 files changed

+1246
-813
lines changed

.aegir.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ build/Release
2727
node_modules
2828

2929
dist
30-
lib
3130

32-
test/test-repo-for*
31+
test/test-repo-for*

.travis.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
sudo: false
22
language: node_js
3-
node_js:
4-
- 4
5-
- stable
3+
matrix:
4+
include:
5+
- node_js: 4
6+
env: CXX=g++-4.8
7+
- node_js: 6
8+
env:
9+
- SAUCE=true
10+
- CXX=g++-4.8
11+
- node_js: stable
12+
env: CXX=g++-4.8
613

714
# Make sure we have new NPM.
815
before_install:
@@ -13,12 +20,17 @@ script:
1320
- npm test
1421
- npm run coverage
1522

16-
addons:
17-
firefox: 'latest'
18-
1923
before_script:
2024
- export DISPLAY=:99.0
2125
- sh -e /etc/init.d/xvfb start
2226

2327
after_success:
2428
- npm run coverage-publish
29+
30+
addons:
31+
firefox: 'latest'
32+
apt:
33+
sources:
34+
- ubuntu-toolchain-r-test
35+
packages:
36+
- g++-4.8

API.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ Cancel previously requested keys.
5959
### `putStream()`
6060

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

63-
### `put(block, cb)`
64+
### `put(blockAndKey, cb)`
6465

65-
- `block: IpfsBlock`
66+
- `blockAndKey: {data: Buffer, key: Multihash}`
6667
- `cb: Function`
6768

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

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
[![Travis CI](https://travis-ci.org/ipfs/js-ipfs-bitswap.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfs-bitswap)
99
[![Circle CI](https://circleci.com/gh/ipfs/js-ipfs-bitswap.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfs-bitswap)
1010
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs-bitswap.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-bitswap) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
11+
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
12+
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
13+
14+
[![Sauce Test Status](https://saucelabs.com/browser-matrix/js-ipfs-bitswap.svg)](https://saucelabs.com/u/js-ipfs-bitswap)
1115

1216
> Node.js implementation of the Bitswap 'data exchange' protocol used by IPFS
1317

package.json

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"name": "ipfs-bitswap",
33
"version": "0.7.1",
44
"description": "Node.js implementation of the Bitswap data exchange protocol used by IPFS",
5-
"main": "lib/index.js",
6-
"jsnext:main": "src/index.js",
5+
"main": "src/index.js",
6+
"browser": {
7+
"libp2p-ipfs": false
8+
},
79
"scripts": {
810
"test": "aegir-test",
911
"test:browser": "aegir-test browser",
@@ -33,38 +35,39 @@
3335
},
3436
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
3537
"devDependencies": {
36-
"aegir": "^8.0.1",
38+
"aegir": "^9.1.1",
3739
"buffer-loader": "0.0.1",
3840
"chai": "^3.5.0",
3941
"fs-pull-blob-store": "^0.4.1",
40-
"idb-pull-blob-store": "^0.4.0",
41-
"interface-pull-blob-store": "^0.5.0",
42-
"ipfs-repo": "^0.9.0",
43-
"libp2p-ipfs": "^0.14.1",
44-
"lodash": "^4.16.2",
42+
"idb-pull-blob-store": "^0.5.1",
43+
"interface-pull-blob-store": "^0.6.0",
44+
"ipfs-repo": "^0.11.1",
45+
"libp2p-ipfs": "^0.15.0",
46+
"lodash": "^4.16.6",
4547
"multiaddr": "^2.0.3",
4648
"ncp": "^2.0.0",
4749
"peer-book": "^0.3.0",
48-
"peer-id": "^0.7.0",
49-
"peer-info": "^0.7.1",
50+
"peer-id": "^0.8.0",
51+
"peer-info": "^0.8.0",
5052
"rimraf": "^2.5.4",
5153
"safe-buffer": "^5.0.1"
5254
},
5355
"dependencies": {
54-
"async": "^2.1.0",
55-
"debug": "^2.3.1",
56+
"async": "^2.1.2",
57+
"cids": "^0.2.0",
58+
"debug": "^2.3.2",
5659
"heap": "^0.2.6",
57-
"ipfs-block": "^0.3.0",
60+
"ipfs-block": "^0.5.0",
61+
"lodash.debounce": "^4.0.8",
5862
"lodash.isequalwith": "^4.4.0",
5963
"lodash.isundefined": "^3.0.1",
6064
"multihashes": "^0.2.2",
61-
"protocol-buffers": "^3.1.6",
65+
"protocol-buffers": "^3.1.8",
6266
"pull-defer": "^0.2.2",
63-
"pull-generate": "^2.2.0",
6467
"pull-length-prefixed": "^1.2.0",
65-
"pull-paramap": "^1.1.6",
68+
"pull-paramap": "^1.2.0",
6669
"pull-pushable": "^2.0.1",
67-
"pull-stream": "^3.4.5"
70+
"pull-stream": "^3.5.0"
6871
},
6972
"contributors": [
7073
"David Dias <[email protected]>",
@@ -74,4 +77,4 @@
7477
"greenkeeperio-bot <[email protected]>",
7578
"npmcdn-to-unpkg-bot <[email protected]>"
7679
]
77-
}
80+
}

src/decision/engine.js

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
const debug = require('debug')
44
const mh = require('multihashes')
55
const pull = require('pull-stream')
6-
const generate = require('pull-generate')
6+
const whilst = require('async/whilst')
7+
const setImmediate = require('async/setImmediate')
8+
const each = require('async/each')
9+
const debounce = require('lodash.debounce')
710

811
const log = debug('bitswap:engine')
912
log.error = debug('bitswap:engine:error')
@@ -26,37 +29,45 @@ module.exports = class Engine {
2629
this.peerRequestQueue = new PeerRequestQueue()
2730

2831
this._running = false
32+
33+
this._outbox = debounce(this._outboxExec.bind(this), 100)
2934
}
3035

3136
_sendBlock (env, cb) {
3237
const msg = new Message(false)
33-
msg.addBlock(env.block)
34-
35-
log('Sending block to %s', env.peer.toB58String(), env.block.data.toString())
36-
37-
this.network.sendMessage(env.peer, msg, (err) => {
38+
msg.addBlock(env.block, (err) => {
3839
if (err) {
39-
log('sendblock error: %s', err.message)
40+
return cb(err)
4041
}
41-
cb(null, 'done')
42+
43+
log('Sending block to %s', env.peer.toB58String(), env.block.data.toString())
44+
45+
this.network.sendMessage(env.peer, msg, (err) => {
46+
if (err) {
47+
log('sendblock error: %s', err.message)
48+
}
49+
cb(null, 'done')
50+
})
4251
})
4352
}
4453

45-
_outbox () {
46-
if (!this._running) return
54+
_outboxExec () {
55+
let nextTask
56+
log('outbox')
4757

48-
const doIt = (cb) => pull(
49-
generate(null, (state, cb) => {
50-
log('generating', this._running)
58+
whilst(
59+
() => {
5160
if (!this._running) {
52-
return cb(true)
61+
return
5362
}
5463

55-
const nextTask = this.peerRequestQueue.pop()
64+
nextTask = this.peerRequestQueue.pop()
65+
log('check', this._running && nextTask)
66+
return Boolean(nextTask)
67+
},
68+
(next) => {
69+
log('generating')
5670
log('got task', nextTask)
57-
if (!nextTask) {
58-
return cb(true)
59-
}
6071

6172
pull(
6273
this.blockstore.getStream(nextTask.entry.key),
@@ -65,31 +76,20 @@ module.exports = class Engine {
6576
const block = blocks[0]
6677
if (err || !block) {
6778
nextTask.done()
68-
return cb(null, false)
79+
return next()
6980
}
7081

71-
cb(null, {
82+
this._sendBlock({
7283
peer: nextTask.target,
7384
block: block,
74-
sent: () => {
85+
sent () {
7586
nextTask.done()
7687
}
77-
})
88+
}, next)
7889
})
7990
)
80-
}),
81-
pull.filter(Boolean),
82-
pull.asyncMap(this._sendBlock.bind(this)),
83-
pull.onEnd(cb)
91+
}
8492
)
85-
86-
if (!this._timer) {
87-
this._timer = setTimeout(() => {
88-
doIt(() => {
89-
this._timer = null
90-
})
91-
}, 50)
92-
}
9393
}
9494

9595
wantlistForPeer (peerId) {
@@ -118,20 +118,25 @@ module.exports = class Engine {
118118
ledger.wantlist = new Wantlist()
119119
}
120120

121-
this._processBlocks(msg.blocks, ledger)
122-
log('wantlist', Array.from(msg.wantlist.values()).map((e) => e.toString()))
123-
124-
pull(
125-
pull.values(Array.from(msg.wantlist.values())),
126-
pull.asyncMap((entry, cb) => {
127-
this._processWantlist(ledger, peerId, entry, cb)
128-
}),
129-
pull.onEnd((err) => {
130-
if (err) return cb(err)
131-
this._outbox()
132-
cb()
133-
})
134-
)
121+
this._processBlocks(msg.blocks, ledger, (err) => {
122+
if (err) {
123+
log.error(`failed to process blocks: ${err.message}`)
124+
}
125+
126+
log('wantlist', Array.from(msg.wantlist.values()).map((e) => e.toString()))
127+
128+
pull(
129+
pull.values(Array.from(msg.wantlist.values())),
130+
pull.asyncMap((entry, cb) => {
131+
this._processWantlist(ledger, peerId, entry, cb)
132+
}),
133+
pull.onEnd((err) => {
134+
if (err) return cb(err)
135+
this._outbox()
136+
cb()
137+
})
138+
)
139+
})
135140
}
136141

137142
receivedBlock (key) {
@@ -173,23 +178,36 @@ module.exports = class Engine {
173178
}
174179
}
175180

176-
_processBlocks (blocks, ledger) {
177-
for (let block of blocks.values()) {
178-
log('got block %s (%s bytes)', mh.toB58String(block.key), block.data.length)
179-
ledger.receivedBytes(block.data.length)
181+
_processBlocks (blocks, ledger, callback) {
182+
each(blocks.values(), (block, cb) => {
183+
block.key((err, key) => {
184+
if (err) {
185+
return cb(err)
186+
}
187+
log('got block %s (%s bytes)', mh.toB58String(key), block.data.length)
188+
ledger.receivedBytes(block.data.length)
180189

181-
this.receivedBlock(block.key)
182-
}
190+
this.receivedBlock(key)
191+
cb()
192+
})
193+
}, callback)
183194
}
184195

185196
// Clear up all accounting things after message was sent
186-
messageSent (peerId, msg) {
197+
messageSent (peerId, msg, callback) {
187198
const ledger = this._findOrCreate(peerId)
188-
for (let block of msg.blocks.values()) {
199+
each(msg.blocks.values(), (block, cb) => {
189200
ledger.sentBytes(block.data.length)
190-
ledger.wantlist.remove(block.key)
191-
this.peerRequestQueue.remove(block.key, peerId)
192-
}
201+
block.key((err, key) => {
202+
if (err) {
203+
return cb(err)
204+
}
205+
206+
ledger.wantlist.remove(key)
207+
this.peerRequestQueue.remove(key, peerId)
208+
cb()
209+
})
210+
}, callback)
193211
}
194212

195213
numBytesSentTo (peerId) {

0 commit comments

Comments
 (0)