@leofcoin/chain 1.7.126 → 1.7.128

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
  }
@@ -5125,29 +5131,39 @@ class State extends Contract {
5125
5131
  }
5126
5132
  // @ts-ignore
5127
5133
  promises = await this.promiseRequests(promises);
5128
- let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
5129
- promises = promises.sort((a, b) => b.index - a.index);
5130
- if (promises.length > 0)
5131
- latest = promises[0].value;
5132
- if (latest.hash && latest.hash !== '0x0') {
5133
- let message = await globalThis.peernet.get(latest.hash, 'block');
5134
- message = await new BlockMessage(message);
5135
- const hash = await message.hash();
5136
- if (hash !== latest.hash)
5137
- throw new Error('invalid block @getLatestBlock');
5138
- latest = { ...message.decoded, hash };
5139
- const peer = promises[0].peer;
5140
- if (peer.connected && peer.version === this.version) {
5141
- let data = await new globalThis.peernet.protos['peernet-request']({
5142
- request: 'knownBlocks'
5134
+ const candidates = promises.sort((a, b) => b.value.index - a.value.index);
5135
+ for (const { value, peer } of candidates) {
5136
+ if (!value?.hash || value.hash === '0x0')
5137
+ continue;
5138
+ try {
5139
+ let message = await globalThis.peernet.get(value.hash, 'block');
5140
+ message = await new BlockMessage(message);
5141
+ const hash = await message.hash();
5142
+ if (hash !== value.hash) {
5143
+ throw new Error(`invalid block hash for candidate ${value.hash}`);
5144
+ }
5145
+ const latest = { ...message.decoded, hash };
5146
+ if (peer?.connected && peer.version === this.version) {
5147
+ let data = await new globalThis.peernet.protos['peernet-request']({
5148
+ request: 'knownBlocks'
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
5143
5162
  });
5144
- let node = await globalThis.peernet.prepareMessage(data);
5145
- let message = await peer.request(node.encode());
5146
- message = await new globalThis.peernet.protos['peernet-response'](message);
5147
- this.knownBlocks = message.decoded.response;
5163
+ continue;
5148
5164
  }
5149
5165
  }
5150
- return latest;
5166
+ return { index: 0, hash: '0x0', previousHash: '0x0' };
5151
5167
  }
5152
5168
  #loadBlockTransactions;
5153
5169
  #getLastTransactions;
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
  }
@@ -1252,29 +1258,39 @@ class State extends Contract {
1252
1258
  }
1253
1259
  // @ts-ignore
1254
1260
  promises = await this.promiseRequests(promises);
1255
- let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
1256
- promises = promises.sort((a, b) => b.index - a.index);
1257
- if (promises.length > 0)
1258
- latest = promises[0].value;
1259
- if (latest.hash && latest.hash !== '0x0') {
1260
- let message = await globalThis.peernet.get(latest.hash, 'block');
1261
- message = await new BlockMessage(message);
1262
- const hash = await message.hash();
1263
- if (hash !== latest.hash)
1264
- throw new Error('invalid block @getLatestBlock');
1265
- latest = { ...message.decoded, hash };
1266
- const peer = promises[0].peer;
1267
- if (peer.connected && peer.version === this.version) {
1268
- let data = await new globalThis.peernet.protos['peernet-request']({
1269
- request: 'knownBlocks'
1261
+ const candidates = promises.sort((a, b) => b.value.index - a.value.index);
1262
+ for (const { value, peer } of candidates) {
1263
+ if (!value?.hash || value.hash === '0x0')
1264
+ continue;
1265
+ try {
1266
+ let message = await globalThis.peernet.get(value.hash, 'block');
1267
+ message = await new BlockMessage(message);
1268
+ const hash = await message.hash();
1269
+ if (hash !== value.hash) {
1270
+ throw new Error(`invalid block hash for candidate ${value.hash}`);
1271
+ }
1272
+ const latest = { ...message.decoded, hash };
1273
+ if (peer?.connected && peer.version === this.version) {
1274
+ let data = await new globalThis.peernet.protos['peernet-request']({
1275
+ request: 'knownBlocks'
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
1270
1289
  });
1271
- let node = await globalThis.peernet.prepareMessage(data);
1272
- let message = await peer.request(node.encode());
1273
- message = await new globalThis.peernet.protos['peernet-response'](message);
1274
- this.knownBlocks = message.decoded.response;
1290
+ continue;
1275
1291
  }
1276
1292
  }
1277
- return latest;
1293
+ return { index: 0, hash: '0x0', previousHash: '0x0' };
1278
1294
  }
1279
1295
  #loadBlockTransactions;
1280
1296
  #getLastTransactions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.126",
3
+ "version": "1.7.128",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {