// Copyright 2019-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 .
//! 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::{
BatchProofTransaction, MessageLaneAdapter, ReceiveMessagesProofCallBuilder,
SubstrateMessageLane,
},
messages_source::{ensure_messages_pallet_active, read_client_state, SubstrateMessagesProof},
on_demand::OnDemandRelay,
TransactionParams,
};
use async_std::sync::Arc;
use async_trait::async_trait;
use bp_messages::{
storage_keys::inbound_lane_data_key, total_unrewarded_messages, InboundLaneData, LaneId,
MessageNonce, UnrewardedRelayersState,
};
use bridge_runtime_common::messages::source::FromBridgedChainMessagesDeliveryProof;
use messages_relay::{
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
message_lane_loop::{NoncesSubmitArtifacts, TargetClient, TargetClientState},
};
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, BalanceOf, CallOf, Chain, ChainWithMessages, Client,
Error as SubstrateError, HashOf, TransactionEra, TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
use sp_core::Pair;
use std::{collections::VecDeque, convert::TryFrom, ops::RangeInclusive};
/// Message receiving proof returned by the target Substrate node.
pub type SubstrateMessagesDeliveryProof =
(UnrewardedRelayersState, FromBridgedChainMessagesDeliveryProof>);
/// Substrate client as Substrate messages target.
pub struct SubstrateMessagesTarget {
target_client: Client,
source_client: Client,
lane_id: LaneId,
relayer_id_at_source: AccountIdOf,
transaction_params: TransactionParams>,
source_to_target_headers_relay: Option>>,
}
impl SubstrateMessagesTarget
{
/// Create new Substrate headers target.
pub fn new(
target_client: Client,
source_client: Client,
lane_id: LaneId,
relayer_id_at_source: AccountIdOf,
transaction_params: TransactionParams>,
source_to_target_headers_relay: Option<
Arc>,
>,
) -> Self {
SubstrateMessagesTarget {
target_client,
source_client,
lane_id,
relayer_id_at_source,
transaction_params,
source_to_target_headers_relay,
}
}
/// Read inbound lane state from the on-chain storage at given block.
async fn inbound_lane_data(
&self,
id: TargetHeaderIdOf>,
) -> Result