-
Notifications
You must be signed in to change notification settings - Fork 49
feat: demonstrate consensus x rpc types unification #211
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! This example demonstrates how alloy's RPC types and consensus types are tied together. | ||
//! | ||
//! Consensus types are used in Ethereum execution layer consensus and communication. These include | ||
//! transactions, headers, blocks, [EIP-2718](https://eips.ethereum.org/EIPS/eip-2718) envelopes, [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930), [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844), and more. | ||
//! | ||
//! The RPC types are used to communicate with Ethereum nodes. | ||
//! | ||
//! In the case of alloy, the consensus types are embedded into the RPC types unlocking a ton of | ||
yash-atreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! simplications across these two categories of types and also preventing accidental divergence | ||
//! between the two. | ||
//! This has been achieved without altering the resultant serialized JSON-RPC representations. | ||
//! | ||
//! One can easily convert the RPC types to their consensus counterparts using the `into_consensus`, | ||
//! `into_inner` or in the case of transactions `into_recovered` methods. | ||
//! | ||
//! See: | ||
//! | ||
//! - [Embed consensus `Header` into RPC type](https://github.com/alloy-rs/alloy/pull/1573) | ||
//! - [Embed `TxEnvelope` into RPC `Transaction`](https://github.com/alloy-rs/alloy/pull/1460) | ||
//! - [Embed consenus `Log` and `Receipt` into respective RPC types](https://github.com/alloy-rs/alloy/pull/396) | ||
|
||
use alloy::{ | ||
eips::BlockId, | ||
primitives::b256, | ||
providers::{Provider, ProviderBuilder}, | ||
}; | ||
use eyre::OptionExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> eyre::Result<()> { | ||
let provider = ProviderBuilder::new().connect("https://reth-ethereum.ithaca.xyz/rpc").await?; | ||
|
||
// Get the latest block from the RPC. | ||
let block = provider.get_block(BlockId::latest()).await?.ok_or_eyre("Block not found")?; | ||
// The immediate type returned is the RPC `Block` type which consists of the relevant consensus | ||
// types. | ||
assert!(matches!(block, alloy::rpc::types::Block { .. })); | ||
// This rpc block type contains the RPC `Header` type which encapsulates the consensus `Header` | ||
// type. | ||
// Easily access the consensus `Header` type without having to rebuild into another type. | ||
// If one needs to map the header or transaction to a different type, the following mapping | ||
// methods can be used: `try_map_header`, `map_header`, `try_map_transactions`, | ||
// `map_transactions`. | ||
assert!(matches!(block.header.inner, alloy::consensus::Header { .. })); | ||
// One can use the `into_consensus` method to get the consensus representation of the block. | ||
let consensus = block.into_consensus(); | ||
assert!(matches!(consensus, alloy::consensus::Block { .. })); | ||
|
||
// Similarly, the RPC `Transaction` and `TransactionReceipt` types encapsulate their | ||
// corresponding consensus types. | ||
|
||
// Get a transaction by hash | ||
// <https://etherscan.io/tx/0x5b470467985bfd34f18979b5438ffce4f2a309a32bcc857fcbf48c4e4253ce16> | ||
let tx_hash = b256!("0x5b470467985bfd34f18979b5438ffce4f2a309a32bcc857fcbf48c4e4253ce16"); | ||
let tx = | ||
provider.get_transaction_by_hash(tx_hash).await?.ok_or_eyre("Transaction not found")?; | ||
assert!(matches!(tx, alloy::rpc::types::Transaction { .. })); | ||
// The RPC `Transaction` type wraps a `Recovered<T>` containing the consensus `Transaction` | ||
yash-atreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// type. | ||
// The `Recovered<T>` consists of the signer recovered from the signature. | ||
// Unifying these reduces verbosity and allows for any one of the types using simple helper | ||
// methods as shown below. | ||
let recovered_tx = tx.into_recovered(); | ||
assert!(matches!(recovered_tx, alloy::consensus::transaction::Recovered { .. })); | ||
assert!(matches!(recovered_tx.inner(), alloy::consensus::EthereumTxEnvelope::Eip1559(_))); | ||
|
||
let receipt = provider | ||
.get_transaction_receipt(tx_hash) | ||
.await? | ||
.ok_or_eyre("Transaction receipt not found")?; | ||
assert!(matches!(receipt, alloy::rpc::types::TransactionReceipt { .. })); | ||
// The `TransactionReceipt` type contains the consensus `ReceiptEnvelope` type. | ||
assert!(matches!(receipt.inner, alloy::consensus::ReceiptEnvelope::Eip1559(_))); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.