@libp2p/kad-dht 12.0.15-d9366f9aa → 12.0.16-1a41c28bb
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/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/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 +15 -24
- 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.map +1 -1
- package/dist/src/routing-table/refresh.js +9 -4
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/package.json +15 -17
- package/src/content-routing/index.ts +3 -2
- package/src/index.ts +37 -1
- package/src/network.ts +38 -9
- package/src/peer-list/peer-distance-list.ts +36 -25
- package/src/peer-routing/index.ts +19 -28
- 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 +10 -4
- 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/src/query/utils.ts +0 -64
|
@@ -1,186 +1,151 @@
|
|
|
1
1
|
import { TypedEventEmitter } from '@libp2p/interface';
|
|
2
2
|
import type { PeerId } from '@libp2p/interface';
|
|
3
3
|
export interface PingEventDetails {
|
|
4
|
-
oldContacts:
|
|
5
|
-
newContact:
|
|
6
|
-
}
|
|
7
|
-
export interface UpdatedEventDetails {
|
|
8
|
-
incumbent: Contact;
|
|
9
|
-
selection: Contact;
|
|
4
|
+
oldContacts: Peer[];
|
|
5
|
+
newContact: Peer;
|
|
10
6
|
}
|
|
11
7
|
export interface KBucketEvents {
|
|
12
8
|
'ping': CustomEvent<PingEventDetails>;
|
|
13
|
-
'added': CustomEvent<
|
|
14
|
-
'removed': CustomEvent<
|
|
15
|
-
'updated': CustomEvent<UpdatedEventDetails>;
|
|
9
|
+
'added': CustomEvent<Peer>;
|
|
10
|
+
'removed': CustomEvent<Peer>;
|
|
16
11
|
}
|
|
17
12
|
export interface KBucketOptions {
|
|
18
13
|
/**
|
|
19
|
-
*
|
|
14
|
+
* The current peer. All subsequently added peers must have a KadID that is
|
|
15
|
+
* the same length as this peer.
|
|
20
16
|
*/
|
|
21
|
-
|
|
17
|
+
localPeer: Peer;
|
|
22
18
|
/**
|
|
23
|
-
*
|
|
19
|
+
* How many bits of the key to use when forming the bucket trie. The larger
|
|
20
|
+
* this value, the deeper the tree will grow and the slower the lookups will
|
|
21
|
+
* be but the peers returned will be more specific to the key.
|
|
24
22
|
*/
|
|
25
|
-
|
|
23
|
+
prefixLength: number;
|
|
26
24
|
/**
|
|
27
|
-
* The number of nodes
|
|
28
|
-
* full.
|
|
29
|
-
*
|
|
25
|
+
* The number of nodes that a max-depth k-bucket can contain before being
|
|
26
|
+
* full.
|
|
27
|
+
*
|
|
28
|
+
* @default 20
|
|
30
29
|
*/
|
|
31
|
-
|
|
30
|
+
kBucketSize?: number;
|
|
32
31
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
32
|
+
* The number of nodes that an intermediate k-bucket can contain before being
|
|
33
|
+
* split.
|
|
34
|
+
*
|
|
35
|
+
* @default kBucketSize
|
|
35
36
|
*/
|
|
36
|
-
|
|
37
|
+
splitThreshold?: number;
|
|
37
38
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
39
|
+
* The number of nodes to ping when a bucket that should not be split becomes
|
|
40
|
+
* full. KBucket will emit a `ping` event that contains `numberOfNodesToPing`
|
|
41
|
+
* nodes that have not been contacted the longest.
|
|
41
42
|
*/
|
|
42
|
-
|
|
43
|
+
numberOfNodesToPing?: number;
|
|
43
44
|
}
|
|
44
|
-
export interface
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
vectorClock?: number;
|
|
45
|
+
export interface Peer {
|
|
46
|
+
kadId: Uint8Array;
|
|
47
|
+
peerId: PeerId;
|
|
48
48
|
}
|
|
49
|
-
export interface
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
export interface LeafBucket {
|
|
50
|
+
prefix: string;
|
|
51
|
+
depth: number;
|
|
52
|
+
peers: Peer[];
|
|
53
|
+
}
|
|
54
|
+
export interface InternalBucket {
|
|
55
|
+
prefix: string;
|
|
56
|
+
depth: number;
|
|
53
57
|
left: Bucket;
|
|
54
58
|
right: Bucket;
|
|
55
59
|
}
|
|
60
|
+
export type Bucket = LeafBucket | InternalBucket;
|
|
61
|
+
export declare function isLeafBucket(obj: any): obj is LeafBucket;
|
|
56
62
|
/**
|
|
57
|
-
* Implementation of a Kademlia DHT
|
|
58
|
-
*
|
|
63
|
+
* Implementation of a Kademlia DHT routing table as a prefix binary trie with
|
|
64
|
+
* configurable prefix length, bucket split threshold and size.
|
|
59
65
|
*/
|
|
60
66
|
export declare class KBucket extends TypedEventEmitter<KBucketEvents> {
|
|
61
|
-
localNodeId: Uint8Array;
|
|
62
67
|
root: Bucket;
|
|
63
|
-
|
|
68
|
+
localPeer: Peer;
|
|
69
|
+
private readonly prefixLength;
|
|
70
|
+
private readonly splitThreshold;
|
|
71
|
+
private readonly kBucketSize;
|
|
64
72
|
private readonly numberOfNodesToPing;
|
|
65
|
-
private readonly distance;
|
|
66
|
-
private readonly arbiter;
|
|
67
73
|
constructor(options: KBucketOptions);
|
|
68
|
-
/**
|
|
69
|
-
* Default arbiter function for contacts with the same id. Uses
|
|
70
|
-
* contact.vectorClock to select which contact to update the k-bucket with.
|
|
71
|
-
* Contact with larger vectorClock field will be selected. If vectorClock is
|
|
72
|
-
* the same, candidate will be selected.
|
|
73
|
-
*
|
|
74
|
-
* @param {object} incumbent - Contact currently stored in the k-bucket.
|
|
75
|
-
* @param {object} candidate - Contact being added to the k-bucket.
|
|
76
|
-
* @returns {object} Contact to updated the k-bucket with.
|
|
77
|
-
*/
|
|
78
|
-
static arbiter(incumbent: Contact, candidate: Contact): Contact;
|
|
79
|
-
/**
|
|
80
|
-
* Default distance function. Finds the XOR
|
|
81
|
-
* distance between firstId and secondId.
|
|
82
|
-
*
|
|
83
|
-
* @param {Uint8Array} firstId - Uint8Array containing first id.
|
|
84
|
-
* @param {Uint8Array} secondId - Uint8Array containing second id.
|
|
85
|
-
* @returns {number} Integer The XOR distance between firstId and secondId.
|
|
86
|
-
*/
|
|
87
|
-
static distance(firstId: Uint8Array, secondId: Uint8Array): number;
|
|
88
74
|
/**
|
|
89
75
|
* Adds a contact to the k-bucket.
|
|
90
76
|
*
|
|
91
|
-
* @param {
|
|
77
|
+
* @param {Peer} peer - the contact object to add
|
|
92
78
|
*/
|
|
93
|
-
add(
|
|
79
|
+
add(peer: Peer): void;
|
|
94
80
|
/**
|
|
95
|
-
* Get
|
|
81
|
+
* Get 0-n closest contacts to the provided node id. "Closest" here means:
|
|
96
82
|
* closest according to the XOR metric of the contact node id.
|
|
97
83
|
*
|
|
98
84
|
* @param {Uint8Array} id - Contact node id
|
|
99
|
-
* @
|
|
100
|
-
* @returns {Array} Array Maximum of n closest contacts to the node id
|
|
85
|
+
* @returns {Generator<Peer, void, undefined>} Array Maximum of n closest contacts to the node id
|
|
101
86
|
*/
|
|
102
|
-
closest(id: Uint8Array, n?: number):
|
|
87
|
+
closest(id: Uint8Array, n?: number): Generator<PeerId, void, undefined>;
|
|
103
88
|
/**
|
|
104
89
|
* Counts the total number of contacts in the tree.
|
|
105
90
|
*
|
|
106
91
|
* @returns {number} The number of contacts held in the tree
|
|
107
92
|
*/
|
|
108
93
|
count(): number;
|
|
109
|
-
/**
|
|
110
|
-
* Determines whether the id at the bitIndex is 0 or 1.
|
|
111
|
-
* Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
|
|
112
|
-
*
|
|
113
|
-
* @param {object} node - internal object that has 2 leafs: left and right
|
|
114
|
-
* @param {Uint8Array} id - Id to compare localNodeId with.
|
|
115
|
-
* @param {number} bitIndex - Integer (Default: 0) The bit index to which bit to check in the id Uint8Array.
|
|
116
|
-
* @returns {object} left leaf if id at bitIndex is 0, right leaf otherwise.
|
|
117
|
-
*/
|
|
118
|
-
_determineNode(node: any, id: Uint8Array, bitIndex: number): Bucket;
|
|
119
94
|
/**
|
|
120
95
|
* Get a contact by its exact ID.
|
|
121
96
|
* If this is a leaf, loop through the bucket contents and return the correct
|
|
122
97
|
* contact if we have it or null if not. If this is an inner node, determine
|
|
123
98
|
* which branch of the tree to traverse and repeat.
|
|
124
99
|
*
|
|
125
|
-
* @param {Uint8Array}
|
|
126
|
-
* @returns {object |
|
|
100
|
+
* @param {Uint8Array} kadId - The ID of the contact to fetch.
|
|
101
|
+
* @returns {object | undefined} The contact if available, otherwise null
|
|
127
102
|
*/
|
|
128
|
-
get(
|
|
103
|
+
get(kadId: Uint8Array): Peer | undefined;
|
|
129
104
|
/**
|
|
130
|
-
*
|
|
131
|
-
* id if it exists, returns -1 otherwise.
|
|
105
|
+
* Removes contact with the provided id.
|
|
132
106
|
*
|
|
133
|
-
* @param {
|
|
134
|
-
* @param {Uint8Array} id - Contact node id.
|
|
135
|
-
* @returns {number} Integer Index of contact with provided id if it exists, -1 otherwise.
|
|
107
|
+
* @param {Uint8Array} kadId - The ID of the contact to remove
|
|
136
108
|
*/
|
|
137
|
-
|
|
109
|
+
remove(kadId: Uint8Array): void;
|
|
138
110
|
/**
|
|
139
|
-
*
|
|
111
|
+
* Similar to `toArray()` but instead of buffering everything up into an
|
|
112
|
+
* array before returning it, yields contacts as they are encountered while
|
|
113
|
+
* walking the tree.
|
|
140
114
|
*
|
|
141
|
-
* @
|
|
142
|
-
* @returns {object} The k-bucket itself
|
|
115
|
+
* @returns {Iterable} All of the contacts in the tree, as an iterable
|
|
143
116
|
*/
|
|
144
|
-
|
|
117
|
+
toIterable(): Generator<Peer, void, undefined>;
|
|
145
118
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* setting this.root.contacts = null
|
|
119
|
+
* Default distance function. Finds the XOR distance between firstId and
|
|
120
|
+
* secondId.
|
|
149
121
|
*
|
|
150
|
-
* @param
|
|
151
|
-
* @param
|
|
122
|
+
* @param {Uint8Array} firstId - Uint8Array containing first id.
|
|
123
|
+
* @param {Uint8Array} secondId - Uint8Array containing second id.
|
|
124
|
+
* @returns {number} Integer The XOR distance between firstId and secondId.
|
|
152
125
|
*/
|
|
153
|
-
|
|
126
|
+
distance(firstId: Uint8Array, secondId: Uint8Array): bigint;
|
|
154
127
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* return the union of the low and high branches (themselves also as arrays).
|
|
128
|
+
* Determines whether the id at the bitIndex is 0 or 1
|
|
129
|
+
* Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
|
|
158
130
|
*
|
|
159
|
-
* @
|
|
131
|
+
* @param {Uint8Array} kadId - Id to compare localNodeId with
|
|
132
|
+
* @returns {LeafBucket} left leaf if id at bitIndex is 0, right leaf otherwise.
|
|
160
133
|
*/
|
|
161
|
-
|
|
134
|
+
private _determineBucket;
|
|
162
135
|
/**
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* walking the tree.
|
|
136
|
+
* Returns the index of the contact with provided
|
|
137
|
+
* id if it exists, returns -1 otherwise.
|
|
166
138
|
*
|
|
167
|
-
* @
|
|
139
|
+
* @param {object} bucket - internal object that has 2 leafs: left and right
|
|
140
|
+
* @param {Uint8Array} kadId - KadId of peer
|
|
141
|
+
* @returns {number} Integer Index of contact with provided id if it exists, -1 otherwise.
|
|
168
142
|
*/
|
|
169
|
-
|
|
143
|
+
private _indexOf;
|
|
170
144
|
/**
|
|
171
|
-
*
|
|
172
|
-
* If the selection is our old contact and the candidate is some new contact
|
|
173
|
-
* then the new contact is abandoned (not added).
|
|
174
|
-
* If the selection is our old contact and the candidate is our old contact
|
|
175
|
-
* then we are refreshing the contact and it is marked as most recently
|
|
176
|
-
* contacted (by being moved to the right/end of the bucket array).
|
|
177
|
-
* If the selection is our new contact, the old contact is removed and the new
|
|
178
|
-
* contact is marked as most recently contacted.
|
|
145
|
+
* Modify the bucket, turn it from a leaf bucket to an internal bucket
|
|
179
146
|
*
|
|
180
|
-
* @param {
|
|
181
|
-
* @param {number} index - the index in the bucket where contact exists (index has already been computed in a previous calculation)
|
|
182
|
-
* @param {object} contact - The contact object to update
|
|
147
|
+
* @param {any} bucket - bucket for splitting
|
|
183
148
|
*/
|
|
184
|
-
|
|
149
|
+
private _split;
|
|
185
150
|
}
|
|
186
151
|
//# sourceMappingURL=k-bucket.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"k-bucket.d.ts","sourceRoot":"","sources":["../../../src/routing-table/k-bucket.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"k-bucket.d.ts","sourceRoot":"","sources":["../../../src/routing-table/k-bucket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAMrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AA2B/C,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,IAAI,EAAE,CAAA;IACnB,UAAU,EAAE,IAAI,CAAA;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,WAAW,CAAC,gBAAgB,CAAC,CAAA;IACrC,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,CAAA;IAC1B,SAAS,EAAE,WAAW,CAAC,IAAI,CAAC,CAAA;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,SAAS,EAAE,IAAI,CAAA;IAEf;;;;OAIG;IACH,YAAY,EAAE,MAAM,CAAA;IAEpB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED,MAAM,WAAW,IAAI;IACnB,KAAK,EAAE,UAAU,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,IAAI,EAAE,CAAA;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,MAAM,MAAM,GAAG,UAAU,G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