Skip to content

Commit

Permalink
chore: improve codec for TxValue (#5838)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody authored Dec 21, 2023
1 parent b37cd83 commit 521aefe
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions crates/primitives/src/transaction/tx_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{ruint::UintTryFrom, U256};
use alloy_rlp::{Decodable, Encodable, Error};
use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};

Expand All @@ -8,7 +8,19 @@ use serde::{Deserialize, Serialize};
/// 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)]
#[derive(
Default,
Debug,
Copy,
Clone,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
RlpEncodableWrapper,
RlpDecodableWrapper,
)]
pub struct TxValue(U256);

impl From<TxValue> for U256 {
Expand All @@ -29,32 +41,6 @@ where
}
}

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> {
#[cfg(feature = "optimism")]
{
U256::decode(buf).map(Self)
}
#[cfg(not(feature = "optimism"))]
{
u128::decode(buf).map(Self::from)
}
}
}

/// As ethereum circulation on mainnet is around 120mil eth as of 2022 that is around
/// 120000000000000000000000000 wei we are safe to use u128 for TxValue's encoding
/// as its max number is 340282366920938463463374607431768211455.
Expand Down

0 comments on commit 521aefe

Please sign in to comment.