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

creating a local testnet with multiple nodes fails using only flags #1482

Merged
merged 16 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/basic_testnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Rust
uses: actions/setup-rust@v1
with:
toolchain: stable

- name: Build and run script
run: |
bash scripts/run_testnet.sh
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
- chore: update cairo-vm commit and update gas per op
- refactor(rpc): use single arc instance of starknet rpc
- build: remove patch on `ring-vrf` dependecy
Expand Down
30 changes: 25 additions & 5 deletions crates/node/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fs::File;
use std::io::Write;

use frame_benchmarking_cli::{BenchmarkCmd, ExtrinsicFactory, SUBSTRATE_REFERENCE_HARDWARE};
use madara_runtime::Block;
use sc_cli::{ChainSpec, SubstrateCli};
Expand All @@ -10,6 +13,21 @@ 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> {
hhamud marked this conversation as resolved.
Show resolved Hide resolved
// find a better place for this tmp file
let temp_path = format!("{:?}-spec.json", base_config.chain_type());
let json_config = ChainSpec::as_json(&base_config, true).unwrap();
let f_o = File::open(temp_path.clone());
match f_o {
Ok(_) => Ok(Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(temp_path.clone()))?)),
Err(_) => {
let mut f = File::create(temp_path.clone()).expect("failed to create tmp file");
f.write_all(json_config.as_bytes()).expect("failed to write file");
Ok(Box::new(chain_spec::ChainSpec::from_json_file(std::path::PathBuf::from(temp_path.clone()))?))
}
}
}

impl SubstrateCli for Cli {
fn impl_name() -> String {
"Madara Node".into()
Expand All @@ -36,22 +54,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
59 changes: 59 additions & 0 deletions scripts/run_testnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env sh

# Function to kill background processes
cleanup() {
hhamud marked this conversation as resolved.
Show resolved Hide resolved
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
}

# build release
cargo build --release
hhamud marked this conversation as resolved.
Show resolved Hide resolved

# download configs
./target/release/madara setup --from-remote
hhamud marked this conversation as resolved.
Show resolved Hide resolved

# copy configs from local
./target/release/madara setup --from-local ./configs/ --chain=local --base-path /tmp/alice

# copy configs over from local
./target/release/madara setup --from-local ./configs/ --chain=local --base-path /tmp/node1

# purge validator chain
./target/release/madara purge-chain --base-path /tmp/alice --chain local -y

# purge node chain
./target/release/madara purge-chain --base-path /tmp/node1 --chain local -y

# run validator in background instance
./target/release/madara \
--base-path /tmp/alice \
--chain local \
--alice \
--node-key 0000000000000000000000000000000000000000000000000000000000000001 \
--validator > validator_output.txt 2>&1 &
validator_pid=$!


# run node in another background instance
./target/release/madara \
--chain local \
--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"
exit 1
fi

echo "Validator successfully connected."
Loading