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

Commit

Permalink
IPC-361: add pre-release command and command reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
adlrocha committed Nov 2, 2023
1 parent f2da320 commit 0ce891b
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions ipc/cli/src/commands/crossmsg/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,45 @@ pub(crate) struct FundArgs {
#[arg(help = "The amount to fund in FIL, in whole FIL")]
pub amount: f64,
}

pub struct PreFund;

#[async_trait]
impl CommandLineHandler for PreFund {
type Arguments = PreFundArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("pre-fund subnet 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
.pre_fund(
subnet.clone(),
from,
f64_to_token_amount(arguments.initial_balance)?,
)
.await?;
println!("address pre-funded successfully");

Ok(())
}
}

#[derive(Debug, Args)]
#[command(
name = "pre-fund",
about = "Add some funds in genesis to an address in a child-subnet"
)]
pub struct PreFundArgs {
#[arg(long, short, help = "The address funded in the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to add balance to")]
pub subnet: String,
#[arg(help = "Add an initial balance for the address in genesis in the subnet")]
pub initial_balance: f64,
}
6 changes: 6 additions & 0 deletions ipc/cli/src/commands/crossmsg/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
use self::fund::{PreFund, PreFundArgs};
use self::release::{PreRelease, PreReleaseArgs};
use self::topdown_cross::{ListTopdownMsgs, ListTopdownMsgsArgs};
use crate::commands::crossmsg::fund::Fund;
use crate::commands::crossmsg::propagate::Propagate;
Expand Down Expand Up @@ -28,7 +30,9 @@ impl CrossMsgsCommandsArgs {
pub async fn handle(&self, global: &GlobalArguments) -> anyhow::Result<()> {
match &self.command {
Commands::Fund(args) => Fund::handle(global, args).await,
Commands::PreFund(args) => PreFund::handle(global, args).await,
Commands::Release(args) => Release::handle(global, args).await,
Commands::PreRelease(args) => PreRelease::handle(global, args).await,
Commands::Propagate(args) => Propagate::handle(global, args).await,
Commands::ListTopdownMsgs(args) => ListTopdownMsgs::handle(global, args).await,
}
Expand All @@ -38,7 +42,9 @@ impl CrossMsgsCommandsArgs {
#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
Fund(FundArgs),
PreFund(PreFundArgs),
Release(ReleaseArgs),
PreRelease(PreReleaseArgs),
Propagate(PropagateArgs),
ListTopdownMsgs(ListTopdownMsgsArgs),
}
38 changes: 38 additions & 0 deletions ipc/cli/src/commands/crossmsg/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,41 @@ pub(crate) struct ReleaseArgs {
#[arg(help = "The amount to release in FIL, in whole FIL")]
pub amount: f64,
}

pub struct PreRelease;

#[async_trait]
impl CommandLineHandler for PreRelease {
type Arguments = PreReleaseArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("pre-release subnet 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
.pre_release(subnet.clone(), from, f64_to_token_amount(arguments.amount)?)
.await?;
println!("address pre-release successfully");

Ok(())
}
}

#[derive(Debug, Args)]
#[command(
name = "pre-release",
about = "Release some funds from the genesis balance of the child subnet"
)]
pub struct PreReleaseArgs {
#[arg(long, short, help = "The address funded in the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to release balance from")]
pub subnet: String,
#[arg(help = "Amount to release from the genesis balance of a child subnet")]
pub amount: f64,
}
44 changes: 1 addition & 43 deletions ipc/cli/src/commands/subnet/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl CommandLineHandler for JoinSubnet {
if let Some(initial_balance) = arguments.initial_balance {
println!("pre-funding address with {initial_balance}");
provider
.prefund_subnet(subnet.clone(), from, f64_to_token_amount(initial_balance)?)
.pre_fund(subnet.clone(), from, f64_to_token_amount(initial_balance)?)
.await?;
}
let epoch = provider
Expand Down Expand Up @@ -147,45 +147,3 @@ pub struct UnstakeSubnetArgs {
)]
pub collateral: f64,
}

pub struct PreFundSubnet;

#[async_trait]
impl CommandLineHandler for PreFundSubnet {
type Arguments = PreFundSubnetArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("pre-fund subnet 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
.prefund_subnet(
subnet.clone(),
from,
f64_to_token_amount(arguments.initial_balance)?,
)
.await?;
println!("address pre-funded successfully");

Ok(())
}
}

