Skip to content

Commit

Permalink
Encode and estimate Rococo/Wococo/Kusama/Polkadot messages (paritytec…
Browse files Browse the repository at this point in the history
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent 5dc4714 commit e89155d
Show file tree
Hide file tree
Showing 36 changed files with 518 additions and 241 deletions.
20 changes: 12 additions & 8 deletions bridges/primitives/polkadot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use bp_messages::MessageNonce;
use bp_runtime::Chain;
use bp_runtime::{Chain, EncodedOrDecodedCall};
use frame_support::{
dispatch::Dispatchable,
parameter_types,
Expand Down Expand Up @@ -228,8 +228,12 @@ pub type SignedBlock = generic::SignedBlock<Block>;
pub type Balance = u128;

/// Unchecked Extrinsic type.
pub type UncheckedExtrinsic<Call> =
generic::UncheckedExtrinsic<AccountAddress, Call, Signature, SignedExtensions<Call>>;
pub type UncheckedExtrinsic<Call> = generic::UncheckedExtrinsic<
AccountAddress,
EncodedOrDecodedCall<Call>,
Signature,
SignedExtensions<Call>,
>;

/// Account address, used by the Polkadot-like chain.
pub type Address = MultiAddress<AccountId, ()>;
Expand Down Expand Up @@ -336,12 +340,12 @@ where

fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
_who: &Self::AccountId,
_call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(self.validate(who, call, info, len).map(|_| ())?)
Ok(())
}
}

Expand Down
65 changes: 64 additions & 1 deletion bridges/primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use codec::{Decode, Encode};
use frame_support::{weights::Weight, Parameter};
use num_traits::{AsPrimitive, Bounded, CheckedSub, Saturating, SaturatingAdd, Zero};
use sp_runtime::{
Expand All @@ -23,7 +24,69 @@ use sp_runtime::{
},
FixedPointOperand,
};
use sp_std::{convert::TryFrom, fmt::Debug, hash::Hash, str::FromStr};
use sp_std::{convert::TryFrom, fmt::Debug, hash::Hash, str::FromStr, vec, vec::Vec};

/// Chain call, that is either SCALE-encoded, or decoded.
#[derive(Debug, Clone)]
pub enum EncodedOrDecodedCall<ChainCall> {
/// The call that is SCALE-encoded.
///
/// This variant is used when we the chain runtime is not bundled with the relay, but
/// we still need the represent call in some RPC calls or transactions.
Encoded(Vec<u8>),
/// The decoded call.
Decoded(ChainCall),
}

impl<ChainCall: Clone + Decode> EncodedOrDecodedCall<ChainCall> {
/// Returns decoded call.
pub fn to_decoded(&self) -> Result<ChainCall, codec::Error> {
match self {
Self::Encoded(ref encoded_call) =>
ChainCall::decode(&mut &encoded_call[..]).map_err(Into::into),
Self::Decoded(ref decoded_call) => Ok(decoded_call.clone()),
}
}

/// Converts self to decoded call.
pub fn into_decoded(self) -> Result<ChainCall, codec::Error> {
match self {
Self::Encoded(encoded_call) =>
ChainCall::decode(&mut &encoded_call[..]).map_err(Into::into),
Self::Decoded(decoded_call) => Ok(decoded_call),
}
}
}

impl<ChainCall> From<ChainCall> for EncodedOrDecodedCall<ChainCall> {
fn from(call: ChainCall) -> EncodedOrDecodedCall<ChainCall> {
EncodedOrDecodedCall::Decoded(call)
}
}

impl<ChainCall: Decode> Decode for EncodedOrDecodedCall<ChainCall> {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
// having encoded version is better than decoded, because decoding isn't required
// everywhere and for mocked calls it may lead to **unneeded** errors
match input.remaining_len()? {
Some(remaining_len) => {
let mut encoded_call = vec![0u8; remaining_len];
input.read(&mut encoded_call)?;
Ok(EncodedOrDecodedCall::Encoded(encoded_call))
},
None => Ok(EncodedOrDecodedCall::Decoded(ChainCall::decode(input)?)),
}
}
}

impl<ChainCall: Encode> Encode for EncodedOrDecodedCall<ChainCall> {
fn encode(&self) -> Vec<u8> {
match *self {
Self::Encoded(ref encoded_call) => encoded_call.clone(),
Self::Decoded(ref decoded_call) => decoded_call.encode(),
}
}
}

/// Minimal Substrate-based chain representation that may be used from no_std environment.
pub trait Chain: Send + Sync + 'static {
Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use sp_io::hashing::blake2_256;
use sp_std::{convert::TryFrom, vec, vec::Vec};

