@leofcoin/chain 1.4.39 → 1.4.41

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.
@@ -8082,6 +8082,7 @@ class Chain extends Contract {
8082
8082
  await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
8083
8083
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
8084
8084
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
8085
+ globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
8085
8086
  globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
8086
8087
  globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
8087
8088
  globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
@@ -8118,6 +8119,10 @@ class Chain extends Contract {
8118
8119
  globalThis.globalThis.pubsub.publish('chain:ready', true);
8119
8120
  return this;
8120
8121
  }
8122
+ async #invalidTransaction(hash) {
8123
+ await transactionPoolStore.delete(hash);
8124
+ console.log(`removed invalid transaction: ${hash}`);
8125
+ }
8121
8126
  async #validatorTimeout(validatorInfo) {
8122
8127
  setTimeout(() => {
8123
8128
  this.#jail.splice(this.#jail.indexOf(validatorInfo.address), 1);
@@ -8179,8 +8184,11 @@ class Chain extends Contract {
8179
8184
  pool = pool.filter(item => !item.has);
8180
8185
  await Promise.all(pool.map(async ({ hash }) => {
8181
8186
  const result = await globalThis.peernet.get(hash);
8182
- await globalThis.peernet.put(hash, result);
8187
+ const node = await new TransactionMessage(result);
8188
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
8183
8189
  }));
8190
+ if (pool.length > 0)
8191
+ this.#runEpoch();
8184
8192
  console.log(pool);
8185
8193
  if (lastBlock)
8186
8194
  this.#syncChain(lastBlock);
@@ -8288,9 +8296,10 @@ class Chain extends Contract {
8288
8296
  return result || 'no state change';
8289
8297
  }
8290
8298
  catch (error) {
8291
- console.log(error);
8299
+ console.log({ error });
8300
+ globalThis.peernet.pubsub.publish('invalid-transaction', hash);
8292
8301
  globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
8293
- throw error;
8302
+ throw { error, hash, from, to, params, nonce };
8294
8303
  }
8295
8304
  }
8296
8305
  async #addBlock(block) {
@@ -8369,6 +8378,7 @@ class Chain extends Contract {
8369
8378
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
8370
8379
  if (Object.keys(transactions)?.length === 0)
8371
8380
  return;
8381
+ const keys = await globalThis.transactionPoolStore.keys();
8372
8382
  let block = {
8373
8383
  transactions: [],
8374
8384
  validators: [],
@@ -8384,15 +8394,20 @@ class Chain extends Contract {
8384
8394
  for (let transaction of transactions) {
8385
8395
  const hash = await transaction.hash();
8386
8396
  try {
8387
- await this.#executeTransaction({ ...transaction.decoded, hash });
8397
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
8398
+ console.log({ result });
8388
8399
  block.transactions.push({ hash, ...transaction.decoded });
8389
8400
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
8390
8401
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
8391
8402
  }
8392
8403
  catch (e) {
8404
+ console.log(keys.includes(hash));
8405
+ console.log({ e });
8406
+ console.log(hash);
8393
8407
  await globalThis.transactionPoolStore.delete(hash);
8394
8408
  }
8395
8409
  }
8410
+ console.log(block.transactions);
8396
8411
  // don't add empty block
8397
8412
  if (block.transactions.length === 0)
8398
8413
  return;
package/exports/chain.js CHANGED
@@ -677,6 +677,7 @@ class Chain extends Contract {
677
677
  await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
678
678
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
679
679
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
680
+ globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
680
681
  globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
681
682
  globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
682
683
  globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
@@ -713,6 +714,10 @@ class Chain extends Contract {
713
714
  globalThis.globalThis.pubsub.publish('chain:ready', true);
714
715
  return this;
715
716
  }
717
+ async #invalidTransaction(hash) {
718
+ await transactionPoolStore.delete(hash);
719
+ console.log(`removed invalid transaction: ${hash}`);
720
+ }
716
721
  async #validatorTimeout(validatorInfo) {
717
722
  setTimeout(() => {
718
723
  this.#jail.splice(this.#jail.indexOf(validatorInfo.address), 1);
@@ -774,8 +779,11 @@ class Chain extends Contract {
774
779
  pool = pool.filter(item => !item.has);
775
780
  await Promise.all(pool.map(async ({ hash }) => {
776
781
  const result = await globalThis.peernet.get(hash);
777
- await globalThis.peernet.put(hash, result);
782
+ const node = await new TransactionMessage(result);
783
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
778
784
  }));
785
+ if (pool.length > 0)
786
+ this.#runEpoch();
779
787
  console.log(pool);
780
788
  if (lastBlock)
781
789
  this.#syncChain(lastBlock);
@@ -883,9 +891,10 @@ class Chain extends Contract {
883
891
  return result || 'no state change';
884
892
  }
885
893
  catch (error) {
886
- console.log(error);
894
+ console.log({ error });
895
+ globalThis.peernet.pubsub.publish('invalid-transaction', hash);
887
896
  globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
888
- throw error;
897
+ throw { error, hash, from, to, params, nonce };
889
898
  }
890
899
  }
891
900
  async #addBlock(block) {
@@ -964,6 +973,7 @@ class Chain extends Contract {
964
973
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
965
974
  if (Object.keys(transactions)?.length === 0)
966
975
  return;
976
+ const keys = await globalThis.transactionPoolStore.keys();
967
977
  let block = {
968
978
  transactions: [],
969
979
  validators: [],
@@ -979,15 +989,20 @@ class Chain extends Contract {
979
989
  for (let transaction of transactions) {
980
990
  const hash = await transaction.hash();
981
991
  try {
982
- await this.#executeTransaction({ ...transaction.decoded, hash });
992
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
993
+ console.log({ result });
983
994
  block.transactions.push({ hash, ...transaction.decoded });
984
995
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
985
996
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
986
997
  }
987
998
  catch (e) {
999
+ console.log(keys.includes(hash));
1000
+ console.log({ e });
1001
+ console.log(hash);
988
1002
  await globalThis.transactionPoolStore.delete(hash);
989
1003
  }
990
1004
  }
1005
+ console.log(block.transactions);
991
1006
  // don't add empty block
992
1007
  if (block.transactions.length === 0)
993
1008
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.39",
3
+ "version": "1.4.41",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",