-
Notifications
You must be signed in to change notification settings - Fork 109
refactor: convert bitswap tests to use async/await #391
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,60 +5,59 @@ | |
|
||
### `bitswap.wantlist` | ||
|
||
> Returns the wantlist, optionally filtered by peer ID | ||
> Get the list of wanted CIDs for this peer or another peer on the network. | ||
|
||
#### `Go` **WIP** | ||
#### Go **WIP** | ||
|
||
#### `JavaScript` - ipfs.bitswap.wantlist([peerId], [callback]) | ||
#### JavaScript - `ipfs.bitswap.wantlist([peerId])` | ||
|
||
`callback` must follow `function (err, list) {}` signature, where `err` is an error if the operation was not successful. `list` is an Object containing the following keys: | ||
##### Parameters | ||
|
||
- `Keys` An array of objects containing the following keys: | ||
- `/` A string multihash | ||
| Name | Type | Description | | ||
|------|------|-------------| | ||
| peerId | `String` | (optional) Base 58 encoded Peer ID to get the wantlist for. Default: this node's peer ID | | ||
|
||
If no `callback` is passed, a promise is returned. | ||
##### Returns | ||
|
||
**Example:** | ||
| Type | Description | | ||
|------|-------------| | ||
| `Promise<{`<br/> `Keys<Array<Object>>`<br/>`}>` | The list of CIDs wanted by the peer. Each object in the array has a single property "/" a string CID. | | ||
|
||
```JavaScript | ||
ipfs.bitswap.wantlist((err, list) => console.log(list)) | ||
##### Example | ||
|
||
// { Keys: [{ '/': 'QmHash' }] } | ||
```js | ||
const list = await ipfs.bitswap.wantlist() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this return an iterator instead of a list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in general we should either return single values or iterators of single values, never lists of single values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, the wantlist could get really big so an iterator makes sense.
Do you think this should always be true? I think we should return async iterators where it makes sense to "stream" the output but I wouldn't have thought it would be necessary for something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we talking iterable ? or really async iterator ? |
||
console.log(list) // { Keys: [{ '/': 'QmHash' }] } | ||
``` | ||
|
||
ipfs.bitswap.wantlist(peerId, (err, list) => console.log(list)) | ||
Wantlist for a given peer: | ||
|
||
// { Keys: [{ '/': 'QmHash' }] } | ||
```js | ||
ipfs.bitswap.wantlist('QmZEYeEin6wEB7WNyiT7stYTmbYFGy7BzM7T3hRDzRxTvY') | ||
console.log(list) // { Keys: [{ '/': 'QmHash' }] } | ||
``` | ||
|
||
#### `bitswap.stat` | ||
|
||
> Show diagnostic information on the bitswap agent. | ||
|
||
##### `Go` **WIP** | ||
##### Go **WIP** | ||
|
||
##### `JavaScript` - ipfs.bitswap.stat([callback]) | ||
##### JavaScript - `ipfs.bitswap.stat()` | ||
|
||
Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably. | ||
|
||
`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful. `stats` is an Object containing the following keys: | ||
|
||
- `provideBufLen` is an integer. | ||
- `wantlist` (array of CIDs) | ||
- `peers` (array of peer IDs) | ||
- `blocksReceived` is a [Big Int][1] | ||
- `dataReceived` is a [Big Int][1] | ||
- `blocksSent` is a [Big Int][1] | ||
- `dataSent` is a [Big Int][1] | ||
- `dupBlksReceived` is a [Big Int][1] | ||
- `dupDataReceived` is a [Big Int][1] | ||
|
||
If no `callback` is passed, a promise is returned. | ||
##### Returns | ||
|
||
**Example:** | ||
| Type | Description | | ||
|------|-------------| | ||
| `Promise<{`<br/> `provideBufLen<Number>,`<br/> `wantlist<Array<Object>>,`<br/> `peers<Array<String>>,`<br/> `blocksReceived<`[`Big`][1]`>,`<br/> `dataReceived<`[`Big`][1]`>,`<br/> `blocksSent<`[`Big`][1]`>,`<br/> `dataSent<`[`Big`][1]`>,`<br/> `dupBlksReceived<`[`Big`][1]`>,`<br/> `dupDataReceived<`[`Big`][1]`>,`<br/>`}>` | Diagnostic information on the bitswap agent | | ||
|
||
```JavaScript | ||
ipfs.bitswap.stat((err, stats) => console.log(stats)) | ||
##### Example | ||
|
||
```js | ||
const stats = await ipfs.bitswap.stat() | ||
console.log(stats) | ||
// { provideBufLen: 0, | ||
// wantlist: [ { '/': 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM' } ], | ||
// peers: | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
'use strict' | ||
|
||
const until = require('async/until') | ||
|
||
function waitForWantlistKey (ipfs, key, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} | ||
|
||
async function waitForWantlistKey (ipfs, key, opts) { | ||
opts = opts || {} | ||
opts.timeout = opts.timeout || 1000 | ||
|
||
let list = { Keys: [] } | ||
let timedOut = false | ||
const start = Date.now() | ||
|
||
setTimeout(() => { timedOut = true }, opts.timeout) | ||
|
||
const test = () => timedOut ? true : list.Keys.every(k => k['/'] === key) | ||
const iteratee = (cb) => ipfs.bitswap.wantlist(opts.peerId, cb) | ||
while (Date.now() <= start + opts.timeout) { | ||
const list = await ipfs.bitswap.wantlist(opts.peerId) | ||
if (list.Keys.find(k => k['/'] === key)) return | ||
} | ||
|
||
until(test, iteratee, (err) => { | ||
if (err) return cb(err) | ||
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`)) | ||
cb() | ||
}) | ||
throw new Error(`Timed out waiting for ${key} in wantlist`) | ||
} | ||
|
||
module.exports.waitForWantlistKey = waitForWantlistKey |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems weird, html inside inline code block just to add new lines and space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know 😢. I would like this to be clearer when viewed as markdown but I don't know how right now. Unfortunately multi-line code blocks in a table aren't possible. If you have any ideas for how to format this in markdown so that it reads clearly when rendered then please comment!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've recently also started to use tables for parameters (for js-ipld). But I'll move away from that again. I find tables hard to read if you just read the unrendered Markdown.
The signature is quite complicated, I actually missed that it is an object containing a single field called
Keys
. I found that clearer in the old docs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will think up a better way to layout this information that is readable rendered or not.