Skip to content

Commit

Permalink
valset: update min_weight/max_validators by exec msg
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jan 27, 2022
1 parent d4e4375 commit 15332f6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ pub fn execute(
info,
admin.map(|admin| api.addr_validate(&admin)).transpose()?,
)?),
ExecuteMsg::UpdateConfig {
min_weight,
max_validators,
} => execute_update_config(deps, info, min_weight, max_validators),

ExecuteMsg::RegisterValidatorKey { pubkey, metadata } => {
execute_register_validator_key(deps, env, info, pubkey, metadata)
}
Expand All @@ -163,6 +168,31 @@ pub fn execute(
}
}

fn execute_update_config(
deps: DepsMut,
info: MessageInfo,
min_weight: Option<u64>,
max_validators: Option<u32>,
) -> Result<Response, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;

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

let res = Response::new()
.add_attribute("action", "update_config")
.add_attribute("operator", &info.sender);

Ok(res)
}

fn execute_register_validator_key(
deps: DepsMut,
_env: Env,
Expand Down
5 changes: 5 additions & 0 deletions contracts/tgrade-valset/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ pub enum ExecuteMsg {
UpdateAdmin {
admin: Option<String>,
},
/// Alter config values
UpdateConfig {
min_weight: Option<u64>,
max_validators: Option<u32>,
},
/// Links info.sender (operator) to this Tendermint consensus key.
/// The operator cannot re-register another key.
/// No two operators may have the same consensus_key.
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 @@ -8,3 +8,4 @@ mod rewards_split;
mod slashing;
mod stake;
mod suite;
mod update_config;
17 changes: 17 additions & 0 deletions contracts/tgrade-valset/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,23 @@ impl Suite {
)
}

pub fn update_config(
&mut self,
executor: &str,
min_weight: impl Into<Option<u64>>,
max_validators: impl Into<Option<u32>>,
) -> AnyResult<AppResponse> {
self.app.execute_contract(
Addr::unchecked(executor),
self.valset.clone(),
&ExecuteMsg::UpdateConfig {
min_weight: min_weight.into(),
max_validators: max_validators.into(),
},
&[],
)
}

pub fn slash(
&mut self,
executor: &str,
Expand Down
57 changes: 57 additions & 0 deletions contracts/tgrade-valset/src/multitest/update_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use cw_controllers::AdminError;

use crate::error::ContractError;

use super::suite::SuiteBuilder;

#[test]
fn update_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.update_config(&admin, Some(5), Some(10)).unwrap();

let cfg = suite.config().unwrap();
assert_eq!(cfg.max_validators, 10);
assert_eq!(cfg.min_weight, 5);
}

#[test]
fn none_values_do_not_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.update_config(&admin, 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_weight, 3);
}

#[test]
fn non_admin_cannot_update_cfg() {
let mut suite = SuiteBuilder::new().build();

let err = suite
.update_config("random fella", Some(5), Some(10))
.unwrap_err();
assert_eq!(
ContractError::AdminError(AdminError::NotAdmin {}),
err.downcast().unwrap(),
);
}

0 comments on commit 15332f6

Please sign in to comment.