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

Reduce gas when using governance proposals to store wasm #761

Closed
ethanfrey opened this issue Feb 22, 2022 · 5 comments · Fixed by #850
Closed

Reduce gas when using governance proposals to store wasm #761

ethanfrey opened this issue Feb 22, 2022 · 5 comments · Fixed by #850
Labels
optional Not absolutely required for the milestone
Milestone

Comments

@ethanfrey
Copy link
Member

wasm contracts tend to be 200-300kb which is large and expensive.

In order to reduce gas, we already zip the wasm on wasmd tx wasm store in order to reduce the tx size by 3x. We never write the wasm to the iavl tree, but to disk directly, where it is handled by Rust code later.

However, I do not believe we perform these optimisations on wasmd tx gov submit-proposal wasm-store. We should check.

Also, even if we reduce the tx size, I am almost certain that when we create the proposal, we store the unzipped wasm. Once we validate the syntax (in memory), we can continue to store the original zip data in the proposal (in the IAVL tree) and unzip it when processing a passed proposal before sending it to the Rust library.

Context: when testing this on osmosis, I got surprisingly high gas costs for creating this proposal and for depositing on it (which read and wrote the proposal)

@ethanfrey ethanfrey modified the milestones: v0.24.0, v0.25.0 Feb 22, 2022
@alpe
Copy link
Contributor

alpe commented Feb 22, 2022

I need to dive deeper but at a first look it seems stored compressed with the proposal:

CLI: https://github.com/CosmWasm/wasmd/blob/master/x/wasm/client/cli/gov_tx.go#L29

then we validate only the length on the compressed data:
https://github.com/CosmWasm/wasmd/blob/master/x/wasm/types/proposal.go#L103

@jhernandezb
Copy link
Contributor

I can confirm that the contract is stored compressed in the proposal we use the following to verify the contract checksum

# download the contract from the proposal
starsd q gov proposal [proposal-number] -o json | jq -r .content.wasm_byte_code | base64 --decode > contract.wasm.gz

# decompress de contract
gzip -d -c contract.wasm.gz > contract.wasm 

# checksum
sha256sum contract.wasm

However this still takes huge amount of gas to submit a proposal and even after it causes issues for voters, taking almost 1M gas to submit a vote see example.

Not really sure if there could be a different way to submit proposals for storing code like would it be possible to submit the hash to governance for approval? and the contract can be uploaded with the regular store command. Not really familiar with the process of verifying and storing contracts but some wallets doesn't even let the user change the gas param (like Keplr Mobile)

@ethanfrey
Copy link
Member Author

ethanfrey commented Mar 2, 2022

Thanks for that check.

I took a brief look at the x/gov module and I don't see how we can somehow store the wasm outside of the contract using the normal submit proposal process.

If someone else wants to dig into the cosmos-sdk and look for some hooks where we can split this in the normal gov process, please do provide that info. The ProposalContent interface doesn't give so much power as far as I can see.

The only idea I can think of is a 2 step proposal:

  1. upload the compressed wasm into some on-chain kv-storage.
  2. upload a gov proposal referencing that location by key.
  3. if it passes, load that data and then use it in the call to the wasmvm

@jhernandezb
Copy link
Contributor

jhernandezb commented Mar 14, 2022

I looked into this there is a func (h Hooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) {} hook, but basically it will have to hijack the gov module to remove the blob from the proposal and move it to it's own storage location, not really fan of this.

The 2 steps would be nice but maybe adding a store deposit that is released when the proposal ends to prevent contracts from being uploaded and never removed.

I also had the idea of submitting the hash only in the proposal assuming a contract build can be reproduced byte to byte, and then the contract can be uploaded with the regular store transaction where it would check if the hash has been previously approved.

@ethanfrey
Copy link
Member Author

I make a proposal how we can handle this with a minor refactor here: #785 (comment)

Happy for feedback

@ethanfrey ethanfrey added the optional Not absolutely required for the milestone label Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optional Not absolutely required for the milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants