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

enha: more serde tests #1568

Merged
merged 1 commit into from
Jul 30, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ testcontainers = "=0.15.0"
testcontainers-modules = { version = "=0.3.5", features = ["postgres"] }

# test
fake = { version = "=2.9.2", features = ["derive"] }
fake = { version = "=2.9.2", features = ["chrono", "derive"] }

# ------------------------------------------------------------------------------
# Platform specific dependencies
Expand Down
9 changes: 6 additions & 3 deletions src/eth/primitives/call_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
use crate::eth::primitives::Wei;

#[derive(Debug, Clone, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)]
pub struct CallInput {
#[serde(rename = "from")]
pub from: Option<Address>,

#[serde(rename = "to")]
pub to: Option<Address>,

#[serde(default)]
#[serde(rename = "value", default)]
pub value: Wei,

#[serde(alias = "input", default)]
#[serde(rename = "data", alias = "input", default)]
pub data: Bytes,
}
16 changes: 12 additions & 4 deletions src/eth/primitives/ecdsa_rs.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use display_json::DebugAsJson;
use ethereum_types::U256;
use fake::Dummy;
use fake::Faker;

use crate::gen_newtype_from;

// Type representing `r` and `s` variables
// from the ECDSA signature
#[derive(Clone, Copy)]
// Type representing `r` and `s` variables from the ECDSA signature.
#[derive(DebugAsJson, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EcdsaRs(U256);

impl From<EcdsaRs> for U256 {
Expand All @@ -13,7 +15,13 @@ impl From<EcdsaRs> for U256 {
}
}

impl Dummy<Faker> for EcdsaRs {
fn dummy_with_rng<R: rand::prelude::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
Self(U256([rng.next_u64(), rng.next_u64(), rng.next_u64(), rng.next_u64()]))
}
}

// -----------------------------------------------------------------------------
// Conversions: Self -> Other
// -----------------------------------------------------------------------------
gen_newtype_from!(self = EcdsaRs, other = i64, [u8; 32]);
gen_newtype_from!(self = EcdsaRs, other = u8, u16, u32, u64, u128, U256, i8, i16, i32, i64, i128, [u8; 32]);
16 changes: 12 additions & 4 deletions src/eth/primitives/ecdsa_v.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use display_json::DebugAsJson;
use ethereum_types::U64;
use fake::Dummy;
use fake::Faker;

use crate::gen_newtype_from;

// Type representing `v` variable
// from the ECDSA signature
#[derive(Clone, Copy)]
// Type representing `v` variable from the ECDSA signature.
#[derive(DebugAsJson, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EcdsaV(U64);

impl From<EcdsaV> for U64 {
Expand All @@ -13,7 +15,13 @@ impl From<EcdsaV> for U64 {
}
}

impl Dummy<Faker> for EcdsaV {
fn dummy_with_rng<R: rand::prelude::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
Self::from(rng.next_u64())
}
}

// -----------------------------------------------------------------------------
// Conversions: Self -> Other
// -----------------------------------------------------------------------------
gen_newtype_from!(self = EcdsaV, other = i32, [u8; 8]);
gen_newtype_from!(self = EcdsaV, other = u8, u16, u32, u64, U64, i8, i16, i32, [u8; 8]);
2 changes: 1 addition & 1 deletion src/eth/primitives/execution_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use display_json::DebugAsJson;

#[derive(DebugAsJson, Clone, Copy, Default, derive_more::Add, derive_more::AddAssign, serde::Serialize)]
#[derive(DebugAsJson, Clone, Copy, Default, PartialEq, Eq, derive_more::Add, derive_more::AddAssign, fake::Dummy, serde::Serialize, serde::Deserialize)]
pub struct EvmExecutionMetrics {
/// Number of account reads during EVM execution.
pub account_reads: usize,
Expand Down
11 changes: 5 additions & 6 deletions src/eth/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ mod tests {
// FIX: gen_test_serde!(ExecutionChanges);
// FIX: gen_test_serde!(TransactionMined);
// 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);
Expand All @@ -141,15 +136,19 @@ mod tests {
// TODO: gen_test_serde!(StratusError);
// TODO: gen_test_serde!(TransactionExecution);
// TODO: gen_test_serde!(TransactionStage);
// Type aliases
gen_test_serde!(Account);
gen_test_serde!(Address);
gen_test_serde!(BlockHeader);
gen_test_serde!(BlockNumber);
gen_test_serde!(Bytes);
gen_test_serde!(CallInput);
gen_test_serde!(ChainId);
gen_test_serde!(CodeHash);
gen_test_serde!(DateTimeNow);
gen_test_serde!(Difficulty);
gen_test_serde!(EcdsaRs);
gen_test_serde!(EcdsaV);
gen_test_serde!(EvmExecutionMetrics);
gen_test_serde!(ExecutionResult);
gen_test_serde!(Gas);
gen_test_serde!(Hash);
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/now.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use chrono::DateTime;
use chrono::Utc;
use display_json::DebugAsJson;

/// DateTime that automatically sets the current time when created.
#[derive(Debug, Clone, derive_more::Deref, serde::Serialize)]
#[derive(DebugAsJson, Clone, PartialEq, Eq, derive_more::Deref, fake::Dummy, serde::Serialize, serde::Deserialize)]
pub struct DateTimeNow(#[deref] DateTime<Utc>);

impl Default for DateTimeNow {
Expand Down