@leofcoin/chain 1.10.21 → 1.11.0
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 +95 -0
- package/exports/browser/beacon-wire.js +2 -2
- package/exports/browser/{browser-CCfR9KOM-BGMMnVsR.js → browser-CCfR9KOM-CNWKLusu.js} +3 -3
- package/exports/browser/{browser-crypto-CH0ua22L.js → browser-crypto-B_UJguhk.js} +453 -35
- package/exports/browser/chain.js +1275 -996
- package/exports/browser/client-DbZBfpG6-C1cXgvfr.js +5943 -0
- package/exports/browser/{connection-monitor-D-krNiRH.js → connection-monitor-BzbeYGEt.js} +153 -24
- package/exports/browser/{index-dTQVgkpf-DOL2rIjp.js → index-dTQVgkpf-CkoAiRNX.js} +3 -3
- package/exports/browser/{index-DCbLvMKL.js → index-phv5KvaE.js} +238 -234
- package/exports/browser/{messages-DCePyqKC-BSvJxDWZ.js → messages-DZYCxoZI-B472zldo.js} +3 -3
- package/exports/browser/{node-browser-Ha4Lvntj.js → node-browser-BmyPBkL5.js} +55 -11
- package/exports/browser/node-browser.js +3 -3
- package/exports/browser/workers/block-worker.js +8 -9
- package/exports/browser/workers/machine-worker.js +141 -1195
- package/exports/{workers/worker-BDTWOCzS-COrAszCt.js → browser/workers/proposal.proto-8zgMfutf-B3So_tnS.js} +910 -398
- package/exports/chain.js +296 -1078
- package/exports/connection-monitor-BdFLRh-J.js +465 -0
- package/exports/connection-monitor.js +2 -324
- package/exports/{constants-Dy9i9S_o.js → constants-BQ6f-_OT.js} +1 -1
- package/exports/hash-format-state.js +3 -3
- package/exports/machine.js +550 -0
- package/exports/node.js +35 -2
- package/exports/state-checkpoint-BiQ1EXzo.js +110 -0
- package/exports/state-checkpoint.js +3 -0
- package/exports/workers/block-worker.js +8 -9
- package/exports/workers/machine-worker.js +141 -1195
- package/exports/{browser/workers/worker-BDTWOCzS-COrAszCt.js → workers/proposal.proto-8zgMfutf-B3So_tnS.js} +910 -398
- package/package.json +9 -7
- package/exports/browser/client-DKKzPSlb-C_1ug6ux.js +0 -2401
|
@@ -1,324 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const isSendablePeer = (peer) => {
|
|
4
|
-
if (!peer || typeof peer !== "object") return false;
|
|
5
|
-
const candidate = peer;
|
|
6
|
-
if (typeof candidate.send !== "function") return false;
|
|
7
|
-
if (candidate.connected === true) return true;
|
|
8
|
-
return typeof candidate.once === "function" && typeof candidate.removeListener === "function";
|
|
9
|
-
};
|
|
10
|
-
const guardPeerMessages = (peernet) => {
|
|
11
|
-
const sendMessage = peernet.sendMessage.bind(peernet);
|
|
12
|
-
peernet.sendMessage = (peer, ...args) => {
|
|
13
|
-
if (isSendablePeer(peer)) return sendMessage(peer, ...args);
|
|
14
|
-
for (const [peerId, connection] of Object.entries(peernet.connections || {})) {
|
|
15
|
-
if (connection === peer) delete peernet.connections[peerId];
|
|
16
|
-
}
|
|
17
|
-
return Promise.resolve(void 0);
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
class ConnectionMonitor {
|
|
21
|
-
#isMonitoring = false;
|
|
22
|
-
#checkInterval = null;
|
|
23
|
-
#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
24
|
-
#reconnectTimer = null;
|
|
25
|
-
#healthCheckInterval = 6e4;
|
|
26
|
-
#version;
|
|
27
|
-
#lastHealthCheckAt = 0;
|
|
28
|
-
#reconnecting = false;
|
|
29
|
-
// event handlers to remove later
|
|
30
|
-
#onOnline = null;
|
|
31
|
-
#onVisibilityChange = null;
|
|
32
|
-
#onSigcont = null;
|
|
33
|
-
#onPeerConnected = null;
|
|
34
|
-
get isMonitoring() {
|
|
35
|
-
return this.#isMonitoring;
|
|
36
|
-
}
|
|
37
|
-
get connectedPeers() {
|
|
38
|
-
return Object.values(globalThis.peernet?.connections || {}).filter((peer) => peer.connected);
|
|
39
|
-
}
|
|
40
|
-
get compatiblePeers() {
|
|
41
|
-
return this.connectedPeers.filter((peer) => {
|
|
42
|
-
if (!peer.version || !this.#version) return false;
|
|
43
|
-
const [peerMajor, peerMinor] = peer.version.split(".");
|
|
44
|
-
const [localMajor, localMinor] = this.#version.split(".");
|
|
45
|
-
return peerMajor === localMajor && peerMinor === localMinor;
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
get disconnectedPeers() {
|
|
49
|
-
return Object.values(globalThis.peernet?.connections || {}).filter((peer) => !peer.connected);
|
|
50
|
-
}
|
|
51
|
-
#pruneInvalidConnections() {
|
|
52
|
-
const connections = globalThis.peernet?.connections;
|
|
53
|
-
if (!connections) return;
|
|
54
|
-
for (const [peerId, peer] of Object.entries(connections)) {
|
|
55
|
-
if (!isSendablePeer(peer)) {
|
|
56
|
-
delete connections[peerId];
|
|
57
|
-
console.warn(`Removed invalid peer connection: ${peerId}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
start(version) {
|
|
62
|
-
this.#version = version;
|
|
63
|
-
console.log(`\u{1F517} Connection Monitor initialized for version: ${this.#version}`);
|
|
64
|
-
if (this.#isMonitoring) return;
|
|
65
|
-
this.#isMonitoring = true;
|
|
66
|
-
console.log("\u{1F504} Starting connection monitor...");
|
|
67
|
-
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
68
|
-
this.#onOnline = () => {
|
|
69
|
-
console.log("\u{1F310} Network online \u2014 attempting restore");
|
|
70
|
-
void this.#restoreNetwork();
|
|
71
|
-
};
|
|
72
|
-
window.addEventListener("online", this.#onOnline);
|
|
73
|
-
this.#onVisibilityChange = () => {
|
|
74
|
-
if (document.visibilityState === "visible") {
|
|
75
|
-
console.log("\u{1F4A1} Visibility regained");
|
|
76
|
-
if (this.connectedPeers.length === 0) {
|
|
77
|
-
console.log("\u{1F4A1} Visibility regained \u2014 attempting restore");
|
|
78
|
-
void this.#restoreNetwork();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
document.addEventListener("visibilitychange", this.#onVisibilityChange);
|
|
83
|
-
}
|
|
84
|
-
if (typeof process !== "undefined" && typeof process.on === "function") {
|
|
85
|
-
this.#onSigcont = () => {
|
|
86
|
-
console.log("\u{1F514} Process resumed (SIGCONT) \u2014 attempting restore");
|
|
87
|
-
void this.#restoreNetwork();
|
|
88
|
-
};
|
|
89
|
-
try {
|
|
90
|
-
process.on("SIGCONT", this.#onSigcont);
|
|
91
|
-
} catch (e) {
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
this.#lastHealthCheckAt = Date.now();
|
|
95
|
-
this.#checkInterval = setInterval(() => {
|
|
96
|
-
this.#healthCheck();
|
|
97
|
-
}, this.#healthCheckInterval);
|
|
98
|
-
this.#onPeerConnected = () => {
|
|
99
|
-
if (this.#reconnectTimer) {
|
|
100
|
-
clearTimeout(this.#reconnectTimer);
|
|
101
|
-
this.#reconnectTimer = null;
|
|
102
|
-
}
|
|
103
|
-
this.#reconnecting = false;
|
|
104
|
-
this.#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
105
|
-
};
|
|
106
|
-
globalThis.pubsub?.subscribe("peer:connected", this.#onPeerConnected);
|
|
107
|
-
}
|
|
108
|
-
stop() {
|
|
109
|
-
if (!this.#isMonitoring) return;
|
|
110
|
-
this.#isMonitoring = false;
|
|
111
|
-
if (this.#checkInterval) {
|
|
112
|
-
clearInterval(this.#checkInterval);
|
|
113
|
-
this.#checkInterval = null;
|
|
114
|
-
}
|
|
115
|
-
if (typeof window !== "undefined") {
|
|
116
|
-
if (this.#onOnline) {
|
|
117
|
-
window.removeEventListener("online", this.#onOnline);
|
|
118
|
-
this.#onOnline = null;
|
|
119
|
-
}
|
|
120
|
-
if (this.#onVisibilityChange) {
|
|
121
|
-
document.removeEventListener("visibilitychange", this.#onVisibilityChange);
|
|
122
|
-
this.#onVisibilityChange = null;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (typeof process !== "undefined" && typeof process.removeListener === "function" && this.#onSigcont) {
|
|
126
|
-
try {
|
|
127
|
-
process.removeListener("SIGCONT", this.#onSigcont);
|
|
128
|
-
} catch (e) {
|
|
129
|
-
}
|
|
130
|
-
this.#onSigcont = null;
|
|
131
|
-
}
|
|
132
|
-
if (this.#onPeerConnected) {
|
|
133
|
-
globalThis.pubsub?.unsubscribe("peer:connected", this.#onPeerConnected);
|
|
134
|
-
this.#onPeerConnected = null;
|
|
135
|
-
}
|
|
136
|
-
if (this.#reconnectTimer) {
|
|
137
|
-
clearTimeout(this.#reconnectTimer);
|
|
138
|
-
this.#reconnectTimer = null;
|
|
139
|
-
}
|
|
140
|
-
console.log("\u23F9\uFE0F Connection monitor stopped");
|
|
141
|
-
}
|
|
142
|
-
async #healthCheck() {
|
|
143
|
-
this.#pruneInvalidConnections();
|
|
144
|
-
const now = Date.now();
|
|
145
|
-
const expectedNext = this.#lastHealthCheckAt + this.#healthCheckInterval;
|
|
146
|
-
const drift = now - expectedNext;
|
|
147
|
-
this.#lastHealthCheckAt = now;
|
|
148
|
-
if (drift > 5e3) {
|
|
149
|
-
console.log(`\u23F0 Detected timer drift of ${drift}ms (likely system sleep) \u2014 attempting restore`);
|
|
150
|
-
void this.#restoreNetwork();
|
|
151
|
-
}
|
|
152
|
-
const connectedPeers = this.connectedPeers;
|
|
153
|
-
const compatiblePeers = this.compatiblePeers;
|
|
154
|
-
const disconnectedPeers = this.disconnectedPeers;
|
|
155
|
-
console.log(
|
|
156
|
-
`\u{1F50D} Health check: ${connectedPeers.length} connected, ${compatiblePeers.length} compatible, ${disconnectedPeers.length} negotiating`
|
|
157
|
-
);
|
|
158
|
-
if (connectedPeers.length === 0 && disconnectedPeers.length === 0 && !this.#reconnectTimer) {
|
|
159
|
-
console.warn("\u26A0\uFE0F No peer connections detected \u2014 attempting reconnection");
|
|
160
|
-
await this.#attemptReconnection();
|
|
161
|
-
} else if (compatiblePeers.length === 0 && connectedPeers.length > 0 && !this.#reconnectTimer) {
|
|
162
|
-
console.warn("\u26A0\uFE0F No compatible peers found \u2014 attempting reconnection");
|
|
163
|
-
await this.#attemptReconnection();
|
|
164
|
-
}
|
|
165
|
-
globalThis.pubsub?.publish("connection-status", {
|
|
166
|
-
connected: connectedPeers.length,
|
|
167
|
-
compatible: compatiblePeers.length,
|
|
168
|
-
healthy: compatiblePeers.length > 0
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
// lightweight TCP probe to detect internet connectivity in Node.js
|
|
172
|
-
async #isOnLine(timeout = 1500) {
|
|
173
|
-
if (navigator?.onLine !== void 0) {
|
|
174
|
-
return navigator.onLine;
|
|
175
|
-
}
|
|
176
|
-
return new Promise(async (resolve) => {
|
|
177
|
-
try {
|
|
178
|
-
const net = await import('net');
|
|
179
|
-
const socket = new net.Socket();
|
|
180
|
-
let finished = false;
|
|
181
|
-
const finish = (val) => {
|
|
182
|
-
if (finished) return;
|
|
183
|
-
finished = true;
|
|
184
|
-
try {
|
|
185
|
-
socket.destroy();
|
|
186
|
-
} catch (e) {
|
|
187
|
-
}
|
|
188
|
-
resolve(val);
|
|
189
|
-
};
|
|
190
|
-
socket.setTimeout(timeout);
|
|
191
|
-
socket.once("connect", () => finish(true));
|
|
192
|
-
socket.once("error", () => finish(false));
|
|
193
|
-
socket.once("timeout", () => finish(false));
|
|
194
|
-
socket.connect(53, "1.1.1.1");
|
|
195
|
-
} catch (e) {
|
|
196
|
-
resolve(false);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
// Called on visibility/online/resume events
|
|
201
|
-
async #restoreNetwork() {
|
|
202
|
-
if (this.#reconnecting) {
|
|
203
|
-
console.log("\u{1F501} Reconnection already in progress, skipping");
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
this.#reconnecting = true;
|
|
207
|
-
if (this.connectedPeers.length > 0) {
|
|
208
|
-
console.log("\u2705 Already connected to peers, skipping restoration");
|
|
209
|
-
this.#reconnecting = false;
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
console.log("\u{1F501} Restoring network");
|
|
213
|
-
try {
|
|
214
|
-
const online = await this.#isOnLine(1500);
|
|
215
|
-
if (!online) {
|
|
216
|
-
console.warn("\u26A0\uFE0F No internet detected, skipping restore");
|
|
217
|
-
this.#reconnecting = false;
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
} catch (e) {
|
|
221
|
-
console.warn("\u26A0\uFE0F Online probe failed, proceeding with restore", e?.message || e);
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
console.log("\u{1F504} Attempting network restoration...");
|
|
225
|
-
if (globalThis.peernet?.client?.reinit) {
|
|
226
|
-
console.log(" \u2192 Trying client.reinit()");
|
|
227
|
-
try {
|
|
228
|
-
await globalThis.peernet.client.reinit();
|
|
229
|
-
console.log(" \u2705 client.reinit() succeeded");
|
|
230
|
-
} catch (e) {
|
|
231
|
-
console.warn(" \u26A0\uFE0F client.reinit() failed:", e?.message || e);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (globalThis.peernet?.start) {
|
|
235
|
-
console.log(" \u2192 Trying peernet.start()");
|
|
236
|
-
try {
|
|
237
|
-
await globalThis.peernet.start();
|
|
238
|
-
console.log(" \u2705 peernet.start() succeeded");
|
|
239
|
-
} catch (e) {
|
|
240
|
-
console.warn(" \u26A0\uFE0F peernet.start() failed:", e?.message || e);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
try {
|
|
244
|
-
const networkName = globalThis.peernet?.network;
|
|
245
|
-
if (networkName && typeof networkName === "string") {
|
|
246
|
-
const { default: networks } = await import('@leofcoin/networks');
|
|
247
|
-
const [mainKey, subKey] = networkName.split(":");
|
|
248
|
-
const networkConfig = networks?.[mainKey]?.[subKey];
|
|
249
|
-
if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
|
|
250
|
-
console.log(" \u2192 Attempting to dial star servers:", networkConfig.stars.join(", "));
|
|
251
|
-
for (const star of networkConfig.stars) {
|
|
252
|
-
try {
|
|
253
|
-
if (globalThis.peernet?.client && "dial" in globalThis.peernet.client) {
|
|
254
|
-
await globalThis.peernet.client.dial(star);
|
|
255
|
-
console.log(` \u2705 Connected to star server: ${star}`);
|
|
256
|
-
}
|
|
257
|
-
} catch (e) {
|
|
258
|
-
console.warn(` \u26A0\uFE0F Failed to dial ${star}:`, e?.message || e);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
} catch (e) {
|
|
264
|
-
console.warn(" \u26A0\uFE0F Could not load or dial star servers:", e?.message || e);
|
|
265
|
-
}
|
|
266
|
-
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
267
|
-
this.#pruneInvalidConnections();
|
|
268
|
-
const connectedAfter = this.connectedPeers.length;
|
|
269
|
-
console.log(`\u{1F504} Restoration complete. Connected peers: ${connectedAfter}`);
|
|
270
|
-
} catch (error) {
|
|
271
|
-
console.error("\u274C Network restoration failed:", error?.message || error);
|
|
272
|
-
} finally {
|
|
273
|
-
this.#reconnecting = false;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
async waitForPeers(timeoutMs = 3e4) {
|
|
277
|
-
return new Promise((resolve) => {
|
|
278
|
-
const startTime = Date.now();
|
|
279
|
-
const checkPeers = () => {
|
|
280
|
-
if (this.compatiblePeers.length > 0) {
|
|
281
|
-
resolve(true);
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
if (Date.now() - startTime >= timeoutMs) {
|
|
285
|
-
resolve(false);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
setTimeout(checkPeers, 1e3);
|
|
289
|
-
};
|
|
290
|
-
checkPeers();
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
async #attemptReconnection() {
|
|
294
|
-
try {
|
|
295
|
-
await this.#restoreNetwork();
|
|
296
|
-
const hasConnections = this.connectedPeers.length > 0 || this.disconnectedPeers.length > 0;
|
|
297
|
-
if (hasConnections) {
|
|
298
|
-
console.log("\u2705 Reconnection successful, resetting backoff delay");
|
|
299
|
-
this.#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
300
|
-
} else {
|
|
301
|
-
console.warn("\u26A0\uFE0F Reconnection attempt completed but no peers connected");
|
|
302
|
-
this.#scheduleReconnect();
|
|
303
|
-
}
|
|
304
|
-
} catch (error) {
|
|
305
|
-
console.error("\u274C Reconnection failed:", error?.message || error);
|
|
306
|
-
this.#scheduleReconnect();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
#scheduleReconnect() {
|
|
310
|
-
if (!this.#isMonitoring || this.#reconnectTimer) return;
|
|
311
|
-
if (this.#reconnectDelay >= MAX_RECONNECT_DELAY) {
|
|
312
|
-
console.warn("\u26A0\uFE0F No peers available; automatic retries paused until the next health check or network event");
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, MAX_RECONNECT_DELAY);
|
|
316
|
-
console.warn(`\u26A0\uFE0F Increasing reconnection delay to ${this.#reconnectDelay} ms`);
|
|
317
|
-
this.#reconnectTimer = setTimeout(() => {
|
|
318
|
-
this.#reconnectTimer = null;
|
|
319
|
-
void this.#attemptReconnection();
|
|
320
|
-
}, this.#reconnectDelay);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export { ConnectionMonitor as default, guardPeerMessages, isSendablePeer };
|
|
1
|
+
import '@leofcoin/codec-format-interface';
|
|
2
|
+
export { C as default, g as guardPeerMessages, a as guardProviderDiscovery, i as isSendablePeer } from './connection-monitor-BdFLRh-J.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as PROTOCOL_VERSION } from './constants-
|
|
1
|
+
import { P as PROTOCOL_VERSION } from './constants-BQ6f-_OT.js';
|
|
2
2
|
import '@leofcoin/networks';
|
|
3
3
|
|
|
4
4
|
const assertHashFormatState = async (chainStore, stateStore) => {
|
|
@@ -7,9 +7,9 @@ const assertHashFormatState = async (chainStore, stateStore) => {
|
|
|
7
7
|
const parsed = /^(\d+)\.(\d+)\.\d+$/.exec(version);
|
|
8
8
|
const current = PROTOCOL_VERSION.split(".");
|
|
9
9
|
if (parsed && parsed[1] === current[0] && parsed[2] === current[1]) return;
|
|
10
|
-
throw new Error(`stored protocol ${version} is incompatible with canonical hash protocol ${PROTOCOL_VERSION};
|
|
10
|
+
throw new Error(`stored protocol ${version} is incompatible with canonical hash protocol ${PROTOCOL_VERSION}; explicit ledger migration or a testnet reset is required (preserve the wallet identity)`);
|
|
11
11
|
}
|
|
12
|
-
if (await chainStore.has("lastBlock") || await stateStore?.has("lastBlock")) {
|
|
12
|
+
if (await chainStore.has("lastBlock") || await chainStore.has("recoveryTarget") || await stateStore?.has("lastBlock") || await stateStore?.has("checkpoint:0") || await stateStore?.has("checkpoint:1")) {
|
|
13
13
|
throw new Error("existing chainstate has no protocol version; explicit canonical hash migration required");
|
|
14
14
|
}
|
|
15
15
|
};
|