kudu 0.1.0

Library for interacting with Antelope blockchains
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
use std::str::FromStr;
use std::sync::Once;

use color_eyre::eyre::Result;
use serde_json::{json, Value as JsonValue};
use tracing_subscriber::{
    EnvFilter,
    // fmt::format::FmtSpan,
};

use kudu::*;


// =============================================================================
//
//     Unittests coming from the reference Spring implementation
//     https://github.com/AntelopeIO/spring/blob/main/unittests/abi_tests.cpp
//
//     skipping the following tests:
//      - linkauth_test, unlinkauth_test, updateauth_test, deleteauth_test,
//        newaccount_test, setcode_test, setabi_test, packed_transaction
//        -> they seem to provide relatively low value
//      - abi_serialize_detailed_error_message, abi_serialize_short_error_message,
//        abi_deserialize_detailed_error_messsage
//        -> can come at a later stage under the `detailed-error` feature flag
//      - abi_very_deep_structs, abi_very_deep_structs_1us, abi_deep_structs_validate
//        -> they use the `deep_nested_abi` and `large_nested_abis` that are inexistent
//      - abi_large_signature
//        -> use webauthn signatures which are not yet implemented
//      - abi_to_variant__add_action__good_return_value,
//        abi_to_variant__add_action__bad_return_value,
//        abi_to_variant__add_action__no_return_value,
//        -> we haven't implemented `ABI::abi_to_variant` and it seems to be
//           pretty involved with internals, we might not need it anyway
//
// =============================================================================

static TRACING_INIT: Once = Once::new();

fn init() {
    TRACING_INIT.call_once(|| {
        tracing_subscriber::fmt()
            .with_env_filter(EnvFilter::from_default_env())
            // .with_span_events(FmtSpan::ACTIVE)
            .init();
    });
}


// -----------------------------------------------------------------------------
//     Utility functions & macros
// -----------------------------------------------------------------------------

#[track_caller]
fn verify_byte_round_trip(abi: &ABI, typename: &str, value: &JsonValue) -> Result<()> {
    let encoded = abi.variant_to_binary(typename, value)?;
    let decoded = abi.binary_to_variant(typename, encoded.clone())?;
    let encoded2 = abi.variant_to_binary(typename, &decoded)?;

    // assert_eq!(value, &decoded);
    assert_eq!(encoded, encoded2);
    Ok(())
}

#[track_caller]
fn verify_round_trip2(abi: &ABI, typename: &str, value: &JsonValue,
                      hex_repr: &str, expected_json: &str) -> Result<()> {
    let encoded = abi.variant_to_binary(typename, value)?;
    assert_eq!(hex::encode(&encoded), hex_repr);
    let decoded = abi.binary_to_variant(typename, encoded.clone())?;
    assert_eq!(&decoded.to_string(), expected_json);
    let encoded2 = abi.variant_to_binary(typename, value)?;
    assert_eq!(encoded, encoded2);
    Ok(())
}

#[track_caller]
fn verify_round_trip(abi: &ABI, typename: &str, value: &JsonValue,
                     hex_repr: &str) -> Result<()> {
    verify_round_trip2(abi, typename, value, hex_repr, &value.to_string())
}

macro_rules! check_error {
    ($t:ident, $error_type:pat) => {
        // let result = $t ;
        assert!($t.is_err(), "expected error, found some result instead");
        match $t.err().unwrap() {
            $error_type => (),
            err => panic!("wrong error type: expected `{}`, got `{}`", stringify!($error_type), err),
        }
    };
    ($t:ident, $error_type:pat, $msg:literal) => {
        assert!($t.is_err(), "expected error, found some result instead");
        let err = $t.err().unwrap();
        match err {
            $error_type => {
                let received = err.to_string();
                if !received.contains($msg) {
                    panic!(r#"expected error with message "{}", got this instead: "{}""#,
                           $msg, received);
                }
            },
            err => panic!("wrong error type: expected `{}`, got `{}`", stringify!($error_type), err),
        }
    };
}

macro_rules! check_encode_error {
    ($t:ident, $msg:literal) => {
        check_error!($t, ABIError::EncodeError { .. }, $msg);
    }
}

macro_rules! check_integrity_error {
    ($t:ident, $msg:literal) => {
        check_error!($t, ABIError::IntegrityError { .. }, $msg);
    }
}

