Skip to content
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
44 changes: 4 additions & 40 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
# While testing new npm
package-lock.json
yarn.lock

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output/

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

dist

test/test-repo-for*
docs

test/test-repo/datastore

*.flamegraph
types
.docs
.coverage
package-lock.json
yarn.lock
83 changes: 19 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
# ipfs-bitswap <!-- omit in toc -->

[![ipfs.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io)
[![IRC](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Discord](https://img.shields.io/discord/806902334369824788?style=flat-square)](https://discord.gg/ipfs)
[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-ipfs-bitswap.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-ipfs-bitswap)
[![CI](https://img.shields.io/github/workflow/status/ipfs/js-ipfs-bitswap/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/ipfs/js-ipfs-bitswap/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/actions/workflow/status/ipfs/js-ipfs-bitswap/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/ipfs/js-ipfs-bitswap/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)

> JavaScript implementation of the Bitswap data exchange protocol used by IPFS

## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Use in Node.js or in the browser with browserify, webpack or any other bundler](#use-in-nodejs-or-in-the-browser-with-browserify-webpack-or-any-other-bundler)
- [Use in a browser using a script tag](#use-in-a-browser-using-a-script-tag)
- [API](#api)
- [Stats](#stats)
- [Peer accessor:](#peer-accessor)
- [Global snapshot accessor:](#global-snapshot-accessor)
- [Moving average accessor:](#moving-average-accessor)
- [Development](#development)
- [Structure](#structure)
- [Browser `<script>` tag](#browser-script-tag)
- [Stats](#stats)
- [Peer accessor:](#peer-accessor)
- [Global snapshot accessor:](#global-snapshot-accessor)
- [Moving average accessor:](#moving-average-accessor)
- [Performance tests](#performance-tests)
- [Profiling](#profiling)
- [API Docs](#api-docs)
- [License](#license)
- [Contribute](#contribute)

Expand All @@ -31,31 +27,15 @@
$ npm i ipfs-bitswap
```

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

### Use in Node.js or in the browser with browserify, webpack or any other bundler

```js
const { createBitswap } from 'ipfs-bitswap'
```

### Use in a browser using a script tag
### Browser `<script>` tag

Loading this module through a script tag will make the `IpfsBitswap` object available in the global namespace.
Loading this module through a script tag will make it's exports available as `IpfsBitswap` in the global namespace.

```html
<script src="https://unpkg.com/ipfs-bitswap/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/ipfs-bitswap/dist/index.js"></script>
```

## API

See <https://ipfs.github.io/js-ipfs-bitswap>

### Stats
## Stats

```js
const bitswapNode = // ...
Expand All @@ -73,7 +53,7 @@ stats.on('update', (stats) => {
})
```

#### Peer accessor:
### Peer accessor:

You can get the stats for a specific peer by doing:

Expand All @@ -83,7 +63,7 @@ const peerStats = stats.forPeer(peerId)

The returned object behaves like the root stats accessor (has a snapshot, a moving average accessors and is an event emitter).

#### Global snapshot accessor:
### Global snapshot accessor:

```js
const snapshot = stats.snapshot
Expand All @@ -107,7 +87,7 @@ the snapshot will contain the following keys, with the values being [bignumber.j
// }
```

#### Moving average accessor:
### Moving average accessor:

```js
const movingAverages = stats.movingAverages
Expand Down Expand Up @@ -143,35 +123,6 @@ const oneMinuteDataReceivedMovingAverages = dataReceivedMovingAverages[60000]

This object will be a [movingAverage](https://github.com/pgte/moving-average#readme) instance.

## Development

### Structure

![](/img/architecture.png)

```sh
» tree src
src
├── constants.js
├── decision-engine
│ ├── index.js
│ └── ledger.js
├── index.js
├── network.js # Handles peerSet and open new conns
├── notifications.js # Handles tracking of incomning blocks and wants/unwants.
├─── want-manager # Keeps track of all blocks the peer (self) wants
│ ├── index.js
│ └── msg-queue.js # Messages to send queue, one per peer
└─── types
├── message # (Type) message that is put in the wire
│ ├── entry.js
│ ├── index.js
│ └── message.proto.js
└── wantlist # (Type) track wanted blocks
├── entry.js
└── index.js
```

## Performance tests

You can run performance tests like this:
Expand Down Expand Up @@ -199,6 +150,10 @@ Use the browser Chrome to open and inspect the generated graph.

![Flame graph](https://ipfs.io/ipfs/QmVbyLgYfkLewNtzTAFwAEMmP2hTJgs8sSqsRTBNBjyQ1y)

## API Docs

- <https://ipfs.github.io/js-ipfs-bitswap>

## License

Licensed under either of
Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,27 @@
"dep-check": "aegir dep-check -i rimraf",
"generate": "run-s generate:*",
"generate:proto": "pbjs -t static-module -w es6 -r ipfs-bitswap --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/message/message.js src/message/message.proto",
"generate:proto-types": "pbts -o src/message/message.d.ts src/message/message.js"
"generate:proto-types": "pbts -o src/message/message.d.ts src/message/message.js",
"docs": "aegir docs"
},
"dependencies": {
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-peer-id": "^1.0.4",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-store": "^1.2.0",
"@libp2p/topology": "^3.0.0",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.5",
"@libp2p/topology": "^4.0.0",
"@libp2p/tracked-map": "^3.0.0",
"@multiformats/multiaddr": "^11.0.0",
"@vascosantos/moving-average": "^1.1.0",
"abortable-iterator": "^4.0.2",
"any-signal": "^3.0.0",
"blockstore-core": "^2.0.1",
"debug": "^4.2.0",
"err-code": "^3.0.1",
"interface-blockstore": "^3.0.0",
"blockstore-core": "^3.0.0",
"interface-blockstore": "^4.0.0",
"it-length-prefixed": "^8.0.2",
"it-pipe": "^2.0.4",
"just-debounce-it": "^3.0.1",
"multiformats": "^10.0.1",
"multiformats": "^11.0.0",
"protobufjs": "^7.0.0",
"readable-stream": "^4.0.0",
"timeout-abort-controller": "^3.0.0",
Expand All @@ -191,16 +192,15 @@
},
"devDependencies": {
"@chainsafe/libp2p-noise": "^10.0.1",
"@libp2p/kad-dht": "^6.0.0",
"@libp2p/kad-dht": "^7.0.0",
"@libp2p/mplex": "^7.0.0",
"@libp2p/peer-id": "^1.1.8",
"@libp2p/peer-id-factory": "^1.0.8",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/peer-id-factory": "^2.0.0",
"@libp2p/tcp": "^6.0.0",
"@nodeutils/defaults-deep": "^1.1.0",
"@types/debug": "^4.1.5",
"@types/stats-lite": "^2.2.0",
"@types/varint": "^6.0.0",
"aegir": "^37.0.4",
"aegir": "^37.10.0",
"assert": "^2.0.0",
"benchmark": "^2.1.4",
"delay": "^5.0.0",
Expand All @@ -221,7 +221,7 @@
"promisify-es6": "^1.0.3",
"protobufjs-cli": "^1.0.0",
"rimraf": "^3.0.2",
"sinon": "^14.0.0",
"sinon": "^15.0.1",
"stats-lite": "^2.2.0",
"url": "^0.11.0",
"util": "^0.12.3",
Expand Down
2 changes: 1 addition & 1 deletion src/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const statsKeys = [
*/
export class Bitswap extends BaseBlockstore {
/**
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
* @param {Blockstore} blockstore
* @param {object} [options]
* @param {boolean} [options.statsEnabled=false]
Expand Down
2 changes: 1 addition & 1 deletion src/decision-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class DecisionEngine {
* @param {import('interface-blockstore').Blockstore} blockstore
* @param {import('../network').Network} network
* @param {import('../stats').Stats} stats
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
* @param {object} [opts]
* @param {number} [opts.targetMessageSize]
* @param {number} [opts.maxSizeReplaceHasWithBlock]
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Bitswap } from './bitswap.js'
*/

/**
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
* @param {Blockstore} blockstore
* @param {object} [options]
* @param {boolean} [options.statsEnabled=false]
Expand Down
4 changes: 2 additions & 2 deletions src/message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ve from '../utils/varint-encoder.js'
import { isMapEqual } from '../utils/index.js'
import { Message } from './message.js'
import { BitswapMessageEntry as Entry } from './entry.js'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'

/**
* @typedef {import('../types').MultihashHasherLoader} MultihashHasherLoader
Expand Down Expand Up @@ -291,7 +291,7 @@ BitswapMessage.deserialize = async (raw, hashLoader) => {
const hasher = hashAlg === sha256.code ? sha256 : hashLoader && await hashLoader.getHasher(hashAlg)

if (!hasher) {
throw errcode(new Error('Unknown hash algorithm'), 'ERR_UNKNOWN_HASH_ALG')
throw new CodeError('Unknown hash algorithm', 'ERR_UNKNOWN_HASH_ALG')
}

// const hashLen = values[3] // We haven't need to use this so far
Expand Down
6 changes: 3 additions & 3 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DEFAULT_INCOMING_STREAM_TIMEOUT = 30000

export class Network {
/**
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
* @param {import('./bitswap').Bitswap} bitswap
* @param {import('./stats').Stats} stats
* @param {object} [options]
Expand Down Expand Up @@ -84,7 +84,7 @@ export class Network {
this._registrarIds = []

for (const protocol of this._protocols) {
this._registrarIds.push(await this._libp2p.registrar.register(protocol, topology))
this._registrarIds.push(await this._libp2p.register(protocol, topology))
}

// All existing connections are like new ones for us
Expand All @@ -102,7 +102,7 @@ export class Network {
// unregister protocol and handlers
if (this._registrarIds != null) {
for (const id of this._registrarIds) {
this._libp2p.registrar.unregister(id)
this._libp2p.unregister(id)
}

this._registrarIds = []
Expand Down
2 changes: 1 addition & 1 deletion src/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const defaultOptions = {

export class Stats extends EventEmitter {
/**
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
* @param {string[]} [initialCounters]
* @param {object} _options
* @param {boolean} _options.enabled
Expand Down
6 changes: 2 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import debug from 'debug'
import { logger as createLogger } from '@libp2p/logger'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { BitswapMessageEntry } from '../message/entry.js'

Expand All @@ -17,9 +17,7 @@ export const logger = (id, subsystem) => {
name.push(`${id.toString().slice(0, 8)}`)
}

return Object.assign(debug(name.join(':')), {
error: debug(name.concat(['error']).join(':'))
})
return createLogger(name.join(':'))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/want-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class WantManager {
* @param {PeerId} peerId
* @param {import('../network').Network} network
* @param {import('../stats').Stats} stats
* @param {import('libp2p').Libp2p} libp2p
* @param {import('@libp2p/interface-libp2p').Libp2p} libp2p
*/
constructor (peerId, network, stats, libp2p) {
/** @type {Map<string, MsgQueue>} */
Expand Down
2 changes: 1 addition & 1 deletion src/wantlist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const sortBy = (fn, list) => {
export class Wantlist {
/**
* @param {import('../stats').Stats} [stats]
* @param {import('libp2p').Libp2p} [libp2p]
* @param {import('@libp2p/interface-libp2p').Libp2p} [libp2p]
*/
constructor (stats, libp2p) {
/** @type {Map<string, Entry>} */
Expand Down
2 changes: 1 addition & 1 deletion test/bitswap-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { makeBlocks } from './utils/make-blocks.js'
import { makePeerIds } from './utils/make-peer-id.js'

/**
* @typedef {import('libp2p').Libp2p} Libp2p
* @typedef {import('@libp2p/interface-libp2p').Libp2p} Libp2p
* @typedef {import('multiformats/cid').CID} CID
*/

Expand Down
Loading