Skip to content

Commit

Permalink
fix rust naming
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoAtwill committed Sep 20, 2024
1 parent c997cba commit 7f2cb84
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 60 deletions.
4 changes: 2 additions & 2 deletions fendermint/testing/contract-test/tests/staking/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ impl StateMachine for StakingMachine {
min_activation_collateral: to_eth_tokens(&state.min_collateral()).unwrap(),
min_validators: state.min_validators() as u64,
permission_mode: 0, // collateral based
supply_source: ipc_actors_abis::register_subnet_facet::GenericToken {
supply_source: ipc_actors_abis::register_subnet_facet::Asset {
kind: 0, // native token
token_address: ethers::types::Address::zero(),
},
collateral_source: ipc_actors_abis::register_subnet_facet::GenericToken {
collateral_source: ipc_actors_abis::register_subnet_facet::Asset {
kind: 0, // native token
token_address: ethers::types::Address::zero(),
},
Expand Down
18 changes: 9 additions & 9 deletions ipc/api/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::checkpoint::BottomUpMsgBatch;
use crate::cross::{IpcEnvelope, IpcMsgKind};
use crate::staking::StakingChange;
use crate::staking::StakingChangeRequest;
use crate::subnet::{GenericToken, GenericTokenKind};
use crate::subnet::{Asset, AssetKind};
use crate::subnet_id::SubnetID;
use crate::{eth_to_fil_amount, ethers_address_to_fil_address};
use anyhow::anyhow;
Expand Down Expand Up @@ -183,10 +183,10 @@ macro_rules! bottom_up_msg_batch_conversion {
/// The type conversion between different generic token types
macro_rules! generic_token_conversion {
($module:ident) => {
impl TryFrom<GenericToken> for $module::GenericToken {
impl TryFrom<Asset> for $module::Asset {
type Error = anyhow::Error;

fn try_from(value: GenericToken) -> Result<Self, Self::Error> {
fn try_from(value: Asset) -> Result<Self, Self::Error> {
let token_address = if let Some(token_address) = value.token_address {
payload_to_evm_address(token_address.payload())?
} else {
Expand All @@ -200,18 +200,18 @@ macro_rules! generic_token_conversion {
}
}

impl TryFrom<$module::GenericToken> for GenericToken {
impl TryFrom<$module::Asset> for Asset {
type Error = anyhow::Error;

fn try_from(value: $module::GenericToken) -> Result<Self, Self::Error> {
fn try_from(value: $module::Asset) -> Result<Self, Self::Error> {
let token_address = if value.token_address == ethers::types::Address::zero() {
None
} else {
Some(ethers_address_to_fil_address(&value.token_address)?)
};

Ok(Self {
kind: GenericTokenKind::try_from(value.kind)?,
kind: AssetKind::try_from(value.kind)?,
token_address,
})
}
Expand Down Expand Up @@ -241,13 +241,13 @@ generic_token_conversion!(subnet_actor_diamond);
generic_token_conversion!(register_subnet_facet);
generic_token_conversion!(subnet_actor_getter_facet);

impl TryFrom<u8> for GenericTokenKind {
impl TryFrom<u8> for AssetKind {
type Error = anyhow::Error;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(GenericTokenKind::Native),
1 => Ok(GenericTokenKind::ERC20),
0 => Ok(AssetKind::Native),
1 => Ok(AssetKind::ERC20),
_ => Err(anyhow!("invalid kind {value}")),
}
}
Expand Down
15 changes: 7 additions & 8 deletions ipc/api/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ pub enum PermissionMode {

/// Defines a generic token of a subnet on its parent subnet.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct GenericToken {
pub struct Asset {
/// The kind of supply.
pub kind: GenericTokenKind,
pub kind: AssetKind,
/// The address of the ERC20 token if that supply kind is selected.
pub token_address: Option<Address>,
}

impl Default for GenericToken {
impl Default for Asset {
fn default() -> Self {
Self {
kind: GenericTokenKind::Native,
kind: AssetKind::Native,
token_address: None,
}
}
Expand All @@ -69,7 +69,7 @@ impl Default for GenericToken {
strum::VariantNames,
)]
#[strum(serialize_all = "snake_case")]
pub enum GenericTokenKind {
pub enum AssetKind {
Native,
ERC20,
}
Expand All @@ -85,9 +85,8 @@ pub struct ConstructParams {
pub active_validators_limit: u16,
pub min_cross_msg_fee: TokenAmount,
pub permission_mode: PermissionMode,
pub supply_source: GenericToken,
pub collateral_source: GenericToken,
pub validator_gater: Address,
pub supply_source: Asset,
pub collateral_source: Asset,
pub validator_gater: Address,
}

Expand Down
28 changes: 11 additions & 17 deletions ipc/cli/src/commands/subnet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use async_trait::async_trait;
use clap::Args;
use fvm_shared::clock::ChainEpoch;

use ipc_api::subnet::{GenericToken, GenericTokenKind, PermissionMode};
use ipc_api::subnet::{Asset, AssetKind, PermissionMode};
use ipc_api::subnet_id::SubnetID;

use crate::commands::get_ipc_provider;
Expand Down Expand Up @@ -37,12 +37,6 @@ impl CreateSubnet {
let supply_source = parse_supply_source(arguments)?;
let collateral_source = parse_collateral_source(arguments)?;

let raw_addr = arguments
.validator_gater
.clone()
.unwrap_or(ZERO_ADDRESS.to_string());
let validator_gater = require_fil_addr_from_str(&raw_addr)?;

let raw_addr = arguments
.validator_gater
.clone()
Expand Down Expand Up @@ -70,21 +64,21 @@ impl CreateSubnet {
}
}

fn parse_supply_source(arguments: &CreateSubnetArgs) -> anyhow::Result<GenericToken> {
fn parse_supply_source(arguments: &CreateSubnetArgs) -> anyhow::Result<Asset> {
let token_address = if let Some(addr) = &arguments.supply_source_address {
Some(require_fil_addr_from_str(addr)?)
} else {
None
};
Ok(GenericToken {
Ok(Asset {
kind: arguments.supply_source_kind,
token_address,
})
}

fn parse_collateral_source(arguments: &CreateSubnetArgs) -> anyhow::Result<GenericToken> {
fn parse_collateral_source(arguments: &CreateSubnetArgs) -> anyhow::Result<Asset> {
let Some(ref kind) = arguments.collateral_source_kind else {
return Ok(GenericToken::default());
return Ok(Asset::default());
};

let token_address = if let Some(addr) = &arguments.collateral_source_address {
Expand All @@ -93,7 +87,7 @@ fn parse_collateral_source(arguments: &CreateSubnetArgs) -> anyhow::Result<Gener
None
};

Ok(GenericToken {
Ok(Asset {
kind: *kind,
token_address,
})
Expand Down Expand Up @@ -156,11 +150,11 @@ pub struct CreateSubnetArgs {
#[arg(
long,
help = "The kind of supply source of a subnet on its parent subnet: native or erc20",
value_parser = GenericTokenKind::from_str,
value_parser = AssetKind::from_str,
)]
// TODO figure out a way to use a newtype + ValueEnum, or reference GenericTokenKind::VARIANTS to
// TODO figure out a way to use a newtype + ValueEnum, or reference AssetKind::VARIANTS to
// enumerate all variants
pub supply_source_kind: GenericTokenKind,
pub supply_source_kind: AssetKind,
#[arg(
long,
help = "The address of supply source of a subnet on its parent subnet. None if kind is native"
Expand All @@ -174,9 +168,9 @@ pub struct CreateSubnetArgs {
#[arg(
long,
help = "The kind of collateral source of a subnet on its parent subnet: native or erc20",
value_parser = GenericTokenKind::from_str,
value_parser = AssetKind::from_str,
)]
pub collateral_source_kind: Option<GenericTokenKind>,
pub collateral_source_kind: Option<AssetKind>,
#[arg(
long,
help = "The address of collateral source of a subnet on its parent subnet. None if kind is native"
Expand Down
6 changes: 3 additions & 3 deletions ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fvm_shared::{
use ipc_api::checkpoint::{BottomUpCheckpointBundle, QuorumReachedEvent};
use ipc_api::evm::payload_to_evm_address;
use ipc_api::staking::{StakingChangeRequest, ValidatorInfo};
use ipc_api::subnet::{GenericToken, PermissionMode};
use ipc_api::subnet::{Asset, PermissionMode};
use ipc_api::{
cross::IpcEnvelope,
subnet::{ConsensusType, ConstructParams},
Expand Down Expand Up @@ -253,8 +253,8 @@ impl IpcProvider {
active_validators_limit: u16,
min_cross_msg_fee: TokenAmount,
permission_mode: PermissionMode,
supply_source: GenericToken,
collateral_source: GenericToken,
supply_source: Asset,
collateral_source: Asset,
validator_gater: Address,
) -> anyhow::Result<Address> {
let conn = self.get_connection(&parent)?;
Expand Down
30 changes: 13 additions & 17 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use reqwest::header::HeaderValue;
use reqwest::Client;
use std::net::{IpAddr, SocketAddr};

use ipc_api::subnet::{GenericToken, GenericTokenKind, PermissionMode};
use ipc_api::subnet::{Asset, AssetKind, PermissionMode};
use ipc_api::{eth_to_fil_amount, ethers_address_to_fil_address};

use crate::config::subnet::SubnetConfig;
Expand Down Expand Up @@ -275,10 +275,8 @@ impl SubnetManager for EthSubnetManager {
active_validators_limit: params.active_validators_limit,
power_scale: 3,
permission_mode: params.permission_mode as u8,
supply_source: register_subnet_facet::GenericToken::try_from(params.supply_source)?,
collateral_source: register_subnet_facet::GenericToken::try_from(
params.collateral_source,
)?,
supply_source: register_subnet_facet::Asset::try_from(params.supply_source)?,
collateral_source: register_subnet_facet::Asset::try_from(params.collateral_source)?,
validator_gater: payload_to_evm_address(params.validator_gater.payload())?,
};

Expand Down Expand Up @@ -590,7 +588,7 @@ impl SubnetManager for EthSubnetManager {
let signer = Arc::new(self.get_signer(&from)?);

let subnet_supply_source = self.get_subnet_supply_source(&subnet).await?;
if subnet_supply_source.kind != GenericTokenKind::ERC20 {
if subnet_supply_source.kind != AssetKind::ERC20 {
return Err(anyhow!("Invalid operation: Expected the subnet's supply source to be ERC20, but found a different kind."));
}

Expand Down Expand Up @@ -770,24 +768,24 @@ impl SubnetManager for EthSubnetManager {
Ok(commit_sha)
}

async fn get_subnet_supply_source(&self, subnet: &SubnetID) -> Result<GenericToken> {
async fn get_subnet_supply_source(&self, subnet: &SubnetID) -> Result<Asset> {
let address = contract_address_from_subnet(subnet)?;
let contract = subnet_actor_getter_facet::SubnetActorGetterFacet::new(
address,
Arc::new(self.ipc_contract_info.provider.clone()),
);
let raw = contract.collateral_source().call().await?;
Ok(GenericToken::try_from(raw)?)
Ok(Asset::try_from(raw)?)
}

async fn get_subnet_collateral_source(&self, subnet: &SubnetID) -> Result<GenericToken> {
async fn get_subnet_collateral_source(&self, subnet: &SubnetID) -> Result<Asset> {
let address = contract_address_from_subnet(subnet)?;
let contract = subnet_actor_getter_facet::SubnetActorGetterFacet::new(
address,
Arc::new(self.ipc_contract_info.provider.clone()),
);
let raw = contract.collateral_source().call().await?;
Ok(GenericToken::try_from(raw)?)
Ok(Asset::try_from(raw)?)
}

async fn get_genesis_info(&self, subnet: &SubnetID) -> Result<SubnetGenesisInfo> {
Expand Down Expand Up @@ -816,8 +814,8 @@ impl SubnetManager for EthSubnetManager {
genesis_balances: into_genesis_balance_map(genesis_balances.0, genesis_balances.1)?,
// TODO: fixme https://github.com/consensus-shipyard/ipc-monorepo/issues/496
permission_mode: PermissionMode::Collateral,
supply_source: GenericToken {
kind: GenericTokenKind::Native,
supply_source: Asset {
kind: AssetKind::Native,
token_address: None,
},
})
Expand Down Expand Up @@ -1058,11 +1056,9 @@ impl EthSubnetManager {
let collateral_source = self.get_subnet_collateral_source(subnet).await?;

match (supply_source.kind, collateral_source.kind) {
(GenericTokenKind::Native, GenericTokenKind::Native) => {
_ = txn.tx.set_value(balance + collateral)
}
(GenericTokenKind::Native, GenericTokenKind::ERC20) => _ = txn.tx.set_value(balance),
(GenericTokenKind::ERC20, GenericTokenKind::Native) => _ = txn.tx.set_value(collateral),
(AssetKind::Native, AssetKind::Native) => _ = txn.tx.set_value(balance + collateral),
(AssetKind::Native, AssetKind::ERC20) => _ = txn.tx.set_value(balance),
(AssetKind::ERC20, AssetKind::Native) => _ = txn.tx.set_value(collateral),
_ => {}
}
Ok(txn)
Expand Down
8 changes: 4 additions & 4 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ipc_api::checkpoint::{
};
use ipc_api::cross::IpcEnvelope;
use ipc_api::staking::{StakingChangeRequest, ValidatorInfo};
use ipc_api::subnet::{ConstructParams, GenericToken, PermissionMode};
use ipc_api::subnet::{Asset, ConstructParams, PermissionMode};
use ipc_api::subnet_id::SubnetID;
use ipc_api::validator::Validator;

Expand Down Expand Up @@ -163,10 +163,10 @@ pub trait SubnetManager: Send + Sync + TopDownFinalityQuery + BottomUpCheckpoint
async fn get_commit_sha(&self) -> Result<[u8; 32]>;

/// Gets the subnet supply source
async fn get_subnet_supply_source(&self, subnet: &SubnetID) -> Result<GenericToken>;
async fn get_subnet_supply_source(&self, subnet: &SubnetID) -> Result<Asset>;

/// Gets the subnet collateral source
async fn get_subnet_collateral_source(&self, subnet: &SubnetID) -> Result<GenericToken>;
async fn get_subnet_collateral_source(&self, subnet: &SubnetID) -> Result<Asset>;

/// Gets the genesis information required to bootstrap a child subnet
async fn get_genesis_info(&self, subnet: &SubnetID) -> Result<SubnetGenesisInfo>;
Expand Down Expand Up @@ -209,7 +209,7 @@ pub struct SubnetGenesisInfo {
pub validators: Vec<Validator>,
pub genesis_balances: BTreeMap<Address, TokenAmount>,
pub permission_mode: PermissionMode,
pub supply_source: GenericToken,
pub supply_source: Asset,
}

/// The generic payload that returns the block hash of the data returning block with the actual
Expand Down

0 comments on commit 7f2cb84

Please sign in to comment.