@leofcoin/chain 1.4.89 → 1.4.91

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.
@@ -2216,6 +2216,13 @@ class State extends Contract {
2216
2216
  }
2217
2217
  return latest;
2218
2218
  }
2219
+ #loadBlockTransactions = (transactions) => Promise.all(transactions.map((transaction) => new TransactionMessage(transaction)));
2220
+ #getLastTransactions = async () => {
2221
+ let lastTransactions = (await Promise.all(this.#blocks.filter(block => block.loaded).slice(-128)
2222
+ .map(block => this.#loadBlockTransactions(block.transactions))))
2223
+ .reduce((all, transactions) => [...all, ...transactions], []);
2224
+ return Promise.all(lastTransactions.map(transaction => transaction.hash()));
2225
+ };
2219
2226
  /**
2220
2227
  *
2221
2228
  * @param {Block[]} blocks
@@ -2226,24 +2233,33 @@ class State extends Contract {
2226
2233
  if (block && !block.loaded) {
2227
2234
  if (block.index === 0)
2228
2235
  this.#loaded = true;
2229
- for (const transaction of block.transactions) {
2230
- if (poolTransactionKeys.includes(transaction.hash))
2231
- await globalThis.transactionPoolStore.delete(transaction.hash);
2236
+ const transactions = await this.#loadBlockTransactions([...block.transactions] || []);
2237
+ for (const transaction of transactions) {
2238
+ const lastTransactions = await this.#getLastTransactions();
2239
+ const hash = await transaction.hash();
2240
+ if (poolTransactionKeys.includes(hash))
2241
+ await globalThis.transactionPoolStore.delete(hash);
2242
+ if (lastTransactions.includes(hash)) {
2243
+ console.log('removing invalid block');
2244
+ await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
2245
+ return this.#loadBlocks(blocks);
2246
+ }
2232
2247
  try {
2233
- await this.#machine.execute(transaction.to, transaction.method, transaction.params);
2234
- if (transaction.to === nativeToken$2) {
2248
+ await this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params);
2249
+ if (transaction.decoded.to === nativeToken$2) {
2235
2250
  this.#nativeCalls += 1;
2236
- if (transaction.method === 'burn')
2251
+ if (transaction.decoded.method === 'burn')
2237
2252
  this.#nativeBurns += 1;
2238
- if (transaction.method === 'mint')
2253
+ if (transaction.decoded.method === 'mint')
2239
2254
  this.#nativeMints += 1;
2240
- if (transaction.method === 'transfer')
2255
+ if (transaction.decoded.method === 'transfer')
2241
2256
  this.#nativeTransfers += 1;
2242
2257
  }
2243
2258
  this.#totalTransactions += 1;
2244
2259
  }
2245
2260
  catch (error) {
2246
- await globalThis.transactionPoolStore.delete(transaction.hash ? transaction.hash : await (new TransactionMessage(transaction)).hash());
2261
+ console.log(error);
2262
+ await globalThis.transactionPoolStore.delete(hash);
2247
2263
  console.log('removing invalid transaction');
2248
2264
  console.log(error);
2249
2265
  return false;
@@ -2267,7 +2283,6 @@ class State extends Contract {
2267
2283
  clearTimeout(timeout);
2268
2284
  if (promises.length > 0) {
2269
2285
  promises = promises.map(async ({ value }) => {
2270
- console.log(value);
2271
2286
  const node = await new globalThis.peernet.protos['peernet-response'](value.result);
2272
2287
  return { value: node.decoded.response, peer: value.peer };
2273
2288
  });
@@ -2670,7 +2685,6 @@ class Chain extends State {
2670
2685
  // block.reward = block.reward.toString()
2671
2686
  // block.fees = block.fees.toString()
2672
2687
  try {
2673
- console.log(block.transactions);
2674
2688
  block.transactions = await Promise.all(block.transactions
2675
2689
  .map(async (transaction) => {
2676
2690
  await globalThis.transactionPoolStore.delete(await transaction.hash());
package/exports/chain.js CHANGED
@@ -836,6 +836,13 @@ class State extends Contract {
836
836
  }
837
837
  return latest;
838
838
  }
839
+ #loadBlockTransactions = (transactions) => Promise.all(transactions.map((transaction) => new TransactionMessage(transaction)));
840
+ #getLastTransactions = async () => {
841
+ let lastTransactions = (await Promise.all(this.#blocks.filter(block => block.loaded).slice(-128)
842
+ .map(block => this.#loadBlockTransactions(block.transactions))))
843
+ .reduce((all, transactions) => [...all, ...transactions], []);
844
+ return Promise.all(lastTransactions.map(transaction => transaction.hash()));
845
+ };
839
846
  /**
840
847
  *
841
848
  * @param {Block[]} blocks
@@ -846,24 +853,33 @@ class State extends Contract {
846
853
  if (block && !block.loaded) {
847
854
  if (block.index === 0)
848
855
  this.#loaded = true;
849
- for (const transaction of block.transactions) {
850
- if (poolTransactionKeys.includes(transaction.hash))
851
- await globalThis.transactionPoolStore.delete(transaction.hash);
856
+ const transactions = await this.#loadBlockTransactions([...block.transactions] || []);
857
+ for (const transaction of transactions) {
858
+ const lastTransactions = await this.#getLastTransactions();
859
+ const hash = await transaction.hash();
860
+ if (poolTransactionKeys.includes(hash))
861
+ await globalThis.transactionPoolStore.delete(hash);
862
+ if (lastTransactions.includes(hash)) {
863
+ console.log('removing invalid block');
864
+ await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
865
+ return this.#loadBlocks(blocks);
866
+ }
852
867
  try {
853
- await this.#machine.execute(transaction.to, transaction.method, transaction.params);
854
- if (transaction.to === nativeToken) {
868
+ await this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params);
869
+ if (transaction.decoded.to === nativeToken) {
855
870
  this.#nativeCalls += 1;
856
- if (transaction.method === 'burn')
871
+ if (transaction.decoded.method === 'burn')
857
872
  this.#nativeBurns += 1;
858
- if (transaction.method === 'mint')
873
+ if (transaction.decoded.method === 'mint')
859
874
  this.#nativeMints += 1;
860
- if (transaction.method === 'transfer')
875
+ if (transaction.decoded.method === 'transfer')
861
876
  this.#nativeTransfers += 1;
862
877
  }
863
878
  this.#totalTransactions += 1;
864
879
  }
865
880
  catch (error) {
866
- await globalThis.transactionPoolStore.delete(transaction.hash ? transaction.hash : await (new TransactionMessage(transaction)).hash());
881
+ console.log(error);
882
+ await globalThis.transactionPoolStore.delete(hash);
867
883
  console.log('removing invalid transaction');
868
884
  console.log(error);
869
885
  return false;
@@ -887,7 +903,6 @@ class State extends Contract {
887
903
  clearTimeout(timeout);
888
904
  if (promises.length > 0) {
889
905
  promises = promises.map(async ({ value }) => {
890
- console.log(value);
891
906
  const node = await new globalThis.peernet.protos['peernet-response'](value.result);
892
907
  return { value: node.decoded.response, peer: value.peer };
893
908
  });
@@ -1290,7 +1305,6 @@ class Chain extends State {
1290
1305
  // block.reward = block.reward.toString()
1291
1306
  // block.fees = block.fees.toString()
1292
1307
  try {
1293
- console.log(block.transactions);
1294
1308
  block.transactions = await Promise.all(block.transactions
1295
1309
  .map(async (transaction) => {
1296
1310
  await globalThis.transactionPoolStore.delete(await transaction.hash());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.89",
3
+ "version": "1.4.91",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",