|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { Transaction } from 'ethers'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Result of attempting to add a transaction to the pending list. |
| 7 | + * |
| 8 | + * This type provides information about whether the operation succeeded and the resulting state is as following: |
| 9 | + * - `{ ok: true; newValue: number }` — The transaction was added successfully and the new pending count is returned. |
| 10 | + * - `{ ok: false; current: number }` — The transaction was not added and the current pending count is returned. |
| 11 | + */ |
| 12 | +export type AddToListResult = { ok: true; newValue: number } | { ok: false; current: number }; |
| 13 | + |
| 14 | +/** |
| 15 | + * Service responsible for managing pending transactions in the pool and coordinating with consensus results. |
| 16 | + */ |
| 17 | +export interface TransactionPoolService { |
| 18 | + /** |
| 19 | + * Saves a transaction into the transaction pool for the given address. |
| 20 | + * |
| 21 | + * @param address - The account address that submits the transaction. |
| 22 | + * @param tx - The transaction object to be stored. |
| 23 | + * @returns A promise that resolves once the transaction is stored. |
| 24 | + */ |
| 25 | + saveTransaction(address: string, tx: Transaction): Promise<void>; |
| 26 | + |
| 27 | + /** |
| 28 | + * Handles consensus results and updates the pool state accordingly. |
| 29 | + * |
| 30 | + * @returns A promise that resolves when the consensus result has been received. |
| 31 | + */ |
| 32 | + onConsensusResult(): Promise<void>; |
| 33 | + |
| 34 | + /** |
| 35 | + * Retrieves the number of pending transactions for a given address. |
| 36 | + * |
| 37 | + * @param address - The account address to query. |
| 38 | + * @returns A promise that resolves to the number of pending transactions. |
| 39 | + */ |
| 40 | + getPendingCount(address: string): Promise<number>; |
| 41 | + |
| 42 | + /** |
| 43 | + * Clears the transaction pool state (on application restart or crash). |
| 44 | + * |
| 45 | + * @returns A promise that resolves once the state has been reset. |
| 46 | + */ |
| 47 | + resetState(): Promise<void>; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Storage layer for managing pending transactions metadata. |
| 52 | + */ |
| 53 | +export interface PendingTransactionStorage { |
| 54 | + /** |
| 55 | + * Retrieves the number of pending transactions for a given address. |
| 56 | + * |
| 57 | + * @param addr - The account address to look up. |
| 58 | + * @returns A promise that resolves to the pending transaction count. |
| 59 | + */ |
| 60 | + getList(addr: string): Promise<number>; |
| 61 | + |
| 62 | + /** |
| 63 | + * Attempts to add a pending transaction entry for the given address. |
| 64 | + * |
| 65 | + * @param addr - The account address. |
| 66 | + * @param expectedPending - The expected number of pending transactions. |
| 67 | + * @returns A promise that resolves to an {@link AddToListResult}. |
| 68 | + */ |
| 69 | + addToList(addr: string, expectedPending: number): Promise<AddToListResult>; |
| 70 | + |
| 71 | + /** |
| 72 | + * Removes a transaction from the pending list of the given address. |
| 73 | + * |
| 74 | + * @param address - The account address whose transaction should be removed. |
| 75 | + * @param transaction - The transaction identifier (e.g., hash). |
| 76 | + * @returns A promise that resolves to the updated pending count. |
| 77 | + */ |
| 78 | + removeFromList(address: string, transaction: string): Promise<number>; |
| 79 | + |
| 80 | + /** |
| 81 | + * Removes all pending transactions across all addresses. |
| 82 | + * |
| 83 | + * @returns A promise that resolves once all entries have been cleared. |
| 84 | + */ |
| 85 | + removeAll(): Promise<void>; |
| 86 | +} |
0 commit comments