Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl TryFrom<TransactionReceipt> for ReceiptEnvelope #395

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions crates/rpc-types/src/eth/transaction/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{other::OtherFields, ConversionError, Log};
use alloy_consensus::{Receipt, ReceiptEnvelope, ReceiptWithBloom, TxType};
use alloy_primitives::{Address, Bloom, Log as PrimitivesLog, LogData, B256, U128, U256, U64, U8};
use alloy_primitives::{Address, Bloom, B256, U128, U256, U64, U8};
use itertools::Itertools;
use serde::{Deserialize, Serialize};

/// Transaction receipt
Expand Down Expand Up @@ -75,23 +76,18 @@ impl TransactionReceipt {
/// A transaction is considered successful if the status code is `1`.
fn success(&self) -> bool {
match &self.status_code {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with changing the status_code from U64 to u64 and use a serde_with hex_opt for this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean #[serde(with = "hex")]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but we can do this separately

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that status is always 0 or 1, I would also be comfortable just deserializing it to a bool. "real" future compatibility would be a non-exhaustive enum imho

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked #396 , will leave this nit on that PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya sorry for stepping on your PR's toes a bit here. I feel like the infallible conversion will serve your needs better? 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries, i like embed approach better.
one q is I had checked impl AsRef<ReceiptEnvelope> for TransactionReceipt meet the goal what this PR had supposed to, should I have to close the PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will talk internally, and then close one of them. If you like the embed approach better, we'll likely go in that direction :)

thank you for making the issue and PR regardless!

Some(status) => status == &U64::from(1),
Some(status) => status.to::<u64>() == 1,
None => false,
}
}

/// Returns the logs emitted by the transaction.
/// Converts the logs from the RPC type to the internal type.
fn logs(&self) -> Vec<PrimitivesLog<LogData>> {
let mut logs = Vec::new();
for log in &self.logs {
let rpc_log: Log = log.clone();
let log_data = LogData::try_from(rpc_log).unwrap_or_default();
let result = PrimitivesLog { address: log.address, data: log_data };
logs.push(result);
}

logs
/// Returns an iterator over the logs for prmitives type conversion.
fn logs_iter(
&self,
) -> impl Iterator<Item = alloy_primitives::Log<alloy_primitives::LogData>> + '_ {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also want into_logs, so we can consume the type in TryFrom

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 fn into_logs(self) -> impl Iterator<Item = alloy_primitives::Log<alloy_primitives::LogData>> {
        self.logs
            .into_iter()
            .map(|log| alloy_primitives::Log::new_unchecked(log.address, log.topics, log.data))
    }

just to confirm, is this what you suggested to have? not 100% sure

self.logs.iter().map(|log| {
alloy_primitives::Log::new_unchecked(log.address, log.topics.clone(), log.data.clone())
})
}
}

Expand All @@ -103,7 +99,7 @@ impl TryFrom<TransactionReceipt> for ReceiptWithBloom {
receipt: Receipt {
success: tx_receipt.success(),
cumulative_gas_used: tx_receipt.cumulative_gas_used.to::<u64>(),
logs: tx_receipt.logs(),
logs: tx_receipt.logs_iter().collect_vec(),
},
bloom: tx_receipt.logs_bloom,
};
Expand Down