Skip to content

Commit

Permalink
add WithOtherFields for TransactionReceipt (#1455)
Browse files Browse the repository at this point in the history
add WithOtherFields for TransactionReceipt
  • Loading branch information
tcoratger authored Oct 14, 2024
1 parent 278a562 commit cec43f8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/eth_rpc/api/eth_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub trait EthApi {

/// Returns the receipt of a transaction by transaction hash.
#[method(name = "getTransactionReceipt")]
async fn transaction_receipt(&self, hash: B256) -> Result<Option<TransactionReceipt>>;
async fn transaction_receipt(&self, hash: B256) -> Result<Option<WithOtherFields<TransactionReceipt>>>;

/// Returns the balance of the account of given address.
#[method(name = "getBalance")]
Expand Down Expand Up @@ -268,5 +268,8 @@ pub trait EthApi {

/// Returns all transaction receipts for a given block.
#[method(name = "getBlockReceipts")]
async fn block_receipts(&self, block_id: Option<BlockId>) -> Result<Option<Vec<TransactionReceipt>>>;
async fn block_receipts(
&self,
block_id: Option<BlockId>,
) -> Result<Option<Vec<WithOtherFields<TransactionReceipt>>>>;
}
7 changes: 5 additions & 2 deletions src/eth_rpc/servers/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
}

#[tracing::instrument(skip(self), ret, err)]
async fn transaction_receipt(&self, hash: B256) -> Result<Option<TransactionReceipt>> {
async fn transaction_receipt(&self, hash: B256) -> Result<Option<WithOtherFields<TransactionReceipt>>> {
Ok(self.eth_client.eth_provider().transaction_receipt(hash).await?)
}

Expand Down Expand Up @@ -305,7 +305,10 @@ where
Err(EthApiError::Unsupported("eth_getFilterLogs").into())
}

async fn block_receipts(&self, block_id: Option<BlockId>) -> Result<Option<Vec<TransactionReceipt>>> {
async fn block_receipts(
&self,
block_id: Option<BlockId>,
) -> Result<Option<Vec<WithOtherFields<TransactionReceipt>>>> {
Ok(self.eth_client.eth_provider().block_receipts(block_id).await?)
}
}
5 changes: 3 additions & 2 deletions src/providers/debug_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugProvider for DebugDataPro
.map_err(|_| EthApiError::EthereumDataFormat(EthereumDataFormatError::ReceiptConversion))?;

// Tries to convert the cumulative gas used to u64
let cumulative_gas_used = TryInto::<u64>::try_into(receipt.inner.cumulative_gas_used())
let cumulative_gas_used = TryInto::<u64>::try_into(receipt.inner.inner.cumulative_gas_used())
.map_err(|_| EthApiError::EthereumDataFormat(EthereumDataFormatError::ReceiptConversion))?;

// Creates a ReceiptWithBloom from the receipt data
Expand All @@ -161,13 +161,14 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugProvider for DebugDataPro
success: receipt.inner.status(),
cumulative_gas_used,
logs: receipt
.inner
.inner
.logs()
.iter()
.filter_map(|log| Log::new(log.address(), log.topics().to_vec(), log.data().data.clone()))
.collect(),
},
bloom: *receipt.inner.logs_bloom(),
bloom: *receipt.inner.inner.logs_bloom(),
}
.envelope_encoded(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/providers/eth_provider/database/types/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl From<Log> for StoredLog {

impl From<StoredTransactionReceipt> for Vec<StoredLog> {
fn from(value: StoredTransactionReceipt) -> Self {
value.receipt.inner.logs().iter().cloned().map(Into::into).collect()
value.receipt.inner.inner.logs().iter().cloned().map(Into::into).collect()
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/providers/eth_provider/database/types/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloy_rpc_types::TransactionReceipt;
use alloy_serde::WithOtherFields;
#[cfg(any(test, feature = "arbitrary", feature = "testing"))]
use reth_primitives::Receipt;
use serde::{Deserialize, Serialize};
Expand All @@ -7,10 +8,10 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct StoredTransactionReceipt {
#[serde(deserialize_with = "crate::providers::eth_provider::database::types::serde::deserialize_intermediate")]
pub receipt: TransactionReceipt,
pub receipt: WithOtherFields<TransactionReceipt>,
}

impl From<StoredTransactionReceipt> for TransactionReceipt {
impl From<StoredTransactionReceipt> for WithOtherFields<TransactionReceipt> {
fn from(receipt: StoredTransactionReceipt) -> Self {
receipt.receipt
}
Expand Down Expand Up @@ -48,7 +49,7 @@ impl<'a> arbitrary::Arbitrary<'a> for StoredTransactionReceipt {
};

Ok(Self {
receipt: TransactionReceipt {
receipt: WithOtherFields::new(TransactionReceipt {
transaction_hash: B256::arbitrary(u)?,
transaction_index: Some(u64::arbitrary(u)?),
block_hash: Some(B256::arbitrary(u)?),
Expand All @@ -69,7 +70,7 @@ impl<'a> arbitrary::Arbitrary<'a> for StoredTransactionReceipt {
_ => unreachable!(),
},
authorization_list: None,
},
}),
})
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/providers/eth_provider/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::providers::eth_provider::{
};
use alloy_primitives::B256;
use alloy_rpc_types::TransactionReceipt;
use alloy_serde::WithOtherFields;
use async_trait::async_trait;
use auto_impl::auto_impl;
use mongodb::bson::doc;
Expand All @@ -17,23 +18,29 @@ use reth_primitives::{BlockId, BlockNumberOrTag};
#[auto_impl(Arc, &)]
pub trait ReceiptProvider {
/// Returns the transaction receipt by hash of the transaction.
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<TransactionReceipt>>;
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<WithOtherFields<TransactionReceipt>>>;

/// Returns the block receipts for a block.
async fn block_receipts(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<TransactionReceipt>>>;
async fn block_receipts(
&self,
block_id: Option<BlockId>,
) -> EthApiResult<Option<Vec<WithOtherFields<TransactionReceipt>>>>;
}

#[async_trait]
impl<SP> ReceiptProvider for EthDataProvider<SP>
where
SP: starknet::providers::Provider + Send + Sync,
{
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<TransactionReceipt>> {
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<WithOtherFields<TransactionReceipt>>> {
let filter = EthDatabaseFilterBuilder::<filter::Receipt>::default().with_tx_hash(&hash).build();
Ok(self.database().get_one::<StoredTransactionReceipt>(filter, None).await?.map(Into::into))
}

async fn block_receipts(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<TransactionReceipt>>> {
async fn block_receipts(
&self,
block_id: Option<BlockId>,
) -> EthApiResult<Option<Vec<WithOtherFields<TransactionReceipt>>>> {
match block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)) {
BlockId::Number(number_or_tag) => {
let block_number = self.tag_into_block_number(number_or_tag).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/test_utils/mock_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ mock! {

#[async_trait]
impl ReceiptProvider for EthereumProviderStruct {
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<TransactionReceipt>>;
async fn transaction_receipt(&self, hash: B256) -> EthApiResult<Option<WithOtherFields<TransactionReceipt>>>;

async fn block_receipts(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<TransactionReceipt>>>;
async fn block_receipts(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<WithOtherFields<TransactionReceipt>>>>;
}

#[async_trait]
Expand Down
4 changes: 2 additions & 2 deletions src/test_utils/mongo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl MongoFuzzer {
let mut receipt = StoredTransactionReceipt::arbitrary(&mut unstructured).unwrap();

// Ensure the block number in receipt is equal to the block number in transaction.
let mut modified_logs = (*receipt.receipt.inner.as_receipt_with_bloom().unwrap()).clone();
let mut modified_logs = (*receipt.receipt.inner.inner.as_receipt_with_bloom().unwrap()).clone();
for log in &mut modified_logs.receipt.logs {
log.block_number = Some(transaction.block_number.unwrap_or_default());
log.block_hash = transaction.block_hash;
Expand All @@ -220,7 +220,7 @@ impl MongoFuzzer {
receipt.receipt.to = transaction.to;
receipt.receipt.block_number = transaction.block_number;
receipt.receipt.block_hash = transaction.block_hash;
receipt.receipt.inner = match transaction.transaction_type.unwrap_or_default().try_into() {
receipt.receipt.inner.inner = match transaction.transaction_type.unwrap_or_default().try_into() {
Ok(TxType::Legacy) => alloy_rpc_types::ReceiptEnvelope::Legacy(modified_logs),
Ok(TxType::Eip2930) => alloy_rpc_types::ReceiptEnvelope::Eip2930(modified_logs),
Ok(TxType::Eip1559) => alloy_rpc_types::ReceiptEnvelope::Eip1559(modified_logs),
Expand Down
5 changes: 3 additions & 2 deletions tests/tests/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,16 @@ async fn test_raw_receipts(#[future] katana: Katana, _setup: ()) {
receipt: Receipt {
tx_type: Into::<u8>::into(tx_receipt.transaction_type()).try_into().unwrap(),
success: tx_receipt.inner.status(),
cumulative_gas_used: TryInto::<u64>::try_into(tx_receipt.inner.cumulative_gas_used()).unwrap(),
cumulative_gas_used: TryInto::<u64>::try_into(tx_receipt.inner.inner.cumulative_gas_used()).unwrap(),
logs: tx_receipt
.inner
.inner
.logs()
.iter()
.filter_map(|log| Log::new(log.address(), log.topics().to_vec(), log.data().data.clone()))
.collect(),
},
bloom: *receipt.inner.logs_bloom(),
bloom: *receipt.inner.inner.logs_bloom(),
}
.envelope_encoded();

Expand Down

0 comments on commit cec43f8

Please sign in to comment.