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

Investigate compressing JSON keys in state.rs #420

Open
ethanfrey opened this issue Sep 14, 2021 · 1 comment
Open

Investigate compressing JSON keys in state.rs #420

ethanfrey opened this issue Sep 14, 2021 · 1 comment

Comments

@ethanfrey
Copy link
Member

ethanfrey commented Sep 14, 2021

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 to be something like:

#[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<u64>,
    /// 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<u64>,
    /// 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<Addr>,
}

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.

@uint
Copy link
Contributor

uint commented Oct 17, 2022

How about just having an alternative binary encoding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants