@leofcoin/chain 1.7.121 → 1.7.123

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.
@@ -4948,6 +4948,11 @@ class State extends Contract {
4948
4948
  }
4949
4949
  }
4950
4950
  async resolveBlocks() {
4951
+ // Don't re-resolve if already syncing or resolving
4952
+ if (this.#chainSyncing || this.#resolving) {
4953
+ debug$1('Already syncing or resolving, skipping resolveBlocks()');
4954
+ return;
4955
+ }
4951
4956
  try {
4952
4957
  if (this.jobber.busy && this.jobber.destroy) {
4953
4958
  await this.jobber.destroy();
@@ -4960,6 +4965,7 @@ class State extends Contract {
4960
4965
  const localBlock = await globalThis.chainStore.get('lastBlock');
4961
4966
  const hash = new TextDecoder().decode(localBlock);
4962
4967
  if (hash && hash !== '0x0') {
4968
+ debug$1(`Resolving blocks from hash: ${hash}`);
4963
4969
  await this.resolveBlock(hash);
4964
4970
  }
4965
4971
  }
@@ -5043,20 +5049,39 @@ class State extends Contract {
5043
5049
  // await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)))
5044
5050
  // }
5045
5051
  const localBlock = await this.lastBlock;
5046
- if (!localBlock || Number(localBlock.index) < Number(lastBlock.index)) {
5047
- // TODO: check if valid
5048
- const localIndex = localBlock ? Number(localBlock.index) : 0;
5049
- const index = Number(lastBlock.index);
5050
- await this.resolveBlock(lastBlock.hash);
5051
- console.log('ok');
5052
- let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index + 1 : index + -localIndex + 1) : index + 1;
5053
- debug$1(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
5052
+ const localIndex = localBlock ? Number(localBlock.index) : -1;
5053
+ const remoteIndex = Number(lastBlock.index);
5054
+ const remoteBlockHash = lastBlock.hash;
5055
+ // Get the local state hash from chainStore
5056
+ let localStateHash = '0x0';
5057
+ try {
5058
+ localStateHash = new TextDecoder().decode(await globalThis.chainStore.get('lastBlock'));
5059
+ }
5060
+ catch (error) {
5061
+ debug$1(`No local state hash found: ${error}`);
5062
+ }
5063
+ debug$1(`Local block height: ${localIndex}, remote block height: ${remoteIndex}`);
5064
+ debug$1(`Local state hash: ${localStateHash}, remote block hash: ${remoteBlockHash}`);
5065
+ // Use state hash comparison: only resolve if remote hash differs from local state hash
5066
+ if (localStateHash !== remoteBlockHash) {
5067
+ // Remote block hash differs from our local state, need to resolve
5068
+ debug$1(`Resolving remote block: ${remoteBlockHash} @${remoteIndex} (differs from local state)`);
5069
+ await this.resolveBlock(remoteBlockHash);
5070
+ const blocksSynced = remoteIndex - localIndex;
5071
+ debug$1(`Resolved ${blocksSynced} new block(s)`);
5054
5072
  const blocks = this.#blocks;
5055
- const start = localIndex - blocksSynced;
5056
- if (this.#machine) {
5073
+ debug$1(`Loading blocks from index ${localIndex + 1} to ${remoteIndex}`);
5074
+ const start = localIndex + 1;
5075
+ if (this.#machine && blocks.length > start) {
5057
5076
  await this.#loadBlocks(blocks.slice(start));
5058
5077
  }
5059
- await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
5078
+ // Update state with the latest block
5079
+ if (blocks.length > 0) {
5080
+ await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
5081
+ }
5082
+ }
5083
+ else {
5084
+ debug$1(`Block already in local state. Remote hash: ${remoteBlockHash} matches local state`);
5060
5085
  }
5061
5086
  }
5062
5087
  catch (error) {
@@ -5499,6 +5524,10 @@ class ConnectionMonitor {
5499
5524
  console.log('🔁 Reconnection already in progress, skipping');
5500
5525
  return;
5501
5526
  }
5527
+ if (this.connectedPeers.length > 0) {
5528
+ console.log('✅ Already connected to peers, skipping restoration');
5529
+ return;
5530
+ }
5502
5531
  this.#reconnecting = true;
5503
5532
  console.log('🔁 Restoring network');
5504
5533
  try {
package/exports/chain.js CHANGED
@@ -1075,6 +1075,11 @@ class State extends Contract {
1075
1075
  }
1076
1076
  }
1077
1077
  async resolveBlocks() {
1078
+ // Don't re-resolve if already syncing or resolving
1079
+ if (this.#chainSyncing || this.#resolving) {
1080
+ debug$1('Already syncing or resolving, skipping resolveBlocks()');
1081
+ return;
1082
+ }
1078
1083
  try {
1079
1084
  if (this.jobber.busy && this.jobber.destroy) {
1080
1085
  await this.jobber.destroy();
@@ -1087,6 +1092,7 @@ class State extends Contract {
1087
1092
  const localBlock = await globalThis.chainStore.get('lastBlock');
1088
1093
  const hash = new TextDecoder().decode(localBlock);
1089
1094
  if (hash && hash !== '0x0') {
1095
+ debug$1(`Resolving blocks from hash: ${hash}`);
1090
1096
  await this.resolveBlock(hash);
1091
1097
  }
1092
1098
  }
@@ -1170,20 +1176,39 @@ class State extends Contract {
1170
1176
  // await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)))
1171
1177
  // }
1172
1178
  const localBlock = await this.lastBlock;
1173
- if (!localBlock || Number(localBlock.index) < Number(lastBlock.index)) {
1174
- // TODO: check if valid
1175
- const localIndex = localBlock ? Number(localBlock.index) : 0;
1176
- const index = Number(lastBlock.index);
1177
- await this.resolveBlock(lastBlock.hash);
1178
- console.log('ok');
1179
- let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index + 1 : index + -localIndex + 1) : index + 1;
1180
- debug$1(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
1179
+ const localIndex = localBlock ? Number(localBlock.index) : -1;
1180
+ const remoteIndex = Number(lastBlock.index);
1181
+ const remoteBlockHash = lastBlock.hash;
1182
+ // Get the local state hash from chainStore
1183
+ let localStateHash = '0x0';
1184
+ try {
1185
+ localStateHash = new TextDecoder().decode(await globalThis.chainStore.get('lastBlock'));
1186
+ }
1187
+ catch (error) {
1188
+ debug$1(`No local state hash found: ${error}`);
1189
+ }
1190
+ debug$1(`Local block height: ${localIndex}, remote block height: ${remoteIndex}`);
1191
+ debug$1(`Local state hash: ${localStateHash}, remote block hash: ${remoteBlockHash}`);
1192
+ // Use state hash comparison: only resolve if remote hash differs from local state hash
1193
+ if (localStateHash !== remoteBlockHash) {
1194
+ // Remote block hash differs from our local state, need to resolve
1195
+ debug$1(`Resolving remote block: ${remoteBlockHash} @${remoteIndex} (differs from local state)`);
1196
+ await this.resolveBlock(remoteBlockHash);
1197
+ const blocksSynced = remoteIndex - localIndex;
1198
+ debug$1(`Resolved ${blocksSynced} new block(s)`);
1181
1199
  const blocks = this.#blocks;
1182
- const start = localIndex - blocksSynced;
1183
- if (this.#machine) {
1200
+ debug$1(`Loading blocks from index ${localIndex + 1} to ${remoteIndex}`);
1201
+ const start = localIndex + 1;
1202
+ if (this.#machine && blocks.length > start) {
1184
1203
  await this.#loadBlocks(blocks.slice(start));
1185
1204
  }
1186
- await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
1205
+ // Update state with the latest block
1206
+ if (blocks.length > 0) {
1207
+ await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
1208
+ }
1209
+ }
1210
+ else {
1211
+ debug$1(`Block already in local state. Remote hash: ${remoteBlockHash} matches local state`);
1187
1212
  }
1188
1213
  }
1189
1214
  catch (error) {
@@ -1626,6 +1651,10 @@ class ConnectionMonitor {
1626
1651
  console.log('🔁 Reconnection already in progress, skipping');
1627
1652
  return;
1628
1653
  }
1654
+ if (this.connectedPeers.length > 0) {
1655
+ console.log('✅ Already connected to peers, skipping restoration');
1656
+ return;
1657
+ }
1629
1658
  this.#reconnecting = true;
1630
1659
  console.log('🔁 Restoring network');
1631
1660
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.121",
3
+ "version": "1.7.123",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {