soroban-cli icon indicating copy to clipboard operation
soroban-cli copied to clipboard

`stellar contract build` should strip contract specs, removing types that are not referenced by functions or events

Open leighmcculloch opened this issue 6 months ago • 1 comments
trafficstars

We have an issue in the soroban-sdk that any exported contracttypes defined in the SDK, or any library for that matter, that don't get used in the importing contract still end up in the contract spec.

The problem results in some awkward behaviour where we avoid exporting contracttypes in the SDK, even though doing so would be convenient and enable using contracttypes more frequently there.

Because of how proc-macros run in isolation and cannot coordinate in wasm builds in Rust (the ctor crate cannot be used like we use it in non-wasm builds in tests to coordinate), there's nothing the soroban-sdk can do to identify whether a contracttype that sometimes needs to be exported hasn't been used and can be excluded.

The stellar-cli contract build command though does have an opportunity to help here. It can inspect the contract specs and identify unused types that are using up space for now reason. It could either do this widely for any type that is unused, or do it only for types exported from the soroban sdk, because types can specify what library they came from.

This will require some care because it will require rewriting the wasm file after build, which is something the stellar-cli doesn't currently do. Currently the stellar-cli does modify the wasm, but only in an append fashion.

leighmcculloch avatar May 14 '25 22:05 leighmcculloch