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

Update MaxEncodedLen derive macro #512

Merged
merged 12 commits into from
Sep 8, 2023
Merged

Update MaxEncodedLen derive macro #512

merged 12 commits into from
Sep 8, 2023

Conversation

pgherveou
Copy link
Contributor

@pgherveou pgherveou commented Sep 6, 2023

#508 fixed the generated code for MaxEncoded derive trait

#[codec(compact)]
foo: u64

// compile to
// old:  u64::max_encoded_len()
// new: <::codec::Compact<u64> as ::codec::MaxEncodedLen>::max_encoded_len()

This is all fine but cause some compilation errors when this field is generic, since Rust can not tell that Compact<T> implements MaxEncodedLen when T implements it, without extra constraints

#[codec(compact)]
deposit: BalanceOf<T>,

// 53 |         pub deposit: BalanceOf<T, OldCurrency>,
//    |                      ^^^^^^^^^ the trait `parity_scale_codec::MaxEncodedLen` is not implemented for `parity_scale_codec::Compact<<OldCurrency as Currency<<T as frame_system::Config>::AccountId>>::Balance>`

@pgherveou pgherveou requested a review from bkchr September 6, 2023 21:32
src/max_encoded_len.rs Outdated Show resolved Hide resolved
@pgherveou pgherveou marked this pull request as ready for review September 7, 2023 08:07
Copy link
Contributor

@koute koute left a comment

Choose a reason for hiding this comment

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

Could you add a test which would not compile without this change?

Comment on lines 36 to 38
fn max_compact_encoded_len() -> usize {
Self::max_encoded_len()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... personally I'd probably prefer to keep this as a #[no_doc] and make it an internal implementation detail, instead of making it public.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes good point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

by internal you mean simply adding the #[doc(hidden)] attribute or is there other things you can do to truly keep it internal?

Copy link
Contributor

Choose a reason for hiding this comment

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

by internal you mean simply adding the #[doc(hidden)] attribute or is there other things you can do to truly keep it internal?

Just hide it. I guess you could also prefix its name, e.g. prepend scale_internal_ to the name (which I did when I was adding scale_internal_decode_bytes on Input)

Copy link
Member

Choose a reason for hiding this comment

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

I don't get why we need this at all.

The first thing should also be that there is a test added that fails with the old derive macro.

@koute
Copy link
Contributor

koute commented Sep 7, 2023

Also, a side note only tangentially related to this PR - when reviewing this code I've noticed that a compact u16's maximum encoded length is apparently 4 bytes!? That is strange, because for all of the other types it only takes at most one extra byte over the size of the type itself, and IIRC the bare minimum to represent a u16 in a space-efficient varint encoding should be 3 bytes (which can actually encode a number up to 21 bits, [edit]although, okay, this assumes that the encodings for different types are compatible and that the maximum size is 64-bit; with different constraints the maximum number of bits is going to be different[/edit]).

But apparently we kinda screwed this up, and that's how we encode u16s:

assert_eq!(Compact(u16::MAX).encode().len(), 4); // Doesn't panic!

Kinda unfortunate that we waste space here.

impl_primitives!(
i8, i16, i32, i64, i128, bool,
Copy link
Member

Choose a reason for hiding this comment

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

You know that you skipped all unsigned types?

Copy link
Contributor

Choose a reason for hiding this comment

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

For the unsigned types the impl_compact macro is used now, so they're not skipped. (:

Comment on lines 36 to 38
fn max_compact_encoded_len() -> usize {
Self::max_encoded_len()
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't get why we need this at all.

The first thing should also be that there is a test added that fails with the old derive macro.

);
let max_len = CompactField::<u64>::max_encoded_len();
assert_eq!(max_len, Compact::<u64>::max_encoded_len() + u64::max_encoded_len());
assert_eq!(max_len, 17);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd prefer to have a separate test for this, instead of modifying an existing one.

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, this test would fail with

  Compiling parity-scale-codec v3.6.6 (/Users/pg/github/parity-scale-codec)                                                                                                                                                                                            
error[E0277]: the trait bound `Compact<T>: MaxEncodedLen` is not satisfied                                                                                                                                                                                              
  --> tests/max_encoded_len.rs:87:5                                                                                                                                                                                                                                     
   |                                                                                                                                                                                                                                                                    
87 |     t: T,                                                                                                                                                                                                                                                          
   |        ^ the trait `MaxEncodedLen` is not implemented for `Compact<T>`                                                                                                                                                                                             
   |                                                                                                                                                                                                                                                                    
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement                                                                                                                                                   
   |                                                                                                                                                                                                                                                                    
84 | #[derive(Encode, MaxEncodedLen, Compact<T>: MaxEncodedLen)]                                                                    
   |                               +++++++++++++++++++++++++++

```rust
	impl<T> ::parity_scale_codec::MaxEncodedLen for Generic<T>
	where
		T: ::parity_scale_codec::MaxEncodedLen,
```
instead of
```
	impl<T> ::parity_scale_codec::MaxEncodedLen for Generic<T>
	where
		T: ::parity_scale_codec::MaxEncodedLen,
		T: ::parity_scale_codec::MaxEncodedLen,
		T: ::parity_scale_codec::MaxEncodedLen,
```
derive/src/trait_bounds.rs Outdated Show resolved Hide resolved
Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

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

Wait, why is this adding a new function max_compact_encoded_len instead of fixing the impl of the max_encoded_len for the Compact wrapper?

@pgherveou
Copy link
Contributor Author

Wait, why is this adding a new function max_compact_encoded_len instead of fixing the impl of the max_encoded_len for the Compact wrapper?

the impl that we fixed previously #508 is not wrong, but it forces us to add extra constraint on the type when used with generics.

See #512 (comment)

If you have a better solution, I'll take it.

@bkchr
Copy link
Member

bkchr commented Sep 7, 2023

Pushed the proper solution. We add a new required trait for HasCompact::Type. But I doubt that someone implements this trait downstream.

We need to seal this trait.

@pgherveou pgherveou changed the title Add MaxCompactEncodedLen Update MaxEncodedLen derive macro Sep 8, 2023
@pgherveou
Copy link
Contributor Author

lgtm too, checking if everything compile properly on polkadot-sdk and will merge this next and create a new release

@pgherveou pgherveou merged commit 1e3f80c into master Sep 8, 2023
@ggwpez ggwpez deleted the pg/max-encoded-len-2 branch October 27, 2023 19:29
ascjones added a commit that referenced this pull request Nov 29, 2023
ascjones added a commit that referenced this pull request Nov 29, 2023
* Revert "Update MaxEncodedLen derive macro (#512)"

This reverts commit 1e3f80c.

* Revert "Fix max_encoded_len for Compact fields (#508)"

This reverts commit ddf9439

* Bump version to 3.6.9

* Cargo.lock

* ui test
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.

4 participants