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

chore: improve codec for TxValue #5838

Merged
merged 3 commits into from
Dec 21, 2023
Merged
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
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
Loading