Skip to content

fix: avoid recovering address from fake transactions #4911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use edr_eth::{
},
reward_percentile::RewardPercentile,
signature::{RecoveryMessage, Signature},
transaction::{SignedTransaction, TransactionRequestAndSender},
transaction::TransactionRequestAndSender,
Address, Bytes, SpecId, B256, U256,
};
use edr_evm::{
Expand Down Expand Up @@ -942,6 +942,7 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
&self.instance_id
}

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

Expand Down Expand Up @@ -1732,10 +1733,8 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
hash: &B256,
) -> Result<Option<TransactionAndBlock>, ProviderError<LoggerErrorT>> {
let transaction = if let Some(tx) = self.mem_pool.transaction_by_hash(hash) {
let signed_transaction = tx.pending().as_inner().clone();

Some(TransactionAndBlock {
signed_transaction,
transaction: tx.pending().clone(),
block_data: None,
is_pending: true,
})
Expand All @@ -1748,15 +1747,14 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
let tx_index =
usize::try_from(tx_index_u64).expect("Indices cannot be larger than usize::MAX");

let signed_transaction = block
let transaction = block
.transactions()
.get(tx_index)
.expect("Transaction index must be valid, since it's from the receipt.")
.as_inner()
.clone();

Some(TransactionAndBlock {
signed_transaction,
transaction,
block_data: Some(BlockDataForTransaction {
block,
transaction_index: tx_index_u64,
Expand Down Expand Up @@ -2337,8 +2335,8 @@ fn create_blockchain_and_state(
/// The result returned by requesting a transaction.
#[derive(Debug, Clone)]
pub struct TransactionAndBlock {
/// The signed transaction.
pub signed_transaction: SignedTransaction,
/// The transaction.
pub transaction: ExecutableTransaction,
/// Block data in which the transaction is found if it has been mined.
pub block_data: Option<BlockDataForTransaction>,
/// Whether the transaction is pending
Expand Down Expand Up @@ -3362,10 +3360,7 @@ mod tests {
.transaction_by_hash(&transaction_hash)?
.context("transaction not found")?;

assert_eq!(
transaction_result.signed_transaction.hash(),
&transaction_hash
);
assert_eq!(transaction_result.transaction.hash(), &transaction_hash);

Ok(())
}
Expand Down Expand Up @@ -3397,10 +3392,7 @@ mod tests {
.transaction_by_hash(&transaction_hash)?
.context("transaction not found")?;

assert_eq!(
transaction_result.signed_transaction.hash(),
&transaction_hash
);
assert_eq!(transaction_result.transaction.hash(), &transaction_hash);

Ok(())
}
Expand Down
65 changes: 37 additions & 28 deletions crates/edr_provider/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,9 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static> IntervalMiner<LoggerErrorT> {
config: IntervalConfig,
data: Arc<Mutex<ProviderData<LoggerErrorT>>>,
) -> Self {
let (cancellation_sender, mut cancellation_receiver) = oneshot::channel();
let background_task = runtime.spawn(async move {
let mut now = Instant::now();
loop {
let delay = config.generate_interval();
let deadline = now + std::time::Duration::from_millis(delay);

tokio::select! {
_ = &mut cancellation_receiver => return Ok(()),
_ = tokio::time::sleep_until(deadline) => {
tokio::select! {
// Check whether the interval miner needs to be destroyed
_ = &mut cancellation_receiver => return Ok(()),
mut data = data.lock() => {
now = Instant::now();

if let Err(error) = data.interval_mine() {
log::error!("Unexpected error while performing interval mining: {error}");
return Err(error);
}

Result::<(), ProviderError<LoggerErrorT>>::Ok(())
}
}
},
}?;
}
});
let (cancellation_sender, cancellation_receiver) = oneshot::channel();
let background_task = runtime
.spawn(async move { interval_mining_loop(config, data, cancellation_receiver).await });

Self {
inner: Some(Inner {
Expand All @@ -68,7 +43,41 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static> IntervalMiner<LoggerErrorT> {
}
}

#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
async fn interval_mining_loop<LoggerErrorT: Debug + Send + Sync + 'static>(
config: IntervalConfig,
data: Arc<Mutex<ProviderData<LoggerErrorT>>>,
mut cancellation_receiver: oneshot::Receiver<()>,
) -> Result<(), ProviderError<LoggerErrorT>> {
let mut now = Instant::now();
loop {
let delay = config.generate_interval();
let deadline = now + std::time::Duration::from_millis(delay);

tokio::select! {
_ = &mut cancellation_receiver => return Ok(()),
_ = tokio::time::sleep_until(deadline) => {
tokio::select! {
// Check whether the interval miner needs to be destroyed
_ = &mut cancellation_receiver => return Ok(()),
mut data = data.lock() => {
now = Instant::now();

if let Err(error) = data.interval_mine() {
log::error!("Unexpected error while performing interval mining: {error}");
return Err(error);
}

Result::<(), ProviderError<LoggerErrorT>>::Ok(())
}
}
},
}?;
}
}

impl<LoggerErrorT: Debug> Drop for IntervalMiner<LoggerErrorT> {
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
fn drop(&mut self) {
if let Some(Inner {
cancellation_sender,
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_provider/src/requests/eth/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn block_to_rpc_output<LoggerErrorT: Debug>(
.iter()
.enumerate()
.map(|(i, tx)| TransactionAndBlock {
signed_transaction: tx.as_inner().clone(),
transaction: tx.clone(),
block_data: Some(BlockDataForTransaction {
block: block.clone(),
transaction_index: i.try_into().expect("usize fits into u64"),
Expand Down
11 changes: 6 additions & 5 deletions crates/edr_provider/src/requests/eth/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn handle_pending_transactions<LoggerErrorT: Debug>(
data.pending_transactions()
.map(|pending_transaction| {
let transaction_and_block = TransactionAndBlock {
signed_transaction: pending_transaction.as_inner().clone(),
transaction: pending_transaction.clone(),
block_data: None,
is_pending: true,
};
Expand Down Expand Up @@ -131,7 +131,7 @@ fn transaction_from_block(
.transactions()
.get(transaction_index)
.map(|transaction| TransactionAndBlock {
signed_transaction: transaction.as_inner().clone(),
transaction: transaction.clone(),
block_data: Some(BlockDataForTransaction {
block: block.clone(),
transaction_index: transaction_index.try_into().expect("usize fits into u64"),
Expand Down Expand Up @@ -170,10 +170,11 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
}

let TransactionAndBlock {
signed_transaction,
transaction,
block_data,
is_pending,
} = transaction_and_block;
let signed_transaction = transaction.as_inner();
let block = block_data.as_ref().map(|b| &b.block);
let header = block.map(|b| b.header());

Expand All @@ -182,7 +183,7 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
SignedTransaction::PostEip155Legacy(tx) => tx.gas_price,
SignedTransaction::Eip2930(tx) => tx.gas_price,
SignedTransaction::Eip1559(_) | SignedTransaction::Eip4844(_) => {
gas_price_for_post_eip1559(&signed_transaction, block)
gas_price_for_post_eip1559(signed_transaction, block)
}
};

Expand Down Expand Up @@ -224,7 +225,7 @@ pub fn transaction_to_rpc_result<LoggerErrorT: Debug>(
block_hash,
block_number,
transaction_index,
from: signed_transaction.recover()?,
from: *transaction.caller(),
to: signed_transaction.to(),
value: signed_transaction.value(),
gas_price,
Expand Down