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

Commit

Permalink
More CLI commands (#347)
Browse files Browse the repository at this point in the history
* list cross msgs

* cli

* query parent

* add genesis epoch

* query genesis epoch

* debug

* Revert debug

* add more commands

* add to cli

* update event

* update crate

* update cargo

* Fix tests

* Update ipc/cli/src/commands/checkpoint/list_validator_changes.rs

Co-authored-by: adlrocha <[email protected]>

* Update ipc/cli/src/commands/checkpoint/list_validator_changes.rs

Co-authored-by: adlrocha <[email protected]>

* Update ipc/cli/src/commands/checkpoint/topdow_cross.rs

Co-authored-by: adlrocha <[email protected]>

* Update ipc/cli/src/commands/checkpoint/topdow_cross.rs

Co-authored-by: adlrocha <[email protected]>

* Update ipc/cli/src/commands/checkpoint/list_validator_changes.rs

Co-authored-by: adlrocha <[email protected]>

* Update ipc/cli/src/commands/subnet/genesis_epoch.rs

Co-authored-by: adlrocha <[email protected]>

---------

Co-authored-by: adlrocha <[email protected]>
  • Loading branch information
cryptoAtwill and adlrocha authored Oct 25, 2023
1 parent d4a1fe2 commit 31a3be2
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 103 deletions.
208 changes: 129 additions & 79 deletions Cargo.lock

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions ipc/cli/src/commands/checkpoint/list_validator_changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! List validator change set cli command
use std::fmt::Debug;
use std::str::FromStr;

use async_trait::async_trait;
use clap::Args;
use fvm_shared::clock::ChainEpoch;
use ipc_sdk::subnet_id::SubnetID;

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

/// The command to list validator changes committed in a subnet.
pub(crate) struct ListValidatorChanges;

#[async_trait]
impl CommandLineHandler for ListValidatorChanges {
type Arguments = ListValidatorChangesArgs;

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

let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

for h in arguments.from_epoch..=arguments.to_epoch {
let changes = provider.get_validator_changeset(&subnet, h).await?;
log::info!("changes at height: {h} are: {:?}", changes.value);
}

Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "List of validator changes commmitted for a child subnet")]
pub(crate) struct ListValidatorChangesArgs {
#[arg(long, short, help = "Lists the validator changes between two epochs")]
pub subnet: String,
#[arg(long, short, help = "Include checkpoints from this epoch")]
pub from_epoch: ChainEpoch,
#[arg(long, short, help = "Include checkpoints up to this epoch")]
pub to_epoch: ChainEpoch,
}
16 changes: 16 additions & 0 deletions ipc/cli/src/commands/checkpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
use crate::commands::checkpoint::list_checkpoints::{
ListBottomUpCheckpoints, ListBottomUpCheckpointsArgs,
};
use crate::commands::checkpoint::list_validator_changes::{
ListValidatorChanges, ListValidatorChangesArgs,
};
use crate::commands::checkpoint::relayer::{BottomUpRelayer, BottomUpRelayerArgs};
use crate::commands::checkpoint::topdow_cross::{
ListTopdownCrossMessages, ListTopdownCrossMessagesArgs,
};
use crate::{CommandLineHandler, GlobalArguments};
use clap::{Args, Subcommand};

mod list_checkpoints;
mod list_validator_changes;
mod relayer;
mod topdow_cross;

#[derive(Debug, Args)]
#[command(name = "checkpoint", about = "checkpoint related commands")]
Expand All @@ -23,6 +31,12 @@ impl CheckpointCommandsArgs {
match &self.command {
Commands::ListBottomup(args) => ListBottomUpCheckpoints::handle(global, args).await,
Commands::Relayer(args) => BottomUpRelayer::handle(global, args).await,
Commands::ListTopdownCrossMsgs(args) => {
ListTopdownCrossMessages::handle(global, args).await
}
Commands::ListValidatorChanges(args) => {
ListValidatorChanges::handle(global, args).await
}
}
}
}
Expand All @@ -31,4 +45,6 @@ impl CheckpointCommandsArgs {
pub(crate) enum Commands {
ListBottomup(ListBottomUpCheckpointsArgs),
Relayer(BottomUpRelayerArgs),
ListTopdownCrossMsgs(ListTopdownCrossMessagesArgs),
ListValidatorChanges(ListValidatorChangesArgs),
}
60 changes: 60 additions & 0 deletions ipc/cli/src/commands/checkpoint/topdow_cross.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! List top down cross messages
use anyhow::anyhow;
use std::fmt::Debug;
use std::str::FromStr;

use async_trait::async_trait;
use clap::Args;
use fvm_shared::clock::ChainEpoch;
use ipc_sdk::subnet_id::SubnetID;

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

/// The command to list top down cross messages in a subnet
pub(crate) struct ListTopdownCrossMessages;

#[async_trait]
impl CommandLineHandler for ListTopdownCrossMessages {
type Arguments = ListTopdownCrossMessagesArgs;

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

let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

let hash = if let Some(hash) = &arguments.block_hash {
hex::decode(hash)?
} else {
let parent = subnet
.parent()
.ok_or_else(|| anyhow!("subnet has not parent"))?;
let epoch = provider.get_chain_head_height(&parent).await?;
let hash = provider.get_block_hash(&parent, epoch).await?;
hash.block_hash
};
let msgs = provider
.get_top_down_msgs(&subnet, arguments.epoch, &hash)
.await?;
for msg in msgs {
println!("{msg:?}");
}

Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "List topdown cross messages for a specific epoch")]
pub(crate) struct ListTopdownCrossMessagesArgs {
#[arg(long, short, help = "The subnet id of the topdown subnet")]
pub subnet: String,
#[arg(long, short, help = "Include topdown messages of this epoch")]
pub epoch: ChainEpoch,
#[arg(long, short, help = "The block hash to query until")]
pub block_hash: Option<String>,
}
38 changes: 38 additions & 0 deletions ipc/cli/src/commands/subnet/genesis_epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! Get the genesis epoch cli command
use async_trait::async_trait;
use clap::Args;
use ipc_sdk::subnet_id::SubnetID;
use std::fmt::Debug;
use std::str::FromStr;

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

