Skip to content

Commit

Permalink
Merge branch 'main' into CI-update-rust-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy authored Jan 30, 2022
2 parents eb5afa8 + 544bf59 commit e4996cc
Show file tree
Hide file tree
Showing 21 changed files with 657 additions and 340 deletions.
4 changes: 2 additions & 2 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub fn query_all_allowances(
) -> StdResult<AllAllowancesResponse> {
let limit = calc_limit(limit);
// we use raw addresses here....
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));

let allowances = ALLOWANCES
.range(deps.storage, start, None, Order::Ascending)
Expand Down Expand Up @@ -437,7 +437,7 @@ pub fn query_all_permissions(
limit: Option<u32>,
) -> StdResult<AllPermissionsResponse> {
let limit = calc_limit(limit);
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));

let permissions = PERMISSIONS
.range(deps.storage, start, None, Order::Ascending)
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw1155-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ fn query_all_approvals(
limit: Option<u32>,
) -> StdResult<ApprovedForAllResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|addr| Bound::exclusive(addr.as_ref()));
let start = start_after.as_ref().map(Bound::exclusive);

let operators = APPROVES
.prefix(&owner)
Expand All @@ -513,7 +513,7 @@ fn query_tokens(
limit: Option<u32>,
) -> StdResult<TokensResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.as_ref().map(|s| Bound::exclusive(s.as_str()));

let tokens = BALANCES
.prefix(&owner)
Expand All @@ -529,7 +529,7 @@ fn query_all_tokens(
limit: Option<u32>,
) -> StdResult<TokensResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.as_ref().map(|s| Bound::exclusive(s.as_str()));
let tokens = TOKENS
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw20-base/src/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn query_all_allowances(
) -> StdResult<AllAllowancesResponse> {
let owner_addr = deps.api.addr_validate(&owner)?;
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.as_bytes().into()));

let allowances = ALLOWANCES
.prefix(&owner_addr)
Expand All @@ -39,7 +39,7 @@ pub fn query_all_accounts(
limit: Option<u32>,
) -> StdResult<AllAccountsResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));

let accounts = BALANCES
.keys(deps.storage, start, None, Order::Ascending)
Expand Down
8 changes: 3 additions & 5 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::msg::{
ListAllowedResponse, ListChannelsResponse, MigrateMsg, PortResponse, QueryMsg, TransferMsg,
};
use crate::state::{AllowInfo, Config, ALLOW_LIST, CHANNEL_INFO, CHANNEL_STATE, CONFIG};
use cw_utils::{nonpayable, one_coin};
use cw_utils::{maybe_addr, nonpayable, one_coin};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw20-ics20";
Expand Down Expand Up @@ -284,10 +284,8 @@ fn list_allowed(
limit: Option<u32>,
) -> StdResult<ListAllowedResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = match start_after {
Some(x) => Some(Bound::exclusive(deps.api.addr_validate(&x)?.into_string())),
None => None,
};
let addr = maybe_addr(deps.api, start_after)?;
let start = addr.as_ref().map(Bound::exclusive);

let allow = ALLOW_LIST
.range(deps.storage, start, None, Order::Ascending)
Expand Down
8 changes: 4 additions & 4 deletions contracts/cw3-fixed-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ fn list_proposals(
limit: Option<u32>,
) -> StdResult<ProposalListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive_int);
let start = start_after.map(Bound::exclusive);
let proposals = PROPOSALS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
Expand All @@ -304,7 +304,7 @@ fn reverse_proposals(
limit: Option<u32>,
) -> StdResult<ProposalListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let end = start_before.map(Bound::exclusive_int);
let end = start_before.map(Bound::exclusive);
let props: StdResult<Vec<_>> = PROPOSALS
.range(deps.storage, None, end, Order::Descending)
.take(limit)
Expand Down Expand Up @@ -351,7 +351,7 @@ fn list_votes(
limit: Option<u32>,
) -> StdResult<VoteListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));

let votes = BALLOTS
.prefix(proposal_id)
Expand Down Expand Up @@ -381,7 +381,7 @@ fn list_voters(
limit: Option<u32>,
) -> StdResult<VoterListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let start = start_after.map(|s| Bound::ExclusiveRaw(s.into()));

let voters = VOTERS
.range(deps.storage, start, None, Order::Ascending)
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn list_proposals(
limit: Option<u32>,
) -> StdResult<ProposalListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive_int);
let start = start_after.map(Bound::exclusive);
let proposals = PROPOSALS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
Expand All @@ -332,7 +332,7 @@ fn reverse_proposals(
limit: Option<u32>,
) -> StdResult<ProposalListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let end = start_before.map(Bound::exclusive_int);
let end = start_before.map(Bound::exclusive);
let props: StdResult<Vec<_>> = PROPOSALS
.range(deps.storage, None, end, Order::Descending)
.take(limit)
Expand Down Expand Up @@ -380,7 +380,7 @@ fn list_votes(
) -> StdResult<VoteListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let addr = maybe_addr(deps.api, start_after)?;
let start = addr.map(|addr| Bound::exclusive(addr.as_ref()));
let start = addr.as_ref().map(Bound::exclusive);

