@libp2p/kad-dht 12.0.14 → 12.0.15-43046b9ae
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/dist/index.min.js +4 -4
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +1 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +3 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/index.d.ts +34 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +3 -19
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/network.d.ts +3 -0
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +33 -8
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-list/peer-distance-list.d.ts +13 -4
- package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
- package/dist/src/peer-list/peer-distance-list.js +29 -21
- package/dist/src/peer-list/peer-distance-list.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +5 -5
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +16 -25
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/query/manager.d.ts +3 -0
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +14 -5
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query/query-path.d.ts +6 -6
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/query-path.js +32 -20
- package/dist/src/query/query-path.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +11 -5
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +84 -42
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +80 -115
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -1
- package/dist/src/routing-table/k-bucket.js +165 -311
- package/dist/src/routing-table/k-bucket.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts +1 -1
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js +10 -5
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/handlers/find-node.d.ts +3 -0
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/find-node.js +10 -1
- package/dist/src/rpc/handlers/find-node.js.map +1 -1
- package/package.json +14 -16
- package/src/constants.ts +1 -1
- package/src/content-routing/index.ts +3 -2
- package/src/index.ts +37 -1
- package/src/kad-dht.ts +19 -21
- package/src/network.ts +38 -9
- package/src/peer-list/peer-distance-list.ts +36 -25
- package/src/peer-routing/index.ts +20 -29
- package/src/query/manager.ts +18 -5
- package/src/query/query-path.ts +46 -30
- package/src/routing-table/index.ts +100 -46
- package/src/routing-table/k-bucket.ts +214 -359
- package/src/routing-table/refresh.ts +11 -5
- package/src/rpc/handlers/find-node.ts +13 -1
- package/dist/src/query/utils.d.ts +0 -6
- package/dist/src/query/utils.d.ts.map +0 -1
- package/dist/src/query/utils.js +0 -53
- package/dist/src/query/utils.js.map +0 -1
- package/dist/typedoc-urls.json +0 -55
- package/src/query/utils.ts +0 -64
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refresh.d.ts","sourceRoot":"","sources":["../../../src/routing-table/refresh.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"refresh.d.ts","sourceRoot":"","sources":["../../../src/routing-table/refresh.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAOxE,MAAM,WAAW,6BAA6B;IAC5C,MA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MAAM,GAAG,MAAM;IAY9C;;OAEG;IACD,cAAc,IAAK,SAAS,CAAC,MAAM,CAAC;CAoBvC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomBytes } from '@libp2p/crypto';
|
|
2
|
+
import { setMaxListeners } from '@libp2p/interface';
|
|
2
3
|
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
3
4
|
import length from 'it-length';
|
|
4
5
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
@@ -31,7 +32,7 @@ export class RoutingTableRefresh {
|
|
|
31
32
|
this.commonPrefixLengthRefreshedAt = [];
|
|
32
33
|
this.refreshTable = this.refreshTable.bind(this);
|
|
33
34
|
}
|
|
34
|
-
async
|
|
35
|
+
async afterStart() {
|
|
35
36
|
this.log(`refreshing routing table every ${this.refreshInterval}ms`);
|
|
36
37
|
this.refreshTable(true);
|
|
37
38
|
}
|
|
@@ -104,7 +105,11 @@ export class RoutingTableRefresh {
|
|
|
104
105
|
// gen a key for the query to refresh the cpl
|
|
105
106
|
const peerId = await this._generateRandomPeerId(cpl);
|
|
106
107
|
this.log('starting refreshing cpl %s with key %p (routing table size was %s)', cpl, peerId, this.routingTable.size);
|
|
107
|
-
const
|
|
108
|
+
const signal = AbortSignal.timeout(this.refreshQueryTimeout);
|
|
109
|
+
setMaxListeners(Infinity, signal);
|
|
110
|
+
const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), {
|
|
111
|
+
signal
|
|
112
|
+
}));
|
|
108
113
|
this.log(`found ${peers} peers that were close to imaginary peer %p`, peerId);
|
|
109
114
|
this.log('finished refreshing cpl %s with key %p (routing table size is now %s)', cpl, peerId, this.routingTable.size);
|
|
110
115
|
}
|
|
@@ -125,7 +130,7 @@ export class RoutingTableRefresh {
|
|
|
125
130
|
}
|
|
126
131
|
const randomData = randomBytes(2);
|
|
127
132
|
const randomUint16 = (randomData[1] << 8) + randomData[0];
|
|
128
|
-
const key = await this._makePeerId(this.routingTable.kb.
|
|
133
|
+
const key = await this._makePeerId(this.routingTable.kb.localPeer.kadId, randomUint16, targetCommonPrefixLength);
|
|
129
134
|
return peerIdFromBytes(key);
|
|
130
135
|
}
|
|
131
136
|
async _makePeerId(localKadId, randomPrefix, targetCommonPrefixLength) {
|
|
@@ -185,8 +190,8 @@ export class RoutingTableRefresh {
|
|
|
185
190
|
if (this.routingTable.kb == null) {
|
|
186
191
|
return;
|
|
187
192
|
}
|
|
188
|
-
for (const {
|
|
189
|
-
const distance = uint8ArrayXor(this.routingTable.kb.
|
|
193
|
+
for (const { kadId } of this.routingTable.kb.toIterable()) {
|
|
194
|
+
const distance = uint8ArrayXor(this.routingTable.kb.localPeer.kadId, kadId);
|
|
190
195
|
let leadingZeros = 0;
|
|
191
196
|
for (const byte of distance) {
|
|
192
197
|
if (byte === 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refresh.js","sourceRoot":"","sources":["../../../src/routing-table/refresh.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAA;AACrF,OAAO,kBAAkB,MAAM,4BAA4B,CAAA;AAK3D;;GAEG;AACH,MAAM,wBAAwB,GAAG,EAAE,CAAA;AAcnC;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IACb,GAAG,CAAQ;IACX,WAAW,CAAa;IACxB,YAAY,CAAc;IAC1B,eAAe,CAAQ;IACvB,mBAAmB,CAAQ;IAC3B,6BAA6B,CAAQ;IAC9C,gBAAgB,CAAgC;IAExD,YAAa,UAAyC,EAAE,IAA6B;Q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AAC,CAAA;QAEpB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC;YAC3C,IAAI,MAAM,GAAG,YAAY,EAAE,CAAC;gBAC1B,YAAY,GAAG,MAAM,CAAA;YACvB,CAAC;QACH,CAAC;QAED,OAAO,YAAY,CAAA;IACrB,CAAC;IAED;;OAEG;IACH,eAAe,CAAE,YAAoB;QACnC,IAAI,KAAK,GAAG,CAAC,CAAA;QAEb,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC;YAC3C,IAAI,MAAM,KAAK,YAAY,EAAE,CAAC;gBAC5B,KAAK,EAAE,CAAA;YACT,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;OAEG;IACH,CAAE,cAAc;QACd,IAAI,IAAI,CAAC,YAAY,CAAC,EAAE,IAAI,IAAI,EAAE,CAAC;YACjC,OAAM;QACR,CAAC;QAED,KAAK,MAAM,EAAE,KAAK,EAAE,IAAI,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC,UAAU,EAAE,EAAE,CAAC;YAC1D,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;YAC3E,IAAI,YAAY,GAAG,CAAC,CAAA;YAEpB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;gBAC5B,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;oBACf,YAAY,EAAE,CAAA;gBAChB,CAAC;qBAAM,CAAC;oBACN,MAAK;gBACP,CAAC;YACH,CAAC;YAED,MAAM,YAAY,CAAA;QACpB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -3,6 +3,7 @@ import type { Message } from '../../message/dht.js';
|
|
|
3
3
|
import type { PeerRouting } from '../../peer-routing/index.js';
|
|
4
4
|
import type { DHTMessageHandler } from '../index.js';
|
|
5
5
|
import type { ComponentLogger, PeerId } from '@libp2p/interface';
|
|
6
|
+
import type { AddressManager } from '@libp2p/interface-internal';
|
|
6
7
|
export interface FindNodeHandlerInit {
|
|
7
8
|
peerRouting: PeerRouting;
|
|
8
9
|
logPrefix: string;
|
|
@@ -10,12 +11,14 @@ export interface FindNodeHandlerInit {
|
|
|
10
11
|
}
|
|
11
12
|
export interface FindNodeHandlerComponents {
|
|
12
13
|
peerId: PeerId;
|
|
14
|
+
addressManager: AddressManager;
|
|
13
15
|
logger: ComponentLogger;
|
|
14
16
|
}
|
|
15
17
|
export declare class FindNodeHandler implements DHTMessageHandler {
|
|
16
18
|
private readonly peerRouting;
|
|
17
19
|
private readonly peerInfoMapper;
|
|
18
20
|
private readonly peerId;
|
|
21
|
+
private readonly addressManager;
|
|
19
22
|
private readonly log;
|
|
20
23
|
constructor(components: FindNodeHandlerComponents, init: FindNodeHandlerInit);
|
|
21
24
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-node.d.ts","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"find-node.d.ts","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,EAAY,MAAM,mBAAmB,CAAA;AAClF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAEhE,MAAM,WAAW,mBAAmB;IAClC,WAAW,EAAE,WAAW,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,MAAM,CAAA;IACd,cAAc,EAAE,cAAc,CAAA;IAC9B,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,qBAAa,eAAgB,YAAW,iBAAiB;IACvD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,UAAU,EAAE,yBAAyB,EAAE,IAAI,EAAE,mBAAmB;IAU7E;;OAEG;IACG,MAAM,CAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAmC9D"}
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface';
|
|
2
|
+
import { protocols } from '@multiformats/multiaddr';
|
|
3
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays';
|
|
2
4
|
import { MessageType } from '../../message/dht.js';
|
|
3
5
|
export class FindNodeHandler {
|
|
4
6
|
peerRouting;
|
|
5
7
|
peerInfoMapper;
|
|
6
8
|
peerId;
|
|
9
|
+
addressManager;
|
|
7
10
|
log;
|
|
8
11
|
constructor(components, init) {
|
|
9
12
|
const { peerRouting, logPrefix } = init;
|
|
10
13
|
this.log = components.logger.forComponent(`${logPrefix}:rpc:handlers:find-node`);
|
|
11
14
|
this.peerId = components.peerId;
|
|
15
|
+
this.addressManager = components.addressManager;
|
|
12
16
|
this.peerRouting = peerRouting;
|
|
13
17
|
this.peerInfoMapper = init.peerInfoMapper;
|
|
14
18
|
}
|
|
@@ -21,13 +25,18 @@ export class FindNodeHandler {
|
|
|
21
25
|
throw new CodeError('Invalid FIND_NODE message received - key was missing', 'ERR_INVALID_MESSAGE');
|
|
22
26
|
}
|
|
23
27
|
const closer = await this.peerRouting.getCloserPeersOffline(msg.key, peerId);
|
|
28
|
+
if (uint8ArrayEquals(this.peerId.toBytes(), msg.key)) {
|
|
29
|
+
closer.push({
|
|
30
|
+
id: this.peerId,
|
|
31
|
+
multiaddrs: this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
32
|
+
});
|
|
33
|
+
}
|
|
24
34
|
const response = {
|
|
25
35
|
type: MessageType.FIND_NODE,
|
|
26
36
|
clusterLevel: msg.clusterLevel,
|
|
27
37
|
closer: closer
|
|
28
38
|
.map(this.peerInfoMapper)
|
|
29
39
|
.filter(({ multiaddrs }) => multiaddrs.length)
|
|
30
|
-
.filter(({ id }) => !id.equals(this.peerId))
|
|
31
40
|
.map(peerInfo => ({
|
|
32
41
|
id: peerInfo.id.toBytes(),
|
|
33
42
|
multiaddrs: peerInfo.multiaddrs.map(ma => ma.bytes)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-node.js","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"find-node.js","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAoBlD,MAAM,OAAO,eAAe;IACT,WAAW,CAAa;IACxB,cAAc,CAAgB;IAC9B,MAAM,CAAQ;IACd,cAAc,CAAgB;IAC9B,GAAG,CAAQ;IAE5B,YAAa,UAAqC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,GAAG,CAAC,YAAY;YAC9B,MAAM,EAAE,MAAM;iBACX,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC;iBACxB,MAAM,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC;iBAC7C,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;gBAChB,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE;gBACzB,UAAU,EAAE,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC;aACpD,CAAC,CAAC;YACL,SAAS,EAAE,EAAE;SACd,CAAA;QAED,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,+CAA+C,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QAC5E,CAAC;QAED,OAAO,QAAQ,CAAA;IACjB,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.15-43046b9ae",
|
|
4
4
|
"description": "JavaScript implementation of the Kad-DHT for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht#readme",
|
|
@@ -57,13 +57,13 @@
|
|
|
57
57
|
"doc-check": "aegir doc-check"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@libp2p/crypto": "
|
|
61
|
-
"@libp2p/interface": "
|
|
62
|
-
"@libp2p/interface-internal": "
|
|
63
|
-
"@libp2p/peer-collections": "
|
|
64
|
-
"@libp2p/peer-id": "
|
|
65
|
-
"@libp2p/record": "
|
|
66
|
-
"@libp2p/utils": "
|
|
60
|
+
"@libp2p/crypto": "4.1.1-43046b9ae",
|
|
61
|
+
"@libp2p/interface": "1.3.1-43046b9ae",
|
|
62
|
+
"@libp2p/interface-internal": "1.2.0-43046b9ae",
|
|
63
|
+
"@libp2p/peer-collections": "5.2.0-43046b9ae",
|
|
64
|
+
"@libp2p/peer-id": "4.1.1-43046b9ae",
|
|
65
|
+
"@libp2p/record": "4.0.1-43046b9ae",
|
|
66
|
+
"@libp2p/utils": "5.4.0-43046b9ae",
|
|
67
67
|
"@multiformats/multiaddr": "^12.2.1",
|
|
68
68
|
"any-signal": "^4.1.1",
|
|
69
69
|
"hashlru": "^2.3.0",
|
|
@@ -76,7 +76,6 @@
|
|
|
76
76
|
"it-parallel": "^3.0.6",
|
|
77
77
|
"it-pipe": "^3.0.1",
|
|
78
78
|
"it-protobuf-stream": "^1.1.2",
|
|
79
|
-
"it-pushable": "^3.2.3",
|
|
80
79
|
"it-take": "^3.0.4",
|
|
81
80
|
"multiformats": "^13.1.0",
|
|
82
81
|
"p-defer": "^4.0.1",
|
|
@@ -87,13 +86,13 @@
|
|
|
87
86
|
"race-signal": "^1.0.2",
|
|
88
87
|
"uint8-varint": "^2.0.4",
|
|
89
88
|
"uint8arraylist": "^2.4.8",
|
|
90
|
-
"uint8arrays": "^5.0
|
|
89
|
+
"uint8arrays": "^5.1.0"
|
|
91
90
|
},
|
|
92
91
|
"devDependencies": {
|
|
93
|
-
"@libp2p/interface-compliance-tests": "
|
|
94
|
-
"@libp2p/logger": "
|
|
95
|
-
"@libp2p/peer-id-factory": "
|
|
96
|
-
"@libp2p/peer-store": "
|
|
92
|
+
"@libp2p/interface-compliance-tests": "5.4.3-43046b9ae",
|
|
93
|
+
"@libp2p/logger": "4.0.12-43046b9ae",
|
|
94
|
+
"@libp2p/peer-id-factory": "4.1.1-43046b9ae",
|
|
95
|
+
"@libp2p/peer-store": "10.0.17-43046b9ae",
|
|
97
96
|
"@types/lodash.random": "^3.2.9",
|
|
98
97
|
"@types/lodash.range": "^3.2.9",
|
|
99
98
|
"@types/sinon": "^17.0.3",
|
|
@@ -102,7 +101,7 @@
|
|
|
102
101
|
"datastore-core": "^9.2.9",
|
|
103
102
|
"datastore-level": "^10.1.7",
|
|
104
103
|
"delay": "^6.0.0",
|
|
105
|
-
"execa": "^
|
|
104
|
+
"execa": "^9.0.2",
|
|
106
105
|
"it-all": "^3.0.4",
|
|
107
106
|
"it-filter": "^3.0.4",
|
|
108
107
|
"it-last": "^3.0.4",
|
|
@@ -115,7 +114,6 @@
|
|
|
115
114
|
"protons": "^7.5.0",
|
|
116
115
|
"sinon": "^17.0.1",
|
|
117
116
|
"sinon-ts": "^2.0.0",
|
|
118
|
-
"wherearewe": "^2.0.1",
|
|
119
117
|
"which": "^4.0.0"
|
|
120
118
|
},
|
|
121
119
|
"browser": {
|
package/src/constants.ts
CHANGED
|
@@ -50,4 +50,4 @@ export const TABLE_REFRESH_INTERVAL = 5 * minute
|
|
|
50
50
|
export const TABLE_REFRESH_QUERY_TIMEOUT = 30 * second
|
|
51
51
|
|
|
52
52
|
// When a timeout is not specified, run a query for this long
|
|
53
|
-
export const DEFAULT_QUERY_TIMEOUT =
|
|
53
|
+
export const DEFAULT_QUERY_TIMEOUT = 180 * second
|
|
@@ -58,13 +58,14 @@ export class ContentRouting {
|
|
|
58
58
|
*/
|
|
59
59
|
async * provide (key: CID, multiaddrs: Multiaddr[], options: RoutingOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
|
|
60
60
|
this.log('provide %s', key)
|
|
61
|
+
const target = key.multihash.bytes
|
|
61
62
|
|
|
62
63
|
// Add peer as provider
|
|
63
64
|
await this.providers.addProvider(key, this.components.peerId)
|
|
64
65
|
|
|
65
66
|
const msg: Partial<Message> = {
|
|
66
67
|
type: MessageType.ADD_PROVIDER,
|
|
67
|
-
key:
|
|
68
|
+
key: target,
|
|
68
69
|
providers: [
|
|
69
70
|
toPbPeerInfo({
|
|
70
71
|
id: this.components.peerId,
|
|
@@ -107,7 +108,7 @@ export class ContentRouting {
|
|
|
107
108
|
|
|
108
109
|
// Notify closest peers
|
|
109
110
|
yield * pipe(
|
|
110
|
-
this.peerRouting.getClosestPeers(
|
|
111
|
+
this.peerRouting.getClosestPeers(target, options),
|
|
111
112
|
(source) => map(source, (event) => maybeNotifyPeer(event)),
|
|
112
113
|
(source) => parallel(source, {
|
|
113
114
|
ordered: false,
|
package/src/index.ts
CHANGED
|
@@ -86,6 +86,7 @@ import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughM
|
|
|
86
86
|
import type { ProvidersInit } from './providers.js'
|
|
87
87
|
import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
|
|
88
88
|
import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
89
|
+
import type { AdaptiveTimeoutInit } from '@libp2p/utils/src/adaptive-timeout.js'
|
|
89
90
|
import type { Datastore } from 'interface-datastore'
|
|
90
91
|
import type { CID } from 'multiformats/cid'
|
|
91
92
|
import type { ProgressEvent } from 'progress-events'
|
|
@@ -300,12 +301,42 @@ export type Validators = Record<string, ValidateFn>
|
|
|
300
301
|
|
|
301
302
|
export interface KadDHTInit {
|
|
302
303
|
/**
|
|
303
|
-
* How many peers to store in each kBucket
|
|
304
|
+
* How many peers to store in each kBucket. Once there are more than this
|
|
305
|
+
* number of peers for a given prefix in a kBucket, the node will start to
|
|
306
|
+
* ping existing peers to see if they are still online - if they are offline
|
|
307
|
+
* they will be evicted and the new peer added.
|
|
304
308
|
*
|
|
305
309
|
* @default 20
|
|
306
310
|
*/
|
|
307
311
|
kBucketSize?: number
|
|
308
312
|
|
|
313
|
+
/**
|
|
314
|
+
* The threshold at which a kBucket will be split into two smaller kBuckets.
|
|
315
|
+
*
|
|
316
|
+
* KBuckets will not be split once the maximum trie depth is reached
|
|
317
|
+
* (controlled by the `prefixLength` option) so one can replicate go-libp2p's
|
|
318
|
+
* accelerated DHT client by (for example) setting `kBucketSize` to `Infinity`
|
|
319
|
+
* and `kBucketSplitThreshold` to 20.
|
|
320
|
+
*
|
|
321
|
+
* @default kBucketSize
|
|
322
|
+
*/
|
|
323
|
+
kBucketSplitThreshold?: number
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* How many bits of the KAD-ID of peers to use when creating the routing
|
|
327
|
+
* table.
|
|
328
|
+
*
|
|
329
|
+
* The routing table is a binary trie with peers stored in the leaf nodes. The
|
|
330
|
+
* larger this number gets, the taller the trie can grow and the more peers
|
|
331
|
+
* can be stored.
|
|
332
|
+
*
|
|
333
|
+
* Storing more peers means fewer lookups (and network operations) are needed
|
|
334
|
+
* to locate a certain peer, but also that more memory is consumed.
|
|
335
|
+
*
|
|
336
|
+
* @default 32
|
|
337
|
+
*/
|
|
338
|
+
prefixLength?: number
|
|
339
|
+
|
|
309
340
|
/**
|
|
310
341
|
* If true, only ever be a DHT client. If false, be a DHT client until told
|
|
311
342
|
* to be a DHT server via `setMode`.
|
|
@@ -405,6 +436,11 @@ export interface KadDHTInit {
|
|
|
405
436
|
* with this filter.
|
|
406
437
|
*/
|
|
407
438
|
peerInfoMapper?(peer: PeerInfo): PeerInfo
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Dynamic network timeout settings for sending messages to peers
|
|
442
|
+
*/
|
|
443
|
+
networkDialTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
|
|
408
444
|
}
|
|
409
445
|
|
|
410
446
|
export interface KadDHTComponents {
|
package/src/kad-dht.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol } from '@libp2p/interface'
|
|
1
|
+
import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol, start, stop } from '@libp2p/interface'
|
|
2
2
|
import drain from 'it-drain'
|
|
3
3
|
import pDefer from 'p-defer'
|
|
4
4
|
import { PROTOCOL } from './constants.js'
|
|
@@ -379,16 +379,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
|
|
|
379
379
|
// Only respond to queries when not in client mode
|
|
380
380
|
await this.setMode(this.clientMode ? 'client' : 'server')
|
|
381
381
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
this.
|
|
386
|
-
this.
|
|
387
|
-
this.
|
|
388
|
-
this.
|
|
389
|
-
this.
|
|
390
|
-
|
|
391
|
-
])
|
|
382
|
+
await start(
|
|
383
|
+
this.querySelf,
|
|
384
|
+
this.providers,
|
|
385
|
+
this.queryManager,
|
|
386
|
+
this.network,
|
|
387
|
+
this.routingTable,
|
|
388
|
+
this.topologyListener,
|
|
389
|
+
this.routingTableRefresh
|
|
390
|
+
)
|
|
392
391
|
}
|
|
393
392
|
|
|
394
393
|
/**
|
|
@@ -398,16 +397,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
|
|
|
398
397
|
async stop (): Promise<void> {
|
|
399
398
|
this.running = false
|
|
400
399
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
this.
|
|
405
|
-
this.
|
|
406
|
-
this.
|
|
407
|
-
this.
|
|
408
|
-
this.
|
|
409
|
-
|
|
410
|
-
])
|
|
400
|
+
await stop(
|
|
401
|
+
this.querySelf,
|
|
402
|
+
this.providers,
|
|
403
|
+
this.queryManager,
|
|
404
|
+
this.network,
|
|
405
|
+
this.routingTable,
|
|
406
|
+
this.routingTableRefresh,
|
|
407
|
+
this.topologyListener
|
|
408
|
+
)
|
|
411
409
|
}
|
|
412
410
|
|
|
413
411
|
/**
|
package/src/network.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TypedEventEmitter } from '@libp2p/interface'
|
|
2
2
|
import { Libp2pRecord } from '@libp2p/record'
|
|
3
|
+
import { AdaptiveTimeout, type AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
|
|
3
4
|
import { pbStream } from 'it-protobuf-stream'
|
|
4
5
|
import { CodeError } from 'protons-runtime'
|
|
5
6
|
import { Message } from './message/dht.js'
|
|
@@ -16,6 +17,7 @@ import type { AbortOptions, Logger, Stream, PeerId, PeerInfo, Startable, Routing
|
|
|
16
17
|
export interface NetworkInit {
|
|
17
18
|
protocol: string
|
|
18
19
|
logPrefix: string
|
|
20
|
+
timeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
interface NetworkEvents {
|
|
@@ -30,6 +32,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
30
32
|
private readonly protocol: string
|
|
31
33
|
private running: boolean
|
|
32
34
|
private readonly components: KadDHTComponents
|
|
35
|
+
private readonly timeout: AdaptiveTimeout
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
38
|
* Create a new network
|
|
@@ -42,6 +45,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
42
45
|
this.log = components.logger.forComponent(`${init.logPrefix}:network`)
|
|
43
46
|
this.running = false
|
|
44
47
|
this.protocol = protocol
|
|
48
|
+
this.timeout = new AdaptiveTimeout({
|
|
49
|
+
...(init.timeout ?? {}),
|
|
50
|
+
metrics: components.metrics,
|
|
51
|
+
metricName: `${init.logPrefix.replaceAll(':', '_')}_network_message_send_times_milliseconds`
|
|
52
|
+
})
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/**
|
|
@@ -88,13 +96,24 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
88
96
|
yield sendQueryEvent({ to, type }, options)
|
|
89
97
|
|
|
90
98
|
let stream: Stream | undefined
|
|
99
|
+
const signal = this.timeout.getTimeoutSignal(options)
|
|
100
|
+
|
|
101
|
+
options = {
|
|
102
|
+
...options,
|
|
103
|
+
signal
|
|
104
|
+
}
|
|
91
105
|
|
|
92
106
|
try {
|
|
93
107
|
const connection = await this.components.connectionManager.openConnection(to, options)
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
stream = await connection.newStream(this.protocol, options)
|
|
96
109
|
const response = await this._writeReadMessage(stream, msg, options)
|
|
97
110
|
|
|
111
|
+
stream.close(options)
|
|
112
|
+
.catch(err => {
|
|
113
|
+
this.log.error('error closing stream to %p', to, err)
|
|
114
|
+
stream?.abort(err)
|
|
115
|
+
})
|
|
116
|
+
|
|
98
117
|
yield peerResponseEvent({
|
|
99
118
|
from: to,
|
|
100
119
|
messageType: response.type,
|
|
@@ -103,12 +122,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
103
122
|
record: response.record == null ? undefined : Libp2pRecord.deserialize(response.record)
|
|
104
123
|
}, options)
|
|
105
124
|
} catch (err: any) {
|
|
125
|
+
stream?.abort(err)
|
|
106
126
|
this.log.error('could not send %s to %p', msg.type, to, err)
|
|
107
127
|
yield queryErrorEvent({ from: to, error: err }, options)
|
|
108
128
|
} finally {
|
|
109
|
-
|
|
110
|
-
await stream.close()
|
|
111
|
-
}
|
|
129
|
+
this.timeout.cleanUp(signal)
|
|
112
130
|
}
|
|
113
131
|
}
|
|
114
132
|
|
|
@@ -131,20 +149,31 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
|
|
|
131
149
|
yield sendQueryEvent({ to, type }, options)
|
|
132
150
|
|
|
133
151
|
let stream: Stream | undefined
|
|
152
|
+
const signal = this.timeout.getTimeoutSignal(options)
|
|
153
|
+
|
|
154
|
+
options = {
|
|
155
|
+
...options,
|
|
156
|
+
signal
|
|
157
|
+
}
|
|
134
158
|
|
|
135
159
|
try {
|
|
136
160
|
const connection = await this.components.connectionManager.openConnection(to, options)
|
|
137
|
-
|
|
161
|
+
stream = await connection.newStream(this.protocol, options)
|
|
138
162
|
|
|
139
163
|
await this._writeMessage(stream, msg, options)
|
|
140
164
|
|
|
165
|
+
stream.close(options)
|
|
166
|
+
.catch(err => {
|
|
167
|
+
this.log.error('error closing stream to %p', to, err)
|
|
168
|
+
stream?.abort(err)
|
|
169
|
+
})
|
|
170
|
+
|
|
141
171
|
yield peerResponseEvent({ from: to, messageType: type }, options)
|
|
142
172
|
} catch (err: any) {
|
|
173
|
+
stream?.abort(err)
|
|
143
174
|
yield queryErrorEvent({ from: to, error: err }, options)
|
|
144
175
|
} finally {
|
|
145
|
-
|
|
146
|
-
await stream.close()
|
|
147
|
-
}
|
|
176
|
+
this.timeout.cleanUp(signal)
|
|
148
177
|
}
|
|
149
178
|
}
|
|
150
179
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
|
|
2
1
|
import { xor as uint8ArrayXor } from 'uint8arrays/xor'
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import { xorCompare as uint8ArrayXorCompare } from 'uint8arrays/xor-compare'
|
|
3
|
+
import { convertPeerId } from '../utils.js'
|
|
4
|
+
import type { PeerId, PeerInfo } from '@libp2p/interface'
|
|
5
5
|
|
|
6
6
|
interface PeerDistance {
|
|
7
|
-
|
|
7
|
+
peer: PeerInfo
|
|
8
8
|
distance: Uint8Array
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -38,28 +38,36 @@ export class PeerDistanceList {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* The
|
|
41
|
+
* The peers in the list, in order of distance from the origin key
|
|
42
42
|
*/
|
|
43
|
-
get peers ():
|
|
44
|
-
return this.peerDistances.map(pd => pd.
|
|
43
|
+
get peers (): PeerInfo[] {
|
|
44
|
+
return this.peerDistances.map(pd => pd.peer)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Add a peerId to the list.
|
|
49
49
|
*/
|
|
50
|
-
async add (
|
|
51
|
-
|
|
50
|
+
async add (peer: PeerInfo): Promise<void> {
|
|
51
|
+
const dhtKey = await convertPeerId(peer.id)
|
|
52
|
+
|
|
53
|
+
this.addWitKadId(peer, dhtKey)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Add a peerId to the list.
|
|
58
|
+
*/
|
|
59
|
+
addWitKadId (peer: PeerInfo, kadId: Uint8Array): void {
|
|
60
|
+
if (this.peerDistances.find(pd => pd.peer.id.equals(peer.id)) != null) {
|
|
52
61
|
return
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
const dhtKey = await utils.convertPeerId(peerId)
|
|
56
64
|
const el = {
|
|
57
|
-
|
|
58
|
-
distance: uint8ArrayXor(this.originDhtKey,
|
|
65
|
+
peer,
|
|
66
|
+
distance: uint8ArrayXor(this.originDhtKey, kadId)
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
this.peerDistances.push(el)
|
|
62
|
-
this.peerDistances.sort((a, b) =>
|
|
70
|
+
this.peerDistances.sort((a, b) => uint8ArrayXorCompare(a.distance, b.distance))
|
|
63
71
|
this.peerDistances = this.peerDistances.slice(0, this.capacity)
|
|
64
72
|
}
|
|
65
73
|
|
|
@@ -67,26 +75,29 @@ export class PeerDistanceList {
|
|
|
67
75
|
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
68
76
|
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
69
77
|
*/
|
|
70
|
-
async
|
|
71
|
-
if (peerIds.length === 0) {
|
|
72
|
-
return false
|
|
73
|
-
}
|
|
74
|
-
|
|
78
|
+
async isCloser (peerId: PeerId): Promise<boolean> {
|
|
75
79
|
if (this.length === 0) {
|
|
76
80
|
return true
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
const
|
|
83
|
+
const dhtKey = await convertPeerId(peerId)
|
|
84
|
+
const dhtKeyXor = uint8ArrayXor(dhtKey, this.originDhtKey)
|
|
80
85
|
const furthestDistance = this.peerDistances[this.peerDistances.length - 1].distance
|
|
81
86
|
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
return uint8ArrayXorCompare(dhtKeyXor, furthestDistance) === -1
|
|
88
|
+
}
|
|
84
89
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
92
|
+
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
93
|
+
*/
|
|
94
|
+
async anyCloser (peerIds: PeerId[]): Promise<boolean> {
|
|
95
|
+
if (peerIds.length === 0) {
|
|
96
|
+
return false
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
return
|
|
99
|
+
return Promise.any(
|
|
100
|
+
peerIds.map(async peerId => this.isCloser(peerId))
|
|
101
|
+
)
|
|
91
102
|
}
|
|
92
103
|
}
|