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

chore: fix types #321

Merged
merged 4 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 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,7 +71,7 @@
"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",
Expand All @@ -97,10 +98,9 @@
"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",
"ipfs-core-types": "next",
Copy link
Member

Choose a reason for hiding this comment

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

we need to get rid of these circular dependencies

Copy link
Member Author

@achingbrain achingbrain Apr 16, 2021

Choose a reason for hiding this comment

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

I don't think this is even required now, since we can pull the Blockstore type from ipfs-repo it's only in the tests, and even then it was only being used to pull types for the repo, which are in ipfs-repo now so it can go.

"ipld-block": "^0.11.0",
"it-length-prefixed": "^5.0.2",
"it-pipe": "^1.1.0",
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
5 changes: 0 additions & 5 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class Network {
* @param {string} connection.protocol - The protocol the stream is running
* @param {Stream} 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 +142,6 @@ class Network {
/**
* @private
* @param {PeerId} peerId
* @returns {void}
*/
_onPeerDisconnect (peerId) {
this._bitswap._onPeerDisconnected(peerId)
Expand Down Expand Up @@ -177,7 +175,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 +191,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,7 +203,6 @@ 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')
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
5 changes: 1 addition & 4 deletions src/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { EventEmitter } = require('events')
const Stat = require('./stat')

/**
* @typedef {import('cids')} CID
* @typedef {import('peer-id')} PeerId
* @typedef {import('ipfs-core-types/src/bitswap').Stats} API
*/

/**
Expand All @@ -22,9 +22,6 @@ const defaultOptions = {
])
}

/**
* @implements {API}
*/
class Stats extends EventEmitter {
/**
* @param {string[]} [initialCounters]
Expand Down
14 changes: 5 additions & 9 deletions src/stats/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { EventEmitter } = require('events')
const { BigNumber: Big } = require('bignumber.js')
const MovingAverage = require('moving-average')

/**
Expand All @@ -24,7 +23,7 @@ class Stats extends EventEmitter {
this._options = options
/** @type {Op[]} */
this._queue = []
/** @type {Record<string, Big>} */
/** @type {Record<string, bigint>} */
this._stats = {}

this._frequencyLastTime = Date.now()
Expand All @@ -37,7 +36,7 @@ class Stats extends EventEmitter {
this._update = this._update.bind(this)

initialCounters.forEach((key) => {
this._stats[key] = new Big(0)
this._stats[key] = BigInt(0)
this._movingAverages[key] = {}
this._options.movingAverageIntervals.forEach((interval) => {
const ma = this._movingAverages[key][interval] = MovingAverage(interval)
Expand Down Expand Up @@ -173,14 +172,11 @@ class Stats extends EventEmitter {
throw new Error(`invalid increment number: ${inc}`)
}

let n

if (!Object.prototype.hasOwnProperty.call(this._stats, key)) {
n = this._stats[key] = new Big(0)
} else {
n = this._stats[key]
this._stats[key] = BigInt(0)
}
this._stats[key] = n.plus(inc)

this._stats[key] = BigInt(this._stats[key]) + BigInt(inc)

if (!this._frequencyAccumulators[key]) {
this._frequencyAccumulators[key] = 0
Expand Down
Loading