Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Oct 16, 2024
1 parent d612b91 commit 3ef48c4
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions packages/sequencer/src/mempool/private/PrivateMempool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,8 +95,6 @@ export class PrivateMempool extends SequencerModule implements Mempool {

public async getTxs(): Promise<PendingTransaction[]> {
const txs = await this.transactionStorage.getPendingUserTransactions();
const sortedTxs: PendingTransaction[] = [];
const skippedTxs: Record<string, MempoolTransactionPaths> = {};
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
RuntimeMethodExecutionContext
);
Expand All @@ -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,
Expand All @@ -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<string, MempoolTransactionPaths>,
baseService: CachedStateService,
stateServiceProvider: StateServiceProvider,
networkState: NetworkState,
executionContext: RuntimeMethodExecutionContext
) {
for (const [index, tx] of transactions.entries()) {
): Promise<PendingTransaction[]> {
const skippedTransactions: Record<string, MempoolTransactionPaths> = {};
const sortedTransactions: PendingTransaction[] = [];
const queue: PendingTransaction[] = [...transactions];

for (const tx of queue) {
const txStateService = new CachedStateService(baseService);
stateServiceProvider.setCurrentStateService(txStateService);
const contextInputs: RuntimeMethodExecutionData = {
Expand All @@ -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()) {
Expand All @@ -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 {
Expand All @@ -192,5 +189,6 @@ export class PrivateMempool extends SequencerModule implements Mempool {
}
}
}
return sortedTransactions;
}
}

0 comments on commit 3ef48c4

Please sign in to comment.