@leofcoin/chain 1.5.0 → 1.5.2

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.
@@ -1034,14 +1034,34 @@ class Jobber {
1034
1034
  this.busy = false;
1035
1035
  resolve('stopped');
1036
1036
  };
1037
- const result = await fn();
1038
- clearTimeout(timeout);
1039
- this.busy = false;
1040
- resolve(result);
1037
+ try {
1038
+ const result = await fn();
1039
+ clearTimeout(timeout);
1040
+ this.busy = false;
1041
+ resolve(result);
1042
+ }
1043
+ catch (error) {
1044
+ reject(error);
1045
+ }
1041
1046
  });
1042
1047
  }
1043
1048
  }
1044
1049
 
1050
+ class LeofcoinError extends Error {
1051
+ #message;
1052
+ constructor(message, options) {
1053
+ super(message, options);
1054
+ this.#message = message;
1055
+ }
1056
+ get message() {
1057
+ return `${this.name}: ${this.#message}`;
1058
+ }
1059
+ }
1060
+ class ResolveError extends LeofcoinError {
1061
+ name = 'ResolveError';
1062
+ }
1063
+ const isResolveError = (error) => error.name === 'ResolveError';
1064
+
1045
1065
  class State extends Contract {
1046
1066
  #resolveErrored;
1047
1067
  #lastResolvedTime;
@@ -1201,9 +1221,17 @@ class State extends Contract {
1201
1221
  console.error(error);
1202
1222
  throw error;
1203
1223
  }
1204
- await this.resolveBlocks();
1205
- this.#machine = await new Machine(this.#blocks);
1206
- await this.#loadBlocks(this.#blocks);
1224
+ try {
1225
+ await this.resolveBlocks();
1226
+ this.#machine = await new Machine(this.#blocks);
1227
+ await this.#loadBlocks(this.#blocks);
1228
+ }
1229
+ catch (error) {
1230
+ if (isResolveError(error)) {
1231
+ console.error(error);
1232
+ }
1233
+ console.log(error);
1234
+ }
1207
1235
  }
1208
1236
  async updateState(message) {
1209
1237
  const hash = await message.hash();
@@ -1258,13 +1286,11 @@ class State extends Contract {
1258
1286
  catch (error) {
1259
1287
  this.#resolving = false;
1260
1288
  this.#chainSyncing = false;
1261
- throw new Error('resolve error');
1289
+ throw new ResolveError(`block: ${hash}@${index}`);
1262
1290
  }
1263
1291
  return;
1264
1292
  }
1265
1293
  async resolveBlock(hash) {
1266
- if (this.#resolveErrorCount === 3)
1267
- this.#resolveErrorCount = 0;
1268
1294
  if (!hash)
1269
1295
  throw new Error(`expected hash, got: ${hash}`);
1270
1296
  if (hash === '0x0')
@@ -1279,11 +1305,12 @@ class State extends Contract {
1279
1305
  return this.resolveBlock(this.#lastResolved.previousHash);
1280
1306
  }
1281
1307
  catch (error) {
1308
+ console.log({ error });
1282
1309
  this.#resolveErrorCount += 1;
1283
1310
  if (this.#resolveErrorCount < 3)
1284
1311
  return this.resolveBlock(hash);
1285
- else
1286
- throw new Error('resolve errored');
1312
+ this.#resolveErrorCount = 0;
1313
+ throw new ResolveError(`block: ${hash}`, { cause: error });
1287
1314
  }
1288
1315
  }
1289
1316
  async resolveBlocks() {
@@ -1303,10 +1330,30 @@ class State extends Contract {
1303
1330
  this.#lastBlock = this.#blocks[this.#blocks.length - 1];
1304
1331
  }
1305
1332
  }
1306
- catch {
1333
+ catch (error) {
1334
+ console.log(error);
1335
+ this.#resolveErrored = true;
1336
+ this.#resolveErrorCount += 1;
1337
+ this.#resolving = false;
1338
+ this.restoreChain();
1339
+ // console.log(e);
1340
+ }
1341
+ }
1342
+ async restoreChain() {
1343
+ try {
1344
+ const { hash } = await this.#getLatestBlock();
1345
+ await globalThis.chainStore.put('lastBlock', hash);
1346
+ if (hash && hash !== '0x0') {
1347
+ await this.resolveBlock(hash);
1348
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1];
1349
+ }
1350
+ }
1351
+ catch (error) {
1352
+ console.log(error);
1307
1353
  this.#resolveErrored = true;
1308
1354
  this.#resolveErrorCount += 1;
1309
1355
  this.#resolving = false;
1356
+ this.restoreChain();
1310
1357
  // console.log(e);
1311
1358
  }
1312
1359
  }
package/exports/chain.js CHANGED
@@ -4,6 +4,7 @@ import addresses, { contractFactory, nativeToken, validators, nameService } from
4
4
  import { calculateFee, createContractMessage, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage, signTransaction } from '@leofcoin/lib';
5
5
  import { randombytes } from '@leofcoin/crypto';
6
6
  import EasyWorker from '@vandeurenglenn/easy-worker';
7
+ import { isResolveError, ResolveError } from '@leofcoin/errors';
7
8
 
8
9
  const limit = 1800;
9
10
  const transactionLimit = 1800;
@@ -488,10 +489,15 @@ class Jobber {
488
489
  this.busy = false;
489
490
  resolve('stopped');
490
491
  };
491
- const result = await fn();
492
- clearTimeout(timeout);
493
- this.busy = false;
494
- resolve(result);
492
+ try {
493
+ const result = await fn();
494
+ clearTimeout(timeout);
495
+ this.busy = false;
496
+ resolve(result);
497
+ }
498
+ catch (error) {
499
+ reject(error);
500
+ }
495
501
  });
496
502
  }
