From 3ef48c408fea44609b58967a8d09ddbef7172073 Mon Sep 17 00:00:00 2001 From: ejMina226 <118474890+ejMina226@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:10:23 +0100 Subject: [PATCH] Code review changes --- .../src/mempool/private/PrivateMempool.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/sequencer/src/mempool/private/PrivateMempool.ts b/packages/sequencer/src/mempool/private/PrivateMempool.ts index d80e8e12..bc9179de 100644 --- a/packages/sequencer/src/mempool/private/PrivateMempool.ts +++ b/packages/sequencer/src/mempool/private/PrivateMempool.ts @@ -26,6 +26,7 @@ import { } from "../../sequencer/executor/Sequencer"; import { CachedStateService } from "../../state/state/CachedStateService"; import { AsyncStateService } from "../../state/async/AsyncStateService"; +import { distinctByPredicate } from "../../helpers/utils"; type MempoolTransactionPaths = { transaction: PendingTransaction; @@ -94,8 +95,6 @@ export class PrivateMempool extends SequencerModule implements Mempool { public async getTxs(): Promise { const txs = await this.transactionStorage.getPendingUserTransactions(); - const sortedTxs: PendingTransaction[] = []; - const skippedTxs: Record = {}; const executionContext = container.resolve( RuntimeMethodExecutionContext ); @@ -104,10 +103,8 @@ export class PrivateMempool extends SequencerModule implements Mempool { const networkState = (await this.getStagedNetworkState()) ?? NetworkState.empty(); - await this.checkTxValid( + const sortedTxs = await this.checkTxValid( txs, - sortedTxs, - skippedTxs, baseCachedStateService, this.protocol.stateServiceProvider, networkState, @@ -128,14 +125,16 @@ export class PrivateMempool extends SequencerModule implements Mempool { // because a failed tx may succeed now if the failure was to do with a nonce issue, say. private async checkTxValid( transactions: PendingTransaction[], - sortedTransactions: PendingTransaction[], - skippedTransactions: Record, baseService: CachedStateService, stateServiceProvider: StateServiceProvider, networkState: NetworkState, executionContext: RuntimeMethodExecutionContext - ) { - for (const [index, tx] of transactions.entries()) { + ): Promise { + const skippedTransactions: Record = {}; + const sortedTransactions: PendingTransaction[] = []; + const queue: PendingTransaction[] = [...transactions]; + + for (const tx of queue) { const txStateService = new CachedStateService(baseService); stateServiceProvider.setCurrentStateService(txStateService); const contextInputs: RuntimeMethodExecutionData = { @@ -152,7 +151,6 @@ export class PrivateMempool extends SequencerModule implements Mempool { transaction: signedTransaction.transaction, signature: signedTransaction.signature, }); - delete transactions[index]; const { status, statusMessage, stateTransitions } = executionContext.current().result; if (status.toBoolean()) { @@ -165,18 +163,17 @@ export class PrivateMempool extends SequencerModule implements Mempool { stateServiceProvider.popCurrentStateService(); delete skippedTransactions[tx.hash().toString()]; if (Object.entries(skippedTransactions).length > 0) { - // eslint-disable-next-line @typescript-eslint/no-loop-func + const oldTxsReAdded: PendingTransaction[] = []; stateTransitions.forEach((st) => { Object.values(skippedTransactions).forEach((value) => { if (value.paths.some((x) => x.equals(st.path))) { - transactions.push(value.transaction); + oldTxsReAdded.push(value.transaction); } }); }); - // eslint-disable-next-line no-param-reassign - transactions = transactions.filter( - (id, idx, arr) => arr.indexOf(id) === idx + queue.push( + ...oldTxsReAdded.filter(distinctByPredicate((a, b) => a === b)) ); } } else { @@ -192,5 +189,6 @@ export class PrivateMempool extends SequencerModule implements Mempool { } } } + return sortedTransactions; } }