From fdaf630b35f2060aa3c29d893b5741e44c69f4d2 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 15 Jul 2024 14:46:56 +0200 Subject: [PATCH] feat: add 7702 tx type (#1046) --- crates/consensus/src/constants.rs | 3 + crates/consensus/src/transaction/eip7702.rs | 446 ++++++++++++++++++++ crates/consensus/src/transaction/mod.rs | 3 + 3 files changed, 452 insertions(+) create mode 100644 crates/consensus/src/transaction/eip7702.rs diff --git a/crates/consensus/src/constants.rs b/crates/consensus/src/constants.rs index 79987a18de9..c7a7090d098 100644 --- a/crates/consensus/src/constants.rs +++ b/crates/consensus/src/constants.rs @@ -80,3 +80,6 @@ pub const EIP1559_TX_TYPE_ID: u8 = 2; /// Identifier for an EIP4844 transaction. pub const EIP4844_TX_TYPE_ID: u8 = 3; + +/// Identifier for an EIP7702 transaction. +pub const EIP7702_TX_TYPE_ID: u8 = 4; diff --git a/crates/consensus/src/transaction/eip7702.rs b/crates/consensus/src/transaction/eip7702.rs new file mode 100644 index 00000000000..e8934d474d3 --- /dev/null +++ b/crates/consensus/src/transaction/eip7702.rs @@ -0,0 +1,446 @@ +use crate::{SignableTransaction, Signed, Transaction, TxType}; +use alloy_eips::eip2930::AccessList; +use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256}; +use alloy_rlp::{BufMut, Decodable, Encodable, Header}; +use core::mem; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +use alloy_eips::eip7702::{constants::EIP7702_TX_TYPE_ID, SignedAuthorization}; + +/// A transaction with a priority fee ([EIP-7702](https://eips.ethereum.org/EIPS/eip-7702)). +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +#[doc(alias = "Eip7702Transaction", alias = "TransactionEip7702", alias = "Eip7702Tx")] +pub struct TxEip7702 { + /// EIP-155: Simple replay attack protection + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))] + pub chain_id: ChainId, + /// A scalar value equal to the number of transactions sent by the sender; formally Tn. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))] + pub nonce: u64, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))] + pub gas_limit: u128, + /// A scalar value equal to the maximum + /// amount of gas that should be used in executing + /// this transaction. This is paid up-front, before any + /// computation is done and may not be increased + /// later; formally Tg. + /// + /// 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 + /// + /// This is also known as `GasFeeCap` + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))] + pub max_fee_per_gas: u128, + /// Max Priority fee that transaction is paying + /// + /// 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 + /// + /// 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, + /// 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 + /// to the newly created account; formally Tv. + pub value: U256, + /// 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). + /// A gas cost is charged, though at a discount relative to the cost of + /// accessing outside the list. + pub access_list: AccessList, + /// Authorizations are used to temporarily set the code of its signer to + /// the code referenced by `address`. These also include a `chain_id` (which + /// can be set to zero and not evaluated) as well as an optional `nonce`. + pub authorization_list: Vec, + /// 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, + /// data: An unlimited size byte array specifying the + /// input data of the message call, formally Td. + pub input: Bytes, +} + +impl TxEip7702 { + /// Returns the effective gas price for the given `base_fee`. + pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } + } + + /// Decodes the inner [TxEip7702] fields from RLP bytes. + /// + /// NOTE: This assumes a RLP header has already been decoded, and _just_ decodes the following + /// RLP fields in the following order: + /// + /// - `chain_id` + /// - `nonce` + /// - `max_priority_fee_per_gas` + /// - `max_fee_per_gas` + /// - `gas_limit` + /// - `to` + /// - `value` + /// - `data` (`input`) + /// - `access_list` + /// - `authorization_list` + pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result { + Ok(Self { + chain_id: Decodable::decode(buf)?, + nonce: Decodable::decode(buf)?, + max_priority_fee_per_gas: Decodable::decode(buf)?, + max_fee_per_gas: Decodable::decode(buf)?, + gas_limit: Decodable::decode(buf)?, + to: Decodable::decode(buf)?, + value: Decodable::decode(buf)?, + input: Decodable::decode(buf)?, + access_list: Decodable::decode(buf)?, + authorization_list: Decodable::decode(buf)?, + }) + } + + /// Outputs the length of the transaction's fields, without a RLP header. + #[doc(hidden)] + pub fn fields_len(&self) -> usize { + let mut len = 0; + len += self.chain_id.length(); + len += self.nonce.length(); + len += self.max_priority_fee_per_gas.length(); + len += self.max_fee_per_gas.length(); + len += self.gas_limit.length(); + len += self.to.length(); + len += self.value.length(); + len += self.input.0.length(); + len += self.access_list.length(); + len += self.authorization_list.length(); + len + } + + /// Encodes only the transaction's fields into the desired buffer, without a RLP header. + pub(crate) fn encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) { + self.chain_id.encode(out); + self.nonce.encode(out); + self.max_priority_fee_per_gas.encode(out); + self.max_fee_per_gas.encode(out); + self.gas_limit.encode(out); + self.to.encode(out); + self.value.encode(out); + self.input.0.encode(out); + self.access_list.encode(out); + self.authorization_list.encode(out); + } + + /// Returns what the encoded length should be, if the transaction were RLP encoded with the + /// given signature, depending on the value of `with_header`. + /// + /// If `with_header` is `true`, the payload length will include the RLP header length. + /// If `with_header` is `false`, the payload length will not include the RLP header length. + pub(crate) fn encoded_len_with_signature( + &self, + signature: &Signature, + with_header: bool, + ) -> usize { + // this counts the tx fields and signature fields + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + + // this counts: + // * tx type byte + // * inner header length + // * inner payload length + let inner_payload_length = + 1 + Header { list: true, payload_length }.length() + payload_length; + + if with_header { + // header length plus length of the above, wrapped with a string header + Header { list: false, payload_length: inner_payload_length }.length() + + inner_payload_length + } else { + inner_payload_length + } + } + + /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating + /// hash that for eip2718 does not require a rlp header. + #[doc(hidden)] + pub fn encode_with_signature( + &self, + signature: &Signature, + out: &mut dyn BufMut, + with_header: bool, + ) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + if with_header { + Header { + list: false, + payload_length: 1 + Header { list: true, payload_length }.length() + payload_length, + } + .encode(out); + } + out.put_u8(EIP7702_TX_TYPE_ID); + self.encode_with_signature_fields(signature, out); + } + + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + let header = Header::decode(buf)?; + if !header.list { + return Err(alloy_rlp::Error::UnexpectedString); + } + + // record original length so we can check encoding + let original_len = buf.len(); + + let tx = Self::decode_fields(buf)?; + let signature = Signature::decode_rlp_vrs(buf)?; + + let signed = tx.into_signed(signature); + if buf.len() + header.payload_length != original_len { + return Err(alloy_rlp::Error::ListLengthMismatch { + expected: header.payload_length, + got: original_len - buf.len(), + }); + } + + Ok(signed) + } + + /// Encodes the transaction from RLP bytes, including the signature. This __does not__ encode a + /// tx type byte or string header. + /// + /// This __does__ encode a list header and include a signature. + pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + let payload_length = self.fields_len() + signature.rlp_vrs_len(); + let header = Header { list: true, payload_length }; + header.encode(out); + self.encode_fields(out); + signature.write_rlp_vrs(out); + } + + /// Get transaction type + #[doc(alias = "transaction_type")] + #[allow(unused)] + pub(crate) fn tx_type(&self) -> TxType { + unimplemented!("not yet added to tx type enum") + } + + /// Calculates a heuristic for the in-memory size of the [TxEip7702] transaction. + #[inline] + pub fn size(&self) -> usize { + mem::size_of::() + // chain_id + mem::size_of::() + // nonce + mem::size_of::() + // gas_limit + mem::size_of::() + // max_fee_per_gas + mem::size_of::() + // max_priority_fee_per_gas + self.to.size() + // to + mem::size_of::() + // value + self.access_list.size() + // access_list + self.input.len() + // input + self.authorization_list.capacity() * mem::size_of::() // authorization_list + } +} + +impl Transaction for TxEip7702 { + fn chain_id(&self) -> Option { + Some(self.chain_id) + } + + fn nonce(&self) -> u64 { + self.nonce + } + + fn gas_limit(&self) -> u128 { + self.gas_limit + } + + fn gas_price(&self) -> Option { + None + } + + fn to(&self) -> TxKind { + self.to + } + + fn value(&self) -> U256 { + self.value + } + + fn input(&self) -> &[u8] { + &self.input + } +} + +impl SignableTransaction for TxEip7702 { + fn set_chain_id(&mut self, chain_id: ChainId) { + self.chain_id = chain_id; + } + + fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) { + out.put_u8(EIP7702_TX_TYPE_ID); + self.encode(out) + } + + fn payload_len_for_signature(&self) -> usize { + self.length() + 1 + } + + fn into_signed(self, signature: Signature) -> Signed { + // Drop any v chain id value to ensure the signature format is correct at the time of + // combination for an EIP-7702 transaction. V should indicate the y-parity of the + // signature. + let signature = signature.with_parity_bool(); + + let mut buf = Vec::with_capacity(self.encoded_len_with_signature(&signature, false)); + self.encode_with_signature(&signature, &mut buf, false); + let hash = keccak256(&buf); + + Signed::new_unchecked(self, signature, hash) + } +} + +impl Encodable for TxEip7702 { + fn encode(&self, out: &mut dyn BufMut) { + Header { list: true, payload_length: self.fields_len() }.encode(out); + self.encode_fields(out); + } + + fn length(&self) -> usize { + let payload_length = self.fields_len(); + Header { list: true, payload_length }.length() + payload_length + } +} + +impl Decodable for TxEip7702 { + fn decode(data: &mut &[u8]) -> alloy_rlp::Result { + let header = Header::decode(data)?; + let remaining_len = data.len(); + + if header.payload_length > remaining_len { + return Err(alloy_rlp::Error::InputTooShort); + } + + Self::decode_fields(data) + } +} + +#[cfg(all(test, feature = "k256"))] +mod tests { + use super::TxEip7702; + use crate::SignableTransaction; + use alloy_eips::eip2930::AccessList; + use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, U256}; + + #[test] + fn encode_decode_eip7702() { + let tx = TxEip7702 { + chain_id: 1, + nonce: 0x42, + gas_limit: 44386, + to: address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6").into(), + value: U256::from(0_u64), + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + access_list: AccessList::default(), + authorization_list: vec![], + }; + + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + + let mut buf = vec![]; + tx.encode_with_signature_fields(&sig, &mut buf); + let decoded = TxEip7702::decode_signed_fields(&mut &buf[..]).unwrap(); + assert_eq!(decoded, tx.into_signed(sig)); + } + + #[test] + fn test_decode_create() { + // tests that a contract creation tx encodes and decodes properly + let tx = TxEip7702 { + chain_id: 1u64, + nonce: 0, + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + gas_limit: 2, + to: TxKind::Create, + value: U256::ZERO, + input: vec![1, 2].into(), + access_list: Default::default(), + authorization_list: Default::default(), + }; + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + let mut buf = vec![]; + tx.encode_with_signature_fields(&sig, &mut buf); + let decoded = TxEip7702::decode_signed_fields(&mut &buf[..]).unwrap(); + assert_eq!(decoded, tx.into_signed(sig)); + } + + #[test] + fn test_decode_call() { + let tx = TxEip7702 { + chain_id: 1u64, + nonce: 0, + max_fee_per_gas: 0x4a817c800, + max_priority_fee_per_gas: 0x3b9aca00, + gas_limit: 2, + to: Address::default().into(), + value: U256::ZERO, + input: vec![1, 2].into(), + access_list: Default::default(), + authorization_list: Default::default(), + }; + + let sig = Signature::from_scalars_and_parity( + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), + false, + ) + .unwrap(); + + let mut buf = vec![]; + tx.encode_with_signature_fields(&sig, &mut buf); + let decoded = TxEip7702::decode_signed_fields(&mut &buf[..]).unwrap(); + assert_eq!(decoded, tx.into_signed(sig)); + } +} diff --git a/crates/consensus/src/transaction/mod.rs b/crates/consensus/src/transaction/mod.rs index ab8237346a7..0b2307c4122 100644 --- a/crates/consensus/src/transaction/mod.rs +++ b/crates/consensus/src/transaction/mod.rs @@ -13,6 +13,9 @@ pub use eip1559::TxEip1559; mod eip2930; pub use eip2930::TxEip2930; +mod eip7702; +pub use eip7702::TxEip7702; + /// [EIP-4844] constants, helpers, and types. pub mod eip4844;