macro_rules! check_invalid_abi {
    ($path:literal, $msg:literal) => {
        let abi_def = include_str!($path);
        let abi = ABI::from_str(abi_def);
        check_error!(abi, ABIError::IntegrityError { .. }, $msg);
    }
}


// -----------------------------------------------------------------------------
//     Unittests
// -----------------------------------------------------------------------------

#[test]
fn uint_types() -> Result<()> {
    init();

    let currency_abi = r#"
    {
        "version": "eosio::abi/1.0",
        "types": [],
        "structs": [{
            "name": "transfer",
            "base": "",
            "fields": [{
                "name": "amount64",
                "type": "uint64"
            },{
                "name": "amount32",
                "type": "uint32"
            },{
                "name": "amount16",
                "type": "uint16"
            },{
                "name": "amount8",
                "type": "uint8"
            }]
        }],
        "actions": [],
        "tables": [],
        "ricardian_clauses": []
    }
    "#;
    let currency_abi = ABIDefinition::from_str(currency_abi)?;
    let abi = ABI::from_definition(&currency_abi.with_contract_abi()?)?;

    let test_data = json!({
        "amount64": 64,
        "amount32": 32,
        "amount16": 16,
        "amount8": 8,
    });

    verify_byte_round_trip(&abi, "transfer", &test_data)
}


#[test]
fn general() -> Result<()> {
    init();

    let my_abi = ABIDefinition::from_str(include_str!("data/general_abi.json"))?;
    let abi = ABI::from_definition(&my_abi.with_contract_abi()?)?;
    let test_data = JsonValue::from_str(include_str!("data/general_data.json"))?;

    verify_byte_round_trip(&abi, "A", &test_data)
}

#[test]
fn duplicate_types() -> Result<()> {
    init();
    check_invalid_abi!("data/duplicate_types_abi.json", "type already exists");
    Ok(())
}

// FIXME: review me!
#[test]
fn nested_types() -> Result<()> {
    init();

    use kudu::ByteStream;

    let indirectly_nested_abi = r#"
    {
        "version": "eosio::abi/1.0",
        "types": [{
            "new_type_name": "name_arr",
            "type": "name[]"
        },{
            "new_type_name": "name_matrix",
            "type": "name_arr[]"
        }],
        "structs": [],
        "actions": [],
        "tables": [],
        "ricardian_clauses": []
    }
    "#;
    let abi = ABI::from_str(indirectly_nested_abi)?;
    let mut ds = ByteStream::new();

    let value = json!([["a", "b"],["c", "d"]]);
    abi.encode_variant(&mut ds, "name_matrix", &value)?;

    let decoded = abi.decode_variant(&mut ds, "name_matrix")?;
    println!("{:?}", decoded);

    assert_eq!(value, decoded);

    let directly_nested_abi = r#"
    {
        "version": "eosio::abi/1.0",
        "types": [{
            "new_type_name": "name_arr",
            "type": "name[]"
        },{
            "new_type_name": "name_matrix",
            "type": "name[][]"
        }],
        "structs": [],
        "actions": [],
        "tables": [],
        "ricardian_clauses": []
    }
    "#;
    let _abi = ABI::from_str(directly_nested_abi);
    // check_error!(abi, ABIError::IntegrityError { .. }, "invalid type used in typedefs");

    Ok(())
}

#[test]
fn abi_cycle() -> Result<()> {
    init();
    // NOTE: we'd like "circular reference" here in the message but the issue is caught before
    //       by a different integrity check (namely: we can define the same type twice)
    check_invalid_abi!("data/typedef_cycle_abi.json", "type already exists");
    check_invalid_abi!("data/struct_cycle_abi.json", "circular reference in struct");
    Ok(())
}

#[test]
fn abi_type_repeat() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_type_repeat.json", "type already exists");
    Ok(())
}

#[test]
fn abi_struct_repeat() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_struct_repeat.json", "duplicate struct definition");
    Ok(())
}

#[test]
fn abi_action_repeat() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_action_repeat.json", "duplicate action definition");
    Ok(())
}

#[test]
fn abi_table_repeat() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_table_repeat.json", "duplicate table definition");
    Ok(())
}

#[test]
fn abi_type_def() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.0",
        "types": [{
            "new_type_name": "account_name",
            "type": "name"
        }],
        "structs": [{
            "name": "transfer",
            "base": "",
            "fields": [{
                "name": "from",
                "type": "account_name"
            },{
                "name": "to",
                "type": "name"
            },{
                "name": "amount",
                "type": "uint64"
            }]
        }],
        "actions": [{
            "name": "transfer",
            "type": "transfer",
            "ricardian_contract": "transfer contract"
        }],
        "tables": []
    }
    "#)?;

    assert!(abi.is_type("name".into()));
    assert!(abi.is_type("account_name".into()));

    let data = json!({
        "from": "kevin",
        "to": "dan",
        "amount": 16
    });

    verify_byte_round_trip(&abi, "transfer", &data)
}

#[test]
fn abi_type_loop() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_type_loop.json", "type already exists");
    Ok(())
}

#[test]
fn abi_std_optional() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.2",
        "types": [],
        "structs": [{
            "name": "fees",
            "base": "",
            "fields": [{
                "name": "gas_price",
                "type": "uint64?"
            },{
                "name": "miner_cut",
                "type": "uint32?"
            },{
                "name": "bridge_fee",
                "type": "uint32?"
            }]
        }],
        "actions": [{
            "name": "fees",
            "type": "fees",
            "ricardian_contract": ""
        }],
        "tables": [],
        "ricardian_clauses": [],
        "variants": [],
        "action_results": []
    }
    "#)?;

    // check conversion when all members are provided
    verify_byte_round_trip(&abi, "fees", &json!({
        "gas_price": "42",
        "miner_cut": "2",
        "bridge_fee": "2",
    }))?;

    // check conversion when the first optional member is missing
    verify_byte_round_trip(&abi, "fees", &json!({
        "miner_cut": "2",
        "bridge_fee": "2",
    }))?;

    // check conversion when the second optional member is missing
    verify_byte_round_trip(&abi, "fees", &json!({
        "gas_price": "42",
        "bridge_fee": "2",
    }))?;

    // check conversion when the last optional member is missing
    verify_byte_round_trip(&abi, "fees", &json!({
        "gas_price": "42",
        "miner_cut": "2",
    }))?;

    // check conversion when all optional members are missing
    verify_byte_round_trip(&abi, "fees", &json!({}))?;

    Ok(())
}

#[test]
fn abi_type_redefine() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_type_redefine.json", "circular reference in type");
    Ok(())
}

#[test]
fn abi_type_redefine_to_name() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_type_redefine_to_name.json", "type already exists");
    Ok(())
}

// NOTE: the JSON in Spring is not correct, hence the test either can't be correct
// TODO: report bug!!
// NOTE: we change the behavior of this test as we can and actually want to allow
//       recursive structs like this, can be useful eg. to represent trees
#[test]
fn abi_type_nested_in_vector() -> Result<()> {
    init();

    let abi = r#"
    {
        "version": "eosio::abi/1.0",
        "types": [],
        "structs": [{
            "name": "store_t",
            "base": "",
            "fields": [{
                "name": "id",
                "type": "uint64"
            },{
                "name": "children",
                "type": "store_t[]"
            }]
        }],
        "actions": [],
        "tables": []
    }
    "#;

    let abi = ABI::from_str(abi);
    assert!(abi.is_ok());

    Ok(())
}


#[test]
fn abi_account_name_in_eosio_abi() -> Result<()> {
    init();

    let abi_def = include_str!("data/abi_account_name_in_eosio_abi.json");

    let abi = ABI::from_definition(&ABIDefinition::from_str(abi_def)?.with_contract_abi()?);
    check_integrity_error!(abi, "type already exists");

    Ok(())
}

// Unlimited array size during abi serialization can exhaust memory and crash the process
#[test]
fn abi_large_array() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.1",
        "types": [],
        "structs": [{
            "name": "hi",
            "base": "",
            "fields": [{"name": "a", "type": "int8"}]
        }],
        "actions": [{
            "name": "hi",
            "type": "hi[]",
            "ricardian_contract": ""
        }],
        "tables": []
    }
    "#)?;

    let data = b"\xff\xff\xff\xff\x08";

    let result = abi.binary_to_variant("hi[]", Bytes(data.to_vec()));
    check_error!(result, ABIError::DecodeError { .. }, "stream ended unexpectedly; unable to unpack field 'a' of struct 'hi'");

    Ok(())
}

