From 65ac374a6e8acb33483032111857e283ee38abf2 Mon Sep 17 00:00:00 2001 From: raulk Date: Sat, 16 Mar 2024 23:24:13 +0000 Subject: [PATCH] cleanup: rename type, implement Arbitrary, remove comments, refactor. --- fendermint/app/src/cmd/genesis.rs | 6 +-- .../testing/materializer/src/docker/mod.rs | 4 +- fendermint/vm/actor_interface/src/ipc.rs | 4 +- fendermint/vm/genesis/src/arb.rs | 14 ++++-- fendermint/vm/genesis/src/lib.rs | 25 +++------- ipc/api/src/validator.rs | 46 ------------------- 6 files changed, 23 insertions(+), 76 deletions(-) diff --git a/fendermint/app/src/cmd/genesis.rs b/fendermint/app/src/cmd/genesis.rs index e357e9a0a..be5f144d5 100644 --- a/fendermint/app/src/cmd/genesis.rs +++ b/fendermint/app/src/cmd/genesis.rs @@ -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, }; @@ -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(), }, @@ -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, }, diff --git a/fendermint/testing/materializer/src/docker/mod.rs b/fendermint/testing/materializer/src/docker/mod.rs index 1d3a66455..fef098eed 100644 --- a/fendermint/testing/materializer/src/docker/mod.rs +++ b/fendermint/testing/materializer/src/docker/mod.rs @@ -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}; @@ -431,7 +431,7 @@ impl Materializer 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(), }, diff --git a/fendermint/vm/actor_interface/src/ipc.rs b/fendermint/vm/actor_interface/src/ipc.rs index 81881416a..e44ea9f5e 100644 --- a/fendermint/vm/actor_interface/src/ipc.rs +++ b/fendermint/vm/actor_interface/src/ipc.rs @@ -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; @@ -329,7 +329,7 @@ pub mod gateway { impl ConstructorParameters { pub fn new( params: GatewayParams, - validators: Vec>, + validators: Vec>, ) -> anyhow::Result { // Every validator has an Ethereum address. let validators = validators diff --git a/fendermint/vm/genesis/src/arb.rs b/fendermint/vm/genesis/src/arb.rs index 99128cd0d..39ef01079 100644 --- a/fendermint/vm/genesis/src/arb.rs +++ b/fendermint/vm/genesis/src/arb.rs @@ -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}; @@ -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), + } + } +} \ No newline at end of file diff --git a/fendermint/vm/genesis/src/lib.rs b/fendermint/vm/genesis/src/lib.rs index d6856aff9..8f2ee8960 100644 --- a/fendermint/vm/genesis/src/lib.rs +++ b/fendermint/vm/genesis/src/lib.rs @@ -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>, + pub validators: Vec>, pub accounts: Vec, /// The custom eam permission mode that controls who can deploy contracts pub eam_permission_mode: PermissionMode, @@ -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), diff --git a/ipc/api/src/validator.rs b/ipc/api/src/validator.rs index db9c493fd..953c4e4ba 100644 --- a/ipc/api/src/validator.rs +++ b/ipc/api/src/validator.rs @@ -51,49 +51,3 @@ pub fn vec_try_from>(values: Vec) -> anyhow::Result, Ok(out) } -// #[derive(Clone, Debug, PartialEq, Eq)] -// pub struct Validator { -// pub addr: Address, -// pub metadata: Vec, -// pub weight: TokenAmount, -// } -// -// impl TryFrom for subnet_actor_getter_facet::Validator { -// type Error = anyhow::Error; -// -// fn try_from(value: Validator) -> Result { -// 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, -// ) -> anyhow::Result> { -// let result: Result, _> = vals -// .into_iter() -// .map(|validator| validator.try_into()) -// .collect(); -// -// result -// } -// -// pub fn from_contract_validators( -// vals: Vec, -// ) -> anyhow::Result> { -// let result: Result, _> = 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 -// }