peerpigeon 0.0.5 → 0.0.7

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