Skip to content

Commit

Permalink
add migration handler+tests for voting registry v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sotnikov-s committed Aug 15, 2023
1 parent 237e0e4 commit 1d90c07
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
24 changes: 19 additions & 5 deletions contracts/dao/voting/neutron-voting-registry/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, VotingVault};
use crate::state::{Config, VotingVaultState, CONFIG, DAO, VAULT_STATES};
#[cfg(not(feature = "library"))]
use cosmwasm_std::{
entry_point, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response,
StdError, StdResult,
};
use cw2::set_contract_version;
use cw_storage_plus::Item;
use cwd_interface::voting::{self, TotalPowerAtHeightResponse, VotingPowerAtHeightResponse};
use neutron_vault::msg::QueryMsg as VaultQueryMsg;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, VotingVault};
use crate::state::{Config, VotingVaultState, CONFIG, DAO, VAULT_STATES};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pub(crate) const CONTRACT_NAME: &str = "crates.io:neutron-voting-registry";
pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -305,6 +306,19 @@ pub fn query_dao(deps: Deps) -> StdResult<Binary> {

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
// read the prev version config
#[derive(Serialize, Deserialize, JsonSchema)]
struct OldConfig {
pub owner: Addr,
pub voting_vaults: Vec<Addr>,
}
let old_config: OldConfig = Item::new("config").load(deps.storage)?;

// move vaults from old config to a dedicated Item
for vault in old_config.voting_vaults {
VAULT_STATES.save(deps.storage, vault, &VotingVaultState::Active, 1u64)?;
}

// Set contract to version to latest
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
Expand Down
71 changes: 69 additions & 2 deletions contracts/dao/voting/neutron-voting-registry/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use crate::testing::mock_querier::{
};
use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::{from_binary, Addr, Deps, DepsMut, Env, MessageInfo, Response, Uint128};
use cw_storage_plus::Item;
use cwd_interface::voting::{
InfoResponse, TotalPowerAtHeightResponse, VotingPowerAtHeightResponse,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

const DAO_ADDR: &str = "dao";
const ADDR1: &str = "addr1";
Expand Down Expand Up @@ -792,13 +795,77 @@ fn test_vaults_activation_deactivation_wrong_switch() {
}

#[test]
pub fn test_migrate_update_version() {
pub fn test_migrate() {
let mut deps = mock_dependencies();
let env = mock_env();
let migration_height = env.block.height;
cw2::set_contract_version(&mut deps.storage, "my-contract", "old-version").unwrap();
migrate(deps.as_mut(), mock_env(), MigrateMsg {}).unwrap();

// define and store the previous version of config
#[derive(Serialize, Deserialize, JsonSchema)]
struct OldConfig {
pub owner: Addr,
pub voting_vaults: Vec<Addr>,
}
Item::new("config")
.save(
deps.as_mut().storage,
&OldConfig {
owner: Addr::unchecked(DAO_ADDR),
voting_vaults: vec![Addr::unchecked(MOCK_VAULT_1), Addr::unchecked(MOCK_VAULT_2)],
},
)
.unwrap();

migrate(deps.as_mut(), env.clone(), MigrateMsg {}).unwrap();

let version = cw2::get_contract_version(&deps.storage).unwrap();
assert_eq!(version.version, CONTRACT_VERSION);
assert_eq!(version.contract, CONTRACT_NAME);
assert_eq!(
get_voting_vaults(deps.as_ref(), env.clone(), Some(migration_height)),
vec![
VotingVault {
address: String::from(MOCK_VAULT_1),
name: String::from(MOCK_VAULT_1_NAME),
description: String::from(MOCK_VAULT_1_DESC),
state: VotingVaultState::Active,
},
VotingVault {
address: String::from(MOCK_VAULT_2),
name: String::from(MOCK_VAULT_2_NAME),
description: String::from(MOCK_VAULT_2_DESC),
state: VotingVaultState::Active,
}
],
);
assert_eq!(
get_total_voting_power(deps.as_ref(), env.clone(), Some(migration_height)).power,
Uint128::from(MOCK_VAULT_1_VP + MOCK_VAULT_2_VP),
);

// make sure vaults are active starting with the height next to the beginning of the chain
assert_eq!(
get_voting_vaults(deps.as_ref(), env.clone(), Some(2u64)),
vec![
VotingVault {
address: String::from(MOCK_VAULT_1),
name: String::from(MOCK_VAULT_1_NAME),
description: String::from(MOCK_VAULT_1_DESC),
state: VotingVaultState::Active,
},
VotingVault {
address: String::from(MOCK_VAULT_2),
name: String::from(MOCK_VAULT_2_NAME),
description: String::from(MOCK_VAULT_2_DESC),
state: VotingVaultState::Active,
}
],
);
assert_eq!(
get_total_voting_power(deps.as_ref(), env, Some(2u64)).power,
Uint128::from(MOCK_VAULT_1_VP + MOCK_VAULT_2_VP),
);
}

fn get_voting_vaults(deps: Deps, env: Env, height: Option<u64>) -> Vec<VotingVault> {
Expand Down

0 comments on commit 1d90c07

Please sign in to comment.