Skip to content

Commit 1907020

Browse files
committed
refactor validate_tx
1 parent 75ef851 commit 1907020

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

crates/ingress-rpc/src/service.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloy_consensus::transaction::Recovered;
12
use alloy_consensus::{Transaction, transaction::SignerRecoverable};
23
use alloy_primitives::{B256, Bytes};
34
use alloy_provider::{Provider, RootProvider, network::eip2718::Decodable2718};
@@ -88,24 +89,7 @@ where
8889
// Decode and validate all transactions
8990
let mut total_gas = 0u64;
9091
for tx_data in &bundle.txs {
91-
if tx_data.is_empty() {
92-
return Err(EthApiError::EmptyRawTransactionData.into_rpc_err());
93-
}
94-
95-
let envelope = OpTxEnvelope::decode_2718_exact(tx_data.iter().as_slice())
96-
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
97-
98-
let transaction = envelope
99-
.clone()
100-
.try_into_recovered()
101-
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
102-
103-
let mut l1_block_info = self.provider.fetch_l1_block_info().await?;
104-
let account = self
105-
.provider
106-
.fetch_account_info(transaction.signer())
107-
.await?;
108-
validate_tx(account, &transaction, tx_data, &mut l1_block_info).await?;
92+
let transaction = self.validate_tx(tx_data).await?;
10993
total_gas = total_gas.saturating_add(transaction.gas_limit());
11094
}
11195

@@ -145,24 +129,7 @@ where
145129
}
146130

147131
async fn send_raw_transaction(&self, data: Bytes) -> RpcResult<B256> {
148-
if data.is_empty() {
149-
return Err(EthApiError::EmptyRawTransactionData.into_rpc_err());
150-
}
151-
152-
let envelope = OpTxEnvelope::decode_2718_exact(data.iter().as_slice())
153-
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
154-
155-
let transaction = envelope
156-
.clone()
157-
.try_into_recovered()
158-
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
159-
160-
let mut l1_block_info = self.provider.fetch_l1_block_info().await?;
161-
let account = self
162-
.provider
163-
.fetch_account_info(transaction.signer())
164-
.await?;
165-
validate_tx(account, &transaction, &data, &mut l1_block_info).await?;
132+
let transaction = self.validate_tx(&data).await?;
166133

167134
let expiry_timestamp = SystemTime::now()
168135
.duration_since(UNIX_EPOCH)
@@ -209,3 +176,31 @@ where
209176
Ok(transaction.tx_hash())
210177
}
211178
}
179+
180+
impl<Queue> IngressService<Queue>
181+
where
182+
Queue: QueuePublisher + Sync + Send + 'static,
183+
{
184+
async fn validate_tx(&self, data: &Bytes) -> RpcResult<Recovered<OpTxEnvelope>> {
185+
if data.is_empty() {
186+
return Err(EthApiError::EmptyRawTransactionData.into_rpc_err());
187+
}
188+
189+
let envelope = OpTxEnvelope::decode_2718_exact(data.iter().as_slice())
190+
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
191+
192+
let transaction = envelope
193+
.clone()
194+
.try_into_recovered()
195+
.map_err(|_| EthApiError::FailedToDecodeSignedTransaction.into_rpc_err())?;
196+
197+
let mut l1_block_info = self.provider.fetch_l1_block_info().await?;
198+
let account = self
199+
.provider
200+
.fetch_account_info(transaction.signer())
201+
.await?;
202+
validate_tx(account, &transaction, data, &mut l1_block_info).await?;
203+
204+
Ok(transaction)
205+
}
206+
}

0 commit comments

Comments
 (0)