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

Commit

Permalink
fm: add-bootstrap command in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
adlrocha committed Oct 24, 2023
1 parent 725e558 commit e7a9900
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ipc/cli/src/commands/subnet/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! Subnet bootstrap-related commands
use async_trait::async_trait;
use clap::Args;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::{get_ipc_provider, require_fil_addr_from_str, CommandLineHandler, GlobalArguments};

/// The command to add a bootstrap subnet
pub struct AddBootstrap;

#[async_trait]
impl CommandLineHandler for AddBootstrap {
type Arguments = AddBootstrapArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("add subnet bootstrap with args: {:?}", arguments);

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

provider
.add_bootstrap(&subnet, from, arguments.endpoint.clone())
.await
}
}

#[derive(Debug, Args)]
#[command(name = "add-bootstrap", about = "Advertise bootstrap in the subnet")]
pub struct AddBootstrapArgs {
#[arg(
long,
short,
help = "The address of the validator adding the bootstrap"
)]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to add the bootstrap to")]
pub subnet: String,
#[arg(long, short, help = "The bootstrap node's network endpoint")]
pub endpoint: String,
}
4 changes: 4 additions & 0 deletions ipc/cli/src/commands/subnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use crate::commands::subnet::send_value::{SendValue, SendValueArgs};
use crate::{CommandLineHandler, GlobalArguments};
use clap::{Args, Subcommand};

use self::bootstrap::{AddBootstrap, AddBootstrapArgs};
use self::join::{StakeSubnet, StakeSubnetArgs};
use self::leave::{Claim, ClaimArgs};

pub mod bootstrap;
pub mod create;
pub mod join;
pub mod kill;
Expand Down Expand Up @@ -45,6 +47,7 @@ impl SubnetCommandsArgs {
Commands::SendValue(args) => SendValue::handle(global, args).await,
Commands::Stake(args) => StakeSubnet::handle(global, args).await,
Commands::Claim(args) => Claim::handle(global, args).await,
Commands::AddBootstrap(args) => AddBootstrap::handle(global, args).await,
}
}
}
Expand All @@ -60,4 +63,5 @@ pub(crate) enum Commands {
SendValue(SendValueArgs),
Stake(StakeSubnetArgs),
Claim(ClaimArgs),
AddBootstrap(AddBootstrapArgs),
}
21 changes: 21 additions & 0 deletions ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,27 @@ impl IpcProvider {

conn.manager().chain_head_height().await
}

/// Advertises the endpoint of a bootstrap node for the subnet.
pub async fn add_bootstrap(
&mut self,
subnet: &SubnetID,
from: Option<Address>,
endpoint: String,
) -> anyhow::Result<()> {
let parent = subnet.parent().ok_or_else(|| anyhow!("no parent found"))?;
let conn = match self.connection(&parent) {
None => return Err(anyhow!("target parent subnet not found")),
Some(conn) => conn,
};

let subnet_config = conn.subnet();
let sender = self.check_sender(subnet_config, from)?;

conn.manager()
.add_bootstrap(subnet, &sender, endpoint)
.await
}
}

/// Lotus JSON keytype format
Expand Down
42 changes: 42 additions & 0 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use ipc_actors_abis::{
};
use ipc_sdk::evm::{fil_to_eth_amount, payload_to_evm_address, subnet_id_to_evm_addresses};
use ipc_sdk::validator::from_contract_validators;
use std::net::{IpAddr, SocketAddr};

use ipc_sdk::{eth_to_fil_amount, ethers_address_to_fil_address};

use crate::config::subnet::SubnetConfig;
Expand Down Expand Up @@ -630,6 +632,31 @@ impl SubnetManager for EthSubnetManager {
validators: from_contract_validators(contract.genesis_validators().call().await?)?,
})
}

async fn add_bootstrap(
&self,
subnet: &SubnetID,
from: &Address,
endpoint: String,
) -> Result<()> {
let address = contract_address_from_subnet(subnet)?;

if is_valid_bootstrap_addr(&endpoint).is_none() {
return Err(anyhow!("wrong format for bootstrap endpoint"));
}

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

call_with_premium_estimation(signer, contract.add_bootstrap_node(endpoint))
.await?
.send()
.await?
.await?;

Ok(())
}
}

#[async_trait]
Expand Down Expand Up @@ -1085,6 +1112,21 @@ fn block_number_from_receipt(
}
}

fn is_valid_bootstrap_addr(input: &str) -> Option<(String, IpAddr, u16)> {
let parts: Vec<&str> = input.split('@').collect();

if parts.len() == 2 {
let pubkey = parts[0].to_string();
let addr_str = parts[1];

if let Ok(addr) = addr_str.parse::<SocketAddr>() {
return Some((pubkey, addr.ip(), addr.port()));
}
}

None
}

/// Convert the ipc SubnetID type to an evm address. It extracts the last address from the Subnet id
/// children and turns it into evm address.
pub(crate) fn contract_address_from_subnet(subnet: &SubnetID) -> Result<ethers::types::Address> {
Expand Down
8 changes: 8 additions & 0 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ pub trait SubnetManager: Send + Sync + TopDownCheckpointQuery + BottomUpCheckpoi

/// Gets the genesis information required to bootstrap a child subnet
async fn get_genesis_info(&self, subnet: &SubnetID) -> Result<SubnetGenesisInfo>;

/// Advertises the endpoint of a bootstrap node for the subnet.
async fn add_bootstrap(
&self,
subnet: &SubnetID,
from: &Address,
endpoint: String,
) -> Result<()>;
}

#[derive(Debug)]
Expand Down

0 comments on commit e7a9900

Please sign in to comment.