Skip to content
Snippets Groups Projects
Unverified Commit fc315ac5 authored by Giuseppe Re's avatar Giuseppe Re Committed by GitHub
Browse files

Hide nonce implementation details in metadata (#6562)


See https://github.com/polkadot-fellows/runtimes/issues/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: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>
parent f520adb0
No related merge requests found
Pipeline #507093 waiting for manual action with stages
in 1 hour, 35 minutes, and 22 seconds
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
title: Hide nonce implementation details in metadata
doc:
- audience: Runtime Dev
description: |
Use custom implementation of TypeInfo for TypeWithDefault to show inner value's type info.
This should bring back nonce to u64 in metadata.
crates:
- name: sp-runtime
bump: minor
\ No newline at end of file
......@@ -31,7 +31,7 @@ use num_traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
Num, NumCast, PrimInt, Saturating, ToPrimitive,
};
use scale_info::TypeInfo;
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_core::Get;
#[cfg(feature = "serde")]
......@@ -40,7 +40,8 @@ use serde::{Deserialize, Serialize};
/// A type that wraps another type and provides a default value.
///
/// Passes through arithmetical and many other operations to the inner value.
#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen)]
/// Type information for metadata is the same as the inner value's type.
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TypeWithDefault<T, D: Get<T>>(T, PhantomData<D>);
......@@ -50,6 +51,17 @@ impl<T, D: Get<T>> TypeWithDefault<T, D> {
}
}
// Hides implementation details from the outside (for metadata type information).
//
// The type info showed in metadata is the one of the inner value's type.
impl<T: StaticTypeInfo, D: Get<T> + 'static> TypeInfo for TypeWithDefault<T, D> {
type Identity = Self;
fn type_info() -> scale_info::Type {
T::type_info()
}
}
impl<T: Clone, D: Get<T>> Clone for TypeWithDefault<T, D> {
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
......@@ -511,6 +523,7 @@ impl<T: HasCompact, D: Get<T>> CompactAs for TypeWithDefault<T, D> {
#[cfg(test)]
mod tests {
use super::TypeWithDefault;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{AtLeast16Bit, AtLeast32Bit, AtLeast8Bit};
use sp_core::Get;
......@@ -565,5 +578,11 @@ mod tests {
}
type U128WithDefault = TypeWithDefault<u128, Getu128>;
impl WrapAtLeast32Bit for U128WithDefault {}
assert_eq!(U8WithDefault::type_info(), <u8 as TypeInfo>::type_info());
assert_eq!(U16WithDefault::type_info(), <u16 as TypeInfo>::type_info());
assert_eq!(U32WithDefault::type_info(), <u32 as TypeInfo>::type_info());
assert_eq!(U64WithDefault::type_info(), <u64 as TypeInfo>::type_info());
assert_eq!(U128WithDefault::type_info(), <u128 as TypeInfo>::type_info());
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment