|
1 | 1 | import { singleton } from "tsyringe"; |
2 | 2 | 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"; |
4 | 8 | import { BlockHashMerkleTreeWitness, NetworkState } from "@proto-kit/protocol"; |
5 | 9 |
|
6 | 10 | import { ObjectMapper } from "../../../ObjectMapper"; |
7 | 11 |
|
8 | | -import { StateTransitionArrayMapper } from "./StateTransitionMapper"; |
| 12 | +import { StateTransitionBatchArrayMapper } from "./StateTransitionMapper"; |
9 | 13 |
|
10 | 14 | @singleton() |
11 | 15 | 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 | + > |
13 | 30 | { |
14 | 31 | public constructor( |
15 | | - private readonly stArrayMapper: StateTransitionArrayMapper |
| 32 | + private readonly stArrayMapper: StateTransitionBatchArrayMapper |
16 | 33 | ) {} |
17 | 34 |
|
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]; |
19 | 49 | return { |
20 | 50 | afterNetworkState: new NetworkState( |
21 | 51 | // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
22 | | - NetworkState.fromJSON(input.afterNetworkState as any) |
| 52 | + NetworkState.fromJSON(dbBlockResult.afterNetworkState as any) |
23 | 53 | ), |
24 | 54 |
|
25 | | - stateRoot: BigInt(input.stateRoot), |
26 | | - blockHashRoot: BigInt(input.blockHashRoot), |
| 55 | + stateRoot: BigInt(dbBlockResult.stateRoot), |
| 56 | + blockHashRoot: BigInt(dbBlockResult.blockHashRoot), |
27 | 57 | 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 | + ) |
33 | 62 | ), |
34 | | - blockHash: BigInt(input.blockHash), |
| 63 | + afterBlockStateTransitions: |
| 64 | + this.stArrayMapper.mapIn(stBatch)[0].stateTransitions, |
| 65 | + blockHash: BigInt(dbBlockResult.blockHash), |
35 | 66 |
|
36 | | - witnessedRoots: [BigInt(input.witnessedRoots[0])], |
| 67 | + witnessedRoots: [BigInt(dbBlockResult.witnessedRoots[0])], |
37 | 68 | }; |
38 | 69 | } |
39 | 70 |
|
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 = { |
42 | 84 | stateRoot: input.stateRoot.toString(), |
43 | 85 | blockHash: input.blockHash.toString(), |
44 | 86 | blockHashRoot: input.blockHashRoot.toString(), |
45 | 87 |
|
46 | 88 | blockHashWitness: BlockHashMerkleTreeWitness.toJSON( |
47 | 89 | input.blockHashWitness |
48 | 90 | ), |
49 | | - afterBlockStateTransitions: this.stArrayMapper.mapOut( |
50 | | - input.afterBlockStateTransitions |
51 | | - ), |
52 | 91 | afterNetworkState: NetworkState.toJSON(input.afterNetworkState), |
53 | 92 |
|
54 | 93 | witnessedRoots: [input.witnessedRoots[0].toString()], |
55 | 94 | }; |
| 95 | + const stBatches = this.stArrayMapper.mapOut([ |
| 96 | + { stateTransitions: input.afterBlockStateTransitions, applied: true }, |
| 97 | + ]); |
| 98 | + return [dbBlockResult, stBatches]; |
56 | 99 | } |
57 | 100 | } |
0 commit comments