diff --git a/packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts b/packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts index 106833f4..ec71fb1f 100644 --- a/packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts +++ b/packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts @@ -18,7 +18,11 @@ import { BlockQueue } from "../../../storage/repositories/BlockStorage"; import { PendingTransaction } from "../../../mempool/PendingTransaction"; import { AsyncMerkleTreeStore } from "../../../state/async/AsyncMerkleTreeStore"; import { AsyncStateService } from "../../../state/async/AsyncStateService"; -import { Block, BlockWithResult } from "../../../storage/model/Block"; +import { + Block, + BlockResult, + BlockWithResult, +} from "../../../storage/model/Block"; import { CachedStateService } from "../../../state/state/CachedStateService"; import { MessageStorage } from "../../../storage/repositories/MessageStorage"; @@ -99,7 +103,23 @@ export class BlockProducerModule extends SequencerModule { } } - public async tryProduceBlock(): Promise { + public async generateMetadata(block: Block): Promise { + const { result, blockHashTreeStore, treeStore } = + await this.executionService.generateMetadataForNextBlock( + block, + this.unprovenMerkleStore, + this.blockTreeStore + ); + + await blockHashTreeStore.mergeIntoParent(); + await treeStore.mergeIntoParent(); + + await this.blockQueue.pushResult(result); + + return result; + } + + public async tryProduceBlock(): Promise { if (!this.productionInProgress) { try { const block = await this.produceBlock(); @@ -118,20 +138,7 @@ export class BlockProducerModule extends SequencerModule { ); this.prettyPrintBlockContents(block); - // Generate metadata for next block - - // TODO: make async of production in the future - const result = await this.executionService.generateMetadataForNextBlock( - block, - this.unprovenMerkleStore, - this.blockTreeStore, - true - ); - - return { - block, - result, - }; + return block; } catch (error: unknown) { if (error instanceof Error) { throw error; @@ -196,7 +203,11 @@ export class BlockProducerModule extends SequencerModule { this.allowEmptyBlock() ); - await cachedStateService.mergeIntoParent(); + if (block !== undefined) { + await cachedStateService.mergeIntoParent(); + + await this.blockQueue.pushBlock(block); + } this.productionInProgress = false; diff --git a/packages/sequencer/src/protocol/production/sequencing/TransactionExecutionService.ts b/packages/sequencer/src/protocol/production/sequencing/TransactionExecutionService.ts index dae2a7b7..6b107f28 100644 --- a/packages/sequencer/src/protocol/production/sequencing/TransactionExecutionService.ts +++ b/packages/sequencer/src/protocol/production/sequencing/TransactionExecutionService.ts @@ -323,9 +323,12 @@ export class TransactionExecutionService { public async generateMetadataForNextBlock( block: Block, merkleTreeStore: AsyncMerkleTreeStore, - blockHashTreeStore: AsyncMerkleTreeStore, - modifyTreeStore = true - ): Promise { + blockHashTreeStore: AsyncMerkleTreeStore + ): Promise<{ + result: BlockResult; + treeStore: CachedMerkleTreeStore; + blockHashTreeStore: CachedMerkleTreeStore; + }> { // Flatten diff list into a single diff by applying them over each other const combinedDiff = block.transactions .map((tx) => { @@ -403,22 +406,21 @@ export class TransactionExecutionService { ); const blockHashWitness = blockHashTree.getWitness(block.height.toBigInt()); const newBlockHashRoot = blockHashTree.getRoot(); - await blockHashInMemoryStore.mergeIntoParent(); - - if (modifyTreeStore) { - await inMemoryStore.mergeIntoParent(); - } return { - afterNetworkState: resultingNetworkState, - stateRoot: stateRoot.toBigInt(), - blockHashRoot: newBlockHashRoot.toBigInt(), - blockHashWitness, - - blockStateTransitions: reducedStateTransitions.map((st) => - UntypedStateTransition.fromStateTransition(st) - ), - blockHash: block.hash.toBigInt(), + result: { + afterNetworkState: resultingNetworkState, + stateRoot: stateRoot.toBigInt(), + blockHashRoot: newBlockHashRoot.toBigInt(), + blockHashWitness, + + blockStateTransitions: reducedStateTransitions.map((st) => + UntypedStateTransition.fromStateTransition(st) + ), + blockHash: block.hash.toBigInt(), + }, + treeStore: inMemoryStore, + blockHashTreeStore: blockHashInMemoryStore, }; } diff --git a/packages/sequencer/src/protocol/production/trigger/BlockTrigger.ts b/packages/sequencer/src/protocol/production/trigger/BlockTrigger.ts index 4a1862cb..97252e56 100644 --- a/packages/sequencer/src/protocol/production/trigger/BlockTrigger.ts +++ b/packages/sequencer/src/protocol/production/trigger/BlockTrigger.ts @@ -65,28 +65,29 @@ export class BlockTriggerBase< return undefined; } - protected async produceBlockWithResult( - enqueueInSettlementQueue: boolean - ): Promise { + protected async produceBlockWithResult(): Promise< + BlockWithResult | undefined + > { const block = await this.blockProducerModule.tryProduceBlock(); + if (block) { + this.events.emit("block-produced", block); - if (block && enqueueInSettlementQueue) { - await this.blockQueue.pushBlock(block.block); - this.events.emit("block-produced", block.block); + const result = await this.blockProducerModule.generateMetadata(block); - await this.blockQueue.pushResult(block.result); - this.events.emit("block-metadata-produced", block); - } + const blockWithMetadata = { + block, + result, + }; + + this.events.emit("block-metadata-produced", blockWithMetadata); - return block; + return blockWithMetadata; + } + return undefined; } - protected async produceBlock( - enqueueInSettlementQueue: boolean - ): Promise { - const blockWithResult = await this.produceBlockWithResult( - enqueueInSettlementQueue - ); + protected async produceBlock(): Promise { + const blockWithResult = await this.produceBlockWithResult(); return blockWithResult?.block; } diff --git a/packages/sequencer/src/protocol/production/trigger/ManualBlockTrigger.ts b/packages/sequencer/src/protocol/production/trigger/ManualBlockTrigger.ts index 2a8b755f..38699125 100644 --- a/packages/sequencer/src/protocol/production/trigger/ManualBlockTrigger.ts +++ b/packages/sequencer/src/protocol/production/trigger/ManualBlockTrigger.ts @@ -62,15 +62,11 @@ export class ManualBlockTrigger return await super.settle(batch); } - public async produceBlock( - enqueueInSettlementQueue: boolean = true - ): Promise { - return await super.produceBlock(enqueueInSettlementQueue); + public async produceBlock(): Promise { + return await super.produceBlock(); } - public async produceBlockWithResult( - enqueueInSettlementQueue: boolean = true - ): Promise { - return await super.produceBlockWithResult(enqueueInSettlementQueue); + public async produceBlockWithResult(): Promise { + return await super.produceBlockWithResult(); } } diff --git a/packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts b/packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts index 5c892329..c1b8e132 100644 --- a/packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts +++ b/packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts @@ -129,7 +129,7 @@ export class TimedBlockTrigger // Produce a block if either produceEmptyBlocks is true or we have more // than 1 tx in mempool if (mempoolTxs.length > 0 || (this.config.produceEmptyBlocks ?? true)) { - await this.produceBlock(true); + await this.produceBlock(); } }