@leofcoin/chain 1.5.23 → 1.5.24

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.
@@ -1,8 +1,8 @@
1
1
  import { BigNumber } from '@leofcoin/utils';
2
2
  import { BigNumberish } from '@ethersproject/bignumber';
3
- import State from './state.js';
4
3
  import { Address } from './types.js';
5
- export default class Chain extends State {
4
+ import { VersionControl } from './version-control.js';
5
+ export default class Chain extends VersionControl {
6
6
  #private;
7
7
  id: any;
8
8
  utils: {};
@@ -10,7 +10,6 @@ export default class Chain extends State {
10
10
  get nativeToken(): string;
11
11
  get validators(): any[];
12
12
  hasTransactionToHandle(): Promise<boolean>;
13
- clearPool(): Promise<void>;
14
13
  participate(address: any): Promise<void>;
15
14
  /**
16
15
  * every tx done is trough contracts so no need for amount
package/exports/chain.js CHANGED
@@ -632,6 +632,18 @@ class State extends Contract {
632
632
  return Promise.all(lastTransactions.map(transaction => transaction.hash()));
633
633
  };
634
634
  }
635
+ async clearPool() {
636
+ await globalThis.transactionPoolStore.clear();
637
+ }
638
+ /**
639
+ * drastic measurement, removes everything!
640
+ */
641
+ async clearAll() {
642
+ await globalThis.accountsStore.clear();
643
+ await globalThis.chainStore.clear();
644
+ await globalThis.blockStore.clear();
645
+ await globalThis.transactionPoolStore.clear();
646
+ }
635
647
  async init() {
636
648
  this.jobber = new Jobber(30000);
637
649
  if (super.init)
@@ -1027,10 +1039,43 @@ class State extends Contract {
1027
1039
  }
1028
1040
  }
1029
1041
 
1042
+ class VersionControl extends State {
1043
+ constructor() {
1044
+ super();
1045
+ }
1046
+ async init() {
1047
+ super.init && await super.init();
1048
+ console.log('init');
1049
+ try {
1050
+ const version = await globalThis.chainStore.get('version');
1051
+ this.version = new TextDecoder().decode(version);
1052
+ console.log(this.version);
1053
+ /**
1054
+ * protocol version control!
1055
+ * note v1 and 1.1 delete everything because of big changes, this is not what we want in the future
1056
+ * in the future we want newer nodes to handle the new changes and still confirm old version transactions
1057
+ * unless there is a security issue!
1058
+ */
1059
+ if (this.version !== '1.1.1') {
1060
+ this.version = '1.1.1';
1061
+ await this.clearAll();
1062
+ await globalThis.chainStore.put('version', this.version);
1063
+ }
1064
+ // if (version)
1065
+ }
1066
+ catch (e) {
1067
+ console.log(e);
1068
+ this.version = '1.1.1';
1069
+ await this.clearAll();
1070
+ await globalThis.chainStore.put('version', this.version);
1071
+ }
1072
+ }
1073
+ }
1074
+
1030
1075
  globalThis.BigNumber = BigNumber;
1031
1076
  const ignorelist = [];
1032
1077
  // check if browser or local
1033
- class Chain extends State {
1078
+ class Chain extends VersionControl {
1034
1079
  #state;
1035
1080
  #slotTime;
1036
1081
  /** {Address[]} */
@@ -1110,46 +1155,7 @@ class Chain extends State {
1110
1155
  console.log('handle native contracts');
1111
1156
  // handle native contracts
1112
1157
  }
1113
- async clearPool() {
1114
- await globalThis.transactionPoolStore.clear();
1115
- }
1116
- /**
1117
- * drastic measurement, removes everything!
1118
- */
1119
- async #clearAll() {
1120
- await globalThis.accountsStore.clear();
1121
- await globalThis.chainStore.clear();
1122
- await globalThis.blockStore.clear();
1123
- await globalThis.transactionPoolStore.clear();
1124
- }
1125
1158
  async #init() {
1126
- try {
1127
- const version = await globalThis.chainStore.get('version');
1128
- this.version = new TextDecoder().decode(version);
1129
- /**
1130
- * protocol version control!
1131
- * note v1 and 1.1 delete everything because of big changes, this is not what we want in the future
1132
- * in the future we want newer nodes to handle the new changes and still confirm old version transactions
1133
- * unless there is a security issue!
1134
- */
1135
- if (this.version !== '1.0.0') {
1136
- this.version = '1.0.0';
1137
- await this.#clearAll();
1138
- await globalThis.chainStore.put('version', this.version);
1139
- }
1140
- else if (this.version !== '1.1.1') {
1141
- this.version = '1.1.1';
1142
- await this.#clearAll();
1143
- await globalThis.chainStore.put('version', this.version);
1144
- }
1145
- // if (version)
1146
- }
1147
- catch (e) {
1148
- console.log(e);
1149
- this.version = '1.0.0';
1150
- await this.#clearAll();
1151
- await globalThis.chainStore.put('version', new TextEncoder().encode(this.version));
1152
- }
1153
1159
  // this.node = await new Node()
1154
1160
  this.#participants = [];
1155
1161
  this.#participating = false;
@@ -1158,6 +1164,8 @@ class Chain extends State {
1158
1164
  await this.#setup();
1159
1165
  this.utils = { BigNumber, formatUnits, parseUnits };
1160
1166
  // this.#state = new State()
1167
+ // todo some functions rely on state
1168
+ await super.init();
1161
1169
  await globalThis.peernet.addRequestHandler('bw-request-message', () => {
1162
1170
  return new BWMessage(globalThis.peernet.client.bw) || { up: 0, down: 0 };
1163
1171
  });
@@ -1173,8 +1181,6 @@ class Chain extends State {
1173
1181
  globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
1174
1182
  globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
1175
1183
  globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
1176
- // todo some functions rely on state
1177
- await super.init();
1178
1184
  globalThis.pubsub.publish('chain:ready', true);
1179
1185
  return this;
1180
1186
  }
@@ -25,6 +25,11 @@ export default class State extends Contract {
25
25
  get totalSize(): number;
26
26
  get machine(): Machine;
27
27
  constructor();
28
+ clearPool(): Promise<void>;
29
+ /**
30
+ * drastic measurement, removes everything!
31
+ */
32
+ clearAll(): Promise<void>;
28
33
  init(): Promise<void>;
29
34
  updateState(message: any): Promise<void>;
30
35
  getLatestBlock(): Promise<BlockMessage['decoded']>;
@@ -0,0 +1,5 @@
1
+ import State from "./state.js";
2
+ export declare class VersionControl extends State {
3
+ constructor();
4
+ init(): Promise<void>;
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.5.23",
3
+ "version": "1.5.24",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": {
@@ -54,6 +54,7 @@
54
54
  "@types/bs58check": "^2.1.0",
55
55
  "@types/pako": "^2.0.0",
56
56
  "@types/randombytes": "^2.0.0",
57
+ "@types/semver": "^7.5.6",
57
58
  "@types/varint": "^6.0.1",
58
59
  "@vandeurenglenn/debug": "^1.0.0",
59
60
  "eslint": "^8.28.0",
@@ -89,6 +90,7 @@
89
90
  "p-queue": "^7.3.4",
90
91
  "p2pt": "^1.5.1",
91
92
  "pako": "^2.0.4",
93
+ "semver": "^7.5.4",
92
94
  "simple-peer": "^9.11.1"
93
95
  },
94
96
  "eslintConfig": {