@leofcoin/chain 1.4.44 → 1.4.45

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.
@@ -7861,6 +7861,8 @@ globalThis.BigNumber = BigNumber;
7861
7861
  // check if browser or local
7862
7862
  class Chain extends Contract {
7863
7863
  #state;
7864
+ #lastResolved;
7865
+ #slotTime = 10000;
7864
7866
  id;
7865
7867
  utils;
7866
7868
  /** {Address[]} */
@@ -8137,6 +8139,19 @@ class Chain extends Contract {
8137
8139
  return 'synced';
8138
8140
  }
8139
8141
  async #syncChain(lastBlock) {
8142
+ let current;
8143
+ const timeout = () => current = setTimeout(() => {
8144
+ if (this.#chainSyncing) {
8145
+ if (this.#lastResolved + 10000 > Date.now())
8146
+ timeout();
8147
+ else {
8148
+ this.#chainSyncing = false;
8149
+ console.log('resyncing');
8150
+ this.#syncChain(lastBlock);
8151
+ }
8152
+ }
8153
+ }, 10000);
8154
+ timeout();
8140
8155
  if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
8141
8156
  return;
8142
8157
  this.#chainSyncing = true;
@@ -8161,6 +8176,7 @@ class Chain extends Contract {
8161
8176
  await this.#loadBlocks(this.blocks.slice(start));
8162
8177
  await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
8163
8178
  }
8179
+ clearTimeout(current);
8164
8180
  this.#chainSyncing = false;
8165
8181
  }
8166
8182
  async #prepareRequest(request) {
@@ -8236,6 +8252,7 @@ class Chain extends Contract {
8236
8252
  this.#blocks[index] = { hash, ...block.decoded };
8237
8253
  this.#blockHashMap.set(hash, index);
8238
8254
  console.log(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
8255
+ this.#lastResolved = Date.now();
8239
8256
  if (previousHash !== '0x0') {
8240
8257
  return this.resolveBlock(previousHash);
8241
8258
  }
@@ -8382,11 +8399,12 @@ class Chain extends Contract {
8382
8399
  if (Object.keys(transactions)?.length === 0)
8383
8400
  return;
8384
8401
  const keys = await globalThis.transactionPoolStore.keys();
8402
+ const timestamp = Date.now();
8385
8403
  let block = {
8386
8404
  transactions: [],
8387
8405
  validators: [],
8388
8406
  fees: BigNumber.from(0),
8389
- timestamp: Date.now(),
8407
+ timestamp,
8390
8408
  previousHash: '',
8391
8409
  reward: parseUnits('150'),
8392
8410
  index: 0
@@ -8402,18 +8420,20 @@ class Chain extends Contract {
8402
8420
  await globalThis.peernet.publish('invalid-transaction', hash);
8403
8421
  }
8404
8422
  else {
8405
- try {
8406
- const result = await this.#executeTransaction({ ...transaction.decoded, hash });
8407
- console.log({ result });
8408
- block.transactions.push({ hash, ...transaction.decoded });
8409
- block.fees = block.fees.add(await calculateFee(transaction.decoded));
8410
- await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
8411
- }
8412
- catch (e) {
8413
- console.log(keys.includes(hash));
8414
- console.log({ e });
8415
- console.log(hash);
8416
- await globalThis.transactionPoolStore.delete(hash);
8423
+ if (timestamp + this.#slotTime > Date.now()) {
8424
+ try {
8425
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
8426
+ console.log({ result });
8427
+ block.transactions.push({ hash, ...transaction.decoded });
8428
+ block.fees = block.fees.add(await calculateFee(transaction.decoded));
8429
+ await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
8430
+ }
8431
+ catch (e) {
8432
+ console.log(keys.includes(hash));
8433
+ console.log({ e });
8434
+ console.log(hash);
8435
+ await globalThis.transactionPoolStore.delete(hash);
8436
+ }
8417
8437
  }
8418
8438
  }
8419
8439
  }
@@ -8496,17 +8516,21 @@ class Chain extends Contract {
8496
8516
  // transactionStore.put(message.hash, message.encoded)
8497
8517
  }
8498
8518
  async #addTransaction(transaction) {
8519
+ transaction = await new TransactionMessage(transaction);
8520
+ const hash = await transaction.hash();
8499
8521
  try {
8500
- transaction = await new TransactionMessage(transaction);
8501
- const hash = await transaction.hash();
8502
8522
  const has = await globalThis.transactionPoolStore.has(hash);
8503
- if (!has)
8523
+ if (!has) {
8504
8524
  await globalThis.transactionPoolStore.put(hash, transaction.encoded);
8505
- if (this.#participating && !this.#runningEpoch)
8506
- this.#runEpoch();
8525
+ if (this.#participating && !this.#runningEpoch)
8526
+ this.#runEpoch();
8527
+ }
8528
+ else
8529
+ globalThis.peernet.publish('invalid-transaction', hash);
8507
8530
  }
8508
8531
  catch (e) {
8509
8532
  console.log(e);
8533
+ globalThis.peernet.publish('invalid-transaction', hash);
8510
8534
  throw new Error('invalid transaction');
8511
8535
  }
8512
8536
  }
@@ -1,4 +1,4 @@
1
- import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-cf761de2.js';
1
+ import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-aa07a41d.js';
2
2
  import './contract-f76383c3.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-cf761de2.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-aa07a41d.js';
2
2
  import './contract-f76383c3.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './node-browser-cf761de2.js';
1
+ import { F as FormatInterface } from './node-browser-aa07a41d.js';
2
2
  import './contract-f76383c3.js';
3
3
 
4
4
  var proto$b = {
@@ -4868,12 +4868,13 @@ const isUint8Array$1 = (type) => type === 'uint8Array';
4868
4868
  const isBigNumber = (type) => type === 'bigNumber';
4869
4869
  const tokenize = (key, value) => {
4870
4870
  const optional = key.endsWith('?');
4871
- let type = value;
4872
- type = Array.isArray(type) ? 'array' : typeof type;
4873
- if (value instanceof Uint8Array)
4871
+ let type = value === undefined ? key : value;
4872
+ if (type instanceof Uint8Array)
4874
4873
  type = 'uint8Array';
4875
- else if (value._isBigNumber)
4874
+ else if (type?._isBigNumber || type.isBigNumber)
4876
4875
  type = 'bigNumber';
4876
+ else
4877
+ type = Array.isArray(type) ? 'array' : typeof type;
4877
4878
  const parts = key.split('?');
4878
4879
  const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
4879
4880
  return { type, optional, key: parts[0], minimumLength };
@@ -4885,6 +4886,7 @@ const toType = (data) => {
4885
4886
  // returns the ArrayBuffer as a UintArray
4886
4887
  if (data instanceof ArrayBuffer)
4887
4888
  return new Uint8Array(data);
4889
+ // returns the bigNumbers hex as a UintArray
4888
4890
  if (data._isBigNumber)
4889
4891
  return new TextEncoder().encode(data._hex || data.toHexString());
4890
4892
  // returns the string as a UintArray
@@ -4898,11 +4900,11 @@ const toType = (data) => {
4898
4900
  return new TextEncoder().encode(data.toString());
4899
4901
  throw new Error(`unsuported type ${typeof data || data}`);
4900
4902
  };
4901
- const encode$3 = (proto, input) => {
4903
+ const encode$3 = (proto, input, compress) => {
4902
4904
  const keys = Object.keys(proto);
4903
4905
  const values = Object.values(proto);
4904
4906
  const set = [];
4905
- for (let i = 0; i < keys.length; i++) {
4907
+ for (let i = 0; i < values.length; i++) {
4906
4908
  const token = tokenize(keys[i], values[i]);
4907
4909
  const data = input[token.key];
4908
4910
  if (!token.optional && data === undefined)
@@ -4915,14 +4917,14 @@ const encode$3 = (proto, input) => {
4915
4917
  }
4916
4918
  return index$6(set);
4917
4919
  };
4918
- const decode$4 = (proto, uint8Array) => {
4920
+ const decode$4 = (proto, uint8Array, compressed) => {
4919
4921
  let deconcated = index$5(uint8Array);
4920
4922
  const output = {};
4921
4923
  const keys = Object.keys(proto);
4922
4924
  const values = Object.values(proto);
4923
4925
  if (keys.length !== deconcated.length)
4924
4926
  console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
4925
- for (let i = 0; i < keys.length; i++) {
4927
+ for (let i = 0; i < values.length; i++) {
4926
4928
  const token = tokenize(keys[i], values[i]);
4927
4929
  if (isUint8Array$1(token.type))
4928
4930
  output[token.key] = deconcated[i];
@@ -6009,7 +6011,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
6009
6011
  this.fromUint8Array(buffer);
6010
6012
  else if (buffer instanceof ArrayBuffer)
6011
6013
  this.fromArrayBuffer(buffer);
6012
- else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
6014
+ else if (buffer instanceof FormatInterface && buffer?.name === this.name)
6013
6015
  return buffer;
6014
6016
  else if (typeof buffer === 'string') {
6015
6017
  if (this.isHex(buffer))
@@ -20263,7 +20265,7 @@ class Identity {
20263
20265
  globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
20264
20266
  }
20265
20267
  else {
20266
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-2c7d7136-ba66b864.js');
20268
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-2c2a9d47-00b5d0df.js');
20267
20269
  const { identity, accounts } = await importee.default(password, this.network);
20268
20270
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
20269
20271
  await globalThis.walletStore.put('version', String(1));
@@ -20434,7 +20436,7 @@ class Peernet {
20434
20436
  this.root = options.root;
20435
20437
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
20436
20438
  // FolderMessageResponse
20437
- } = await import(/* webpackChunkName: "messages" */ './messages-bcb7873b-22fd727d.js');
20439
+ } = await import(/* webpackChunkName: "messages" */ './messages-6cc87ed7-c16a1408.js');
20438
20440
  /**
20439
20441
  * proto Object containing protos
20440
20442
  * @type {Object}
@@ -20506,7 +20508,7 @@ class Peernet {
20506
20508
  if (this.#starting || this.#started)
20507
20509
  return;
20508
20510
  this.#starting = true;
20509
- const importee = await import('./client-91364a04-457da758.js');
20511
+ const importee = await import('./client-e3216b03-fd2d3b03.js');
20510
20512
  /**
20511
20513
  * @access public
20512
20514
  * @type {PeernetClient}
@@ -1,2 +1,2 @@
1
- export { N as default } from './node-browser-cf761de2.js';
1
+ export { N as default } from './node-browser-aa07a41d.js';
2
2
  import './contract-f76383c3.js';
package/exports/chain.js CHANGED
@@ -456,6 +456,8 @@ globalThis.BigNumber = BigNumber;
456
456
  // check if browser or local
457
457
  class Chain extends Contract {
458
458
  #state;
459
+ #lastResolved;
460
+ #slotTime = 10000;
459
461
  id;
460
462
  utils;
461
463
  /** {Address[]} */
@@ -732,6 +734,19 @@ class Chain extends Contract {
732
734
  return 'synced';
733
735
  }
734
736
  async #syncChain(lastBlock) {
737
+ let current;
738
+ const timeout = () => current = setTimeout(() => {
739
+ if (this.#chainSyncing) {
740
+ if (this.#lastResolved + 10000 > Date.now())
741
+ timeout();
742
+ else {
743
+ this.#chainSyncing = false;
744
+ console.log('resyncing');
745
+ this.#syncChain(lastBlock);
746
+ }
747
+ }
748
+ }, 10000);
749
+ timeout();
735
750
  if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
736
751
  return;
737
752
  this.#chainSyncing = true;
@@ -756,6 +771,7 @@ class Chain extends Contract {
756
771
  await this.#loadBlocks(this.blocks.slice(start));
757
772
  await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
758
773
  }
774
+ clearTimeout(current);
759
775
  this.#chainSyncing = false;
760
776
  }
761
777
  async #prepareRequest(request) {
@@ -831,6 +847,7 @@ class Chain extends Contract {
831
847
  this.#blocks[index] = { hash, ...block.decoded };
832
848
  this.#blockHashMap.set(hash, index);
833
849
  console.log(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
850
+ this.#lastResolved = Date.now();
834
851
  if (previousHash !== '0x0') {
835
852
  return this.resolveBlock(previousHash);
836
853
  }
@@ -977,11 +994,12 @@ class Chain extends Contract {
977
994
  if (Object.keys(transactions)?.length === 0)
978
995
  return;
979
996
  const keys = await globalThis.transactionPoolStore.keys();
997
+ const timestamp = Date.now();
980
998
  let block = {
981
999
  transactions: [],
982
1000
  validators: [],
983
1001
  fees: BigNumber.from(0),
984
- timestamp: Date.now(),
1002
+ timestamp,
985
1003
  previousHash: '',
986
1004
  reward: parseUnits('150'),
987
1005
  index: 0
@@ -997,18 +1015,20 @@ class Chain extends Contract {
997
1015
  await globalThis.peernet.publish('invalid-transaction', hash);
998
1016
  }
999
1017
  else {
1000
- try {
1001
- const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1002
- console.log({ result });
1003
- block.transactions.push({ hash, ...transaction.decoded });
1004
- block.fees = block.fees.add(await calculateFee(transaction.decoded));
1005
- await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1006
- }
1007
- catch (e) {
1008
- console.log(keys.includes(hash));
1009
- console.log({ e });
1010
- console.log(hash);
1011
- await globalThis.transactionPoolStore.delete(hash);
1018
+ if (timestamp + this.#slotTime > Date.now()) {
1019
+ try {
1020
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1021
+ console.log({ result });
1022
+ block.transactions.push({ hash, ...transaction.decoded });
1023
+ block.fees = block.fees.add(await calculateFee(transaction.decoded));
1024
+ await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1025
+ }
1026
+ catch (e) {
1027
+ console.log(keys.includes(hash));
1028
+ console.log({ e });
1029
+ console.log(hash);
1030
+ await globalThis.transactionPoolStore.delete(hash);
1031
+ }
1012
1032
  }
1013
1033
  }
1014
1034
  }
@@ -1091,17 +1111,21 @@ class Chain extends Contract {
1091
1111
  // transactionStore.put(message.hash, message.encoded)
1092
1112
  }
1093
1113
  async #addTransaction(transaction) {
1114
+ transaction = await new TransactionMessage(transaction);
1115
+ const hash = await transaction.hash();
1094
1116
  try {
1095
- transaction = await new TransactionMessage(transaction);
1096
- const hash = await transaction.hash();
1097
1117
  const has = await globalThis.transactionPoolStore.has(hash);
1098
- if (!has)
1118
+ if (!has) {
1099
1119
  await globalThis.transactionPoolStore.put(hash, transaction.encoded);
1100
- if (this.#participating && !this.#runningEpoch)
1101
- this.#runEpoch();
1120
+ if (this.#participating && !this.#runningEpoch)
1121
+ this.#runEpoch();
1122
+ }
1123
+ else
1124
+ globalThis.peernet.publish('invalid-transaction', hash);
1102
1125
  }
1103
1126
  catch (e) {
1104
1127
  console.log(e);
1128
+ globalThis.peernet.publish('invalid-transaction', hash);
1105
1129
  throw new Error('invalid transaction');
1106
1130
  }
1107
1131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.44",
3
+ "version": "1.4.45",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",