Skip to content

Commit fc315ac

Browse files
re-giuskianenigma
andauthored
Hide nonce implementation details in metadata (#6562)
See polkadot-fellows/runtimes#248 : using `TypeWithDefault` having derived `TypeInfo` for `Nonce` causes a breaking change in metadata for nonce type because it's no longer `u64`. Adding a default implementation of `TypeInfo` for `TypeWithDefault` to restore the original type info in metadata. --------- Co-authored-by: Kian Paimani <[email protected]>
1 parent f520adb commit fc315ac

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

prdoc/pr_6562.prdoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Hide nonce implementation details in metadata
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: |
9+
Use custom implementation of TypeInfo for TypeWithDefault to show inner value's type info.
10+
This should bring back nonce to u64 in metadata.
11+
12+
crates:
13+
- name: sp-runtime
14+
bump: minor

substrate/primitives/runtime/src/type_with_default.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use num_traits::{
3131
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
3232
Num, NumCast, PrimInt, Saturating, ToPrimitive,
3333
};
34-
use scale_info::TypeInfo;
34+
use scale_info::{StaticTypeInfo, TypeInfo};
3535
use sp_core::Get;
3636

3737
#[cfg(feature = "serde")]
@@ -40,7 +40,8 @@ use serde::{Deserialize, Serialize};
4040
/// A type that wraps another type and provides a default value.
4141
///
4242
/// Passes through arithmetical and many other operations to the inner value.
43-
#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen)]
43+
/// Type information for metadata is the same as the inner value's type.
44+
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
4445
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4546
pub struct TypeWithDefault<T, D: Get<T>>(T, PhantomData<D>);
4647

@@ -50,6 +51,17 @@ impl<T, D: Get<T>> TypeWithDefault<T, D> {
5051
}
5152
}
5253

54+
// Hides implementation details from the outside (for metadata type information).
55+
//
56+
// The type info showed in metadata is the one of the inner value's type.
57+
impl<T: StaticTypeInfo, D: Get<T> + 'static> TypeInfo for TypeWithDefault<T, D> {
58+
type Identity = Self;
59+
60+
fn type_info() -> scale_info::Type {
61+
T::type_info()
62+
}
63+
}
64+
5365
impl<T: Clone, D: Get<T>> Clone for TypeWithDefault<T, D> {
5466
fn clone(&self) -> Self {
5567
Self(self.0.clone(), PhantomData)
@@ -511,6 +523,7 @@ impl<T: HasCompact, D: Get<T>> CompactAs for TypeWithDefault<T, D> {
511523
#[cfg(test)]
512524
mod tests {
513525
use super::TypeWithDefault;
526+
use scale_info::TypeInfo;
514527
use sp_arithmetic::traits::{AtLeast16Bit, AtLeast32Bit, AtLeast8Bit};
515528
use sp_core::Get;
516529

@@ -565,5 +578,11 @@ mod tests {
565578
}
566579
type U128WithDefault = TypeWithDefault<u128, Getu128>;
567580
impl WrapAtLeast32Bit for U128WithDefault {}
581+
582+
assert_eq!(U8WithDefault::type_info(), <u8 as TypeInfo>::type_info());
583+
assert_eq!(U16WithDefault::type_info(), <u16 as TypeInfo>::type_info());
584+
assert_eq!(U32WithDefault::type_info(), <u32 as TypeInfo>::type_info());
585+
assert_eq!(U64WithDefault::type_info(), <u64 as TypeInfo>::type_info());
586+
assert_eq!(U128WithDefault::type_info(), <u128 as TypeInfo>::type_info());
568587
}
569588
}

0 commit comments

Comments
 (0)