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

Make a mismatch of PK in an IndexedMap impossible #84

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
79 changes: 41 additions & 38 deletions src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::map::Map;
use crate::prefix::{namespaced_prefix_range, Prefix};
use crate::{Bound, Path};

pub trait IndexList<T> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<T>> + '_>;
pub trait IndexList<PK, T> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<PK, T>> + '_>;
}

/// `IndexedMap` works like a `Map` but has a secondary index
Expand All @@ -32,7 +32,7 @@ impl<'a, K, T, I> IndexedMap<K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// Creates a new [`IndexedMap`] with the given storage key. This is a constant function only suitable
/// when you have a prefix in the form of a static string slice.
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<'a, K, T, I> IndexedMap<K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// save will serialize the model and store, returns an error on serialization issues.
/// this must load the old value to update the indexes properly
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'a, K, T, I> IndexedMap<K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// While `range_raw` over a `prefix` fixes the prefix to one element and iterates over the
/// remaining, `prefix_range_raw` accepts bounds for the lowest and highest elements of the `Prefix`
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'a, K, T, I> IndexedMap<K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
I: IndexList<T>,
I: IndexList<K, T>,
{
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<K::SuperSuffix, T, K::SuperSuffix> {
Prefix::new(self.pk_namespace.as_slice(), &p.prefix())
Expand All @@ -227,7 +227,7 @@ impl<'a, K, T, I> IndexedMap<K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + KeyDeserialize,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// While `range` over a `prefix` fixes the prefix to one element and iterates over the
/// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the
Expand Down Expand Up @@ -334,23 +334,24 @@ mod test {
}

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name, &self.age, &self.name_lastname];
impl<'a, 's> IndexList<&'s str, Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<&'s str, Data>> + '_> {
let v: Vec<&dyn Index<&'s str, Data>> =
vec![&self.name, &self.age, &self.name_lastname];
Box::new(v.into_iter())
}
}

// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
struct DataCompositeMultiIndex<'a, PK> {
// Last type parameter is for signaling pk deserialization
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, String>,
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, PK>,
}

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataCompositeMultiIndex<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name_age];
impl<'a, 's, PK> IndexList<PK, Data> for DataCompositeMultiIndex<'a, PK> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<PK, Data>> + '_> {
let v: Vec<&dyn Index<PK, Data>> = vec![&self.name_age];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -693,31 +694,31 @@ mod test {
last_name: "".to_string(),
age: 42,
};
let pk1: &[u8] = b"5627";
let pk1 = "5627";
map.save(&mut store, pk1, &data1).unwrap();

let data2 = Data {
name: "Juan".to_string(),
last_name: "Perez".to_string(),
age: 13,
};
let pk2: &[u8] = b"5628";
let pk2 = "5628";
map.save(&mut store, pk2, &data2).unwrap();

let data3 = Data {
name: "Maria".to_string(),
last_name: "Young".to_string(),
age: 24,
};
let pk3: &[u8] = b"5629";
let pk3 = "5629";
map.save(&mut store, pk3, &data3).unwrap();

let data4 = Data {
name: "Maria Luisa".to_string(),
last_name: "Bemberg".to_string(),
age: 43,
};
let pk4: &[u8] = b"5630";
let pk4 = "5630";
map.save(&mut store, pk4, &data4).unwrap();

let marias: Vec<_> = map
Expand All @@ -731,15 +732,16 @@ mod test {
assert_eq!(2, count);

// Pks, sorted by (descending) age
assert_eq!(pk1, marias[0].0);
assert_eq!(pk3, marias[1].0);
assert_eq!(pk1.as_bytes(), marias[0].0);
assert_eq!(pk3.as_bytes(), marias[1].0);

// Data
assert_eq!(data1, marias[0].1);
assert_eq!(data3, marias[1].1);
}

#[test]
#[ignore]
fn range_composite_key_by_multi_index() {
let mut store = MockStorage::new();

Expand Down Expand Up @@ -1454,12 +1456,12 @@ mod test {
use super::*;

struct Indexes<'a> {
secondary: UniqueIndex<'a, u64, u64, ()>,
secondary: UniqueIndex<'a, u64, u64, String>,
}

impl<'a> IndexList<u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<u64>> + '_> {
let v: Vec<&dyn Index<u64>> = vec![&self.secondary];
impl<'a, 's> IndexList<&'s str, u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<&'s str, u64>> + '_> {
let v: Vec<&dyn Index<&'s str, u64>> = vec![&self.secondary];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1497,8 +1499,7 @@ mod test {
.range(&store, Some(Bound::exclusive(2u64)), None, Order::Ascending)
.collect::<Result<_, _>>()
.unwrap();

assert_eq!(items, vec![((), 3)]);
matches!(items.as_slice(), [(_, 3)]);
}
}

Expand All @@ -1510,9 +1511,9 @@ mod test {
secondary: MultiIndex<'a, u64, u64, &'a str>,
}

impl<'a> IndexList<u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<u64>> + '_> {
let v: Vec<&dyn Index<u64>> = vec![&self.secondary];
impl<'a> IndexList<&'a str, u64> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<&'a str, u64>> + '_> {
let v: Vec<&dyn Index<&'a str, u64>> = vec![&self.secondary];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1583,9 +1584,11 @@ mod test {
spender: MultiIndex<'a, Addr, Uint128, (Addr, Addr)>,
}

impl<'a> IndexList<Uint128> for Indexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Uint128>> + '_> {
let v: Vec<&dyn Index<Uint128>> = vec![&self.spender];
impl<'a> IndexList<(Addr, Addr), Uint128> for Indexes<'a> {
fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<(Addr, Addr), Uint128>> + '_> {
let v: Vec<&dyn Index<(Addr, Addr), Uint128>> = vec![&self.spender];
Box::new(v.into_iter())
}
}
Expand All @@ -1605,24 +1608,24 @@ mod test {
"allowances__spender",
),
};
let map = IndexedMap::<(&Addr, &Addr), Uint128, Indexes>::new("allowances", indexes);
let map = IndexedMap::<(Addr, Addr), Uint128, Indexes>::new("allowances", indexes);
let mut store = MockStorage::new();

map.save(
&mut store,
(&Addr::unchecked("owner1"), &Addr::unchecked("spender1")),
(Addr::unchecked("owner1"), Addr::unchecked("spender1")),
&Uint128::new(11),
)
.unwrap();
map.save(
&mut store,
(&Addr::unchecked("owner1"), &Addr::unchecked("spender2")),
(Addr::unchecked("owner1"), Addr::unchecked("spender2")),
&Uint128::new(12),
)
.unwrap();
map.save(
&mut store,
(&Addr::unchecked("owner2"), &Addr::unchecked("spender1")),
(Addr::unchecked("owner2"), Addr::unchecked("spender1")),
&Uint128::new(21),
)
.unwrap();
Expand Down Expand Up @@ -1653,7 +1656,7 @@ mod test {

// Prefix over the main values
let items: Vec<_> = map
.prefix(&Addr::unchecked("owner1"))
.prefix(Addr::unchecked("owner1"))
.range_raw(&store, None, None, Order::Ascending)
.collect::<Result<_, _>>()
.unwrap();
Expand Down
46 changes: 31 additions & 15 deletions src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a, K, T, I> IndexedSnapshotMap<K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize,
I: IndexList<T>,
I: IndexList<K, T>,
{
pub fn add_checkpoint(&self, store: &mut dyn Storage, height: u64) -> StdResult<()> {
self.primary.add_checkpoint(store, height)
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a, K, T, I> IndexedSnapshotMap<K, T, I>
where
K: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// save will serialize the model and store, returns an error on serialization issues.
/// this must load the old value to update the indexes properly
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'a, K, T, I> IndexedSnapshotMap<K, T, I>
where
K: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
I: IndexList<K, T>,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand Down Expand Up @@ -225,7 +225,7 @@ impl<'a, K, T, I> IndexedSnapshotMap<K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
I: IndexList<T>,
I: IndexList<K, T>,
{
pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix<K::SuperSuffix, T, K::SuperSuffix> {
Prefix::new(self.pk_namespace.as_slice(), &p.prefix())
Expand All @@ -241,7 +241,7 @@ impl<'a, K, T, I> IndexedSnapshotMap<K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + KeyDeserialize,
I: IndexList<T>,
I: IndexList<K, T>,
{
/// While `range` over a `prefix` fixes the prefix to one element and iterates over the
/// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the
Expand Down Expand Up @@ -325,23 +325,34 @@ mod test {
}

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name, &self.age, &self.name_lastname];
impl<'a, 's> IndexList<&'s str, Data> for DataIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<&'s str, Data>> + '_> {
let v: Vec<&dyn Index<&str, Data>> = vec![&self.name, &self.age, &self.name_lastname];
Box::new(v.into_iter())
}
}

// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
struct DataCompositeMultiIndex<'a, PK = String> {
// Last type parameter is for signaling pk deserialization
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, String>,
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, PK>,
}

// Future Note: this can likely be macro-derived
impl<'a> IndexList<Data> for DataCompositeMultiIndex<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Data>> + '_> {
let v: Vec<&dyn Index<Data>> = vec![&self.name_age];
impl<'a, 's> IndexList<&'s str, Data> for DataCompositeMultiIndex<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<&'s str, Data>> + '_> {
let v: Vec<&dyn Index<&str, Data>> = vec![&self.name_age];
Box::new(v.into_iter())
}
}

impl<'a, 's> IndexList<(&'s str, &'s str), Data>
for DataCompositeMultiIndex<'a, (&'s str, &'s str)>
{
fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<(&'s str, &'s str), Data>> + '_> {
let v: Vec<&dyn Index<(&str, &str), Data>> = vec![&self.name_age];
Box::new(v.into_iter())
}
}
Expand Down Expand Up @@ -1150,8 +1161,13 @@ mod test {
"data__name_age",
),
};
let map =
IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes);
let map = IndexedSnapshotMap::<(&str, &str), _, _>::new(
"data",
"checks",
"changes",
Strategy::EveryBlock,
indexes,
);