#[derive(Debug, Args)]
#[command(
name = "pre-fund",
about = "Add some funds in genesis to an address in a child-subnet"
)]
pub struct PreFundSubnetArgs {
#[arg(long, short, help = "The address funded in the subnet")]
pub from: Option<String>,
#[arg(long, short, help = "The subnet to add balance to")]
pub subnet: String,
#[arg(help = "Add an initial balance for the address in genesis in the subnet")]
pub initial_balance: f64,
}
7 changes: 1 addition & 6 deletions ipc/cli/src/commands/subnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::{CommandLineHandler, GlobalArguments};
use clap::{Args, Subcommand};

use self::bootstrap::{AddBootstrap, AddBootstrapArgs, ListBootstraps, ListBootstrapsArgs};
use self::join::{
PreFundSubnet, PreFundSubnetArgs, StakeSubnet, StakeSubnetArgs, UnstakeSubnet,
UnstakeSubnetArgs,
};
use self::join::{StakeSubnet, StakeSubnetArgs, UnstakeSubnet, UnstakeSubnetArgs};
use self::leave::{Claim, ClaimArgs};
use self::rpc::{ChainIdSubnet, ChainIdSubnetArgs};

Expand Down Expand Up @@ -58,7 +55,6 @@ impl SubnetCommandsArgs {
Commands::AddBootstrap(args) => AddBootstrap::handle(global, args).await,
Commands::ListBootstraps(args) => ListBootstraps::handle(global, args).await,
Commands::GenesisEpoch(args) => GenesisEpoch::handle(global, args).await,
Commands::PreFund(args) => PreFundSubnet::handle(global, args).await,
}
}
}
Expand All @@ -79,5 +75,4 @@ pub(crate) enum Commands {
AddBootstrap(AddBootstrapArgs),
ListBootstraps(ListBootstrapsArgs),
GenesisEpoch(GenesisEpochArgs),
PreFund(PreFundSubnetArgs),
}
20 changes: 19 additions & 1 deletion ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl IpcProvider {
.await
}

pub async fn prefund_subnet(
pub async fn pre_fund(
&mut self,
subnet: SubnetID,
from: Option<Address>,
Expand All @@ -316,6 +316,24 @@ impl IpcProvider {
conn.manager().pre_fund(subnet, sender, balance).await
}

pub async fn pre_release(
&mut self,
subnet: SubnetID,
from: Option<Address>,
amount: TokenAmount,
) -> 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().pre_release(subnet, sender, amount).await
}

pub async fn stake(
&mut self,
subnet: SubnetID,
Expand Down
27 changes: 27 additions & 0 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,33 @@ impl SubnetManager for EthSubnetManager {
Ok(())
}

async fn pre_release(
&self,
subnet: SubnetID,
from: Address,
amount: TokenAmount,
) -> Result<()> {
let address = contract_address_from_subnet(&subnet)?;
log::info!("pre-release funds from {subnet:} at contract: {address:}");

let amount = amount
.atto()
.to_u128()
.ok_or_else(|| anyhow!("invalid pre-release amount"))?;

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.pre_release(amount.into()))
.await?
.send()
.await?
.await?;

Ok(())
}

async fn stake(&self, subnet: SubnetID, from: Address, collateral: TokenAmount) -> Result<()> {
let collateral = collateral
.atto()
Expand Down
4 changes: 4 additions & 0 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub trait SubnetManager: Send + Sync + TopDownCheckpointQuery + BottomUpCheckpoi
/// it available in the subnet at genesis.
async fn pre_fund(&self, subnet: SubnetID, from: Address, balance: TokenAmount) -> Result<()>;

/// Releases initial funds from an address for a subnet that has not yet been bootstrapped
async fn pre_release(&self, subnet: SubnetID, from: Address, amount: TokenAmount)
-> Result<()>;

/// Allows validators that have already joined the subnet to stake more collateral
/// and increase their power in the subnet.
async fn stake(&self, subnet: SubnetID, from: Address, collateral: TokenAmount) -> Result<()>;
Expand Down

0 comments on commit 0ce891b

Please sign in to comment.