Skip to content

Commit 92babbe

Browse files
committed
tweaks to bring equivalence to ethers cryo
1 parent 1abef98 commit 92babbe

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

crates/freeze/src/datasets/blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Blocks {
1717
state_root: Vec<Vec<u8>>,
1818
transactions_root: Vec<Vec<u8>>,
1919
receipts_root: Vec<Vec<u8>>,
20-
block_number: Vec<Option<u64>>,
20+
block_number: Vec<Option<u32>>,
2121
gas_used: Vec<u64>,
2222
gas_limit: Vec<u64>,
2323
extra_data: Vec<Vec<u8>>,
@@ -108,7 +108,7 @@ pub(crate) fn process_block<TX>(block: Block<TX>, columns: &mut Blocks, schema:
108108
store!(schema, columns, state_root, block.header.state_root.0.to_vec());
109109
store!(schema, columns, transactions_root, block.header.transactions_root.0.to_vec());
110110
store!(schema, columns, receipts_root, block.header.receipts_root.0.to_vec());
111-
store!(schema, columns, block_number, Some(block.header.number));
111+
store!(schema, columns, block_number, Some(block.header.number as u32));
112112
store!(schema, columns, gas_used, block.header.gas_used);
113113
store!(schema, columns, gas_limit, block.header.gas_limit);
114114
store!(schema, columns, extra_data, block.header.extra_data.to_vec());

crates/freeze/src/datasets/transactions.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ impl Dataset for Transactions {
6666
"n_input_bytes",
6767
"n_input_zero_bytes",
6868
"n_input_nonzero_bytes",
69-
"n_rlp_bytes",
7069
"chain_id",
7170
])
7271
}
@@ -143,13 +142,15 @@ impl CollectByBlock for Transactions {
143142
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
144143
let (block, transactions_with_receipts, exclude_failed) = response;
145144
for (tx, receipt) in transactions_with_receipts.into_iter() {
145+
let gas_price = get_gas_price(&block, &tx);
146146
process_transaction(
147147
tx,
148148
receipt,
149149
columns,
150150
schema,
151151
exclude_failed,
152152
block.header.timestamp as u32,
153+
gas_price
153154
)?;
154155
}
155156
Ok(())
@@ -158,7 +159,7 @@ impl CollectByBlock for Transactions {
158159

159160
#[async_trait::async_trait]
160161
impl CollectByTransaction for Transactions {
161-
type Response = (TransactionAndReceipt, bool, u32);
162+
type Response = (TransactionAndReceipt, Block, bool, u32);
162163

163164
async fn extract(request: Params, source: Arc<Source>, query: Arc<Query>) -> R<Self::Response> {
164165
let tx_hash = request.ethers_transaction_hash()?;
@@ -184,13 +185,22 @@ impl CollectByTransaction for Transactions {
184185

185186
let timestamp = block.header.timestamp as u32;
186187

187-
Ok(((transaction, receipt), query.exclude_failed, timestamp))
188+
Ok(((transaction, receipt), block, query.exclude_failed, timestamp))
188189
}
189190

190191
fn transform(response: Self::Response, columns: &mut Self, query: &Arc<Query>) -> R<()> {
191192
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
192-
let ((transaction, receipt), exclude_failed, timestamp) = response;
193-
process_transaction(transaction, receipt, columns, schema, exclude_failed, timestamp)?;
193+
let ((transaction, receipt), block, exclude_failed, timestamp) = response;
194+
let gas_price = get_gas_price(&block, &transaction);
195+
process_transaction(
196+
transaction,
197+
receipt,
198+
columns,
199+
schema,
200+
exclude_failed,
201+
timestamp,
202+
gas_price
203+
)?;
194204
Ok(())
195205
}
196206
}
@@ -202,6 +212,7 @@ pub(crate) fn process_transaction(
202212
schema: &Table,
203213
exclude_failed: bool,
204214
timestamp: u32,
215+
gas_price: Option<u64>,
205216
) -> R<()> {
206217
let success = if exclude_failed | schema.has_column("success") {
207218
let success = tx_success(&tx, &receipt)?;
@@ -244,10 +255,11 @@ pub(crate) fn process_transaction(
244255
}
245256
// in alloy eip2718_encoded_length is rlp_encoded_length
246257
store!(schema, columns, n_rlp_bytes, tx.inner.eip2718_encoded_length() as u32);
247-
store!(schema, columns, gas_used, receipt.map(|r| r.gas_used as u64));
248-
store!(schema, columns, gas_price, tx.inner.gas_price().map(|gas_price| gas_price as u64));
258+
store!(schema, columns, gas_used, receipt.as_ref().map(|r| r.gas_used as u64));
259+
// store!(schema, columns, gas_price, Some(receipt.unwrap().effective_gas_price as u64));
260+
store!(schema, columns, gas_price, gas_price);
249261
store!(schema, columns, transaction_type, tx.inner.tx_type() as u32);
250-
store!(schema, columns, max_fee_per_gas, Some(tx.inner.max_fee_per_gas() as u64));
262+
store!(schema, columns, max_fee_per_gas, get_max_fee_per_gas(&tx));
251263
store!(
252264
schema,
253265
columns,
@@ -264,6 +276,29 @@ pub(crate) fn process_transaction(
264276
Ok(())
265277
}
266278

279+
fn get_max_fee_per_gas(tx: &Transaction) -> Option<u64> {
280+
match &tx.inner {
281+
alloy::consensus::TxEnvelope::Legacy(_) => None,
282+
alloy::consensus::TxEnvelope::Eip2930(_) => None,
283+
_ => Some(tx.inner.max_fee_per_gas() as u64),
284+
}
285+
}
286+
287+
pub(crate) fn get_gas_price(block: &Block, tx: &Transaction) -> Option<u64> {
288+
match &tx.inner {
289+
alloy::consensus::TxEnvelope::Legacy(_) => tx.gas_price().map(|gas_price| gas_price as u64),
290+
alloy::consensus::TxEnvelope::Eip2930(_) => tx.gas_price().map(|gas_price| gas_price as u64),
291+
_ => {
292+
let base_fee_per_gas = block.header.inner.base_fee_per_gas.unwrap();
293+
let priority_fee = std::cmp::min(
294+
tx.inner.max_priority_fee_per_gas().unwrap() as u64,
295+
tx.inner.max_fee_per_gas() as u64 - base_fee_per_gas,
296+
);
297+
Some(base_fee_per_gas + priority_fee)
298+
}
299+
}
300+
}
301+
267302
fn tx_success(tx: &Transaction, receipt: &Option<TransactionReceipt>) -> R<bool> {
268303
if let Some(r) = receipt {
269304
Ok(r.status())

crates/freeze/src/multi_datasets/blocks_and_transactions.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{datasets::transactions, types::collection::*, Datatype, *};
2-
use alloy::rpc::types::BlockTransactionsKind;
32
use polars::prelude::*;
43
use std::collections::HashMap;
54

@@ -47,19 +46,15 @@ impl CollectByTransaction for BlocksAndTransactions {
4746
);
4847

4948
async fn extract(request: Params, source: Arc<Source>, query: Arc<Query>) -> R<Self::Response> {
50-
let ((tx, receipt), exclude_failed, timestamp) =
49+
let ((tx, receipt), block, exclude_failed, timestamp) =
5150
<Transactions as CollectByTransaction>::extract(request, source.clone(), query).await?;
52-
let block_number = tx.block_number.ok_or(err("no block number for tx"))?;
53-
let block = source
54-
.get_block(block_number, BlockTransactionsKind::Hashes)
55-
.await?
56-
.ok_or(CollectError::CollectError("block not found".to_string()))?;
57-
Ok((block, ((tx, receipt), exclude_failed, timestamp)))
51+
Ok((block.clone(), ((tx, receipt), block, exclude_failed, timestamp)))
5852
}
5953

6054
fn transform(response: Self::Response, columns: &mut Self, query: &Arc<Query>) -> R<()> {
6155
let BlocksAndTransactions(blocks, transactions) = columns;
62-
let (block, ((tx, receipt), exclude_failed, timestamp)) = response;
56+
let (block, ((tx, receipt), _, exclude_failed, timestamp)) = response;
57+
let gas_price = transactions::get_gas_price(&block, &tx);
6358
let schema = query.schemas.get_schema(&Datatype::Blocks)?;
6459
blocks::process_block(block, blocks, schema)?;
6560
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
@@ -70,6 +65,7 @@ impl CollectByTransaction for BlocksAndTransactions {
7065
schema,
7166
exclude_failed,
7267
timestamp,
68+
gas_price
7369
)?;
7470
Ok(())
7571
}

crates/freeze/src/types/sources.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,14 @@ impl Source {
259259

260260
// get transactions
261261
let txs = if include_transaction_hashes {
262-
self.get_block(block as u64, BlockTransactionsKind::Hashes)
262+
let transactions = self.get_block(block as u64, BlockTransactionsKind::Hashes)
263263
.await?
264264
.ok_or(CollectError::CollectError("could not find block".to_string()))?
265-
.transactions
266-
.as_transactions()
267-
.unwrap()
268-
.iter()
269-
.map(|tx| Some(tx.inner.tx_hash().to_vec()))
270-
.collect()
265+
.transactions;
266+
match transactions {
267+
BlockTransactions::Hashes(hashes) => hashes.into_iter().map(|tx| Some(tx.to_vec())).collect(),
268+
_ => return Err(CollectError::CollectError("wrong transaction format".to_string()))
269+
}
271270
} else {
272271
vec![None; result.len()]
273272
};

0 commit comments

Comments
 (0)