@leofcoin/chain 1.7.125 → 1.7.127

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.
@@ -4876,11 +4876,17 @@ 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
+ }
4879
4885
  const { index } = block.decoded;
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');
4886
+ if (this.#blocks[index] && this.#blocks[index].hash !== computedHash)
4887
+ throw new ResolveError(`invalid block ${computedHash} @${index}`);
4888
+ if (!(await globalThis.peernet.has(computedHash)))
4889
+ await globalThis.peernet.put(computedHash, block.encoded, 'block');
4884
4890
  }
4885
4891
  return block;
4886
4892
  }
@@ -4890,7 +4896,7 @@ class State extends Contract {
4890
4896
  // Block already exists, check if we need to resolve previous blocks
4891
4897
  const localHash = await globalThis.stateStore.get('lastBlock');
4892
4898
  const previousHash = this.#blocks[index].previousHash;
4893
- if (hash === localHash)
4899
+ if (previousHash === localHash)
4894
4900
  return;
4895
4901
  if (previousHash !== '0x0' && !this.#blocks[this.#blockHashMap.get(previousHash)]) {
4896
4902
  // Previous block not in memory, recursively resolve it
package/exports/chain.js CHANGED
@@ -1003,11 +1003,17 @@ 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
+ }
1006
1012
  const { index } = block.decoded;
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');
1013
+ if (this.#blocks[index] && this.#blocks[index].hash !== computedHash)
1014
+ throw new ResolveError(`invalid block ${computedHash} @${index}`);
1015
+ if (!(await globalThis.peernet.has(computedHash)))
1016
+ await globalThis.peernet.put(computedHash, block.encoded, 'block');
1011
1017
  }
1012
1018
  return block;
1013
1019
  }
@@ -1017,7 +1023,7 @@ class State extends Contract {
1017
1023
  // Block already exists, check if we need to resolve previous blocks
1018
1024
  const localHash = await globalThis.stateStore.get('lastBlock');
1019
1025
  const previousHash = this.#blocks[index].previousHash;
1020
- if (hash === localHash)
1026
+ if (previousHash === localHash)
1021
1027
  return;
1022
1028
  if (previousHash !== '0x0' && !this.#blocks[this.#blockHashMap.get(previousHash)]) {
1023
1029
  // Previous block not in memory, recursively resolve it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.125",
3
+ "version": "1.7.127",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {