Skip to content

Commit

Permalink
in auto-relays keep trying to connect to nodes until connection is es…
Browse files Browse the repository at this point in the history
…tablished (paritytech#971)
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent 02b3e4c commit cf08616
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 18 deletions.
4 changes: 2 additions & 2 deletions bridges/relays/bin-ethereum/src/ethereum_deploy_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub async fn run(params: EthereumDeployContractParams) {
} = params;

let result = async move {
let eth_client = EthereumClient::new(eth_params).await.map_err(RpcError::Ethereum)?;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await.map_err(RpcError::Substrate)?;
let eth_client = EthereumClient::try_connect(eth_params).await.map_err(RpcError::Ethereum)?;
let sub_client = SubstrateClient::<Rialto>::try_connect(sub_params).await.map_err(RpcError::Substrate)?;

let (initial_header_id, initial_header) = prepare_initial_header(&sub_client, sub_initial_header).await?;
let initial_set_id = sub_initial_authorities_set_id.unwrap_or(0);
Expand Down
14 changes: 6 additions & 8 deletions bridges/relays/bin-ethereum/src/ethereum_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ async fn run_single_transaction_relay(params: EthereumExchangeParams, eth_tx_has
..
} = params;

let eth_client = EthereumClient::new(eth_params).await.map_err(RpcError::Ethereum)?;
let sub_client = SubstrateClient::<Rialto>::new(sub_params)
let eth_client = EthereumClient::try_connect(eth_params)
.await
.map_err(RpcError::Ethereum)?;
let sub_client = SubstrateClient::<Rialto>::try_connect(sub_params)
.await
.map_err(RpcError::Substrate)?;

Expand All @@ -363,12 +365,8 @@ async fn run_auto_transactions_relay_loop(
..
} = params;

let eth_client = EthereumClient::new(eth_params)
.await
.map_err(|err| format!("Error starting Ethereum client: {:?}", err))?;
let sub_client = SubstrateClient::<Rialto>::new(sub_params)
.await
.map_err(|err| format!("Error starting Substrate client: {:?}", err))?;
let eth_client = EthereumClient::new(eth_params).await;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await;

let eth_start_with_block_number = match eth_start_with_block_number {
Some(eth_start_with_block_number) => eth_start_with_block_number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn run(params: EthereumExchangeSubmitParams) {
} = params;

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

Expand Down
4 changes: 2 additions & 2 deletions bridges/relays/bin-ethereum/src/ethereum_sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ pub async fn run(params: EthereumSyncParams) -> Result<(), RpcError> {
instance,
} = params;

let eth_client = EthereumClient::new(eth_params).await?;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await?;
let eth_client = EthereumClient::new(eth_params).await;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await;

let sign_sub_transactions = match sync_params.target_tx_mode {
TargetTransactionMode::Signed | TargetTransactionMode::Backup => true,
Expand Down
4 changes: 2 additions & 2 deletions bridges/relays/bin-ethereum/src/substrate_sync_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ pub async fn run(params: SubstrateSyncParams) -> Result<(), RpcError> {
metrics_params,
} = params;

let eth_client = EthereumClient::new(eth_params).await?;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await?;
let eth_client = EthereumClient::new(eth_params).await;
let sub_client = SubstrateClient::<Rialto>::new(sub_params).await;

let target = EthereumHeadersTarget::new(eth_client, eth_contract_address, eth_sign);
let source = SubstrateHeadersSource::new(sub_client);
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ macro_rules! declare_chain_options {
port: self.[<$chain_prefix _port>],
secure: self.[<$chain_prefix _secure>],
})
.await?
.await
)
}
}
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
async-std = "1.6.5"
bp-eth-poa = { path = "../../primitives/ethereum-poa" }
codec = { package = "parity-scale-codec", version = "2.0.0" }
headers-relay = { path = "../headers" }
Expand Down
24 changes: 23 additions & 1 deletion bridges/relays/client-ethereum/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::types::{
use crate::{ConnectionParams, Error, Result};

use jsonrpsee_ws_client::{WsClient as RpcClient, WsClientBuilder as RpcClientBuilder};
use relay_utils::relay_loop::RECONNECT_DELAY;
use std::sync::Arc;

/// Number of headers missing from the Ethereum node for us to consider node not synced.
Expand All @@ -36,7 +37,28 @@ pub struct Client {

impl Client {
/// Create a new Ethereum RPC Client.
pub async fn new(params: ConnectionParams) -> Result<Self> {
///
/// This function will keep connecting to given Ethereum node until connection is established
/// and is functional. If attempt fail, it will wait for `RECONNECT_DELAY` and retry again.
pub async fn new(params: ConnectionParams) -> Self {
loop {
match Self::try_connect(params.clone()).await {
Ok(client) => return client,
Err(error) => log::error!(
target: "bridge",
"Failed to connect to Ethereum node: {:?}. Going to retry in {}s",
error,
RECONNECT_DELAY.as_secs(),
),
}

async_std::task::sleep(RECONNECT_DELAY).await;
}
}

/// Try to connect to Ethereum node. Returns Ethereum RPC client if connection has been established
/// or error otherwise.
pub async fn try_connect(params: ConnectionParams) -> Result<Self> {
Ok(Self {
client: Self::build_client(&params).await?,
params,
Expand Down
25 changes: 24 additions & 1 deletion bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use jsonrpsee_ws_client::{traits::SubscriptionClient, v2::params::JsonRpcParams,
use jsonrpsee_ws_client::{Subscription, WsClient as RpcClient, WsClientBuilder as RpcClientBuilder};
use num_traits::Zero;
use pallet_balances::AccountData;
use relay_utils::relay_loop::RECONNECT_DELAY;
use sp_core::{storage::StorageKey, Bytes};
use sp_trie::StorageProof;
use sp_version::RuntimeVersion;
Expand Down Expand Up @@ -77,7 +78,29 @@ impl<C: Chain> std::fmt::Debug for Client<C> {

impl<C: Chain> Client<C> {
/// Returns client that is able to call RPCs on Substrate node over websocket connection.
pub async fn new(params: ConnectionParams) -> Result<Self> {
///
/// This function will keep connecting to given Sustrate node until connection is established
/// and is functional. If attempt fail, it will wait for `RECONNECT_DELAY` and retry again.
pub async fn new(params: ConnectionParams) -> Self {
loop {
match Self::try_connect(params.clone()).await {
Ok(client) => return client,
Err(error) => log::error!(
target: "bridge",
"Failed to connect to {} node: {:?}. Going to retry in {}s",
C::NAME,
error,
RECONNECT_DELAY.as_secs(),
),
}

async_std::task::sleep(RECONNECT_DELAY).await;
}
}

/// Try to connect to Substrate node over websocket. Returns Substrate RPC client if connection
/// has been established or error otherwise.
pub async fn try_connect(params: ConnectionParams) -> Result<Self> {
let client = Self::build_client(params.clone()).await?;

let number: C::BlockNumber = Zero::zero();
Expand Down

0 comments on commit cf08616

Please sign in to comment.