Skip to content

Commit

Permalink
implemented migrate handler, added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
joldie777 committed Apr 7, 2024
1 parent 26f1cc1 commit 3059a34
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 49 additions & 4 deletions contracts/dao/proposal/cwd-proposal-multiple/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use cwd_proposal_hooks::{new_proposal_hooks, proposal_status_changed_hooks};
use cwd_vote_hooks::new_vote_hooks;
use cwd_voting::{
multiple_choice::{
MultipleChoiceOptions, MultipleChoiceVote, MultipleChoiceVotes, VotingStrategy,
CheckedMultipleChoiceOption, MultipleChoiceOptions, MultipleChoiceVote,
MultipleChoiceVotes, VotingStrategy,
},
pre_propose::{PreProposeInfo, ProposalCreationPolicy},
proposal::{DEFAULT_LIMIT, MAX_PROPOSAL_SIZE},
Expand All @@ -30,10 +31,11 @@ use crate::state::PROPOSAL_EXECUTION_ERRORS;
use crate::{msg::MigrateMsg, state::CREATION_POLICY};
use crate::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
proposal::{MultipleChoiceProposal, VoteResult},
proposal::{MultipleChoiceProposal, OldMultipleChoiceProposal, VoteResult},
query::{ProposalListResponse, ProposalResponse, VoteInfo, VoteListResponse, VoteResponse},
state::{
Ballot, Config, BALLOTS, CONFIG, PROPOSALS, PROPOSAL_COUNT, PROPOSAL_HOOKS, VOTE_HOOKS,
Ballot, Config, BALLOTS, CONFIG, OLD_PROPOSALS, PROPOSALS, PROPOSAL_COUNT, PROPOSAL_HOOKS,
VOTE_HOOKS,
},
ContractError,
};
Expand Down Expand Up @@ -918,5 +920,48 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())

let mut migrated_proposal_ids: Vec<String> = vec![];

OLD_PROPOSALS
.range(deps.storage, None, None, cosmwasm_std::Order::Ascending)
.collect::<StdResult<Vec<(u64, OldMultipleChoiceProposal)>>>()?
.into_iter()
.try_for_each(|(id, prop)| {
migrated_proposal_ids.push(id.to_string());

PROPOSALS.save(
deps.storage,
id,
&MultipleChoiceProposal {
title: prop.title,
description: prop.description,
proposer: prop.proposer,
start_height: prop.start_height,
min_voting_period: prop.min_voting_period,
expiration: prop.expiration,
choices: prop
.choices
.into_iter()
.map(|choice| CheckedMultipleChoiceOption {
index: choice.index,
option_type: choice.option_type,
title: "".to_string(),
description: choice.description,
msgs: choice.msgs,
vote_count: choice.vote_count,
})
.collect(),
status: prop.status,
voting_strategy: prop.voting_strategy,
total_power: prop.total_power,
votes: prop.votes,
allow_revoting: prop.allow_revoting,
},
)
})?;

