// Copyright 2019-2020 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 . //! Substrate client as Substrate messages target. The chain we connect to should have //! runtime that implements `HeaderApi` to allow bridging with //! chain. use crate::messages_lane::SubstrateMessageLane; use crate::messages_source::read_client_state; use async_trait::async_trait; use bp_message_lane::{LaneId, MessageNonce}; use bp_runtime::InstanceId; use codec::{Decode, Encode}; use messages_relay::{ message_lane::{SourceHeaderIdOf, TargetHeaderIdOf}, message_lane_loop::{TargetClient, TargetClientState}, }; use relay_substrate_client::{Chain, Client, Error as SubstrateError, HashOf}; use relay_utils::BlockNumberBase; use sp_core::Bytes; use sp_runtime::{traits::Header as HeaderT, DeserializeOwned}; use sp_trie::StorageProof; use std::ops::RangeInclusive; /// Substrate client as Substrate messages target. pub struct SubstrateMessagesTarget { client: Client, lane: P, lane_id: LaneId, instance: InstanceId, } impl SubstrateMessagesTarget { /// Create new Substrate headers target. pub fn new(client: Client, lane: P, lane_id: LaneId, instance: InstanceId) -> Self { SubstrateMessagesTarget { client, lane, lane_id, instance, } } } impl Clone for SubstrateMessagesTarget { fn clone(&self) -> Self { Self { client: self.client.clone(), lane: self.lane.clone(), lane_id: self.lane_id, instance: self.instance, } } } #[async_trait] impl TargetClient

for SubstrateMessagesTarget where C: Chain, C::Header: DeserializeOwned, C::Index: DeserializeOwned, ::Number: BlockNumberBase, P: SubstrateMessageLane< MessagesReceivingProof = (HashOf, StorageProof, LaneId), TargetHeaderNumber = ::Number, TargetHeaderHash = ::Hash, >, P::SourceHeaderNumber: Decode, P::SourceHeaderHash: Decode, { type Error = SubstrateError; async fn reconnect(mut self) -> Result { let new_client = self.client.clone().reconnect().await?; self.client = new_client; Ok(self) } async fn state(&self) -> Result, Self::Error> { read_client_state::<_, P::SourceHeaderHash, P::SourceHeaderNumber>( &self.client, P::BEST_FINALIZED_SOURCE_HEADER_ID_AT_TARGET, ) .await } async fn latest_received_nonce( &self, id: TargetHeaderIdOf

, ) -> Result<(TargetHeaderIdOf

, MessageNonce), Self::Error> { let encoded_response = self .client .state_call( P::INBOUND_LANE_LATEST_RECEIVED_NONCE_METHOD.into(), Bytes(self.lane_id.encode()), Some(id.1), ) .await?; let latest_received_nonce: MessageNonce = Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?; Ok((id, latest_received_nonce)) } async fn latest_confirmed_received_nonce( &self, id: TargetHeaderIdOf

, ) -> Result<(TargetHeaderIdOf

, MessageNonce), Self::Error> { let encoded_response = self .client .state_call( P::INBOUND_LANE_LATEST_CONFIRMED_NONCE_METHOD.into(), Bytes(self.lane_id.encode()), Some(id.1), ) .await?; let latest_received_nonce: MessageNonce = Decode::decode(&mut &encoded_response.0[..]).map_err(SubstrateError::ResponseParseFailed)?; Ok((id, latest_received_nonce)) } async fn prove_messages_receiving( &self, id: TargetHeaderIdOf

, ) -> Result<(TargetHeaderIdOf

, P::MessagesReceivingProof), Self::Error> { let proof = self .client .prove_messages_delivery(self.instance, self.lane_id, id.1) .await?; let proof = (id.1, proof, self.lane_id); Ok((id, proof)) } async fn submit_messages_proof( &self, generated_at_header: SourceHeaderIdOf

, nonces: RangeInclusive, proof: P::MessagesProof, ) -> Result, Self::Error> { let tx = self .lane .make_messages_delivery_transaction(generated_at_header, nonces.clone(), proof) .await?; self.client.submit_extrinsic(Bytes(tx.encode())).await?; Ok(nonces) } }