1
- use juniper:: GraphQLObject ;
2
- use openmina_core:: block:: AppliedBlock ;
3
-
4
1
use crate :: graphql:: zkapp:: { GraphQLFailureReason , GraphQLFeePayer , GraphQLZkappCommand } ;
2
+ use juniper:: { GraphQLEnum , GraphQLObject } ;
3
+ use mina_p2p_messages:: v2:: {
4
+ MinaBaseSignedCommandPayloadBodyStableV2 , MinaBaseStakeDelegationStableV2 ,
5
+ TransactionSnarkWorkTStableV2 ,
6
+ } ;
7
+ use openmina_core:: block:: AppliedBlock ;
5
8
6
- use super :: { zkapp:: GraphQLZkapp , ConversionError } ;
9
+ use super :: { account :: GraphQLDummyAccount , zkapp:: GraphQLZkapp , ConversionError } ;
7
10
8
11
#[ derive( GraphQLObject , Debug ) ]
9
12
#[ graphql( description = "A Mina block" ) ]
10
- pub struct GraphQLBestChainBlock {
11
- pub protocol_state : GraphQLProtocolState ,
13
+ /// Location [src/lib/mina_graphql/types.ml:2095](https://github.com/MinaProtocol/mina/blob/develop/src/lib/mina_graphql/types.ml#L2095-L2151)
14
+ pub struct GraphQLBlock {
15
+ pub creator : String ,
16
+ /// TODO: this must be fetched separately from `AppliedBlock`
17
+ pub creator_account : GraphQLDummyAccount ,
18
+ /// TODO: this must be fetched separately from `AppliedBlock`
19
+ pub winner_account : GraphQLDummyAccount ,
12
20
pub state_hash : String ,
21
+ /// Experimental: Bigint field-element representation of stateHash
22
+ pub state_hash_field : String ,
23
+ pub protocol_state : GraphQLProtocolState ,
24
+ /// Public key of account that produced this block
25
+ /// use creatorAccount field instead
13
26
pub transactions : GraphQLTransactions ,
27
+ /// Base58Check-encoded hash of the state after this block
28
+ /// Count of user command transactions in the block
29
+ pub command_transaction_count : i32 ,
30
+ pub snark_jobs : Vec < GraphQLSnarkJob > ,
31
+ }
32
+
33
+ #[ derive( GraphQLObject , Debug ) ]
34
+ pub struct GraphQLSnarkJob {
35
+ pub fee : String ,
36
+ pub prover : String ,
14
37
}
15
38
16
39
#[ derive( GraphQLObject , Debug ) ]
17
40
pub struct GraphQLTransactions {
18
41
pub zkapp_commands : Vec < GraphQLZkapp > ,
42
+ pub user_commands : Vec < GraphQLUserCommands > ,
43
+ }
44
+
45
+ #[ derive( GraphQLObject , Debug ) ]
46
+ pub struct GraphQLUserCommands {
47
+ pub amount : Option < String > ,
48
+ pub failure_reason : Option < String > ,
49
+ pub fee : String ,
50
+ pub fee_token : String ,
51
+ pub from : String ,
52
+ pub hash : String ,
53
+ pub id : String ,
54
+ pub is_delegation : bool ,
55
+ pub kind : GraphQLUserCommandsKind ,
56
+ pub memo : String ,
57
+ pub nonce : i32 ,
58
+ pub to : String ,
59
+ pub token : String ,
60
+ pub valid_until : String ,
61
+ }
62
+
63
+ #[ derive( Clone , Copy , Debug , GraphQLEnum ) ]
64
+ #[ allow( non_camel_case_types) ]
65
+ pub enum GraphQLUserCommandsKind {
66
+ PAYMENT ,
67
+ STAKE_DELEGATION ,
19
68
}
20
69
21
- impl TryFrom < AppliedBlock > for GraphQLBestChainBlock {
70
+ impl TryFrom < AppliedBlock > for GraphQLBlock {
22
71
type Error = ConversionError ;
23
72
fn try_from ( value : AppliedBlock ) -> Result < Self , Self :: Error > {
24
73
let block = value. block ;
@@ -58,10 +107,28 @@ impl TryFrom<AppliedBlock> for GraphQLBestChainBlock {
58
107
. into ( ) ,
59
108
} ;
60
109
110
+ let command_transaction_count = block. body ( ) . diff ( ) . 0 . commands . len ( ) as i32 ;
111
+
112
+ let snark_jobs = block
113
+ . body ( )
114
+ . completed_works_iter ( )
115
+ . map ( GraphQLSnarkJob :: from)
116
+ . collect ( ) ;
117
+
61
118
Ok ( Self {
119
+ creator_account : GraphQLDummyAccount {
120
+ public_key : block. producer ( ) . to_string ( ) ,
121
+ } ,
122
+ winner_account : GraphQLDummyAccount {
123
+ public_key : block. block_stake_winner ( ) . to_string ( ) ,
124
+ } ,
62
125
protocol_state,
63
126
state_hash : block. hash . to_string ( ) ,
127
+ state_hash_field : block. hash . to_decimal ( ) ,
128
+ creator : block. producer ( ) . to_string ( ) ,
64
129
transactions : block. body ( ) . diff ( ) . clone ( ) . try_into ( ) ?,
130
+ command_transaction_count,
131
+ snark_jobs,
65
132
} )
66
133
}
67
134
}
@@ -123,25 +190,66 @@ impl TryFrom<mina_p2p_messages::v2::StagedLedgerDiffDiffDiffStableV2> for GraphQ
123
190
. 1
124
191
. map_or_else ( Vec :: new, |v| v. commands . into_iter ( ) . collect :: < Vec < _ > > ( ) ) ;
125
192
126
- let zkapp_commands = value
193
+ let commands = value
127
194
. 0
128
195
. commands
129
196
. into_iter ( )
130
197
. chain ( also_zkapp_commands)
131
- . rev ( )
132
- . map ( |cmd| {
133
- // std::fs::create_dir_all("zkapps").unwrap();
134
- // let zkapp_path = format!("zkapps/{}", zkapp.hash().unwrap());
135
- // let path = PathBuf::from(zkapp_path.clone());
136
- // if !path.exists() {
137
- // let mut buff = Vec::new();
138
- // zkapp.binprot_write(&mut buff).unwrap();
139
- // std::fs::write(zkapp_path, buff).unwrap();
140
- // }
141
- if let MinaBaseUserCommandStableV2 :: ZkappCommand ( zkapp) = cmd. data {
198
+ . rev ( ) ;
199
+
200
+ let mut zkapp_commands = Vec :: new ( ) ;
201
+ let mut user_commands = Vec :: new ( ) ;
202
+
203
+ for command in commands {
204
+ match command. data {
205
+ MinaBaseUserCommandStableV2 :: SignedCommand ( user_command) => {
206
+ let is_delegation = matches ! (
207
+ user_command. payload. body,
208
+ MinaBaseSignedCommandPayloadBodyStableV2 :: StakeDelegation ( _)
209
+ ) ;
210
+ let hash = user_command. hash ( ) ?. to_string ( ) ;
211
+
212
+ let fee = user_command. payload . common . fee . to_string ( ) ;
213
+ let memo = user_command. payload . common . memo . to_base58check ( ) ;
214
+ let nonce = user_command. payload . common . nonce . as_u32 ( ) as i32 ;
215
+ let valid_until = user_command. payload . common . valid_until . as_u32 ( ) . to_string ( ) ;
216
+
217
+ let ( to, amount, kind) = match user_command. payload . body {
218
+ MinaBaseSignedCommandPayloadBodyStableV2 :: Payment ( payment) => (
219
+ payment. receiver_pk . to_string ( ) ,
220
+ Some ( payment. amount . to_string ( ) ) ,
221
+ GraphQLUserCommandsKind :: PAYMENT ,
222
+ ) ,
223
+ MinaBaseSignedCommandPayloadBodyStableV2 :: StakeDelegation (
224
+ MinaBaseStakeDelegationStableV2 :: SetDelegate { new_delegate } ,
225
+ ) => (
226
+ new_delegate. to_string ( ) ,
227
+ None ,
228
+ GraphQLUserCommandsKind :: STAKE_DELEGATION ,
229
+ ) ,
230
+ } ;
231
+
232
+ user_commands. push ( GraphQLUserCommands {
233
+ hash,
234
+ from : user_command. signer . to_string ( ) ,
235
+ to,
236
+ is_delegation,
237
+ amount,
238
+ failure_reason : Default :: default ( ) ,
239
+ fee,
240
+ fee_token : Default :: default ( ) ,
241
+ id : Default :: default ( ) ,
242
+ kind,
243
+ memo,
244
+ nonce,
245
+ token : Default :: default ( ) ,
246
+ valid_until,
247
+ } ) ;
248
+ }
249
+ MinaBaseUserCommandStableV2 :: ZkappCommand ( zkapp) => {
142
250
let failure_reason =
143
251
if let MinaBaseTransactionStatusStableV2 :: Failed ( failure_collection) =
144
- cmd . status
252
+ command . status
145
253
{
146
254
let res = failure_collection
147
255
. 0
@@ -158,17 +266,20 @@ impl TryFrom<mina_p2p_messages::v2::StagedLedgerDiffDiffDiffStableV2> for GraphQ
158
266
} )
159
267
. rev ( )
160
268
. collect ( ) ;
269
+
161
270
Some ( res)
162
271
} else {
163
272
None
164
273
} ;
274
+
165
275
let account_updates = zkapp
166
276
. account_updates
167
277
. clone ( )
168
278
. into_iter ( )
169
279
. map ( |v| v. elt . account_update . try_into ( ) )
170
280
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
171
- Ok ( Some ( GraphQLZkapp {
281
+
282
+ zkapp_commands. push ( GraphQLZkapp {
172
283
hash : zkapp. hash ( ) ?. to_string ( ) ,
173
284
failure_reason,
174
285
id : zkapp. to_base64 ( ) ?,
@@ -177,16 +288,15 @@ impl TryFrom<mina_p2p_messages::v2::StagedLedgerDiffDiffDiffStableV2> for GraphQ
177
288
account_updates,
178
289
fee_payer : GraphQLFeePayer :: from ( zkapp. fee_payer ) ,
179
290
} ,
180
- } ) )
181
- } else {
182
- Ok ( None )
291
+ } ) ;
183
292
}
184
- } )
185
- . collect :: < Result < Vec < _ > , Self :: Error > > ( ) ?
186
- . into_iter ( )
187
- . flatten ( )
188
- . collect :: < Vec < _ > > ( ) ;
189
- Ok ( Self { zkapp_commands } )
293
+ }
294
+ }
295
+
296
+ Ok ( Self {
297
+ zkapp_commands,
298
+ user_commands,
299
+ } )
190
300
}
191
301
}
192
302
@@ -258,3 +368,12 @@ impl From<mina_p2p_messages::v2::ConsensusProofOfStakeDataConsensusStateValueSta
258
368
}
259
369
}
260
370
}
371
+
372
+ impl From < & TransactionSnarkWorkTStableV2 > for GraphQLSnarkJob {
373
+ fn from ( value : & TransactionSnarkWorkTStableV2 ) -> Self {
374
+ Self {
375
+ fee : value. fee . to_string ( ) ,
376
+ prover : value. prover . to_string ( ) ,
377
+ }
378
+ }
379
+ }
0 commit comments