1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

#[cfg(all(test, feature = "browser"))]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[cfg(test)]
mod tests {
    use crate::*;
    use ark_ff::BigInteger256;
    use mina_rs_base::{types::*, *};
    use mina_serialization_types::json::*;
    use num::BigUint;
    use pretty_assertions::assert_eq;
    use proof_systems::mina_hasher::{create_legacy, Hashable, Hasher};
    use std::str::FromStr;
    use wasm_bindgen_test::*;

    #[test]
    #[wasm_bindgen_test]
    fn json_binprot_parity_tests() {
        let block_fixture_binprot = test_fixtures::TEST_BLOCKS
            .get("3NKjZ5fjms6BMaH4aq7DopPGyMY7PbG6vhRsX5XnYRxih8i9G7dj.hex")
            .unwrap();
        let block_from_binprot =
            <ExternalTransition as BinProtSerializationType>::try_from_binprot(
                block_fixture_binprot.bytes.as_slice(),
            )
            .unwrap();
        let json_value = test_fixtures::JSON_TEST_BLOCKS
            .get("mainnet-117896-3NKjZ5fjms6BMaH4aq7DopPGyMY7PbG6vhRsX5XnYRxih8i9G7dj.json")
            .unwrap();
        let block_json: <ExternalTransition as JsonSerializationType>::T =
            serde_json::from_value(json_value.clone()).unwrap();
        let block_from_json = block_json.into();
        assert_eq!(block_from_binprot, block_from_json);
    }

