alloy_consensus/transaction/
eip2930.rsuse crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::mem;
use super::RlpEcdsaTx;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[doc(alias = "Eip2930Transaction", alias = "TransactionEip2930", alias = "Eip2930Tx")]
pub struct TxEip2930 {
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub chain_id: ChainId,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub gas_price: u128,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity", rename = "gas"))]
pub gas_limit: u64,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
pub to: TxKind,
pub value: U256,
pub access_list: AccessList,
pub input: Bytes,
}
impl TxEip2930 {
#[doc(alias = "transaction_type")]
pub const fn tx_type() -> TxType {
TxType::Eip2930
}
#[inline]
pub fn size(&self) -> usize {
mem::size_of::<ChainId>() + mem::size_of::<u64>() + mem::size_of::<u128>() + mem::size_of::<u64>() + self.to.size() + mem::size_of::<U256>() + self.access_list.size() + self.input.len() }
}
impl RlpEcdsaTx for TxEip2930 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };
fn rlp_encoded_fields_length(&self) -> usize {
self.chain_id.length()
+ self.nonce.length()
+ self.gas_price.length()
+ self.gas_limit.length()
+ self.to.length()
+ self.value.length()
+ self.input.0.length()
+ self.access_list.length()
}
fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
self.chain_id.encode(out);
self.nonce.encode(out);
self.gas_price.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);
}
fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
nonce: Decodable::decode(buf)?,
gas_price: 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)?,
})
}
}
impl Transaction for TxEip2930 {
fn chain_id(&self) -> Option<ChainId> {
Some(self.chain_id)
}
fn nonce(&self) -> u64 {
self.nonce
}
fn gas_limit(&self) -> u64 {
self.gas_limit
}
fn gas_price(&self) -> Option<u128> {
Some(self.gas_price)
}
fn max_fee_per_gas(&self) -> u128 {
self.gas_price
}
fn max_priority_fee_per_gas(&self) -> Option<u128> {
None
}
fn max_fee_per_blob_gas(&self) -> Option<u128> {
None
}
fn priority_fee_or_price(&self) -> u128 {
self.gas_price
}
fn kind(&self) -> TxKind {
self.to
}
fn value(&self) -> U256 {
self.value
}
fn input(&self) -> &Bytes {
&self.input
}
fn ty(&self) -> u8 {
TxType::Eip2930 as u8
}
fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}
impl SignableTransaction<Signature> for TxEip2930 {
fn set_chain_id(&mut self, chain_id: ChainId) {
self.chain_id = chain_id;
}
fn encode_for_signing(&self, out: &mut dyn BufMut) {
out.put_u8(Self::tx_type() as u8);
self.encode(out);
}
fn payload_len_for_signature(&self) -> usize {
self.length() + 1
}
fn into_signed(self, signature: Signature) -> Signed<Self> {
let tx_hash = self.tx_hash(&signature);
Signed::new_unchecked(self, signature, tx_hash)
}
}
impl Encodable for TxEip2930 {
fn encode(&self, out: &mut dyn BufMut) {
self.rlp_encode(out);
}
fn length(&self) -> usize {
self.rlp_encoded_length()
}
}
impl Decodable for TxEip2930 {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::rlp_decode(buf)
}
}
#[cfg(test)]
mod tests {
use super::TxEip2930;
use crate::{transaction::RlpEcdsaTx, SignableTransaction, TxEnvelope};
use alloy_primitives::{Address, PrimitiveSignature as Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};
#[test]
fn test_decode_create() {
let tx = TxEip2930 {
chain_id: 1u64,
nonce: 0,
gas_price: 1,
gas_limit: 2,
to: TxKind::Create,
value: U256::from(3_u64),
input: vec![1, 2].into(),
access_list: Default::default(),
};
let signature = Signature::test_signature();
let mut encoded = Vec::new();
tx.rlp_encode_signed(&signature, &mut encoded);
let decoded = TxEip2930::rlp_decode_signed(&mut &*encoded).unwrap();
assert_eq!(decoded, tx.into_signed(signature));
}
#[test]
fn test_decode_call() {
let request = TxEip2930 {
chain_id: 1u64,
nonce: 0,
gas_price: 1,
gas_limit: 2,
to: Address::default().into(),
value: U256::from(3_u64),
input: vec![1, 2].into(),
access_list: Default::default(),
};
let signature = Signature::test_signature();
let tx = request.into_signed(signature);
let envelope = TxEnvelope::Eip2930(tx);
let mut encoded = Vec::new();
envelope.encode(&mut encoded);
assert_eq!(encoded.len(), envelope.length());
assert_eq!(
alloy_primitives::hex::encode(&encoded),
"b86401f8610180010294000000000000000000000000000000000000000003820102c080a0840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565a025e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"
);
let decoded = TxEnvelope::decode(&mut encoded.as_ref()).unwrap();
assert_eq!(decoded, envelope);
}
}
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(super) mod serde_bincode_compat {
use alloc::borrow::Cow;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{Bytes, ChainId, TxKind, U256};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
#[derive(Debug, Serialize, Deserialize)]
pub struct TxEip2930<'a> {
chain_id: ChainId,
nonce: u64,
gas_price: u128,
gas_limit: u64,
#[serde(default)]
to: TxKind,
value: U256,
access_list: Cow<'a, AccessList>,
input: Cow<'a, Bytes>,
}
impl<'a> From<&'a super::TxEip2930> for TxEip2930<'a> {
fn from(value: &'a super::TxEip2930) -> Self {
Self {
chain_id: value.chain_id,
nonce: value.nonce,
gas_price: value.gas_price,
gas_limit: value.gas_limit,
to: value.to,
value: value.value,
access_list: Cow::Borrowed(&value.access_list),
input: Cow::Borrowed(&value.input),
}
}
}
impl<'a> From<TxEip2930<'a>> for super::TxEip2930 {
fn from(value: TxEip2930<'a>) -> Self {
Self {
chain_id: value.chain_id,
nonce: value.nonce,
gas_price: value.gas_price,
gas_limit: value.gas_limit,
to: value.to,
value: value.value,
access_list: value.access_list.into_owned(),
input: value.input.into_owned(),
}
}
}
impl SerializeAs<super::TxEip2930> for TxEip2930<'_> {
fn serialize_as<S>(source: &super::TxEip2930, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
TxEip2930::from(source).serialize(serializer)
}
}
impl<'de> DeserializeAs<'de, super::TxEip2930> for TxEip2930<'de> {
fn deserialize_as<D>(deserializer: D) -> Result<super::TxEip2930, D::Error>
where
D: Deserializer<'de>,
{
TxEip2930::deserialize(deserializer).map(Into::into)
}
}
#[cfg(test)]
mod tests {
use arbitrary::Arbitrary;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use super::super::{serde_bincode_compat, TxEip2930};
#[test]
fn test_tx_eip2930_bincode_roundtrip() {
#[serde_as]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Data {
#[serde_as(as = "serde_bincode_compat::TxEip2930")]
transaction: TxEip2930,
}
let mut bytes = [0u8; 1024];
rand::thread_rng().fill(bytes.as_mut_slice());
let data = Data {
transaction: TxEip2930::arbitrary(&mut arbitrary::Unstructured::new(&bytes))
.unwrap(),
};
let encoded = bincode::serialize(&data).unwrap();
let decoded: Data = bincode::deserialize(&encoded).unwrap();
assert_eq!(decoded, data);
}
}
}