@leofcoin/chain 1.4.58 → 1.4.59

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.
@@ -7883,6 +7883,8 @@ class Contract extends Transaction {
7883
7883
  globalThis.BigNumber = BigNumber;
7884
7884
  // check if browser or local
7885
7885
  class Chain extends Contract {
7886
+ #lastBlockInQue;
7887
+ #syncErrorCount = 0;
7886
7888
  version;
7887
7889
  #resolveErrored;
7888
7890
  #state;
@@ -8179,24 +8181,50 @@ class Chain extends Contract {
8179
8181
  if (this.#chainSyncing)
8180
8182
  return 'already syncing';
8181
8183
  const latest = await this.#getLatestBlock();
8182
- await this.#syncChain(latest);
8184
+ await this.syncChain(latest);
8183
8185
  return 'synced';
8184
8186
  }
8185
- async #syncChain(lastBlock) {
8186
- let current;
8187
- const timeout = () => current = setTimeout(() => {
8188
- if (this.#chainSyncing) {
8189
- if (this.#lastResolved + this.syncTimeout > Date.now())
8190
- timeout();
8191
- else {
8192
- this.#chainSyncing = false;
8193
- console.log('resyncing');
8194
- this.#syncChain(lastBlock);
8195
- }
8187
+ async syncChain(lastBlock) {
8188
+ if (!lastBlock)
8189
+ lastBlock = await this.#getLatestBlock();
8190
+ if (this.#chainSyncing) {
8191
+ if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
8192
+ this.#lastBlockInQue = lastBlock;
8193
+ return 'syncing';
8194
+ }
8195
+ this.#chainSyncing = true;
8196
+ const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
8197
+ setTimeout(() => {
8198
+ reject('timedOut');
8199
+ }, 10000);
8200
+ try {
8201
+ await this.#syncChain(lastBlock);
8202
+ resolve(true);
8196
8203
  }
8197
- }, this.syncTimeout);
8198
- timeout();
8199
- if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
8204
+ catch (error) {
8205
+ reject(error);
8206
+ }
8207
+ });
8208
+ if (globalThis.peernet.connections.length === 0)
8209
+ return 'connectionless';
8210
+ try {
8211
+ await syncPromise(lastBlock);
8212
+ }
8213
+ catch (error) {
8214
+ console.log(error);
8215
+ this.#syncErrorCount += 1;
8216
+ if (this.#syncErrorCount <= 3)
8217
+ return this.syncChain(this.#lastBlockInQue);
8218
+ return 'errored';
8219
+ }
8220
+ this.#syncErrorCount = 0;
8221
+ if (this.#lastBlockInQue)
8222
+ return this.syncChain(this.#lastBlockInQue);
8223
+ this.#chainSyncing = false;
8224
+ return 'synced';
8225
+ }
8226
+ async #syncChain(lastBlock) {
8227
+ if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.index)
8200
8228
  return;
8201
8229
  this.#chainSyncing = true;
8202
8230
  if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
@@ -8242,7 +8270,7 @@ class Chain extends Contract {
8242
8270
  console.log(lastBlock);
8243
8271
  this.#resolveErrored = false;
8244
8272
  if (lastBlock)
8245
- await this.#syncChain(lastBlock);
8273
+ await this.syncChain(lastBlock);
8246
8274
  if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
8247
8275
  this.#runEpoch();
8248
8276
  }
package/exports/chain.js CHANGED
@@ -469,6 +469,8 @@ class Contract extends Transaction {
469
469
  globalThis.BigNumber = BigNumber;
470
470
  // check if browser or local
471
471
  class Chain extends Contract {
472
+ #lastBlockInQue;
473
+ #syncErrorCount = 0;
472
474
  version;
473
475
  #resolveErrored;
474
476
  #state;
@@ -765,24 +767,50 @@ class Chain extends Contract {
765
767
  if (this.#chainSyncing)
766
768
  return 'already syncing';
767
769
  const latest = await this.#getLatestBlock();
768
- await this.#syncChain(latest);
770
+ await this.syncChain(latest);
769
771
  return 'synced';
770
772
  }
771
- async #syncChain(lastBlock) {
772
- let current;
773
- const timeout = () => current = setTimeout(() => {
774
- if (this.#chainSyncing) {
775
- if (this.#lastResolved + this.syncTimeout > Date.now())
776
- timeout();
777
- else {
778
- this.#chainSyncing = false;
779
- console.log('resyncing');
780
- this.#syncChain(lastBlock);
781
- }
773
+ async syncChain(lastBlock) {
774
+ if (!lastBlock)
775
+ lastBlock = await this.#getLatestBlock();
776
+ if (this.#chainSyncing) {
777
+ if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
778
+ this.#lastBlockInQue = lastBlock;
779
+ return 'syncing';
780
+ }
781
+ this.#chainSyncing = true;
782
+ const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
783
+ setTimeout(() => {
784
+ reject('timedOut');
785
+ }, 10000);
786
+ try {
787
+ await this.#syncChain(lastBlock);
788
+ resolve(true);
789
+ }
790
+ catch (error) {
791
+ reject(error);
782
792
  }
783
- }, this.syncTimeout);
784
- timeout();
785
- if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
793
+ });
794
+ if (globalThis.peernet.connections.length === 0)
795
+ return 'connectionless';
796
+ try {
797
+ await syncPromise(lastBlock);
798
+ }
799
+ catch (error) {
800
+ console.log(error);
801
+ this.#syncErrorCount += 1;
802
+ if (this.#syncErrorCount <= 3)
803
+ return this.syncChain(this.#lastBlockInQue);
804
+ return 'errored';
805
+ }
806
+ this.#syncErrorCount = 0;
807
+ if (this.#lastBlockInQue)
808
+ return this.syncChain(this.#lastBlockInQue);
809
+ this.#chainSyncing = false;
810
+ return 'synced';
811
+ }
812
+ async #syncChain(lastBlock) {
813
+ if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.index)
786
814
  return;
787
815
  this.#chainSyncing = true;
788
816
  if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
@@ -828,7 +856,7 @@ class Chain extends Contract {
828
856
  console.log(lastBlock);
829
857
  this.#resolveErrored = false;
830
858
  if (lastBlock)
831
- await this.#syncChain(lastBlock);
859
+ await this.syncChain(lastBlock);
832
860
  if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
833
861
  this.#runEpoch();
834
862
  }
@@ -1,6 +1,7 @@
1
1
  import { BlockMessage } from '@leofcoin/messages';
2
2
  import Contract from './contract.js';
3
3
  import { BigNumberish } from '@ethersproject/bignumber';
4
+ declare type chainSyncState = 'syncing' | 'synced' | 'errored' | 'connectionless';
4
5
  export default class Chain extends Contract {
5
6
  #private;
6
7
  version: String;
@@ -29,7 +30,8 @@ export default class Chain extends Contract {
29
30
  hash: string;
30
31
  previousHash: string;
31
32
  }>;
32
- triggerSync(): Promise<"already syncing" | "synced">;
33
+ triggerSync(): Promise<"synced" | "already syncing">;
34
+ syncChain(lastBlock: any): Promise<chainSyncState>;
33
35
  getAndPutBlock(hash: string): BlockMessage;
34
36
  resolveBlock(hash: any): any;
35
37
  resolveBlocks(): any;
@@ -94,3 +96,4 @@ export default class Chain extends Contract {
94
96
  address: string;
95
97
  };
96
98
  }
99
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.58",
3
+ "version": "1.4.59",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",