git.delta.rocks / unique-network / refs/commits / ac3a97703055

difftreelog

misk: add tests for some types

Trubnikov Sergey2022-10-24parent: #b9165a7.patch.diff
in: master

1 file changed

modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
611pub mod test {611pub mod test {
612 use crate::{612 use crate::{
613 abi::{AbiRead, AbiWrite},613 abi::{AbiRead, AbiWrite},
614 types::{string, uint256, address},614 types::*,
615 };615 };
616616
617 use super::{AbiReader, AbiWriter};617 use super::{AbiReader, AbiWriter};
618 use hex_literal::hex;618 use hex_literal::hex;
619 use primitive_types::{H160, U256};619 use primitive_types::{H160, U256};
620 use concat_idents::concat_idents;
621
622 macro_rules! test_impl {
623 ($name:ident, $type:ty, $function_identifier:expr, $decoded_data:expr, $encoded_data:expr) => {
624 concat_idents!(test_name = encode_decode_, $name {
625 #[test]
626 fn test_name() {
627 let function_identifier: u32 = $function_identifier;
628 let decoded_data = $decoded_data;
629 let encoded_data = $encoded_data;
630
631 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();
632 assert_eq!(call, u32::to_be_bytes(function_identifier));
633 let data = <AbiReader<'_> as AbiRead<$type>>::abi_read(&mut decoder).unwrap();
634 assert_eq!(data, decoded_data);
635
636 let mut writer = AbiWriter::new_call(function_identifier);
637 decoded_data.abi_write(&mut writer);
638 let ed = writer.finish();
639 similar_asserts::assert_eq!(encoded_data, ed.as_slice());
640 }
641 });
642 };
643 }
644
645 macro_rules! test_impl_uint {
646 ($type:ident) => {
647 test_impl!(
648 $type,
649 $type,
650 0xdeadbeef,
651 255 as $type,
652 &hex!(
653 "
654 deadbeef
655 00000000000000000000000000000000000000000000000000000000000000ff
656 "
657 )
658 );
659 };
660 }
661
662 test_impl_uint!(uint8);
663 test_impl_uint!(uint32);
664 test_impl_uint!(uint128);
665
666 test_impl!(
667 uint256,
668 uint256,
669 0xdeadbeef,
670 U256([255, 0, 0, 0]),
671 &hex!(
672 "
673 deadbeef
674 00000000000000000000000000000000000000000000000000000000000000ff
675 "
676 )
677 );
678
679 test_impl!(
680 vec_tuple_address_uint256,
681 Vec<(address, uint256)>,
682 0x1ACF2D55,
683 vec![
684 (
685 H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),
686 U256([10, 0, 0, 0]),
687 ),
688 (
689 H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),
690 U256([20, 0, 0, 0]),
691 ),
692 (
693 H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),
694 U256([30, 0, 0, 0]),
695 ),
696 ],
697 &hex!(
698 "
699 1ACF2D55
700 0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]
701 0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]
702
703 0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address
704 000000000000000000000000000000000000000000000000000000000000000A // uint256
705
706 000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address
707 0000000000000000000000000000000000000000000000000000000000000014 // uint256
708
709 0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address
710 000000000000000000000000000000000000000000000000000000000000001E // uint256
711 "
712 )
713 );
714
715 test_impl!(
716 vec_tuple_uint256_string,
717 Vec<(uint256, string)>,
718 0xdeadbeef,
719 vec![
720 (1.into(), "Test URI 0".to_string()),
721 (11.into(), "Test URI 1".to_string()),
722 (12.into(), "Test URI 2".to_string()),
723 ],
724 &hex!(
725 "
726 deadbeef
727 0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]
728 0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]
729
730 0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem
731 00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem
732 0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem
733
734 0000000000000000000000000000000000000000000000000000000000000001 // first token id? #60
735 0000000000000000000000000000000000000000000000000000000000000040 // offset of string
736 000000000000000000000000000000000000000000000000000000000000000a // size of string
737 5465737420555249203000000000000000000000000000000000000000000000 // string
738
739 000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11? #e0
740 0000000000000000000000000000000000000000000000000000000000000040 // offset of string
741 000000000000000000000000000000000000000000000000000000000000000a // size of string
742 5465737420555249203100000000000000000000000000000000000000000000 // string
743
744 000000000000000000000000000000000000000000000000000000000000000c // third token id? Why ==12? #160
745 0000000000000000000000000000000000000000000000000000000000000040 // offset of string
746 000000000000000000000000000000000000000000000000000000000000000a // size of string
747 5465737420555249203200000000000000000000000000000000000000000000 // string
748 "
749 )
750 );
620751
621 #[test]752 #[test]
622 fn dynamic_after_static() {753 fn dynamic_after_static() {
716 similar_asserts::assert_eq!(encoded_data, ed.as_slice());847 similar_asserts::assert_eq!(encoded_data, ed.as_slice());
717 }848 }
718
719 #[test]
720 fn parse_vec_with_simple_type() {
721 let decoded_data = (
722 0x1ACF2D55,
723 vec![
724 (
725 H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),
726 U256([10, 0, 0, 0]),
727 ),
728 (
729 H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),
730 U256([20, 0, 0, 0]),
731 ),
732 (
733 H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),
734 U256([30, 0, 0, 0]),
735 ),
736 ],
737 );
738
739 let encoded_data = &hex!(
740 "
741 1ACF2D55
742 0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]
743 0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]
744
745 0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address
746 000000000000000000000000000000000000000000000000000000000000000A // uint256
747
748 000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address
749 0000000000000000000000000000000000000000000000000000000000000014 // uint256
750
751 0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address
752 000000000000000000000000000000000000000000000000000000000000001E // uint256
753 "
754 );
755
756 let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();
757 assert_eq!(call, u32::to_be_bytes(decoded_data.0));
758 let data =
759 <AbiReader<'_> as AbiRead<Vec<(address, uint256)>>>::abi_read(&mut decoder).unwrap();
760 assert_eq!(data.len(), 3);
761 assert_eq!(data, decoded_data.1);
762
763 let mut writer = AbiWriter::new_call(decoded_data.0);
764 decoded_data.1.abi_write(&mut writer);
765 let ed = writer.finish();
766 assert_eq!(encoded_data, ed.as_slice());
767 }
768}849}
769850