@libp2p/kad-dht 11.0.8 → 12.0.0-28587d24f
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/README.md +78 -0
- package/dist/index.min.js +20 -20
- package/dist/src/constants.d.ts +2 -4
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +7 -9
- package/dist/src/constants.js.map +1 -1
- package/dist/src/content-fetching/index.d.ts +7 -7
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js +13 -7
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +5 -4
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +23 -13
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/index.d.ts +142 -28
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +87 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts +20 -21
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +181 -35
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/message/dht.d.ts +35 -35
- package/dist/src/message/dht.d.ts.map +1 -1
- package/dist/src/message/dht.js +150 -130
- package/dist/src/message/dht.js.map +1 -1
- package/dist/src/message/utils.d.ts +5 -0
- package/dist/src/message/utils.d.ts.map +1 -0
- package/dist/src/message/utils.js +20 -0
- package/dist/src/message/utils.js.map +1 -0
- package/dist/src/network.d.ts +8 -8
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +30 -18
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +6 -6
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +48 -35
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/providers.d.ts +7 -0
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js.map +1 -1
- package/dist/src/query/events.d.ts +13 -12
- package/dist/src/query/events.d.ts.map +1 -1
- package/dist/src/query/events.js +2 -2
- package/dist/src/query/events.js.map +1 -1
- package/dist/src/query/manager.d.ts +8 -5
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +6 -6
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query/query-path.d.ts +3 -3
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query-self.d.ts +1 -1
- package/dist/src/query-self.d.ts.map +1 -1
- package/dist/src/query-self.js +2 -2
- package/dist/src/query-self.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +5 -6
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +72 -58
- package/dist/src/routing-table/index.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 +2 -2
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/handlers/add-provider.d.ts +2 -1
- package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
- package/dist/src/rpc/handlers/add-provider.js +8 -6
- package/dist/src/rpc/handlers/add-provider.js.map +1 -1
- package/dist/src/rpc/handlers/find-node.d.ts +5 -3
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/find-node.js +22 -14
- package/dist/src/rpc/handlers/find-node.js.map +1 -1
- package/dist/src/rpc/handlers/get-providers.d.ts +5 -3
- package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-providers.js +29 -16
- package/dist/src/rpc/handlers/get-providers.js.map +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts +2 -1
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.js +16 -7
- package/dist/src/rpc/handlers/get-value.js.map +1 -1
- package/dist/src/rpc/handlers/ping.d.ts +5 -2
- package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
- package/dist/src/rpc/handlers/ping.js +2 -2
- package/dist/src/rpc/handlers/ping.js.map +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts +2 -1
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.js +8 -7
- package/dist/src/rpc/handlers/put-value.js.map +1 -1
- package/dist/src/rpc/index.d.ts +4 -3
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +11 -11
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/src/topology-listener.d.ts +1 -1
- package/dist/src/topology-listener.d.ts.map +1 -1
- package/dist/src/topology-listener.js +2 -2
- package/dist/src/topology-listener.js.map +1 -1
- package/dist/src/utils.d.ts +5 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +32 -2
- package/dist/src/utils.js.map +1 -1
- package/package.json +13 -11
- package/src/constants.ts +7 -11
- package/src/content-fetching/index.ts +21 -14
- package/src/content-routing/index.ts +29 -18
- package/src/index.ts +148 -32
- package/src/kad-dht.ts +225 -56
- package/src/message/dht.proto +32 -32
- package/src/message/dht.ts +155 -138
- package/src/message/utils.ts +25 -0
- package/src/network.ts +41 -25
- package/src/peer-routing/index.ts +57 -42
- package/src/providers.ts +7 -0
- package/src/query/events.ts +14 -14
- package/src/query/manager.ts +14 -10
- package/src/query/query-path.ts +3 -3
- package/src/query-self.ts +3 -3
- package/src/routing-table/index.ts +86 -64
- package/src/routing-table/refresh.ts +4 -4
- package/src/rpc/handlers/add-provider.ts +10 -7
- package/src/rpc/handlers/find-node.ts +27 -18
- package/src/rpc/handlers/get-providers.ts +33 -20
- package/src/rpc/handlers/get-value.ts +18 -7
- package/src/rpc/handlers/ping.ts +7 -3
- package/src/rpc/handlers/put-value.ts +11 -9
- package/src/rpc/index.ts +14 -13
- package/src/topology-listener.ts +3 -3
- package/src/utils.ts +41 -2
- package/dist/src/dual-kad-dht.d.ts +0 -69
- package/dist/src/dual-kad-dht.d.ts.map +0 -1
- package/dist/src/dual-kad-dht.js +0 -304
- package/dist/src/dual-kad-dht.js.map +0 -1
- package/dist/src/message/index.d.ts +0 -35
- package/dist/src/message/index.d.ts.map +0 -1
- package/dist/src/message/index.js +0 -92
- package/dist/src/message/index.js.map +0 -1
- package/dist/typedoc-urls.json +0 -55
- package/src/dual-kad-dht.ts +0 -384
- package/src/message/index.ts +0 -117
package/dist/src/utils.js
CHANGED
|
@@ -9,7 +9,7 @@ import { RECORD_KEY_PREFIX } from './constants.js';
|
|
|
9
9
|
import { Libp2pRecord } from './record/index.js';
|
|
10
10
|
// const IPNS_PREFIX = uint8ArrayFromString('/ipns/')
|
|
11
11
|
const PK_PREFIX = uint8ArrayFromString('/pk/');
|
|
12
|
-
export function
|
|
12
|
+
export function removePrivateAddressesMapper(peer) {
|
|
13
13
|
return {
|
|
14
14
|
...peer,
|
|
15
15
|
multiaddrs: peer.multiaddrs.filter(multiaddr => {
|
|
@@ -37,7 +37,7 @@ export function removePrivateAddresses(peer) {
|
|
|
37
37
|
})
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
-
export function
|
|
40
|
+
export function removePublicAddressesMapper(peer) {
|
|
41
41
|
return {
|
|
42
42
|
...peer,
|
|
43
43
|
multiaddrs: peer.multiaddrs.filter(multiaddr => {
|
|
@@ -60,6 +60,9 @@ export function removePublicAddresses(peer) {
|
|
|
60
60
|
})
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
+
export function passthroughMapper(info) {
|
|
64
|
+
return info;
|
|
65
|
+
}
|
|
63
66
|
/**
|
|
64
67
|
* Creates a DHT ID by hashing a given Uint8Array
|
|
65
68
|
*/
|
|
@@ -118,4 +121,31 @@ export function debounce(callback, wait = 100) {
|
|
|
118
121
|
timeout = setTimeout(() => { callback(); }, wait);
|
|
119
122
|
};
|
|
120
123
|
}
|
|
124
|
+
// see https://github.com/multiformats/multiaddr/blob/master/protocols.csv
|
|
125
|
+
const P2P_CIRCUIT_CODE = 290;
|
|
126
|
+
const DNS4_CODE = 54;
|
|
127
|
+
const DNS6_CODE = 55;
|
|
128
|
+
const DNSADDR_CODE = 56;
|
|
129
|
+
const IP4_CODE = 4;
|
|
130
|
+
const IP6_CODE = 41;
|
|
131
|
+
export function multiaddrIsPublic(multiaddr) {
|
|
132
|
+
const tuples = multiaddr.stringTuples();
|
|
133
|
+
// p2p-circuit should not enable server mode
|
|
134
|
+
for (const tuple of tuples) {
|
|
135
|
+
if (tuple[0] === P2P_CIRCUIT_CODE) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// dns4 or dns6 or dnsaddr
|
|
140
|
+
if (tuples[0][0] === DNS4_CODE || tuples[0][0] === DNS6_CODE || tuples[0][0] === DNSADDR_CODE) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
// ip4 or ip6
|
|
144
|
+
if (tuples[0][0] === IP4_CODE || tuples[0][0] === IP6_CODE) {
|
|
145
|
+
const result = isPrivateIp(`${tuples[0][1]}`);
|
|
146
|
+
const isPublic = result == null || !result;
|
|
147
|
+
return isPublic;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
121
151
|
//# sourceMappingURL=utils.js.map
|
package/dist/src/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,GAAG,EAAE,MAAM,yBAAyB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,WAAW,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ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AED,aAAa;IACb,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3D,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QAC7C,MAAM,QAAQ,GAAG,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAA;QAE1C,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0-28587d24f",
|
|
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",
|
|
@@ -56,11 +56,12 @@
|
|
|
56
56
|
"dep-check": "aegir dep-check"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@libp2p/crypto": "
|
|
60
|
-
"@libp2p/interface": "
|
|
61
|
-
"@libp2p/interface-internal": "
|
|
62
|
-
"@libp2p/peer-collections": "
|
|
63
|
-
"@libp2p/peer-id": "
|
|
59
|
+
"@libp2p/crypto": "3.0.2-28587d24f",
|
|
60
|
+
"@libp2p/interface": "1.0.2-28587d24f",
|
|
61
|
+
"@libp2p/interface-internal": "1.0.3-28587d24f",
|
|
62
|
+
"@libp2p/peer-collections": "5.1.1-28587d24f",
|
|
63
|
+
"@libp2p/peer-id": "4.0.2-28587d24f",
|
|
64
|
+
"@libp2p/utils": "5.1.0-28587d24f",
|
|
64
65
|
"@multiformats/multiaddr": "^12.1.10",
|
|
65
66
|
"@types/sinon": "^17.0.0",
|
|
66
67
|
"any-signal": "^4.1.1",
|
|
@@ -79,7 +80,7 @@
|
|
|
79
80
|
"it-pushable": "^3.2.1",
|
|
80
81
|
"it-stream-types": "^2.0.1",
|
|
81
82
|
"it-take": "^3.0.1",
|
|
82
|
-
"multiformats": "^
|
|
83
|
+
"multiformats": "^13.0.0",
|
|
83
84
|
"p-defer": "^4.0.0",
|
|
84
85
|
"p-event": "^6.0.0",
|
|
85
86
|
"p-queue": "^8.0.0",
|
|
@@ -91,10 +92,10 @@
|
|
|
91
92
|
"uint8arrays": "^5.0.0"
|
|
92
93
|
},
|
|
93
94
|
"devDependencies": {
|
|
94
|
-
"@libp2p/interface-compliance-tests": "
|
|
95
|
-
"@libp2p/logger": "
|
|
96
|
-
"@libp2p/peer-id-factory": "
|
|
97
|
-
"@libp2p/peer-store": "
|
|
95
|
+
"@libp2p/interface-compliance-tests": "5.0.8-28587d24f",
|
|
96
|
+
"@libp2p/logger": "4.0.2-28587d24f",
|
|
97
|
+
"@libp2p/peer-id-factory": "4.0.1-28587d24f",
|
|
98
|
+
"@libp2p/peer-store": "10.0.3-28587d24f",
|
|
98
99
|
"@types/lodash.random": "^3.2.6",
|
|
99
100
|
"@types/lodash.range": "^3.2.6",
|
|
100
101
|
"@types/which": "^3.0.0",
|
|
@@ -104,6 +105,7 @@
|
|
|
104
105
|
"execa": "^8.0.1",
|
|
105
106
|
"it-filter": "^3.0.1",
|
|
106
107
|
"it-last": "^3.0.3",
|
|
108
|
+
"it-pair": "^2.0.6",
|
|
107
109
|
"lodash.random": "^3.2.0",
|
|
108
110
|
"lodash.range": "^3.2.0",
|
|
109
111
|
"p-retry": "^6.1.0",
|
package/src/constants.ts
CHANGED
|
@@ -11,11 +11,7 @@ export const hour = 60 * minute
|
|
|
11
11
|
|
|
12
12
|
export const MAX_RECORD_AGE = 36 * hour
|
|
13
13
|
|
|
14
|
-
export const
|
|
15
|
-
|
|
16
|
-
export const PROTOCOL_PREFIX = '/ipfs'
|
|
17
|
-
|
|
18
|
-
export const PROTOCOL_DHT = '/kad/1.0.0'
|
|
14
|
+
export const PROTOCOL = '/ipfs/kad/1.0.0'
|
|
19
15
|
|
|
20
16
|
export const RECORD_KEY_PREFIX = '/dht/record'
|
|
21
17
|
|
|
@@ -39,19 +35,19 @@ export const K = 20
|
|
|
39
35
|
export const ALPHA = 3
|
|
40
36
|
|
|
41
37
|
// How often we look for our closest DHT neighbours
|
|
42
|
-
export const QUERY_SELF_INTERVAL =
|
|
38
|
+
export const QUERY_SELF_INTERVAL = 5 * minute
|
|
43
39
|
|
|
44
40
|
// How often we look for the first set of our closest DHT neighbours
|
|
45
|
-
export const QUERY_SELF_INITIAL_INTERVAL =
|
|
41
|
+
export const QUERY_SELF_INITIAL_INTERVAL = second
|
|
46
42
|
|
|
47
43
|
// How long to look for our closest DHT neighbours for
|
|
48
|
-
export const QUERY_SELF_TIMEOUT =
|
|
44
|
+
export const QUERY_SELF_TIMEOUT = 5 * second
|
|
49
45
|
|
|
50
46
|
// How often we try to find new peers
|
|
51
|
-
export const TABLE_REFRESH_INTERVAL =
|
|
47
|
+
export const TABLE_REFRESH_INTERVAL = 5 * minute
|
|
52
48
|
|
|
53
49
|
// How how long to look for new peers for
|
|
54
|
-
export const TABLE_REFRESH_QUERY_TIMEOUT =
|
|
50
|
+
export const TABLE_REFRESH_QUERY_TIMEOUT = 30 * second
|
|
55
51
|
|
|
56
52
|
// When a timeout is not specified, run a query for this long
|
|
57
|
-
export const DEFAULT_QUERY_TIMEOUT =
|
|
53
|
+
export const DEFAULT_QUERY_TIMEOUT = 30 * second
|
|
@@ -6,7 +6,7 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
|
6
6
|
import {
|
|
7
7
|
ALPHA
|
|
8
8
|
} from '../constants.js'
|
|
9
|
-
import {
|
|
9
|
+
import { MessageType } from '../message/dht.js'
|
|
10
10
|
import {
|
|
11
11
|
valueEvent,
|
|
12
12
|
queryErrorEvent
|
|
@@ -15,12 +15,13 @@ import { Libp2pRecord } from '../record/index.js'
|
|
|
15
15
|
import { bestRecord } from '../record/selectors.js'
|
|
16
16
|
import { verifyRecord } from '../record/validators.js'
|
|
17
17
|
import { createPutRecord, bufferToRecordKey } from '../utils.js'
|
|
18
|
-
import type { KadDHTComponents, Validators, Selectors, ValueEvent,
|
|
18
|
+
import type { KadDHTComponents, Validators, Selectors, ValueEvent, QueryEvent } from '../index.js'
|
|
19
|
+
import type { Message } from '../message/dht.js'
|
|
19
20
|
import type { Network } from '../network.js'
|
|
20
21
|
import type { PeerRouting } from '../peer-routing/index.js'
|
|
21
22
|
import type { QueryManager } from '../query/manager.js'
|
|
22
23
|
import type { QueryFunc } from '../query/types.js'
|
|
23
|
-
import type {
|
|
24
|
+
import type { Logger, RoutingOptions } from '@libp2p/interface'
|
|
24
25
|
|
|
25
26
|
export interface ContentFetchingInit {
|
|
26
27
|
validators: Validators
|
|
@@ -28,7 +29,7 @@ export interface ContentFetchingInit {
|
|
|
28
29
|
peerRouting: PeerRouting
|
|
29
30
|
queryManager: QueryManager
|
|
30
31
|
network: Network
|
|
31
|
-
|
|
32
|
+
logPrefix: string
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
export class ContentFetching {
|
|
@@ -41,10 +42,10 @@ export class ContentFetching {
|
|
|
41
42
|
private readonly network: Network
|
|
42
43
|
|
|
43
44
|
constructor (components: KadDHTComponents, init: ContentFetchingInit) {
|
|
44
|
-
const { validators, selectors, peerRouting, queryManager, network,
|
|
45
|
+
const { validators, selectors, peerRouting, queryManager, network, logPrefix } = init
|
|
45
46
|
|
|
46
47
|
this.components = components
|
|
47
|
-
this.log = components.logger.forComponent(
|
|
48
|
+
this.log = components.logger.forComponent(`${logPrefix}:content-fetching`)
|
|
48
49
|
this.validators = validators
|
|
49
50
|
this.selectors = selectors
|
|
50
51
|
this.peerRouting = peerRouting
|
|
@@ -81,7 +82,7 @@ export class ContentFetching {
|
|
|
81
82
|
/**
|
|
82
83
|
* Send the best record found to any peers that have an out of date record
|
|
83
84
|
*/
|
|
84
|
-
async * sendCorrectionRecord (key: Uint8Array, vals: ValueEvent[], best: Uint8Array, options:
|
|
85
|
+
async * sendCorrectionRecord (key: Uint8Array, vals: ValueEvent[], best: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
|
|
85
86
|
this.log('sendCorrection for %b', key)
|
|
86
87
|
const fixupRec = createPutRecord(key, best)
|
|
87
88
|
|
|
@@ -107,8 +108,11 @@ export class ContentFetching {
|
|
|
107
108
|
|
|
108
109
|
// send correction
|
|
109
110
|
let sentCorrection = false
|
|
110
|
-
const request
|
|
111
|
-
|
|
111
|
+
const request: Partial<Message> = {
|
|
112
|
+
type: MessageType.PUT_VALUE,
|
|
113
|
+
key,
|
|
114
|
+
record: fixupRec
|
|
115
|
+
}
|
|
112
116
|
|
|
113
117
|
for await (const event of this.network.sendRequest(from, request, options)) {
|
|
114
118
|
if (event.name === 'PEER_RESPONSE' && (event.record != null) && uint8ArrayEquals(event.record.value, Libp2pRecord.deserialize(fixupRec).value)) {
|
|
@@ -129,7 +133,7 @@ export class ContentFetching {
|
|
|
129
133
|
/**
|
|
130
134
|
* Store the given key/value pair in the DHT
|
|
131
135
|
*/
|
|
132
|
-
async * put (key: Uint8Array, value: Uint8Array, options:
|
|
136
|
+
async * put (key: Uint8Array, value: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<unknown, void, undefined> {
|
|
133
137
|
this.log('put key %b value %b', key, value)
|
|
134
138
|
|
|
135
139
|
// create record in the dht format
|
|
@@ -151,8 +155,11 @@ export class ContentFetching {
|
|
|
151
155
|
|
|
152
156
|
const events = []
|
|
153
157
|
|
|
154
|
-
const msg
|
|
155
|
-
|
|
158
|
+
const msg: Partial<Message> = {
|
|
159
|
+
type: MessageType.PUT_VALUE,
|
|
160
|
+
key,
|
|
161
|
+
record
|
|
162
|
+
}
|
|
156
163
|
|
|
157
164
|
this.log('send put to %p', event.peer.id)
|
|
158
165
|
for await (const putEvent of this.network.sendRequest(event.peer.id, msg, options)) {
|
|
@@ -185,7 +192,7 @@ export class ContentFetching {
|
|
|
185
192
|
/**
|
|
186
193
|
* Get the value to the given key
|
|
187
194
|
*/
|
|
188
|
-
async * get (key: Uint8Array, options:
|
|
195
|
+
async * get (key: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<QueryEvent | ValueEvent> {
|
|
189
196
|
this.log('get %b', key)
|
|
190
197
|
|
|
191
198
|
const vals: ValueEvent[] = []
|
|
@@ -229,7 +236,7 @@ export class ContentFetching {
|
|
|
229
236
|
/**
|
|
230
237
|
* Get the `n` values to the given key without sorting
|
|
231
238
|
*/
|
|
232
|
-
async * getMany (key: Uint8Array, options:
|
|
239
|
+
async * getMany (key: Uint8Array, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
|
|
233
240
|
this.log('getMany values for %b', key)
|
|
234
241
|
|
|
235
242
|
try {
|
|
@@ -1,21 +1,24 @@
|
|
|
1
|
+
import { PeerSet } from '@libp2p/peer-collections'
|
|
1
2
|
import map from 'it-map'
|
|
2
3
|
import parallel from 'it-parallel'
|
|
3
4
|
import { pipe } from 'it-pipe'
|
|
4
5
|
import { ALPHA } from '../constants.js'
|
|
5
|
-
import {
|
|
6
|
+
import { MessageType } from '../message/dht.js'
|
|
7
|
+
import { toPbPeerInfo } from '../message/utils.js'
|
|
6
8
|
import {
|
|
7
9
|
queryErrorEvent,
|
|
8
10
|
peerResponseEvent,
|
|
9
11
|
providerEvent
|
|
10
12
|
} from '../query/events.js'
|
|
11
|
-
import type { KadDHTComponents, PeerResponseEvent, ProviderEvent, QueryEvent
|
|
13
|
+
import type { KadDHTComponents, PeerResponseEvent, ProviderEvent, QueryEvent } from '../index.js'
|
|
14
|
+
import type { Message } from '../message/dht.js'
|
|
12
15
|
import type { Network } from '../network.js'
|
|
13
16
|
import type { PeerRouting } from '../peer-routing/index.js'
|
|
14
17
|
import type { Providers } from '../providers.js'
|
|
15
18
|
import type { QueryManager } from '../query/manager.js'
|
|
16
19
|
import type { QueryFunc } from '../query/types.js'
|
|
17
20
|
import type { RoutingTable } from '../routing-table/index.js'
|
|
18
|
-
import type { Logger, PeerInfo } from '@libp2p/interface'
|
|
21
|
+
import type { Logger, PeerInfo, RoutingOptions } from '@libp2p/interface'
|
|
19
22
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
20
23
|
import type { CID } from 'multiformats/cid'
|
|
21
24
|
|
|
@@ -25,7 +28,7 @@ export interface ContentRoutingInit {
|
|
|
25
28
|
queryManager: QueryManager
|
|
26
29
|
routingTable: RoutingTable
|
|
27
30
|
providers: Providers
|
|
28
|
-
|
|
31
|
+
logPrefix: string
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
export class ContentRouting {
|
|
@@ -38,10 +41,10 @@ export class ContentRouting {
|
|
|
38
41
|
private readonly providers: Providers
|
|
39
42
|
|
|
40
43
|
constructor (components: KadDHTComponents, init: ContentRoutingInit) {
|
|
41
|
-
const { network, peerRouting, queryManager, routingTable, providers,
|
|
44
|
+
const { network, peerRouting, queryManager, routingTable, providers, logPrefix } = init
|
|
42
45
|
|
|
43
46
|
this.components = components
|
|
44
|
-
this.log = components.logger.forComponent(
|
|
47
|
+
this.log = components.logger.forComponent(`${logPrefix}:content-routing`)
|
|
45
48
|
this.network = network
|
|
46
49
|
this.peerRouting = peerRouting
|
|
47
50
|
this.queryManager = queryManager
|
|
@@ -53,17 +56,22 @@ export class ContentRouting {
|
|
|
53
56
|
* Announce to the network that we can provide the value for a given key and
|
|
54
57
|
* are contactable on the given multiaddrs
|
|
55
58
|
*/
|
|
56
|
-
async * provide (key: CID, multiaddrs: Multiaddr[], options:
|
|
59
|
+
async * provide (key: CID, multiaddrs: Multiaddr[], options: RoutingOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
|
|
57
60
|
this.log('provide %s', key)
|
|
58
61
|
|
|
59
62
|
// Add peer as provider
|
|
60
63
|
await this.providers.addProvider(key, this.components.peerId)
|
|
61
64
|
|
|
62
|
-
const msg
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
const msg: Partial<Message> = {
|
|
66
|
+
type: MessageType.ADD_PROVIDER,
|
|
67
|
+
key: key.multihash.bytes,
|
|
68
|
+
providers: [
|
|
69
|
+
toPbPeerInfo({
|
|
70
|
+
id: this.components.peerId,
|
|
71
|
+
multiaddrs
|
|
72
|
+
})
|
|
73
|
+
]
|
|
74
|
+
}
|
|
67
75
|
|
|
68
76
|
let sent = 0
|
|
69
77
|
|
|
@@ -118,7 +126,7 @@ export class ContentRouting {
|
|
|
118
126
|
/**
|
|
119
127
|
* Search the dht for up to `K` providers of the given CID.
|
|
120
128
|
*/
|
|
121
|
-
async * findProviders (key: CID, options:
|
|
129
|
+
async * findProviders (key: CID, options: RoutingOptions): AsyncGenerator<PeerResponseEvent | ProviderEvent | QueryEvent> {
|
|
122
130
|
const toFind = this.routingTable.kBucketSize
|
|
123
131
|
const target = key.multihash.bytes
|
|
124
132
|
const self = this // eslint-disable-line @typescript-eslint/no-this-alias
|
|
@@ -148,7 +156,7 @@ export class ContentRouting {
|
|
|
148
156
|
}
|
|
149
157
|
}
|
|
150
158
|
|
|
151
|
-
yield peerResponseEvent({ from: this.components.peerId, messageType:
|
|
159
|
+
yield peerResponseEvent({ from: this.components.peerId, messageType: MessageType.GET_PROVIDERS, providers }, options)
|
|
152
160
|
yield providerEvent({ from: this.components.peerId, providers }, options)
|
|
153
161
|
}
|
|
154
162
|
|
|
@@ -161,7 +169,10 @@ export class ContentRouting {
|
|
|
161
169
|
* The query function to use on this particular disjoint path
|
|
162
170
|
*/
|
|
163
171
|
const findProvidersQuery: QueryFunc = async function * ({ peer, signal }) {
|
|
164
|
-
const request =
|
|
172
|
+
const request = {
|
|
173
|
+
type: MessageType.GET_PROVIDERS,
|
|
174
|
+
key: target
|
|
175
|
+
}
|
|
165
176
|
|
|
166
177
|
yield * self.network.sendRequest(peer, request, {
|
|
167
178
|
...options,
|
|
@@ -169,7 +180,7 @@ export class ContentRouting {
|
|
|
169
180
|
})
|
|
170
181
|
}
|
|
171
182
|
|
|
172
|
-
const providers = new
|
|
183
|
+
const providers = new PeerSet(provs)
|
|
173
184
|
|
|
174
185
|
for await (const event of this.queryManager.run(target, findProvidersQuery, options)) {
|
|
175
186
|
yield event
|
|
@@ -180,11 +191,11 @@ export class ContentRouting {
|
|
|
180
191
|
const newProviders = []
|
|
181
192
|
|
|
182
193
|
for (const peer of event.providers) {
|
|
183
|
-
if (providers.has(peer.id
|
|
194
|
+
if (providers.has(peer.id)) {
|
|
184
195
|
continue
|
|
185
196
|
}
|
|
186
197
|
|
|
187
|
-
providers.add(peer.id
|
|
198
|
+
providers.add(peer.id)
|
|
188
199
|
newProviders.push(peer)
|
|
189
200
|
}
|
|
190
201
|
|