Skip to content

Commit

Permalink
Extract unsigned tx from the SignParam structure (paritytech#1561)
Browse files Browse the repository at this point in the history
* change sign_transaction method

* clippy

* rustup update && clippy

* remove redudnant clone
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent 33ab726 commit cadf379
Show file tree
Hide file tree
Showing 29 changed files with 434 additions and 402 deletions.
10 changes: 2 additions & 8 deletions bridges/bin/rialto-parachain/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ pub fn development_config(id: ParaId) -> ChainSpec {
move || {
testnet_genesis(
get_account_id_from_seed::<sr25519::Public>(SUDO_ACCOUNT),
DEV_AUTHORITIES_ACCOUNTS
.into_iter()
.map(|x| get_from_seed::<AuraId>(x))
.collect(),
DEV_AUTHORITIES_ACCOUNTS.into_iter().map(get_from_seed::<AuraId>).collect(),
endowed_accounts(),
id,
)
Expand Down Expand Up @@ -157,10 +154,7 @@ pub fn local_testnet_config(id: ParaId) -> ChainSpec {
move || {
testnet_genesis(
get_account_id_from_seed::<sr25519::Public>(SUDO_ACCOUNT),
LOCAL_AUTHORITIES_ACCOUNTS
.into_iter()
.map(|x| get_from_seed::<AuraId>(x))
.collect(),
LOCAL_AUTHORITIES_ACCOUNTS.into_iter().map(get_from_seed::<AuraId>).collect(),
endowed_accounts(),
id,
)
Expand Down
10 changes: 9 additions & 1 deletion bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Size for PreComputedSize {
}

/// Era of specific transaction.
#[derive(RuntimeDebug, Clone, Copy)]
#[derive(RuntimeDebug, Clone, Copy, PartialEq)]
pub enum TransactionEra<BlockNumber, BlockHash> {
/// Transaction is immortal.
Immortal,
Expand All @@ -207,6 +207,14 @@ impl<BlockNumber: Copy + Into<u64>, BlockHash: Copy> TransactionEra<BlockNumber,
TransactionEra::Immortal
}

/// Returns mortality period if transaction is mortal.
pub fn mortality_period(&self) -> Option<u32> {
match *self {
TransactionEra::Immortal => None,
TransactionEra::Mortal(_, period) => Some(period),
}
}

/// Returns era that is used by FRAME-based runtimes.
pub fn frame_era(&self) -> sp_runtime::generic::Era {
match *self {
Expand Down
2 changes: 1 addition & 1 deletion bridges/primitives/test-utils/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Account {
pub fn secret(&self) -> SecretKey {
let data = self.0.encode();
let mut bytes = [0_u8; 32];
bytes[0..data.len()].copy_from_slice(&*data);
bytes[0..data.len()].copy_from_slice(&data);
SecretKey::from_bytes(&bytes)
.expect("A static array of the correct length is a known good.")
}
Expand Down
34 changes: 18 additions & 16 deletions bridges/relays/bin-substrate/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ mod tests {
fn rialto_tx_extra_bytes_constant_is_correct() {
let rialto_call =
rialto_runtime::Call::System(rialto_runtime::SystemCall::remark { remark: vec![] });
let rialto_tx = Rialto::sign_transaction(SignParam {
spec_version: 1,
transaction_version: 1,
genesis_hash: Default::default(),
signer: sp_keyring::AccountKeyring::Alice.pair(),
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(rialto_call.clone().into(), 0),
})
let rialto_tx = Rialto::sign_transaction(
SignParam {
spec_version: 1,
transaction_version: 1,
genesis_hash: Default::default(),
signer: sp_keyring::AccountKeyring::Alice.pair(),
},
UnsignedTransaction::new(rialto_call.clone().into(), 0),
)
.unwrap();
let extra_bytes_in_transaction = rialto_tx.encode().len() - rialto_call.encode().len();
assert!(
Expand All @@ -93,14 +94,15 @@ mod tests {
fn millau_tx_extra_bytes_constant_is_correct() {
let millau_call =
millau_runtime::Call::System(millau_runtime::SystemCall::remark { remark: vec![] });
let millau_tx = Millau::sign_transaction(SignParam {
spec_version: 0,
transaction_version: 0,
genesis_hash: Default::default(),
signer: sp_keyring::AccountKeyring::Alice.pair(),
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(millau_call.clone().into(), 0),
})
let millau_tx = Millau::sign_transaction(
SignParam {
spec_version: 0,
transaction_version: 0,
genesis_hash: Default::default(),
signer: sp_keyring::AccountKeyring::Alice.pair(),
},
UnsignedTransaction::new(millau_call.clone().into(), 0),
)
.unwrap();
let extra_bytes_in_transaction = millau_tx.encode().len() - millau_call.encode().len();
assert!(
Expand Down
29 changes: 11 additions & 18 deletions bridges/relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ use crate::{
cli::{bridge::CliBridgeBase, chain_schema::*},
};
use bp_runtime::Chain as ChainBase;
use codec::Encode;
use relay_substrate_client::{
AccountKeyPairOf, Chain, SignParam, TransactionSignScheme, UnsignedTransaction,
};
use sp_core::{Bytes, Pair};
use relay_substrate_client::{AccountKeyPairOf, Chain, SignParam, UnsignedTransaction};
use sp_core::Pair;
use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};
use substrate_relay_helper::finality::engine::{Engine, Grandpa as GrandpaFinalityEngine};
Expand Down Expand Up @@ -82,20 +79,16 @@ where
source_client,
target_client.clone(),
target_sign.public().into(),
SignParam {
spec_version,
transaction_version,
genesis_hash: *target_client.genesis_hash(),
signer: target_sign,
},
move |transaction_nonce, initialization_data| {
Ok(Bytes(
Self::Target::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash: *target_client.genesis_hash(),
signer: target_sign,
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(
Self::encode_init_bridge(initialization_data).into(),
transaction_nonce,
),
})?
.encode(),
Ok(UnsignedTransaction::new(
Self::encode_init_bridge(initialization_data).into(),
transaction_nonce,
))
},
)
Expand Down
7 changes: 0 additions & 7 deletions bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,6 @@ impl std::fmt::Display for HexBytes {
}
}

impl HexBytes {
/// Encode given object and wrap into nicely formatted bytes.
pub fn encode<T: Encode>(t: &T) -> Self {
Self(t.encode())
}
}

/// Prometheus metrics params.
#[derive(Clone, Debug, PartialEq, StructOpt)]
pub struct PrometheusParams {
Expand Down
78 changes: 33 additions & 45 deletions bridges/relays/bin-substrate/src/cli/register_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ use polkadot_runtime_common::{
};
use polkadot_runtime_parachains::paras::ParaLifecycle;
use relay_substrate_client::{
AccountIdOf, CallOf, Chain, Client, HashOf, SignParam, Subscription, TransactionSignScheme,
TransactionStatusOf, UnsignedTransaction,
AccountIdOf, CallOf, Chain, Client, HashOf, SignParam, Subscription, TransactionStatusOf,
UnsignedTransaction,
};
use rialto_runtime::SudoCall;
use sp_core::{
storage::{well_known_keys::CODE, StorageKey},
Bytes, Pair,
Pair,
};
use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};
Expand Down Expand Up @@ -120,20 +120,16 @@ impl RegisterParachain {
relay_client
.submit_and_watch_signed_extrinsic(
relay_sudo_account.clone(),
SignParam::<Relaychain> {
spec_version,
transaction_version,
genesis_hash: relay_genesis_hash,
signer: reserve_parachain_signer,
},
move |_, transaction_nonce| {
Ok(Bytes(
Relaychain::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash: relay_genesis_hash,
signer: reserve_parachain_signer,
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(
reserve_parachain_id_call.into(),
transaction_nonce,
),
})?
.encode(),
Ok(UnsignedTransaction::new(
reserve_parachain_id_call.into(),
transaction_nonce,
))
},
)
Expand Down Expand Up @@ -169,20 +165,16 @@ impl RegisterParachain {
relay_client
.submit_and_watch_signed_extrinsic(
relay_sudo_account.clone(),
SignParam::<Relaychain> {
spec_version,
transaction_version,
genesis_hash: relay_genesis_hash,
signer: register_parathread_signer,
},
move |_, transaction_nonce| {
Ok(Bytes(
Relaychain::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash: relay_genesis_hash,
signer: register_parathread_signer,
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: UnsignedTransaction::new(
register_parathread_call.into(),
transaction_nonce,
),
})?
.encode(),
Ok(UnsignedTransaction::new(
register_parathread_call.into(),
transaction_nonce,
))
},
)
Expand Down Expand Up @@ -231,22 +223,18 @@ impl RegisterParachain {
.into();
let force_lease_signer = relay_sign.clone();
relay_client
.submit_signed_extrinsic(relay_sudo_account.clone(), move |_, transaction_nonce| {
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.into(),
transaction_nonce,
),
})?
.encode(),
))
})
.submit_signed_extrinsic(
relay_sudo_account,
SignParam::<Relaychain> {
spec_version,
transaction_version,
genesis_hash: relay_genesis_hash,
signer: force_lease_signer,
},
move |_, transaction_nonce| {
Ok(UnsignedTransaction::new(force_lease_call.into(), transaction_nonce))
},
)
.await?;
log::info!(target: "bridge", "Registered parachain leases: {:?}. Waiting for onboarding", para_id);

Expand Down
36 changes: 19 additions & 17 deletions bridges/relays/bin-substrate/src/cli/resubmit_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,15 @@ async fn update_transaction_tip<C: Chain, S: TransactionSignScheme<Chain = C>>(
current_priority = client
.validate_transaction(
at_block.1,
S::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash: *client.genesis_hash(),
signer: transaction_params.signer.clone(),
era: relay_substrate_client::TransactionEra::immortal(),
unsigned: unsigned_tx.clone(),
})?,
S::sign_transaction(
SignParam {
spec_version,
transaction_version,
genesis_hash: *client.genesis_hash(),
signer: transaction_params.signer.clone(),
},
unsigned_tx.clone(),
)?,
)
.await??
.priority;
Expand All @@ -448,17 +449,18 @@ async fn update_transaction_tip<C: Chain, S: TransactionSignScheme<Chain = C>>(

Ok((
old_tip != unsigned_tx.tip,
S::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash: *client.genesis_hash(),
signer: transaction_params.signer.clone(),
era: relay_substrate_client::TransactionEra::new(
S::sign_transaction(
SignParam {
spec_version,
transaction_version,
genesis_hash: *client.genesis_hash(),
signer: transaction_params.signer.clone(),
},
unsigned_tx.era(relay_substrate_client::TransactionEra::new(
at_block,
transaction_params.mortality,
),
unsigned: unsigned_tx,
})?,
)),
)?,
))
}

Expand Down
Loading

0 comments on commit cadf379

Please sign in to comment.