// Unlimited array size during abi serialization can exhaust memory and crash the process
// a non-zero struct would fail early like in the test before, but a zero-sized struct
// will seemingly loop forever (as it is deserializing them it doesn't exhaust the stream
// so can go on for a very long time)
#[test]
#[cfg(feature = "hardened")]
fn abi_large_array_hardened() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.1",
        "types": [],
        "structs": [{
            "name": "hi",
            "base": "",
            "fields": []
        }],
        "actions": [{
            "name": "hi",
            "type": "hi[]",
            "ricardian_contract": ""
        }],
        "tables": []
    }
    "#)?;

    let data = b"\xff\xff\xff\xff\x08";

    let result = abi.binary_to_variant("hi[]", data.to_vec());
    check_error!(result, ABIError::DecodeError { .. }, "timeout or something like that");

    Ok(())
}

#[test]
fn abi_is_type_recursion() -> Result<()> {
    init();
    check_invalid_abi!("data/abi_is_type_recursion.json", "invalid type");
    Ok(())
}

#[test]
#[cfg(feature = "hardened")]
fn abi_recursive_structs() -> Result<()> {
    init();

    let abi = ABI::from_str(include_str!("data/abi_recursive_structs.json"))?;

    let data = json!({"user": "eosio"});
    let encoded = abi.variant_to_binary("hi2", &data)?;

    let result = abi.binary_to_variant("hi", encoded);
    check_encode_error!(result, "yep");

    Ok(())
}

#[test]
fn variants() -> Result<()> {
    init();

    let duplicate_variant_abi = r#"
    {
        "version": "eosio::abi/1.1",
        "variants": [
            {"name": "v1", "types": ["int8", "string", "bool"]},
            {"name": "v1", "types": ["int8", "string", "bool"]}
        ]
    }
    "#;

    let variant_abi_invalid_type = r#"
    {
        "version": "eosio::abi/1.1",
        "variants": [
            {"name": "v1", "types": ["int91", "string", "bool"]}
        ]
    }
    "#;

    let variant_abi = r#"
    {
        "version": "eosio::abi/1.1",
        "types": [
            {"new_type_name": "foo", "type": "s"},
            {"new_type_name": "bar", "type": "s"}
        ],
        "structs": [
            {"name": "s", "base": "", "fields": [
                {"name": "i0", "type": "int8"},
                {"name": "i1", "type": "int8"}
            ]}
        ],
        "variants": [
            {"name": "v1", "types": ["int8", "string", "int16"]},
            {"name": "v2", "types": ["foo", "bar"]}
        ]
    }
    "#;

    let result = ABI::from_str(duplicate_variant_abi);
    check_integrity_error!(result, "duplicate variants definition");

    let result = ABI::from_str(variant_abi_invalid_type);
    check_integrity_error!(result, "invalid type `int91` used in variant 'v1'");

    // round-trip abi through multiple formats
    // json -> variant -> abi_def -> bin
    let mut stream = ByteStream::new();
    ABIDefinition::from_str(variant_abi)?.to_bin(&mut stream);
    // bin -> abi_def -> variant -> abi_def
    let abi = ABI::from_str(&json!(ABIDefinition::from_bin(&mut stream)?).to_string())?;

    // expected array containing variant
    let result = abi.variant_to_binary("v1", &json!(9));
    check_encode_error!(result, "expected input to be an array of 2 elements while processing variant: 9");

    let result = abi.variant_to_binary("v1", &json!([4]));
    check_encode_error!(result, "expected input to be an array of 2 elements while processing variant: [4]");

    let result = abi.variant_to_binary("v1", &json!([4, 5]));
    check_encode_error!(result, "expected variant typename to be a string: 4");

    let result = abi.variant_to_binary("v1", &json!([4, 5, 6]));
    check_encode_error!(result, "expected input to be an array of 2 elements while processing variant: [4,5,6]");

    // type is not valid within this variant
    let result = abi.variant_to_binary("v1", &json!(["int9", 21]));
    check_encode_error!(result, "specified type `int9` is not valid within the variant 'v1'");

    verify_round_trip(&abi, "v1", &json!(["int8",21]), "0015")?;
    verify_round_trip(&abi, "v1", &json!(["string","abcd"]), "010461626364")?;
    verify_round_trip(&abi, "v1", &json!(["int16",3]), "020300")?;
    verify_round_trip(&abi, "v1", &json!(["int16",4]), "020400")?;
    verify_round_trip(&abi, "v2", &json!(["foo",{"i0":5,"i1":6}]), "000506")?;
    verify_round_trip(&abi, "v2", &json!(["bar",{"i0":5,"i1":6}]), "010506")?;

    Ok(())
}

