Skip to content

Commit

Permalink
Add in improvement to make more efficient search.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Oct 11, 2024
1 parent 7925739 commit 17a5a94
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions packages/sequencer/src/mempool/private/PrivateMempool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RuntimeMethodExecutionContext,
RuntimeMethodExecutionData,
} from "@proto-kit/protocol";
import { Field } from "o1js";

import type { Mempool, MempoolEvents } from "../Mempool";
import type { PendingTransaction } from "../PendingTransaction";
Expand All @@ -25,6 +26,10 @@ import {
import { CachedStateService } from "../../state/state/CachedStateService";
import { AsyncStateService } from "../../state/async/AsyncStateService";

type MempoolStateTransition = {
transaction: PendingTransaction;
paths: Field[];
};
@sequencerModule()
export class PrivateMempool extends SequencerModule implements Mempool {
public readonly events = new EventEmitter<MempoolEvents>();
Expand Down Expand Up @@ -87,7 +92,7 @@ export class PrivateMempool extends SequencerModule implements Mempool {
public async getTxs(): Promise<PendingTransaction[]> {
const txs = await this.transactionStorage.getPendingUserTransactions();
const sortedTxs: PendingTransaction[] = [];
const skippedTxs: PendingTransaction[] = [];
const skippedTxs: Record<string, MempoolStateTransition> = {};
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
RuntimeMethodExecutionContext
);
Expand All @@ -102,6 +107,7 @@ export class PrivateMempool extends SequencerModule implements Mempool {
const checkTxValid = async (
transactions: PendingTransaction[],
reEvaluation: boolean = false
// eslint-disable-next-line sonarjs/cognitive-complexity
) => {
for (const tx of transactions) {
const txStateService = new CachedStateService(baseCachedStateService);
Expand All @@ -127,26 +133,38 @@ export class PrivateMempool extends SequencerModule implements Mempool {
if (status.toBoolean()) {
console.log(`Accepted tx ${tx.hash().toString()}`);
sortedTxs.push(tx);
if (skippedTxs.includes(tx)) {
skippedTxs.splice(skippedTxs.indexOf(tx), 1);
}

// eslint-disable-next-line no-await-in-loop
await txStateService.applyStateTransitions(stateTransitions);
// eslint-disable-next-line no-await-in-loop
await txStateService.mergeIntoParent();
this.protocol.stateServiceProvider.popCurrentStateService();

if (skippedTxs.length > 0) {
// eslint-disable-next-line no-await-in-loop
await checkTxValid(skippedTxs, true);
delete skippedTxs[tx.hash().toString()];
if (Object.entries(skippedTxs).length > 0) {
const candidateMissedTxs: PendingTransaction[] = [];
// eslint-disable-next-line array-callback-return
stateTransitions.map((st) => {
Object.values(skippedTxs).forEach((value) => {
if (value.paths.some((x) => x.equals(st.path))) {
candidateMissedTxs.push(value.transaction);
}
});
});
if (candidateMissedTxs.length > 0) {
// eslint-disable-next-line no-await-in-loop
await checkTxValid(candidateMissedTxs, true);
}
}
} else {
console.log(
`Skipped tx ${tx.hash().toString()} because ${statusMessage}`
);
this.protocol.stateServiceProvider.popCurrentStateService();
if (!reEvaluation) {
skippedTxs.push(tx);
skippedTxs[tx.hash().toString()] = {
transaction: tx,
paths: stateTransitions.map((x) => x.path),
};
}
}
}
Expand Down

0 comments on commit 17a5a94

Please sign in to comment.