Skip to content

Commit

Permalink
feat(rpc): add blob support for TransactionRequest (#5382)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Devon Bear and mattsse authored Nov 10, 2023
1 parent 277cd3a commit 55db2dc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
66 changes: 59 additions & 7 deletions crates/rpc/rpc-types/src/eth/transaction/request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::eth::transaction::{
typed::{
EIP1559TransactionRequest, EIP2930TransactionRequest, LegacyTransactionRequest,
TransactionKind, TypedTransactionRequest,
BlobTransactionSidecar, EIP1559TransactionRequest, EIP2930TransactionRequest,
LegacyTransactionRequest, TransactionKind, TypedTransactionRequest,
},
AccessList,
};
use alloy_primitives::{Address, Bytes, U128, U256, U64, U8};
use alloy_primitives::{Address, Bytes, B256, U128, U256, U64, U8};
use serde::{Deserialize, Serialize};

/// Represents _all_ transaction requests received from RPC
Expand Down Expand Up @@ -37,6 +37,12 @@ pub struct TransactionRequest {
/// EIP-2718 type
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub transaction_type: Option<U8>,
/// max fee per blob gas for EIP-4844 transactions
pub max_fee_per_blob_gas: Option<U128>,
/// blob versioned hashes for EIP-4844 transactions.
pub blob_versioned_hashes: Option<Vec<B256>>,
/// sidecar for EIP-4844 transactions
pub sidecar: Option<BlobTransactionSidecar>,
}

// == impl TransactionRequest ==
Expand All @@ -57,11 +63,22 @@ impl TransactionRequest {
input: data,
nonce,
mut access_list,
max_fee_per_blob_gas,
blob_versioned_hashes,
sidecar,
..
} = self;
match (gas_price, max_fee_per_gas, access_list.take()) {
match (
gas_price,
max_fee_per_gas,
access_list.take(),
max_fee_per_blob_gas,
blob_versioned_hashes,
sidecar,
) {
// legacy transaction
(Some(_), None, None) => {
// gas price required
(Some(_), None, None, None, None, None) => {
Some(TypedTransactionRequest::Legacy(LegacyTransactionRequest {
nonce: nonce.unwrap_or_default(),
gas_price: gas_price.unwrap_or_default(),
Expand All @@ -76,7 +93,8 @@ impl TransactionRequest {
}))
}
// EIP2930
(_, None, Some(access_list)) => {
// if only accesslist is set, and no eip1599 fees
(_, None, Some(access_list), None, None, None) => {
Some(TypedTransactionRequest::EIP2930(EIP2930TransactionRequest {
nonce: nonce.unwrap_or_default(),
gas_price: gas_price.unwrap_or_default(),
Expand All @@ -92,7 +110,10 @@ impl TransactionRequest {
}))
}
// EIP1559
(None, Some(_), access_list) | (None, None, access_list @ None) => {
// if 4844 fields missing
// gas_price, max_fee_per_gas, access_list, max_fee_per_blob_gas, blob_versioned_hashes,
// sidecar,
(None, _, _, None, None, None) => {
// Empty fields fall back to the canonical transaction schema.
Some(TypedTransactionRequest::EIP1559(EIP1559TransactionRequest {
nonce: nonce.unwrap_or_default(),
Expand All @@ -109,6 +130,37 @@ impl TransactionRequest {
access_list: access_list.unwrap_or_default(),
}))
}
// EIP4884
// all blob fields required
(
None,
_,
_,
Some(max_fee_per_blob_gas),
Some(blob_versioned_hashes),
Some(sidecar),
) => {
// As per the EIP, we follow the same semantics as EIP-1559.
Some(TypedTransactionRequest::EIP4844(crate::EIP4844TransactionRequest {
chain_id: 0,
nonce: nonce.unwrap_or_default(),
max_priority_fee_per_gas: max_priority_fee_per_gas.unwrap_or_default(),
max_fee_per_gas: max_fee_per_gas.unwrap_or_default(),
gas_limit: gas.unwrap_or_default(),
value: value.unwrap_or_default(),
input: data.unwrap_or_default(),
kind: match to {
Some(to) => TransactionKind::Call(to),
None => TransactionKind::Create,
},
access_list: access_list.unwrap_or_default(),

// eip-4844 specific.
max_fee_per_blob_gas,
blob_versioned_hashes,
sidecar,
}))
}

_ => None,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc-types/src/eth/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use serde::{Deserialize, Serialize};
/// 1. Legacy (pre-EIP2718) [`LegacyTransactionRequest`]
/// 2. EIP2930 (state access lists) [`EIP2930TransactionRequest`]
/// 3. EIP1559 [`EIP1559TransactionRequest`]
/// 4. EIP4844 [`EIP4844TransactionRequest`]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TypedTransactionRequest {
Legacy(LegacyTransactionRequest),
EIP2930(EIP2930TransactionRequest),
EIP1559(EIP1559TransactionRequest),
EIP4844(Eip4844TransactionRequest),
EIP4844(EIP4844TransactionRequest),
}

/// Represents a legacy transaction request
Expand Down Expand Up @@ -64,7 +65,7 @@ pub struct EIP1559TransactionRequest {

/// Represents an EIP-4844 transaction request
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Eip4844TransactionRequest {
pub struct EIP4844TransactionRequest {
pub chain_id: u64,
pub nonce: U64,
pub max_priority_fee_per_gas: U128,
Expand All @@ -76,7 +77,6 @@ pub struct Eip4844TransactionRequest {
pub access_list: AccessList,
pub max_fee_per_blob_gas: U128,
pub blob_versioned_hashes: Vec<B256>,
pub gas_price: U128,
pub sidecar: BlobTransactionSidecar,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ where
Some(TypedTransactionRequest::EIP4844(mut m)) => {
m.chain_id = chain_id.to();
m.gas_limit = gas_limit;
m.gas_price = gas_price;
m.max_fee_per_gas = max_fee_per_gas;

TypedTransactionRequest::EIP4844(m)
}
Expand Down

0 comments on commit 55db2dc

Please sign in to comment.