/// The command to get the genensis epoch.
pub(crate) struct GenesisEpoch;

#[async_trait]
impl CommandLineHandler for GenesisEpoch {
type Arguments = GenesisEpochArgs;

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

let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

let ls = provider.genesis_epoch(&subnet).await?;
println!("genesis epoch: {}", ls);

Ok(())
}
}

#[derive(Debug, Args)]
#[command(name = "genesis-epoch", about = "Get the genesis epoch of subnet")]
pub(crate) struct GenesisEpochArgs {
#[arg(long, short, help = "The subnet id to query genesis epoch")]
pub subnet: String,
}
7 changes: 5 additions & 2 deletions ipc/cli/src/commands/subnet/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ impl CommandLineHandler for JoinSubnet {
None => None,
};
let public_key = hex::decode(&arguments.public_key)?;
provider
let epoch = provider
.join_subnet(
subnet,
from,
f64_to_token_amount(arguments.collateral)?,
public_key,
)
.await
.await?;
log::info!("joined at epoch: {epoch}");

Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion ipc/cli/src/commands/subnet/list_subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ impl CommandLineHandler for ListSubnets {

for (_, s) in ls.iter() {
println!(
"{:?} - status: {:?}, collateral: {:?} FIL, circ.supply: {:?} FIL",
"{:?} - status: {:?}, collateral: {:?} FIL, circ.supply: {:?} FIL, genesis: {:}",
s.id.to_string(),
s.status,
TokenAmount::from_whole(s.stake.atto().clone()),
TokenAmount::from_whole(s.circ_supply.atto().clone()),
s.genesis_epoch
);
}

Expand Down
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 @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT

pub use crate::commands::subnet::create::{CreateSubnet, CreateSubnetArgs};
use crate::commands::subnet::genesis_epoch::{GenesisEpoch, GenesisEpochArgs};
pub use crate::commands::subnet::join::{JoinSubnet, JoinSubnetArgs};
pub use crate::commands::subnet::kill::{KillSubnet, KillSubnetArgs};
pub use crate::commands::subnet::leave::{LeaveSubnet, LeaveSubnetArgs};
Expand All @@ -15,6 +16,7 @@ use self::join::{StakeSubnet, StakeSubnetArgs, UnstakeSubnet, UnstakeSubnetArgs}
use self::leave::{Claim, ClaimArgs};

pub mod create;
mod genesis_epoch;
pub mod join;
pub mod kill;
pub mod leave;
Expand Down Expand Up @@ -46,6 +48,7 @@ impl SubnetCommandsArgs {
Commands::Stake(args) => StakeSubnet::handle(global, args).await,
Commands::Unstake(args) => UnstakeSubnet::handle(global, args).await,
Commands::Claim(args) => Claim::handle(global, args).await,
Commands::GenesisEpoch(args) => GenesisEpoch::handle(global, args).await,
}
}
}
Expand All @@ -62,4 +65,5 @@ pub(crate) enum Commands {
Stake(StakeSubnetArgs),
Unstake(UnstakeSubnetArgs),
Claim(ClaimArgs),
GenesisEpoch(GenesisEpochArgs),
}
2 changes: 1 addition & 1 deletion ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl IpcProvider {
from: Option<Address>,
collateral: TokenAmount,
public_key: Vec<u8>,
) -> anyhow::Result<()> {
) -> anyhow::Result<ChainEpoch> {
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")),
Expand Down
1 change: 1 addition & 0 deletions ipc/provider/src/lotus/message/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct SubnetInfo {
/// State of the Subnet (Initialized, Active, Killed)
#[serde(rename(deserialize = "Status"))]
pub status: Status,
pub genesis_epoch: ChainEpoch,
}

/// We need to redefine the struct here due to:
Expand Down
1 change: 1 addition & 0 deletions ipc/provider/src/lotus/message/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn test_subnet_info_to_str() {
stake: Default::default(),
circ_supply: Default::default(),
status: Status::Active,
genesis_epoch: 0,
};

let w = serde_json::to_string(&s);
Expand Down
15 changes: 8 additions & 7 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

use ethers::types::H256;
use ipc_actors_abis::{
gateway_getter_facet, gateway_manager_facet, gateway_messenger_facet,
gateway_getter_facet, gateway_manager_facet, gateway_messenger_facet, lib_staking_change_log,
subnet_actor_getter_facet, subnet_actor_manager_facet, subnet_registry,
};
use ipc_sdk::evm::{fil_to_eth_amount, payload_to_evm_address, subnet_id_to_evm_addresses};
Expand Down Expand Up @@ -36,7 +36,7 @@ use ipc_identity::{EthKeyAddress, EvmKeyStore, PersistentKeyStore};
use ipc_sdk::checkpoint::{BottomUpCheckpoint, BottomUpCheckpointBundle};
use ipc_sdk::cross::CrossMsg;
use ipc_sdk::gateway::Status;
use ipc_sdk::staking::{NewStakingRequest, StakingChangeRequest};
use ipc_sdk::staking::StakingChangeRequest;
use ipc_sdk::subnet::ConstructParams;
use ipc_sdk::subnet_id::SubnetID;
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -171,7 +171,7 @@ impl TopDownCheckpointQuery for EthSubnetManager {
);

let ev = contract
.event::<NewStakingRequest>()
.event::<lib_staking_change_log::NewStakingChangeRequestFilter>()
.from_block(epoch as u64)
.to_block(epoch as u64);

Expand Down Expand Up @@ -286,7 +286,7 @@ impl SubnetManager for EthSubnetManager {
from: Address,
collateral: TokenAmount,
pub_key: Vec<u8>,
) -> Result<()> {
) -> Result<ChainEpoch> {
let collateral = collateral
.atto()
.to_u128()
Expand All @@ -305,9 +305,9 @@ impl SubnetManager for EthSubnetManager {
txn.tx.set_value(collateral);
let txn = call_with_premium_estimation(signer, txn).await?;

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

Ok(())
let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
block_number_from_receipt(receipt)
}

async fn stake(&self, subnet: SubnetID, from: Address, collateral: TokenAmount) -> Result<()> {
Expand Down Expand Up @@ -1130,6 +1130,7 @@ impl TryFrom<gateway_getter_facet::Subnet> for SubnetInfo {
id: SubnetID::try_from(value.id)?,
stake: eth_to_fil_amount(&value.stake)?,
circ_supply: eth_to_fil_amount(&value.circ_supply)?,
genesis_epoch: value.genesis_epoch.as_u64() as ChainEpoch,
status: match value.status {
1 => Status::Active,
2 => Status::Inactive,
Expand Down
2 changes: 1 addition & 1 deletion ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait SubnetManager: Send + Sync + TopDownCheckpointQuery + BottomUpCheckpoi
from: Address,
collateral: TokenAmount,
metadata: Vec<u8>,
) -> Result<()>;
) -> Result<ChainEpoch>;

/// Allows validators that have already joined the subnet to stake more collateral
/// and increase their power in the subnet
Expand Down
17 changes: 5 additions & 12 deletions ipc/sdk/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! Staking module related types and functions
use crate::ethers_address_to_fil_address;
use ethers::contract::EthEvent;
use fvm_shared::address::Address;
use ipc_actors_abis::lib_staking_change_log;

pub type ConfigurationNumber = u64;

Expand Down Expand Up @@ -40,19 +40,12 @@ pub struct StakingChange {
pub validator: Address,
}

/// The event emitted when a staking request is perform in solidity contracts
#[derive(Clone, Debug, EthEvent)]
pub struct NewStakingRequest {
op: u8,
validator: ethers::types::Address,
payload: ethers::types::Bytes,
configuration_number: u64,
}

impl TryFrom<NewStakingRequest> for StakingChangeRequest {
impl TryFrom<lib_staking_change_log::NewStakingChangeRequestFilter> for StakingChangeRequest {
type Error = anyhow::Error;

fn try_from(value: NewStakingRequest) -> Result<Self, Self::Error> {
fn try_from(
value: lib_staking_change_log::NewStakingChangeRequestFilter,
) -> Result<Self, Self::Error> {
Ok(Self {
configuration_number: value.configuration_number,
change: StakingChange {
Expand Down

0 comments on commit 31a3be2

Please sign in to comment.