Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check next ParaID and reserve it if needed #590

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cli/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ pub fn create_batch_all_call(calls: Vec<Call>) -> Result<Call, Box<dyn std::erro
Ok(batch_call)
}

//
// Reserve the next para_id available in Rococo
//
pub async fn reserve(api: OnlineClient<PolkadotConfig>) -> 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
//
Expand Down
32 changes: 25 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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,
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;
Expand Down Expand Up @@ -59,11 +59,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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
Expand Down
19 changes: 18 additions & 1 deletion cli/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ 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<PolkadotConfig>) -> 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<PolkadotConfig>,
Expand Down
9 changes: 8 additions & 1 deletion cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, next_free_para, paras_registered};

// Rococo types
#[subxt::subxt(runtime_metadata_path = "metadata/local_metadata.scale")]
Expand Down Expand Up @@ -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<PolkadotConfig>,
) -> Result<u32, Box<dyn std::error::Error>> {
Ok(next_free_para(rococo_api).await)
}

// Check if the parachain is registerd in Rococo
pub async fn is_registered(
rococo_api: OnlineClient<PolkadotConfig>,
Expand Down