@libp2p/kad-dht 9.3.2 → 9.3.3
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 +19 -34
- package/dist/src/routing-table/index.d.ts +4 -32
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +7 -7
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +188 -0
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -0
- package/dist/src/routing-table/k-bucket.js +409 -0
- package/dist/src/routing-table/k-bucket.js.map +1 -0
- package/package.json +1 -2
- package/src/routing-table/index.ts +13 -45
- package/src/routing-table/k-bucket.ts +523 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Queue from 'p-queue';
|
|
2
|
+
import { KBucket, type PingEventDetails } from './k-bucket.js';
|
|
2
3
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager';
|
|
3
4
|
import type { Metrics } from '@libp2p/interface-metrics';
|
|
4
5
|
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
@@ -9,34 +10,6 @@ export declare const KAD_CLOSE_TAG_VALUE = 50;
|
|
|
9
10
|
export declare const KBUCKET_SIZE = 20;
|
|
10
11
|
export declare const PING_TIMEOUT = 10000;
|
|
11
12
|
export declare const PING_CONCURRENCY = 10;
|
|
12
|
-
export interface KBucketPeer {
|
|
13
|
-
id: Uint8Array;
|
|
14
|
-
peer: PeerId;
|
|
15
|
-
}
|
|
16
|
-
export interface KBucket {
|
|
17
|
-
id: Uint8Array;
|
|
18
|
-
contacts: KBucketPeer[];
|
|
19
|
-
dontSplit: boolean;
|
|
20
|
-
left: KBucket;
|
|
21
|
-
right: KBucket;
|
|
22
|
-
}
|
|
23
|
-
interface KBucketTreeEvents {
|
|
24
|
-
'ping': (oldContacts: KBucketPeer[], newContact: KBucketPeer) => void;
|
|
25
|
-
'added': (contact: KBucketPeer) => void;
|
|
26
|
-
'removed': (contact: KBucketPeer) => void;
|
|
27
|
-
}
|
|
28
|
-
export interface KBucketTree {
|
|
29
|
-
root: KBucket;
|
|
30
|
-
localNodeId: Uint8Array;
|
|
31
|
-
on: <U extends keyof KBucketTreeEvents>(event: U, listener: KBucketTreeEvents[U]) => this;
|
|
32
|
-
closest: (key: Uint8Array, count: number) => KBucketPeer[];
|
|
33
|
-
closestPeer: (key: Uint8Array) => KBucketPeer;
|
|
34
|
-
remove: (key: Uint8Array) => void;
|
|
35
|
-
add: (peer: KBucketPeer) => void;
|
|
36
|
-
get: (key: Uint8Array) => Uint8Array;
|
|
37
|
-
count: () => number;
|
|
38
|
-
toIterable: () => Iterable<KBucketPeer>;
|
|
39
|
-
}
|
|
40
13
|
export interface RoutingTableInit {
|
|
41
14
|
lan: boolean;
|
|
42
15
|
protocol: string;
|
|
@@ -58,7 +31,7 @@ export interface RoutingTableComponents {
|
|
|
58
31
|
*/
|
|
59
32
|
export declare class RoutingTable implements Startable {
|
|
60
33
|
kBucketSize: number;
|
|
61
|
-
kb?:
|
|
34
|
+
kb?: KBucket;
|
|
62
35
|
pingQueue: Queue;
|
|
63
36
|
private readonly log;
|
|
64
37
|
private readonly components;
|
|
@@ -79,7 +52,7 @@ export declare class RoutingTable implements Startable {
|
|
|
79
52
|
* - this will lower the chances that connections to them get closed when
|
|
80
53
|
* we reach connection limits
|
|
81
54
|
*/
|
|
82
|
-
_tagPeers(kBuck:
|
|
55
|
+
_tagPeers(kBuck: KBucket): void;
|
|
83
56
|
/**
|
|
84
57
|
* Called on the `ping` event from `k-bucket` when a bucket is full
|
|
85
58
|
* and cannot split.
|
|
@@ -90,7 +63,7 @@ export declare class RoutingTable implements Startable {
|
|
|
90
63
|
* `oldContacts` will not be empty and is the list of contacts that
|
|
91
64
|
* have not been contacted for the longest.
|
|
92
65
|
*/
|
|
93
|
-
_onPing(
|
|
66
|
+
_onPing(evt: CustomEvent<PingEventDetails>): void;
|
|
94
67
|
/**
|
|
95
68
|
* Amount of currently stored peers
|
|
96
69
|
*/
|
|
@@ -116,5 +89,4 @@ export declare class RoutingTable implements Startable {
|
|
|
116
89
|
*/
|
|
117
90
|
remove(peer: PeerId): Promise<void>;
|
|
118
91
|
}
|
|
119
|
-
export {};
|
|
120
92
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/routing-table/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/routing-table/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,SAAS,CAAA;AAE3B,OAAO,EAAE,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAU,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAG7D,eAAO,MAAM,kBAAkB,cAAc,CAAA;AAC7C,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,YAAY,KAAK,CAAA;AAC9B,eAAO,MAAM,YAAY,QAAQ,CAAA;AACjC,eAAO,MAAM,gBAAgB,KAAK,CAAA;AAElC,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,OAAO,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;CACl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AE,UAAU,GAAG,MAAM,GAAG,SAAS;IAUjD;;OAEG;IACH,YAAY,CAAE,GAAG,EAAE,UAAU,EAAE,KAAK,SAAmB,GAAG,MAAM,EAAE;IAUlE;;OAEG;IACG,GAAG,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAcvC;;OAEG;IACG,MAAM,CAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAW3C"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger';
|
|
2
2
|
import { PeerSet } from '@libp2p/peer-collections';
|
|
3
|
-
// @ts-expect-error no types
|
|
4
|
-
import KBuck from 'k-bucket';
|
|
5
3
|
import Queue from 'p-queue';
|
|
6
4
|
import * as utils from '../utils.js';
|
|
5
|
+
import { KBucket } from './k-bucket.js';
|
|
7
6
|
export const KAD_CLOSE_TAG_NAME = 'kad-close';
|
|
8
7
|
export const KAD_CLOSE_TAG_VALUE = 50;
|
|
9
8
|
export const KBUCKET_SIZE = 20;
|
|
@@ -60,14 +59,14 @@ export class RoutingTable {
|
|
|
60
59
|
pingRunning: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_ping_running`)
|
|
61
60
|
};
|
|
62
61
|
}
|
|
63
|
-
const kBuck = new
|
|
62
|
+
const kBuck = new KBucket({
|
|
64
63
|
localNodeId: await utils.convertPeerId(this.components.peerId),
|
|
65
64
|
numberOfNodesPerKBucket: this.kBucketSize,
|
|
66
65
|
numberOfNodesToPing: 1
|
|
67
66
|
});
|
|
68
67
|
this.kb = kBuck;
|
|
69
68
|
// test whether to evict peers
|
|
70
|
-
kBuck.
|
|
69
|
+
kBuck.addEventListener('ping', this._onPing);
|
|
71
70
|
// tag kad-close peers
|
|
72
71
|
this._tagPeers(kBuck);
|
|
73
72
|
}
|
|
@@ -111,10 +110,10 @@ export class RoutingTable {
|
|
|
111
110
|
});
|
|
112
111
|
kClosest = newClosest;
|
|
113
112
|
});
|
|
114
|
-
kBuck.
|
|
113
|
+
kBuck.addEventListener('added', () => {
|
|
115
114
|
updatePeerTags();
|
|
116
115
|
});
|
|
117
|
-
kBuck.
|
|
116
|
+
kBuck.addEventListener('removed', () => {
|
|
118
117
|
updatePeerTags();
|
|
119
118
|
});
|
|
120
119
|
}
|
|
@@ -128,7 +127,8 @@ export class RoutingTable {
|
|
|
128
127
|
* `oldContacts` will not be empty and is the list of contacts that
|
|
129
128
|
* have not been contacted for the longest.
|
|
130
129
|
*/
|
|
131
|
-
_onPing(
|
|
130
|
+
_onPing(evt) {
|
|
131
|
+
const { oldContacts, newContact } = evt.detail;
|
|
132
132
|
// add to a queue so multiple ping requests do not overlap and we don't
|
|
133
133
|
// flood the network with ping requests if lots of newContact requests
|
|
134
134
|
// are received
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/routing-table/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/routing-table/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,KAAK,MAAM,SAAS,CAAA;AAC3B,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,OAAO,EAAyB,MAAM,eAAe,CAAA;AAQ9D,MAAM,CAAC,MAAM,kBAAkB,GAAG,WAAW,CAAA;AAC7C,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAA;AACrC,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAA;AAC9B,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAA;AACjC,MAAM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C,IAAI,CAAC,CAAA;IACjC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG,CAAE,IAAY;QACrB,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;SAC/C;QAED,MAAM,EAAE,GAAG,MAAM,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;QAE1C,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;QAEzB,IAAI,CAAC,GAAG,CAAC,yBAAyB,EAAE,IAAI,EAAE,EAAE,CAAC,CAAA;QAE7C,IAAI,CAAC,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM,CAAE,IAAY;QACxB,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;SAC/C;QAED,MAAM,EAAE,GAAG,MAAM,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;QAE1C,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAElB,IAAI,CAAC,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClD,CAAC;CACF"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
2
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
3
|
+
export interface PingEventDetails {
|
|
4
|
+
oldContacts: Contact[];
|
|
5
|
+
newContact: Contact;
|
|
6
|
+
}
|
|
7
|
+
export interface UpdatedEventDetails {
|
|
8
|
+
incumbent: Contact;
|
|
9
|
+
selection: Contact;
|
|
10
|
+
}
|
|
11
|
+
export interface KBucketEvents {
|
|
12
|
+
'ping': CustomEvent<PingEventDetails>;
|
|
13
|
+
'added': CustomEvent<Contact>;
|
|
14
|
+
'removed': CustomEvent<Contact>;
|
|
15
|
+
'updated': CustomEvent<UpdatedEventDetails>;
|
|
16
|
+
}
|
|
17
|
+
export interface KBucketOptions {
|
|
18
|
+
/**
|
|
19
|
+
* A Uint8Array representing the local node id
|
|
20
|
+
*/
|
|
21
|
+
localNodeId: Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* The number of nodes that a k-bucket can contain before being full or split.
|
|
24
|
+
*/
|
|
25
|
+
numberOfNodesPerKBucket?: number;
|
|
26
|
+
/**
|
|
27
|
+
* The number of nodes to ping when a bucket that should not be split becomes
|
|
28
|
+
* full. KBucket will emit a `ping` event that contains `numberOfNodesToPing`
|
|
29
|
+
* nodes that have not been contacted the longest.
|
|
30
|
+
*/
|
|
31
|
+
numberOfNodesToPing?: number;
|
|
32
|
+
/**
|
|
33
|
+
* An optional `distance` function that gets two `id` Uint8Arrays and return
|
|
34
|
+
* distance (as number) between them.
|
|
35
|
+
*/
|
|
36
|
+
distance?: (a: Uint8Array, b: Uint8Array) => number;
|
|
37
|
+
/**
|
|
38
|
+
* An optional `arbiter` function that given two `contact` objects with the
|
|
39
|
+
* same `id` returns the desired object to be used for updating the k-bucket.
|
|
40
|
+
* For more details, see [arbiter function](#arbiter-function).
|
|
41
|
+
*/
|
|
42
|
+
arbiter?: (incumbent: Contact, candidate: Contact) => Contact;
|
|
43
|
+
}
|
|
44
|
+
export interface Contact {
|
|
45
|
+
id: Uint8Array;
|
|
46
|
+
peer: PeerId;
|
|
47
|
+
vectorClock?: number;
|
|
48
|
+
}
|
|
49
|
+
export interface Bucket {
|
|
50
|
+
id: Uint8Array;
|
|
51
|
+
contacts: Contact[];
|
|
52
|
+
dontSplit: boolean;
|
|
53
|
+
left: Bucket;
|
|
54
|
+
right: Bucket;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Implementation of a Kademlia DHT k-bucket used for storing
|
|
58
|
+
* contact (peer node) information.
|
|
59
|
+
*
|
|
60
|
+
* @extends EventEmitter
|
|
61
|
+
*/
|
|
62
|
+
export declare class KBucket extends EventEmitter<KBucketEvents> {
|
|
63
|
+
localNodeId: Uint8Array;
|
|
64
|
+
root: Bucket;
|
|
65
|
+
private readonly numberOfNodesPerKBucket;
|
|
66
|
+
private readonly numberOfNodesToPing;
|
|
67
|
+
private readonly distance;
|
|
68
|
+
private readonly arbiter;
|
|
69
|
+
constructor(options: KBucketOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Default arbiter function for contacts with the same id. Uses
|
|
72
|
+
* contact.vectorClock to select which contact to update the k-bucket with.
|
|
73
|
+
* Contact with larger vectorClock field will be selected. If vectorClock is
|
|
74
|
+
* the same, candidate will be selected.
|
|
75
|
+
*
|
|
76
|
+
* @param {object} incumbent - Contact currently stored in the k-bucket.
|
|
77
|
+
* @param {object} candidate - Contact being added to the k-bucket.
|
|
78
|
+
* @returns {object} Contact to updated the k-bucket with.
|
|
79
|
+
*/
|
|
80
|
+
static arbiter(incumbent: Contact, candidate: Contact): Contact;
|
|
81
|
+
/**
|
|
82
|
+
* Default distance function. Finds the XOR
|
|
83
|
+
* distance between firstId and secondId.
|
|
84
|
+
*
|
|
85
|
+
* @param {Uint8Array} firstId - Uint8Array containing first id.
|
|
86
|
+
* @param {Uint8Array} secondId - Uint8Array containing second id.
|
|
87
|
+
* @returns {number} Integer The XOR distance between firstId and secondId.
|
|
88
|
+
*/
|
|
89
|
+
static distance(firstId: Uint8Array, secondId: Uint8Array): number;
|
|
90
|
+
/**
|
|
91
|
+
* Adds a contact to the k-bucket.
|
|
92
|
+
*
|
|
93
|
+
* @param {object} contact - the contact object to add
|
|
94
|
+
*/
|
|
95
|
+
add(contact: Contact): KBucket;
|
|
96
|
+
/**
|
|
97
|
+
* Get the n closest contacts to the provided node id. "Closest" here means:
|
|
98
|
+
* closest according to the XOR metric of the contact node id.
|
|
99
|
+
*
|
|
100
|
+
* @param {Uint8Array} id - Contact node id
|
|
101
|
+
* @param {number} n - Integer (Default: Infinity) The maximum number of closest contacts to return
|
|
102
|
+
* @returns {Array} Array Maximum of n closest contacts to the node id
|
|
103
|
+
*/
|
|
104
|
+
closest(id: Uint8Array, n?: number): Contact[];
|
|
105
|
+
/**
|
|
106
|
+
* Counts the total number of contacts in the tree.
|
|
107
|
+
*
|
|
108
|
+
* @returns {number} The number of contacts held in the tree
|
|
109
|
+
*/
|
|
110
|
+
count(): number;
|
|
111
|
+
/**
|
|
112
|
+
* Determines whether the id at the bitIndex is 0 or 1.
|
|
113
|
+
* Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
|
|
114
|
+
*
|
|
115
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
116
|
+
* @param {Uint8Array} id - Id to compare localNodeId with.
|
|
117
|
+
* @param {number} bitIndex - Integer (Default: 0) The bit index to which bit to check in the id Uint8Array.
|
|
118
|
+
* @returns {object} left leaf if id at bitIndex is 0, right leaf otherwise.
|
|
119
|
+
*/
|
|
120
|
+
_determineNode(node: any, id: Uint8Array, bitIndex: number): Bucket;
|
|
121
|
+
/**
|
|
122
|
+
* Get a contact by its exact ID.
|
|
123
|
+
* If this is a leaf, loop through the bucket contents and return the correct
|
|
124
|
+
* contact if we have it or null if not. If this is an inner node, determine
|
|
125
|
+
* which branch of the tree to traverse and repeat.
|
|
126
|
+
*
|
|
127
|
+
* @param {Uint8Array} id - The ID of the contact to fetch.
|
|
128
|
+
* @returns {object | null} The contact if available, otherwise null
|
|
129
|
+
*/
|
|
130
|
+
get(id: Uint8Array): Contact | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* Returns the index of the contact with provided
|
|
133
|
+
* id if it exists, returns -1 otherwise.
|
|
134
|
+
*
|
|
135
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
136
|
+
* @param {Uint8Array} id - Contact node id.
|
|
137
|
+
* @returns {number} Integer Index of contact with provided id if it exists, -1 otherwise.
|
|
138
|
+
*/
|
|
139
|
+
_indexOf(node: Bucket, id: Uint8Array): number;
|
|
140
|
+
/**
|
|
141
|
+
* Removes contact with the provided id.
|
|
142
|
+
*
|
|
143
|
+
* @param {Uint8Array} id - The ID of the contact to remove
|
|
144
|
+
* @returns {object} The k-bucket itself
|
|
145
|
+
*/
|
|
146
|
+
remove(id: Uint8Array): KBucket;
|
|
147
|
+
/**
|
|
148
|
+
* Splits the node, redistributes contacts to the new nodes, and marks the
|
|
149
|
+
* node that was split as an inner node of the binary tree of nodes by
|
|
150
|
+
* setting this.root.contacts = null
|
|
151
|
+
*
|
|
152
|
+
* @param {object} node - node for splitting
|
|
153
|
+
* @param {number} bitIndex - the bitIndex to which byte to check in the Uint8Array for navigating the binary tree
|
|
154
|
+
*/
|
|
155
|
+
_split(node: Bucket, bitIndex: number): void;
|
|
156
|
+
/**
|
|
157
|
+
* Returns all the contacts contained in the tree as an array.
|
|
158
|
+
* If this is a leaf, return a copy of the bucket. If this is not a leaf,
|
|
159
|
+
* return the union of the low and high branches (themselves also as arrays).
|
|
160
|
+
*
|
|
161
|
+
* @returns {Array} All of the contacts in the tree, as an array
|
|
162
|
+
*/
|
|
163
|
+
toArray(): Contact[];
|
|
164
|
+
/**
|
|
165
|
+
* Similar to `toArray()` but instead of buffering everything up into an
|
|
166
|
+
* array before returning it, yields contacts as they are encountered while
|
|
167
|
+
* walking the tree.
|
|
168
|
+
*
|
|
169
|
+
* @returns {Iterable} All of the contacts in the tree, as an iterable
|
|
170
|
+
*/
|
|
171
|
+
toIterable(): Iterable<Contact>;
|
|
172
|
+
/**
|
|
173
|
+
* Updates the contact selected by the arbiter.
|
|
174
|
+
* If the selection is our old contact and the candidate is some new contact
|
|
175
|
+
* then the new contact is abandoned (not added).
|
|
176
|
+
* If the selection is our old contact and the candidate is our old contact
|
|
177
|
+
* then we are refreshing the contact and it is marked as most recently
|
|
178
|
+
* contacted (by being moved to the right/end of the bucket array).
|
|
179
|
+
* If the selection is our new contact, the old contact is removed and the new
|
|
180
|
+
* contact is marked as most recently contacted.
|
|
181
|
+
*
|
|
182
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
183
|
+
* @param {number} index - the index in the bucket where contact exists (index has already been computed in a previous calculation)
|
|
184
|
+
* @param {object} contact - The contact object to update
|
|
185
|
+
*/
|
|
186
|
+
_update(node: Bucket, index: number, contact: Contact): void;
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=k-bucket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"k-bucket.d.ts","sourceRoot":"","sources":["../../../src/routing-table/k-bucket.ts"],"names":[],"mappings":"AA6BA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AA4BvD,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,OAAO,EAAE,CAAA;IACtB,UAAU,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,OAAO,CAAA;IAClB,SAAS,EAAE,OAAO,CAAA;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,WAAW,CAAC,gBAAgB,CAAC,CAAA;IACrC,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC7B,SAAS,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC/B,SAAS,EAAE,WAAW,CAAC,mBAAmB,CAAC,CAAA;CAC5C;AAED,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,WAAW,EAAE,UAAU,CAAA;IAEvB;;OAEG;IACH,uBAAuB,CAAC,EAAE,MAAM,CAAA;IAEhC;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAE5B;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,KAAK,MAAM,CAAA;IAEnD;;;;OAIG;IACH,OAA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