Skip to content

Remove codec::Encode and codec::Decode derives from generated APIs by default #2008

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented May 23, 2025

Most APIs in Subxt haven't used codec::Encode or codec::Decode in a while; these traits encode and decode bytes from/to a chain based on the precise shape of our generated types and don't take into account any changes in the chain metadata.

One of the issues we recently hit was #2006, whereby Encode and Decode derives in our generated APIs were not playing nicely with compact encoded generics.

A solution is to migrate our generated APIs away from using codec::Encode or codec::Decode and relying only on EncodeAsType and DecodeAstype, which take the chain metadata into account and handle things like encoding and decoding compact encoded values transparently. This PR takes this path.

As a nice side effect, the generated code size is reduced in line count by ~10%.

Users can still derive Encode and Decode themselves on types that they want these traits on, or on everything, but all of the main Subxt interfaces no longer have any reliance on them, and so they do not need to be derviced by default.

Closes #2006

@jsdw jsdw requested a review from a team as a code owner May 23, 2025 14:46
@jsdw jsdw changed the title Remove codec::Encode and codec::Decode from generated APIs by default Remove codec::Encode and codec::Decode derives from generated APIs by default May 23, 2025
@@ -206,7 +206,7 @@ fn generate_storage_entry_fns(
let key = &keys_slice[0];
if key.hasher.ends_with_key() {
let arg = &key.arg_name;
let keys = quote!(#static_storage_key::new(#arg.borrow()));
let keys = quote!(#static_storage_key::new(#arg));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use references anymore because we need the self of the key to work with EncodeAsType and DecodeAsType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic reason was just because previously we would SCALE encode the key before doing anything with it, but now we want to avoid SCALE encoding and so we keep the key itself, requiring us to have ownership of the key.

It might be that we can allow the stored key to be a reference as well or something like that, because we only need it for long enough to encode it given actual metadata!

Copy link
Collaborator

@lexnv lexnv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🙏

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

Successfully merging this pull request may close these issues.

Error originates in the attribute macro subxt::subxt for asset-hub-westend
2 participants