-
Notifications
You must be signed in to change notification settings - Fork 23
storage-plus
: Introduction and Basics
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc541c2
storage-plus: intro
uint 67fd9ab
storage-plus: flesh out Basics
uint 951a662
storage-plus: delete "key collisions"
uint 1568dec
Update src/pages/cw-storage-plus.md
uint ef4500f
apply some suggestions from code review
uint f8b6597
apply more suggestions from code review
uint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,45 @@ | ||
# Introduction | ||
|
||
TODO: Describe what low-level interface `cw-storage-plus` builds on, and how it | ||
provides typed abstractions on top of it | ||
Not being able to persist data across calls would limit the utility of smart | ||
contracts. Think of these problems: | ||
|
||
- How could a smart contract implement a token if it could not keep track of | ||
balances? | ||
- How could a smart contract implement a voting system if it could not keep | ||
track of votes? | ||
- How could a smart contract implement a game if it could not keep track of | ||
scores? | ||
|
||
This is why a _CosmWasm_ smart contract has access to the storage facilities | ||
offered by the [_Cosmos SDK_](https://docs.cosmos.network/). These facilities | ||
are essentially a binary key-value store, with records stored on-chain. | ||
|
||
## Storage limits | ||
|
||
While developing smart contracts, it's important to remember on-chain storage | ||
is, as always, pricey. Conventionally, developers often draw the line at a | ||
"small logo image" (think a few KBs). If you need to store bigger things, it's | ||
likely time to consider off-chain storage (like IPFS or some centralized | ||
storage). Techniques for securely and reliably storing large data off-chain are | ||
beyond the scope of this document. | ||
|
||
Trying to minimize bloat is always good practice when it comes to on-chain | ||
storage. | ||
|
||
## What _cw-storage-plus_ builds on | ||
|
||
The smart contract framework itself (`cosmwasm-std`) provides a simple API for | ||
storing and retrieving data. If you're curious, you can check it out right | ||
[here](https://docs.rs/cosmwasm-std/2.0.3/cosmwasm_std/trait.Storage.html). | ||
|
||
This API is raw in that it exposes the **binary** key-value store. While you're | ||
free to use it directly, you're likely to find that finicky and error-prone. | ||
`cw-storage-plus` is a library that builds on top of this API to do the | ||
following: | ||
|
||
- eliminate the need to manually serialize and deserialize data | ||
- provide a type-safe interface for storing and retrieving data | ||
- help manage keys | ||
- provide featureful persistent data structures | ||
|
||
Let's explore! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
# Basics | ||
|
||
## Containers | ||
|
||
`cw-storage-plus`` provides several storage containers that can be used to store | ||
data on the blockchain. The fundamental ones are: | ||
|
||
- [`Item`](containers/item) | ||
- [`Map`](containers/map) | ||
- [`Deque`](containers/deque) | ||
|
||
An [`Item`](containers/item) is a simple container that stores a single value. | ||
The other two are a little more involved - they are collections capable of | ||
storing multiple values, and provide several methods to interact with them. | ||
|
||
## Keys and prefixes | ||
|
||
One task of a storage library like `cw-storage-plus` is to manage the namespace | ||
of keys provided by the blockchain. | ||
|
||
When constructing a container, you must provide a key of type `&'static str` (or | ||
use | ||
[`new_dyn`](https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Item.html#method.new_dyn)). | ||
This usually means you'll be providing a string literal or some constant. | ||
|
||
In the case of an [`Item`](containers/item), the provided string is the exact | ||
key (a standard UTF-8 string) under which the value will be saved. In the case | ||
of collections, the provided string is a prefix (sometimes called `namespace` in | ||
the code); the collection will use this prefix to generate keys (by appending to | ||
it) for the individual values it stores. | ||
|
||
As a contract dev, it is your responsibility to ensure that the keys you provide | ||
are unique and do not conflict with keys used by other parts of the contract. | ||
|
||
<Callout> | ||
Over time, we've learned that using long keys hurts storage performance. If | ||
you _gotta go fast_, a good approach might be to only provide single-character | ||
ASCII prefixes for your containers, like _"a"_, _"b"_, _"c"_, etc. | ||
</Callout> | ||
|
||
Generally speaking, we tried to make it so that as long as you use a unique | ||
prefix for each container, you shouldn't have to worry about key conflicts. | ||
|
||
TODO: Research the key collisions in the warning below more! Then we can provide | ||
better advice. | ||
|
||
<Callout type="warning"> | ||
...yet there's a small _but_. A `Map`'s prefix is length-prefixed. An `Item` | ||
is saved without any sort of length-prefixing of the key. It is, in theory, | ||
possible for an `Item`'s key to conflict with one of the keys generated by a | ||
`Map` or `Deque`. In practice, this is unlikely unless you use very long | ||
prefixes or start your `Item`'s key with the null byte. Probably don't | ||
do that! | ||
</Callout> | ||
|
||
## Values | ||
|
||
In `cw-storage-plus`, every value is saved using JSON serialization. For that to | ||
be possible, only types that implement | ||
[`serde::Serialize`](https://docs.rs/serde/1.0.201/serde/trait.Serialize.html) | ||
and | ||
[`serde::Deserialize`](https://docs.rs/serde/1.0.201/serde/trait.Deserialize.html) | ||
are allowed. | ||
|
||
Most of the Rust standard library types already implement these traits. | ||
Additionally, types you'll find in `cosmwasm_std` (like | ||
[`Addr`](https://docs.rs/cosmwasm-std/2.0.3/cosmwasm_std/struct.Addr.html), | ||
[`Decimal`](https://docs.rs/cosmwasm-std/2.0.3/cosmwasm_std/struct.Decimal.html), | ||
[`Binary`](https://docs.rs/cosmwasm-std/2.0.3/cosmwasm_std/struct.Binary.html) | ||
or [`Coin`](https://docs.rs/cosmwasm-std/2.0.3/cosmwasm_std/struct.Coin.html)) | ||
often do as well. | ||
|
||
If you're writing a custom type and wish to send it across call or put it in | ||
storage, you'll have to derive these traits. | ||
|
||
<Callout> | ||
*cw-storage-plus* uses | ||
[*serde-json-wasm*](https://github.com/CosmWasm/serde-json-wasm) under the | ||
hood. This provides determinism in some corners - | ||
[*serde_json*](https://github.com/serde-rs/json) would not be suited for the | ||
blockchain world! On top of that, `serde-json-wasm` is just smaller. | ||
</Callout> |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.