CosmWasm / CosmWasm/cw-plus

Investigate compressing JSON keys in state.rs

Open
#420 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
527
Forks
362
PR merge metrics
No merged PRs in 30d

Description

We have very nice JSON structs for legibility, and they have been designed to be the public API for the contracts, used by eg. JS devs. However, some structs, especially those we store, are not part of the public API and we do not need to worry about legibility. Many of those have very long names in the keys. Let's try using `#[serde(rename)]` to make that smaller.

I'd suggest trying it on 1 or 2 contracts and see if this gives reduced gas usage and/or wasm size (testing gas usage with wasmd - so we charge for bytes in storage). If it seems to have a nice impact, we can recommend using it. The public API needs to be legible, the internal structs, just usable by this contract.

For an example, we could modify the [cw20-escrow state](https://github.com/CosmWasm/cw-plus/blob/main/contracts/cw20-escrow/src/state.rs#L50-L69) to be something like:

```rust
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct Escrow {
/// arbiter can decide to approve or refund the escrow
#[serde(rename="a")]
pub arbiter: Addr,
/// if approved, funds go to the recipient
#[serde(rename="r")]
pub recipient: Addr,
/// if refunded, funds go to the source
#[serde(rename="s")]
pub source: Addr,
/// When end height set and block height exceeds this value, the escrow is expired.
/// Once an escrow is expired, it can be returned to the original funder (via "refund").
#[serde(rename="h")]
pub end_height: Option,
/// When end time (in seconds since epoch 00:00:00 UTC on 1 January 1970) is set and
/// block time exceeds this value, the escrow is expired.
/// Once an escrow is expired, it can be returned to the original funder (via "refund").
#[serde(rename="t")]
pub end_time: Option,
/// Balance in Native and Cw20 tokens
#[serde(rename="b")]
pub balance: GenericBalance,
/// All possible contracts that we accept tokens from
#[serde(rename="w")]
pub cw20_whitelist: Vec,
}
```

We could even look at GenericBalance and compress that / make storage-friendly versions of Coin that can be converted to-from the normal Coin API (ideally same layout so zero cost into) but store as smaller, simpler JSON.

Contributor guide

Open the contributing guide

Research direction

Start in state.rs and compare the linked cw20-escrow state example, then inspect GenericBalance for the suggested storage representation. Try the change on one or two contracts and use wasmd to compare gas usage and wasm size. Done means the impact is measured and there is a clear recommendation about compressing internal storage keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
blockchain
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.