// save data
let data1 = Data {
Expand Down
2 changes: 1 addition & 1 deletion src/indexes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cosmwasm_std::{StdResult, Storage};

// Note: we cannot store traits with generic functions inside `Box<dyn Index>`,
// so I pull S: Storage to a top-level
pub trait Index<T>
pub trait Index<K, T>
where
T: Serialize + DeserializeOwned + Clone,
{
Expand Down
3 changes: 2 additions & 1 deletion src/indexes/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ fn deserialize_multi_kv<K: KeyDeserialize, T: DeserializeOwned>(
Ok((K::from_slice(pk)?, v))
}

impl<'a, IK, T, PK> Index<T> for MultiIndex<'a, IK, T, PK>
impl<'a, IK, T, PK, ALTPK> Index<ALTPK, T> for MultiIndex<'a, IK, T, PK>
where
T: Serialize + DeserializeOwned + Clone,
IK: PrimaryKey<'a>,
ALTPK: Into<PK>,
{
fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()> {
let idx = (self.index)(pk, data).joined_extra_key(pk);
Expand Down
3 changes: 2 additions & 1 deletion src/indexes/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ impl<'a, IK, T, PK> UniqueIndex<'a, IK, T, PK> {
}
}

impl<'a, IK, T, PK> Index<T> for UniqueIndex<'a, IK, T, PK>
impl<'a, IK, T, PK, ALTPK> Index<ALTPK, T> for UniqueIndex<'a, IK, T, PK>
where
T: Serialize + DeserializeOwned + Clone,
IK: PrimaryKey<'a>,
ALTPK: Into<PK>,
{
fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()> {
let idx = (self.index)(data);
Expand Down