@helia/bitswap 0.0.0-329652a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +64 -0
- package/dist/index.min.js +3 -0
- package/dist/src/bitswap.d.ts +50 -0
- package/dist/src/bitswap.d.ts.map +1 -0
- package/dist/src/bitswap.js +120 -0
- package/dist/src/bitswap.js.map +1 -0
- package/dist/src/constants.d.ts +12 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +12 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/index.d.ts +178 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/network.d.ts +84 -0
- package/dist/src/network.d.ts.map +1 -0
- package/dist/src/network.js +370 -0
- package/dist/src/network.js.map +1 -0
- package/dist/src/pb/message.d.ts +67 -0
- package/dist/src/pb/message.d.ts.map +1 -0
- package/dist/src/pb/message.js +359 -0
- package/dist/src/pb/message.js.map +1 -0
- package/dist/src/peer-want-lists/index.d.ts +44 -0
- package/dist/src/peer-want-lists/index.d.ts.map +1 -0
- package/dist/src/peer-want-lists/index.js +116 -0
- package/dist/src/peer-want-lists/index.js.map +1 -0
- package/dist/src/peer-want-lists/ledger.d.ts +54 -0
- package/dist/src/peer-want-lists/ledger.d.ts.map +1 -0
- package/dist/src/peer-want-lists/ledger.js +104 -0
- package/dist/src/peer-want-lists/ledger.js.map +1 -0
- package/dist/src/session.d.ts +20 -0
- package/dist/src/session.d.ts.map +1 -0
- package/dist/src/session.js +100 -0
- package/dist/src/session.js.map +1 -0
- package/dist/src/stats.d.ts +16 -0
- package/dist/src/stats.d.ts.map +1 -0
- package/dist/src/stats.js +49 -0
- package/dist/src/stats.js.map +1 -0
- package/dist/src/utils/cid-prefix.d.ts +3 -0
- package/dist/src/utils/cid-prefix.d.ts.map +1 -0
- package/dist/src/utils/cid-prefix.js +7 -0
- package/dist/src/utils/cid-prefix.js.map +1 -0
- package/dist/src/utils/varint-decoder.d.ts +3 -0
- package/dist/src/utils/varint-decoder.d.ts.map +1 -0
- package/dist/src/utils/varint-decoder.js +15 -0
- package/dist/src/utils/varint-decoder.js.map +1 -0
- package/dist/src/utils/varint-encoder.d.ts +3 -0
- package/dist/src/utils/varint-encoder.d.ts.map +1 -0
- package/dist/src/utils/varint-encoder.js +14 -0
- package/dist/src/utils/varint-encoder.js.map +1 -0
- package/dist/src/want-list.d.ts +120 -0
- package/dist/src/want-list.d.ts.map +1 -0
- package/dist/src/want-list.js +361 -0
- package/dist/src/want-list.js.map +1 -0
- package/package.json +200 -0
- package/src/bitswap.ts +152 -0
- package/src/constants.ts +11 -0
- package/src/index.ts +215 -0
- package/src/network.ts +506 -0
- package/src/pb/message.proto +42 -0
- package/src/pb/message.ts +450 -0
- package/src/peer-want-lists/index.ts +165 -0
- package/src/peer-want-lists/ledger.ts +161 -0
- package/src/session.ts +150 -0
- package/src/stats.ts +67 -0
- package/src/utils/cid-prefix.ts +8 -0
- package/src/utils/varint-decoder.ts +19 -0
- package/src/utils/varint-encoder.ts +18 -0
- package/src/want-list.ts +529 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { WantType } from '../pb/message.js';
|
|
2
|
+
import type { Network } from '../network.js';
|
|
3
|
+
import type { PeerId } from '@libp2p/interface';
|
|
4
|
+
import type { Blockstore } from 'interface-blockstore';
|
|
5
|
+
import type { AbortOptions } from 'it-length-prefixed-stream';
|
|
6
|
+
import type { CID } from 'multiformats/cid';
|
|
7
|
+
export interface LedgerComponents {
|
|
8
|
+
peerId: PeerId;
|
|
9
|
+
blockstore: Blockstore;
|
|
10
|
+
network: Network;
|
|
11
|
+
}
|
|
12
|
+
export interface LedgerInit {
|
|
13
|
+
maxSizeReplaceHasWithBlock?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface PeerWantListEntry {
|
|
16
|
+
/**
|
|
17
|
+
* The CID the peer has requested
|
|
18
|
+
*/
|
|
19
|
+
cid: CID;
|
|
20
|
+
/**
|
|
21
|
+
* The priority with which the remote should return the block
|
|
22
|
+
*/
|
|
23
|
+
priority: number;
|
|
24
|
+
/**
|
|
25
|
+
* If we want the block or if we want the remote to tell us if they have the
|
|
26
|
+
* block - note if the block is small they'll send it to us anyway.
|
|
27
|
+
*/
|
|
28
|
+
wantType: WantType;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the remote should tell us if they have the block or not
|
|
31
|
+
*/
|
|
32
|
+
sendDontHave: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* If we don't have the block and we've told them we don't have the block
|
|
35
|
+
*/
|
|
36
|
+
sentDontHave?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export declare class Ledger {
|
|
39
|
+
peerId: PeerId;
|
|
40
|
+
private readonly blockstore;
|
|
41
|
+
private readonly network;
|
|
42
|
+
wants: Map<string, PeerWantListEntry>;
|
|
43
|
+
exchangeCount: number;
|
|
44
|
+
bytesSent: number;
|
|
45
|
+
bytesReceived: number;
|
|
46
|
+
lastExchange?: number;
|
|
47
|
+
private readonly maxSizeReplaceHasWithBlock;
|
|
48
|
+
constructor(components: LedgerComponents, init: LedgerInit);
|
|
49
|
+
sentBytes(n: number): void;
|
|
50
|
+
receivedBytes(n: number): void;
|
|
51
|
+
debtRatio(): number;
|
|
52
|
+
sendBlocksToPeer(options?: AbortOptions): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../../src/peer-want-lists/ledger.ts"],"names":[],"mappings":"AAEA,OAAO,EAA0C,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAEnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,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 +1,104 @@
|
|
|
1
|
+
/* eslint-disable max-depth */
|
|
2
|
+
import { DEFAULT_MAX_SIZE_REPLACE_HAS_WITH_BLOCK } from '../constants.js';
|
|
3
|
+
import { BlockPresenceType, WantType } from '../pb/message.js';
|
|
4
|
+
import { cidToPrefix } from '../utils/cid-prefix.js';
|
|
5
|
+
export class Ledger {
|
|
6
|
+
peerId;
|
|
7
|
+
blockstore;
|
|
8
|
+
network;
|
|
9
|
+
wants;
|
|
10
|
+
exchangeCount;
|
|
11
|
+
bytesSent;
|
|
12
|
+
bytesReceived;
|
|
13
|
+
lastExchange;
|
|
14
|
+
maxSizeReplaceHasWithBlock;
|
|
15
|
+
constructor(components, init) {
|
|
16
|
+
this.peerId = components.peerId;
|
|
17
|
+
this.blockstore = components.blockstore;
|
|
18
|
+
this.network = components.network;
|
|
19
|
+
this.wants = new Map();
|
|
20
|
+
this.exchangeCount = 0;
|
|
21
|
+
this.bytesSent = 0;
|
|
22
|
+
this.bytesReceived = 0;
|
|
23
|
+
this.maxSizeReplaceHasWithBlock = init.maxSizeReplaceHasWithBlock ?? DEFAULT_MAX_SIZE_REPLACE_HAS_WITH_BLOCK;
|
|
24
|
+
}
|
|
25
|
+
sentBytes(n) {
|
|
26
|
+
this.exchangeCount++;
|
|
27
|
+
this.lastExchange = (new Date()).getTime();
|
|
28
|
+
this.bytesSent += n;
|
|
29
|
+
}
|
|
30
|
+
receivedBytes(n) {
|
|
31
|
+
this.exchangeCount++;
|
|
32
|
+
this.lastExchange = (new Date()).getTime();
|
|
33
|
+
this.bytesReceived += n;
|
|
34
|
+
}
|
|
35
|
+
debtRatio() {
|
|
36
|
+
return (this.bytesSent / (this.bytesReceived + 1)); // +1 is to prevent division by zero
|
|
37
|
+
}
|
|
38
|
+
async sendBlocksToPeer(options) {
|
|
39
|
+
const message = {
|
|
40
|
+
blockPresences: [],
|
|
41
|
+
blocks: []
|
|
42
|
+
};
|
|
43
|
+
const sentBlocks = new Set();
|
|
44
|
+
for (const [key, entry] of this.wants.entries()) {
|
|
45
|
+
const has = await this.blockstore.has(entry.cid, options);
|
|
46
|
+
if (!has) {
|
|
47
|
+
// we don't have the requested block and the remote is not interested
|
|
48
|
+
// in us telling them that
|
|
49
|
+
if (!entry.sendDontHave) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
// we have already told them we don't have the block
|
|
53
|
+
if (entry.sentDontHave === true) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
entry.sentDontHave = true;
|
|
57
|
+
message.blockPresences.push({
|
|
58
|
+
cid: entry.cid.bytes,
|
|
59
|
+
type: BlockPresenceType.DontHaveBlock
|
|
60
|
+
});
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const block = await this.blockstore.get(entry.cid, options);
|
|
64
|
+
// do they want the block or just us to tell them we have the block
|
|
65
|
+
if (entry.wantType === WantType.WantHave) {
|
|
66
|
+
if (block.byteLength < this.maxSizeReplaceHasWithBlock) {
|
|
67
|
+
// if the block is small we just send it to them
|
|
68
|
+
sentBlocks.add(key);
|
|
69
|
+
message.blocks.push({
|
|
70
|
+
data: block,
|
|
71
|
+
prefix: cidToPrefix(entry.cid)
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// otherwise tell them we have the block
|
|
76
|
+
message.blockPresences.push({
|
|
77
|
+
cid: entry.cid.bytes,
|
|
78
|
+
type: BlockPresenceType.HaveBlock
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// they want the block, send it to them
|
|
84
|
+
sentBlocks.add(key);
|
|
85
|
+
message.blocks.push({
|
|
86
|
+
data: block,
|
|
87
|
+
prefix: cidToPrefix(entry.cid)
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// only send the message if we actually have something to send
|
|
92
|
+
if (message.blocks.length > 0 || message.blockPresences.length > 0) {
|
|
93
|
+
await this.network.sendMessage(this.peerId, message, options);
|
|
94
|
+
// update accounting
|
|
95
|
+
this.sentBytes(message.blocks.reduce((acc, curr) => acc + curr.data.byteLength, 0));
|
|
96
|
+
// remove sent blocks from local copy of their want list - they can still
|
|
97
|
+
// re-request if required
|
|
98
|
+
for (const key of sentBlocks) {
|
|
99
|
+
this.wants.delete(key);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../src/peer-want-lists/ledger.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,OAAO,EAAE,uCAAuC,EAAE,MAAM,iBAAiB,CAAA;AACzE,OAAO,EAAE,iBAAiB,EAAuB,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AA6CpD,MAAM,OAAO,MAAM;IACV,MAAM,CAAQ;IACJ,UAAU,CAAY;IACtB,OAAO,CAAS;IAC1B,KAAK,CAAgC;IACrC,aAAa,CAAQ;IACrB,SAAS,CAAQ;IACjB,aAAa,CAAQ;IACrB,YAAY,CAAS;IACX,0BAA0B,CAAQ;IAEnD,YAAa,UAA4B,EAAE,IAAgB;QACzD,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,UAAU,CAAA;QACvC,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,OAAO,CAAA;QACjC,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAA;QAEtB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAA;QAClB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,0BAA0B,GAAG,IAAI,CAAC,0BAA0B,IAAI,uCAAuC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,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;YAE7D,oBAAoB;YACpB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;YAEnF,yEAAyE;YACzE,yBAAyB;YACzB,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;gBAC7B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACxB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BitswapSession as BitswapSessionInterface } from './index.js';
|
|
2
|
+
import type { Network } from './network.js';
|
|
3
|
+
import type { WantList } from './want-list.js';
|
|
4
|
+
import type { ComponentLogger, PeerId } from '@libp2p/interface';
|
|
5
|
+
import type { AbortOptions } from 'interface-store';
|
|
6
|
+
import type { CID } from 'multiformats/cid';
|
|
7
|
+
export interface BitswapSessionComponents {
|
|
8
|
+
network: Network;
|
|
9
|
+
wantList: WantList;
|
|
10
|
+
logger: ComponentLogger;
|
|
11
|
+
}
|
|
12
|
+
export interface BitswapSessionInit extends AbortOptions {
|
|
13
|
+
root: CID;
|
|
14
|
+
queryConcurrency: number;
|
|
15
|
+
minProviders: number;
|
|
16
|
+
maxProviders: number;
|
|
17
|
+
connectedPeers: PeerId[];
|
|
18
|
+
}
|
|
19
|
+
export declare function createBitswapSession(components: BitswapSessionComponents, init: BitswapSessionInit): Promise<BitswapSessionInterface>;
|
|
20
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAA6B,cAAc,IAAI,uBAAuB,EAAE,MAAM,YAAY,CAAA;AACtG,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAG3C,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,QAAQ,CAAA;IAClB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,kBAAmB,SAAQ,YAAY;IACtD,IAAI,EAAE,GAAG,CAAA;IACT,gBAAgB,EAAE,MAAM,CAAA;IACxB,YAAY,EAAE,MAAM,CAAA;IACpB,YAAY,EAAE,MAAM,CAAA;IACpB,cAAc,EAAE,MAAM,EAAE,CAAA;CACzB;AAmHD,wBAAsB,oBAAoB,CAAE,UAAU,EAAE,wBAAwB,EAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAQ5I"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface';
|
|
2
|
+
import { PeerSet } from '@libp2p/peer-collections';
|
|
3
|
+
import { PeerQueue } from '@libp2p/utils/peer-queue';
|
|
4
|
+
import map from 'it-map';
|
|
5
|
+
import merge from 'it-merge';
|
|
6
|
+
import pDefer, {} from 'p-defer';
|
|
7
|
+
class BitswapSession {
|
|
8
|
+
root;
|
|
9
|
+
peers;
|
|
10
|
+
log;
|
|
11
|
+
wantList;
|
|
12
|
+
network;
|
|
13
|
+
queue;
|
|
14
|
+
maxProviders;
|
|
15
|
+
constructor(components, init) {
|
|
16
|
+
this.peers = new PeerSet();
|
|
17
|
+
this.root = init.root;
|
|
18
|
+
this.maxProviders = init.maxProviders;
|
|
19
|
+
this.log = components.logger.forComponent(`helia:bitswap:session:${init.root}`);
|
|
20
|
+
this.wantList = components.wantList;
|
|
21
|
+
this.network = components.network;
|
|
22
|
+
this.queue = new PeerQueue({
|
|
23
|
+
concurrency: init.queryConcurrency
|
|
24
|
+
});
|
|
25
|
+
this.queue.addEventListener('error', (evt) => {
|
|
26
|
+
this.log.error('error querying peer for %c', this.root, evt.detail);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async want(cid, options = {}) {
|
|
30
|
+
if (this.peers.size === 0) {
|
|
31
|
+
throw new CodeError('Bitswap session had no peers', 'ERR_NO_SESSION_PEERS');
|
|
32
|
+
}
|
|
33
|
+
this.log('sending WANT-BLOCK for %c to', cid, this.peers);
|
|
34
|
+
const result = await Promise.any([...this.peers].map(async (peerId) => {
|
|
35
|
+
return this.wantList.wantBlock(cid, {
|
|
36
|
+
peerId,
|
|
37
|
+
...options
|
|
38
|
+
});
|
|
39
|
+
}));
|
|
40
|
+
this.log('received block for %c from %p', cid, result.sender);
|
|
41
|
+
// TODO findNewProviders when promise.any throws aggregate error and signal
|
|
42
|
+
// is not aborted
|
|
43
|
+
return result.block;
|
|
44
|
+
}
|
|
45
|
+
async findNewProviders(cid, count, options = {}) {
|
|
46
|
+
const deferred = pDefer();
|
|
47
|
+
let found = 0;
|
|
48
|
+
this.log('find %d-%d new provider(s) for %c', count, this.maxProviders, cid);
|
|
49
|
+
const source = merge([...this.wantList.peers.keys()], map(this.network.findProviders(cid, options), prov => prov.id));
|
|
50
|
+
void Promise.resolve()
|
|
51
|
+
.then(async () => {
|
|
52
|
+
for await (const peerId of source) {
|
|
53
|
+
// eslint-disable-next-line no-loop-func
|
|
54
|
+
await this.queue.add(async () => {
|
|
55
|
+
try {
|
|
56
|
+
this.log('asking potential session peer %p if they have %c', peerId, cid);
|
|
57
|
+
const result = await this.wantList.wantPresence(cid, {
|
|
58
|
+
peerId,
|
|
59
|
+
...options
|
|
60
|
+
});
|
|
61
|
+
if (!result.has) {
|
|
62
|
+
this.log('potential session peer %p did not have %c', peerId, cid);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.log('potential session peer %p had %c', peerId, cid);
|
|
66
|
+
found++;
|
|
67
|
+
// add to list
|
|
68
|
+
this.peers.add(peerId);
|
|
69
|
+
if (found === count) {
|
|
70
|
+
this.log('found %d session peers', found);
|
|
71
|
+
deferred.resolve();
|
|
72
|
+
}
|
|
73
|
+
if (found === this.maxProviders) {
|
|
74
|
+
this.log('found max provider session peers', found);
|
|
75
|
+
this.queue.clear();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
this.log.error('error querying potential session peer %p for %c', peerId, cid, err.errors ?? err);
|
|
80
|
+
}
|
|
81
|
+
}, {
|
|
82
|
+
peerId
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
this.log('found %d session peers total', found);
|
|
86
|
+
if (count > 0) {
|
|
87
|
+
deferred.reject(new CodeError(`Found ${found} of ${count} providers`, 'ERR_NO_PROVIDERS_FOUND'));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return deferred.promise;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export async function createBitswapSession(components, init) {
|
|
94
|
+
const session = new BitswapSession(components, init);
|
|
95
|
+
await session.findNewProviders(init.root, init.minProviders, {
|
|
96
|
+
signal: init.signal
|
|
97
|
+
});
|
|
98
|
+
return session;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;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AE,CAAC;gBACd,QAAQ,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC,SAAS,KAAK,OAAO,KAAK,YAAY,EAAE,wBAAwB,CAAC,CAAC,CAAA;YAClG,CAAC;QACH,CAAC,CAAC,CAAA;QAEJ,OAAO,QAAQ,CAAC,OAAO,CAAA;IACzB,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAE,UAAoC,EAAE,IAAwB;IACxG,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IAEpD,MAAM,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE;QAC3D,MAAM,EAAE,IAAI,CAAC,MAAM;KACpB,CAAC,CAAA;IAEF,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Metrics, PeerId } from '@libp2p/interface';
|
|
2
|
+
export interface StatsComponents {
|
|
3
|
+
metrics?: Metrics;
|
|
4
|
+
}
|
|
5
|
+
export declare class Stats {
|
|
6
|
+
private readonly blocksReceived?;
|
|
7
|
+
private readonly duplicateBlocksReceived?;
|
|
8
|
+
private readonly dataReceived?;
|
|
9
|
+
private readonly duplicateDataReceived?;
|
|
10
|
+
constructor(components: StatsComponents);
|
|
11
|
+
updateBlocksReceived(count?: number, peerId?: PeerId): void;
|
|
12
|
+
updateDuplicateBlocksReceived(count?: number, peerId?: PeerId): void;
|
|
13
|
+
updateDataReceived(bytes: number, peerId?: PeerId): void;
|
|
14
|
+
updateDuplicateDataReceived(bytes: number, peerId?: PeerId): void;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=stats.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stats.d.ts","sourceRoot":"","sources":["../../src/stats.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAErE,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,qBAAa,KAAK;IAChB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAa;IAC7C,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAC,CAAa;IACtD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAa;IAC3C,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAa;gBAEvC,UAAU,EAAE,eAAe;IAOxC,oBAAoB,CAAE,KAAK,GAAE,MAAU,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAY/D,6BAA6B,CAAE,KAAK,GAAE,MAAU,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAYxE,kBAAkB,CAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAYzD,2BAA2B,CAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;CAWnE"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export class Stats {
|
|
2
|
+
blocksReceived;
|
|
3
|
+
duplicateBlocksReceived;
|
|
4
|
+
dataReceived;
|
|
5
|
+
duplicateDataReceived;
|
|
6
|
+
constructor(components) {
|
|
7
|
+
this.blocksReceived = components.metrics?.registerMetricGroup('ipfs_bitswap_received_blocks');
|
|
8
|
+
this.duplicateBlocksReceived = components.metrics?.registerMetricGroup('ipfs_bitswap_duplicate_received_blocks');
|
|
9
|
+
this.dataReceived = components.metrics?.registerMetricGroup('ipfs_bitswap_data_received_bytes');
|
|
10
|
+
this.duplicateDataReceived = components.metrics?.registerMetricGroup('ipfs_bitswap_duplicate_data_received_bytes');
|
|
11
|
+
}
|
|
12
|
+
updateBlocksReceived(count = 1, peerId) {
|
|
13
|
+
const stats = {
|
|
14
|
+
global: count
|
|
15
|
+
};
|
|
16
|
+
if (peerId != null) {
|
|
17
|
+
stats[peerId.toString()] = count;
|
|
18
|
+
}
|
|
19
|
+
this.blocksReceived?.increment(stats);
|
|
20
|
+
}
|
|
21
|
+
updateDuplicateBlocksReceived(count = 1, peerId) {
|
|
22
|
+
const stats = {
|
|
23
|
+
global: count
|
|
24
|
+
};
|
|
25
|
+
if (peerId != null) {
|
|
26
|
+
stats[peerId.toString()] = count;
|
|
27
|
+
}
|
|
28
|
+
this.duplicateBlocksReceived?.increment(stats);
|
|
29
|
+
}
|
|
30
|
+
updateDataReceived(bytes, peerId) {
|
|
31
|
+
const stats = {
|
|
32
|
+
global: bytes
|
|
33
|
+
};
|
|
34
|
+
if (peerId != null) {
|
|
35
|
+
stats[peerId.toString()] = bytes;
|
|
36
|
+
}
|
|
37
|
+
this.dataReceived?.increment(stats);
|
|
38
|
+
}
|
|
39
|
+
updateDuplicateDataReceived(bytes, peerId) {
|
|
40
|
+
const stats = {
|
|
41
|
+
global: bytes
|
|
42
|
+
};
|
|
43
|
+
if (peerId != null) {
|
|
44
|
+
stats[peerId.toString()] = bytes;
|
|
45
|
+
}
|
|
46
|
+
this.duplicateDataReceived?.increment(stats);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=stats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stats.js","sourceRoot":"","sources":["../../src/stats.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,KAAK;IACC,cAAc,CAAc;IAC5B,uBAAuB,CAAc;IACrC,YAAY,CAAc;IAC1B,qBAAqB,CAAc;IAEpD,YAAa,UAA2B;QACtC,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,OAAO,EAAE,mBAAmB,CAAC,8BAA8B,CAAC,CAAA;QAC7F,IAAI,CAAC,uBAAuB,GAAG,UAAU,CAAC,OAAO,EAAE,mBAAmB,CAAC,wCAAwC,CAAC,CAAA;QAChH,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,OAAO,EAAE,mBA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C,CAAC;QAED,IAAI,CAAC,qBAAqB,EAAE,SAAS,CAAC,KAAK,CAAC,CAAA;IAC9C,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cid-prefix.d.ts","sourceRoot":"","sources":["../../../src/utils/cid-prefix.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,wBAAgB,WAAW,CAAE,GAAG,EAAE,GAAG,GAAG,UAAU,CAIjD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cid-prefix.js","sourceRoot":"","sources":["../../../src/utils/cid-prefix.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAGpC,MAAM,UAAU,WAAW,CAAE,GAAQ;IACnC,OAAO,EAAE,CAAC;QACR,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varint-decoder.d.ts","sourceRoot":"","sources":["../../../src/utils/varint-decoder.ts"],"names":[],"mappings":"AAEA,iBAAS,aAAa,CAAE,GAAG,EAAE,UAAU,GAAG,MAAM,EAAE,CAcjD;AAED,eAAe,aAAa,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { decode, encodingLength } from 'uint8-varint';
|
|
2
|
+
function varintDecoder(buf) {
|
|
3
|
+
if (!(buf instanceof Uint8Array)) {
|
|
4
|
+
throw new Error('arg needs to be a Uint8Array');
|
|
5
|
+
}
|
|
6
|
+
const result = [];
|
|
7
|
+
while (buf.length > 0) {
|
|
8
|
+
const num = decode(buf);
|
|
9
|
+
result.push(num);
|
|
10
|
+
buf = buf.slice(encodingLength(num));
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
export default varintDecoder;
|
|
15
|
+
//# sourceMappingURL=varint-decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varint-decoder.js","sourceRoot":"","sources":["../../../src/utils/varint-decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAErD,SAAS,aAAa,CAAE,GAAe;IACrC,IAAI,CAAC,CAAC,GAAG,YAAY,UAAU,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;IACjD,CAAC;IAED,MAAM,MAAM,GAAa,EAAE,CAAA;IAE3B,OAAO,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QACvB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAChB,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,eAAe,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varint-encoder.d.ts","sourceRoot":"","sources":["../../../src/utils/varint-encoder.ts"],"names":[],"mappings":"AAEA,iBAAS,aAAa,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,UAAU,CAajD;AAED,eAAe,aAAa,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { encode, encodingLength } from 'uint8-varint';
|
|
2
|
+
function varintEncoder(buf) {
|
|
3
|
+
let out = new Uint8Array(buf.reduce((acc, curr) => {
|
|
4
|
+
return acc + encodingLength(curr);
|
|
5
|
+
}, 0));
|
|
6
|
+
let offset = 0;
|
|
7
|
+
for (const num of buf) {
|
|
8
|
+
out = encode(num, out, offset);
|
|
9
|
+
offset += encodingLength(num);
|
|
10
|
+
}
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
export default varintEncoder;
|
|
14
|
+
//# sourceMappingURL=varint-encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"varint-encoder.js","sourceRoot":"","sources":["../../../src/utils/varint-encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAErD,SAAS,aAAa,CAAE,GAAa;IACnC,IAAI,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QAChD,OAAO,GAAG,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACN,IAAI,MAAM,GAAG,CAAC,CAAA;IAEd,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,GAAG,GAAG,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;QAE9B,MAAM,IAAI,cAAc,CAAC,GAAG,CAAC,CAAA;IAC/B,CAAC;IAED,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,eAAe,aAAa,CAAA"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { PeerSet } from '@libp2p/peer-collections';
|
|
2
|
+
import { CID } from 'multiformats/cid';
|
|
3
|
+
import { WantType } from './pb/message.js';
|
|
4
|
+
import type { MultihashHasherLoader } from './index.js';
|
|
5
|
+
import type { BitswapNetworkWantProgressEvents, Network } from './network.js';
|
|
6
|
+
import type { ComponentLogger, Metrics, PeerId, Startable, AbortOptions } from '@libp2p/interface';
|
|
7
|
+
import type { PeerMap } from '@libp2p/peer-collections';
|
|
8
|
+
import type { DeferredPromise } from 'p-defer';
|
|
9
|
+
import type { ProgressOptions } from 'progress-events';
|
|
10
|
+
export interface WantListComponents {
|
|
11
|
+
network: Network;
|
|
12
|
+
logger: ComponentLogger;
|
|
13
|
+
metrics?: Metrics;
|
|
14
|
+
}
|
|
15
|
+
export interface WantListInit {
|
|
16
|
+
sendMessagesDelay?: number;
|
|
17
|
+
hashLoader?: MultihashHasherLoader;
|
|
18
|
+
}
|
|
19
|
+
export interface WantListEntry {
|
|
20
|
+
/**
|
|
21
|
+
* The CID we send to the remote
|
|
22
|
+
*/
|
|
23
|
+
cid: CID;
|
|
24
|
+
/**
|
|
25
|
+
* The priority with which the remote should return the block
|
|
26
|
+
*/
|
|
27
|
+
priority: number;
|
|
28
|
+
/**
|
|
29
|
+
* If we want the block or if we want the remote to tell us if they have the
|
|
30
|
+
* block - note if the block is small they'll send it to us anyway.
|
|
31
|
+
*/
|
|
32
|
+
wantType: WantType;
|
|
33
|
+
/**
|
|
34
|
+
* Whether we are cancelling the block want or not
|
|
35
|
+
*/
|
|
36
|
+
cancel: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Whether the remote should tell us if they have the block or not
|
|
39
|
+
*/
|
|
40
|
+
sendDontHave: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* If this set has members, the want will only be sent to these peers
|
|
43
|
+
*/
|
|
44
|
+
session: PeerSet;
|
|
45
|
+
/**
|
|
46
|
+
* Promises returned from `.wantBlock` for this block
|
|
47
|
+
*/
|
|
48
|
+
blockWantListeners: Array<DeferredPromise<WantBlockResult>>;
|
|
49
|
+
/**
|
|
50
|
+
* Promises returned from `.wantPresence` for this block
|
|
51
|
+
*/
|
|
52
|
+
blockPresenceListeners: Array<DeferredPromise<WantPresenceResult>>;
|
|
53
|
+
}
|
|
54
|
+
export interface WantOptions extends AbortOptions, ProgressOptions<BitswapNetworkWantProgressEvents> {
|
|
55
|
+
/**
|
|
56
|
+
* If set, this WantList entry will only be sent to this peer
|
|
57
|
+
*/
|
|
58
|
+
peerId?: PeerId;
|
|
59
|
+
/**
|
|
60
|
+
* Allow prioritising blocks
|
|
61
|
+
*/
|
|
62
|
+
priority?: number;
|
|
63
|
+
}
|
|
64
|
+
export interface WantBlockResult {
|
|
65
|
+
sender: PeerId;
|
|
66
|
+
cid: CID;
|
|
67
|
+
block: Uint8Array;
|
|
68
|
+
}
|
|
69
|
+
export interface WantDontHaveResult {
|
|
70
|
+
sender: PeerId;
|
|
71
|
+
cid: CID;
|
|
72
|
+
has: false;
|
|
73
|
+
}
|
|
74
|
+
export interface WantHaveResult {
|
|
75
|
+
sender: PeerId;
|
|
76
|
+
cid: CID;
|
|
77
|
+
has: true;
|
|
78
|
+
block?: Uint8Array;
|
|
79
|
+
}
|
|
80
|
+
export type WantPresenceResult = WantDontHaveResult | WantHaveResult;
|
|
81
|
+
export declare class WantList implements Startable {
|
|
82
|
+
/**
|
|
83
|
+
* Tracks what CIDs we've previously sent to which peers
|
|
84
|
+
*/
|
|
85
|
+
readonly peers: PeerMap<Set<string>>;
|
|
86
|
+
readonly wants: Map<string, WantListEntry>;
|
|
87
|
+
private readonly network;
|
|
88
|
+
private readonly log;
|
|
89
|
+
private readonly sendMessagesDelay;
|
|
90
|
+
private sendMessagesTimeout?;
|
|
91
|
+
private readonly hashLoader?;
|
|
92
|
+
constructor(components: WantListComponents, init?: WantListInit);
|
|
93
|
+
private addEntry;
|
|
94
|
+
private sendMessages;
|
|
95
|
+
has(cid: CID): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Add a CID to the wantlist
|
|
98
|
+
*/
|
|
99
|
+
wantPresence(cid: CID, options?: WantOptions): Promise<WantPresenceResult>;
|
|
100
|
+
/**
|
|
101
|
+
* Add a CID to the wantlist
|
|
102
|
+
*/
|
|
103
|
+
wantBlock(cid: CID, options?: WantOptions): Promise<WantBlockResult>;
|
|
104
|
+
/**
|
|
105
|
+
* Invoked when a message is received from a bitswap peer
|
|
106
|
+
*/
|
|
107
|
+
private receiveMessage;
|
|
108
|
+
/**
|
|
109
|
+
* Invoked when the network topology notices a new peer that supports Bitswap
|
|
110
|
+
*/
|
|
111
|
+
peerConnected(peerId: PeerId): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Invoked when the network topology notices peer that supports Bitswap has
|
|
114
|
+
* disconnected
|
|
115
|
+
*/
|
|
116
|
+
peerDisconnected(peerId: PeerId): void;
|
|
117
|
+
start(): void;
|
|
118
|
+
stop(): void;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=want-list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"want-list.d.ts","sourceRoot":"","sources":["../../src/want-list.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAMlE,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAKtC,OAAO,EAAqB,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAE7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AACvD,OAAO,KAAK,EAAE,gCAAgC,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAElG,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,UAAU,CAAC,EAAE,qBAAqB,CAAA;CACnC;AAED,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,GAAG,EAAE,GAAG,CAAA;IAER;;OAEG;IACH,QAAQ,EAAE,MAAM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