From 7dded84a5401471cd36b1e2067cfffefb1ea6c58 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Tue, 7 Nov 2023 12:40:55 +0100 Subject: [PATCH] Implemented automatic block trigger --- packages/sequencer/src/index.ts | 1 + .../trigger/AutomaticBlockTrigger.ts | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 packages/sequencer/src/protocol/production/trigger/AutomaticBlockTrigger.ts diff --git a/packages/sequencer/src/index.ts b/packages/sequencer/src/index.ts index b0df563e..3914e700 100644 --- a/packages/sequencer/src/index.ts +++ b/packages/sequencer/src/index.ts @@ -28,6 +28,7 @@ export * from "./protocol/production/tasks/StateTransitionTaskParameters"; export * from "./protocol/production/trigger/BlockTrigger"; export * from "./protocol/production/trigger/ManualBlockTrigger"; export * from "./protocol/production/trigger/TimedBlockTrigger"; +export * from "./protocol/production/trigger/AutomaticBlockTrigger"; export * from "./protocol/production/BlockProducerModule"; export * from "./protocol/production/BlockTaskFlowService"; export * from "./protocol/production/TransactionTraceService"; diff --git a/packages/sequencer/src/protocol/production/trigger/AutomaticBlockTrigger.ts b/packages/sequencer/src/protocol/production/trigger/AutomaticBlockTrigger.ts new file mode 100644 index 00000000..16e87f68 --- /dev/null +++ b/packages/sequencer/src/protocol/production/trigger/AutomaticBlockTrigger.ts @@ -0,0 +1,34 @@ +import { inject } from "tsyringe"; +import { log } from "@proto-kit/common"; + +import { SequencerModule } from "../../../sequencer/builder/SequencerModule"; +import { UnprovenProducerModule } from "../unproven/UnprovenProducerModule"; +import { Mempool } from "../../../mempool/Mempool"; + +import { BlockTrigger } from "./BlockTrigger"; + +/** + * Only unproven invocation at the moment, because + * this is primarily for development and testing purposes + */ +export class AutomaticBlockTrigger + extends SequencerModule> + implements BlockTrigger +{ + public constructor( + @inject("UnprovenProducerModule") + private readonly unprovenProducerModule: UnprovenProducerModule, + @inject("Mempool") + private readonly mempool: Mempool + ) { + super(); + } + + public async start(): Promise { + // eslint-disable-next-line @typescript-eslint/no-misused-promises + this.mempool.events.on("transactionAdded", async () => { + log.info("Transaction received, creating block..."); + await this.unprovenProducerModule.tryProduceUnprovenBlock(); + }); + } +}