peerpigeon 0.0.1
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 +21 -0
- package/README.md +1187 -0
- package/examples/browser/app-dht-disabled.js +42 -0
- package/examples/browser/app.js +51 -0
- package/examples/browser/assets/images/favicon.png +0 -0
- package/examples/browser/assets/images/pigeon.png +0 -0
- package/examples/browser/index-dht-disabled.html +123 -0
- package/examples/browser/index.html +340 -0
- package/examples/browser/styles.css +1288 -0
- package/examples/browser/ui-clean.js +0 -0
- package/examples/browser/ui.js +1930 -0
- package/index.js +8 -0
- package/package.json +70 -0
- package/server/index.js +515 -0
- package/src/CleanupManager.js +181 -0
- package/src/ConnectionManager.js +841 -0
- package/src/EventEmitter.js +27 -0
- package/src/EvictionManager.js +257 -0
- package/src/GossipManager.js +416 -0
- package/src/MediaManager.js +225 -0
- package/src/MeshOptimizer.js +181 -0
- package/src/PeerConnection.js +831 -0
- package/src/PeerDiscovery.js +297 -0
- package/src/PeerPigeonMesh.js +703 -0
- package/src/SignalingClient.js +536 -0
- package/src/SignalingHandler.js +325 -0
- package/src/StorageManager.js +88 -0
- package/src/TimerUtils.js +46 -0
- package/src/WebDHT.js +1099 -0
- package/websocket-server/README.md +257 -0
- package/websocket-server/memory-leak-test.js +193 -0
- package/websocket-server/node_modules/.package-lock.json +29 -0
- package/websocket-server/node_modules/ws/LICENSE +20 -0
- package/websocket-server/node_modules/ws/README.md +548 -0
- package/websocket-server/node_modules/ws/browser.js +8 -0
- package/websocket-server/node_modules/ws/index.js +13 -0
- package/websocket-server/node_modules/ws/lib/buffer-util.js +131 -0
- package/websocket-server/node_modules/ws/lib/constants.js +18 -0
- package/websocket-server/node_modules/ws/lib/event-target.js +292 -0
- package/websocket-server/node_modules/ws/lib/extension.js +203 -0
- package/websocket-server/node_modules/ws/lib/limiter.js +55 -0
- package/websocket-server/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/websocket-server/node_modules/ws/lib/receiver.js +706 -0
- package/websocket-server/node_modules/ws/lib/sender.js +602 -0
- package/websocket-server/node_modules/ws/lib/stream.js +161 -0
- package/websocket-server/node_modules/ws/lib/subprotocol.js +62 -0
- package/websocket-server/node_modules/ws/lib/validation.js +152 -0
- package/websocket-server/node_modules/ws/lib/websocket-server.js +550 -0
- package/websocket-server/node_modules/ws/lib/websocket.js +1388 -0
- package/websocket-server/node_modules/ws/package.json +69 -0
- package/websocket-server/node_modules/ws/wrapper.mjs +8 -0
- package/websocket-server/package-lock.json +39 -0
- package/websocket-server/server.js +298 -0
- package/websocket-server/test.js +133 -0
package/src/WebDHT.js
ADDED
|
@@ -0,0 +1,1099 @@
|
|
|
1
|
+
import { EventEmitter } from './EventEmitter.js';
|
|
2
|
+
import { safeSetInterval, safeClearInterval, safeSetTimeout, safeClearTimeout } from './TimerUtils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WebDHT - A Kademlia-like Distributed Hash Table for WebRTC mesh networks
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Key-value storage with closest peer routing
|
|
9
|
+
* - Subscribe/unsubscribe to key changes
|
|
10
|
+
* - Automatic replication and fault tolerance
|
|
11
|
+
* - XOR distance-based routing
|
|
12
|
+
*/
|
|
13
|
+
export class WebDHT extends EventEmitter {
|
|
14
|
+
constructor(mesh) {
|
|
15
|
+
super();
|
|
16
|
+
this.mesh = mesh;
|
|
17
|
+
this.peerId = mesh.peerId;
|
|
18
|
+
|
|
19
|
+
// Local storage for key-value pairs this peer is responsible for
|
|
20
|
+
this.localStorage = new Map();
|
|
21
|
+
|
|
22
|
+
// Subscriptions: key -> Set of peer IDs that want notifications
|
|
23
|
+
this.subscriptions = new Map();
|
|
24
|
+
|
|
25
|
+
// Active subscriptions from this peer to others
|
|
26
|
+
this.mySubscriptions = new Set();
|
|
27
|
+
|
|
28
|
+
// Routing table for faster lookups (k-buckets)
|
|
29
|
+
this.routingTable = new Map();
|
|
30
|
+
|
|
31
|
+
// Configuration
|
|
32
|
+
this.config = {
|
|
33
|
+
k: 20, // Bucket size (number of peers per bucket)
|
|
34
|
+
alpha: 3, // Parallelism factor for lookups
|
|
35
|
+
replicationFactor: 3, // Number of peers to store each key
|
|
36
|
+
ttl: null, // Default TTL: null = no expiration (records persist indefinitely)
|
|
37
|
+
refreshInterval: 300000 // Refresh interval (5 minutes)
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Setup message handlers
|
|
41
|
+
this.setupMessageHandlers();
|
|
42
|
+
|
|
43
|
+
// Start periodic maintenance
|
|
44
|
+
this.startMaintenance();
|
|
45
|
+
|
|
46
|
+
console.log(`WebDHT initialized for peer ${this.peerId.substring(0, 8)}...`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Setup message handlers for DHT operations
|
|
51
|
+
*/
|
|
52
|
+
setupMessageHandlers() {
|
|
53
|
+
console.log(`🔥 DHT Setting up message handlers for peer ${this.peerId.substring(0, 8)}`);
|
|
54
|
+
|
|
55
|
+
// DHT messages now come through ConnectionManager's handleIncomingMessage
|
|
56
|
+
// which routes 'dht' type messages to this.mesh.webDHT.handleMessage
|
|
57
|
+
// So we don't need to listen to gossip manager events anymore
|
|
58
|
+
|
|
59
|
+
console.log(`🔥 DHT Message handlers setup complete for peer ${this.peerId.substring(0, 8)}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Handle incoming DHT messages - called by ConnectionManager
|
|
64
|
+
*/
|
|
65
|
+
async handleMessage(message, fromPeerId) {
|
|
66
|
+
console.log(`🔥 DHT Message received from ${fromPeerId.substring(0, 8)}:`, message);
|
|
67
|
+
|
|
68
|
+
// Extract the DHT operation type and data
|
|
69
|
+
const type = message.messageType || message.type;
|
|
70
|
+
const data = message.data;
|
|
71
|
+
|
|
72
|
+
if (!type || !data) {
|
|
73
|
+
console.error(`🔥 DHT Invalid message structure from ${fromPeerId.substring(0, 8)}:`, message);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log(`🔥 DHT Processing ${type} from ${fromPeerId.substring(0, 8)}, data:`, data);
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
switch (type) {
|
|
81
|
+
case 'store':
|
|
82
|
+
console.log(`🔥 DHT Handling store request from ${fromPeerId.substring(0, 8)}`);
|
|
83
|
+
await this.handleStoreRequest(fromPeerId, data);
|
|
84
|
+
break;
|
|
85
|
+
case 'find_value':
|
|
86
|
+
console.log(`🔥 DHT Handling find_value request from ${fromPeerId.substring(0, 8)}`);
|
|
87
|
+
await this.handleFindValueRequest(fromPeerId, data);
|
|
88
|
+
break;
|
|
89
|
+
case 'find_node':
|
|
90
|
+
console.log(`🔥 DHT Handling find_node request from ${fromPeerId.substring(0, 8)}`);
|
|
91
|
+
await this.handleFindNodeRequest(fromPeerId, data);
|
|
92
|
+
break;
|
|
93
|
+
case 'subscribe':
|
|
94
|
+
console.log(`🔥 DHT Handling subscribe request from ${fromPeerId.substring(0, 8)}`);
|
|
95
|
+
await this.handleSubscribeRequest(fromPeerId, data);
|
|
96
|
+
break;
|
|
97
|
+
case 'unsubscribe':
|
|
98
|
+
console.log(`🔥 DHT Handling unsubscribe request from ${fromPeerId.substring(0, 8)}`);
|
|
99
|
+
await this.handleUnsubscribeRequest(fromPeerId, data);
|
|
100
|
+
break;
|
|
101
|
+
case 'value_changed':
|
|
102
|
+
console.log(`🔥 DHT Handling value_changed notification from ${fromPeerId.substring(0, 8)}`);
|
|
103
|
+
await this.handleValueChangedNotification(fromPeerId, data);
|
|
104
|
+
break;
|
|
105
|
+
case 'update_value':
|
|
106
|
+
console.log(`🔥 DHT Handling update_value request from ${fromPeerId.substring(0, 8)}`);
|
|
107
|
+
await this.handleUpdateValueRequest(fromPeerId, data);
|
|
108
|
+
break;
|
|
109
|
+
case 'store_response':
|
|
110
|
+
case 'find_value_response':
|
|
111
|
+
case 'find_node_response':
|
|
112
|
+
case 'update_value_response':
|
|
113
|
+
console.log(`🔥 DHT Handling response (${type}) from ${fromPeerId.substring(0, 8)}`);
|
|
114
|
+
this.handleResponse(fromPeerId, data);
|
|
115
|
+
break;
|
|
116
|
+
default:
|
|
117
|
+
console.warn(`Unknown DHT message type: ${type}`);
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error(`🔥 DHT Error handling message type ${type} from ${fromPeerId.substring(0, 8)}:`, error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Store a key-value pair in the DHT
|
|
126
|
+
*/
|
|
127
|
+
async put(key, value, options = {}) {
|
|
128
|
+
const keyId = await this.generateKeyId(key);
|
|
129
|
+
const ttl = options.ttl || this.config.ttl;
|
|
130
|
+
|
|
131
|
+
console.log(`DHT PUT: ${key} -> ${keyId.substring(0, 8)}...`);
|
|
132
|
+
|
|
133
|
+
const storeData = {
|
|
134
|
+
key,
|
|
135
|
+
keyId,
|
|
136
|
+
value,
|
|
137
|
+
timestamp: Date.now(),
|
|
138
|
+
ttl,
|
|
139
|
+
publisher: this.peerId
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// CRITICAL FIX: Always store locally first (the originator always keeps a copy)
|
|
143
|
+
this.storeLocally(keyId, storeData);
|
|
144
|
+
console.log(`DHT PUT: Stored locally: ${key}`);
|
|
145
|
+
|
|
146
|
+
// AUTO-SUBSCRIBE: When putting a value, automatically subscribe to future changes
|
|
147
|
+
this.mySubscriptions.add(keyId);
|
|
148
|
+
console.log(`DHT PUT: Auto-subscribed to ${key} for future updates`);
|
|
149
|
+
|
|
150
|
+
// Find the closest peers to store this key for replication
|
|
151
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.k); // Get more peers initially
|
|
152
|
+
|
|
153
|
+
// Implement iterative storage for better replication success
|
|
154
|
+
return await this.performIterativePut(keyId, storeData, closestPeers);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Retrieve a value from the DHT
|
|
159
|
+
*/
|
|
160
|
+
async get(key, options = {}) {
|
|
161
|
+
const keyId = await this.generateKeyId(key);
|
|
162
|
+
// ALWAYS subscribe when doing a get() - this is the desired behavior
|
|
163
|
+
const subscribe = true;
|
|
164
|
+
const forceRefresh = options.forceRefresh || false;
|
|
165
|
+
|
|
166
|
+
console.log(`DHT GET: ${key} -> ${keyId.substring(0, 8)}... (auto-subscribing, forceRefresh: ${forceRefresh})`);
|
|
167
|
+
|
|
168
|
+
// Check local storage FIRST (unless force refresh is requested)
|
|
169
|
+
if (!forceRefresh && this.localStorage.has(keyId)) {
|
|
170
|
+
const data = this.localStorage.get(keyId);
|
|
171
|
+
if (!this.isExpired(data)) {
|
|
172
|
+
console.log(`DHT GET: Found locally: ${key} (using cached data)`);
|
|
173
|
+
// Always add subscription even for local data
|
|
174
|
+
this.mySubscriptions.add(keyId);
|
|
175
|
+
return data.value;
|
|
176
|
+
} else {
|
|
177
|
+
// Remove expired data
|
|
178
|
+
this.localStorage.delete(keyId);
|
|
179
|
+
console.log(`DHT GET: Removed expired local data for: ${key}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (forceRefresh) {
|
|
184
|
+
console.log(`DHT GET: Force refresh requested - bypassing local cache, fetching from original storing peers`);
|
|
185
|
+
} else {
|
|
186
|
+
console.log(`DHT GET: Not found locally, routing to network for key: ${key}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Implement iterative Kademlia lookup with automatic subscription
|
|
190
|
+
return await this.performIterativeGet(keyId, key, subscribe);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Subscribe to changes for a key
|
|
195
|
+
* NOTE: get() operations automatically subscribe, so this is mainly for explicit subscription without retrieval
|
|
196
|
+
*/
|
|
197
|
+
async subscribe(key) {
|
|
198
|
+
const keyId = await this.generateKeyId(key);
|
|
199
|
+
|
|
200
|
+
console.log(`DHT EXPLICIT SUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
|
|
201
|
+
|
|
202
|
+
// Add to our subscriptions immediately
|
|
203
|
+
this.mySubscriptions.add(keyId);
|
|
204
|
+
|
|
205
|
+
// Try to get the current value (which will also ensure we're subscribed on storage peers)
|
|
206
|
+
const currentValue = await this.get(key);
|
|
207
|
+
|
|
208
|
+
console.log(`DHT SUBSCRIBE: Subscribed to ${key}, current value:`, currentValue);
|
|
209
|
+
return currentValue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Unsubscribe from changes for a key
|
|
214
|
+
*/
|
|
215
|
+
async unsubscribe(key) {
|
|
216
|
+
const keyId = await this.generateKeyId(key);
|
|
217
|
+
|
|
218
|
+
if (!this.mySubscriptions.has(keyId)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Find the peers responsible for this key and unsubscribe
|
|
223
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
|
|
224
|
+
|
|
225
|
+
const unsubscribePromises = closestPeers.map(async (peerId) => {
|
|
226
|
+
if (peerId !== this.peerId) {
|
|
227
|
+
return this.sendDHTMessage(peerId, 'unsubscribe', {
|
|
228
|
+
keyId,
|
|
229
|
+
key,
|
|
230
|
+
subscriber: this.peerId
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}).filter(Boolean);
|
|
234
|
+
|
|
235
|
+
await Promise.allSettled(unsubscribePromises);
|
|
236
|
+
this.mySubscriptions.delete(keyId);
|
|
237
|
+
|
|
238
|
+
// Remove local subscription if we're storing this key
|
|
239
|
+
if (this.subscriptions.has(keyId)) {
|
|
240
|
+
this.subscriptions.get(keyId).delete(this.peerId);
|
|
241
|
+
if (this.subscriptions.get(keyId).size === 0) {
|
|
242
|
+
this.subscriptions.delete(keyId);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
console.log(`DHT UNSUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Update a key's value and notify all replicas and subscribers
|
|
251
|
+
*/
|
|
252
|
+
async update(key, newValue, options = {}) {
|
|
253
|
+
const keyId = await this.generateKeyId(key);
|
|
254
|
+
|
|
255
|
+
console.log(`DHT UPDATE: Updating ${key} with new value across all replicas and subscribers`);
|
|
256
|
+
|
|
257
|
+
// AUTO-SUBSCRIBE: When updating a value, automatically subscribe to future changes
|
|
258
|
+
this.mySubscriptions.add(keyId);
|
|
259
|
+
console.log(`DHT UPDATE: Auto-subscribed to ${key} for future updates`);
|
|
260
|
+
|
|
261
|
+
// Find all peers that should store this key (closest peers)
|
|
262
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
|
|
263
|
+
|
|
264
|
+
// Create update data
|
|
265
|
+
const updateData = {
|
|
266
|
+
key,
|
|
267
|
+
keyId,
|
|
268
|
+
value: newValue,
|
|
269
|
+
timestamp: Date.now(),
|
|
270
|
+
ttl: options.ttl || this.config.ttl,
|
|
271
|
+
publisher: this.peerId
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
console.log(`DHT UPDATE: Target replica peers for ${key}:`, closestPeers.map(p => p.substring(0, 8)));
|
|
275
|
+
|
|
276
|
+
// CRITICAL FIX: Always update locally first if we're among the closest
|
|
277
|
+
let localUpdateSuccess = false;
|
|
278
|
+
if (this.isAmongClosest(keyId, closestPeers)) {
|
|
279
|
+
this.storeLocally(keyId, updateData);
|
|
280
|
+
localUpdateSuccess = true;
|
|
281
|
+
console.log(`DHT UPDATE: Updated ${key} locally (we are replica peer)`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// CRITICAL FIX: Send update to ALL replica peers and wait for ALL to succeed
|
|
285
|
+
const updatePromises = closestPeers.map(async (peerId) => {
|
|
286
|
+
if (peerId !== this.peerId) {
|
|
287
|
+
try {
|
|
288
|
+
console.log(`DHT UPDATE: Sending update to replica peer ${peerId.substring(0, 8)}`);
|
|
289
|
+
const response = await this.sendDHTMessage(peerId, 'update_value', updateData);
|
|
290
|
+
if (response && response.success) {
|
|
291
|
+
console.log(`DHT UPDATE: Successfully updated replica peer ${peerId.substring(0, 8)}`);
|
|
292
|
+
return { peerId, success: true };
|
|
293
|
+
} else {
|
|
294
|
+
console.warn(`DHT UPDATE: Replica peer ${peerId.substring(0, 8)} rejected update:`, response);
|
|
295
|
+
return { peerId, success: false, response };
|
|
296
|
+
}
|
|
297
|
+
} catch (error) {
|
|
298
|
+
console.warn(`DHT UPDATE: Failed to update replica peer ${peerId.substring(0, 8)}:`, error.message);
|
|
299
|
+
return { peerId, success: false, error: error.message };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return null;
|
|
303
|
+
}).filter(Boolean);
|
|
304
|
+
|
|
305
|
+
const results = await Promise.allSettled(updatePromises);
|
|
306
|
+
const updateResults = results
|
|
307
|
+
.filter(r => r.status === 'fulfilled' && r.value)
|
|
308
|
+
.map(r => r.value);
|
|
309
|
+
|
|
310
|
+
const successfulUpdates = updateResults.filter(r => r.success).length;
|
|
311
|
+
const failedUpdates = updateResults.filter(r => !r.success);
|
|
312
|
+
|
|
313
|
+
console.log(`DHT UPDATE: Replica peer update results for ${key}:`);
|
|
314
|
+
console.log(` - Successful: ${successfulUpdates}`);
|
|
315
|
+
console.log(` - Failed: ${failedUpdates.length}`);
|
|
316
|
+
console.log(` - Local: ${localUpdateSuccess ? 'success' : 'not applicable'}`);
|
|
317
|
+
|
|
318
|
+
// ENHANCED: Log failed updates for debugging
|
|
319
|
+
if (failedUpdates.length > 0) {
|
|
320
|
+
console.warn(`DHT UPDATE: Failed to update these replica peers:`,
|
|
321
|
+
failedUpdates.map(f => ({ peer: f.peerId.substring(0, 8), reason: f.error || f.response })));
|
|
322
|
+
console.warn(`DHT UPDATE: This may lead to consistency issues. Consider implementing retry logic.`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// CRITICAL: Broadcast value change to ALL peers (including non-replicas)
|
|
326
|
+
// This ensures any peer with cached values gets the update immediately
|
|
327
|
+
await this.broadcastValueChange(key, keyId, newValue, updateData.timestamp);
|
|
328
|
+
|
|
329
|
+
// SUCCESS CRITERIA: ALL replica peers must be updated for consistency
|
|
330
|
+
// This prevents the case where a later GET operation hits a replica peer with stale data
|
|
331
|
+
const totalSuccessful = (localUpdateSuccess ? 1 : 0) + successfulUpdates;
|
|
332
|
+
const totalReplicas = closestPeers.length;
|
|
333
|
+
const wasSuccessful = totalSuccessful >= totalReplicas;
|
|
334
|
+
|
|
335
|
+
if (!wasSuccessful) {
|
|
336
|
+
console.warn(`DHT UPDATE: ${key} update FAILED - only ${totalSuccessful}/${totalReplicas} replicas updated successfully`);
|
|
337
|
+
console.warn(`DHT UPDATE: This may cause consistency issues where GET operations return stale data`);
|
|
338
|
+
} else {
|
|
339
|
+
console.log(`DHT UPDATE: ${key} update SUCCESSFUL - all ${totalSuccessful}/${totalReplicas} replicas updated`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return wasSuccessful;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Broadcast value change to ALL peers in the network
|
|
347
|
+
* This ensures both subscribers and peers with cached values are updated
|
|
348
|
+
*/
|
|
349
|
+
async broadcastValueChange(key, keyId, newValue, timestamp) {
|
|
350
|
+
// Get ALL connected peers, not just subscribers
|
|
351
|
+
const allPeers = this.mesh.connectionManager.getConnectedPeers().map(p => p.peerId);
|
|
352
|
+
|
|
353
|
+
console.log(`DHT UPDATE: Broadcasting value change for ${key} to ${allPeers.length} peers`);
|
|
354
|
+
|
|
355
|
+
const notificationPromises = allPeers.map(async (peerId) => {
|
|
356
|
+
try {
|
|
357
|
+
// Send notification to ALL peers - they'll decide if they care
|
|
358
|
+
return await this.sendDHTMessage(peerId, 'value_changed', {
|
|
359
|
+
key,
|
|
360
|
+
keyId,
|
|
361
|
+
newValue,
|
|
362
|
+
timestamp
|
|
363
|
+
});
|
|
364
|
+
} catch (error) {
|
|
365
|
+
console.warn(`DHT BROADCAST: Failed to notify peer ${peerId.substring(0, 8)}:`, error.message);
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const results = await Promise.allSettled(notificationPromises);
|
|
371
|
+
const successfulNotifications = results.filter(r => r.status === 'fulfilled' && r.value).length;
|
|
372
|
+
|
|
373
|
+
console.log(`DHT UPDATE: Broadcasted ${key} change to ${successfulNotifications}/${allPeers.length} peers`);
|
|
374
|
+
|
|
375
|
+
// Also emit local event if this peer has subscriptions
|
|
376
|
+
if (this.mySubscriptions.has(keyId)) {
|
|
377
|
+
this.emit('valueChanged', { key, keyId, newValue, timestamp });
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Handle store request from another peer
|
|
383
|
+
*/
|
|
384
|
+
async handleStoreRequest(fromPeerId, data) {
|
|
385
|
+
const { keyId, key, value, timestamp, ttl, publisher, messageId } = data;
|
|
386
|
+
|
|
387
|
+
console.log(`🔥 DHT STORE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
|
|
388
|
+
|
|
389
|
+
// Check if we should store this key (are we close enough?)
|
|
390
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
|
|
391
|
+
|
|
392
|
+
if (this.isAmongClosest(keyId, closestPeers)) {
|
|
393
|
+
this.storeLocally(keyId, { key, value, timestamp, ttl, publisher });
|
|
394
|
+
|
|
395
|
+
console.log(`🔥 DHT STORE: Accepting and storing ${key}, sending success response`);
|
|
396
|
+
|
|
397
|
+
// Send response using the same direct messaging structure
|
|
398
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
399
|
+
type: 'dht',
|
|
400
|
+
messageType: 'store_response',
|
|
401
|
+
data: {
|
|
402
|
+
keyId,
|
|
403
|
+
success: true,
|
|
404
|
+
storedBy: this.peerId,
|
|
405
|
+
messageId // Echo back the messageId for response matching
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
console.log(`DHT STORE: accepted ${key} from ${fromPeerId.substring(0, 8)}...`);
|
|
410
|
+
} else {
|
|
411
|
+
console.log(`🔥 DHT STORE: Rejecting ${key} (not among closest), sending failure response`);
|
|
412
|
+
|
|
413
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
414
|
+
type: 'dht',
|
|
415
|
+
messageType: 'store_response',
|
|
416
|
+
data: {
|
|
417
|
+
keyId,
|
|
418
|
+
success: false,
|
|
419
|
+
reason: 'not_closest',
|
|
420
|
+
closestPeers: closestPeers.slice(0, this.config.replicationFactor),
|
|
421
|
+
messageId // Echo back the messageId for response matching
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Handle find value request
|
|
429
|
+
*/
|
|
430
|
+
async handleFindValueRequest(fromPeerId, data) {
|
|
431
|
+
const { keyId, key, subscribe, requester, messageId } = data;
|
|
432
|
+
|
|
433
|
+
console.log(`🔥 DHT FIND_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
|
|
434
|
+
console.log(`🔥 DHT FIND_VALUE REQUEST: Full data received:`, data);
|
|
435
|
+
|
|
436
|
+
if (this.localStorage.has(keyId)) {
|
|
437
|
+
const storedData = this.localStorage.get(keyId);
|
|
438
|
+
|
|
439
|
+
if (!this.isExpired(storedData)) {
|
|
440
|
+
console.log(`🔥 DHT FIND_VALUE: Found ${key} locally, sending success response with messageId: ${messageId}`);
|
|
441
|
+
|
|
442
|
+
// Add subscription if requested
|
|
443
|
+
if (subscribe) {
|
|
444
|
+
this.addSubscription(keyId, requester);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// CRITICAL FIX: Use the request's messageId, not generate a new one
|
|
448
|
+
const responseData = {
|
|
449
|
+
keyId,
|
|
450
|
+
found: true,
|
|
451
|
+
messageId: messageId, // Echo back the EXACT messageId from request
|
|
452
|
+
key: storedData.key,
|
|
453
|
+
value: storedData.value,
|
|
454
|
+
timestamp: storedData.timestamp,
|
|
455
|
+
ttl: storedData.ttl,
|
|
456
|
+
publisher: storedData.publisher
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
console.log(`🔥 DHT FIND_VALUE: Sending response data with messageId ${messageId}:`, responseData);
|
|
460
|
+
|
|
461
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
462
|
+
type: 'dht',
|
|
463
|
+
messageType: 'find_value_response',
|
|
464
|
+
data: responseData
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
console.log(`DHT FIND_VALUE: found ${key} for ${fromPeerId.substring(0, 8)}...`);
|
|
468
|
+
return;
|
|
469
|
+
} else {
|
|
470
|
+
this.localStorage.delete(keyId);
|
|
471
|
+
console.log(`🔥 DHT FIND_VALUE: Found ${key} but expired, removed from storage`);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
console.log(`🔥 DHT FIND_VALUE: ${key} not found locally, sending closest peers response with messageId: ${messageId}`);
|
|
476
|
+
|
|
477
|
+
// If not found, return closest peers
|
|
478
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.k);
|
|
479
|
+
|
|
480
|
+
const responseData = {
|
|
481
|
+
keyId,
|
|
482
|
+
found: false,
|
|
483
|
+
messageId: messageId, // Echo back the EXACT messageId from request
|
|
484
|
+
closestPeers
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
console.log(`🔥 DHT FIND_VALUE: Sending not-found response data with messageId ${messageId}:`, responseData);
|
|
488
|
+
|
|
489
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
490
|
+
type: 'dht',
|
|
491
|
+
messageType: 'find_value_response',
|
|
492
|
+
data: responseData
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Handle subscribe request
|
|
498
|
+
*/
|
|
499
|
+
async handleSubscribeRequest(fromPeerId, data) {
|
|
500
|
+
const { keyId, key, subscriber } = data;
|
|
501
|
+
|
|
502
|
+
if (this.localStorage.has(keyId)) {
|
|
503
|
+
this.addSubscription(keyId, subscriber);
|
|
504
|
+
console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} notifications`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Handle unsubscribe request
|
|
510
|
+
*/
|
|
511
|
+
async handleUnsubscribeRequest(fromPeerId, data) {
|
|
512
|
+
const { keyId, key, subscriber } = data;
|
|
513
|
+
|
|
514
|
+
if (this.subscriptions.has(keyId)) {
|
|
515
|
+
this.subscriptions.get(keyId).delete(subscriber);
|
|
516
|
+
if (this.subscriptions.get(keyId).size === 0) {
|
|
517
|
+
this.subscriptions.delete(keyId);
|
|
518
|
+
}
|
|
519
|
+
console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} notifications`);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Handle value changed notification
|
|
525
|
+
*/
|
|
526
|
+
async handleValueChangedNotification(fromPeerId, data) {
|
|
527
|
+
const { key, keyId, newValue, timestamp } = data;
|
|
528
|
+
|
|
529
|
+
console.log(`🔥 DHT VALUE_CHANGED notification: ${key} = ${newValue} from ${fromPeerId.substring(0, 8)}`);
|
|
530
|
+
|
|
531
|
+
// CRITICAL FIX: Always update local cache if we have it, regardless of subscriptions
|
|
532
|
+
// This prevents stale cached values from being returned by get() operations
|
|
533
|
+
if (this.localStorage.has(keyId)) {
|
|
534
|
+
const storedData = this.localStorage.get(keyId);
|
|
535
|
+
// Only update if the new timestamp is newer (prevent old notifications from overwriting newer data)
|
|
536
|
+
if (!storedData.timestamp || timestamp >= storedData.timestamp) {
|
|
537
|
+
storedData.value = newValue;
|
|
538
|
+
storedData.timestamp = timestamp;
|
|
539
|
+
console.log(`🔥 DHT VALUE_CHANGED: Updated local cache for ${key} (timestamp: ${timestamp})`);
|
|
540
|
+
} else {
|
|
541
|
+
console.log(`🔥 DHT VALUE_CHANGED: Ignoring older notification for ${key} (current: ${storedData.timestamp}, notification: ${timestamp})`);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// Check if we have subscribers for this key
|
|
546
|
+
let hasSubscribers = this.mySubscriptions.has(keyId);
|
|
547
|
+
let hasLocalSubscribers = this.subscriptions.has(keyId) && this.subscriptions.get(keyId).size > 0;
|
|
548
|
+
|
|
549
|
+
if (hasSubscribers || hasLocalSubscribers) {
|
|
550
|
+
console.log(`🔥 DHT VALUE_CHANGED: Processing notification for ${key} (mySubscriptions: ${hasSubscribers}, localSubscribers: ${hasLocalSubscribers})`);
|
|
551
|
+
|
|
552
|
+
// Emit change event if this peer is subscribed
|
|
553
|
+
if (hasSubscribers) {
|
|
554
|
+
this.emit('valueChanged', { key, keyId, newValue, timestamp });
|
|
555
|
+
console.log(`🔥 DHT VALUE_CHANGED: Emitted local event for ${key}`);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Forward to local subscribers (other peers that subscribed through this peer)
|
|
559
|
+
if (hasLocalSubscribers) {
|
|
560
|
+
const localSubscribers = Array.from(this.subscriptions.get(keyId));
|
|
561
|
+
console.log(`🔥 DHT VALUE_CHANGED: Forwarding to ${localSubscribers.length} local subscribers for ${key}`);
|
|
562
|
+
|
|
563
|
+
const forwardPromises = localSubscribers.map(async (subscriberId) => {
|
|
564
|
+
if (subscriberId !== this.peerId && subscriberId !== fromPeerId) {
|
|
565
|
+
try {
|
|
566
|
+
return await this.sendDHTMessage(subscriberId, 'value_changed', {
|
|
567
|
+
key,
|
|
568
|
+
keyId,
|
|
569
|
+
newValue,
|
|
570
|
+
timestamp
|
|
571
|
+
});
|
|
572
|
+
} catch (error) {
|
|
573
|
+
console.warn(`Failed to forward notification to ${subscriberId.substring(0, 8)}:`, error.message);
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}).filter(Boolean);
|
|
578
|
+
|
|
579
|
+
await Promise.allSettled(forwardPromises);
|
|
580
|
+
}
|
|
581
|
+
} else {
|
|
582
|
+
console.log(`🔥 DHT VALUE_CHANGED: No subscriptions for ${key}, but updated cache anyway`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Handle update value request - used to propagate updates to replica peers
|
|
588
|
+
*/
|
|
589
|
+
async handleUpdateValueRequest(fromPeerId, data) {
|
|
590
|
+
const { keyId, key, value, timestamp, ttl, publisher, messageId } = data;
|
|
591
|
+
|
|
592
|
+
console.log(`🔥 DHT UPDATE_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}`);
|
|
593
|
+
|
|
594
|
+
// Check if we should store this key (are we among the closest?)
|
|
595
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
|
|
596
|
+
|
|
597
|
+
if (this.isAmongClosest(keyId, closestPeers)) {
|
|
598
|
+
// Update our local copy
|
|
599
|
+
this.storeLocally(keyId, { key, value, timestamp, ttl, publisher });
|
|
600
|
+
console.log(`DHT UPDATE_VALUE: Updated ${key} locally`);
|
|
601
|
+
|
|
602
|
+
// Notify any local subscribers
|
|
603
|
+
if (this.subscriptions.has(keyId)) {
|
|
604
|
+
const subscribers = Array.from(this.subscriptions.get(keyId));
|
|
605
|
+
console.log(`DHT UPDATE_VALUE: Notifying ${subscribers.length} local subscribers for ${key}`);
|
|
606
|
+
|
|
607
|
+
const notificationPromises = subscribers.map(async (subscriberId) => {
|
|
608
|
+
if (subscriberId !== this.peerId) {
|
|
609
|
+
try {
|
|
610
|
+
return await this.sendDHTMessage(subscriberId, 'value_changed', {
|
|
611
|
+
key,
|
|
612
|
+
keyId,
|
|
613
|
+
newValue: value,
|
|
614
|
+
timestamp
|
|
615
|
+
});
|
|
616
|
+
} catch (error) {
|
|
617
|
+
console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)}:`, error.message);
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}).filter(Boolean);
|
|
622
|
+
|
|
623
|
+
await Promise.allSettled(notificationPromises);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// Send success response
|
|
627
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
628
|
+
type: 'dht',
|
|
629
|
+
messageType: 'update_value_response',
|
|
630
|
+
data: {
|
|
631
|
+
keyId,
|
|
632
|
+
success: true,
|
|
633
|
+
updatedBy: this.peerId,
|
|
634
|
+
messageId
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
} else {
|
|
638
|
+
console.log(`DHT UPDATE_VALUE: Not responsible for ${key}, rejecting update`);
|
|
639
|
+
|
|
640
|
+
// Send failure response
|
|
641
|
+
this.sendDirectToPeer(fromPeerId, {
|
|
642
|
+
type: 'dht',
|
|
643
|
+
messageType: 'update_value_response',
|
|
644
|
+
data: {
|
|
645
|
+
keyId,
|
|
646
|
+
success: false,
|
|
647
|
+
reason: 'not_closest',
|
|
648
|
+
messageId
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Send a DHT message to a specific peer using direct P2P connection
|
|
656
|
+
*/
|
|
657
|
+
async sendDHTMessage(targetPeerId, type, data) {
|
|
658
|
+
return new Promise((resolve, reject) => {
|
|
659
|
+
const messageId = this.generateMessageId();
|
|
660
|
+
|
|
661
|
+
console.log(`🔥 DHT Sending ${type} to ${targetPeerId.substring(0, 8)} with messageId ${messageId}`);
|
|
662
|
+
console.log(`🔥 DHT Request data being sent:`, { ...data, messageId });
|
|
663
|
+
|
|
664
|
+
// Set up response handler
|
|
665
|
+
const timeout = safeSetTimeout(() => {
|
|
666
|
+
console.log(`🔥 DHT Timeout for messageId ${messageId} to peer ${targetPeerId.substring(0, 8)}`);
|
|
667
|
+
this.removeResponseHandler(messageId);
|
|
668
|
+
reject(new Error('DHT message timeout'));
|
|
669
|
+
}, 5000);
|
|
670
|
+
|
|
671
|
+
this.setResponseHandler(messageId, (response) => {
|
|
672
|
+
console.log(`🔥 DHT Response received for ${type} messageId ${messageId}:`, response);
|
|
673
|
+
safeClearTimeout(timeout);
|
|
674
|
+
resolve(response);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
// Send message directly via peer-to-peer connection with proper message structure
|
|
678
|
+
const messageToSend = {
|
|
679
|
+
type: 'dht', // This tells ConnectionManager to route to DHT
|
|
680
|
+
data: { ...data, messageId },
|
|
681
|
+
messageType: type // The actual DHT operation type
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
console.log(`🔥 DHT Full message being sent to ${targetPeerId.substring(0, 8)}:`, messageToSend);
|
|
685
|
+
|
|
686
|
+
const success = this.sendDirectToPeer(targetPeerId, messageToSend);
|
|
687
|
+
|
|
688
|
+
if (!success) {
|
|
689
|
+
console.log(`🔥 DHT Failed to send to ${targetPeerId.substring(0, 8)}, cleaning up messageId ${messageId}`);
|
|
690
|
+
safeClearTimeout(timeout);
|
|
691
|
+
this.removeResponseHandler(messageId);
|
|
692
|
+
reject(new Error('Failed to send DHT message - no direct connection'));
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Send a message directly to a peer using the data channel or route through mesh
|
|
699
|
+
* This implements proper Kademlia routing for peers that aren't directly connected
|
|
700
|
+
*/
|
|
701
|
+
sendDirectToPeer(targetPeerId, message) {
|
|
702
|
+
console.log(`DHT: Attempting to send message to ${targetPeerId.substring(0, 8)}:`, message.type);
|
|
703
|
+
|
|
704
|
+
// First try direct connection if available
|
|
705
|
+
const success = this.mesh.connectionManager.sendDirectMessage(targetPeerId, message);
|
|
706
|
+
|
|
707
|
+
if (success) {
|
|
708
|
+
console.log(`DHT: Successfully sent direct message to ${targetPeerId.substring(0, 8)}`);
|
|
709
|
+
return true;
|
|
710
|
+
} else {
|
|
711
|
+
console.log(`DHT: No direct connection to ${targetPeerId.substring(0, 8)}, using Kademlia routing`);
|
|
712
|
+
|
|
713
|
+
// Use gossip/routing mechanism to reach the target peer
|
|
714
|
+
// Create a special DHT routing message that will be forwarded through the mesh
|
|
715
|
+
const routingMessage = {
|
|
716
|
+
id: this.generateMessageId(),
|
|
717
|
+
from: this.peerId,
|
|
718
|
+
to: targetPeerId, // Target peer ID for routing
|
|
719
|
+
subtype: 'dht-routing', // Special subtype for DHT routing
|
|
720
|
+
content: message, // The actual DHT message to deliver
|
|
721
|
+
timestamp: Date.now(),
|
|
722
|
+
ttl: 5, // Allow up to 5 hops to reach the target
|
|
723
|
+
path: [this.peerId] // Track routing path
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
console.log(`DHT: Routing message to ${targetPeerId.substring(0, 8)} via gossip network`);
|
|
727
|
+
this.mesh.gossipManager.propagateMessage(routingMessage);
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Find the closest peers to a given key ID
|
|
734
|
+
*/
|
|
735
|
+
async findClosestPeers(keyId, count) {
|
|
736
|
+
const allPeers = [this.peerId, ...this.mesh.connectionManager.getConnectedPeers().map(p => p.peerId)];
|
|
737
|
+
|
|
738
|
+
console.log(`DHT findClosestPeers: Looking for ${count} closest peers to key ${keyId.substring(0, 8)}... from ${allPeers.length} total peers`);
|
|
739
|
+
|
|
740
|
+
// Sort by XOR distance to the key
|
|
741
|
+
allPeers.sort((a, b) => {
|
|
742
|
+
const distA = this.xorDistance(keyId, a);
|
|
743
|
+
const distB = this.xorDistance(keyId, b);
|
|
744
|
+
return distA.localeCompare(distB);
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
const result = allPeers.slice(0, count);
|
|
748
|
+
|
|
749
|
+
console.log(`DHT findClosestPeers result:`, result.map(peerId => ({
|
|
750
|
+
peerId: peerId.substring(0, 8) + '...',
|
|
751
|
+
distance: this.xorDistance(keyId, peerId).substring(0, 8) + '...',
|
|
752
|
+
isSelf: peerId === this.peerId
|
|
753
|
+
})));
|
|
754
|
+
|
|
755
|
+
return result;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Calculate XOR distance between two IDs
|
|
760
|
+
*/
|
|
761
|
+
xorDistance(id1, id2) {
|
|
762
|
+
// Convert hex strings to numbers and XOR them
|
|
763
|
+
let distance = '';
|
|
764
|
+
for (let i = 0; i < Math.min(id1.length, id2.length); i++) {
|
|
765
|
+
const xor = parseInt(id1[i], 16) ^ parseInt(id2[i], 16);
|
|
766
|
+
distance += xor.toString(16);
|
|
767
|
+
}
|
|
768
|
+
return distance;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Generate a deterministic key ID from a key string using SHA-1
|
|
773
|
+
*/
|
|
774
|
+
async generateKeyId(key) {
|
|
775
|
+
// Use Web Crypto API to generate SHA-1 hash like peer IDs
|
|
776
|
+
const encoder = new TextEncoder();
|
|
777
|
+
const data = encoder.encode(key);
|
|
778
|
+
const hashBuffer = await crypto.subtle.digest('SHA-1', data);
|
|
779
|
+
const hashArray = new Uint8Array(hashBuffer);
|
|
780
|
+
return Array.from(hashArray, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* Generate a unique message ID
|
|
785
|
+
*/
|
|
786
|
+
generateMessageId() {
|
|
787
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Store data locally
|
|
792
|
+
*/
|
|
793
|
+
storeLocally(keyId, data, isPrimary = true) {
|
|
794
|
+
this.localStorage.set(keyId, {
|
|
795
|
+
...data,
|
|
796
|
+
storedAt: Date.now(),
|
|
797
|
+
isPrimary
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Add a subscription for a key
|
|
803
|
+
*/
|
|
804
|
+
addSubscription(keyId, subscriberId) {
|
|
805
|
+
if (!this.subscriptions.has(keyId)) {
|
|
806
|
+
this.subscriptions.set(keyId, new Set());
|
|
807
|
+
}
|
|
808
|
+
this.subscriptions.get(keyId).add(subscriberId);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Check if this peer is among the closest to a key
|
|
813
|
+
*/
|
|
814
|
+
isAmongClosest(keyId, closestPeers) {
|
|
815
|
+
return closestPeers.includes(this.peerId);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Check if stored data has expired
|
|
820
|
+
*/
|
|
821
|
+
isExpired(data) {
|
|
822
|
+
// If TTL is null/undefined, record never expires
|
|
823
|
+
if (!data.ttl) {
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
// TTL is stored in seconds, so convert to milliseconds
|
|
827
|
+
return data.timestamp + (data.ttl * 1000) < Date.now();
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Response handler management
|
|
832
|
+
*/
|
|
833
|
+
setResponseHandler(messageId, handler) {
|
|
834
|
+
if (!this.responseHandlers) {
|
|
835
|
+
this.responseHandlers = new Map();
|
|
836
|
+
}
|
|
837
|
+
this.responseHandlers.set(messageId, handler);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
removeResponseHandler(messageId) {
|
|
841
|
+
if (this.responseHandlers) {
|
|
842
|
+
this.responseHandlers.delete(messageId);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
handleResponse(fromPeerId, data) {
|
|
847
|
+
console.log(`🔥 DHT Response received from ${fromPeerId.substring(0, 8)}: messageId=${data.messageId}, type=${data.type || 'unknown'}`);
|
|
848
|
+
console.log(`🔥 DHT Response full data:`, data);
|
|
849
|
+
console.log(`🔥 DHT Response available handlers:`, this.responseHandlers ? Array.from(this.responseHandlers.keys()) : 'none');
|
|
850
|
+
|
|
851
|
+
if (this.responseHandlers && data.messageId) {
|
|
852
|
+
const handler = this.responseHandlers.get(data.messageId);
|
|
853
|
+
if (handler) {
|
|
854
|
+
console.log(`🔥 DHT Response: Found handler for messageId ${data.messageId}, calling handler`);
|
|
855
|
+
this.removeResponseHandler(data.messageId);
|
|
856
|
+
handler(data);
|
|
857
|
+
} else {
|
|
858
|
+
console.warn(`🔥 DHT Response: No handler found for messageId ${data.messageId}`);
|
|
859
|
+
console.log(`🔥 DHT Response: Available handlers:`, Array.from(this.responseHandlers.keys()));
|
|
860
|
+
}
|
|
861
|
+
} else {
|
|
862
|
+
console.warn(`🔥 DHT Response: Missing responseHandlers or messageId`, {
|
|
863
|
+
hasHandlers: !!this.responseHandlers,
|
|
864
|
+
messageId: data.messageId
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Start periodic maintenance tasks
|
|
871
|
+
*/
|
|
872
|
+
startMaintenance() {
|
|
873
|
+
// Clean up expired keys
|
|
874
|
+
// Use safeSetInterval to avoid issues with wrapped setInterval functions
|
|
875
|
+
this.maintenanceInterval = safeSetInterval(() => {
|
|
876
|
+
this.performMaintenance();
|
|
877
|
+
}, this.config.refreshInterval);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* Perform maintenance tasks
|
|
882
|
+
*/
|
|
883
|
+
performMaintenance() {
|
|
884
|
+
// Remove expired keys
|
|
885
|
+
for (const [keyId, data] of this.localStorage.entries()) {
|
|
886
|
+
if (this.isExpired(data)) {
|
|
887
|
+
this.localStorage.delete(keyId);
|
|
888
|
+
this.subscriptions.delete(keyId);
|
|
889
|
+
console.log(`DHT MAINTENANCE: removed expired key ${keyId.substring(0, 8)}...`);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// TODO: Republish keys if needed
|
|
894
|
+
// TODO: Update routing table
|
|
895
|
+
|
|
896
|
+
console.log(`DHT MAINTENANCE: ${this.localStorage.size} keys stored, ${this.subscriptions.size} subscriptions active`);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Get DHT statistics
|
|
901
|
+
*/
|
|
902
|
+
getStats() {
|
|
903
|
+
return {
|
|
904
|
+
storedKeys: this.localStorage.size,
|
|
905
|
+
activeSubscriptions: this.subscriptions.size,
|
|
906
|
+
mySubscriptions: this.mySubscriptions.size,
|
|
907
|
+
connectedPeers: this.mesh.connectionManager.getConnectedPeerCount()
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/**
|
|
912
|
+
* Cleanup DHT resources
|
|
913
|
+
*/
|
|
914
|
+
cleanup() {
|
|
915
|
+
if (this.maintenanceInterval) {
|
|
916
|
+
safeClearInterval(this.maintenanceInterval);
|
|
917
|
+
this.maintenanceInterval = null;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
this.localStorage.clear();
|
|
921
|
+
this.subscriptions.clear();
|
|
922
|
+
this.mySubscriptions.clear();
|
|
923
|
+
|
|
924
|
+
if (this.responseHandlers) {
|
|
925
|
+
this.responseHandlers.clear();
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
console.log('WebDHT cleaned up');
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Perform iterative Kademlia lookup for GET operations
|
|
933
|
+
* This improves success rate by querying multiple rounds of peers
|
|
934
|
+
*/
|
|
935
|
+
async performIterativeGet(keyId, key, subscribe = false) {
|
|
936
|
+
const queriedPeers = new Set([this.peerId]);
|
|
937
|
+
const closestPeers = await this.findClosestPeers(keyId, this.config.k);
|
|
938
|
+
|
|
939
|
+
// Remove self from the list
|
|
940
|
+
const availablePeers = closestPeers.filter(peerId => peerId !== this.peerId);
|
|
941
|
+
|
|
942
|
+
console.log(`DHT Iterative GET: Found ${availablePeers.length} potential peers for key ${key}`);
|
|
943
|
+
|
|
944
|
+
// Try querying peers in rounds, increasing the number each round
|
|
945
|
+
const maxRounds = 3;
|
|
946
|
+
let currentRound = 0;
|
|
947
|
+
let peersPerRound = this.config.alpha; // Start with alpha peers
|
|
948
|
+
|
|
949
|
+
while (currentRound < maxRounds && queriedPeers.size < availablePeers.length + 1) {
|
|
950
|
+
currentRound++;
|
|
951
|
+
|
|
952
|
+
// Get the next batch of peers to query
|
|
953
|
+
const peersToQuery = availablePeers
|
|
954
|
+
.filter(peerId => !queriedPeers.has(peerId))
|
|
955
|
+
.slice(0, peersPerRound);
|
|
956
|
+
|
|
957
|
+
if (peersToQuery.length === 0) {
|
|
958
|
+
console.log(`DHT Iterative GET Round ${currentRound}: No more peers to query`);
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
console.log(`DHT Iterative GET Round ${currentRound}: Querying ${peersToQuery.length} peers`);
|
|
963
|
+
|
|
964
|
+
// Mark these peers as queried
|
|
965
|
+
peersToQuery.forEach(peerId => queriedPeers.add(peerId));
|
|
966
|
+
|
|
967
|
+
// Query this batch of peers in parallel
|
|
968
|
+
const queryPromises = peersToQuery.map(async (peerId) => {
|
|
969
|
+
try {
|
|
970
|
+
return await this.sendDHTMessage(peerId, 'find_value', {
|
|
971
|
+
keyId,
|
|
972
|
+
key,
|
|
973
|
+
subscribe,
|
|
974
|
+
requester: this.peerId
|
|
975
|
+
});
|
|
976
|
+
} catch (error) {
|
|
977
|
+
console.warn(`DHT GET: Failed to query peer ${peerId.substring(0, 8)}:`, error.message);
|
|
978
|
+
return null;
|
|
979
|
+
}
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
const results = await Promise.allSettled(queryPromises);
|
|
983
|
+
|
|
984
|
+
// Collect all valid responses from this round
|
|
985
|
+
const validResponses = [];
|
|
986
|
+
for (let i = 0; i < results.length; i++) {
|
|
987
|
+
const result = results[i];
|
|
988
|
+
if (result.status === 'fulfilled' && result.value) {
|
|
989
|
+
const data = result.value;
|
|
990
|
+
if (data.found && !this.isExpired(data)) {
|
|
991
|
+
validResponses.push({
|
|
992
|
+
peerId: peersToQuery[i],
|
|
993
|
+
data: data,
|
|
994
|
+
timestamp: data.timestamp || 0
|
|
995
|
+
});
|
|
996
|
+
console.log(`DHT Iterative GET: Found value on peer ${peersToQuery[i].substring(0, 8)} with timestamp ${data.timestamp}, value: ${JSON.stringify(data.value).substring(0, 50)}...`);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// If we found any valid responses, find the most recent one
|
|
1002
|
+
if (validResponses.length > 0) {
|
|
1003
|
+
// Sort by timestamp descending to get the most recent value
|
|
1004
|
+
validResponses.sort((a, b) => b.timestamp - a.timestamp);
|
|
1005
|
+
const mostRecent = validResponses[0];
|
|
1006
|
+
|
|
1007
|
+
console.log(`DHT Iterative GET: Selected most recent value from peer ${mostRecent.peerId.substring(0, 8)} (timestamp: ${mostRecent.timestamp}) in round ${currentRound}`);
|
|
1008
|
+
|
|
1009
|
+
if (validResponses.length > 1) {
|
|
1010
|
+
console.log(`DHT Iterative GET: Found ${validResponses.length} versions, timestamps: ${validResponses.map(r => r.timestamp).join(', ')}`);
|
|
1011
|
+
// Log inconsistency warning if timestamps are very different
|
|
1012
|
+
const timeDiffs = validResponses.map(r => Math.abs(mostRecent.timestamp - r.timestamp));
|
|
1013
|
+
const maxTimeDiff = Math.max(...timeDiffs);
|
|
1014
|
+
if (maxTimeDiff > 10000) { // More than 10 seconds difference
|
|
1015
|
+
console.warn(`DHT CONSISTENCY WARNING: Large timestamp differences found (max: ${maxTimeDiff}ms). This suggests replica peers have inconsistent data!`);
|
|
1016
|
+
console.warn(`DHT CONSISTENCY: Detailed responses:`, validResponses.map(r => ({
|
|
1017
|
+
peer: r.peerId.substring(0, 8),
|
|
1018
|
+
timestamp: r.timestamp,
|
|
1019
|
+
value: JSON.stringify(r.data.value).substring(0, 30) + '...'
|
|
1020
|
+
})));
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Cache the most recent value locally
|
|
1025
|
+
this.storeLocally(keyId, mostRecent.data, false);
|
|
1026
|
+
|
|
1027
|
+
// ALWAYS add subscription when we successfully get a value
|
|
1028
|
+
if (subscribe) {
|
|
1029
|
+
this.mySubscriptions.add(keyId);
|
|
1030
|
+
console.log(`DHT GET: Auto-subscribed to ${key}`);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
return mostRecent.data.value;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
// Increase peers per round for next iteration (more aggressive search)
|
|
1037
|
+
peersPerRound = Math.min(peersPerRound * 2, Math.max(1, Math.floor(availablePeers.length / maxRounds)));
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
console.log(`DHT Iterative GET failed: key ${key} not found after querying ${queriedPeers.size - 1} peers`);
|
|
1041
|
+
return null;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Perform iterative storage for PUT operations with better replication
|
|
1046
|
+
*/
|
|
1047
|
+
async performIterativePut(keyId, storeData, closestPeers) {
|
|
1048
|
+
const targetReplicas = this.config.replicationFactor;
|
|
1049
|
+
const availablePeers = closestPeers.filter(peerId => peerId !== this.peerId);
|
|
1050
|
+
|
|
1051
|
+
console.log(`DHT Iterative PUT: Attempting to store ${storeData.key} on ${targetReplicas} of ${availablePeers.length} available peers`);
|
|
1052
|
+
|
|
1053
|
+
let successful = 0;
|
|
1054
|
+
let attempted = 0;
|
|
1055
|
+
const batchSize = Math.min(this.config.alpha, availablePeers.length);
|
|
1056
|
+
|
|
1057
|
+
// Try to store on peers in batches until we reach target replication
|
|
1058
|
+
for (let i = 0; i < availablePeers.length && successful < targetReplicas; i += batchSize) {
|
|
1059
|
+
const batch = availablePeers.slice(i, i + batchSize);
|
|
1060
|
+
attempted += batch.length;
|
|
1061
|
+
|
|
1062
|
+
console.log(`DHT PUT Batch ${Math.floor(i / batchSize) + 1}: Storing on ${batch.length} peers`);
|
|
1063
|
+
|
|
1064
|
+
const storePromises = batch.map(async (peerId) => {
|
|
1065
|
+
try {
|
|
1066
|
+
const result = await this.sendDHTMessage(peerId, 'store', storeData);
|
|
1067
|
+
return { peerId, success: true, result };
|
|
1068
|
+
} catch (error) {
|
|
1069
|
+
console.warn(`DHT PUT: Failed to store on peer ${peerId.substring(0, 8)}:`, error.message);
|
|
1070
|
+
return { peerId, success: false, error };
|
|
1071
|
+
}
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
const results = await Promise.allSettled(storePromises);
|
|
1075
|
+
|
|
1076
|
+
// Count successful stores in this batch
|
|
1077
|
+
const batchSuccessful = results.filter(r =>
|
|
1078
|
+
r.status === 'fulfilled' && r.value && r.value.success
|
|
1079
|
+
).length;
|
|
1080
|
+
|
|
1081
|
+
successful += batchSuccessful;
|
|
1082
|
+
|
|
1083
|
+
console.log(`DHT PUT Batch completed: ${batchSuccessful}/${batch.length} successful (total: ${successful}/${targetReplicas})`);
|
|
1084
|
+
|
|
1085
|
+
// If we've reached our target, we can stop
|
|
1086
|
+
if (successful >= targetReplicas) {
|
|
1087
|
+
break;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
// Small delay between batches to avoid overwhelming the network
|
|
1091
|
+
if (i + batchSize < availablePeers.length) {
|
|
1092
|
+
await new Promise(resolve => safeSetTimeout(resolve, 100));
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
console.log(`DHT PUT completed: ${successful}/${targetReplicas} target replicas stored (${attempted} peers attempted)`);
|
|
1097
|
+
return successful > 0;
|
|
1098
|
+
}
|
|
1099
|
+
}
|