Ok(Response::default()
.add_attribute("action", "migrate")
.add_attribute("migrated_proposal_ids", migrated_proposal_ids.join(",")))
}
19 changes: 18 additions & 1 deletion contracts/dao/proposal/cwd-proposal-multiple/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use cosmwasm_std::{Addr, BlockInfo, StdError, StdResult, Uint128};
use cw_utils::Expiration;
use cwd_voting::{
multiple_choice::{
CheckedMultipleChoiceOption, MultipleChoiceOptionType, MultipleChoiceVotes, VotingStrategy,
CheckedMultipleChoiceOption, MultipleChoiceOptionType, MultipleChoiceVotes,
OldCheckedMultipleChoiceOption, VotingStrategy,
},
proposal::Proposal,
status::Status,
Expand Down Expand Up @@ -46,6 +47,22 @@ pub struct MultipleChoiceProposal {
pub allow_revoting: bool,
}

#[cw_serde]
pub struct OldMultipleChoiceProposal {
pub title: String,
pub description: String,
pub proposer: Addr,
pub start_height: u64,
pub min_voting_period: Option<Expiration>,
pub expiration: Expiration,
pub choices: Vec<OldCheckedMultipleChoiceOption>,
pub status: Status,
pub voting_strategy: VotingStrategy,
pub total_power: Uint128,
pub votes: MultipleChoiceVotes,
pub allow_revoting: bool,
}

pub enum VoteResult {
SingleWinner(CheckedMultipleChoiceOption),
Tie,
Expand Down
6 changes: 4 additions & 2 deletions contracts/dao/proposal/cwd-proposal-multiple/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::proposal::MultipleChoiceProposal;
use crate::proposal::{MultipleChoiceProposal, OldMultipleChoiceProposal};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Uint128};
use cw_storage_plus::{Item, Map};
Expand Down Expand Up @@ -55,10 +55,12 @@ pub struct Ballot {
pub vote: MultipleChoiceVote,
}

pub const OLD_PROPOSALS: Map<u64, OldMultipleChoiceProposal> = Map::new("proposals");

/// The current top level config for the module.
pub const CONFIG: Item<Config> = Item::new("config");
pub const PROPOSAL_COUNT: Item<u64> = Item::new("proposal_count");
pub const PROPOSALS: Map<u64, MultipleChoiceProposal> = Map::new("proposals");
pub const PROPOSALS: Map<u64, MultipleChoiceProposal> = Map::new("proposals_v2");
pub const BALLOTS: Map<(u64, Addr), Ballot> = Map::new("ballots");
/// Consumers of proposal state change hooks.
pub const PROPOSAL_HOOKS: Hooks = Hooks::new("proposal_hooks");
Expand Down
120 changes: 116 additions & 4 deletions contracts/dao/proposal/cwd-proposal-multiple/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use cwd_voting::{
deposit::{CheckedDepositInfo, DepositRefundPolicy, DepositToken, UncheckedDepositInfo},
multiple_choice::{
CheckedMultipleChoiceOption, MultipleChoiceOption, MultipleChoiceOptionType,
MultipleChoiceOptions, MultipleChoiceVote, MultipleChoiceVotes, VotingStrategy,
MAX_NUM_CHOICES,
MultipleChoiceOptions, MultipleChoiceVote, MultipleChoiceVotes,
OldCheckedMultipleChoiceOption, VotingStrategy, MAX_NUM_CHOICES,
},
pre_propose::PreProposeInfo,
status::Status,
Expand All @@ -28,8 +28,8 @@ use neutron_sdk::bindings::msg::NeutronMsg;
use std::panic;

use crate::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
proposal::MultipleChoiceProposal,
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
proposal::{MultipleChoiceProposal, OldMultipleChoiceProposal},
query::{ProposalListResponse, ProposalResponse},
state::Config,
testing::{
Expand Down Expand Up @@ -2222,3 +2222,115 @@ fn test_reply_proposal_mock() {
let error: Option<String> = from_json(query_res).unwrap();
assert_eq!(error, Some("error".to_string()));
}

#[test]
fn test_migrate_mock() {
use crate::contract::migrate;
use crate::state::{OLD_PROPOSALS, PROPOSALS};

let mut deps = mock_dependencies();
let env = mock_env();
let max_voting_period = cw_utils::Duration::Height(6);

OLD_PROPOSALS
.save(
deps.as_mut().storage,
0,
&OldMultipleChoiceProposal {
title: "A simple text proposal".to_string(),
description: "This is a simple text proposal".to_string(),
proposer: Addr::unchecked(CREATOR_ADDR),
start_height: env.block.height,
min_voting_period: None,
expiration: max_voting_period.after(&env.block),
choices: vec![
OldCheckedMultipleChoiceOption {
description: "multiple choice option 1".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::Standard,
vote_count: Uint128::zero(),
index: 0,
},
OldCheckedMultipleChoiceOption {
description: "multiple choice option 2".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::Standard,
vote_count: Uint128::zero(),
index: 1,
},
OldCheckedMultipleChoiceOption {
description: "None of the above".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::None,
vote_count: Uint128::zero(),
index: 2,
},
],
status: Status::Open,
voting_strategy: VotingStrategy::SingleChoice {
quorum: cwd_voting::threshold::PercentageThreshold::Majority {},
},
total_power: Uint128::new(100),
votes: MultipleChoiceVotes {
vote_weights: vec![Uint128::zero(); 3],
},
allow_revoting: false,
},
)
.unwrap();

let msg = MigrateMsg::FromV1 {
close_proposal_on_execution_failure: true,
pre_propose_info: PreProposeInfo::AnyoneMayPropose {},
};

migrate(deps.as_mut(), env.clone(), msg.clone()).unwrap();

let migrated_proposal = PROPOSALS.load(deps.as_mut().storage, 0).unwrap();

let expected = MultipleChoiceProposal {
title: "A simple text proposal".to_string(),
description: "This is a simple text proposal".to_string(),
proposer: Addr::unchecked(CREATOR_ADDR),
start_height: env.block.height,
min_voting_period: None,
expiration: max_voting_period.after(&env.block),
choices: vec![
CheckedMultipleChoiceOption {
title: "".to_string(),
description: "multiple choice option 1".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::Standard,
vote_count: Uint128::zero(),
index: 0,
},
CheckedMultipleChoiceOption {
title: "".to_string(),
description: "multiple choice option 2".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::Standard,
vote_count: Uint128::zero(),
index: 1,
},
CheckedMultipleChoiceOption {
title: "".to_string(),
description: "None of the above".to_string(),
msgs: None,
option_type: MultipleChoiceOptionType::None,
vote_count: Uint128::zero(),
index: 2,
},
],
status: Status::Open,
voting_strategy: VotingStrategy::SingleChoice {
quorum: cwd_voting::threshold::PercentageThreshold::Majority {},
},
total_power: Uint128::new(100),
votes: MultipleChoiceVotes {
vote_weights: vec![Uint128::zero(); 3],
},
allow_revoting: false,
};

assert_eq!(migrated_proposal, expected);
}
1 change: 1 addition & 0 deletions packages/cwd-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "Types and methods for CosmWasm DAO voting."

[dependencies]
neutron-sdk = "0.10.0"
cosmwasm-schema = { version = "1.3.0" }
cosmwasm-std = { version = "1.3.0" }
schemars = "0.8.8"
serde = { version = "1.0.175", default-features = false, features = ["derive"] }
Expand Down
10 changes: 10 additions & 0 deletions packages/cwd-voting/src/multiple_choice.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{CosmosMsg, StdError, StdResult, Uint128};
use neutron_sdk::bindings::msg::NeutronMsg;
use schemars::JsonSchema;
Expand Down Expand Up @@ -125,6 +126,15 @@ pub struct CheckedMultipleChoiceOption {
pub vote_count: Uint128,
}

#[cw_serde]
pub struct OldCheckedMultipleChoiceOption {
pub index: u32,
pub option_type: MultipleChoiceOptionType,
pub description: String,
pub msgs: Option<Vec<CosmosMsg<NeutronMsg>>>,
pub vote_count: Uint128,
}

impl MultipleChoiceOptions {
pub fn into_checked(self) -> StdResult<CheckedMultipleChoiceOptions> {
if self.options.len() < 2 || self.options.len() > MAX_NUM_CHOICES as usize {
Expand Down

0 comments on commit 3059a34

Please sign in to comment.