CosmWasm / optimizer

Dockerfile and script to deterministically produce the smallest possible Wasm for your Rust contract
Apache License 2.0
123 stars 61 forks source link

Configurable wasm builds #148

Closed CyberHoward closed 5 months ago

CyberHoward commented 8 months ago

Updated version of #130

The need for featured contract compilation has been thoroughly discussed in issue #80. This PR upstreams the ability to optimize smart-contracts with different features, without hurting reproducibility.

Example

As an example let's say you have some functionality that should only be enabled on some specific chain. Currently it's extremely hard to do this without creating different contracts.

Our solution uses cargo's support for custom metadata fields to allow you to specify features that you want your contract to be built with.

I.e. if I want different logic when deploying to the cw-sdk then I add a feature in the contract's Cargo.toml like so:

[features]
cw-sdk = []

To trigger the optimizer into building the contract with this feature add the following to the same Cargo.toml:

[package.metadata.optimizer]
default-build = true
builds = [
  { name = "cw-sdk", features = [
    "cw-sdk",
  ], default-features = false }
]

After running the optimizer image as usual you will be left with:

When using the ARM image:

Extra features

Discussion

Some questions were raised about the additional caching layer that prevents re-build if the build settings are identical. I left this caching layer in place because we (Abstract) make extensive use of it. See here. However as mentioned re-building a previously built package shouldn't take much time. Edit: We decided to remove this layer.

apollo-sturdy commented 6 months ago

@webmaster128 Any chance you have time to review and merge this? Would be very helpful for us.

CyberHoward commented 6 months ago

Applied feedback, thanks!

@apollo-sturdy for the default-build, do you want the ability to build without default features or does the current solution work for you?

webmaster128 commented 6 months ago

Not directly related but FYI I think we should get rid of the aarch64 suffixes at some point. I don't think they have value anymore. https://github.com/CosmWasm/optimizer/issues/151

webmaster128 commented 6 months ago

Cool!

Last question here: are we happy with the naming "default-features" and "default-build" to close to each other? Isn't this likely to cause confusion amongst contract devs who don't have a very clear understanding of how those things relate to each other? Should we rename one, e.g. "default-build" -> "standard-build"?

webmaster128 commented 5 months ago

I'll merge this now and then change the terms to

in a separate PR. Feel free to disagree and let me know.

Thanks a lot for this contributions!

webmaster128 commented 5 months ago

Follow-up work is here: https://github.com/CosmWasm/optimizer/pull/156

ethanfrey commented 5 months ago

Great job @CyberHoward