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

IPC-299: Implementation of subnet cli commands #310

Merged
merged 17 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
2 changes: 0 additions & 2 deletions ipc/cli/src/commands/subnet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ impl CommandLineHandler for CreateSubnet {
#[derive(Debug, Args)]
#[command(name = "create", about = "Create a new subnet actor")]
pub struct CreateSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The address that creates the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The parent subnet to create the new actor in")]
Expand Down
45 changes: 22 additions & 23 deletions ipc/cli/src/commands/subnet/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::sdk::IpcAgentClient;
use crate::server::join::JoinSubnetParams;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{f64_to_token_amount, get_ipc_provider, CommandLineHandler, GlobalArguments};

/// The command to join a subnet
pub struct JoinSubnet;
Expand All @@ -21,32 +20,32 @@ impl CommandLineHandler for JoinSubnet {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("join subnet with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;

// The json rpc server will handle directing the request to
// the correct parent.
let params = JoinSubnetParams {
subnet: arguments.subnet.clone(),
from: arguments.from.clone(),
collateral: arguments.collateral,
validator_net_addr: arguments.validator_net_addr.clone(),
worker_addr: arguments.worker_addr.clone(),
let mut provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;
let from = match &arguments.from {
Some(address) => Some(Address::from_str(address)?),
None => None,
};
let worker_addr = match &arguments.worker_addr {
Some(address) => Some(Address::from_str(address)?),
None => None,
};

let client = IpcAgentClient::default_from_url(url);
client.join_subnet(params).await?;

log::info!("joined subnet: {:}", arguments.subnet);

Ok(())
provider
.join_subnet(
subnet,
from,
f64_to_token_amount(arguments.collateral)?,
arguments.validator_net_addr.clone(),
worker_addr,
)
.await
}
}

#[derive(Debug, Args)]
#[command(name = "join", about = "Join a subnet")]
pub struct JoinSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The address that joins the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to join")]
Expand Down
27 changes: 10 additions & 17 deletions ipc/cli/src/commands/subnet/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::sdk::IpcAgentClient;
use crate::server::kill::KillSubnetParams;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};

/// The command to kill an existing subnet.
pub struct KillSubnet;
Expand All @@ -21,26 +20,20 @@ impl CommandLineHandler for KillSubnet {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("kill subnet with args: {:?}", arguments);

let params = KillSubnetParams {
subnet: arguments.subnet.clone(),
from: arguments.from.clone(),
let mut provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;
let from = match &arguments.from {
Some(address) => Some(Address::from_str(address)?),
None => None,
};

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let client = IpcAgentClient::default_from_url(url);
client.kill_subnet(params).await?;

log::info!("killed subnet: {:}", arguments.subnet);

Ok(())
provider.kill_subnet(subnet, from).await
}
}

#[derive(Debug, Args)]
#[command(name = "kill", about = "Kill an existing subnet")]
pub struct KillSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The address that kills the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to kill")]
Expand Down
29 changes: 10 additions & 19 deletions ipc/cli/src/commands/subnet/leave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::sdk::IpcAgentClient;
use crate::server::leave::LeaveSubnetParams;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};

/// The command to leave a new subnet.
pub struct LeaveSubnet;
Expand All @@ -21,27 +20,19 @@ impl CommandLineHandler for LeaveSubnet {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("leave subnet with args: {:?}", arguments);

let params = LeaveSubnetParams {
subnet: arguments.subnet.clone(),
from: arguments.from.clone(),
let mut provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;
let from = match &arguments.from {
Some(address) => Some(Address::from_str(address)?),
None => None,
};

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;

let client = IpcAgentClient::default_from_url(url);
client.leave_subnet(params).await?;

log::info!("left subnet: {:}", arguments.subnet);

Ok(())
provider.leave_subnet(subnet, from).await
}
}

#[derive(Debug, Args)]
#[command(name = "leave", about = "Leaving a subnet")]
pub struct LeaveSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The address that leaves the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to leave")]
Expand Down
63 changes: 15 additions & 48 deletions ipc/cli/src/commands/subnet/list_subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

use async_trait::async_trait;
use clap::Args;
use fvm_shared::bigint::BigInt;
use fvm_shared::econ::TokenAmount;
use std::collections::HashMap;
use fvm_shared::{address::Address, econ::TokenAmount};
use ipc_sdk::subnet_id::SubnetID;
use std::fmt::Debug;
use std::str::FromStr;

use crate::commands::get_ipc_agent_url;
use crate::config::json_rpc_methods;
use crate::server::list_subnets::ListSubnetsParams;
use crate::{CommandLineHandler, GlobalArguments};
use ipc_provider::jsonrpc::{JsonRpcClient, JsonRpcClientImpl};
use serde::Deserialize;
use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};

