Skip to content

Commit

Permalink
valset: update min_weight/max_validators by migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jan 27, 2022
1 parent fe6d292 commit d4e4375
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
19 changes: 15 additions & 4 deletions contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::convert::TryInto;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Addr, Binary, BlockInfo, Decimal, Deps, DepsMut, Empty, Env, MessageInfo, Order,
Reply, StdResult, Timestamp, WasmMsg,
to_binary, Addr, Binary, BlockInfo, Decimal, Deps, DepsMut, Env, MessageInfo, Order, Reply,
StdError, StdResult, Timestamp, WasmMsg,
};

use cw2::set_contract_version;
Expand All @@ -24,7 +24,7 @@ use tg_utils::{ensure_from_older_version, JailingDuration, SlashMsg, ADMIN};
use crate::error::ContractError;
use crate::msg::{
EpochResponse, ExecuteMsg, InstantiateMsg, InstantiateResponse, JailingPeriod,
ListActiveValidatorsResponse, ListValidatorResponse, ListValidatorSlashingResponse,
ListActiveValidatorsResponse, ListValidatorResponse, ListValidatorSlashingResponse, MigrateMsg,
OperatorResponse, QueryMsg, RewardsDistribution, RewardsInstantiateMsg, ValidatorMetadata,
ValidatorResponse,
};
Expand Down Expand Up @@ -722,8 +722,19 @@ fn calculate_diff(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

CONFIG.update::<_, StdError>(deps.storage, |mut cfg| {
if let Some(min_weight) = msg.min_weight {
cfg.min_weight = min_weight;
}
if let Some(max_validators) = msg.max_validators {
cfg.max_validators = max_validators;
}
Ok(cfg)
})?;

Ok(Response::new())
}

Expand Down
7 changes: 7 additions & 0 deletions contracts/tgrade-valset/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ pub struct InstantiateResponse {
pub rewards_contract: Addr,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct MigrateMsg {
pub min_weight: Option<u64>,
pub max_validators: Option<u32>,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions contracts/tgrade-valset/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod contract;
mod double_sign;
mod helpers;
mod jailing;
mod migration;
mod rewards_split;
mod slashing;
mod stake;
Expand Down
29 changes: 29 additions & 0 deletions contracts/tgrade-valset/src/multitest/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::suite::SuiteBuilder;
use crate::msg::MigrateMsg;

#[test]
fn migration_can_alter_cfg() {
let mut suite = SuiteBuilder::new()
.with_max_validators(6)
.with_min_weight(3)
.build();
let admin = suite.admin().to_string();

let cfg = suite.config().unwrap();
assert_eq!(cfg.max_validators, 6);
assert_eq!(cfg.min_weight, 3);

suite
.migrate(
&admin,
&MigrateMsg {
min_weight: Some(5),
max_validators: Some(10),
},
)
.unwrap();

let cfg = suite.config().unwrap();
assert_eq!(cfg.max_validators, 10);
assert_eq!(cfg.min_weight, 5);
}
17 changes: 16 additions & 1 deletion contracts/tgrade-valset/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pub fn contract_valset() -> Box<dyn Contract<TgradeMsg>> {
crate::contract::query,
)
.with_sudo(crate::contract::sudo)
.with_reply(crate::contract::reply);
.with_reply(crate::contract::reply)
.with_migrate(crate::contract::migrate);

Box::new(contract)
}
Expand Down Expand Up @@ -391,6 +392,7 @@ impl SuiteBuilder {

Suite {
app,
valset_code_id: valset_id,
valset,
membership,
distribution_contracts,
Expand All @@ -409,6 +411,8 @@ pub struct Suite {
/// Multitest app
#[derivative(Debug = "ignore")]
app: TgradeApp,
/// The code id of the valset contract
valset_code_id: u64,
/// tgrade-valset contract address
valset: Addr,
/// membership contract address
Expand Down Expand Up @@ -734,4 +738,15 @@ impl Suite {
&[],
)
}

/// Migrates the contract to the same version (same code id), but possibly changing
/// some cfg values via MigrateMsg.
pub fn migrate(&mut self, addr: &str, msg: &MigrateMsg) -> AnyResult<AppResponse> {
self.app.migrate_contract(
Addr::unchecked(addr),
self.valset.clone(),
msg,
self.valset_code_id,
)
}
}

0 comments on commit d4e4375

Please sign in to comment.