Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
creating a local testnet with multiple nodes fails using only flags (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hhamud committed Mar 29, 2024
1 parent 2aa30d6 commit 518e7f4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/madara-commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 36 additions & 5 deletions crates/node/src/command.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<Box<dyn ChainSpec>, 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()
Expand All @@ -36,22 +65,24 @@ impl SubstrateCli for Cli {
}

fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, 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(
&include_bytes!("../../../configs/chain-specs/testnet-sharingan-raw.json")[..],
)?),
"" | "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))?)),
}
}
}

Expand Down
61 changes: 61 additions & 0 deletions scripts/run_testnet.sh
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 518e7f4

Please sign in to comment.