Skip to content

Commit f9bccd3

Browse files
committed
Integrated tracing into new block proving pipeline
1 parent 84db441 commit f9bccd3

File tree

14 files changed

+94
-47
lines changed

14 files changed

+94
-47
lines changed

packages/sequencer/src/logging/trace.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
12
import snakeCase from "lodash/snakeCase";
23

34
import { Tracer } from "./Tracer";
@@ -6,7 +7,7 @@ type Attributes = Record<string, string | number | boolean>;
67

78
export function trace<P extends any[], R>(
89
name?: string,
9-
metadata?: ((p: P /*, r: R*/) => Attributes) | Attributes
10+
metadata?: ((p: P) => Attributes) | Attributes
1011
) {
1112
return (
1213
target: { tracer: Tracer },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export class PrivateMempool extends SequencerModule implements Mempool {
131131
// paths are shared between the just succeeded tx and any of the skipped txs. This is
132132
// because a failed tx may succeed now if the failure was to do with a nonce issue, say.
133133
// TODO Refactor
134-
// eslint-disable-next-line sonarjs/cognitive-complexity
135134
@trace("mempool.validate_txs")
135+
// eslint-disable-next-line sonarjs/cognitive-complexity
136136
private async checkTxValid(
137137
transactions: PendingTransaction[],
138138
baseService: CachedStateService,

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ 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";
2120

2221
import { BlockProofSerializer } from "./tasks/serializers/BlockProofSerializer";
2322
import { BatchTracingService } from "./tracing/BatchTracingService";
2423
import { BatchFlow } from "./flow/BatchFlow";
25-
import { trace } from "../../logging/trace";
2624

2725
export type StateRecord = Record<string, Field[] | undefined>;
2826

@@ -54,8 +52,6 @@ export class BatchProducerModule extends SequencerModule {
5452
@inject("BatchStorage") private readonly batchStorage: BatchStorage,
5553
@inject("Database")
5654
private readonly database: Database,
57-
@inject("Tracer")
58-
public readonly tracer: Tracer,
5955
private readonly batchFlow: BatchFlow,
6056
private readonly blockProofSerializer: BlockProofSerializer,
6157
private readonly batchTraceService: BatchTracingService
@@ -139,7 +135,6 @@ export class BatchProducerModule extends SequencerModule {
139135
noop();
140136
}
141137

142-
@trace("batch")
143138
private async produceBatch(
144139
blocks: BlockWithResult[],
145140
height: number
@@ -175,12 +170,12 @@ export class BatchProducerModule extends SequencerModule {
175170
*
176171
*
177172
* @param blocks
178-
* @param blockId
173+
* @param batchId
179174
* @private
180175
*/
181176
private async computeBatch(
182177
blocks: BlockWithResult[],
183-
blockId: number
178+
batchId: number
184179
): Promise<{
185180
proof: Proof<BlockProverPublicInput, BlockProverPublicOutput>;
186181
changes: CachedMerkleTreeStore;
@@ -195,10 +190,11 @@ export class BatchProducerModule extends SequencerModule {
195190

196191
const trace = await this.batchTraceService.traceBatch(
197192
blocks.map((block) => block),
198-
merkleTreeStore
193+
merkleTreeStore,
194+
batchId
199195
);
200196

201-
const proof = await this.batchFlow.executeBatch(trace, blockId);
197+
const proof = await this.batchFlow.executeBatch(trace, batchId);
202198

203199
const fromNetworkState = blocks[0].block.networkState.before;
204200
const toNetworkState = blocks.at(-1)!.result.afterNetworkState;

packages/sequencer/src/protocol/production/flow/BatchFlow.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { FlowCreator } from "../../../worker/flow/Flow";
1818
import { NewBlockProvingParameters, NewBlockTask } from "../tasks/NewBlockTask";
1919
import { BlockReductionTask } from "../tasks/BlockReductionTask";
2020
import { BatchTrace } from "../tracing/BatchTracingService";
21+
import { Tracer } from "../../../logging/Tracer";
22+
import { trace } from "../../../logging/trace";
2123

2224
import { ReductionTaskFlow } from "./ReductionTaskFlow";
2325
import { StateTransitionFlow } from "./StateTransitionFlow";
@@ -33,7 +35,9 @@ export class BatchFlow {
3335
private readonly stateTransitionFlow: StateTransitionFlow,
3436
private readonly blockFlow: BlockFlow,
3537
@inject("Protocol")
36-
private readonly protocol: Protocol<MandatoryProtocolModulesRecord>
38+
private readonly protocol: Protocol<MandatoryProtocolModulesRecord>,
39+
@inject("Tracer")
40+
public readonly tracer: Tracer
3741
) {}
3842

3943
private isBlockProofsMergable(a: BlockProof, b: BlockProof): boolean {
@@ -88,6 +92,7 @@ export class BatchFlow {
8892
);
8993
}
9094

95+
@trace("batch.prove", ([, batchId]) => ({ batchId }))
9196
public async executeBatch(batch: BatchTrace, batchId: number) {
9297
const batchFlow = new ReductionTaskFlow(
9398
{
@@ -104,9 +109,13 @@ export class BatchFlow {
104109
number,
105110
Nullable<NewBlockProvingParameters>
106111
> = Object.fromEntries(
107-
batch.blocks.map((trace, i) => [
112+
batch.blocks.map((blockTrace, i) => [
108113
i,
109-
{ params: trace.blockParams, input1: undefined, input2: undefined },
114+
{
115+
params: blockTrace.blockParams,
116+
input1: undefined,
117+
input2: undefined,
118+
},
110119
])
111120
);
112121

@@ -125,8 +134,8 @@ export class BatchFlow {
125134
}
126135
);
127136

128-
await mapSequential(batch.blocks, async (trace, blockIndex) => {
129-
await this.blockFlow.executeBlock(trace, async (proof) => {
137+
await mapSequential(batch.blocks, async (blockTrace, blockIndex) => {
138+
await this.blockFlow.executeBlock(blockTrace, async (proof) => {
130139
map[blockIndex].input2 = proof;
131140
await this.pushBlockInput(map[blockIndex], batchFlow);
132141
});

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import {
2626
import { MessageStorage } from "../../../storage/repositories/MessageStorage";
2727
import { Database } from "../../../storage/Database";
2828
import { Tracer } from "../../../logging/Tracer";
29+
import { trace } from "../../../logging/trace";
2930

3031
import { BlockProductionService } from "./BlockProductionService";
3132
import { BlockResultService } from "./BlockResultService";
32-
import { trace } from "../../../logging/trace";
3333

3434
export interface BlockConfig {
3535
allowEmptyBlock?: boolean;
@@ -109,7 +109,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
109109
}
110110
}
111111

112-
@trace("block.metadata", ([block]) => ({ height: block.height.toString() }))
112+
@trace("block.result", ([block]) => ({ height: block.height.toString() }))
113113
public async generateMetadata(block: Block): Promise<BlockResult> {
114114
const traceMetadata = {
115115
height: block.height.toString(),
@@ -123,13 +123,18 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
123123
this.unprovenStateService
124124
);
125125

126-
await this.database.executeInTransaction(async () => {
127-
await blockHashTreeStore.mergeIntoParent();
128-
await treeStore.mergeIntoParent();
129-
await stateService.mergeIntoParent();
130-
131-
await this.blockQueue.pushResult(result);
132-
});
126+
await this.tracer.trace(
127+
"block.result.commit",
128+
async () =>
129+
await this.database.executeInTransaction(async () => {
130+
await blockHashTreeStore.mergeIntoParent();
131+
await treeStore.mergeIntoParent();
132+
await stateService.mergeIntoParent();
133+
134+
await this.blockQueue.pushResult(result);
135+
}),
136+
traceMetadata
137+
);
133138

134139
return result;
135140
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ import { CachedStateService } from "../../../state/state/CachedStateService";
2525
import { PendingTransaction } from "../../../mempool/PendingTransaction";
2626
import { AsyncStateService } from "../../../state/async/AsyncStateService";
2727
import { UntypedStateTransition } from "../helpers/UntypedStateTransition";
28+
import { Tracer } from "../../../logging/Tracer";
29+
import { trace } from "../../../logging/trace";
2830

2931
import {
3032
BlockTrackers,
3133
executeWithExecutionContext,
3234
TransactionExecutionService,
3335
} from "./TransactionExecutionService";
34-
import { Tracer } from "../../../logging/Tracer";
3536

3637
@injectable()
3738
@scoped(Lifecycle.ContainerScoped)
@@ -42,7 +43,7 @@ export class BlockProductionService {
4243
@inject("Protocol")
4344
protocol: Protocol<MandatoryProtocolModulesRecord & ProtocolModulesRecord>,
4445
@inject("Tracer")
45-
private readonly tracer: Tracer,
46+
public readonly tracer: Tracer,
4647
private readonly transactionExecutionService: TransactionExecutionService,
4748
@inject("StateServiceProvider")
4849
private readonly stateServiceProvider: StateServiceProvider
@@ -51,6 +52,7 @@ export class BlockProductionService {
5152
protocol.dependencyContainer.resolveAll("ProvableBlockHook");
5253
}
5354

55+
@trace("block.hook.before")
5456
public async executeBeforeBlockHook(
5557
args: BeforeBlockHookArguments,
5658
inputNetworkState: NetworkState,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { UntypedStateTransition } from "../helpers/UntypedStateTransition";
2525
import { CachedStateService } from "../../../state/state/CachedStateService";
2626
import { AsyncStateService } from "../../../state/async/AsyncStateService";
2727
import type { StateRecord } from "../BatchProducerModule";
28+
import { trace } from "../../../logging/trace";
29+
import { Tracer } from "../../../logging/Tracer";
2830

2931
import { executeWithExecutionContext } from "./TransactionExecutionService";
3032

@@ -72,12 +74,15 @@ export class BlockResultService {
7274
@inject("Protocol")
7375
protocol: Protocol<MandatoryProtocolModulesRecord & ProtocolModulesRecord>,
7476
@inject("StateServiceProvider")
75-
private readonly stateServiceProvider: StateServiceProvider
77+
private readonly stateServiceProvider: StateServiceProvider,
78+
@inject("Tracer")
79+
public readonly tracer: Tracer
7680
) {
7781
this.blockHooks =
7882
protocol.dependencyContainer.resolveAll("ProvableBlockHook");
7983
}
8084

85+
@trace("block.hook.after")
8186
public async executeAfterBlockHook(
8287
args: AfterBlockHookArguments,
8388
inputNetworkState: NetworkState,
@@ -169,6 +174,9 @@ export class BlockResultService {
169174
return tree;
170175
}
171176

177+
@trace("block.result.generate", ([block]) => ({
178+
height: block.height.toString(),
179+
}))
172180
public async generateMetadataForNextBlock(
173181
block: Block,
174182
merkleTreeStore: AsyncMerkleTreeStore,

packages/sequencer/src/protocol/production/tracing/BatchTracingService.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {
66
WitnessedRootHashList,
77
} from "@proto-kit/protocol";
88
import { Field } from "o1js";
9-
import { injectable } from "tsyringe";
9+
import { inject, injectable } from "tsyringe";
1010

1111
import { CachedMerkleTreeStore } from "../../../state/merkle/CachedMerkleTreeStore";
1212
import { StateTransitionProofParameters } from "../tasks/StateTransitionTask";
1313
import { BlockWithResult } from "../../../storage/model/Block";
14+
import { trace } from "../../../logging/trace";
15+
import { Tracer } from "../../../logging/Tracer";
1416

1517
import {
1618
BlockTrace,
@@ -30,7 +32,9 @@ export type BatchTrace = {
3032
export class BatchTracingService {
3133
public constructor(
3234
private readonly blockTracingService: BlockTracingService,
33-
private readonly stateTransitionTracingService: StateTransitionTracingService
35+
private readonly stateTransitionTracingService: StateTransitionTracingService,
36+
@inject("Tracer")
37+
public readonly tracer: Tracer
3438
) {}
3539

3640
private createBatchState(block: BlockWithResult): BatchTracingState {
@@ -46,6 +50,7 @@ export class BatchTracingService {
4650
};
4751
}
4852

53+
@trace("batch.trace.blocks")
4954
public async traceBlocks(blocks: BlockWithResult[]) {
5055
const batchState = this.createBatchState(blocks[0]);
5156

@@ -58,34 +63,42 @@ export class BatchTracingService {
5863
...state,
5964
transactionList: new TransactionHashList(),
6065
};
61-
const [newState, trace] = await this.blockTracingService.traceBlock(
62-
blockProverState,
63-
block,
64-
index === numBlocks - 1
65-
);
66-
return [newState, trace];
66+
const [newState, blockTrace] =
67+
await this.blockTracingService.traceBlock(
68+
blockProverState,
69+
block,
70+
index === numBlocks - 1
71+
);
72+
return [newState, blockTrace];
6773
},
6874
batchState
6975
);
7076

7177
return blockTraces;
7278
}
7379

80+
@trace("batch.trace.transitions")
7481
public async traceStateTransitions(
7582
blocks: BlockWithResult[],
7683
merkleTreeStore: CachedMerkleTreeStore
7784
) {
78-
const batches = this.stateTransitionTracingService.extractSTBatches(blocks);
85+
const batches = await this.tracer.trace(
86+
"batch.trace.transitions.encoding",
87+
async () => this.stateTransitionTracingService.extractSTBatches(blocks)
88+
);
7989

8090
return await this.stateTransitionTracingService.createMerkleTrace(
8191
merkleTreeStore,
8292
batches
8393
);
8494
}
8595

96+
@trace("batch.trace", ([, , batchId]) => ({ batchId }))
8697
public async traceBatch(
8798
blocks: BlockWithResult[],
88-
merkleTreeStore: CachedMerkleTreeStore
99+
merkleTreeStore: CachedMerkleTreeStore,
100+
// Only for trace metadata
101+
batchId: number
89102
): Promise<BatchTrace> {
90103
if (blocks.length === 0) {
91104
return { blocks: [], stateTransitionTrace: [] };

packages/sequencer/src/protocol/production/tracing/BlockTracingService.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { toStateTransitionsHash } from "@proto-kit/module";
88
import { yieldSequential } from "@proto-kit/common";
99
// eslint-disable-next-line import/no-extraneous-dependencies
1010
import chunk from "lodash/chunk";
11-
import { injectable } from "tsyringe";
11+
import { inject, injectable } from "tsyringe";
1212

1313
import { BlockWithResult } from "../../../storage/model/Block";
1414
import type { NewBlockProverParameters } from "../tasks/NewBlockTask";
15+
import { Tracer } from "../../../logging/Tracer";
16+
import { trace } from "../../../logging/trace";
1517

1618
import {
1719
collectStartingState,
@@ -42,9 +44,14 @@ export type BlockTrace = {
4244
@injectable()
4345
export class BlockTracingService {
4446
public constructor(
45-
private readonly transactionTracing: TransactionTracingService
47+
private readonly transactionTracing: TransactionTracingService,
48+
@inject("Tracer")
49+
public readonly tracer: Tracer
4650
) {}
4751

52+
@trace("batch.trace.block", ([, block]) => ({
53+
height: block.block.height.toString(),
54+
}))
4855
public async traceBlock(
4956
state: BlockTracingState,
5057
block: BlockWithResult,
@@ -85,7 +92,7 @@ export class BlockTracingService {
8592
const [afterState, transactionTraces] = await yieldSequential(
8693
chunk(block.block.transactions, 2),
8794
async (input, [transaction1, transaction2]) => {
88-
const [output, trace] =
95+
const [output, transactionTrace] =
8996
transaction2 !== undefined
9097
? await this.transactionTracing.createMultiTransactionTrace(
9198
input,
@@ -97,7 +104,7 @@ export class BlockTracingService {
97104
transaction1
98105
);
99106

100-
return [output, trace];
107+
return [output, transactionTrace];
101108
},
102109
state
103110
);

0 commit comments

Comments
 (0)