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

[stable2407] Backport #6562 #6698

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions prdoc/pr_6562.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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
86 changes: 84 additions & 2 deletions substrate/primitives/runtime/src/type_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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>);

Expand All @@ -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)
Expand Down Expand Up @@ -504,3 +516,73 @@ impl<T: HasCompact, D: Get<T>> CompactAs for TypeWithDefault<T, D> {
Ok(Self::new(val))
}
}
<<<<<<< HEAD
=======

#[cfg(test)]
mod tests {
use super::TypeWithDefault;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{AtLeast16Bit, AtLeast32Bit, AtLeast8Bit};
use sp_core::Get;

#[test]
#[allow(dead_code)]
fn test_type_with_default_impl_base_arithmetic() {
trait WrapAtLeast8Bit: AtLeast8Bit {}
trait WrapAtLeast16Bit: AtLeast16Bit {}
trait WrapAtLeast32Bit: AtLeast32Bit {}

struct Getu8;
impl Get<u8> for Getu8 {
fn get() -> u8 {
0
}
}
type U8WithDefault = TypeWithDefault<u8, Getu8>;
impl WrapAtLeast8Bit for U8WithDefault {}

struct Getu16;
impl Get<u16> for Getu16 {
fn get() -> u16 {
0
}
}
type U16WithDefault = TypeWithDefault<u16, Getu16>;
impl WrapAtLeast16Bit for U16WithDefault {}

struct Getu32;
impl Get<u32> for Getu32 {
fn get() -> u32 {
0
}
}
type U32WithDefault = TypeWithDefault<u32, Getu32>;
impl WrapAtLeast32Bit for U32WithDefault {}

struct Getu64;
impl Get<u64> for Getu64 {
fn get() -> u64 {
0
}
}
type U64WithDefault = TypeWithDefault<u64, Getu64>;
impl WrapAtLeast32Bit for U64WithDefault {}

struct Getu128;
impl Get<u128> for Getu128 {
fn get() -> u128 {
0
}
}
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());
}
}
>>>>>>> fc315ac (Hide nonce implementation details in metadata (#6562))
Loading