Skip to content
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

Support bitswap 1.1.0 and bitswap 1.0.0 using CID #76

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4384208
add new protobuf
daviddias Dec 7, 2016
b15fb89
docs: move API docs to readme
daviddias Dec 7, 2016
effdbe2
fix: string is not the same as bytes in JS
daviddias Dec 7, 2016
ae95bbe
feat: update wantlist to support cid
daviddias Dec 7, 2016
84abea9
wip: bitswap message
daviddias Dec 7, 2016
99d64dd
feat: cid support in Bitswap message
daviddias Dec 7, 2016
cebd4b2
docs: add some notes to the readme about what is the code structure
daviddias Dec 7, 2016
3189db6
test: cidV1 test in wantlist
daviddias Dec 7, 2016
8758da6
feat: update wantmanager msg-queue to use cid
daviddias Dec 8, 2016
fd60a9c
feat: wantmanager uses cid
daviddias Dec 10, 2016
e7835cb
chore: refactor structure to make it more explicit what is which thin…
daviddias Dec 10, 2016
de99976
docs: add architecture graph
daviddias Dec 10, 2016
8e3b7a7
cr: apply CR
daviddias Dec 10, 2016
d15910d
feat: priority-queue done
daviddias Dec 10, 2016
c17f410
feat: PeerRequestQueue with CID support
daviddias Dec 10, 2016
89d922e
feat: upgrade ledger to use CID
daviddias Dec 10, 2016
65dbd28
feat: decision engine migration to CID (just missing one test)
daviddias Dec 11, 2016
a8da40a
fix engine tests
dignifiedquire Dec 11, 2016
0dc3b60
use .multihash intead of toV0
dignifiedquire Dec 12, 2016
32e8997
feat: update network to understand CID
daviddias Dec 16, 2016
beff8d1
upgrade message to support bitswap 1.0.0 and 1.1.0 simultaneously, also
daviddias Dec 16, 2016
3dc3493
feat: upgrade network component to support bitswap 1.0.0 and bitwap 1…
daviddias Dec 18, 2016
e892863
feat: update bitswap own API
daviddias Dec 18, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 0 additions & 82 deletions API.md

This file was deleted.

84 changes: 81 additions & 3 deletions README.md
Expand Up @@ -31,21 +31,21 @@
### npm

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

### Use in Node.js

```js
const bitswap = require('ipfs-bitswap')
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')
const Bitswap = require('ipfs-bitswap')
```

### Use in a browser using a script tag
Expand All @@ -62,6 +62,84 @@ Loading this module through a script tag will make the `IpfsBitswap` object avai

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

### API

#### `new Bitswap(id, libp2p, datastore)`

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

Create a new instance.

#### `getStream(key)`

- `key: Multihash|Array`

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

Example:

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

// Many blocks
pull(
bitswap.getStream([key1, key2, key3]),
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(keys)`

- `keys: Mutlihash|[]Multihash`

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

#### `cancelWants(keys)`

- `keys: Multihash|[]Multihash`

Cancel previously requested keys.

#### `putStream()`

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

#### `put(blockAndKey, cb)`

- `blockAndKey: {data: Buffer, key: Multihash}`
- `cb: 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.

#### `stat()`

Get stats about about the current state of the bitswap instance.

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs-bitswap/issues)!
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -60,7 +60,6 @@
"ipfs-block": "^0.5.1",
"lodash.debounce": "^4.0.8",
"lodash.isequalwith": "^4.4.0",
"lodash.isundefined": "^3.0.1",
"multihashes": "^0.3.0",
"protocol-buffers": "^3.2.1",
"pull-defer": "^0.2.2",
Expand All @@ -77,4 +76,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
10 changes: 6 additions & 4 deletions src/index.js
Expand Up @@ -18,9 +18,9 @@ const Network = require('./network')
const decision = require('./decision')

