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