Skip to content

Commit

Permalink
make Value a wrapper type of U256 for custom to/from compact
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-bayardo committed Sep 3, 2023
1 parent 94d9210 commit 492e77d
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 68 deletions.
2 changes: 1 addition & 1 deletion crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ mod tests {
gas_price: 0x28f000fff,
gas_limit: 10,
to: TransactionKind::Call(Address::default()),
value: U256::from(3),
value: 3_u64.into(),
input: Bytes::from(vec![1, 2]),
access_list: Default::default(),
});
Expand Down
6 changes: 3 additions & 3 deletions crates/interfaces/src/test_utils/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn random_tx<R: Rng>(rng: &mut R) -> Transaction {
gas_price: rng.gen::<u16>().into(),
gas_limit: rng.gen::<u16>().into(),
to: TransactionKind::Call(Address::random()),
value: U256::from(rng.gen::<u16>()),
value: U256::from(rng.gen::<u16>()).into(),
input: Bytes::default(),
})
}
Expand Down Expand Up @@ -395,7 +395,7 @@ mod test {
nonce: 0x42,
gas_limit: 44386,
to: TransactionKind::Call(hex!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into()),
value: U256::default(),
value: 0_u64.into(),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
Expand Down Expand Up @@ -427,7 +427,7 @@ mod test {
gas_price: 20 * 10_u128.pow(9),
gas_limit: 21000,
to: TransactionKind::Call(hex!("3535353535353535353535353535353535353535").into()),
value: U256::from(10_u128.pow(18)),
value: 10_u128.pow(18).into(),
input: Bytes::default(),
});

