test for RPC block serialize && deserialize

This commit is contained in:
Svyatoslav Nikolsky 2016-12-09 22:18:44 +03:00
parent b279b4f170
commit 1b9afd4e01
1 changed files with 68 additions and 2 deletions

View File

@ -12,7 +12,7 @@ pub enum GetBlockResponse {
}
/// Verbose block information
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Block {
/// Block hash
pub hash: H256,
@ -55,8 +55,74 @@ pub struct Block {
#[cfg(test)]
mod tests {
use super::super::bytes::Bytes;
use super::super::hash::H256;
use serde_json;
use super::*;
#[test]
fn block_serialize() {
let block = Block {
version_hex: Bytes::new(vec![0]),
..Default::default()
};
assert_eq!(serde_json::to_string(&block).unwrap(), r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":0,"version":0,"versionHex":"00","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":0,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"0000000000000000000000000000000000000000000000000000000000000000","previousblockhash":null,"nextblockhash":null}"#);
let block = Block {
hash: H256::from(1),
confirmations: -1,
size: 500000,
strippedsize: 444444,
weight: 5236235,
height: 3513513,
version: 1,
version_hex: Bytes::new(vec![1]),
merkleroot: H256::from(2),
tx: vec![H256::from(3), H256::from(4)],
time: 111,
mediantime: 100,
nonce: 124,
bits: 13513,
difficulty: 555.555,
chainwork: H256::from(3),
previousblockhash: Some(H256::from(4)),
nextblockhash: Some(H256::from(5)),
};
assert_eq!(serde_json::to_string(&block).unwrap(), r#"{"hash":"0100000000000000000000000000000000000000000000000000000000000000","confirmations":-1,"size":500000,"strippedsize":444444,"weight":5236235,"height":3513513,"version":1,"versionHex":"01","merkleroot":"0200000000000000000000000000000000000000000000000000000000000000","tx":["0300000000000000000000000000000000000000000000000000000000000000","0400000000000000000000000000000000000000000000000000000000000000"],"time":111,"mediantime":100,"nonce":124,"bits":13513,"difficulty":555.555,"chainwork":"0300000000000000000000000000000000000000000000000000000000000000","previousblockhash":"0400000000000000000000000000000000000000000000000000000000000000","nextblockhash":"0500000000000000000000000000000000000000000000000000000000000000"}"#);
}
}
#[test]
fn block_deserialize() {
let block = Block {
version_hex: Bytes::new(vec![0]),
..Default::default()
};
assert_eq!(
serde_json::from_str::<Block>(r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":0,"version":0,"versionHex":"00","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":0,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"0000000000000000000000000000000000000000000000000000000000000000","previousblockhash":null,"nextblockhash":null}"#).unwrap(),
block);
let block = Block {
hash: H256::from(1),
confirmations: -1,
size: 500000,
strippedsize: 444444,
weight: 5236235,
height: 3513513,
version: 1,
version_hex: Bytes::new(vec![1]),
merkleroot: H256::from(2),
tx: vec![H256::from(3), H256::from(4)],
time: 111,
mediantime: 100,
nonce: 124,
bits: 13513,
difficulty: 555.555,
chainwork: H256::from(3),
previousblockhash: Some(H256::from(4)),
nextblockhash: Some(H256::from(5)),
};
assert_eq!(
serde_json::from_str::<Block>(r#"{"hash":"0100000000000000000000000000000000000000000000000000000000000000","confirmations":-1,"size":500000,"strippedsize":444444,"weight":5236235,"height":3513513,"version":1,"versionHex":"01","merkleroot":"0200000000000000000000000000000000000000000000000000000000000000","tx":["0300000000000000000000000000000000000000000000000000000000000000","0400000000000000000000000000000000000000000000000000000000000000"],"time":111,"mediantime":100,"nonce":124,"bits":13513,"difficulty":555.555,"chainwork":"0300000000000000000000000000000000000000000000000000000000000000","previousblockhash":"0400000000000000000000000000000000000000000000000000000000000000","nextblockhash":"0500000000000000000000000000000000000000000000000000000000000000"}"#).unwrap(),
block);
}
}