Skip to content

Commit

Permalink
refactor: add stash account to set in session for relaychain valida…
Browse files Browse the repository at this point in the history
…tors (#137)
  • Loading branch information
pepoviola authored Nov 14, 2023
1 parent 3e6823d commit ef6e6a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
31 changes: 15 additions & 16 deletions crates/orchestrator/src/generators/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,7 @@ impl ChainSpec {
.pointer(&format!("{}/session", pointer))
.is_some()
{
add_authorities(
&pointer,
&mut chain_spec_json,
&validators,
KeyType::Session,
);
add_authorities(&pointer, &mut chain_spec_json, &validators, false);
} else {
add_aura_authorities(&pointer, &mut chain_spec_json, &validators, KeyType::Aura);
// await addParaCustom(chainSpecFullPathPlain, node);
Expand Down Expand Up @@ -422,12 +417,7 @@ impl ChainSpec {
.pointer(&format!("{}/session", pointer))
.is_some()
{
add_authorities(
&pointer,
&mut chain_spec_json,
&validators,
KeyType::Session,
);
add_authorities(&pointer, &mut chain_spec_json, &validators, true);
}

// staking && nominators
Expand Down Expand Up @@ -691,8 +681,9 @@ fn add_balances(
}
}

fn get_node_keys(node: &NodeSpec) -> GenesisNodeKey {
fn get_node_keys(node: &NodeSpec, use_stash: bool) -> GenesisNodeKey {
let sr_account = node.accounts.accounts.get("sr").unwrap();
let sr_stash = node.accounts.accounts.get("sr_stash").unwrap();
let ed_account = node.accounts.accounts.get("ed").unwrap();
let ec_account = node.accounts.accounts.get("ec").unwrap();
let mut keys = HashMap::new();
Expand All @@ -713,16 +704,24 @@ fn get_node_keys(node: &NodeSpec) -> GenesisNodeKey {
keys.insert("grandpa".to_string(), ed_account.address.clone());
keys.insert("beefy".to_string(), ec_account.address.clone());

(sr_account.address.clone(), sr_account.address.clone(), keys)
let account_to_use = if use_stash { sr_stash } else { sr_account };
(
account_to_use.address.clone(),
account_to_use.address.clone(),
keys,
)
}
fn add_authorities(
runtime_config_ptr: &str,
chain_spec_json: &mut serde_json::Value,
nodes: &[&NodeSpec],
_key_type: KeyType,
use_stash: bool,
) {
if let Some(val) = chain_spec_json.pointer_mut(runtime_config_ptr) {
let keys: Vec<GenesisNodeKey> = nodes.iter().map(|node| get_node_keys(node)).collect();
let keys: Vec<GenesisNodeKey> = nodes
.iter()
.map(|node| get_node_keys(node, use_stash))
.collect();
val["session"]["keys"] = json!(keys);
} else {
unreachable!("pointer to runtime config should be valid!")
Expand Down
24 changes: 17 additions & 7 deletions crates/orchestrator/src/generators/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sp_core::{crypto::SecretStringError, ecdsa, ed25519, sr25519, Pair};

use super::errors::GeneratorError;
use crate::shared::types::{Accounts, NodeAccount};
const KEY_SCHEME: [&str; 3] = ["sr", "ed", "ec"];
const KEYS: [&str; 4] = ["sr", "sr_stash", "ed", "ec"];

pub fn generate_pair<T: Pair>(seed: &str) -> Result<T::Pair, SecretStringError> {
let pair = T::Pair::from_string(seed, None)?;
Expand All @@ -11,26 +11,31 @@ pub fn generate_pair<T: Pair>(seed: &str) -> Result<T::Pair, SecretStringError>

pub fn generate(seed: &str) -> Result<Accounts, GeneratorError> {
let mut accounts: Accounts = Default::default();
for scheme in KEY_SCHEME {
let (address, public_key) = match scheme {
for k in KEYS {
let (address, public_key) = match k {
"sr" => {
let pair = generate_pair::<sr25519::Pair>(seed)
.map_err(|_| GeneratorError::KeyGeneration(scheme.into(), seed.into()))?;
.map_err(|_| GeneratorError::KeyGeneration(k.into(), seed.into()))?;
(pair.public().to_string(), hex::encode(pair.public()))
},
"sr_stash" => {
let pair = generate_pair::<sr25519::Pair>(&format!("{}/stash", seed))
.map_err(|_| GeneratorError::KeyGeneration(k.into(), seed.into()))?;
(pair.public().to_string(), hex::encode(pair.public()))
},
"ed" => {
let pair = generate_pair::<ed25519::Pair>(seed)
.map_err(|_| GeneratorError::KeyGeneration(scheme.into(), seed.into()))?;
.map_err(|_| GeneratorError::KeyGeneration(k.into(), seed.into()))?;
(pair.public().to_string(), hex::encode(pair.public()))
},
"ec" => {
let pair = generate_pair::<ecdsa::Pair>(seed)
.map_err(|_| GeneratorError::KeyGeneration(scheme.into(), seed.into()))?;
.map_err(|_| GeneratorError::KeyGeneration(k.into(), seed.into()))?;
(pair.public().to_string(), hex::encode(pair.public()))
},
_ => unreachable!(),
};
accounts.insert(scheme.into(), NodeAccount::new(address, public_key));
accounts.insert(k.into(), NodeAccount::new(address, public_key));
}
Ok(accounts)
}
Expand Down Expand Up @@ -90,12 +95,17 @@ mod tests {

let pair = generate(&seed).unwrap();
let sr = pair.get("sr").unwrap();
let sr_stash = pair.get("sr_stash").unwrap();
let ed = pair.get("ed").unwrap();
let ec = pair.get("ec").unwrap();
assert_eq!(
sr.address,
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
);
assert_eq!(
sr_stash.address,
"5DZnGRAr28KP4GvbuxW2cBNo9Aodcm4QKUMj3Zqj67YjYStr"
);
assert_eq!(
ed.address,
"5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu"
Expand Down

0 comments on commit ef6e6a4

Please sign in to comment.