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

range to range raw #576

Merged
merged 47 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d012560
Introduce range_raw over Prefix
maurolacy Dec 10, 2021
c7d1723
Replace prefix() + range() by prefix_de() + range_raw()
maurolacy Dec 10, 2021
7069f39
Replace sub_prefix() + range() by sub_prefix_de() + range_raw()
maurolacy Dec 10, 2021
376cc58
Replace no_prefix() + range() by no_prefix() + range_raw()
maurolacy Dec 10, 2021
e047a90
Rename keys to keys_raw
maurolacy Dec 10, 2021
d0fd20e
Rename range to range_raw
maurolacy Dec 11, 2021
05d1855
Replace no_prefix() + range() by no_prefix() + range_raw() in indexes
maurolacy Dec 11, 2021
57c470f
Replace no_prefix() + range() by no_prefix() + range_raw() in indexed…
maurolacy Dec 11, 2021
249b35f
Replace prefix() + range() by prefix_de() + range_raw() in unique index
maurolacy Dec 11, 2021
a2b64b8
Remove UniqueIndex::sub_prefix
maurolacy Dec 11, 2021
40a587f
Replace prefix() + range() by prefix_de() + range_raw() in multi index
maurolacy Dec 11, 2021
a2112ab
Replace sub_prefix() + range() by sub_prefix_de() + range_raw() in mu…
maurolacy Dec 11, 2021
69d2604
Remove Prefix::range
maurolacy Dec 11, 2021
e6e1926
Rename keys/range to keys_raw/range_raw in indexes
maurolacy Dec 11, 2021
938b897
Rename keys/range to keys_raw/range_raw in indexed maps
maurolacy Dec 11, 2021
0e1af08
Rename keys/range to keys_raw/range_raw in snampshot map
maurolacy Dec 11, 2021
3542f03
Remove sub_/prefix() from indexed maps
maurolacy Dec 11, 2021
6fc2608
Rename prefix_range to prefix_range_raw
maurolacy Dec 11, 2021
8a0d2eb
Rename prefix_range to prefix_range_raw in indexed map
maurolacy Dec 11, 2021
d582728
Rename prefix_range to prefix_range_raw in multi index
maurolacy Dec 11, 2021
2f6b4cb
Remove useless dereference
maurolacy Dec 11, 2021
bb3137c
Rename keys_/range_de to keys/range in Prefix
maurolacy Dec 11, 2021
e06eb14
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
fb36916
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
32c0aae
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
4db106f
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
d835665
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
72a2f4a
Rename prefix_/prefix_range_/keys_/range_de to prefix/prefix_range/ke…
maurolacy Dec 11, 2021
b2f46bc
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in Map for
maurolacy Dec 11, 2021
7d7f425
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in
maurolacy Dec 11, 2021
1f1a87a
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in
maurolacy Dec 11, 2021
42d5db9
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in
maurolacy Dec 11, 2021
b37f1eb
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in
maurolacy Dec 11, 2021
e7aabea
Rename no_prefix to no_prefix_raw, no_prefix_de to no_prefix in
maurolacy Dec 11, 2021
ed513e6
Replace range_raw() plus manual deserialization by keys()
maurolacy Dec 11, 2021
8f00fb3
Replace range_raw() plus manual deserialization by range()
maurolacy Dec 11, 2021
64f3a33
Replace range_raw() plus manual deserialization by keys()
maurolacy Dec 11, 2021
3b4ba15
Replace range_raw() + deserialization by range()
maurolacy Dec 11, 2021
d0d7c59
Replace keys_raw() + deserialization by keys()
maurolacy Dec 11, 2021
3c78f51
Replace range_raw() + deserialization by range()
maurolacy Dec 11, 2021
c5eba5b
Replace keys_raw() + deserialization by keys()
maurolacy Dec 11, 2021
5f9da6b
Replace range_raw() + deserialization by range()
maurolacy Dec 11, 2021
f04f1f3
Replace range_raw() + deserialization by range()
maurolacy Dec 11, 2021
ae2537c
Update test cases/comments in Map for clarity
maurolacy Dec 13, 2021
8a0365f
Update test cases/comments in SnapshotMap
maurolacy Dec 13, 2021
200948f
Adjust indexes comments
maurolacy Dec 13, 2021
4181f54
Update test cases/comments in indexed maps
maurolacy Dec 13, 2021
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
30 changes: 13 additions & 17 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn query_all_allowances(
// we use raw addresses here....
let start = start_after.map(Bound::exclusive);

let res: StdResult<Vec<AllowanceInfo>> = ALLOWANCES
let allowances = ALLOWANCES
.range(deps.storage, start, None, Order::Ascending)
.filter(|item| {
if let Ok((_, allow)) = item {
Expand All @@ -420,16 +420,14 @@ pub fn query_all_allowances(
})
.take(limit)
.map(|item| {
item.and_then(|(k, allow)| {
Ok(AllowanceInfo {
spender: String::from_utf8(k)?,
balance: allow.balance,
expires: allow.expires,
})
item.map(|(addr, allow)| AllowanceInfo {
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
spender: addr.into(),
balance: allow.balance,
expires: allow.expires,
})
})
.collect();
Ok(AllAllowancesResponse { allowances: res? })
.collect::<StdResult<Vec<_>>>()?;
Ok(AllAllowancesResponse { allowances })
}

// return a list of all permissions here
Expand All @@ -441,19 +439,17 @@ pub fn query_all_permissions(
let limit = calc_limit(limit);
let start = start_after.map(Bound::exclusive);

let res: StdResult<Vec<PermissionsInfo>> = PERMISSIONS
let permissions = PERMISSIONS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
item.and_then(|(k, perm)| {
Ok(PermissionsInfo {
spender: String::from_utf8(k)?,
permissions: perm,
})
item.map(|(addr, perm)| PermissionsInfo {
spender: addr.into(),
permissions: perm,
})
})
.collect();
Ok(AllPermissionsResponse { permissions: res? })
.collect::<StdResult<Vec<_>>>()?;
Ok(AllPermissionsResponse { permissions })
}

// Migrate contract if version is lower than current version
Expand Down
20 changes: 9 additions & 11 deletions contracts/cw1155-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Record, Response, StdResult,
SubMsg, Uint128,
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, SubMsg,
Uint128,
};
use cw_storage_plus::Bound;

Expand Down Expand Up @@ -478,10 +478,10 @@ pub fn query(deps: Deps, env: Env, msg: Cw1155QueryMsg) -> StdResult<Binary> {
}
}

fn parse_approval(item: StdResult<Record<Expiration>>) -> StdResult<cw1155::Approval> {
item.and_then(|(k, expires)| {
let spender = String::from_utf8(k)?;
Ok(cw1155::Approval { spender, expires })
fn build_approval(item: StdResult<(Addr, Expiration)>) -> StdResult<cw1155::Approval> {
item.map(|(addr, expires)| cw1155::Approval {
spender: addr.into(),
expires,
})
}

Expand All @@ -501,7 +501,7 @@ fn query_all_approvals(
.range(deps.storage, start, None, Order::Ascending)
.filter(|r| include_expired || r.is_err() || !r.as_ref().unwrap().1.is_expired(&env.block))
.take(limit)
.map(parse_approval)
.map(build_approval)
.collect::<StdResult<_>>()?;
Ok(ApprovedForAllResponse { operators })
}
Expand All @@ -517,9 +517,8 @@ fn query_tokens(

let tokens = BALANCES
.prefix(&owner)
.range(deps.storage, start, None, Order::Ascending)
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap()))
.collect::<StdResult<_>>()?;
Ok(TokensResponse { tokens })
}
Expand All @@ -532,9 +531,8 @@ fn query_all_tokens(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let tokens = TOKENS
.range(deps.storage, start, None, Order::Ascending)
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap()))
.collect::<StdResult<_>>()?;
Ok(TokensResponse { tokens })
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw20-atomic-swap/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdError, StdResult, Storage};
use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdResult, Storage};
use cw_storage_plus::{Bound, Map};

