Skip to content

Commit

Permalink
Pulled apart block production and metadata generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Nov 22, 2024
1 parent 04124a9 commit d2f7251
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -99,7 +103,23 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
}
}

public async tryProduceBlock(): Promise<BlockWithResult | undefined> {
public async generateMetadata(block: Block): Promise<BlockResult> {
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<Block | undefined> {
if (!this.productionInProgress) {
try {
const block = await this.produceBlock();
Expand All @@ -118,20 +138,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
);
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;
Expand Down Expand Up @@ -196,7 +203,11 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
this.allowEmptyBlock()
);

await cachedStateService.mergeIntoParent();
if (block !== undefined) {
await cachedStateService.mergeIntoParent();

await this.blockQueue.pushBlock(block);
}

this.productionInProgress = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ export class TransactionExecutionService {
public async generateMetadataForNextBlock(
block: Block,
merkleTreeStore: AsyncMerkleTreeStore,
blockHashTreeStore: AsyncMerkleTreeStore,
modifyTreeStore = true
): Promise<BlockResult> {
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) => {
Expand Down Expand Up @@ -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,
};
}

Expand Down
33 changes: 17 additions & 16 deletions packages/sequencer/src/protocol/production/trigger/BlockTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,29 @@ export class BlockTriggerBase<
return undefined;
}

protected async produceBlockWithResult(
enqueueInSettlementQueue: boolean
): Promise<BlockWithResult | undefined> {
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<Block | undefined> {
const blockWithResult = await this.produceBlockWithResult(
enqueueInSettlementQueue
);
protected async produceBlock(): Promise<Block | undefined> {
const blockWithResult = await this.produceBlockWithResult();

return blockWithResult?.block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ export class ManualBlockTrigger
return await super.settle(batch);
}

public async produceBlock(
enqueueInSettlementQueue: boolean = true
): Promise<Block | undefined> {
return await super.produceBlock(enqueueInSettlementQueue);
public async produceBlock(): Promise<Block | undefined> {
return await super.produceBlock();
}

public async produceBlockWithResult(
enqueueInSettlementQueue: boolean = true
): Promise<BlockWithResult | undefined> {
return await super.produceBlockWithResult(enqueueInSettlementQueue);
public async produceBlockWithResult(): Promise<BlockWithResult | undefined> {
return await super.produceBlockWithResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit d2f7251

Please sign in to comment.