@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.
package/exports/chain.js CHANGED
@@ -234,7 +234,7 @@ class State {
234
234
 
235
235
  class Protocol {
236
236
  limit = 1800;
237
- transactionLimit = 1800;
237
+ transactionLimit = 10_000;
238
238
  }
239
239
 
240
240
  class Transaction extends Protocol {
@@ -396,7 +396,7 @@ class Transaction extends Protocol {
396
396
  await globalThis.transactionPoolStore.put(hash, message.encoded);
397
397
  // debug(`Added ${hash} to the transaction pool`)
398
398
  peernet.publish('add-transaction', message.encoded);
399
- return { hash: hash, data, fee: await calculateFee(message.decoded), wait, message };
399
+ return { hash, data, fee: await calculateFee(message.decoded), wait, message };
400
400
  }
401
401
  catch (error) {
402
402
  throw error;
@@ -661,8 +661,8 @@ class Chain extends Contract {
661
661
  async #init() {
662
662
  try {
663
663
  const version = await globalThis.chainStore.get('version');
664
- this.version = version;
665
- if (version !== '1.0.0') {
664
+ this.version = new TextDecoder().decode(version);
665
+ if (this.version !== '1.0.0') {
666
666
  this.version = '1.0.0';
667
667
  await globalThis.chainStore.clear();
668
668
  await globalThis.blockStore.clear();
@@ -671,14 +671,14 @@ class Chain extends Contract {
671
671
  }
672
672
  // if (version)
673
673
  }
674
- catch {
674
+ catch (e) {
675
+ console.log(e);
675
676
  this.version = '1.0.0';
676
677
  await globalThis.chainStore.clear();
677
678
  await globalThis.blockStore.clear();
678
679
  await globalThis.transactionPoolStore.clear();
679
- await globalThis.chainStore.put('version', this.version);
680
+ await globalThis.chainStore.put('version', new TextEncoder().encode(this.version));
680
681
  }
681
- await this.#clearPool();
682
682
  // this.node = await new Node()
683
683
  this.#participants = [];
684
684
  this.#participating = false;
@@ -700,7 +700,7 @@ class Chain extends Contract {
700
700
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
701
701
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
702
702
  globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
703
- globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
703
+ globalThis.peernet.subscribe('send-transaction', this.#sendTransaction.bind(this));
704
704
  globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
705
705
  globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
706
706
  // todo some functions rely on state
@@ -805,26 +805,33 @@ class Chain extends Contract {
805
805
  return response.decoded.response;
806
806
  }
807
807
  async #peerConnected(peer) {
808
+ console.log(peer);
808
809
  if (!peer.version || peer.version !== this.version)
809
810
  return;
810
811
  const lastBlock = await this.#makeRequest(peer, 'lastBlock');
811
- this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
812
- let pool = await this.#makeRequest(peer, 'transactionPool');
813
- pool = await Promise.all(pool.map(async (hash) => {
814
- const has = await globalThis.peernet.has(hash);
815
- return { has, hash };
816
- }));
817
- pool = pool.filter(item => !item.has);
818
- await Promise.all(pool.map(async ({ hash }) => {
819
- const result = await globalThis.peernet.get(hash);
820
- const node = await new TransactionMessage(result);
821
- await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
822
- }));
823
- if (pool.length > 0)
824
- this.#runEpoch();
825
- console.log(pool);
826
- if (lastBlock)
827
- this.#syncChain(lastBlock);
812
+ if (!this.#lastBlock || lastBlock && lastBlock.index > this.#lastBlock.index && lastBlock.hash === this.#lastBlock?.hash) {
813
+ this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
814
+ let pool = await this.#makeRequest(peer, 'transactionPool');
815
+ pool = await Promise.all(pool.map(async (hash) => {
816
+ const has = await globalThis.peernet.has(hash);
817
+ return { has, hash };
818
+ }));
819
+ pool = pool.filter(item => !item.has);
820
+ await Promise.all(pool.map(async ({ hash }) => {
821
+ const result = await globalThis.peernet.get(hash);
822
+ // result could be undefined cause invalid/double transactions could be deleted already
823
+ if (!result)
824
+ console.log(result);
825
+ if (result) {
826
+ const node = await new TransactionMessage(result);
827
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
828
+ }
829
+ }));
830
+ if (lastBlock)
831
+ await this.#syncChain(lastBlock);
832
+ if (await this.hasTransactionToHandle())
833
+ this.#runEpoch();
834
+ }
828
835
  }
829
836
  #epochTimeout;
830
837
  async #transactionPoolHandler() {
@@ -1018,7 +1025,7 @@ class Chain extends Contract {
1018
1025
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
1019
1026
  if (Object.keys(transactions)?.length === 0)
1020
1027
  return;
1021
- const keys = await globalThis.transactionPoolStore.keys();
1028
+ await globalThis.transactionPoolStore.keys();
1022
1029
  const timestamp = Date.now();
1023
1030
  let block = {
1024
1031
  transactions: [],
@@ -1042,35 +1049,30 @@ class Chain extends Contract {
1042
1049
  }
1043
1050
  }
1044
1051
  }
1045
- console.log();
1046
1052
  if (doubleTransactions.length > 0) {
1047
1053
  await globalThis.transactionPoolStore.delete(hash);
1048
1054
  await globalThis.peernet.publish('invalid-transaction', hash);
1049
1055
  }
1050
1056
  else {
1051
- if (timestamp + this.#slotTime > Date.now()) {
1052
- try {
1053
- const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1054
- console.log({ result });
1055
- block.transactions.push({ hash, ...transaction.decoded });
1056
- block.fees = block.fees.add(await calculateFee(transaction.decoded));
1057
- await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1058
- }
1059
- catch (e) {
1060
- console.log(keys.includes(hash));
1061
- console.log({ e });
1062
- console.log(hash);
1063
- await globalThis.transactionPoolStore.delete(hash);
1064
- }
1057
+ // if (timestamp + this.#slotTime > Date.now()) {
1058
+ try {
1059
+ const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1060
+ block.transactions.push(transaction);
1061
+ block.fees = block.fees.add(await calculateFee(transaction.decoded));
1062
+ await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1065
1063
  }
1064
+ catch (e) {
1065
+ console.log({ e });
1066
+ console.log(hash);
1067
+ await globalThis.transactionPoolStore.delete(hash);
1068
+ }
1069
+ // }
1066
1070
  }
1067
1071
  }
1068
- console.log(block.transactions);
1069
1072
  // don't add empty block
1070
1073
  if (block.transactions.length === 0)
1071
1074
  return;
1072
1075
  const validators = await this.staticCall(addresses.validators, 'validators');
1073
- console.log({ validators });
1074
1076
  // block.validators = Object.keys(block.validators).reduce((set, key) => {
1075
1077
  // if (block.validators[key].active) {
1076
1078
  // push({
@@ -1090,7 +1092,6 @@ class Chain extends Contract {
1090
1092
  const node = await globalThis.peernet.prepareMessage(validator, data.encoded);
1091
1093
  try {
1092
1094
  const bw = await peer.request(node.encoded);
1093
- console.log({ bw });
1094
1095
  block.validators.push({
1095
1096
  address: validator,
1096
1097
  bw: bw.up + bw.down
@@ -1106,16 +1107,13 @@ class Chain extends Contract {
1106
1107
  }
1107
1108
  }
1108
1109
  }
1109
- console.log({ validators: block.validators });
1110
1110
  block.validators = block.validators.map(validator => {
1111
1111
  validator.reward = block.fees;
1112
1112
  validator.reward = validator.reward.add(block.reward);
1113
1113
  validator.reward = validator.reward.div(block.validators.length);
1114
- validator.reward = validator.reward.toString();
1115
1114
  delete validator.bw;
1116
1115
  return validator;
1117
1116
  });
1118
- console.log({ validators: block.validators });
1119
1117
  // block.validators = calculateValidatorReward(block.validators, block.fees)
1120
1118
  block.index = this.lastBlock?.index;
1121
1119
  if (block.index === undefined)
@@ -1123,12 +1121,15 @@ class Chain extends Contract {
1123
1121
  else
1124
1122
  block.index += 1;
1125
1123
  block.previousHash = this.lastBlock?.hash || '0x0';
1126
- block.timestamp = Date.now();
1127
- block.reward = block.reward.toString();
1128
- block.fees = block.fees.toString();
1124
+ // block.timestamp = Date.now()
1125
+ // block.reward = block.reward.toString()
1126
+ // block.fees = block.fees.toString()
1129
1127
  try {
1130
- await Promise.all(block.transactions
1131
- .map(async (transaction) => globalThis.transactionPoolStore.delete(transaction.hash)));
1128
+ block.transactions = await Promise.all(block.transactions
1129
+ .map(async (transaction) => {
1130
+ await globalThis.transactionPoolStore.delete(await transaction.hash());
1131
+ return transaction.decoded;
1132
+ }));
1132
1133
  let blockMessage = await new BlockMessage(block);
1133
1134
  const hash = await blockMessage.hash();
1134
1135
  await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
@@ -1138,23 +1139,22 @@ class Chain extends Contract {
1138
1139
  globalThis.pubsub.publish('add-block', blockMessage.decoded);
1139
1140
  }
1140
1141
  catch (error) {
1142
+ console.log(error);
1141
1143
  throw new Error(`invalid block ${block}`);
1142
1144
  }
1143
1145
  // data = await this.#machine.execute(to, method, params)
1144
1146
  // transactionStore.put(message.hash, message.encoded)
1145
1147
  }
1146
- async #addTransaction(transaction) {
1147
- transaction = await new TransactionMessage(transaction);
1148
+ async #sendTransaction(transaction) {
1149
+ transaction = await new TransactionMessage(transaction.encoded);
1148
1150
  const hash = await transaction.hash();
1149
1151
  try {
1150
1152
  const has = await globalThis.transactionPoolStore.has(hash);
1151
1153
  if (!has) {
1152
1154
  await globalThis.transactionPoolStore.put(hash, transaction.encoded);
1153
- if (this.#participating && !this.#runningEpoch)
1154
- this.#runEpoch();
1155
1155
  }
1156
- else
1157
- globalThis.peernet.publish('invalid-transaction', hash);
1156
+ if (this.#participating && !this.#runningEpoch)
1157
+ this.#runEpoch();
1158
1158
  }
1159
1159
  catch (e) {
1160
1160
  console.log(e);
@@ -1169,7 +1169,8 @@ class Chain extends Contract {
1169
1169
  **/
1170
1170
  async sendTransaction(transaction) {
1171
1171
  const event = await super.sendTransaction(transaction);
1172
- this.#addTransaction(event.message.encoded);
1172
+ this.#sendTransaction(await new TransactionMessage(event.message.encoded));
1173
+ globalThis.peernet.publish('send-transaction', event.message.encoded);
1173
1174
  return event;
1174
1175
  }
1175
1176
  async addContract(transaction, contractMessage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.49",
3
+ "version": "1.4.51",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",
@@ -53,9 +53,9 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "@leofcoin/addresses": "^1.0.4",
56
- "@leofcoin/codec-format-interface": "^1.6.4",
56
+ "@leofcoin/codec-format-interface": "^1.6.14",
57
57
  "@leofcoin/lib": "^1.0.11",
58
- "@leofcoin/messages": "^1.2.0",
58
+ "@leofcoin/messages": "^1.3.7",
59
59
  "@leofcoin/multi-wallet": "^2.1.1",
60
60
  "@leofcoin/networks": "^1.0.0",
61
61
  "@leofcoin/peernet": "^1.1.12",