Skip to content

Commit

Permalink
cleanup: rename type, implement Arbitrary, remove comments, refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Mar 16, 2024
1 parent 0634811 commit 65ac374
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 76 deletions.
6 changes: 3 additions & 3 deletions fendermint/app/src/cmd/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::PathBuf;
use fendermint_vm_actor_interface::eam::EthAddress;
use fendermint_vm_core::{chainid, Timestamp};
use fendermint_vm_genesis::{
ipc, Account, Actor, ActorMeta, Genesis, LibStakingPower, Multisig, PermissionMode, SignerAddr,
ipc, Account, Actor, ActorMeta, Genesis, GenesisPower, Multisig, PermissionMode, SignerAddr,
Validator, ValidatorKey,
};

Expand Down Expand Up @@ -169,7 +169,7 @@ fn add_validator(genesis_file: &PathBuf, args: &GenesisAddValidatorArgs) -> anyh
}
let validator = Validator {
public_key: vk,
power: LibStakingPower {
power: GenesisPower {
collateral: args.collateral.clone(),
federated_power: args.federated_power.clone(),
},
Expand Down Expand Up @@ -335,7 +335,7 @@ async fn new_genesis_from_parent(
let pk = PublicKey::parse_slice(&v.metadata, None)?;
genesis.validators.push(Validator {
public_key: ValidatorKey(pk),
power: LibStakingPower {
power: GenesisPower {
collateral: v.collateral,
federated_power: v.federated_power,
},
Expand Down
4 changes: 2 additions & 2 deletions fendermint/testing/materializer/src/docker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use fendermint_vm_actor_interface::eam::EthAddress;
use fendermint_vm_core::{chainid, Timestamp};
use fendermint_vm_genesis::{
ipc::{GatewayParams, IpcParams},
Account, Actor, ActorMeta, Collateral, Genesis, LibStakingPower, SignerAddr, Validator,
Account, Actor, ActorMeta, Collateral, Genesis, GenesisPower, SignerAddr, Validator,
ValidatorKey,
};
use fvm_shared::{bigint::Zero, econ::TokenAmount, version::NetworkVersion};
Expand Down Expand Up @@ -431,7 +431,7 @@ impl Materializer<DockerMaterials> for DockerMaterializer {
.into_iter()
.map(|(v, c)| Validator {
public_key: ValidatorKey(*v.public_key()),
power: LibStakingPower {
power: GenesisPower {
collateral: c.0,
federated_power: TokenAmount::zero(),
},
Expand Down
4 changes: 2 additions & 2 deletions fendermint/vm/actor_interface/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub mod gateway {
use ethers::contract::{EthAbiCodec, EthAbiType};
use ethers::core::types::{Bytes, H160, U256};
use fendermint_vm_genesis::ipc::GatewayParams;
use fendermint_vm_genesis::{LibStakingPower, Validator};
use fendermint_vm_genesis::{GenesisPower, Validator};
use fvm_shared::address::Error as AddressError;
use fvm_shared::econ::TokenAmount;

Expand Down Expand Up @@ -329,7 +329,7 @@ pub mod gateway {
impl ConstructorParameters {
pub fn new(
params: GatewayParams,
validators: Vec<Validator<LibStakingPower>>,
validators: Vec<Validator<GenesisPower>>,
) -> anyhow::Result<Self> {
// Every validator has an Ethereum address.
let validators = validators
Expand Down
14 changes: 10 additions & 4 deletions fendermint/vm/genesis/src/arb.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use crate::{
ipc, Account, Actor, ActorMeta, Collateral, Genesis, Multisig, PermissionMode, Power,
SignerAddr, Validator, ValidatorKey,
};
use crate::{ipc, Account, Actor, ActorMeta, Collateral, Genesis, Multisig, PermissionMode, Power, SignerAddr, Validator, ValidatorKey, GenesisPower};
use cid::multihash::MultihashDigest;
use fendermint_crypto::SecretKey;
use fendermint_testing::arb::{ArbSubnetID, ArbTokenAmount};
Expand Down Expand Up @@ -137,3 +134,12 @@ impl Arbitrary for ipc::IpcParams {
}
}
}

impl Arbitrary for GenesisPower {
fn arbitrary(g: &mut Gen) -> Self {
Self {
collateral: u64::arbitrary(g).unwrap().saturating_add(1),
federated_power: u64::arbitrary(g).unwrap().saturating_add(1),
}
}
}
25 changes: 6 additions & 19 deletions fendermint/vm/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Genesis {
/// Validators in genesis are given with their FIL collateral to maintain the
/// highest possible fidelity when we are deriving a genesis file in IPC,
/// where the parent subnet tracks collateral.
pub validators: Vec<Validator<LibStakingPower>>,
pub validators: Vec<Validator<GenesisPower>>,
pub accounts: Vec<Actor>,
/// The custom eam permission mode that controls who can deploy contracts
pub eam_permission_mode: PermissionMode,
Expand Down Expand Up @@ -99,33 +99,20 @@ pub struct Actor {
/// federated power assigned by the super admin/owner of the subnet
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibStakingPower {
pub struct GenesisPower {
#[serde_as(as = "IsHumanReadable")]
pub collateral: TokenAmount,
#[serde_as(as = "IsHumanReadable")]
pub federated_power: TokenAmount,
}

impl LibStakingPower {
/// Convert from [LibStakingPower] to [Power] by specifying the number of significant
/// decimal places per FIL that grant 1 power.
///
/// For example:
/// * with 3 decimal places, we get 1 power per milli FIL: 0.001 FIL => 1 power
/// * with 0 decimal places, we get 1 power per whole FIL: 1 FIL => 1 power
pub fn into_power(self: LibStakingPower, scale: PowerScale) -> Power {
let atto_per_power = Collateral::atto_per_power(scale);
let atto = self.federated_power.atto() + self.collateral.atto();
// Rounding away from zero, so with little collateral (e.g. in testing)
// we don't end up with everyone having 0 power and then being unable
// to produce a checkpoint because the threshold is 0.
let power = atto.div_ceil(&atto_per_power);
let power = power.min(BigInt::from(u64::MAX));
Power(power.try_into().expect("clipped to u64::MAX"))
impl GenesisPower {
pub fn into_power(self: GenesisPower, scale: PowerScale) -> Power {
Collateral(self.federated_power + self.collateral).into_power(scale)
}
}

impl Default for LibStakingPower {
impl Default for GenesisPower {
fn default() -> Self {
Self {
collateral: TokenAmount::from_atto(0),
Expand Down
46 changes: 0 additions & 46 deletions ipc/api/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,3 @@ pub fn vec_try_from<T, W: TryFrom<T>>(values: Vec<T>) -> anyhow::Result<Vec<W>,
Ok(out)
}

// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Validator {
// pub addr: Address,
// pub metadata: Vec<u8>,
// pub weight: TokenAmount,
// }
//
// impl TryFrom<Validator> for subnet_actor_getter_facet::Validator {
// type Error = anyhow::Error;
//
// fn try_from(value: Validator) -> Result<Self, Self::Error> {
// Ok(subnet_actor_getter_facet::Validator {
// addr: payload_to_evm_address(value.addr.payload())?,
// weight: fil_to_eth_amount(&value.weight)?,
// metadata: ethers::core::types::Bytes::from(value.metadata),
// })
// }
// }
//
// pub fn into_contract_validators(
// vals: Vec<Validator>,
// ) -> anyhow::Result<Vec<subnet_actor_getter_facet::Validator>> {
// let result: Result<Vec<subnet_actor_getter_facet::Validator>, _> = vals
// .into_iter()
// .map(|validator| validator.try_into())
// .collect();
//
// result
// }
//
// pub fn from_contract_validators(
// vals: Vec<subnet_actor_getter_facet::Validator>,
// ) -> anyhow::Result<Vec<Validator>> {
// let result: Result<Vec<Validator>, _> = vals
// .into_iter()
// .map(|validator| {
// Ok(Validator {
// addr: ethers_address_to_fil_address(&validator.addr)?,
// weight: eth_to_fil_amount(&validator.weight)?,
// metadata: validator.metadata.to_vec(),
// })
// })
// .collect();
//
// result
// }

0 comments on commit 65ac374

Please sign in to comment.