From 64ed304d7dc6c47776e95cce823a78e81b00e147 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 09:55:56 +0200 Subject: [PATCH 1/7] Update valset config comments --- contracts/tgrade-valset/src/state.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/contracts/tgrade-valset/src/state.rs b/contracts/tgrade-valset/src/state.rs index 3ed7f80f..d97eb9ba 100644 --- a/contracts/tgrade-valset/src/state.rs +++ b/contracts/tgrade-valset/src/state.rs @@ -23,8 +23,7 @@ pub struct Config { pub min_points: u64, /// The maximum number of validators that can be included in the Tendermint validator set. /// If there are more validators than slots, we select the top N by membership points - /// descending. (In case of ties at the last slot, select by "first" tendermint pubkey - /// lexicographically sorted). + /// descending. In case of ties at the last slot, the first (oldest) validator wins. pub max_validators: u32, /// A scaling factor to multiply tg4-engagement points to produce the tendermint validator power pub scaling: Option, @@ -61,8 +60,8 @@ pub struct Config { /// in a string of a number of blocks (typically 1000 blocks), they are jailed. pub verify_validators: bool, - /// The duration to jail a validator for in case they don't sign their first epoch - /// boundary block. After the period, they have to pass verification again, ad infinitum. + /// The duration to jail a validator for in case they don't sign any blocks for a period of time. + /// After the jailing period, they will be jailed again if not signing, ad infinitum. pub offline_jail_duration: Duration, } From 1ef1f5e945cfc0b4db1a2a3467d460f6e66c7f63 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 09:56:15 +0200 Subject: [PATCH 2/7] Add support for all configurable fields to UpdateConfig --- contracts/tgrade-valset/src/contract.rs | 68 +++++++++++++++++-- contracts/tgrade-valset/src/msg.rs | 39 +++++++++++ .../tgrade-valset/src/multitest/suite.rs | 8 +++ 3 files changed, 108 insertions(+), 7 deletions(-) diff --git a/contracts/tgrade-valset/src/contract.rs b/contracts/tgrade-valset/src/contract.rs index 0dd34400..36f7cb34 100644 --- a/contracts/tgrade-valset/src/contract.rs +++ b/contracts/tgrade-valset/src/contract.rs @@ -5,8 +5,8 @@ use std::convert::{TryFrom, TryInto}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_binary, Addr, Binary, BlockInfo, CustomQuery, Decimal, Deps, DepsMut, Env, MessageInfo, - Order, QueryRequest, Reply, StdError, StdResult, Timestamp, WasmMsg, + to_binary, Addr, Binary, BlockInfo, Coin, CustomQuery, Decimal, Deps, DepsMut, Env, + MessageInfo, Order, QueryRequest, Reply, StdError, StdResult, Timestamp, WasmMsg, }; use cw2::set_contract_version; @@ -20,7 +20,7 @@ use tg_bindings::{ Pubkey, TgradeMsg, TgradeQuery, TgradeSudoMsg, ToAddress, ValidatorDiff, ValidatorUpdate, ValidatorVoteResponse, }; -use tg_utils::{JailingDuration, SlashMsg, ADMIN}; +use tg_utils::{Duration, JailingDuration, SlashMsg, ADMIN}; use crate::error::ContractError; use crate::migration::{migrate_jailing_period, migrate_verify_validators}; @@ -32,9 +32,9 @@ use crate::msg::{ }; use crate::rewards::pay_block_rewards; use crate::state::{ - export, import, operators, Config, EpochInfo, OperatorInfo, ValidatorInfo, ValidatorSlashing, - ValsetState, BLOCK_SIGNERS, CONFIG, EPOCH, JAIL, VALIDATORS, VALIDATOR_SLASHING, - VALIDATOR_START_HEIGHT, + export, import, operators, Config, DistributionContract, EpochInfo, OperatorInfo, + ValidatorInfo, ValidatorSlashing, ValsetState, BLOCK_SIGNERS, CONFIG, EPOCH, JAIL, VALIDATORS, + VALIDATOR_SLASHING, VALIDATOR_START_HEIGHT, }; // version info for migration info @@ -164,7 +164,28 @@ pub fn execute( ExecuteMsg::UpdateConfig { min_points, max_validators, - } => execute_update_config(deps, info, min_points, max_validators), + scaling, + epoch_reward, + fee_percentage, + auto_unjail, + double_sign_slash_ratio, + distribution_contracts, + verify_validators, + offline_jail_duration, + } => execute_update_config( + deps, + info, + min_points, + max_validators, + scaling, + epoch_reward, + fee_percentage, + auto_unjail, + double_sign_slash_ratio, + distribution_contracts, + verify_validators, + offline_jail_duration, + ), ExecuteMsg::RegisterValidatorKey { pubkey, metadata } => { execute_register_validator_key(deps, env, info, pubkey, metadata) @@ -182,11 +203,20 @@ pub fn execute( } } +#[allow(clippy::too_many_arguments)] fn execute_update_config( deps: DepsMut, info: MessageInfo, min_points: Option, max_validators: Option, + scaling: Option, + epoch_reward: Option, + fee_percentage: Option, + auto_unjail: Option, + double_sign_slash_ratio: Option, + distribution_contracts: Option>, + verify_validators: Option, + offline_jail_duration: Option, ) -> Result { ADMIN.assert_admin(deps.as_ref(), &info.sender)?; @@ -197,6 +227,30 @@ fn execute_update_config( if let Some(max_validators) = max_validators { cfg.max_validators = max_validators; } + if let Some(scaling) = scaling { + cfg.scaling = Option::from(scaling); + } + if let Some(epoch_reward) = epoch_reward { + cfg.epoch_reward = epoch_reward; + } + if let Some(fee_percentage) = fee_percentage { + cfg.fee_percentage = fee_percentage; + } + if let Some(auto_unjail) = auto_unjail { + cfg.auto_unjail = auto_unjail; + } + if let Some(double_sign_slash_ratio) = double_sign_slash_ratio { + cfg.double_sign_slash_ratio = double_sign_slash_ratio; + } + if let Some(distribution_contracts) = distribution_contracts { + cfg.distribution_contracts = distribution_contracts; + } + if let Some(verify_validators) = verify_validators { + cfg.verify_validators = verify_validators; + } + if let Some(offline_jail_duration) = offline_jail_duration { + cfg.offline_jail_duration = offline_jail_duration; + } Ok(cfg) })?; diff --git a/contracts/tgrade-valset/src/msg.rs b/contracts/tgrade-valset/src/msg.rs index fcb91908..fabb058e 100644 --- a/contracts/tgrade-valset/src/msg.rs +++ b/contracts/tgrade-valset/src/msg.rs @@ -135,8 +135,47 @@ pub enum ExecuteMsg { }, /// Alter config values UpdateConfig { + /// minimum points needed by an address in `membership` to be considered for the validator set. + /// 0-point members are always filtered out. min_points: Option, + /// The maximum number of validators that can be included in the Tendermint validator set. + /// If there are more validators than slots, we select the top N by membership points + /// descending. max_validators: Option, + /// A scaling factor to multiply tg4-engagement points to produce the tendermint validator power + scaling: Option, + /// Total reward paid out each epoch. This will be split among all validators during the last + /// epoch. + /// (epoch_reward.amount * 86_400 * 30 / epoch_length) is reward tokens to mint each month. + /// Ensure this is sensible in relation to the total token supply. + epoch_reward: Option, + /// Percentage of total accumulated fees which is subtracted from tokens minted as a rewards. + /// 50% as default. To disable this feature just set it to 0 (which effectively means that fees + /// doesn't affect the per epoch reward). + fee_percentage: Option, + /// Flag determining if validators should be automatically unjailed after jailing period, false + /// by default. + auto_unjail: Option, + + /// Validators who are caught double signing are jailed forever and their bonded tokens are + /// slashed based on this value. + double_sign_slash_ratio: Option, + + /// Addresses where part of the reward for non-validators is sent for further distribution. These are + /// required to handle the `Distribute {}` message (eg. tg4-engagement contract) which would + /// distribute the funds sent with this message. + /// The sum of ratios here has to be in the [0, 1] range. The remainder is sent to validators via the + /// rewards contract. + distribution_contracts: Option>, + + /// If this is enabled, signed blocks are watched for, and if a validator fails to sign any blocks + /// in a string of a number of blocks (typically 1000 blocks), they are jailed. + verify_validators: Option, + + /// The duration to jail a validator for in case they don't sign any blocks for a period of time, + /// if `verify_validators` is enabled. + /// After the jailing period, they will be jailed again if not signing blocks, ad infinitum. + offline_jail_duration: Option, }, /// Links info.sender (operator) to this Tendermint consensus key. /// The operator cannot re-register another key. diff --git a/contracts/tgrade-valset/src/multitest/suite.rs b/contracts/tgrade-valset/src/multitest/suite.rs index 4f2083b3..acbacda6 100644 --- a/contracts/tgrade-valset/src/multitest/suite.rs +++ b/contracts/tgrade-valset/src/multitest/suite.rs @@ -596,6 +596,14 @@ impl Suite { &ExecuteMsg::UpdateConfig { min_points: min_points.into(), max_validators: max_validators.into(), + scaling: None, + epoch_reward: None, + fee_percentage: None, + auto_unjail: None, + double_sign_slash_ratio: None, + distribution_contracts: None, + verify_validators: None, + offline_jail_duration: None, }, &[], ) From 64388eb4423a556cda06c34babb80b73c26db7c5 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 11:19:06 +0200 Subject: [PATCH 3/7] Add update config distribution contract list test --- .../tgrade-valset/src/multitest/suite.rs | 5 ++- .../src/multitest/update_config.rs | 43 +++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/contracts/tgrade-valset/src/multitest/suite.rs b/contracts/tgrade-valset/src/multitest/suite.rs index acbacda6..96197d0d 100644 --- a/contracts/tgrade-valset/src/multitest/suite.rs +++ b/contracts/tgrade-valset/src/multitest/suite.rs @@ -1,5 +1,5 @@ use super::helpers::addr_to_pubkey; -use crate::state::{Config, ValsetState}; +use crate::state::{Config, DistributionContract, ValsetState}; use crate::test_helpers::{mock_metadata, mock_pubkey}; use crate::{msg::*, state::ValidatorInfo}; use anyhow::{bail, Result as AnyResult}; @@ -589,6 +589,7 @@ impl Suite { executor: &str, min_points: impl Into>, max_validators: impl Into>, + distribution_contracts: impl Into>>, ) -> AnyResult { self.app.execute_contract( Addr::unchecked(executor), @@ -601,7 +602,7 @@ impl Suite { fee_percentage: None, auto_unjail: None, double_sign_slash_ratio: None, - distribution_contracts: None, + distribution_contracts: distribution_contracts.into(), verify_validators: None, offline_jail_duration: None, }, diff --git a/contracts/tgrade-valset/src/multitest/update_config.rs b/contracts/tgrade-valset/src/multitest/update_config.rs index 1fbfc03b..82213d47 100644 --- a/contracts/tgrade-valset/src/multitest/update_config.rs +++ b/contracts/tgrade-valset/src/multitest/update_config.rs @@ -1,6 +1,9 @@ +use cosmwasm_std::{Addr, Decimal}; use cw_controllers::AdminError; use crate::error::ContractError; +use crate::multitest::suite::Suite; +use crate::state::DistributionContract; use super::suite::SuiteBuilder; @@ -16,31 +19,63 @@ fn update_cfg() { assert_eq!(cfg.max_validators, 6); assert_eq!(cfg.min_points, 3); - suite.update_config(&admin, Some(5), Some(10)).unwrap(); + suite + .update_config( + &admin, + Some(5), + Some(10), + vec![DistributionContract { + contract: Addr::unchecked("contract1"), + ratio: Decimal::percent(15), + }], + ) + .unwrap(); let cfg = suite.config().unwrap(); assert_eq!(cfg.max_validators, 10); assert_eq!(cfg.min_points, 5); + assert_eq!( + cfg.distribution_contracts, + vec![DistributionContract { + contract: Addr::unchecked("contract1"), + ratio: Decimal::percent(15) + }] + ); } #[test] fn none_values_do_not_alter_cfg() { - let mut suite = SuiteBuilder::new() + let mut suite: Suite = SuiteBuilder::new() .with_max_validators(6) .with_min_points(3) + .with_distribution(Decimal::percent(50), &[("engagement1", 20)], None) .build(); let admin = suite.admin().to_string(); let cfg = suite.config().unwrap(); assert_eq!(cfg.max_validators, 6); assert_eq!(cfg.min_points, 3); + assert_eq!( + cfg.distribution_contracts, + vec![DistributionContract { + contract: Addr::unchecked("contract1"), + ratio: Decimal::percent(50) + }] + ); - suite.update_config(&admin, None, None).unwrap(); + suite.update_config(&admin, None, None, None).unwrap(); // Make sure the values haven't changed. let cfg = suite.config().unwrap(); assert_eq!(cfg.max_validators, 6); assert_eq!(cfg.min_points, 3); + assert_eq!( + cfg.distribution_contracts, + vec![DistributionContract { + contract: Addr::unchecked("contract1"), + ratio: Decimal::percent(50) + }] + ); } #[test] @@ -48,7 +83,7 @@ fn non_admin_cannot_update_cfg() { let mut suite = SuiteBuilder::new().build(); let err = suite - .update_config("random fella", Some(5), Some(10)) + .update_config("random fella", Some(5), Some(10), None) .unwrap_err(); assert_eq!( ContractError::AdminError(AdminError::NotAdmin {}), From 1e517deac28db93b0757e81ada43c692a211ee41 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 12:39:48 +0200 Subject: [PATCH 4/7] Add support for updating the distribution contracts list during migration --- contracts/tgrade-valset/src/contract.rs | 3 +++ contracts/tgrade-valset/src/msg.rs | 1 + contracts/tgrade-valset/src/multitest/migration.rs | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/contracts/tgrade-valset/src/contract.rs b/contracts/tgrade-valset/src/contract.rs index 36f7cb34..70523d76 100644 --- a/contracts/tgrade-valset/src/contract.rs +++ b/contracts/tgrade-valset/src/contract.rs @@ -997,6 +997,9 @@ pub fn migrate( if let Some(max_validators) = msg.max_validators { cfg.max_validators = max_validators; } + if let Some(distribution_contracts) = msg.distribution_contracts { + cfg.distribution_contracts = distribution_contracts; + } if let Some(verify_validators) = msg.verify_validators { cfg.verify_validators = verify_validators; } diff --git a/contracts/tgrade-valset/src/msg.rs b/contracts/tgrade-valset/src/msg.rs index fabb058e..748e4dc0 100644 --- a/contracts/tgrade-valset/src/msg.rs +++ b/contracts/tgrade-valset/src/msg.rs @@ -547,6 +547,7 @@ pub struct InstantiateResponse { pub struct MigrateMsg { pub min_points: Option, pub max_validators: Option, + pub distribution_contracts: Option>, pub verify_validators: Option, } diff --git a/contracts/tgrade-valset/src/multitest/migration.rs b/contracts/tgrade-valset/src/multitest/migration.rs index ce6d0d37..fe233d5e 100644 --- a/contracts/tgrade-valset/src/multitest/migration.rs +++ b/contracts/tgrade-valset/src/multitest/migration.rs @@ -1,5 +1,7 @@ use super::suite::SuiteBuilder; use crate::msg::MigrateMsg; +use crate::state::DistributionContract; +use cosmwasm_std::{Addr, Decimal}; #[test] fn migration_can_alter_cfg() { @@ -19,6 +21,10 @@ fn migration_can_alter_cfg() { &MigrateMsg { min_points: Some(5), max_validators: Some(10), + distribution_contracts: Some(vec![DistributionContract { + contract: Addr::unchecked("engagement1".to_string()), + ratio: Decimal::percent(50), + }]), verify_validators: Some(true), }, ) @@ -27,4 +33,9 @@ fn migration_can_alter_cfg() { let cfg = suite.config().unwrap(); assert_eq!(cfg.max_validators, 10); assert_eq!(cfg.min_points, 5); + assert!(cfg.verify_validators); + assert_eq!(cfg.distribution_contracts, vec![DistributionContract { + contract: Addr::unchecked("engagement1".to_string()), + ratio: Decimal::percent(50), + }]); } From c6dc85c243d3533ed067355a7a6f1a19359a6e43 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 12:47:34 +0200 Subject: [PATCH 5/7] Don't enable verify validators during migration by default --- contracts/tgrade-valset/src/contract.rs | 4 +--- contracts/tgrade-valset/src/migration.rs | 17 +---------------- .../tgrade-valset/src/multitest/migration.rs | 11 +++++++---- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/contracts/tgrade-valset/src/contract.rs b/contracts/tgrade-valset/src/contract.rs index 70523d76..e39a9d51 100644 --- a/contracts/tgrade-valset/src/contract.rs +++ b/contracts/tgrade-valset/src/contract.rs @@ -23,7 +23,7 @@ use tg_bindings::{ use tg_utils::{Duration, JailingDuration, SlashMsg, ADMIN}; use crate::error::ContractError; -use crate::migration::{migrate_jailing_period, migrate_verify_validators}; +use crate::migration::migrate_jailing_period; use crate::msg::{ EpochResponse, ExecuteMsg, InstantiateMsg, InstantiateResponse, JailingEnd, JailingPeriod, ListActiveValidatorsResponse, ListValidatorResponse, ListValidatorSlashingResponse, MigrateMsg, @@ -1008,8 +1008,6 @@ pub fn migrate( migrate_jailing_period(deps.branch(), &original_version)?; - migrate_verify_validators(deps.branch(), &original_version)?; - Ok(Response::new()) } diff --git a/contracts/tgrade-valset/src/migration.rs b/contracts/tgrade-valset/src/migration.rs index e78a1cbf..faa69414 100644 --- a/contracts/tgrade-valset/src/migration.rs +++ b/contracts/tgrade-valset/src/migration.rs @@ -7,7 +7,7 @@ use tg_utils::Expiration; use crate::error::ContractError; use crate::msg::{JailingEnd, JailingPeriod}; -use crate::state::{CONFIG, JAIL}; +use crate::state::JAIL; /// `crate::msg::JailingPeriod` version from v0.6.2 and before #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] @@ -50,21 +50,6 @@ pub fn migrate_jailing_period( Ok(()) } -pub fn migrate_verify_validators( - deps: DepsMut, - version: &Version, -) -> Result<(), ContractError> { - let mut config = if *version <= "0.14.0".parse::().unwrap() { - CONFIG.load(deps.storage)? - } else { - return Ok(()); - }; - config.verify_validators = true; - CONFIG.save(deps.storage, &config)?; - - Ok(()) -} - #[cfg(test)] mod tests { //! These are very rudimentary tests that only -mock- old state and perform migrations on it. diff --git a/contracts/tgrade-valset/src/multitest/migration.rs b/contracts/tgrade-valset/src/multitest/migration.rs index fe233d5e..00388174 100644 --- a/contracts/tgrade-valset/src/multitest/migration.rs +++ b/contracts/tgrade-valset/src/multitest/migration.rs @@ -34,8 +34,11 @@ fn migration_can_alter_cfg() { assert_eq!(cfg.max_validators, 10); assert_eq!(cfg.min_points, 5); assert!(cfg.verify_validators); - assert_eq!(cfg.distribution_contracts, vec![DistributionContract { - contract: Addr::unchecked("engagement1".to_string()), - ratio: Decimal::percent(50), - }]); + assert_eq!( + cfg.distribution_contracts, + vec![DistributionContract { + contract: Addr::unchecked("engagement1".to_string()), + ratio: Decimal::percent(50), + }] + ); } From 3bed84272296f53884cc22a011354f547977cf65 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 12:51:01 +0200 Subject: [PATCH 6/7] Remove outdated migration code in passing --- contracts/tgrade-valset/src/contract.rs | 8 +- contracts/tgrade-valset/src/lib.rs | 1 - contracts/tgrade-valset/src/migration.rs | 124 ----------------------- 3 files changed, 2 insertions(+), 131 deletions(-) delete mode 100644 contracts/tgrade-valset/src/migration.rs diff --git a/contracts/tgrade-valset/src/contract.rs b/contracts/tgrade-valset/src/contract.rs index e39a9d51..610fa8af 100644 --- a/contracts/tgrade-valset/src/contract.rs +++ b/contracts/tgrade-valset/src/contract.rs @@ -23,7 +23,6 @@ use tg_bindings::{ use tg_utils::{Duration, JailingDuration, SlashMsg, ADMIN}; use crate::error::ContractError; -use crate::migration::migrate_jailing_period; use crate::msg::{ EpochResponse, ExecuteMsg, InstantiateMsg, InstantiateResponse, JailingEnd, JailingPeriod, ListActiveValidatorsResponse, ListValidatorResponse, ListValidatorSlashingResponse, MigrateMsg, @@ -983,12 +982,11 @@ fn calculate_diff( #[cfg_attr(not(feature = "library"), entry_point)] pub fn migrate( - mut deps: DepsMut, + deps: DepsMut, _env: Env, msg: MigrateMsg, ) -> Result { - let original_version = - ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + let _ = ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; CONFIG.update::<_, StdError>(deps.storage, |mut cfg| { if let Some(min_points) = msg.min_points { @@ -1006,8 +1004,6 @@ pub fn migrate( Ok(cfg) })?; - migrate_jailing_period(deps.branch(), &original_version)?; - Ok(Response::new()) } diff --git a/contracts/tgrade-valset/src/lib.rs b/contracts/tgrade-valset/src/lib.rs index f56dc8ff..c6b4d747 100644 --- a/contracts/tgrade-valset/src/lib.rs +++ b/contracts/tgrade-valset/src/lib.rs @@ -1,6 +1,5 @@ pub mod contract; pub mod error; -mod migration; pub mod msg; #[cfg(test)] mod multitest; diff --git a/contracts/tgrade-valset/src/migration.rs b/contracts/tgrade-valset/src/migration.rs deleted file mode 100644 index faa69414..00000000 --- a/contracts/tgrade-valset/src/migration.rs +++ /dev/null @@ -1,124 +0,0 @@ -use cosmwasm_std::{Addr, CustomQuery, DepsMut, Order, Timestamp}; -use cw_storage_plus::Map; -use schemars::JsonSchema; -use semver::Version; -use serde::{Deserialize, Serialize}; -use tg_utils::Expiration; - -use crate::error::ContractError; -use crate::msg::{JailingEnd, JailingPeriod}; -use crate::state::JAIL; - -/// `crate::msg::JailingPeriod` version from v0.6.2 and before -#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] -pub enum JailingPeriodV0_6_2 { - Until(Expiration), - Forever {}, -} - -impl JailingPeriodV0_6_2 { - fn update(self) -> JailingPeriod { - JailingPeriod { - start: Timestamp::from_seconds(0), - end: match self { - JailingPeriodV0_6_2::Until(u) => JailingEnd::Until(u), - JailingPeriodV0_6_2::Forever {} => JailingEnd::Forever {}, - }, - } - } -} - -pub fn migrate_jailing_period( - deps: DepsMut, - version: &Version, -) -> Result<(), ContractError> { - let jailings: Vec<_> = if *version <= "0.6.2".parse::().unwrap() { - let jailings: Map<&Addr, JailingPeriodV0_6_2> = Map::new("jail"); - - jailings - .range(deps.storage, None, None, Order::Ascending) - .map(|record| record.map(|(key, jailing_period)| (key, jailing_period.update()))) - .collect::>()? - } else { - return Ok(()); - }; - - for (addr, jailing_period) in jailings { - JAIL.save(deps.storage, &addr, &jailing_period)?; - } - - Ok(()) -} - -#[cfg(test)] -mod tests { - //! These are very rudimentary tests that only -mock- old state and perform migrations on it. - //! It's absolutely vital to do more thorough migration testing on some actual old state. - - use cosmwasm_std::{testing::mock_dependencies, StdError, Storage}; - - use super::*; - - fn mock_v_0_6_2_jailing_periods( - store: &mut dyn Storage, - jailings: &[(&str, JailingPeriodV0_6_2)], - ) { - let jail_map: Map<&Addr, JailingPeriodV0_6_2> = Map::new("jail"); - - for (addr, period) in jailings.iter().cloned() { - jail_map - .update(store, &Addr::unchecked(addr), |_| -> Result<_, StdError> { - Ok(period) - }) - .unwrap(); - } - } - - #[test] - fn migrate_jailing_period_v_0_6_2() { - let mut deps = mock_dependencies(); - - mock_v_0_6_2_jailing_periods( - &mut deps.storage, - &[ - ( - "alice", - JailingPeriodV0_6_2::Until(Expiration::at_timestamp(Timestamp::from_seconds( - 123, - ))), - ), - ("bob", JailingPeriodV0_6_2::Forever {}), - ], - ); - - migrate_jailing_period(deps.as_mut(), &Version::parse("0.6.2").unwrap()).unwrap(); - - // verify the data is what we expect - let jailed = JAIL - .range(&deps.storage, None, None, Order::Ascending) - .collect::, _>>() - .unwrap(); - - assert_eq!( - jailed, - [ - ( - Addr::unchecked("alice"), - JailingPeriod { - start: Timestamp::from_seconds(0), - end: JailingEnd::Until(Expiration::at_timestamp(Timestamp::from_seconds( - 123 - ))) - } - ), - ( - Addr::unchecked("bob"), - JailingPeriod { - start: Timestamp::from_seconds(0), - end: JailingEnd::Forever {} - } - ) - ] - ); - } -} From 6c091ec779ad2414f4ec1a477ac815666b3a59b7 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 19 Sep 2022 19:17:54 +0200 Subject: [PATCH 7/7] Improve syntax Co-authored-by: Jakub Bogucki --- contracts/tgrade-valset/src/contract.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/tgrade-valset/src/contract.rs b/contracts/tgrade-valset/src/contract.rs index 610fa8af..7d19a1b6 100644 --- a/contracts/tgrade-valset/src/contract.rs +++ b/contracts/tgrade-valset/src/contract.rs @@ -986,7 +986,7 @@ pub fn migrate( _env: Env, msg: MigrateMsg, ) -> Result { - let _ = ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; CONFIG.update::<_, StdError>(deps.storage, |mut cfg| { if let Some(min_points) = msg.min_points {