@leofcoin/chain 1.4.88 → 1.4.90

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.
@@ -2159,10 +2159,10 @@ class State extends Contract {
2159
2159
  if (!this.#lastBlock || Number(this.#lastBlock.index) < Number(lastBlock.index)) {
2160
2160
  // TODO: check if valid
2161
2161
  const localIndex = this.#lastBlock ? this.lastBlock.index : 0;
2162
- const index = lastBlock.index - 1;
2162
+ const index = lastBlock.index;
2163
2163
  await this.resolveBlock(lastBlock.hash);
2164
2164
  console.log('ok');
2165
- let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index - localIndex) : index;
2165
+ let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index + -localIndex) : index;
2166
2166
  globalThis.debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
2167
2167
  const start = (this.#blocks.length - blocksSynced);
2168
2168
  if (this.#machine)
@@ -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))))
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,9 +2233,17 @@ class State extends Contract {
2226
2233
  if (block && !block.loaded) {
2227
2234
  if (block.index === 0)
2228
2235
  this.#loaded = true;
2236
+ block.transactions = await this.#loadBlockTransactions(block.transactions);
2229
2237
  for (const transaction of block.transactions) {
2230
- if (poolTransactionKeys.includes(transaction.hash))
2231
- await globalThis.transactionPoolStore.delete(transaction.hash);
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
2248
  await this.#machine.execute(transaction.to, transaction.method, transaction.params);
2234
2249
  if (transaction.to === nativeToken$2) {
package/exports/chain.js CHANGED
@@ -779,10 +779,10 @@ class State extends Contract {
779
779
  if (!this.#lastBlock || Number(this.#lastBlock.index) < Number(lastBlock.index)) {
780
780
  // TODO: check if valid
781
781
  const localIndex = this.#lastBlock ? this.lastBlock.index : 0;
782
- const index = lastBlock.index - 1;
782
+ const index = lastBlock.index;
783
783
  await this.resolveBlock(lastBlock.hash);
784
784
  console.log('ok');
785
- let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index - localIndex) : index;
785
+ let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index + -localIndex) : index;
786
786
  globalThis.debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
787
787
  const start = (this.#blocks.length - blocksSynced);
788
788
  if (this.#machine)
@@ -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))))
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,9 +853,17 @@ class State extends Contract {
846
853
  if (block && !block.loaded) {
847
854
  if (block.index === 0)
848
855
  this.#loaded = true;
856
+ block.transactions = await this.#loadBlockTransactions(block.transactions);
849
857
  for (const transaction of block.transactions) {
850
- if (poolTransactionKeys.includes(transaction.hash))
851
- await globalThis.transactionPoolStore.delete(transaction.hash);
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
868
  await this.#machine.execute(transaction.to, transaction.method, transaction.params);
854
869
  if (transaction.to === nativeToken) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.88",
3
+ "version": "1.4.90",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",