diff --git a/crates/iota-framework/packages/iota-system/sources/genesis.move b/crates/iota-framework/packages/iota-system/sources/genesis.move index d3d641d8f22..32ad83aeabf 100644 --- a/crates/iota-framework/packages/iota-system/sources/genesis.move +++ b/crates/iota-framework/packages/iota-system/sources/genesis.move @@ -10,7 +10,7 @@ module iota_system::genesis { use iota::iota::{Self, IotaTreasuryCap}; use iota::timelock::SystemTimelockCap; use iota_system::iota_system; - use iota_system::validator::{Self, Validator}; + use iota_system::validator::{Self, ValidatorV1}; use iota_system::validator_set; use iota_system::iota_system_state_inner; use iota_system::timelocked_staking; @@ -43,7 +43,7 @@ module iota_system::genesis { chain_start_timestamp_ms: u64, epoch_duration_ms: u64, - // Validator committee parameters + // ValidatorV1 committee parameters max_validator_count: u64, min_validator_joining_stake: u64, validator_low_stake_threshold: u64, @@ -101,7 +101,7 @@ module iota_system::genesis { let storage_fund = balance::zero(); - // Create all the `Validator` structs + // Create all the `ValidatorV1` structs let mut validators = vector[]; let count = genesis_validators.length(); let mut i = 0; @@ -169,7 +169,7 @@ module iota_system::genesis { let system_parameters = iota_system_state_inner::create_system_parameters( genesis_chain_parameters.epoch_duration_ms, - // Validator committee parameters + // ValidatorV1 committee parameters genesis_chain_parameters.max_validator_count, genesis_chain_parameters.min_validator_joining_stake, genesis_chain_parameters.validator_low_stake_threshold, @@ -195,7 +195,7 @@ module iota_system::genesis { fun allocate_tokens( iota_treasury_cap: &mut IotaTreasuryCap, mut allocations: vector, - validators: &mut vector, + validators: &mut vector, timelock_genesis_label: Option, ctx: &mut TxContext, ) { @@ -242,7 +242,7 @@ module iota_system::genesis { allocations.destroy_empty(); } - fun activate_validators(validators: &mut vector) { + fun activate_validators(validators: &mut vector) { // Activate all genesis validators let count = validators.length(); let mut i = 0; diff --git a/crates/iota-framework/packages/iota-system/sources/iota_system.move b/crates/iota-framework/packages/iota-system/sources/iota_system.move index be1cfe715ce..7aadc5ed292 100644 --- a/crates/iota-framework/packages/iota-system/sources/iota_system.move +++ b/crates/iota-framework/packages/iota-system/sources/iota_system.move @@ -3,24 +3,24 @@ // SPDX-License-Identifier: Apache-2.0 /// Iota System State Type Upgrade Guide -/// `IotaSystemState` is a thin wrapper around `IotaSystemStateInner` that provides a versioned interface. -/// The `IotaSystemState` object has a fixed ID 0x5, and the `IotaSystemStateInner` object is stored as a dynamic field. -/// There are a few different ways to upgrade the `IotaSystemStateInner` type: +/// `IotaSystemState` is a thin wrapper around `IotaSystemStateV1` that provides a versioned interface. +/// The `IotaSystemState` object has a fixed ID 0x5, and the `IotaSystemStateV1` object is stored as a dynamic field. +/// There are a few different ways to upgrade the `IotaSystemStateV1` type: /// /// The simplest and one that doesn't involve a real upgrade is to just add dynamic fields to the `extra_fields` field -/// of `IotaSystemStateInner` or any of its sub type. This is useful when we are in a rush, or making a small change, +/// of `IotaSystemStateV1` or any of its sub type. This is useful when we are in a rush, or making a small change, /// or still experimenting a new field. /// -/// To properly upgrade the `IotaSystemStateInner` type, we need to ship a new framework that does the following: -/// 1. Define a new `IotaSystemStateInner`type (e.g. `IotaSystemStateInnerV2`). -/// 2. Define a data migration function that migrates the old `IotaSystemStateInner` to the new one (i.e. IotaSystemStateInnerV2). -/// 3. Replace all uses of `IotaSystemStateInner` with `IotaSystemStateInnerV2` in both iota_system.move and iota_system_state_inner.move, +/// To properly upgrade the `IotaSystemStateV1` type, we need to ship a new framework that does the following: +/// 1. Define a new `IotaSystemState`type (e.g. `IotaSystemStateV2`). +/// 2. Define a data migration function that migrates the old (e.g. `IotaSystemStateV1`) to the new one (e.g. `IotaSystemStateV2`). +/// 3. Replace all uses of `IotaSystemStateV1` with `IotaSystemStateV2` in both iota_system.move and iota_system_state_inner.move, /// with the exception of the `iota_system_state_inner::create` function, which should always return the genesis type. /// 4. Inside `load_inner_maybe_upgrade` function, check the current version in the wrapper, and if it's not the latest version, /// call the data migration function to upgrade the inner object. Make sure to also update the version in the wrapper. /// A detailed example can be found in iota/tests/framework_upgrades/mock_iota_systems/shallow_upgrade. /// Along with the Move change, we also need to update the Rust code to support the new type. This includes: -/// 1. Define a new `IotaSystemStateInner` struct type that matches the new Move type, and implement the IotaSystemStateTrait. +/// 1. Define a new `IotaSystemState` struct type that matches the new Move type, and implement the `IotaSystemStateTrait`. /// 2. Update the `IotaSystemState` struct to include the new version as a new enum variant. /// 3. Update the `get_iota_system_state` function to handle the new version. /// To test that the upgrade will be successful, we need to modify `iota_system_state_production_upgrade_test` test in @@ -29,33 +29,33 @@ /// /// To upgrade Validator type, besides everything above, we also need to: /// 1. Define a new Validator type (e.g. ValidatorV2). -/// 2. Define a data migration function that migrates the old Validator to the new one (i.e. ValidatorV2). -/// 3. Replace all uses of Validator with ValidatorV2 except the genesis creation function. +/// 2. Define a data migration function that migrates the old ValidatorV1 to the new one (i.e. ValidatorV2). +/// 3. Replace all uses of ValidatorV1 with ValidatorV2 except the genesis creation function. /// 4. In validator_wrapper::upgrade_to_latest, check the current version in the wrapper, and if it's not the latest version, /// call the data migration function to upgrade it. /// In Rust, we also need to add a new case in `get_validator_from_table`. -/// Note that it is possible to upgrade IotaSystemStateInner without upgrading Validator, but not the other way around. -/// And when we only upgrade IotaSystemStateInner, the version of Validator in the wrapper will not be updated, and hence may become -/// inconsistent with the version of IotaSystemStateInner. This is fine as long as we don't use the Validator version to determine -/// the IotaSystemStateInner version, or vice versa. +/// Note that it is possible to upgrade IotaSystemStateV1 without upgrading ValidatorV1, but not the other way around. +/// And when we only upgrade IotaSystemStateV1, the version of ValidatorV1 in the wrapper will not be updated, and hence may become +/// inconsistent with the version of IotaSystemStateV1. This is fine as long as we don't use the ValidatorV1 version to determine +/// the IotaSystemStateV1 version, or vice versa. module iota_system::iota_system { use iota::balance::Balance; use iota::coin::Coin; - use iota_system::staking_pool::StakedIota; + use iota_system::staking_pool::StakedIotaV1; use iota::iota::{IOTA, IotaTreasuryCap}; use iota::table::Table; use iota::timelock::SystemTimelockCap; - use iota_system::validator::Validator; + use iota_system::validator::ValidatorV1; use iota_system::validator_cap::UnverifiedValidatorOperationCap; - use iota_system::iota_system_state_inner::{Self, SystemParameters, IotaSystemStateInner, IotaSystemStateInnerV2}; - use iota_system::staking_pool::PoolTokenExchangeRate; + use iota_system::iota_system_state_inner::{Self, SystemParametersV1, IotaSystemStateV1}; + use iota_system::staking_pool::PoolTokenExchangeRateV1; use iota::dynamic_field; use iota::vec_map::VecMap; #[test_only] use iota::balance; - #[test_only] use iota_system::validator_set::ValidatorSet; + #[test_only] use iota_system::validator_set::ValidatorSetV1; #[test_only] use iota::vec_set::VecSet; public struct IotaSystemState has key { @@ -75,11 +75,11 @@ module iota_system::iota_system { public(package) fun create( id: UID, iota_treasury_cap: IotaTreasuryCap, - validators: vector, + validators: vector, storage_fund: Balance, protocol_version: u64, epoch_start_timestamp_ms: u64, - parameters: SystemParameters, + parameters: SystemParametersV1, system_timelock_cap: SystemTimelockCap, ctx: &mut TxContext, ) { @@ -242,7 +242,7 @@ module iota_system::iota_system { stake: Coin, validator_address: address, ctx: &mut TxContext, - ): StakedIota { + ): StakedIotaV1 { let self = load_system_state_mut(wrapper); self.request_add_stake(stake, validator_address, ctx) } @@ -263,7 +263,7 @@ module iota_system::iota_system { /// Withdraw stake from a validator's staking pool. public entry fun request_withdraw_stake( wrapper: &mut IotaSystemState, - staked_iota: StakedIota, + staked_iota: StakedIotaV1, ctx: &mut TxContext, ) { let withdrawn_stake = request_withdraw_stake_non_entry(wrapper, staked_iota, ctx); @@ -273,7 +273,7 @@ module iota_system::iota_system { /// Non-entry version of `request_withdraw_stake` that returns the withdrawn IOTA instead of transferring it to the sender. public fun request_withdraw_stake_non_entry( wrapper: &mut IotaSystemState, - staked_iota: StakedIota, + staked_iota: StakedIotaV1, ctx: &mut TxContext, ) : Balance { let self = load_system_state_mut(wrapper); @@ -514,7 +514,7 @@ module iota_system::iota_system { public fun pool_exchange_rates( wrapper: &mut IotaSystemState, pool_id: &ID - ): &Table { + ): &Table { let self = load_system_state_mut(wrapper); self.pool_exchange_rates(pool_id) } @@ -550,7 +550,7 @@ module iota_system::iota_system { ctx: &mut TxContext, ) : Balance { let self = load_system_state_mut(wrapper); - // Validator will make a special system call with sender set as 0x0. + // ValidatorV1 will make a special system call with sender set as 0x0. assert!(ctx.sender() == @0x0, ENotSystemAddress); let storage_rebate = self.advance_epoch( new_epoch, @@ -568,23 +568,16 @@ module iota_system::iota_system { storage_rebate } - fun load_system_state(self: &mut IotaSystemState): &IotaSystemStateInnerV2 { + fun load_system_state(self: &mut IotaSystemState): &IotaSystemStateV1 { load_inner_maybe_upgrade(self) } - fun load_system_state_mut(self: &mut IotaSystemState): &mut IotaSystemStateInnerV2 { + fun load_system_state_mut(self: &mut IotaSystemState): &mut IotaSystemStateV1 { load_inner_maybe_upgrade(self) } - fun load_inner_maybe_upgrade(self: &mut IotaSystemState): &mut IotaSystemStateInnerV2 { - if (self.version == 1) { - let v1: IotaSystemStateInner = dynamic_field::remove(&mut self.id, self.version); - let v2 = v1.v1_to_v2(); - self.version = 2; - dynamic_field::add(&mut self.id, self.version, v2); - }; - - let inner: &mut IotaSystemStateInnerV2 = dynamic_field::borrow_mut( + fun load_inner_maybe_upgrade(self: &mut IotaSystemState): &mut IotaSystemStateV1 { + let inner: &mut IotaSystemStateV1 = dynamic_field::borrow_mut( &mut self.id, self.version ); @@ -658,26 +651,26 @@ module iota_system::iota_system { #[test_only] /// Return the current validator set - public fun validators(wrapper: &mut IotaSystemState): &ValidatorSet { + public fun validators(wrapper: &mut IotaSystemState): &ValidatorSetV1 { let self = load_system_state(wrapper); self.validators() } #[test_only] /// Return the currently active validator by address - public fun active_validator_by_address(self: &mut IotaSystemState, validator_address: address): &Validator { + public fun active_validator_by_address(self: &mut IotaSystemState, validator_address: address): &ValidatorV1 { validators(self).get_active_validator_ref(validator_address) } #[test_only] /// Return the currently pending validator by address - public fun pending_validator_by_address(self: &mut IotaSystemState, validator_address: address): &Validator { + public fun pending_validator_by_address(self: &mut IotaSystemState, validator_address: address): &ValidatorV1 { validators(self).get_pending_validator_ref(validator_address) } #[test_only] /// Return the currently candidate validator by address - public fun candidate_validator_by_address(self: &mut IotaSystemState, validator_address: address): &Validator { + public fun candidate_validator_by_address(self: &mut IotaSystemState, validator_address: address): &ValidatorV1 { validators(self).get_candidate_validator_ref(validator_address) } diff --git a/crates/iota-framework/packages/iota-system/sources/iota_system_state_inner.move b/crates/iota-framework/packages/iota-system/sources/iota_system_state_inner.move index 62a4e8b60ea..ed99a079788 100644 --- a/crates/iota-framework/packages/iota-system/sources/iota_system_state_inner.move +++ b/crates/iota-framework/packages/iota-system/sources/iota_system_state_inner.move @@ -5,13 +5,13 @@ module iota_system::iota_system_state_inner { use iota::balance::{Self, Balance}; use iota::coin::Coin; - use iota_system::staking_pool::StakedIota; + use iota_system::staking_pool::StakedIotaV1; use iota::iota::{IOTA, IotaTreasuryCap}; - use iota_system::validator::{Self, Validator}; - use iota_system::validator_set::{Self, ValidatorSet}; + use iota_system::validator::{Self, ValidatorV1}; + use iota_system::validator_set::{Self, ValidatorSetV1}; use iota_system::validator_cap::{UnverifiedValidatorOperationCap, ValidatorOperationCap}; - use iota_system::storage_fund::{Self, StorageFund}; - use iota_system::staking_pool::PoolTokenExchangeRate; + use iota_system::storage_fund::{Self, StorageFundV1}; + use iota_system::staking_pool::PoolTokenExchangeRateV1; use iota::vec_map::{Self, VecMap}; use iota::vec_set::{Self, VecSet}; use iota::event; @@ -27,36 +27,7 @@ module iota_system::iota_system_state_inner { const SYSTEM_STATE_VERSION_V1: u64 = 1; /// A list of system config parameters. - public struct SystemParameters has store { - /// The duration of an epoch, in milliseconds. - epoch_duration_ms: u64, - - /// Maximum number of active validators at any moment. - /// We do not allow the number of validators in any epoch to go above this. - max_validator_count: u64, - - /// Lower-bound on the amount of stake required to become a validator. - min_validator_joining_stake: u64, - - /// Validators with stake amount below `validator_low_stake_threshold` are considered to - /// have low stake and will be escorted out of the validator set after being below this - /// threshold for more than `validator_low_stake_grace_period` number of epochs. - validator_low_stake_threshold: u64, - - /// Validators with stake below `validator_very_low_stake_threshold` will be removed - /// immediately at epoch change, no grace period. - validator_very_low_stake_threshold: u64, - - /// A validator can have stake below `validator_low_stake_threshold` - /// for this many epochs before being kicked out. - validator_low_stake_grace_period: u64, - - /// Any extra fields that's not defined statically. - extra_fields: Bag, - } - - /// Added min_validator_count. - public struct SystemParametersV2 has store { + public struct SystemParametersV1 has store { /// The duration of an epoch, in milliseconds. epoch_duration_ms: u64, @@ -88,71 +59,23 @@ module iota_system::iota_system_state_inner { } /// The top-level object containing all information of the Iota system. - public struct IotaSystemStateInner has store { + public struct IotaSystemStateV1 has store { /// The current epoch ID, starting from 0. epoch: u64, /// The current protocol version, starting from 1. protocol_version: u64, /// The current version of the system state data structure type. /// This is always the same as IotaSystemState.version. Keeping a copy here so that - /// we know what version it is by inspecting IotaSystemStateInner as well. + /// we know what version it is by inspecting IotaSystemStateV1 as well. system_state_version: u64, /// The IOTA's TreasuryCap. iota_treasury_cap: IotaTreasuryCap, /// Contains all information about the validators. - validators: ValidatorSet, + validators: ValidatorSetV1, /// The storage fund. - storage_fund: StorageFund, + storage_fund: StorageFundV1, /// A list of system config parameters. - parameters: SystemParameters, - /// The reference gas price for the current epoch. - reference_gas_price: u64, - /// A map storing the records of validator reporting each other. - /// There is an entry in the map for each validator that has been reported - /// at least once. The entry VecSet contains all the validators that reported - /// them. If a validator has never been reported they don't have an entry in this map. - /// This map persists across epoch: a peer continues being in a reported state until the - /// reporter doesn't explicitly remove their report. - /// Note that in case we want to support validator address change in future, - /// the reports should be based on validator ids - validator_report_records: VecMap>, - - /// Whether the system is running in a downgraded safe mode due to a non-recoverable bug. - /// This is set whenever we failed to execute advance_epoch, and ended up executing advance_epoch_safe_mode. - /// It can be reset once we are able to successfully execute advance_epoch. - /// The rest of the fields starting with `safe_mode_` are accmulated during safe mode - /// when advance_epoch_safe_mode is executed. They will eventually be processed once we - /// are out of safe mode. - safe_mode: bool, - safe_mode_storage_charges: Balance, - safe_mode_computation_rewards: Balance, - safe_mode_storage_rebates: u64, - safe_mode_non_refundable_storage_fee: u64, - - /// Unix timestamp of the current epoch start - epoch_start_timestamp_ms: u64, - /// Any extra fields that's not defined statically. - extra_fields: Bag, - } - - /// Uses SystemParametersV2 as the parameters. - public struct IotaSystemStateInnerV2 has store { - /// The current epoch ID, starting from 0. - epoch: u64, - /// The current protocol version, starting from 1. - protocol_version: u64, - /// The current version of the system state data structure type. - /// This is always the same as IotaSystemState.version. Keeping a copy here so that - /// we know what version it is by inspecting IotaSystemStateInner as well. - system_state_version: u64, - /// The IOTA's TreasuryCap. - iota_treasury_cap: IotaTreasuryCap, - /// Contains all information about the validators. - validators: ValidatorSet, - /// The storage fund. - storage_fund: StorageFund, - /// A list of system config parameters. - parameters: SystemParametersV2, + parameters: SystemParametersV1, /// The reference gas price for the current epoch. reference_gas_price: u64, /// A map storing the records of validator reporting each other. @@ -185,7 +108,7 @@ module iota_system::iota_system_state_inner { /// Event containing system-level epoch information, emitted during /// the epoch advancement transaction. - public struct SystemEpochInfoEvent has copy, drop { + public struct SystemEpochInfoEventV1 has copy, drop { epoch: u64, protocol_version: u64, reference_gas_price: u64, @@ -218,17 +141,17 @@ module iota_system::iota_system_state_inner { /// This function will be called only once in genesis. public(package) fun create( iota_treasury_cap: IotaTreasuryCap, - validators: vector, + validators: vector, initial_storage_fund: Balance, protocol_version: u64, epoch_start_timestamp_ms: u64, - parameters: SystemParameters, + parameters: SystemParametersV1, ctx: &mut TxContext, - ): IotaSystemStateInner { + ): IotaSystemStateV1 { let validators = validator_set::new(validators, ctx); let reference_gas_price = validators.derive_reference_gas_price(); // This type is fixed as it's created at genesis. It should not be updated during type upgrade. - let system_state = IotaSystemStateInner { + let system_state = IotaSystemStateV1 { epoch: 0, protocol_version, system_state_version: genesis_system_state_version(), @@ -252,16 +175,17 @@ module iota_system::iota_system_state_inner { public(package) fun create_system_parameters( epoch_duration_ms: u64, - // Validator committee parameters + // ValidatorV1 committee parameters max_validator_count: u64, min_validator_joining_stake: u64, validator_low_stake_threshold: u64, validator_very_low_stake_threshold: u64, validator_low_stake_grace_period: u64, ctx: &mut TxContext, - ): SystemParameters { - SystemParameters { + ): SystemParametersV1 { + SystemParametersV1 { epoch_duration_ms, + min_validator_count: 4, max_validator_count, min_validator_joining_stake, validator_low_stake_threshold, @@ -271,63 +195,6 @@ module iota_system::iota_system_state_inner { } } - public(package) fun v1_to_v2(self: IotaSystemStateInner): IotaSystemStateInnerV2 { - let IotaSystemStateInner { - epoch, - protocol_version, - system_state_version: _, - iota_treasury_cap, - validators, - storage_fund, - parameters, - reference_gas_price, - validator_report_records, - safe_mode, - safe_mode_storage_charges, - safe_mode_computation_rewards, - safe_mode_storage_rebates, - safe_mode_non_refundable_storage_fee, - epoch_start_timestamp_ms, - extra_fields: state_extra_fields, - } = self; - let SystemParameters { - epoch_duration_ms, - max_validator_count, - min_validator_joining_stake, - validator_low_stake_threshold, - validator_very_low_stake_threshold, - validator_low_stake_grace_period, - extra_fields: param_extra_fields, - } = parameters; - IotaSystemStateInnerV2 { - epoch, - protocol_version, - system_state_version: 2, - iota_treasury_cap, - validators, - storage_fund, - parameters: SystemParametersV2 { - epoch_duration_ms, - min_validator_count: 4, - max_validator_count, - min_validator_joining_stake, - validator_low_stake_threshold, - validator_very_low_stake_threshold, - validator_low_stake_grace_period, - extra_fields: param_extra_fields, - }, - reference_gas_price, - validator_report_records, - safe_mode, - safe_mode_storage_charges, - safe_mode_computation_rewards, - safe_mode_storage_rebates, - safe_mode_non_refundable_storage_fee, - epoch_start_timestamp_ms, - extra_fields: state_extra_fields - } - } - // ==== public(package) functions ==== /// Can be called by anyone who wishes to become a validator candidate and starts accuring delegated @@ -337,7 +204,7 @@ module iota_system::iota_system_state_inner { /// Note: `proof_of_possession` MUST be a valid signature using iota_address and protocol_pubkey_bytes. /// To produce a valid PoP, run [fn test_proof_of_possession]. public(package) fun request_add_validator_candidate( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, pubkey_bytes: vector, network_pubkey_bytes: vector, worker_pubkey_bytes: vector, @@ -379,7 +246,7 @@ module iota_system::iota_system_state_inner { /// Called by a validator candidate to remove themselves from the candidacy. After this call /// their staking pool becomes deactivate. public(package) fun request_remove_validator_candidate( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, ctx: &mut TxContext, ) { self.validators.request_remove_validator_candidate(ctx); @@ -390,7 +257,7 @@ module iota_system::iota_system_state_inner { /// stake the validator has doesn't meet the min threshold, or if the number of new validators for the next /// epoch has already reached the maximum. public(package) fun request_add_validator( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, ctx: &TxContext, ) { assert!( @@ -407,7 +274,7 @@ module iota_system::iota_system_state_inner { /// At the end of the epoch, the `validator` object will be returned to the iota_address /// of the validator. public(package) fun request_remove_validator( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, ctx: &TxContext, ) { // Only check min validator condition if the current number of validators satisfy the constraint. @@ -426,7 +293,7 @@ module iota_system::iota_system_state_inner { /// A validator can call this function to submit a new gas price quote, to be /// used for the reference gas price calculation at the end of the epoch. public(package) fun request_set_gas_price( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, cap: &UnverifiedValidatorOperationCap, new_gas_price: u64, ) { @@ -439,7 +306,7 @@ module iota_system::iota_system_state_inner { /// This function is used to set new gas price for candidate validators public(package) fun set_candidate_validator_gas_price( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, cap: &UnverifiedValidatorOperationCap, new_gas_price: u64, ) { @@ -452,7 +319,7 @@ module iota_system::iota_system_state_inner { /// A validator can call this function to set a new commission rate, updated at the end of /// the epoch. public(package) fun request_set_commission_rate( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, new_commission_rate: u64, ctx: &TxContext, ) { @@ -464,7 +331,7 @@ module iota_system::iota_system_state_inner { /// This function is used to set new commission rate for candidate validators public(package) fun set_candidate_validator_commission_rate( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, new_commission_rate: u64, ctx: &TxContext, ) { @@ -474,11 +341,11 @@ module iota_system::iota_system_state_inner { /// Add stake to a validator's staking pool. public(package) fun request_add_stake( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, stake: Coin, validator_address: address, ctx: &mut TxContext, - ) : StakedIota { + ) : StakedIotaV1 { self.validators.request_add_stake( validator_address, stake.into_balance(), @@ -488,20 +355,20 @@ module iota_system::iota_system_state_inner { /// Add stake to a validator's staking pool using multiple coins. public(package) fun request_add_stake_mul_coin( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, stakes: vector>, stake_amount: option::Option, validator_address: address, ctx: &mut TxContext, - ) : StakedIota { + ) : StakedIotaV1 { let balance = extract_coin_balance(stakes, stake_amount, ctx); self.validators.request_add_stake(validator_address, balance, ctx) } /// Withdraw some portion of a stake from a validator's staking pool. public(package) fun request_withdraw_stake( - self: &mut IotaSystemStateInnerV2, - staked_iota: StakedIota, + self: &mut IotaSystemStateV1, + staked_iota: StakedIotaV1, ctx: &TxContext, ) : Balance { self.validators.request_withdraw_stake(staked_iota, ctx) @@ -514,7 +381,7 @@ module iota_system::iota_system_state_inner { /// 3. the cap object is still valid. /// This function is idempotent. public(package) fun report_validator( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, cap: &UnverifiedValidatorOperationCap, reportee_addr: address, ) { @@ -531,7 +398,7 @@ module iota_system::iota_system_state_inner { /// 2. the sender has not previously reported the `reportee_addr`, or /// 3. the cap is not valid public(package) fun undo_report_validator( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, cap: &UnverifiedValidatorOperationCap, reportee_addr: address, ) { @@ -578,7 +445,7 @@ module iota_system::iota_system_state_inner { /// Create a new `UnverifiedValidatorOperationCap`, transfer it to the /// validator and registers it. The original object is thus revoked. public(package) fun rotate_operation_cap( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, ctx: &mut TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx_including_candidates(ctx); @@ -587,7 +454,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's name. public(package) fun update_validator_name( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, name: vector, ctx: &TxContext, ) { @@ -598,7 +465,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's description public(package) fun update_validator_description( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, description: vector, ctx: &TxContext, ) { @@ -608,7 +475,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's image url public(package) fun update_validator_image_url( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, image_url: vector, ctx: &TxContext, ) { @@ -618,7 +485,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's project url public(package) fun update_validator_project_url( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, project_url: vector, ctx: &TxContext, ) { @@ -629,19 +496,19 @@ module iota_system::iota_system_state_inner { /// Update a validator's network address. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_network_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, network_address: vector, ctx: &TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx(ctx); validator.update_next_epoch_network_address(network_address); - let validator :&Validator = validator; // Force immutability for the following call + let validator :&ValidatorV1 = validator; // Force immutability for the following call self.validators.assert_no_pending_or_active_duplicates(validator); } /// Update candidate validator's network address. public(package) fun update_candidate_validator_network_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, network_address: vector, ctx: &TxContext, ) { @@ -652,19 +519,19 @@ module iota_system::iota_system_state_inner { /// Update a validator's p2p address. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_p2p_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, p2p_address: vector, ctx: &TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx(ctx); validator.update_next_epoch_p2p_address(p2p_address); - let validator :&Validator = validator; // Force immutability for the following call + let validator :&ValidatorV1 = validator; // Force immutability for the following call self.validators.assert_no_pending_or_active_duplicates(validator); } /// Update candidate validator's p2p address. public(package) fun update_candidate_validator_p2p_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, p2p_address: vector, ctx: &TxContext, ) { @@ -675,7 +542,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's narwhal primary address. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_primary_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, primary_address: vector, ctx: &TxContext, ) { @@ -685,7 +552,7 @@ module iota_system::iota_system_state_inner { /// Update candidate validator's narwhal primary address. public(package) fun update_candidate_validator_primary_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, primary_address: vector, ctx: &TxContext, ) { @@ -696,7 +563,7 @@ module iota_system::iota_system_state_inner { /// Update a validator's narwhal worker address. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_worker_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, worker_address: vector, ctx: &TxContext, ) { @@ -706,7 +573,7 @@ module iota_system::iota_system_state_inner { /// Update candidate validator's narwhal worker address. public(package) fun update_candidate_validator_worker_address( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, worker_address: vector, ctx: &TxContext, ) { @@ -717,20 +584,20 @@ module iota_system::iota_system_state_inner { /// Update a validator's public key of protocol key and proof of possession. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_protocol_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, protocol_pubkey: vector, proof_of_possession: vector, ctx: &TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx(ctx); validator.update_next_epoch_protocol_pubkey(protocol_pubkey, proof_of_possession); - let validator :&Validator = validator; // Force immutability for the following call + let validator :&ValidatorV1 = validator; // Force immutability for the following call self.validators.assert_no_pending_or_active_duplicates(validator); } /// Update candidate validator's public key of protocol key and proof of possession. public(package) fun update_candidate_validator_protocol_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, protocol_pubkey: vector, proof_of_possession: vector, ctx: &TxContext, @@ -742,19 +609,19 @@ module iota_system::iota_system_state_inner { /// Update a validator's public key of worker key. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_worker_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, worker_pubkey: vector, ctx: &TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx(ctx); validator.update_next_epoch_worker_pubkey(worker_pubkey); - let validator :&Validator = validator; // Force immutability for the following call + let validator :&ValidatorV1 = validator; // Force immutability for the following call self.validators.assert_no_pending_or_active_duplicates(validator); } /// Update candidate validator's public key of worker key. public(package) fun update_candidate_validator_worker_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, worker_pubkey: vector, ctx: &TxContext, ) { @@ -765,19 +632,19 @@ module iota_system::iota_system_state_inner { /// Update a validator's public key of network key. /// The change will only take effects starting from the next epoch. public(package) fun update_validator_next_epoch_network_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, network_pubkey: vector, ctx: &TxContext, ) { let validator = self.validators.get_validator_mut_with_ctx(ctx); validator.update_next_epoch_network_pubkey(network_pubkey); - let validator :&Validator = validator; // Force immutability for the following call + let validator :&ValidatorV1 = validator; // Force immutability for the following call self.validators.assert_no_pending_or_active_duplicates(validator); } /// Update candidate validator's public key of network key. public(package) fun update_candidate_validator_network_pubkey( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, network_pubkey: vector, ctx: &TxContext, ) { @@ -796,7 +663,7 @@ module iota_system::iota_system_state_inner { /// 5. Burn any leftover rewards. /// 6. Update all validators. public(package) fun advance_epoch( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, new_epoch: u64, next_protocol_version: u64, validator_target_reward: u64, @@ -874,7 +741,7 @@ module iota_system::iota_system_state_inner { ); event::emit( - SystemEpochInfoEvent { + SystemEpochInfoEventV1 { epoch: self.epoch, protocol_version: self.protocol_version, reference_gas_price: self.reference_gas_price, @@ -925,15 +792,15 @@ module iota_system::iota_system_state_inner { /// Return the current epoch number. Useful for applications that need a coarse-grained concept of time, /// since epochs are ever-increasing and epoch changes are intended to happen every 24 hours. - public(package) fun epoch(self: &IotaSystemStateInnerV2): u64 { + public(package) fun epoch(self: &IotaSystemStateV1): u64 { self.epoch } - public(package) fun protocol_version(self: &IotaSystemStateInnerV2): u64 { + public(package) fun protocol_version(self: &IotaSystemStateV1): u64 { self.protocol_version } - public(package) fun system_state_version(self: &IotaSystemStateInnerV2): u64 { + public(package) fun system_state_version(self: &IotaSystemStateV1): u64 { self.system_state_version } @@ -944,19 +811,19 @@ module iota_system::iota_system_state_inner { } /// Returns unix timestamp of the start of current epoch - public(package) fun epoch_start_timestamp_ms(self: &IotaSystemStateInnerV2): u64 { + public(package) fun epoch_start_timestamp_ms(self: &IotaSystemStateV1): u64 { self.epoch_start_timestamp_ms } /// Returns the total amount staked with `validator_addr`. /// Aborts if `validator_addr` is not an active validator. - public(package) fun validator_stake_amount(self: &IotaSystemStateInnerV2, validator_addr: address): u64 { + public(package) fun validator_stake_amount(self: &IotaSystemStateV1, validator_addr: address): u64 { self.validators.validator_total_stake_amount(validator_addr) } /// Returns the voting power for `validator_addr`. /// Aborts if `validator_addr` is not an active validator. - public(package) fun active_validator_voting_powers(self: &IotaSystemStateInnerV2): VecMap { + public(package) fun active_validator_voting_powers(self: &IotaSystemStateV1): VecMap { let mut active_validators = active_validator_addresses(self); let mut voting_powers = vec_map::empty(); while (!vector::is_empty(&active_validators)) { @@ -969,24 +836,24 @@ module iota_system::iota_system_state_inner { /// Returns the staking pool id of a given validator. /// Aborts if `validator_addr` is not an active validator. - public(package) fun validator_staking_pool_id(self: &IotaSystemStateInnerV2, validator_addr: address): ID { + public(package) fun validator_staking_pool_id(self: &IotaSystemStateV1, validator_addr: address): ID { self.validators.validator_staking_pool_id(validator_addr) } /// Returns reference to the staking pool mappings that map pool ids to active validator addresses - public(package) fun validator_staking_pool_mappings(self: &IotaSystemStateInnerV2): &Table { + public(package) fun validator_staking_pool_mappings(self: &IotaSystemStateV1): &Table { self.validators.staking_pool_mappings() } /// Returns the total iota supply. - public(package) fun get_total_iota_supply(self: &IotaSystemStateInnerV2): u64 { + public(package) fun get_total_iota_supply(self: &IotaSystemStateV1): u64 { self.iota_treasury_cap.total_supply() } /// Returns all the validators who are currently reporting `addr` - public(package) fun get_reporters_of(self: &IotaSystemStateInnerV2, addr: address): VecSet
{ + public(package) fun get_reporters_of(self: &IotaSystemStateV1, addr: address): VecSet
{ if (self.validator_report_records.contains(&addr)) { self.validator_report_records[&addr] @@ -995,23 +862,23 @@ module iota_system::iota_system_state_inner { } } - public(package) fun get_storage_fund_total_balance(self: &IotaSystemStateInnerV2): u64 { + public(package) fun get_storage_fund_total_balance(self: &IotaSystemStateV1): u64 { self.storage_fund.total_balance() } - public(package) fun get_storage_fund_object_rebates(self: &IotaSystemStateInnerV2): u64 { + public(package) fun get_storage_fund_object_rebates(self: &IotaSystemStateV1): u64 { self.storage_fund.total_object_storage_rebates() } public(package) fun pool_exchange_rates( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, pool_id: &ID - ): &Table { + ): &Table { let validators = &mut self.validators; validators.pool_exchange_rates(pool_id) } - public(package) fun active_validator_addresses(self: &IotaSystemStateInnerV2): vector
{ + public(package) fun active_validator_addresses(self: &IotaSystemStateV1): vector
{ let validator_set = &self.validators; validator_set.active_validator_addresses() } @@ -1041,36 +908,36 @@ module iota_system::iota_system_state_inner { #[test_only] /// Return the current validator set - public(package) fun validators(self: &IotaSystemStateInnerV2): &ValidatorSet { + public(package) fun validators(self: &IotaSystemStateV1): &ValidatorSetV1 { &self.validators } #[test_only] /// Return the currently active validator by address - public(package) fun active_validator_by_address(self: &IotaSystemStateInnerV2, validator_address: address): &Validator { + public(package) fun active_validator_by_address(self: &IotaSystemStateV1, validator_address: address): &ValidatorV1 { self.validators().get_active_validator_ref(validator_address) } #[test_only] /// Return the currently pending validator by address - public(package) fun pending_validator_by_address(self: &IotaSystemStateInnerV2, validator_address: address): &Validator { + public(package) fun pending_validator_by_address(self: &IotaSystemStateV1, validator_address: address): &ValidatorV1 { self.validators().get_pending_validator_ref(validator_address) } #[test_only] /// Return the currently candidate validator by address - public(package) fun candidate_validator_by_address(self: &IotaSystemStateInnerV2, validator_address: address): &Validator { + public(package) fun candidate_validator_by_address(self: &IotaSystemStateV1, validator_address: address): &ValidatorV1 { validators(self).get_candidate_validator_ref(validator_address) } #[test_only] - public(package) fun set_epoch_for_testing(self: &mut IotaSystemStateInnerV2, epoch_num: u64) { + public(package) fun set_epoch_for_testing(self: &mut IotaSystemStateV1, epoch_num: u64) { self.epoch = epoch_num } #[test_only] public(package) fun request_add_validator_for_testing( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, min_joining_stake_for_testing: u64, ctx: &TxContext, ) { @@ -1087,7 +954,7 @@ module iota_system::iota_system_state_inner { // in the process. #[test_only] public(package) fun request_add_validator_candidate_for_testing( - self: &mut IotaSystemStateInnerV2, + self: &mut IotaSystemStateV1, pubkey_bytes: vector, network_pubkey_bytes: vector, worker_pubkey_bytes: vector, diff --git a/crates/iota-framework/packages/iota-system/sources/staking_pool.move b/crates/iota-framework/packages/iota-system/sources/staking_pool.move index 6e41baee1f1..0839b880619 100644 --- a/crates/iota-framework/packages/iota-system/sources/staking_pool.move +++ b/crates/iota-framework/packages/iota-system/sources/staking_pool.move @@ -10,7 +10,7 @@ module iota_system::staking_pool { use iota::bag::Bag; use iota::bag; - /// StakedIota objects cannot be split to below this amount. + /// StakedIotaV1 objects cannot be split to below this amount. const MIN_STAKING_THRESHOLD: u64 = 1_000_000_000; // 1 IOTA const EInsufficientPoolTokenBalance: u64 = 0; @@ -34,7 +34,7 @@ module iota_system::staking_pool { const EStakedIotaBelowThreshold: u64 = 18; /// A staking pool embedded in each validator struct in the system state object. - public struct StakingPool has key, store { + public struct StakingPoolV1 has key, store { id: UID, /// The epoch at which this pool became active. /// The value is `None` if the pool is pre-active and `Some()` if active or inactive. @@ -43,7 +43,7 @@ module iota_system::staking_pool { /// `Some()` if in-active, and it was de-activated at epoch ``. deactivation_epoch: Option, /// The total number of IOTA tokens in this pool, including the IOTA in the rewards_pool, as well as in all the principal - /// in the `StakedIota` object, updated at epoch boundaries. + /// in the `StakedIotaV1` object, updated at epoch boundaries. iota_balance: u64, /// The epoch stake rewards will be added here at the end of each epoch. rewards_pool: Balance, @@ -52,7 +52,7 @@ module iota_system::staking_pool { /// Exchange rate history of previous epochs. Key is the epoch number. /// The entries start from the `activation_epoch` of this pool and contains exchange rates at the beginning of each epoch, /// i.e., right after the rewards for the previous epoch have been deposited into the pool. - exchange_rates: Table, + exchange_rates: Table, /// Pending stake amount for this epoch, emptied at epoch boundaries. pending_stake: u64, /// Pending stake withdrawn during the current epoch, emptied at epoch boundaries. @@ -65,13 +65,13 @@ module iota_system::staking_pool { } /// Struct representing the exchange rate of the stake pool token to IOTA. - public struct PoolTokenExchangeRate has store, copy, drop { + public struct PoolTokenExchangeRateV1 has store, copy, drop { iota_amount: u64, pool_token_amount: u64, } /// A self-custodial object holding the staked IOTA tokens. - public struct StakedIota has key, store { + public struct StakedIotaV1 has key, store { id: UID, /// ID of the staking pool we are staking with. pool_id: ID, @@ -84,9 +84,9 @@ module iota_system::staking_pool { // ==== initializer ==== /// Create a new, empty staking pool. - public(package) fun new(ctx: &mut TxContext) : StakingPool { + public(package) fun new(ctx: &mut TxContext) : StakingPoolV1 { let exchange_rates = table::new(ctx); - StakingPool { + StakingPoolV1 { id: object::new(ctx), activation_epoch: option::none(), deactivation_epoch: option::none(), @@ -105,15 +105,15 @@ module iota_system::staking_pool { /// Request to stake to a staking pool. The stake starts counting at the beginning of the next epoch, public(package) fun request_add_stake( - pool: &mut StakingPool, + pool: &mut StakingPoolV1, stake: Balance, stake_activation_epoch: u64, ctx: &mut TxContext - ) : StakedIota { + ) : StakedIotaV1 { let iota_amount = stake.value(); assert!(!is_inactive(pool), EDelegationToInactivePool); assert!(iota_amount > 0, EDelegationOfZeroIota); - let staked_iota = StakedIota { + let staked_iota = StakedIotaV1 { id: object::new(ctx), pool_id: object::id(pool), stake_activation_epoch, @@ -127,8 +127,8 @@ module iota_system::staking_pool { /// Both the principal and corresponding rewards in IOTA are withdrawn. /// A proportional amount of pool token withdraw is recorded and processed at epoch change time. public(package) fun request_withdraw_stake( - pool: &mut StakingPool, - staked_iota: StakedIota, + pool: &mut StakingPoolV1, + staked_iota: StakedIotaV1, ctx: &TxContext ) : Balance { // stake is inactive @@ -159,12 +159,12 @@ module iota_system::staking_pool { principal_withdraw } - /// Withdraw the principal IOTA stored in the StakedIota object, and calculate the corresponding amount of pool + /// Withdraw the principal IOTA stored in the StakedIotaV1 object, and calculate the corresponding amount of pool /// tokens using exchange rate at staking epoch. /// Returns values are amount of pool tokens withdrawn and withdrawn principal portion of IOTA. public(package) fun withdraw_from_principal( - pool: &StakingPool, - staked_iota: StakedIota, + pool: &StakingPoolV1, + staked_iota: StakedIotaV1, ) : (u64, Balance) { // Check that the stake information matches the pool. @@ -183,8 +183,8 @@ module iota_system::staking_pool { ) } - fun unwrap_staked_iota(staked_iota: StakedIota): Balance { - let StakedIota { + fun unwrap_staked_iota(staked_iota: StakedIotaV1): Balance { + let StakedIotaV1 { id, pool_id: _, stake_activation_epoch: _, @@ -194,31 +194,31 @@ module iota_system::staking_pool { principal } - /// Allows calling `.into_balance()` on `StakedIota` to invoke `unwrap_staked_iota` - public use fun unwrap_staked_iota as StakedIota.into_balance; + /// Allows calling `.into_balance()` on `StakedIotaV1` to invoke `unwrap_staked_iota` + public use fun unwrap_staked_iota as StakedIotaV1.into_balance; // ==== functions called at epoch boundaries === /// Called at epoch advancement times to add rewards (in IOTA) to the staking pool. - public(package) fun deposit_rewards(pool: &mut StakingPool, rewards: Balance) { + public(package) fun deposit_rewards(pool: &mut StakingPoolV1, rewards: Balance) { pool.iota_balance = pool.iota_balance + rewards.value(); pool.rewards_pool.join(rewards); } - public(package) fun process_pending_stakes_and_withdraws(pool: &mut StakingPool, ctx: &TxContext) { + public(package) fun process_pending_stakes_and_withdraws(pool: &mut StakingPoolV1, ctx: &TxContext) { let new_epoch = ctx.epoch() + 1; process_pending_stake_withdraw(pool); process_pending_stake(pool); pool.exchange_rates.add( new_epoch, - PoolTokenExchangeRate { iota_amount: pool.iota_balance, pool_token_amount: pool.pool_token_balance }, + PoolTokenExchangeRateV1 { iota_amount: pool.iota_balance, pool_token_amount: pool.pool_token_balance }, ); check_balance_invariants(pool, new_epoch); } /// Called at epoch boundaries to process pending stake withdraws requested during the epoch. /// Also called immediately upon withdrawal if the pool is inactive. - fun process_pending_stake_withdraw(pool: &mut StakingPool) { + fun process_pending_stake_withdraw(pool: &mut StakingPoolV1) { pool.iota_balance = pool.iota_balance - pool.pending_total_iota_withdraw; pool.pool_token_balance = pool.pool_token_balance - pool.pending_pool_token_withdraw; pool.pending_total_iota_withdraw = 0; @@ -226,10 +226,10 @@ module iota_system::staking_pool { } /// Called at epoch boundaries to process the pending stake. - public(package) fun process_pending_stake(pool: &mut StakingPool) { + public(package) fun process_pending_stake(pool: &mut StakingPoolV1) { // Use the most up to date exchange rate with the rewards deposited and withdraws effectuated. let latest_exchange_rate = - PoolTokenExchangeRate { iota_amount: pool.iota_balance, pool_token_amount: pool.pool_token_balance }; + PoolTokenExchangeRateV1 { iota_amount: pool.iota_balance, pool_token_amount: pool.pool_token_balance }; pool.iota_balance = pool.iota_balance + pool.pending_stake; pool.pool_token_balance = get_token_amount(&latest_exchange_rate, pool.iota_balance); pool.pending_stake = 0; @@ -241,9 +241,9 @@ module iota_system::staking_pool { /// 2. Using the above number and the given `principal_withdraw_amount`, calculates the rewards portion of the /// stake we should withdraw. /// 3. Withdraws the rewards portion from the rewards pool at the current exchange rate. We only withdraw the rewards - /// portion because the principal portion was already taken out of the staker's self custodied StakedIota. + /// portion because the principal portion was already taken out of the staker's self custodied StakedIotaV1. fun withdraw_rewards( - pool: &mut StakingPool, + pool: &mut StakingPoolV1, principal_withdraw_amount: u64, pool_token_withdraw_amount: u64, epoch: u64, @@ -264,7 +264,7 @@ module iota_system::staking_pool { // ==== preactive pool related ==== /// Called by `validator` module to activate a staking pool. - public(package) fun activate_staking_pool(pool: &mut StakingPool, activation_epoch: u64) { + public(package) fun activate_staking_pool(pool: &mut StakingPoolV1, activation_epoch: u64) { // Add the initial exchange rate to the table. pool.exchange_rates.add( activation_epoch, @@ -282,7 +282,7 @@ module iota_system::staking_pool { /// Deactivate a staking pool by setting the `deactivation_epoch`. After /// this pool deactivation, the pool stops earning rewards. Only stake /// withdraws can be made to the pool. - public(package) fun deactivate_staking_pool(pool: &mut StakingPool, deactivation_epoch: u64) { + public(package) fun deactivate_staking_pool(pool: &mut StakingPoolV1, deactivation_epoch: u64) { // We can't deactivate an already deactivated pool. assert!(!is_inactive(pool), EDeactivationOfInactivePool); pool.deactivation_epoch = option::some(deactivation_epoch); @@ -290,40 +290,40 @@ module iota_system::staking_pool { // ==== getters and misc utility functions ==== - public fun iota_balance(pool: &StakingPool): u64 { pool.iota_balance } + public fun iota_balance(pool: &StakingPoolV1): u64 { pool.iota_balance } - public fun pool_id(staked_iota: &StakedIota): ID { staked_iota.pool_id } + public fun pool_id(staked_iota: &StakedIotaV1): ID { staked_iota.pool_id } - public fun staked_iota_amount(staked_iota: &StakedIota): u64 { staked_iota.principal.value() } + public fun staked_iota_amount(staked_iota: &StakedIotaV1): u64 { staked_iota.principal.value() } - /// Allows calling `.amount()` on `StakedIota` to invoke `staked_iota_amount` - public use fun staked_iota_amount as StakedIota.amount; + /// Allows calling `.amount()` on `StakedIotaV1` to invoke `staked_iota_amount` + public use fun staked_iota_amount as StakedIotaV1.amount; - public fun stake_activation_epoch(staked_iota: &StakedIota): u64 { + public fun stake_activation_epoch(staked_iota: &StakedIotaV1): u64 { staked_iota.stake_activation_epoch } /// Returns true if the input staking pool is preactive. - public fun is_preactive(pool: &StakingPool): bool{ + public fun is_preactive(pool: &StakingPoolV1): bool{ pool.activation_epoch.is_none() } /// Returns true if the input staking pool is inactive. - public fun is_inactive(pool: &StakingPool): bool { + public fun is_inactive(pool: &StakingPoolV1): bool { pool.deactivation_epoch.is_some() } - /// Split StakedIota `self` to two parts, one with principal `split_amount`, + /// Split StakedIotaV1 `self` to two parts, one with principal `split_amount`, /// and the remaining principal is left in `self`. - /// All the other parameters of the StakedIota like `stake_activation_epoch` or `pool_id` remain the same. - public fun split(self: &mut StakedIota, split_amount: u64, ctx: &mut TxContext): StakedIota { + /// All the other parameters of the StakedIotaV1 like `stake_activation_epoch` or `pool_id` remain the same. + public fun split(self: &mut StakedIotaV1, split_amount: u64, ctx: &mut TxContext): StakedIotaV1 { let original_amount = self.principal.value(); assert!(split_amount <= original_amount, EInsufficientIotaTokenBalance); let remaining_amount = original_amount - split_amount; // Both resulting parts should have at least MIN_STAKING_THRESHOLD. assert!(remaining_amount >= MIN_STAKING_THRESHOLD, EStakedIotaBelowThreshold); assert!(split_amount >= MIN_STAKING_THRESHOLD, EStakedIotaBelowThreshold); - StakedIota { + StakedIotaV1 { id: object::new(ctx), pool_id: self.pool_id, stake_activation_epoch: self.stake_activation_epoch, @@ -331,20 +331,20 @@ module iota_system::staking_pool { } } - /// Split the given StakedIota to the two parts, one with principal `split_amount`, + /// Split the given StakedIotaV1 to the two parts, one with principal `split_amount`, /// transfer the newly split part to the sender address. - public entry fun split_staked_iota(stake: &mut StakedIota, split_amount: u64, ctx: &mut TxContext) { + public entry fun split_staked_iota(stake: &mut StakedIotaV1, split_amount: u64, ctx: &mut TxContext) { transfer::transfer(split(stake, split_amount, ctx), ctx.sender()); } - /// Allows calling `.split_to_sender()` on `StakedIota` to invoke `split_staked_iota` - public use fun split_staked_iota as StakedIota.split_to_sender; + /// Allows calling `.split_to_sender()` on `StakedIotaV1` to invoke `split_staked_iota` + public use fun split_staked_iota as StakedIotaV1.split_to_sender; /// Consume the staked iota `other` and add its value to `self`. /// Aborts if some of the staking parameters are incompatible (pool id, stake activation epoch, etc.) - public entry fun join_staked_iota(self: &mut StakedIota, other: StakedIota) { + public entry fun join_staked_iota(self: &mut StakedIotaV1, other: StakedIotaV1) { assert!(is_equal_staking_metadata(self, &other), EIncompatibleStakedIota); - let StakedIota { + let StakedIotaV1 { id, pool_id: _, stake_activation_epoch: _, @@ -355,16 +355,16 @@ module iota_system::staking_pool { self.principal.join(principal); } - /// Allows calling `.join()` on `StakedIota` to invoke `join_staked_iota` - public use fun join_staked_iota as StakedIota.join; + /// Allows calling `.join()` on `StakedIotaV1` to invoke `join_staked_iota` + public use fun join_staked_iota as StakedIotaV1.join; /// Returns true if all the staking parameters of the staked iota except the principal are identical - public fun is_equal_staking_metadata(self: &StakedIota, other: &StakedIota): bool { + public fun is_equal_staking_metadata(self: &StakedIotaV1, other: &StakedIotaV1): bool { (self.pool_id == other.pool_id) && (self.stake_activation_epoch == other.stake_activation_epoch) } - public fun pool_token_exchange_rate_at_epoch(pool: &StakingPool, epoch: u64): PoolTokenExchangeRate { + public fun pool_token_exchange_rate_at_epoch(pool: &StakingPoolV1, epoch: u64): PoolTokenExchangeRateV1 { // If the pool is preactive then the exchange rate is always 1:1. if (is_preactive_at_epoch(pool, epoch)) { return initial_exchange_rate() @@ -385,34 +385,34 @@ module iota_system::staking_pool { } /// Returns the total value of the pending staking requests for this staking pool. - public fun pending_stake_amount(staking_pool: &StakingPool): u64 { + public fun pending_stake_amount(staking_pool: &StakingPoolV1): u64 { staking_pool.pending_stake } /// Returns the total withdrawal from the staking pool this epoch. - public fun pending_stake_withdraw_amount(staking_pool: &StakingPool): u64 { + public fun pending_stake_withdraw_amount(staking_pool: &StakingPoolV1): u64 { staking_pool.pending_total_iota_withdraw } - public(package) fun exchange_rates(pool: &StakingPool): &Table { + public(package) fun exchange_rates(pool: &StakingPoolV1): &Table { &pool.exchange_rates } - public fun iota_amount(exchange_rate: &PoolTokenExchangeRate): u64 { + public fun iota_amount(exchange_rate: &PoolTokenExchangeRateV1): u64 { exchange_rate.iota_amount } - public fun pool_token_amount(exchange_rate: &PoolTokenExchangeRate): u64 { + public fun pool_token_amount(exchange_rate: &PoolTokenExchangeRateV1): u64 { exchange_rate.pool_token_amount } /// Returns true if the provided staking pool is preactive at the provided epoch. - fun is_preactive_at_epoch(pool: &StakingPool, epoch: u64): bool{ + fun is_preactive_at_epoch(pool: &StakingPoolV1, epoch: u64): bool{ // Either the pool is currently preactive or the pool's starting epoch is later than the provided epoch. is_preactive(pool) || (*pool.activation_epoch.borrow() > epoch) } - fun get_iota_amount(exchange_rate: &PoolTokenExchangeRate, token_amount: u64): u64 { + fun get_iota_amount(exchange_rate: &PoolTokenExchangeRateV1, token_amount: u64): u64 { // When either amount is 0, that means we have no stakes with this pool. // The other amount might be non-zero when there's dust left in the pool. if (exchange_rate.iota_amount == 0 || exchange_rate.pool_token_amount == 0) { @@ -424,7 +424,7 @@ module iota_system::staking_pool { res as u64 } - fun get_token_amount(exchange_rate: &PoolTokenExchangeRate, iota_amount: u64): u64 { + fun get_token_amount(exchange_rate: &PoolTokenExchangeRateV1, iota_amount: u64): u64 { // When either amount is 0, that means we have no stakes with this pool. // The other amount might be non-zero when there's dust left in the pool. if (exchange_rate.iota_amount == 0 || exchange_rate.pool_token_amount == 0) { @@ -436,11 +436,11 @@ module iota_system::staking_pool { res as u64 } - fun initial_exchange_rate(): PoolTokenExchangeRate { - PoolTokenExchangeRate { iota_amount: 0, pool_token_amount: 0 } + fun initial_exchange_rate(): PoolTokenExchangeRateV1 { + PoolTokenExchangeRateV1 { iota_amount: 0, pool_token_amount: 0 } } - fun check_balance_invariants(pool: &StakingPool, epoch: u64) { + fun check_balance_invariants(pool: &StakingPoolV1, epoch: u64) { let exchange_rate = pool_token_exchange_rate_at_epoch(pool, epoch); // check that the pool token balance and iota balance ratio matches the exchange rate stored. let expected = get_token_amount(&exchange_rate, pool.iota_balance); @@ -453,8 +453,8 @@ module iota_system::staking_pool { // Given the `staked_iota` receipt calculate the current rewards (in terms of IOTA) for it. #[test_only] public fun calculate_rewards( - pool: &StakingPool, - staked_iota: &StakedIota, + pool: &StakingPoolV1, + staked_iota: &StakedIotaV1, current_epoch: u64, ): u64 { let staked_amount = staked_iota_amount(staked_iota); diff --git a/crates/iota-framework/packages/iota-system/sources/storage_fund.move b/crates/iota-framework/packages/iota-system/sources/storage_fund.move index af347f40594..c25a5ebeabf 100644 --- a/crates/iota-framework/packages/iota-system/sources/storage_fund.move +++ b/crates/iota-framework/packages/iota-system/sources/storage_fund.move @@ -14,14 +14,14 @@ module iota_system::storage_fund { /// the non-refundable portion taken out and put into `non_refundable_balance`. /// - `non_refundable_balance` contains any remaining inflow of the storage fund that should not /// be taken out of the fund. - public struct StorageFund has store { + public struct StorageFundV1 has store { total_object_storage_rebates: Balance, non_refundable_balance: Balance, } /// Called by `iota_system` at genesis time. - public(package) fun new(initial_fund: Balance) : StorageFund { - StorageFund { + public(package) fun new(initial_fund: Balance) : StorageFundV1 { + StorageFundV1 { // At the beginning there's no object in the storage yet total_object_storage_rebates: balance::zero(), non_refundable_balance: initial_fund, @@ -30,7 +30,7 @@ module iota_system::storage_fund { /// Called by `iota_system` at epoch change times to process the inflows and outflows of storage fund. public(package) fun advance_epoch( - self: &mut StorageFund, + self: &mut StorageFundV1, storage_charges: Balance, storage_rebate_amount: u64, non_refundable_storage_fee_amount: u64, @@ -53,11 +53,11 @@ module iota_system::storage_fund { storage_rebate } - public fun total_object_storage_rebates(self: &StorageFund): u64 { + public fun total_object_storage_rebates(self: &StorageFundV1): u64 { self.total_object_storage_rebates.value() } - public fun total_balance(self: &StorageFund): u64 { + public fun total_balance(self: &StorageFundV1): u64 { self.total_object_storage_rebates.value() + self.non_refundable_balance.value() } } diff --git a/crates/iota-framework/packages/iota-system/sources/timelocked_staking.move b/crates/iota-framework/packages/iota-system/sources/timelocked_staking.move index abc3e10a107..4a6e06f0d16 100644 --- a/crates/iota-framework/packages/iota-system/sources/timelocked_staking.move +++ b/crates/iota-framework/packages/iota-system/sources/timelocked_staking.move @@ -10,19 +10,19 @@ module iota_system::timelocked_staking { use iota::timelock::{Self, TimeLock}; use iota_system::iota_system::{IotaSystemState}; - use iota_system::staking_pool::StakedIota; - use iota_system::validator::{Validator}; + use iota_system::staking_pool::StakedIotaV1; + use iota_system::validator::{ValidatorV1}; /// For when trying to stake an expired time-locked balance. const ETimeLockShouldNotBeExpired: u64 = 0; - /// Incompatible objects when joining TimelockedStakedIota + /// Incompatible objects when joining TimelockedStakedIotaV1 const EIncompatibleTimelockedStakedIota: u64 = 1; /// A self-custodial object holding the timelocked staked IOTA tokens. - public struct TimelockedStakedIota has key { + public struct TimelockedStakedIotaV1 has key { id: UID, /// A self-custodial object holding the staked IOTA tokens. - staked_iota: StakedIota, + staked_iota: StakedIotaV1, /// This is the epoch time stamp of when the lock expires. expiration_timestamp_ms: u64, /// Timelock related label. @@ -76,7 +76,7 @@ module iota_system::timelocked_staking { /// Withdraw a time-locked stake from a validator's staking pool. public entry fun request_withdraw_stake( iota_system: &mut IotaSystemState, - timelocked_staked_iota: TimelockedStakedIota, + timelocked_staked_iota: TimelockedStakedIotaV1, ctx: &mut TxContext, ) { // Withdraw the time-locked balance. @@ -102,7 +102,7 @@ module iota_system::timelocked_staking { timelocked_balance: TimeLock>, validator_address: address, ctx: &mut TxContext, - ) : TimelockedStakedIota { + ) : TimelockedStakedIotaV1 { // Check the preconditions. assert!(timelocked_balance.is_locked(ctx), ETimeLockShouldNotBeExpired); @@ -118,7 +118,7 @@ module iota_system::timelocked_staking { ); // Create and return a receipt. - TimelockedStakedIota { + TimelockedStakedIotaV1 { id: object::new(ctx), staked_iota, expiration_timestamp_ms, @@ -133,7 +133,7 @@ module iota_system::timelocked_staking { mut timelocked_balances: vector>>, validator_address: address, ctx: &mut TxContext, - ) : vector { + ) : vector { // Create a vector to store the results. let mut result = vector[]; @@ -164,10 +164,10 @@ module iota_system::timelocked_staking { /// instead of transferring it to the sender. public fun request_withdraw_stake_non_entry( iota_system: &mut IotaSystemState, - timelocked_staked_iota: TimelockedStakedIota, + timelocked_staked_iota: TimelockedStakedIotaV1, ctx: &mut TxContext, ) : (TimeLock>, Balance) { - // Unpack the `TimelockedStakedIota` instance. + // Unpack the `TimelockedStakedIotaV1` instance. let (staked_iota, expiration_timestamp_ms, label) = timelocked_staked_iota.unpack(); // Store the original stake amount. @@ -185,15 +185,15 @@ module iota_system::timelocked_staking { (timelock::system_pack(sys_timelock_cap, principal, expiration_timestamp_ms, label, ctx), withdraw_stake) } - // === TimelockedStakedIota balance functions === + // === TimelockedStakedIotaV1 balance functions === - /// Split `TimelockedStakedIota` into two parts, one with principal `split_amount`, + /// Split `TimelockedStakedIotaV1` into two parts, one with principal `split_amount`, /// and the remaining principal is left in `self`. - /// All the other parameters of the `TimelockedStakedIota` like `stake_activation_epoch` or `pool_id` remain the same. - public fun split(self: &mut TimelockedStakedIota, split_amount: u64, ctx: &mut TxContext): TimelockedStakedIota { + /// All the other parameters of the `TimelockedStakedIotaV1` like `stake_activation_epoch` or `pool_id` remain the same. + public fun split(self: &mut TimelockedStakedIotaV1, split_amount: u64, ctx: &mut TxContext): TimelockedStakedIotaV1 { let split_stake = self.staked_iota.split(split_amount, ctx); - TimelockedStakedIota { + TimelockedStakedIotaV1 { id: object::new(ctx), staked_iota: split_stake, expiration_timestamp_ms: self.expiration_timestamp_ms, @@ -201,21 +201,21 @@ module iota_system::timelocked_staking { } } - /// Split the given `TimelockedStakedIota` to the two parts, one with principal `split_amount`, + /// Split the given `TimelockedStakedIotaV1` to the two parts, one with principal `split_amount`, /// transfer the newly split part to the sender address. - public entry fun split_staked_iota(stake: &mut TimelockedStakedIota, split_amount: u64, ctx: &mut TxContext) { + public entry fun split_staked_iota(stake: &mut TimelockedStakedIotaV1, split_amount: u64, ctx: &mut TxContext) { split(stake, split_amount, ctx).transfer_to_sender(ctx); } - /// Allows calling `.split_to_sender()` on `TimelockedStakedIota` to invoke `split_staked_iota` - public use fun split_staked_iota as TimelockedStakedIota.split_to_sender; + /// Allows calling `.split_to_sender()` on `TimelockedStakedIotaV1` to invoke `split_staked_iota` + public use fun split_staked_iota as TimelockedStakedIotaV1.split_to_sender; /// Consume the staked iota `other` and add its value to `self`. /// Aborts if some of the staking parameters are incompatible (pool id, stake activation epoch, etc.) - public entry fun join_staked_iota(self: &mut TimelockedStakedIota, other: TimelockedStakedIota) { + public entry fun join_staked_iota(self: &mut TimelockedStakedIotaV1, other: TimelockedStakedIotaV1) { assert!(self.is_equal_staking_metadata(&other), EIncompatibleTimelockedStakedIota); - let TimelockedStakedIota { + let TimelockedStakedIotaV1 { id, staked_iota, expiration_timestamp_ms: _, @@ -227,57 +227,57 @@ module iota_system::timelocked_staking { self.staked_iota.join(staked_iota); } - /// Allows calling `.join()` on `TimelockedStakedIota` to invoke `join_staked_iota` - public use fun join_staked_iota as TimelockedStakedIota.join; + /// Allows calling `.join()` on `TimelockedStakedIotaV1` to invoke `join_staked_iota` + public use fun join_staked_iota as TimelockedStakedIotaV1.join; - // === TimelockedStakedIota public utilities === + // === TimelockedStakedIotaV1 public utilities === - /// A utility function to transfer a `TimelockedStakedIota`. - public fun transfer_to_sender(stake: TimelockedStakedIota, ctx: &TxContext) { + /// A utility function to transfer a `TimelockedStakedIotaV1`. + public fun transfer_to_sender(stake: TimelockedStakedIotaV1, ctx: &TxContext) { transfer(stake, ctx.sender()) } - /// A utility function to transfer multiple `TimelockedStakedIota`. - public fun transfer_to_sender_multiple(stakes: vector, ctx: &TxContext) { + /// A utility function to transfer multiple `TimelockedStakedIotaV1`. + public fun transfer_to_sender_multiple(stakes: vector, ctx: &TxContext) { transfer_multiple(stakes, ctx.sender()) } /// A utility function that returns true if all the staking parameters /// of the staked iota except the principal are identical - public fun is_equal_staking_metadata(self: &TimelockedStakedIota, other: &TimelockedStakedIota): bool { + public fun is_equal_staking_metadata(self: &TimelockedStakedIotaV1, other: &TimelockedStakedIotaV1): bool { self.staked_iota.is_equal_staking_metadata(&other.staked_iota) && (self.expiration_timestamp_ms == other.expiration_timestamp_ms) && (self.label() == other.label()) } - // === TimelockedStakedIota getters === + // === TimelockedStakedIotaV1 getters === - /// Function to get the pool id of a `TimelockedStakedIota`. - public fun pool_id(self: &TimelockedStakedIota): ID { self.staked_iota.pool_id() } + /// Function to get the pool id of a `TimelockedStakedIotaV1`. + public fun pool_id(self: &TimelockedStakedIotaV1): ID { self.staked_iota.pool_id() } - /// Function to get the staked iota amount of a `TimelockedStakedIota`. - public fun staked_iota_amount(self: &TimelockedStakedIota): u64 { self.staked_iota.staked_iota_amount() } + /// Function to get the staked iota amount of a `TimelockedStakedIotaV1`. + public fun staked_iota_amount(self: &TimelockedStakedIotaV1): u64 { self.staked_iota.staked_iota_amount() } - /// Allows calling `.amount()` on `TimelockedStakedIota` to invoke `staked_iota_amount` - public use fun staked_iota_amount as TimelockedStakedIota.amount; + /// Allows calling `.amount()` on `TimelockedStakedIotaV1` to invoke `staked_iota_amount` + public use fun staked_iota_amount as TimelockedStakedIotaV1.amount; - /// Function to get the stake activation epoch of a `TimelockedStakedIota`. - public fun stake_activation_epoch(self: &TimelockedStakedIota): u64 { + /// Function to get the stake activation epoch of a `TimelockedStakedIotaV1`. + public fun stake_activation_epoch(self: &TimelockedStakedIotaV1): u64 { self.staked_iota.stake_activation_epoch() } - /// Function to get the expiration timestamp of a `TimelockedStakedIota`. - public fun expiration_timestamp_ms(self: &TimelockedStakedIota): u64 { + /// Function to get the expiration timestamp of a `TimelockedStakedIotaV1`. + public fun expiration_timestamp_ms(self: &TimelockedStakedIotaV1): u64 { self.expiration_timestamp_ms } - /// Function to get the label of a `TimelockedStakedIota`. - public fun label(self: &TimelockedStakedIota): Option { + /// Function to get the label of a `TimelockedStakedIotaV1`. + public fun label(self: &TimelockedStakedIotaV1): Option { self.label } - /// Check if a `TimelockedStakedIota` is labeled with the type `L`. - public fun is_labeled_with(self: &TimelockedStakedIota): bool { + /// Check if a `TimelockedStakedIotaV1` is labeled with the type `L`. + public fun is_labeled_with(self: &TimelockedStakedIotaV1): bool { if (self.label.is_some()) { self.label.borrow() == timelock::type_name() } @@ -288,9 +288,9 @@ module iota_system::timelocked_staking { // === Internal === - /// A utility function to destroy a `TimelockedStakedIota`. - fun unpack(self: TimelockedStakedIota): (StakedIota, u64, Option) { - let TimelockedStakedIota { + /// A utility function to destroy a `TimelockedStakedIotaV1`. + fun unpack(self: TimelockedStakedIotaV1): (StakedIotaV1, u64, Option) { + let TimelockedStakedIotaV1 { id, staked_iota, expiration_timestamp_ms, @@ -303,13 +303,13 @@ module iota_system::timelocked_staking { } - /// A utility function to transfer a `TimelockedStakedIota` to a receiver. - fun transfer(stake: TimelockedStakedIota, receiver: address) { + /// A utility function to transfer a `TimelockedStakedIotaV1` to a receiver. + fun transfer(stake: TimelockedStakedIotaV1, receiver: address) { transfer::transfer(stake, receiver); } - /// A utility function to transfer a vector of `TimelockedStakedIota` to a receiver. - fun transfer_multiple(mut stakes: vector, receiver: address) { + /// A utility function to transfer a vector of `TimelockedStakedIotaV1` to a receiver. + fun transfer_multiple(mut stakes: vector, receiver: address) { // Transfer all the time-locked stakes to the recipient. while (!stakes.is_empty()) { let stake = stakes.pop_back(); @@ -324,7 +324,7 @@ module iota_system::timelocked_staking { /// Request to add timelocked stake to the validator's staking pool at genesis public(package) fun request_add_stake_at_genesis( - validator: &mut Validator, + validator: &mut ValidatorV1, stake: Balance, staker_address: address, expiration_timestamp_ms: u64, @@ -332,7 +332,7 @@ module iota_system::timelocked_staking { ctx: &mut TxContext, ) { let staked_iota = validator.request_add_stake_at_genesis_with_receipt(stake, ctx); - let timelocked_staked_iota = TimelockedStakedIota { + let timelocked_staked_iota = TimelockedStakedIotaV1 { id: object::new(ctx), staked_iota, expiration_timestamp_ms, diff --git a/crates/iota-framework/packages/iota-system/sources/validator.move b/crates/iota-framework/packages/iota-system/sources/validator.move index 909edce0bde..994c7b356de 100644 --- a/crates/iota-framework/packages/iota-system/sources/validator.move +++ b/crates/iota-framework/packages/iota-system/sources/validator.move @@ -9,7 +9,7 @@ module iota_system::validator { use iota::balance::Balance; use iota::iota::IOTA; use iota_system::validator_cap::{Self, ValidatorOperationCap}; - use iota_system::staking_pool::{Self, PoolTokenExchangeRate, StakedIota, StakingPool}; + use iota_system::staking_pool::{Self, PoolTokenExchangeRateV1, StakedIotaV1, StakingPoolV1}; use std::string::String; use iota::url::Url; use iota::url; @@ -74,8 +74,8 @@ module iota_system::validator { /// Max gas price a validator can set is 100K NANOS. const MAX_VALIDATOR_GAS_PRICE: u64 = 100_000; - public struct ValidatorMetadata has store { - /// The Iota Address of the validator. This is the sender that created the Validator object, + public struct ValidatorMetadataV1 has store { + /// The Iota Address of the validator. This is the sender that created the ValidatorV1 object, /// and also the address to send validator/coins to during withdraws. iota_address: address, /// The public key bytes corresponding to the private key that the validator @@ -117,9 +117,9 @@ module iota_system::validator { extra_fields: Bag, } - public struct Validator has store { + public struct ValidatorV1 has store { /// Summary of the validator. - metadata: ValidatorMetadata, + metadata: ValidatorMetadataV1, /// The voting power of this validator, which might be different from its /// stake amount. voting_power: u64, @@ -128,7 +128,7 @@ module iota_system::validator { /// Gas price quote, updated only at end of epoch. gas_price: u64, /// Staking pool for this validator. - staking_pool: StakingPool, + staking_pool: StakingPoolV1, /// Commission rate of the validator, in basis point. commission_rate: u64, /// Total amount of stake that would be active in the next epoch. @@ -176,8 +176,8 @@ module iota_system::validator { primary_address: String, worker_address: String, extra_fields: Bag, - ): ValidatorMetadata { - let metadata = ValidatorMetadata { + ): ValidatorMetadataV1 { + let metadata = ValidatorMetadataV1 { iota_address, protocol_pubkey_bytes, network_pubkey_bytes, @@ -221,7 +221,7 @@ module iota_system::validator { gas_price: u64, commission_rate: u64, ctx: &mut TxContext - ): Validator { + ): ValidatorV1 { assert!( net_address.length() <= MAX_VALIDATOR_METADATA_LENGTH && p2p_address.length() <= MAX_VALIDATOR_METADATA_LENGTH @@ -265,27 +265,27 @@ module iota_system::validator { } /// Deactivate this validator's staking pool - public(package) fun deactivate(self: &mut Validator, deactivation_epoch: u64) { + public(package) fun deactivate(self: &mut ValidatorV1, deactivation_epoch: u64) { self.staking_pool.deactivate_staking_pool(deactivation_epoch) } - public(package) fun activate(self: &mut Validator, activation_epoch: u64) { + public(package) fun activate(self: &mut ValidatorV1, activation_epoch: u64) { self.staking_pool.activate_staking_pool(activation_epoch); } /// Process pending stake and pending withdraws, and update the gas price. - public(package) fun adjust_stake_and_gas_price(self: &mut Validator) { + public(package) fun adjust_stake_and_gas_price(self: &mut ValidatorV1) { self.gas_price = self.next_epoch_gas_price; self.commission_rate = self.next_epoch_commission_rate; } /// Request to add stake to the validator's staking pool, processed at the end of the epoch. public(package) fun request_add_stake( - self: &mut Validator, + self: &mut ValidatorV1, stake: Balance, staker_address: address, ctx: &mut TxContext, - ) : StakedIota { + ) : StakedIotaV1 { let stake_amount = stake.value(); assert!(stake_amount > 0, EInvalidStakeAmount); let stake_epoch = ctx.epoch() + 1; @@ -309,7 +309,7 @@ module iota_system::validator { /// Request to add stake to the validator's staking pool at genesis public(package) fun request_add_stake_at_genesis( - self: &mut Validator, + self: &mut ValidatorV1, stake: Balance, staker_address: address, ctx: &mut TxContext, @@ -323,12 +323,12 @@ module iota_system::validator { } /// Internal request to add stake to the validator's staking pool at genesis. - /// Returns a StakedIota + /// Returns a StakedIotaV1 public(package) fun request_add_stake_at_genesis_with_receipt( - self: &mut Validator, + self: &mut ValidatorV1, stake: Balance, ctx: &mut TxContext, - ) : StakedIota { + ) : StakedIotaV1 { assert!(ctx.epoch() == 0, ECalledDuringNonGenesis); let stake_amount = stake.value(); assert!(stake_amount > 0, EInvalidStakeAmount); @@ -348,8 +348,8 @@ module iota_system::validator { /// Request to withdraw stake from the validator's staking pool, processed at the end of the epoch. public(package) fun request_withdraw_stake( - self: &mut Validator, - staked_iota: StakedIota, + self: &mut ValidatorV1, + staked_iota: StakedIotaV1, ctx: &TxContext, ) : Balance { let principal_amount = staked_iota.staked_iota_amount(); @@ -375,7 +375,7 @@ module iota_system::validator { /// Request to set new gas price for the next epoch. /// Need to present a `ValidatorOperationCap`. public(package) fun request_set_gas_price( - self: &mut Validator, + self: &mut ValidatorV1, verified_cap: ValidatorOperationCap, new_price: u64, ) { @@ -387,7 +387,7 @@ module iota_system::validator { /// Set new gas price for the candidate validator. public(package) fun set_candidate_gas_price( - self: &mut Validator, + self: &mut ValidatorV1, verified_cap: ValidatorOperationCap, new_price: u64 ) { @@ -400,182 +400,182 @@ module iota_system::validator { } /// Request to set new commission rate for the next epoch. - public(package) fun request_set_commission_rate(self: &mut Validator, new_commission_rate: u64) { + public(package) fun request_set_commission_rate(self: &mut ValidatorV1, new_commission_rate: u64) { assert!(new_commission_rate <= MAX_COMMISSION_RATE, ECommissionRateTooHigh); self.next_epoch_commission_rate = new_commission_rate; } /// Set new commission rate for the candidate validator. - public(package) fun set_candidate_commission_rate(self: &mut Validator, new_commission_rate: u64) { + public(package) fun set_candidate_commission_rate(self: &mut ValidatorV1, new_commission_rate: u64) { assert!(is_preactive(self), ENotValidatorCandidate); assert!(new_commission_rate <= MAX_COMMISSION_RATE, ECommissionRateTooHigh); self.commission_rate = new_commission_rate; } /// Deposit stakes rewards into the validator's staking pool, called at the end of the epoch. - public(package) fun deposit_stake_rewards(self: &mut Validator, reward: Balance) { + public(package) fun deposit_stake_rewards(self: &mut ValidatorV1, reward: Balance) { self.next_epoch_stake = self.next_epoch_stake + reward.value(); self.staking_pool.deposit_rewards(reward); } /// Process pending stakes and withdraws, called at the end of the epoch. - public(package) fun process_pending_stakes_and_withdraws(self: &mut Validator, ctx: &TxContext) { + public(package) fun process_pending_stakes_and_withdraws(self: &mut ValidatorV1, ctx: &TxContext) { self.staking_pool.process_pending_stakes_and_withdraws(ctx); assert!(stake_amount(self) == self.next_epoch_stake, EInvalidStakeAmount); } /// Returns true if the validator is preactive. - public fun is_preactive(self: &Validator): bool { + public fun is_preactive(self: &ValidatorV1): bool { self.staking_pool.is_preactive() } - public fun metadata(self: &Validator): &ValidatorMetadata { + public fun metadata(self: &ValidatorV1): &ValidatorMetadataV1 { &self.metadata } - public fun iota_address(self: &Validator): address { + public fun iota_address(self: &ValidatorV1): address { self.metadata.iota_address } - public fun name(self: &Validator): &String { + public fun name(self: &ValidatorV1): &String { &self.metadata.name } - public fun description(self: &Validator): &String { + public fun description(self: &ValidatorV1): &String { &self.metadata.description } - public fun image_url(self: &Validator): &Url { + public fun image_url(self: &ValidatorV1): &Url { &self.metadata.image_url } - public fun project_url(self: &Validator): &Url { + public fun project_url(self: &ValidatorV1): &Url { &self.metadata.project_url } - public fun network_address(self: &Validator): &String { + public fun network_address(self: &ValidatorV1): &String { &self.metadata.net_address } - public fun p2p_address(self: &Validator): &String { + public fun p2p_address(self: &ValidatorV1): &String { &self.metadata.p2p_address } - public fun primary_address(self: &Validator): &String { + public fun primary_address(self: &ValidatorV1): &String { &self.metadata.primary_address } - public fun worker_address(self: &Validator): &String { + public fun worker_address(self: &ValidatorV1): &String { &self.metadata.worker_address } - public fun protocol_pubkey_bytes(self: &Validator): &vector { + public fun protocol_pubkey_bytes(self: &ValidatorV1): &vector { &self.metadata.protocol_pubkey_bytes } - public fun proof_of_possession(self: &Validator): &vector { + public fun proof_of_possession(self: &ValidatorV1): &vector { &self.metadata.proof_of_possession } - public fun network_pubkey_bytes(self: &Validator): &vector { + public fun network_pubkey_bytes(self: &ValidatorV1): &vector { &self.metadata.network_pubkey_bytes } - public fun worker_pubkey_bytes(self: &Validator): &vector { + public fun worker_pubkey_bytes(self: &ValidatorV1): &vector { &self.metadata.worker_pubkey_bytes } - public fun next_epoch_network_address(self: &Validator): &Option { + public fun next_epoch_network_address(self: &ValidatorV1): &Option { &self.metadata.next_epoch_net_address } - public fun next_epoch_p2p_address(self: &Validator): &Option { + public fun next_epoch_p2p_address(self: &ValidatorV1): &Option { &self.metadata.next_epoch_p2p_address } - public fun next_epoch_primary_address(self: &Validator): &Option { + public fun next_epoch_primary_address(self: &ValidatorV1): &Option { &self.metadata.next_epoch_primary_address } - public fun next_epoch_worker_address(self: &Validator): &Option { + public fun next_epoch_worker_address(self: &ValidatorV1): &Option { &self.metadata.next_epoch_worker_address } - public fun next_epoch_protocol_pubkey_bytes(self: &Validator): &Option> { + public fun next_epoch_protocol_pubkey_bytes(self: &ValidatorV1): &Option> { &self.metadata.next_epoch_protocol_pubkey_bytes } - public fun next_epoch_proof_of_possession(self: &Validator): &Option> { + public fun next_epoch_proof_of_possession(self: &ValidatorV1): &Option> { &self.metadata.next_epoch_proof_of_possession } - public fun next_epoch_network_pubkey_bytes(self: &Validator): &Option> { + public fun next_epoch_network_pubkey_bytes(self: &ValidatorV1): &Option> { &self.metadata.next_epoch_network_pubkey_bytes } - public fun next_epoch_worker_pubkey_bytes(self: &Validator): &Option> { + public fun next_epoch_worker_pubkey_bytes(self: &ValidatorV1): &Option> { &self.metadata.next_epoch_worker_pubkey_bytes } - public fun operation_cap_id(self: &Validator): &ID { + public fun operation_cap_id(self: &ValidatorV1): &ID { &self.operation_cap_id } - public fun next_epoch_gas_price(self: &Validator): u64 { + public fun next_epoch_gas_price(self: &ValidatorV1): u64 { self.next_epoch_gas_price } // TODO: this and `delegate_amount` and `total_stake` all seem to return the same value? // two of the functions can probably be removed. - public fun total_stake_amount(self: &Validator): u64 { + public fun total_stake_amount(self: &ValidatorV1): u64 { self.staking_pool.iota_balance() } - public fun stake_amount(self: &Validator): u64 { + public fun stake_amount(self: &ValidatorV1): u64 { self.staking_pool.iota_balance() } /// Return the total amount staked with this validator - public fun total_stake(self: &Validator): u64 { + public fun total_stake(self: &ValidatorV1): u64 { stake_amount(self) } /// Return the voting power of this validator. - public fun voting_power(self: &Validator): u64 { + public fun voting_power(self: &ValidatorV1): u64 { self.voting_power } /// Set the voting power of this validator, called only from validator_set. - public(package) fun set_voting_power(self: &mut Validator, new_voting_power: u64) { + public(package) fun set_voting_power(self: &mut ValidatorV1, new_voting_power: u64) { self.voting_power = new_voting_power; } - public fun pending_stake_amount(self: &Validator): u64 { + public fun pending_stake_amount(self: &ValidatorV1): u64 { self.staking_pool.pending_stake_amount() } - public fun pending_stake_withdraw_amount(self: &Validator): u64 { + public fun pending_stake_withdraw_amount(self: &ValidatorV1): u64 { self.staking_pool.pending_stake_withdraw_amount() } - public fun gas_price(self: &Validator): u64 { + public fun gas_price(self: &ValidatorV1): u64 { self.gas_price } - public fun commission_rate(self: &Validator): u64 { + public fun commission_rate(self: &ValidatorV1): u64 { self.commission_rate } - public fun pool_token_exchange_rate_at_epoch(self: &Validator, epoch: u64): PoolTokenExchangeRate { + public fun pool_token_exchange_rate_at_epoch(self: &ValidatorV1, epoch: u64): PoolTokenExchangeRateV1 { self.staking_pool.pool_token_exchange_rate_at_epoch(epoch) } - public fun staking_pool_id(self: &Validator): ID { + public fun staking_pool_id(self: &ValidatorV1): ID { object::id(&self.staking_pool) } // MUSTFIX: We need to check this when updating metadata as well. - public fun is_duplicate(self: &Validator, other: &Validator): bool { + public fun is_duplicate(self: &ValidatorV1, other: &ValidatorV1): bool { self.metadata.iota_address == other.metadata.iota_address || self.metadata.name == other.metadata.name || self.metadata.net_address == other.metadata.net_address @@ -631,7 +631,7 @@ module iota_system::validator { /// Create a new `UnverifiedValidatorOperationCap`, transfer to the validator, /// and registers it, thus revoking the previous cap's permission. - public(package) fun new_unverified_validator_operation_cap_and_transfer(self: &mut Validator, ctx: &mut TxContext) { + public(package) fun new_unverified_validator_operation_cap_and_transfer(self: &mut ValidatorV1, ctx: &mut TxContext) { let address = ctx.sender(); assert!(address == self.metadata.iota_address, ENewCapNotCreatedByValidatorItself); let new_id = validator_cap::new_unverified_validator_operation_cap_and_transfer(address, ctx); @@ -639,7 +639,7 @@ module iota_system::validator { } /// Update name of the validator. - public(package) fun update_name(self: &mut Validator, name: vector) { + public(package) fun update_name(self: &mut ValidatorV1, name: vector) { assert!( name.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -648,7 +648,7 @@ module iota_system::validator { } /// Update description of the validator. - public(package) fun update_description(self: &mut Validator, description: vector) { + public(package) fun update_description(self: &mut ValidatorV1, description: vector) { assert!( description.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -657,7 +657,7 @@ module iota_system::validator { } /// Update image url of the validator. - public(package) fun update_image_url(self: &mut Validator, image_url: vector) { + public(package) fun update_image_url(self: &mut ValidatorV1, image_url: vector) { assert!( image_url.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -666,7 +666,7 @@ module iota_system::validator { } /// Update project url of the validator. - public(package) fun update_project_url(self: &mut Validator, project_url: vector) { + public(package) fun update_project_url(self: &mut ValidatorV1, project_url: vector) { assert!( project_url.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -675,7 +675,7 @@ module iota_system::validator { } /// Update network address of this validator, taking effects from next epoch - public(package) fun update_next_epoch_network_address(self: &mut Validator, net_address: vector) { + public(package) fun update_next_epoch_network_address(self: &mut ValidatorV1, net_address: vector) { assert!( net_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -686,7 +686,7 @@ module iota_system::validator { } /// Update network address of this candidate validator - public(package) fun update_candidate_network_address(self: &mut Validator, net_address: vector) { + public(package) fun update_candidate_network_address(self: &mut ValidatorV1, net_address: vector) { assert!(is_preactive(self), ENotValidatorCandidate); assert!( net_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, @@ -698,7 +698,7 @@ module iota_system::validator { } /// Update p2p address of this validator, taking effects from next epoch - public(package) fun update_next_epoch_p2p_address(self: &mut Validator, p2p_address: vector) { + public(package) fun update_next_epoch_p2p_address(self: &mut ValidatorV1, p2p_address: vector) { assert!( p2p_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -709,7 +709,7 @@ module iota_system::validator { } /// Update p2p address of this candidate validator - public(package) fun update_candidate_p2p_address(self: &mut Validator, p2p_address: vector) { + public(package) fun update_candidate_p2p_address(self: &mut ValidatorV1, p2p_address: vector) { assert!(is_preactive(self), ENotValidatorCandidate); assert!( p2p_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, @@ -721,7 +721,7 @@ module iota_system::validator { } /// Update primary address of this validator, taking effects from next epoch - public(package) fun update_next_epoch_primary_address(self: &mut Validator, primary_address: vector) { + public(package) fun update_next_epoch_primary_address(self: &mut ValidatorV1, primary_address: vector) { assert!( primary_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -732,7 +732,7 @@ module iota_system::validator { } /// Update primary address of this candidate validator - public(package) fun update_candidate_primary_address(self: &mut Validator, primary_address: vector) { + public(package) fun update_candidate_primary_address(self: &mut ValidatorV1, primary_address: vector) { assert!(is_preactive(self), ENotValidatorCandidate); assert!( primary_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, @@ -744,7 +744,7 @@ module iota_system::validator { } /// Update worker address of this validator, taking effects from next epoch - public(package) fun update_next_epoch_worker_address(self: &mut Validator, worker_address: vector) { + public(package) fun update_next_epoch_worker_address(self: &mut ValidatorV1, worker_address: vector) { assert!( worker_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, EValidatorMetadataExceedingLengthLimit @@ -755,7 +755,7 @@ module iota_system::validator { } /// Update worker address of this candidate validator - public(package) fun update_candidate_worker_address(self: &mut Validator, worker_address: vector) { + public(package) fun update_candidate_worker_address(self: &mut ValidatorV1, worker_address: vector) { assert!(is_preactive(self), ENotValidatorCandidate); assert!( worker_address.length() <= MAX_VALIDATOR_METADATA_LENGTH, @@ -767,14 +767,14 @@ module iota_system::validator { } /// Update protocol public key of this validator, taking effects from next epoch - public(package) fun update_next_epoch_protocol_pubkey(self: &mut Validator, protocol_pubkey: vector, proof_of_possession: vector) { + public(package) fun update_next_epoch_protocol_pubkey(self: &mut ValidatorV1, protocol_pubkey: vector, proof_of_possession: vector) { self.metadata.next_epoch_protocol_pubkey_bytes = option::some(protocol_pubkey); self.metadata.next_epoch_proof_of_possession = option::some(proof_of_possession); validate_metadata(&self.metadata); } /// Update protocol public key of this candidate validator - public(package) fun update_candidate_protocol_pubkey(self: &mut Validator, protocol_pubkey: vector, proof_of_possession: vector) { + public(package) fun update_candidate_protocol_pubkey(self: &mut ValidatorV1, protocol_pubkey: vector, proof_of_possession: vector) { assert!(is_preactive(self), ENotValidatorCandidate); self.metadata.protocol_pubkey_bytes = protocol_pubkey; self.metadata.proof_of_possession = proof_of_possession; @@ -782,26 +782,26 @@ module iota_system::validator { } /// Update network public key of this validator, taking effects from next epoch - public(package) fun update_next_epoch_network_pubkey(self: &mut Validator, network_pubkey: vector) { + public(package) fun update_next_epoch_network_pubkey(self: &mut ValidatorV1, network_pubkey: vector) { self.metadata.next_epoch_network_pubkey_bytes = option::some(network_pubkey); validate_metadata(&self.metadata); } /// Update network public key of this candidate validator - public(package) fun update_candidate_network_pubkey(self: &mut Validator, network_pubkey: vector) { + public(package) fun update_candidate_network_pubkey(self: &mut ValidatorV1, network_pubkey: vector) { assert!(is_preactive(self), ENotValidatorCandidate); self.metadata.network_pubkey_bytes = network_pubkey; validate_metadata(&self.metadata); } /// Update Narwhal worker public key of this validator, taking effects from next epoch - public(package) fun update_next_epoch_worker_pubkey(self: &mut Validator, worker_pubkey: vector) { + public(package) fun update_next_epoch_worker_pubkey(self: &mut ValidatorV1, worker_pubkey: vector) { self.metadata.next_epoch_worker_pubkey_bytes = option::some(worker_pubkey); validate_metadata(&self.metadata); } /// Update Narwhal worker public key of this candidate validator - public(package) fun update_candidate_worker_pubkey(self: &mut Validator, worker_pubkey: vector) { + public(package) fun update_candidate_worker_pubkey(self: &mut ValidatorV1, worker_pubkey: vector) { assert!(is_preactive(self), ENotValidatorCandidate); self.metadata.worker_pubkey_bytes = worker_pubkey; validate_metadata(&self.metadata); @@ -810,7 +810,7 @@ module iota_system::validator { /// Effectutate all staged next epoch metadata for this validator. /// NOTE: this function SHOULD ONLY be called by validator_set when /// advancing an epoch. - public(package) fun effectuate_staged_metadata(self: &mut Validator) { + public(package) fun effectuate_staged_metadata(self: &mut ValidatorV1) { if (next_epoch_network_address(self).is_some()) { self.metadata.net_address = self.metadata.next_epoch_net_address.extract(); self.metadata.next_epoch_net_address = option::none(); @@ -850,30 +850,30 @@ module iota_system::validator { } /// Aborts if validator metadata is valid - public fun validate_metadata(metadata: &ValidatorMetadata) { + public fun validate_metadata(metadata: &ValidatorMetadataV1) { validate_metadata_bcs(bcs::to_bytes(metadata)); } public native fun validate_metadata_bcs(metadata: vector); - public(package) fun get_staking_pool_ref(self: &Validator) : &StakingPool { + public(package) fun get_staking_pool_ref(self: &ValidatorV1) : &StakingPoolV1 { &self.staking_pool } - /// Create a new validator from the given `ValidatorMetadata`, called by both `new` and `new_for_testing`. + /// Create a new validator from the given `ValidatorMetadataV1`, called by both `new` and `new_for_testing`. fun new_from_metadata( - metadata: ValidatorMetadata, + metadata: ValidatorMetadataV1, gas_price: u64, commission_rate: u64, ctx: &mut TxContext - ): Validator { + ): ValidatorV1 { let iota_address = metadata.iota_address; let staking_pool = staking_pool::new(ctx); let operation_cap_id = validator_cap::new_unverified_validator_operation_cap_and_transfer(iota_address, ctx); - Validator { + ValidatorV1 { metadata, // Initialize the voting power to be 0. // At the epoch change where this validator is actually added to the @@ -915,7 +915,7 @@ module iota_system::validator { commission_rate: u64, is_active_at_genesis: bool, ctx: &mut TxContext - ): Validator { + ): ValidatorV1 { let mut validator = new_from_metadata( new_metadata( iota_address, diff --git a/crates/iota-framework/packages/iota-system/sources/validator_set.move b/crates/iota-framework/packages/iota-system/sources/validator_set.move index 649ea8b5cb0..522a3e33e6d 100644 --- a/crates/iota-framework/packages/iota-system/sources/validator_set.move +++ b/crates/iota-framework/packages/iota-system/sources/validator_set.move @@ -6,9 +6,9 @@ module iota_system::validator_set { use iota::balance::Balance; use iota::iota::IOTA; - use iota_system::validator::{Validator, staking_pool_id, iota_address}; + use iota_system::validator::{ValidatorV1, staking_pool_id, iota_address}; use iota_system::validator_cap::{Self, UnverifiedValidatorOperationCap, ValidatorOperationCap}; - use iota_system::staking_pool::{PoolTokenExchangeRate, StakedIota, pool_id}; + use iota_system::staking_pool::{PoolTokenExchangeRateV1, StakedIotaV1, pool_id}; use iota::priority_queue as pq; use iota::vec_map::{Self, VecMap}; use iota::vec_set::VecSet; @@ -21,16 +21,16 @@ module iota_system::validator_set { use iota::bag::Bag; use iota::bag; - public struct ValidatorSet has store { + public struct ValidatorSetV1 has store { /// Total amount of stake from all active validators at the beginning of the epoch. total_stake: u64, /// The current list of active validators. - active_validators: vector, + active_validators: vector, /// List of new validator candidates added during the current epoch. /// They will be processed at the end of the epoch. - pending_active_validators: TableVec, + pending_active_validators: TableVec, /// Removal requests from the validators. Each element is an index /// pointing to `active_validators`. @@ -44,7 +44,7 @@ module iota_system::validator_set { /// is added to this table so that stakers can continue to withdraw their stake from it. inactive_validators: Table, - /// Table storing preactive/candidate validators, mapping their addresses to their `Validator ` structs. + /// Table storing preactive/candidate validators, mapping their addresses to their `ValidatorV1 ` structs. /// When an address calls `request_add_validator_candidate`, they get added to this table and become a preactive /// validator. /// When the candidate has met the min stake requirement, they can call `request_add_validator` to @@ -61,20 +61,7 @@ module iota_system::validator_set { #[allow(unused_field)] /// Event containing staking and rewards related information of /// each validator, emitted during epoch advancement. - public struct ValidatorEpochInfoEvent has copy, drop { - epoch: u64, - validator_address: address, - reference_gas_survey_quote: u64, - stake: u64, - commission_rate: u64, - pool_staking_reward: u64, - pool_token_exchange_rate: PoolTokenExchangeRate, - tallying_rule_reporters: vector
, - tallying_rule_global_score: u64, - } - - /// V2 of ValidatorEpochInfoEvent containing more information about the validator. - public struct ValidatorEpochInfoEventV2 has copy, drop { + public struct ValidatorEpochInfoEventV1 has copy, drop { epoch: u64, validator_address: address, reference_gas_survey_quote: u64, @@ -82,7 +69,7 @@ module iota_system::validator_set { voting_power: u64, commission_rate: u64, pool_staking_reward: u64, - pool_token_exchange_rate: PoolTokenExchangeRate, + pool_token_exchange_rate: PoolTokenExchangeRateV1, tallying_rule_reporters: vector
, tallying_rule_global_score: u64, } @@ -134,7 +121,7 @@ module iota_system::validator_set { // ==== initialization at genesis ==== - public(package) fun new(init_active_validators: vector, ctx: &mut TxContext): ValidatorSet { + public(package) fun new(init_active_validators: vector, ctx: &mut TxContext): ValidatorSetV1 { let total_stake = calculate_total_stakes(&init_active_validators); let mut staking_pool_mappings = table::new(ctx); let num_validators = init_active_validators.length(); @@ -144,7 +131,7 @@ module iota_system::validator_set { staking_pool_mappings.add(staking_pool_id(validator), iota_address(validator)); i = i + 1; }; - let mut validators = ValidatorSet { + let mut validators = ValidatorSetV1 { total_stake, active_validators: init_active_validators, pending_active_validators: table_vec::empty(ctx), @@ -164,8 +151,8 @@ module iota_system::validator_set { /// Called by `iota_system` to add a new validator candidate. public(package) fun request_add_validator_candidate( - self: &mut ValidatorSet, - validator: Validator, + self: &mut ValidatorSetV1, + validator: ValidatorV1, ctx: &mut TxContext, ) { // The next assertions are not critical for the protocol, but they are here to catch problematic configs earlier. @@ -191,7 +178,7 @@ module iota_system::validator_set { } /// Called by `iota_system` to remove a validator candidate, and move them to `inactive_validators`. - public(package) fun request_remove_validator_candidate(self: &mut ValidatorSet, ctx: &mut TxContext) { + public(package) fun request_remove_validator_candidate(self: &mut ValidatorSetV1, ctx: &mut TxContext) { let validator_address = ctx.sender(); assert!( self.validator_candidates.contains(validator_address), @@ -218,7 +205,7 @@ module iota_system::validator_set { /// Called by `iota_system` to add a new validator to `pending_active_validators`, which will be /// processed at the end of epoch. - public(package) fun request_add_validator(self: &mut ValidatorSet, min_joining_stake_amount: u64, ctx: &TxContext) { + public(package) fun request_add_validator(self: &mut ValidatorSetV1, min_joining_stake_amount: u64, ctx: &TxContext) { let validator_address = ctx.sender(); assert!( self.validator_candidates.contains(validator_address), @@ -237,7 +224,7 @@ module iota_system::validator_set { self.pending_active_validators.push_back(validator); } - public(package) fun assert_no_pending_or_active_duplicates(self: &ValidatorSet, validator: &Validator) { + public(package) fun assert_no_pending_or_active_duplicates(self: &ValidatorSetV1, validator: &ValidatorV1) { // Validator here must be active or pending, and thus must be identified as duplicate exactly once. assert!( count_duplicates_vec(&self.active_validators, validator) + @@ -251,7 +238,7 @@ module iota_system::validator_set { /// will be processed at the end of epoch. /// Only an active validator can request to be removed. public(package) fun request_remove_validator( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, ctx: &TxContext, ) { let validator_address = ctx.sender(); @@ -273,11 +260,11 @@ module iota_system::validator_set { /// of the epoch. /// Aborts in case the staking amount is smaller than MIN_STAKING_THRESHOLD public(package) fun request_add_stake( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, validator_address: address, stake: Balance, ctx: &mut TxContext, - ) : StakedIota { + ) : StakedIotaV1 { let iota_amount = stake.value(); assert!(iota_amount >= MIN_STAKING_THRESHOLD, EStakingBelowThreshold); let validator = get_candidate_or_active_validator_mut(self, validator_address); @@ -291,8 +278,8 @@ module iota_system::validator_set { /// 2. If the `staked_iota` was staked with a validator that is no longer active, /// the stake and any rewards corresponding to it will be immediately processed. public(package) fun request_withdraw_stake( - self: &mut ValidatorSet, - staked_iota: StakedIota, + self: &mut ValidatorSetV1, + staked_iota: StakedIotaV1, ctx: &TxContext, ) : Balance { let staking_pool_id = pool_id(&staked_iota); @@ -311,7 +298,7 @@ module iota_system::validator_set { // ==== validator config setting functions ==== public(package) fun request_set_commission_rate( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, new_commission_rate: u64, ctx: &TxContext, ) { @@ -331,7 +318,7 @@ module iota_system::validator_set { /// 4. Process pending validator application and withdraws. /// 5. At the end, we calculate the total stake for the new epoch. public(package) fun advance_epoch( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, total_validator_rewards: &mut Balance, validator_report_records: &mut VecMap>, reward_slashing_rate: u64, @@ -420,7 +407,7 @@ module iota_system::validator_set { } fun update_and_process_low_stake_departures( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, low_stake_threshold: u64, very_low_stake_threshold: u64, low_stake_grace_period: u64, @@ -466,7 +453,7 @@ module iota_system::validator_set { /// Effectutate pending next epoch metadata if they are staged. fun effectuate_staged_metadata( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, ) { let num_validators = self.active_validators.length(); let mut i = 0; @@ -481,7 +468,7 @@ module iota_system::validator_set { /// Derive the reference gas price based on the gas price quote submitted by each validator. /// The returned gas price should be greater than or equal to 2/3 of the validators submitted /// gas price, weighted by stake. - public fun derive_reference_gas_price(self: &ValidatorSet): u64 { + public fun derive_reference_gas_price(self: &ValidatorSetV1): u64 { let vs = &self.active_validators; let num_validators = vs.length(); let mut entries = vector[]; @@ -508,37 +495,37 @@ module iota_system::validator_set { // ==== getter functions ==== - public fun total_stake(self: &ValidatorSet): u64 { + public fun total_stake(self: &ValidatorSetV1): u64 { self.total_stake } - public fun validator_total_stake_amount(self: &ValidatorSet, validator_address: address): u64 { + public fun validator_total_stake_amount(self: &ValidatorSetV1, validator_address: address): u64 { let validator = get_validator_ref(&self.active_validators, validator_address); validator.total_stake_amount() } - public fun validator_stake_amount(self: &ValidatorSet, validator_address: address): u64 { + public fun validator_stake_amount(self: &ValidatorSetV1, validator_address: address): u64 { let validator = get_validator_ref(&self.active_validators, validator_address); validator.stake_amount() } - public fun validator_voting_power(self: &ValidatorSet, validator_address: address): u64 { + public fun validator_voting_power(self: &ValidatorSetV1, validator_address: address): u64 { let validator = get_validator_ref(&self.active_validators, validator_address); validator.voting_power() } - public fun validator_staking_pool_id(self: &ValidatorSet, validator_address: address): ID { + public fun validator_staking_pool_id(self: &ValidatorSetV1, validator_address: address): ID { let validator = get_validator_ref(&self.active_validators, validator_address); validator.staking_pool_id() } - public fun staking_pool_mappings(self: &ValidatorSet): &Table { + public fun staking_pool_mappings(self: &ValidatorSetV1): &Table { &self.staking_pool_mappings } public(package) fun pool_exchange_rates( - self: &mut ValidatorSet, pool_id: &ID - ) : &Table { + self: &mut ValidatorSetV1, pool_id: &ID + ) : &Table { let validator = // If the pool id is recorded in the mapping, then it must be either candidate or active. if (self.staking_pool_mappings.contains(*pool_id)) { @@ -552,13 +539,13 @@ module iota_system::validator_set { } /// Get the total number of validators in the next epoch. - public(package) fun next_epoch_validator_count(self: &ValidatorSet): u64 { + public(package) fun next_epoch_validator_count(self: &ValidatorSetV1): u64 { self.active_validators.length() - self.pending_removals.length() + self.pending_active_validators.length() } /// Returns true iff the address exists in active validators. public(package) fun is_active_validator_by_iota_address( - self: &ValidatorSet, + self: &ValidatorSetV1, validator_address: address, ): bool { find_validator(&self.active_validators, validator_address).is_some() @@ -569,15 +556,15 @@ module iota_system::validator_set { /// Checks whether `new_validator` is duplicate with any currently active validators. /// It differs from `is_active_validator_by_iota_address` in that the former checks /// only the iota address but this function looks at more metadata. - fun is_duplicate_with_active_validator(self: &ValidatorSet, new_validator: &Validator): bool { + fun is_duplicate_with_active_validator(self: &ValidatorSetV1, new_validator: &ValidatorV1): bool { is_duplicate_validator(&self.active_validators, new_validator) } - public(package) fun is_duplicate_validator(validators: &vector, new_validator: &Validator): bool { + public(package) fun is_duplicate_validator(validators: &vector, new_validator: &ValidatorV1): bool { count_duplicates_vec(validators, new_validator) > 0 } - fun count_duplicates_vec(validators: &vector, validator: &Validator): u64 { + fun count_duplicates_vec(validators: &vector, validator: &ValidatorV1): u64 { let len = validators.length(); let mut i = 0; let mut result = 0; @@ -592,11 +579,11 @@ module iota_system::validator_set { } /// Checks whether `new_validator` is duplicate with any currently pending validators. - fun is_duplicate_with_pending_validator(self: &ValidatorSet, new_validator: &Validator): bool { + fun is_duplicate_with_pending_validator(self: &ValidatorSetV1, new_validator: &ValidatorV1): bool { count_duplicates_tablevec(&self.pending_active_validators, new_validator) > 0 } - fun count_duplicates_tablevec(validators: &TableVec, validator: &Validator): u64 { + fun count_duplicates_tablevec(validators: &TableVec, validator: &ValidatorV1): u64 { let len = validators.length(); let mut i = 0; let mut result = 0; @@ -611,7 +598,7 @@ module iota_system::validator_set { } /// Get mutable reference to either a candidate or an active validator by address. - fun get_candidate_or_active_validator_mut(self: &mut ValidatorSet, validator_address: address): &mut Validator { + fun get_candidate_or_active_validator_mut(self: &mut ValidatorSetV1, validator_address: address): &mut ValidatorV1 { if (self.validator_candidates.contains(validator_address)) { let wrapper = &mut self.validator_candidates[validator_address]; return wrapper.load_validator_maybe_upgrade() @@ -622,7 +609,7 @@ module iota_system::validator_set { /// Find validator by `validator_address`, in `validators`. /// Returns (true, index) if the validator is found, and the index is its index in the list. /// If not found, returns (false, 0). - fun find_validator(validators: &vector, validator_address: address): Option { + fun find_validator(validators: &vector, validator_address: address): Option { let length = validators.length(); let mut i = 0; while (i < length) { @@ -638,7 +625,7 @@ module iota_system::validator_set { /// Find validator by `validator_address`, in `validators`. /// Returns (true, index) if the validator is found, and the index is its index in the list. /// If not found, returns (false, 0). - fun find_validator_from_table_vec(validators: &TableVec, validator_address: address): Option { + fun find_validator_from_table_vec(validators: &TableVec, validator_address: address): Option { let length = validators.length(); let mut i = 0; while (i < length) { @@ -654,7 +641,7 @@ module iota_system::validator_set { /// Given a vector of validator addresses, return their indices in the validator set. /// Aborts if any address isn't in the given validator set. - fun get_validator_indices(validators: &vector, validator_addresses: &vector
): vector { + fun get_validator_indices(validators: &vector, validator_addresses: &vector
): vector { let length = validator_addresses.length(); let mut i = 0; let mut res = vector[]; @@ -669,9 +656,9 @@ module iota_system::validator_set { } public(package) fun get_validator_mut( - validators: &mut vector, + validators: &mut vector, validator_address: address, - ): &mut Validator { + ): &mut ValidatorV1 { let mut validator_index_opt = find_validator(validators, validator_address); assert!(validator_index_opt.is_some(), ENotAValidator); let validator_index = validator_index_opt.extract(); @@ -681,12 +668,12 @@ module iota_system::validator_set { /// Get mutable reference to an active or (if active does not exist) pending or (if pending and /// active do not exist) or candidate validator by address. /// Note: this function should be called carefully, only after verifying the transaction - /// sender has the ability to modify the `Validator`. + /// sender has the ability to modify the `ValidatorV1`. fun get_active_or_pending_or_candidate_validator_mut( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, validator_address: address, include_candidate: bool, - ): &mut Validator { + ): &mut ValidatorV1 { let mut validator_index_opt = find_validator(&self.active_validators, validator_address); if (validator_index_opt.is_some()) { let validator_index = validator_index_opt.extract(); @@ -704,33 +691,33 @@ module iota_system::validator_set { } public(package) fun get_validator_mut_with_verified_cap( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, verified_cap: &ValidatorOperationCap, include_candidate: bool, - ): &mut Validator { + ): &mut ValidatorV1 { get_active_or_pending_or_candidate_validator_mut(self, *verified_cap.verified_operation_cap_address(), include_candidate) } public(package) fun get_validator_mut_with_ctx( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, ctx: &TxContext, - ): &mut Validator { + ): &mut ValidatorV1 { let validator_address = ctx.sender(); get_active_or_pending_or_candidate_validator_mut(self, validator_address, false) } public(package) fun get_validator_mut_with_ctx_including_candidates( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, ctx: &TxContext, - ): &mut Validator { + ): &mut ValidatorV1 { let validator_address = ctx.sender(); get_active_or_pending_or_candidate_validator_mut(self, validator_address, true) } fun get_validator_ref( - validators: &vector, + validators: &vector, validator_address: address, - ): &Validator { + ): &ValidatorV1 { let mut validator_index_opt = find_validator(validators, validator_address); assert!(validator_index_opt.is_some(), ENotAValidator); let validator_index = validator_index_opt.extract(); @@ -738,10 +725,10 @@ module iota_system::validator_set { } public(package) fun get_active_or_pending_or_candidate_validator_ref( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, validator_address: address, which_validator: u8, - ): &Validator { + ): &ValidatorV1 { let mut validator_index_opt = find_validator(&self.active_validators, validator_address); if (validator_index_opt.is_some() || which_validator == ACTIVE_VALIDATOR_ONLY) { let validator_index = validator_index_opt.extract(); @@ -756,9 +743,9 @@ module iota_system::validator_set { } public fun get_active_validator_ref( - self: &ValidatorSet, + self: &ValidatorSetV1, validator_address: address, - ): &Validator { + ): &ValidatorV1 { let mut validator_index_opt = find_validator(&self.active_validators, validator_address); assert!(validator_index_opt.is_some(), ENotAValidator); let validator_index = validator_index_opt.extract(); @@ -766,9 +753,9 @@ module iota_system::validator_set { } public fun get_pending_validator_ref( - self: &ValidatorSet, + self: &ValidatorSetV1, validator_address: address, - ): &Validator { + ): &ValidatorV1 { let mut validator_index_opt = find_validator_from_table_vec(&self.pending_active_validators, validator_address); assert!(validator_index_opt.is_some(), ENotAPendingValidator); let validator_index = validator_index_opt.extract(); @@ -777,9 +764,9 @@ module iota_system::validator_set { #[test_only] public fun get_candidate_validator_ref( - self: &ValidatorSet, + self: &ValidatorSetV1, validator_address: address, - ): &Validator { + ): &ValidatorV1 { self.validator_candidates[validator_address].get_inner_validator_ref() } @@ -787,7 +774,7 @@ module iota_system::validator_set { /// If `active_validator_only` is true, only verify the Cap for an active validator. /// Otherwise, verify the Cap for au either active or pending validator. public(package) fun verify_cap( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, cap: &UnverifiedValidatorOperationCap, which_validator: u8, ): ValidatorOperationCap { @@ -804,7 +791,7 @@ module iota_system::validator_set { /// Process the pending withdraw requests. For each pending request, the validator /// is removed from `validators` and its staking pool is put into the `inactive_validators` table. fun process_pending_removals( - self: &mut ValidatorSet, + self: &mut ValidatorSetV1, validator_report_records: &mut VecMap>, ctx: &mut TxContext, ) { @@ -817,8 +804,8 @@ module iota_system::validator_set { } fun process_validator_departure( - self: &mut ValidatorSet, - mut validator: Validator, + self: &mut ValidatorSetV1, + mut validator: ValidatorV1, validator_report_records: &mut VecMap>, is_voluntary: bool, ctx: &mut TxContext, @@ -882,7 +869,7 @@ module iota_system::validator_set { /// Process the pending new validators. They are activated and inserted into `validators`. fun process_pending_validators( - self: &mut ValidatorSet, new_epoch: u64, + self: &mut ValidatorSetV1, new_epoch: u64, ) { while (!self.pending_active_validators.is_empty()) { let mut validator = self.pending_active_validators.pop_back(); @@ -919,7 +906,7 @@ module iota_system::validator_set { /// Process all active validators' pending stake deposits and withdraws. fun process_pending_stakes_and_withdraws( - validators: &mut vector, ctx: &TxContext + validators: &mut vector, ctx: &TxContext ) { let length = validators.length(); let mut i = 0; @@ -931,7 +918,7 @@ module iota_system::validator_set { } /// Calculate the total active validator stake. - fun calculate_total_stakes(validators: &vector): u64 { + fun calculate_total_stakes(validators: &vector): u64 { let mut stake = 0; let length = validators.length(); let mut i = 0; @@ -944,7 +931,7 @@ module iota_system::validator_set { } /// Process the pending stake changes for each validator. - fun adjust_stake_and_gas_price(validators: &mut vector) { + fun adjust_stake_and_gas_price(validators: &mut vector) { let length = validators.length(); let mut i = 0; while (i < length) { @@ -986,7 +973,7 @@ module iota_system::validator_set { /// Process the validator report records of the epoch and return the addresses of the /// non-performant validators according to the input threshold. fun compute_slashed_validators( - self: &ValidatorSet, + self: &ValidatorSetV1, mut validator_report_records: VecMap>, ): vector
{ let mut slashed_validators = vector[]; @@ -1011,7 +998,7 @@ module iota_system::validator_set { /// account the tallying rule results. /// Returns the unadjusted amounts of staking reward for each validator. fun compute_unadjusted_reward_distribution( - validators: &vector, + validators: &vector, total_voting_power: u64, total_staking_reward: u64, ): vector { @@ -1035,7 +1022,7 @@ module iota_system::validator_set { /// Returns the staking rewards each validator gets. /// The staking rewards are shared with the stakers. fun compute_adjusted_reward_distribution( - validators: &vector, + validators: &vector, total_voting_power: u64, total_slashed_validator_voting_power: u64, unadjusted_staking_reward_amounts: vector, @@ -1078,7 +1065,7 @@ module iota_system::validator_set { } fun distribute_reward( - validators: &mut vector, + validators: &mut vector, adjusted_staking_reward_amounts: &vector, staking_rewards: &mut Balance, ctx: &mut TxContext @@ -1116,7 +1103,7 @@ module iota_system::validator_set { /// including stakes, rewards, performance, etc. fun emit_validator_epoch_events( new_epoch: u64, - vs: &vector, + vs: &vector, pool_staking_reward_amounts: &vector, report_records: &VecMap>, slashed_validators: &vector
, @@ -1136,7 +1123,7 @@ module iota_system::validator_set { if (slashed_validators.contains(&validator_address)) 0 else 1; event::emit( - ValidatorEpochInfoEventV2 { + ValidatorEpochInfoEventV1 { epoch: new_epoch, validator_address, reference_gas_survey_quote: v.gas_price(), @@ -1154,7 +1141,7 @@ module iota_system::validator_set { } /// Sum up the total stake of a given list of validator addresses. - public fun sum_voting_power_by_addresses(vs: &vector, addresses: &vector
): u64 { + public fun sum_voting_power_by_addresses(vs: &vector, addresses: &vector
): u64 { let mut sum = 0; let mut i = 0; let length = addresses.length(); @@ -1167,21 +1154,21 @@ module iota_system::validator_set { } /// Return the active validators in `self` - public fun active_validators(self: &ValidatorSet): &vector { + public fun active_validators(self: &ValidatorSetV1): &vector { &self.active_validators } /// Returns true if the `addr` is a validator candidate. - public fun is_validator_candidate(self: &ValidatorSet, addr: address): bool { + public fun is_validator_candidate(self: &ValidatorSetV1, addr: address): bool { self.validator_candidates.contains(addr) } /// Returns true if the staking pool identified by `staking_pool_id` is of an inactive validator. - public fun is_inactive_validator(self: &ValidatorSet, staking_pool_id: ID): bool { + public fun is_inactive_validator(self: &ValidatorSetV1, staking_pool_id: ID): bool { self.inactive_validators.contains(staking_pool_id) } - public(package) fun active_validator_addresses(self: &ValidatorSet): vector
{ + public(package) fun active_validator_addresses(self: &ValidatorSetV1): vector
{ let vs = &self.active_validators; let mut res = vector[]; let mut i = 0; diff --git a/crates/iota-framework/packages/iota-system/sources/validator_wrapper.move b/crates/iota-framework/packages/iota-system/sources/validator_wrapper.move index 81e0d53100a..56f63e27b36 100644 --- a/crates/iota-framework/packages/iota-system/sources/validator_wrapper.move +++ b/crates/iota-framework/packages/iota-system/sources/validator_wrapper.move @@ -4,7 +4,7 @@ module iota_system::validator_wrapper { use iota::versioned::Versioned; - use iota_system::validator::Validator; + use iota_system::validator::ValidatorV1; use iota::versioned; const EInvalidVersion: u64 = 0; @@ -13,8 +13,8 @@ module iota_system::validator_wrapper { inner: Versioned } - // Validator corresponds to version 1. - public(package) fun create_v1(validator: Validator, ctx: &mut TxContext): ValidatorWrapper { + // ValidatorV1 corresponds to version 1. + public(package) fun create_v1(validator: ValidatorV1, ctx: &mut TxContext): ValidatorWrapper { ValidatorWrapper { inner: versioned::create(1, validator, ctx) } @@ -22,13 +22,13 @@ module iota_system::validator_wrapper { /// This function should always return the latest supported version. /// If the inner version is old, we upgrade it lazily in-place. - public(package) fun load_validator_maybe_upgrade(self: &mut ValidatorWrapper): &mut Validator { + public(package) fun load_validator_maybe_upgrade(self: &mut ValidatorWrapper): &mut ValidatorV1 { upgrade_to_latest(self); versioned::load_value_mut(&mut self.inner) } /// Destroy the wrapper and retrieve the inner validator object. - public(package) fun destroy(self: ValidatorWrapper): Validator { + public(package) fun destroy(self: ValidatorWrapper): ValidatorV1 { upgrade_to_latest(&self); let ValidatorWrapper { inner } = self; versioned::destroy(inner) @@ -36,7 +36,7 @@ module iota_system::validator_wrapper { #[test_only] /// Load the inner validator with assumed type. This should be used for testing only. - public(package) fun get_inner_validator_ref(self: &ValidatorWrapper): &Validator { + public(package) fun get_inner_validator_ref(self: &ValidatorWrapper): &ValidatorV1 { versioned::load_value(&self.inner) } diff --git a/crates/iota-framework/packages/iota-system/sources/voting_power.move b/crates/iota-framework/packages/iota-system/sources/voting_power.move index d5a0cc68b8b..2c839fe6a84 100644 --- a/crates/iota-framework/packages/iota-system/sources/voting_power.move +++ b/crates/iota-framework/packages/iota-system/sources/voting_power.move @@ -3,16 +3,9 @@ // SPDX-License-Identifier: Apache-2.0 module iota_system::voting_power { - use iota_system::validator::Validator; + use iota_system::validator::ValidatorV1; - #[allow(unused_field)] - /// Deprecated. Use VotingPowerInfoV2 instead. - public struct VotingPowerInfo has drop { - validator_index: u64, - voting_power: u64, - } - - public struct VotingPowerInfoV2 has drop { + public struct VotingPowerInfoV1 has drop { validator_index: u64, voting_power: u64, stake: u64, @@ -41,7 +34,7 @@ module iota_system::voting_power { /// Set the voting power of all validators. /// Each validator's voting power is initialized using their stake. We then attempt to cap their voting power /// at `MAX_VOTING_POWER`. If `MAX_VOTING_POWER` is not a feasible cap, we pick the lowest possible cap. - public(package) fun set_voting_power(validators: &mut vector) { + public(package) fun set_voting_power(validators: &mut vector) { // If threshold_pct is too small, it's possible that even when all validators reach the threshold we still don't // have 100%. So we bound the threshold_pct to be always enough to find a solution. let threshold = TOTAL_VOTING_POWER.min( @@ -58,9 +51,9 @@ module iota_system::voting_power { /// descending order using voting power. /// Anything beyond the threshold is added to the remaining_power, which is also returned. fun init_voting_power_info( - validators: &vector, + validators: &vector, threshold: u64, - ): (vector, u64) { + ): (vector, u64) { let total_stake = total_stake(validators); let mut i = 0; let len = validators.length(); @@ -71,7 +64,7 @@ module iota_system::voting_power { let stake = validator.total_stake(); let adjusted_stake = stake as u128 * (TOTAL_VOTING_POWER as u128) / (total_stake as u128); let voting_power = (adjusted_stake as u64).min(threshold); - let info = VotingPowerInfoV2 { + let info = VotingPowerInfoV1 { validator_index: i, voting_power, stake, @@ -84,7 +77,7 @@ module iota_system::voting_power { } /// Sum up the total stake of all validators. - fun total_stake(validators: &vector): u64 { + fun total_stake(validators: &vector): u64 { let mut i = 0; let len = validators.length(); let mut total_stake =0 ; @@ -97,7 +90,7 @@ module iota_system::voting_power { /// Insert `new_info` to `info_list` as part of insertion sort, such that `info_list` is always sorted /// using stake, in descending order. - fun insert(info_list: &mut vector, new_info: VotingPowerInfoV2) { + fun insert(info_list: &mut vector, new_info: VotingPowerInfoV1) { let mut i = 0; let len = info_list.length(); while (i < len && info_list[i].stake > new_info.stake) { @@ -107,7 +100,7 @@ module iota_system::voting_power { } /// Distribute remaining_power to validators that are not capped at threshold. - fun adjust_voting_power(info_list: &mut vector, threshold: u64, mut remaining_power: u64) { + fun adjust_voting_power(info_list: &mut vector, threshold: u64, mut remaining_power: u64) { let mut i = 0; let len = info_list.length(); while (i < len && remaining_power > 0) { @@ -127,9 +120,9 @@ module iota_system::voting_power { } /// Update validators with the decided voting power. - fun update_voting_power(validators: &mut vector, mut info_list: vector) { + fun update_voting_power(validators: &mut vector, mut info_list: vector) { while (!info_list.is_empty()) { - let VotingPowerInfoV2 { + let VotingPowerInfoV1 { validator_index, voting_power, stake: _, @@ -141,7 +134,7 @@ module iota_system::voting_power { } /// Check a few invariants that must hold after setting the voting power. - fun check_invariants(v: &vector) { + fun check_invariants(v: &vector) { // First check that the total voting power must be TOTAL_VOTING_POWER. let mut i = 0; let len = v.length(); diff --git a/crates/iota-framework/packages/iota-system/tests/delegation_tests.move b/crates/iota-framework/packages/iota-system/tests/delegation_tests.move index 7a233847c4f..0b1bbecce25 100644 --- a/crates/iota-framework/packages/iota-system/tests/delegation_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/delegation_tests.move @@ -7,7 +7,7 @@ module iota_system::stake_tests { use iota::coin; use iota::test_scenario; use iota_system::iota_system::IotaSystemState; - use iota_system::staking_pool::{Self, StakedIota, PoolTokenExchangeRate}; + use iota_system::staking_pool::{Self, StakedIotaV1, PoolTokenExchangeRateV1}; use iota::test_utils::assert_eq; use iota_system::validator_set; use iota::test_utils; @@ -45,7 +45,7 @@ module iota_system::stake_tests { #[test] fun test_split_join_staked_iota() { - // All this is just to generate a dummy StakedIota object to split and join later + // All this is just to generate a dummy StakedIotaV1 object to split and join later set_up_iota_system_state(); let mut scenario_val = test_scenario::begin(STAKER_ADDR_1); let scenario = &mut scenario_val; @@ -53,7 +53,7 @@ module iota_system::stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); staked_iota.split_to_sender(20 * NANOS_PER_IOTA, ctx); scenario.return_to_sender(staked_iota); @@ -62,12 +62,12 @@ module iota_system::stake_tests { // Verify the correctness of the split and send the join txn scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); + let staked_iota_ids = scenario.ids_for_sender(); assert!(staked_iota_ids.length() == 2); // staked iota split to 2 coins - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); let amount1 = part1.amount(); let amount2 = part2.amount(); @@ -98,9 +98,9 @@ module iota_system::stake_tests { // Verify that these cannot be merged scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let staked_iota_ids = scenario.ids_for_sender(); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); part1.join(part2); @@ -120,7 +120,7 @@ module iota_system::stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); // The remaining amount after splitting is below the threshold so this should fail. staked_iota.split_to_sender(1 * NANOS_PER_IOTA + 1, ctx); @@ -140,7 +140,7 @@ module iota_system::stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); // The remaining amount after splitting is below the threshold so this should fail. let stake = staked_iota.split(1 * NANOS_PER_IOTA + 1, ctx); @@ -179,7 +179,7 @@ module iota_system::stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert!(staked_iota.amount() == 60 * NANOS_PER_IOTA); @@ -255,7 +255,7 @@ module iota_system::stake_tests { assert!(!system_state_mut_ref.validators().is_active_validator_by_iota_address(VALIDATOR_ADDR_1)); - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 100 * NANOS_PER_IOTA); // Unstake from VALIDATOR_ADDR_1 @@ -304,7 +304,7 @@ module iota_system::stake_tests { let mut system_state = scenario.take_shared(); let system_state_mut_ref = &mut system_state; - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 100 * NANOS_PER_IOTA); // Unstake from VALIDATOR_ADDR_1 @@ -517,7 +517,7 @@ module iota_system::stake_tests { let scenario = &mut scenario_val; stake_with(@0x42, @0x2, 100, scenario); // stakes 100 IOTA with 0x2 scenario.next_tx(@0x42); - let staked_iota = scenario.take_from_address(@0x42); + let staked_iota = scenario.take_from_address(@0x42); let pool_id = staked_iota.pool_id(); test_scenario::return_to_address(@0x42, staked_iota); advance_epoch(scenario); // advances epoch to effectuate the stake @@ -534,7 +534,7 @@ module iota_system::stake_tests { } fun assert_exchange_rate_eq( - rates: &Table, epoch: u64, iota_amount: u64, pool_token_amount: u64 + rates: &Table, epoch: u64, iota_amount: u64, pool_token_amount: u64 ) { let rate = &rates[epoch]; assert_eq(rate.iota_amount(), iota_amount * NANOS_PER_IOTA); diff --git a/crates/iota-framework/packages/iota-system/tests/governance_test_utils.move b/crates/iota-framework/packages/iota-system/tests/governance_test_utils.move index 16e2c2a071a..86fc56f561a 100644 --- a/crates/iota-framework/packages/iota-system/tests/governance_test_utils.move +++ b/crates/iota-framework/packages/iota-system/tests/governance_test_utils.move @@ -8,9 +8,9 @@ module iota_system::governance_test_utils { use iota::balance; use iota::iota::{Self, IOTA}; use iota::coin::{Self, Coin}; - use iota_system::staking_pool::{StakedIota, StakingPool}; + use iota_system::staking_pool::{StakedIotaV1, StakingPoolV1}; use iota::test_utils::assert_eq; - use iota_system::validator::{Self, Validator}; + use iota_system::validator::{Self, ValidatorV1}; use iota_system::iota_system::{Self, IotaSystemState}; use iota_system::iota_system_state_inner; use iota::test_scenario::{Self, Scenario}; @@ -22,7 +22,7 @@ module iota_system::governance_test_utils { public fun create_validator_for_testing( addr: address, init_stake_amount_in_iota: u64, ctx: &mut TxContext - ): Validator { + ): ValidatorV1 { let validator = validator::new_for_testing( addr, x"AA", @@ -47,7 +47,7 @@ module iota_system::governance_test_utils { } /// Create a validator set with the given stake amounts - public fun create_validators_with_stakes(stakes: vector, ctx: &mut TxContext): vector { + public fun create_validators_with_stakes(stakes: vector, ctx: &mut TxContext): vector { let mut i = 0; let mut validators = vector[]; while (i < stakes.length()) { @@ -59,7 +59,7 @@ module iota_system::governance_test_utils { } public fun create_iota_system_state_for_testing( - validators: vector, iota_supply_amount: u64, storage_fund_amount: u64, ctx: &mut TxContext + validators: vector, iota_supply_amount: u64, storage_fund_amount: u64, ctx: &mut TxContext ) { let system_parameters = iota_system_state_inner::create_system_parameters( 42, // epoch_duration_ms, doesn't matter what number we put here @@ -188,7 +188,7 @@ module iota_system::governance_test_utils { staker: address, staked_iota_idx: u64, scenario: &mut Scenario ) { scenario.next_tx(staker); - let stake_iota_ids = scenario.ids_for_sender(); + let stake_iota_ids = scenario.ids_for_sender(); let staked_iota = scenario.take_from_sender_by_id(stake_iota_ids[staked_iota_idx]); let mut system_state = scenario.take_shared(); @@ -328,15 +328,15 @@ module iota_system::governance_test_utils { amount } - public fun stake_plus_current_rewards(addr: address, staking_pool: &StakingPool, scenario: &mut Scenario): u64 { + public fun stake_plus_current_rewards(addr: address, staking_pool: &StakingPoolV1, scenario: &mut Scenario): u64 { let mut sum = 0; scenario.next_tx(addr); - let mut stake_ids = scenario.ids_for_sender(); + let mut stake_ids = scenario.ids_for_sender(); let current_epoch = scenario.ctx().epoch(); while (!stake_ids.is_empty()) { let staked_iota_id = stake_ids.pop_back(); - let staked_iota = scenario.take_from_sender_by_id(staked_iota_id); + let staked_iota = scenario.take_from_sender_by_id(staked_iota_id); sum = sum + staking_pool.calculate_rewards(&staked_iota, current_epoch); scenario.return_to_sender(staked_iota); }; diff --git a/crates/iota-framework/packages/iota-system/tests/iota_system_tests.move b/crates/iota-framework/packages/iota-system/tests/iota_system_tests.move index a8cc154be70..fba2dc51c51 100644 --- a/crates/iota-framework/packages/iota-system/tests/iota_system_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/iota_system_tests.move @@ -13,7 +13,7 @@ module iota_system::iota_system_tests { use iota_system::governance_test_utils::{add_validator_full_flow, advance_epoch, remove_validator, set_up_iota_system_state, create_iota_system_state_for_testing, stake_with, unstake}; use iota_system::iota_system::IotaSystemState; use iota_system::iota_system_state_inner; - use iota_system::validator::{Self, Validator}; + use iota_system::validator::{Self, ValidatorV1}; use iota_system::validator_set; use iota_system::validator_cap::UnverifiedValidatorOperationCap; use iota::balance; @@ -383,7 +383,7 @@ module iota_system::iota_system_tests { fun verify_candidate( - validator: &Validator, + validator: &ValidatorV1, name: vector, protocol_pub_key: vector, pop: vector, @@ -442,7 +442,7 @@ module iota_system::iota_system_tests { } fun verify_metadata( - validator: &Validator, + validator: &ValidatorV1, name: vector, protocol_pub_key: vector, pop: vector, @@ -495,7 +495,7 @@ module iota_system::iota_system_tests { } fun verify_current_epoch_metadata( - validator: &Validator, + validator: &ValidatorV1, name: vector, protocol_pub_key: vector, pop: vector, @@ -523,7 +523,7 @@ module iota_system::iota_system_tests { fun verify_metadata_after_advancing_epoch( - validator: &Validator, + validator: &ValidatorV1, name: vector, protocol_pub_key: vector, pop: vector, diff --git a/crates/iota-framework/packages/iota-system/tests/timelocked_delegation_tests.move b/crates/iota-framework/packages/iota-system/tests/timelocked_delegation_tests.move index 2b2ba3689d0..de7a4395232 100644 --- a/crates/iota-framework/packages/iota-system/tests/timelocked_delegation_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/timelocked_delegation_tests.move @@ -14,8 +14,8 @@ module iota_system::timelocked_stake_tests { use iota::test_utils; use iota_system::iota_system::IotaSystemState; - use iota_system::staking_pool::{Self, PoolTokenExchangeRate}; - use iota_system::validator_set::{Self, ValidatorSet}; + use iota_system::staking_pool::{Self, PoolTokenExchangeRateV1}; + use iota_system::validator_set::{Self, ValidatorSetV1}; use iota_system::governance_test_utils::{ add_validator, add_validator_candidate, @@ -30,7 +30,7 @@ module iota_system::timelocked_stake_tests { total_iota_balance, unstake, }; - use iota_system::timelocked_staking::{Self, TimelockedStakedIota}; + use iota_system::timelocked_staking::{Self, TimelockedStakedIotaV1}; use iota::labeler::LabelerCap; use iota::timelock::{Self, TimeLock}; @@ -63,7 +63,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); staked_iota.split_to_sender(20 * NANOS_PER_IOTA, ctx); scenario.return_to_sender(staked_iota); @@ -72,11 +72,11 @@ module iota_system::timelocked_stake_tests { // Verify the correctness of the split and send the join txn scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); + let staked_iota_ids = scenario.ids_for_sender(); assert!(staked_iota_ids.length() == 2, 101); // staked iota split to 2 coins - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); let amount1 = part1.amount(); let amount2 = part2.amount(); @@ -105,9 +105,9 @@ module iota_system::timelocked_stake_tests { // Verify that these cannot be merged scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let staked_iota_ids = scenario.ids_for_sender(); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); part1.join(part2); @@ -129,9 +129,9 @@ module iota_system::timelocked_stake_tests { // Verify that these cannot be merged scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let staked_iota_ids = scenario.ids_for_sender(); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); part1.join(part2); @@ -163,9 +163,9 @@ module iota_system::timelocked_stake_tests { // Verify that these can be merged scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let staked_iota_ids = scenario.ids_for_sender(); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); part1.join(part2); @@ -204,9 +204,9 @@ module iota_system::timelocked_stake_tests { // Verify that these cannot be merged scenario.next_tx(STAKER_ADDR_1); { - let staked_iota_ids = scenario.ids_for_sender(); - let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); - let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); + let staked_iota_ids = scenario.ids_for_sender(); + let mut part1 = scenario.take_from_sender_by_id(staked_iota_ids[0]); + let part2 = scenario.take_from_sender_by_id(staked_iota_ids[1]); part1.join(part2); @@ -239,7 +239,7 @@ module iota_system::timelocked_stake_tests { // Verify that it can be split scenario.next_tx(STAKER_ADDR_1); { - let mut original = scenario.take_from_sender(); + let mut original = scenario.take_from_sender(); let split = original.split(20 * NANOS_PER_IOTA, scenario.ctx()); assert_eq(original.staked_iota_amount(), 40 * NANOS_PER_IOTA); @@ -267,7 +267,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); // The remaining amount after splitting is below the threshold so this should fail. staked_iota.split_to_sender(1 * NANOS_PER_IOTA + 1, ctx); @@ -287,7 +287,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let mut staked_iota = scenario.take_from_sender(); + let mut staked_iota = scenario.take_from_sender(); let ctx = scenario.ctx(); // The remaining amount after splitting is below the threshold so this should fail. let stake = staked_iota.split(1 * NANOS_PER_IOTA + 1, ctx); @@ -328,7 +328,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 60 * NANOS_PER_IOTA); let mut system_state = scenario.take_shared(); @@ -394,7 +394,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 60 * NANOS_PER_IOTA); let mut system_state = scenario.take_shared(); @@ -468,11 +468,11 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let stake_iota_ids = scenario.ids_for_sender(); + let stake_iota_ids = scenario.ids_for_sender(); - let staked_iota1 = scenario.take_from_sender_by_id(stake_iota_ids[0]); + let staked_iota1 = scenario.take_from_sender_by_id(stake_iota_ids[0]); assert_eq(staked_iota1.amount(), 30 * NANOS_PER_IOTA); - let staked_iota2 = scenario.take_from_sender_by_id(stake_iota_ids[1]); + let staked_iota2 = scenario.take_from_sender_by_id(stake_iota_ids[1]); assert_eq(staked_iota2.amount(), 60 * NANOS_PER_IOTA); let mut system_state = scenario.take_shared(); @@ -496,7 +496,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 60 * NANOS_PER_IOTA); let mut system_state = scenario.take_shared(); @@ -518,7 +518,7 @@ module iota_system::timelocked_stake_tests { scenario.next_tx(STAKER_ADDR_1); { - assert_eq(scenario.has_most_recent_for_sender(), false); + assert_eq(scenario.has_most_recent_for_sender(), false); let mut system_state = scenario.take_shared(); let system_state_mut_ref = &mut system_state; @@ -562,7 +562,7 @@ module iota_system::timelocked_stake_tests { assert!(!is_active_validator_by_iota_address(system_state_mut_ref.validators(), VALIDATOR_ADDR_1), 0); - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 100 * NANOS_PER_IOTA); // Unstake from VALIDATOR_ADDR_1 @@ -620,7 +620,7 @@ module iota_system::timelocked_stake_tests { assert!(!is_active_validator_by_iota_address(system_state_mut_ref.validators(), VALIDATOR_ADDR_1), 0); - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 100 * NANOS_PER_IOTA); // Unstake from VALIDATOR_ADDR_1 @@ -670,7 +670,7 @@ module iota_system::timelocked_stake_tests { let mut system_state = scenario.take_shared(); let system_state_mut_ref = &mut system_state; - let staked_iota = scenario.take_from_sender(); + let staked_iota = scenario.take_from_sender(); assert_eq(staked_iota.amount(), 100 * NANOS_PER_IOTA); // Unstake from VALIDATOR_ADDR_1 @@ -893,7 +893,7 @@ module iota_system::timelocked_stake_tests { let scenario = &mut scenario_val; stake_timelocked_with(@0x42, @0x2, 100, 10, scenario); // stakes 100 IOTA with 0x2 scenario.next_tx(@0x42); - let staked_iota = scenario.take_from_address(@0x42); + let staked_iota = scenario.take_from_address(@0x42); let pool_id = staked_iota.pool_id(); test_scenario::return_to_address(@0x42, staked_iota); advance_epoch(scenario); // advances epoch to effectuate the stake @@ -987,7 +987,7 @@ module iota_system::timelocked_stake_tests { } fun assert_exchange_rate_eq( - rates: &Table, epoch: u64, iota_amount: u64, pool_token_amount: u64 + rates: &Table, epoch: u64, iota_amount: u64, pool_token_amount: u64 ) { let rate = &rates[epoch]; assert_eq(rate.iota_amount(), iota_amount * NANOS_PER_IOTA); @@ -1081,7 +1081,7 @@ module iota_system::timelocked_stake_tests { staker: address, staked_iota_idx: u64, scenario: &mut Scenario ) { scenario.next_tx(staker); - let stake_iota_ids = scenario.ids_for_sender(); + let stake_iota_ids = scenario.ids_for_sender(); let staked_iota = scenario.take_from_sender_by_id(stake_iota_ids[staked_iota_idx]); let mut system_state = scenario.take_shared(); @@ -1115,7 +1115,7 @@ module iota_system::timelocked_stake_tests { scenario.has_most_recent_for_sender>() } - fun is_active_validator_by_iota_address(set: &ValidatorSet, validator_address: address): bool { + fun is_active_validator_by_iota_address(set: &ValidatorSetV1, validator_address: address): bool { let validators = set.active_validators(); let length = validators.length(); let mut i = 0; diff --git a/crates/iota-framework/packages/iota-system/tests/validator_set_tests.move b/crates/iota-framework/packages/iota-system/tests/validator_set_tests.move index 6d8466dd50e..70589d844f1 100644 --- a/crates/iota-framework/packages/iota-system/tests/validator_set_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/validator_set_tests.move @@ -6,9 +6,9 @@ module iota_system::validator_set_tests { use iota::balance; use iota::coin; - use iota_system::staking_pool::StakedIota; - use iota_system::validator::{Self, Validator, staking_pool_id}; - use iota_system::validator_set::{Self, ValidatorSet, active_validator_addresses}; + use iota_system::staking_pool::StakedIotaV1; + use iota_system::validator::{Self, ValidatorV1, staking_pool_id}; + use iota_system::validator_set::{Self, ValidatorSetV1, active_validator_addresses}; use iota::test_scenario::{Self, Scenario}; use iota::test_utils::{Self, assert_eq}; use iota::vec_map; @@ -368,7 +368,7 @@ module iota_system::validator_set_tests { // Withdraw the stake from @0x4. scenario.next_tx(@0x42); { - let stake = scenario.take_from_sender(); + let stake = scenario.take_from_sender(); let ctx = scenario.ctx(); let withdrawn_balance = validator_set.request_withdraw_stake( stake, @@ -399,7 +399,7 @@ module iota_system::validator_set_tests { scenario_val.end(); } - fun create_validator(addr: address, hint: u8, gas_price: u64, is_initial_validator: bool, ctx: &mut TxContext): Validator { + fun create_validator(addr: address, hint: u8, gas_price: u64, is_initial_validator: bool, ctx: &mut TxContext): ValidatorV1 { let stake_value = hint as u64 * 100 * NANOS_PER_IOTA; let name = hint_to_ascii(hint); let validator = validator::new_for_testing( @@ -430,7 +430,7 @@ module iota_system::validator_set_tests { ascii_bytes.to_ascii_string().into_bytes() } - fun advance_epoch_with_dummy_rewards(validator_set: &mut ValidatorSet, scenario: &mut Scenario) { + fun advance_epoch_with_dummy_rewards(validator_set: &mut ValidatorSetV1, scenario: &mut Scenario) { scenario.next_epoch(@0x0); let mut dummy_computation_reward = balance::zero(); @@ -448,7 +448,7 @@ module iota_system::validator_set_tests { } fun advance_epoch_with_low_stake_params( - validator_set: &mut ValidatorSet, + validator_set: &mut ValidatorSetV1, low_stake_threshold: u64, very_low_stake_threshold: u64, low_stake_grace_period: u64, @@ -469,7 +469,7 @@ module iota_system::validator_set_tests { dummy_computation_reward.destroy_zero(); } - fun add_and_activate_validator(validator_set: &mut ValidatorSet, validator: Validator, scenario: &mut Scenario) { + fun add_and_activate_validator(validator_set: &mut ValidatorSetV1, validator: ValidatorV1, scenario: &mut Scenario) { scenario.next_tx(validator.iota_address()); let ctx = scenario.ctx(); validator_set.request_add_validator_candidate(validator, ctx); diff --git a/crates/iota-framework/packages/iota-system/tests/validator_tests.move b/crates/iota-framework/packages/iota-system/tests/validator_tests.move index 12f2078c64d..6a6c361c3fc 100644 --- a/crates/iota-framework/packages/iota-system/tests/validator_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/validator_tests.move @@ -11,8 +11,8 @@ module iota_system::validator_tests { use iota::test_scenario; use iota::test_utils; use iota::url; - use iota_system::staking_pool::StakedIota; - use iota_system::validator::{Self, Validator}; + use iota_system::staking_pool::StakedIotaV1; + use iota_system::validator::{Self, ValidatorV1}; const VALID_NET_PUBKEY: vector = vector[171, 2, 39, 3, 139, 105, 166, 171, 153, 151, 102, 197, 151, 186, 140, 116, 114, 90, 213, 225, 20, 167, 60, 69, 203, 12, 180, 198, 9, 217, 117, 38]; @@ -32,7 +32,7 @@ module iota_system::validator_tests { const TOO_LONG_257_BYTES: vector = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; #[test_only] - fun get_test_validator(ctx: &mut TxContext): Validator { + fun get_test_validator(ctx: &mut TxContext): ValidatorV1 { let init_stake = coin::mint_for_testing(10_000_000_000, ctx).into_balance(); let mut validator = validator::new( VALID_ADDRESS, @@ -82,7 +82,7 @@ module iota_system::validator_tests { // Check that after destroy, the original stake still exists. scenario.next_tx(sender); { - let stake = scenario.take_from_sender(); + let stake = scenario.take_from_sender(); assert!(stake.amount() == 10_000_000_000); scenario.return_to_sender(stake); }; @@ -110,8 +110,8 @@ module iota_system::validator_tests { scenario.next_tx(sender); { - let coin_ids = scenario.ids_for_sender(); - let stake = scenario.take_from_sender_by_id(coin_ids[0]); + let coin_ids = scenario.ids_for_sender(); + let stake = scenario.take_from_sender_by_id(coin_ids[0]); let ctx = scenario.ctx(); let withdrawn_balance = validator.request_withdraw_stake(stake, ctx); transfer::public_transfer(withdrawn_balance.into_coin(ctx), sender); @@ -650,7 +650,7 @@ module iota_system::validator_tests { tear_down(validator, scenario); } - fun set_up(): (address, test_scenario::Scenario, validator::Validator) { + fun set_up(): (address, test_scenario::Scenario, validator::ValidatorV1) { let sender = VALID_ADDRESS; let mut scenario_val = test_scenario::begin(sender); let ctx = scenario_val.ctx(); @@ -658,7 +658,7 @@ module iota_system::validator_tests { (sender, scenario_val, validator) } - fun tear_down(validator: validator::Validator, scenario: test_scenario::Scenario) { + fun tear_down(validator: validator::ValidatorV1, scenario: test_scenario::Scenario) { test_utils::destroy(validator); scenario.end(); } diff --git a/crates/iota-framework/packages/iota-system/tests/voting_power_tests.move b/crates/iota-framework/packages/iota-system/tests/voting_power_tests.move index 799ca5dc71c..f9aa8ac8965 100644 --- a/crates/iota-framework/packages/iota-system/tests/voting_power_tests.move +++ b/crates/iota-framework/packages/iota-system/tests/voting_power_tests.move @@ -8,7 +8,7 @@ module iota_system::voting_power_tests { use iota_system::voting_power; use iota::test_scenario; use iota::test_utils; - use iota_system::validator::{Self, Validator}; + use iota_system::validator::{Self, ValidatorV1}; const TOTAL_VOTING_POWER: u64 = 10_000; @@ -68,7 +68,7 @@ module iota_system::voting_power_tests { scenario.end(); } - fun get_voting_power(validators: &vector): vector { + fun get_voting_power(validators: &vector): vector { let mut result = vector[]; let mut i = 0; let len = validators.length();