From 06ef987b66d24544c46166c38b806fdce6f4a9f6 Mon Sep 17 00:00:00 2001 From: Pia Date: Tue, 16 Apr 2024 17:01:35 +0900 Subject: [PATCH] field parse --- src/lib.rs | 1 + src/tx.rs | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3a19c61..a661cd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ pub enum Error { UnexpectedRoot, InvalidMPTProof, TrieNotFound, + FieldNotFound, } #[derive(Debug)] diff --git a/src/tx.rs b/src/tx.rs index 644e3ad..496a738 100644 --- a/src/tx.rs +++ b/src/tx.rs @@ -2,11 +2,12 @@ use crate::{Error, Field}; use alloy_consensus::{ SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEnvelope, TxLegacy, TxType, }; +use alloy_consensus::{Transaction as ConsensusTransaction, TxEip4844Variant}; use alloy_eips::eip2718::Decodable2718; use alloy_eips::eip2930::AccessList; use alloy_eips::eip2930::AccessListItem; use alloy_network::eip2718::Encodable2718; -use alloy_primitives::{Parity, Signature, TxKind, U64}; +use alloy_primitives::{ChainId, FixedBytes, Parity, Signature, TxKind, U256, U64}; use alloy_rpc_types::Transaction; #[derive(Debug, Clone)] @@ -21,6 +22,160 @@ impl ConsensusTx { let tx = TxEnvelope::decode_2718(&mut data).map_err(Error::Rlp)?; Ok(ConsensusTx(tx)) } + + pub fn nonce(&self) -> u64 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().nonce(), + TxEnvelope::Eip2930(tx) => tx.tx().nonce(), + TxEnvelope::Eip1559(tx) => tx.tx().nonce(), + TxEnvelope::Eip4844(tx) => tx.tx().nonce(), + } + } + + pub fn gas_limit(&self) -> u64 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().gas_limit(), + TxEnvelope::Eip2930(tx) => tx.tx().gas_limit(), + TxEnvelope::Eip1559(tx) => tx.tx().gas_limit(), + TxEnvelope::Eip4844(tx) => tx.tx().gas_limit(), + } + } + + pub fn gas_price(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().gas_price(), + TxEnvelope::Eip2930(tx) => tx.tx().gas_price(), + TxEnvelope::Eip1559(tx) => tx.tx().gas_price(), + TxEnvelope::Eip4844(tx) => tx.tx().gas_price(), + } + } + + pub fn to(&self) -> TxKind { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().to(), + TxEnvelope::Eip2930(tx) => tx.tx().to(), + TxEnvelope::Eip1559(tx) => tx.tx().to(), + TxEnvelope::Eip4844(tx) => tx.tx().to(), + } + } + + pub fn value(&self) -> U256 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().value(), + TxEnvelope::Eip2930(tx) => tx.tx().value(), + TxEnvelope::Eip1559(tx) => tx.tx().value(), + TxEnvelope::Eip4844(tx) => tx.tx().value(), + } + } + + pub fn input(&self) -> &[u8] { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().input(), + TxEnvelope::Eip2930(tx) => tx.tx().input(), + TxEnvelope::Eip1559(tx) => tx.tx().input(), + TxEnvelope::Eip4844(tx) => tx.tx().input(), + } + } + + pub fn v(&self) -> u64 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.signature().v().to_u64(), + TxEnvelope::Eip2930(tx) => tx.signature().v().to_u64(), + TxEnvelope::Eip1559(tx) => tx.signature().v().to_u64(), + TxEnvelope::Eip4844(tx) => tx.signature().v().to_u64(), + } + } + + pub fn r(&self) -> U256 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.signature().r(), + TxEnvelope::Eip2930(tx) => tx.signature().r(), + TxEnvelope::Eip1559(tx) => tx.signature().r(), + TxEnvelope::Eip4844(tx) => tx.signature().r(), + } + } + + pub fn s(&self) -> U256 { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.signature().s(), + TxEnvelope::Eip2930(tx) => tx.signature().s(), + TxEnvelope::Eip1559(tx) => tx.signature().s(), + TxEnvelope::Eip4844(tx) => tx.signature().s(), + } + } + + pub fn chain_id(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(tx) => tx.tx().chain_id(), + TxEnvelope::Eip2930(tx) => tx.tx().chain_id(), + TxEnvelope::Eip1559(tx) => tx.tx().chain_id(), + TxEnvelope::Eip4844(tx) => tx.tx().chain_id(), + } + } + + pub fn access_list(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(_) => None, + TxEnvelope::Eip2930(tx) => Some(tx.tx().access_list.clone()), + TxEnvelope::Eip1559(tx) => Some(tx.tx().access_list.clone()), + TxEnvelope::Eip4844(tx) => match tx.tx() { + TxEip4844Variant::TxEip4844(tx) => Some(tx.access_list.clone()), + TxEip4844Variant::TxEip4844WithSidecar(tx) => Some(tx.tx().access_list.clone()), + }, + } + } + + pub fn max_fee_per_gas(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(_) => None, + TxEnvelope::Eip2930(_) => None, + TxEnvelope::Eip1559(tx) => Some(tx.tx().max_fee_per_gas), + TxEnvelope::Eip4844(tx) => match tx.tx() { + TxEip4844Variant::TxEip4844(tx) => Some(tx.max_fee_per_gas), + TxEip4844Variant::TxEip4844WithSidecar(tx) => Some(tx.tx().max_fee_per_gas), + }, + } + } + + pub fn max_priority_fee_per_gas(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(_) => None, + TxEnvelope::Eip2930(_) => None, + TxEnvelope::Eip1559(tx) => Some(tx.tx().max_priority_fee_per_gas), + TxEnvelope::Eip4844(tx) => match tx.tx() { + TxEip4844Variant::TxEip4844(tx) => Some(tx.max_priority_fee_per_gas), + TxEip4844Variant::TxEip4844WithSidecar(tx) => { + Some(tx.tx().max_priority_fee_per_gas) + } + }, + } + } + + pub fn blob_versioned_hashes(&self) -> Option>> { + match &self.0 { + TxEnvelope::Legacy(_) => None, + TxEnvelope::Eip2930(_) => None, + TxEnvelope::Eip1559(_) => None, + TxEnvelope::Eip4844(tx) => match tx.tx() { + TxEip4844Variant::TxEip4844(tx) => Some(tx.blob_versioned_hashes.clone()), + TxEip4844Variant::TxEip4844WithSidecar(tx) => { + Some(tx.tx().blob_versioned_hashes.clone()) + } + }, + } + } + + pub fn max_fee_per_blob_gas(&self) -> Option { + match &self.0 { + TxEnvelope::Legacy(_) => None, + TxEnvelope::Eip2930(_) => None, + TxEnvelope::Eip1559(_) => None, + TxEnvelope::Eip4844(tx) => match tx.tx() { + TxEip4844Variant::TxEip4844(tx) => Some(tx.max_fee_per_blob_gas), + TxEip4844Variant::TxEip4844WithSidecar(tx) => Some(tx.tx().max_fee_per_blob_gas), + }, + } + } } #[derive(Debug, Clone)]