module.exports = class Bitwap {
constructor (p, libp2p, blockstore, peerBook) {
constructor (id, libp2p, blockstore, peerBook) {
// the ID of the peer to act on behalf of
this.self = p
this.self = id

// the network delivers messages
this.network = new Network(libp2p, peerBook, this)
Expand Down Expand Up @@ -57,7 +57,6 @@ module.exports = class Bitwap {
}

// quickly send out cancels, reduces chances of duplicate block receives

pull(
pull.values(iblocks),
pull.asyncMap((block, cb) => block.key(cb)),
Expand Down Expand Up @@ -332,6 +331,9 @@ function blockToStore (b, cb) {
if (err) {
return cb(err)
}
cb(null, {data: b.data, key: key})
cb(null, {
data: b.data,
key: key
})
})
}
35 changes: 22 additions & 13 deletions src/message/message.proto.js
@@ -1,21 +1,30 @@
'use strict'

module.exports = `package bitswap.message.pb;
// from: https://github.com/ipfs/go-ipfs/blob/master/exchange/bitswap/message/pb/message.proto

message Message {
module.exports = `
message Message {
message Wantlist {
message Entry {
// changed from string to bytes,
// because it makes a difference
// in JavaScript
optional bytes block = 1; // the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0)
optional int32 priority = 2; // the priority (normalized). default to 1
optional bool cancel = 3; // whether this revokes an entry
}

message Wantlist {
repeated Entry entries = 1; // a list of wantlist entries
optional bool full = 2; // whether this is the full wantlist. default to false
}

message Entry {
optional bytes block = 1; // the block key
optional int32 priority = 2; // the priority (normalized). default to 1
optional bool cancel = 3; // whether this revokes an entry
message Block {
optional bytes prefix = 1; // CID prefix (cid version, multicodec and multihash prefix (type + length)
optional bytes data = 2;
}

repeated Entry entries = 1; // a list of wantlist entries
optional bool full = 2; // whether this is the full wantlist. default to false
optional Wantlist wantlist = 1;
repeated bytes blocks = 2; // used to send Blocks in bitswap 1.0.0
repeated Block payload = 3; // used to send Blocks in bitswap 1.1.0
}

optional Wantlist wantlist = 1;
repeated bytes blocks = 2;
}`
`
14 changes: 7 additions & 7 deletions src/network/index.js
Expand Up @@ -83,9 +83,9 @@ module.exports = class Network {
}

// Connect to the given peer
connectTo (peerId, cb) {
connectTo (peerId, callback) {
log('connecting to %s', peerId.toB58String())
const done = (err) => setImmediate(() => cb(err))
const done = (err) => setImmediate(() => callback(err))
// NOTE: For now, all this does is ensure that we are
// connected. Once we have Peer Routing, we will be able
// to find the Peer
Expand All @@ -97,27 +97,27 @@ module.exports = class Network {
}

// Send the given msg (instance of Message) to the given peer
sendMessage (peerId, msg, cb) {
sendMessage (peerId, msg, callback) {
const stringId = peerId.toB58String()
log('sendMessage to %s', stringId)
let peerInfo
try {
peerInfo = this.peerBook.getByMultihash(peerId.toBytes())
} catch (err) {
return cb(err)
return callback(err)
}

if (this.conns.has(stringId)) {
log('connection exists')
this.conns.get(stringId).push(msg.toProto())
return cb()
return callback()
}

log('dialByPeerInfo')
this.libp2p.dialByPeerInfo(peerInfo, PROTOCOL_IDENTIFIER, (err, conn) => {
log('dialed %s', peerInfo.id.toB58String(), err)
if (err) {
return cb(err)
return callback(err)
}

const msgQueue = pushable()
Expand All @@ -138,7 +138,7 @@ module.exports = class Network {
})
)

cb()
callback()
})
}
}
20 changes: 12 additions & 8 deletions src/wantlist/entry.js
@@ -1,17 +1,17 @@
'use strict'

const assert = require('assert')
const isUndefined = require('lodash.isundefined')
const mh = require('multihashes')
const CID = require('cids')

class WantListEntry {
constructor (cid, priority) {
assert(CID.isCID(cid), 'must be valid CID')

module.exports = class WantlistEntry {
constructor (key, priority) {
assert(Buffer.isBuffer(key), 'key must be a buffer')
// Keep track of how many requests we have for this key
this._refCounter = 1

this.key = key
this.priority = isUndefined(priority) ? 1 : priority
this.cid = cid
this.priority = priority || 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not good, you shouldn't use || if the input is a number.

}

inc () {
Expand All @@ -26,8 +26,10 @@ module.exports = class WantlistEntry {
return this._refCounter > 0
}

// So that console.log prints a nice description of this object
get [Symbol.toStringTag] () {
return `WantlistEntry <key: ${mh.toB58String(this.key)}, priority: ${this.priority}, refs: ${this._refCounter}>`
const cidStr = this.cid.toBaseEncodedString()
return `WantlistEntry <key: ${cidStr}, priority: ${this.priority}, refs: ${this._refCounter}>`
}

equals (other) {
Expand All @@ -36,3 +38,5 @@ module.exports = class WantlistEntry {
this.priority === other.priority
}
}

module.exports = WantListEntry