Skip to content

Commit

Permalink
Merge pull request #564 from CosmWasm/549-migrate-intkey-to-naked-int…
Browse files Browse the repository at this point in the history
…-type

Migrate from `IntKey` to new naked int key
  • Loading branch information
ueco-jb authored Dec 1, 2021
2 parents ffd16d6 + 0862505 commit 1e60b0a
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 190 deletions.
4 changes: 2 additions & 2 deletions contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ fn verify_png_logo(logo: &[u8]) -> Result<(), ContractError> {
/// Checks if passed logo is correct, and if not, returns an error
fn verify_logo(logo: &Logo) -> Result<(), ContractError> {
match logo {
Logo::Embedded(EmbeddedLogo::Svg(logo)) => verify_xml_logo(&logo),
Logo::Embedded(EmbeddedLogo::Png(logo)) => verify_png_logo(&logo),
Logo::Embedded(EmbeddedLogo::Svg(logo)) => verify_xml_logo(logo),
Logo::Embedded(EmbeddedLogo::Png(logo)) => verify_png_logo(logo),
Logo::Url(_) => Ok(()), // Any reasonable url validation would be regex based, probably not worth it
}
}
Expand Down
13 changes: 6 additions & 7 deletions contracts/cw20-merkle-airdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use cosmwasm_std::{
};
use cw2::{get_contract_version, set_contract_version};
use cw20::Cw20ExecuteMsg;
use cw_storage_plus::U8Key;
use sha2::Digest;
use std::convert::TryInto;

Expand Down Expand Up @@ -113,7 +112,7 @@ pub fn execute_register_merkle_root(

let stage = LATEST_STAGE.update(deps.storage, |stage| -> StdResult<_> { Ok(stage + 1) })?;

MERKLE_ROOT.save(deps.storage, U8Key::from(stage), &merkle_root)?;
MERKLE_ROOT.save(deps.storage, stage, &merkle_root)?;
LATEST_STAGE.save(deps.storage, &stage)?;

Ok(Response::new().add_attributes(vec![
Expand All @@ -132,13 +131,13 @@ pub fn execute_claim(
proof: Vec<String>,
) -> Result<Response, ContractError> {
// verify not claimed
let claimed = CLAIM.may_load(deps.storage, (&info.sender, U8Key::from(stage)))?;
let claimed = CLAIM.may_load(deps.storage, (&info.sender, stage))?;
if claimed.is_some() {
return Err(ContractError::Claimed {});
}

let config = CONFIG.load(deps.storage)?;
let merkle_root = MERKLE_ROOT.load(deps.storage, stage.into())?;
let merkle_root = MERKLE_ROOT.load(deps.storage, stage)?;

let user_input = format!("{}{}", info.sender, amount);
let hash = sha2::Sha256::digest(user_input.as_bytes())
Expand All @@ -164,7 +163,7 @@ pub fn execute_claim(
}

// Update claim index to the current stage
CLAIM.save(deps.storage, (&info.sender, stage.into()), &true)?;
CLAIM.save(deps.storage, (&info.sender, stage), &true)?;

let res = Response::new()
.add_message(WasmMsg::Execute {
Expand Down Expand Up @@ -205,7 +204,7 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
}

pub fn query_merkle_root(deps: Deps, stage: u8) -> StdResult<MerkleRootResponse> {
let merkle_root = MERKLE_ROOT.load(deps.storage, U8Key::from(stage))?;
let merkle_root = MERKLE_ROOT.load(deps.storage, stage)?;
let resp = MerkleRootResponse { stage, merkle_root };

Ok(resp)
Expand All @@ -219,7 +218,7 @@ pub fn query_latest_stage(deps: Deps) -> StdResult<LatestStageResponse> {
}

pub fn query_is_claimed(deps: Deps, stage: u8, address: String) -> StdResult<IsClaimedResponse> {
let key: (&Addr, U8Key) = (&deps.api.addr_validate(&address)?, stage.into());
let key: (&Addr, u8) = (&deps.api.addr_validate(&address)?, stage);
let is_claimed = CLAIM.may_load(deps.storage, key)?.unwrap_or(false);
let resp = IsClaimedResponse { is_claimed };

Expand Down
6 changes: 3 additions & 3 deletions contracts/cw20-merkle-airdrop/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Addr;
use cw_storage_plus::{Item, Map, U8Key};
use cw_storage_plus::{Item, Map};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Config {
Expand All @@ -18,7 +18,7 @@ pub const LATEST_STAGE_KEY: &str = "stage";
pub const LATEST_STAGE: Item<u8> = Item::new(LATEST_STAGE_KEY);

pub const MERKLE_ROOT_PREFIX: &str = "merkle_root";
pub const MERKLE_ROOT: Map<U8Key, String> = Map::new(MERKLE_ROOT_PREFIX);
pub const MERKLE_ROOT: Map<u8, String> = Map::new(MERKLE_ROOT_PREFIX);

pub const CLAIM_PREFIX: &str = "claim";
pub const CLAIM: Map<(&Addr, U8Key), bool> = Map::new(CLAIM_PREFIX);
pub const CLAIM: Map<(&Addr, u8), bool> = Map::new(CLAIM_PREFIX);
40 changes: 18 additions & 22 deletions contracts/cw3-fixed-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ pub fn execute_propose(
required_weight: cfg.required_weight,
};
let id = next_id(deps.storage)?;
PROPOSALS.save(deps.storage, id.into(), &prop)?;
PROPOSALS.save(deps.storage, id, &prop)?;

// add the first yes vote from voter
let ballot = Ballot {
weight: vote_power,
vote: Vote::Yes,
};
BALLOTS.save(deps.storage, (id.into(), &info.sender), &ballot)?;
BALLOTS.save(deps.storage, (id, &info.sender), &ballot)?;

Ok(Response::new()
.add_attribute("action", "propose")
Expand All @@ -154,7 +154,7 @@ pub fn execute_vote(
.ok_or(ContractError::Unauthorized {})?;

// ensure proposal exists and can be voted on
let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
if prop.status != Status::Open {
return Err(ContractError::NotOpen {});
}
Expand All @@ -163,17 +163,13 @@ pub fn execute_vote(
}

// cast vote if no vote previously cast
BALLOTS.update(
deps.storage,
(proposal_id.into(), &info.sender),
|bal| match bal {
Some(_) => Err(ContractError::AlreadyVoted {}),
None => Ok(Ballot {
weight: vote_power,
vote,
}),
},
)?;
BALLOTS.update(deps.storage, (proposal_id, &info.sender), |bal| match bal {
Some(_) => Err(ContractError::AlreadyVoted {}),
None => Ok(Ballot {
weight: vote_power,
vote,
}),
})?;

// if yes vote, update tally
if vote == Vote::Yes {
Expand All @@ -182,7 +178,7 @@ pub fn execute_vote(
if prop.yes_weight >= prop.required_weight {
prop.status = Status::Passed;
}
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;
}

Ok(Response::new()
Expand All @@ -200,7 +196,7 @@ pub fn execute_execute(
) -> Result<Response, ContractError> {
// anyone can trigger this if the vote passed

let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
// we allow execution even after the proposal "expiration" as long as all vote come in before
// that point. If it was approved on time, it can be executed any time.
if prop.status != Status::Passed {
Expand All @@ -209,7 +205,7 @@ pub fn execute_execute(

// set it to executed
prop.status = Status::Executed;
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;

// dispatch all proposed messages
Ok(Response::new()
Expand All @@ -227,7 +223,7 @@ pub fn execute_close(
) -> Result<Response<Empty>, ContractError> {
// anyone can trigger this if the vote passed

let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
if [Status::Executed, Status::Rejected, Status::Passed]
.iter()
.any(|x| *x == prop.status)
Expand All @@ -240,7 +236,7 @@ pub fn execute_close(

// set it to failed
prop.status = Status::Rejected;
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;

Ok(Response::new()
.add_attribute("action", "close")
Expand Down Expand Up @@ -282,7 +278,7 @@ fn query_threshold(deps: Deps) -> StdResult<ThresholdResponse> {
}

fn query_proposal(deps: Deps, env: Env, id: u64) -> StdResult<ProposalResponse> {
let prop = PROPOSALS.load(deps.storage, id.into())?;
let prop = PROPOSALS.load(deps.storage, id)?;
let status = prop.current_status(&env.block);

let cfg = CONFIG.load(deps.storage)?;
Expand Down Expand Up @@ -371,7 +367,7 @@ fn map_proposal(

fn query_vote(deps: Deps, proposal_id: u64, voter: String) -> StdResult<VoteResponse> {
let voter = deps.api.addr_validate(&voter)?;
let ballot = BALLOTS.may_load(deps.storage, (proposal_id.into(), &voter))?;
let ballot = BALLOTS.may_load(deps.storage, (proposal_id, &voter))?;
let vote = ballot.map(|b| VoteInfo {
voter: voter.into(),
vote: b.vote,
Expand All @@ -390,7 +386,7 @@ fn list_votes(
let start = start_after.map(Bound::exclusive);

let votes: StdResult<Vec<_>> = BALLOTS
.prefix(proposal_id.into())
.prefix(proposal_id)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw3-fixed-multisig/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{Addr, BlockInfo, CosmosMsg, Empty, StdError, StdResult, Stora

use cw0::{Duration, Expiration};
use cw3::{Status, Vote};
use cw_storage_plus::{Item, Map, U64Key};
use cw_storage_plus::{Item, Map};

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct Config {
Expand Down Expand Up @@ -58,8 +58,8 @@ pub const PROPOSAL_COUNT: Item<u64> = Item::new("proposal_count");

// multiple-item maps
pub const VOTERS: Map<&Addr, u64> = Map::new("voters");
pub const PROPOSALS: Map<U64Key, Proposal> = Map::new("proposals");
pub const BALLOTS: Map<(U64Key, &Addr), Ballot> = Map::new("ballots");
pub const PROPOSALS: Map<u64, Proposal> = Map::new("proposals");
pub const BALLOTS: Map<(u64, &Addr), Ballot> = Map::new("ballots");

pub fn next_id(store: &mut dyn Storage) -> StdResult<u64> {
let id: u64 = PROPOSAL_COUNT.may_load(store)?.unwrap_or_default() + 1;
Expand Down
40 changes: 18 additions & 22 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ pub fn execute_propose(
};
prop.update_status(&env.block);
let id = next_id(deps.storage)?;
PROPOSALS.save(deps.storage, id.into(), &prop)?;
PROPOSALS.save(deps.storage, id, &prop)?;

// add the first yes vote from voter
let ballot = Ballot {
weight: vote_power,
vote: Vote::Yes,
};
BALLOTS.save(deps.storage, (id.into(), &info.sender), &ballot)?;
BALLOTS.save(deps.storage, (id, &info.sender), &ballot)?;

Ok(Response::new()
.add_attribute("action", "propose")
Expand All @@ -150,7 +150,7 @@ pub fn execute_vote(
let cfg = CONFIG.load(deps.storage)?;

// ensure proposal exists and can be voted on
let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
if prop.status != Status::Open {
return Err(ContractError::NotOpen {});
}
Expand All @@ -167,22 +167,18 @@ pub fn execute_vote(
.ok_or(ContractError::Unauthorized {})?;

// cast vote if no vote previously cast
BALLOTS.update(
deps.storage,
(proposal_id.into(), &info.sender),
|bal| match bal {
Some(_) => Err(ContractError::AlreadyVoted {}),
None => Ok(Ballot {
weight: vote_power,
vote,
}),
},
)?;
BALLOTS.update(deps.storage, (proposal_id, &info.sender), |bal| match bal {
Some(_) => Err(ContractError::AlreadyVoted {}),
None => Ok(Ballot {
weight: vote_power,
vote,
}),
})?;

// update vote tally
prop.votes.add_vote(vote, vote_power);
prop.update_status(&env.block);
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;

Ok(Response::new()
.add_attribute("action", "vote")
Expand All @@ -199,7 +195,7 @@ pub fn execute_execute(
) -> Result<Response, ContractError> {
// anyone can trigger this if the vote passed

let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
// we allow execution even after the proposal "expiration" as long as all vote come in before
// that point. If it was approved on time, it can be executed any time.
if prop.status != Status::Passed {
Expand All @@ -208,7 +204,7 @@ pub fn execute_execute(

// set it to executed
prop.status = Status::Executed;
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;

// dispatch all proposed messages
Ok(Response::new()
Expand All @@ -226,7 +222,7 @@ pub fn execute_close(
) -> Result<Response<Empty>, ContractError> {
// anyone can trigger this if the vote passed

let mut prop = PROPOSALS.load(deps.storage, proposal_id.into())?;
let mut prop = PROPOSALS.load(deps.storage, proposal_id)?;
if [Status::Executed, Status::Rejected, Status::Passed]
.iter()
.any(|x| *x == prop.status)
Expand All @@ -239,7 +235,7 @@ pub fn execute_close(

// set it to failed
prop.status = Status::Rejected;
PROPOSALS.save(deps.storage, proposal_id.into(), &prop)?;
PROPOSALS.save(deps.storage, proposal_id, &prop)?;

Ok(Response::new()
.add_attribute("action", "close")
Expand Down Expand Up @@ -295,7 +291,7 @@ fn query_threshold(deps: Deps) -> StdResult<ThresholdResponse> {
}

fn query_proposal(deps: Deps, env: Env, id: u64) -> StdResult<ProposalResponse> {
let prop = PROPOSALS.load(deps.storage, id.into())?;
let prop = PROPOSALS.load(deps.storage, id)?;
let status = prop.current_status(&env.block);
let threshold = prop.threshold.to_response(prop.total_weight);
Ok(ProposalResponse {
Expand Down Expand Up @@ -367,7 +363,7 @@ fn map_proposal(

fn query_vote(deps: Deps, proposal_id: u64, voter: String) -> StdResult<VoteResponse> {
let voter_addr = deps.api.addr_validate(&voter)?;
let prop = BALLOTS.may_load(deps.storage, (proposal_id.into(), &voter_addr))?;
let prop = BALLOTS.may_load(deps.storage, (proposal_id, &voter_addr))?;
let vote = prop.map(|b| VoteInfo {
voter,
vote: b.vote,
Expand All @@ -387,7 +383,7 @@ fn list_votes(
let start = addr.map(|addr| Bound::exclusive(addr.as_ref()));

let votes: StdResult<Vec<_>> = BALLOTS
.prefix(proposal_id.into())
.prefix(proposal_id)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw3-flex-multisig/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmwasm_std::{
use cw0::{Duration, Expiration};
use cw3::{Status, Vote};
use cw4::Cw4Contract;
use cw_storage_plus::{Item, Map, U64Key};
use cw_storage_plus::{Item, Map};

use crate::msg::Threshold;

Expand Down Expand Up @@ -153,8 +153,8 @@ pub const CONFIG: Item<Config> = Item::new("config");
pub const PROPOSAL_COUNT: Item<u64> = Item::new("proposal_count");

// multiple-item map
pub const BALLOTS: Map<(U64Key, &Addr), Ballot> = Map::new("votes");
pub const PROPOSALS: Map<U64Key, Proposal> = Map::new("proposals");
pub const BALLOTS: Map<(u64, &Addr), Ballot> = Map::new("votes");
pub const PROPOSALS: Map<u64, Proposal> = Map::new("proposals");

pub fn next_id(store: &mut dyn Storage) -> StdResult<u64> {
let id: u64 = PROPOSAL_COUNT.may_load(store)?.unwrap_or_default() + 1;
Expand Down
Loading

0 comments on commit 1e60b0a

Please sign in to comment.