Skip to content

Commit

Permalink
feat(transaction-pool-api): boot API server from Start handler (#723)
Browse files Browse the repository at this point in the history
* Set height on start

* Start transaction pool API on start

* Extract contract

* style: resolve style guide violations

---------

Co-authored-by: sebastijankuzner <[email protected]>
  • Loading branch information
sebastijankuzner and sebastijankuzner authored Oct 4, 2024
1 parent d595d5f commit 464b5d5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 16 deletions.
4 changes: 0 additions & 4 deletions packages/api-transaction-pool/source/identifiers.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/api-transaction-pool/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./identifiers.js";
export * from "./server.js";
export * from "./service-provider.js";
8 changes: 5 additions & 3 deletions packages/api-transaction-pool/source/service-provider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AbstractServiceProvider, Plugins, ServerConstructor } from "@mainsail/api-common";
import { Identifiers } from "@mainsail/contracts";

import Handlers from "./handlers.js";
import { Identifiers as ApiTransactionPoolIdentifiers } from "./identifiers.js";
import { Server } from "./server.js";

export class ServiceProvider extends AbstractServiceProvider<Server> {
protected httpIdentifier(): symbol {
return ApiTransactionPoolIdentifiers.HTTP;
return Identifiers.TransactionPool.API.HTTP;
}

protected httpsIdentifier(): symbol {
return ApiTransactionPoolIdentifiers.HTTPS;
return Identifiers.TransactionPool.API.HTTPS;
}

protected getServerConstructor(): ServerConstructor<Server> {
Expand All @@ -21,6 +21,8 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
return Handlers;
}

public async boot(): Promise<void> {}

protected getPlugins(): any[] {
const config = this.config().get<any>("plugins");

Expand Down
2 changes: 1 addition & 1 deletion packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Bootstrapper {
this.state.setBootstrap(false);

this.validatorRepository.printLoadedValidators();
await this.txPoolWorker.start();
await this.txPoolWorker.start(this.stateStore.getHeight());

void this.runConsensus();

Expand Down
5 changes: 5 additions & 0 deletions packages/contracts/source/contracts/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { Processor } from "./rpc.js";

export type ApiServer = Hapi.Server<ServerState>;

export interface Server {
boot(): Promise<void>;
dispose(): Promise<void>;
}

export enum ServerType {
Http = "HTTP",
Https = "HTTPS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface WorkerScriptHandler {
commit(height: number, sendersAddresses: string[]): Promise<void>;
setPeer(ip: string): Promise<void>;
forgetPeer(ip: string): Promise<void>;
start(): Promise<void>;
start(height: number): Promise<void>;
reloadWebhooks(): Promise<void>;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/source/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ export const Identifiers = {
},
},
TransactionPool: {
API: {
HTTP: Symbol.for("API<TransactionPool.HTTP>"),
HTTPS: Symbol.for("API<TransactionPool.HTTPS>"),
},
Broadcaster: Symbol("TransactionPoolBroadcaster<Broadcaster>"),
ExpirationService: Symbol("TransactionPool<ExpirationService>"),
Mempool: Symbol("TransactionPool<Mempool>"),
Expand Down
24 changes: 22 additions & 2 deletions packages/transaction-pool-worker/source/handlers/start.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import { inject, injectable } from "@mainsail/container";
import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Providers } from "@mainsail/kernel";

@injectable()
export class StartHandler {
@inject(Identifiers.Application.Instance)
protected readonly app!: Contracts.Kernel.Application;

@inject(Identifiers.State.Store)
protected readonly store!: Contracts.State.Store;

@inject(Identifiers.TransactionPool.Service)
private readonly transactionPoolService!: Contracts.TransactionPool.Service;

public async handle(): Promise<void> {
@inject(Identifiers.ServiceProvider.Configuration)
@tagged("plugin", "api-transaction-pool")
private readonly configuration!: Providers.PluginConfiguration;

public async handle(height: number): Promise<void> {
this.store.setHeight(height);
await this.transactionPoolService.reAddTransactions();

if (this.configuration.get("server.http.enabled")) {
await this.app.get<Contracts.Api.Server>(Identifiers.TransactionPool.API.HTTP).boot();
}

if (this.configuration.get("server.https.enabled")) {
await this.app.get<Contracts.Api.Server>(Identifiers.TransactionPool.API.HTTPS).boot();
}
}
}
4 changes: 2 additions & 2 deletions packages/transaction-pool-worker/source/worker-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class WorkerScriptHandler implements Contracts.TransactionPool.WorkerScri
this.#app = app;
}

public async start(): Promise<void> {
await this.#app.resolve(StartHandler).handle();
public async start(height: number): Promise<void> {
await this.#app.resolve(StartHandler).handle(height);
}

public async commit(height: number, sendersAddresses: string[]): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions packages/transaction-pool-worker/source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class Worker implements Contracts.TransactionPool.Worker {
await this.ipcSubprocess.sendRequest("commit", unit.height, [...sendersAddresses.keys()]);
}

public async start(): Promise<void> {
await this.ipcSubprocess.sendRequest("start");
public async start(height: number): Promise<void> {
await this.ipcSubprocess.sendRequest("start", height);
}

public async getTransactionBytes(): Promise<Buffer[]> {
Expand Down

0 comments on commit 464b5d5

Please sign in to comment.