Skip to content

Commit

Permalink
test: primitives serde test with serde_json and bincode
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed Jul 30, 2024
1 parent b66637e commit 507580e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 40 deletions.
34 changes: 33 additions & 1 deletion src/eth/primitives/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl BlockHeader {
gas_limit: Gas::ZERO,
bloom: LogsBloom::default(),
timestamp,
parent_hash: number.prev().map(|n| n.hash()).unwrap_or(Hash::zero()),
parent_hash: number.prev().map(|n| n.hash()).unwrap_or(Hash::ZERO),
author: Address::default(),
extra_data: Bytes::default(),
miner: Address::default(),
Expand Down Expand Up @@ -239,3 +239,35 @@ impl From<BlockHeader> for SubscriptionMessage {
Self::from_json(&ethers_block).expect_infallible()
}
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use crate::eth::primitives::BlockHeader;
use crate::eth::primitives::BlockNumber;
use crate::eth::primitives::Hash;
use crate::eth::primitives::UnixTime;

#[test]
fn block_header_hash_calculation() {
let header = BlockHeader::new(BlockNumber::ZERO, UnixTime::from(1234567890));
assert_eq!(header.hash.to_string(), "0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce");
}

#[test]
fn block_header_parent_hash() {
let header = BlockHeader::new(BlockNumber::ONE, UnixTime::from(1234567891));
assert_eq!(
header.parent_hash.to_string(),
"0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce"
);
}

#[test]
fn block_header_genesis_parent_hash() {
let header = BlockHeader::new(BlockNumber::ZERO, UnixTime::from(1234567890));
assert_eq!(header.parent_hash, Hash::ZERO);
}
}
7 changes: 2 additions & 5 deletions src/eth/primitives/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::gen_newtype_from;
pub struct Hash(pub H256);

