@leofcoin/chain 1.4.34 → 1.4.35

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.
package/exports/chain.js CHANGED
@@ -457,6 +457,7 @@ class Chain extends Contract {
457
457
  #validators = [];
458
458
  /** {Block[]} */
459
459
  #blocks = [];
460
+ #blockHashMap = new Map();
460
461
  #machine;
461
462
  /** {Boolean} */
462
463
  #runningEpoch = false;
@@ -715,7 +716,7 @@ class Chain extends Contract {
715
716
  return { has, address };
716
717
  }));
717
718
  promises = promises.filter(({ status, value }) => status === 'fulfilled' && !value.has);
718
- await Promise.allSettled(promises.map(({ value }) => peernet.get(value.address, 'block')));
719
+ await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)));
719
720
  }
720
721
  if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
721
722
  // TODO: check if valid
@@ -724,7 +725,6 @@ class Chain extends Contract {
724
725
  await this.resolveBlock(lastBlock.hash);
725
726
  let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index - localIndex) : index;
726
727
  debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
727
- this.#blocks.length;
728
728
  const start = (this.#blocks.length - blocksSynced) - 1;
729
729
  await this.#loadBlocks(this.blocks.slice(start));
730
730
  await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
@@ -752,22 +752,40 @@ class Chain extends Contract {
752
752
  async #knownBlocksHandler() {
753
753
  return new peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
754
754
  }
755
- async resolveBlock(hash) {
756
- if (!hash)
757
- throw new Error(`expected hash, got: ${hash}`);
755
+ async getAndPutBlock(hash) {
758
756
  let block = await peernet.get(hash, 'block');
759
757
  block = await new BlockMessage(block);
758
+ const { index } = block.decoded;
759
+ if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
760
+ throw `invalid block ${hash} @${index}`;
760
761
  if (!await peernet.has(hash, 'block'))
761
762
  await peernet.put(hash, block.encoded, 'block');
762
- const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength;
763
- this.#totalSize += size;
764
- block = { ...block.decoded, hash };
765
- if (this.#blocks[block.index] && this.#blocks[block.index].hash !== block.hash)
766
- throw `invalid block ${hash} @${block.index}`;
767
- this.#blocks[block.index] = block;
768
- console.log(`resolved block: ${hash} @${block.index} ${formatBytes(size)}`);
769
- if (block.previousHash !== '0x0') {
770
- return this.resolveBlock(block.previousHash);
763
+ return block;
764
+ }
765
+ async resolveBlock(hash) {
766
+ if (!hash)
767
+ throw new Error(`expected hash, got: ${hash}`);
768
+ const index = this.#blockHashMap.get(hash);
769
+ if (this.#blocks[index]) {
770
+ if (this.#blocks[index].previousHash !== '0x0') {
771
+ return this.resolveBlock(this.#blocks[index]);
772
+ }
773
+ return;
774
+ }
775
+ try {
776
+ const block = await this.getAndPutBlock(hash);
777
+ const { previousHash, index } = block.decoded;
778
+ const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength;
779
+ this.#totalSize += size;
780
+ this.#blocks[index] = { hash, ...block.decoded };
781
+ this.#blockHashMap.set(hash, index);
782
+ console.log(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
783
+ if (previousHash !== '0x0') {
784
+ return this.resolveBlock(previousHash);
785
+ }
786
+ }
787
+ catch (error) {
788
+ console.error(error);
771
789
  }
772
790
  }
773
791
  async resolveBlocks() {
@@ -1,3 +1,4 @@
1
+ import { BlockMessage } from '@leofcoin/messages';
1
2
  import Contract from './contract.js';
2
3
  import { BigNumberish } from '@ethersproject/bignumber';
3
4
  export default class Chain extends Contract {
@@ -25,6 +26,7 @@ export default class Chain extends Contract {
25
26
  hash: string;
26
27
  previousHash: string;
27
28
  }>;
29
+ getAndPutBlock(hash: string): BlockMessage;
28
30
  resolveBlock(hash: any): any;
29
31
  resolveBlocks(): any;
30
32
  participate(address: any): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.34",
3
+ "version": "1.4.35",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",