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

Introduce ConstUint to replace ConstU* for convenient runtime configuration #6193

Open
2 tasks done
conr2d opened this issue Oct 23, 2024 · 1 comment
Open
2 tasks done
Labels
I5-enhancement An additional feature request.

Comments

@conr2d
Copy link
Contributor

conr2d commented Oct 23, 2024

Is there an existing issue?

  • I have searched the existing issues

Experiencing problems? Have you tried our Stack Exchange first?

  • This is not a support question.

Motivation

The current approach in Substrate runtime configuration requires defining separate types (ConstU8, ConstU16, ConstU32, etc.) for constants that represent basic unsigned integer types. This results in repetitive code and limits flexibility.

Request

Consider the proposed introduction of ConstUint in Solution section.

Solution

https://github.com/conr2d/polkadot-sdk/blob/const-uint/substrate/primitives/core/src/const_uint.rs

I have introduced a new type, ConstUint, which implements the Get trait for the basic unsigned number types (u8, u16, u32, u64, u128, and usize). This allows for more flexibility in handling constants in configuring runtimes.

Registered default config:

// Example from pallet_balances::pallet::config_preludes::TestDefaultConfig
#[register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
  type Balance = u64;
  // The ConstU64<1> is replaced by ConstUint<1>.
-  type ExistentialDeposit = ConstU64<1>;
+  type ExistentialDeposit = ConstUint<1>;
  /* ... other configurations ... */
}

Derived config example:

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::DefaultConfig for TestDefaultConfig {
  type Balance = u128;
  // Even though we've changed the Balance type from u64 to u128,
  // no need to specify ConstU128<1> for ExistentialDeposit, as ConstUint covers it.
  // type ExistentialDeposit = ConstU128<1>;
}

Benefits

  • Compile-Time Error Handling
    If the default constant value is out of range for the newly configured type, it will be caught at compile time, preventing potential runtime errors.

  • Simplification of Type Definitions:
    ConstUint reduces the need for multiple ConstU* types, making the configuration simpler and less repetitive.

Next Steps

If this feature is acceptable, I will proceed with creating a pull request for it. Currently, ConstUint is added to sp-core, but I would appreciate your feedback on whether it would be better to move it to parity-common/bounded-collections, in line with other ConstU* types.

Are you willing to help with this request?

Yes!

@conr2d conr2d added the I5-enhancement An additional feature request. label Oct 23, 2024
@bkchr
Copy link
Member

bkchr commented Oct 23, 2024

Generally this is a nice idea!

I would appreciate your feedback on whether it would be better to move it to parity-common/bounded-collections, in line with other ConstU* types

Yes, please do it this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I5-enhancement An additional feature request.
Projects
None yet
Development

No branches or pull requests

2 participants