Skip to content

Commit 68639d2

Browse files
authored
fix: avoid recovering address from fake transactions (#4911)
1 parent 6b6f447 commit 68639d2

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

crates/edr_provider/src/data.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use edr_eth::{
2525
},
2626
reward_percentile::RewardPercentile,
2727
signature::{RecoveryMessage, Signature},
28-
transaction::{SignedTransaction, TransactionRequestAndSender},
28+
transaction::TransactionRequestAndSender,
2929
Address, Bytes, SpecId, B256, U256,
3030
};
3131
use edr_evm::{
@@ -952,6 +952,7 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
952952
&self.instance_id
953953
}
954954

955+
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
955956
pub fn interval_mine(&mut self) -> Result<bool, ProviderError<LoggerErrorT>> {
956957
let result = self.mine_and_commit_block(BlockOptions::default())?;
957958

@@ -1746,10 +1747,8 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
17461747
hash: &B256,
17471748
) -> Result<Option<TransactionAndBlock>, ProviderError<LoggerErrorT>> {
17481749
let transaction = if let Some(tx) = self.mem_pool.transaction_by_hash(hash) {
1749-
let signed_transaction = tx.pending().as_inner().clone();
1750-
17511750
Some(TransactionAndBlock {
1752-
signed_transaction,
1751+
transaction: tx.pending().clone(),
17531752
block_data: None,
17541753
is_pending: true,
17551754
})
@@ -1762,15 +1761,14 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
17621761
let tx_index =
17631762
usize::try_from(tx_index_u64).expect("Indices cannot be larger than usize::MAX");
17641763

1765-
let signed_transaction = block
1764+
let transaction = block
17661765
.transactions()
17671766
.get(tx_index)
17681767
.expect("Transaction index must be valid, since it's from the receipt.")
1769-
.as_inner()
17701768
.clone();
17711769

17721770
Some(TransactionAndBlock {
1773-
signed_transaction,
1771+
transaction,
17741772
block_data: Some(BlockDataForTransaction {
17751773
block,
17761774
transaction_index: tx_index_u64,
@@ -2375,8 +2373,8 @@ fn create_blockchain_and_state(
23752373
/// The result returned by requesting a transaction.
23762374
#[derive(Debug, Clone)]
23772375
pub struct TransactionAndBlock {
2378-
/// The signed transaction.
2379-
pub signed_transaction: SignedTransaction,
2376+
/// The transaction.
2377+
pub transaction: ExecutableTransaction,
23802378
/// Block data in which the transaction is found if it has been mined.
23812379
pub block_data: Option<BlockDataForTransaction>,
23822380
/// Whether the transaction is pending
@@ -3401,10 +3399,7 @@ mod tests {
34013399
.transaction_by_hash(&transaction_hash)?
34023400
.context("transaction not found")?;
34033401

3404-
assert_eq!(
3405-
transaction_result.signed_transaction.hash(),
3406-
&transaction_hash
3407-
);
3402+
assert_eq!(transaction_result.transaction.hash(), &transaction_hash);
34083403

34093404
Ok(())
34103405
}
@@ -3436,10 +3431,7 @@ mod tests {
34363431
.transaction_by_hash(&transaction_hash)?
34373432
.context("transaction not found")?;
34383433

3439-
assert_eq!(
3440-
transaction_result.signed_transaction.hash(),
3441-
&transaction_hash
3442-
);
3434+
assert_eq!(transaction_result.transaction.hash(), &transaction_hash);
34433435

34443436
Ok(())
34453437
}

crates/edr_provider/src/interval.rs

+37-28
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,9 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static> IntervalMiner<LoggerErrorT> {
2929
config: IntervalConfig,
3030
data: Arc<Mutex<ProviderData<LoggerErrorT>>>,
3131
) -> Self {
32-
let (cancellation_sender, mut cancellation_receiver) = oneshot::channel();
33-
let background_task = runtime.spawn(async move {
34-
let mut now = Instant::now();
35-
loop {
36-
let delay = config.generate_interval();
37-
let deadline = now + std::time::Duration::from_millis(delay);
38-
39-
tokio::select! {
40-
_ = &mut cancellation_receiver => return Ok(()),
41-
_ = tokio::time::sleep_until(deadline) => {
42-
tokio::select! {
43-
// Check whether the interval miner needs to be destroyed
44-
_ = &mut cancellation_receiver => return Ok(()),
45-
mut data = data.lock() => {
46-
now = Instant::now();
47-
48-
if let Err(error) = data.interval_mine() {
49-
log::error!("Unexpected error while performing interval mining: {error}");
50-
return Err(error);
51-
}
52-
53-
Result::<(), ProviderError<LoggerErrorT>>::Ok(())
54-
}
55-
}
56-
},
57-
}?;
58-
}
59-
});
32+
let (cancellation_sender, cancellation_receiver) = oneshot::channel();
33+
let background_task = runtime
34+
.spawn(async move { interval_mining_loop(config, data, cancellation_receiver).await });
6035

6136
Self {
6237
inner: Some(Inner {
@@ -68,7 +43,41 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static> IntervalMiner<LoggerErrorT> {
6843
}
6944
}
7045

46+
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
47+
async fn interval_mining_loop<LoggerErrorT: Debug + Send + Sync + 'static>(
48+
config: IntervalConfig,
49+
data: Arc<Mutex<ProviderData<LoggerErrorT>>>,
50+
mut cancellation_receiver: oneshot::Receiver<()>,
51+
) -> Result<(), ProviderError<LoggerErrorT>> {
52+
let mut now = Instant::now();
53+
loop {
54+
let delay = config.generate_interval();
55+
let deadline = now + std::time::Duration::from_millis(delay);
56+
57+
tokio::select! {
58+
_ = &mut cancellation_receiver => return Ok(()),
59+
_ = tokio::time::sleep_until(deadline) => {
60+
tokio::select! {
61+
// Check whether the interval miner needs to be destroyed
62+
_ = &mut cancellation_receiver => return Ok(()),
63+
mut data = data.lock() => {
64+
now = Instant::now();
65+
66+
if let Err(error) = data.interval_mine() {
67+
log::error!("Unexpected error while performing interval mining: {error}");
68+
return Err(error);
69+
}
70+
71+
Result::<(), ProviderError<LoggerErrorT>>::Ok(())
72+
}
73+
}
74+
},
75+
}?;
76+
}
77+
}
78+
7179
impl<LoggerErrorT: Debug> Drop for IntervalMiner<LoggerErrorT> {
80+
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
7281
fn drop(&mut self) {
7382
if let Some(Inner {
7483
cancellation_sender,

crates/edr_provider/src/requests/eth/blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn block_to_rpc_output<LoggerErrorT: Debug>(
144144
.iter()
145145
.enumerate()
146146
.map(|(i, tx)| TransactionAndBlock {
147-
signed_transaction: tx.as_inner().clone(),
147+
transaction: tx.clone(),
148148
block_data: Some(BlockDataForTransaction {
149149
block: block.clone(),
150150
transaction_index: i.try_into().expect("usize fits into u64"),

crates/edr_provider/src/requests/eth/transactions.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn handle_pending_transactions<LoggerErrorT: Debug>(
7272
data.pending_transactions()
7373
.map(|pending_transaction| {
7474
let transaction_and_block = TransactionAndBlock {
75-
signed_transaction: pending_transaction.as_inner().clone(),
75+
transaction: pending_transaction.clone(),
7676
block_data: None,
7777
is_pending: true,
7878
};
@@ -131,7 +131,7 @@ fn transaction_from_block(
131131
.transactions()
132132
.get(transaction_index)
133133
.map(|transaction| TransactionAndBlock {
134-
signed_transaction: transaction.as_inner().clone(),
134+
transaction: transaction.clone(),
135135
block_data: Some(BlockDataForTransaction {
136136
block: block.clone(),
137137
transaction_index: transaction_index.try_into().expect("usize fits into u64"),
@@ -170,10 +170,11 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
170170
}
171171

172172
let TransactionAndBlock {
173-
signed_transaction,
173+
transaction,
174174
block_data,
175175
is_pending,
176176
} = transaction_and_block;
177+
let signed_transaction = transaction.as_inner();
177178
let block = block_data.as_ref().map(|b| &b.block);
178179
let header = block.map(|b| b.header());
179180

@@ -182,7 +183,7 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
182183
SignedTransaction::PostEip155Legacy(tx) => tx.gas_price,
183184
SignedTransaction::Eip2930(tx) => tx.gas_price,
184185
SignedTransaction::Eip1559(_) | SignedTransaction::Eip4844(_) => {
185-
gas_price_for_post_eip1559(&signed_transaction, block)
186+
gas_price_for_post_eip1559(signed_transaction, block)
186187
}
187188
};
188189

@@ -224,7 +225,7 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
224225
block_hash,
225226
block_number,
226227
transaction_index,
227-
from: signed_transaction.recover()?,
228+
from: *transaction.caller(),
228229
to: signed_transaction.to(),
229230
value: signed_transaction.value(),
230231
gas_price,

0 commit comments

Comments
 (0)