kabu_evm_utils/
reth_types.rs

1use alloy::consensus::transaction::{Recovered, SignerRecoverable};
2use alloy::consensus::TxEnvelope;
3use alloy::eips::eip2718::Decodable2718;
4use alloy::primitives::Bytes;
5use alloy::rpc::types::Transaction;
6use alloy::rpc::types::{BlockNumHash, Log as ALog};
7use eyre::{OptionExt, Result};
8use reth_db::models::StoredBlockBodyIndices;
9use reth_primitives::{Block, Receipt, RecoveredBlock};
10
11pub trait Convert<T> {
12    fn convert(&self) -> T;
13}
14
15/// Appends all matching logs of a block's receipts.
16/// If the log matches, look up the corresponding transaction hash.
17pub fn append_all_matching_block_logs(
18    all_logs: &mut Vec<ALog>,
19    block_num_hash: BlockNumHash,
20    receipts: Vec<Receipt>,
21    removed: bool,
22    block_body_indices: StoredBlockBodyIndices,
23    block: RecoveredBlock<Block>,
24) -> Result<()> {
25    // Lazy loaded number of the first transaction in the block.
26    // This is useful for blocks with multiple matching logs because it prevents
27    // re-querying the block body indices.
28    let loaded_first_tx_num = block_body_indices.first_tx_num;
29
30    let mut tx_iter = block.body().transactions.iter();
31
32    // Iterate over receipts and append matching logs.
33    for (log_index, (receipt_idx, receipt)) in (0_u64..).zip(receipts.iter().enumerate()) {
34        // The transaction hash of the current receipt.
35        let transaction_hash = tx_iter.next().ok_or_eyre("NO_NEXT_TX")?.hash();
36
37        for log in &receipt.logs {
38            let log = ALog {
39                inner: log.clone(),
40                block_hash: Some(block_num_hash.hash),
41                block_number: Some(block_num_hash.number),
42                transaction_hash: Some(*transaction_hash),
43                // The transaction and receipt index is always the same.
44                transaction_index: Some(receipt_idx as u64 + loaded_first_tx_num),
45                log_index: Some(log_index),
46                removed,
47                block_timestamp: Some(block.timestamp),
48            };
49            all_logs.push(log);
50        }
51    }
52    Ok(())
53}
54
55/// Appends all matching logs of a block's receipts.
56/// If the log matches, look up the corresponding transaction hash.
57pub fn append_all_matching_block_logs_sealed(
58    all_logs: &mut Vec<ALog>,
59    block_num_hash: BlockNumHash,
60    receipts: Vec<Receipt>,
61    removed: bool,
62    block: &RecoveredBlock<Block>,
63) -> Result<()> {
64    let mut tx_iter = block.body().transactions.iter();
65
66    // Iterate over receipts and append matching logs.
67    for (log_index, (receipt_idx, receipt)) in (0_u64..).zip(receipts.iter().enumerate()) {
68        // The transaction hash of the current receipt.
69        let transaction_hash = tx_iter.next().ok_or_eyre("NO_NEXT_TX")?.hash();
70
71        for log in &receipt.logs {
72            let log = ALog {
73                inner: log.clone(),
74                block_hash: Some(block_num_hash.hash),
75                block_number: Some(block_num_hash.number),
76                transaction_hash: Some(*transaction_hash),
77                // The transaction and receipt index is always the same.
78                transaction_index: Some(receipt_idx as u64),
79                log_index: Some(log_index),
80                removed,
81                block_timestamp: Some(block.timestamp),
82            };
83            all_logs.push(log);
84        }
85    }
86    Ok(())
87}
88
89pub fn decode_into_transaction(rlp_tx: &Bytes) -> Result<Transaction> {
90    let raw_tx = rlp_tx.clone().to_vec();
91    let mut raw_tx = raw_tx.as_slice();
92    //let transaction_recovered: TransactionSignedEcRecovered = TransactionSignedEcRecovered::decode(&mut raw_tx)?;
93    //let transaction_recovered: TransactionSignedEcRecovered = TransactionSignedEcRecovered::decode(&mut raw_tx)?;
94    //let transaction_recovered = TransactionSignedEcRecovered::decode(&mut raw_tx)?;
95
96    //let inner = Recovered::decode(&mut raw_tx)?;
97
98    let inner = TxEnvelope::decode_2718(&mut raw_tx)?;
99    let signer = inner.recover_signer()?;
100
101    let tx = Transaction {
102        inner: Recovered::new_unchecked(inner, signer),
103        block_hash: None,
104        block_number: None,
105        transaction_index: None,
106        effective_gas_price: None,
107    };
108
109    //let env: TxEnvelope = tx.into();
110
111    //let tx: Transaction = transaction_recovered.transaction().try_into()?;
112
113    Ok(tx)
114}