6
6
BlockQueue ,
7
7
BlockStorage ,
8
8
BlockWithResult ,
9
+ BlockWithMaybeResult ,
9
10
} from "@proto-kit/sequencer" ;
10
11
import { log } from "@proto-kit/common" ;
11
12
import {
@@ -37,7 +38,7 @@ export class PrismaBlockStorage
37
38
38
39
private async getBlockByQuery (
39
40
where : { height : number } | { hash : string }
40
- ) : Promise < BlockWithResult | undefined > {
41
+ ) : Promise < BlockWithMaybeResult | undefined > {
41
42
const dbResult = await this . connection . prismaClient . block . findFirst ( {
42
43
where,
43
44
include : {
@@ -55,18 +56,15 @@ export class PrismaBlockStorage
55
56
const transactions = dbResult . transactions . map < TransactionExecutionResult > (
56
57
( txresult ) => this . transactionResultMapper . mapIn ( [ txresult , txresult . tx ] )
57
58
) ;
58
- if ( dbResult . result === undefined || dbResult . result === null ) {
59
- throw new Error (
60
- `No Metadata has been set for block ${ JSON . stringify ( where ) } yet`
61
- ) ;
62
- }
63
59
64
60
return {
65
61
block : {
66
62
...this . blockMapper . mapIn ( dbResult ) ,
67
63
transactions,
68
64
} ,
69
- result : this . blockResultMapper . mapIn ( dbResult . result ) ,
65
+ result : dbResult . result
66
+ ? this . blockResultMapper . mapIn ( dbResult . result )
67
+ : undefined ,
70
68
} ;
71
69
}
72
70
@@ -98,45 +96,43 @@ export class PrismaBlockStorage
98
96
99
97
const { prismaClient } = this . connection ;
100
98
101
- await prismaClient . $transaction ( [
102
- prismaClient . transaction . createMany ( {
103
- data : block . transactions . map ( ( txr ) =>
104
- this . transactionMapper . mapOut ( txr . tx )
105
- ) ,
106
- skipDuplicates : true ,
107
- } ) ,
108
-
109
- prismaClient . block . create ( {
110
- data : {
111
- ...encodedBlock ,
112
- beforeBlockStateTransitions :
113
- encodedBlock . beforeBlockStateTransitions as Prisma . InputJsonArray ,
114
- beforeNetworkState :
115
- encodedBlock . beforeNetworkState as Prisma . InputJsonObject ,
116
- duringNetworkState :
117
- encodedBlock . duringNetworkState as Prisma . InputJsonObject ,
118
-
119
- transactions : {
120
- createMany : {
121
- data : transactions . map ( ( tx ) => {
122
- return {
123
- status : tx . status ,
124
- statusMessage : tx . statusMessage ,
125
- txHash : tx . txHash ,
126
-
127
- stateTransitions :
128
- tx . stateTransitions as Prisma . InputJsonArray ,
129
- events : tx . events as Prisma . InputJsonArray ,
130
- } ;
131
- } ) ,
132
- skipDuplicates : true ,
133
- } ,
134
- } ,
99
+ await prismaClient . transaction . createMany ( {
100
+ data : block . transactions . map ( ( txr ) =>
101
+ this . transactionMapper . mapOut ( txr . tx )
102
+ ) ,
103
+ skipDuplicates : true ,
104
+ } ) ;
105
+
106
+ await prismaClient . block . create ( {
107
+ data : {
108
+ ...encodedBlock ,
109
+ beforeBlockStateTransitions :
110
+ encodedBlock . beforeBlockStateTransitions as Prisma . InputJsonArray ,
111
+ beforeNetworkState :
112
+ encodedBlock . beforeNetworkState as Prisma . InputJsonObject ,
113
+ duringNetworkState :
114
+ encodedBlock . duringNetworkState as Prisma . InputJsonObject ,
135
115
136
- batchHeight : undefined ,
116
+ transactions : {
117
+ createMany : {
118
+ data : transactions . map ( ( tx ) => {
119
+ return {
120
+ status : tx . status ,
121
+ statusMessage : tx . statusMessage ,
122
+ txHash : tx . txHash ,
123
+
124
+ stateTransitions :
125
+ tx . stateTransitions as Prisma . InputJsonArray ,
126
+ events : tx . events as Prisma . InputJsonArray ,
127
+ } ;
128
+ } ) ,
129
+ skipDuplicates : true ,
130
+ } ,
137
131
} ,
138
- } ) ,
139
- ] ) ;
132
+
133
+ batchHeight : undefined ,
134
+ } ,
135
+ } ) ;
140
136
}
141
137
142
138
public async pushResult ( result : BlockResult ) : Promise < void > {
@@ -167,7 +163,9 @@ export class PrismaBlockStorage
167
163
return ( result ?. _max . height ?? - 1 ) + 1 ;
168
164
}
169
165
170
- public async getLatestBlock ( ) : Promise < BlockWithResult | undefined > {
166
+ public async getLatestBlockAndResult ( ) : Promise <
167
+ BlockWithMaybeResult | undefined
168
+ > {
171
169
const latestBlock = await this . connection . prismaClient . $queryRaw <
172
170
{ hash : string } [ ]
173
171
> `SELECT b1."hash" FROM "Block" b1
@@ -183,6 +181,22 @@ export class PrismaBlockStorage
183
181
} ) ;
184
182
}
185
183
184
+ public async getLatestBlock ( ) : Promise < BlockWithResult | undefined > {
185
+ const result = await this . getLatestBlockAndResult ( ) ;
186
+ if ( result !== undefined ) {
187
+ if ( result . result === undefined ) {
188
+ throw new Error (
189
+ `Block result for block ${ result . block . height . toString ( ) } not found`
190
+ ) ;
191
+ }
192
+ return {
193
+ block : result . block ,
194
+ result : result . result ,
195
+ } ;
196
+ }
197
+ return result ;
198
+ }
199
+
186
200
public async getNewBlocks ( ) : Promise < BlockWithResult [ ] > {
187
201
const blocks = await this . connection . prismaClient . block . findMany ( {
188
202
where : {
0 commit comments