From 701533fc6956e4cf8c7bf0f3c8a94ef10136bb09 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:16:46 +0900 Subject: [PATCH] remove transaction from pool if apply fails --- .../source/contracts/transaction-pool/worker.ts | 2 ++ .../transaction-pool-worker/source/handlers/index.ts | 1 + .../source/handlers/remove-transaction.ts | 12 ++++++++++++ .../transaction-pool-worker/source/worker-handler.ts | 5 +++++ packages/transaction-pool-worker/source/worker.ts | 4 ++++ packages/validator/source/validator.ts | 3 +++ .../transaction-pool-api/source/pool-worker.ts | 4 ++++ 7 files changed, 31 insertions(+) create mode 100644 packages/transaction-pool-worker/source/handlers/remove-transaction.ts diff --git a/packages/contracts/source/contracts/transaction-pool/worker.ts b/packages/contracts/source/contracts/transaction-pool/worker.ts index 4dd8b17a2..db4fd82cd 100644 --- a/packages/contracts/source/contracts/transaction-pool/worker.ts +++ b/packages/contracts/source/contracts/transaction-pool/worker.ts @@ -8,6 +8,7 @@ export interface WorkerFlags extends KeyValuePair {} export interface WorkerScriptHandler { boot(flags: WorkerFlags): Promise; getTransactions(): Promise; + removeTransaction(address: string, id: string): Promise; commit(height: number, sendersAddresses: string[]): Promise; setPeer(ip: string): Promise; forgetPeer(ip: string): Promise; @@ -25,5 +26,6 @@ export interface Worker extends Omit; getTransactionBytes(): Promise; + removeTransaction(address: string, id: string): Promise; registerEventHandler(event: string, callback: EventCallback): void; } diff --git a/packages/transaction-pool-worker/source/handlers/index.ts b/packages/transaction-pool-worker/source/handlers/index.ts index 6c366279b..46d1b4712 100644 --- a/packages/transaction-pool-worker/source/handlers/index.ts +++ b/packages/transaction-pool-worker/source/handlers/index.ts @@ -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"; diff --git a/packages/transaction-pool-worker/source/handlers/remove-transaction.ts b/packages/transaction-pool-worker/source/handlers/remove-transaction.ts new file mode 100644 index 000000000..1497a1bc9 --- /dev/null +++ b/packages/transaction-pool-worker/source/handlers/remove-transaction.ts @@ -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 { + await this.mempool.removeTransaction(address, id); + } +} diff --git a/packages/transaction-pool-worker/source/worker-handler.ts b/packages/transaction-pool-worker/source/worker-handler.ts index e345da53f..ed9e14939 100644 --- a/packages/transaction-pool-worker/source/worker-handler.ts +++ b/packages/transaction-pool-worker/source/worker-handler.ts @@ -7,6 +7,7 @@ import { ForgetPeerHandler, GetTransactionsHandler, ReloadWebhooksHandler, + RemoveTransactionHandler, SetPeerHandler, StartHandler, } from "./handlers/index.js"; @@ -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 { + await this.#app.resolve(RemoveTransactionHandler).handle(address, id); + } + public async setPeer(ip: string): Promise { return await this.#app.resolve(SetPeerHandler).handle(ip); } diff --git a/packages/transaction-pool-worker/source/worker.ts b/packages/transaction-pool-worker/source/worker.ts index 1a0761c34..65ff43dd8 100644 --- a/packages/transaction-pool-worker/source/worker.ts +++ b/packages/transaction-pool-worker/source/worker.ts @@ -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 { + await this.ipcSubprocess.sendRequest("removeTransaction", address, id); + } + public async setPeer(ip: string): Promise { await this.ipcSubprocess.sendRequest("setPeer", ip); } diff --git a/packages/validator/source/validator.ts b/packages/validator/source/validator.ts index 475bd5679..64f75dac6 100644 --- a/packages/validator/source/validator.ts +++ b/packages/validator/source/validator.ts @@ -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); } } diff --git a/tests/functional/transaction-pool-api/source/pool-worker.ts b/tests/functional/transaction-pool-api/source/pool-worker.ts index d3b011c16..93dbd60b3 100644 --- a/tests/functional/transaction-pool-api/source/pool-worker.ts +++ b/tests/functional/transaction-pool-api/source/pool-worker.ts @@ -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 { + await this.transactionPoolMempool.removeTransaction(address, id); + } + registerEventHandler(event: string, callback: Contracts.Kernel.IPC.EventCallback): void {} async setPeer(ip: string): Promise {}