Skip to content

Commit

Permalink
Show simple way we could (de)serialize the whole amino type/value spiel
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Apr 11, 2020
1 parent e21179b commit 720d841
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions tendermint/src/amino_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod ed25519;
pub mod message;
pub mod ping;
pub mod proposal;
mod registered_json;
pub mod remote_error;
pub mod signature;
pub mod time;
Expand Down
36 changes: 36 additions & 0 deletions tendermint/src/amino_types/registered_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[cfg(test)]
mod test {
use crate::test::test_serialization_roundtrip;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
struct MsgUnjail {
#[serde(alias = "address")]
validator_addr: String,
// NOTE: Above shouldn't be a string but: validator_addr: Vec<u8>,
// In reality you would need to tell serde how to read bechifyed addresses instead!
// but this is orthogonal to what this code wants to show.
}

// TODO: deserves a better name
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
struct AminoJSON<T: Serialize + DeserializeOwned> {
#[serde(alias = "type")]
pub type_name: Option<String>,
#[serde(bound(deserialize = "Option<T>: Deserialize<'de>"))]
pub value: Option<T>,
}

#[test]
fn test_simple_example() {
let json_data =
r#"{"type":"cosmos-sdk/MsgUnjail","value":{"address":"cosmosvaloper1v93xxeqhg9nn6"}}"#;
let res = serde_json::from_str::<AminoJSON<MsgUnjail>>(json_data);
println!("res: {:?}", res);
assert!(res.is_ok());
let msg_unjail = res.unwrap().value;
println!("{:?}", msg_unjail);
test_serialization_roundtrip::<AminoJSON<MsgUnjail>>(&json_data);
}
}

0 comments on commit 720d841

Please sign in to comment.