Skip to content

Commit 6bf0741

Browse files
committed
Functional refactor of block production
1 parent a40d09e commit 6bf0741

File tree

5 files changed

+162
-147
lines changed

5 files changed

+162
-147
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { Block, BlockWithResult } from "../../../storage/model/Block";
2222
import { CachedStateService } from "../../../state/state/CachedStateService";
2323
import { MessageStorage } from "../../../storage/repositories/MessageStorage";
2424

25-
import { TransactionExecutionService } from "./TransactionExecutionService";
2625
import { BlockProductionService } from "./BlockProductionService";
2726
import { BlockResultService } from "./BlockResultService";
2827

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "../../../storage/model/Block";
2020
import { CachedStateService } from "../../../state/state/CachedStateService";
2121
import { PendingTransaction } from "../../../mempool/PendingTransaction";
22+
2223
import { TransactionExecutionService } from "./TransactionExecutionService";
2324

2425
@injectable()

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { StateRecord } from "../BatchProducerModule";
21
import { Bool, Field, Poseidon } from "o1js";
32
import { RollupMerkleTree } from "@proto-kit/common";
43
import {
@@ -10,16 +9,21 @@ import {
109
Protocol,
1110
ProtocolModulesRecord,
1211
ProvableBlockHook,
13-
reduceStateTransitions,
14-
RuntimeMethodExecutionContext,
1512
RuntimeTransaction,
1613
} from "@proto-kit/protocol";
1714
import { inject, injectable, Lifecycle, scoped } from "tsyringe";
1815

19-
import { Block, BlockResult } from "../../../storage/model/Block";
16+
import {
17+
Block,
18+
BlockResult,
19+
TransactionExecutionResult,
20+
} from "../../../storage/model/Block";
2021
import { AsyncMerkleTreeStore } from "../../../state/async/AsyncMerkleTreeStore";
2122
import { CachedMerkleTreeStore } from "../../../state/merkle/CachedMerkleTreeStore";
2223
import { UntypedStateTransition } from "../helpers/UntypedStateTransition";
24+
import type { StateRecord } from "../BatchProducerModule";
25+
26+
import { executeWithExecutionContext } from "./TransactionExecutionService";
2327

2428
function collectStateDiff(
2529
stateTransitions: UntypedStateTransition[]
@@ -35,13 +39,27 @@ function collectStateDiff(
3539
);
3640
}
3741

42+
function createCombinedStateDiff(transactions: TransactionExecutionResult[]) {
43+
// Flatten diff list into a single diff by applying them over each other
44+
return transactions
45+
.map((tx) => {
46+
const transitions = tx.protocolTransitions.concat(
47+
tx.status.toBoolean() ? tx.stateTransitions : []
48+
);
49+
return collectStateDiff(transitions);
50+
})
51+
.reduce<StateRecord>((accumulator, diff) => {
52+
// accumulator properties will be overwritten by diff's values
53+
return Object.assign(accumulator, diff);
54+
}, {});
55+
}
56+
3857
@injectable()
3958
@scoped(Lifecycle.ContainerScoped)
4059
export class BlockResultService {
4160
private readonly blockHooks: ProvableBlockHook<unknown>[];
4261

4362
public constructor(
44-
private readonly executionContext: RuntimeMethodExecutionContext,
4563
@inject("Protocol")
4664
protocol: Protocol<MandatoryProtocolModulesRecord & ProtocolModulesRecord>
4765
) {
@@ -55,18 +73,7 @@ export class BlockResultService {
5573
blockHashTreeStore: AsyncMerkleTreeStore,
5674
modifyTreeStore = true
5775
): Promise<BlockResult> {
58-
// Flatten diff list into a single diff by applying them over each other
59-
const combinedDiff = block.transactions
60-
.map((tx) => {
61-
const transitions = tx.protocolTransitions.concat(
62-
tx.status.toBoolean() ? tx.stateTransitions : []
63-
);
64-
return collectStateDiff(transitions);
65-
})
66-
.reduce<StateRecord>((accumulator, diff) => {
67-
// accumulator properties will be overwritten by diff's values
68-
return Object.assign(accumulator, diff);
69-
}, {});
76+
const combinedDiff = createCombinedStateDiff(block.transactions);
7077

7178
const inMemoryStore = new CachedMerkleTreeStore(merkleTreeStore);
7279
const tree = new RollupMerkleTree(inMemoryStore);
@@ -104,23 +111,22 @@ export class BlockResultService {
104111
};
105112

106113
// TODO Set StateProvider for @state access to state
107-
this.executionContext.clear();
108-
this.executionContext.setup({
114+
const context = {
109115
networkState: block.networkState.during,
110116
transaction: RuntimeTransaction.dummyTransaction(),
111-
});
117+
};
112118

113-
const resultingNetworkState = await this.blockHooks.reduce<
114-
Promise<NetworkState>
115-
>(
116-
async (networkState, hook) =>
117-
await hook.afterBlock(await networkState, state),
118-
Promise.resolve(block.networkState.during)
119+
const executionResult = await executeWithExecutionContext(
120+
async () =>
121+
await this.blockHooks.reduce<Promise<NetworkState>>(
122+
async (networkState, hook) =>
123+
await hook.afterBlock(await networkState, state),
124+
Promise.resolve(block.networkState.during)
125+
),
126+
context
119127
);
120128

121-
const { stateTransitions } = this.executionContext.result;
122-
this.executionContext.clear();
123-
const reducedStateTransitions = reduceStateTransitions(stateTransitions);
129+
const { stateTransitions, methodResult } = executionResult;
124130

125131
// Update the block hash tree with this block
126132
blockHashTree.setLeaf(
@@ -139,12 +145,12 @@ export class BlockResultService {
139145
}
140146

141147
return {
142-
afterNetworkState: resultingNetworkState,
148+
afterNetworkState: methodResult,
143149
stateRoot: stateRoot.toBigInt(),
144150
blockHashRoot: newBlockHashRoot.toBigInt(),
145151
blockHashWitness,
146152

147-
blockStateTransitions: reducedStateTransitions.map((st) =>
153+
blockStateTransitions: stateTransitions.map((st) =>
148154
UntypedStateTransition.fromStateTransition(st)
149155
),
150156
blockHash: block.hash.toBigInt(),

0 commit comments

Comments
 (0)