Skip to content

Commit

Permalink
feat : re-introduce auto-insert-keys flag for babe
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Jan 8, 2024
1 parent a9aae11 commit f3bb190
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ parity-scale-codec = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
hex = {workspace = true }

# Substrate dependencies
frame-benchmarking = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub struct Cli {

#[command(flatten)]
pub eth: EthConfiguration,

#[arg(short, long)]
pub auto_insert_keys: bool,
}

#[derive(Debug, clap::Subcommand)]
Expand Down
1 change: 1 addition & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub fn run() -> sc_cli::Result<()> {
rpc_config,
eth_config: cli.eth,
debug_output: cli.output_path,
auto_insert_keys: cli.auto_insert_keys,
})
.map_err(Into::into)
.await
Expand Down
1 change: 1 addition & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod mainnet_fixtures;
pub mod rpc;
pub mod service;
pub mod testnet_fixtures;
pub mod utils;
2 changes: 1 addition & 1 deletion node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod eth;
mod mainnet_fixtures;
mod rpc;
mod testnet_fixtures;

mod utils;
fn main() -> sc_cli::Result<()> {
command::run()
}
44 changes: 43 additions & 1 deletion node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ pub fn new_partial(
>,
ServiceError,
> {
println!(" ++++++++++++++++++++++++
+++++++++++++++++++++++++++
+++++++++++++++++++++++++++
+++ ++++++ +++ @%%%%%%%%%%% %%%
++++++ ++++ +++++ %%%%%%%%%%%% %%%@
++++++++++++++++++++++++++ %%%% %%%%@ %%% %%@ @%%%%%%% %%%@ %%%%@
++++++++ %%%% @%%%%%%%@ %%%%%%%%% @%%%%%%%%% %%%@ %%%%%%%%%
++++++++ %%%% %%%%%%%%% %%%% @%%%@ %%%% %%%% %%%@ %%%%%%%%%%
++++++++++++++++++++++++++ %%%% %%%%%%%%% %%% %%%% %%% @%%% %%%@ @%%%%% %%%%%
++++++ ++++ ++++++ %%%% %%%%%%%%% %%% %%%% %%%%%%%%%% %%%@ %%%%%%%%%@
+++ ++++++ +++ %%%% %%%%%%%%% %%% %%%@ %%%%%%%%% %%% %%%%%%%@
++++ +++++++++ +++ %%%% %%%%
++++++++++++++++++++++++++++ %%%%%%%%%
+++++++++++++++++++++++ %%%%% \n");

let telemetry = config
.telemetry_endpoints
.clone()
Expand Down Expand Up @@ -267,10 +282,11 @@ pub struct RunFullParams {
pub eth_config: EthConfiguration,
pub rpc_config: RpcConfig,
pub debug_output: Option<std::path::PathBuf>,
pub auto_insert_keys: bool,
}
/// Builds a new service for a full client.
pub async fn new_full(
RunFullParams { mut config, eth_config, rpc_config, debug_output: _ }: RunFullParams,
RunFullParams { mut config, eth_config, rpc_config, debug_output: _, auto_insert_keys }: RunFullParams,
) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client,
Expand All @@ -292,6 +308,32 @@ pub async fn new_full(
),
} = new_partial(&config, &eth_config)?;

if config.role.is_authority() {
if auto_insert_keys {
crate::utils::insert_controller_account_keys_into_keystore(
&config,
Some(keystore_container.keystore()),
);
} else {
crate::utils::insert_dev_controller_account_keys_into_keystore(
&config,
Some(keystore_container.keystore()),
);
}

// finally check if keys are inserted correctly
if crate::utils::ensure_all_keys_exist_in_keystore(keystore_container.keystore()).is_err() {
println!("
++++++++++++++++++++++++++++++++++++++++++++++++
Validator keys not found, validator keys are essential to run a validator on
Tangle Network, refer to https://docs.webb.tools/docs/ecosystem-roles/validator/required-keys/ on
how to generate and insert keys. OR start the node with --auto-insert-keys to automatically generate the keys.
++++++++++++++++++++++++++++++++++++++++++++++++
\n");
panic!("Keys not detected!")
}
}

let FrontierPartialComponents { filter_pool, fee_history_cache, fee_history_cache_limit } =
new_frontier_partial(&eth_config)?;

Expand Down
134 changes: 134 additions & 0 deletions node/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use sc_service::{ChainType, Configuration};
use sp_core::{ecdsa, ed25519, sr25519, ByteArray, Pair, Public};
use sp_keystore::{Keystore, KeystorePtr};
use sp_runtime::{
key_types::{ACCOUNT, BABE, GRANDPA, IM_ONLINE},
KeyTypeId,
};

/// Helper function to generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{seed}"), None)
.expect("static values are valid; qed")
.public()
}

/// Inserts a key of type `ACCOUNT` into the keystore for development/testing.
pub fn insert_controller_account_keys_into_keystore(
config: &Configuration,
key_store: Option<KeystorePtr>,
) {
insert_account_keys_into_keystore::<sr25519::Public>(
config,
ACCOUNT,
key_store.clone(),
"acco",
);
insert_account_keys_into_keystore::<ecdsa::Public>(
config,
tangle_crypto_primitives::ROLE_KEY_TYPE,
key_store.clone(),
"Role",
);
insert_account_keys_into_keystore::<ed25519::Public>(
config,
GRANDPA,
key_store.clone(),
"Grandpa",
);
insert_account_keys_into_keystore::<sr25519::Public>(config, BABE, key_store.clone(), "Babe");
insert_account_keys_into_keystore::<sr25519::Public>(
config,
IM_ONLINE,
key_store.clone(),
"ImOnline",
);
}

/// Inserts keys of specified type into the keystore.
fn insert_account_keys_into_keystore<TPublic: Public>(
config: &Configuration,
key_type: KeyTypeId,
key_store: Option<KeystorePtr>,
key_name: &str,
) {
let seed = &config.network.node_name[..];

let pub_key = get_from_seed::<TPublic>(seed).to_raw_vec();
if let Some(keystore) = key_store {
let _ = Keystore::insert(&*keystore, key_type, &format!("//{seed}"), &pub_key);
}

println!("++++++++++++++++++++++++++++++++++++++++++++++++
AUTO GENERATED KEYS
{:?} key inserted to keystore
Seed : //{:?}
Pubkey : {:?}
STORE THE KEYS SAFELY, NOT TO BE SHARED WITH ANYONE ELSE.
++++++++++++++++++++++++++++++++++++++++++++++++
\n", key_name, seed, pub_key.encode());
}

/// Inserts a key of type `ACCOUNT` into the keystore for development/testing.
///
/// Currently, this only successfully inserts keys if the seed is development related.
/// i.e. for Alice, Bob, Charlie, etc.
pub fn insert_dev_controller_account_keys_into_keystore(
config: &Configuration,
key_store: Option<KeystorePtr>,
) {
insert_dev_account_keys_into_keystore::<sr25519::Public>(config, ACCOUNT, key_store.clone());
insert_dev_account_keys_into_keystore::<ecdsa::Public>(
config,
tangle_crypto_primitives::ROLE_KEY_TYPE,
key_store.clone(),
);
}

/// Inserts keys of specified type into the keystore for predefined nodes in development mode.
pub fn insert_dev_account_keys_into_keystore<TPublic: Public>(
config: &Configuration,
key_type: KeyTypeId,
key_store: Option<KeystorePtr>,
) {
let chain_type = config.chain_spec.chain_type();
let seed = &config.network.node_name[..];

match seed {
// When running the chain in dev or local test net, we insert the sr25519 account keys for
// collator accounts or validator accounts into the keystore Only if the node running is one
// of the predefined nodes Alice, Bob, Charlie, Dave, Eve or Ferdie
"Alice" | "Bob" | "Charlie" | "Dave" | "Eve" | "Ferdie" => {
if chain_type == ChainType::Development || chain_type == ChainType::Local {
let pub_key = get_from_seed::<TPublic>(seed).to_raw_vec();
if let Some(keystore) = key_store {
let _ = Keystore::insert(&*keystore, key_type, &format!("//{seed}"), &pub_key);
}
}
},
_ => {},
}
}

/// Ensures all keys exist in the keystore.
pub fn ensure_all_keys_exist_in_keystore(key_store: KeystorePtr) -> Result<(), String> {
let key_types = [tangle_crypto_primitives::ROLE_KEY_TYPE, ACCOUNT, GRANDPA, BABE, IM_ONLINE];

for key_type in key_types {
// Ensure key is present
if !ensure_keytype_exists_in_keystore(key_type, key_store.clone()) {
println!("{key_type:?} key not found!");
return Err("Key not found!".to_string())
}
}

Ok(())
}

/// Checks if a key of a specific type exists in the keystore.
fn ensure_keytype_exists_in_keystore(key_type: KeyTypeId, key_store: KeystorePtr) -> bool {
match Keystore::keys(&key_store, key_type) {
Ok(keys) => !keys.is_empty(),
Err(_) => false,
}
}

0 comments on commit f3bb190

Please sign in to comment.