From bf7b6f8d2e38bcd09c59b3414bc9fea87cec0ad7 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:34:54 +0100 Subject: [PATCH] fix: manually implements `arbitrary` for `TxValue` (#4907) --- crates/primitives/src/transaction/tx_value.rs | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/transaction/tx_value.rs b/crates/primitives/src/transaction/tx_value.rs index b4ed61d96f..e9a5f536e9 100644 --- a/crates/primitives/src/transaction/tx_value.rs +++ b/crates/primitives/src/transaction/tx_value.rs @@ -5,13 +5,19 @@ use crate::{ U256, }; use alloy_rlp::{Decodable, Encodable, Error}; -use reth_codecs::{derive_arbitrary, Compact}; +use reth_codecs::{add_arbitrary_tests, Compact}; use serde::{Deserialize, Serialize}; +#[cfg(any(test, feature = "arbitrary"))] +use proptest::{ + arbitrary::ParamsFor, + strategy::{BoxedStrategy, Strategy}, +}; + /// TxValue is the type of the `value` field in the various Ethereum transactions structs. /// While the field is 256 bits, for many chains it's not possible for the field to use /// this full precision, hence we use a wrapper type to allow for overriding of encoding. -#[derive_arbitrary(compact, rlp)] +#[add_arbitrary_tests(compact, rlp)] #[derive(Default, Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] pub struct TxValue(U256); @@ -109,3 +115,38 @@ impl Compact for TxValue { } } } + +#[cfg(any(test, feature = "arbitrary"))] +impl<'a> arbitrary::Arbitrary<'a> for TxValue { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + #[cfg(feature = "value-256")] + { + Ok(Self(U256::arbitrary(u)?)) + } + + #[cfg(not(feature = "value-256"))] + { + Ok(Self::try_from(u128::arbitrary(u)?).expect("to fit")) + } + } +} + +#[cfg(any(test, feature = "arbitrary"))] +impl proptest::arbitrary::Arbitrary for TxValue { + type Parameters = ParamsFor<()>; + fn arbitrary_with(_: Self::Parameters) -> Self::Strategy { + #[cfg(feature = "value-256")] + { + proptest::prelude::any::().prop_map(Self).boxed() + } + + #[cfg(not(feature = "value-256"))] + { + proptest::prelude::any::() + .prop_map(|num| Self::try_from(num).expect("to fit")) + .boxed() + } + } + + type Strategy = BoxedStrategy; +}