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

[Feature] Simplify conversion from TransactionReceipt for RLP-encoding #1939

Closed
Wollac opened this issue Jan 22, 2025 · 3 comments
Closed

[Feature] Simplify conversion from TransactionReceipt for RLP-encoding #1939

Wollac opened this issue Jan 22, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@Wollac
Copy link
Contributor

Wollac commented Jan 22, 2025

Component

network, json-rpc

Describe the feature you would like

It would be beneficial to have a more straightforward way to RLP-encode a TransactionReceipt or to easily convert it into a ReceiptEnvelope, which already implements the necessary encoding traits. Currently, the only process I could find requires manually converting internal structures like alloy_rpc_types::Log to alloy_primitives::Log, which is very cumbersome and error-prone.

Here's an example of the current workaround:

match tx_receipt.inner {
  ReceiptEnvelope::Legacy(t) => ReceiptEnvelope::Legacy(
        ReceiptWithBloom::new(
            Receipt {
                status: t.receipt.status,
                cumulative_gas_used: t.cumulative_gas_used(),
                logs: t.receipt.logs.into_iter().map(|log| log.inner).collect(),
            },
            t.logs_bloom,
        )),
  // ... (Handle other ReceiptEnvelope variants)
})

While I understand that issues #623 and #854 aim to provide a more generic solution, a simple utility method or trait implementation in the interim would significantly improve developer experience and potentially save time for those facing similar use cases.

Context:

A primary motivation for this request is the need to validate receipts returned for a block. While calculating the transaction root is straightforward using the existing features (as shown below), verifying the receipt root for a generic Provider connected to a Network does not currently appear to be possible at all, and even for Ethereum it requires the manual conversion outlined above.

let block = provider
    .get_block_by_number(BlockNumberOrTag::Latest, BlockTransactionsKind::Full)
    .await?
    .unwrap();

let transactions = block.transactions().as_transactions().unwrap();
let transactions_root = alloy_trie::root::ordered_trie_root_with_encoder(transactions, |tx, buf| {
        Encodable2718::encode_2718(tx.as_ref(), buf)
    });
assert_eq!(block.header().transactions_root(), transactions_root);

Ideally, a similar approach should be possible for validating receipt roots. A utility or trait to simplify the conversion from TransactionReceipt to a readily RLP-encodable format would enable this functionality until the aforementioned issues are resolved.

Additional context

No response

@Wollac Wollac added the enhancement New feature or request label Jan 22, 2025
@mattsse
Copy link
Member

mattsse commented Jan 22, 2025

I see, yeah the rpc embeddings still have some rough edges,

what we can do here is solving this via a few helper functions

  1. add easy conversion from ReceiptEnvelope<rpc::Log> to ReceiptEnvelope<primitives::Log> we can make this possible if we add a
impl<T> ReceiptEnvelope<T> where T: Into<primitives::Log> {
   // name tbd
   fn map_consensus(&self) -> ReceiptEnvelope<primitives::Log> {delegate to receipt impl}
}

impl<T> Receipt<T> where T: Into<primitives::Log> { 
   //.. convert log type
}

and then do something similar on tx receipt

do you want to give this a try?

also

let transactions_root = alloy_trie::root::ordered_trie_root_with_encoder(transactions, |tx, buf| {
        Encodable2718::encode_2718(tx.as_ref(), buf)
    });

this we can add to BlockTransactions/block directly

@mattsse
Copy link
Member

mattsse commented Jan 25, 2025

@Wollac I've added a few helpers for this in #1949 now the txreceipt conversion to primitives log should be a simple .into() call

@mattsse
Copy link
Member

mattsse commented Jan 31, 2025

marking this as resolved via #1949 #1950

@mattsse mattsse closed this as completed Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants