Skip to content

Commit

Permalink
use rust code to generate ports for github action
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Sep 21, 2024
1 parent 893654d commit c6f167e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 82 deletions.
26 changes: 23 additions & 3 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,37 @@ jobs:
./tests/nodes/start.sh &
# Wait for the nodes to start, the initialization takes some time
# check port 127.0.0.1:(41714 ~ 41716) are open
# check all the ports are open
# when .ports file is not generated, we will retry 20 times to check if all ports are open
port_file=./tests/nodes/.ports
retry_count=0
while [ $retry_count -lt 100 ]; do
if [ -f $port_file ]; then
break
else
echo "File $port_file not found. Retrying in 2 seconds..."
sleep 2
retry_count=$((retry_count + 1))
fi
done
ports=()
while IFS= read -r line; do
ports+=("$line")
done < ./tests/nodes/.ports
for i in {1..20}; do
all_open=true
for port in 41714 41715 41716 8114; do
for port in $ports; do
echo "Checking port $port"
if ! nc -z 127.0.0.1 $port; then
all_open=false
break
fi
done
if $all_open; then
echo "All ports are open"
echo "All ports are open now ..."
break
else
echo "Not all ports are open, waiting 3 seconds before retrying"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/tests/nodes/*/fiber/store
/tests/nodes/*/config.yml
/tests/deploy/udt-init/target
tests/nodes/.ports
49 changes: 40 additions & 9 deletions tests/deploy/udt-init/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 tests/deploy/udt-init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ serde_json = "1.0"
thiserror = "1.0.30"
hex = "0.4.3"
serde_yaml = "0.9.34"
rand = "0.8.5"
61 changes: 58 additions & 3 deletions tests/deploy/udt-init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use ckb_types::{
H256,
};
use ckb_types::{packed::CellDep, prelude::Builder};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

use std::{error::Error as StdErr, str::FromStr};

Expand Down Expand Up @@ -196,6 +198,18 @@ struct UdtInfos {
infos: Vec<UdtInfo>,
}

fn generate_ports(num_ports: usize) -> Vec<u16> {
let mut ports = HashSet::new();
let mut rng = rand::thread_rng();

while ports.len() < num_ports {
let port: u16 = rng.gen_range(1024..=65535);
ports.insert(port);
}

ports.into_iter().collect()
}

