Skip to content

Commit

Permalink
chore: clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 5, 2023
1 parent 11d25c5 commit 9a991d4
Showing 1 changed file with 30 additions and 59 deletions.
89 changes: 30 additions & 59 deletions crates/primitives/src/transaction/tx_value.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,50 @@
#[allow(unused_imports)]
// suppress warning for UIntTryTo, which is required only when optimism feature is disabled
use crate::{
ruint::{ToUintError, UintTryFrom, UintTryTo},
U256,
};
use crate::{ruint::UintTryFrom, U256};
use alloy_rlp::{Decodable, Encodable, Error};
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.
#[add_arbitrary_tests(compact, rlp)]
#[derive(Default, Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxValue(U256);

impl From<TxValue> for U256 {
/// unwrap Value to U256
#[inline]
fn from(value: TxValue) -> U256 {
value.0
}
}

impl<T> From<T> for TxValue
where
Self: UintTryFrom<T>,
U256: UintTryFrom<T>,
{
/// construct a Value from misc. other uint types
fn from(value: T) -> Self {
match Self::uint_try_from(value) {
Ok(n) => n,
Err(e) => panic!("Uint conversion error: {e}"),
}
}
}

impl UintTryFrom<U256> for TxValue {
#[inline]
fn uint_try_from(value: U256) -> Result<Self, ToUintError<Self>> {
Ok(Self(value))
}
}

impl UintTryFrom<u128> for TxValue {
#[inline]
fn uint_try_from(value: u128) -> Result<Self, ToUintError<Self>> {
Ok(Self(U256::from(value)))
}
}

impl UintTryFrom<u64> for TxValue {
#[inline]
fn uint_try_from(value: u64) -> Result<Self, ToUintError<Self>> {
Ok(Self(U256::from(value)))
#[track_caller]
fn from(value: T) -> Self {
Self(U256::uint_try_from(value).unwrap())
}
}

impl Encodable for TxValue {
#[inline]
fn encode(&self, out: &mut dyn bytes::BufMut) {
self.0.encode(out)
}

#[inline]
fn length(&self) -> usize {
self.0.length()
}
}

impl Decodable for TxValue {
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, Error> {
Ok(TxValue(U256::decode(buf)?))
U256::decode(buf).map(Self)
}
}

Expand All @@ -83,7 +54,7 @@ impl Decodable for TxValue {
/// This optimization should be disabled for chains such as Optimism, where
/// some tx values may require more than 128-bit precision.
impl Compact for TxValue {
#[allow(unreachable_code)]
#[inline]
fn to_compact<B>(self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
Expand All @@ -94,14 +65,11 @@ impl Compact for TxValue {
}
#[cfg(not(feature = "optimism"))]
{
// SAFETY: For ethereum mainnet this is safe as the max value is
// 120000000000000000000000000 wei
let i: u128 = self.0.uint_try_to().expect("value could not be converted to u128");
i.to_compact(buf)
self.0.to::<u128>().to_compact(buf)
}
}

#[allow(unreachable_code)]
#[inline]
fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
#[cfg(feature = "optimism")]
{
Expand All @@ -118,35 +86,38 @@ impl Compact for TxValue {

#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for TxValue {
#[inline]
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
#[cfg(feature = "optimism")]
{
Ok(Self(U256::arbitrary(u)?))
U256::arbitrary(u).map(Self)
}

#[cfg(not(feature = "optimism"))]
{
Ok(Self::try_from(u128::arbitrary(u)?).expect("to fit"))
u128::arbitrary(u).map(Self::from)
}
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl proptest::arbitrary::Arbitrary for TxValue {
type Parameters = ParamsFor<()>;
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
#[cfg(feature = "optimism")]
type Strategy = proptest::arbitrary::Mapped<U256, Self>;
#[cfg(not(feature = "optimism"))]
type Strategy = proptest::arbitrary::Mapped<u128, Self>;
type Parameters = <U256 as proptest::arbitrary::Arbitrary>::Parameters;

#[inline]
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
use proptest::strategy::Strategy;

#[cfg(feature = "optimism")]
{
proptest::prelude::any::<U256>().prop_map(Self).boxed()
proptest::prelude::any::<U256>().prop_map(Self)
}

#[cfg(not(feature = "optimism"))]
{
proptest::prelude::any::<u128>()
.prop_map(|num| Self::try_from(num).expect("to fit"))
.boxed()
proptest::prelude::any::<u128>().prop_map(Self::from)
}
}

type Strategy = BoxedStrategy<TxValue>;
}

0 comments on commit 9a991d4

Please sign in to comment.