Skip to content

Commit

Permalink
chore: fix types (#321)
Browse files Browse the repository at this point in the history
- Remove bignumber.js in favour of BigInt
- Use ipld-block in favour of the old ipfs-core-types
- Use blockstore from ipfs-repo instead of the old ipfs-core-types
  • Loading branch information
achingbrain authored Apr 16, 2021
1 parent de83324 commit 82c8935
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 171 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"src"
],
"scripts": {
"prepare": "npm run build",
"build": "npm run build:proto && npm run build:proto-types && npm run build:bundle",
"build:proto": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto",
"build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
Expand Down Expand Up @@ -70,15 +71,15 @@
"assert": "^2.0.0",
"benchmark": "^2.1.4",
"delay": "^5.0.0",
"ipfs-repo": "^9.0.0",
"ipfs-repo": "^9.1.0",
"ipfs-utils": "^6.0.1",
"iso-random-stream": "^2.0.0",
"it-all": "^1.0.5",
"it-drain": "^1.0.4",
"libp2p": "^0.30.9",
"libp2p": "0.31.0-rc.0",
"libp2p-kad-dht": "^0.21.0",
"libp2p-mplex": "^0.10.2",
"libp2p-secio": "^0.13.1",
"libp2p-noise": "^2.0.5",
"libp2p-tcp": "^0.15.3",
"lodash.difference": "^4.5.0",
"lodash.flatten": "^4.4.0",
Expand All @@ -97,16 +98,15 @@
"dependencies": {
"abort-controller": "^3.0.0",
"any-signal": "^2.1.2",
"bignumber.js": "^9.0.0",
"cids": "^1.1.6",
"debug": "^4.2.0",
"ipfs-core-types": "^0.3.1",
"ipld-block": "^0.11.0",
"it-length-prefixed": "^5.0.2",
"it-pipe": "^1.1.0",
"just-debounce-it": "^1.1.0",
"libp2p-interfaces": "^0.8.3",
"libp2p-interfaces": "^0.10.0",
"moving-average": "^1.0.1",
"multiaddr": "^9.0.1",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
"native-abort-controller": "^1.0.3",
Expand Down
9 changes: 5 additions & 4 deletions src/decision-engine/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
* @typedef {import('ipld-block')} Block
* @typedef {import('../types/message/entry')} BitswapMessageEntry
* @typedef {import('peer-id')} PeerId
*/

const CID = require('cids')
Expand Down Expand Up @@ -34,7 +34,7 @@ const MAX_SIZE_REPLACE_HAS_WITH_BLOCK = 1024
class DecisionEngine {
/**
* @param {PeerId} peerId
* @param {import('ipfs-core-types/src/block-store').BlockStore} blockstore
* @param {import('ipfs-repo').Blockstore} blockstore
* @param {import('../network')} network
* @param {import('../stats')} stats
* @param {Object} [opts]
Expand Down Expand Up @@ -180,15 +180,16 @@ class DecisionEngine {

/**
* @param {PeerId} peerId
* @returns {import('ipfs-core-types/src/bitswap').LedgerForPeer|null}
*/
ledgerForPeer (peerId) {
const peerIdStr = peerId.toB58String()

const ledger = this.ledgerMap.get(peerIdStr)

if (!ledger) {
return null
}

return {
peer: ledger.partner.toPrint(),
value: ledger.debtRatio(),
Expand Down
53 changes: 9 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ const { AbortController } = require('native-abort-controller')
const { anySignal } = require('any-signal')

/**
* @typedef {import('ipfs-core-types/src/basic').AbortOptions} AbortOptions
* @typedef {import('ipfs-core-types/src/bitswap').Bitswap} API
* @typedef {import('ipfs-core-types/src/bitswap').WantListEntry} WantListEntry
* @typedef {import('ipfs-core-types/src/bitswap').LedgerForPeer} LedgerForPeer
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
* @typedef {import('ipld-block')} Block
* @typedef {import('peer-id')} PeerId
* @typedef {import('./types/message')} BitswapMessage
* @typedef {import('cids')} CID
Expand All @@ -40,13 +36,11 @@ const statsKeys = [
/**
* JavaScript implementation of the Bitswap 'data exchange' protocol
* used by IPFS.
*
* @implements {API}
*/
class Bitswap {
/**
* @param {import('libp2p')} libp2p
* @param {import('ipfs-core-types/src/block-store').BlockStore} blockstore
* @param {import('ipfs-repo').Blockstore} blockstore
* @param {Object} [options]
* @param {boolean} [options.statsEnabled=false]
* @param {number} [options.statsComputeThrottleTimeout=1000]
Expand Down Expand Up @@ -91,7 +85,6 @@ class Bitswap {
*
* @param {PeerId} peerId
* @param {BitswapMessage} incoming
* @returns {Promise<void>}
*/
async _receiveMessage (peerId, incoming) {
try {
Expand Down Expand Up @@ -130,7 +123,6 @@ class Bitswap {
* @param {PeerId} peerId
* @param {Block} block
* @param {boolean} wasWanted
* @returns {Promise<void>}
*/
async _handleReceivedBlock (peerId, block, wasWanted) {
this._log('received block')
Expand Down Expand Up @@ -166,7 +158,6 @@ class Bitswap {
* handle errors on the receiving channel
*
* @param {Error} err
* @returns {void}
*/
_receiveError (err) {
this._log.error('ReceiveError: %s', err.message)
Expand All @@ -192,16 +183,10 @@ class Bitswap {
this._stats.disconnected(peerId)
}

/**
* @returns {void}
*/
enableStats () {
this._stats.enable()
}

/**
* @returns {void}
*/
disableStats () {
this._stats.disable()
}
Expand All @@ -210,8 +195,7 @@ class Bitswap {
* Return the current wantlist for a given `peerId`
*
* @param {PeerId} peerId
* @param {AbortOptions} [_options]
* @returns {Map<string, WantListEntry>}
* @param {any} [_options]
*/
wantlistForPeer (peerId, _options) {
return this.engine.wantlistForPeer(peerId)
Expand All @@ -221,7 +205,6 @@ class Bitswap {
* Return ledger information for a given `peerId`
*
* @param {PeerId} peerId
* @returns {null|LedgerForPeer}
*/
ledgerForPeer (peerId) {
return this.engine.ledgerForPeer(peerId)
Expand All @@ -234,14 +217,12 @@ class Bitswap {
* @param {CID} cid
* @param {Object} [options]
* @param {AbortSignal} [options.signal]
* @returns {Promise<Block>}
*/
async get (cid, options = {}) {
/**
* @param {CID} cid
* @param {Object} options
* @param {AbortSignal} options.signal
* @returns {Promise<Block>}
*/
const fetchFromNetwork = (cid, options) => {
// add it to the want list - n.b. later we will abort the AbortSignal
Expand All @@ -258,7 +239,6 @@ class Bitswap {
* @param {CID} cid
* @param {Object} options
* @param {AbortSignal} options.signal
* @returns {Promise<Block>}
*/
const loadOrFetchFromNetwork = async (cid, options) => {
try {
Expand Down Expand Up @@ -314,7 +294,6 @@ class Bitswap {
* @param {AsyncIterable<CID>|Iterable<CID>} cids
* @param {Object} [options]
* @param {AbortSignal} [options.signal]
* @returns {AsyncIterable<Block>}
*/
async * getMany (cids, options = {}) {
for await (const cid of cids) {
Expand All @@ -331,7 +310,6 @@ class Bitswap {
* AbortSignal in to `.get` or `.getMany` and abort it.
*
* @param {CID[]|CID} cids
* @returns {void}
*/
unwant (cids) {
const cidsArray = Array.isArray(cids) ? cids : [cids]
Expand All @@ -346,7 +324,6 @@ class Bitswap {
* call `unwant(cids)` instead.
*
* @param {CID[]|CID} cids
* @returns {void}
*/
cancelWants (cids) {
this.wm.cancelWants(Array.isArray(cids) ? cids : [cids])
Expand All @@ -357,8 +334,7 @@ class Bitswap {
* send it to nodes that have it in their wantlist.
*
* @param {Block} block
* @param {AbortOptions} [_options]
* @returns {Promise<void>}
* @param {any} [_options]
*/
async put (block, _options) {
await this.blockstore.put(block)
Expand All @@ -370,7 +346,6 @@ class Bitswap {
* send it to nodes that have it them their wantlist.
*
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
* @returns {AsyncIterable<Block>}
*/
async * putMany (blocks) {
for await (const block of this.blockstore.putMany(blocks)) {
Expand All @@ -396,36 +371,28 @@ class Bitswap {
}

/**
* Get the current list of wants.
*
* @returns {Iterable<[string, WantListEntry]>}
* Get the current list of wants
*/
getWantlist () {
return this.wm.wantlist.entries()
}

/**
* Get the current list of partners.
*
* @returns {PeerId[]}
* Get the current list of partners
*/
peers () {
return this.engine.peers()
}

/**
* Get stats about the bitswap node.
*
* @returns {import('ipfs-core-types/src/bitswap').Stats}
* Get stats about the bitswap node
*/
stat () {
return this._stats
}

/**
* Start the bitswap node.
*
* @returns {void}
* Start the bitswap node
*/
start () {
this.wm.start()
Expand All @@ -434,9 +401,7 @@ class Bitswap {
}

/**
* Stop the bitswap node.
*
* @returns {void}
* Stop the bitswap node
*/
stop () {
this._stats.stop()
Expand Down
30 changes: 7 additions & 23 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const logger = require('./utils').logger
/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('cids')} CID
* @typedef {import('multiaddr')} Multiaddr
*
* @typedef {Object} Connection
* @property {string} id
* @property {PeerId} remotePeer
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*
* @typedef {Object} Provider
* @property {PeerId} id
Expand Down Expand Up @@ -100,9 +98,8 @@ class Network {
* @private
* @param {object} connection
* @param {string} connection.protocol - The protocol the stream is running
* @param {Stream} connection.stream - A duplex iterable stream
* @param {MuxedStream} connection.stream - A duplex iterable stream
* @param {Connection} connection.connection - A libp2p Connection
* @returns {Promise<void>}
*/
async _onConnection ({ protocol, stream, connection }) {
if (!this._running) { return }
Expand Down Expand Up @@ -143,7 +140,6 @@ class Network {
/**
* @private
* @param {PeerId} peerId
* @returns {void}
*/
_onPeerDisconnect (peerId) {
this._bitswap._onPeerDisconnected(peerId)
Expand Down Expand Up @@ -177,7 +173,6 @@ class Network {
* @param {CID} cid
* @param {Object} [options]
* @param {AbortSignal} [options.signal]
* @returns {Promise<void>}
*/
async findAndConnect (cid, options) {
const connectAttempts = []
Expand All @@ -194,7 +189,6 @@ class Network {
* @param {CID} cid
* @param {Object} [options]
* @param {AbortSignal} [options.signal]
* @returns {Promise<void>}
*/
async provide (cid, options) {
// @ts-expect-error - contentRouting takes no options
Expand All @@ -207,15 +201,15 @@ class Network {
*
* @param {PeerId} peer
* @param {Message} msg
* @returns {Promise<void>}
*/
async sendMessage (peer, msg) {
if (!this._running) throw new Error('network isn\'t running')

const stringId = peer.toB58String()
this._log('sendMessage to %s', stringId, msg)

const { stream, protocol } = await this._dialPeer(peer)
const connection = await this._libp2p.dial(peer)
const { stream, protocol } = await connection.newStream([BITSWAP120, BITSWAP110, BITSWAP100])

/** @type {Uint8Array} */
let serialized
Expand Down Expand Up @@ -253,16 +247,6 @@ class Network {
return this._libp2p.dial(peer, options)
}

/**
* Dial to the peer and try to use the most recent Bitswap
*
* @private
* @param {PeerId|Multiaddr} peer
*/
_dialPeer (peer) {
return this._libp2p.dialProtocol(peer, [BITSWAP120, BITSWAP110, BITSWAP100])
}

/**
* @private
* @param {PeerId} peer
Expand All @@ -280,7 +264,7 @@ class Network {

/**
*
* @param {Stream} stream
* @param {MuxedStream} stream
* @param {Uint8Array} msg
* @param {*} log
*/
Expand Down
2 changes: 1 addition & 1 deletion src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CONSTANTS = require('./constants')
const logger = require('./utils').logger

/**
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
* @typedef {import('ipld-block')} Block
*/

/**
Expand Down
Loading

0 comments on commit 82c8935

Please sign in to comment.