@leofcoin/chain 1.4.58 → 1.4.60

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,26 +8181,59 @@ 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
- }
8196
- }
8197
- }, this.syncTimeout);
8198
- timeout();
8199
- if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
8200
- return;
8187
+ async syncChain(lastBlock) {
8188
+ if (!lastBlock)
8189
+ lastBlock = await this.#getLatestBlock();
8190
+ if (this.#chainSyncing) {
8191
+ console.log('already syncing');
8192
+ if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
8193
+ this.#lastBlockInQue = lastBlock;
8194
+ return 'syncing';
8195
+ }
8196
+ console.log('starting sync');
8201
8197
  this.#chainSyncing = true;
8198
+ const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
8199
+ const timeout = setTimeout(() => {
8200
+ this.#chainSyncing = false;
8201
+ this.#syncErrorCount = 0;
8202
+ reject('timedOut');
8203
+ }, 60000);
8204
+ try {
8205
+ await this.#syncChain(lastBlock);
8206
+ clearTimeout(timeout);
8207
+ resolve(true);
8208
+ }
8209
+ catch (error) {
8210
+ clearTimeout(timeout);
8211
+ reject(error);
8212
+ }
8213
+ });
8214
+ if (globalThis.peernet.connections.length === 0)
8215
+ return 'connectionless';
8216
+ try {
8217
+ await syncPromise(lastBlock);
8218
+ if (lastBlock.index === this.#lastBlockInQue?.index || lastBlock.index > this.#lastBlockInQue?.index)
8219
+ this.#lastBlockInQue = undefined;
8220
+ }
8221
+ catch (error) {
8222
+ console.log(error);
8223
+ this.#syncErrorCount += 1;
8224
+ if (this.#syncErrorCount < 3)
8225
+ await syncPromise(lastBlock);
8226
+ this.#chainSyncing = false;
8227
+ this.#syncErrorCount = 0;
8228
+ return 'errored';
8229
+ }
8230
+ this.#syncErrorCount = 0;
8231
+ if (this.#lastBlockInQue)
8232
+ return this.syncChain(this.#lastBlockInQue);
8233
+ this.#chainSyncing = false;
8234
+ return 'synced';
8235
+ }
8236
+ async #syncChain(lastBlock) {
8202
8237
  if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
8203
8238
  let promises = [];
8204
8239
  promises = await Promise.allSettled(this.#knownBlocks.map(async (address) => {
@@ -8220,8 +8255,6 @@ class Chain extends Contract {
8220
8255
  await this.#loadBlocks(this.blocks.slice(start));
8221
8256
  await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
8222
8257
  }
8223
- clearTimeout(current);
8224
- this.#chainSyncing = false;
8225
8258
  }
8226
8259
  async #prepareRequest(request) {
8227
8260
  let node = await new globalThis.peernet.protos['peernet-request']({ request });
@@ -8242,7 +8275,7 @@ class Chain extends Contract {
8242
8275
  console.log(lastBlock);
8243
8276
  this.#resolveErrored = false;
8244
8277
  if (lastBlock)
8245
- await this.#syncChain(lastBlock);
8278
+ await this.syncChain(lastBlock);
8246
8279
  if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
8247
8280
  this.#runEpoch();
8248
8281
  }
@@ -1,4 +1,4 @@
1
- import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-79442920.js';
1
+ import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-39985c71.js';
2
2
  import './index-640d9f36.js';
3
3
 
4
4
  function commonjsRequire(path) {
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-79442920.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-39985c71.js';
2
2
  import './index-640d9f36.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './node-browser-79442920.js';
1
+ import { F as FormatInterface } from './node-browser-39985c71.js';
2
2
  import './index-640d9f36.js';
3
3
 
4
4
  var proto$b = {
@@ -20266,7 +20266,7 @@ class Identity {
20266
20266
  globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
20267
20267
  }
20268
20268
  else {
20269
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-4464b87e-d39e42fc.js');
20269
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-cf8b44ca-a27c61c1.js');
20270
20270
  const { identity, accounts } = await importee.default(password, this.network);
20271
20271
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
20272
20272
  await globalThis.walletStore.put('version', String(1));
@@ -20307,7 +20307,7 @@ class Identity {
20307
20307
  globalThis.LeofcoinStorage = LeofcoinStorage;
20308
20308
  globalThis.leofcoin = globalThis.leofcoin || {};
20309
20309
  globalThis.pubsub = globalThis.pubsub || new LittlePubSub();
20310
- globalThis.globalSub = globalThis.globalSub || new LittlePubSub(true);
20310
+ globalThis.globalSub = globalThis.globalSub || new LittlePubSub();
20311
20311
  /**
20312
20312
  * @access public
20313
20313
  * @example
@@ -20437,7 +20437,7 @@ class Peernet {
20437
20437
  this.root = options.root;
20438
20438
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
20439
20439
  // FolderMessageResponse
20440
- } = await import(/* webpackChunkName: "messages" */ './messages-27279443-8ccccaf2.js');
20440
+ } = await import(/* webpackChunkName: "messages" */ './messages-d292642b-e5aa934c.js');
20441
20441
  /**
20442
20442
  * proto Object containing protos
20443
20443
  * @type {Object}
@@ -20516,7 +20516,7 @@ class Peernet {
20516
20516
  if (this.#starting || this.#started)
20517
20517
  return;
20518
20518
  this.#starting = true;
20519
- const importee = await import('./client-4d80940e-a63ca03c.js');
20519
+ const importee = await import('./client-10cfc17d-c47b062b.js');
20520
20520
  /**
20521
20521
  * @access public
20522
20522
  * @type {PeernetClient}
@@ -1,2 +1,2 @@
1
- export { N as default } from './node-browser-79442920.js';
1
+ export { N as default } from './node-browser-39985c71.js';
2
2
  import './index-640d9f36.js';
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,26 +767,59 @@ 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
- }
782
- }
783
- }, this.syncTimeout);
784
- timeout();
785
- if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
786
- return;
773
+ async syncChain(lastBlock) {
774
+ if (!lastBlock)
775
+ lastBlock = await this.#getLatestBlock();
776
+ if (this.#chainSyncing) {
777
+ console.log('already syncing');
778
+ if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
779
+ this.#lastBlockInQue = lastBlock;
780
+ return 'syncing';
781
+ }
782
+ console.log('starting sync');
787
783
  this.#chainSyncing = true;
784
+ const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
785
+ const timeout = setTimeout(() => {
786
+ this.#chainSyncing = false;
787
+ this.#syncErrorCount = 0;
788
+ reject('timedOut');
789
+ }, 60000);
790
+ try {
791
+ await this.#syncChain(lastBlock);
792
+ clearTimeout(timeout);
793
+ resolve(true);
794
+ }
795
+ catch (error) {
796
+ clearTimeout(timeout);
797
+ reject(error);
798
+ }
799
+ });
800
+ if (globalThis.peernet.connections.length === 0)
801
+ return 'connectionless';
802
+ try {
803
+ await syncPromise(lastBlock);
804
+ if (lastBlock.index === this.#lastBlockInQue?.index || lastBlock.index > this.#lastBlockInQue?.index)
805
+ this.#lastBlockInQue = undefined;
806
+ }
807
+ catch (error) {
808
+ console.log(error);
809
+ this.#syncErrorCount += 1;
810
+ if (this.#syncErrorCount < 3)
811
+ await syncPromise(lastBlock);
812
+ this.#chainSyncing = false;
813
+ this.#syncErrorCount = 0;
814
+ return 'errored';
815
+ }
816
+ this.#syncErrorCount = 0;
817
+ if (this.#lastBlockInQue)
818
+ return this.syncChain(this.#lastBlockInQue);
819
+ this.#chainSyncing = false;
820
+ return 'synced';
821
+ }
822
+ async #syncChain(lastBlock) {
788
823
  if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
789
824
  let promises = [];
790
825
  promises = await Promise.allSettled(this.#knownBlocks.map(async (address) => {
@@ -806,8 +841,6 @@ class Chain extends Contract {
806
841
  await this.#loadBlocks(this.blocks.slice(start));
807
842
  await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
808
843
  }
809
- clearTimeout(current);
810
- this.#chainSyncing = false;
811
844
  }
812
845
  async #prepareRequest(request) {
813
846
  let node = await new globalThis.peernet.protos['peernet-request']({ request });
@@ -828,7 +861,7 @@ class Chain extends Contract {
828
861
  console.log(lastBlock);
829
862
  this.#resolveErrored = false;
830
863
  if (lastBlock)
831
- await this.#syncChain(lastBlock);
864
+ await this.syncChain(lastBlock);
832
865
  if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
833
866
  this.#runEpoch();
834
867
  }
@@ -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.60",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",