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

Commit

Permalink
Merge pull request #288 from consensus-shipyard/fix/netWorkerAddr
Browse files Browse the repository at this point in the history
Support setValidatorNetAddr, setValidatorWorkerAddr, listValidators
  • Loading branch information
adlrocha authored Aug 25, 2023
2 parents 6c25707 + 4621488 commit e15a7d4
Show file tree
Hide file tree
Showing 13 changed files with 8,157 additions and 7,027 deletions.
6,662 changes: 4,052 additions & 2,610 deletions contracts/SubnetActorGetterFacet.json

Large diffs are not rendered by default.

8,259 changes: 3,852 additions & 4,407 deletions contracts/SubnetActorManagerFacet.json

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/cli/commands/subnet/list_validators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! List subnet validators cli command

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;

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

/// The command to create a new subnet actor.
pub(crate) struct ListValidators;

#[async_trait]
impl CommandLineHandler for ListValidators {
type Arguments = ListValidatorsArgs;

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 params = QueryValidatorSetParams {
subnet: arguments.subnet.clone(),
};

let valset = json_rpc_client
.request::<QueryValidatorSetResponse>(
json_rpc_methods::QUERY_VALIDATOR_SET,
serde_json::to_value(params)?,
)
.await?;

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

Ok(())
}
}

#[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 subnet id to query validators")]
pub subnet: String,
}
12 changes: 12 additions & 0 deletions src/cli/commands/subnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ pub use crate::cli::commands::subnet::join::{JoinSubnet, JoinSubnetArgs};
pub use crate::cli::commands::subnet::kill::{KillSubnet, KillSubnetArgs};
pub use crate::cli::commands::subnet::leave::{LeaveSubnet, LeaveSubnetArgs};
use crate::cli::commands::subnet::list_subnets::{ListSubnets, ListSubnetsArgs};
use crate::cli::commands::subnet::list_validators::{ListValidators, ListValidatorsArgs};
use crate::cli::commands::subnet::net_addr::{SetValidatorNetAddr, SetValidatorNetAddrArgs};
use crate::cli::commands::subnet::send_value::{SendValue, SendValueArgs};
use crate::cli::commands::subnet::worker_addr::{
SetValidatorWorkerAddr, SetValidatorWorkerAddrArgs,
};
use crate::cli::{CommandLineHandler, GlobalArguments};
use clap::{Args, Subcommand};

Expand All @@ -18,9 +22,11 @@ pub mod join;
pub mod kill;
pub mod leave;
pub mod list_subnets;
pub mod list_validators;
pub mod net_addr;
pub mod rpc;
pub mod send_value;
pub mod worker_addr;

#[derive(Debug, Args)]
#[command(
Expand All @@ -38,12 +44,16 @@ impl SubnetCommandsArgs {
match &self.command {
Commands::Create(args) => CreateSubnet::handle(global, args).await,
Commands::List(args) => ListSubnets::handle(global, args).await,
Commands::ListValidators(args) => ListValidators::handle(global, args).await,
Commands::Join(args) => JoinSubnet::handle(global, args).await,
Commands::Rpc(args) => RPCSubnet::handle(global, args).await,
Commands::Leave(args) => LeaveSubnet::handle(global, args).await,
Commands::Kill(args) => KillSubnet::handle(global, args).await,
Commands::SendValue(args) => SendValue::handle(global, args).await,
Commands::SetValidatorNetAddr(args) => SetValidatorNetAddr::handle(global, args).await,
Commands::SetValidatorWorkerAddr(args) => {
SetValidatorWorkerAddr::handle(global, args).await
}
}
}
}
Expand All @@ -52,10 +62,12 @@ impl SubnetCommandsArgs {
pub(crate) enum Commands {
Create(CreateSubnetArgs),
List(ListSubnetsArgs),
ListValidators(ListValidatorsArgs),
Join(JoinSubnetArgs),
Rpc(RPCSubnetArgs),
Leave(LeaveSubnetArgs),
Kill(KillSubnetArgs),
SendValue(SendValueArgs),
SetValidatorNetAddr(SetValidatorNetAddrArgs),
SetValidatorWorkerAddr(SetValidatorWorkerAddrArgs),
}
60 changes: 60 additions & 0 deletions src/cli/commands/subnet/worker_addr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! The command to set the validator worker address

use crate::cli::commands::get_ipc_agent_url;
use crate::cli::{CommandLineHandler, GlobalArguments};
use crate::config::json_rpc_methods;
use crate::jsonrpc::{JsonRpcClient, JsonRpcClientImpl};
use crate::server::worker_addr::SetValidatorWorkerAddrParams;
use async_trait::async_trait;
use clap::Args;

/// Setting the validator worker address
pub(crate) struct SetValidatorWorkerAddr;

#[async_trait]
impl CommandLineHandler for SetValidatorWorkerAddr {
type Arguments = SetValidatorWorkerAddrArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("set the validator worker addr args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let json_rpc_client = JsonRpcClientImpl::new(url, None);

let params = SetValidatorWorkerAddrParams {
subnet: arguments.subnet.clone(),
from: arguments.from.clone(),
validator_worker_addr: arguments.validator_worker_addr.clone(),
};

json_rpc_client
.request::<()>(
json_rpc_methods::SET_VALIDATOR_WORKER_ADDR,
serde_json::to_value(params)?,
)
.await?;

log::info!(
"set the validator worker addr to: {:} in subnet: {:}",
arguments.validator_worker_addr,
arguments.subnet
);

Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "Set the validator worker address")]
pub(crate) struct SetValidatorWorkerAddrArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "Owner address of the validator being updated")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to set the validator")]
pub subnet: String,
#[arg(long, short, help = "New validator worker address")]
pub validator_worker_addr: String,
}
1 change: 1 addition & 0 deletions src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod json_rpc_methods {
pub const RELOAD_CONFIG: &str = "ipc_reloadConfig";
pub const QUERY_VALIDATOR_SET: &str = "ipc_queryValidatorSet";
pub const SET_VALIDATOR_NET_ADDR: &str = "ipc_setValidatorNetAddr";
pub const SET_VALIDATOR_WORKER_ADDR: &str = "ipc_setValidatorWorkerAddr";
pub const SEND_VALUE: &str = "ipc_sendValue";
pub const WALLET_NEW: &str = "ipc_walletNew";
pub const WALLET_REMOVE: &str = "ipc_walletRemove";
Expand Down
50 changes: 42 additions & 8 deletions src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,47 @@ impl SubnetManager for EthSubnetManager {

async fn set_validator_net_addr(
&self,
_subnet: SubnetID,
_from: Address,
_validator_net_addr: String,
subnet: SubnetID,
from: Address,
net_addr: String,
) -> Result<()> {
let address = contract_address_from_subnet(&subnet)?;
log::info!(
"set validator net addr: {net_addr:} on evm subnet: {subnet:} at contract: {address:}"
);

let signer = Arc::new(self.get_signer(&from)?);
let contract = SubnetActorManagerFacet::new(address, signer.clone());

let txn = contract.set_validator_net_addr(net_addr);

let txn = call_with_premium_estimation(signer, txn).await?;

txn.send().await?.await?;

Ok(())
}

async fn set_validator_worker_addr(
&self,
subnet: SubnetID,
from: Address,
worker_addr: Address,
) -> Result<()> {
todo!()
let address = contract_address_from_subnet(&subnet)?;
log::info!("set validator worker addr: {worker_addr:} on evm subnet: {subnet:} at contract: {address:}");

let signer = Arc::new(self.get_signer(&from)?);
let contract = SubnetActorManagerFacet::new(address, signer.clone());

let txn = contract
.set_validator_worker_addr(subnet_actor_manager_facet::FvmAddress::from(worker_addr));

let txn = call_with_premium_estimation(signer, txn).await?;

txn.send().await?.await?;

Ok(())
}

/// Send value between two addresses in a subnet
Expand Down Expand Up @@ -744,10 +780,8 @@ impl EthManager for EthSubnetManager {
epoch: ChainEpoch,
) -> Result<[u8; 32]> {
let address = contract_address_from_subnet(subnet_id)?;
let contract = SubnetActorManagerFacet::new(
address,
Arc::new(self.ipc_contract_info.provider.clone()),
);
let contract =
SubnetActorGetterFacet::new(address, Arc::new(self.ipc_contract_info.provider.clone()));
let (exists, hash) = contract
.bottom_up_checkpoint_hash_at_epoch(epoch as u64)
.await?;
Expand Down
9 changes: 9 additions & 0 deletions src/manager/fvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ impl<T: JsonRpcClient + Send + Sync> SubnetManager for LotusSubnetManager<T> {
Ok(())
}

async fn set_validator_worker_addr(
&self,
_subnet: SubnetID,
_from: Address,
_validator_worker_addr: Address,
) -> Result<()> {
todo!()
}

/// Send value between two addresses in a subnet
async fn send_value(&self, from: Address, to: Address, amount: TokenAmount) -> Result<()> {
let mut message = MpoolPushMessage::new(to, from, METHOD_SEND, Vec::new());
Expand Down
8 changes: 8 additions & 0 deletions src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ pub trait SubnetManager: Send + Sync {
validator_net_addr: String,
) -> Result<()>;

/// Sets a new worker address to an existing validator
async fn set_validator_worker_addr(
&self,
subnet: SubnetID,
from: Address,
validator_worker_addr: Address,
) -> Result<()>;

/// Send value between two addresses in a subnet
async fn send_value(&self, from: Address, to: Address, amount: TokenAmount) -> Result<()>;

Expand Down
2 changes: 2 additions & 0 deletions src/server/handlers/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub mod list_checkpoints;
pub mod list_subnets;
pub mod net_addr;
pub mod propagate;
pub mod query_validators;
pub mod release;
pub mod rpc;
pub mod send_cross;
pub mod send_value;
pub mod subnet;
pub mod topdown_executed;
pub mod worker_addr;

pub(crate) fn check_subnet(subnet: &Subnet) -> Result<()> {
match &subnet.config {
Expand Down
57 changes: 57 additions & 0 deletions src/server/handlers/manager/worker_addr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! Set the subnet actor validator worker addr

use crate::server::subnet::SubnetManagerPool;
use crate::server::{check_subnet, parse_from, JsonRPCRequestHandler};
use anyhow::anyhow;
use async_trait::async_trait;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize)]
pub struct SetValidatorWorkerAddrParams {
pub subnet: String,
pub from: Option<String>,
pub validator_worker_addr: String,
}

/// Sets a new worker address to an existing validator
pub(crate) struct SetValidatorWorkerAddrHandler {
pool: Arc<SubnetManagerPool>,
}

impl SetValidatorWorkerAddrHandler {
pub(crate) fn new(pool: Arc<SubnetManagerPool>) -> Self {
Self { pool }
}
}

#[async_trait]
impl JsonRPCRequestHandler for SetValidatorWorkerAddrHandler {
type Request = SetValidatorWorkerAddrParams;
type Response = ();

async fn handle(&self, request: Self::Request) -> anyhow::Result<Self::Response> {
let subnet = SubnetID::from_str(&request.subnet)?;
let parent = subnet.parent().ok_or_else(|| anyhow!("no parent found"))?;
let conn = match self.pool.get(&parent) {
None => return Err(anyhow!("target parent subnet not found")),
Some(conn) => conn,
};

let subnet_config = conn.subnet();
check_subnet(subnet_config)?;

let from = parse_from(subnet_config, request.from)?;

let worker_addr = Address::from_str(&request.validator_worker_addr)?;

conn.manager()
.set_validator_worker_addr(subnet, from, worker_addr)
.await
}
}
10 changes: 8 additions & 2 deletions src/server/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use crate::server::handlers::config::ReloadConfigHandler;
use crate::server::handlers::manager::fund::FundHandler;
use crate::server::handlers::manager::list_subnets::ListSubnetsHandler;
use crate::server::handlers::manager::propagate::PropagateHandler;
use crate::server::handlers::manager::query_validators::QueryValidatorSetHandler;
use crate::server::handlers::manager::release::ReleaseHandler;
use crate::server::handlers::manager::send_cross::SendCrossMsgHandler;
use crate::server::handlers::send_value::SendValueHandler;
use crate::server::handlers::validator::QueryValidatorSetHandler;
use crate::server::handlers::wallet::balances::WalletBalancesHandler;
use crate::server::handlers::wallet::new::WalletNewHandler;
use crate::server::list_checkpoints::ListBottomUpCheckpointsHandler;
use crate::server::net_addr::SetValidatorNetAddrHandler;
use crate::server::worker_addr::SetValidatorWorkerAddrHandler;
use crate::server::JsonRPCRequestHandler;
use ipc_identity::Wallet;

Expand All @@ -47,7 +48,6 @@ use self::wallet::remove::WalletRemoveHandler;

mod config;
mod manager;
mod validator;
pub mod wallet;

pub type Method = String;
Expand Down Expand Up @@ -166,6 +166,12 @@ impl Handlers {
let h: Box<dyn HandlerWrapper> = Box::new(SetValidatorNetAddrHandler::new(pool.clone()));
handlers.insert(String::from(json_rpc_methods::SET_VALIDATOR_NET_ADDR), h);

let h: Box<dyn HandlerWrapper> = Box::new(SetValidatorWorkerAddrHandler::new(pool.clone()));
handlers.insert(String::from(json_rpc_methods::SET_VALIDATOR_WORKER_ADDR), h);

let h: Box<dyn HandlerWrapper> = Box::new(ListSubnetsHandler::new(pool.clone()));
handlers.insert(String::from(json_rpc_methods::LIST_CHILD_SUBNETS), h);

let h: Box<dyn HandlerWrapper> = Box::new(ListSubnetsHandler::new(pool.clone()));
handlers.insert(String::from(json_rpc_methods::LIST_CHILD_SUBNETS), h);

Expand Down

0 comments on commit e15a7d4

Please sign in to comment.