mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 04:01:02 +00:00
Encode and estimate Rococo/Wococo/Kusama/Polkadot messages (#1322)
* encode and estimate Rococo/Wococo/Kusama/Polkadot messages * allow send-message for non-bundled chains * weight -> dispatch-weight * fmt * fix spelling
This commit is contained in:
committed by
Bastian Köcher
parent
097a28418b
commit
efa3e97210
@@ -20,6 +20,7 @@ use crate::{
|
||||
},
|
||||
select_full_bridge,
|
||||
};
|
||||
use bp_runtime::EncodedOrDecodedCall;
|
||||
use frame_support::weights::DispatchInfo;
|
||||
use relay_substrate_client::Chain;
|
||||
use structopt::StructOpt;
|
||||
@@ -85,10 +86,10 @@ pub enum Call {
|
||||
|
||||
pub trait CliEncodeCall: Chain {
|
||||
/// Encode a CLI call.
|
||||
fn encode_call(call: &Call) -> anyhow::Result<Self::Call>;
|
||||
fn encode_call(call: &Call) -> anyhow::Result<EncodedOrDecodedCall<Self::Call>>;
|
||||
|
||||
/// Get dispatch info for the call.
|
||||
fn get_dispatch_info(call: &Self::Call) -> anyhow::Result<DispatchInfo>;
|
||||
fn get_dispatch_info(call: &EncodedOrDecodedCall<Self::Call>) -> anyhow::Result<DispatchInfo>;
|
||||
}
|
||||
|
||||
impl EncodeCall {
|
||||
@@ -100,7 +101,10 @@ impl EncodeCall {
|
||||
let encoded = HexBytes::encode(&call);
|
||||
|
||||
log::info!(target: "bridge", "Generated {} call: {:#?}", Source::NAME, call);
|
||||
log::info!(target: "bridge", "Weight of {} call: {}", Source::NAME, Source::get_dispatch_info(&call)?.weight);
|
||||
log::info!(target: "bridge", "Weight of {} call: {}", Source::NAME, Source::get_dispatch_info(&call)
|
||||
.map(|dispatch_info| format!("{}", dispatch_info.weight))
|
||||
.unwrap_or_else(|_| "<unknown>".to_string())
|
||||
);
|
||||
log::info!(target: "bridge", "Encoded {} call: {:?}", Source::NAME, encoded);
|
||||
|
||||
Ok(encoded)
|
||||
|
||||
@@ -18,6 +18,7 @@ use crate::{
|
||||
cli::{bridge::FullBridge, AccountId, CliChain, HexBytes},
|
||||
select_full_bridge,
|
||||
};
|
||||
use frame_support::weights::Weight;
|
||||
use structopt::StructOpt;
|
||||
use strum::VariantNames;
|
||||
|
||||
@@ -37,6 +38,12 @@ pub enum MessagePayload {
|
||||
/// SS58 encoded Source account that will send the payload.
|
||||
#[structopt(long)]
|
||||
sender: AccountId,
|
||||
/// Weight of the call.
|
||||
///
|
||||
/// It must be specified if the chain runtime is not bundled with the relay, or if
|
||||
/// you want to override bundled weight.
|
||||
#[structopt(long)]
|
||||
dispatch_weight: Option<Weight>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -97,6 +104,8 @@ mod tests {
|
||||
"call",
|
||||
"--sender",
|
||||
&sender,
|
||||
"--dispatch-weight",
|
||||
"42",
|
||||
"remark",
|
||||
"--remark-size",
|
||||
"12",
|
||||
@@ -106,6 +115,6 @@ mod tests {
|
||||
let hex = encode_message.encode().unwrap();
|
||||
|
||||
// then
|
||||
assert_eq!(format!("{:?}", hex), "0x01000000000000000000000002d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d003c000130000000000000000000000000");
|
||||
assert_eq!(format!("{:?}", hex), "0x010000002a0000000000000002d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d003c000130000000000000000000000000");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +103,8 @@ mod tests {
|
||||
"call",
|
||||
"--sender",
|
||||
&alice,
|
||||
"--dispatch-weight",
|
||||
"42",
|
||||
"remark",
|
||||
"--remark-payload",
|
||||
"1234",
|
||||
@@ -129,7 +131,8 @@ mod tests {
|
||||
call: encode_call::Call::Remark {
|
||||
remark_payload: Some(HexBytes(vec![0x12, 0x34])),
|
||||
remark_size: None,
|
||||
}
|
||||
},
|
||||
dispatch_weight: Some(42),
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -222,7 +222,7 @@ impl InitBridge {
|
||||
target_client.clone(),
|
||||
target_sign.public().into(),
|
||||
move |transaction_nonce, initialization_data| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Target::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -230,12 +230,12 @@ impl InitBridge {
|
||||
signer: target_sign,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
encode_init_bridge(initialization_data),
|
||||
encode_init_bridge(initialization_data).into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -132,7 +132,7 @@ impl RegisterParachain {
|
||||
.submit_and_watch_signed_extrinsic(
|
||||
relay_sudo_account.clone(),
|
||||
move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Relaychain::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -140,12 +140,12 @@ impl RegisterParachain {
|
||||
signer: reserve_parachain_signer,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
reserve_parachain_id_call,
|
||||
reserve_parachain_id_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await?,
|
||||
@@ -181,7 +181,7 @@ impl RegisterParachain {
|
||||
.submit_and_watch_signed_extrinsic(
|
||||
relay_sudo_account.clone(),
|
||||
move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Relaychain::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -189,12 +189,12 @@ impl RegisterParachain {
|
||||
signer: register_parathread_signer,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
register_parathread_call,
|
||||
register_parathread_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await?,
|
||||
@@ -243,17 +243,20 @@ impl RegisterParachain {
|
||||
let force_lease_signer = relay_sign.clone();
|
||||
relay_client
|
||||
.submit_signed_extrinsic(relay_sudo_account.clone(), move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Relaychain::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
genesis_hash: relay_genesis_hash,
|
||||
signer: force_lease_signer,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(force_lease_call, transaction_nonce),
|
||||
})
|
||||
unsigned: UnsignedTransaction::new(
|
||||
force_lease_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
})
|
||||
.await?;
|
||||
log::info!(target: "bridge", "Registered parachain leases: {:?}. Waiting for onboarding", para_id);
|
||||
|
||||
@@ -615,17 +615,17 @@ where
|
||||
let (spec_version, transaction_version) = client.simple_runtime_version().await?;
|
||||
client
|
||||
.submit_signed_extrinsic(sign.public().into(), move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
C::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
genesis_hash,
|
||||
signer: sign,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(call, transaction_nonce),
|
||||
})
|
||||
unsigned: UnsignedTransaction::new(call.into(), transaction_nonce),
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
})
|
||||
.await
|
||||
.map(drop)
|
||||
|
||||
@@ -445,7 +445,7 @@ async fn update_transaction_tip<C: Chain, S: TransactionSignScheme<Chain = C>>(
|
||||
signer: key_pair.clone(),
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: unsigned_tx.clone(),
|
||||
}),
|
||||
})?,
|
||||
)
|
||||
.await??
|
||||
.priority;
|
||||
@@ -468,7 +468,7 @@ async fn update_transaction_tip<C: Chain, S: TransactionSignScheme<Chain = C>>(
|
||||
signer: key_pair.clone(),
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: unsigned_tx,
|
||||
}),
|
||||
})?,
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -124,9 +124,13 @@ impl SendMessage {
|
||||
let payload = {
|
||||
let target_call_weight = prepare_call_dispatch_weight(
|
||||
dispatch_weight,
|
||||
ExplicitOrMaximal::Explicit(Target::get_dispatch_info(&target_call)?.weight),
|
||||
|| {
|
||||
Ok(ExplicitOrMaximal::Explicit(
|
||||
Target::get_dispatch_info(&target_call)?.weight,
|
||||
))
|
||||
},
|
||||
compute_maximal_message_dispatch_weight(Target::max_extrinsic_weight()),
|
||||
);
|
||||
)?;
|
||||
let source_sender_public: MultiSigner = source_sign.public().into();
|
||||
let source_account_id = source_sender_public.into_account();
|
||||
|
||||
@@ -200,7 +204,7 @@ impl SendMessage {
|
||||
signer: source_sign.clone(),
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(send_message_call.clone(), 0),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
))
|
||||
.await?;
|
||||
@@ -213,7 +217,7 @@ impl SendMessage {
|
||||
signer: source_sign.clone(),
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(send_message_call, transaction_nonce),
|
||||
})
|
||||
})?
|
||||
.encode();
|
||||
|
||||
log::info!(
|
||||
@@ -241,7 +245,7 @@ impl SendMessage {
|
||||
HexBytes::encode(&signed_source_call)
|
||||
);
|
||||
|
||||
Bytes(signed_source_call)
|
||||
Ok(Bytes(signed_source_call))
|
||||
})
|
||||
.await?;
|
||||
});
|
||||
@@ -252,12 +256,16 @@ impl SendMessage {
|
||||
|
||||
fn prepare_call_dispatch_weight(
|
||||
user_specified_dispatch_weight: &Option<ExplicitOrMaximal<Weight>>,
|
||||
weight_from_pre_dispatch_call: ExplicitOrMaximal<Weight>,
|
||||
weight_from_pre_dispatch_call: impl Fn() -> anyhow::Result<ExplicitOrMaximal<Weight>>,
|
||||
maximal_allowed_weight: Weight,
|
||||
) -> Weight {
|
||||
match user_specified_dispatch_weight.clone().unwrap_or(weight_from_pre_dispatch_call) {
|
||||
ExplicitOrMaximal::Explicit(weight) => weight,
|
||||
ExplicitOrMaximal::Maximal => maximal_allowed_weight,
|
||||
) -> anyhow::Result<Weight> {
|
||||
match user_specified_dispatch_weight
|
||||
.clone()
|
||||
.map(Ok)
|
||||
.unwrap_or_else(weight_from_pre_dispatch_call)?
|
||||
{
|
||||
ExplicitOrMaximal::Explicit(weight) => Ok(weight),
|
||||
ExplicitOrMaximal::Maximal => Ok(maximal_allowed_weight),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +245,7 @@ impl SwapTokens {
|
||||
.submit_and_watch_signed_extrinsic(
|
||||
accounts.source_account_at_this_chain.clone(),
|
||||
move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Source::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -253,12 +253,12 @@ impl SwapTokens {
|
||||
signer: create_swap_signer,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
create_swap_call,
|
||||
create_swap_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await?,
|
||||
@@ -386,7 +386,7 @@ impl SwapTokens {
|
||||
.submit_and_watch_signed_extrinsic(
|
||||
accounts.target_account_at_bridged_chain.clone(),
|
||||
move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Target::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -394,12 +394,12 @@ impl SwapTokens {
|
||||
signer: target_sign,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
send_message_call,
|
||||
send_message_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await?,
|
||||
@@ -430,7 +430,7 @@ impl SwapTokens {
|
||||
.submit_and_watch_signed_extrinsic(
|
||||
accounts.source_account_at_this_chain.clone(),
|
||||
move |_, transaction_nonce| {
|
||||
Bytes(
|
||||
Ok(Bytes(
|
||||
Source::sign_transaction(SignParam {
|
||||
spec_version,
|
||||
transaction_version,
|
||||
@@ -438,12 +438,12 @@ impl SwapTokens {
|
||||
signer: source_sign,
|
||||
era: relay_substrate_client::TransactionEra::immortal(),
|
||||
unsigned: UnsignedTransaction::new(
|
||||
cancel_swap_call,
|
||||
cancel_swap_call.into(),
|
||||
transaction_nonce,
|
||||
),
|
||||
})
|
||||
})?
|
||||
.encode(),
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
.await?,
|
||||
|
||||
Reference in New Issue
Block a user