Skip to content

Commit 892117b

Browse files
committed
Integrated tracing points into block prod pipeline
1 parent 849228b commit 892117b

File tree

5 files changed

+66
-25
lines changed

5 files changed

+66
-25
lines changed

packages/sequencer/src/mempool/private/PrivateMempool.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { CachedStateService } from "../../state/state/CachedStateService";
3030
import { AsyncStateService } from "../../state/async/AsyncStateService";
3131
import { distinctByPredicate } from "../../helpers/utils";
32+
import { Tracer } from "../../logging/Tracer";
3233

3334
type MempoolTransactionPaths = {
3435
transaction: PendingTransaction;
@@ -49,13 +50,19 @@ export class PrivateMempool extends SequencerModule implements Mempool {
4950
@inject("Sequencer")
5051
private readonly sequencer: Sequencer<SequencerModulesRecord>,
5152
@inject("UnprovenStateService")
52-
private readonly stateService: AsyncStateService
53+
private readonly stateService: AsyncStateService,
54+
@inject("Tracer") private readonly tracer: Tracer
5355
) {
5456
super();
5557
this.accountStateHook =
5658
this.protocol.dependencyContainer.resolve("AccountState");
5759
}
5860

61+
public async length(): Promise<number> {
62+
const txs = await this.transactionStorage.getPendingUserTransactions();
63+
return txs.length;
64+
}
65+
5966
public async add(tx: PendingTransaction): Promise<boolean> {
6067
const [txValid, error] = this.transactionValidator.validateTx(tx);
6168
if (txValid) {
@@ -96,22 +103,29 @@ export class PrivateMempool extends SequencerModule implements Mempool {
96103
}
97104

98105
public async getTxs(limit?: number): Promise<PendingTransaction[]> {
99-
const txs = await this.transactionStorage.getPendingUserTransactions();
100-
101-
const baseCachedStateService = new CachedStateService(this.stateService);
102-
103-
const networkState =
104-
(await this.getStagedNetworkState()) ?? NetworkState.empty();
105-
106-
const sortedTxs = await this.checkTxValid(
107-
txs,
108-
baseCachedStateService,
109-
this.protocol.stateServiceProvider,
110-
networkState,
111-
limit
112-
);
113-
this.protocol.stateServiceProvider.popCurrentStateService();
114-
return sortedTxs;
106+
return await this.tracer.trace("mempool.get_txs", async () => {
107+
const txs = await this.transactionStorage.getPendingUserTransactions();
108+
109+
const baseCachedStateService = new CachedStateService(this.stateService);
110+
111+
const networkState =
112+
(await this.getStagedNetworkState()) ?? NetworkState.empty();
113+
114+
const sortedTxs = await this.tracer.trace(
115+
"mempool.validate_txs",
116+
async () =>
117+
await this.checkTxValid(
118+
txs,
119+
baseCachedStateService,
120+
this.protocol.stateServiceProvider,
121+
networkState,
122+
limit
123+
)
124+
);
125+
126+
this.protocol.stateServiceProvider.popCurrentStateService();
127+
return sortedTxs;
128+
});
115129
}
116130

117131
// We iterate through the transactions. For each tx we run the account state hook.

packages/sequencer/src/protocol/production/BatchProducerModule.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { CachedMerkleTreeStore } from "../../state/merkle/CachedMerkleTreeStore"
1717
import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore";
1818
import { BlockWithResult } from "../../storage/model/Block";
1919
import type { Database } from "../../storage/Database";
20+
import { Tracer } from "../../logging/Tracer";
2021

2122
import { BlockProofSerializer } from "./tasks/serializers/BlockProofSerializer";
2223
import { BatchTracingService } from "./tracing/BatchTracingService";
@@ -52,6 +53,8 @@ export class BatchProducerModule extends SequencerModule {
5253
@inject("BatchStorage") private readonly batchStorage: BatchStorage,
5354
@inject("Database")
5455
private readonly database: Database,
56+
@inject("Tracer")
57+
private readonly tracer: Tracer,
5558
private readonly batchFlow: BatchFlow,
5659
private readonly blockProofSerializer: BlockProofSerializer,
5760
private readonly batchTraceService: BatchTracingService

packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "../../../storage/model/Block";
2626
import { MessageStorage } from "../../../storage/repositories/MessageStorage";
2727
import { Database } from "../../../storage/Database";
28+
import { Tracer } from "../../../logging/Tracer";
2829

2930
import { BlockProductionService } from "./BlockProductionService";
3031
import { BlockResultService } from "./BlockResultService";
@@ -54,7 +55,8 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
5455
@inject("MethodIdResolver")
5556
private readonly methodIdResolver: MethodIdResolver,
5657
@inject("Runtime") private readonly runtime: Runtime<RuntimeModulesRecord>,
57-
@inject("Database") private readonly database: Database
58+
@inject("Database") private readonly database: Database,
59+
@inject("Tracer") public readonly tracer: Tracer
5860
) {
5961
super();
6062
}
@@ -107,6 +109,10 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
107109
}
108110

109111
public async generateMetadata(block: Block): Promise<BlockResult> {
112+
const traceMetadata = {
113+
height: block.height.toString(),
114+
};
115+
110116
const { result, blockHashTreeStore, treeStore, stateService } =
111117
await this.resultService.generateMetadataForNextBlock(
112118
block,
@@ -129,7 +135,10 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
129135
public async tryProduceBlock(): Promise<Block | undefined> {
130136
if (!this.productionInProgress) {
131137
try {
132-
const block = await this.produceBlock();
138+
const block = await this.tracer.trace(
139+
"block",
140+
async () => await this.produceBlock()
141+
);
133142

134143
if (block === undefined) {
135144
if (!this.allowEmptyBlock()) {
@@ -204,7 +213,10 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
204213
private async produceBlock(): Promise<Block | undefined> {
205214
this.productionInProgress = true;
206215

207-
const { txs, metadata } = await this.collectProductionData();
216+
const { txs, metadata } = await this.tracer.trace(
217+
"block.collect_inputs",
218+
async () => await this.collectProductionData()
219+
);
208220

209221
// Skip production if no transactions are available for now
210222
if (txs.length === 0 && !this.allowEmptyBlock()) {
@@ -221,10 +233,18 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
221233
if (blockResult !== undefined) {
222234
const { block, stateChanges } = blockResult;
223235

224-
await this.database.executeInTransaction(async () => {
225-
await stateChanges.mergeIntoParent();
226-
await this.blockQueue.pushBlock(block);
227-
});
236+
await this.tracer.trace(
237+
"block.commit",
238+
async () =>
239+
// Push changes to the database atomically
240+
await this.database.executeInTransaction(async () => {
241+
await stateChanges.mergeIntoParent();
242+
await this.blockQueue.pushBlock(block);
243+
}),
244+
{
245+
height: block.height.toString(),
246+
}
247+
);
228248
}
229249

230250
this.productionInProgress = false;

packages/sequencer/src/protocol/production/sequencing/BlockProductionService.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
executeWithExecutionContext,
3232
TransactionExecutionService,
3333
} from "./TransactionExecutionService";
34+
import { Tracer } from "../../../logging/Tracer";
3435

3536
@injectable()
3637
@scoped(Lifecycle.ContainerScoped)
@@ -40,6 +41,8 @@ export class BlockProductionService {
4041
public constructor(
4142
@inject("Protocol")
4243
protocol: Protocol<MandatoryProtocolModulesRecord & ProtocolModulesRecord>,
44+
@inject("Tracer")
45+
private readonly tracer: Tracer,
4346
private readonly transactionExecutionService: TransactionExecutionService,
4447
@inject("StateServiceProvider")
4548
private readonly stateServiceProvider: StateServiceProvider

packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface TimedBlockTriggerEvent extends BlockEvents {
3434
@closeable()
3535
export class TimedBlockTrigger
3636
extends BlockTriggerBase<TimedBlockTriggerConfig, TimedBlockTriggerEvent>
37-
implements BlockTrigger, Closeable
37+
implements Closeable
3838
{
3939
// There is no real type for interval ids somehow, so any it is
4040

@@ -122,6 +122,7 @@ export class TimedBlockTrigger
122122
}
123123

124124
private async produceUnprovenBlock() {
125+
// TODO Optimize towards mempool.length()
125126
const mempoolTxs = await this.mempool.getTxs();
126127
// Produce a block if either produceEmptyBlocks is true or we have more
127128
// than 1 tx in mempool

0 commit comments

Comments
 (0)