@leofcoin/chain 1.4.49 → 1.4.51

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,4 +1,4 @@
1
- import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, c as CodecHash, d as BlockMessage, e as BWMessage, f as BWRequestMessage } from './contract-f76383c3.js';
1
+ import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, R as RawTransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-640d9f36.js';
2
2
 
3
3
  const logger$1 = new Logger(version$1);
4
4
  const _constructorGuard = {};
@@ -729,7 +729,16 @@ const calculateFee = async (transaction, format = false) => {
729
729
  // fee = fee.div(1000000)
730
730
  return format ? formatUnits(fee.toString()) : fee;
731
731
  };
732
- const createTransactionHash = async (transaction) => (await new CodecHash(transaction, { name: 'transaction-message' })).digest;
732
+ const createTransactionHash = async (transaction) => {
733
+ const isRawTransactionMessage = transaction instanceof RawTransactionMessage;
734
+ if (!isRawTransactionMessage) {
735
+ if (transaction.decoded && transaction instanceof TransactionMessage)
736
+ transaction = await new RawTransactionMessage(transaction.decoded);
737
+ else
738
+ transaction = await new RawTransactionMessage(transaction);
739
+ }
740
+ return (await transaction.peernetHash).digest;
741
+ };
733
742
  const signTransaction = async (transaction, wallet) => {
734
743
  const signature = await wallet.sign(await createTransactionHash(transaction));
735
744
  const signedTransaction = { ...transaction, signature };
@@ -7639,7 +7648,7 @@ class State {
7639
7648
 
7640
7649
  class Protocol {
7641
7650
  limit = 1800;
7642
- transactionLimit = 1800;
7651
+ transactionLimit = 10_000;
7643
7652
  }
7644
7653
 
7645
7654
  class Transaction extends Protocol {
@@ -7801,7 +7810,7 @@ class Transaction extends Protocol {
7801
7810
  await globalThis.transactionPoolStore.put(hash, message.encoded);
7802
7811
  // debug(`Added ${hash} to the transaction pool`)
7803
7812
  peernet.publish('add-transaction', message.encoded);
7804
- return { hash: hash, data, fee: await calculateFee(message.decoded), wait, message };
7813
+ return { hash, data, fee: await calculateFee(message.decoded), wait, message };
7805
7814
  }
7806
7815
  catch (error) {
7807
7816
  throw error;
@@ -8066,8 +8075,8 @@ class Chain extends Contract {
8066
8075
  async #init() {
8067
8076
  try {
8068
8077
  const version = await globalThis.chainStore.get('version');
8069
- this.version = version;
8070
- if (version !== '1.0.0') {
8078
+ this.version = new TextDecoder().decode(version);
8079
+ if (this.version !== '1.0.0') {
8071
8080
  this.version = '1.0.0';
8072
8081
  await globalThis.chainStore.clear();
8073
8082
  await globalThis.blockStore.clear();
@@ -8076,14 +8085,14 @@ class Chain extends Contract {
8076
8085
  }
8077
8086
  // if (version)
8078
8087
  }
8079
- catch {
8088
+ catch (e) {
8089
+ console.log(e);
8080
8090
  this.version = '1.0.0';
8081
8091
  await globalThis.chainStore.clear();
8082
8092
  await globalThis.blockStore.clear();
8083
8093
  await globalThis.transactionPoolStore.clear();
8084
- await globalThis.chainStore.put('version', this.version);
8094
+ await globalThis.chainStore.put('version', new TextEncoder().encode(this.version));
8085
8095
  }
8086
- await this.#clearPool();
8087
8096
  // this.node = await new Node()
8088
8097
  this.#participants = [];
8089
8098
  this.#participating = false;
@@ -8105,7 +8114,7 @@ class Chain extends Contract {
8105
8114
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
8106
8115
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
8107
8116
  globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
8108
- globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
8117
+ globalThis.peernet.subscribe('send-transaction', this.#sendTransaction.bind(this));
8109
8118
  globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
8110
8119
  globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
8111
8120
  // todo some functions rely on state
@@ -8210,26 +8219,33 @@ class Chain extends Contract {
8210
8219
  return response.decoded.response;
8211
8220
  }
8212
8221
  async #peerConnected(peer) {
8222
+ console.log(peer);
8213
8223
  if (!peer.version || peer.version !== this.version)
8214
8224
  return;
8215
8225
  const lastBlock = await this.#makeRequest(peer, 'lastBlock');
8216
- this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
8217
- let pool = await this.#makeRequest(peer, 'transactionPool');
8218
- pool = await Promise.all(pool.map(async (hash) => {
8219
- const has = await globalThis.peernet.has(hash);
8220
- return { has, hash };
8221
- }));
8222
- pool = pool.filter(item => !item.has);
8223
- await Promise.all(pool.map(async ({ hash }) => {
8224
- const result = await globalThis.peernet.get(hash);
8225
- const node = await new TransactionMessage(result);
8226
- await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
8227
- }));
8228
- if (pool.length > 0)
8229
- this.#runEpoch();
8230
- console.log(pool);
8231
- if (lastBlock)
8232
- this.#syncChain(lastBlock);
8226
+ if (!this.#lastBlock || lastBlock && lastBlock.index > this.#lastBlock.index && lastBlock.hash === this.#lastBlock?.hash) {
8227
+ this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
8228
+ let pool = await this.#makeRequest(peer, 'transactionPool');
8229
+ pool = await Promise.all(pool.map(async (hash) => {
8230
+ const has = await globalThis.peernet.has(hash);
8231
+ return { has, hash };
8232
+ }));
8233
+ pool = pool.filter(item => !item.has);
8234
+ await Promise.all(pool.map(async ({ hash }) => {
8235
+ const result = await globalThis.peernet.get(hash);
8236
+ // result could be undefined cause invalid/double transactions could be deleted already
8237
+ if (!result)
8238
+ console.log(result);
8239
+ if (result) {
8240
+ const node = await new TransactionMessage(result);
8241
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
8242
+ }
8243
+ }));
8244
+ if (lastBlock)
8245
+ await this.#syncChain(lastBlock);
8246
+ if (await this.hasTransactionToHandle())
8247
+ this.#runEpoch();
8248
+ }
8233
8249
  }
8234
8250
  #epochTimeout;
8235
8251
  async #transactionPoolHandler() {
@@ -8423,7 +8439,7 @@ class Chain extends Contract {
8423
8439
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
8424
8440
  if (Object.keys(transactions)?.length === 0)
8425
8441
  return;
8426
- const keys = await globalThis.transactionPoolStore.keys();
8442
+ await globalThis.transactionPoolStore.keys();
8427
8443
  const timestamp = Date.now();
8428
8444
  let block = {
8429
8445
  transactions: [],
@@ -8447,35 +8463,30 @@ class Chain extends Contract {
8447
8463
  }
8448
8464
  }
8449
8465
  }
8450
- console.log();
8451
8466
  if (doubleTransactions.length > 0) {
8452
8467
  await globalThis.transactionPoolStore.delete(hash);
8453
8468
  await globalThis.peernet.publish('invalid-transaction', hash);
8454
8469
  }
8455
8470
  else {
8456
- if (timestamp + this.#slotTime > Date.now()) {
8457
- try {
8458
- const result = await this.#executeTransaction({ ...transaction.decoded, hash });
8459
- console.log({ result });
8460
- block.transactions.push({ hash, ...transaction.decoded });
8461
- block.fees = block.fees.add(await calculateFee(transaction.decoded));
8462
- await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
8463
- }
8464
- catch (e) {
8465
- console.log(keys.includes(hash));
8466
- console.log({ e });
8467
- console.log(hash);
8468
- await globalThis.transactionPoolStore.delete(hash);
8469
- }
8471
+ // if (timestamp + this.#slotTime > Date.now()) {
8472
+ try {
8473
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
8474
+ block.transactions.push(transaction);
8475
+ block.fees = block.fees.add(await calculateFee(transaction.decoded));
8476
+ await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
8477
+ }
8478
+ catch (e) {
8479
+ console.log({ e });
8480
+ console.log(hash);
8481
+ await globalThis.transactionPoolStore.delete(hash);
8470
8482
  }
8483
+ // }
8471
8484
  }
8472
8485
  }
8473
- console.log(block.transactions);
8474
8486
  // don't add empty block
8475
8487
  if (block.transactions.length === 0)
8476
8488
  return;
8477
8489
  const validators = await this.staticCall(addresses.validators, 'validators');
8478
- console.log({ validators });
8479
8490
  // block.validators = Object.keys(block.validators).reduce((set, key) => {
8480
8491
  // if (block.validators[key].active) {
8481
8492
  // push({
@@ -8495,7 +8506,6 @@ class Chain extends Contract {
8495
8506
  const node = await globalThis.peernet.prepareMessage(validator, data.encoded);
8496
8507
  try {
8497
8508
  const bw = await peer.request(node.encoded);
8498
- console.log({ bw });
8499
8509
  block.validators.push({
8500
8510
  address: validator,
8501
8511
  bw: bw.up + bw.down
@@ -8511,16 +8521,13 @@ class Chain extends Contract {
8511
8521
  }
8512
8522
  }
8513
8523
  }
8514
- console.log({ validators: block.validators });
8515
8524
  block.validators = block.validators.map(validator => {
8516
8525
  validator.reward = block.fees;
8517
8526
  validator.reward = validator.reward.add(block.reward);
8518
8527
  validator.reward = validator.reward.div(block.validators.length);
8519
- validator.reward = validator.reward.toString();
8520
8528
  delete validator.bw;
8521
8529
  return validator;
8522
8530
  });
8523
- console.log({ validators: block.validators });
8524
8531
  // block.validators = calculateValidatorReward(block.validators, block.fees)
8525
8532
  block.index = this.lastBlock?.index;
8526
8533
  if (block.index === undefined)
@@ -8528,12 +8535,15 @@ class Chain extends Contract {
8528
8535
  else
8529
8536
  block.index += 1;
8530
8537
  block.previousHash = this.lastBlock?.hash || '0x0';
8531
- block.timestamp = Date.now();
8532
- block.reward = block.reward.toString();
8533
- block.fees = block.fees.toString();
8538
+ // block.timestamp = Date.now()
8539
+ // block.reward = block.reward.toString()
8540
+ // block.fees = block.fees.toString()
8534
8541
  try {
8535
- await Promise.all(block.transactions
8536
- .map(async (transaction) => globalThis.transactionPoolStore.delete(transaction.hash)));
8542
+ block.transactions = await Promise.all(block.transactions
8543
+ .map(async (transaction) => {
8544
+ await globalThis.transactionPoolStore.delete(await transaction.hash());
8545
+ return transaction.decoded;
8546
+ }));
8537
8547
  let blockMessage = await new BlockMessage(block);
8538
8548
  const hash = await blockMessage.hash();
8539
8549
  await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
@@ -8543,23 +8553,22 @@ class Chain extends Contract {
8543
8553
  globalThis.pubsub.publish('add-block', blockMessage.decoded);
8544
8554
  }
8545
8555
  catch (error) {
8556
+ console.log(error);
8546
8557
  throw new Error(`invalid block ${block}`);
8547
8558
  }
8548
8559
  // data = await this.#machine.execute(to, method, params)
8549
8560
  // transactionStore.put(message.hash, message.encoded)
8550
8561
  }
8551
- async #addTransaction(transaction) {
8552
- transaction = await new TransactionMessage(transaction);
8562
+ async #sendTransaction(transaction) {
8563
+ transaction = await new TransactionMessage(transaction.encoded);
8553
8564
  const hash = await transaction.hash();
8554
8565
  try {
8555
8566
  const has = await globalThis.transactionPoolStore.has(hash);
8556
8567
  if (!has) {
8557
8568
  await globalThis.transactionPoolStore.put(hash, transaction.encoded);
8558
- if (this.#participating && !this.#runningEpoch)
8559
- this.#runEpoch();
8560
8569
  }
8561
- else
8562
- globalThis.peernet.publish('invalid-transaction', hash);
8570
+ if (this.#participating && !this.#runningEpoch)
8571
+ this.#runEpoch();
8563
8572
  }
8564
8573
  catch (e) {
8565
8574
  console.log(e);
@@ -8574,7 +8583,8 @@ class Chain extends Contract {
8574
8583
  **/
8575
8584
  async sendTransaction(transaction) {
8576
8585
  const event = await super.sendTransaction(transaction);
8577
- this.#addTransaction(event.message.encoded);
8586
+ this.#sendTransaction(await new TransactionMessage(event.message.encoded));
8587
+ globalThis.peernet.publish('send-transaction', event.message.encoded);
8578
8588
  return event;
8579
8589
  }
8580
8590
  async addContract(transaction, contractMessage) {
@@ -1,5 +1,5 @@
1
- import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-8791470c.js';
2
- import './contract-f76383c3.js';
1
+ import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-63d44d8e.js';
2
+ import './index-640d9f36.js';
3
3
 
4
4
  function commonjsRequire(path) {
5
5
  throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
@@ -1,5 +1,5 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-8791470c.js';
2
- import './contract-f76383c3.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-63d44d8e.js';
2
+ import './index-640d9f36.js';
3
3
 
4
4
  /**
5
5
  * @params {String} network