Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matching serializer for app_hash field of block::Header struct #188

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub struct Header {
pub consensus_hash: Hash,

/// State after txs from the previous block
#[serde(deserialize_with = "serializers::parse_hex")]
#[serde(
serialize_with = "serializers::serialize_hex",
deserialize_with = "serializers::parse_hex"
)]
pub app_hash: Vec<u8>,

/// Root hash of all results from the txs from the previous block
Expand Down Expand Up @@ -94,3 +97,15 @@ pub struct Version {
)]
pub app: u64,
}

#[cfg(test)]
mod tests {
use super::Header;
use crate::test::test_serialization_roundtrip;

#[test]
fn serialization_roundtrip() {
let json_data = include_str!("../../tests/support/serialization/block/header.json");
test_serialization_roundtrip::<Header>(json_data);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
3 changes: 3 additions & 0 deletions tendermint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub mod validator;
mod version;
pub mod vote;

#[cfg(test)]
mod test;

pub use crate::genesis::Genesis;
pub use crate::{
block::Block,
Expand Down
27 changes: 27 additions & 0 deletions tendermint/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;

/// Test that a struct `T` can be:
///
/// - parsed out of the provided JSON data
/// - serialized back to JSON
/// - parsed back from the serialized JSON of the previous step
/// - that the two parsed structs are equal according to their `PartialEq` impl
pub fn test_serialization_roundtrip<T>(json_data: &str)
where
T: Debug + PartialEq + Serialize + DeserializeOwned,
{
let parsed0 = serde_json::from_str::<T>(json_data);
assert!(parsed0.is_ok());
let parsed0 = parsed0.unwrap();

let serialized = serde_json::to_string(&parsed0);
assert!(serialized.is_ok());
let serialized = serialized.unwrap();

let parsed1 = serde_json::from_str::<T>(&serialized);
assert!(parsed1.is_ok());
let parsed1 = parsed1.unwrap();

assert_eq!(parsed0, parsed1);
}
27 changes: 27 additions & 0 deletions tendermint/tests/support/serialization/block/header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": {
"block": "10",
"app": "0"
},
"chain_id": "cosmoshub-1",
"height": "15",
"time": "2019-03-13T23:09:34.503701042Z",
"num_txs": "2",
"total_txs": "4",
"last_block_id": {
"hash": "42C70F10EF1835CED7248114514B4EF3D06F0D7FD24F6486E3315DEE310D305C",
"parts": {
"total": "1",
"hash": "F51D1B8E6ED859CE23F6B0539E0101653ED4025B13DAA3E76FCC779D5FD96ABE"
}
},
"last_commit_hash": "C499C138BCABA4D40D68A1446F6E5DE1965E07DF17EEACE1A69C1C9B1B8AC5AB",
"data_hash": "AC6A27A91A6EF9057D1A33F7944DD9EDD5FC1A3CA49E04EF0801A17FF01B4412",
"validators_hash": "EB25B1ACF639219180EB77AFC67E75A51A7CA0D666123E514B6882EC38868652",
"next_validators_hash": "EB25B1ACF639219180EB77AFC67E75A51A7CA0D666123E514B6882EC38868652",
"consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C",
"app_hash": "0A5826D21A3B0B341C843A0E4946AC787EC9B42A7DC1BEAA344C03C43943B179",
"last_results_hash": "",
"evidence_hash": "",
"proposer_address": "CC05882978FC5FDD6A7721687E14C0299AE004B8"
}