testnet manager: create client against specific nym-node

This commit is contained in:
Jędrzej Stuczyński
2024-07-23 15:39:24 +01:00
parent 38663e41c3
commit 765589c444
4 changed files with 78 additions and 50 deletions
@@ -15,6 +15,9 @@ pub(crate) struct Args {
#[clap(long)]
nym_client_bin: PathBuf,
#[clap(long)]
gateway: Option<String>,
#[clap(long)]
network_name: Option<String>,
@@ -27,7 +30,7 @@ pub(crate) async fn execute(args: Args) -> Result<(), NetworkManagerError> {
let network = manager.load_existing_network(args.network_name).await?;
let run_cmd = manager
.init_local_nym_client(args.nym_client_bin, &network)
.init_local_nym_client(args.nym_client_bin, &network, args.gateway)
.await?;
if !args.output.is_text() {
@@ -16,10 +16,10 @@ pub(crate) struct Args {
nym_node_bin: PathBuf,
#[clap(long, default_value_t = 3)]
num_mixnodes: u16,
mixnodes: u16,
#[clap(long, default_value_t = 1)]
num_gateways: u16,
gateways: u16,
#[clap(long)]
network_name: Option<String>,
@@ -33,12 +33,7 @@ pub(crate) async fn execute(args: Args) -> Result<(), NetworkManagerError> {
let network = manager.load_existing_network(args.network_name).await?;
let run_cmds = manager
.init_local_nym_nodes(
args.nym_node_bin,
&network,
args.num_mixnodes,
args.num_gateways,
)
.init_local_nym_nodes(args.nym_node_bin, &network, args.mixnodes, args.gateways)
.await?;
if !args.output.is_text() {
@@ -25,6 +25,7 @@ use url::Url;
struct LocalClientCtx<'a> {
nym_client_binary: PathBuf,
client_id: String,
gateway: Option<String>,
progress: ProgressTracker,
network: &'a LoadedNetwork,
@@ -39,6 +40,7 @@ impl<'a> ProgressCtx for LocalClientCtx<'a> {
impl<'a> LocalClientCtx<'a> {
fn new(
nym_client_binary: PathBuf,
gateway: Option<String>,
network: &'a LoadedNetwork,
) -> Result<Self, NetworkManagerError> {
let progress = ProgressTracker::new(format!(
@@ -53,6 +55,7 @@ impl<'a> LocalClientCtx<'a> {
network,
progress,
client_id,
gateway,
})
}
@@ -105,6 +108,21 @@ impl NetworkManager {
}
};
// if we explicitly specified some identity, find THIS node
if let Some(identity) = ctx.gateway.as_ref() {
if let Some(node) = gateways
.nodes
.iter()
.find(|gw| &gw.ed25519_identity_pubkey == identity)
{
return SocketAddr::new(
node.ip_addresses[0],
node.entry.clone().unwrap().ws_port,
);
}
}
// otherwise look for ANY node
if let Some(node) = gateways.nodes.pop() {
return SocketAddr::new(node.ip_addresses[0], node.entry.unwrap().ws_port);
}
@@ -184,23 +202,28 @@ impl NetworkManager {
ctx.set_pb_message(format!("initialising client {id}..."));
ctx.println(format!("\tinitialising client {id}..."));
let mut child = Command::new(&ctx.nym_client_binary)
.args([
"-c",
&env.display().to_string(),
"init",
"--id",
id,
"--enabled-credentials-mode",
"true",
"--port",
&port.to_string(),
])
.stdout(Stdio::null())
.stdin(Stdio::null())
.stderr(Stdio::null())
.kill_on_drop(true)
.spawn()?;
let mut cmd = Command::new(&ctx.nym_client_binary);
cmd.args([
"-c",
&env.display().to_string(),
"init",
"--id",
id,
"--enabled-credentials-mode",
"true",
"--port",
&port.to_string(),
])
.stdout(Stdio::null())
.stdin(Stdio::null())
.stderr(Stdio::null())
.kill_on_drop(true);
if let Some(gateway) = &ctx.gateway {
cmd.args(["--gateway", &gateway]);
}
let mut child = cmd.spawn()?;
let child_fut = child.wait();
let out = ctx.async_with_progress(child_fut).await?;
@@ -251,8 +274,9 @@ minimum_gateway_performance = 0
&self,
nym_client_binary: P,
network: &LoadedNetwork,
gateway: Option<String>,
) -> Result<String, NetworkManagerError> {
let ctx = LocalClientCtx::new(nym_client_binary.as_ref().to_path_buf(), network)?;
let ctx = LocalClientCtx::new(nym_client_binary.as_ref().to_path_buf(), gateway, network)?;
let env_file = ctx.network.default_env_file_path();
if !env_file.exists() {
@@ -37,7 +37,7 @@ impl<'a> ProgressCtx for LocalNodesCtx<'a> {
impl<'a> LocalNodesCtx<'a> {
fn nym_node_id(&self, node: &NymNode) -> String {
format!("{}-{}", self.network.name, node.identity_key)
format!("{}-{}", self.network.name, node.owner.address)
}
fn new(
@@ -247,11 +247,11 @@ impl NetworkManager {
async fn initialise_nym_nodes<'a>(
&self,
ctx: &mut LocalNodesCtx<'a>,
num_mixnodes: u16,
num_gateways: u16,
mixnodes: u16,
gateways: u16,
) -> Result<(), NetworkManagerError> {
const OFFSET: u16 = 100;
if num_mixnodes > OFFSET {
if mixnodes > OFFSET {
panic!("seriously? over 100 mixnodes?")
}
@@ -260,10 +260,10 @@ impl NetworkManager {
style("[1/5]").bold().dim()
));
for i in 0..num_mixnodes {
for i in 0..mixnodes {
self.initialise_nym_node(ctx, i, false).await?;
}
for i in 0..num_gateways {
for i in 0..gateways {
self.initialise_nym_node(ctx, i + OFFSET, true).await?;
}
@@ -317,25 +317,31 @@ impl NetworkManager {
let owner = ctx.signing_node_owner(node)?;
let bonding_fut = if is_gateway {
owner.bond_gateway(
node.gateway(),
node.bonding_signature(),
node.pledge().into(),
None,
let (bonding_fut, typ) = if is_gateway {
(
owner.bond_gateway(
node.gateway(),
node.bonding_signature(),
node.pledge().into(),
None,
),
"gateway",
)
} else {
owner.bond_mixnode(
node.mixnode(),
node.cost_params(),
node.bonding_signature(),
node.pledge().into(),
None,
(
owner.bond_mixnode(
node.mixnode(),
node.cost_params(),
node.bonding_signature(),
node.pledge().into(),
None,
),
"mixnode",
)
};
let res = ctx.async_with_progress(bonding_fut).await?;
ctx.println(format!(
"\t{id} bonded in transaction: {}",
"\t{id} ({typ}) bonded in transaction: {}",
res.transaction_hash
));
@@ -420,7 +426,7 @@ impl NetworkManager {
for gateway in ctx.gateways.iter() {
ctx.println(format!(
"\tpreparing node {} (mixnode)",
"\tpreparing node {} (gateway)",
gateway.identity_key
));
let id = ctx.nym_node_id(gateway);
@@ -464,8 +470,8 @@ impl NetworkManager {
&self,
nym_node_binary: P,
network: &LoadedNetwork,
num_mixnodes: u16,
num_gateways: u16,
mixnodes: u16,
gateways: u16,
) -> Result<RunCommands, NetworkManagerError> {
let mut ctx = LocalNodesCtx::new(
nym_node_binary.as_ref().to_path_buf(),
@@ -478,7 +484,7 @@ impl NetworkManager {
return Err(NetworkManagerError::EnvFileNotGenerated);
}
self.initialise_nym_nodes(&mut ctx, num_mixnodes, num_gateways)
self.initialise_nym_nodes(&mut ctx, mixnodes, gateways)
.await?;
self.transfer_bonding_tokens(&ctx).await?;
self.bond_nym_nodes(&ctx).await?;