Skip to content

Commit be1003e

Browse files
feat(transaction-pool-service): fetch transactions for forging in batches (#1296)
* Implement transaction selector * Add selector * Register selector * Update worker * Clean packages on ts:token * Update forger * Pass transaction data * Use block-round identifier * Use iterator * Inline * Use initializer * Reduce complexity * Improve code * Improve code * Handle gasLeft < 0 * Gas used as number * Remove identifier * Clear selector on commit * Cleanup worker * Fix consensus tests * Fix workers * Cleanup worker * Fix pool workers * style: resolve style guide violations [ci-lint-fix] * Fix validator unit tests * Fix functional tests
1 parent 9f86c35 commit be1003e

23 files changed

Lines changed: 364 additions & 264 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build:rs": "lerna run build-rs --scope=@mainsail/evm",
1313
"build:rs:token": "echo \"force-rs $(date +%s)\" > .build-rs",
1414
"build:ts": "lerna run build",
15-
"build:ts:token": "echo \"force-ts $(date +%s)\" > .build-ts",
15+
"build:ts:token": "echo \"force-ts $(date +%s)\" > .build-ts && pnpm run clean:packages",
1616
"clean": "pnpm run clean:packages && rm -r .nx",
1717
"clean:all": "pnpm run clean && pnpm run clean:node_modules",
1818
"clean:node_modules": "rm -rf packages/*/node_modules && rm -rf node_modules",

packages/constants/source/identifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ export const Identifiers = {
175175
},
176176
Forger: {
177177
Block: Symbol("Forger<Block>"),
178-
Transaction: Symbol("Forger<Transaction>"),
179178
},
180179
P2P: {
181180
ApiNode: {
@@ -301,6 +300,7 @@ export const Identifiers = {
301300
Processor: Symbol("TransactionPool<Processor>"),
302301
ProcessorExtension: Symbol("TransactionPool<ProcessorExtension>"),
303302
Query: Symbol("TransactionPool<Query>"),
303+
Selector: Symbol("TransactionPool<Selector>"),
304304
SenderMempool: {
305305
Factory: Symbol("TransactionPool<SenderMempool.Factory>"),
306306
},

packages/contracts/source/contracts/transaction-pool/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./client.js";
33
export * from "./mempool.js";
44
export * from "./processor.js";
55
export * from "./query.js";
6+
export * from "./selector.js";
67
export * from "./sender-mempool.js";
78
export * from "./sender-state.js";
89
export * from "./service.js";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { TransactionData } from "../crypto/index.js";
2+
3+
export type GetBatchOptions = {
4+
blockRound: string;
5+
maxSize: number;
6+
maxBytes: number;
7+
};
8+
9+
export type GetBatchResult = {
10+
transactions: TransactionData[];
11+
remaining: number;
12+
};
13+
14+
export interface Selector {
15+
getBatch(options: GetBatchOptions): Promise<GetBatchResult>;
16+
clear(): void;
17+
}

packages/contracts/source/contracts/transaction-pool/worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import type { CommitHandler } from "../crypto/index.js";
22
import type { EventListener } from "../kernel/index.js";
33
import type { EventCallback, Subprocess } from "../kernel/ipc.js";
44
import type { KeyValuePair } from "../types/index.js";
5+
import type { GetBatchResult, GetBatchOptions } from "./selector.js";
56

67
export type WorkerFlags = KeyValuePair;
78

89
export interface WorkerScriptHandler {
910
boot(flags: WorkerFlags): Promise<void>;
10-
getTransactions(): Promise<string[]>;
11+
getTransactions(options: GetBatchOptions): Promise<GetBatchResult>;
1112
removeTransaction(address: string, id: string): Promise<void>;
1213
commit(height: number, sendersAddresses: string[], consumedGas: number, isSyncing: boolean): Promise<void>;
1314
setPeer(ip: string): Promise<void>;
@@ -22,9 +23,8 @@ export type WorkerSubprocess = Subprocess<WorkerScriptHandler>;
2223

2324
export type WorkerSubprocessFactory = () => WorkerSubprocess;
2425

25-
export interface Worker extends Omit<WorkerScriptHandler, "commit" | "getTransactions">, CommitHandler, EventListener {
26+
export interface Worker extends Omit<WorkerScriptHandler, "commit">, CommitHandler, EventListener {
2627
getQueueSize(): number;
2728
kill(): Promise<number>;
28-
getTransactionBytes(): Promise<Buffer[]>;
2929
registerEventHandler<T>(event: string, callback: EventCallback<T>): void;
3030
}

packages/forger/source/block-forger.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { Identifiers } from "@mainsail/constants";
44
import { inject, injectable } from "@mainsail/container";
55
import { assert } from "@mainsail/utils";
66

7+
import { TransactionForger } from "./transaction-forger.js";
8+
79
@injectable()
810
export class BlockForger implements Contracts.Forger.BlockForger {
11+
@inject(Identifiers.Application.Instance)
12+
private readonly app!: Contracts.Kernel.Application;
13+
914
@inject(Identifiers.Cryptography.Configuration)
1015
private readonly cryptoConfiguration!: Contracts.Crypto.Configuration;
1116

@@ -18,9 +23,6 @@ export class BlockForger implements Contracts.Forger.BlockForger {
1823
@inject(Identifiers.Cryptography.Hash.Factory)
1924
private readonly hashFactory!: Contracts.Crypto.HashFactory;
2025

21-
@inject(Identifiers.Forger.Transaction)
22-
protected readonly transactionForger!: Contracts.Forger.TransactionForger;
23-
2426
@inject(Identifiers.BlockchainUtils.FeeCalculator)
2527
protected readonly gasFeeCalculator!: Contracts.BlockchainUtils.FeeCalculator;
2628

@@ -32,14 +34,12 @@ export class BlockForger implements Contracts.Forger.BlockForger {
3234
const previousBlock = this.stateStore.getLastBlock();
3335
const blockNumber = previousBlock.number + 1;
3436

35-
const { fee, gasUsed, logsBloom, stateRoot, transactions } = await this.transactionForger.getTransactions(
36-
generatorAddress,
37-
timestamp,
38-
{
39-
blockNumber: BigInt(blockNumber),
40-
round: BigInt(round),
41-
},
42-
);
37+
const transactionForger = this.app.resolve(TransactionForger).initialize(generatorAddress, timestamp, {
38+
blockNumber: BigInt(blockNumber),
39+
round: BigInt(round),
40+
});
41+
42+
const { fee, gasUsed, logsBloom, stateRoot, transactions } = await transactionForger.getTransactions();
4343
return this.#makeBlock(round, generatorAddress, logsBloom, stateRoot, transactions, timestamp, gasUsed, fee);
4444
}
4545

packages/forger/source/service-provider.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import { Providers } from "@mainsail/kernel";
44
import Joi from "joi";
55

66
import { BlockForger } from "./block-forger.js";
7-
import { TransactionForger } from "./transaction-forger.js";
87

98
@injectable()
109
export class ServiceProvider extends Providers.ServiceProvider {
1110
public async register(): Promise<void> {
12-
this.app.bind(Identifiers.Forger.Transaction).to(TransactionForger).inSingletonScope();
1311
this.app.bind(Identifiers.Forger.Block).to(BlockForger).inSingletonScope();
1412
}
1513

0 commit comments

Comments
 (0)