Skip to content

Commit

Permalink
feat(evm-api-worker): start API server from Start handler (#724)
Browse files Browse the repository at this point in the history
* Add start handler

* Move identifiers

* Rename identifier

* Start server from start handler

* Start evm worker

* Empty boot
  • Loading branch information
sebastijankuzner authored Oct 4, 2024
1 parent 464b5d5 commit ea33dac
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 12 deletions.
4 changes: 0 additions & 4 deletions packages/api-evm/source/identifiers.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/api-evm/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./identifiers.js";
export * as Schemas from "./schemas.js";
export * from "./server.js";
export * from "./service-provider.js";
9 changes: 5 additions & 4 deletions packages/api-evm/source/service-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractServiceProvider, Plugins, ServerConstructor } from "@mainsail/api-common";
import { Contracts } from "@mainsail/contracts";
import { Contracts, Identifiers } from "@mainsail/contracts";
import Joi from "joi";

import {
Expand All @@ -14,16 +14,15 @@ import {
Web3ClientVersionAction,
} from "./actions/index.js";
import Handlers from "./handlers.js";
import { Identifiers as ApiIdentifiers } from "./identifiers.js";
import { Server } from "./server.js";

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

protected httpsIdentifier(): symbol {
return ApiIdentifiers.HTTPS;
return Identifiers.Evm.API.HTTPS;
}

protected getServerConstructor(): ServerConstructor<Server> {
Expand All @@ -34,6 +33,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
4 changes: 4 additions & 0 deletions packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class Bootstrapper {
@inject(Identifiers.TransactionPool.Worker)
private readonly txPoolWorker!: Contracts.TransactionPool.Worker;

@inject(Identifiers.Evm.Worker)
private readonly evmWorker!: Contracts.Evm.Worker;

public async bootstrap(): Promise<void> {
try {
if (this.apiSync) {
Expand All @@ -71,6 +74,7 @@ export class Bootstrapper {

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

void this.runConsensus();

Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/contracts/evm/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface WorkerScriptHandler {
boot(flags: WorkerFlags): Promise<void>;
setPeerCount(peerCount: number): Promise<void>;
commit(height: number): Promise<void>;
start(height: number): Promise<void>;
}

export type WorkerFactory = () => Worker;
Expand Down
8 changes: 6 additions & 2 deletions packages/contracts/source/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export const Identifiers = {
},
},
Evm: {
API: {
HTTP: Symbol.for("Evm<API.HTTP>"),
HTTPS: Symbol.for("Evm<API.HTTPS>"),
},
Gas: {
FeeCalculator: Symbol("Evm<Gas.FeeCalculator>"),
Limits: Symbol("Evm<Gas.Limits>"),
Expand Down Expand Up @@ -254,8 +258,8 @@ export const Identifiers = {
},
TransactionPool: {
API: {
HTTP: Symbol.for("API<TransactionPool.HTTP>"),
HTTPS: Symbol.for("API<TransactionPool.HTTPS>"),
HTTP: Symbol.for("TransactionPool<API.HTTP>"),
HTTPS: Symbol.for("TransactionPool<API.HTTPS>"),
},
Broadcaster: Symbol("TransactionPoolBroadcaster<Broadcaster>"),
ExpirationService: Symbol("TransactionPool<ExpirationService>"),
Expand Down
1 change: 1 addition & 0 deletions packages/evm-api-worker/source/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./commit.js";
export * from "./set-peer-count.js";
export * from "./start.js";
28 changes: 28 additions & 0 deletions packages/evm-api-worker/source/handlers/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)
private readonly store!: Contracts.State.Store;

@inject(Identifiers.ServiceProvider.Configuration)
@tagged("plugin", "api-evm")
private readonly configuration!: Providers.PluginConfiguration;

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

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

if (this.configuration.get("server.https.enabled")) {
await this.app.get<Contracts.Api.Server>(Identifiers.Evm.API.HTTPS).boot();
}
}
}
6 changes: 5 additions & 1 deletion packages/evm-api-worker/source/worker-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Container } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Application } from "@mainsail/kernel";

import { CommitHandler, SetPeerCountHandler } from "./handlers/index.js";
import { CommitHandler, SetPeerCountHandler, StartHandler } from "./handlers/index.js";

export class WorkerScriptHandler implements Contracts.Evm.WorkerScriptHandler {
// @ts-ignore
Expand All @@ -20,6 +20,10 @@ export class WorkerScriptHandler implements Contracts.Evm.WorkerScriptHandler {
this.#app = app;
}

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

public async setPeerCount(peerCount: number): Promise<void> {
await this.#app.resolve(SetPeerCountHandler).handle(peerCount);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/evm-api-worker/source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class Worker implements Contracts.Evm.Worker {
return this.ipcSubprocess.getQueueSize();
}

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

async onCommit(unit: Contracts.Processor.ProcessableUnit): Promise<void> {
await this.ipcSubprocess.sendRequest("commit", unit.height);
}
Expand Down

0 comments on commit ea33dac

Please sign in to comment.