use cw20::{Balance, Expiration};
Expand Down Expand Up @@ -34,7 +34,6 @@ pub fn all_swap_ids(
SWAPS
.keys(storage, start, None, Order::Ascending)
.take(limit)
.map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("Parsing swap id")))
.collect()
}

Expand Down
27 changes: 11 additions & 16 deletions contracts/cw20-base/src/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ pub fn query_all_allowances(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let allowances: StdResult<Vec<AllowanceInfo>> = ALLOWANCES
let allowances = ALLOWANCES
.prefix(&owner_addr)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (k, v) = item?;
Ok(AllowanceInfo {
spender: String::from_utf8(k)?,
allowance: v.allowance,
expires: v.expires,
item.map(|(addr, allow)| AllowanceInfo {
spender: addr.into(),
allowance: allow.allowance,
expires: allow.expires,
})
})
.collect();
Ok(AllAllowancesResponse {
allowances: allowances?,
})
.collect::<StdResult<_>>()?;
Ok(AllAllowancesResponse { allowances })
}

pub fn query_all_accounts(
Expand All @@ -44,15 +41,13 @@ pub fn query_all_accounts(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let accounts: Result<Vec<_>, _> = BALANCES
let accounts = BALANCES
.keys(deps.storage, start, None, Order::Ascending)
.map(String::from_utf8)
.take(limit)
.collect();
.map(|item| item.map(Into::into))
.collect::<StdResult<_>>()?;

Ok(AllAccountsResponse {
accounts: accounts?,
})
Ok(AllAccountsResponse { accounts })
}

#[cfg(test)]
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw20-escrow/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, Coin, Env, Order, StdError, StdResult, Storage, Timestamp};
use cosmwasm_std::{Addr, Coin, Env, Order, StdResult, Storage, Timestamp};
use cw_storage_plus::Map;

use cw20::{Balance, Cw20CoinVerified};
Expand Down Expand Up @@ -96,7 +96,6 @@ pub const ESCROWS: Map<&str, Escrow> = Map::new("escrow");
pub fn all_escrow_ids(storage: &dyn Storage) -> StdResult<Vec<String>> {
ESCROWS
.keys(storage, None, None, Order::Ascending)
.map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("parsing escrow key")))
.collect()
}