Expand Down
8 changes: 4 additions & 4 deletions crates/net/eth-wire/src/types/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ mod test {
gas_limit: 0x2e248u64,
to:
TransactionKind::Call(hex!("3535353535353535353535353535353535353535").into()),
value: U256::from(0x200u64),
value: 0x200u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -370,7 +370,7 @@ mod test {
gas_limit: 0x33450u64,
to:
TransactionKind::Call(hex!("3535353535353535353535353535353535353535").into()),
value: U256::from(0x2d9u64),
value: 0x2d9u64.into(),
input: Default::default(),
}), Signature {
odd_y_parity: false,
Expand Down Expand Up @@ -438,7 +438,7 @@ mod test {
gas_limit: 0x2e248u64,
to:
TransactionKind::Call(hex!("3535353535353535353535353535353535353535").into()),
value: U256::from(0x200u64),
value: 0x200u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -456,7 +456,7 @@ mod test {
gas_limit: 0x33450u64,
to:
TransactionKind::Call(hex!("3535353535353535353535353535353535353535").into()),
value: U256::from(0x2d9u64),
value: 0x2d9u64.into(),
input: Default::default(),
}),
Signature {
Expand Down
28 changes: 14 additions & 14 deletions crates/net/eth-wire/src/types/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod test {
to: TransactionKind::Call(
hex!("3535353535353535353535353535353535353535").into(),
),
value: U256::from(0x200u64),
value: 0x200u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -129,7 +129,7 @@ mod test {
to: TransactionKind::Call(
hex!("3535353535353535353535353535353535353535").into(),
),
value: U256::from(0x2d9u64),
value: 0x2d9u64.into(),
input: Default::default(),
}),
Signature {
Expand Down Expand Up @@ -167,7 +167,7 @@ mod test {
to: TransactionKind::Call(
hex!("3535353535353535353535353535353535353535").into(),
),
value: U256::from(0x200u64),
value: 0x200u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -191,7 +191,7 @@ mod test {
to: TransactionKind::Call(
hex!("3535353535353535353535353535353535353535").into(),
),
value: U256::from(0x2d9u64),
value: 0x2d9u64.into(),
input: Default::default(),
}),
Signature {
Expand Down Expand Up @@ -232,7 +232,7 @@ mod test {
to: TransactionKind::Call(
hex!("cf7f9e66af820a19257a2108375b180b0ec49167").into(),
),
value: U256::from(1234u64),
value: 1234u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -257,7 +257,7 @@ mod test {
to: TransactionKind::Call(
hex!("61815774383099e24810ab832a5b2a5425c154d5").into(),
),
value: U256::from(3000000000000000000u64),
value: 3000000000000000000u64.into(),
input: Default::default(),
access_list: Default::default(),
}),
Expand All @@ -282,7 +282,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(1000000000000000u64),
value: 1000000000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -306,7 +306,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(693361000000000u64),
value: 693361000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -330,7 +330,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(1000000000000000u64),
value: 1000000000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand Down Expand Up @@ -375,7 +375,7 @@ mod test {
to: TransactionKind::Call(
hex!("cf7f9e66af820a19257a2108375b180b0ec49167").into(),
),
value: U256::from(1234u64),
value: 1234u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -400,7 +400,7 @@ mod test {
to: TransactionKind::Call(
hex!("61815774383099e24810ab832a5b2a5425c154d5").into(),
),
value: U256::from(3000000000000000000u64),
value: 3000000000000000000u64.into(),
input: Default::default(),
access_list: Default::default(),
}),
Expand All @@ -425,7 +425,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(1000000000000000u64),
value: 1000000000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -449,7 +449,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(693361000000000u64),
value: 693361000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand All @@ -473,7 +473,7 @@ mod test {
to: TransactionKind::Call(
hex!("d3e8763675e4c425df46cc3b5c0f6cbdac396046").into(),
),
value: U256::from(1000000000000000u64),
value: 1000000000000000u64.into(),
input: Default::default(),
}),
Signature {
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/tests/it/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn rng_transaction(rng: &mut impl rand::RngCore) -> TransactionSigned {
gas_price: rng.gen(),
gas_limit: rng.gen(),
to: TransactionKind::Create,
value: U256::from(rng.gen::<u128>()),
value: rng.gen::<u128>().into(),
input: Bytes::from(vec![1, 2]),
access_list: Default::default(),
});
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub use transaction::{
IntoRecoveredTransaction, InvalidTransactionError, PooledTransactionsElement,
PooledTransactionsElementEcRecovered, Signature, Transaction, TransactionKind, TransactionMeta,
TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, TxEip1559, TxEip2930,
TxEip4844, TxLegacy, TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID,
TxEip4844, TxLegacy, TxType, Value, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID,
LEGACY_TX_TYPE_ID,
};
pub use withdrawal::Withdrawal;
Expand Down Expand Up @@ -129,7 +129,7 @@ pub use ethers_core::{
pub use revm_primitives::{B160 as H160, B256 as H256, U256};
pub use ruint::{
aliases::{U128, U8},
UintTryTo,
ToUintError, UintTryFrom, UintTryTo,
};

#[doc(hidden)]
Expand Down
8 changes: 4 additions & 4 deletions crates/primitives/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::access_list::AccessList;
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, H256, U256};
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, Value, H256};
use bytes::BytesMut;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header};
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct TxEip1559 {
/// be transferred to the message call’s recipient or,
/// in the case of contract creation, as an endowment
/// to the newly created account; formally Tv.
pub value: U256,
pub value: Value,
/// The accessList specifies a list of addresses and storage keys;
/// these addresses and storage keys are added into the `accessed_addresses`
/// and `accessed_storage_keys` global sets (introduced in EIP-2929).
Expand Down Expand Up @@ -181,7 +181,7 @@ impl TxEip1559 {
mem::size_of::<u128>() + // max_fee_per_gas
mem::size_of::<u128>() + // max_priority_fee_per_gas
self.to.size() + // to
mem::size_of::<U256>() + // value
mem::size_of::<Value>() + // value
self.access_list.size() + // access_list
self.input.len() // input
}
Expand Down Expand Up @@ -231,7 +231,7 @@ mod tests {
nonce: 0x42,
gas_limit: 44386,
to: TransactionKind::Call( hex!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into()),
value: U256::default(),
value: 0_u64.into(),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
Expand Down
10 changes: 5 additions & 5 deletions crates/primitives/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::access_list::AccessList;
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, H256, U256};
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, Value, H256};
use bytes::BytesMut;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header};
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct TxEip2930 {
/// be transferred to the message call’s recipient or,
/// in the case of contract creation, as an endowment
/// to the newly created account; formally Tv.
pub value: U256,
pub value: Value,
/// The accessList specifies a list of addresses and storage keys;
/// these addresses and storage keys are added into the `accessed_addresses`
/// and `accessed_storage_keys` global sets (introduced in EIP-2929).
Expand All @@ -58,7 +58,7 @@ impl TxEip2930 {
mem::size_of::<u128>() + // gas_price
mem::size_of::<u64>() + // gas_limit
self.to.size() + // to
mem::size_of::<U256>() + // value
mem::size_of::<Value>() + // value
self.access_list.size() + // access_list
self.input.len() // input
}
Expand Down Expand Up @@ -193,7 +193,7 @@ mod tests {
gas_price: 1,
gas_limit: 2,
to: TransactionKind::Create,
value: U256::from(3),
value: 3_u64.into(),
input: Bytes::from(vec![1, 2]),
access_list: Default::default(),
});
Expand All @@ -216,7 +216,7 @@ mod tests {
gas_price: 1,
gas_limit: 2,
to: TransactionKind::Call(Address::default()),
value: U256::from(3),
value: 3_u64.into(),
input: Bytes::from(vec![1, 2]),
access_list: Default::default(),
});
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
BYTES_PER_COMMITMENT, BYTES_PER_PROOF,
},
kzg_to_versioned_hash, Bytes, ChainId, Signature, Transaction, TransactionKind,
TransactionSigned, TxHash, TxType, EIP4844_TX_TYPE_ID, H256, U256,
TransactionSigned, TxHash, TxType, Value, EIP4844_TX_TYPE_ID, H256,
};
use bytes::BytesMut;
use reth_codecs::{main_codec, Compact};
Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct TxEip4844 {
/// As ethereum circulation is around 120mil eth as of 2022 that is around
/// 120000000000000000000000000 wei we are safe to use u128 as its max number is:
/// 340282366920938463463374607431768211455
pub value: U256,
pub value: Value,
/// The accessList specifies a list of addresses and storage keys;
/// these addresses and storage keys are added into the `accessed_addresses`
/// and `accessed_storage_keys` global sets (introduced in EIP-2929).
Expand Down Expand Up @@ -247,7 +247,7 @@ impl TxEip4844 {
mem::size_of::<u128>() + // max_fee_per_gas
mem::size_of::<u128>() + // max_priority_fee_per_gas
self.to.size() + // to
mem::size_of::<U256>() + // value
mem::size_of::<Value>() + // value
self.access_list.size() + // access_list
self.input.len() + // input
self.blob_versioned_hashes.capacity() * mem::size_of::<H256>() + // blob hashes size
Expand Down
8 changes: 4 additions & 4 deletions crates/primitives/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, H256, U256};
use crate::{keccak256, Bytes, ChainId, Signature, TransactionKind, TxType, Value, H256};
use bytes::BytesMut;
use reth_codecs::{main_codec, Compact};
use reth_rlp::{length_of_length, Encodable, Header};
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct TxLegacy {
/// be transferred to the message call’s recipient or,
/// in the case of contract creation, as an endowment
/// to the newly created account; formally Tv.
pub value: U256,
pub value: Value,
/// Input has two uses depending if transaction is Create or Call (if `to` field is None or
/// Some). pub init: An unlimited size byte array specifying the
/// EVM-code for the account initialisation procedure CREATE,
Expand All @@ -51,7 +51,7 @@ impl TxLegacy {
mem::size_of::<u128>() + // gas_price
mem::size_of::<u64>() + // gas_limit
self.to.size() + // to
mem::size_of::<U256>() + // value
mem::size_of::<Value>() + // value
self.input.len() // input
}

Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
gas_price: 0xfa56ea00,
gas_limit: 119902,
to: TransactionKind::Call( hex!("06012c8cf97bead5deae237070f9587f8e7a266d").into()),
value: U256::from(0x1c6bf526340000u64),
value: 0x1c6bf526340000_u64.into(),
input: hex!("f7d8c88300000000000000000000000000000000000000000000000000000000000cee6100000000000000000000000000000000000000000000000000000000000ac3e1").into(),
});

Expand Down
Loading

0 comments on commit 492e77d

Please sign in to comment.