let votes = BALLOTS
.prefix(proposal_id)
Expand Down
2 changes: 1 addition & 1 deletion contracts/cw4-group/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn list_members(
) -> StdResult<MemberListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let addr = maybe_addr(deps.api, start_after)?;
let start = addr.map(|addr| Bound::exclusive(addr.to_string()));
let start = addr.as_ref().map(Bound::exclusive);

let members = MEMBERS
.range(deps.storage, start, None, Order::Ascending)
Expand Down
2 changes: 1 addition & 1 deletion contracts/cw4-stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ fn list_members(
) -> StdResult<MemberListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let addr = maybe_addr(deps.api, start_after)?;
let start = addr.map(|addr| Bound::exclusive(addr.as_ref()));
let start = addr.as_ref().map(Bound::exclusive);

let members = MEMBERS
.range(deps.storage, start, None, Order::Ascending)
Expand Down
184 changes: 184 additions & 0 deletions packages/storage-plus/src/bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#![cfg(feature = "iterator")]

use cosmwasm_std::Addr;
use std::marker::PhantomData;

use crate::de::KeyDeserialize;
use crate::{Prefixer, PrimaryKey};

/// `RawBound` is used to define the two ends of a range, more explicit than `Option<u8>`.
/// `None` means that we don't limit that side of the range at all.
/// `Inclusive` means we use the given bytes as a limit and *include* anything at that exact key.
/// `Exclusive` means we use the given bytes as a limit and *exclude* anything at that exact key.
/// See `Bound` for a type safe way to build these bounds.
#[derive(Clone, Debug)]
pub enum RawBound {
Inclusive(Vec<u8>),
Exclusive(Vec<u8>),
}

/// `Bound` is used to define the two ends of a range.
/// `None` means that we don't limit that side of the range at all.
/// `Inclusive` means we use the given value as a limit and *include* anything at that exact key.
/// `Exclusive` means we use the given value as a limit and *exclude* anything at that exact key.
#[derive(Clone, Debug)]
pub enum Bound<'a, K: PrimaryKey<'a>> {
Inclusive((K, PhantomData<&'a bool>)),
Exclusive((K, PhantomData<&'a bool>)),
InclusiveRaw(Vec<u8>),
ExclusiveRaw(Vec<u8>),
}

impl<'a, K: PrimaryKey<'a>> Bound<'a, K> {
pub fn inclusive<T: Into<K>>(k: T) -> Self {
Self::Inclusive((k.into(), PhantomData))
}

pub fn exclusive<T: Into<K>>(k: T) -> Self {
Self::Exclusive((k.into(), PhantomData))
}

pub fn to_raw_bound(&self) -> RawBound {
match self {
Bound::Inclusive((k, _)) => RawBound::Inclusive(k.joined_key()),
Bound::Exclusive((k, _)) => RawBound::Exclusive(k.joined_key()),
Bound::ExclusiveRaw(raw_k) => RawBound::Exclusive(raw_k.clone()),
Bound::InclusiveRaw(raw_k) => RawBound::Inclusive(raw_k.clone()),
}
}
}

#[derive(Clone, Debug)]
pub enum PrefixBound<'a, K: Prefixer<'a>> {
Inclusive((K, PhantomData<&'a bool>)),
Exclusive((K, PhantomData<&'a bool>)),
}

impl<'a, K: Prefixer<'a>> PrefixBound<'a, K> {
pub fn inclusive<T: Into<K>>(k: T) -> Self {
Self::Inclusive((k.into(), PhantomData))
}

pub fn exclusive<T: Into<K>>(k: T) -> Self {
Self::Exclusive((k.into(), PhantomData))
}

pub fn to_raw_bound(&self) -> RawBound {
match self {
PrefixBound::Exclusive((k, _)) => RawBound::Exclusive(k.joined_prefix()),
PrefixBound::Inclusive((k, _)) => RawBound::Inclusive(k.joined_prefix()),
}
}
}

pub trait Bounder<'a>: PrimaryKey<'a> + Sized {
fn inclusive_bound(self) -> Option<Bound<'a, Self>>;
fn exclusive_bound(self) -> Option<Bound<'a, Self>>;
}

impl<'a> Bounder<'a> for () {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
None
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
None
}
}

impl<'a> Bounder<'a> for &'a [u8] {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<
'a,
T: PrimaryKey<'a> + KeyDeserialize + Prefixer<'a> + Clone,
U: PrimaryKey<'a> + KeyDeserialize + Clone,
> Bounder<'a> for (T, U)
{
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<
'a,
T: PrimaryKey<'a> + Prefixer<'a> + Clone,
U: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize + Clone,
V: PrimaryKey<'a> + KeyDeserialize + Clone,
> Bounder<'a> for (T, U, V)
{
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<'a> Bounder<'a> for &'a str {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<'a> Bounder<'a> for String {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<'a> Bounder<'a> for Vec<u8> {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<'a> Bounder<'a> for &'a Addr {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

impl<'a> Bounder<'a> for Addr {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
}

macro_rules! integer_bound {
(for $($t:ty),+) => {
$(impl<'a> Bounder<'a> for $t {
fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::inclusive(self))
}
fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
Some(Bound::exclusive(self))
}
})*
}
}

integer_bound!(for i8, u8, i16, u16, i32, u32, i64, u64);
Loading

0 comments on commit e4996cc

Please sign in to comment.