From 64efc07ca4888e54853cde51277f6637569a501d Mon Sep 17 00:00:00 2001 From: Cyrill Leutwiler Date: Mon, 13 Nov 2023 20:04:57 +0100 Subject: [PATCH] Bugfix `ink_metadata`: `RootLayout` must work for PortableForm (#1989) The constructor `RootLayout::new()` needs to be generic to allow projects relying on working with PortableForm exclusively to re-use the ink! metadata. --- CHANGELOG.md | 3 +++ crates/ink/codegen/src/generator/metadata.rs | 3 ++- crates/metadata/src/layout/mod.rs | 27 ++++++++++---------- crates/metadata/src/layout/tests.rs | 9 +++++++ crates/storage/src/lazy/mapping.rs | 3 ++- crates/storage/src/lazy/mod.rs | 3 ++- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71543f99cca..e81a30944a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,9 @@ You can see a more detailed log of changes below: - Use `decode_all`` for decoding cross contract call result - [#1810](https://github.com/paritytech/ink/pull/1810) - E2E: improve call API, remove `build_message` + callback - [#1782](https://github.com/paritytech/ink/pull/1782) +### Fixed +- `RootLayout::new()` is generic again to allow using `ink_metadata` in pure `PortableForm` contexts - [#1782](https://github.com/paritytech/ink/pull/1989) + ## 4.3.0 ### Fixed diff --git a/crates/ink/codegen/src/generator/metadata.rs b/crates/ink/codegen/src/generator/metadata.rs index 909a503448e..298f4397ff1 100644 --- a/crates/ink/codegen/src/generator/metadata.rs +++ b/crates/ink/codegen/src/generator/metadata.rs @@ -76,11 +76,12 @@ impl Metadata<'_> { quote_spanned!(storage_span=> // Wrap the layout of the contract into the `RootLayout`, because // contract storage key is reserved for all packed fields - ::ink::metadata::layout::Layout::Root(::ink::metadata::layout::RootLayout::new::<#storage_ident, _>( + ::ink::metadata::layout::Layout::Root(::ink::metadata::layout::RootLayout::new( #layout_key, <#storage_ident as ::ink::storage::traits::StorageLayout>::layout( &#key, ), + ::ink::scale_info::meta_type::<#storage_ident>(), )) ) } diff --git a/crates/metadata/src/layout/mod.rs b/crates/metadata/src/layout/mod.rs index a0694ef6d29..c667daa423f 100644 --- a/crates/metadata/src/layout/mod.rs +++ b/crates/metadata/src/layout/mod.rs @@ -154,25 +154,12 @@ impl IntoPortable for RootLayout { } impl RootLayout { - /// Creates a new root layout. - pub fn new(root_key: LayoutKey, layout: L) -> Self - where - Root: TypeInfo + 'static, - L: Into>, - { - Self { - root_key, - layout: Box::new(layout.into()), - ty: meta_type::(), - } - } - /// Creates a new root layout with empty root type. pub fn new_empty(root_key: LayoutKey, layout: L) -> Self where L: Into>, { - Self::new::<(), L>(root_key, layout) + Self::new::(root_key, layout, meta_type::<()>()) } } @@ -180,6 +167,18 @@ impl RootLayout where F: Form, { + /// Create a new root layout + pub fn new(root_key: LayoutKey, layout: L, ty: ::Type) -> Self + where + L: Into>, + { + Self { + root_key, + layout: Box::new(layout.into()), + ty, + } + } + /// Returns the root key of the sub-tree. pub fn root_key(&self) -> &LayoutKey { &self.root_key diff --git a/crates/metadata/src/layout/tests.rs b/crates/metadata/src/layout/tests.rs index db48b2ba9e0..72993dfec10 100644 --- a/crates/metadata/src/layout/tests.rs +++ b/crates/metadata/src/layout/tests.rs @@ -351,3 +351,12 @@ fn runtime_storage_layout_works() { ); assert_eq!(json, expected); } + +#[test] +fn ensure_portable_root_layout_are_supported() { + let root_key = LayoutKey::new(0u32); + let layout = Layout::Struct(StructLayout::new(String::new(), Vec::new())); + let ty = 0.into(); + + let _: RootLayout = RootLayout::new(root_key, layout, ty); +} diff --git a/crates/storage/src/lazy/mapping.rs b/crates/storage/src/lazy/mapping.rs index 22fd15bed85..d8c4893dd29 100644 --- a/crates/storage/src/lazy/mapping.rs +++ b/crates/storage/src/lazy/mapping.rs @@ -369,9 +369,10 @@ const _: () = { KeyType: StorageKey + scale_info::TypeInfo + 'static, { fn layout(_: &Key) -> Layout { - Layout::Root(RootLayout::new::( + Layout::Root(RootLayout::new( LayoutKey::from(&KeyType::KEY), ::layout(&KeyType::KEY), + scale_info::meta_type::(), )) } } diff --git a/crates/storage/src/lazy/mod.rs b/crates/storage/src/lazy/mod.rs index c2ceda7fb85..ef0fed60268 100644 --- a/crates/storage/src/lazy/mod.rs +++ b/crates/storage/src/lazy/mod.rs @@ -266,9 +266,10 @@ const _: () = { KeyType: StorageKey + scale_info::TypeInfo + 'static, { fn layout(_: &Key) -> Layout { - Layout::Root(RootLayout::new::( + Layout::Root(RootLayout::new( LayoutKey::from(&KeyType::KEY), ::layout(&KeyType::KEY), + scale_info::meta_type::(), )) } }