#[test]
fn aliased_variants() -> Result<()> {
    init();

    let aliased_variant = r#"
    {
        "version": "eosio::abi/1.1",
        "types": [
            { "new_type_name": "foo", "type": "foo_variant" }
        ],
        "variants": [
            {"name": "foo_variant", "types": ["int8", "string"]}
        ]
    }
    "#;

    // round-trip abi through multiple formats
    // json -> variant -> abi_def -> bin
    let mut stream = ByteStream::new();
    ABIDefinition::from_str(aliased_variant)?.to_bin(&mut stream);
    // bin -> abi_def -> variant -> abi_def
    let abi = ABI::from_str(&json!(ABIDefinition::from_bin(&mut stream)?).to_string())?;

    verify_round_trip(&abi, "foo", &json!(["int8",21]), "0015")
}

#[test]
fn variant_of_aliases() -> Result<()> {
    init();

    let aliased_variant = r#"
    {
        "version": "eosio::abi/1.1",
        "types": [
            { "new_type_name": "foo_0", "type": "int8" },
            { "new_type_name": "foo_1", "type": "string" }
        ],
        "variants": [
            {"name": "foo", "types": ["foo_0", "foo_1"]}
        ]
    }
    "#;
    let abi = ABI::from_str(aliased_variant)?;

    verify_round_trip(&abi, "foo", &json!(["foo_0",21]), "0015")
}

