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 13, 2023
1 parent 4220ae9 commit 6b11dd1
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 67 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
6 changes: 6 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ proptest.workspace = true
proptest-derive.workspace = true
assert_matches.workspace = true
toml = "0.7.4"
# value-256 is needed for the main_codec proptests to pass
reth-primitives = { path = ".", features = ["value-256"] }


# necessary so we don't hit a "undeclared 'std'":
# https://github.com/paradigmxyz/reth/pull/177#discussion_r1021172198
Expand All @@ -98,6 +101,9 @@ pprof = { version = "0.12", features = ["flamegraph", "frame-pointer", "criterio
default = []
arbitrary = ["revm-primitives/arbitrary", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
test-utils = []
# value-256 controls whether transaction Value fields are DB-encoded as 256 bits instead of the
# default of 128 bits.
value-256 = ["reth-codecs/value-256"]

[[bench]]
name = "recover_ecdsa_crit"
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 @@ -97,7 +97,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 @@ -131,7 +131,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 6b11dd1

Please sign in to comment.