-
Notifications
You must be signed in to change notification settings - Fork 1k
/
tx_value.rs
128 lines (117 loc) · 3.43 KB
/
tx_value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{ruint::UintTryFrom, U256};
use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};
/// 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,
RlpEncodableWrapper,
RlpDecodableWrapper,
)]
pub struct TxValue(U256);
impl From<TxValue> for U256 {
#[inline]
fn from(value: TxValue) -> U256 {
value.0
}
}
impl<T> From<T> for TxValue
where
U256: UintTryFrom<T>,
{
#[inline]
#[track_caller]
fn from(value: T) -> Self {
Self(U256::uint_try_from(value).unwrap())
}
}
/// 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.
/// 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 {
#[inline]
fn to_compact<B>(self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
#[cfg(feature = "optimism")]
{
self.0.to_compact(buf)
}
#[cfg(not(feature = "optimism"))]
{
self.0.to::<u128>().to_compact(buf)
}
}
#[inline]
fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
#[cfg(feature = "optimism")]
{
let (i, buf) = U256::from_compact(buf, identifier);
(TxValue(i), buf)
}
#[cfg(not(feature = "optimism"))]
{
let (i, buf) = u128::from_compact(buf, identifier);
(TxValue::from(i), buf)
}
}
}
impl std::fmt::Display for TxValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<U256> for TxValue {
fn eq(&self, other: &U256) -> bool {
self.0.eq(other)
}
}
#[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")]
{
U256::arbitrary(u).map(Self)
}
#[cfg(not(feature = "optimism"))]
{
u128::arbitrary(u).map(Self::from)
}
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl proptest::arbitrary::Arbitrary for TxValue {
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)
}
#[cfg(not(feature = "optimism"))]
{
proptest::prelude::any::<u128>().prop_map(Self::from)
}
}
#[cfg(feature = "optimism")]
type Strategy = proptest::arbitrary::Mapped<U256, Self>;
#[cfg(not(feature = "optimism"))]
type Strategy = proptest::arbitrary::Mapped<u128, Self>;
}