impl Hash {
pub const ZERO: Hash = Hash(H256::zero());

/// Creates a hash from the given bytes.
pub const fn new(bytes: [u8; 32]) -> Self {
Self(H256(bytes))
Expand All @@ -30,11 +32,6 @@ impl Hash {
Self(H256::random())
}

/// Returns the zero hash
pub fn zero() -> Self {
Self(H256::zero())
}

pub fn into_hash_partition(self) -> i16 {
let n = self.0.to_low_u64_ne() % 10;
n as i16
Expand Down
67 changes: 38 additions & 29 deletions src/eth/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,53 +114,62 @@ mod tests {
type TransactionExecutionValueChangeSlot = ExecutionValueChange<Slot>;
type TransactionExecutionValueChangeWei = ExecutionValueChange<Wei>;

// TODO: add more serde tests for new primitives
gen_test_serde!(Address);
// TODO: gen_test_serde!(BlockFilter);
// TODO: gen_test_serde!(CallInput);
// TODO: gen_test_serde!(DateTimeNow);
// TODO: gen_test_serde!(EcdsaRs);
// TODO: gen_test_serde!(EcdsaV);
// TODO: gen_test_serde!(EvmExecutionMetrics);
// TODO: gen_test_serde!(ExecutionConflict);
// TODO: gen_test_serde!(ExecutionConflicts);
// TODO: gen_test_serde!(ExecutionConflictsBuilder);
// TODO: gen_test_serde!(ExternalBlock);
// TODO: gen_test_serde!(ExternalReceipt);
// TODO: gen_test_serde!(ExternalReceipts);
// TODO: gen_test_serde!(ExternalTransaction);
// TODO: gen_test_serde!(ExternalTransactionExecution);
// TODO: gen_test_serde!(LocalTransactionExecution);
// TODO: gen_test_serde!(LogFilter);
// TODO: gen_test_serde!(LogFilterInput);
// TODO: gen_test_serde!(LogFilterInputTopic);
// TODO: gen_test_serde!(PendingBlock);
// TODO: gen_test_serde!(StratusError);
// TODO: gen_test_serde!(TransactionExecution);
// TODO: gen_test_serde!(TransactionStage);
gen_test_serde!(Account);
gen_test_serde!(Address);
gen_test_serde!(Block);
gen_test_serde!(BlockHeader);
gen_test_serde!(BlockNumber);
gen_test_serde!(Bytes);
gen_test_serde!(ChainId);
gen_test_serde!(CodeHash);
gen_test_serde!(Difficulty);
gen_test_serde!(EvmExecution);
gen_test_serde!(ExecutionAccountChanges);
gen_test_serde!(ExecutionChanges);
gen_test_serde!(ExecutionResult);
gen_test_serde!(Gas);
gen_test_serde!(Hash);
gen_test_serde!(Index);
gen_test_serde!(Log);
gen_test_serde!(LogMined);
gen_test_serde!(LogTopic);
gen_test_serde!(MinerNonce);
gen_test_serde!(Nonce);
gen_test_serde!(Size);
gen_test_serde!(Slot);
gen_test_serde!(SlotIndex);
gen_test_serde!(SlotValue);
gen_test_serde!(ExecutionAccountChanges);
gen_test_serde!(ExecutionResult);
gen_test_serde!(TransactionInput);
gen_test_serde!(TransactionMined);
gen_test_serde!(UnixTime);
gen_test_serde!(Wei);

// Type aliases
gen_test_serde!(TransactionExecutionValueChangeBytes);
gen_test_serde!(TransactionExecutionValueChangeNonce);
gen_test_serde!(TransactionExecutionValueChangeOptionString);
gen_test_serde!(TransactionExecutionValueChangeSlot);
gen_test_serde!(TransactionExecutionValueChangeWei);
gen_test_serde!(TransactionInput);
gen_test_serde!(TransactionMined);
gen_test_serde!(Wei);

// TODO: move these tests to block_header module
#[test]
fn block_hash_calculation() {
let header = BlockHeader::new(BlockNumber::ZERO, UnixTime::from(1234567890));
assert_eq!(header.hash.to_string(), "0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce");
}

#[test]
fn parent_hash() {
let header = BlockHeader::new(BlockNumber::ONE, UnixTime::from(1234567891));
assert_eq!(
header.parent_hash.to_string(),
"0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce"
);
}

#[test]
fn genesis_parent_hash() {
let header = BlockHeader::new(BlockNumber::ZERO, UnixTime::from(1234567890));
assert_eq!(header.parent_hash, Hash::zero());
}
}
25 changes: 20 additions & 5 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,27 @@ macro_rules! gen_test_serde {
($type:ty) => {
paste::paste! {
#[test]
pub fn [<serde_ $type:snake>]() {
use $crate::ext::ResultExt;
pub fn [<serde_json_ $type:snake>]() {
let value_original = <fake::Faker as fake::Fake>::fake::<$type>(&fake::Faker);
let value_encoded = serde_json::to_string(&value_original).unwrap();

let value = <fake::Faker as fake::Fake>::fake::<$type>(&fake::Faker);
let json = serde_json::to_string(&value).expect_infallible();
assert_eq!(serde_json::from_str::<$type>(&json).unwrap(), value);
let value_decoded = serde_json::from_str::<$type>(&value_encoded).unwrap();
assert_eq!(value_decoded, value_original);

let value_reencoded = serde_json::to_string(&value_original).unwrap();
assert_eq!(value_reencoded, value_encoded);
}

#[test]
pub fn [<serde_bincode_ $type:snake>]() {
let value_original = <fake::Faker as fake::Fake>::fake::<$type>(&fake::Faker);
let value_encoded = bincode::serialize(&value_original).unwrap();

let value_decoded = bincode::deserialize::<$type>(&value_encoded).unwrap();
assert_eq!(value_decoded, value_original);

let value_reencoded = bincode::serialize(&value_original).unwrap();
assert_eq!(value_reencoded, value_encoded);
}
}
};
Expand Down

0 comments on commit 507580e

Please sign in to comment.