From 34657ffa3341a8c39ad95d1bb532cca792c91d14 Mon Sep 17 00:00:00 2001 From: 170210 <85928898+170210@users.noreply.github.com> Date: Tue, 5 Mar 2024 13:27:34 +0900 Subject: [PATCH] Enable expand_as_any serialize & deserialize a pair of type_url and value (#20) * feat: enable expand_as_any serialize a pair of type_url and value Signed-off-by: 170210 * feat: enable expand_as_any deserialize a pair of type_url and value Signed-off-by: 170210 * chore: add tests for serialize & deserialize Signed-off-by: 170210 * fix: update cargo.lock Signed-off-by: 170210 * chore: change test cases Signed-off-by: 170210 * chore: move serde_json in dev-dependencies Signed-off-by: 170210 * fix: fix for comment Signed-off-by: 170210 --------- Signed-off-by: 170210 --- Cargo.lock | 125 +++++++++++++++-------------- examples/Cargo.lock | 47 ++++++----- packages/finschia-std/Cargo.toml | 4 + packages/finschia-std/src/shim.rs | 127 +++++++++++++++++++++++++++++- 4 files changed, 220 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4ee037f..f9427632 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,7 +62,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -73,7 +73,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -165,6 +165,12 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64ct" version = "1.6.0" @@ -224,12 +230,9 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cc" -version = "1.0.83" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" -dependencies = [ - "libc", -] +checksum = "02f341c093d19155a6e41631ce5971aac4e9a868262212153124c15fa22d1cdc" [[package]] name = "cfg-if" @@ -296,7 +299,7 @@ name = "cosmwasm-std" version = "1.1.9+0.8.1" source = "git+https://github.com/Finschia/cosmwasm?tag=v1.1.9+0.8.1#344858de73055acae78d7146c57abe4318631cf3" dependencies = [ - "base64", + "base64 0.13.1", "cosmwasm-crypto", "cosmwasm-derive", "derivative", @@ -415,9 +418,9 @@ checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" [[package]] name = "dyn-clone" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" [[package]] name = "ecdsa" @@ -521,6 +524,7 @@ dependencies = [ name = "finschia-std" version = "0.21.0" dependencies = [ + "base64 0.21.7", "chrono", "cosmwasm-std", "finschia-std-derive", @@ -529,6 +533,7 @@ dependencies = [ "schemars", "serde", "serde-cw-value", + "serde_json", ] [[package]] @@ -659,7 +664,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap 2.2.3", + "indexmap 2.2.5", "slab", "tokio", "tokio-util", @@ -816,9 +821,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.3" +version = "2.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" +checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -880,9 +885,9 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "log" -version = "0.4.20" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "matchit" @@ -913,9 +918,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.10" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" dependencies = [ "libc", "wasi", @@ -954,9 +959,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "opaque-debug" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "percent-encoding" @@ -971,7 +976,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ "fixedbitset", - "indexmap 2.2.3", + "indexmap 2.2.5", ] [[package]] @@ -991,7 +996,7 @@ checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1139,7 +1144,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1390,7 +1395,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1479,12 +1484,12 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" +checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" dependencies = [ "libc", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -1522,9 +1527,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.50" +version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" dependencies = [ "proc-macro2", "quote", @@ -1539,9 +1544,9 @@ checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" [[package]] name = "tempfile" -version = "3.10.0" +version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", "fastrand", @@ -1575,7 +1580,7 @@ checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1612,7 +1617,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1649,7 +1654,7 @@ dependencies = [ "async-stream", "async-trait", "axum", - "base64", + "base64 0.13.1", "bytes", "futures-core", "futures-util", @@ -1756,7 +1761,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.52", ] [[package]] @@ -1842,9 +1847,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "walkdir" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", @@ -1923,7 +1928,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.0", + "windows-targets 0.52.4", ] [[package]] @@ -1943,17 +1948,17 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm 0.52.4", + "windows_aarch64_msvc 0.52.4", + "windows_i686_gnu 0.52.4", + "windows_i686_msvc 0.52.4", + "windows_x86_64_gnu 0.52.4", + "windows_x86_64_gnullvm 0.52.4", + "windows_x86_64_msvc 0.52.4", ] [[package]] @@ -1964,9 +1969,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" [[package]] name = "windows_aarch64_msvc" @@ -1976,9 +1981,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" [[package]] name = "windows_i686_gnu" @@ -1988,9 +1993,9 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" [[package]] name = "windows_i686_msvc" @@ -2000,9 +2005,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" [[package]] name = "windows_x86_64_gnu" @@ -2012,9 +2017,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" [[package]] name = "windows_x86_64_gnullvm" @@ -2024,9 +2029,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" [[package]] name = "windows_x86_64_msvc" @@ -2036,9 +2041,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.0" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" [[package]] name = "zeroize" diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 56e6860f..906cf7e0 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -15,9 +15,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.79" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" +checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1" [[package]] name = "autocfg" @@ -37,6 +37,12 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64ct" version = "1.6.0" @@ -165,7 +171,7 @@ name = "cosmwasm-std" version = "1.1.9+0.8.1" source = "git+https://github.com/Finschia/cosmwasm?tag=v1.1.9+0.8.1#344858de73055acae78d7146c57abe4318631cf3" dependencies = [ - "base64", + "base64 0.13.1", "cosmwasm-crypto", "cosmwasm-derive", "derivative", @@ -297,9 +303,9 @@ dependencies = [ [[package]] name = "dyn-clone" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" [[package]] name = "ecdsa" @@ -368,6 +374,7 @@ dependencies = [ name = "finschia-std" version = "0.21.0" dependencies = [ + "base64 0.21.7", "chrono", "cosmwasm-std", "finschia-std-derive", @@ -510,9 +517,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "opaque-debug" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "pkcs8" @@ -576,7 +583,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.52", ] [[package]] @@ -634,9 +641,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" +checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "schemars" @@ -678,9 +685,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.196" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] @@ -705,13 +712,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.196" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.52", ] [[package]] @@ -727,9 +734,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.113" +version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" +checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ "itoa", "ryu", @@ -824,9 +831,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.48" +version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" dependencies = [ "proc-macro2", "quote", @@ -850,7 +857,7 @@ checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.52", ] [[package]] diff --git a/packages/finschia-std/Cargo.toml b/packages/finschia-std/Cargo.toml index ded4b7c0..8c0e10cd 100644 --- a/packages/finschia-std/Cargo.toml +++ b/packages/finschia-std/Cargo.toml @@ -14,7 +14,11 @@ prost = { version = "0.12.3", default-features = false, features = [ ] } prost-types = { version = "0.12.3", default-features = false } schemars = "0.8.8" +base64 = "0.21.7" # for query serde = { version = "1.0", default-features = false, features = ["derive"] } serde-cw-value = "0.7.0" + +[dev-dependencies] +serde_json = "1.0.40" diff --git a/packages/finschia-std/src/shim.rs b/packages/finschia-std/src/shim.rs index d0c6f7fe..022fda7d 100644 --- a/packages/finschia-std/src/shim.rs +++ b/packages/finschia-std/src/shim.rs @@ -1,8 +1,10 @@ use ::serde::{ser, Deserialize, Deserializer, Serialize, Serializer}; +use base64::prelude::*; use chrono::{DateTime, NaiveDateTime, Utc}; use cosmwasm_std::StdResult; use serde::de; use serde::de::Visitor; +use serde::ser::SerializeStruct; use std::fmt; use std::str::FromStr; @@ -198,9 +200,17 @@ macro_rules! expand_as_any { } )* - Err(serde::ser::Error::custom( - "data did not match any type that supports serialization as `Any`", - )) + if self.type_url.is_empty() { + return Err(serde::ser::Error::custom( + "type_url is empty", + )); + } + + let mut any_type = serializer.serialize_struct("Any", 2)?; + any_type.serialize_field("type_url", &self.type_url)?; + let encode_value = BASE64_STANDARD.encode(&self.value); + any_type.serialize_field("value", &encode_value)?; + any_type.end() } } @@ -260,6 +270,25 @@ macro_rules! expand_as_any { }); } )* + + // if cannot match type struct, try to find type_url + if let serde_cw_value::Value::Map(m) = &value { + if let (Some(serde_cw_value::Value::String(type_url)), Some(serde_cw_value::Value::String(encode_value))) = + (m.get(&serde_cw_value::Value::String("type_url".to_string())), m.get(&serde_cw_value::Value::String("value".to_string()))) + { + match BASE64_STANDARD.decode(encode_value) { + Ok(decoded_value) => { + return Ok(Any { + type_url: type_url.clone(), + value: decoded_value, + }); + }, + Err(e) => { + return Err(serde::de::Error::custom(format!("Failed to decode base64 value: {}", e))); + }, + } + } + } } }; @@ -285,6 +314,7 @@ macro_rules! expand_as_any { // must order by type that has more information for Any deserialization to // work correctly. Since after serialization, it currently loses @type tag. // And deserialization works by trying to iteratively match the structure. +#[cfg(not(test))] expand_as_any!( // accounts have distincted structure crate::types::cosmos::auth::v1beta1::BaseAccount, @@ -296,6 +326,10 @@ expand_as_any!( crate::types::cosmos::crypto::secp256r1::PubKey, crate::types::cosmos::crypto::ed25519::PubKey, ); +// Test environment and production environment are separated. +// In test environment, expand_as_any only includs ClearAdminProposal +#[cfg(test)] +expand_as_any!(crate::types::cosmwasm::wasm::v1::ClearAdminProposal,); macro_rules! impl_prost_types_exact_conversion { ($t:ident | $($arg:ident),*) => { @@ -365,7 +399,13 @@ pub fn cosmwasm_to_proto_coins( mod tests { use cosmwasm_std::Uint128; + use crate::types::{ + cosmos::gov::v1beta1::MsgSubmitProposal, + cosmwasm::wasm::v1::{ClearAdminProposal, UpdateAdminProposal}, + }; + use super::*; + use serde_json; #[test] fn test_coins_conversion() { @@ -385,4 +425,85 @@ mod tests { assert_eq!(coins, cosmwasm_coins); } + + #[test] + fn test_deserialization() { + let test_proposal_vec = UpdateAdminProposal { + title: "foo".to_string(), + description: "bar".to_string(), + new_admin: "alice".to_string(), + contract: "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8".to_string(), + } + .encode_to_vec(); + let test_proposal_base64 = BASE64_STANDARD.encode(&test_proposal_vec); + + let meta_data = format!( + r#"{{"content":{{"type_url":"/cosmwasm.wasm.v1.UpdateAdminProposal","value":"{}"}},"initial_deposit":[],"proposer":"link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8"}}"#, + test_proposal_base64 + ); + let result: MsgSubmitProposal = serde_json::from_str(&meta_data).unwrap(); + let expected_result = MsgSubmitProposal { + content: Some(Any { + type_url: "/cosmwasm.wasm.v1.UpdateAdminProposal".to_string(), + value: test_proposal_vec, + }), + initial_deposit: vec![], + proposer: "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8".to_string(), + }; + assert_eq!(result, expected_result); + } + + #[test] + fn test_serialization() { + let test_proposal_vec = UpdateAdminProposal { + title: "foo".to_string(), + description: "bar".to_string(), + new_admin: "alice".to_string(), + contract: "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8".to_string(), + } + .encode_to_vec(); + let test_proposal_base64 = BASE64_STANDARD.encode(&test_proposal_vec); + + let meta_data = MsgSubmitProposal { + content: Some(Any { + type_url: "/cosmwasm.wasm.v1.UpdateAdminProposal".to_string(), + value: test_proposal_vec, + }), + initial_deposit: vec![], + proposer: "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8".to_string(), + }; + let result = serde_json::to_string(&meta_data).unwrap(); + let expected_result = format!( + r#"{{"content":{{"type_url":"/cosmwasm.wasm.v1.UpdateAdminProposal","value":"{}"}},"initial_deposit":[],"proposer":"link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8"}}"#, + test_proposal_base64 + ); + assert_eq!(result, expected_result); + } + + #[test] + fn integration_test_serialization_and_deserialization_with_origin() { + let test_proposal_vec = ClearAdminProposal { + title: "foo".to_string(), + description: "bar".to_string(), + contract: "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8".to_string(), + } + .encode_to_vec(); + let test_proposal_base64 = BASE64_STANDARD.encode(&test_proposal_vec); + + // Any type which is not included in the expand_as_any list + let meta_data = format!( + r#"{{"content":{{"type_url":"/cosmwasm.wasm.v1.ClearAdminProposal","value":"{}"}}, "initial_deposit": [], "proposer": "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8"}}"#, + test_proposal_base64 + ); + let deserialized_data: MsgSubmitProposal = serde_json::from_str(&meta_data).unwrap(); + let result = serde_json::to_string(&deserialized_data).unwrap(); + + // Any type which is included in the expand_as_any list + let macro_data = r#"{"content":{"title":"foo","description":"bar", "contract":"link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8"}, "initial_deposit": [], "proposer": "link14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sgf2vn8"}"#; + let expected_deserialized_data: MsgSubmitProposal = + serde_json::from_str(macro_data).unwrap(); + let expected_result = serde_json::to_string(&expected_deserialized_data).unwrap(); + + assert_eq!(result, expected_result); + } }