test: serde roundtrip test for rpc types (#1571)

This commit is contained in:
Aditya Pandey
2023-02-27 20:58:29 +05:30
committed by GitHub
parent 56394eec2c
commit 494a9e88b2
3 changed files with 105 additions and 0 deletions

View File

@ -330,4 +330,43 @@ mod tests {
let full = false;
assert_eq!(BlockTransactionsKind::Hashes, full.into());
}
#[test]
fn serde_block() {
let block = Block {
header: Header {
hash: Some(H256::from_low_u64_be(1)),
parent_hash: H256::from_low_u64_be(2),
uncles_hash: H256::from_low_u64_be(3),
author: Address::from_low_u64_be(4),
miner: Address::from_low_u64_be(4),
state_root: H256::from_low_u64_be(5),
transactions_root: H256::from_low_u64_be(6),
receipts_root: H256::from_low_u64_be(7),
withdrawals_root: Some(H256::from_low_u64_be(8)),
number: Some(U256::from(9)),
gas_used: U256::from(10),
gas_limit: U256::from(11),
extra_data: Bytes::from(vec![1, 2, 3]),
logs_bloom: Bloom::default(),
timestamp: U256::from(12),
difficulty: U256::from(13),
mix_hash: H256::from_low_u64_be(14),
nonce: Some(H64::from_low_u64_be(15)),
},
total_difficulty: U256::from(100000),
uncles: vec![H256::from_low_u64_be(17)],
transactions: BlockTransactions::Hashes(vec![H256::from_low_u64_be(18)]),
size: Some(U256::from(19)),
base_fee_per_gas: Some(U256::from(20)),
withdrawals: None,
};
let serialized = serde_json::to_string(&block).unwrap();
assert_eq!(
serialized,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","author":"0x0000000000000000000000000000000000000004","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","number":"0x9","gasUsed":"0xa","gasLimit":"0xb","extraData":"0x010203","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0xc","difficulty":"0xd","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","totalDifficulty":"0x186a0","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13","baseFeePerGas":"0x14","withdrawals":null}"#
);
let deserialized: Block = serde_json::from_str(&serialized).unwrap();
assert_eq!(block, deserialized);
}
}

View File

@ -27,3 +27,33 @@ pub struct Log {
#[serde(default)]
pub removed: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json;
#[test]
fn serde_log() {
let log = Log {
address: Address::from_low_u64_be(0x1234),
topics: vec![H256::from_low_u64_be(0x1234)],
data: Bytes::from(vec![0x12, 0x34]),
block_hash: Some(H256::from_low_u64_be(0x1234)),
block_number: Some(U256::from(0x1234)),
transaction_hash: Some(H256::from_low_u64_be(0x1234)),
transaction_index: Some(U256::from(0x1234)),
log_index: Some(U256::from(0x1234)),
transaction_log_index: Some(U256::from(0x1234)),
removed: false,
};
let serialized = serde_json::to_string(&log).unwrap();
assert_eq!(
serialized,
r#"{"address":"0x0000000000000000000000000000000000001234","topics":["0x0000000000000000000000000000000000000000000000000000000000001234"],"data":"0x1234","blockHash":"0x0000000000000000000000000000000000000000000000000000000000001234","blockNumber":"0x1234","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000001234","transactionIndex":"0x1234","logIndex":"0x1234","transactionLogIndex":"0x1234","removed":false}"#
);
let deserialized: Log = serde_json::from_str(&serialized).unwrap();
assert_eq!(log, deserialized);
}
}

View File

@ -153,3 +153,39 @@ impl Transaction {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json;
#[test]
fn serde_transaction() {
let transaction = Transaction {
hash: H256::from_low_u64_be(1),
nonce: U256::from(2),
block_hash: Some(H256::from_low_u64_be(3)),
block_number: Some(U256::from(4)),
transaction_index: Some(U256::from(5)),
from: Address::from_low_u64_be(6),
to: Some(Address::from_low_u64_be(7)),
value: U256::from(8),
gas_price: Some(U128::from(9)),
gas: U256::from(10),
input: Bytes::from(vec![11, 12, 13]),
signature: Some(Signature { v: U256::from(14), r: U256::from(14), s: U256::from(14) }),
chain_id: Some(U64::from(17)),
access_list: None,
transaction_type: Some(U64::from(20)),
max_fee_per_gas: Some(U128::from(21)),
max_priority_fee_per_gas: Some(U128::from(22)),
};
let serialized = serde_json::to_string(&transaction).unwrap();
assert_eq!(
serialized,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","nonce":"0x2","blockHash":"0x0000000000000000000000000000000000000000000000000000000000000003","blockNumber":"0x4","transactionIndex":"0x5","from":"0x0000000000000000000000000000000000000006","to":"0x0000000000000000000000000000000000000007","value":"0x8","gasPrice":"0x9","gas":"0xa","maxFeePerGas":"0x15","maxPriorityFeePerGas":"0x16","input":"0x0b0c0d","r":"0xe","s":"0xe","v":"0xe","chainId":"0x11","type":"0x14"}"#
);
let deserialized: Transaction = serde_json::from_str(&serialized).unwrap();
assert_eq!(transaction, deserialized);
}
}