@leofcoin/workers 1.5.41 → 1.5.43

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.
@@ -1738,11 +1738,25 @@ const _ = {
1738
1738
  addLoadedBlock: (block) => {
1739
1739
  const size = formatBytes(block.length);
1740
1740
  block = JSON.parse(block, jsonParseBigInt);
1741
- // if (block.decoded) block = { ...block.decoded, hash: await new BlockMessage(block).hash() }
1742
- // if (blocks[block.index - 1]) {
1743
- // console.warn(`block ${block.index} already loaded, skipping`)
1744
- // return false
1745
- // }
1741
+ const index = Number(block.index);
1742
+ const tipIndex = Number(lastBlock.index);
1743
+ if (!['number', 'bigint'].includes(typeof block.index) ||
1744
+ !Number.isSafeInteger(index) || index < 0 || typeof block.hash !== 'string' || !block.hash) {
1745
+ throw new Error('Invalid loaded block identity');
1746
+ }
1747
+ if (lastBlock.hash !== '0x0') {
1748
+ if (index < tipIndex)
1749
+ return false;
1750
+ if (index === tipIndex) {
1751
+ if (block.hash === lastBlock.hash)
1752
+ return false;
1753
+ throw new Error(`Loaded block conflict at index ${index}`);
1754
+ }
1755
+ }
1756
+ const expectedIndex = lastBlock.hash === '0x0' ? 0 : tipIndex + 1;
1757
+ if (index !== expectedIndex || block.previousHash !== lastBlock.hash) {
1758
+ throw new Error(`Loaded block does not extend canonical tip at index ${index}`);
1759
+ }
1746
1760
  blocks[block.index.toString()] = block;
1747
1761
  lastBlock = blocks[blocks.length - 1];
1748
1762
  totalBlocks += 1n;
@@ -1738,11 +1738,25 @@ const _ = {
1738
1738
  addLoadedBlock: (block) => {
1739
1739
  const size = formatBytes(block.length);
1740
1740
  block = JSON.parse(block, jsonParseBigInt);
1741
- // if (block.decoded) block = { ...block.decoded, hash: await new BlockMessage(block).hash() }
1742
- // if (blocks[block.index - 1]) {
1743
- // console.warn(`block ${block.index} already loaded, skipping`)
1744
- // return false
1745
- // }
1741
+ const index = Number(block.index);
1742
+ const tipIndex = Number(lastBlock.index);
1743
+ if (!['number', 'bigint'].includes(typeof block.index) ||
1744
+ !Number.isSafeInteger(index) || index < 0 || typeof block.hash !== 'string' || !block.hash) {
1745
+ throw new Error('Invalid loaded block identity');
1746
+ }
1747
+ if (lastBlock.hash !== '0x0') {
1748
+ if (index < tipIndex)
1749
+ return false;
1750
+ if (index === tipIndex) {
1751
+ if (block.hash === lastBlock.hash)
1752
+ return false;
1753
+ throw new Error(`Loaded block conflict at index ${index}`);
1754
+ }
1755
+ }
1756
+ const expectedIndex = lastBlock.hash === '0x0' ? 0 : tipIndex + 1;
1757
+ if (index !== expectedIndex || block.previousHash !== lastBlock.hash) {
1758
+ throw new Error(`Loaded block does not extend canonical tip at index ${index}`);
1759
+ }
1746
1760
  blocks[block.index.toString()] = block;
1747
1761
  lastBlock = blocks[blocks.length - 1];
1748
1762
  totalBlocks += 1n;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/workers",
3
- "version": "1.5.41",
3
+ "version": "1.5.43",
4
4
  "description": "Workers used in @leofcoin/chain",
5
5
  "type": "module",
6
6
  "private": false,
@@ -38,7 +38,7 @@
38
38
  "@noble/hashes": "^2.4.0",
39
39
  "acorn": "^8.19.0",
40
40
  "@leofcoin/addresses": "^1.0.64",
41
- "@leofcoin/lib": "^1.3.13",
41
+ "@leofcoin/lib": "^1.3.16",
42
42
  "@leofcoin/messages": "^1.6.5",
43
43
  "@leofcoin/storage": "^3.5.38",
44
44
  "@leofcoin/utils": "^1.1.45",