@leofcoin/chain 1.7.128 → 1.7.130
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/exports/browser/chain.js +32 -41
- package/exports/chain.js +32 -41
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -4876,17 +4876,11 @@ class State extends Contract {
|
|
|
4876
4876
|
let block = await globalThis.peernet.get(hash, 'block');
|
|
4877
4877
|
if (block !== undefined) {
|
|
4878
4878
|
block = await new BlockMessage(block);
|
|
4879
|
-
const computedHash = await block.hash();
|
|
4880
|
-
if (computedHash !== hash) {
|
|
4881
|
-
throw new ResolveError(`block hash mismatch for ${hash}`, {
|
|
4882
|
-
cause: new Error('hash does not match payload')
|
|
4883
|
-
});
|
|
4884
|
-
}
|
|
4885
4879
|
const { index } = block.decoded;
|
|
4886
|
-
if (this.#blocks[index] && this.#blocks[index].hash !==
|
|
4887
|
-
throw
|
|
4888
|
-
if (!(await globalThis.peernet.has(
|
|
4889
|
-
await globalThis.peernet.put(
|
|
4880
|
+
if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
|
|
4881
|
+
throw `invalid block ${hash} @${index}`;
|
|
4882
|
+
if (!(await globalThis.peernet.has(hash)))
|
|
4883
|
+
await globalThis.peernet.put(hash, block.encoded, 'block');
|
|
4890
4884
|
}
|
|
4891
4885
|
return block;
|
|
4892
4886
|
}
|
|
@@ -5131,39 +5125,30 @@ class State extends Contract {
|
|
|
5131
5125
|
}
|
|
5132
5126
|
// @ts-ignore
|
|
5133
5127
|
promises = await this.promiseRequests(promises);
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
-
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5145
|
-
|
|
5146
|
-
|
|
5147
|
-
|
|
5148
|
-
|
|
5149
|
-
|
|
5150
|
-
let node = await globalThis.peernet.prepareMessage(data);
|
|
5151
|
-
let response = await peer.request(node.encode());
|
|
5152
|
-
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
5153
|
-
this.knownBlocks = response.decoded.response;
|
|
5154
|
-
}
|
|
5155
|
-
return latest;
|
|
5156
|
-
}
|
|
5157
|
-
catch (error) {
|
|
5158
|
-
console.warn('[state] ignoring invalid lastBlock candidate', {
|
|
5159
|
-
error,
|
|
5160
|
-
hash: value?.hash,
|
|
5161
|
-
index: value?.index
|
|
5128
|
+
console.log({ promises });
|
|
5129
|
+
let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
5130
|
+
promises = promises.sort((a, b) => b.index - a.index);
|
|
5131
|
+
if (promises.length > 0)
|
|
5132
|
+
latest = promises[0].value;
|
|
5133
|
+
if (latest.hash && latest.hash !== '0x0') {
|
|
5134
|
+
let message = await globalThis.peernet.get(latest.hash, 'block');
|
|
5135
|
+
message = await new BlockMessage(message);
|
|
5136
|
+
const hash = await message.hash();
|
|
5137
|
+
if (hash !== latest.hash)
|
|
5138
|
+
throw new Error('invalid block @getLatestBlock');
|
|
5139
|
+
latest = { ...message.decoded, hash };
|
|
5140
|
+
const peer = promises[0].peer;
|
|
5141
|
+
if (peer.connected && peer.version === this.version) {
|
|
5142
|
+
let data = await new globalThis.peernet.protos['peernet-request']({
|
|
5143
|
+
request: 'knownBlocks'
|
|
5162
5144
|
});
|
|
5163
|
-
|
|
5145
|
+
let node = await globalThis.peernet.prepareMessage(data);
|
|
5146
|
+
let message = await peer.request(node.encode());
|
|
5147
|
+
message = await new globalThis.peernet.protos['peernet-response'](message);
|
|
5148
|
+
this.knownBlocks = message.decoded.response;
|
|
5164
5149
|
}
|
|
5165
5150
|
}
|
|
5166
|
-
return
|
|
5151
|
+
return latest;
|
|
5167
5152
|
}
|
|
5168
5153
|
#loadBlockTransactions;
|
|
5169
5154
|
#getLastTransactions;
|
|
@@ -5493,6 +5478,11 @@ class ConnectionMonitor {
|
|
|
5493
5478
|
const connectedPeers = this.connectedPeers;
|
|
5494
5479
|
const compatiblePeers = this.compatiblePeers;
|
|
5495
5480
|
console.log(`🔍 Health check: ${connectedPeers.length} connected, ${compatiblePeers.length} compatible`);
|
|
5481
|
+
// If a reconnection is already ongoing, skip this cycle to avoid log spam/loops
|
|
5482
|
+
if (this.#reconnecting) {
|
|
5483
|
+
console.log('⏭️ Health check: reconnection already in progress, skipping reconnect attempt');
|
|
5484
|
+
return;
|
|
5485
|
+
}
|
|
5496
5486
|
// If we have no connections or none are compatible, try to reconnect
|
|
5497
5487
|
if (connectedPeers.length === 0) {
|
|
5498
5488
|
console.warn('⚠️ No peer connections detected — attempting reconnection');
|
|
@@ -5552,11 +5542,12 @@ class ConnectionMonitor {
|
|
|
5552
5542
|
console.log('🔁 Reconnection already in progress, skipping');
|
|
5553
5543
|
return;
|
|
5554
5544
|
}
|
|
5545
|
+
this.#reconnecting = true;
|
|
5555
5546
|
if (this.connectedPeers.length > 0) {
|
|
5556
5547
|
console.log('✅ Already connected to peers, skipping restoration');
|
|
5548
|
+
this.#reconnecting = false;
|
|
5557
5549
|
return;
|
|
5558
5550
|
}
|
|
5559
|
-
this.#reconnecting = true;
|
|
5560
5551
|
console.log('🔁 Restoring network');
|
|
5561
5552
|
try {
|
|
5562
5553
|
const online = await this.#isOnLine(1500);
|
package/exports/chain.js
CHANGED
|
@@ -1003,17 +1003,11 @@ class State extends Contract {
|
|
|
1003
1003
|
let block = await globalThis.peernet.get(hash, 'block');
|
|
1004
1004
|
if (block !== undefined) {
|
|
1005
1005
|
block = await new BlockMessage(block);
|
|
1006
|
-
const computedHash = await block.hash();
|
|
1007
|
-
if (computedHash !== hash) {
|
|
1008
|
-
throw new ResolveError(`block hash mismatch for ${hash}`, {
|
|
1009
|
-
cause: new Error('hash does not match payload')
|
|
1010
|
-
});
|
|
1011
|
-
}
|
|
1012
1006
|
const { index } = block.decoded;
|
|
1013
|
-
if (this.#blocks[index] && this.#blocks[index].hash !==
|
|
1014
|
-
throw
|
|
1015
|
-
if (!(await globalThis.peernet.has(
|
|
1016
|
-
await globalThis.peernet.put(
|
|
1007
|
+
if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
|
|
1008
|
+
throw `invalid block ${hash} @${index}`;
|
|
1009
|
+
if (!(await globalThis.peernet.has(hash)))
|
|
1010
|
+
await globalThis.peernet.put(hash, block.encoded, 'block');
|
|
1017
1011
|
}
|
|
1018
1012
|
return block;
|
|
1019
1013
|
}
|
|
@@ -1258,39 +1252,30 @@ class State extends Contract {
|
|
|
1258
1252
|
}
|
|
1259
1253
|
// @ts-ignore
|
|
1260
1254
|
promises = await this.promiseRequests(promises);
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
let node = await globalThis.peernet.prepareMessage(data);
|
|
1278
|
-
let response = await peer.request(node.encode());
|
|
1279
|
-
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
1280
|
-
this.knownBlocks = response.decoded.response;
|
|
1281
|
-
}
|
|
1282
|
-
return latest;
|
|
1283
|
-
}
|
|
1284
|
-
catch (error) {
|
|
1285
|
-
console.warn('[state] ignoring invalid lastBlock candidate', {
|
|
1286
|
-
error,
|
|
1287
|
-
hash: value?.hash,
|
|
1288
|
-
index: value?.index
|
|
1255
|
+
console.log({ promises });
|
|
1256
|
+
let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
1257
|
+
promises = promises.sort((a, b) => b.index - a.index);
|
|
1258
|
+
if (promises.length > 0)
|
|
1259
|
+
latest = promises[0].value;
|
|
1260
|
+
if (latest.hash && latest.hash !== '0x0') {
|
|
1261
|
+
let message = await globalThis.peernet.get(latest.hash, 'block');
|
|
1262
|
+
message = await new BlockMessage(message);
|
|
1263
|
+
const hash = await message.hash();
|
|
1264
|
+
if (hash !== latest.hash)
|
|
1265
|
+
throw new Error('invalid block @getLatestBlock');
|
|
1266
|
+
latest = { ...message.decoded, hash };
|
|
1267
|
+
const peer = promises[0].peer;
|
|
1268
|
+
if (peer.connected && peer.version === this.version) {
|
|
1269
|
+
let data = await new globalThis.peernet.protos['peernet-request']({
|
|
1270
|
+
request: 'knownBlocks'
|
|
1289
1271
|
});
|
|
1290
|
-
|
|
1272
|
+
let node = await globalThis.peernet.prepareMessage(data);
|
|
1273
|
+
let message = await peer.request(node.encode());
|
|
1274
|
+
message = await new globalThis.peernet.protos['peernet-response'](message);
|
|
1275
|
+
this.knownBlocks = message.decoded.response;
|
|
1291
1276
|
}
|
|
1292
1277
|
}
|
|
1293
|
-
return
|
|
1278
|
+
return latest;
|
|
1294
1279
|
}
|
|
1295
1280
|
#loadBlockTransactions;
|
|
1296
1281
|
#getLastTransactions;
|
|
@@ -1620,6 +1605,11 @@ class ConnectionMonitor {
|
|
|
1620
1605
|
const connectedPeers = this.connectedPeers;
|
|
1621
1606
|
const compatiblePeers = this.compatiblePeers;
|
|
1622
1607
|
console.log(`🔍 Health check: ${connectedPeers.length} connected, ${compatiblePeers.length} compatible`);
|
|
1608
|
+
// If a reconnection is already ongoing, skip this cycle to avoid log spam/loops
|
|
1609
|
+
if (this.#reconnecting) {
|
|
1610
|
+
console.log('⏭️ Health check: reconnection already in progress, skipping reconnect attempt');
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1623
1613
|
// If we have no connections or none are compatible, try to reconnect
|
|
1624
1614
|
if (connectedPeers.length === 0) {
|
|
1625
1615
|
console.warn('⚠️ No peer connections detected — attempting reconnection');
|
|
@@ -1679,11 +1669,12 @@ class ConnectionMonitor {
|
|
|
1679
1669
|
console.log('🔁 Reconnection already in progress, skipping');
|
|
1680
1670
|
return;
|
|
1681
1671
|
}
|
|
1672
|
+
this.#reconnecting = true;
|
|
1682
1673
|
if (this.connectedPeers.length > 0) {
|
|
1683
1674
|
console.log('✅ Already connected to peers, skipping restoration');
|
|
1675
|
+
this.#reconnecting = false;
|
|
1684
1676
|
return;
|
|
1685
1677
|
}
|
|
1686
|
-
this.#reconnecting = true;
|
|
1687
1678
|
console.log('🔁 Restoring network');
|
|
1688
1679
|
try {
|
|
1689
1680
|
const online = await this.#isOnLine(1500);
|