Skip to content

Commit

Permalink
tweaks to bring equivalence to ethers cryo
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Jan 6, 2025
1 parent 1abef98 commit 92babbe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/freeze/src/datasets/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Blocks {
state_root: Vec<Vec<u8>>,
transactions_root: Vec<Vec<u8>>,
receipts_root: Vec<Vec<u8>>,
block_number: Vec<Option<u64>>,
block_number: Vec<Option<u32>>,
gas_used: Vec<u64>,
gas_limit: Vec<u64>,
extra_data: Vec<Vec<u8>>,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub(crate) fn process_block<TX>(block: Block<TX>, columns: &mut Blocks, schema:
store!(schema, columns, state_root, block.header.state_root.0.to_vec());
store!(schema, columns, transactions_root, block.header.transactions_root.0.to_vec());
store!(schema, columns, receipts_root, block.header.receipts_root.0.to_vec());
store!(schema, columns, block_number, Some(block.header.number));
store!(schema, columns, block_number, Some(block.header.number as u32));
store!(schema, columns, gas_used, block.header.gas_used);
store!(schema, columns, gas_limit, block.header.gas_limit);
store!(schema, columns, extra_data, block.header.extra_data.to_vec());
Expand Down
51 changes: 43 additions & 8 deletions crates/freeze/src/datasets/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl Dataset for Transactions {
"n_input_bytes",
"n_input_zero_bytes",
"n_input_nonzero_bytes",
"n_rlp_bytes",
"chain_id",
])
}
Expand Down Expand Up @@ -143,13 +142,15 @@ impl CollectByBlock for Transactions {
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
let (block, transactions_with_receipts, exclude_failed) = response;
for (tx, receipt) in transactions_with_receipts.into_iter() {
let gas_price = get_gas_price(&block, &tx);
process_transaction(
tx,
receipt,
columns,
schema,
exclude_failed,
block.header.timestamp as u32,
gas_price
)?;
}
Ok(())
Expand All @@ -158,7 +159,7 @@ impl CollectByBlock for Transactions {

#[async_trait::async_trait]
impl CollectByTransaction for Transactions {
type Response = (TransactionAndReceipt, bool, u32);
type Response = (TransactionAndReceipt, Block, bool, u32);

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

let timestamp = block.header.timestamp as u32;

Ok(((transaction, receipt), query.exclude_failed, timestamp))
Ok(((transaction, receipt), block, query.exclude_failed, timestamp))
}

fn transform(response: Self::Response, columns: &mut Self, query: &Arc<Query>) -> R<()> {
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
let ((transaction, receipt), exclude_failed, timestamp) = response;
process_transaction(transaction, receipt, columns, schema, exclude_failed, timestamp)?;
let ((transaction, receipt), block, exclude_failed, timestamp) = response;
let gas_price = get_gas_price(&block, &transaction);
process_transaction(
transaction,
receipt,
columns,
schema,
exclude_failed,
timestamp,
gas_price
)?;
Ok(())
}
}
Expand All @@ -202,6 +212,7 @@ pub(crate) fn process_transaction(
schema: &Table,
exclude_failed: bool,
timestamp: u32,
gas_price: Option<u64>,
) -> R<()> {
let success = if exclude_failed | schema.has_column("success") {
let success = tx_success(&tx, &receipt)?;
Expand Down Expand Up @@ -244,10 +255,11 @@ pub(crate) fn process_transaction(
}
// in alloy eip2718_encoded_length is rlp_encoded_length
store!(schema, columns, n_rlp_bytes, tx.inner.eip2718_encoded_length() as u32);
store!(schema, columns, gas_used, receipt.map(|r| r.gas_used as u64));
store!(schema, columns, gas_price, tx.inner.gas_price().map(|gas_price| gas_price as u64));
store!(schema, columns, gas_used, receipt.as_ref().map(|r| r.gas_used as u64));
// store!(schema, columns, gas_price, Some(receipt.unwrap().effective_gas_price as u64));
store!(schema, columns, gas_price, gas_price);
store!(schema, columns, transaction_type, tx.inner.tx_type() as u32);
store!(schema, columns, max_fee_per_gas, Some(tx.inner.max_fee_per_gas() as u64));
store!(schema, columns, max_fee_per_gas, get_max_fee_per_gas(&tx));
store!(
schema,
columns,
Expand All @@ -264,6 +276,29 @@ pub(crate) fn process_transaction(
Ok(())
}

fn get_max_fee_per_gas(tx: &Transaction) -> Option<u64> {
match &tx.inner {
alloy::consensus::TxEnvelope::Legacy(_) => None,
alloy::consensus::TxEnvelope::Eip2930(_) => None,
_ => Some(tx.inner.max_fee_per_gas() as u64),
}
}

pub(crate) fn get_gas_price(block: &Block, tx: &Transaction) -> Option<u64> {
match &tx.inner {
alloy::consensus::TxEnvelope::Legacy(_) => tx.gas_price().map(|gas_price| gas_price as u64),
alloy::consensus::TxEnvelope::Eip2930(_) => tx.gas_price().map(|gas_price| gas_price as u64),
_ => {
let base_fee_per_gas = block.header.inner.base_fee_per_gas.unwrap();
let priority_fee = std::cmp::min(
tx.inner.max_priority_fee_per_gas().unwrap() as u64,
tx.inner.max_fee_per_gas() as u64 - base_fee_per_gas,
);
Some(base_fee_per_gas + priority_fee)
}
}
}

fn tx_success(tx: &Transaction, receipt: &Option<TransactionReceipt>) -> R<bool> {
if let Some(r) = receipt {
Ok(r.status())
Expand Down
14 changes: 5 additions & 9 deletions crates/freeze/src/multi_datasets/blocks_and_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{datasets::transactions, types::collection::*, Datatype, *};
use alloy::rpc::types::BlockTransactionsKind;
use polars::prelude::*;
use std::collections::HashMap;

Expand Down Expand Up @@ -47,19 +46,15 @@ impl CollectByTransaction for BlocksAndTransactions {
);

async fn extract(request: Params, source: Arc<Source>, query: Arc<Query>) -> R<Self::Response> {
let ((tx, receipt), exclude_failed, timestamp) =
let ((tx, receipt), block, exclude_failed, timestamp) =
<Transactions as CollectByTransaction>::extract(request, source.clone(), query).await?;
let block_number = tx.block_number.ok_or(err("no block number for tx"))?;
let block = source
.get_block(block_number, BlockTransactionsKind::Hashes)
.await?
.ok_or(CollectError::CollectError("block not found".to_string()))?;
Ok((block, ((tx, receipt), exclude_failed, timestamp)))
Ok((block.clone(), ((tx, receipt), block, exclude_failed, timestamp)))
}

fn transform(response: Self::Response, columns: &mut Self, query: &Arc<Query>) -> R<()> {
let BlocksAndTransactions(blocks, transactions) = columns;
let (block, ((tx, receipt), exclude_failed, timestamp)) = response;
let (block, ((tx, receipt), _, exclude_failed, timestamp)) = response;
let gas_price = transactions::get_gas_price(&block, &tx);
let schema = query.schemas.get_schema(&Datatype::Blocks)?;
blocks::process_block(block, blocks, schema)?;
let schema = query.schemas.get_schema(&Datatype::Transactions)?;
Expand All @@ -70,6 +65,7 @@ impl CollectByTransaction for BlocksAndTransactions {
schema,
exclude_failed,
timestamp,
gas_price
)?;
Ok(())
}
Expand Down
13 changes: 6 additions & 7 deletions crates/freeze/src/types/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,14 @@ impl Source {

// get transactions
let txs = if include_transaction_hashes {
self.get_block(block as u64, BlockTransactionsKind::Hashes)
let transactions = self.get_block(block as u64, BlockTransactionsKind::Hashes)
.await?
.ok_or(CollectError::CollectError("could not find block".to_string()))?
.transactions
.as_transactions()
.unwrap()
.iter()
.map(|tx| Some(tx.inner.tx_hash().to_vec()))
.collect()
.transactions;
match transactions {
BlockTransactions::Hashes(hashes) => hashes.into_iter().map(|tx| Some(tx.to_vec())).collect(),
_ => return Err(CollectError::CollectError("wrong transaction format".to_string()))
}
} else {
vec![None; result.len()]
};
Expand Down

0 comments on commit 92babbe

Please sign in to comment.