/// The command to create a new subnet actor.
pub(crate) struct ListSubnets;
Expand All @@ -27,32 +21,23 @@ impl CommandLineHandler for ListSubnets {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("list subnets with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let json_rpc_client = JsonRpcClientImpl::new(url, None);
let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

let params = ListSubnetsParams {
gateway_address: arguments.gateway_address.clone(),
subnet_id: arguments.subnet.clone(),
let gateway_addr = match &arguments.gateway_address {
Some(address) => Some(Address::from_str(address)?),
None => None,
};

let subnets = json_rpc_client
.request::<HashMap<String, SubnetInfoWrapper>>(
json_rpc_methods::LIST_CHILD_SUBNETS,
serde_json::to_value(params)?,
)
.await?;
let ls = provider.list_child_subnets(gateway_addr, &subnet).await?;

for (_, s) in subnets.iter() {
let u = BigInt::from_str(&s.stake).unwrap();
let stake = TokenAmount::from_atto(u);
let u = BigInt::from_str(&s.circ_supply).unwrap();
let supply = TokenAmount::from_atto(u);
log::info!(
"{} - status: {}, collateral: {} FIL, circ.supply: {} FIL",
for (_, s) in ls.iter() {
println!(
"{:?} - status: {:?}, collateral: {:?} FIL, circ.supply: {:?} FIL",
s.id,
s.status,
stake,
supply,
TokenAmount::from_whole(s.stake.atto().clone()),
TokenAmount::from_whole(s.circ_supply.atto().clone()),
);
}

Expand All @@ -66,26 +51,8 @@ impl CommandLineHandler for ListSubnets {
about = "List all child subnets registered in the gateway (i.e. that have provided enough collateral)"
)]
pub(crate) struct ListSubnetsArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The gateway address to query subnets")]
pub gateway_address: String,
pub gateway_address: Option<String>,
#[arg(long, short, help = "The subnet id to query child subnets")]
pub subnet: String,
}

/// A simplified wrapper for Subnet Info response. The SubnetInfo struct is deserialized differently
/// as that struct is targeting deserialization from Actor. SubnetInfoWrapper is targeting ipc-agent
/// rpc server, it is using different data structure and casing, i.e. id in actor is represented as
/// a map, but in ipc-agent rpc server, it is a string.
#[derive(Debug, Deserialize)]
struct SubnetInfoWrapper {
#[allow(dead_code)]
id: String,
#[allow(dead_code)]
stake: String,
#[allow(dead_code)]
circ_supply: String,
#[allow(dead_code)]
status: i32,
}
36 changes: 15 additions & 21 deletions ipc/cli/src/commands/subnet/list_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use async_trait::async_trait;
use clap::Args;
use ipc_provider::jsonrpc::{JsonRpcClient, JsonRpcClientImpl};
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::config::json_rpc_methods;
use crate::lotus::message::ipc::QueryValidatorSetResponse;
use crate::server::query_validators::QueryValidatorSetParams;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};

/// The command to create a new subnet actor.
pub(crate) struct ListValidators;
Expand All @@ -23,23 +20,20 @@ impl CommandLineHandler for ListValidators {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("list validators with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let json_rpc_client = JsonRpcClientImpl::new(url, None);
let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

let params = QueryValidatorSetParams {
subnet: arguments.subnet.clone(),
epoch: None,
let gateway_addr = match &arguments.gateway_address {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as previous PR, I think we dont need gateway address anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked in #316

Some(address) => Some(Address::from_str(address)?),
None => None,
};

let valset = json_rpc_client
.request::<QueryValidatorSetResponse>(
json_rpc_methods::QUERY_VALIDATOR_SET,
serde_json::to_value(params)?,
)
let valset = provider
.get_validator_set(&subnet, gateway_addr, None)
.await?;

log::info!("validators number: {}", valset.min_validators);
log::info!("validator set: {:?}", valset.validator_set);
println!("minimum number of validators: {}", valset.min_validators);
println!("validator set: {:?}", valset.validator_set);

Ok(())
}
Expand All @@ -48,8 +42,8 @@ impl CommandLineHandler for ListValidators {
#[derive(Debug, Args)]
#[command(name = "list-validators", about = "Show the validators of the subnet")]
pub(crate) struct ListValidatorsArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The gateway address to query subnets")]
pub gateway_address: Option<String>,
#[arg(long, short, help = "The subnet id to query validators")]
pub subnet: String,
}
Loading