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

bump all alloy dependencies into 0.1.0, prepare for release #10

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
670 changes: 574 additions & 96 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ exclude = [".github"]

[dependencies]
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread", "macros"] }
# alloy is still yet stable, important to pin the revision
alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-transport = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3", features = [
alloy-primitives = { version = "0.7.6" }
alloy = { version = "0.1.1", features = [
"rpc",
"rpc-types",
"rpc-client",
"network",
"providers",
"eips",
"transports",
"transport-http",
"consensus",
"k256",
] }
alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "52d16d3" }
alloy-primitives = "0.6.4"
url = "2.5.0"
reqwest = "0.11.26"
alloy-rlp = "0.3.4"
alloy-rlp = { version = "0.3.5" }
eth_trie = "0.4.0"
ethereum-types = "0.14.1"
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async fn main() -> Result<(), Error> {
}

async fn generate_tx_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
let rpc_url = url::Url::parse(rpc_url).expect("Invalid URL");
let mut txs_mpt_handler = TxsMptHandler::new(rpc_url)?;
let tx_hash = B256::from_hex(tx_hash).unwrap();
txs_mpt_handler.build_tx_tree_from_tx_hash(tx_hash).await?;
Expand All @@ -86,6 +87,7 @@ async fn generate_tx_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
}

async fn generate_receipt_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
let rpc_url = url::Url::parse(rpc_url).expect("Invalid URL");
let mut tx_receipts_mpt_handler = TxReceiptsMptHandler::new(rpc_url)?;
let tx_hash = B256::from_hex(tx_hash).unwrap();
tx_receipts_mpt_handler
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_transport::{RpcError, TransportErrorKind};
use alloy::transports::{RpcError, TransportErrorKind};
use eth_trie::TrieError;

mod rpc;
Expand All @@ -10,6 +10,7 @@ pub mod tx_trie;
#[derive(Debug)]
pub enum Error {
Trie(TrieError),
Eip(alloy::eips::eip2718::Eip2718Error),
Rlp(alloy_rlp::Error),
RPC(RpcError<TransportErrorKind>),
TxNotFound,
Expand Down
49 changes: 30 additions & 19 deletions src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use crate::Error;
use alloy_network::Ethereum;
use alloy_primitives::B256;
use alloy_provider::{Provider, ProviderBuilder, RootProvider};
use alloy_rpc_client::RpcClient;
use alloy_rpc_types::{BlockTransactions, Transaction, TransactionReceipt};
use alloy_transport::{RpcError, TransportErrorKind};
use alloy_transport_http::Http;
use reqwest::Client;
use alloy::network::Ethereum;
use alloy::primitives::B256;
use alloy::providers::{Provider, RootProvider};

use alloy::rpc::types::{BlockTransactions, Transaction, TransactionReceipt};
use alloy::transports::http::{Client, Http};
use alloy::transports::{RpcError, TransportErrorKind};

pub(crate) struct RpcProvider {
provider: RootProvider<Ethereum, Http<Client>>,
provider: RootProvider<Http<Client>, Ethereum>,
}

impl RpcProvider {
pub(crate) fn new(url: &str) -> Self {
let http = Http::<Client>::new(url.to_string().parse().unwrap());
let provider = ProviderBuilder::<_, Ethereum>::new()
.provider(RootProvider::new(RpcClient::new(http, true)));
pub(crate) fn new(rpc_url: url::Url) -> Self {
let provider = RootProvider::new_http(rpc_url);
Self { provider }
}

Expand All @@ -26,7 +23,10 @@ impl RpcProvider {
) -> Result<(Vec<Transaction>, B256), Error> {
let block = self
.provider
.get_block(block_number.into(), true)
.get_block(
block_number.into(),
alloy::rpc::types::BlockTransactionsKind::Full,
)
.await?
.ok_or_else(|| Error::BlockNotFound)?;

Expand All @@ -44,7 +44,10 @@ impl RpcProvider {
) -> Result<(Vec<TransactionReceipt>, B256), Error> {
let block = self
.provider
.get_block(block_number.into(), true)
.get_block(
block_number.into(),
alloy::rpc::types::BlockTransactionsKind::Full,
)
.await?
.ok_or_else(|| Error::BlockNotFound)?;

Expand All @@ -58,21 +61,29 @@ impl RpcProvider {
}

pub(crate) async fn get_tx_index_by_hash(&self, tx_hash: B256) -> Result<u64, Error> {
let tx = self.provider.get_transaction_by_hash(tx_hash).await?;
let tx = self
.provider
.get_transaction_by_hash(tx_hash)
.await?
.expect("tx not found");

let index: u64 = match tx.transaction_index {
Some(index) => index.try_into().map_err(|_| Error::TxNotFound)?,
Some(index) => index,
None => return Err(Error::TxNotFound),
};

Ok(index)
}

pub(crate) async fn get_tx_block_height(&self, tx_hash: B256) -> Result<u64, Error> {
let tx = self.provider.get_transaction_by_hash(tx_hash).await?;
let tx = self
.provider
.get_transaction_by_hash(tx_hash)
.await?
.expect("tx not found");

let height: u64 = match tx.block_number {
Some(height) => height.try_into().map_err(|_| Error::TxNotFound)?,
Some(height) => height,
None => return Err(Error::TxNotFound),
};

Expand Down
Loading