Skip to content

Commit

Permalink
fix: checking if the eip1559 gas fields are not set on eip2930 check (a…
Browse files Browse the repository at this point in the history
…lloy-rs#635)

* fix: checking if the eip1559 gas fields are not set on eip2930 check

* fix: some touchup to trim_conflicting

---------

Co-authored-by: James <[email protected]>
  • Loading branch information
2 people authored and ben186 committed Jul 27, 2024
1 parent a262316 commit 3ca60d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
31 changes: 23 additions & 8 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,25 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {

#[cfg(test)]
mod tests {
use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction};
use alloy_primitives::Address;
use crate::{TransactionBuilder, TransactionBuilderError};
use alloy_consensus::{BlobTransactionSidecar, TxEip1559, TxType, TypedTransaction};
use alloy_primitives::{Address, TxKind};
use alloy_rpc_types::{AccessList, TransactionRequest};

use crate::{TransactionBuilder, TransactionBuilderError};
#[test]
fn from_eip1559_to_tx_req() {
let tx = TxEip1559 {
chain_id: 1,
nonce: 0,
gas_limit: 21_000,
to: TxKind::Call(Address::ZERO),
max_priority_fee_per_gas: 20e9 as u128,
max_fee_per_gas: 20e9 as u128,
..Default::default()
};
let tx_req: TransactionRequest = tx.into();
tx_req.build_unsigned().unwrap();
}

#[test]
fn test_4844_when_sidecar() {
Expand Down Expand Up @@ -216,7 +230,7 @@ mod tests {
.with_max_priority_fee_per_gas(0)
.with_to(Address::ZERO)
.with_gas_price(0)
.access_list(AccessList::default());
.with_access_list(AccessList::default());

let tx = request.build_unsigned().unwrap();

Expand Down Expand Up @@ -245,7 +259,7 @@ mod tests {
fn test_fail_when_sidecar_and_access_list() {
let request = TransactionRequest::default()
.with_blob_sidecar(BlobTransactionSidecar::default())
.access_list(AccessList::default());
.with_access_list(AccessList::default());

let error = request.clone().build_unsigned().unwrap_err();

Expand Down Expand Up @@ -290,7 +304,9 @@ mod tests {

#[test]
fn test_invalid_2930_fields() {
let request = TransactionRequest::default().access_list(AccessList::default());
let request = TransactionRequest::default()
.with_access_list(AccessList::default())
.with_gas_price(Default::default());

let error = request.clone().build_unsigned().unwrap_err();

Expand All @@ -299,11 +315,10 @@ mod tests {
};

assert_eq!(tx_type, TxType::Eip2930);
assert_eq!(errors.len(), 4);
assert_eq!(errors.len(), 3);
assert!(errors.contains(&"to"));
assert!(errors.contains(&"nonce"));
assert!(errors.contains(&"gas_limit"));
assert!(errors.contains(&"gas_price"));
}

#[test]
Expand Down
13 changes: 9 additions & 4 deletions crates/rpc-types/src/eth/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,23 +340,28 @@ impl TransactionRequest {
/// submission via rpc.
pub fn trim_conflicting_keys(&mut self) {
match self.preferred_type() {
TxType::Legacy | TxType::Eip2930 => {
TxType::Legacy => {
self.max_fee_per_gas = None;
self.max_priority_fee_per_gas = None;
self.max_fee_per_blob_gas = None;
self.blob_versioned_hashes = None;
self.sidecar = None;
self.access_list = None;
}
TxType::Eip2930 => {
self.max_fee_per_gas = None;
self.max_priority_fee_per_gas = None;
self.max_fee_per_blob_gas = None;
self.blob_versioned_hashes = None;
self.sidecar = None;
}
TxType::Eip1559 => {
self.gas_price = None;
self.access_list = None;
self.blob_versioned_hashes = None;
self.sidecar = None;
}
TxType::Eip4844 => {
self.gas_price = None;
self.access_list = None;
}
}
}
Expand All @@ -371,7 +376,7 @@ impl TransactionRequest {
pub const fn preferred_type(&self) -> TxType {
if self.sidecar.is_some() || self.max_fee_per_blob_gas.is_some() {
TxType::Eip4844
} else if self.access_list.is_some() {
} else if self.access_list.is_some() && self.gas_price.is_some() {
TxType::Eip2930
} else if self.gas_price.is_some() {
TxType::Legacy
Expand Down

0 comments on commit 3ca60d4

Please sign in to comment.