pub use chain::{
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf,
IndexOf, SignatureOf, TransactionEraOf,
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, EncodedOrDecodedCall, HashOf,
HasherOf, HeaderOf, IndexOf, SignatureOf, TransactionEraOf,
};
pub use frame_support::storage::storage_prefix as storage_value_final_key;
pub use storage_proof::{Error as StorageProofError, StorageProofChecker};
Expand Down
64 changes: 52 additions & 12 deletions bridges/relays/bin-substrate/src/chains/kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use anyhow::anyhow;
use bp_message_dispatch::{CallOrigin, MessagePayload};
use bp_runtime::EncodedOrDecodedCall;
use codec::Decode;
use frame_support::weights::{DispatchClass, DispatchInfo, Pays, Weight};
use relay_kusama_client::Kusama;
use sp_version::RuntimeVersion;

use crate::cli::{
bridge,
encode_call::{Call, CliEncodeCall},
encode_message, CliChain,
encode_call::{self, Call, CliEncodeCall},
encode_message,
send_message::{self, DispatchFeePayment},
CliChain,
};

/// Weight of the `system::remark` call at Kusama.
Expand All @@ -32,13 +37,15 @@ use crate::cli::{
pub(crate) const SYSTEM_REMARK_CALL_WEIGHT: Weight = 2 * 1_345_000;

impl CliEncodeCall for Kusama {
fn encode_call(call: &Call) -> anyhow::Result<Self::Call> {
fn encode_call(call: &Call) -> anyhow::Result<EncodedOrDecodedCall<Self::Call>> {
Ok(match call {
Call::Raw { data } => EncodedOrDecodedCall::Encoded(data.0.clone()),
Call::Remark { remark_payload, .. } => relay_kusama_client::runtime::Call::System(
relay_kusama_client::runtime::SystemCall::remark(
remark_payload.as_ref().map(|x| x.0.clone()).unwrap_or_default(),
),
),
)
.into(),
Call::BridgeSendMessage { lane, payload, fee, bridge_instance_index } =>
match *bridge_instance_index {
bridge::KUSAMA_TO_POLKADOT_INDEX => {
Expand All @@ -48,6 +55,7 @@ impl CliEncodeCall for Kusama {
lane.0, payload, fee.0,
),
)
.into()
},
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
Expand All @@ -58,13 +66,11 @@ impl CliEncodeCall for Kusama {
})
}

fn get_dispatch_info(
call: &relay_kusama_client::runtime::Call,
) -> anyhow::Result<DispatchInfo> {
fn get_dispatch_info(call: &EncodedOrDecodedCall<Self::Call>) -> anyhow::Result<DispatchInfo> {
match *call {
relay_kusama_client::runtime::Call::System(
EncodedOrDecodedCall::Decoded(relay_kusama_client::runtime::Call::System(
relay_kusama_client::runtime::SystemCall::remark(_),
) => Ok(DispatchInfo {
)) => Ok(DispatchInfo {
weight: crate::chains::kusama::SYSTEM_REMARK_CALL_WEIGHT,
class: DispatchClass::Normal,
pays_fee: Pays::Yes,
Expand All @@ -78,7 +84,12 @@ impl CliChain for Kusama {
const RUNTIME_VERSION: RuntimeVersion = bp_kusama::VERSION;

type KeyPair = sp_core::sr25519::Pair;
type MessagePayload = ();
type MessagePayload = MessagePayload<
bp_kusama::AccountId,
bp_polkadot::AccountPublic,
bp_polkadot::Signature,
Vec<u8>,
>;

fn ss58_format() -> u16 {
sp_core::crypto::Ss58AddressFormat::from(
Expand All @@ -88,8 +99,37 @@ impl CliChain for Kusama {
}

fn encode_message(
_message: encode_message::MessagePayload,
message: encode_message::MessagePayload,
) -> anyhow::Result<Self::MessagePayload> {
anyhow::bail!("Sending messages from Kusama is not yet supported.")
match message {
encode_message::MessagePayload::Raw { data } => MessagePayload::decode(&mut &*data.0)
.map_err(|e| anyhow!("Failed to decode Kusama's MessagePayload: {:?}", e)),
encode_message::MessagePayload::Call { mut call, mut sender, dispatch_weight } => {
type Source = Kusama;
type Target = relay_polkadot_client::Polkadot;

sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(
&mut call,
bridge::KUSAMA_TO_POLKADOT_INDEX,
);
let call = Target::encode_call(&call)?;
let dispatch_weight = dispatch_weight.map(Ok).unwrap_or_else(|| {
Err(anyhow::format_err!(
"Please specify dispatch weight of the encoded Polkadot call"
))
})?;

Ok(send_message::message_payload(
spec_version,
dispatch_weight,
origin,
&call,
DispatchFeePayment::AtSourceChain,
))
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) async fn update_polkadot_to_kusama_conversion_rate(
let (spec_version, transaction_version) = client.simple_runtime_version().await?;
client
.submit_signed_extrinsic(signer_id, move |_, transaction_nonce| {
Bytes(
Ok(Bytes(
Kusama::sign_transaction(SignParam {
spec_version,
transaction_version,
Expand All @@ -96,12 +96,12 @@ pub(crate) async fn update_polkadot_to_kusama_conversion_rate(
sp_runtime::FixedU128::from_float(updated_rate),
)
)
),
).into(),
transaction_nonce,
),
})
})?
.encode(),
)
))
})
.await
.map(drop)
Expand Down
24 changes: 15 additions & 9 deletions bridges/relays/bin-substrate/src/chains/millau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@ use crate::cli::{
};
use anyhow::anyhow;
use bp_message_dispatch::{CallOrigin, MessagePayload};
use bp_runtime::EncodedOrDecodedCall;
use codec::Decode;
use frame_support::weights::{DispatchInfo, GetDispatchInfo};
use relay_millau_client::Millau;
use sp_version::RuntimeVersion;

impl CliEncodeCall for Millau {
fn encode_call(call: &Call) -> anyhow::Result<Self::Call> {
fn encode_call(call: &Call) -> anyhow::Result<EncodedOrDecodedCall<Self::Call>> {
Ok(match call {
Call::Raw { data } => Decode::decode(&mut &*data.0)?,
Call::Raw { data } => Self::Call::decode(&mut &*data.0)?.into(),
Call::Remark { remark_payload, .. } =>
millau_runtime::Call::System(millau_runtime::SystemCall::remark {
remark: remark_payload.as_ref().map(|x| x.0.clone()).unwrap_or_default(),
}),
})
.into(),
Call::Transfer { recipient, amount } =>
millau_runtime::Call::Balances(millau_runtime::BalancesCall::transfer {
dest: recipient.raw_id(),
value: amount.cast(),
}),
})
.into(),
Call::BridgeSendMessage { lane, payload, fee, bridge_instance_index } =>
match *bridge_instance_index {
bridge::MILLAU_TO_RIALTO_INDEX => {
Expand All @@ -54,6 +57,7 @@ impl CliEncodeCall for Millau {
delivery_and_dispatch_fee: fee.cast(),
},
)
.into()
},
_ => anyhow::bail!(
"Unsupported target bridge pallet with instance index: {}",
Expand All @@ -63,8 +67,8 @@ impl CliEncodeCall for Millau {
})
}

fn get_dispatch_info(call: &millau_runtime::Call) -> anyhow::Result<DispatchInfo> {
Ok(call.get_dispatch_info())
fn get_dispatch_info(call: &EncodedOrDecodedCall<Self::Call>) -> anyhow::Result<DispatchInfo> {
Ok(call.to_decoded()?.get_dispatch_info())
}
}

Expand All @@ -90,7 +94,7 @@ impl CliChain for Millau {
match message {
encode_message::MessagePayload::Raw { data } => MessagePayload::decode(&mut &*data.0)
.map_err(|e| anyhow!("Failed to decode Millau's MessagePayload: {:?}", e)),
encode_message::MessagePayload::Call { mut call, mut sender } => {
encode_message::MessagePayload::Call { mut call, mut sender, dispatch_weight } => {
type Source = Millau;
type Target = relay_rialto_client::Rialto;

Expand All @@ -102,11 +106,13 @@ impl CliChain for Millau {
bridge::MILLAU_TO_RIALTO_INDEX,
);
let call = Target::encode_call(&call)?;
let weight = call.get_dispatch_info().weight;
let dispatch_weight = dispatch_weight.map(Ok).unwrap_or_else(|| {
call.to_decoded().map(|call| call.get_dispatch_info().weight)
})?;

Ok(send_message::message_payload(
spec_version,
weight,
dispatch_weight,
origin,
&call,
DispatchFeePayment::AtSourceChain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,24 @@ pub(crate) async fn update_rialto_to_millau_conversion_rate(
let (spec_version, transaction_version) = client.simple_runtime_version().await?;
client
.submit_signed_extrinsic(signer_id, move |_, transaction_nonce| {
Bytes(
Ok(Bytes(
Millau::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash,
signer,
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(
millau_runtime::MessagesCall::update_pallet_parameter {
millau_runtime::Call::from(millau_runtime::MessagesCall::update_pallet_parameter {
parameter: millau_runtime::rialto_messages::MillauToRialtoMessagesParameter::RialtoToMillauConversionRate(
sp_runtime::FixedU128::from_float(updated_rate),
),
}
.into(),
}).into(),
transaction_nonce,
),
})
})?
.encode(),
)
))
})
.await
.map(drop)
Expand Down
Loading

0 comments on commit e89155d

Please sign in to comment.