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: require destination for 7702 #1262

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,10 @@ impl<'a> arbitrary::Arbitrary<'a> for Header {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;
mattsse marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "serde")]
#[test]
fn header_serde() {
let raw = r#"{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","ommersHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","beneficiary":"0x0000000000000000000000000000000000000000","stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x0","gasLimit":"0x0","gasUsed":"0x0","timestamp":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x1","extraData":"0x"}"#;
Expand Down
20 changes: 9 additions & 11 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -50,10 +50,8 @@ pub struct TxEip7702 {
/// This is also known as `GasTipCap`
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub max_priority_fee_per_gas: u128,
/// The 160-bit address of the message call’s recipient or, for a contract creation
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
pub to: TxKind,
/// The 160-bit address of the message call’s recipient.
pub to: Address,
/// A scalar value equal to the number of Wei to
/// be transferred to the message call’s recipient or,
/// in the case of contract creation, as an endowment
Expand Down Expand Up @@ -263,7 +261,7 @@ impl TxEip7702 {
mem::size_of::<u64>() + // gas_limit
mem::size_of::<u128>() + // max_fee_per_gas
mem::size_of::<u128>() + // max_priority_fee_per_gas
self.to.size() + // to
mem::size_of::<Address>() + // to
mem::size_of::<U256>() + // value
self.access_list.size() + // access_list
self.input.len() + // input
Expand Down Expand Up @@ -305,7 +303,7 @@ impl Transaction for TxEip7702 {
}

fn to(&self) -> TxKind {
self.to
self.to.into()
}

fn value(&self) -> U256 {
Expand Down Expand Up @@ -391,15 +389,15 @@ mod tests {
use super::TxEip7702;
use crate::SignableTransaction;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, U256};
use alloy_primitives::{address, b256, hex, Address, Signature, U256};

#[test]
fn encode_decode_eip7702() {
let tx = TxEip7702 {
chain_id: 1,
nonce: 0x42,
gas_limit: 44386,
to: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(),
to: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6"),
value: U256::from(0_u64),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
Expand Down Expand Up @@ -430,7 +428,7 @@ mod tests {
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
gas_limit: 2,
to: TxKind::Create,
to: Address::default(),
value: U256::ZERO,
input: vec![1, 2].into(),
access_list: Default::default(),
Expand All @@ -456,7 +454,7 @@ mod tests {
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
gas_limit: 2,
to: Address::default().into(),
to: Address::default(),
value: U256::ZERO,
input: vec![1, 2].into(),
access_list: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ mod tests {
gas_limit: 3,
max_fee_per_gas: 4,
max_priority_fee_per_gas: 5,
to: Address::left_padding_from(&[5]).into(),
to: Address::left_padding_from(&[5]),
value: U256::from(6_u64),
input: vec![7].into(),
access_list: AccessList(vec![AccessListItem {
Expand Down Expand Up @@ -994,7 +994,7 @@ mod tests {
gas_limit: u128::MAX,
max_fee_per_gas: u128::MAX,
max_priority_fee_per_gas: u128::MAX,
to: Address::random().into(),
to: Address::random(),
value: U256::MAX,
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl TryFrom<Transaction> for Signed<TxEip7702> {
max_priority_fee_per_gas: tx
.max_priority_fee_per_gas
.ok_or(ConversionError::MissingMaxPriorityFeePerGas)?,
to: tx.to.into(),
to: tx.to.ok_or(ConversionError::MissingTo)?,
value: tx.value,
access_list: tx.access_list.ok_or(ConversionError::MissingAccessList)?,
authorization_list: tx
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-types-eth/src/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl TransactionRequest {
/// If required fields are missing. Use `complete_7702` to check if the
/// request can be built.
fn build_7702(self) -> Result<TxEip7702, &'static str> {
let checked_to = self.to.ok_or("Missing 'to' field for Eip7702 transaction.")?;
let to_address = self.to.ok_or("Missing 'to' field for Eip7702 transaction.")?.to().copied().ok_or("The field `to` can only be of type TxKind::Call(Account). Please change it accordingly.")?;

Ok(TxEip7702 {
chain_id: self.chain_id.unwrap_or(1),
Expand All @@ -324,7 +324,7 @@ impl TransactionRequest {
max_priority_fee_per_gas: self
.max_priority_fee_per_gas
.ok_or("Missing 'max_priority_fee_per_gas' field for Eip7702 transaction.")?,
to: checked_to,
to: to_address,
value: self.value.unwrap_or_default(),
input: self.input.into_input().unwrap_or_default(),
access_list: self.access_list.unwrap_or_default(),
Expand Down Expand Up @@ -808,7 +808,7 @@ impl From<TxEip4844Variant> for TransactionRequest {
impl From<TxEip7702> for TransactionRequest {
fn from(tx: TxEip7702) -> Self {
Self {
to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None },
to: Some(tx.to.into()),
gas: Some(tx.gas_limit),
max_fee_per_gas: Some(tx.max_fee_per_gas),
max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
Expand Down