Skip to content

Commit

Permalink
remove transaction from pool if apply fails
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Dec 20, 2024
1 parent 3500a24 commit 701533f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface WorkerFlags extends KeyValuePair {}
export interface WorkerScriptHandler {
boot(flags: WorkerFlags): Promise<void>;
getTransactions(): Promise<string[]>;
removeTransaction(address: string, id: string): Promise<void>;
commit(height: number, sendersAddresses: string[]): Promise<void>;
setPeer(ip: string): Promise<void>;
forgetPeer(ip: string): Promise<void>;
Expand All @@ -25,5 +26,6 @@ export interface Worker extends Omit<WorkerScriptHandler, "commit" | "getTransac
getQueueSize(): number;
kill(): Promise<number>;
getTransactionBytes(): Promise<Buffer[]>;
removeTransaction(address: string, id: string): Promise<void>;
registerEventHandler(event: string, callback: EventCallback<any>): void;
}
1 change: 1 addition & 0 deletions packages/transaction-pool-worker/source/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from "./commit.js";
export * from "./forget-peer.js";
export * from "./get-transactions.js";
export * from "./reload-webhooks.js";
export * from "./remove-transaction.js";
export * from "./set-peer.js";
export * from "./start.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

@injectable()
export class RemoveTransactionHandler {
@inject(Identifiers.TransactionPool.Mempool)
private readonly mempool!: Contracts.TransactionPool.Mempool;

public async handle(address: string, id: string): Promise<void> {
await this.mempool.removeTransaction(address, id);
}
}
5 changes: 5 additions & 0 deletions packages/transaction-pool-worker/source/worker-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ForgetPeerHandler,
GetTransactionsHandler,
ReloadWebhooksHandler,
RemoveTransactionHandler,
SetPeerHandler,
StartHandler,
} from "./handlers/index.js";
Expand Down Expand Up @@ -39,6 +40,10 @@ export class WorkerScriptHandler implements Contracts.TransactionPool.WorkerScri
return await this.#app.resolve(GetTransactionsHandler).handle();
}

public async removeTransaction(address: string, id: string): Promise<void> {
await this.#app.resolve(RemoveTransactionHandler).handle(address, id);
}

public async setPeer(ip: string): Promise<void> {
return await this.#app.resolve(SetPeerHandler).handle(ip);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/transaction-pool-worker/source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class Worker implements Contracts.TransactionPool.Worker {
return response.map((transaction: string) => Buffer.from(transaction, "hex"));
}

public async removeTransaction(address: string, id: string): Promise<void> {
await this.ipcSubprocess.sendRequest("removeTransaction", address, id);
}

public async setPeer(ip: string): Promise<void> {
await this.ipcSubprocess.sendRequest("setPeer", ip);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/validator/source/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ export class Validator implements Contracts.Validator.Validator {
candidateTransactions.push(transaction);
} catch (error) {
this.logger.warning(`${transaction.id} failed to collate: ${error.message}`);

await this.txPoolWorker.removeTransaction(transaction.data.senderAddress, transaction.id);

failedSenders.add(transaction.data.senderPublicKey);
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/transaction-pool-api/source/pool-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class PoolWorker implements Contracts.TransactionPool.Worker {
return response.map((transaction: string) => Buffer.from(transaction, "hex"));
}

public async removeTransaction(address: string, id: string): Promise<void> {
await this.transactionPoolMempool.removeTransaction(address, id);
}

registerEventHandler(event: string, callback: Contracts.Kernel.IPC.EventCallback<any>): void {}

async setPeer(ip: string): Promise<void> {}
Expand Down

0 comments on commit 701533f

Please sign in to comment.