@leofcoin/chain 1.7.122 → 1.7.124

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.
@@ -4933,6 +4933,11 @@ class State extends Contract {
4933
4933
  try {
4934
4934
  await this.jobber.add(() => this.#resolveBlock(hash));
4935
4935
  this.#resolving = false;
4936
+ const lastBlockHash = await globalThis.stateStore.get('lastBlock');
4937
+ if (lastBlockHash === hash) {
4938
+ this.#resolveErrored = false;
4939
+ return;
4940
+ }
4936
4941
  if (!this.#blockHashMap.has(this.#lastResolved.previousHash) && this.#lastResolved.previousHash !== '0x0')
4937
4942
  return this.resolveBlock(this.#lastResolved.previousHash);
4938
4943
  }
@@ -4948,6 +4953,11 @@ class State extends Contract {
4948
4953
  }
4949
4954
  }
4950
4955
  async resolveBlocks() {
4956
+ // Don't re-resolve if already syncing or resolving
4957
+ if (this.#chainSyncing || this.#resolving) {
4958
+ debug$1('Already syncing or resolving, skipping resolveBlocks()');
4959
+ return;
4960
+ }
4951
4961
  try {
4952
4962
  if (this.jobber.busy && this.jobber.destroy) {
4953
4963
  await this.jobber.destroy();
@@ -4960,6 +4970,7 @@ class State extends Contract {
4960
4970
  const localBlock = await globalThis.chainStore.get('lastBlock');
4961
4971
  const hash = new TextDecoder().decode(localBlock);
4962
4972
  if (hash && hash !== '0x0') {
4973
+ debug$1(`Resolving blocks from hash: ${hash}`);
4963
4974
  await this.resolveBlock(hash);
4964
4975
  }
4965
4976
  }
@@ -5043,20 +5054,39 @@ class State extends Contract {
5043
5054
  // await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)))
5044
5055
  // }
5045
5056
  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'}`);
5057
+ const localIndex = localBlock ? Number(localBlock.index) : -1;
5058
+ const remoteIndex = Number(lastBlock.index);
5059
+ const remoteBlockHash = lastBlock.hash;
5060
+ // Get the local state hash from chainStore
5061
+ let localStateHash = '0x0';
5062
+ try {
5063
+ localStateHash = new TextDecoder().decode(await globalThis.chainStore.get('lastBlock'));
5064
+ }
5065
+ catch (error) {
5066
+ debug$1(`No local state hash found: ${error}`);
5067
+ }
5068
+ debug$1(`Local block height: ${localIndex}, remote block height: ${remoteIndex}`);
5069
+ debug$1(`Local state hash: ${localStateHash}, remote block hash: ${remoteBlockHash}`);
5070
+ // Use state hash comparison: only resolve if remote hash differs from local state hash
5071
+ if (localStateHash !== remoteBlockHash) {
5072
+ // Remote block hash differs from our local state, need to resolve
5073
+ debug$1(`Resolving remote block: ${remoteBlockHash} @${remoteIndex} (differs from local state)`);
5074
+ await this.resolveBlock(remoteBlockHash);
5075
+ const blocksSynced = remoteIndex - localIndex;
5076
+ debug$1(`Resolved ${blocksSynced} new block(s)`);
5054
5077
  const blocks = this.#blocks;
5078
+ debug$1(`Loading blocks from index ${localIndex + 1} to ${remoteIndex}`);
5055
5079
  const start = localIndex + 1;
5056
- if (this.#machine) {
5080
+ if (this.#machine && blocks.length > start) {
5057
5081
  await this.#loadBlocks(blocks.slice(start));
5058
5082
  }
5059
- await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
5083
+ // Update state with the latest block
5084
+ if (blocks.length > 0) {
5085
+ await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
5086
+ }
5087
+ }
5088
+ else {
5089
+ debug$1(`Block already in local state. Remote hash: ${remoteBlockHash} matches local state`);
5060
5090
  }
5061
5091
  }
5062
5092
  catch (error) {
package/exports/chain.js CHANGED
@@ -1060,6 +1060,11 @@ class State extends Contract {
1060
1060
  try {
1061
1061
  await this.jobber.add(() => this.#resolveBlock(hash));
1062
1062
  this.#resolving = false;
1063
+ const lastBlockHash = await globalThis.stateStore.get('lastBlock');
1064
+ if (lastBlockHash === hash) {
1065
+ this.#resolveErrored = false;
1066
+ return;
1067
+ }
1063
1068
  if (!this.#blockHashMap.has(this.#lastResolved.previousHash) && this.#lastResolved.previousHash !== '0x0')
1064
1069
  return this.resolveBlock(this.#lastResolved.previousHash);
1065
1070
  }
@@ -1075,6 +1080,11 @@ class State extends Contract {
1075
1080
  }
1076
1081
  }
1077
1082
  async resolveBlocks() {
1083
+ // Don't re-resolve if already syncing or resolving
1084
+ if (this.#chainSyncing || this.#resolving) {
1085
+ debug$1('Already syncing or resolving, skipping resolveBlocks()');
1086
+ return;
1087
+ }
1078
1088
  try {
1079
1089
  if (this.jobber.busy && this.jobber.destroy) {
1080
1090
  await this.jobber.destroy();
@@ -1087,6 +1097,7 @@ class State extends Contract {
1087
1097
  const localBlock = await globalThis.chainStore.get('lastBlock');
1088
1098
  const hash = new TextDecoder().decode(localBlock);
1089
1099
  if (hash && hash !== '0x0') {
1100
+ debug$1(`Resolving blocks from hash: ${hash}`);
1090
1101
  await this.resolveBlock(hash);
1091
1102
  }
1092
1103
  }
@@ -1170,20 +1181,39 @@ class State extends Contract {
1170
1181
  // await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)))
1171
1182
  // }
1172
1183
  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'}`);
1184
+ const localIndex = localBlock ? Number(localBlock.index) : -1;
1185
+ const remoteIndex = Number(lastBlock.index);
1186
+ const remoteBlockHash = lastBlock.hash;
1187
+ // Get the local state hash from chainStore
1188
+ let localStateHash = '0x0';
1189
+ try {
1190
+ localStateHash = new TextDecoder().decode(await globalThis.chainStore.get('lastBlock'));
1191
+ }
1192
+ catch (error) {
1193
+ debug$1(`No local state hash found: ${error}`);
1194
+ }
1195
+ debug$1(`Local block height: ${localIndex}, remote block height: ${remoteIndex}`);
1196
+ debug$1(`Local state hash: ${localStateHash}, remote block hash: ${remoteBlockHash}`);
1197
+ // Use state hash comparison: only resolve if remote hash differs from local state hash
1198
+ if (localStateHash !== remoteBlockHash) {
1199
+ // Remote block hash differs from our local state, need to resolve
1200
+ debug$1(`Resolving remote block: ${remoteBlockHash} @${remoteIndex} (differs from local state)`);
1201
+ await this.resolveBlock(remoteBlockHash);
1202
+ const blocksSynced = remoteIndex - localIndex;
1203
+ debug$1(`Resolved ${blocksSynced} new block(s)`);
1181
1204
  const blocks = this.#blocks;
1205
+ debug$1(`Loading blocks from index ${localIndex + 1} to ${remoteIndex}`);
1182
1206
  const start = localIndex + 1;
1183
- if (this.#machine) {
1207
+ if (this.#machine && blocks.length > start) {
1184
1208
  await this.#loadBlocks(blocks.slice(start));
1185
1209
  }
1186
- await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
1210
+ // Update state with the latest block
1211
+ if (blocks.length > 0) {
1212
+ await this.updateState(new BlockMessage(blocks[blocks.length - 1]));
1213
+ }
1214
+ }
1215
+ else {
1216
+ debug$1(`Block already in local state. Remote hash: ${remoteBlockHash} matches local state`);
1187
1217
  }
1188
1218
  }
1189
1219
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.122",
3
+ "version": "1.7.124",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {