Skip to content

Commit 6a6717d

Browse files
authored
Merge pull request #12 from HerodotusDev/error
error unify with `thisiserror`
2 parents e2b4606 + 8e0f71a commit 6a6717d

9 files changed

+137
-85
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ clap = { version = "4.5.4", features = ["derive"] }
3737
serde = "1.0.197"
3838
serde_with = { version = "3.7.0", features = ["hex"] }
3939
serde_json = "1.0.114"
40+
thiserror = "1.0"
4041

4142
[[bin]]
4243
name = "etp-cli"

src/bin/cli.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::Serialize;
77
use serde_with::serde_as;
88

99
use eth_trie_proofs::tx_receipt_trie::TxReceiptsMptHandler;
10-
use eth_trie_proofs::Error;
10+
use eth_trie_proofs::EthTrieError;
1111

1212
#[derive(Debug, Parser)]
1313
#[command(name = "eth-trie-proof")]
@@ -45,7 +45,7 @@ struct MptProof {
4545
}
4646

4747
#[tokio::main]
48-
async fn main() -> Result<(), Error> {
48+
async fn main() -> Result<(), EthTrieError> {
4949
let cli = Cli::parse();
5050
match cli.command {
5151
Commands::Tx { tx_hash, rpc_url } => {
@@ -71,7 +71,7 @@ async fn main() -> Result<(), Error> {
7171
Ok(())
7272
}
7373

74-
async fn generate_tx_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
74+
async fn generate_tx_proof(tx_hash: &str, rpc_url: &str) -> Result<(), EthTrieError> {
7575
let rpc_url = url::Url::parse(rpc_url).expect("Invalid URL");
7676
let mut txs_mpt_handler = TxsMptHandler::new(rpc_url)?;
7777
let tx_hash = B256::from_hex(tx_hash).unwrap();
@@ -86,7 +86,7 @@ async fn generate_tx_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
8686
Ok(())
8787
}
8888

89-
async fn generate_receipt_proof(tx_hash: &str, rpc_url: &str) -> Result<(), Error> {
89+
async fn generate_receipt_proof(tx_hash: &str, rpc_url: &str) -> Result<(), EthTrieError> {
9090
let rpc_url = url::Url::parse(rpc_url).expect("Invalid URL");
9191
let mut tx_receipts_mpt_handler = TxReceiptsMptHandler::new(rpc_url)?;
9292
let tx_hash = B256::from_hex(tx_hash).unwrap();

src/lib.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1+
use core::fmt;
2+
13
use alloy::transports::{RpcError, TransportErrorKind};
24
use eth_trie::TrieError;
5+
use thiserror::Error;
36

47
mod rpc;
58
pub mod tx;
69
pub mod tx_receipt;
710
pub mod tx_receipt_trie;
811
pub mod tx_trie;
912

10-
#[derive(Debug)]
11-
pub enum Error {
13+
#[derive(Error, Debug)]
14+
pub enum EthTrieError {
15+
#[error("Trie error: {0}")]
1216
Trie(TrieError),
17+
#[error("EIP error: {0}")]
1318
Eip(alloy::eips::eip2718::Eip2718Error),
19+
#[error("RLP error: {0}")]
1420
Rlp(alloy_rlp::Error),
21+
#[error("RPC error: {0}")]
1522
RPC(RpcError<TransportErrorKind>),
23+
#[error("Transaction not found")]
1624
TxNotFound,
25+
#[error("Block not found")]
1726
BlockNotFound,
27+
#[error("Invalid transaction version")]
1828
InvalidTxVersion,
29+
#[error("Error converting field: {0}")]
1930
ConversionError(Field),
31+
#[error("Unexpected root")]
2032
UnexpectedRoot,
33+
#[error("Invalid mpt proof")]
2134
InvalidMPTProof,
35+
#[error("Invalid transaction trie")]
2236
TrieNotFound,
37+
#[error("Field not found")]
2338
FieldNotFound,
2439
}
2540

@@ -37,8 +52,25 @@ pub enum Field {
3752
Signature,
3853
}
3954

40-
impl From<TrieError> for Error {
55+
impl fmt::Display for Field {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
match self {
58+
Field::ChainId => write!(f, "chain_id"),
59+
Field::Nonce => write!(f, "nonce"),
60+
Field::GasPrice => write!(f, "gas_price"),
61+
Field::GasLimit => write!(f, "gas_limit"),
62+
Field::Input => write!(f, "input"),
63+
Field::AccessList => write!(f, "access_list"),
64+
Field::MaxFeePerGas => write!(f, "max_fee_per_gas"),
65+
Field::MaxPriorityFeePerGas => write!(f, "max_priority_fee_per_gas"),
66+
Field::MaxFeePerBlobGas => write!(f, "max_fee_per_blob_gas"),
67+
Field::Signature => write!(f, "signature"),
68+
}
69+
}
70+
}
71+
72+
impl From<TrieError> for EthTrieError {
4173
fn from(err: TrieError) -> Self {
42-
Error::Trie(err)
74+
EthTrieError::Trie(err)
4375
}
4476
}

src/rpc.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Error;
1+
use crate::EthTrieError;
22
use alloy::network::Ethereum;
33
use alloy::primitives::B256;
44
use alloy::providers::{Provider, RootProvider};
@@ -20,19 +20,19 @@ impl RpcProvider {
2020
pub(crate) async fn get_block_transactions(
2121
&self,
2222
block_number: u64,
23-
) -> Result<(Vec<Transaction>, B256), Error> {
23+
) -> Result<(Vec<Transaction>, B256), EthTrieError> {
2424
let block = self
2525
.provider
2626
.get_block(
2727
block_number.into(),
2828
alloy::rpc::types::BlockTransactionsKind::Full,
2929
)
3030
.await?
31-
.ok_or_else(|| Error::BlockNotFound)?;
31+
.ok_or_else(|| EthTrieError::BlockNotFound)?;
3232

3333
let txs = match block.transactions {
3434
BlockTransactions::Full(txs) => txs,
35-
_ => return Err(Error::TxNotFound),
35+
_ => return Err(EthTrieError::TxNotFound),
3636
};
3737

3838
Ok((txs, block.header.transactions_root))
@@ -41,26 +41,26 @@ impl RpcProvider {
4141
pub(crate) async fn get_block_transaction_receipts(
4242
&self,
4343
block_number: u64,
44-
) -> Result<(Vec<TransactionReceipt>, B256), Error> {
44+
) -> Result<(Vec<TransactionReceipt>, B256), EthTrieError> {
4545
let block = self
4646
.provider
4747
.get_block(
4848
block_number.into(),
4949
alloy::rpc::types::BlockTransactionsKind::Full,
5050
)
5151
.await?
52-
.ok_or_else(|| Error::BlockNotFound)?;
52+
.ok_or_else(|| EthTrieError::BlockNotFound)?;
5353

5454
let tx_receipts = self
5555
.provider
5656
.get_block_receipts(block_number.into())
5757
.await?
58-
.ok_or_else(|| Error::BlockNotFound)?;
58+
.ok_or_else(|| EthTrieError::BlockNotFound)?;
5959

6060
Ok((tx_receipts, block.header.receipts_root))
6161
}
6262

63-
pub(crate) async fn get_tx_index_by_hash(&self, tx_hash: B256) -> Result<u64, Error> {
63+
pub(crate) async fn get_tx_index_by_hash(&self, tx_hash: B256) -> Result<u64, EthTrieError> {
6464
let tx = self
6565
.provider
6666
.get_transaction_by_hash(tx_hash)
@@ -69,13 +69,13 @@ impl RpcProvider {
6969

7070
let index: u64 = match tx.transaction_index {
7171
Some(index) => index,
72-
None => return Err(Error::TxNotFound),
72+
None => return Err(EthTrieError::TxNotFound),
7373
};
7474

7575
Ok(index)
7676
}
7777

78-
pub(crate) async fn get_tx_block_height(&self, tx_hash: B256) -> Result<u64, Error> {
78+
pub(crate) async fn get_tx_block_height(&self, tx_hash: B256) -> Result<u64, EthTrieError> {
7979
let tx = self
8080
.provider
8181
.get_transaction_by_hash(tx_hash)
@@ -84,15 +84,15 @@ impl RpcProvider {
8484

8585
let height: u64 = match tx.block_number {
8686
Some(height) => height,
87-
None => return Err(Error::TxNotFound),
87+
None => return Err(EthTrieError::TxNotFound),
8888
};
8989

9090
Ok(height)
9191
}
9292
}
9393

94-
impl From<RpcError<TransportErrorKind>> for Error {
94+
impl From<RpcError<TransportErrorKind>> for EthTrieError {
9595
fn from(err: RpcError<TransportErrorKind>) -> Self {
96-
Error::RPC(err)
96+
EthTrieError::RPC(err)
9797
}
9898
}

src/tx.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Error, Field};
1+
use crate::{EthTrieError, Field};
22
use alloy::consensus::{
33
SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEnvelope, TxLegacy, TxType,
44
};
@@ -18,8 +18,8 @@ impl ConsensusTx {
1818
self.0.encoded_2718()
1919
}
2020

21-
pub fn rlp_decode(mut data: &[u8]) -> Result<Self, Error> {
22-
let tx = TxEnvelope::decode_2718(&mut data).map_err(Error::Eip)?;
21+
pub fn rlp_decode(mut data: &[u8]) -> Result<Self, EthTrieError> {
22+
let tx = TxEnvelope::decode_2718(&mut data).map_err(EthTrieError::Eip)?;
2323
Ok(ConsensusTx(tx))
2424
}
2525

@@ -207,8 +207,8 @@ impl ConsensusTx {
207207
pub(crate) struct RpcTx(pub Transaction);
208208

209209
impl TryFrom<RpcTx> for ConsensusTx {
210-
type Error = Error;
211-
fn try_from(tx: RpcTx) -> Result<ConsensusTx, Error> {
210+
type Error = EthTrieError;
211+
fn try_from(tx: RpcTx) -> Result<ConsensusTx, EthTrieError> {
212212
let chain_id = tx.chain_id();
213213
let nonce: u64 = tx.0.nonce;
214214
let gas_limit: u128 = tx.0.gas;
@@ -267,13 +267,13 @@ impl TryFrom<RpcTx> for ConsensusTx {
267267
TxType::Eip4844 => {
268268
let to = match tx.to() {
269269
TxKind::Call(to) => to,
270-
TxKind::Create => return Err(Error::InvalidTxVersion),
270+
TxKind::Create => return Err(EthTrieError::InvalidTxVersion),
271271
};
272272
let blob_versioned_hashes = tx
273273
.clone()
274274
.0
275275
.blob_versioned_hashes
276-
.ok_or(Error::ConversionError(Field::Input))?;
276+
.ok_or(EthTrieError::ConversionError(Field::Input))?;
277277
let max_fee_per_gas = tx.max_fee_per_gas()?;
278278
let max_priority_fee_per_gas = tx.max_priority_fee_per_gas()?;
279279
let max_fee_per_blob_gas = tx.max_fee_per_blob_gas()?;
@@ -309,42 +309,42 @@ impl RpcTx {
309309
}
310310
}
311311

312-
fn version(&self) -> Result<TxType, Error> {
312+
fn version(&self) -> Result<TxType, EthTrieError> {
313313
match self.0.transaction_type {
314314
Some(0) => Ok(TxType::Legacy),
315315
Some(1) => Ok(TxType::Eip2930),
316316
Some(2) => Ok(TxType::Eip1559),
317317
Some(3) => Ok(TxType::Eip4844),
318318
None => Ok(TxType::Legacy),
319-
_ => Err(Error::InvalidTxVersion),
319+
_ => Err(EthTrieError::InvalidTxVersion),
320320
}
321321
}
322322

323-
fn max_fee_per_gas(&self) -> Result<u128, Error> {
323+
fn max_fee_per_gas(&self) -> Result<u128, EthTrieError> {
324324
if let Some(value) = self.0.max_fee_per_gas {
325325
Ok(value)
326326
} else {
327327
Ok(0)
328328
}
329329
}
330330

331-
fn max_priority_fee_per_gas(&self) -> Result<u128, Error> {
331+
fn max_priority_fee_per_gas(&self) -> Result<u128, EthTrieError> {
332332
if let Some(value) = self.0.max_priority_fee_per_gas {
333333
Ok(value)
334334
} else {
335335
Ok(0)
336336
}
337337
}
338338

339-
fn max_fee_per_blob_gas(&self) -> Result<u128, Error> {
339+
fn max_fee_per_blob_gas(&self) -> Result<u128, EthTrieError> {
340340
if let Some(value) = self.0.max_fee_per_blob_gas {
341341
Ok(value)
342342
} else {
343343
Ok(0)
344344
}
345345
}
346346

347-
fn signature(&self) -> Result<Signature, Error> {
347+
fn signature(&self) -> Result<Signature, EthTrieError> {
348348
if let Some(signature) = self.0.signature {
349349
let sig = Signature::from_rs_and_parity(
350350
signature.r,
@@ -353,23 +353,23 @@ impl RpcTx {
353353
signature
354354
.v
355355
.try_into()
356-
.map_err(|_| Error::ConversionError(Field::Signature))?,
356+
.map_err(|_| EthTrieError::ConversionError(Field::Signature))?,
357357
),
358358
)
359-
.map_err(|_| Error::ConversionError(Field::Signature))?;
359+
.map_err(|_| EthTrieError::ConversionError(Field::Signature))?;
360360

361361
Ok(sig)
362362
} else {
363-
Err(Error::ConversionError(Field::Signature))
363+
Err(EthTrieError::ConversionError(Field::Signature))
364364
}
365365
}
366366

367-
fn access_list(&self) -> Result<AccessList, Error> {
367+
fn access_list(&self) -> Result<AccessList, EthTrieError> {
368368
if let Some(al) = self.0.access_list.clone() {
369369
let target_list_items: Vec<AccessListItem> = Vec::<AccessListItem>::from(al);
370370
Ok(AccessList(target_list_items))
371371
} else {
372-
Err(Error::ConversionError(Field::AccessList))
372+
Err(EthTrieError::ConversionError(Field::AccessList))
373373
}
374374
}
375375
}

src/tx_receipt.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Error;
1+
use crate::EthTrieError;
22
use alloy::consensus::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt};
33
use alloy::consensus::{ReceiptEnvelope, TxType};
44
use alloy::eips::eip2718::Decodable2718;
@@ -15,8 +15,8 @@ impl ConsensusTxReceipt {
1515
self.0.encoded_2718()
1616
}
1717

18-
pub fn rlp_decode(mut data: &[u8]) -> Result<Self, Error> {
19-
let envelope = ReceiptEnvelope::decode_2718(&mut data).map_err(Error::Eip)?;
18+
pub fn rlp_decode(mut data: &[u8]) -> Result<Self, EthTrieError> {
19+
let envelope = ReceiptEnvelope::decode_2718(&mut data).map_err(EthTrieError::Eip)?;
2020
Ok(ConsensusTxReceipt(envelope))
2121
}
2222

@@ -65,8 +65,8 @@ impl ConsensusTxReceipt {
6565
pub(crate) struct RpcTxReceipt(pub TransactionReceipt);
6666

6767
impl TryFrom<RpcTxReceipt> for ConsensusTxReceipt {
68-
type Error = Error;
69-
fn try_from(tx: RpcTxReceipt) -> Result<ConsensusTxReceipt, Error> {
68+
type Error = EthTrieError;
69+
fn try_from(tx: RpcTxReceipt) -> Result<ConsensusTxReceipt, EthTrieError> {
7070
match &tx.version()? {
7171
TxType::Legacy => {
7272
let res = ReceiptEnvelope::Legacy(ReceiptWithBloom {
@@ -117,7 +117,7 @@ impl TryFrom<RpcTxReceipt> for ConsensusTxReceipt {
117117
}
118118

119119
impl RpcTxReceipt {
120-
fn version(&self) -> Result<TxType, Error> {
120+
fn version(&self) -> Result<TxType, EthTrieError> {
121121
Ok(self.0.transaction_type())
122122
}
123123

0 commit comments

Comments
 (0)