Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tgrade gov change params 2 #159

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions contracts/tgrade-validator-voting/src/multitest/proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,28 @@ fn cancel_upgrade() {
// We canceled the upgrade, so there should be no upgrade planned on the chain.
assert_eq!(suite.check_upgrade().unwrap(), None);
}

#[test]
fn change_params() {
let rules = RulesBuilder::new()
.with_threshold(Decimal::percent(50))
.build();

let mut suite = SuiteBuilder::new()
.with_group_member("member", 1)
.with_voting_rules(rules)
.build();

// We haven't executed a params change, so nothing yet
assert_eq!(suite.check_params().unwrap(), None);

let proposal = suite.propose_change_params("member").unwrap();
let proposal_id = get_proposal_id(&proposal).unwrap();
suite.execute("member", proposal_id).unwrap();

// There should now be a param change in the map
assert_eq!(
suite.check_params().unwrap(),
Some(vec![("foo/bar".to_string(), "baz".to_string())])
);
}
25 changes: 24 additions & 1 deletion contracts/tgrade-validator-voting/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{to_binary, Addr, ContractInfoResponse, Decimal};
use cw_multi_test::{AppResponse, Contract, ContractWrapper, Executor};
use tg3::Status;
use tg4::{Member, Tg4ExecuteMsg};
use tg_bindings::{TgradeMsg, TgradeQuery};
use tg_bindings::{ParamChange, TgradeMsg, TgradeQuery};
use tg_bindings_test::{TgradeApp, UpgradePlan};

use crate::msg::ValidatorProposal;
Expand Down Expand Up @@ -247,6 +247,19 @@ impl Suite {
)
}

pub fn propose_change_params(&mut self, executor: &str) -> AnyResult<AppResponse> {
self.propose(
executor,
"proposal title",
"proposal description",
ValidatorProposal::ChangeParams(vec![ParamChange {
subspace: "foo".to_string(),
key: "bar".to_string(),
value: "baz".to_string(),
}]),
)
}

pub fn check_pinned(&self, code_id: u64) -> AnyResult<bool> {
Ok(self
.app
Expand All @@ -259,6 +272,16 @@ impl Suite {
.read_module(|router, _, storage| router.custom.upgrade_is_planned(storage))?)
}

pub fn check_params(&self) -> AnyResult<Option<Vec<(String, String)>>> {
let params = self
.app
.read_module(|router, _, storage| router.custom.get_params(storage))?;
Ok(match params.len() {
0 => None,
_ => Some(params),
})
}

pub fn execute(&mut self, executor: &str, proposal_id: u64) -> AnyResult<AppResponse> {
self.app.execute_contract(
Addr::unchecked(executor),
Expand Down
26 changes: 26 additions & 0 deletions packages/bindings-test/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
use cosmwasm_std::OwnedDeps;
use std::marker::PhantomData;

use cosmwasm_std::Order::Ascending;
use cosmwasm_std::{
from_slice, to_binary, Addr, Api, Binary, BlockInfo, Coin, CustomQuery, Empty, Order, Querier,
QuerierResult, StdError, StdResult, Storage, Timestamp,
Expand Down Expand Up @@ -38,6 +39,7 @@ const PRIVILEGES: Map<&Addr, Privileges> = Map::new("privileges");
const VOTES: Item<ValidatorVoteResponse> = Item::new("votes");
const PINNED: Item<Vec<u64>> = Item::new("pinned");
const PLANNED_UPGRADE: Item<UpgradePlan> = Item::new("planned_upgrade");
const PARAMS: Map<String, String> = Map::new("params");

const ADMIN_PRIVILEGES: &[Privilege] = &[
Privilege::GovProposalExecutor,
Expand Down Expand Up @@ -82,6 +84,10 @@ impl TgradeModule {
PLANNED_UPGRADE.may_load(storage)
}

pub fn get_params(&self, storage: &dyn Storage) -> StdResult<Vec<(String, String)>> {
PARAMS.range(storage, None, None, Ascending).collect()
}

fn require_privilege(
&self,
storage: &dyn Storage,
Expand Down Expand Up @@ -244,6 +250,26 @@ impl Module for TgradeModule {
GovProposal::MigrateContract { .. } => {
bail!("GovProposal::MigrateContract not implemented")
}
GovProposal::ChangeParams(params) => {
let mut sorted_params = params.clone();
sorted_params.sort_unstable();
sorted_params.dedup_by(|a, b| a.subspace == b.subspace && a.key == b.key);
if sorted_params.len() < params.len() {
return Err(anyhow::anyhow!(
"duplicate subspace + keys in params vector"
));
}
for p in params {
if p.subspace.is_empty() {
return Err(anyhow::anyhow!("empty subspace key"));
}
if p.key.is_empty() {
return Err(anyhow::anyhow!("empty key key"));
}
PARAMS.save(storage, format!("{}/{}", p.subspace, p.key), &p.value)?;
}
Ok(AppResponse::default())
}
// most are ignored
_ => Ok(AppResponse::default()),
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bindings/src/gov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum GovProposal {
}

/// ParamChange defines an individual parameter change, for use in ParameterChangeProposal.
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, JsonSchema, Debug)]
pub struct ParamChange {
pub subspace: String,
pub key: String,
Expand Down