Skip to content

Commit dd9c9e7

Browse files
authored
refactor: chain-specific provider (#603)
1 parent f0ebfd0 commit dd9c9e7

File tree

111 files changed

+5955
-3869
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+5955
-3869
lines changed

crates/edr_eth/src/eips/eip2718.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl<DataT: Receipt<LogT>, LogT> Receipt<LogT> for TypedEnvelope<DataT> {
120120
self.data().logs_bloom()
121121
}
122122

123-
fn logs(&self) -> &[LogT] {
124-
self.data().logs()
123+
fn transaction_logs(&self) -> &[LogT] {
124+
self.data().transaction_logs()
125125
}
126126

127127
fn root_or_status(&self) -> receipt::RootOrStatus<'_> {

crates/edr_eth/src/receipt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub trait Receipt<LogT> {
6666
/// Returns the bloom filter of the logs generated within this transaction.
6767
fn logs_bloom(&self) -> &Bloom;
6868
/// Returns the logs generated within this transaction.
69-
fn logs(&self) -> &[LogT];
69+
fn transaction_logs(&self) -> &[LogT];
7070
/// Returns the state root (pre-EIP-658) or status (post-EIP-658) of the
7171
/// receipt.
7272
fn root_or_status(&self) -> RootOrStatus<'_>;

crates/edr_eth/src/receipt/execution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl<LogT> Receipt<LogT> for Execution<LogT> {
173173
}
174174
}
175175

176-
fn logs(&self) -> &[LogT] {
176+
fn transaction_logs(&self) -> &[LogT] {
177177
match self {
178178
Execution::Legacy(receipt) => &receipt.logs,
179179
Execution::Eip658(receipt) => &receipt.logs,

crates/edr_eth/src/receipt/transaction.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use revm_primitives::{EvmWiring, ExecutionResult, Output};
55

66
use super::{MapReceiptLogs, Receipt};
77
use crate::{
8-
transaction::{SignedTransaction, TransactionType},
8+
transaction::{ExecutableTransaction, Transaction, TransactionType},
99
Address, Bloom, SpecId, B256, U256,
1010
};
1111

@@ -52,7 +52,7 @@ impl<ExecutionReceiptT: Receipt<LogT>, LogT> TransactionReceipt<ExecutionReceipt
5252
/// transaction
5353
pub fn new<ChainSpecT>(
5454
execution_receipt: ExecutionReceiptT,
55-
transaction: &impl SignedTransaction,
55+
transaction: &(impl Transaction + ExecutableTransaction),
5656
result: &ExecutionResult<ChainSpecT>,
5757
transaction_index: u64,
5858
block_base_fee: U256,
@@ -131,8 +131,8 @@ impl<ExecutionReceiptT: Receipt<LogT>, LogT> Receipt<LogT>
131131
self.inner.logs_bloom()
132132
}
133133

134-
fn logs(&self) -> &[LogT] {
135-
self.inner.logs()
134+
fn transaction_logs(&self) -> &[LogT] {
135+
self.inner.transaction_logs()
136136
}
137137

138138
fn root_or_status(&self) -> super::RootOrStatus<'_> {

crates/edr_eth/src/signature.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ mod fakeable;
88
mod recovery_id;
99
mod y_parity;
1010

11-
use k256::{elliptic_curve::sec1::ToEncodedPoint, FieldBytes, PublicKey, SecretKey};
11+
pub use k256::SecretKey;
12+
use k256::{elliptic_curve::sec1::ToEncodedPoint, FieldBytes, PublicKey};
1213
use sha3::{Digest, Keccak256};
1314

1415
pub use self::{recovery_id::SignatureWithRecoveryId, y_parity::SignatureWithYParity};

crates/edr_eth/src/transaction.rs

+63-54
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::str::FromStr;
1717
pub use revm_primitives::{alloy_primitives::TxKind, Transaction, TransactionValidation};
1818
use revm_primitives::{ruint, B256};
1919

20-
use crate::{AccessListItem, Address, Bytes, U256, U8};
20+
use crate::{signature::Signature, Bytes, U256, U8};
2121

2222
pub const INVALID_TX_TYPE_ERROR_MESSAGE: &str = "invalid tx type";
2323

@@ -72,6 +72,18 @@ impl From<Type> for u8 {
7272
}
7373
}
7474

75+
impl IsEip4844 for Type {
76+
fn is_eip4844(&self) -> bool {
77+
matches!(self, Type::Eip4844)
78+
}
79+
}
80+
81+
impl IsLegacy for Type {
82+
fn is_legacy(&self) -> bool {
83+
matches!(self, Type::Legacy)
84+
}
85+
}
86+
7587
#[derive(Debug, thiserror::Error)]
7688
pub enum ParseError {
7789
#[error("{0}")]
@@ -148,14 +160,15 @@ impl serde::Serialize for Type {
148160
}
149161
}
150162

151-
pub trait SignedTransaction: Transaction + TransactionType {
163+
/// Trait for information about executable transactions.
164+
pub trait ExecutableTransaction {
152165
/// The effective gas price of the transaction, calculated using the
153166
/// provided block base fee. Only applicable for post-EIP-1559 transactions.
154167
fn effective_gas_price(&self, block_base_fee: U256) -> Option<U256>;
155168

156169
/// The maximum fee per gas the sender is willing to pay. Only applicable
157170
/// for post-EIP-1559 transactions.
158-
fn max_fee_per_gas(&self) -> Option<U256>;
171+
fn max_fee_per_gas(&self) -> Option<&U256>;
159172

160173
/// The enveloped (EIP-2718) RLP-encoding of the transaction.
161174
fn rlp_encoding(&self) -> &Bytes;
@@ -168,71 +181,67 @@ pub trait SignedTransaction: Transaction + TransactionType {
168181
fn transaction_hash(&self) -> &B256;
169182
}
170183

184+
/// Trait for transactions that may be signed.
185+
pub trait MaybeSignedTransaction {
186+
/// Returns the [`Signature`] of the transaction, if any.
187+
fn maybe_signature(&self) -> Option<&dyn Signature>;
188+
}
189+
190+
/// Trait for transactions that have been signed.
191+
pub trait SignedTransaction {
192+
/// Returns the [`Signature`] of the transaction.
193+
fn signature(&self) -> &dyn Signature;
194+
}
195+
196+
impl<TransactionT: SignedTransaction> MaybeSignedTransaction for TransactionT {
197+
fn maybe_signature(&self) -> Option<&dyn Signature> {
198+
Some(self.signature())
199+
}
200+
}
201+
202+
/// Trait for mutable transactions.
171203
pub trait TransactionMut {
172204
/// Sets the gas limit of the transaction.
173205
fn set_gas_limit(&mut self, gas_limit: u64);
174206
}
175207

208+
/// Trait for determining the type of a transaction.
176209
pub trait TransactionType {
177210
/// Type of the transaction.
178-
type Type;
211+
type Type: Into<u8>;
179212

180213
/// Returns the type of the transaction.
181214
fn transaction_type(&self) -> Self::Type;
182215
}
183216

184-
pub fn max_cost(transaction: &impl SignedTransaction) -> U256 {
185-
U256::from(transaction.gas_limit()).saturating_mul(*transaction.gas_price())
217+
/// Trait for determining whether a transaction has an access list.
218+
pub trait HasAccessList {
219+
/// Whether the transaction has an access list.
220+
fn has_access_list(&self) -> bool;
186221
}
187222

188-
pub fn upfront_cost(transaction: &impl SignedTransaction) -> U256 {
189-
max_cost(transaction).saturating_add(*transaction.value())
223+
/// Trait for determining whether a transaction is an EIP-155 transaction.
224+
pub trait IsEip155 {
225+
/// Whether the transaction is an EIP-155 transaction.
226+
fn is_eip155(&self) -> bool;
190227
}
191228

192-
/// Represents _all_ transaction requests received from RPC
193-
#[derive(Clone, Debug, PartialEq, Eq, Default)]
194-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
195-
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
196-
pub struct EthTransactionRequest {
197-
/// from address
198-
pub from: Address,
199-
/// to address
200-
#[cfg_attr(feature = "serde", serde(default))]
201-
pub to: Option<Address>,
202-
/// legacy, gas Price
203-
#[cfg_attr(feature = "serde", serde(default))]
204-
pub gas_price: Option<U256>,
205-
/// max base fee per gas sender is willing to pay
206-
#[cfg_attr(feature = "serde", serde(default))]
207-
pub max_fee_per_gas: Option<U256>,
208-
/// miner tip
209-
#[cfg_attr(feature = "serde", serde(default))]
210-
pub max_priority_fee_per_gas: Option<U256>,
211-
/// gas
212-
#[cfg_attr(feature = "serde", serde(default, with = "crate::serde::optional_u64"))]
213-
pub gas: Option<u64>,
214-
/// value of th tx in wei
215-
pub value: Option<U256>,
216-
/// Any additional data sent
217-
#[cfg_attr(feature = "serde", serde(alias = "input"))]
218-
pub data: Option<Bytes>,
219-
/// Transaction nonce
220-
#[cfg_attr(feature = "serde", serde(default, with = "crate::serde::optional_u64"))]
221-
pub nonce: Option<u64>,
222-
/// Chain ID
223-
#[cfg_attr(feature = "serde", serde(default, with = "crate::serde::optional_u64"))]
224-
pub chain_id: Option<u64>,
225-
/// warm storage access pre-payment
226-
#[cfg_attr(feature = "serde", serde(default))]
227-
pub access_list: Option<Vec<AccessListItem>>,
228-
/// EIP-2718 type
229-
#[cfg_attr(
230-
feature = "serde",
231-
serde(default, rename = "type", with = "crate::serde::optional_u8")
232-
)]
233-
pub transaction_type: Option<u8>,
234-
/// Blobs (EIP-4844)
235-
pub blobs: Option<Vec<Bytes>>,
236-
/// Blob versioned hashes (EIP-4844)
237-
pub blob_hashes: Option<Vec<B256>>,
229+
/// Trait for determining whether a transaction is an EIP-4844 transaction.
230+
pub trait IsEip4844 {
231+
/// Whether the transaction is an EIP-4844 transaction.
232+
fn is_eip4844(&self) -> bool;
233+
}
234+
235+
/// Trait for determining whether a transaction is a legacy transaction.
236+
pub trait IsLegacy {
237+
/// Whether the transaction is a legacy transaction.
238+
fn is_legacy(&self) -> bool;
239+
}
240+
241+
pub fn max_cost(transaction: &impl Transaction) -> U256 {
242+
U256::from(transaction.gas_limit()).saturating_mul(*transaction.gas_price())
243+
}
244+
245+
pub fn upfront_cost(transaction: &impl Transaction) -> U256 {
246+
max_cost(transaction).saturating_add(*transaction.value())
238247
}

crates/edr_eth/src/transaction/fake_signature.rs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub(super) mod tests {
44
() => {
55
#[test]
66
fn hash_with_fake_signature_same_sender() {
7+
use $crate::transaction::ExecutableTransaction as _;
8+
79
let transaction_request = dummy_request();
810

911
let sender = Address::from(revm_primitives::ruint::aliases::U160::from(1));
@@ -19,6 +21,8 @@ pub(super) mod tests {
1921

2022
#[test]
2123
fn hash_with_fake_signature_different_senders() {
24+
use $crate::transaction::ExecutableTransaction as _;
25+
2226
let transaction_request = dummy_request();
2327

2428
let sender_one = Address::from(revm_primitives::ruint::aliases::U160::from(1));
@@ -35,6 +39,8 @@ pub(super) mod tests {
3539

3640
#[test]
3741
fn recovers_fake_sender() {
42+
use $crate::transaction::Transaction as _;
43+
3844
let transaction_request = dummy_request();
3945

4046
// Fails to recover with signature error if tried to ecrocver a fake signature

0 commit comments

Comments
 (0)