Skip to content

Commit 5842968

Browse files
vmarkushinbkchr
authored andcommitted
Unify error enums in substrate and ethereum clients with thiserror (#1094)
* Unify error enums in substrate and ethereum clients with `thiserror` Related to paritytech/parity-bridges-common#857 * Add license pre-amble * rustfmt * Fix spelling
1 parent 7b4f1c2 commit 5842968

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+482
-381
lines changed

bridges/relays/bin-ethereum/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["hmac"]
2121
log = "0.4.14"
2222
num-traits = "0.2"
2323
serde_json = "1.0.64"
24+
thiserror = "1.0.26"
2425

2526
# Bridge dependencies
2627

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2021 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity Bridges Common.
3+
// Parity Bridges Common is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
// Parity Bridges Common is distributed in the hope that it will be useful,
8+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
// GNU General Public License for more details.
11+
// You should have received a copy of the GNU General Public License
12+
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
13+
14+
use crate::rpc_errors::RpcError;
15+
use thiserror::Error;
16+
17+
/// Result type used by PoA relay.
18+
pub type Result<T> = std::result::Result<T, Error>;
19+
20+
/// Ethereum PoA relay errors.
21+
#[derive(Error, Debug)]
22+
pub enum Error {
23+
/// Failed to decode initial header.
24+
#[error("Error decoding initial header: {0}")]
25+
DecodeInitialHeader(codec::Error),
26+
/// RPC error.
27+
#[error("{0}")]
28+
Rpc(#[from] RpcError),
29+
/// Failed to read genesis header.
30+
#[error("Error reading Substrate genesis header: {0:?}")]
31+
ReadGenesisHeader(relay_substrate_client::Error),
32+
/// Failed to read initial GRANDPA authorities.
33+
#[error("Error reading GRANDPA authorities set: {0:?}")]
34+
ReadAuthorities(relay_substrate_client::Error),
35+
/// Failed to deploy bridge contract to Ethereum chain.
36+
#[error("Error deploying contract: {0:?}")]
37+
DeployContract(RpcError),
38+
}

bridges/relays/bin-ethereum/src/ethereum_deploy_contract.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use crate::{
18+
error::{Error, Result},
1819
ethereum_client::{bridge_contract, EthereumHighLevelRpc},
1920
rpc_errors::RpcError,
2021
};
@@ -104,20 +105,20 @@ pub async fn run(params: EthereumDeployContractParams) {
104105
async fn prepare_initial_header(
105106
sub_client: &SubstrateClient<Rialto>,
106107
sub_initial_header: Option<Vec<u8>>,
107-
) -> Result<(RialtoHeaderId, Vec<u8>), String> {
108+
) -> Result<(RialtoHeaderId, Vec<u8>)> {
108109
match sub_initial_header {
109110
Some(raw_initial_header) => {
110111
match rialto_runtime::Header::decode(&mut &raw_initial_header[..]) {
111112
Ok(initial_header) =>
112113
Ok((HeaderId(initial_header.number, initial_header.hash()), raw_initial_header)),
113-
Err(error) => Err(format!("Error decoding initial header: {}", error)),
114+
Err(error) => Err(Error::DecodeInitialHeader(error)),
114115
}
115116
},
116117
None => {
117118
let initial_header = sub_client.header_by_number(Zero::zero()).await;
118119
initial_header
119120
.map(|header| (HeaderId(Zero::zero(), header.hash()), header.encode()))
120-
.map_err(|error| format!("Error reading Substrate genesis header: {:?}", error))
121+
.map_err(|error| Error::ReadGenesisHeader(error))
121122
},
122123
}
123124
}
@@ -127,14 +128,13 @@ async fn prepare_initial_authorities_set(
127128
sub_client: &SubstrateClient<Rialto>,
128129
sub_initial_header_hash: rialto_runtime::Hash,
129130
sub_initial_authorities_set: Option<Vec<u8>>,
130-
) -> Result<OpaqueGrandpaAuthoritiesSet, String> {
131+
) -> Result<OpaqueGrandpaAuthoritiesSet> {
131132
let initial_authorities_set = match sub_initial_authorities_set {
132133
Some(initial_authorities_set) => Ok(initial_authorities_set),
133134
None => sub_client.grandpa_authorities_set(sub_initial_header_hash).await,
134135
};
135136

136-
initial_authorities_set
137-
.map_err(|error| format!("Error reading GRANDPA authorities set: {:?}", error))
137+
initial_authorities_set.map_err(|error| Error::ReadAuthorities(error))
138138
}
139139

140140
/// Deploy bridge contract to Ethereum chain.
@@ -145,7 +145,7 @@ async fn deploy_bridge_contract(
145145
initial_header: Vec<u8>,
146146
initial_set_id: u64,
147147
initial_authorities: Vec<u8>,
148-
) -> Result<(), String> {
148+
) -> Result<()> {
149149
eth_client
150150
.submit_ethereum_transaction(
151151
params,
@@ -160,5 +160,5 @@ async fn deploy_bridge_contract(
160160
),
161161
)
162162
.await
163-
.map_err(|error| format!("Error deploying contract: {:?}", error))
163+
.map_err(|error| Error::DeployContract(error))
164164
}

bridges/relays/bin-ethereum/src/ethereum_exchange.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub async fn run(params: EthereumExchangeParams) {
339339
async fn run_single_transaction_relay(
340340
params: EthereumExchangeParams,
341341
eth_tx_hash: H256,
342-
) -> Result<(), String> {
342+
) -> anyhow::Result<()> {
343343
let EthereumExchangeParams { eth_params, sub_params, sub_sign, instance, .. } = params;
344344

345345
let eth_client = EthereumClient::try_connect(eth_params).await.map_err(RpcError::Ethereum)?;
@@ -354,7 +354,9 @@ async fn run_single_transaction_relay(
354354
bridge_instance: instance,
355355
};
356356

357-
relay_single_transaction_proof(&source, &target, eth_tx_hash).await
357+
relay_single_transaction_proof(&source, &target, eth_tx_hash)
358+
.await
359+
.map_err(Into::into)
358360
}
359361

360362
async fn run_auto_transactions_relay_loop(

bridges/relays/bin-ethereum/src/ethereum_exchange_submit.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
//! Submitting Ethereum -> Substrate exchange transactions.
1818
19+
use anyhow::anyhow;
1920
use bp_eth_poa::{
2021
signatures::{secret_to_address, SignTransaction},
2122
UnsignedTransaction,
@@ -47,10 +48,10 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
4748
let EthereumExchangeSubmitParams { eth_params, eth_sign, eth_nonce, eth_amount, sub_recipient } =
4849
params;
4950

50-
let result: Result<_, String> = async move {
51+
let result: anyhow::Result<_> = async move {
5152
let eth_client = EthereumClient::try_connect(eth_params)
5253
.await
53-
.map_err(|err| format!("error connecting to Ethereum node: {:?}", err))?;
54+
.map_err(|err| anyhow!("error connecting to Ethereum node: {:?}", err))?;
5455

5556
let eth_signer_address = secret_to_address(&eth_sign.signer);
5657
let sub_recipient_encoded = sub_recipient;
@@ -59,7 +60,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
5960
None => eth_client
6061
.account_nonce(eth_signer_address)
6162
.await
62-
.map_err(|err| format!("error fetching acount nonce: {:?}", err))?,
63+
.map_err(|err| anyhow!("error fetching acount nonce: {:?}", err))?,
6364
};
6465
let gas = eth_client
6566
.estimate_gas(CallRequest {
@@ -70,7 +71,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
7071
..Default::default()
7172
})
7273
.await
73-
.map_err(|err| format!("error estimating gas requirements: {:?}", err))?;
74+
.map_err(|err| anyhow!("error estimating gas requirements: {:?}", err))?;
7475
let eth_tx_unsigned = UnsignedTransaction {
7576
nonce,
7677
gas_price: eth_sign.gas_price,
@@ -84,7 +85,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
8485
eth_client
8586
.submit_transaction(eth_tx_signed)
8687
.await
87-
.map_err(|err| format!("error submitting transaction: {:?}", err))?;
88+
.map_err(|err| anyhow!("error submitting transaction: {:?}", err))?;
8889

8990
Ok(eth_tx_unsigned)
9091
}

0 commit comments

Comments
 (0)