diff --git a/.github/workflows/madara-commands.yml b/.github/workflows/madara-commands.yml index 7e8c0b794f..074e93554e 100644 --- a/.github/workflows/madara-commands.yml +++ b/.github/workflows/madara-commands.yml @@ -37,5 +37,5 @@ jobs: target/production/madara key inspect --scheme Ed25519 "${{ steps.key-gen.outputs.SEED_PHRASE }}" - name: Add keys to the node keystore run: | - target/production/madara key insert --scheme Sr25519 --suri "${{ steps.key-gen.outputs.SEED_PHRASE }}" --key-type aura - target/production/madara key insert --scheme Ed25519 --suri "${{ steps.key-gen.outputs.SEED_PHRASE }}" --key-type gran + target/production/madara key insert --chain=chain-raw.json --scheme Sr25519 --suri "${{ steps.key-gen.outputs.SEED_PHRASE }}" --key-type aura + target/production/madara key insert --chain=chain-raw.json --scheme Ed25519 --suri "${{ steps.key-gen.outputs.SEED_PHRASE }}" --key-type gran diff --git a/CHANGELOG.md b/CHANGELOG.md index d78174cf1a..c6432e3eb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Next release +- Fix(node): Fix creating a local testnet with multiple nodes fails using only + cli flags - dev: change `Vec::new` to `Vec::with_capacity` where possible. - chore(rpc): clean trace api - feat(rpc): added state diff real value in trace api diff --git a/crates/node/src/command.rs b/crates/node/src/command.rs index dec543c15b..2b6df6e8af 100644 --- a/crates/node/src/command.rs +++ b/crates/node/src/command.rs @@ -1,3 +1,8 @@ +use std::env; +use std::fs::{self, File}; +use std::io::Write; +use std::path::PathBuf; + use frame_benchmarking_cli::{BenchmarkCmd, ExtrinsicFactory, SUBSTRATE_REFERENCE_HARDWARE}; use madara_runtime::Block; use sc_cli::{ChainSpec, SubstrateCli}; @@ -10,6 +15,30 @@ use crate::constants::DEV_CHAIN_ID; use crate::constants::SHARINGAN_CHAIN_ID; use crate::{chain_spec, service}; +fn spit_spec_to_json(base_config: impl ChainSpec) -> Result, String> { + let mut temp_dir = match env::var_os("HOME") { + Some(home_dir) => PathBuf::from(home_dir), + None => return Err("Failed to get home directory".to_string()), + }; + + temp_dir.push(".madara"); + + fs::create_dir_all(&temp_dir).map_err(|e| format!("Failed to create directory: {}", e))?; + + let temp_file_name = format!("{:?}-spec.json", base_config.chain_type()); + let temp_file_path = temp_dir.join(temp_file_name); + + let json_config = ChainSpec::as_json(&base_config, true).expect("failed to create json dump of chainspec"); + + if temp_file_path.exists() { + Ok(Box::new(chain_spec::ChainSpec::from_json_file(temp_file_path.clone())?)) + } else { + let mut file = File::create(&temp_file_path).map_err(|e| format!("Failed to create temporary file: {}", e))?; + file.write_all(json_config.as_bytes()).map_err(|e| format!("Failed to write to temporary file: {}", e))?; + Ok(Box::new(chain_spec::ChainSpec::from_json_file(temp_file_path)?)) + } +} + impl SubstrateCli for Cli { fn impl_name() -> String { "Madara Node".into() @@ -36,11 +65,12 @@ impl SubstrateCli for Cli { } fn load_spec(&self, id: &str) -> Result, String> { - Ok(match id { + match id { DEV_CHAIN_ID => { let sealing = self.run.sealing.map(Into::into).unwrap_or_default(); let base_path = self.run.base_path().map_err(|e| e.to_string())?; - Box::new(chain_spec::development_config(sealing, base_path)?) + let base_config = chain_spec::development_config(sealing, base_path)?; + spit_spec_to_json(base_config) } #[cfg(feature = "sharingan")] SHARINGAN_CHAIN_ID => Box::new(chain_spec::ChainSpec::from_json_bytes( @@ -48,10 +78,11 @@ impl SubstrateCli for Cli { )?), "" | "local" | "madara-local" => { let base_path = self.run.base_path().map_err(|e| e.to_string())?; - Box::new(chain_spec::local_testnet_config(base_path, id)?) + let base_config = chain_spec::local_testnet_config(base_path, id)?; + spit_spec_to_json(base_config) } - path_or_url => Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(path_or_url))?), - }) + path_or_url => Ok(Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(path_or_url))?)), + } } } diff --git a/scripts/run_testnet.sh b/scripts/run_testnet.sh new file mode 100644 index 0000000000..e9be101e2c --- /dev/null +++ b/scripts/run_testnet.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env sh + +# Function to kill background processes +cleanup() { + echo "Stopping background processes and removing files..." + find . -type f -name "validator_output.txt" -delete + find . -type f -name "node_output.txt" -delete + kill "$validator_pid" "$node_pid" 2>/dev/null +} + +# Trap EXIT signal to ensure cleanup runs on script exit +trap cleanup EXIT + +# build release +cargo build --release + +# copy configs from dev +./target/release/madara setup --from-local ./configs/ --chain=dev --base-path /tmp/alice + +# copy configs over from dev +./target/release/madara setup --from-local ./configs/ --chain=dev --base-path /tmp/node1 + +# purge validator chain +./target/release/madara purge-chain --base-path /tmp/alice --chain dev -y + +# purge node chain +./target/release/madara purge-chain --base-path /tmp/node1 --chain dev -y + +# run validator in background instance +./target/release/madara \ +--base-path=/tmp/alice \ +--chain=dev \ +--alice \ +--node-key=0000000000000000000000000000000000000000000000000000000000000001 \ +--validator > validator_output.txt 2>&1 & +validator_pid=$! + + +# run node in another background instance + ./target/release/madara \ +--chain=dev \ +--bootnodes=/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp \ +--base-path=/tmp/node1 \ +--rpc-port=9946 > node_output.txt 2>&1 & +node_pid=$! + +# Give some time for the services to start and produce output +sleep 20 + +# look for the 1 peer message +specific_message="1 peers" + +# Check if the message is there in the file and fail if not +if ! grep -q "$specific_message" validator_output.txt; then + echo "Node failed to connect to validator" + cat validator_output.txt + cat node_output.txt + exit 1 +fi + +echo "Validator successfully connected."