Skip to content

Commit c774879

Browse files
committed
BlockResultsMapper
1 parent ebe7803 commit c774879

File tree

1 file changed

+63
-20
lines changed

1 file changed

+63
-20
lines changed
Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,100 @@
11
import { singleton } from "tsyringe";
22
import { BlockResult } from "@proto-kit/sequencer";
3-
import { BlockResult as DBBlockResult } from "@prisma/client";
3+
import {
4+
BlockResult as DBBlockResult,
5+
StateTransition as DBStateTransition,
6+
StateTransitionBatch as DBStateTransitionBatch,
7+
} from "@prisma/client";
48
import { BlockHashMerkleTreeWitness, NetworkState } from "@proto-kit/protocol";
59

610
import { ObjectMapper } from "../../../ObjectMapper";
711

8-
import { StateTransitionArrayMapper } from "./StateTransitionMapper";
12+
import { StateTransitionBatchArrayMapper } from "./StateTransitionMapper";
913

1014
@singleton()
1115
export class BlockResultMapper
12-
implements ObjectMapper<BlockResult, DBBlockResult>
16+
implements
17+
ObjectMapper<
18+
BlockResult,
19+
[
20+
DBBlockResult,
21+
[
22+
Omit<
23+
DBStateTransitionBatch,
24+
"txExecutionResultId" | "id" | "blockId" | "blockResultId"
25+
>,
26+
Omit<DBStateTransition, "batchId" | "id">[],
27+
][],
28+
]
29+
>
1330
{
1431
public constructor(
15-
private readonly stArrayMapper: StateTransitionArrayMapper
32+
private readonly stArrayMapper: StateTransitionBatchArrayMapper
1633
) {}
1734

18-
public mapIn(input: DBBlockResult): BlockResult {
35+
public mapIn(
36+
input: [
37+
DBBlockResult,
38+
[
39+
Omit<
40+
DBStateTransitionBatch,
41+
"txExecutionResultId" | "id" | "blockId" | "blockResultId"
42+
>,
43+
Omit<DBStateTransition, "batchId" | "id">[],
44+
][],
45+
]
46+
): BlockResult {
47+
const dbBlockResult = input[0];
48+
const stBatch = input[1];
1949
return {
2050
afterNetworkState: new NetworkState(
2151
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
22-
NetworkState.fromJSON(input.afterNetworkState as any)
52+
NetworkState.fromJSON(dbBlockResult.afterNetworkState as any)
2353
),
2454

25-
stateRoot: BigInt(input.stateRoot),
26-
blockHashRoot: BigInt(input.blockHashRoot),
55+
stateRoot: BigInt(dbBlockResult.stateRoot),
56+
blockHashRoot: BigInt(dbBlockResult.blockHashRoot),
2757
blockHashWitness: new BlockHashMerkleTreeWitness(
28-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
29-
BlockHashMerkleTreeWitness.fromJSON(input.blockHashWitness as any)
30-
),
31-
afterBlockStateTransitions: this.stArrayMapper.mapIn(
32-
input.afterBlockStateTransitions
58+
BlockHashMerkleTreeWitness.fromJSON(
59+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
60+
dbBlockResult.blockHashWitness as any
61+
)
3362
),
34-
blockHash: BigInt(input.blockHash),
63+
afterBlockStateTransitions:
64+
this.stArrayMapper.mapIn(stBatch)[0].stateTransitions,
65+
blockHash: BigInt(dbBlockResult.blockHash),
3566

36-
witnessedRoots: [BigInt(input.witnessedRoots[0])],
67+
witnessedRoots: [BigInt(dbBlockResult.witnessedRoots[0])],
3768
};
3869
}
3970

40-
public mapOut(input: BlockResult): DBBlockResult {
41-
return {
71+
public mapOut(
72+
input: BlockResult
73+
): [
74+
DBBlockResult,
75+
[
76+
Omit<
77+
DBStateTransitionBatch,
78+
"txExecutionResultId" | "id" | "blockId" | "blockResultId"
79+
>,
80+
Omit<DBStateTransition, "batchId" | "id">[],
81+
][],
82+
] {
83+
const dbBlockResult = {
4284
stateRoot: input.stateRoot.toString(),
4385
blockHash: input.blockHash.toString(),
4486
blockHashRoot: input.blockHashRoot.toString(),
4587

4688
blockHashWitness: BlockHashMerkleTreeWitness.toJSON(
4789
input.blockHashWitness
4890
),
49-
afterBlockStateTransitions: this.stArrayMapper.mapOut(
50-
input.afterBlockStateTransitions
51-
),
5291
afterNetworkState: NetworkState.toJSON(input.afterNetworkState),
5392

5493
witnessedRoots: [input.witnessedRoots[0].toString()],
5594
};
95+
const stBatches = this.stArrayMapper.mapOut([
96+
{ stateTransitions: input.afterBlockStateTransitions, applied: true },
97+
]);
98+
return [dbBlockResult, stBatches];
5699
}
57100
}

0 commit comments

Comments
 (0)