fn genrate_nodes_config() {
let nodes_dir = std::env::var("NODES_DIR").expect("env var");
let yaml_file_path = format!("{}/deployer/config.yml", nodes_dir);
Expand Down Expand Up @@ -225,18 +239,32 @@ fn genrate_nodes_config() {
"# you can edit nodes/deployer/config.yml and run `REMOVE_OLD_STATE=y ./tests/nodes/start.sh` to regenerate"
);
let config_dirs = vec!["bootnode", "1", "2", "3"];
let mut ports_map = vec![];
let on_github_action = std::env::var("ON_GITHUB_ACTION").is_ok();
let gen_ports = generate_ports(6);
let mut ports_iter = gen_ports.iter();
for (i, config_dir) in config_dirs.iter().enumerate() {
let use_gen_port = on_github_action && i != 0;
let default_fiber_port = (8343 + i) as u16;
let default_rpc_port = (41713 + i) as u16;
let (fiber_port, rpc_port) = if use_gen_port {
(*ports_iter.next().unwrap(), *ports_iter.next().unwrap())
} else {
(default_fiber_port, default_rpc_port)
};
ports_map.push((default_fiber_port, fiber_port));
ports_map.push((default_rpc_port, rpc_port));
let mut data = data.clone();
data["fiber"]["listening_addr"] =
serde_yaml::Value::String(format!("/ip4/0.0.0.0/tcp/{}", 8343 + i));
serde_yaml::Value::String(format!("/ip4/0.0.0.0/tcp/{}", fiber_port));
data["fiber"]["announced_addrs"] =
serde_yaml::Value::Sequence(vec![serde_yaml::Value::String(format!(
"/ip4/127.0.0.1/tcp/{}",
8343 + i
fiber_port
))]);
data["fiber"]["announced_node_name"] = serde_yaml::Value::String(format!("fiber-{}", i));
data["rpc"]["listening_addr"] =
serde_yaml::Value::String(format!("127.0.0.1:{}", 41713 + i));
serde_yaml::Value::String(format!("127.0.0.1:{}", rpc_port));
data["ckb"]["udt_whitelist"] = serde_yaml::to_value(&udt_infos).unwrap();

// Node 3 acts as a CCH node.
Expand All @@ -249,8 +277,35 @@ fn genrate_nodes_config() {

let new_yaml = header.to_string() + &serde_yaml::to_string(&data).unwrap();
let config_path = format!("{}/{}/config.yml", nodes_dir, config_dir);

std::fs::write(config_path, new_yaml).expect("write failed");
}

if on_github_action {
let bruno_dir = format!("{}/../bruno/environments/", nodes_dir);
for config in std::fs::read_dir(bruno_dir).expect("read dir") {
let config = config.expect("read config");
for (default_port, port) in ports_map.iter() {
eprintln!(
"update bruno config: {:?} {} -> {}",
config, default_port, port
);
let content = std::fs::read_to_string(config.path()).expect("read config");
let new_content = content.replace(&default_port.to_string(), &port.to_string());
std::fs::write(config.path(), new_content).expect("write config");
}
}
}

// write the real ports into a file so that later script can use it to double check the ports
let content = ports_map
.iter()
.map(|(_, port)| port.to_string())
.collect::<Vec<_>>()
.join("\n");

let port_file_path = format!("{}/.ports", nodes_dir);
std::fs::write(port_file_path, content).expect("write ports list");
}

fn init_udt_accounts() -> Result<(), Box<dyn StdErr>> {
Expand Down
67 changes: 0 additions & 67 deletions tests/nodes/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,75 +41,8 @@ if [[ -f "$deploy_dir/.env.local" ]]; then
fi

echo "Initializing finished, begin to start services ...."

sleep 1

replace_port() {
local config_file=$1
shift

if [ ! -f "$config_file" ]; then
echo "Config file $config_file not found"
return 1
fi

cp "$config_file" "$config_file.bak"

while [ $# -gt 0 ]; do
local old_port=$1
local new_port=$2
shift 2
sed -i.bak -e "s/$old_port/$new_port/g" "$config_file"
done

rm "$config_file.bak"

echo "Replaced ports in $config_file"
}

generate_ports() {
local num_ports=$1
local ports=()

for i in $(seq 1 $num_ports); do
while :; do
port=$((RANDOM % 65535 + 1024))
if ! [[ " ${ports[@]} " =~ " ${port} " ]]; then
ports+=($port)
break
fi
done
done

echo "${ports[@]}"
}

generate_ports_and_replace() {
local ports=($(generate_ports 6))

echo "Generated ports: ${ports[@]}"
local config_files=(
"$nodes_dir/1/config.yml"
"$nodes_dir/2/config.yml"
"$nodes_dir/3/config.yml"
"$bruno_dir/test.bru"
"$bruno_dir/xudt-test.bru"
)

for config_file in "${config_files[@]}"; do
replace_port "$config_file" \
8344 "${ports[0]}" 41714 "${ports[1]}" \
8345 "${ports[2]}" 41715 "${ports[3]}" \
8346 "${ports[4]}" 41716 "${ports[5]}"
done
}

# if ON_GITHUB_ACTION is set we generate the ports
if [[ -n "$should_generate_port" ]]; then
generate_ports_and_replace
sleep 1
fi

ckb run -C "$deploy_dir/node-data" --indexer &

# Start the dev node in the background.
Expand Down

0 comments on commit c6f167e

Please sign in to comment.