#[test]
fn extend() -> Result<()> {
    init();

    // NOTE: Ideally this ABI would be rejected during validation for an improper definition for struct "s2".
    //       Such a check is not yet implemented during validation, but it can check during serialization.
    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.1",
        "structs": [
            {"name": "s", "base": "", "fields": [
                {"name": "i0", "type": "int8"},
                {"name": "i1", "type": "int8"},
                {"name": "i2", "type": "int8$"},
                {"name": "a", "type": "int8[]$"},
                {"name": "o", "type": "int8?$"}
            ]},
            {"name": "s2", "base": "", "fields": [
                {"name": "i0", "type": "int8"},
                {"name": "i1", "type": "int8$"},
                {"name": "i2", "type": "int8"}
            ]}
        ]
    }
    "#)?;

    // missing i1
    let result = abi.variant_to_binary("s", &json!({"i0":5}));
    check_encode_error!(result, "missing field 'i1' in input object while processing struct 's'");

    // Unexpected 'a'
    let result = abi.variant_to_binary("s", &json!({"i0":5,"i1":6,"a":[8,9,10]}));
    check_encode_error!(result, "Unexpected field 'a' found in input object while processing struct");

    verify_round_trip(&abi, "s", &json!({"i0":5,"i1":6}), "0506")?;
    verify_round_trip(&abi, "s", &json!({"i0":5,"i1":6,"i2":7}), "050607")?;
    verify_round_trip(&abi, "s", &json!({"i0":5,"i1":6,"i2":7,"a":[8,9,10]}), "0506070308090a")?;
    verify_round_trip(&abi, "s", &json!({"i0":5,"i1":6,"i2":7,"a":[8,9,10],"o":null}), "0506070308090a00")?;
    verify_round_trip(&abi, "s", &json!({"i0":5,"i1":6,"i2":7,"a":[8,9,10],"o":31}), "0506070308090a011f")?;

    verify_round_trip2(&abi, "s", &json!([5,6]), "0506", r#"{"i0":5,"i1":6}"#)?;
    verify_round_trip2(&abi, "s", &json!([5,6,7]), "050607", r#"{"i0":5,"i1":6,"i2":7}"#)?;
    verify_round_trip2(&abi, "s", &json!([5,6,7,[8,9,10]]), "0506070308090a", r#"{"i0":5,"i1":6,"i2":7,"a":[8,9,10]}"#)?;
    verify_round_trip2(&abi, "s", &json!([5,6,7,[8,9,10],null]), "0506070308090a00", r#"{"i0":5,"i1":6,"i2":7,"a":[8,9,10],"o":null}"#)?;
    verify_round_trip2(&abi, "s", &json!([5,6,7,[8,9,10],31]), "0506070308090a011f", r#"{"i0":5,"i1":6,"i2":7,"a":[8,9,10],"o":31}"#)?;

    let result = abi.variant_to_binary("s2", &json!({"i0":1}));
    check_encode_error!(result, "Encountered field 'i2' without binary extension designation while processing struct");

    Ok(())
}

#[test]
fn version() -> Result<()> {
    init();

    let abi = ABI::from_str("{}");
    check_error!(abi, ABIError::JsonError { .. }, "cannot deserialize ABIDefinition from JSON");
    // check_integrity_error!(abi, "yepyep");

    let abi = ABI::from_str(r#"{"version": ""}"#);
    check_error!(abi, ABIError::VersionError { .. }, "unsupported ABI version");

    let abi = ABI::from_str(r#"{"version": "eosio::abi/9.0"}"#);
    check_error!(abi, ABIError::VersionError { .. }, "unsupported ABI version");

    assert!(ABI::from_str(r#"{"version": "eosio::abi/1.0"}"#).is_ok());
    assert!(ABI::from_str(r#"{"version": "eosio::abi/1.1"}"#).is_ok());
    assert!(ABI::from_str(r#"{"version": "eosio::abi/1.2"}"#).is_ok());

    Ok(())
}

#[test]
fn abi_serialize_incomplete_json_array() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"{
        "version": "eosio::abi/1.0",
        "structs": [
            {"name": "s", "base": "", "fields": [
                {"name": "i0", "type": "int8"},
                {"name": "i1", "type": "int8"},
                {"name": "i2", "type": "int8"}
            ]}
        ]
    }"#)?;

    let result = abi.variant_to_binary("s", &json!([]));
    check_encode_error!(result, "early end to input array specifying the fields of struct");

    let result = abi.variant_to_binary("s", &json!([1, 2]));
    check_encode_error!(result, "early end to input array specifying the fields of struct");

    verify_round_trip2(&abi, "s", &json!([1,2,3]), "010203", r#"{"i0":1,"i1":2,"i2":3}"#)?;

    Ok(())
}

#[test]
fn abi_serialize_incomplete_json_object() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.0",
        "structs": [
            {"name": "s1", "base": "", "fields": [
                {"name": "i0", "type": "int8"},
                {"name": "i1", "type": "int8"}
            ]},
            {"name": "s2", "base": "", "fields": [
                {"name": "f0", "type": "s1"},
                {"name": "i2", "type": "int8"}
            ]}
        ]
    }
    "#)?;

    let result = abi.variant_to_binary("s2", &json!({}));
    check_encode_error!(result, "missing field 'f0' in input object");

    let result = abi.variant_to_binary("s2", &json!({"f0":{"i0":1}}));
    check_encode_error!(result, "missing field 'i1' in input object");

    verify_round_trip(&abi, "s2", &json!({"f0":{"i0":1,"i1":2},"i2":3}), "010203")?;

    Ok(())
}

#[test]
fn abi_serialize_json_mismatched_type() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.0",
        "structs": [
            {"name": "s1", "base": "", "fields": [
                {"name": "i0", "type": "int8"}
            ]},
            {"name": "s2", "base": "", "fields": [
                {"name": "f0", "type": "s1"},
                {"name": "i1", "type": "int8"}
            ]}
        ]
    }
    "#)?;

    let result = abi.variant_to_binary("s2", &json!({"f0":1,"i1":2}));
    // FIXME: add context for ABI traversal so we can have the better error message
    // check_encode_error!(result, "unexpected input encountered while encoding struct 's2.f0'");
    check_encode_error!(result, "unexpected input while encoding struct 's1'");

    verify_round_trip(&abi, "s2", &json!({"f0":{"i0":1},"i1":2}), "0102")?;

    Ok(())
}

#[test]
fn abi_serialize_json_empty_name() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.0",
        "structs": [
            {"name": "s1", "base": "", "fields": [
                {"name": "", "type": "int8"}
            ]}
        ]
    }
    "#)?;

    let result = abi.variant_to_binary("s1", &json!({"": 1}));
    assert!(result.is_ok());

    // check_error!(result, ABIError::EncodeError { .. }, "blip");

    verify_round_trip(&abi, "s1", &json!({"": 1}), "01")?;

    Ok(())
}

#[test]
fn serialize_optional_struct_type() -> Result<()> {
    init();

    let abi = ABI::from_str(r#"
    {
        "version": "eosio::abi/1.0",
        "structs": [
            {"name": "s", "base": "", "fields": [
                {"name": "i0", "type": "int8"}
            ]}
        ]
    }
    "#)?;

    verify_round_trip(&abi, "s?", &json!({"i0": 5}), "0105")?;
    verify_round_trip(&abi, "s?", &JsonValue::Null, "00")?;

    Ok(())
}