Skip to content

Commit

Permalink
Address commments
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod committed Oct 4, 2023
1 parent 97d73d7 commit f1038f2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 57 deletions.
2 changes: 1 addition & 1 deletion crates/orchestrator/src/generators.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod chain_spec;
pub mod errors;
pub mod key;
pub mod para_artifact;

mod bootnode_addr;
mod command;
mod identity;
mod key;
mod keystore;
mod port;

Expand Down
69 changes: 42 additions & 27 deletions crates/orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,13 @@ where
.await?;
}

let para_to_register_in_genesis: Vec<&ParachainSpec> = network_spec
.parachains
.iter()
.filter(|para| match &para.registration_strategy {
RegistrationStrategy::InGenesis => true,
RegistrationStrategy::UsingExtrinsic => false,
})
.collect();
// Gather the parachains to register in genesis and the ones to register with extrinsic
let (para_to_register_in_genesis, para_to_register_with_extrinsic): (
Vec<&ParachainSpec>,
Vec<&ParachainSpec>,
) = network_spec.parachains.iter().partition(|para| {
matches!(para.registration_strategy, RegistrationStrategy::InGenesis)
});

let mut para_artifacts = vec![];
for para in para_to_register_in_genesis {
Expand Down Expand Up @@ -255,11 +254,17 @@ where

ctx.bootnodes_addr = &bootnodes_addr;

// Initiate the node_ws_uel which will be later used in the Parachain_with_extrinsic config
let mut node_ws_url: String = "".to_string();

// spawn the rest of the nodes (TODO: in batches)
let spawning_tasks = relaynodes
.iter()
.map(|node| spawner::spawn_node(node, global_files_to_inject.clone(), &ctx));
for node in futures::future::try_join_all(spawning_tasks).await? {
// Is used in the register_para_options (We need to get this from the relay and not the collators)
node_ws_url = node.ws_uri.clone();

// Add the node to the `Network` instance
network.add_running_node(node, None);
}
Expand Down Expand Up @@ -323,29 +328,39 @@ where
.map(|node| spawner::spawn_node(node, para_files_to_inject.clone(), &ctx_para));
// TODO: Add para to Network instance
for node in futures::future::try_join_all(spawning_tasks).await? {
// Is used in the register_para_options
let node_ws_url = node.ws_uri.clone();

network.add_running_node(node, Some(para.id));

let register_para_options: RegisterParachainOptions = RegisterParachainOptions {
para_id: para.id,
// This needs to resolve correctly
wasm_path: para.genesis_wasm.artifact_path().unwrap().to_path_buf(),
state_path: para.genesis_state.artifact_path().unwrap().to_path_buf(),
node_ws_url,
onboard_as_para: para.onboard_as_parachain,
seed: None,
finalization: false,
};

println!("{:#?}", register_para_options);

// registerParachain
let _ = Parachain::register(register_para_options).await;
}
}

// Now we need to register the paras with extrinsic from the Vec collected before;
for para in para_to_register_with_extrinsic {
let register_para_options: RegisterParachainOptions = RegisterParachainOptions {
id: para.id,
// This needs to resolve correctly
wasm_path: para
.genesis_wasm
.artifact_path()
.ok_or(OrchestratorError::InvariantError(
"artifact path for wasm must be set at this point",
))?
.to_path_buf(),
state_path: para
.genesis_state
.artifact_path()
.ok_or(OrchestratorError::InvariantError(
"artifact path for state must be set at this point",
))?
.to_path_buf(),
node_ws_url: node_ws_url.clone(),
onboard_as_para: para.onboard_as_parachain,
seed: None, // TODO: Seed is passed by?
finalization: false, // TODO: Seed is passed by?
};

println!("{:#?}", register_para_options);
let _ = Parachain::register(register_para_options).await;
}

// TODO (future):

// - add-ons (introspector/tracing/etc)
Expand Down
50 changes: 22 additions & 28 deletions crates/orchestrator/src/network/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
use subxt::{dynamic::Value, OnlineClient, SubstrateConfig};
use subxt_signer::sr25519::Keypair;

// use crate::generators::key::generate_pair;
// use sp_core::{sr25519, Pair};
use super::node::NetworkNode;
use crate::shared::types::{ParachainGenesisArgs, RegisterParachainOptions};

Expand Down Expand Up @@ -43,54 +45,46 @@ impl Parachain {
}
}

pub async fn register(
options: RegisterParachainOptions,
) -> Result<(), Box<dyn std::error::Error>> {
pub async fn register(options: RegisterParachainOptions) -> Result<(), anyhow::Error> {
println!("Registering parachain: {:?}", options);
// get the seed
let seed: [u8; 32];
if let Some(possible_seed) = options.seed {
seed = possible_seed;
} else {
seed = b"//Alice".to_vec().try_into().unwrap();
seed = b"//Alice"
.to_vec()
.try_into()
.expect("Alice seed should be ok at this point.")
// seed = "//Alice".to_string();
}
// get the sudo account for registering the parachain
let sudo = Keypair::from_seed(seed).unwrap();
let sudo = Keypair::from_seed(seed).expect("seed should return a Keypair.");

let genesis_state = fs::read_to_string(options.state_path).unwrap();
let wasm_data = fs::read_to_string(options.wasm_path).unwrap();
// TODO: pass the fs as we do in other places as well
let genesis_state =
fs::read_to_string(options.state_path).expect("State Path should be ok by this point.");
let wasm_data =
fs::read_to_string(options.wasm_path).expect("Wasm Path should be ok by this point.");

let parachain_genesis_value = Value::from_bytes(ParachainGenesisArgs {
let parachain_genesis_value = ParachainGenesisArgs {
genesis_head: genesis_state,
validation_code: wasm_data,
parachain: options.onboard_as_para,
});
};

let api = OnlineClient::<SubstrateConfig>::from_url(options.node_ws_url).await?;

// based on subXT docs: The public key bytes are equivalent to a Substrate `AccountId32`;
let account_id_value = Value::from_bytes(sudo.public_key());

// get the nonce for the sudo account
let account_nonce_call = subxt::dynamic::runtime_api_call(
"AccountNonceApi",
"account_nonce",
vec![account_id_value.clone()],
);

let nonce = api
.runtime_api()
.at_latest()
.await?
.call(account_nonce_call)
.await?;

println!("Account nonce: {:#?}", nonce.to_value());
// // based on subXT docs: The public key bytes are equivalent to a Substrate `AccountId32`;
let account_id = sudo.public_key();

let schedule_para = subxt::dynamic::tx(
"ParasSudoWrapperCall",
"sudo_schedule_para_initialize",
vec![account_id_value, parachain_genesis_value],
vec![
Value::from_bytes(account_id),
Value::from_bytes(parachain_genesis_value),
],
);

// TODO: uncomment below and fix the sign and submit (and follow afterwards until
Expand Down
2 changes: 1 addition & 1 deletion crates/orchestrator/src/shared/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct ChainDefaultContext<'a> {

#[derive(Debug, Clone)]
pub struct RegisterParachainOptions {
pub para_id: u32,
pub id: u32,
pub wasm_path: PathBuf,
pub state_path: PathBuf,
pub node_ws_url: String,
Expand Down

0 comments on commit f1038f2

Please sign in to comment.