Skip to content

Commit

Permalink
Unify error enums in substrate and ethereum clients with thiserror
Browse files Browse the repository at this point in the history
…(#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
  • Loading branch information
vmarkushin authored and serban300 committed Apr 9, 2024
1 parent f3b3240 commit 8628d94
Show file tree
Hide file tree
Showing 48 changed files with 482 additions and 381 deletions.
1 change: 1 addition & 0 deletions bridges/relays/bin-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["hmac"]
log = "0.4.14"
num-traits = "0.2"
serde_json = "1.0.64"
thiserror = "1.0.26"

# Bridge dependencies

Expand Down
38 changes: 38 additions & 0 deletions bridges/relays/bin-ethereum/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::rpc_errors::RpcError;
use thiserror::Error;

/// Result type used by PoA relay.
pub type Result<T> = std::result::Result<T, Error>;

/// Ethereum PoA relay errors.
#[derive(Error, Debug)]
pub enum Error {
/// Failed to decode initial header.
#[error("Error decoding initial header: {0}")]
DecodeInitialHeader(codec::Error),
/// RPC error.
#[error("{0}")]
Rpc(#[from] RpcError),
/// Failed to read genesis header.
#[error("Error reading Substrate genesis header: {0:?}")]
ReadGenesisHeader(relay_substrate_client::Error),
/// Failed to read initial GRANDPA authorities.
#[error("Error reading GRANDPA authorities set: {0:?}")]
ReadAuthorities(relay_substrate_client::Error),
/// Failed to deploy bridge contract to Ethereum chain.
#[error("Error deploying contract: {0:?}")]
DeployContract(RpcError),
}
16 changes: 8 additions & 8 deletions bridges/relays/bin-ethereum/src/ethereum_deploy_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::{
error::{Error, Result},
ethereum_client::{bridge_contract, EthereumHighLevelRpc},
rpc_errors::RpcError,
};
Expand Down Expand Up @@ -104,20 +105,20 @@ pub async fn run(params: EthereumDeployContractParams) {
async fn prepare_initial_header(
sub_client: &SubstrateClient<Rialto>,
sub_initial_header: Option<Vec<u8>>,
) -> Result<(RialtoHeaderId, Vec<u8>), String> {
) -> Result<(RialtoHeaderId, Vec<u8>)> {
match sub_initial_header {
Some(raw_initial_header) => {
match rialto_runtime::Header::decode(&mut &raw_initial_header[..]) {
Ok(initial_header) =>
Ok((HeaderId(initial_header.number, initial_header.hash()), raw_initial_header)),
Err(error) => Err(format!("Error decoding initial header: {}", error)),
Err(error) => Err(Error::DecodeInitialHeader(error)),
}
},
None => {
let initial_header = sub_client.header_by_number(Zero::zero()).await;
initial_header
.map(|header| (HeaderId(Zero::zero(), header.hash()), header.encode()))
.map_err(|error| format!("Error reading Substrate genesis header: {:?}", error))
.map_err(|error| Error::ReadGenesisHeader(error))
},
}
}
Expand All @@ -127,14 +128,13 @@ async fn prepare_initial_authorities_set(
sub_client: &SubstrateClient<Rialto>,
sub_initial_header_hash: rialto_runtime::Hash,
sub_initial_authorities_set: Option<Vec<u8>>,
) -> Result<OpaqueGrandpaAuthoritiesSet, String> {
) -> Result<OpaqueGrandpaAuthoritiesSet> {
let initial_authorities_set = match sub_initial_authorities_set {
Some(initial_authorities_set) => Ok(initial_authorities_set),
None => sub_client.grandpa_authorities_set(sub_initial_header_hash).await,
};

initial_authorities_set
.map_err(|error| format!("Error reading GRANDPA authorities set: {:?}", error))
initial_authorities_set.map_err(|error| Error::ReadAuthorities(error))
}

/// Deploy bridge contract to Ethereum chain.
Expand All @@ -145,7 +145,7 @@ async fn deploy_bridge_contract(
initial_header: Vec<u8>,
initial_set_id: u64,
initial_authorities: Vec<u8>,
) -> Result<(), String> {
) -> Result<()> {
eth_client
.submit_ethereum_transaction(
params,
Expand All @@ -160,5 +160,5 @@ async fn deploy_bridge_contract(
),
)
.await
.map_err(|error| format!("Error deploying contract: {:?}", error))
.map_err(|error| Error::DeployContract(error))
}
6 changes: 4 additions & 2 deletions bridges/relays/bin-ethereum/src/ethereum_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub async fn run(params: EthereumExchangeParams) {
async fn run_single_transaction_relay(
params: EthereumExchangeParams,
eth_tx_hash: H256,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let EthereumExchangeParams { eth_params, sub_params, sub_sign, instance, .. } = params;

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

relay_single_transaction_proof(&source, &target, eth_tx_hash).await
relay_single_transaction_proof(&source, &target, eth_tx_hash)
.await
.map_err(Into::into)
}

async fn run_auto_transactions_relay_loop(
Expand Down
11 changes: 6 additions & 5 deletions bridges/relays/bin-ethereum/src/ethereum_exchange_submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//! Submitting Ethereum -> Substrate exchange transactions.

use anyhow::anyhow;
use bp_eth_poa::{
signatures::{secret_to_address, SignTransaction},
UnsignedTransaction,
Expand Down Expand Up @@ -47,10 +48,10 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
let EthereumExchangeSubmitParams { eth_params, eth_sign, eth_nonce, eth_amount, sub_recipient } =
params;

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

let eth_signer_address = secret_to_address(&eth_sign.signer);
let sub_recipient_encoded = sub_recipient;
Expand All @@ -59,7 +60,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
None => eth_client
.account_nonce(eth_signer_address)
.await
.map_err(|err| format!("error fetching acount nonce: {:?}", err))?,
.map_err(|err| anyhow!("error fetching acount nonce: {:?}", err))?,
};
let gas = eth_client
.estimate_gas(CallRequest {
Expand All @@ -70,7 +71,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
..Default::default()
})
.await
.map_err(|err| format!("error estimating gas requirements: {:?}", err))?;
.map_err(|err| anyhow!("error estimating gas requirements: {:?}", err))?;
let eth_tx_unsigned = UnsignedTransaction {
nonce,
gas_price: eth_sign.gas_price,
Expand All @@ -84,7 +85,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
eth_client
.submit_transaction(eth_tx_signed)
.await
.map_err(|err| format!("error submitting transaction: {:?}", err))?;
.map_err(|err| anyhow!("error submitting transaction: {:?}", err))?;

Ok(eth_tx_unsigned)
}
Expand Down
Loading

0 comments on commit 8628d94

Please sign in to comment.