497
503
  }
@@ -655,9 +661,17 @@ class State extends Contract {
655
661
  console.error(error);
656
662
  throw error;
657
663
  }
658
- await this.resolveBlocks();
659
- this.#machine = await new Machine(this.#blocks);
660
- await this.#loadBlocks(this.#blocks);
664
+ try {
665
+ await this.resolveBlocks();
666
+ this.#machine = await new Machine(this.#blocks);
667
+ await this.#loadBlocks(this.#blocks);
668
+ }
669
+ catch (error) {
670
+ if (isResolveError(error)) {
671
+ console.error(error);
672
+ }
673
+ console.log(error);
674
+ }
661
675
  }
662
676
  async updateState(message) {
663
677
  const hash = await message.hash();
@@ -712,13 +726,11 @@ class State extends Contract {
712
726
  catch (error) {
713
727
  this.#resolving = false;
714
728
  this.#chainSyncing = false;
715
- throw new Error('resolve error');
729
+ throw new ResolveError(`block: ${hash}@${index}`);
716
730
  }
717
731
  return;
718
732
  }
719
733
  async resolveBlock(hash) {
720
- if (this.#resolveErrorCount === 3)
721
- this.#resolveErrorCount = 0;
722
734
  if (!hash)
723
735
  throw new Error(`expected hash, got: ${hash}`);
724
736
  if (hash === '0x0')
@@ -733,11 +745,12 @@ class State extends Contract {
733
745
  return this.resolveBlock(this.#lastResolved.previousHash);
734
746
  }
735
747
  catch (error) {
748
+ console.log({ error });
736
749
  this.#resolveErrorCount += 1;
737
750
  if (this.#resolveErrorCount < 3)
738
751
  return this.resolveBlock(hash);
739
- else
740
- throw new Error('resolve errored');
752
+ this.#resolveErrorCount = 0;
753
+ throw new ResolveError(`block: ${hash}`, { cause: error });
741
754
  }
742
755
  }
743
756
  async resolveBlocks() {
@@ -757,10 +770,30 @@ class State extends Contract {
757
770
  this.#lastBlock = this.#blocks[this.#blocks.length - 1];
758
771
  }
759
772
  }
760
- catch {
773
+ catch (error) {
774
+ console.log(error);
775
+ this.#resolveErrored = true;
776
+ this.#resolveErrorCount += 1;
777
+ this.#resolving = false;
778
+ this.restoreChain();
779
+ // console.log(e);
780
+ }
781
+ }
782
+ async restoreChain() {
783
+ try {
784
+ const { hash } = await this.#getLatestBlock();
785
+ await globalThis.chainStore.put('lastBlock', hash);
786
+ if (hash && hash !== '0x0') {
787
+ await this.resolveBlock(hash);
788
+ this.#lastBlock = this.#blocks[this.#blocks.length - 1];
789
+ }
790
+ }
791
+ catch (error) {
792
+ console.log(error);
761
793
  this.#resolveErrored = true;
762
794
  this.#resolveErrorCount += 1;
763
795
  this.#resolving = false;
796
+ this.restoreChain();
764
797
  // console.log(e);
765
798
  }
766
799
  }
@@ -31,6 +31,7 @@ export default class State extends Contract {
31
31
  getAndPutBlock(hash: string): BlockMessage;
32
32
  resolveBlock(hash: any): any;
33
33
  resolveBlocks(): Promise<void>;
34
+ restoreChain(): Promise<void>;
34
35
  syncChain(lastBlock?: any): Promise<SyncState>;
35
36
  promiseRequests(promises: any): Promise<unknown>;
36
37
  get canSync(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",
@@ -66,7 +66,7 @@
66
66
  "@leofcoin/storage": "^3.0.6",
67
67
  "@leofcoin/utils": "^1.1.4",
68
68
  "@leofcoin/workers": "^1.3.13",
69
- "@netpeer/p2pt-swarm": "^1.3.3",
69
+ "@netpeer/p2pt-swarm": "^1.3.5",
70
70
  "@vandeurenglenn/base32": "^1.1.0",
71
71
  "@vandeurenglenn/easy-worker": "^1.0.2",
72
72
  "@vandeurenglenn/queue": "^1.0.0",