-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add verifier to check readiness (#118)
- Loading branch information
Showing
9 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod verifier; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::time::Duration; | ||
|
||
use tokio::time::timeout; | ||
|
||
use crate::network::node::NetworkNode; | ||
|
||
pub async fn verify_nodes(nodes: &[&NetworkNode]) -> Result<(), anyhow::Error> { | ||
timeout(Duration::from_secs(90), check_nodes(nodes)) | ||
.await | ||
.map_err(|_| anyhow::anyhow!("one or more nodes are not ready!")) | ||
} | ||
|
||
// TODO: we should inject in someway the logic to make the request | ||
// in order to allow us to `mock` and easily test this. | ||
// maybe moved to the provider with a NodeStatus, and some helpers like wait_running, wait_ready, etc... ? to be discussed | ||
async fn check_nodes(nodes: &[&NetworkNode]) { | ||
loop { | ||
let tasks: Vec<_> = nodes | ||
.iter() | ||
.map(|node| { | ||
// TODO: move to logger | ||
// println!("getting from {}", node.name); | ||
reqwest::get(node.prometheus_uri.clone()) | ||
}) | ||
.collect(); | ||
|
||
let all_ready = futures::future::try_join_all(tasks).await; | ||
if all_ready.is_ok() { | ||
return; | ||
} | ||
|
||
tokio::time::sleep(Duration::from_millis(500)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod register_para; | ||
pub mod validator_actions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::str::FromStr; | ||
|
||
use subxt::{dynamic::Value, OnlineClient, SubstrateConfig}; | ||
use subxt_signer::{sr25519::Keypair, SecretUri}; | ||
use support::fs::FileSystem; | ||
|
||
use crate::{shared::types::RegisterParachainOptions, ScopedFilesystem}; | ||
|
||
|
||
pub async fn register( | ||
options: RegisterParachainOptions, | ||
scoped_fs: &ScopedFilesystem<'_, impl FileSystem>, | ||
) -> Result<(), anyhow::Error> { | ||
println!("Registering parachain: {:?}", options); | ||
// get the seed | ||
let sudo: Keypair; | ||
if let Some(possible_seed) = options.seed { | ||
sudo = Keypair::from_seed(possible_seed).expect("seed should return a Keypair."); | ||
} else { | ||
let uri = SecretUri::from_str("//Alice")?; | ||
sudo = Keypair::from_uri(&uri)?; | ||
} | ||
|
||
let genesis_state = scoped_fs | ||
.read_to_string(options.state_path) | ||
.await | ||
.expect("State Path should be ok by this point."); | ||
let wasm_data = scoped_fs | ||
.read_to_string(options.wasm_path) | ||
.await | ||
.expect("Wasm Path should be ok by this point."); | ||
|
||
let api = OnlineClient::<SubstrateConfig>::from_url(options.node_ws_url).await?; | ||
|
||
let schedule_para = subxt::dynamic::tx( | ||
"ParasSudoWrapper", | ||
"sudo_schedule_para_initialize", | ||
vec![ | ||
Value::primitive(options.id.into()), | ||
Value::named_composite([ | ||
( | ||
"genesis_head", | ||
Value::from_bytes(hex::decode(&genesis_state[2..])?), | ||
), | ||
( | ||
"validation_code", | ||
Value::from_bytes(hex::decode(&wasm_data[2..])?), | ||
), | ||
("para_kind", Value::bool(options.onboard_as_para)), | ||
]), | ||
], | ||
); | ||
|
||
let sudo_call = subxt::dynamic::tx("Sudo", "sudo", vec![schedule_para.into_value()]); | ||
|
||
// TODO: uncomment below and fix the sign and submit (and follow afterwards until | ||
// finalized block) to register the parachain | ||
let result = api | ||
.tx() | ||
.sign_and_submit_then_watch_default(&sudo_call, &sudo) | ||
.await?; | ||
|
||
let result = result.wait_for_in_block().await?; | ||
println!("In block: {:#?}", result.block_hash()); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::str::FromStr; | ||
|
||
use subxt::{dynamic::Value, OnlineClient, SubstrateConfig}; | ||
use subxt_signer::{sr25519::Keypair, SecretUri}; | ||
|
||
|
||
pub async fn register( | ||
validator_ids: Vec<String>, | ||
node_ws_url: &str, | ||
) -> Result<(), anyhow::Error> { | ||
println!("Registering validators: {:?}", validator_ids); | ||
// get the seed | ||
// let sudo: Keypair; | ||
// if let Some(possible_seed) = options.seed { | ||
// sudo = Keypair::from_seed(possible_seed).expect("seed should return a Keypair."); | ||
// } else { | ||
let uri = SecretUri::from_str("//Alice")?; | ||
let sudo = Keypair::from_uri(&uri)?; | ||
// } | ||
|
||
println!("pse"); | ||
let api = OnlineClient::<SubstrateConfig>::from_url(node_ws_url).await?; | ||
println!("pse connected"); | ||
|
||
// let bytes: Vec<Value> = validator_ids.iter().map(|id| Value::from_bytes(id)).collect(); | ||
// println!("{:?}", bytes); | ||
|
||
let register_call = subxt::dynamic::tx( | ||
"ValidatorManager", | ||
"register_validators", | ||
vec![Value::unnamed_composite(vec![Value::from_bytes(validator_ids.first().unwrap().as_bytes())])], | ||
); | ||
|
||
let sudo_call = subxt::dynamic::tx("Sudo", "sudo", vec![register_call.into_value()]); | ||
|
||
println!("pse1"); | ||
// TODO: uncomment below and fix the sign and submit (and follow afterwards until | ||
// finalized block) to register the parachain | ||
let result = api | ||
.tx() | ||
.sign_and_submit_then_watch_default(&sudo_call, &sudo) | ||
.await?; | ||
|
||
println!("result: {:#?}", result); | ||
let result = result.wait_for_in_block().await?; | ||
println!("In block: {:#?}", result.block_hash()); | ||
Ok(()) | ||
} |