From b16ca2846b0d7dc206b89ce483b92f32e55c760c Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Thu, 31 Aug 2023 16:30:02 +0200 Subject: [PATCH 1/2] check if para_id is reserved and ok and reserve it if needed --- cli/src/calls.rs | 16 ++++++++++++++++ cli/src/main.rs | 30 ++++++++++++++++++++++++------ cli/src/query.rs | 13 ++++++++++++- cli/src/utils.rs | 9 ++++++++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/cli/src/calls.rs b/cli/src/calls.rs index 9bac22c..f242eb3 100644 --- a/cli/src/calls.rs +++ b/cli/src/calls.rs @@ -32,6 +32,22 @@ pub fn create_batch_all_call(calls: Vec) -> Result) -> Result<(), subxt::Error> { + let root = get_signer(); + + let tx = rococo::tx().registrar().reserve(); + + api.tx() + .sign_and_submit_then_watch_default(&tx, &root) + .await? + .wait_for_finalized_success() + .await?; + Ok(()) +} + // // Fund the parachain manager from the faucet address using sudo // diff --git a/cli/src/main.rs b/cli/src/main.rs index 29e8d3e..6207d41 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -4,12 +4,12 @@ use para_onboarding::{ calls::{ create_batch_all_call, create_force_register_call, create_force_transfer_call, create_scheduled_assign_slots_call, create_scheduled_remove_lock_call, create_sudo_call, - sign_and_send_proxy_call, Call, + sign_and_send_proxy_call, reserve, Call, }, chain_connector::{kusama_connection, polkadot_connection, rococo_connection}, utils::{ calculate_sovereign_account, get_file_content, has_slot_in_rococo, is_registered, - needs_perm_slot, parse_validation_code, + needs_perm_slot, parse_validation_code, get_next_free_para }, }; use sp_core::sr25519::Pair; @@ -59,11 +59,29 @@ async fn main() -> Result<(), Box> { let is_perm_slot: bool = needs_perm_slot(polkadot_api, kusama_api, para_id.clone()) .await .unwrap_or(false); - // prints just for testing, remove before publishing - if is_perm_slot { - println!("ParaId: {} needs a permanent slot", para_id.clone()); - } else { + + // If needs a permanent slot, check if the paraId has been reserved and if not, reserve it in Rococo + // + // If paraId < nextParaId needs means is reserved, + // If paraId = nextParaId needs to be reserved + // If paraId > nextParaId throws an Error, because teh para_id indicated should be the nextParaId + if !is_perm_slot { println!("ParaId: {} needs a temporary slot", para_id.clone()); + let next_para_id = get_next_free_para(rococo_api.clone()).await?; + if next_para_id == para_id.clone() { + if let Err(subxt::Error::Runtime(dispatch_err)) = + reserve(rococo_api.clone()).await + { + eprintln!("Could not dispatch the call to reserve the para_id: {}", dispatch_err); + } + } + else if next_para_id < para_id { + println!( + "Error: ParaId: {} is not reserved and is not the next free para id", + args.para_id.clone() + ); + return Ok(()); + } } // Initialise an empty call buffer diff --git a/cli/src/query.rs b/cli/src/query.rs index 98e5c22..d31d227 100644 --- a/cli/src/query.rs +++ b/cli/src/query.rs @@ -37,7 +37,18 @@ pub async fn maybe_leases( } // -// Checks if paraId is already registered +// Check the next free para available in in Rococo +// +pub async fn next_free_para( + api: OnlineClient, +) -> u32 { + let query = rococo::storage().registrar().next_free_para_id(); + let id = api.storage().at_latest().await.expect("Error getting the next free para id").fetch(&query).await.unwrap().expect("Error getting the next free para id"); + id.0 +} + +// +// Checks if paraId is already registered in Rococo // pub async fn paras_registered( api: OnlineClient, diff --git a/cli/src/utils.rs b/cli/src/utils.rs index 5d2b574..97c0db3 100644 --- a/cli/src/utils.rs +++ b/cli/src/utils.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use subxt::{utils::AccountId32, OnlineClient, PolkadotConfig}; use subxt_signer::{bip39::Mnemonic, sr25519::Keypair}; -use crate::query::{maybe_leases, paras_registered}; +use crate::query::{maybe_leases, paras_registered, next_free_para}; // Rococo types #[subxt::subxt(runtime_metadata_path = "metadata/local_metadata.scale")] @@ -104,6 +104,13 @@ pub async fn has_slot_in_rococo( } } +// Check if the next free para available in in Rococo is grater than ours +pub async fn get_next_free_para( + rococo_api: OnlineClient, +) -> Result> { + Ok(next_free_para(rococo_api).await) +} + // Check if the parachain is registerd in Rococo pub async fn is_registered( rococo_api: OnlineClient, From b4dad2e07dcd307073bdaac56411fc90856015c8 Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Thu, 31 Aug 2023 16:30:40 +0200 Subject: [PATCH 2/2] cargo fmt --- cli/src/main.rs | 22 +++++++++++----------- cli/src/query.rs | 14 ++++++++++---- cli/src/utils.rs | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 6207d41..25ef4ff 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -4,12 +4,12 @@ use para_onboarding::{ calls::{ create_batch_all_call, create_force_register_call, create_force_transfer_call, create_scheduled_assign_slots_call, create_scheduled_remove_lock_call, create_sudo_call, - sign_and_send_proxy_call, reserve, Call, + reserve, sign_and_send_proxy_call, Call, }, chain_connector::{kusama_connection, polkadot_connection, rococo_connection}, utils::{ - calculate_sovereign_account, get_file_content, has_slot_in_rococo, is_registered, - needs_perm_slot, parse_validation_code, get_next_free_para + calculate_sovereign_account, get_file_content, get_next_free_para, has_slot_in_rococo, + is_registered, needs_perm_slot, parse_validation_code, }, }; use sp_core::sr25519::Pair; @@ -60,8 +60,8 @@ async fn main() -> Result<(), Box> { .await .unwrap_or(false); - // If needs a permanent slot, check if the paraId has been reserved and if not, reserve it in Rococo - // + // If needs a permanent slot, check if the paraId has been reserved and if not, reserve it in Rococo + // // If paraId < nextParaId needs means is reserved, // If paraId = nextParaId needs to be reserved // If paraId > nextParaId throws an Error, because teh para_id indicated should be the nextParaId @@ -69,13 +69,13 @@ async fn main() -> Result<(), Box> { println!("ParaId: {} needs a temporary slot", para_id.clone()); let next_para_id = get_next_free_para(rococo_api.clone()).await?; if next_para_id == para_id.clone() { - if let Err(subxt::Error::Runtime(dispatch_err)) = - reserve(rococo_api.clone()).await - { - eprintln!("Could not dispatch the call to reserve the para_id: {}", dispatch_err); + if let Err(subxt::Error::Runtime(dispatch_err)) = reserve(rococo_api.clone()).await { + eprintln!( + "Could not dispatch the call to reserve the para_id: {}", + dispatch_err + ); } - } - else if next_para_id < para_id { + } else if next_para_id < para_id { println!( "Error: ParaId: {} is not reserved and is not the next free para id", args.para_id.clone() diff --git a/cli/src/query.rs b/cli/src/query.rs index d31d227..fca1d3c 100644 --- a/cli/src/query.rs +++ b/cli/src/query.rs @@ -39,11 +39,17 @@ pub async fn maybe_leases( // // Check the next free para available in in Rococo // -pub async fn next_free_para( - api: OnlineClient, -) -> u32 { +pub async fn next_free_para(api: OnlineClient) -> u32 { let query = rococo::storage().registrar().next_free_para_id(); - let id = api.storage().at_latest().await.expect("Error getting the next free para id").fetch(&query).await.unwrap().expect("Error getting the next free para id"); + let id = api + .storage() + .at_latest() + .await + .expect("Error getting the next free para id") + .fetch(&query) + .await + .unwrap() + .expect("Error getting the next free para id"); id.0 } diff --git a/cli/src/utils.rs b/cli/src/utils.rs index 97c0db3..04967a5 100644 --- a/cli/src/utils.rs +++ b/cli/src/utils.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use subxt::{utils::AccountId32, OnlineClient, PolkadotConfig}; use subxt_signer::{bip39::Mnemonic, sr25519::Keypair}; -use crate::query::{maybe_leases, paras_registered, next_free_para}; +use crate::query::{maybe_leases, next_free_para, paras_registered}; // Rococo types #[subxt::subxt(runtime_metadata_path = "metadata/local_metadata.scale")]