Expand Down
26 changes: 12 additions & 14 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,30 @@ fn query_port(deps: Deps) -> StdResult<PortResponse> {
}

fn query_list(deps: Deps) -> StdResult<ListChannelsResponse> {
let channels: StdResult<Vec<_>> = CHANNEL_INFO
.range(deps.storage, None, None, Order::Ascending)
let channels = CHANNEL_INFO
.range_raw(deps.storage, None, None, Order::Ascending)
.map(|r| r.map(|(_, v)| v))
.collect();
Ok(ListChannelsResponse {
channels: channels?,
})
.collect::<StdResult<_>>()?;
Ok(ListChannelsResponse { channels })
}

// make public for ibc tests
pub fn query_channel(deps: Deps, id: String) -> StdResult<ChannelResponse> {
let info = CHANNEL_INFO.load(deps.storage, &id)?;
// this returns Vec<(outstanding, total)>
let state: StdResult<Vec<_>> = CHANNEL_STATE
let state = CHANNEL_STATE
.prefix(&id)
.range(deps.storage, None, None, Order::Ascending)
.map(|r| {
let (k, v) = r?;
let denom = String::from_utf8(k)?;
let outstanding = Amount::from_parts(denom.clone(), v.outstanding);
let total = Amount::from_parts(denom, v.total_sent);
Ok((outstanding, total))
r.map(|(denom, v)| {
let outstanding = Amount::from_parts(denom.clone(), v.outstanding);
let total = Amount::from_parts(denom, v.total_sent);
(outstanding, total)
})
})
.collect();
.collect::<StdResult<Vec<_>>>()?;
// we want (Vec<outstanding>, Vec<total>)
let (balances, total_sent) = state?.into_iter().unzip();
let (balances, total_sent) = state.into_iter().unzip();

Ok(ChannelResponse {
info,
Expand Down
61 changes: 29 additions & 32 deletions contracts/cw3-fixed-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use utils::Expiration;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{
next_id, parse_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS,
};
use crate::state::{next_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw3-fixed-multisig";
Expand Down Expand Up @@ -315,13 +313,13 @@ fn list_proposals(

let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive_int);
let props: StdResult<Vec<_>> = PROPOSALS
let proposals = PROPOSALS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|p| map_proposal(&env.block, &threshold, p))
.collect();
.collect::<StdResult<_>>()?;

Ok(ProposalListResponse { proposals: props? })
Ok(ProposalListResponse { proposals })
}

fn reverse_proposals(
Expand All @@ -338,30 +336,31 @@ fn reverse_proposals(

let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let end = start_before.map(Bound::exclusive_int);
let props: StdResult<Vec<_>> = PROPOSALS
let proposals = PROPOSALS
.range(deps.storage, None, end, Order::Descending)
.take(limit)
.map(|p| map_proposal(&env.block, &threshold, p))
.collect();
.collect::<StdResult<_>>()?;

Ok(ProposalListResponse { proposals: props? })
Ok(ProposalListResponse { proposals })
}

fn map_proposal(
block: &BlockInfo,
threshold: &ThresholdResponse,
item: StdResult<(Vec<u8>, Proposal)>,
item: StdResult<(u64, Proposal)>,
) -> StdResult<ProposalResponse> {
let (key, prop) = item?;
let status = prop.current_status(block);
Ok(ProposalResponse {
id: parse_id(&key)?,
title: prop.title,
description: prop.description,
msgs: prop.msgs,
status,
expires: prop.expires,
threshold: threshold.clone(),
item.map(|(id, prop)| {
let status = prop.current_status(block);
ProposalResponse {
id,
title: prop.title,
description: prop.description,
msgs: prop.msgs,
status,
expires: prop.expires,
threshold: threshold.clone(),
}
})
}

Expand All @@ -385,21 +384,20 @@ fn list_votes(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let votes: StdResult<Vec<_>> = BALLOTS
let votes = BALLOTS
.prefix(proposal_id)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (key, ballot) = item?;
Ok(VoteInfo {
voter: String::from_utf8(key)?,
item.map(|(addr, ballot)| VoteInfo {
voter: addr.into(),
vote: ballot.vote,
weight: ballot.weight,
})
})
.collect();
.collect::<StdResult<_>>()?;

Ok(VoteListResponse { votes: votes? })
Ok(VoteListResponse { votes })
}

fn query_voter(deps: Deps, voter: String) -> StdResult<VoterResponse> {
Expand All @@ -416,19 +414,18 @@ fn list_voters(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let voters: StdResult<Vec<_>> = VOTERS
let voters = VOTERS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (key, weight) = item?;
Ok(VoterDetail {
addr: String::from_utf8(key)?,
item.map(|(addr, weight)| VoterDetail {
addr: addr.into(),
weight,
})
})
.collect();
.collect::<StdResult<_>>()?;

Ok(VoterListResponse { voters: voters? })
Ok(VoterListResponse { voters })
}

#[cfg(test)]
Expand Down
Loading