Replace BATCH_CALL_SUPPORTED (#1733)

* Simplify submit_and_watch_signed_extrinsic

The way submit_and_watch_signed_extrinsic is used now, we can always
derive the SignParam from other params. If in the future we need more
customization possibilities, we can define a new method.

* Simplify submit_signed_extrinsic

* Send maybe_batch_tx as a parameter

Send `maybe_batch_tx` as a parameter to `submit_proof()`. This way we
can deduplicate the logic that submits the extrinsic for
`messages_source and `messages_target` and we can simplify the logic in
the race loop a bit.

* Define BatchProofTransaction

Deduplicate BatchConfirmationTransaction and BatchDeliveryTransaction by
replacing both of them with BatchProofTransaction

* Define ChainWithUtilityPallet and BatchCallBuilderConstructor

- Define `ChainWithUtilityPallet` in order to be able to associate the
  batching functionality with chains
- Defining `BatchCallBuilderConstructor` in order to have a more reliable
  way of checking whether an end of a messages pipeline supports batching
  or no. `BatchCallBuilderConstructor::new_builder()` returns an
  `Option<BatchCallBuilder>`.This is a bit safer because each time a caller
  tries to start creating a batch call, it will call `new_builder()` and
  will be required to handle the returned `Option`. Before we only had a
  bool `BATCH_CALL_SUPPORTED` the caller could have forgetten to check.
This commit is contained in:
Serban Iorga
2022-12-27 15:39:23 +02:00
committed by Bastian Köcher
parent df1aed01c4
commit e47f1e42e0
20 changed files with 290 additions and 494 deletions
+30 -38
View File
@@ -18,7 +18,8 @@
#![warn(missing_docs)]
use relay_substrate_client::Error as SubstrateError;
use relay_substrate_client::{Chain, ChainWithUtilityPallet, UtilityPallet};
use std::marker::PhantomData;
pub mod error;
@@ -90,50 +91,41 @@ impl<AccountId> TaggedAccount<AccountId> {
}
/// Batch call builder.
pub trait BatchCallBuilder<Call> {
/// Associated error type.
type Error;
/// If `true`, then batch calls are supported at the chain.
const BATCH_CALL_SUPPORTED: bool;
pub trait BatchCallBuilder<Call>: Send {
/// Create batch call from given calls vector.
fn build_batch_call(_calls: Vec<Call>) -> Result<Call, Self::Error>;
fn build_batch_call(&self, _calls: Vec<Call>) -> Call;
}
impl<Call> BatchCallBuilder<Call> for () {
type Error = SubstrateError;
const BATCH_CALL_SUPPORTED: bool = false;
fn build_batch_call(_calls: Vec<Call>) -> Result<Call, SubstrateError> {
debug_assert!(
false,
"only called if `BATCH_CALL_SUPPORTED` is true;\
`BATCH_CALL_SUPPORTED` is false;\
qed"
);
Err(SubstrateError::Custom("<() as BatchCallBuilder>::build_batch_call() is called".into()))
}
/// Batch call builder constructor.
pub trait BatchCallBuilderConstructor<Call> {
/// Create a new instance of a batch call builder.
fn new_builder() -> Option<Box<dyn BatchCallBuilder<Call>>>;
}
/// Batch call builder for bundled runtimes.
pub struct BundledBatchCallBuilder<R>(PhantomData<R>);
/// Batch call builder based on `pallet-utility`.
pub struct UtilityPalletBatchCallBuilder<C: Chain>(PhantomData<C>);
impl<R> BatchCallBuilder<<R as frame_system::Config>::RuntimeCall> for BundledBatchCallBuilder<R>
impl<C: Chain> BatchCallBuilder<C::Call> for UtilityPalletBatchCallBuilder<C>
where
R: pallet_utility::Config<RuntimeCall = <R as frame_system::Config>::RuntimeCall>,
<R as frame_system::Config>::RuntimeCall: From<pallet_utility::Call<R>>,
C: ChainWithUtilityPallet,
{
type Error = SubstrateError;
const BATCH_CALL_SUPPORTED: bool = true;
fn build_batch_call(
mut calls: Vec<<R as frame_system::Config>::RuntimeCall>,
) -> Result<<R as frame_system::Config>::RuntimeCall, SubstrateError> {
Ok(if calls.len() == 1 {
calls.remove(0)
} else {
pallet_utility::Call::batch_all { calls }.into()
})
fn build_batch_call(&self, calls: Vec<C::Call>) -> C::Call {
C::UtilityPallet::build_batch_call(calls)
}
}
impl<C: Chain> BatchCallBuilderConstructor<C::Call> for UtilityPalletBatchCallBuilder<C>
where
C: ChainWithUtilityPallet,
{
fn new_builder() -> Option<Box<dyn BatchCallBuilder<C::Call>>> {
Some(Box::new(Self(Default::default())))
}
}
/// A `BatchCallBuilderConstructor` that always returns `None`.
impl<Call> BatchCallBuilderConstructor<Call> for () {
fn new_builder() -> Option<Box<dyn BatchCallBuilder<Call>>> {
None
}
}