Skip to content

Commit

Permalink
Merge pull request #10 from HerodotusDev/chore/bump-alloy
Browse files Browse the repository at this point in the history
bump all alloy dependencies into `0.1.0`, prepare for release
  • Loading branch information
rkdud007 committed Jun 25, 2024
2 parents 22fcf5a + cf0ce51 commit f96b306
Show file tree
Hide file tree
Showing 9 changed files with 760 additions and 271 deletions.
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

0 comments on commit f96b306

Please sign in to comment.