refactor: replace rpc TransactionKind with alloy_primitives::TxKind (#7885)

Co-authored-by: Oliver Nordbjerg <hi@notbjerg.me>
This commit is contained in:
Andrzej Sulkowski
2024-04-25 20:48:23 +02:00
committed by GitHub
parent 062b3d76b9
commit 844bcb86b7
4 changed files with 13 additions and 102 deletions

View File

@ -16,7 +16,7 @@ pub fn to_primitive_transaction(
nonce: tx.nonce,
gas_price: tx.gas_price.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: to_primitive_transaction_kind(tx.kind),
to: tx.kind,
value: tx.value,
input: tx.input,
}),
@ -25,7 +25,7 @@ pub fn to_primitive_transaction(
nonce: tx.nonce,
gas_price: tx.gas_price.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: to_primitive_transaction_kind(tx.kind),
to: tx.kind,
value: tx.value,
input: tx.input,
access_list: tx.access_list,
@ -35,7 +35,7 @@ pub fn to_primitive_transaction(
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: to_primitive_transaction_kind(tx.kind),
to: tx.kind,
value: tx.value,
input: tx.input,
access_list: tx.access_list,
@ -47,7 +47,7 @@ pub fn to_primitive_transaction(
gas_limit: tx.gas_limit.to(),
max_fee_per_gas: tx.max_fee_per_gas.to(),
max_priority_fee_per_gas: tx.max_priority_fee_per_gas.to(),
to: to_primitive_transaction_kind(tx.kind),
to: tx.kind,
value: tx.value,
access_list: tx.access_list,
blob_versioned_hashes: tx.blob_versioned_hashes,
@ -56,13 +56,3 @@ pub fn to_primitive_transaction(
}),
})
}
/// Transforms a [reth_rpc_types::TransactionKind] into a [reth_primitives::TxKind]
pub fn to_primitive_transaction_kind(
kind: reth_rpc_types::TransactionKind,
) -> reth_primitives::TxKind {
match kind {
reth_rpc_types::TransactionKind::Call(to) => reth_primitives::TxKind::Call(to),
reth_rpc_types::TransactionKind::Create => reth_primitives::TxKind::Create,
}
}

View File

@ -2,10 +2,8 @@
//! transaction deserialized from the json input of an RPC call. Depending on what fields are set,
//! it can be converted into the container type [`TypedTransactionRequest`].
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rlp::{Buf, BufMut, Decodable, Encodable, Error as RlpError, EMPTY_STRING_CODE};
use alloy_primitives::{Bytes, TxKind, B256, U256};
use alloy_rpc_types::{AccessList, BlobTransactionSidecar};
use serde::{Deserialize, Serialize};
/// Container type for various Ethereum transaction requests
///
@ -36,7 +34,7 @@ pub struct LegacyTransactionRequest {
/// The gas limit for the transaction
pub gas_limit: U256,
/// The kind of transaction (e.g., Call, Create)
pub kind: TransactionKind,
pub kind: TxKind,
/// The value of the transaction
pub value: U256,
/// The input data for the transaction
@ -57,7 +55,7 @@ pub struct EIP2930TransactionRequest {
/// The gas limit for the transaction
pub gas_limit: U256,
/// The kind of transaction (e.g., Call, Create)
pub kind: TransactionKind,
pub kind: TxKind,
/// The value of the transaction
pub value: U256,
/// The input data for the transaction
@ -80,7 +78,7 @@ pub struct EIP1559TransactionRequest {
/// The gas limit for the transaction
pub gas_limit: U256,
/// The kind of transaction (e.g., Call, Create)
pub kind: TransactionKind,
pub kind: TxKind,
/// The value of the transaction
pub value: U256,
/// The input data for the transaction
@ -103,7 +101,7 @@ pub struct EIP4844TransactionRequest {
/// The gas limit for the transaction
pub gas_limit: U256,
/// The kind of transaction (e.g., Call, Create)
pub kind: TransactionKind,
pub kind: TxKind,
/// The value of the transaction
pub value: U256,
/// The input data for the transaction
@ -117,81 +115,3 @@ pub struct EIP4844TransactionRequest {
/// Sidecar information for the transaction
pub sidecar: BlobTransactionSidecar,
}
/// Represents the `to` field of a transaction request
///
/// This determines what kind of transaction this is
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionKind {
/// Transaction will call this address or transfer funds to this address
Call(Address),
/// No `to` field set, this transaction will create a contract
Create,
}
// == impl TransactionKind ==
impl TransactionKind {
/// If this transaction is a call this returns the address of the callee
pub fn as_call(&self) -> Option<&Address> {
match self {
TransactionKind::Call(to) => Some(to),
TransactionKind::Create => None,
}
}
}
impl Encodable for TransactionKind {
/// This encodes the `to` field of a transaction request.
/// If the [TransactionKind] is a [TransactionKind::Call] it will encode the inner address:
/// `rlp(address)`
///
/// If the [TransactionKind] is a [TransactionKind::Create] it will encode an empty list:
/// `rlp([])`, which is also
fn encode(&self, out: &mut dyn BufMut) {
match self {
TransactionKind::Call(to) => to.encode(out),
TransactionKind::Create => [].encode(out),
}
}
fn length(&self) -> usize {
match self {
TransactionKind::Call(to) => to.length(),
TransactionKind::Create => [].length(),
}
}
}
impl Decodable for TransactionKind {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
if let Some(&first) = buf.first() {
if first == EMPTY_STRING_CODE {
buf.advance(1);
Ok(TransactionKind::Create)
} else {
let addr = <Address as Decodable>::decode(buf)?;
Ok(TransactionKind::Call(addr))
}
} else {
Err(RlpError::InputTooShort)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn raw_kind_encoding_sanity() {
// check the 0x80 encoding for Create
let mut buf = Vec::new();
TransactionKind::Create.encode(&mut buf);
assert_eq!(buf, vec![0x80]);
// check decoding
let buf = [0x80];
let decoded = TransactionKind::decode(&mut &buf[..]).unwrap();
assert_eq!(decoded, TransactionKind::Create);
}
}

View File

@ -38,7 +38,7 @@ pub use eth::{
ExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2, ExecutionPayloadV3, PayloadError,
},
error::ToRpcError,
transaction::{self, TransactionKind, TransactionRequest, TypedTransactionRequest},
transaction::{self, TransactionRequest, TypedTransactionRequest},
};
pub use mev::*;

View File

@ -8,6 +8,7 @@ use crate::{
},
EthApi, EthApiSpec,
};
use alloy_primitives::TxKind as RpcTransactionKind;
use async_trait::async_trait;
use reth_evm::ConfigureEvm;
use reth_network_api::NetworkInfo;
@ -33,8 +34,8 @@ use reth_rpc_types::{
LegacyTransactionRequest,
},
AnyReceiptEnvelope, AnyTransactionReceipt, Index, Log, ReceiptWithBloom, Transaction,
TransactionInfo, TransactionKind as RpcTransactionKind, TransactionReceipt, TransactionRequest,
TypedTransactionRequest, WithOtherFields,
TransactionInfo, TransactionReceipt, TransactionRequest, TypedTransactionRequest,
WithOtherFields,
};
use reth_rpc_types_compat::transaction::from_recovered_with_block_context;
use reth_transaction_pool::{TransactionOrigin, TransactionPool};