    /*
    Parity test code:

    let%test_unit "test_genesis_protocol_state_to_random_oracle_input" =
    let open Core_kernel in
    let open Async in
    let json =
      In_channel.with_file "/path-to/mainnet-113267-3NKtqqstB6h8SVNQCtspFisjUwCTqoQ6cC1KGvb6kx6n2dqKkiZS.json" ~f:(fun in_channel ->
          try Yojson.Safe.from_channel in_channel with
          | _ ->
              Core.exit 1)
    in
    let precomputed =
      match Mina_block.Precomputed.of_yojson json with
      | Ok precomputed ->
          precomputed
      | Error _ ->
          Core.exit 1
    in
    let protocal_state = precomputed.protocol_state in
    let _ = protocal_state in
    let state_hash = Mina_state.Protocol_state.hashes_abstract ~hash_body: Mina_state.Protocol_state.Body.hash protocal_state in
    printf "state_hash:%s\n" (state_hash.state_hash |> State_hash.to_yojson |> Yojson.Safe.to_string);
    printf "state_hash:%s\n" (state_hash.state_hash |> State_hash.to_decimal_string);
    let body_hash = Mina_state.Protocol_state.Body.hash protocal_state.body in
    printf "body_hash:%s\n" (body_hash |> Mina_base.State_body_hash.to_yojson |> Yojson.Safe.to_string);
    printf "body_hash:%s\n" (body_hash |> Mina_base.State_body_hash.to_decimal_string);
    let input_to_string i = i |> Random_oracle.pack_input |> Random_oracle.hash |> State_hash.of_hash |> State_hash.to_decimal_string in
    let body_constants = Mina_state.Protocol_state.Body.constants protocal_state.body in
    printf "body_constants_hash:%s\n" (body_constants |> Protocol_constants_checked.to_input |> input_to_string);
    let consensus_state = Mina_state.Protocol_state.Body.consensus_state protocal_state.body in
    printf "consensus_state_hash:%s\n" (consensus_state |> Consensus.Data.Consensus_state.to_input |> input_to_string);
    let blockchain_state = Mina_state.Protocol_state.Body.blockchain_state protocal_state.body in
    printf "blockchain_state_hash:%s\n" (blockchain_state |> Mina_state.Blockchain_state.to_input |> input_to_string);
    let snarked_next_available_token = blockchain_state.snarked_next_available_token in
    printf "snarked_next_available_token_hash:%s\n" (snarked_next_available_token |> Token_id.to_input |> input_to_string);
    let timestamp = blockchain_state.timestamp in
    printf "timestamp_hash:%s\n" (timestamp |> Block_time.Bits.to_bits |> Random_oracle.Input.bitstring |> input_to_string);
    let staged_ledger_hash = blockchain_state.staged_ledger_hash in
    printf "staged_ledger_hash_hash:%s\n" (staged_ledger_hash |> Staged_ledger_hash.to_input |> input_to_string);
    let non_snark = blockchain_state.staged_ledger_hash |> Staged_ledger_hash.non_snark in
    printf "non_snark_digest:%s\n" (non_snark |> Staged_ledger_hash.Non_snark.digest |> Hex.encode);
    printf "non_snark_hash:%s\n" (non_snark |> Staged_ledger_hash.Non_snark.to_input |> input_to_string);
    printf "\n"

    Output:
    state_hash:"3NKtqqstB6h8SVNQCtspFisjUwCTqoQ6cC1KGvb6kx6n2dqKkiZS"
    state_hash:18109765379584684499155740919947103416101561945742376017305891046236717214321
    body_hash:"3Wu8htSn6KNAFLGrrKiFFb2J6FzdnL14GCgNXP1KGdv8UZa3u1Qq"
    body_hash:11547288559214200277520549031042137594317244691846831172842173442778999413309
    body_constants_hash:21283551842411620881532468880644102678972422874752466180084533585306358443047
    consensus_state_hash:4567520866406870569587277062959652446267293244931899281786637782228092413468
    blockchain_state_hash:22984217591685958776680303176697091613340796570000046393879843972385768898212
    snarked_next_available_token_hash:5944847923306304475446774703885080487726156474234720095593426533796586636239
    timestamp_hash:48646600159211660110710851180350202765276633001045178862203248356708033087
    staged_ledger_hash_hash:8746300742690142157461847113315645738247638524643087279433100997652654346784
    non_snark_digest:1f3f4109e8e81048dfa42023b6292e8cc0c54721a34097e4333c0c89cefc9c26
    non_snark_hash:26916965920202625828811831006264431911594993787410334374666335644820635123853
    */
    #[test]
    #[wasm_bindgen_test]
    fn protocol_state_hash_parity_test() {
        let json_value = test_fixtures::JSON_TEST_BLOCKS
            .get("mainnet-113267-3NKtqqstB6h8SVNQCtspFisjUwCTqoQ6cC1KGvb6kx6n2dqKkiZS.json")
            .unwrap();
        let block_json: <ExternalTransition as JsonSerializationType>::T =
            serde_json::from_value(json_value.clone()).unwrap();
        let block: ExternalTransition = block_json.into();

        fn hash<T: Hashable<D = ()>>(t: &T) -> String {
            let mut hasher = create_legacy(());
            let hash = hasher.hash(t);
            let big256: BigInteger256 = hash.into();
            let big: BigUint = big256.into();
            big.to_str_radix(10)
        }

        assert_eq!(
            hash(&block.protocol_state.body.constants),
            "21283551842411620881532468880644102678972422874752466180084533585306358443047"
        );

        assert_eq!(
            hash(&block.protocol_state.body.consensus_state.last_vrf_output),
            "25370206210950520779082257948618326030877049579889865033411963068050002039007"
        );

        assert_eq!(
            hash(&block.protocol_state.body.consensus_state),
            "4567520866406870569587277062959652446267293244931899281786637782228092413468"
        );

        assert_eq!(
            hash(&block.protocol_state.body.blockchain_state),
            "22984217591685958776680303176697091613340796570000046393879843972385768898212"
        );

        assert_eq!(
            hash(
                &block
                    .protocol_state
                    .body
                    .blockchain_state
                    .snarked_next_available_token
            ),
            "5944847923306304475446774703885080487726156474234720095593426533796586636239"
        );

        assert_eq!(
            hash(
                &block
                    .protocol_state
                    .body
                    .blockchain_state
                    .snarked_next_available_token
            ),
            "5944847923306304475446774703885080487726156474234720095593426533796586636239"
        );

        assert_eq!(
            hash(&block.protocol_state.body.blockchain_state.timestamp),
            "48646600159211660110710851180350202765276633001045178862203248356708033087"
        );

        assert_eq!(
            hash(
                &block
                    .protocol_state
                    .body
                    .blockchain_state
                    .staged_ledger_hash
            ),
            "8746300742690142157461847113315645738247638524643087279433100997652654346784"
        );

        assert_eq!(
            hex::encode(
                block
                    .protocol_state
                    .body
                    .blockchain_state
                    .staged_ledger_hash
                    .non_snark
                    .digest()
            ),
            "1f3f4109e8e81048dfa42023b6292e8cc0c54721a34097e4333c0c89cefc9c26"
        );

        assert_eq!(
            hash(
                &block
                    .protocol_state
                    .body
                    .blockchain_state
                    .staged_ledger_hash
                    .non_snark
            ),
            "26916965920202625828811831006264431911594993787410334374666335644820635123853"
        );

        assert_eq!(
            hash(&block.protocol_state.body),
            "11547288559214200277520549031042137594317244691846831172842173442778999413309"
        );

        assert_eq!(
            hash(&block.protocol_state),
            "18109765379584684499155740919947103416101561945742376017305891046236717214321"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn json_binprot_state_hash_tests() {
        for (file_name, json) in test_fixtures::JSON_TEST_BLOCKS.iter() {
            let expected_state_hash = file_name
                .split('.')
                .nth(0)
                .unwrap_or_default()
                .split('-')
                .last()
                .unwrap_or_default();
            let block_json: <ExternalTransition as JsonSerializationType>::T =
                serde_json::from_value(json.clone()).unwrap();
            let block: ExternalTransition = block_json.into();
            let state_hash = block.protocol_state.state_hash();
            assert_eq!(&state_hash.to_string(), expected_state_hash);
        }
    }

    #[test]
    #[wasm_bindgen_test]
    fn consensus_state_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ConsensusState,
            ConsensusStateJson,
            "protocol_state/body/consensus_state"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn constants_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ProtocolConstants,
            ProtocolConstantsJson,
            "protocol_state/body/constants"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn blockchain_state_json_serde_roundtrip() {
        json_serde_roundtrip!(
            BlockchainStateLegacy,
            BlockchainStateJson,
            "protocol_state/body/blockchain_state"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn protocol_state_body_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ProtocolStateBodyLegacy,
            ProtocolStateBodyJson,
            "protocol_state/body"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn protocol_state_json_serde_roundtrip() {
        json_serde_roundtrip!(ProtocolStateLegacy, ProtocolStateJson, "protocol_state");
    }

    #[test]
    #[wasm_bindgen_test]
    fn protocol_state_proof_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ProtocolStateProof,
            ProtocolStateProofBase64Json,
            "protocol_state_proof"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn delta_transition_chain_proof_json_serde_roundtrip() {
        json_serde_roundtrip!(
            DeltaTransitionChainProof,
            DeltaTransitionChainProofJson,
            "delta_transition_chain_proof"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn completed_works_json_serde_roundtrip() {
        json_serde_roundtrip!(
            TransactionSnarkWork,
            TransactionSnarkWorkJson,
            "staged_ledger_diff/diff/0/completed_works/0"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn completed_works_proofs_statement_json_serde_roundtrip() {
        json_serde_roundtrip!(
            Statement,
            StatementJson,
            "staged_ledger_diff/diff/0/completed_works/0/proofs/1/statement"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn completed_works_proofs_proof_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ProtocolStateProof,
            ProtocolStateProofJson,
            "staged_ledger_diff/diff/0/completed_works/0/proofs/1/proof"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn completed_works_proofs_proof_statement_json_serde_roundtrip() {
        json_serde_roundtrip!(
            ProofStatement,
            ProofStatementJson,
            "staged_ledger_diff/diff/0/completed_works/0/proofs/1/proof/statement"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn commands_json_serde_roundtrip() {
        json_serde_roundtrip!(
            UserCommandWithStatus,
            UserCommandWithStatusJson,
            "staged_ledger_diff/diff/0/commands/0"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn user_command_json_serde_roundtrip() {
        json_serde_roundtrip!(
            UserCommand,
            UserCommandJson,
            "staged_ledger_diff/diff/0/commands/0/data"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn user_command_json_serde_roundtrip_2() {
        json_serde_roundtrip!(
            UserCommand,
            UserCommandJson,
            "staged_ledger_diff/diff/0/commands/2/data"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn transaction_status_json_serde_roundtrip() {
        json_serde_roundtrip!(
            TransactionStatus,
            TransactionStatusJson,
            "staged_ledger_diff/diff/0/commands/0/status"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn internal_command_balances_json_serde_roundtrip() {
        json_serde_roundtrip!(
            InternalCommandBalanceData,
            InternalCommandBalanceDataJson,
            "staged_ledger_diff/diff/0/internal_command_balances/0"
        );
    }

    #[test]
    #[wasm_bindgen_test]
    fn coinbase_json_serde_roundtrip() {
        json_serde_roundtrip!(CoinBase, CoinBaseJson, "staged_ledger_diff/diff/0/coinbase");
    }

    #[test]
    #[wasm_bindgen_test]
    fn staged_ledger_diff_json_serde_roundtrip() {
        json_serde_roundtrip!(StagedLedgerDiff, StagedLedgerDiffJson, "staged_ledger_diff");
    }

    #[test]
    #[wasm_bindgen_test]
    fn block_json_serde_roundtrip() {
        json_serde_roundtrip!(ExternalTransition, ExternalTransitionJson, "");
    }

    #[macro_export]
    macro_rules! json_serde_roundtrip {
        ($ty: ty, $ty_json: ty, $path: literal) => {
            (|| {
                'outer: for (_, mut json) in test_fixtures::JSON_TEST_BLOCKS.iter() {
                    if $path.len() > 0 {
                        for p in $path.split('/') {
                            json = match usize::from_str(p) {
                                Ok(index) => {
                                    if let Some(array) = json.as_array() {
                                        if index >= array.len() {
                                            continue 'outer;
                                        }
                                        &array[index]
                                    } else {
                                        panic!("Array expect");
                                    }
                                }
                                _ => &json[p],
                            };
                        }
                    }
                    let cs: $ty = {
                        let json_string = serde_json::to_string_pretty(json)?;
                        let json: $ty_json = serde_json::from_str(json_string.as_str())
                            .map_err(|err| anyhow::Error::msg(format!("{json_string}\n\n{err}")))?;
                        json.into()
                    };
                    let json_from_cs = {
                        let json: $ty_json = cs.into();
                        serde_json::to_value(&json)?
                    };
                    assert_eq!(json, &json_from_cs);
                }
                Ok::<_, anyhow::Error>(())
            })()
            .unwrap();
        };
    }
}