Skip to content

Commit abda7f3

Browse files
committed
fixed tests and some rustfmt
1 parent 74a712a commit abda7f3

File tree

3 files changed

+95
-73
lines changed

3 files changed

+95
-73
lines changed

packages/storage/Readme.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,52 @@ This is the simplest storage object in this toolkit. It based on the similarly n
3030

3131
This object is meant to be initialized as a static constant in `state.rs`. However, it would also work perfectly fine if it was initialized during run time with a variable key (in this case though, you'd have to remind it what type of object is stored and its serde). Import it using the following lines:
3232

33-
```rust
33+
```ignore
3434
use secret_toolkit_storage::{Item}
3535
```
3636

3737
And initialize it using the following lines:
3838

39-
```rust
39+
```ignore
4040
pub static OWNER: Item<HumanAddr> = Item::new(b"owner");
4141
```
4242

4343
This uses Bincode2 to serde HumanAddr by default. To specify the Serde algorithm as Json, first import it from `secret-toolkit-serialization`
4444

45-
```rust
45+
```ignore
4646
use secret_toolkit_serialization::{Bincode2, Json};
4747
```
4848

4949
then
5050

51-
```rust
51+
```ignore
5252
pub static SOME_ENUM: Item<SomeEnum, Json> = Item::new(b"some_enum");
5353
```
5454

5555
#### **Read/Write**
5656

5757
The way to read/write to/from strorage is to use its methods. These methods are `save`, `load`, `may_load`, `remove`, `update`. Here is an example usecase for each in execution inside `contract.rs`:
5858

59-
```rust
59+
```ignore
6060
// The compiler knows that owner_addr is HumanAddr
6161
let owner_addr = OWNER.load(&deps.storage)?;
6262
```
6363

64-
```rust
64+
```ignore
6565
OWNER.save(&mut deps.storage, &env.message.sender)?;
6666
```
6767

68-
```rust
68+
```ignore
6969
// The compiler knows that may_addr is Option<HumanAddr>
7070
let may_addr = OWNER.may_load(&deps.storage)?;
7171
```
7272

73-
```rust
73+
```ignore
7474
// The compiler knows that may_addr is Option<HumanAddr>
7575
let may_addr = OWNER.remove(&mut deps.storage)?;
7676
```
7777

78-
```rust
78+
```ignore
7979
// The compiler knows that may_addr is Option<HumanAddr>
8080
let may_addr = OWNER.update(&mut deps.storage, |_x| Ok(env.message.sender))?;
8181
```
@@ -97,19 +97,19 @@ The same conventions from `Item` also apply here, that is:
9797

9898
To import and intialize this storage object as a static constant in `state.rs`, do the following:
9999

100-
```rust
100+
```ignore
101101
use secret_toolkit::storage::{AppendStore}
102102
```
103103

104-
```rust
104+
```ignore
105105
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new(b"count");
106106
```
107107

108108
> ❗ Initializing the object as const instead of static will also work but be less efficient since the variable won't be able to cache length data.
109109
110110
Often times we need these storage objects to be associated to a user address or some other key that is variable. In this case, you need not initialize a completely new AppendStore inside `contract.rs`. Instead, you can create a new AppendStore by adding a suffix to an already existing AppendStore. This has the benefit of preventing you from having to rewrite the signature of the AppendStore. For example
111111

112-
```rust
112+
```ignore
113113
// The compiler knows that user_count_store is AppendStore<i32, Bincode2>
114114
let user_count_store = COUNT_STORE.add_suffix(env.message.sender.to_string().as_bytes());
115115
```
@@ -122,13 +122,13 @@ The main user facing methods to read/write to AppendStore are `pop`, `push`, `ge
122122

123123
AppendStore also implements a readonly iterator feature. This feature is also used to create a paging wrapper method called `paging`. The way you create the iterator is:
124124

125-
```rust
125+
```ignore
126126
let iter = user_count_store.iter(&deps.storage)?;
127127
```
128128

129129
More examples can be found in the unit tests. And the paging wrapper is used in the following manner:
130130

131-
```rust
131+
```ignore
132132
let start_page: u32 = 0;
133133
let page_size: u32 = 5;
134134
// The compiler knows that values is Vec<i32>
@@ -143,11 +143,11 @@ This is a storage wrapper based on AppendStore that replicates a double ended li
143143

144144
To import and intialize this storage object as a static constant in `state.rs`, do the following:
145145

146-
```rust
146+
```ignore
147147
use secret_toolkit_storage::{DequeStore}
148148
```
149149

150-
```rust
150+
```ignore
151151
pub static COUNT_STORE: DequeStore<i32> = DequeStore::new(b"count");
152152
```
153153

@@ -172,11 +172,11 @@ be returned in each page.
172172

173173
To import and intialize this storage object as a static constant in `state.rs`, do the following:
174174

175-
```rust
175+
```ignore
176176
use secret_toolkit_storage::{Keymap}
177177
```
178178

179-
```rust
179+
```ignore
180180
pub static ADDR_VOTE: Keymap<HumanAddr, Foo> = Keymap::new(b"vote");
181181
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new(b"vote");
182182
```
@@ -189,7 +189,7 @@ If you need to associate a keymap to a user address (or any other variable), the
189189

190190
For example suppose that in your contract, a user can make multiple bets. Then, you'd want a Keymap to be associated to each user. You would achieve this my doing the following during execution in `contract.rs`.
191191

192-
```rust
192+
```ignore
193193
// The compiler knows that user_bet_store is AppendStore<u32, BetInfo>
194194
let user_count_store = BET_STORE.add_suffix(env.message.sender.to_string().as_bytes());
195195
```
@@ -200,7 +200,7 @@ You can find more examples of using keymaps in the unit tests of Keymap in `keym
200200

201201
To insert, remove, read from the keymap, do the following:
202202

203-
```rust
203+
```ignore
204204
let user_addr: HumanAddr = env.message.sender;
205205
206206
let foo = Foo {
@@ -224,7 +224,7 @@ Keymap also has two paging methods, these are `.paging` and `.paging_keys`. `pag
224224

225225
Here are some select examples from the unit tests:
226226

227-
```rust
227+
```ignore
228228
fn test_keymap_iter_keys() -> StdResult<()> {
229229
let mut storage = MockStorage::new();
230230
@@ -256,7 +256,7 @@ fn test_keymap_iter_keys() -> StdResult<()> {
256256
}
257257
```
258258

259-
```rust
259+
```ignore
260260
fn test_keymap_iter() -> StdResult<()> {
261261
let mut storage = MockStorage::new();
262262

packages/storage/src/append_store.rs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
//! This is achieved by storing each item in a separate storage entry. A special key is reserved
55
//! for storing the length of the collection so far.
66
use std::any::type_name;
7-
use std::sync::Mutex;
8-
use std::{convert::TryInto};
7+
use std::convert::TryInto;
98
use std::marker::PhantomData;
9+
use std::sync::Mutex;
1010

1111
use serde::{de::DeserializeOwned, Serialize};
1212

1313
use cosmwasm_std::{ReadonlyStorage, StdError, StdResult, Storage};
1414

15-
use secret_toolkit_serialization::{Serde, Bincode2};
15+
use secret_toolkit_serialization::{Bincode2, Serde};
1616

1717
const LEN_KEY: &[u8] = b"len";
1818

1919
pub struct AppendStore<'a, T, Ser = Bincode2>
20-
where
21-
T: Serialize + DeserializeOwned,
22-
Ser: Serde,
20+
where
21+
T: Serialize + DeserializeOwned,
22+
Ser: Serde,
2323
{
2424
/// prefix of the newly constructed Storage
2525
namespace: &'a [u8],
@@ -31,7 +31,7 @@ pub struct AppendStore<'a, T, Ser = Bincode2>
3131
serialization_type: PhantomData<Ser>,
3232
}
3333

34-
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser>{
34+
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
3535
/// constructor
3636
pub const fn new(prefix: &'a [u8]) -> Self {
3737
Self {
@@ -65,19 +65,22 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
6565
pub fn get_len<S: ReadonlyStorage>(&self, storage: &S) -> StdResult<u32> {
6666
let mut may_len = self.length.lock().unwrap();
6767
match *may_len {
68-
Some(len) => { Ok(len) },
68+
Some(len) => Ok(len),
6969
None => {
7070
let len_key = [self.as_slice(), LEN_KEY].concat();
7171
if let Some(len_vec) = storage.get(&len_key) {
72-
let len_bytes = len_vec.as_slice().try_into().map_err(|err| StdError::parse_err("u32", err))?;
72+
let len_bytes = len_vec
73+
.as_slice()
74+
.try_into()
75+
.map_err(|err| StdError::parse_err("u32", err))?;
7376
let len = u32::from_be_bytes(len_bytes);
7477
*may_len = Some(len);
7578
Ok(len)
7679
} else {
7780
*may_len = Some(0);
7881
Ok(0)
7982
}
80-
},
83+
}
8184
}
8285
}
8386
/// checks if the collection has any elements
@@ -87,7 +90,9 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
8790
/// gets the element at pos if within bounds
8891
pub fn get_at<S: ReadonlyStorage>(&self, storage: &S, pos: u32) -> StdResult<T> {
8992
let len = self.get_len(storage)?;
90-
if pos > len { return Err(StdError::generic_err("AppendStore access out of bounds")); }
93+
if pos > len {
94+
return Err(StdError::generic_err("AppendStore access out of bounds"));
95+
}
9196
self.get_at_unchecked(storage, pos)
9297
}
9398
/// tries to get the element at pos
@@ -111,7 +116,9 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
111116
/// Replaces data at a position within bounds
112117
pub fn set_at<S: Storage>(&self, storage: &mut S, pos: u32, item: &T) -> StdResult<()> {
113118
let len = self.get_len(storage)?;
114-
if pos >= len { return Err(StdError::generic_err("AppendStore access out of bounds")); }
119+
if pos >= len {
120+
return Err(StdError::generic_err("AppendStore access out of bounds"));
121+
}
115122
self.set_at_unchecked(storage, pos, item)
116123
}
117124
/// Sets data at a given index
@@ -127,7 +134,7 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
127134
}
128135
/// Pops an item from AppendStore
129136
pub fn pop<S: Storage>(&self, storage: &mut S) -> StdResult<T> {
130-
if let Some(len) = self.get_len(storage)?.checked_sub(1) {
137+
if let Some(len) = self.get_len(storage)?.checked_sub(1) {
131138
let item = self.get_at_unchecked(storage, len);
132139
self.set_len(storage, len);
133140
item
@@ -150,7 +157,7 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
150157
return Err(StdError::generic_err("DequeStorage access out of bounds"));
151158
}
152159
let item = self.get_at_unchecked(storage, pos);
153-
160+
154161
for i in pos..(len - 1) {
155162
let element_to_shift = self.get_at_unchecked(storage, i + 1)?;
156163
self.set_at_unchecked(storage, i, &element_to_shift)?;
@@ -159,14 +166,25 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
159166
item
160167
}
161168
/// Returns a readonly iterator
162-
pub fn iter<S: ReadonlyStorage>(&self, storage: &'a S) -> StdResult<AppendStoreIter<T, S, Ser>> {
169+
pub fn iter<S: ReadonlyStorage>(
170+
&self,
171+
storage: &'a S,
172+
) -> StdResult<AppendStoreIter<T, S, Ser>> {
163173
let len = self.get_len(storage)?;
164174
let iter = AppendStoreIter::new(self, storage, 0, len);
165175
Ok(iter)
166176
}
167177
/// does paging with the given parameters
168-
pub fn paging<S: ReadonlyStorage>(&self, storage: &S, start_page: u32, size: u32) -> StdResult<Vec<T>> {
169-
self.iter(storage)?.skip((start_page as usize)*(size as usize)).take(size as usize).collect()
178+
pub fn paging<S: ReadonlyStorage>(
179+
&self,
180+
storage: &S,
181+
start_page: u32,
182+
size: u32,
183+
) -> StdResult<Vec<T>> {
184+
self.iter(storage)?
185+
.skip((start_page as usize) * (size as usize))
186+
.take(size as usize)
187+
.collect()
170188
}
171189
}
172190

@@ -177,7 +195,8 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> Clone for AppendStore<'a,
177195
prefix: self.prefix.clone(),
178196
length: Mutex::new(None),
179197
item_type: self.item_type.clone(),
180-
serialization_type: self.serialization_type.clone() }
198+
serialization_type: self.serialization_type.clone(),
199+
}
181200
}
182201
}
183202

@@ -234,17 +253,17 @@ where
234253
}
235254

236255
impl<'a, T, S, Ser> AppendStoreIter<'a, T, S, Ser>
237-
where
238-
T: Serialize + DeserializeOwned,
239-
S: ReadonlyStorage,
240-
Ser: Serde,
256+
where
257+
T: Serialize + DeserializeOwned,
258+
S: ReadonlyStorage,
259+
Ser: Serde,
241260
{
242261
/// constructor
243262
pub fn new(
244263
append_store: &'a AppendStore<'a, T, Ser>,
245264
storage: &'a S,
246265
start: u32,
247-
end: u32
266+
end: u32,
248267
) -> Self {
249268
Self {
250269
append_store,
@@ -256,10 +275,10 @@ impl<'a, T, S, Ser> AppendStoreIter<'a, T, S, Ser>
256275
}
257276

258277
impl<'a, T, S, Ser> Iterator for AppendStoreIter<'a, T, S, Ser>
259-
where
260-
T: Serialize + DeserializeOwned,
261-
S: ReadonlyStorage,
262-
Ser: Serde,
278+
where
279+
T: Serialize + DeserializeOwned,
280+
S: ReadonlyStorage,
281+
Ser: Serde,
263282
{
264283
type Item = StdResult<T>;
265284

@@ -291,10 +310,10 @@ impl<'a, T, S, Ser> Iterator for AppendStoreIter<'a, T, S, Ser>
291310
}
292311

293312
impl<'a, T, S, Ser> DoubleEndedIterator for AppendStoreIter<'a, T, S, Ser>
294-
where
295-
T: Serialize + DeserializeOwned,
296-
S: ReadonlyStorage,
297-
Ser: Serde,
313+
where
314+
T: Serialize + DeserializeOwned,
315+
S: ReadonlyStorage,
316+
Ser: Serde,
298317
{
299318
fn next_back(&mut self) -> Option<Self::Item> {
300319
if self.start >= self.end {
@@ -323,13 +342,14 @@ where
323342
T: Serialize + DeserializeOwned,
324343
S: ReadonlyStorage,
325344
Ser: Serde,
326-
{}
345+
{
346+
}
327347

328348
#[cfg(test)]
329349
mod tests {
330-
use cosmwasm_std::{testing::{MockStorage}};
350+
use cosmwasm_std::testing::MockStorage;
331351

332-
use secret_toolkit_serialization::{Json};
352+
use secret_toolkit_serialization::Json;
333353

334354
use super::*;
335355

@@ -590,7 +610,7 @@ mod tests {
590610
let mut storage = MockStorage::new();
591611
let json_append_store: AppendStore<i32, Json> = AppendStore::new(b"test2");
592612
json_append_store.push(&mut storage, &1234)?;
593-
613+
594614
let key = [json_append_store.as_slice(), &0_u32.to_be_bytes()].concat();
595615
let bytes = storage.get(&key);
596616
assert_eq!(bytes, Some(b"1234".to_vec()));
@@ -686,4 +706,4 @@ mod tests {
686706

687707
Ok(())
688708
}
689-
}
709+
}

0 commit comments

Comments
 (0)