Skip to content

Commit 332d51f

Browse files
committed
Merge branch 'develop' into feature/static-dependency-factory
# Conflicts: # packages/sequencer/src/mempool/private/PrivateMempool.ts
2 parents 1cede08 + 89b76c9 commit 332d51f

File tree

5 files changed

+69
-35
lines changed

5 files changed

+69
-35
lines changed

packages/persistance/src/services/prisma/mappers/EventMapper.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,53 @@ import { Field } from "o1js";
44

55
import { ObjectMapper } from "../../../ObjectMapper";
66

7+
type EventData = {
8+
eventName: string;
9+
data: Field[];
10+
source: "afterTxHook" | "beforeTxHook" | "runtime";
11+
};
12+
713
@singleton()
8-
export class EventMapper
9-
implements
10-
ObjectMapper<{ eventName: string; data: Field[] }, Prisma.JsonObject>
11-
{
12-
public mapIn(input: Prisma.JsonObject): { eventName: string; data: Field[] } {
13-
if (input === undefined) return { eventName: "", data: [] };
14+
export class EventMapper implements ObjectMapper<EventData, Prisma.JsonObject> {
15+
public mapIn(input: Prisma.JsonObject): EventData {
1416
return {
1517
eventName: input.eventName as string,
1618
data: (input.data as Prisma.JsonArray).map((field) =>
1719
Field.fromJSON(field as string)
1820
),
21+
source: this.sourceConvert(input.source as string),
1922
};
2023
}
2124

22-
public mapOut(input: {
23-
eventName: string;
24-
data: Field[];
25-
}): Prisma.JsonObject {
25+
public mapOut(input: EventData): Prisma.JsonObject {
2626
return {
2727
eventName: input.eventName,
2828
data: input.data.map((field) => field.toString()),
29+
source: input.source,
2930
} as Prisma.JsonObject;
3031
}
32+
33+
private sourceConvert(input: string) {
34+
if (
35+
input === "beforeTxHook" ||
36+
input === "afterTxHook" ||
37+
input === "runtime"
38+
) {
39+
return input;
40+
}
41+
throw new Error(
42+
"Event Source must be one of 'beforeTxHook', 'afterTxHook' or 'runtime'"
43+
);
44+
}
3145
}
3246

3347
@singleton()
3448
export class EventArrayMapper
35-
implements
36-
ObjectMapper<
37-
{ eventName: string; data: Field[] }[],
38-
Prisma.JsonValue | undefined
39-
>
49+
implements ObjectMapper<EventData[], Prisma.JsonValue | undefined>
4050
{
4151
public constructor(private readonly eventMapper: EventMapper) {}
4252

43-
public mapIn(
44-
input: Prisma.JsonValue | undefined
45-
): { eventName: string; data: Field[] }[] {
53+
public mapIn(input: Prisma.JsonValue | undefined): EventData[] {
4654
if (input === undefined) return [];
4755

4856
if (Array.isArray(input)) {
@@ -53,9 +61,7 @@ export class EventArrayMapper
5361
return [];
5462
}
5563

56-
public mapOut(
57-
input: { eventName: string; data: Field[] }[]
58-
): Prisma.JsonValue {
64+
public mapOut(input: EventData[]): Prisma.JsonValue {
5965
return input.map((event) =>
6066
this.eventMapper.mapOut(event)
6167
) as Prisma.JsonArray;

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ type MempoolTransactionPaths = {
3737
paths: Field[];
3838
};
3939

40+
interface PrivateMempoolConfig {
41+
validationEnabled?: boolean;
42+
}
43+
4044
@sequencerModule()
41-
export class PrivateMempool extends SequencerModule implements Mempool {
45+
export class PrivateMempool
46+
extends SequencerModule<PrivateMempoolConfig>
47+
implements Mempool
48+
{
4249
public readonly events = new EventEmitter<MempoolEvents>();
4350

4451
private readonly accountStateHook: AccountStateHook;
@@ -113,13 +120,16 @@ export class PrivateMempool extends SequencerModule implements Mempool {
113120
const networkState =
114121
(await this.getStagedNetworkState()) ?? NetworkState.empty();
115122

116-
const sortedTxs = await this.checkTxValid(
117-
txs,
118-
baseCachedStateService,
119-
this.protocol.stateServiceProvider,
120-
networkState,
121-
limit
122-
);
123+
const validationEnabled = this.config.validationEnabled ?? true;
124+
const sortedTxs = validationEnabled
125+
? await this.checkTxValid(
126+
txs,
127+
baseCachedStateService,
128+
this.protocol.stateServiceProvider,
129+
networkState,
130+
limit
131+
)
132+
: txs;
123133

124134
this.protocol.stateServiceProvider.popCurrentStateService();
125135
return sortedTxs;

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,32 @@ async function decodeTransaction(
113113
}
114114

115115
function extractEvents(
116-
runtimeResult: RuntimeContextReducedExecutionResult
117-
): { eventName: string; data: Field[] }[] {
116+
runtimeResult: RuntimeContextReducedExecutionResult,
117+
source: "afterTxHook" | "beforeTxHook" | "runtime"
118+
): {
119+
eventName: string;
120+
data: Field[];
121+
source: "afterTxHook" | "beforeTxHook" | "runtime";
122+
}[] {
118123
return runtimeResult.events.reduce(
119124
(acc, event) => {
120125
if (event.condition.toBoolean()) {
121126
const obj = {
122127
eventName: event.eventName,
123128
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
124129
data: event.eventType.toFields(event.event),
130+
source: source,
125131
};
126132
acc.push(obj);
127133
}
128134
return acc;
129135
},
130136
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
131-
[] as { eventName: string; data: Field[] }[]
137+
[] as {
138+
eventName: string;
139+
data: Field[];
140+
source: "afterTxHook" | "beforeTxHook" | "runtime";
141+
}[]
132142
);
133143
}
134144

@@ -323,6 +333,7 @@ export class TransactionExecutionService {
323333
async (hook, hookArgs) => await hook.beforeTransaction(hookArgs),
324334
"beforeTx"
325335
);
336+
const beforeHookEvents = extractEvents(beforeTxHookResult, "beforeTxHook");
326337

327338
await recordingStateService.applyStateTransitions(
328339
beforeTxHookResult.stateTransitions
@@ -372,6 +383,7 @@ export class TransactionExecutionService {
372383
async (hook, hookArgs) => await hook.afterTransaction(hookArgs),
373384
"afterTx"
374385
);
386+
const afterHookEvents = extractEvents(afterTxHookResult, "afterTxHook");
375387
await recordingStateService.applyStateTransitions(
376388
afterTxHookResult.stateTransitions
377389
);
@@ -385,7 +397,7 @@ export class TransactionExecutionService {
385397
appChain.setProofsEnabled(previousProofsEnabled);
386398

387399
// Extract sequencing results
388-
const events = extractEvents(runtimeResult);
400+
const runtimeResultEvents = extractEvents(runtimeResult, "runtime");
389401
const stateTransitions = this.buildSTBatches(
390402
[
391403
beforeTxHookResult.stateTransitions,
@@ -403,7 +415,7 @@ export class TransactionExecutionService {
403415
statusMessage: runtimeResult.statusMessage,
404416

405417
stateTransitions,
406-
events,
418+
events: beforeHookEvents.concat(runtimeResultEvents, afterHookEvents),
407419
},
408420
];
409421
}

packages/sequencer/src/storage/model/Block.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export interface TransactionExecutionResult {
2020
stateTransitions: StateTransitionBatch[];
2121
status: Bool;
2222
statusMessage?: string;
23-
events: { eventName: string; data: Field[] }[];
23+
events: {
24+
eventName: string;
25+
data: Field[];
26+
source: "afterTxHook" | "beforeTxHook" | "runtime";
27+
}[];
2428
}
2529

2630
// TODO Why is Block using Fields, but BlockResult bigints? Align that towards the best option

packages/sequencer/test/integration/BlockProduction.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,13 @@ describe("block production", () => {
687687
const firstEventReduced = {
688688
eventName: firstExpectedEvent.eventName,
689689
data: firstExpectedEvent.eventType.toFields(firstExpectedEvent.event),
690+
source: "runtime",
690691
};
691692

692693
const secondEventReduced = {
693694
eventName: secondExpectedEvent.eventName,
694695
data: secondExpectedEvent.eventType.toFields(secondExpectedEvent.event),
696+
source: "runtime",
695697
};
696698

697699
const block = await test.produceBlock();

0 commit comments

Comments
 (0)