Switch to new RPC interface (#131)

* Move EthereumRpc implementation to Eth client

* Move SubstrateRpc implementation to SubstrateClient

* Update deploy_contract to use new RPC interface

* Fix some types in the Substrate client

* Swap out method bodies in Eth sync loop

* Swap out method bodies in Substrate sync loop

* Remove Client from SourceClient trait return types

* Remove Client from TargetClient trait return types

* Remove client from Source select! arms

* Remove client from Target select! arms

* Add missing mutable refs in Substrate client

* Use mutable references in Source/Target Client traits

* Try and use mutable references in Source/Client trait implementations

* Handle errors more gracefully

* Remove unused imports

* Remove dead_code and unused_variables lints

* Remove usage of `jsonrpsee::RawClient`

By using a `jsonrpsee::Client` we are able to remove all the shared
mutable references required when interacting with the RPC server. This
is convenient as trying to sharing mutable references in code that uses
async/await is a bit of a pain.

However, using a `Client` instead of a `RawClient` is not yet supported
by the `jsonrpsee::rpc_api` macro, so a fork must be used for the moment.

* Clean up dead code and warnings

* Clean up higher level RPCs

Some of the RPCs that were "high level" didn't necessarily belong
as part of the trait, so they were removed.

* Use positional parameters for RPCs

Both Substrate and Ethereum's RPCs use positional (array) parameters,
so in order to be compatible with both we need to make sure that
our API is defined with positional paramters in mind.

* Rename argument for eth_getBlockByNumber

* Remove some unecessary Ok-wraps

* Process client requests synchonously

Before the refactoring the sync loop would wait until a client finished
handling a request before issuing another one. This behaviour was
inadvertently changed during the refactoring leading to race conditions.
This commit makes sure that the previous behaviour is respected.

* Reduce the errors that are considered a connection error

* Only decode bridge contract once

* Set genesis_config at RPC client startup

* Fetch genesis hash in SubstrateRpcClient::new()

* Move Decode error into SubstrateNodeError

* Suppress warnings caused by `rpc_api!`

* Implement From RpcError for String

* Handle Substrate client initalization errors more gracefully

* Remove match in favour of ?

Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com>
This commit is contained in:
Hernando Castano
2020-06-23 16:55:51 -04:00
committed by Bastian Köcher
parent ba3b8537a5
commit ea45fa8da7
11 changed files with 856 additions and 1248 deletions
@@ -16,18 +16,22 @@
//! Substrate -> Ethereum synchronization.
use crate::ethereum_client::{self, EthereumConnectionParams, EthereumSigningParams};
use crate::ethereum_client::{
EthereumConnectionParams, EthereumHighLevelRpc, EthereumRpcClient, EthereumSigningParams,
};
use crate::ethereum_types::Address;
use crate::substrate_client::{self, SubstrateConnectionParams};
use crate::rpc::SubstrateRpc;
use crate::rpc_errors::RpcError;
use crate::substrate_client::{SubstrateConnectionParams, SubstrateRpcClient};
use crate::substrate_types::{
GrandpaJustification, Hash, Header, Number, QueuedSubstrateHeader, SubstrateHeaderId, SubstrateHeadersSyncPipeline,
};
use crate::sync::{HeadersSyncParams, TargetTransactionMode};
use crate::sync_loop::{OwnedSourceFutureOutput, OwnedTargetFutureOutput, SourceClient, TargetClient};
use crate::sync_loop::{SourceClient, TargetClient};
use crate::sync_types::SourceHeader;
use async_trait::async_trait;
use futures::future::FutureExt;
use std::{collections::HashSet, time::Duration};
/// Interval at which we check new Substrate headers when we are synced/almost synced.
@@ -42,7 +46,7 @@ const MAX_SUBMITTED_HEADERS: usize = 4;
const PRUNE_DEPTH: u32 = 256;
/// Substrate synchronization parameters.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SubstrateSyncParams {
/// Ethereum connection params.
pub eth: EthereumConnectionParams,
@@ -84,170 +88,125 @@ impl Default for SubstrateSyncParams {
/// Substrate client as headers source.
struct SubstrateHeadersSource {
/// Substrate node client.
client: substrate_client::Client,
client: SubstrateRpcClient,
}
type SubstrateFutureOutput<T> = OwnedSourceFutureOutput<SubstrateHeadersSource, SubstrateHeadersSyncPipeline, T>;
impl SubstrateHeadersSource {
fn new(client: SubstrateRpcClient) -> Self {
Self { client }
}
}
#[async_trait]
impl SourceClient<SubstrateHeadersSyncPipeline> for SubstrateHeadersSource {
type Error = substrate_client::Error;
type Error = RpcError;
async fn best_block_number(self) -> SubstrateFutureOutput<Number> {
substrate_client::best_header(self.client)
.map(|(client, result)| (SubstrateHeadersSource { client }, result.map(|header| header.number)))
.await
async fn best_block_number(&self) -> Result<Number, Self::Error> {
Ok(self.client.best_header().await?.number)
}
async fn header_by_hash(self, hash: Hash) -> SubstrateFutureOutput<Header> {
substrate_client::header_by_hash(self.client, hash)
.map(|(client, result)| (SubstrateHeadersSource { client }, result))
.await
async fn header_by_hash(&self, hash: Hash) -> Result<Header, Self::Error> {
self.client.header_by_hash(hash).await
}
async fn header_by_number(self, number: Number) -> SubstrateFutureOutput<Header> {
substrate_client::header_by_number(self.client, number)
.map(|(client, result)| (SubstrateHeadersSource { client }, result))
.await
async fn header_by_number(&self, number: Number) -> Result<Header, Self::Error> {
self.client.header_by_number(number).await
}
async fn header_completion(
self,
&self,
id: SubstrateHeaderId,
) -> SubstrateFutureOutput<(SubstrateHeaderId, Option<GrandpaJustification>)> {
substrate_client::grandpa_justification(self.client, id)
.map(|(client, result)| (SubstrateHeadersSource { client }, result))
.await
) -> Result<(SubstrateHeaderId, Option<GrandpaJustification>), Self::Error> {
let hash = id.1;
let signed_block = self.client.get_block(Some(hash)).await?;
let grandpa_justification = signed_block.justification;
Ok((id, grandpa_justification))
}
async fn header_extra(
self,
&self,
id: SubstrateHeaderId,
_header: QueuedSubstrateHeader,
) -> SubstrateFutureOutput<(SubstrateHeaderId, ())> {
(self, Ok((id, ())))
) -> Result<(SubstrateHeaderId, ()), Self::Error> {
Ok((id, ()))
}
}
/// Ethereum client as Substrate headers target.
struct EthereumHeadersTarget {
/// Ethereum node client.
client: ethereum_client::Client,
client: EthereumRpcClient,
/// Bridge contract address.
contract: Address,
/// Ethereum signing params.
sign_params: EthereumSigningParams,
}
type EthereumFutureOutput<T> = OwnedTargetFutureOutput<EthereumHeadersTarget, SubstrateHeadersSyncPipeline, T>;
impl EthereumHeadersTarget {
fn new(client: EthereumRpcClient, contract: Address, sign_params: EthereumSigningParams) -> Self {
Self {
client,
contract,
sign_params,
}
}
}
#[async_trait]
impl TargetClient<SubstrateHeadersSyncPipeline> for EthereumHeadersTarget {
type Error = ethereum_client::Error;
type Error = RpcError;
async fn best_header_id(self) -> EthereumFutureOutput<SubstrateHeaderId> {
let (contract, sign_params) = (self.contract, self.sign_params);
ethereum_client::best_substrate_block(self.client, contract)
.map(move |(client, result)| {
(
EthereumHeadersTarget {
client,
contract,
sign_params,
},
result,
)
})
async fn best_header_id(&self) -> Result<SubstrateHeaderId, Self::Error> {
self.client.best_substrate_block(self.contract).await
}
async fn is_known_header(&self, id: SubstrateHeaderId) -> Result<(SubstrateHeaderId, bool), Self::Error> {
self.client.substrate_header_known(self.contract, id).await
}
async fn submit_headers(&self, headers: Vec<QueuedSubstrateHeader>) -> Result<Vec<SubstrateHeaderId>, Self::Error> {
self.client
.submit_substrate_headers(self.sign_params.clone(), self.contract, headers)
.await
}
async fn is_known_header(self, id: SubstrateHeaderId) -> EthereumFutureOutput<(SubstrateHeaderId, bool)> {
let (contract, sign_params) = (self.contract, self.sign_params);
ethereum_client::substrate_header_known(self.client, contract, id)
.map(move |(client, result)| {
(
EthereumHeadersTarget {
client,
contract,
sign_params,
},
result,
)
})
.await
}
async fn submit_headers(self, headers: Vec<QueuedSubstrateHeader>) -> EthereumFutureOutput<Vec<SubstrateHeaderId>> {
let (contract, sign_params) = (self.contract, self.sign_params);
ethereum_client::submit_substrate_headers(self.client, sign_params.clone(), contract, headers)
.map(move |(client, result)| {
(
EthereumHeadersTarget {
client,
contract,
sign_params,
},
result,
)
})
.await
}
async fn incomplete_headers_ids(self) -> EthereumFutureOutput<HashSet<SubstrateHeaderId>> {
let (contract, sign_params) = (self.contract, self.sign_params);
ethereum_client::incomplete_substrate_headers(self.client, contract)
.map(move |(client, result)| {
(
EthereumHeadersTarget {
client,
contract,
sign_params,
},
result,
)
})
.await
async fn incomplete_headers_ids(&self) -> Result<HashSet<SubstrateHeaderId>, Self::Error> {
self.client.incomplete_substrate_headers(self.contract).await
}
async fn complete_header(
self,
&self,
id: SubstrateHeaderId,
completion: GrandpaJustification,
) -> EthereumFutureOutput<SubstrateHeaderId> {
let (contract, sign_params) = (self.contract, self.sign_params);
ethereum_client::complete_substrate_header(self.client, sign_params.clone(), contract, id, completion)
.map(move |(client, result)| {
(
EthereumHeadersTarget {
client,
contract,
sign_params,
},
result,
)
})
) -> Result<SubstrateHeaderId, Self::Error> {
self.client
.complete_substrate_header(self.sign_params.clone(), self.contract, id, completion)
.await
}
async fn requires_extra(self, header: QueuedSubstrateHeader) -> EthereumFutureOutput<(SubstrateHeaderId, bool)> {
(self, Ok((header.header().id(), false)))
async fn requires_extra(&self, header: QueuedSubstrateHeader) -> Result<(SubstrateHeaderId, bool), Self::Error> {
Ok((header.header().id(), false))
}
}
/// Run Substrate headers synchronization.
pub fn run(params: SubstrateSyncParams) {
let eth_client = ethereum_client::client(params.eth);
let sub_client = substrate_client::client(params.sub);
pub fn run(params: SubstrateSyncParams) -> Result<(), RpcError> {
let sub_params = params.clone();
let eth_client = EthereumRpcClient::new(params.eth);
let sub_client = async_std::task::block_on(async { SubstrateRpcClient::new(sub_params.sub).await })?;
let target = EthereumHeadersTarget::new(eth_client, params.eth_contract_address, params.eth_sign);
let source = SubstrateHeadersSource::new(sub_client);
crate::sync_loop::run(
SubstrateHeadersSource { client: sub_client },
source,
SUBSTRATE_TICK_INTERVAL,
EthereumHeadersTarget {
client: eth_client,
contract: params.eth_contract_address,
sign_params: params.eth_sign,
},
target,
ETHEREUM_TICK_INTERVAL,
params.sync_params,
);
Ok(())
}