Skip to content

Commit ea17cbb

Browse files
committed
Update mappers and make type narrow
1 parent 4a0d34d commit ea17cbb

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

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

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,74 @@ import { ObjectMapper } from "../../../ObjectMapper";
77
@singleton()
88
export class EventMapper
99
implements
10-
ObjectMapper<{ eventName: string; data: Field[] }, Prisma.JsonObject>
10+
ObjectMapper<
11+
{
12+
eventName: string;
13+
data: Field[];
14+
source: "afterTxHook" | "beforeTxHook" | "runtime";
15+
},
16+
Prisma.JsonObject
17+
>
1118
{
12-
public mapIn(input: Prisma.JsonObject): { eventName: string; data: Field[] } {
13-
if (input === undefined) return { eventName: "", data: [] };
19+
public mapIn(input: Prisma.JsonObject): {
20+
eventName: string;
21+
data: Field[];
22+
source: "afterTxHook" | "beforeTxHook" | "runtime";
23+
} {
24+
if (input === undefined)
25+
return { eventName: "", data: [], source: "runtime" };
1426
return {
1527
eventName: input.eventName as string,
1628
data: (input.data as Prisma.JsonArray).map((field) =>
1729
Field.fromJSON(field as string)
1830
),
31+
source: this.sourceConvert(input.source as string),
1932
};
2033
}
2134

2235
public mapOut(input: {
2336
eventName: string;
2437
data: Field[];
38+
source: "afterTxHook" | "beforeTxHook" | "runtime";
2539
}): Prisma.JsonObject {
2640
return {
2741
eventName: input.eventName,
2842
data: input.data.map((field) => field.toString()),
43+
source: input.source,
2944
} as Prisma.JsonObject;
3045
}
46+
47+
private sourceConvert(input: string) {
48+
if (
49+
input === "beforeTxHook" ||
50+
input === "afterTxHook" ||
51+
input === "runtime"
52+
) {
53+
return input;
54+
}
55+
throw new Error();
56+
}
3157
}
3258

3359
@singleton()
3460
export class EventArrayMapper
3561
implements
3662
ObjectMapper<
37-
{ eventName: string; data: Field[] }[],
63+
{
64+
eventName: string;
65+
data: Field[];
66+
source: "afterTxHook" | "beforeTxHook" | "runtime";
67+
}[],
3868
Prisma.JsonValue | undefined
3969
>
4070
{
4171
public constructor(private readonly eventMapper: EventMapper) {}
4272

43-
public mapIn(
44-
input: Prisma.JsonValue | undefined
45-
): { eventName: string; data: Field[] }[] {
73+
public mapIn(input: Prisma.JsonValue | undefined): {
74+
eventName: string;
75+
data: Field[];
76+
source: "afterTxHook" | "beforeTxHook" | "runtime";
77+
}[] {
4678
if (input === undefined) return [];
4779

4880
if (Array.isArray(input)) {
@@ -54,7 +86,11 @@ export class EventArrayMapper
5486
}
5587

5688
public mapOut(
57-
input: { eventName: string; data: Field[] }[]
89+
input: {
90+
eventName: string;
91+
data: Field[];
92+
source: "afterTxHook" | "beforeTxHook" | "runtime";
93+
}[]
5894
): Prisma.JsonValue {
5995
return input.map((event) =>
6096
this.eventMapper.mapOut(event)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ async function decodeTransaction(
112112

113113
function extractEvents(
114114
runtimeResult: RuntimeContextReducedExecutionResult,
115-
source: string
116-
): { eventName: string; data: Field[] }[] {
115+
source: "afterTxHook" | "beforeTxHook" | "runtime"
116+
): {
117+
eventName: string;
118+
data: Field[];
119+
source: "afterTxHook" | "beforeTxHook" | "runtime";
120+
}[] {
117121
return runtimeResult.events.reduce(
118122
(acc, event) => {
119123
if (event.condition.toBoolean()) {
@@ -128,7 +132,11 @@ function extractEvents(
128132
return acc;
129133
},
130134
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
131-
[] as { eventName: string; data: Field[]; source: string }[]
135+
[] as {
136+
eventName: string;
137+
data: Field[];
138+
source: "afterTxHook" | "beforeTxHook" | "runtime";
139+
}[]
132140
);
133141
}
134142

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

0 commit comments

Comments
 (0)