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/README.md +190 -12
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
190
|
-
|
|
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:
|
|
232
|
+
* NOTE: This now supports subscribing to keys that don't exist yet
|
|
196
233
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
|
|
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
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
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
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
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
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
|
488
|
-
|
|
566
|
+
|
|
567
|
+
console.log(`🔥 DHT FIND_VALUE: Sending response data with messageId ${messageId}:`, responseData);
|
|
568
|
+
|
|
489
569
|
this.sendDirectToPeer(fromPeerId, {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
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
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
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
|
-
|
|
582
|
-
|
|
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
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
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
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
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
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
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
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
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
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
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
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
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
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
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
|
-
|
|
787
|
-
|
|
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
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
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
|
-
|
|
805
|
-
|
|
806
|
-
|
|
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
|
-
|
|
815
|
-
|
|
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
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
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
|
-
|
|
834
|
-
|
|
835
|
-
|
|
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
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
}
|
|
1013
|
+
removeResponseHandler(messageId) {
|
|
1014
|
+
if (this.responseHandlers) {
|
|
1015
|
+
this.responseHandlers.delete(messageId);
|
|
844
1016
|
}
|
|
1017
|
+
}
|
|
845
1018
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
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
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
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
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
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
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
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
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
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
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1041
|
-
|
|
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
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
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
|
-
|
|
1097
|
-
|
|
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
|
}
|