From 264b902ab29396a7d9dfefd2097c8d5e7d75c76c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 20 May 2024 11:25:32 +0100 Subject: [PATCH 001/121] Add view functions parsing and definition --- .../procedural/src/pallet/parse/mod.rs | 10 ++++ .../src/pallet/parse/view_functions.rs | 49 +++++++++++++++++++ substrate/frame/support/test/tests/pallet.rs | 9 ++++ 3 files changed, 68 insertions(+) create mode 100644 substrate/frame/support/procedural/src/pallet/parse/view_functions.rs diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index 6e12774611dd..86398c13e3ef 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -36,6 +36,7 @@ pub mod storage; pub mod tasks; pub mod type_value; pub mod validate_unsigned; +pub mod view_functions; #[cfg(test)] pub mod tests; @@ -104,6 +105,7 @@ impl Def { let mut storages = vec![]; let mut type_values = vec![]; let mut composites: Vec = vec![]; + let mut view_functions = None; for (index, item) in items.iter_mut().enumerate() { let pallet_attr: Option = helper::take_first_item_pallet_attr(item)?; @@ -206,6 +208,9 @@ impl Def { } composites.push(composite); }, + Some(PalletAttr::ViewFunctions(span)) => { + view_functions = Some(view_functions::ViewFunctionsDef::try_from(span)?); + } Some(attr) => { let msg = "Invalid duplicated attribute"; return Err(syn::Error::new(attr.span(), msg)) @@ -560,6 +565,7 @@ mod keyword { syn::custom_keyword!(pallet); syn::custom_keyword!(extra_constants); syn::custom_keyword!(composite_enum); + syn::custom_keyword!(view_functions); } /// Parse attributes for item in pallet module @@ -622,6 +628,7 @@ enum PalletAttr { TypeValue(proc_macro2::Span), ExtraConstants(proc_macro2::Span), Composite(proc_macro2::Span), + ViewFunctions(proc_macro2::Span), } impl PalletAttr { @@ -647,6 +654,7 @@ impl PalletAttr { Self::TypeValue(span) => *span, Self::ExtraConstants(span) => *span, Self::Composite(span) => *span, + Self::ViewFunctions(span) => *span, } } } @@ -712,6 +720,8 @@ impl syn::parse::Parse for PalletAttr { Ok(PalletAttr::ExtraConstants(content.parse::()?.span())) } else if lookahead.peek(keyword::composite_enum) { Ok(PalletAttr::Composite(content.parse::()?.span())) + } else if lookahead.peek(keyword::view_functions) { + Ok(PalletAttr::ViewFunctions(content.parse::()?.span())) } else { Err(lookahead.error()) } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs new file mode 100644 index 000000000000..68a39d76edac --- /dev/null +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -0,0 +1,49 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::{helper, InheritedCallWeightAttr}; +use frame_support_procedural_tools::get_doc_literals; +use proc_macro2::Span; +use quote::ToTokens; +use std::collections::HashMap; +use syn::{spanned::Spanned, ExprClosure}; + +/// Definition of dispatchables typically `impl Pallet { ... }` +pub struct ViewFunctionsDef { + // /// The where_clause used. + // pub where_clause: Option, + // /// A set of usage of instance, must be check for consistency with trait. + // pub instances: Vec, + // /// The index of call item in pallet module. + // pub index: usize, + // /// Information on methods (used for expansion). + // pub methods: Vec, + // /// The span of the pallet::call attribute. + // pub attr_span: proc_macro2::Span, + // /// Docs, specified on the impl Block. + // pub docs: Vec, + // /// The optional `weight` attribute on the `pallet::call`. + // pub inherited_call_weight: Option, +} + +impl ViewFunctionsDef { + pub fn try_from( + attr_span: proc_macro2::Span, + ) -> syn::Result { + Ok(Self { }) + } +} diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index f41e606ad7c3..f5d98799acd1 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -450,6 +450,13 @@ pub mod pallet { _myfield: u32, } + #[pallet::view_functions] + impl Pallet + where + T::AccountId: From + SomeAssociation1, + { + } + #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig where @@ -548,6 +555,8 @@ pub mod pallet { } pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"testpall"; + + } // Test that a pallet with non generic event and generic genesis_config is correctly handled From 987802cb6568544a296fde6a489b14daa8af21f7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 20 May 2024 11:54:16 +0100 Subject: [PATCH 002/121] Add to view functions mod definition --- .../procedural/src/pallet/parse/mod.rs | 2 +- .../src/pallet/parse/view_functions.rs | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index 86398c13e3ef..1f7c36131db9 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -209,7 +209,7 @@ impl Def { composites.push(composite); }, Some(PalletAttr::ViewFunctions(span)) => { - view_functions = Some(view_functions::ViewFunctionsDef::try_from(span)?); + view_functions = Some(view_functions::ViewFunctionsDef::try_from(span, item)?); } Some(attr) => { let msg = "Invalid duplicated attribute"; diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 68a39d76edac..a733b6a3b2e9 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -38,12 +38,51 @@ pub struct ViewFunctionsDef { // pub docs: Vec, // /// The optional `weight` attribute on the `pallet::call`. // pub inherited_call_weight: Option, + view_functions: Vec +} + +pub struct ViewFunctionDef { + pub name: syn::Ident, + pub return_type: syn::Type, } impl ViewFunctionsDef { pub fn try_from( attr_span: proc_macro2::Span, + item: &mut syn::Item, ) -> syn::Result { - Ok(Self { }) + let item_impl = if let syn::Item::Impl(item) = item { + item + } else { + return Err(syn::Error::new(item.span(), "Invalid pallet::view_functions, expected item impl")) + }; + let mut view_functions = Vec::new(); + for item in &mut item_impl.items { + if let syn::ImplItem::Fn(method) = item { + if !matches!(method.vis, syn::Visibility::Public(_)) { + let msg = "Invalid pallet::view_functions, view function must be public: \ + `pub fn`"; + + let span = match method.vis { + syn::Visibility::Inherited => method.sig.span(), + _ => method.vis.span(), + }; + + return Err(syn::Error::new(span, msg)) + } + + let syn::ReturnType::Type(_, type_) = &method.sig.output else { + return Err(syn::Error::new(method.sig.output.span(), "view functions must return a value")) + }; + + view_functions.push(ViewFunctionDef { + name: method.sig.ident.clone(), + return_type: *type_.clone(), + }) + } + } + Ok(Self { + view_functions, + }) } } From 4f7a513fb5004f7ea03fa40cea6239334a31340b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 21 May 2024 13:12:51 +0100 Subject: [PATCH 003/121] Adding traits and types and wiring up expansion --- .../procedural/src/pallet/expand/mod.rs | 1 + .../src/pallet/expand/view_functions.rs | 24 ++++++++++ .../procedural/src/pallet/parse/mod.rs | 2 + substrate/frame/support/src/traits.rs | 3 ++ substrate/frame/support/src/traits/query.rs | 47 +++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 substrate/frame/support/procedural/src/pallet/expand/view_functions.rs create mode 100644 substrate/frame/support/src/traits/query.rs diff --git a/substrate/frame/support/procedural/src/pallet/expand/mod.rs b/substrate/frame/support/procedural/src/pallet/expand/mod.rs index 067839c28463..389923f6f480 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/mod.rs @@ -36,6 +36,7 @@ mod tt_default_parts; mod type_value; mod validate_unsigned; mod warnings; +mod view_functions; use crate::pallet::Def; use quote::ToTokens; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs new file mode 100644 index 000000000000..742dbfaafe02 --- /dev/null +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -0,0 +1,24 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::pallet::Def; +use proc_macro2::TokenStream; + +pub fn expand_view_functions(def: &mut Def) -> TokenStream { + quote::quote!() +} + diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index 1f7c36131db9..b9b68a0ff8b9 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -71,6 +71,7 @@ pub struct Def { pub frame_system: syn::Path, pub frame_support: syn::Path, pub dev_mode: bool, + pub view_functions: Option, } impl Def { @@ -256,6 +257,7 @@ impl Def { frame_system, frame_support, dev_mode, + view_functions, }; def.check_instance_usage()?; diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index a423656c394f..578913e70ad9 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -132,6 +132,9 @@ pub use tasks::Task; #[cfg(feature = "try-runtime")] mod try_runtime; + +mod query; + #[cfg(feature = "try-runtime")] pub use try_runtime::{ Select as TryStateSelect, TryDecodeEntireStorage, TryDecodeEntireStorageError, TryState, diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs new file mode 100644 index 000000000000..16436c9b1e92 --- /dev/null +++ b/substrate/frame/support/src/traits/query.rs @@ -0,0 +1,47 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License fsor the specific language governing permissions and +// limitations under the License. + +//! Traits for querying pallet view functions. + +use codec::{Decode, Encode}; + +/// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix +pub trait DispatchQuery { + fn dispatch_query(id: &QueryId, input: Vec) -> Result, codec::Error>; +} + +pub trait QueryIdPrefix { + const PREFIX: [u8; 16]; // same as `PalletInfo::name_hash` twox_128 +} + +pub trait QueryIdSuffix { + const SUFFIX: [u8; 16]; +} + +#[derive(Encode, Decode)] +pub struct QueryId { + prefix: [u8; 16], + suffix: [u8; 16], +} + +/// implemented for each pallet view function method +pub trait Query { + type ReturnType: codec::Codec; + + fn query(self) -> Self::ReturnType; +} + From e8af3c2238e37ff64983c5c744b9734b316d7b7c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 May 2024 09:46:47 +0100 Subject: [PATCH 004/121] Use ToTokens for expansion --- .../src/pallet/expand/view_functions.rs | 34 +++++++++++++++++- .../src/pallet/parse/view_functions.rs | 35 ++++++++++++++----- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 742dbfaafe02..c172443cb659 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -16,9 +16,41 @@ // limitations under the License. use crate::pallet::Def; +use inflector::Inflector; use proc_macro2::TokenStream; +use quote::ToTokens; +use crate::pallet::parse::view_functions::{ + ViewFunctionDef, ViewFunctionsDef +}; pub fn expand_view_functions(def: &mut Def) -> TokenStream { - quote::quote!() + let Some(view_fns_def) = def.view_functions.as_ref() else { + return TokenStream::new(); + }; + + quote::quote! { + #view_fns_def + } +} + +impl ToTokens for ViewFunctionsDef { + fn to_tokens(&self, tokens: &mut TokenStream) { + let view_fn_impls = self.view_functions.iter().map(|view_fn| { + quote::quote! { #view_fn } + }); + + tokens.extend(quote::quote! { + #( #view_fn_impls )* + }); + } +} + +impl ToTokens for ViewFunctionDef { + fn to_tokens(&self, tokens: &mut TokenStream) { + let name = self.query_struct_ident(); + tokens.extend(quote::quote! { + pub struct #name; + }); + } } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index a733b6a3b2e9..8a9f89598cea 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -20,6 +20,7 @@ use frame_support_procedural_tools::get_doc_literals; use proc_macro2::Span; use quote::ToTokens; use std::collections::HashMap; +use inflector::Inflector; use syn::{spanned::Spanned, ExprClosure}; /// Definition of dispatchables typically `impl Pallet { ... }` @@ -38,11 +39,12 @@ pub struct ViewFunctionsDef { // pub docs: Vec, // /// The optional `weight` attribute on the `pallet::call`. // pub inherited_call_weight: Option, - view_functions: Vec + pub view_functions: Vec } pub struct ViewFunctionDef { pub name: syn::Ident, + pub args: Vec, pub return_type: syn::Type, } @@ -71,14 +73,8 @@ impl ViewFunctionsDef { return Err(syn::Error::new(span, msg)) } - let syn::ReturnType::Type(_, type_) = &method.sig.output else { - return Err(syn::Error::new(method.sig.output.span(), "view functions must return a value")) - }; - - view_functions.push(ViewFunctionDef { - name: method.sig.ident.clone(), - return_type: *type_.clone(), - }) + let view_fn_def = ViewFunctionDef::try_from(method.clone())?; + view_functions.push(view_fn_def) } } Ok(Self { @@ -86,3 +82,24 @@ impl ViewFunctionsDef { }) } } + +impl TryFrom for ViewFunctionDef { + type Error = syn::Error; + fn try_from(method: syn::ImplItemFn) -> Result { + let syn::ReturnType::Type(_, type_) = method.sig.output else { + return Err(syn::Error::new(method.sig.output.span(), "view functions must return a value")) + }; + + Ok(Self { + name: method.sig.ident.clone(), + args: method.sig.inputs.iter().cloned().collect::>(), + return_type: *type_.clone(), + }) + } +} + +impl ViewFunctionDef { + pub fn query_struct_ident(&self) -> syn::Ident { + syn::Ident::new(&format!("{}Query", self.name), self.name.span()) + } +} From 70387108bbb1a1609106566642fc1a287d63073c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 May 2024 09:47:57 +0100 Subject: [PATCH 005/121] fmtl --- .../procedural/src/pallet/expand/mod.rs | 2 +- .../src/pallet/expand/view_functions.rs | 51 +++++++++---------- .../src/pallet/parse/view_functions.rs | 23 +++++---- substrate/frame/support/src/traits/query.rs | 15 +++--- substrate/frame/support/test/tests/pallet.rs | 8 +-- 5 files changed, 46 insertions(+), 53 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/mod.rs b/substrate/frame/support/procedural/src/pallet/expand/mod.rs index 389923f6f480..54e847c87157 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/mod.rs @@ -35,8 +35,8 @@ mod tasks; mod tt_default_parts; mod type_value; mod validate_unsigned; -mod warnings; mod view_functions; +mod warnings; use crate::pallet::Def; use quote::ToTokens; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index c172443cb659..5de6db10f0f0 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -15,42 +15,41 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::pallet::Def; +use crate::pallet::{ + parse::view_functions::{ViewFunctionDef, ViewFunctionsDef}, + Def, +}; use inflector::Inflector; use proc_macro2::TokenStream; use quote::ToTokens; -use crate::pallet::parse::view_functions::{ - ViewFunctionDef, ViewFunctionsDef -}; pub fn expand_view_functions(def: &mut Def) -> TokenStream { - let Some(view_fns_def) = def.view_functions.as_ref() else { - return TokenStream::new(); - }; + let Some(view_fns_def) = def.view_functions.as_ref() else { + return TokenStream::new(); + }; - quote::quote! { - #view_fns_def - } + quote::quote! { + #view_fns_def + } } impl ToTokens for ViewFunctionsDef { - fn to_tokens(&self, tokens: &mut TokenStream) { - let view_fn_impls = self.view_functions.iter().map(|view_fn| { - quote::quote! { #view_fn } - }); - - tokens.extend(quote::quote! { - #( #view_fn_impls )* - }); - } + fn to_tokens(&self, tokens: &mut TokenStream) { + let view_fn_impls = self.view_functions.iter().map(|view_fn| { + quote::quote! { #view_fn } + }); + + tokens.extend(quote::quote! { + #( #view_fn_impls )* + }); + } } impl ToTokens for ViewFunctionDef { - fn to_tokens(&self, tokens: &mut TokenStream) { - let name = self.query_struct_ident(); - tokens.extend(quote::quote! { - pub struct #name; - }); - } + fn to_tokens(&self, tokens: &mut TokenStream) { + let name = self.query_struct_ident(); + tokens.extend(quote::quote! { + pub struct #name; + }); + } } - diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 8a9f89598cea..b95919f9f397 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -17,10 +17,10 @@ use super::{helper, InheritedCallWeightAttr}; use frame_support_procedural_tools::get_doc_literals; +use inflector::Inflector; use proc_macro2::Span; use quote::ToTokens; use std::collections::HashMap; -use inflector::Inflector; use syn::{spanned::Spanned, ExprClosure}; /// Definition of dispatchables typically `impl Pallet { ... }` @@ -39,7 +39,7 @@ pub struct ViewFunctionsDef { // pub docs: Vec, // /// The optional `weight` attribute on the `pallet::call`. // pub inherited_call_weight: Option, - pub view_functions: Vec + pub view_functions: Vec, } pub struct ViewFunctionDef { @@ -49,14 +49,14 @@ pub struct ViewFunctionDef { } impl ViewFunctionsDef { - pub fn try_from( - attr_span: proc_macro2::Span, - item: &mut syn::Item, - ) -> syn::Result { + pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result { let item_impl = if let syn::Item::Impl(item) = item { item } else { - return Err(syn::Error::new(item.span(), "Invalid pallet::view_functions, expected item impl")) + return Err(syn::Error::new( + item.span(), + "Invalid pallet::view_functions, expected item impl", + )) }; let mut view_functions = Vec::new(); for item in &mut item_impl.items { @@ -77,9 +77,7 @@ impl ViewFunctionsDef { view_functions.push(view_fn_def) } } - Ok(Self { - view_functions, - }) + Ok(Self { view_functions }) } } @@ -87,7 +85,10 @@ impl TryFrom for ViewFunctionDef { type Error = syn::Error; fn try_from(method: syn::ImplItemFn) -> Result { let syn::ReturnType::Type(_, type_) = method.sig.output else { - return Err(syn::Error::new(method.sig.output.span(), "view functions must return a value")) + return Err(syn::Error::new( + method.sig.output.span(), + "view functions must return a value", + )) }; Ok(Self { diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 16436c9b1e92..33261b80d44a 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -21,27 +21,26 @@ use codec::{Decode, Encode}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { - fn dispatch_query(id: &QueryId, input: Vec) -> Result, codec::Error>; + fn dispatch_query(id: &QueryId, input: Vec) -> Result, codec::Error>; } pub trait QueryIdPrefix { - const PREFIX: [u8; 16]; // same as `PalletInfo::name_hash` twox_128 + const PREFIX: [u8; 16]; // same as `PalletInfo::name_hash` twox_128 } pub trait QueryIdSuffix { - const SUFFIX: [u8; 16]; + const SUFFIX: [u8; 16]; } #[derive(Encode, Decode)] pub struct QueryId { - prefix: [u8; 16], - suffix: [u8; 16], + prefix: [u8; 16], + suffix: [u8; 16], } /// implemented for each pallet view function method pub trait Query { - type ReturnType: codec::Codec; + type ReturnType: codec::Codec; - fn query(self) -> Self::ReturnType; + fn query(self) -> Self::ReturnType; } - diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index f5d98799acd1..7f7f2788d4e7 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -451,11 +451,7 @@ pub mod pallet { } #[pallet::view_functions] - impl Pallet - where - T::AccountId: From + SomeAssociation1, - { - } + impl Pallet where T::AccountId: From + SomeAssociation1 {} #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig @@ -555,8 +551,6 @@ pub mod pallet { } pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"testpall"; - - } // Test that a pallet with non generic event and generic genesis_config is correctly handled From 9267519fc00eb086a8c9cae538ac450a3a69e952 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 May 2024 14:41:59 +0100 Subject: [PATCH 006/121] Adding generics etc to query struct --- .../src/pallet/expand/view_functions.rs | 53 +++++++++++-------- .../procedural/src/pallet/parse/mod.rs | 4 +- .../src/pallet/parse/view_functions.rs | 38 +++++++------ 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 5de6db10f0f0..a215747af4cf 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -16,40 +16,51 @@ // limitations under the License. use crate::pallet::{ - parse::view_functions::{ViewFunctionDef, ViewFunctionsDef}, + parse::view_functions::{ViewFunctionDef, ViewFunctionsImplDef}, Def, }; -use inflector::Inflector; use proc_macro2::TokenStream; -use quote::ToTokens; -pub fn expand_view_functions(def: &mut Def) -> TokenStream { +pub fn expand_view_functions(def: &Def) -> TokenStream { let Some(view_fns_def) = def.view_functions.as_ref() else { return TokenStream::new(); }; + let view_fn_impls = view_fns_def.view_functions.iter().map(|view_fn| { + expand_view_function(def, &view_fns_def, view_fn) + }); + quote::quote! { - #view_fns_def + #( #view_fn_impls )* } } -impl ToTokens for ViewFunctionsDef { - fn to_tokens(&self, tokens: &mut TokenStream) { - let view_fn_impls = self.view_functions.iter().map(|view_fn| { - quote::quote! { #view_fn } - }); +fn expand_view_function(def: &Def, view_fns_impl: &ViewFunctionsImplDef, view_fn: &ViewFunctionDef) -> TokenStream { + let span = view_fns_impl.attr_span; + let frame_support = &def.frame_support; + let type_impl_gen = &def.type_impl_generics(span); + let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); + let type_use_gen = &def.type_use_generics(span); + let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; + let where_clause = &view_fns_impl.where_clause; - tokens.extend(quote::quote! { - #( #view_fn_impls )* - }); - } -} + let query_struct_ident = view_fn.query_struct_ident(); + quote::quote! { + #[derive( + #frame_support::RuntimeDebugNoBound, + #frame_support::CloneNoBound, + #frame_support::EqNoBound, + #frame_support::PartialEqNoBound, + #frame_support::__private::codec::Encode, + #frame_support::__private::codec::Decode, + #frame_support::__private::scale_info::TypeInfo, + )] + #[codec(encode_bound())] + #[codec(decode_bound())] + #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] + #[allow(non_camel_case_types)] + pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { -impl ToTokens for ViewFunctionDef { - fn to_tokens(&self, tokens: &mut TokenStream) { - let name = self.query_struct_ident(); - tokens.extend(quote::quote! { - pub struct #name; - }); + }; } } diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index b9b68a0ff8b9..30e64ac67621 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -71,7 +71,7 @@ pub struct Def { pub frame_system: syn::Path, pub frame_support: syn::Path, pub dev_mode: bool, - pub view_functions: Option, + pub view_functions: Option, } impl Def { @@ -210,7 +210,7 @@ impl Def { composites.push(composite); }, Some(PalletAttr::ViewFunctions(span)) => { - view_functions = Some(view_functions::ViewFunctionsDef::try_from(span, item)?); + view_functions = Some(view_functions::ViewFunctionsImplDef::try_from(span, item)?); } Some(attr) => { let msg = "Invalid duplicated attribute"; diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index b95919f9f397..a3bbd949ff1f 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -15,26 +15,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::{helper, InheritedCallWeightAttr}; -use frame_support_procedural_tools::get_doc_literals; -use inflector::Inflector; -use proc_macro2::Span; -use quote::ToTokens; -use std::collections::HashMap; -use syn::{spanned::Spanned, ExprClosure}; +use syn::{spanned::Spanned}; /// Definition of dispatchables typically `impl Pallet { ... }` -pub struct ViewFunctionsDef { - // /// The where_clause used. - // pub where_clause: Option, +pub struct ViewFunctionsImplDef { + /// The where_clause used. + pub where_clause: Option, // /// A set of usage of instance, must be check for consistency with trait. // pub instances: Vec, // /// The index of call item in pallet module. // pub index: usize, // /// Information on methods (used for expansion). // pub methods: Vec, - // /// The span of the pallet::call attribute. - // pub attr_span: proc_macro2::Span, + /// The span of the pallet::view_functions attribute. + pub attr_span: proc_macro2::Span, // /// Docs, specified on the impl Block. // pub docs: Vec, // /// The optional `weight` attribute on the `pallet::call`. @@ -42,13 +36,7 @@ pub struct ViewFunctionsDef { pub view_functions: Vec, } -pub struct ViewFunctionDef { - pub name: syn::Ident, - pub args: Vec, - pub return_type: syn::Type, -} - -impl ViewFunctionsDef { +impl ViewFunctionsImplDef { pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result { let item_impl = if let syn::Item::Impl(item) = item { item @@ -77,10 +65,20 @@ impl ViewFunctionsDef { view_functions.push(view_fn_def) } } - Ok(Self { view_functions }) + Ok(Self { + view_functions, + attr_span, + where_clause: item_impl.generics.where_clause.clone(), + }) } } +pub struct ViewFunctionDef { + pub name: syn::Ident, + pub args: Vec, + pub return_type: syn::Type, +} + impl TryFrom for ViewFunctionDef { type Error = syn::Error; fn try_from(method: syn::ImplItemFn) -> Result { From c6a4e42eee985d2a35d8f595aa1b75d8cdb140e9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 May 2024 14:42:29 +0100 Subject: [PATCH 007/121] fmt --- .../procedural/src/pallet/expand/view_functions.rs | 13 +++++++++---- .../procedural/src/pallet/parse/view_functions.rs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index a215747af4cf..b35aea6bfc32 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -26,16 +26,21 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { return TokenStream::new(); }; - let view_fn_impls = view_fns_def.view_functions.iter().map(|view_fn| { - expand_view_function(def, &view_fns_def, view_fn) - }); + let view_fn_impls = view_fns_def + .view_functions + .iter() + .map(|view_fn| expand_view_function(def, &view_fns_def, view_fn)); quote::quote! { #( #view_fn_impls )* } } -fn expand_view_function(def: &Def, view_fns_impl: &ViewFunctionsImplDef, view_fn: &ViewFunctionDef) -> TokenStream { +fn expand_view_function( + def: &Def, + view_fns_impl: &ViewFunctionsImplDef, + view_fn: &ViewFunctionDef, +) -> TokenStream { let span = view_fns_impl.attr_span; let frame_support = &def.frame_support; let type_impl_gen = &def.type_impl_generics(span); diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index a3bbd949ff1f..735f7b8cbe30 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use syn::{spanned::Spanned}; +use syn::spanned::Spanned; /// Definition of dispatchables typically `impl Pallet { ... }` pub struct ViewFunctionsImplDef { From 140f7d97a2d0b0d4803474a2a6713268c2116dca Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 23 May 2024 09:34:15 +0100 Subject: [PATCH 008/121] example with args --- substrate/frame/support/test/tests/pallet.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index 7f7f2788d4e7..4bb2c3263edc 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -451,7 +451,20 @@ pub mod pallet { } #[pallet::view_functions] - impl Pallet where T::AccountId: From + SomeAssociation1 {} + impl Pallet + where + T::AccountId: From + SomeAssociation1, + { + /// Query value no args. + pub fn get_value() -> Option { + Value::::get() + } + + /// Query value with args. + pub fn get_value_with_arg(key: u16) -> Option { + Map2::::get(key) + } + } #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig From 23b9ebfc906d57ec1d88a2535db5cb562da7f2c6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 23 May 2024 13:37:55 +0100 Subject: [PATCH 009/121] add example for testing --- Cargo.lock | 17 ++++ substrate/frame/examples/Cargo.toml | 3 + substrate/frame/examples/src/lib.rs | 3 + .../frame/examples/view-functions/Cargo.toml | 55 +++++++++++++ .../frame/examples/view-functions/src/lib.rs | 82 +++++++++++++++++++ .../frame/examples/view-functions/src/mock.rs | 51 ++++++++++++ .../examples/view-functions/src/tests.rs | 31 +++++++ 7 files changed, 242 insertions(+) create mode 100644 substrate/frame/examples/view-functions/Cargo.toml create mode 100644 substrate/frame/examples/view-functions/src/lib.rs create mode 100644 substrate/frame/examples/view-functions/src/mock.rs create mode 100644 substrate/frame/examples/view-functions/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 2a4b9b138bf8..654ca0bc4b27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10392,6 +10392,22 @@ dependencies = [ "sp-std 14.0.0", ] +[[package]] +name = "pallet-example-view-functions" +version = "1.0.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 14.0.0", +] + [[package]] name = "pallet-examples" version = "4.0.0-dev" @@ -10405,6 +10421,7 @@ dependencies = [ "pallet-example-single-block-migrations", "pallet-example-split", "pallet-example-tasks", + "pallet-example-view-functions", ] [[package]] diff --git a/substrate/frame/examples/Cargo.toml b/substrate/frame/examples/Cargo.toml index 45c7440eb891..48abdd115d67 100644 --- a/substrate/frame/examples/Cargo.toml +++ b/substrate/frame/examples/Cargo.toml @@ -25,6 +25,7 @@ pallet-example-offchain-worker = { path = "offchain-worker", default-features = pallet-example-split = { path = "split", default-features = false } pallet-example-single-block-migrations = { path = "single-block-migrations", default-features = false } pallet-example-tasks = { path = "tasks", default-features = false } +pallet-example-view-functions = { path = "view-functions", default-features = false } [features] default = ["std"] @@ -38,6 +39,7 @@ std = [ "pallet-example-single-block-migrations/std", "pallet-example-split/std", "pallet-example-tasks/std", + "pallet-example-view-functions/std", ] try-runtime = [ "pallet-default-config-example/try-runtime", @@ -48,4 +50,5 @@ try-runtime = [ "pallet-example-single-block-migrations/try-runtime", "pallet-example-split/try-runtime", "pallet-example-tasks/try-runtime", + "pallet-example-view-functions/try-runtime", ] diff --git a/substrate/frame/examples/src/lib.rs b/substrate/frame/examples/src/lib.rs index dee23a41379f..3752e08ca636 100644 --- a/substrate/frame/examples/src/lib.rs +++ b/substrate/frame/examples/src/lib.rs @@ -48,4 +48,7 @@ //! //! - [`pallet_example_tasks`]: This pallet demonstrates the use of `Tasks` to execute service work. //! +//! - [`pallet_example_view_functions`]: This pallet demonstrates the use of view functions to query +//! pallet state. +//! //! **Tip**: Use `cargo doc --package --open` to view each pallet's documentation. diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml new file mode 100644 index 000000000000..add2159e1f26 --- /dev/null +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -0,0 +1,55 @@ +[package] +name = "pallet-example-view-functions" +version = "1.0.0" +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "Pallet to demonstrate the usage of view functions to query pallet state" + +[lints] +workspace = true + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false } +log = { workspace = true } +scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } + +frame-support = { path = "../../support", default-features = false } +frame-system = { path = "../../system", default-features = false } + +sp-io = { path = "../../../primitives/io", default-features = false } +sp-runtime = { path = "../../../primitives/runtime", default-features = false } +sp-std = { path = "../../../primitives/std", default-features = false } +sp-core = { default-features = false, path = "../../../primitives/core" } + +frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true } + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "log/std", + "scale-info/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", +] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "sp-runtime/try-runtime", +] \ No newline at end of file diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs new file mode 100644 index 000000000000..22f1b3694ee4 --- /dev/null +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -0,0 +1,82 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! This pallet demonstrates the use of the `pallet::task` api for service work. +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod mock; +pub mod tests; + +use frame_support::{dispatch::DispatchResult, Parameter}; +use frame_system::offchain::SendTransactionTypes; +use scale_info::TypeInfo; + +// Re-export pallet items so that they can be accessed from the crate namespace. +pub use pallet::*; + +pub struct SomeType1; +impl From for u64 { + fn from(_t: SomeType1) -> Self { + 0u64 + } +} + +pub trait SomeAssociation1 { + type _1: Parameter + codec::MaxEncodedLen + TypeInfo; +} +impl SomeAssociation1 for u64 { + type _1 = u64; +} + +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + + #[pallet::error] + pub enum Error {} + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::storage] + pub type SomeValue = StorageValue<_, u32>; + + /// Numbers to be added into the total. + #[pallet::storage] + pub type SomeMap = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; + + #[pallet::view_functions] + impl Pallet + where + T::AccountId: From + SomeAssociation1, + { + /// Query value no args. + pub fn get_value() -> Option { + SomeValue::::get() + } + + /// Query value with args. + pub fn get_value_with_arg(key: u32) -> Option { + SomeMap::::get(key) + } + } +} diff --git a/substrate/frame/examples/view-functions/src/mock.rs b/substrate/frame/examples/view-functions/src/mock.rs new file mode 100644 index 000000000000..95f66a40cc3b --- /dev/null +++ b/substrate/frame/examples/view-functions/src/mock.rs @@ -0,0 +1,51 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Mock runtime for `view-functions-example` tests. +#![cfg(test)] + +use crate::{self as view_functions_example}; +use frame_support::{derive_impl, Parameter}; +use scale_info::TypeInfo; +use sp_runtime::testing::TestXt; + +pub type AccountId = u32; +pub type Balance = u32; + +type Block = frame_system::mocking::MockBlock; +frame_support::construct_runtime!( + pub enum Runtime { + System: frame_system, + ViewFunctionsExample: view_functions_example, + } +); + +pub type Extrinsic = TestXt; + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type Block = Block; +} + +impl view_functions_example::Config for Runtime {} + +pub fn new_test_ext() -> sp_io::TestExternalities { + use sp_runtime::BuildStorage; + + let t = RuntimeGenesisConfig { system: Default::default() }.build_storage().unwrap(); + t.into() +} diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs new file mode 100644 index 000000000000..2b31f425401c --- /dev/null +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -0,0 +1,31 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Tests for `pallet-example-tasks`. +#![cfg(test)] + +use crate::mock::*; +use frame_support::traits::Task; +use sp_runtime::BuildStorage; + +#[test] +fn task_enumerate_works() { + new_test_ext().execute_with(|| { + // Numbers::::insert(0, 1); + // assert_eq!(crate::pallet::Task::::iter().collect::>().len(), 1); + }); +} From 86c5f2fcb9c5522de5cb23659fbb3ae81af1aaad Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 24 May 2024 10:16:20 +0100 Subject: [PATCH 010/121] wire up codegen for view functions --- .../frame/examples/view-functions/src/lib.rs | 1 - .../examples/view-functions/src/tests.rs | 21 ++++++++++++++----- .../procedural/src/pallet/expand/mod.rs | 2 ++ .../src/pallet/expand/view_functions.rs | 4 ++-- .../src/pallet/parse/view_functions.rs | 6 +++++- substrate/frame/support/src/traits.rs | 1 + substrate/frame/support/src/traits/query.rs | 1 + 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index 22f1b3694ee4..a097dc857fbf 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -60,7 +60,6 @@ pub mod pallet { #[pallet::storage] pub type SomeValue = StorageValue<_, u32>; - /// Numbers to be added into the total. #[pallet::storage] pub type SomeMap = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 2b31f425401c..c07c1248bd86 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -18,14 +18,25 @@ //! Tests for `pallet-example-tasks`. #![cfg(test)] -use crate::mock::*; -use frame_support::traits::Task; +use crate::{ + mock::*, + pallet::{self, Pallet}, +}; +use frame_support::traits::{DispatchQuery, Query}; use sp_runtime::BuildStorage; #[test] -fn task_enumerate_works() { +fn pallet_get_value_query() { new_test_ext().execute_with(|| { - // Numbers::::insert(0, 1); - // assert_eq!(crate::pallet::Task::::iter().collect::>().len(), 1); + let some_value = Some(99); + pallet::SomeValue::::set(some_value); + assert_eq!(some_value, Pallet::::get_value()); + assert_eq!( + some_value, + as DispatchQuery>::dispatch_query( + & as Query>::ID, + vec![], + ) + ); }); } diff --git a/substrate/frame/support/procedural/src/pallet/expand/mod.rs b/substrate/frame/support/procedural/src/pallet/expand/mod.rs index 54e847c87157..15810933e593 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/mod.rs @@ -66,6 +66,7 @@ pub fn expand(mut def: Def) -> proc_macro2::TokenStream { let error = error::expand_error(&mut def); let event = event::expand_event(&mut def); let storages = storage::expand_storages(&mut def); + let view_functions = view_functions::expand_view_functions(&def); let inherents = inherent::expand_inherents(&mut def); let instances = instances::expand_instances(&mut def); let hooks = hooks::expand_hooks(&mut def); @@ -107,6 +108,7 @@ storage item. Otherwise, all storage items are listed among [*Type Definitions*] #error #event #storages + #view_functions #inherents #instances #hooks diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index b35aea6bfc32..b43511c1fada 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -65,7 +65,7 @@ fn expand_view_function( #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] #[allow(non_camel_case_types)] pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { - - }; + _marker: ::core::marker::PhantomData<(#type_use_gen,)>, + } } } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 735f7b8cbe30..38672849161b 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -15,6 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use inflector::Inflector; use syn::spanned::Spanned; /// Definition of dispatchables typically `impl Pallet { ... }` @@ -99,6 +100,9 @@ impl TryFrom for ViewFunctionDef { impl ViewFunctionDef { pub fn query_struct_ident(&self) -> syn::Ident { - syn::Ident::new(&format!("{}Query", self.name), self.name.span()) + syn::Ident::new( + &format!("{}Query", self.name.to_string().to_pascal_case()), + self.name.span(), + ) } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 578913e70ad9..481a365c402b 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,6 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; +pub use query::{DispatchQuery, Query}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 33261b80d44a..5941be48b918 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -40,6 +40,7 @@ pub struct QueryId { /// implemented for each pallet view function method pub trait Query { + const ID: QueryId; type ReturnType: codec::Codec; fn query(self) -> Self::ReturnType; From f42c5c44840045bd4de30b5c4a54a14c4da0eabd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 24 May 2024 11:53:28 +0100 Subject: [PATCH 011/121] Generate DispatchQuery impl --- .../examples/view-functions/src/tests.rs | 17 +++++++--- .../src/pallet/expand/view_functions.rs | 32 +++++++++++++++++++ substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/query.rs | 4 +-- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index c07c1248bd86..b6ac1085ff77 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -23,7 +23,7 @@ use crate::{ pallet::{self, Pallet}, }; use frame_support::traits::{DispatchQuery, Query}; -use sp_runtime::BuildStorage; +use codec::{Encode, Decode}; #[test] fn pallet_get_value_query() { @@ -31,12 +31,19 @@ fn pallet_get_value_query() { let some_value = Some(99); pallet::SomeValue::::set(some_value); assert_eq!(some_value, Pallet::::get_value()); + + let query = pallet::GetValueQuery::::new(); + + let query_result = as DispatchQuery>::dispatch_query( + & as Query>::ID, + &*query.encode(), + ).unwrap(); + + let query_result = >::decode(&mut &query_result[..]).unwrap(); + assert_eq!( some_value, - as DispatchQuery>::dispatch_query( - & as Query>::ID, - vec![], - ) + query_result, ); }); } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index b43511c1fada..fb7e53d7aaaf 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -30,9 +30,11 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { .view_functions .iter() .map(|view_fn| expand_view_function(def, &view_fns_def, view_fn)); + let impl_dispatch_query = impl_dispatch_query(def, view_fns_def); quote::quote! { #( #view_fn_impls )* + #impl_dispatch_query } } @@ -67,5 +69,35 @@ fn expand_view_function( pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { _marker: ::core::marker::PhantomData<(#type_use_gen,)>, } + + impl<#type_impl_gen> #query_struct_ident<#type_use_gen> #where_clause { + pub fn new() -> Self { + Self { _marker: ::core::default::Default::default() } + } + } + } +} + +fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> TokenStream { + let span = view_fns_impl.attr_span; + let frame_support = &def.frame_support; + let pallet_ident = &def.pallet_struct.pallet; + let type_impl_gen = &def.type_impl_generics(span); + let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); + let type_use_gen = &def.type_use_generics(span); + + quote::quote! { + impl<#type_impl_gen> #frame_support::traits::DispatchQuery + for #pallet_ident<#type_use_gen> + { + fn dispatch_query< + I: #frame_support::__private::codec::Input, + O: #frame_support::__private::codec::Output, + > + (id: & #frame_support::traits::QueryId, input: I) -> Result + { + todo!() + } + } } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 481a365c402b..0fb9c32a3642 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,7 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; -pub use query::{DispatchQuery, Query}; +pub use query::{DispatchQuery, Query, QueryId}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 5941be48b918..fc5a0fa81952 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -17,11 +17,11 @@ //! Traits for querying pallet view functions. -use codec::{Decode, Encode}; +use codec::{Decode, Encode, Input, Output}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { - fn dispatch_query(id: &QueryId, input: Vec) -> Result, codec::Error>; + fn dispatch_query(id: &QueryId, input: I) -> Result; } pub trait QueryIdPrefix { From ac6643e6340dcedd7d66ba9751086b2b374d8e24 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 24 May 2024 18:52:52 +0100 Subject: [PATCH 012/121] Wire up runtime PalletQuery --- .../examples/view-functions/src/tests.rs | 2 +- substrate/frame/support/procedural/src/lib.rs | 3 ++- .../src/pallet/expand/view_functions.rs | 21 +++++++++++++++++++ substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/query.rs | 17 +++++++++++++-- substrate/frame/system/src/lib.rs | 10 +++++++++ 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index b6ac1085ff77..79779b57487f 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -34,7 +34,7 @@ fn pallet_get_value_query() { let query = pallet::GetValueQuery::::new(); - let query_result = as DispatchQuery>::dispatch_query( + let query_result = as DispatchQuery>::dispatch_query::<_, Vec>( & as Query>::ID, &*query.encode(), ).unwrap(); diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 53f01329d181..604579ad6d81 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -788,6 +788,7 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { if item.ident != "RuntimeCall" && item.ident != "RuntimeEvent" && item.ident != "RuntimeTask" && + item.ident != "RuntimeQuery" && item.ident != "RuntimeOrigin" && item.ident != "RuntimeHoldReason" && item.ident != "RuntimeFreezeReason" && @@ -797,7 +798,7 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { return syn::Error::new_spanned( item, "`#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, \ - `RuntimeTask`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo`", + `RuntimeTask`, `RuntimeQuery`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo`", ) .to_compile_error() .into() diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index fb7e53d7aaaf..4cfe058483c3 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -45,13 +45,18 @@ fn expand_view_function( ) -> TokenStream { let span = view_fns_impl.attr_span; let frame_support = &def.frame_support; + let frame_system = &def.frame_system; let type_impl_gen = &def.type_impl_generics(span); let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); let type_use_gen = &def.type_use_generics(span); + let trait_use_gen = &def.trait_use_generics(span); let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; let where_clause = &view_fns_impl.where_clause; let query_struct_ident = view_fn.query_struct_ident(); + // let args = todo!(); + let return_type = &view_fn.return_type; + quote::quote! { #[derive( #frame_support::RuntimeDebugNoBound, @@ -75,6 +80,22 @@ fn expand_view_function( Self { _marker: ::core::default::Default::default() } } } + + impl<#type_impl_gen> #frame_support::traits::QueryIdSuffix for #query_struct_ident<#type_use_gen> #where_clause { + const SUFFIX: [u8; 16] = [0u8; 16]; + } + + impl<#type_impl_gen> #frame_support::traits::Query for #query_struct_ident<#type_use_gen> #where_clause { + const ID: #frame_support::traits::QueryId = #frame_support::traits::QueryId { + prefix: <::RuntimeQuery as #frame_support::traits::QueryIdPrefix>::PREFIX, + suffix: ::SUFFIX, // todo: [AJ] handle instantiatable pallet suffix + }; + type ReturnType = #return_type; + + fn query(self) -> Self::ReturnType { + todo!() + } + } } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 0fb9c32a3642..3b2234c82117 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,7 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; -pub use query::{DispatchQuery, Query, QueryId}; +pub use query::{DispatchQuery, Query, QueryIdPrefix, QueryIdSuffix, QueryId}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index fc5a0fa81952..26205488e95a 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -24,6 +24,19 @@ pub trait DispatchQuery { fn dispatch_query(id: &QueryId, input: I) -> Result; } +impl DispatchQuery for () { + fn dispatch_query( + _id: &QueryId, + _input: I, + ) -> Result { + Err(codec::Error::from("DispatchQuery not implemented")) // todo: return "query not found" error? + } +} + +impl QueryIdPrefix for () { + const PREFIX: [u8; 16] = [0u8; 16]; +} + pub trait QueryIdPrefix { const PREFIX: [u8; 16]; // same as `PalletInfo::name_hash` twox_128 } @@ -34,8 +47,8 @@ pub trait QueryIdSuffix { #[derive(Encode, Decode)] pub struct QueryId { - prefix: [u8; 16], - suffix: [u8; 16], + pub prefix: [u8; 16], + pub suffix: [u8; 16], } /// implemented for each pallet view function method diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 7ed954d83aa8..80281b0350f8 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -299,6 +299,7 @@ pub mod pallet { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + type RuntimeQuery = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = frame_support::traits::ConstU64<10>; type OnSetCode = (); @@ -388,6 +389,10 @@ pub mod pallet { #[inject_runtime_type] type RuntimeTask = (); + /// The query dispatch type, injected by `construct_runtime!`. + #[inject_runtime_type] + type RuntimeQuery = (); + /// Converts a module to the index of the module, injected by `construct_runtime!`. #[inject_runtime_type] type PalletInfo = (); @@ -477,6 +482,11 @@ pub mod pallet { #[pallet::no_default_bounds] type RuntimeTask: Task; + /// Type for dispatching queries. + #[pallet::no_default_bounds] + type RuntimeQuery: frame_support::traits::QueryIdPrefix + + frame_support::traits::DispatchQuery; + /// This stores the number of previous transactions associated with a sender account. type Nonce: Parameter + Member From 2ea615855937b924feca3c8d0038f6a0f49428cf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 28 May 2024 16:32:03 +0100 Subject: [PATCH 013/121] Wire up query args --- .../frame/examples/view-functions/src/lib.rs | 4 +- .../frame/examples/view-functions/src/mock.rs | 3 +- .../examples/view-functions/src/tests.rs | 19 ++++---- .../src/pallet/expand/view_functions.rs | 48 +++++++++++++++---- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/query.rs | 11 +++-- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index a097dc857fbf..d94e60e2d913 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -21,8 +21,7 @@ pub mod mock; pub mod tests; -use frame_support::{dispatch::DispatchResult, Parameter}; -use frame_system::offchain::SendTransactionTypes; +use frame_support::Parameter; use scale_info::TypeInfo; // Re-export pallet items so that they can be accessed from the crate namespace. @@ -46,7 +45,6 @@ impl SomeAssociation1 for u64 { pub mod pallet { use super::*; use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; #[pallet::error] pub enum Error {} diff --git a/substrate/frame/examples/view-functions/src/mock.rs b/substrate/frame/examples/view-functions/src/mock.rs index 95f66a40cc3b..d128c619598a 100644 --- a/substrate/frame/examples/view-functions/src/mock.rs +++ b/substrate/frame/examples/view-functions/src/mock.rs @@ -19,8 +19,7 @@ #![cfg(test)] use crate::{self as view_functions_example}; -use frame_support::{derive_impl, Parameter}; -use scale_info::TypeInfo; +use frame_support::derive_impl; use sp_runtime::testing::TestXt; pub type AccountId = u32; diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 79779b57487f..27dc0987bf22 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -22,8 +22,8 @@ use crate::{ mock::*, pallet::{self, Pallet}, }; +use codec::{Decode, Encode}; use frame_support::traits::{DispatchQuery, Query}; -use codec::{Encode, Decode}; #[test] fn pallet_get_value_query() { @@ -33,17 +33,18 @@ fn pallet_get_value_query() { assert_eq!(some_value, Pallet::::get_value()); let query = pallet::GetValueQuery::::new(); + let input = query.encode(); + let mut output = Vec::new(); - let query_result = as DispatchQuery>::dispatch_query::<_, Vec>( + let _ = as DispatchQuery>::dispatch_query::<_, Vec>( & as Query>::ID, - &*query.encode(), - ).unwrap(); + &mut &input[..], + &mut output, + ) + .unwrap(); - let query_result = >::decode(&mut &query_result[..]).unwrap(); + let query_result = >::decode(&mut &output[..]).unwrap(); - assert_eq!( - some_value, - query_result, - ); + assert_eq!(some_value, query_result,); }); } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 4cfe058483c3..ce1b8f4dd158 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -46,6 +46,7 @@ fn expand_view_function( let span = view_fns_impl.attr_span; let frame_support = &def.frame_support; let frame_system = &def.frame_system; + let pallet_ident = &def.pallet_struct.pallet; let type_impl_gen = &def.type_impl_generics(span); let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); let type_use_gen = &def.type_use_generics(span); @@ -54,7 +55,18 @@ fn expand_view_function( let where_clause = &view_fns_impl.where_clause; let query_struct_ident = view_fn.query_struct_ident(); - // let args = todo!(); + let view_fn_name = &view_fn.name; + let (arg_names, arg_types): (Vec<_>, Vec<_>) = view_fn + .args + .iter() + .map(|arg| match arg { + syn::FnArg::Typed(pat_type) => match &*pat_type.pat { + syn::Pat::Ident(ident) => (ident.ident.clone(), pat_type.ty.clone()), + _ => panic!("Unsupported pattern in view function argument"), + }, + _ => panic!("Unsupported argument in view function"), + }) + .unzip(); let return_type = &view_fn.return_type; quote::quote! { @@ -72,12 +84,16 @@ fn expand_view_function( #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] #[allow(non_camel_case_types)] pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { + #( pub #arg_names: #arg_types, )* _marker: ::core::marker::PhantomData<(#type_use_gen,)>, } impl<#type_impl_gen> #query_struct_ident<#type_use_gen> #where_clause { - pub fn new() -> Self { - Self { _marker: ::core::default::Default::default() } + pub fn new(#( #arg_names: #arg_types, )*) -> Self { + Self { + #( #arg_names, )* + _marker: ::core::default::Default::default() + } } } @@ -93,7 +109,8 @@ fn expand_view_function( type ReturnType = #return_type; fn query(self) -> Self::ReturnType { - todo!() + let Self { #( #arg_names, )* _marker } = self; + #pallet_ident::<#type_use_gen> :: #view_fn_name( #( #arg_names, )* ) } } } @@ -104,20 +121,35 @@ fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> Token let frame_support = &def.frame_support; let pallet_ident = &def.pallet_struct.pallet; let type_impl_gen = &def.type_impl_generics(span); - let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); let type_use_gen = &def.type_use_generics(span); + let where_clause = &view_fns_impl.where_clause; + + let query_match_arms = view_fns_impl.view_functions.iter().map(|view_fn| { + let query_struct_ident = view_fn.query_struct_ident(); + quote::quote! { + <#query_struct_ident<#type_use_gen> as #frame_support::traits::QueryIdSuffix>::SUFFIX => { + let query = <#query_struct_ident<#type_use_gen> as #frame_support::__private::codec::Decode>::decode(input)?; + let result = <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::query(query); + let output = #frame_support::__private::codec::Encode::encode_to(&result, output); + ::core::result::Result::Ok(output) + } + } + }); quote::quote! { impl<#type_impl_gen> #frame_support::traits::DispatchQuery - for #pallet_ident<#type_use_gen> + for #pallet_ident<#type_use_gen> #where_clause { fn dispatch_query< I: #frame_support::__private::codec::Input, O: #frame_support::__private::codec::Output, > - (id: & #frame_support::traits::QueryId, input: I) -> Result + (id: & #frame_support::traits::QueryId, input: &mut I, output: &mut O) -> Result<(), #frame_support::__private::codec::Error> { - todo!() + match id.suffix { + #( #query_match_arms )* + _ => Err(#frame_support::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] + } } } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 3b2234c82117..40a2e954539b 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,7 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; -pub use query::{DispatchQuery, Query, QueryIdPrefix, QueryIdSuffix, QueryId}; +pub use query::{DispatchQuery, Query, QueryId, QueryIdPrefix, QueryIdSuffix}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 26205488e95a..92bf4546efb6 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -21,14 +21,19 @@ use codec::{Decode, Encode, Input, Output}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { - fn dispatch_query(id: &QueryId, input: I) -> Result; + fn dispatch_query( + id: &QueryId, + input: &mut I, + output: &mut O, + ) -> Result<(), codec::Error>; } impl DispatchQuery for () { fn dispatch_query( _id: &QueryId, - _input: I, - ) -> Result { + _input: &mut I, + _output: &mut O, + ) -> Result<(), codec::Error> { Err(codec::Error::from("DispatchQuery not implemented")) // todo: return "query not found" error? } } From 8b56a844b7d02477538acbdcce634c518dd53271 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 28 May 2024 18:32:23 +0100 Subject: [PATCH 014/121] Add get value with args query --- .../examples/view-functions/src/tests.rs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 27dc0987bf22..34882f441129 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -48,3 +48,28 @@ fn pallet_get_value_query() { assert_eq!(some_value, query_result,); }); } + +#[test] +fn pallet_get_value_with_arg_query() { + new_test_ext().execute_with(|| { + let some_key = 1u32; + let some_value = Some(123); + pallet::SomeMap::::set(some_key, some_value); + assert_eq!(some_value, Pallet::::get_value_with_arg(some_key)); + + let query = pallet::GetValueWithArgQuery::::new(some_key); + let input = query.encode(); + let mut output = Vec::new(); + + let _ = as DispatchQuery>::dispatch_query::<_, Vec>( + & as Query>::ID, + &mut &input[..], + &mut output, + ) + .unwrap(); + + let query_result = >::decode(&mut &output[..]).unwrap(); + + assert_eq!(some_value, query_result,); + }); +} From 06f9ffc1e7ef2ea80ca80109f6823e78d8ef5fba Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 29 May 2024 15:36:53 +0100 Subject: [PATCH 015/121] decode_all --- .../examples/view-functions/src/tests.rs | 58 ++++++++++++++++++- .../src/pallet/expand/view_functions.rs | 30 +++++----- substrate/frame/support/src/traits/query.rs | 10 ++-- 3 files changed, 77 insertions(+), 21 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 34882f441129..70e982162ce0 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -36,7 +36,7 @@ fn pallet_get_value_query() { let input = query.encode(); let mut output = Vec::new(); - let _ = as DispatchQuery>::dispatch_query::<_, Vec>( + let _ = as DispatchQuery>::dispatch_query::>( & as Query>::ID, &mut &input[..], &mut output, @@ -61,7 +61,7 @@ fn pallet_get_value_with_arg_query() { let input = query.encode(); let mut output = Vec::new(); - let _ = as DispatchQuery>::dispatch_query::<_, Vec>( + let _ = as DispatchQuery>::dispatch_query::>( & as Query>::ID, &mut &input[..], &mut output, @@ -73,3 +73,57 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, query_result,); }); } + +// pub struct Test(PhantomData); +// +// impl DispatchQuery for Test +// where +// T::AccountId: From + crate::SomeAssociation1, +// { +// #[automatically_derived] +// // #[deny(unreachable_patterns)] +// fn dispatch_query( +// id: &frame_support::traits::QueryId, +// input: &mut &[u8], +// output: &mut O, +// ) -> Result<(), codec::Error> { +// match id.suffix { +// as frame_support::traits::QueryIdSuffix>::SUFFIX => { +// let query = as codec::DecodeAll>::decode_all(input)?; +// let result = as frame_support::traits::Query>::query(query); +// let output = codec::Encode::encode_to( +// &result, +// output, +// ); +// ::core::result::Result::Ok(output) +// } +// as frame_support::traits::QueryIdSuffix>::SUFFIX => { +// let query = as codec::DecodeAll>::decode_all(input)?; +// let result = as frame_support::traits::Query>::query(query); +// let output = codec::Encode::encode_to( +// &result, +// output, +// ); +// ::core::result::Result::Ok(output) +// } +// _ => { +// Err( +// codec::Error::from( +// "DispatchQuery not implemented", +// ), +// ) +// } +// } +// } +// +// } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index ce1b8f4dd158..8ad7113e5bab 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -82,7 +82,6 @@ fn expand_view_function( #[codec(encode_bound())] #[codec(decode_bound())] #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] - #[allow(non_camel_case_types)] pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { #( pub #arg_names: #arg_types, )* _marker: ::core::marker::PhantomData<(#type_use_gen,)>, @@ -128,7 +127,7 @@ fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> Token let query_struct_ident = view_fn.query_struct_ident(); quote::quote! { <#query_struct_ident<#type_use_gen> as #frame_support::traits::QueryIdSuffix>::SUFFIX => { - let query = <#query_struct_ident<#type_use_gen> as #frame_support::__private::codec::Decode>::decode(input)?; + let query = <#query_struct_ident<#type_use_gen> as #frame_support::__private::codec::DecodeAll>::decode_all(input)?; let result = <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::query(query); let output = #frame_support::__private::codec::Encode::encode_to(&result, output); ::core::result::Result::Ok(output) @@ -137,20 +136,23 @@ fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> Token }); quote::quote! { - impl<#type_impl_gen> #frame_support::traits::DispatchQuery - for #pallet_ident<#type_use_gen> #where_clause - { - fn dispatch_query< - I: #frame_support::__private::codec::Input, - O: #frame_support::__private::codec::Output, - > - (id: & #frame_support::traits::QueryId, input: &mut I, output: &mut O) -> Result<(), #frame_support::__private::codec::Error> + const _: () = { + impl<#type_impl_gen> #frame_support::traits::DispatchQuery + for #pallet_ident<#type_use_gen> #where_clause { - match id.suffix { - #( #query_match_arms )* - _ => Err(#frame_support::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] + #[deny(unreachable_patterns)] + fn dispatch_query< + O: #frame_support::__private::codec::Output, + > + (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::codec::Error> + { + let x = 1; + match id.suffix { + #( #query_match_arms )* + _ => Err(#frame_support::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] + } } } - } + }; } } diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 92bf4546efb6..91ab37d5d4c6 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -17,21 +17,21 @@ //! Traits for querying pallet view functions. -use codec::{Decode, Encode, Input, Output}; +use codec::{Decode, Encode, Output}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { - fn dispatch_query( + fn dispatch_query( id: &QueryId, - input: &mut I, + input: &mut &[u8], output: &mut O, ) -> Result<(), codec::Error>; } impl DispatchQuery for () { - fn dispatch_query( + fn dispatch_query( _id: &QueryId, - _input: &mut I, + _input: &mut &[u8], _output: &mut O, ) -> Result<(), codec::Error> { Err(codec::Error::from("DispatchQuery not implemented")) // todo: return "query not found" error? From cd52010b6fc1327e19b8b7ac1a3e61d2504c629d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 30 May 2024 11:01:38 +0100 Subject: [PATCH 016/121] Docs --- .../procedural/src/pallet/expand/view_functions.rs | 8 +++++++- .../support/procedural/src/pallet/parse/view_functions.rs | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 8ad7113e5bab..83a91124f916 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -68,8 +68,11 @@ fn expand_view_function( }) .unzip(); let return_type = &view_fn.return_type; + let docs = &view_fn.docs; quote::quote! { + #( #[doc = #docs] )* + #[allow(missing_docs)] #[derive( #frame_support::RuntimeDebugNoBound, #frame_support::CloneNoBound, @@ -83,11 +86,14 @@ fn expand_view_function( #[codec(decode_bound())] #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { - #( pub #arg_names: #arg_types, )* + #( + pub #arg_names: #arg_types, + )* _marker: ::core::marker::PhantomData<(#type_use_gen,)>, } impl<#type_impl_gen> #query_struct_ident<#type_use_gen> #where_clause { + /// Create a new [`#query_struct_ident`] instance. pub fn new(#( #arg_names: #arg_types, )*) -> Self { Self { #( #arg_names, )* diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 38672849161b..ee57e1fa3337 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -17,6 +17,7 @@ use inflector::Inflector; use syn::spanned::Spanned; +use frame_support_procedural_tools::get_doc_literals; /// Definition of dispatchables typically `impl Pallet { ... }` pub struct ViewFunctionsImplDef { @@ -76,6 +77,7 @@ impl ViewFunctionsImplDef { pub struct ViewFunctionDef { pub name: syn::Ident, + pub docs: Vec, pub args: Vec, pub return_type: syn::Type, } @@ -92,6 +94,7 @@ impl TryFrom for ViewFunctionDef { Ok(Self { name: method.sig.ident.clone(), + docs: get_doc_literals(&method.attrs), args: method.sig.inputs.iter().cloned().collect::>(), return_type: *type_.clone(), }) From 44707b362b9298dc6fd9ae07d6ab7c3b6f546de2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 30 May 2024 17:07:36 +0100 Subject: [PATCH 017/121] Generate suffix from view_fn signature --- .../src/pallet/expand/view_functions.rs | 20 ++++------- .../src/pallet/parse/view_functions.rs | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 83a91124f916..1fcbbd476a79 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -56,20 +56,12 @@ fn expand_view_function( let query_struct_ident = view_fn.query_struct_ident(); let view_fn_name = &view_fn.name; - let (arg_names, arg_types): (Vec<_>, Vec<_>) = view_fn - .args - .iter() - .map(|arg| match arg { - syn::FnArg::Typed(pat_type) => match &*pat_type.pat { - syn::Pat::Ident(ident) => (ident.ident.clone(), pat_type.ty.clone()), - _ => panic!("Unsupported pattern in view function argument"), - }, - _ => panic!("Unsupported argument in view function"), - }) - .unzip(); + let (arg_names, arg_types) = view_fn.args_names_types(); let return_type = &view_fn.return_type; let docs = &view_fn.docs; + let query_id_suffix_bytes = view_fn.query_id_suffix_bytes_lits(); + quote::quote! { #( #[doc = #docs] )* #[allow(missing_docs)] @@ -103,7 +95,7 @@ fn expand_view_function( } impl<#type_impl_gen> #frame_support::traits::QueryIdSuffix for #query_struct_ident<#type_use_gen> #where_clause { - const SUFFIX: [u8; 16] = [0u8; 16]; + const SUFFIX: [::core::primitive::u8; 16usize] = [ #( #query_id_suffix_bytes ),* ]; } impl<#type_impl_gen> #frame_support::traits::Query for #query_struct_ident<#type_use_gen> #where_clause { @@ -146,13 +138,13 @@ fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> Token impl<#type_impl_gen> #frame_support::traits::DispatchQuery for #pallet_ident<#type_use_gen> #where_clause { - #[deny(unreachable_patterns)] + #[deny(unreachable_patterns)] // todo: [AJ] should error if identical suffixes, does not atm fn dispatch_query< O: #frame_support::__private::codec::Output, > (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::codec::Error> { - let x = 1; + // let y = 1; // todo: [AJ] why is unused variable error not triggered here - unused functions? match id.suffix { #( #query_match_arms )* _ => Err(#frame_support::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index ee57e1fa3337..6e39b924a32a 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -15,9 +15,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +use frame_support_procedural_tools::get_doc_literals; use inflector::Inflector; use syn::spanned::Spanned; -use frame_support_procedural_tools::get_doc_literals; /// Definition of dispatchables typically `impl Pallet { ... }` pub struct ViewFunctionsImplDef { @@ -108,4 +108,36 @@ impl ViewFunctionDef { self.name.span(), ) } + + pub fn query_id_suffix_bytes_lits(&self) -> [syn::LitInt; 16] { + let mut output = [0u8; 16]; + let arg_types = self + .args_names_types() + .1 + .iter() + .map(|ty| quote::quote!(#ty).to_string()) + .collect::>() + .join(","); + let return_type = &self.return_type; + let view_fn_signature = + format!("{}({}) -> {}", self.name, arg_types, quote::quote!(#return_type),); + let hash = sp_crypto_hashing::twox_128(view_fn_signature.as_bytes()); + output.copy_from_slice(&hash[..]); + output.map(|byte| { + syn::LitInt::new(&format!("0x{:X}_u8", byte), proc_macro2::Span::call_site()) + }) + } + + pub fn args_names_types(&self) -> (Vec, Vec) { + self.args + .iter() + .map(|arg| match arg { + syn::FnArg::Typed(pat_type) => match &*pat_type.pat { + syn::Pat::Ident(ident) => (ident.ident.clone(), *pat_type.ty.clone()), + _ => panic!("Unsupported pattern in view function argument"), + }, + _ => panic!("Unsupported argument in view function"), + }) + .unzip() + } } From afa14900d66a3f9591e30145434b89662ee3bcb9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 30 May 2024 17:38:18 +0100 Subject: [PATCH 018/121] separate suffix id --- .../src/pallet/expand/view_functions.rs | 4 +++- .../src/pallet/parse/view_functions.rs | 17 +++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 1fcbbd476a79..b9f712227991 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -60,7 +60,9 @@ fn expand_view_function( let return_type = &view_fn.return_type; let docs = &view_fn.docs; - let query_id_suffix_bytes = view_fn.query_id_suffix_bytes_lits(); + let query_id_suffix_bytes = view_fn + .query_id_suffix_bytes() + .map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), proc_macro2::Span::call_site())); quote::quote! { #( #[doc = #docs] )* diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 6e39b924a32a..7b97952b383c 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -109,8 +109,10 @@ impl ViewFunctionDef { ) } - pub fn query_id_suffix_bytes_lits(&self) -> [syn::LitInt; 16] { + pub fn query_id_suffix_bytes(&self) -> [u8; 16] { let mut output = [0u8; 16]; + + // concatenate the signature string let arg_types = self .args_names_types() .1 @@ -119,13 +121,16 @@ impl ViewFunctionDef { .collect::>() .join(","); let return_type = &self.return_type; - let view_fn_signature = - format!("{}({}) -> {}", self.name, arg_types, quote::quote!(#return_type),); + let view_fn_signature = format!( + "{query_name}({arg_types}) -> {return_type}", + query_name = &self.name, + return_type = quote::quote!(#return_type), + ); + + // hash the signature string let hash = sp_crypto_hashing::twox_128(view_fn_signature.as_bytes()); output.copy_from_slice(&hash[..]); - output.map(|byte| { - syn::LitInt::new(&format!("0x{:X}_u8", byte), proc_macro2::Span::call_site()) - }) + output } pub fn args_names_types(&self) -> (Vec, Vec) { From f97a40c9d70081aaa64d5e9500d3876cad069313 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 30 May 2024 20:06:09 +0100 Subject: [PATCH 019/121] WIP expand runtime level query --- .../examples/view-functions/src/tests.rs | 2 +- .../src/construct_runtime/expand/mod.rs | 2 + .../src/construct_runtime/expand/query.rs | 68 +++++++++++++++++++ .../procedural/src/construct_runtime/mod.rs | 3 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 substrate/frame/support/procedural/src/construct_runtime/expand/query.rs diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 70e982162ce0..1561a5627f8f 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -36,7 +36,7 @@ fn pallet_get_value_query() { let input = query.encode(); let mut output = Vec::new(); - let _ = as DispatchQuery>::dispatch_query::>( + let _ = ::RuntimeQuery::dispatch_query::>( & as Query>::ID, &mut &input[..], &mut output, diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs index 88f9a3c6e33f..dfe54955dfc2 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs @@ -28,6 +28,7 @@ mod outer_enums; mod slash_reason; mod task; mod unsigned; +mod query; pub use call::expand_outer_dispatch; pub use config::expand_outer_config; @@ -41,3 +42,4 @@ pub use outer_enums::{expand_outer_enum, OuterEnumType}; pub use slash_reason::expand_outer_slash_reason; pub use task::expand_outer_task; pub use unsigned::expand_outer_validate_unsigned; +pub use query::expand_outer_query; diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs new file mode 100644 index 000000000000..c649e6369683 --- /dev/null +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -0,0 +1,68 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +use crate::construct_runtime::Pallet; +use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; + +/// Expands implementation of runtime level `DispatchQuery`. +pub fn expand_outer_query( + runtime_name: &Ident, + pallet_decls: &[Pallet], + scrate: &TokenStream2, +) -> TokenStream2 { + let runtime_query = syn::Ident::new("RuntimeQuery", Span::call_site()); + + let query_match_arms = pallet_decls.iter().map(|pallet| { + let pallet_name = &pallet.name; + quote::quote! { + < #pallet_name< #runtime_name > as #scrate::traits::QueryIdPrefix>::PREFIX => { + < #pallet_name< #runtime_name > as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) + } + } + }); + + quote::quote! { + /// Runtime query type. + #[derive( + Clone, PartialEq, Eq, + #scrate::__private::codec::Encode, + #scrate::__private::codec::Decode, + #scrate::__private::scale_info::TypeInfo, + #scrate::__private::RuntimeDebug, + )] + pub enum #runtime_query {} + + const _: () = { + impl #scrate::traits::DispatchQuery for #runtime_query + { + #[deny(unreachable_patterns)] // todo: [AJ] should error if identical prefixes + fn dispatch_query( + id: & #scrate::traits::QueryId, + input: &mut &[u8], + output: &mut O + ) -> Result<(), #scrate::__private::codec::Error> + { + // let y = 1; // todo: [AJ] why is unused variable error not triggered here - unused functions? + match id.suffix { + #( #query_match_arms )* + _ => Err(#scrate::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] + } + } + } + }; + } +} diff --git a/substrate/frame/support/procedural/src/construct_runtime/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/mod.rs index 1505d158895f..4098234ae3a7 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/mod.rs @@ -400,6 +400,7 @@ fn construct_runtime_final_expansion( let dispatch = expand::expand_outer_dispatch(&name, system_pallet, &pallets, &scrate); let tasks = expand::expand_outer_task(&name, &pallets, &scrate); + let query = expand::expand_outer_query(&name, &pallets, &scrate); let metadata = expand::expand_runtime_metadata( &name, &pallets, @@ -491,6 +492,8 @@ fn construct_runtime_final_expansion( #tasks + #query + #metadata #outer_config From ddff4d02c232092814490b677fa1b76585a13356 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 31 May 2024 15:56:01 +0100 Subject: [PATCH 020/121] Implement `DispatchQuery` for pallets even without queries --- .../src/construct_runtime/expand/mod.rs | 4 +- .../src/construct_runtime/expand/query.rs | 4 +- .../procedural/src/construct_runtime/parse.rs | 6 +++ .../src/pallet/expand/view_functions.rs | 40 ++++++++++--------- .../src/pallet/parse/view_functions.rs | 1 + 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs index dfe54955dfc2..8bae23f08351 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs @@ -25,10 +25,10 @@ mod lock_id; mod metadata; mod origin; mod outer_enums; +mod query; mod slash_reason; mod task; mod unsigned; -mod query; pub use call::expand_outer_dispatch; pub use config::expand_outer_config; @@ -39,7 +39,7 @@ pub use lock_id::expand_outer_lock_id; pub use metadata::expand_runtime_metadata; pub use origin::expand_outer_origin; pub use outer_enums::{expand_outer_enum, OuterEnumType}; +pub use query::expand_outer_query; pub use slash_reason::expand_outer_slash_reason; pub use task::expand_outer_task; pub use unsigned::expand_outer_validate_unsigned; -pub use query::expand_outer_query; diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index c649e6369683..70a0998285b9 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -29,8 +29,8 @@ pub fn expand_outer_query( let query_match_arms = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; quote::quote! { - < #pallet_name< #runtime_name > as #scrate::traits::QueryIdPrefix>::PREFIX => { - < #pallet_name< #runtime_name > as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) + < #pallet_name as #scrate::traits::QueryIdPrefix>::PREFIX => { + < #pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) } } }); diff --git a/substrate/frame/support/procedural/src/construct_runtime/parse.rs b/substrate/frame/support/procedural/src/construct_runtime/parse.rs index ded77bed4c8e..3b4e43231b88 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/parse.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/parse.rs @@ -45,6 +45,7 @@ mod keyword { syn::custom_keyword!(Task); syn::custom_keyword!(LockId); syn::custom_keyword!(SlashReason); + syn::custom_keyword!(Query); syn::custom_keyword!(exclude_parts); syn::custom_keyword!(use_parts); syn::custom_keyword!(expanded); @@ -408,6 +409,7 @@ pub enum PalletPartKeyword { Task(keyword::Task), LockId(keyword::LockId), SlashReason(keyword::SlashReason), + Query(keyword::Query), } impl Parse for PalletPartKeyword { @@ -442,6 +444,8 @@ impl Parse for PalletPartKeyword { Ok(Self::LockId(input.parse()?)) } else if lookahead.peek(keyword::SlashReason) { Ok(Self::SlashReason(input.parse()?)) + } else if lookahead.peek(keyword::Query) { + Ok(Self::Query(input.parse()?)) } else { Err(lookahead.error()) } @@ -466,6 +470,7 @@ impl PalletPartKeyword { Self::Task(_) => "Task", Self::LockId(_) => "LockId", Self::SlashReason(_) => "SlashReason", + Self::Query(_) => "Query", } } @@ -497,6 +502,7 @@ impl ToTokens for PalletPartKeyword { Self::Task(inner) => inner.to_tokens(tokens), Self::LockId(inner) => inner.to_tokens(tokens), Self::SlashReason(inner) => inner.to_tokens(tokens), + Self::Query(inner) => inner.to_tokens(tokens), } } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index b9f712227991..fff1d78d28ce 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -15,22 +15,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::pallet::{ - parse::view_functions::{ViewFunctionDef, ViewFunctionsImplDef}, - Def, -}; -use proc_macro2::TokenStream; +use crate::pallet::{parse::view_functions::ViewFunctionDef, Def}; +use proc_macro2::{Span, TokenStream}; +use syn::spanned::Spanned; pub fn expand_view_functions(def: &Def) -> TokenStream { - let Some(view_fns_def) = def.view_functions.as_ref() else { - return TokenStream::new(); + let (span, where_clause, view_fns) = match def.view_functions.as_ref() { + Some(view_fns) => ( + view_fns.attr_span.clone(), + view_fns.where_clause.clone(), + view_fns.view_functions.clone(), + ), + None => (def.item.span(), def.config.where_clause.clone(), Vec::new()), }; - let view_fn_impls = view_fns_def - .view_functions + let view_fn_impls = view_fns .iter() - .map(|view_fn| expand_view_function(def, &view_fns_def, view_fn)); - let impl_dispatch_query = impl_dispatch_query(def, view_fns_def); + .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); + let impl_dispatch_query = impl_dispatch_query(def, span, where_clause.as_ref(), &view_fns); quote::quote! { #( #view_fn_impls )* @@ -40,10 +42,10 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { fn expand_view_function( def: &Def, - view_fns_impl: &ViewFunctionsImplDef, + span: Span, + where_clause: Option<&syn::WhereClause>, view_fn: &ViewFunctionDef, ) -> TokenStream { - let span = view_fns_impl.attr_span; let frame_support = &def.frame_support; let frame_system = &def.frame_system; let pallet_ident = &def.pallet_struct.pallet; @@ -52,7 +54,6 @@ fn expand_view_function( let type_use_gen = &def.type_use_generics(span); let trait_use_gen = &def.trait_use_generics(span); let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; - let where_clause = &view_fns_impl.where_clause; let query_struct_ident = view_fn.query_struct_ident(); let view_fn_name = &view_fn.name; @@ -115,15 +116,18 @@ fn expand_view_function( } } -fn impl_dispatch_query(def: &Def, view_fns_impl: &ViewFunctionsImplDef) -> TokenStream { - let span = view_fns_impl.attr_span; +fn impl_dispatch_query( + def: &Def, + span: Span, + where_clause: Option<&syn::WhereClause>, + view_fns: &[ViewFunctionDef], +) -> TokenStream { let frame_support = &def.frame_support; let pallet_ident = &def.pallet_struct.pallet; let type_impl_gen = &def.type_impl_generics(span); let type_use_gen = &def.type_use_generics(span); - let where_clause = &view_fns_impl.where_clause; - let query_match_arms = view_fns_impl.view_functions.iter().map(|view_fn| { + let query_match_arms = view_fns.iter().map(|view_fn| { let query_struct_ident = view_fn.query_struct_ident(); quote::quote! { <#query_struct_ident<#type_use_gen> as #frame_support::traits::QueryIdSuffix>::SUFFIX => { diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 7b97952b383c..fa8657c57bac 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -75,6 +75,7 @@ impl ViewFunctionsImplDef { } } +#[derive(Clone)] pub struct ViewFunctionDef { pub name: syn::Ident, pub docs: Vec, From 538ea8a8571be483f86d596eabb92fb3bac876ab Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 31 May 2024 17:44:49 +0100 Subject: [PATCH 021/121] WIP --- .../src/construct_runtime/expand/query.rs | 31 ++++++++++++++++--- .../src/pallet/expand/view_functions.rs | 2 +- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index 70a0998285b9..187cc4e045bb 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -20,16 +20,38 @@ use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; /// Expands implementation of runtime level `DispatchQuery`. pub fn expand_outer_query( - runtime_name: &Ident, + _runtime_name: &Ident, pallet_decls: &[Pallet], scrate: &TokenStream2, ) -> TokenStream2 { let runtime_query = syn::Ident::new("RuntimeQuery", Span::call_site()); + let query_id_prefix_impls = pallet_decls.iter().map(|pallet| { + let pallet_path = &pallet.path; + let pallet_name = &pallet.name; + + let mut output = [0u8; 16]; + let pallet_signature = format!( + "{path}::{pallet}", + path = quote::quote!(#pallet_path).to_string(), + pallet = quote::quote!(#pallet_name).to_string() + ); + let hash = sp_crypto_hashing::blake2_128(pallet_signature.as_bytes()); + output.copy_from_slice(&hash); + let query_id_prefix_bytes = + output.map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), Span::call_site())); + + quote::quote! { + impl #scrate::traits::QueryIdPrefix for #scrate::traits::PalletQueryId<#pallet_name> { + const PREFIX: [::core::primitive::u8; 16usize] = [ #( #query_id_prefix_bytes ),* ]; + } + } + }); + let query_match_arms = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; quote::quote! { - < #pallet_name as #scrate::traits::QueryIdPrefix>::PREFIX => { + < #scrate::traits::PalletQueryId<#pallet_name> as #scrate::traits::QueryIdPrefix>::PREFIX => { < #pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) } } @@ -47,8 +69,9 @@ pub fn expand_outer_query( pub enum #runtime_query {} const _: () = { - impl #scrate::traits::DispatchQuery for #runtime_query - { + #( #query_id_prefix_impls )* + + impl #scrate::traits::DispatchQuery for #runtime_query { #[deny(unreachable_patterns)] // todo: [AJ] should error if identical prefixes fn dispatch_query( id: & #scrate::traits::QueryId, diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index fff1d78d28ce..c335221e72a8 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -63,7 +63,7 @@ fn expand_view_function( let query_id_suffix_bytes = view_fn .query_id_suffix_bytes() - .map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), proc_macro2::Span::call_site())); + .map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), Span::call_site())); quote::quote! { #( #[doc = #docs] )* From db616f56f8538d83d5d3ed5947d447bb000dde89 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 14:55:57 +0100 Subject: [PATCH 022/121] Use pallet name hash as query id prefix --- .../examples/view-functions/src/tests.rs | 5 ++- .../src/construct_runtime/expand/query.rs | 43 +++++-------------- .../src/pallet/expand/view_functions.rs | 40 ++++++++++++++--- substrate/frame/support/src/traits/query.rs | 13 +++--- substrate/frame/system/src/lib.rs | 3 +- 5 files changed, 55 insertions(+), 49 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 1561a5627f8f..0c31105f07a2 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -36,8 +36,9 @@ fn pallet_get_value_query() { let input = query.encode(); let mut output = Vec::new(); + let _ = ::RuntimeQuery::dispatch_query::>( - & as Query>::ID, + & as Query>::id(), &mut &input[..], &mut output, ) @@ -62,7 +63,7 @@ fn pallet_get_value_with_arg_query() { let mut output = Vec::new(); let _ = as DispatchQuery>::dispatch_query::>( - & as Query>::ID, + & as Query>::id(), &mut &input[..], &mut output, ) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index 187cc4e045bb..e488e73f6fb8 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -26,33 +26,16 @@ pub fn expand_outer_query( ) -> TokenStream2 { let runtime_query = syn::Ident::new("RuntimeQuery", Span::call_site()); - let query_id_prefix_impls = pallet_decls.iter().map(|pallet| { - let pallet_path = &pallet.path; + let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; - - let mut output = [0u8; 16]; - let pallet_signature = format!( - "{path}::{pallet}", - path = quote::quote!(#pallet_path).to_string(), - pallet = quote::quote!(#pallet_name).to_string() - ); - let hash = sp_crypto_hashing::blake2_128(pallet_signature.as_bytes()); - output.copy_from_slice(&hash); - let query_id_prefix_bytes = - output.map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), Span::call_site())); - + // let instance = pallet.instance.as_ref().into_iter(); + // let path = &pallet.path; + // let pallet_concrete = quote::quote! { + // #pallet_name::<#runtime_name #(, #path::#instance)*> + // }; quote::quote! { - impl #scrate::traits::QueryIdPrefix for #scrate::traits::PalletQueryId<#pallet_name> { - const PREFIX: [::core::primitive::u8; 16usize] = [ #( #query_id_prefix_bytes ),* ]; - } - } - }); - - let query_match_arms = pallet_decls.iter().map(|pallet| { - let pallet_name = &pallet.name; - quote::quote! { - < #scrate::traits::PalletQueryId<#pallet_name> as #scrate::traits::QueryIdPrefix>::PREFIX => { - < #pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) + if id.prefix == <#pallet_name as #scrate::traits::QueryIdPrefix>::prefix() { + return <#pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) } } }); @@ -69,21 +52,15 @@ pub fn expand_outer_query( pub enum #runtime_query {} const _: () = { - #( #query_id_prefix_impls )* - impl #scrate::traits::DispatchQuery for #runtime_query { - #[deny(unreachable_patterns)] // todo: [AJ] should error if identical prefixes fn dispatch_query( id: & #scrate::traits::QueryId, input: &mut &[u8], output: &mut O ) -> Result<(), #scrate::__private::codec::Error> { - // let y = 1; // todo: [AJ] why is unused variable error not triggered here - unused functions? - match id.suffix { - #( #query_match_arms )* - _ => Err(#scrate::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] - } + #( #prefix_conditionals )* + Err(#scrate::__private::codec::Error::from("todo: no prefix")) } } }; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index c335221e72a8..2623b0e516d9 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -29,17 +29,44 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { None => (def.item.span(), def.config.where_clause.clone(), Vec::new()), }; + let query_prefix_impl = expand_query_prefix_impl(def, span, where_clause.as_ref()); + let view_fn_impls = view_fns .iter() .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); let impl_dispatch_query = impl_dispatch_query(def, span, where_clause.as_ref(), &view_fns); quote::quote! { + #query_prefix_impl #( #view_fn_impls )* #impl_dispatch_query } } +fn expand_query_prefix_impl( + def: &Def, + span: Span, + where_clause: Option<&syn::WhereClause>, +) -> TokenStream { + let pallet_ident = &def.pallet_struct.pallet; + let frame_support = &def.frame_support; + let frame_system = &def.frame_system; + let type_impl_gen = &def.type_impl_generics(span); + let type_use_gen = &def.type_use_generics(span); + + quote::quote! { + impl<#type_impl_gen> #frame_support::traits::QueryIdPrefix for #pallet_ident<#type_use_gen> #where_clause { + fn prefix() -> [::core::primitive::u8; 16usize] { + < + ::PalletInfo + as #frame_support::traits::PalletInfo + >::name_hash::>() + .expect("No name_hash found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.") + } + } + } +} + fn expand_view_function( def: &Def, span: Span, @@ -47,12 +74,10 @@ fn expand_view_function( view_fn: &ViewFunctionDef, ) -> TokenStream { let frame_support = &def.frame_support; - let frame_system = &def.frame_system; let pallet_ident = &def.pallet_struct.pallet; let type_impl_gen = &def.type_impl_generics(span); let type_decl_bounded_gen = &def.type_decl_bounded_generics(span); let type_use_gen = &def.type_use_generics(span); - let trait_use_gen = &def.trait_use_generics(span); let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; let query_struct_ident = view_fn.query_struct_ident(); @@ -102,10 +127,13 @@ fn expand_view_function( } impl<#type_impl_gen> #frame_support::traits::Query for #query_struct_ident<#type_use_gen> #where_clause { - const ID: #frame_support::traits::QueryId = #frame_support::traits::QueryId { - prefix: <::RuntimeQuery as #frame_support::traits::QueryIdPrefix>::PREFIX, - suffix: ::SUFFIX, // todo: [AJ] handle instantiatable pallet suffix - }; + fn id() -> #frame_support::traits::QueryId { + #frame_support::traits::QueryId { + prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::QueryIdPrefix>::prefix(), + suffix: ::SUFFIX, + } + } + type ReturnType = #return_type; fn query(self) -> Self::ReturnType { diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 91ab37d5d4c6..665fc64d989f 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -18,6 +18,7 @@ //! Traits for querying pallet view functions. use codec::{Decode, Encode, Output}; +use sp_runtime::RuntimeDebug; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { @@ -38,19 +39,19 @@ impl DispatchQuery for () { } } -impl QueryIdPrefix for () { - const PREFIX: [u8; 16] = [0u8; 16]; -} +// impl QueryIdPrefix for () { +// const PREFIX: [u8; 16] = [0u8; 16]; +// } pub trait QueryIdPrefix { - const PREFIX: [u8; 16]; // same as `PalletInfo::name_hash` twox_128 + fn prefix() -> [u8; 16]; } pub trait QueryIdSuffix { const SUFFIX: [u8; 16]; } -#[derive(Encode, Decode)] +#[derive(Encode, Decode, RuntimeDebug)] pub struct QueryId { pub prefix: [u8; 16], pub suffix: [u8; 16], @@ -58,7 +59,7 @@ pub struct QueryId { /// implemented for each pallet view function method pub trait Query { - const ID: QueryId; + fn id() -> QueryId; type ReturnType: codec::Codec; fn query(self) -> Self::ReturnType; diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index d0f9bacac82a..165a5e8c7c74 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -496,8 +496,7 @@ pub mod pallet { /// Type for dispatching queries. #[pallet::no_default_bounds] - type RuntimeQuery: frame_support::traits::QueryIdPrefix - + frame_support::traits::DispatchQuery; + type RuntimeQuery: frame_support::traits::DispatchQuery; /// This stores the number of previous transactions associated with a sender account. type Nonce: Parameter From fd3318b8335ff6b4697ba08cbd72915de820f6f0 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 14:56:15 +0100 Subject: [PATCH 023/121] Commented out code --- .../examples/view-functions/src/tests.rs | 56 +------------------ 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 0c31105f07a2..3a74f10c78dc 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -73,58 +73,4 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, query_result,); }); -} - -// pub struct Test(PhantomData); -// -// impl DispatchQuery for Test -// where -// T::AccountId: From + crate::SomeAssociation1, -// { -// #[automatically_derived] -// // #[deny(unreachable_patterns)] -// fn dispatch_query( -// id: &frame_support::traits::QueryId, -// input: &mut &[u8], -// output: &mut O, -// ) -> Result<(), codec::Error> { -// match id.suffix { -// as frame_support::traits::QueryIdSuffix>::SUFFIX => { -// let query = as codec::DecodeAll>::decode_all(input)?; -// let result = as frame_support::traits::Query>::query(query); -// let output = codec::Encode::encode_to( -// &result, -// output, -// ); -// ::core::result::Result::Ok(output) -// } -// as frame_support::traits::QueryIdSuffix>::SUFFIX => { -// let query = as codec::DecodeAll>::decode_all(input)?; -// let result = as frame_support::traits::Query>::query(query); -// let output = codec::Encode::encode_to( -// &result, -// output, -// ); -// ::core::result::Result::Ok(output) -// } -// _ => { -// Err( -// codec::Error::from( -// "DispatchQuery not implemented", -// ), -// ) -// } -// } -// } -// -// } +} \ No newline at end of file From a49e5f3e79eab3399321950cbbafd6152f9ea632 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 15:25:58 +0100 Subject: [PATCH 024/121] Inject runtime type --- substrate/frame/examples/view-functions/src/tests.rs | 10 +++++++++- .../procedural/src/construct_runtime/expand/query.rs | 6 +----- substrate/frame/system/src/lib.rs | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 3a74f10c78dc..a9b9f8acad4a 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -36,9 +36,17 @@ fn pallet_get_value_query() { let input = query.encode(); let mut output = Vec::new(); + let id = as Query>::id(); + println!("{id:?}"); + + // let _ = as DispatchQuery>::dispatch_query::>( + // &id, + // &mut &input[..], + // &mut output, + // ).unwrap(); let _ = ::RuntimeQuery::dispatch_query::>( - & as Query>::id(), + &id, &mut &input[..], &mut output, ) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index e488e73f6fb8..72b42d6362a3 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -28,11 +28,6 @@ pub fn expand_outer_query( let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; - // let instance = pallet.instance.as_ref().into_iter(); - // let path = &pallet.path; - // let pallet_concrete = quote::quote! { - // #pallet_name::<#runtime_name #(, #path::#instance)*> - // }; quote::quote! { if id.prefix == <#pallet_name as #scrate::traits::QueryIdPrefix>::prefix() { return <#pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) @@ -59,6 +54,7 @@ pub fn expand_outer_query( output: &mut O ) -> Result<(), #scrate::__private::codec::Error> { + println!("dispatch_query {id:?}"); #( #prefix_conditionals )* Err(#scrate::__private::codec::Error::from("todo: no prefix")) } diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 165a5e8c7c74..76b5fddae93b 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -311,6 +311,7 @@ pub mod pallet { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + #[inject_runtime_type] type RuntimeQuery = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = TestBlockHashCount>; From dd651acb009ccc7b212767ea3b3062028a0609f6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 15:28:59 +0100 Subject: [PATCH 025/121] tidy --- substrate/frame/support/src/traits/query.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 665fc64d989f..a67942eabb61 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -39,10 +39,6 @@ impl DispatchQuery for () { } } -// impl QueryIdPrefix for () { -// const PREFIX: [u8; 16] = [0u8; 16]; -// } - pub trait QueryIdPrefix { fn prefix() -> [u8; 16]; } From c7ff6c997db1d860fdf2ffbae3476b1f4360ec1b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 20:42:58 +0100 Subject: [PATCH 026/121] Add pallet with instances --- .../frame/examples/view-functions/src/lib.rs | 41 +++++++++++++++++-- .../frame/examples/view-functions/src/mock.rs | 11 +++-- .../examples/view-functions/src/tests.rs | 9 +--- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index d94e60e2d913..924dcca98df8 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -24,9 +24,6 @@ pub mod tests; use frame_support::Parameter; use scale_info::TypeInfo; -// Re-export pallet items so that they can be accessed from the crate namespace. -pub use pallet::*; - pub struct SomeType1; impl From for u64 { fn from(_t: SomeType1) -> Self { @@ -77,3 +74,41 @@ pub mod pallet { } } } + +#[frame_support::pallet] +pub mod pallet2 { + use super::*; + use frame_support::pallet_prelude::*; + + #[pallet::error] + pub enum Error {} + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::pallet] + pub struct Pallet(PhantomData<(T, I)>); + + #[pallet::storage] + pub type SomeValue, I: 'static = ()> = StorageValue<_, u32>; + + #[pallet::storage] + pub type SomeMap, I: 'static = ()> = + StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; + + #[pallet::view_functions] + impl, I: 'static> Pallet + where + T::AccountId: From + SomeAssociation1, + { + /// Query value no args. + pub fn get_value() -> Option { + SomeValue::::get() + } + + /// Query value with args. + pub fn get_value_with_arg(key: u32) -> Option { + SomeMap::::get(key) + } + } +} diff --git a/substrate/frame/examples/view-functions/src/mock.rs b/substrate/frame/examples/view-functions/src/mock.rs index d128c619598a..4df30553097e 100644 --- a/substrate/frame/examples/view-functions/src/mock.rs +++ b/substrate/frame/examples/view-functions/src/mock.rs @@ -18,7 +18,7 @@ //! Mock runtime for `view-functions-example` tests. #![cfg(test)] -use crate::{self as view_functions_example}; +use crate::{pallet, pallet2}; use frame_support::derive_impl; use sp_runtime::testing::TestXt; @@ -29,7 +29,9 @@ type Block = frame_system::mocking::MockBlock; frame_support::construct_runtime!( pub enum Runtime { System: frame_system, - ViewFunctionsExample: view_functions_example, + ViewFunctionsExample: pallet, + ViewFunctionsInstance: pallet2, + // ViewFunctionsInstance: pallet2::, } ); @@ -40,7 +42,10 @@ impl frame_system::Config for Runtime { type Block = Block; } -impl view_functions_example::Config for Runtime {} +impl pallet::Config for Runtime {} +impl pallet2::Config for Runtime {} + +impl pallet2::Config for Runtime {} pub fn new_test_ext() -> sp_io::TestExternalities { use sp_runtime::BuildStorage; diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index a9b9f8acad4a..753a30584f5e 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -37,13 +37,6 @@ fn pallet_get_value_query() { let mut output = Vec::new(); let id = as Query>::id(); - println!("{id:?}"); - - // let _ = as DispatchQuery>::dispatch_query::>( - // &id, - // &mut &input[..], - // &mut output, - // ).unwrap(); let _ = ::RuntimeQuery::dispatch_query::>( &id, @@ -81,4 +74,4 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, query_result,); }); -} \ No newline at end of file +} From 5a8e559082c0942d3120491ef0e8629acfafcdff Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 21:21:16 +0100 Subject: [PATCH 027/121] Pallet instances tests --- .../frame/examples/view-functions/src/lib.rs | 2 +- .../frame/examples/view-functions/src/mock.rs | 2 +- .../examples/view-functions/src/tests.rs | 44 ++++++++++++++++++- .../src/construct_runtime/expand/query.rs | 1 - 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index 924dcca98df8..0dffcf73f9c7 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! This pallet demonstrates the use of the `pallet::task` api for service work. +//! This pallet demonstrates the use of the `pallet::view_functions` api for service work. #![cfg_attr(not(feature = "std"), no_std)] pub mod mock; diff --git a/substrate/frame/examples/view-functions/src/mock.rs b/substrate/frame/examples/view-functions/src/mock.rs index 4df30553097e..549c613cb944 100644 --- a/substrate/frame/examples/view-functions/src/mock.rs +++ b/substrate/frame/examples/view-functions/src/mock.rs @@ -31,7 +31,7 @@ frame_support::construct_runtime!( System: frame_system, ViewFunctionsExample: pallet, ViewFunctionsInstance: pallet2, - // ViewFunctionsInstance: pallet2::, + ViewFunctionsInstance1: pallet2::, } ); diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 753a30584f5e..ad3840bbcd43 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -15,12 +15,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Tests for `pallet-example-tasks`. +//! Tests for `pallet-example-view-functions`. #![cfg(test)] use crate::{ mock::*, pallet::{self, Pallet}, + pallet2, }; use codec::{Decode, Encode}; use frame_support::traits::{DispatchQuery, Query}; @@ -75,3 +76,44 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, query_result,); }); } + +#[test] +fn pallet_instances() { + use pallet2::Instance1; + + new_test_ext().execute_with(|| { + let instance_value = Some(123); + let instance1_value = Some(456); + + pallet2::SomeValue::::set(instance_value); + pallet2::SomeValue::::set(instance1_value); + + let query = pallet2::GetValueQuery::::new(); + test_dispatch_query::<::RuntimeQuery, _, _>( + query, + instance_value, + ); + + let query_instance1 = pallet2::GetValueQuery::::new(); + test_dispatch_query::<::RuntimeQuery, _, _>( + query_instance1, + instance1_value, + ); + }); +} + +fn test_dispatch_query(query: Q, expected: V) +where + D: DispatchQuery, + Q: Query + Encode, + V: Decode + Eq + PartialEq + std::fmt::Debug, +{ + let input = query.encode(); + let mut output = Vec::new(); + + D::dispatch_query::>(&Q::id(), &mut &input[..], &mut output).unwrap(); + + let query_result = V::decode(&mut &output[..]).unwrap(); + + assert_eq!(expected, query_result,); +} diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index 72b42d6362a3..437213bcd32d 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -54,7 +54,6 @@ pub fn expand_outer_query( output: &mut O ) -> Result<(), #scrate::__private::codec::Error> { - println!("dispatch_query {id:?}"); #( #prefix_conditionals )* Err(#scrate::__private::codec::Error::from("todo: no prefix")) } From e71b9de3afe0ad8019055b40f86b2da87b30aa35 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 3 Jun 2024 21:38:45 +0100 Subject: [PATCH 028/121] Refactor tests --- .../examples/view-functions/src/tests.rs | 52 +++++-------------- 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index ad3840bbcd43..f7a61c767ffe 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -34,21 +34,7 @@ fn pallet_get_value_query() { assert_eq!(some_value, Pallet::::get_value()); let query = pallet::GetValueQuery::::new(); - let input = query.encode(); - let mut output = Vec::new(); - - let id = as Query>::id(); - - let _ = ::RuntimeQuery::dispatch_query::>( - &id, - &mut &input[..], - &mut output, - ) - .unwrap(); - - let query_result = >::decode(&mut &output[..]).unwrap(); - - assert_eq!(some_value, query_result,); + test_dispatch_query(&query, some_value); }); } @@ -61,24 +47,12 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, Pallet::::get_value_with_arg(some_key)); let query = pallet::GetValueWithArgQuery::::new(some_key); - let input = query.encode(); - let mut output = Vec::new(); - - let _ = as DispatchQuery>::dispatch_query::>( - & as Query>::id(), - &mut &input[..], - &mut output, - ) - .unwrap(); - - let query_result = >::decode(&mut &output[..]).unwrap(); - - assert_eq!(some_value, query_result,); + test_dispatch_query(&query, some_value); }); } #[test] -fn pallet_instances() { +fn pallet_multiple_instances() { use pallet2::Instance1; new_test_ext().execute_with(|| { @@ -89,29 +63,27 @@ fn pallet_instances() { pallet2::SomeValue::::set(instance1_value); let query = pallet2::GetValueQuery::::new(); - test_dispatch_query::<::RuntimeQuery, _, _>( - query, - instance_value, - ); + test_dispatch_query(&query, instance_value); let query_instance1 = pallet2::GetValueQuery::::new(); - test_dispatch_query::<::RuntimeQuery, _, _>( - query_instance1, - instance1_value, - ); + test_dispatch_query(&query_instance1, instance1_value); }); } -fn test_dispatch_query(query: Q, expected: V) +fn test_dispatch_query(query: &Q, expected: V) where - D: DispatchQuery, Q: Query + Encode, V: Decode + Eq + PartialEq + std::fmt::Debug, { let input = query.encode(); let mut output = Vec::new(); - D::dispatch_query::>(&Q::id(), &mut &input[..], &mut output).unwrap(); + ::RuntimeQuery::dispatch_query::>( + &Q::id(), + &mut &input[..], + &mut output, + ) + .unwrap(); let query_result = V::decode(&mut &output[..]).unwrap(); From 22dd94b3e1fa3a4976b488d350005b85adb9cddd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 4 Jun 2024 09:59:47 +0100 Subject: [PATCH 029/121] Move execute from codegen to trait --- .../procedural/src/pallet/expand/view_functions.rs | 5 +---- substrate/frame/support/src/traits/query.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 2623b0e516d9..416cbdd365bc 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -159,10 +159,7 @@ fn impl_dispatch_query( let query_struct_ident = view_fn.query_struct_ident(); quote::quote! { <#query_struct_ident<#type_use_gen> as #frame_support::traits::QueryIdSuffix>::SUFFIX => { - let query = <#query_struct_ident<#type_use_gen> as #frame_support::__private::codec::DecodeAll>::decode_all(input)?; - let result = <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::query(query); - let output = #frame_support::__private::codec::Encode::encode_to(&result, output); - ::core::result::Result::Ok(output) + <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::execute(input, output) } } }); diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index a67942eabb61..69ab4abea319 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -17,7 +17,7 @@ //! Traits for querying pallet view functions. -use codec::{Decode, Encode, Output}; +use codec::{Decode, DecodeAll, Encode, Output}; use sp_runtime::RuntimeDebug; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix @@ -54,9 +54,16 @@ pub struct QueryId { } /// implemented for each pallet view function method -pub trait Query { +pub trait Query: DecodeAll { fn id() -> QueryId; - type ReturnType: codec::Codec; + type ReturnType: Encode; fn query(self) -> Self::ReturnType; + + fn execute(input: &mut &[u8], output: &mut O) -> Result<(), codec::Error> { + let query = Self::decode_all(input)?; + let result = query.query(); + Encode::encode_to(&result, output); + Ok(()) + } } From 117ac87e87bf098068e2a7cd44e7f2e4eb5da288 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 4 Jun 2024 15:00:12 +0100 Subject: [PATCH 030/121] WIP query runtime api --- .../src/construct_runtime/expand/query.rs | 4 ++-- .../src/pallet/expand/view_functions.rs | 4 ++-- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/query.rs | 24 +++++++++++++++---- .../frame/system/rpc/runtime-api/src/lib.rs | 11 +++++++++ 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index 437213bcd32d..e981e3095ed6 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -52,10 +52,10 @@ pub fn expand_outer_query( id: & #scrate::traits::QueryId, input: &mut &[u8], output: &mut O - ) -> Result<(), #scrate::__private::codec::Error> + ) -> Result<(), #scrate::traits::QueryDispatchError> { #( #prefix_conditionals )* - Err(#scrate::__private::codec::Error::from("todo: no prefix")) + Err(#scrate::traits::QueryDispatchError::NotFound(id.clone())) } } }; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 416cbdd365bc..64f9d822484a 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -173,12 +173,12 @@ fn impl_dispatch_query( fn dispatch_query< O: #frame_support::__private::codec::Output, > - (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::codec::Error> + (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::traits::QueryDispatchError> { // let y = 1; // todo: [AJ] why is unused variable error not triggered here - unused functions? match id.suffix { #( #query_match_arms )* - _ => Err(#frame_support::__private::codec::Error::from("DispatchQuery not implemented")), // todo: [AJ] + _ => Err(#frame_support::traits::QueryDispatchError::NotFound(id.clone())), } } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 40a2e954539b..df6bcb615846 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,7 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; -pub use query::{DispatchQuery, Query, QueryId, QueryIdPrefix, QueryIdSuffix}; +pub use query::{DispatchQuery, Query, QueryDispatchError, QueryId, QueryIdPrefix, QueryIdSuffix}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 69ab4abea319..ce753994b2ba 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -26,7 +26,7 @@ pub trait DispatchQuery { id: &QueryId, input: &mut &[u8], output: &mut O, - ) -> Result<(), codec::Error>; + ) -> Result<(), QueryDispatchError>; } impl DispatchQuery for () { @@ -34,8 +34,8 @@ impl DispatchQuery for () { _id: &QueryId, _input: &mut &[u8], _output: &mut O, - ) -> Result<(), codec::Error> { - Err(codec::Error::from("DispatchQuery not implemented")) // todo: return "query not found" error? + ) -> Result<(), QueryDispatchError> { + Err(QueryDispatchError::NotImplemented) } } @@ -47,12 +47,26 @@ pub trait QueryIdSuffix { const SUFFIX: [u8; 16]; } -#[derive(Encode, Decode, RuntimeDebug)] +#[derive(Clone, Encode, Decode, RuntimeDebug)] pub struct QueryId { pub prefix: [u8; 16], pub suffix: [u8; 16], } +#[derive(Encode, Decode, RuntimeDebug)] + +pub enum QueryDispatchError { + NotImplemented, + NotFound(QueryId), + Codec(String), +} + +impl From for QueryDispatchError { + fn from(e: codec::Error) -> Self { + QueryDispatchError::Codec(codec::Error::to_string(&e)) + } +} + /// implemented for each pallet view function method pub trait Query: DecodeAll { fn id() -> QueryId; @@ -60,7 +74,7 @@ pub trait Query: DecodeAll { fn query(self) -> Self::ReturnType; - fn execute(input: &mut &[u8], output: &mut O) -> Result<(), codec::Error> { + fn execute(input: &mut &[u8], output: &mut O) -> Result<(), QueryDispatchError> { let query = Self::decode_all(input)?; let result = query.query(); Encode::encode_to(&result, output); diff --git a/substrate/frame/system/rpc/runtime-api/src/lib.rs b/substrate/frame/system/rpc/runtime-api/src/lib.rs index f59988d818f0..8257980dc408 100644 --- a/substrate/frame/system/rpc/runtime-api/src/lib.rs +++ b/substrate/frame/system/rpc/runtime-api/src/lib.rs @@ -32,4 +32,15 @@ sp_api::decl_runtime_apis! { /// Get current account nonce of given `AccountId`. fn account_nonce(account: AccountId) -> Nonce; } + + /// API for executing view function queries + pub trait ViewFunctionsApi where + QueryId: codec::Codec, + Query: codec::Codec, + QueryResult: codec::Codec, + Error: codec::Codec, + { + /// Execute a view function query. + fn execute_query(query_id: QueryId, query: Query) -> Result; + } } From def7807da09eec2e39eda96c2e4a48c309068316 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 5 Jun 2024 17:52:53 +0100 Subject: [PATCH 031/121] WIP query metadata --- .../src/construct_runtime/expand/metadata.rs | 16 +++- .../src/pallet/expand/view_functions.rs | 87 +++++++++++++++---- .../src/pallet/parse/view_functions.rs | 18 ++-- substrate/frame/support/src/traits/query.rs | 9 ++ substrate/primitives/metadata-ir/src/types.rs | 74 ++++++++++++++++ 5 files changed, 174 insertions(+), 30 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 0e76f9a92469..37ee684b0f8b 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -75,6 +75,18 @@ pub fn expand_runtime_metadata( }) .collect::>(); + let queries = pallet_declarations.iter().map(|decl| { + let name = &decl.name; + let path = &decl.path; + let instance = decl.instance.as_ref().into_iter(); + + quote! { + #path::Pallet::<#runtime #(, #path::#instance)*>::pallet_queries_metadata( + ::core::stringify!(#name) + ) + } + }); + quote! { impl #runtime { fn metadata_ir() -> #scrate::__private::metadata_ir::MetadataIR { @@ -140,7 +152,9 @@ pub fn expand_runtime_metadata( >(), event_enum_ty: #scrate::__private::scale_info::meta_type::(), error_enum_ty: #scrate::__private::scale_info::meta_type::(), - } + }, + queries: #scrate::__private::sp_std::vec![], + // queries: #scrate::__private::sp_std::vec![ #(#queries),* ], } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 64f9d822484a..df246cf2514a 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -20,13 +20,14 @@ use proc_macro2::{Span, TokenStream}; use syn::spanned::Spanned; pub fn expand_view_functions(def: &Def) -> TokenStream { - let (span, where_clause, view_fns) = match def.view_functions.as_ref() { + let (span, where_clause, view_fns, docs) = match def.view_functions.as_ref() { Some(view_fns) => ( view_fns.attr_span.clone(), view_fns.where_clause.clone(), view_fns.view_functions.clone(), + view_fns.docs.clone(), ), - None => (def.item.span(), def.config.where_clause.clone(), Vec::new()), + None => (def.item.span(), def.config.where_clause.clone(), Vec::new(), Vec::new()), }; let query_prefix_impl = expand_query_prefix_impl(def, span, where_clause.as_ref()); @@ -35,11 +36,14 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { .iter() .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); let impl_dispatch_query = impl_dispatch_query(def, span, where_clause.as_ref(), &view_fns); + let impl_query_metadata = + impl_query_metadata(def, span, where_clause.as_ref(), &view_fns, &docs); quote::quote! { #query_prefix_impl #( #view_fn_impls )* #impl_dispatch_query + // #impl_query_metadata } } @@ -165,23 +169,72 @@ fn impl_dispatch_query( }); quote::quote! { - const _: () = { - impl<#type_impl_gen> #frame_support::traits::DispatchQuery - for #pallet_ident<#type_use_gen> #where_clause + impl<#type_impl_gen> #frame_support::traits::DispatchQuery + for #pallet_ident<#type_use_gen> #where_clause + { + #[deny(unreachable_patterns)] + fn dispatch_query< + O: #frame_support::__private::codec::Output, + > + (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::traits::QueryDispatchError> { - #[deny(unreachable_patterns)] // todo: [AJ] should error if identical suffixes, does not atm - fn dispatch_query< - O: #frame_support::__private::codec::Output, - > - (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::traits::QueryDispatchError> - { - // let y = 1; // todo: [AJ] why is unused variable error not triggered here - unused functions? - match id.suffix { - #( #query_match_arms )* - _ => Err(#frame_support::traits::QueryDispatchError::NotFound(id.clone())), - } + match id.suffix { + #( #query_match_arms )* + _ => Err(#frame_support::traits::QueryDispatchError::NotFound(id.clone())), } } - }; + } + } +} + +fn impl_query_metadata( + def: &Def, + span: Span, + where_clause: Option<&syn::WhereClause>, + view_fns: &[ViewFunctionDef], + docs: &[syn::Expr], +) -> TokenStream { + let frame_support = &def.frame_support; + let pallet_ident = &def.pallet_struct.pallet; + let type_impl_gen = &def.type_impl_generics(span); + let type_use_gen = &def.type_use_generics(span); + + let queries = view_fns.iter().map(|view_fn| { + let query_struct_ident = view_fn.query_struct_ident(); + let name = &view_fn.name; + let args: Vec = vec![]; // todo + + let no_docs = vec![]; + let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &view_fn.docs }; + + quote::quote! { + #frame_support::__private::metadata_ir::QueryMetadataIR { + name: #name, + id: <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::id().into(), + args: #frame_support::__private::sp_std::vec![ #( #args ),* ], + output: #frame_support::__private::scale_info::meta_type::< + <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::ReturnType + >(), + docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], + } + } + }); + + let no_docs = vec![]; + let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { docs }; + + quote::quote! { + impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { + #[doc(hidden)] + pub fn pallet_queries_metadata(name: &'static ::core::primitive::str) + -> #frame_support::__private::metadata_ir::QueryInterfaceIR + { + #frame_support::__private::metadata_ir::QueryInterfaceIR { + name, + queries: #frame_support::__private::sp_std::vec![ #( #queries ),* ], + docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], + } + } + } } } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index fa8657c57bac..b21f05ea9f54 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -12,29 +12,22 @@ // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and +// See the License for the specific language governsing permissions and // limitations under the License. use frame_support_procedural_tools::get_doc_literals; use inflector::Inflector; use syn::spanned::Spanned; -/// Definition of dispatchables typically `impl Pallet { ... }` +/// todo: docs pub struct ViewFunctionsImplDef { /// The where_clause used. pub where_clause: Option, - // /// A set of usage of instance, must be check for consistency with trait. - // pub instances: Vec, - // /// The index of call item in pallet module. - // pub index: usize, - // /// Information on methods (used for expansion). - // pub methods: Vec, /// The span of the pallet::view_functions attribute. pub attr_span: proc_macro2::Span, - // /// Docs, specified on the impl Block. - // pub docs: Vec, - // /// The optional `weight` attribute on the `pallet::call`. - // pub inherited_call_weight: Option, + /// Docs, specified on the impl Block. + pub docs: Vec, + /// The view function query definitions. pub view_functions: Vec, } @@ -71,6 +64,7 @@ impl ViewFunctionsImplDef { view_functions, attr_span, where_clause: item_impl.generics.where_clause.clone(), + docs: get_doc_literals(&item_impl.attrs), }) } } diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index ce753994b2ba..19d75a24dd3c 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -53,6 +53,15 @@ pub struct QueryId { pub suffix: [u8; 16], } +impl From for [u8; 32] { + fn from(value: QueryId) -> Self { + let mut output = [0u8; 32]; + output[..16].copy_from_slice(&value.prefix); + output[16..].copy_from_slice(&value.suffix); + output + } +} + #[derive(Encode, Decode, RuntimeDebug)] pub enum QueryDispatchError { diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index b05f26ff55d4..ddb939e42523 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -41,6 +41,8 @@ pub struct MetadataIR { pub apis: Vec>, /// The outer enums types as found in the runtime. pub outer_enums: OuterEnumsIR, + /// Metadata of view function queries + pub queries: Vec>, } /// Metadata of a runtime trait. @@ -112,6 +114,78 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { } } +/// Metadata of a runtime query interface. +#[derive(Clone, PartialEq, Eq, Encode, Debug)] +pub struct QueryInterfaceIR { + /// Name of the query interface. + pub name: T::String, + /// Queries belonging to the query interface. + pub queries: Vec>, + /// Query interface documentation. + pub docs: Vec, +} + +impl IntoPortable for QueryInterfaceIR { + type Output = QueryInterfaceIR; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + QueryInterfaceIR { + name: self.name.into_portable(registry), + queries: registry.map_into_portable(self.queries), + docs: registry.map_into_portable(self.docs), + } + } +} + +/// Metadata of a runtime method. +#[derive(Clone, PartialEq, Eq, Encode, Debug)] +pub struct QueryMetadataIR { + /// Query name. + pub name: T::String, + /// Query id. + pub id: [u8; 32], + /// Query args. + pub args: Vec>, + /// Query output. + pub output: T::Type, + /// Query documentation. + pub docs: Vec, +} + +impl IntoPortable for QueryMetadataIR { + type Output = QueryMetadataIR; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + QueryMetadataIR { + name: self.name.into_portable(registry), + id: self.id, + args: registry.map_into_portable(self.args), + output: registry.register_type(&self.output), + docs: registry.map_into_portable(self.docs), + } + } +} + +/// Metadata of a runtime method argument. +#[derive(Clone, PartialEq, Eq, Encode, Debug)] +pub struct QueryArgMetadataIR { + /// Query argument name. + pub name: T::String, + /// Query argument type. + pub ty: T::Type, +} + +impl IntoPortable for QueryArgMetadataIR { + type Output = QueryArgMetadataIR; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + QueryArgMetadataIR { + name: self.name.into_portable(registry), + ty: registry.register_type(&self.ty), + } + } +} + /// The intermediate representation for a pallet metadata. #[derive(Clone, PartialEq, Eq, Encode, Debug)] pub struct PalletMetadataIR { From fbd4d3af792582d26ca86663a5ac9ec388cca04f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 5 Jun 2024 18:21:29 +0100 Subject: [PATCH 032/121] Fix up metadata generation --- .../procedural/src/construct_runtime/expand/metadata.rs | 3 +-- .../support/procedural/src/pallet/expand/view_functions.rs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 37ee684b0f8b..be47540fc47d 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -153,8 +153,7 @@ pub fn expand_runtime_metadata( event_enum_ty: #scrate::__private::scale_info::meta_type::(), error_enum_ty: #scrate::__private::scale_info::meta_type::(), }, - queries: #scrate::__private::sp_std::vec![], - // queries: #scrate::__private::sp_std::vec![ #(#queries),* ], + queries: #scrate::__private::sp_std::vec![ #(#queries),* ], } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index df246cf2514a..106db12590c4 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -43,7 +43,7 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { #query_prefix_impl #( #view_fn_impls )* #impl_dispatch_query - // #impl_query_metadata + #impl_query_metadata } } @@ -209,7 +209,7 @@ fn impl_query_metadata( quote::quote! { #frame_support::__private::metadata_ir::QueryMetadataIR { - name: #name, + name: ::core::stringify!(#name), id: <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::id().into(), args: #frame_support::__private::sp_std::vec![ #( #args ),* ], output: #frame_support::__private::scale_info::meta_type::< From 04d763e79e08f3c365a690f8d087e8cef1da8b6b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jun 2024 11:56:46 +0100 Subject: [PATCH 033/121] Add queries section to custom metadata --- .../src/construct_runtime/expand/metadata.rs | 7 ++- substrate/primitives/metadata-ir/src/types.rs | 11 +++- substrate/primitives/metadata-ir/src/v15.rs | 52 +++++++++++++------ 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index be47540fc47d..200651e2614f 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -153,7 +153,12 @@ pub fn expand_runtime_metadata( event_enum_ty: #scrate::__private::scale_info::meta_type::(), error_enum_ty: #scrate::__private::scale_info::meta_type::(), }, - queries: #scrate::__private::sp_std::vec![ #(#queries),* ], + query: #scrate::__private::metadata_ir::RuntimeQueryIR { + ty: #scrate::__private::scale_info::meta_type::< + <#runtime as #system_path::Config>::RuntimeQuery + >(), + interfaces: #scrate::__private::sp_std::vec![ #(#queries),* ], + } } } diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index ddb939e42523..79855d452d0c 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -42,7 +42,7 @@ pub struct MetadataIR { /// The outer enums types as found in the runtime. pub outer_enums: OuterEnumsIR, /// Metadata of view function queries - pub queries: Vec>, + pub query: RuntimeQueryIR, } /// Metadata of a runtime trait. @@ -114,6 +114,15 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { } } +/// Metadata of the the runtime query dispatch. +#[derive(Clone, PartialEq, Eq, Encode, Debug)] +pub struct RuntimeQueryIR { + /// The type implementing the runtime query dispatch. + pub ty: T::Type, + /// The query interfaces metadata. + pub interfaces: Vec>, +} + /// Metadata of a runtime query interface. #[derive(Clone, PartialEq, Eq, Encode, Debug)] pub struct QueryInterfaceIR { diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index a942eb73223b..71fe16384d6f 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -17,31 +17,51 @@ //! Convert the IR to V15 metadata. -use crate::OuterEnumsIR; - use super::types::{ - ExtrinsicMetadataIR, MetadataIR, PalletMetadataIR, RuntimeApiMetadataIR, + ExtrinsicMetadataIR, MetadataIR, OuterEnumsIR, PalletMetadataIR, RuntimeApiMetadataIR, RuntimeApiMethodMetadataIR, RuntimeApiMethodParamMetadataIR, SignedExtensionMetadataIR, }; use frame_metadata::v15::{ - CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata, RuntimeApiMetadata, - RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV15, - SignedExtensionMetadata, + CustomMetadata, CustomValueMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata, + RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, + RuntimeMetadataV15, SignedExtensionMetadata, }; +use scale_info::{IntoPortable, Registry}; impl From for RuntimeMetadataV15 { fn from(ir: MetadataIR) -> Self { - RuntimeMetadataV15::new( - ir.pallets.into_iter().map(Into::into).collect(), - ir.extrinsic.into(), - ir.ty, - ir.apis.into_iter().map(Into::into).collect(), - ir.outer_enums.into(), - // Substrate does not collect yet the custom metadata fields. - // This allows us to extend the V15 easily. - CustomMetadata { map: Default::default() }, - ) + let mut registry = Registry::new(); + let pallets = + registry.map_into_portable(ir.pallets.into_iter().map(Into::::into)); + let extrinsic = Into::::into(ir.extrinsic).into_portable(&mut registry); + let ty = registry.register_type(&ir.ty); + let apis = + registry.map_into_portable(ir.apis.into_iter().map(Into::::into)); + let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); + + // todo: serialize queries into custom, add tests. + let query_interfaces = registry.map_into_portable(ir.query.interfaces.into_iter()); + let queries_custom_metadata = CustomValueMetadata { + ty: ir.query.ty, + value: codec::Encode::encode(&query_interfaces), + } + .into_portable(&mut registry); + let mut custom_map = std::collections::BTreeMap::new(); + custom_map.insert("queries".to_string(), queries_custom_metadata); + let custom = CustomMetadata { map: custom_map }; + + Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom } + // RuntimeMetadataV15::new( + // ir.pallets.into_iter().map(Into::into).collect(), + // ir.extrinsic.into(), + // ir.ty, + // ir.apis.into_iter().map(Into::into).collect(), + // ir.outer_enums.into(), + // // Substrate does not collect yet the custom metadata fields. + // // This allows us to extend the V15 easily. + // CustomMetadata { map: Default::default() }, + // ) } } From 86f35eb120fec7835b445de7e22da34b76e5540f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jun 2024 11:57:24 +0100 Subject: [PATCH 034/121] Comment --- substrate/primitives/metadata-ir/src/v15.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 71fe16384d6f..5c26d1152c64 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -40,7 +40,7 @@ impl From for RuntimeMetadataV15 { registry.map_into_portable(ir.apis.into_iter().map(Into::::into)); let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); - // todo: serialize queries into custom, add tests. + // todo: add tests. let query_interfaces = registry.map_into_portable(ir.query.interfaces.into_iter()); let queries_custom_metadata = CustomValueMetadata { ty: ir.query.ty, @@ -52,16 +52,6 @@ impl From for RuntimeMetadataV15 { let custom = CustomMetadata { map: custom_map }; Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom } - // RuntimeMetadataV15::new( - // ir.pallets.into_iter().map(Into::into).collect(), - // ir.extrinsic.into(), - // ir.ty, - // ir.apis.into_iter().map(Into::into).collect(), - // ir.outer_enums.into(), - // // Substrate does not collect yet the custom metadata fields. - // // This allows us to extend the V15 easily. - // CustomMetadata { map: Default::default() }, - // ) } } From 885fd3ab4f5b6e2caf94f66604cdc5cda1f0512c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jun 2024 15:09:59 +0100 Subject: [PATCH 035/121] fix metadata gen --- substrate/primitives/metadata-ir/src/v15.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 5c26d1152c64..d6ff6cb93845 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -27,7 +27,9 @@ use frame_metadata::v15::{ RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV15, SignedExtensionMetadata, }; -use scale_info::{IntoPortable, Registry}; +use scale_info::{ + IntoPortable, Registry +}; impl From for RuntimeMetadataV15 { fn from(ir: MetadataIR) -> Self { @@ -45,11 +47,10 @@ impl From for RuntimeMetadataV15 { let queries_custom_metadata = CustomValueMetadata { ty: ir.query.ty, value: codec::Encode::encode(&query_interfaces), - } - .into_portable(&mut registry); - let mut custom_map = std::collections::BTreeMap::new(); - custom_map.insert("queries".to_string(), queries_custom_metadata); - let custom = CustomMetadata { map: custom_map }; + }; + let mut custom_map = scale_info::prelude::collections::BTreeMap::new(); + custom_map.insert("queries", queries_custom_metadata); + let custom = CustomMetadata { map: custom_map }.into_portable(&mut registry); Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom } } From 3b563fbb7f690f6e7290df573f6fc64307cf3160 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jun 2024 17:40:40 +0100 Subject: [PATCH 036/121] move runtime api and core types to primitives --- substrate/bin/node/runtime/src/lib.rs | 8 ++++- .../src/construct_runtime/expand/query.rs | 6 ++-- .../src/pallet/expand/view_functions.rs | 8 ++--- substrate/frame/support/src/lib.rs | 2 +- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/query.rs | 33 ++----------------- .../frame/system/rpc/runtime-api/src/lib.rs | 11 ------- substrate/primitives/api/src/lib.rs | 9 ++++- substrate/primitives/core/src/lib.rs | 31 +++++++++++++++++ substrate/primitives/metadata-ir/src/v15.rs | 4 +-- 10 files changed, 58 insertions(+), 56 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 7d9128bb940a..218d0ac1164c 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -86,7 +86,7 @@ use sp_consensus_beefy::{ mmr::MmrLeafVersion, }; use sp_consensus_grandpa::AuthorityId as GrandpaId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, QueryDispatchError, QueryId}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ create_runtime_str, @@ -2683,6 +2683,12 @@ impl_runtime_apis! { } } + impl sp_api::RuntimeQuery for Runtime { + fn execute_query(query_id: QueryId, query: Vec) -> Result, QueryDispatchError> { + todo!() + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index e981e3095ed6..4de6adfd0fa7 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -49,13 +49,13 @@ pub fn expand_outer_query( const _: () = { impl #scrate::traits::DispatchQuery for #runtime_query { fn dispatch_query( - id: & #scrate::traits::QueryId, + id: & #scrate::__private::QueryId, input: &mut &[u8], output: &mut O - ) -> Result<(), #scrate::traits::QueryDispatchError> + ) -> Result<(), #scrate::__private::QueryDispatchError> { #( #prefix_conditionals )* - Err(#scrate::traits::QueryDispatchError::NotFound(id.clone())) + Err(#scrate::__private::QueryDispatchError::NotFound(id.clone())) } } }; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 106db12590c4..7e8cf5f52bce 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -131,8 +131,8 @@ fn expand_view_function( } impl<#type_impl_gen> #frame_support::traits::Query for #query_struct_ident<#type_use_gen> #where_clause { - fn id() -> #frame_support::traits::QueryId { - #frame_support::traits::QueryId { + fn id() -> #frame_support::__private::QueryId { + #frame_support::__private::QueryId { prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::QueryIdPrefix>::prefix(), suffix: ::SUFFIX, } @@ -176,11 +176,11 @@ fn impl_dispatch_query( fn dispatch_query< O: #frame_support::__private::codec::Output, > - (id: & #frame_support::traits::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::traits::QueryDispatchError> + (id: & #frame_support::__private::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::QueryDispatchError> { match id.suffix { #( #query_match_arms )* - _ => Err(#frame_support::traits::QueryDispatchError::NotFound(id.clone())), + _ => Err(#frame_support::__private::QueryDispatchError::NotFound(id.clone())), } } } diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 8ae1f56b4d68..377e7730c7cd 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -44,7 +44,7 @@ pub mod __private { pub use paste; pub use scale_info; pub use serde; - pub use sp_core::{Get, OpaqueMetadata, Void}; + pub use sp_core::{Get, OpaqueMetadata, QueryDispatchError, QueryId, Void}; pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; #[cfg(feature = "std")] diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index df6bcb615846..e18679c0fd92 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -134,7 +134,7 @@ pub use tasks::Task; mod try_runtime; mod query; -pub use query::{DispatchQuery, Query, QueryDispatchError, QueryId, QueryIdPrefix, QueryIdSuffix}; +pub use query::{DispatchQuery, Query, QueryIdPrefix, QueryIdSuffix}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index 19d75a24dd3c..a07d04103703 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -17,8 +17,8 @@ //! Traits for querying pallet view functions. -use codec::{Decode, DecodeAll, Encode, Output}; -use sp_runtime::RuntimeDebug; +use codec::{DecodeAll, Encode, Output}; +use sp_core::{QueryDispatchError, QueryId}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { @@ -47,35 +47,6 @@ pub trait QueryIdSuffix { const SUFFIX: [u8; 16]; } -#[derive(Clone, Encode, Decode, RuntimeDebug)] -pub struct QueryId { - pub prefix: [u8; 16], - pub suffix: [u8; 16], -} - -impl From for [u8; 32] { - fn from(value: QueryId) -> Self { - let mut output = [0u8; 32]; - output[..16].copy_from_slice(&value.prefix); - output[16..].copy_from_slice(&value.suffix); - output - } -} - -#[derive(Encode, Decode, RuntimeDebug)] - -pub enum QueryDispatchError { - NotImplemented, - NotFound(QueryId), - Codec(String), -} - -impl From for QueryDispatchError { - fn from(e: codec::Error) -> Self { - QueryDispatchError::Codec(codec::Error::to_string(&e)) - } -} - /// implemented for each pallet view function method pub trait Query: DecodeAll { fn id() -> QueryId; diff --git a/substrate/frame/system/rpc/runtime-api/src/lib.rs b/substrate/frame/system/rpc/runtime-api/src/lib.rs index 8257980dc408..f59988d818f0 100644 --- a/substrate/frame/system/rpc/runtime-api/src/lib.rs +++ b/substrate/frame/system/rpc/runtime-api/src/lib.rs @@ -32,15 +32,4 @@ sp_api::decl_runtime_apis! { /// Get current account nonce of given `AccountId`. fn account_nonce(account: AccountId) -> Nonce; } - - /// API for executing view function queries - pub trait ViewFunctionsApi where - QueryId: codec::Codec, - Query: codec::Codec, - QueryResult: codec::Codec, - Error: codec::Codec, - { - /// Execute a view function query. - fn execute_query(query_id: QueryId, query: Query) -> Result; - } } diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index 20f989c4882e..595f00de1d3e 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -112,7 +112,7 @@ pub mod __private { #[cfg(feature = "std")] pub use sp_core::traits::CallContext; -use sp_core::OpaqueMetadata; +use sp_core::{OpaqueMetadata, QueryDispatchError, QueryId}; #[cfg(feature = "std")] use sp_externalities::{Extension, Extensions}; #[cfg(feature = "std")] @@ -124,6 +124,7 @@ use sp_runtime::{traits::Block as BlockT, ExtrinsicInclusionMode}; pub use sp_state_machine::StorageProof; #[cfg(feature = "std")] use sp_state_machine::{backend::AsTrieBackend, Backend as StateBackend, OverlayedChanges}; +use sp_std::vec::Vec; use sp_version::RuntimeVersion; #[cfg(feature = "std")] use std::cell::RefCell; @@ -834,6 +835,12 @@ decl_runtime_apis! { /// This can be used to call `metadata_at_version`. fn metadata_versions() -> sp_std::vec::Vec; } + + /// API for executing view function queriess + pub trait RuntimeQuery where { + /// Execute a view function query. + fn execute_query(query_id: QueryId, query: Vec) -> Result, QueryDispatchError>; + } } sp_core::generate_feature_enabled_macro!(std_enabled, feature = "std", $); diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 098bd135bfeb..6df0fb4058b9 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -323,6 +323,37 @@ pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 { #[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum Void {} +/// todo: [AJ] docs +#[derive(Encode, Decode, RuntimeDebug, TypeInfo)] + +pub enum QueryDispatchError { + NotImplemented, + NotFound(QueryId), + Codec, +} + +impl From for QueryDispatchError { + fn from(_: codec::Error) -> Self { + QueryDispatchError::Codec + } +} + +/// todo: [AJ] docs +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct QueryId { + pub prefix: [u8; 16], + pub suffix: [u8; 16], +} + +impl From for [u8; 32] { + fn from(value: QueryId) -> Self { + let mut output = [0u8; 32]; + output[..16].copy_from_slice(&value.prefix); + output[16..].copy_from_slice(&value.suffix); + output + } +} + /// Macro for creating `Maybe*` marker traits. /// /// Such a maybe-marker trait requires the given bound when `feature = std` and doesn't require diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index d6ff6cb93845..55c43204c8b8 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -27,9 +27,7 @@ use frame_metadata::v15::{ RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV15, SignedExtensionMetadata, }; -use scale_info::{ - IntoPortable, Registry -}; +use scale_info::{IntoPortable, Registry}; impl From for RuntimeMetadataV15 { fn from(ir: MetadataIR) -> Self { From 43de84102c78afbb3714f4975a8d4156b32156c0 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 11:06:44 +0100 Subject: [PATCH 037/121] Add some RuntimeQuery types --- substrate/frame/support/src/dispatch.rs | 2 ++ substrate/frame/support/src/storage/generator/mod.rs | 2 ++ substrate/frame/support/src/tests/mod.rs | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index 4a313551aca6..718bdbfa863a 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -696,6 +696,7 @@ mod weight_tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; + type RuntimeQuery; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -793,6 +794,7 @@ mod weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; + type RuntimeQuery = RuntimeQuery; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/src/storage/generator/mod.rs b/substrate/frame/support/src/storage/generator/mod.rs index dd6d622852db..5a0bd1e59377 100644 --- a/substrate/frame/support/src/storage/generator/mod.rs +++ b/substrate/frame/support/src/storage/generator/mod.rs @@ -64,6 +64,7 @@ mod tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; + type RuntimeQuery; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -131,6 +132,7 @@ mod tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; + type RuntimeQuery = RuntimeQuery; type PalletInfo = PalletInfo; type DbWeight = (); } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 88afa243f093..26a5fb4685e0 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -54,6 +54,8 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + #[inject_runtime_type] + type RuntimeQuery = (); type DbWeight = (); } } @@ -76,6 +78,8 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] + type RuntimeQuery: crate::traits::DispatchQuery; + #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } From ebcf28304b274d7367a2671a6010a442ba132169 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 14:10:45 +0100 Subject: [PATCH 038/121] Fix up westend runtime --- polkadot/runtime/westend/src/lib.rs | 3 ++- substrate/bin/node/runtime/src/lib.rs | 3 ++- .../frame/support/procedural/src/runtime/expand/mod.rs | 6 ++++++ .../support/procedural/src/runtime/parse/runtime_types.rs | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index e6790329959e..dee5107333e4 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1429,7 +1429,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeQuery, )] pub struct Runtime; diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 218d0ac1164c..978e30f8ece7 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2243,7 +2243,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeQuery, )] pub struct Runtime; diff --git a/substrate/frame/support/procedural/src/runtime/expand/mod.rs b/substrate/frame/support/procedural/src/runtime/expand/mod.rs index 43f11896808c..549ce8b7b858 100644 --- a/substrate/frame/support/procedural/src/runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/expand/mod.rs @@ -176,6 +176,7 @@ fn construct_runtime_final_expansion( let mut slash_reason = None; let mut lock_id = None; let mut task = None; + let mut query = None; for runtime_type in runtime_types.iter() { match runtime_type { @@ -218,6 +219,9 @@ fn construct_runtime_final_expansion( RuntimeType::RuntimeTask(_) => { task = Some(expand::expand_outer_task(&name, &pallets, &scrate)); }, + RuntimeType::RuntimeQuery(_) => { + query = Some(expand::expand_outer_query(&name, &pallets, &scrate)); + }, } } @@ -295,6 +299,8 @@ fn construct_runtime_final_expansion( #task + #query + #metadata #outer_config diff --git a/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs b/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs index a4480e2a1fd3..7e779aff9080 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs @@ -32,6 +32,7 @@ mod keyword { custom_keyword!(RuntimeSlashReason); custom_keyword!(RuntimeLockId); custom_keyword!(RuntimeTask); + custom_keyword!(RuntimeQuery); } #[derive(Debug, Clone, PartialEq)] @@ -45,6 +46,7 @@ pub enum RuntimeType { RuntimeSlashReason(keyword::RuntimeSlashReason), RuntimeLockId(keyword::RuntimeLockId), RuntimeTask(keyword::RuntimeTask), + RuntimeQuery(keyword::RuntimeQuery), } impl Parse for RuntimeType { @@ -69,6 +71,8 @@ impl Parse for RuntimeType { Ok(Self::RuntimeLockId(input.parse()?)) } else if lookahead.peek(keyword::RuntimeTask) { Ok(Self::RuntimeTask(input.parse()?)) + } else if lookahead.peek(keyword::RuntimeQuery) { + Ok(Self::RuntimeQuery(input.parse()?)) } else { Err(lookahead.error()) } From f0b1f3655e21d7dac8f596a404c587250c8837f6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 14:13:00 +0100 Subject: [PATCH 039/121] Add RuntimeQuery derives --- substrate/frame/support/procedural/src/lib.rs | 1 + substrate/frame/support/test/tests/runtime_ui/pass/basic.rs | 2 +- templates/minimal/runtime/src/lib.rs | 3 ++- templates/solochain/runtime/src/lib.rs | 3 ++- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 9985ded06514..63b6fc75f8d6 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -1209,6 +1209,7 @@ pub fn import_section(attr: TokenStream, tokens: TokenStream) -> TokenStream { /// RuntimeSlashReason, /// RuntimeLockId, /// RuntimeTask, +/// RuntimeQuery, /// )] /// pub struct Runtime; /// diff --git a/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs b/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs index 514f15018015..110799b584c6 100644 --- a/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs +++ b/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs @@ -27,7 +27,7 @@ impl frame_system::Config for Runtime { #[frame_support::runtime] mod runtime { #[runtime::runtime] - #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeOrigin, RuntimeError, RuntimeTask)] + #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeOrigin, RuntimeError, RuntimeTask, RuntimeQuery)] pub struct Runtime; #[runtime::pallet_index(0)] diff --git a/templates/minimal/runtime/src/lib.rs b/templates/minimal/runtime/src/lib.rs index d2debbf5689f..f6eb014219ff 100644 --- a/templates/minimal/runtime/src/lib.rs +++ b/templates/minimal/runtime/src/lib.rs @@ -93,7 +93,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeQuery, )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/lib.rs b/templates/solochain/runtime/src/lib.rs index 93a56fb0ad78..7a589bb24fec 100644 --- a/templates/solochain/runtime/src/lib.rs +++ b/templates/solochain/runtime/src/lib.rs @@ -262,7 +262,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeQuery, )] pub struct Runtime; From c6dd8a5d4c31cfd416fc3502c2e3e91c2d47cb57 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 14:57:02 +0100 Subject: [PATCH 040/121] Wire up runtime API query methods --- substrate/bin/node/runtime/src/lib.rs | 2 +- .../src/construct_runtime/expand/query.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 978e30f8ece7..bac9eb5cf6b2 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2686,7 +2686,7 @@ impl_runtime_apis! { impl sp_api::RuntimeQuery for Runtime { fn execute_query(query_id: QueryId, query: Vec) -> Result, QueryDispatchError> { - todo!() + Runtime::execute_query(query_id, query) } } diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index 4de6adfd0fa7..d3384879cf2e 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -20,7 +20,7 @@ use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; /// Expands implementation of runtime level `DispatchQuery`. pub fn expand_outer_query( - _runtime_name: &Ident, + runtime_name: &Ident, pallet_decls: &[Pallet], scrate: &TokenStream2, ) -> TokenStream2 { @@ -58,6 +58,19 @@ pub fn expand_outer_query( Err(#scrate::__private::QueryDispatchError::NotFound(id.clone())) } } + + impl #runtime_name { + /// Convenience function for query execution from the runtime API. + pub fn execute_query( + id: #scrate::__private::QueryId, + input: #scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, + ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::QueryDispatchError> + { + let mut output = #scrate::__private::sp_std::vec![]; + <#runtime_query as #scrate::traits::DispatchQuery>::dispatch_query(&id, &mut &input[..], &mut output)?; + Ok(output) + } + } }; } } From 81150baf83f8b093c0281396ce4088eea4b3957f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 15:08:08 +0100 Subject: [PATCH 041/121] Use Runtime::execute_query generated method --- substrate/frame/examples/view-functions/src/tests.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index f7a61c767ffe..0e7a146ac82e 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -76,15 +76,7 @@ where V: Decode + Eq + PartialEq + std::fmt::Debug, { let input = query.encode(); - let mut output = Vec::new(); - - ::RuntimeQuery::dispatch_query::>( - &Q::id(), - &mut &input[..], - &mut output, - ) - .unwrap(); - + let output = Runtime::execute_query(Q::id(), input).unwrap(); let query_result = V::decode(&mut &output[..]).unwrap(); assert_eq!(expected, query_result,); From 11cb3dbabe291067d3f74c232879ba573cb67efc Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 7 Jun 2024 15:08:38 +0100 Subject: [PATCH 042/121] Fmt --- polkadot/runtime/westend/src/lib.rs | 2 +- substrate/bin/node/runtime/src/lib.rs | 2 +- templates/minimal/runtime/src/lib.rs | 2 +- templates/solochain/runtime/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index dee5107333e4..e57b7dad9214 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1430,7 +1430,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery, + RuntimeQuery )] pub struct Runtime; diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index bac9eb5cf6b2..e62c287b47ac 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2244,7 +2244,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery, + RuntimeQuery )] pub struct Runtime; diff --git a/templates/minimal/runtime/src/lib.rs b/templates/minimal/runtime/src/lib.rs index f6eb014219ff..55f44c44e542 100644 --- a/templates/minimal/runtime/src/lib.rs +++ b/templates/minimal/runtime/src/lib.rs @@ -94,7 +94,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery, + RuntimeQuery )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/lib.rs b/templates/solochain/runtime/src/lib.rs index 7a589bb24fec..830adcdeb68f 100644 --- a/templates/solochain/runtime/src/lib.rs +++ b/templates/solochain/runtime/src/lib.rs @@ -263,7 +263,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery, + RuntimeQuery )] pub struct Runtime; From 7c71b8a2942dd7a38e712eec9ab97cb9b2fb1e06 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 8 Nov 2024 12:16:36 +0000 Subject: [PATCH 043/121] Rename `view_functions` to `view_functions_experimental` --- substrate/frame/examples/view-functions/src/lib.rs | 6 +++--- substrate/frame/support/procedural/src/pallet/parse/mod.rs | 6 +++--- .../support/procedural/src/pallet/parse/view_functions.rs | 6 +++--- substrate/frame/support/test/tests/pallet.rs | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index 0dffcf73f9c7..089d618dd73b 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! This pallet demonstrates the use of the `pallet::view_functions` api for service work. +//! This pallet demonstrates the use of the `pallet::view_functions_experimental` api for service work. #![cfg_attr(not(feature = "std"), no_std)] pub mod mock; @@ -58,7 +58,7 @@ pub mod pallet { #[pallet::storage] pub type SomeMap = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; - #[pallet::view_functions] + #[pallet::view_functions_experimental] impl Pallet where T::AccountId: From + SomeAssociation1, @@ -96,7 +96,7 @@ pub mod pallet2 { pub type SomeMap, I: 'static = ()> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; - #[pallet::view_functions] + #[pallet::view_functions_experimental] impl, I: 'static> Pallet where T::AccountId: From + SomeAssociation1, diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index 1eaeb92ff526..ef8057770fc8 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -570,7 +570,7 @@ mod keyword { syn::custom_keyword!(pallet); syn::custom_keyword!(extra_constants); syn::custom_keyword!(composite_enum); - syn::custom_keyword!(view_functions); + syn::custom_keyword!(view_functions_experimental); } /// The possible values for the `#[pallet::config]` attribute. @@ -788,8 +788,8 @@ impl syn::parse::Parse for PalletAttr { Ok(PalletAttr::ExtraConstants(content.parse::()?.span())) } else if lookahead.peek(keyword::composite_enum) { Ok(PalletAttr::Composite(content.parse::()?.span())) - } else if lookahead.peek(keyword::view_functions) { - Ok(PalletAttr::ViewFunctions(content.parse::()?.span())) + } else if lookahead.peek(keyword::view_functions_experimental) { + Ok(PalletAttr::ViewFunctions(content.parse::()?.span())) } else { Err(lookahead.error()) } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index b21f05ea9f54..2c24c875569b 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -23,7 +23,7 @@ use syn::spanned::Spanned; pub struct ViewFunctionsImplDef { /// The where_clause used. pub where_clause: Option, - /// The span of the pallet::view_functions attribute. + /// The span of the pallet::view_functions_experimental attribute. pub attr_span: proc_macro2::Span, /// Docs, specified on the impl Block. pub docs: Vec, @@ -38,14 +38,14 @@ impl ViewFunctionsImplDef { } else { return Err(syn::Error::new( item.span(), - "Invalid pallet::view_functions, expected item impl", + "Invalid pallet::view_functions_experimental, expected item impl", )) }; let mut view_functions = Vec::new(); for item in &mut item_impl.items { if let syn::ImplItem::Fn(method) = item { if !matches!(method.vis, syn::Visibility::Public(_)) { - let msg = "Invalid pallet::view_functions, view function must be public: \ + let msg = "Invalid pallet::view_functions_experimental, view function must be public: \ `pub fn`"; let span = match method.vis { diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index 88ed5ad433b2..58d2e1d57b17 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -458,7 +458,7 @@ pub mod pallet { _myfield: u32, } - #[pallet::view_functions] + #[pallet::view_functions_experimental] impl Pallet where T::AccountId: From + SomeAssociation1, From 60d78fc933b5bc35eda7ee59ffaec8379468ad25 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 8 Nov 2024 12:22:14 +0000 Subject: [PATCH 044/121] Remove Vec import --- substrate/primitives/api/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index 19d88c278543..d08eda1b5579 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -127,7 +127,6 @@ use sp_runtime::{traits::Block as BlockT, ExtrinsicInclusionMode}; pub use sp_state_machine::StorageProof; #[cfg(feature = "std")] use sp_state_machine::{backend::AsTrieBackend, Backend as StateBackend, OverlayedChanges}; -use sp_std::vec::Vec; use sp_version::RuntimeVersion; #[cfg(feature = "std")] use std::cell::RefCell; From 19d6f655f8155b6e2121ef0350abf76e66dd0a81 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 8 Nov 2024 12:42:26 +0000 Subject: [PATCH 045/121] Resore Vec import --- Cargo.lock | 1 + substrate/primitives/api/Cargo.toml | 2 ++ substrate/primitives/api/src/lib.rs | 1 + 3 files changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e298eb0ee4f2..d2a1a5b2b0ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21589,6 +21589,7 @@ dependencies = [ "sp-runtime 31.0.1", "sp-runtime-interface 24.0.0", "sp-state-machine 0.35.0", + "sp-std 14.0.0", "sp-test-primitives", "sp-trie 29.0.0", "sp-version 29.0.0", diff --git a/substrate/primitives/api/Cargo.toml b/substrate/primitives/api/Cargo.toml index e0a4d06b2d81..3e6e10e63bec 100644 --- a/substrate/primitives/api/Cargo.toml +++ b/substrate/primitives/api/Cargo.toml @@ -24,6 +24,7 @@ sp-runtime-interface = { workspace = true } sp-externalities = { optional = true, workspace = true } sp-version = { workspace = true } sp-state-machine = { optional = true, workspace = true } +sp-std = { workspace = true } sp-trie = { optional = true, workspace = true } hash-db = { optional = true, workspace = true, default-features = true } thiserror = { optional = true, workspace = true } @@ -52,6 +53,7 @@ std = [ "sp-runtime-interface/std", "sp-runtime/std", "sp-state-machine/std", + "sp-std/std", "sp-test-primitives/std", "sp-trie/std", "sp-version/std", diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index d08eda1b5579..19d88c278543 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -127,6 +127,7 @@ use sp_runtime::{traits::Block as BlockT, ExtrinsicInclusionMode}; pub use sp_state_machine::StorageProof; #[cfg(feature = "std")] use sp_state_machine::{backend::AsTrieBackend, Backend as StateBackend, OverlayedChanges}; +use sp_std::vec::Vec; use sp_version::RuntimeVersion; #[cfg(feature = "std")] use std::cell::RefCell; From b93cbf45834b737162ebbe287cd3908e1ea2bb2d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Nov 2024 14:54:33 +0000 Subject: [PATCH 046/121] QueryDispatchError -> ViewFunctionDispatchError --- Cargo.lock | 6 +++--- substrate/bin/node/runtime/src/lib.rs | 4 ++-- .../procedural/src/construct_runtime/expand/query.rs | 6 +++--- .../procedural/src/pallet/expand/view_functions.rs | 4 ++-- substrate/frame/support/src/lib.rs | 2 +- substrate/frame/support/src/traits/query.rs | 10 +++++----- substrate/primitives/api/src/lib.rs | 4 ++-- substrate/primitives/core/src/lib.rs | 6 +++--- 8 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80222177a696..1c2a77039edc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13367,9 +13367,9 @@ dependencies = [ name = "pallet-example-view-functions" version = "1.0.0" dependencies = [ - "frame-benchmarking", - "frame-support", - "frame-system", + "frame-benchmarking 28.0.0", + "frame-support 28.0.0", + "frame-system 28.0.0", "log", "parity-scale-codec", "scale-info", diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 1eba0b9dd5a8..ffedcf756b28 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -101,7 +101,7 @@ use sp_consensus_beefy::{ mmr::MmrLeafVersion, }; use sp_consensus_grandpa::AuthorityId as GrandpaId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata, QueryDispatchError, QueryId, H160}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, QueryId, H160}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ curve::PiecewiseLinear, @@ -2924,7 +2924,7 @@ impl_runtime_apis! { } impl sp_api::RuntimeQuery for Runtime { - fn execute_query(query_id: QueryId, query: Vec) -> Result, QueryDispatchError> { + fn execute_query(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError> { Runtime::execute_query(query_id, query) } } diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs index d3384879cf2e..8eeaf99c48b0 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs @@ -52,10 +52,10 @@ pub fn expand_outer_query( id: & #scrate::__private::QueryId, input: &mut &[u8], output: &mut O - ) -> Result<(), #scrate::__private::QueryDispatchError> + ) -> Result<(), #scrate::__private::ViewFunctionDispatchError> { #( #prefix_conditionals )* - Err(#scrate::__private::QueryDispatchError::NotFound(id.clone())) + Err(#scrate::__private::ViewFunctionDispatchError::NotFound(id.clone())) } } @@ -64,7 +64,7 @@ pub fn expand_outer_query( pub fn execute_query( id: #scrate::__private::QueryId, input: #scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, - ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::QueryDispatchError> + ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> { let mut output = #scrate::__private::sp_std::vec![]; <#runtime_query as #scrate::traits::DispatchQuery>::dispatch_query(&id, &mut &input[..], &mut output)?; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 7e8cf5f52bce..ce567a94ced6 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -176,11 +176,11 @@ fn impl_dispatch_query( fn dispatch_query< O: #frame_support::__private::codec::Output, > - (id: & #frame_support::__private::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::QueryDispatchError> + (id: & #frame_support::__private::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> { match id.suffix { #( #query_match_arms )* - _ => Err(#frame_support::__private::QueryDispatchError::NotFound(id.clone())), + _ => Err(#frame_support::__private::ViewFunctionDispatchError::NotFound(id.clone())), } } } diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 2a1f0b080eff..2e619b82d85d 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -54,7 +54,7 @@ pub mod __private { pub use scale_info; pub use serde; pub use serde_json; - pub use sp_core::{Get, OpaqueMetadata, QueryDispatchError, QueryId, Void}; + pub use sp_core::{Get, OpaqueMetadata, ViewFunctionDispatchError, QueryId, Void}; pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; #[cfg(feature = "std")] diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/query.rs index a07d04103703..fb4d0aa3a8fd 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/query.rs @@ -18,7 +18,7 @@ //! Traits for querying pallet view functions. use codec::{DecodeAll, Encode, Output}; -use sp_core::{QueryDispatchError, QueryId}; +use sp_core::{ViewFunctionDispatchError, QueryId}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix pub trait DispatchQuery { @@ -26,7 +26,7 @@ pub trait DispatchQuery { id: &QueryId, input: &mut &[u8], output: &mut O, - ) -> Result<(), QueryDispatchError>; + ) -> Result<(), ViewFunctionDispatchError>; } impl DispatchQuery for () { @@ -34,8 +34,8 @@ impl DispatchQuery for () { _id: &QueryId, _input: &mut &[u8], _output: &mut O, - ) -> Result<(), QueryDispatchError> { - Err(QueryDispatchError::NotImplemented) + ) -> Result<(), ViewFunctionDispatchError> { + Err(ViewFunctionDispatchError::NotImplemented) } } @@ -54,7 +54,7 @@ pub trait Query: DecodeAll { fn query(self) -> Self::ReturnType; - fn execute(input: &mut &[u8], output: &mut O) -> Result<(), QueryDispatchError> { + fn execute(input: &mut &[u8], output: &mut O) -> Result<(), ViewFunctionDispatchError> { let query = Self::decode_all(input)?; let result = query.query(); Encode::encode_to(&result, output); diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index 19d88c278543..fc35b1fd41bf 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -115,7 +115,7 @@ pub mod __private { #[cfg(feature = "std")] pub use sp_core::traits::CallContext; -use sp_core::{OpaqueMetadata, QueryDispatchError, QueryId}; +use sp_core::{OpaqueMetadata, ViewFunctionDispatchError, QueryId}; #[cfg(feature = "std")] use sp_externalities::{Extension, Extensions}; #[cfg(feature = "std")] @@ -848,7 +848,7 @@ decl_runtime_apis! { /// API for executing view function queriess pub trait RuntimeQuery where { /// Execute a view function query. - fn execute_query(query_id: QueryId, query: Vec) -> Result, QueryDispatchError>; + fn execute_query(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError>; } } diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index dcaf44d95dfd..d51a36167986 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -330,15 +330,15 @@ pub enum Void {} /// todo: [AJ] docs #[derive(Encode, Decode, RuntimeDebug, TypeInfo)] -pub enum QueryDispatchError { +pub enum ViewFunctionDispatchError { NotImplemented, NotFound(QueryId), Codec, } -impl From for QueryDispatchError { +impl From for ViewFunctionDispatchError { fn from(_: codec::Error) -> Self { - QueryDispatchError::Codec + ViewFunctionDispatchError::Codec } } From b58b9ae4c08f6240ea3870f0b8502461251546f4 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Nov 2024 15:14:54 +0000 Subject: [PATCH 047/121] query -> view function naming WIP --- substrate/bin/node/runtime/src/lib.rs | 2 +- .../examples/view-functions/src/tests.rs | 12 +++--- .../src/construct_runtime/expand/mod.rs | 4 +- .../expand/{query.rs => view_function.rs} | 18 ++++----- .../src/pallet/expand/view_functions.rs | 40 +++++++++---------- substrate/frame/support/src/lib.rs | 2 +- substrate/frame/support/src/tests/mod.rs | 2 +- substrate/frame/support/src/traits.rs | 4 +- .../src/traits/{query.rs => view_function.rs} | 16 ++++---- substrate/frame/system/src/lib.rs | 2 +- substrate/primitives/api/src/lib.rs | 2 +- substrate/primitives/core/src/lib.rs | 8 ++-- 12 files changed, 56 insertions(+), 56 deletions(-) rename substrate/frame/support/procedural/src/construct_runtime/expand/{query.rs => view_function.rs} (73%) rename substrate/frame/support/src/traits/{query.rs => view_function.rs} (84%) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index ffedcf756b28..594d3f4c2354 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -101,7 +101,7 @@ use sp_consensus_beefy::{ mmr::MmrLeafVersion, }; use sp_consensus_grandpa::AuthorityId as GrandpaId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, QueryId, H160}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, H160}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ curve::PiecewiseLinear, diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 0e7a146ac82e..71dc2fcf17d8 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -24,7 +24,7 @@ use crate::{ pallet2, }; use codec::{Decode, Encode}; -use frame_support::traits::{DispatchQuery, Query}; +use frame_support::traits::{DispatchViewFunction, Query}; #[test] fn pallet_get_value_query() { @@ -34,7 +34,7 @@ fn pallet_get_value_query() { assert_eq!(some_value, Pallet::::get_value()); let query = pallet::GetValueQuery::::new(); - test_dispatch_query(&query, some_value); + test_dispatch_view_function(&query, some_value); }); } @@ -47,7 +47,7 @@ fn pallet_get_value_with_arg_query() { assert_eq!(some_value, Pallet::::get_value_with_arg(some_key)); let query = pallet::GetValueWithArgQuery::::new(some_key); - test_dispatch_query(&query, some_value); + test_dispatch_view_function(&query, some_value); }); } @@ -63,14 +63,14 @@ fn pallet_multiple_instances() { pallet2::SomeValue::::set(instance1_value); let query = pallet2::GetValueQuery::::new(); - test_dispatch_query(&query, instance_value); + test_dispatch_view_function(&query, instance_value); let query_instance1 = pallet2::GetValueQuery::::new(); - test_dispatch_query(&query_instance1, instance1_value); + test_dispatch_view_function(&query_instance1, instance1_value); }); } -fn test_dispatch_query(query: &Q, expected: V) +fn test_dispatch_view_function(query: &Q, expected: V) where Q: Query + Encode, V: Decode + Eq + PartialEq + std::fmt::Debug, diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs index 8bae23f08351..0ccb936bc5a4 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs @@ -25,7 +25,7 @@ mod lock_id; mod metadata; mod origin; mod outer_enums; -mod query; +mod view_function; mod slash_reason; mod task; mod unsigned; @@ -39,7 +39,7 @@ pub use lock_id::expand_outer_lock_id; pub use metadata::expand_runtime_metadata; pub use origin::expand_outer_origin; pub use outer_enums::{expand_outer_enum, OuterEnumType}; -pub use query::expand_outer_query; +pub use view_function::expand_outer_query; pub use slash_reason::expand_outer_slash_reason; pub use task::expand_outer_task; pub use unsigned::expand_outer_validate_unsigned; diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs similarity index 73% rename from substrate/frame/support/procedural/src/construct_runtime/expand/query.rs rename to substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index 8eeaf99c48b0..a44c30ef3fd5 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/query.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -18,19 +18,19 @@ use crate::construct_runtime::Pallet; use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; -/// Expands implementation of runtime level `DispatchQuery`. +/// Expands implementation of runtime level `DispatchViewFunction`. pub fn expand_outer_query( runtime_name: &Ident, pallet_decls: &[Pallet], scrate: &TokenStream2, ) -> TokenStream2 { - let runtime_query = syn::Ident::new("RuntimeQuery", Span::call_site()); + let runtime_query = syn::Ident::new("RuntimeViewFunction", Span::call_site()); let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; quote::quote! { - if id.prefix == <#pallet_name as #scrate::traits::QueryIdPrefix>::prefix() { - return <#pallet_name as #scrate::traits::DispatchQuery>::dispatch_query(id, input, output) + if id.prefix == <#pallet_name as #scrate::traits::ViewFunctionIdPrefix>::prefix() { + return <#pallet_name as #scrate::traits::DispatchViewFunction>::dispatch_view_function(id, input, output) } } }); @@ -47,9 +47,9 @@ pub fn expand_outer_query( pub enum #runtime_query {} const _: () = { - impl #scrate::traits::DispatchQuery for #runtime_query { - fn dispatch_query( - id: & #scrate::__private::QueryId, + impl #scrate::traits::DispatchViewFunction for #runtime_query { + fn dispatch_view_function( + id: & #scrate::__private::ViewFunctionId, input: &mut &[u8], output: &mut O ) -> Result<(), #scrate::__private::ViewFunctionDispatchError> @@ -62,12 +62,12 @@ pub fn expand_outer_query( impl #runtime_name { /// Convenience function for query execution from the runtime API. pub fn execute_query( - id: #scrate::__private::QueryId, + id: #scrate::__private::ViewFunctionId, input: #scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> { let mut output = #scrate::__private::sp_std::vec![]; - <#runtime_query as #scrate::traits::DispatchQuery>::dispatch_query(&id, &mut &input[..], &mut output)?; + <#runtime_query as #scrate::traits::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; Ok(output) } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index ce567a94ced6..4beca9e84fe6 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -35,14 +35,14 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { let view_fn_impls = view_fns .iter() .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); - let impl_dispatch_query = impl_dispatch_query(def, span, where_clause.as_ref(), &view_fns); + let impl_dispatch_view_function = impl_dispatch_view_function(def, span, where_clause.as_ref(), &view_fns); let impl_query_metadata = impl_query_metadata(def, span, where_clause.as_ref(), &view_fns, &docs); quote::quote! { #query_prefix_impl #( #view_fn_impls )* - #impl_dispatch_query + #impl_dispatch_view_function #impl_query_metadata } } @@ -59,7 +59,7 @@ fn expand_query_prefix_impl( let type_use_gen = &def.type_use_generics(span); quote::quote! { - impl<#type_impl_gen> #frame_support::traits::QueryIdPrefix for #pallet_ident<#type_use_gen> #where_clause { + impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdPrefix for #pallet_ident<#type_use_gen> #where_clause { fn prefix() -> [::core::primitive::u8; 16usize] { < ::PalletInfo @@ -126,15 +126,15 @@ fn expand_view_function( } } - impl<#type_impl_gen> #frame_support::traits::QueryIdSuffix for #query_struct_ident<#type_use_gen> #where_clause { + impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdSuffix for #query_struct_ident<#type_use_gen> #where_clause { const SUFFIX: [::core::primitive::u8; 16usize] = [ #( #query_id_suffix_bytes ),* ]; } - impl<#type_impl_gen> #frame_support::traits::Query for #query_struct_ident<#type_use_gen> #where_clause { - fn id() -> #frame_support::__private::QueryId { - #frame_support::__private::QueryId { - prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::QueryIdPrefix>::prefix(), - suffix: ::SUFFIX, + impl<#type_impl_gen> #frame_support::traits::ViewFunction for #query_struct_ident<#type_use_gen> #where_clause { + fn id() -> #frame_support::__private::ViewFunctionId { + #frame_support::__private::ViewFunctionId { + prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdPrefix>::prefix(), + suffix: ::SUFFIX, } } @@ -148,7 +148,7 @@ fn expand_view_function( } } -fn impl_dispatch_query( +fn impl_dispatch_view_function( def: &Def, span: Span, where_clause: Option<&syn::WhereClause>, @@ -162,21 +162,21 @@ fn impl_dispatch_query( let query_match_arms = view_fns.iter().map(|view_fn| { let query_struct_ident = view_fn.query_struct_ident(); quote::quote! { - <#query_struct_ident<#type_use_gen> as #frame_support::traits::QueryIdSuffix>::SUFFIX => { - <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::execute(input, output) + <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdSuffix>::SUFFIX => { + <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::execute(input, output) } } }); quote::quote! { - impl<#type_impl_gen> #frame_support::traits::DispatchQuery + impl<#type_impl_gen> #frame_support::traits::DispatchViewFunction for #pallet_ident<#type_use_gen> #where_clause { #[deny(unreachable_patterns)] - fn dispatch_query< + fn dispatch_view_function< O: #frame_support::__private::codec::Output, > - (id: & #frame_support::__private::QueryId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> + (id: & #frame_support::__private::ViewFunctionId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> { match id.suffix { #( #query_match_arms )* @@ -208,12 +208,12 @@ fn impl_query_metadata( let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &view_fn.docs }; quote::quote! { - #frame_support::__private::metadata_ir::QueryMetadataIR { + #frame_support::__private::metadata_ir::ViewFunctionMetadataIR { name: ::core::stringify!(#name), - id: <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::id().into(), + id: <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::id().into(), args: #frame_support::__private::sp_std::vec![ #( #args ),* ], output: #frame_support::__private::scale_info::meta_type::< - <#query_struct_ident<#type_use_gen> as #frame_support::traits::Query>::ReturnType + <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::ReturnType >(), docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], } @@ -227,9 +227,9 @@ fn impl_query_metadata( impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { #[doc(hidden)] pub fn pallet_queries_metadata(name: &'static ::core::primitive::str) - -> #frame_support::__private::metadata_ir::QueryInterfaceIR + -> #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR { - #frame_support::__private::metadata_ir::QueryInterfaceIR { + #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR { name, queries: #frame_support::__private::sp_std::vec![ #( #queries ),* ], docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 2e619b82d85d..433b84baf410 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -54,7 +54,7 @@ pub mod __private { pub use scale_info; pub use serde; pub use serde_json; - pub use sp_core::{Get, OpaqueMetadata, ViewFunctionDispatchError, QueryId, Void}; + pub use sp_core::{Get, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, Void}; pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; #[cfg(feature = "std")] diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 43a8723b2922..7a78fe79e2d0 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -75,7 +75,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeQuery: crate::traits::DispatchQuery; + type RuntimeQuery: crate::traits::DispatchViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 3cffb15fd5bb..a3acb1179ff8 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -139,8 +139,8 @@ pub use proving::*; #[cfg(feature = "try-runtime")] mod try_runtime; -mod query; -pub use query::{DispatchQuery, Query, QueryIdPrefix, QueryIdSuffix}; +mod view_function; +pub use view_function::{DispatchViewFunction, Query, QueryIdPrefix, QueryIdSuffix}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/query.rs b/substrate/frame/support/src/traits/view_function.rs similarity index 84% rename from substrate/frame/support/src/traits/query.rs rename to substrate/frame/support/src/traits/view_function.rs index fb4d0aa3a8fd..db9a9c13825e 100644 --- a/substrate/frame/support/src/traits/query.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -18,20 +18,20 @@ //! Traits for querying pallet view functions. use codec::{DecodeAll, Encode, Output}; -use sp_core::{ViewFunctionDispatchError, QueryId}; +use sp_core::{ViewFunctionDispatchError, ViewFunctionId}; /// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix -pub trait DispatchQuery { - fn dispatch_query( - id: &QueryId, +pub trait DispatchViewFunction { + fn dispatch_view_function( + id: &ViewFunctionId, input: &mut &[u8], output: &mut O, ) -> Result<(), ViewFunctionDispatchError>; } -impl DispatchQuery for () { - fn dispatch_query( - _id: &QueryId, +impl DispatchViewFunction for () { + fn dispatch_view_function( + _id: &ViewFunctionId, _input: &mut &[u8], _output: &mut O, ) -> Result<(), ViewFunctionDispatchError> { @@ -49,7 +49,7 @@ pub trait QueryIdSuffix { /// implemented for each pallet view function method pub trait Query: DecodeAll { - fn id() -> QueryId; + fn id() -> ViewFunctionId; type ReturnType: Encode; fn query(self) -> Self::ReturnType; diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index f3b99b3c44e4..99337b257345 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -523,7 +523,7 @@ pub mod pallet { /// Type for dispatching queries. #[pallet::no_default_bounds] - type RuntimeQuery: frame_support::traits::DispatchQuery; + type RuntimeQuery: frame_support::traits::DispatchViewFunction; /// This stores the number of previous transactions associated with a sender account. type Nonce: Parameter diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index fc35b1fd41bf..9af8fa00a24a 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -115,7 +115,7 @@ pub mod __private { #[cfg(feature = "std")] pub use sp_core::traits::CallContext; -use sp_core::{OpaqueMetadata, ViewFunctionDispatchError, QueryId}; +use sp_core::{OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; #[cfg(feature = "std")] use sp_externalities::{Extension, Extensions}; #[cfg(feature = "std")] diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index d51a36167986..992cda2cf939 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -332,7 +332,7 @@ pub enum Void {} pub enum ViewFunctionDispatchError { NotImplemented, - NotFound(QueryId), + NotFound(ViewFunctionId), Codec, } @@ -344,13 +344,13 @@ impl From for ViewFunctionDispatchError { /// todo: [AJ] docs #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct QueryId { +pub struct ViewFunctionId { pub prefix: [u8; 16], pub suffix: [u8; 16], } -impl From for [u8; 32] { - fn from(value: QueryId) -> Self { +impl From for [u8; 32] { + fn from(value: ViewFunctionId) -> Self { let mut output = [0u8; 32]; output[..16].copy_from_slice(&value.prefix); output[16..].copy_from_slice(&value.suffix); From c2c3883bd3f019a3e6f58a7952879c25eef272b8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Nov 2024 15:21:46 +0000 Subject: [PATCH 048/121] query -> view function naming WIP --- substrate/bin/node/runtime/src/lib.rs | 2 +- substrate/frame/examples/view-functions/src/tests.rs | 4 ++-- .../src/construct_runtime/expand/view_function.rs | 10 +++++----- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/view_function.rs | 2 +- substrate/primitives/api/src/lib.rs | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 594d3f4c2354..58c117f7e0de 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2924,7 +2924,7 @@ impl_runtime_apis! { } impl sp_api::RuntimeQuery for Runtime { - fn execute_query(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError> { + fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError> { Runtime::execute_query(query_id, query) } } diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 71dc2fcf17d8..9b7257fef712 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -24,7 +24,7 @@ use crate::{ pallet2, }; use codec::{Decode, Encode}; -use frame_support::traits::{DispatchViewFunction, Query}; +use frame_support::traits::{DispatchViewFunction, ViewFunction}; #[test] fn pallet_get_value_query() { @@ -72,7 +72,7 @@ fn pallet_multiple_instances() { fn test_dispatch_view_function(query: &Q, expected: V) where - Q: Query + Encode, + Q: ViewFunction + Encode, V: Decode + Eq + PartialEq + std::fmt::Debug, { let input = query.encode(); diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index a44c30ef3fd5..9e0b13aa9125 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -24,7 +24,7 @@ pub fn expand_outer_query( pallet_decls: &[Pallet], scrate: &TokenStream2, ) -> TokenStream2 { - let runtime_query = syn::Ident::new("RuntimeViewFunction", Span::call_site()); + let runtime_view_function = syn::Ident::new("RuntimeViewFunction", Span::call_site()); let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; @@ -44,10 +44,10 @@ pub fn expand_outer_query( #scrate::__private::scale_info::TypeInfo, #scrate::__private::RuntimeDebug, )] - pub enum #runtime_query {} + pub enum #runtime_view_function {} const _: () = { - impl #scrate::traits::DispatchViewFunction for #runtime_query { + impl #scrate::traits::DispatchViewFunction for #runtime_view_function { fn dispatch_view_function( id: & #scrate::__private::ViewFunctionId, input: &mut &[u8], @@ -61,13 +61,13 @@ pub fn expand_outer_query( impl #runtime_name { /// Convenience function for query execution from the runtime API. - pub fn execute_query( + pub fn execute_view_function( id: #scrate::__private::ViewFunctionId, input: #scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> { let mut output = #scrate::__private::sp_std::vec![]; - <#runtime_query as #scrate::traits::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; + <#runtime_view_function as #scrate::traits::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; Ok(output) } } diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index a3acb1179ff8..8cd4f86e00cb 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -140,7 +140,7 @@ pub use proving::*; mod try_runtime; mod view_function; -pub use view_function::{DispatchViewFunction, Query, QueryIdPrefix, QueryIdSuffix}; +pub use view_function::{DispatchViewFunction, ViewFunction, QueryIdPrefix, QueryIdSuffix}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index db9a9c13825e..e597bd5c8f3d 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -48,7 +48,7 @@ pub trait QueryIdSuffix { } /// implemented for each pallet view function method -pub trait Query: DecodeAll { +pub trait ViewFunction: DecodeAll { fn id() -> ViewFunctionId; type ReturnType: Encode; diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index 9af8fa00a24a..e2a4d4f5ead9 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -848,7 +848,7 @@ decl_runtime_apis! { /// API for executing view function queriess pub trait RuntimeQuery where { /// Execute a view function query. - fn execute_query(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError>; + fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError>; } } From 0e92e5bb880b384052bccec3a25152e8444ad5b9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Nov 2024 15:30:20 +0000 Subject: [PATCH 049/121] query -> view function naming --- polkadot/runtime/westend/src/lib.rs | 2 +- substrate/bin/node/runtime/src/lib.rs | 4 ++-- .../procedural/src/construct_runtime/expand/metadata.rs | 4 ++-- substrate/frame/support/procedural/src/lib.rs | 4 ++-- .../frame/support/procedural/src/runtime/expand/mod.rs | 2 +- .../support/procedural/src/runtime/parse/runtime_types.rs | 8 ++++---- substrate/frame/support/src/dispatch.rs | 4 ++-- substrate/frame/support/src/storage/generator/mod.rs | 4 ++-- substrate/frame/support/src/tests/mod.rs | 4 ++-- .../frame/support/test/tests/runtime_ui/pass/basic.rs | 2 +- substrate/frame/system/src/lib.rs | 6 +++--- substrate/primitives/api/src/lib.rs | 2 +- substrate/primitives/metadata-ir/src/lib.rs | 2 +- substrate/primitives/metadata-ir/src/types.rs | 4 ++-- templates/minimal/runtime/src/lib.rs | 2 +- templates/solochain/runtime/src/lib.rs | 2 +- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index bc42684173de..161c96c0d0d3 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1594,7 +1594,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 58c117f7e0de..0d784d09af2e 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2381,7 +2381,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery + RuntimeViewFunction )] pub struct Runtime; @@ -2923,7 +2923,7 @@ impl_runtime_apis! { } } - impl sp_api::RuntimeQuery for Runtime { + impl sp_api::RuntimeViewFunction for Runtime { fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError> { Runtime::execute_query(query_id, query) } diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index a2e9085f23af..6900deea03c9 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -160,9 +160,9 @@ pub fn expand_runtime_metadata( event_enum_ty: #scrate::__private::scale_info::meta_type::(), error_enum_ty: #scrate::__private::scale_info::meta_type::(), }, - query: #scrate::__private::metadata_ir::RuntimeQueryIR { + query: #scrate::__private::metadata_ir::RuntimeViewFunctionIR { ty: #scrate::__private::scale_info::meta_type::< - <#runtime as #system_path::Config>::RuntimeQuery + <#runtime as #system_path::Config>::RuntimeViewFunction >(), interfaces: #scrate::__private::sp_std::vec![ #(#queries),* ], } diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 0e5601001dbf..26703a2438ef 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -817,7 +817,7 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { if item.ident != "RuntimeCall" && item.ident != "RuntimeEvent" && item.ident != "RuntimeTask" && - item.ident != "RuntimeQuery" && + item.ident != "RuntimeViewFunction" && item.ident != "RuntimeOrigin" && item.ident != "RuntimeHoldReason" && item.ident != "RuntimeFreezeReason" && @@ -827,7 +827,7 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { return syn::Error::new_spanned( item, "`#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, \ - `RuntimeTask`, `RuntimeQuery`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo`", + `RuntimeTask`, `RuntimeViewFunction`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo`", ) .to_compile_error() .into(); diff --git a/substrate/frame/support/procedural/src/runtime/expand/mod.rs b/substrate/frame/support/procedural/src/runtime/expand/mod.rs index 4620ee9a5da5..005b109c0eb5 100644 --- a/substrate/frame/support/procedural/src/runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/runtime/expand/mod.rs @@ -225,7 +225,7 @@ fn construct_runtime_final_expansion( RuntimeType::RuntimeTask(_) => { task = Some(expand::expand_outer_task(&name, &pallets, &scrate)); }, - RuntimeType::RuntimeQuery(_) => { + RuntimeType::RuntimeViewFunction(_) => { query = Some(expand::expand_outer_query(&name, &pallets, &scrate)); }, } diff --git a/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs b/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs index 7e779aff9080..9a385146a811 100644 --- a/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs +++ b/substrate/frame/support/procedural/src/runtime/parse/runtime_types.rs @@ -32,7 +32,7 @@ mod keyword { custom_keyword!(RuntimeSlashReason); custom_keyword!(RuntimeLockId); custom_keyword!(RuntimeTask); - custom_keyword!(RuntimeQuery); + custom_keyword!(RuntimeViewFunction); } #[derive(Debug, Clone, PartialEq)] @@ -46,7 +46,7 @@ pub enum RuntimeType { RuntimeSlashReason(keyword::RuntimeSlashReason), RuntimeLockId(keyword::RuntimeLockId), RuntimeTask(keyword::RuntimeTask), - RuntimeQuery(keyword::RuntimeQuery), + RuntimeViewFunction(keyword::RuntimeViewFunction), } impl Parse for RuntimeType { @@ -71,8 +71,8 @@ impl Parse for RuntimeType { Ok(Self::RuntimeLockId(input.parse()?)) } else if lookahead.peek(keyword::RuntimeTask) { Ok(Self::RuntimeTask(input.parse()?)) - } else if lookahead.peek(keyword::RuntimeQuery) { - Ok(Self::RuntimeQuery(input.parse()?)) + } else if lookahead.peek(keyword::RuntimeViewFunction) { + Ok(Self::RuntimeViewFunction(input.parse()?)) } else { Err(lookahead.error()) } diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index 10ce750e9691..5a2b79b87113 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -719,7 +719,7 @@ mod weight_tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; - type RuntimeQuery; + type RuntimeViewFunction; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -830,7 +830,7 @@ mod weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeQuery = RuntimeQuery; + type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/src/storage/generator/mod.rs b/substrate/frame/support/src/storage/generator/mod.rs index 759b5331ee50..bc6e2b2278eb 100644 --- a/substrate/frame/support/src/storage/generator/mod.rs +++ b/substrate/frame/support/src/storage/generator/mod.rs @@ -65,7 +65,7 @@ mod tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; - type RuntimeQuery; + type RuntimeViewFunction; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -133,7 +133,7 @@ mod tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeQuery = RuntimeQuery; + type RuntimeViewFunction = RuntimeViewFunction; type PalletInfo = PalletInfo; type DbWeight = (); } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 7a78fe79e2d0..4ae3574352bd 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -52,7 +52,7 @@ pub mod frame_system { #[inject_runtime_type] type RuntimeTask = (); #[inject_runtime_type] - type RuntimeQuery = (); + type RuntimeViewFunction = (); type DbWeight = (); } } @@ -75,7 +75,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeQuery: crate::traits::DispatchViewFunction; + type RuntimeViewFunction: crate::traits::DispatchViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs b/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs index 110799b584c6..8350211335a5 100644 --- a/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs +++ b/substrate/frame/support/test/tests/runtime_ui/pass/basic.rs @@ -27,7 +27,7 @@ impl frame_system::Config for Runtime { #[frame_support::runtime] mod runtime { #[runtime::runtime] - #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeOrigin, RuntimeError, RuntimeTask, RuntimeQuery)] + #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeOrigin, RuntimeError, RuntimeTask, RuntimeViewFunction)] pub struct Runtime; #[runtime::pallet_index(0)] diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 99337b257345..d17beceef14b 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -334,7 +334,7 @@ pub mod pallet { #[inject_runtime_type] type RuntimeTask = (); #[inject_runtime_type] - type RuntimeQuery = (); + type RuntimeViewFunction = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = TestBlockHashCount>; type OnSetCode = (); @@ -429,7 +429,7 @@ pub mod pallet { /// The query dispatch type, injected by `construct_runtime!`. #[inject_runtime_type] - type RuntimeQuery = (); + type RuntimeViewFunction = (); /// Converts a module to the index of the module, injected by `construct_runtime!`. #[inject_runtime_type] @@ -523,7 +523,7 @@ pub mod pallet { /// Type for dispatching queries. #[pallet::no_default_bounds] - type RuntimeQuery: frame_support::traits::DispatchViewFunction; + type RuntimeViewFunction: frame_support::traits::DispatchViewFunction; /// This stores the number of previous transactions associated with a sender account. type Nonce: Parameter diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index e2a4d4f5ead9..5e3a5cd4326d 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -846,7 +846,7 @@ decl_runtime_apis! { } /// API for executing view function queriess - pub trait RuntimeQuery where { + pub trait RuntimeViewFunction where { /// Execute a view function query. fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError>; } diff --git a/substrate/primitives/metadata-ir/src/lib.rs b/substrate/primitives/metadata-ir/src/lib.rs index 34db3dc115ee..2adf28bcd7a2 100644 --- a/substrate/primitives/metadata-ir/src/lib.rs +++ b/substrate/primitives/metadata-ir/src/lib.rs @@ -95,7 +95,7 @@ mod test { event_enum_ty: meta_type::<()>(), error_enum_ty: meta_type::<()>(), }, - query: RuntimeQueryIR { ty: meta_type::<()>(), interfaces: vec![] }, + query: RuntimeViewFunctionIR { ty: meta_type::<()>(), interfaces: vec![] }, } } diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index 29fcc805568d..2be5043b6db6 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -42,7 +42,7 @@ pub struct MetadataIR { /// The outer enums types as found in the runtime. pub outer_enums: OuterEnumsIR, /// Metadata of view function queries - pub query: RuntimeQueryIR, + pub query: RuntimeViewFunctionIR, } /// Metadata of a runtime trait. @@ -122,7 +122,7 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { /// Metadata of the the runtime query dispatch. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct RuntimeQueryIR { +pub struct RuntimeViewFunctionIR { /// The type implementing the runtime query dispatch. pub ty: T::Type, /// The query interfaces metadata. diff --git a/templates/minimal/runtime/src/lib.rs b/templates/minimal/runtime/src/lib.rs index 656da3b928cc..d13cd01bf85e 100644 --- a/templates/minimal/runtime/src/lib.rs +++ b/templates/minimal/runtime/src/lib.rs @@ -135,7 +135,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/lib.rs b/templates/solochain/runtime/src/lib.rs index e08d2f372f91..a70771ef6b1f 100644 --- a/templates/solochain/runtime/src/lib.rs +++ b/templates/solochain/runtime/src/lib.rs @@ -196,7 +196,7 @@ mod runtime { RuntimeSlashReason, RuntimeLockId, RuntimeTask, - RuntimeQuery + RuntimeViewFunction )] pub struct Runtime; From 62674bdb8f28902ef9cf9c28bdb6032c061babcf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Nov 2024 15:33:33 +0000 Subject: [PATCH 050/121] fmt --- substrate/frame/examples/view-functions/src/lib.rs | 3 ++- .../support/procedural/src/construct_runtime/expand/mod.rs | 4 ++-- .../support/procedural/src/pallet/expand/view_functions.rs | 3 ++- substrate/frame/support/procedural/src/pallet/parse/mod.rs | 4 +++- substrate/frame/support/src/lib.rs | 2 +- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/view_function.rs | 5 ++++- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index 089d618dd73b..8357fcf34f08 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -15,7 +15,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! This pallet demonstrates the use of the `pallet::view_functions_experimental` api for service work. +//! This pallet demonstrates the use of the `pallet::view_functions_experimental` api for service +//! work. #![cfg_attr(not(feature = "std"), no_std)] pub mod mock; diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs index 0ccb936bc5a4..823aa69dbdf2 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs @@ -25,10 +25,10 @@ mod lock_id; mod metadata; mod origin; mod outer_enums; -mod view_function; mod slash_reason; mod task; mod unsigned; +mod view_function; pub use call::expand_outer_dispatch; pub use config::expand_outer_config; @@ -39,7 +39,7 @@ pub use lock_id::expand_outer_lock_id; pub use metadata::expand_runtime_metadata; pub use origin::expand_outer_origin; pub use outer_enums::{expand_outer_enum, OuterEnumType}; -pub use view_function::expand_outer_query; pub use slash_reason::expand_outer_slash_reason; pub use task::expand_outer_task; pub use unsigned::expand_outer_validate_unsigned; +pub use view_function::expand_outer_query; diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 4beca9e84fe6..feb1062afef5 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -35,7 +35,8 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { let view_fn_impls = view_fns .iter() .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); - let impl_dispatch_view_function = impl_dispatch_view_function(def, span, where_clause.as_ref(), &view_fns); + let impl_dispatch_view_function = + impl_dispatch_view_function(def, span, where_clause.as_ref(), &view_fns); let impl_query_metadata = impl_query_metadata(def, span, where_clause.as_ref(), &view_fns, &docs); diff --git a/substrate/frame/support/procedural/src/pallet/parse/mod.rs b/substrate/frame/support/procedural/src/pallet/parse/mod.rs index ef8057770fc8..89875974b8b5 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/mod.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/mod.rs @@ -789,7 +789,9 @@ impl syn::parse::Parse for PalletAttr { } else if lookahead.peek(keyword::composite_enum) { Ok(PalletAttr::Composite(content.parse::()?.span())) } else if lookahead.peek(keyword::view_functions_experimental) { - Ok(PalletAttr::ViewFunctions(content.parse::()?.span())) + Ok(PalletAttr::ViewFunctions( + content.parse::()?.span(), + )) } else { Err(lookahead.error()) } diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 433b84baf410..61ad36f8bf57 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -53,7 +53,7 @@ pub mod __private { pub use paste; pub use scale_info; pub use serde; - pub use serde_json; + pub use serde_json; pub use sp_core::{Get, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, Void}; pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 8cd4f86e00cb..53aa0f8b0d3e 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -140,7 +140,7 @@ pub use proving::*; mod try_runtime; mod view_function; -pub use view_function::{DispatchViewFunction, ViewFunction, QueryIdPrefix, QueryIdSuffix}; +pub use view_function::{DispatchViewFunction, QueryIdPrefix, QueryIdSuffix, ViewFunction}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index e597bd5c8f3d..8c59c1e664ce 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -54,7 +54,10 @@ pub trait ViewFunction: DecodeAll { fn query(self) -> Self::ReturnType; - fn execute(input: &mut &[u8], output: &mut O) -> Result<(), ViewFunctionDispatchError> { + fn execute( + input: &mut &[u8], + output: &mut O, + ) -> Result<(), ViewFunctionDispatchError> { let query = Self::decode_all(input)?; let result = query.query(); Encode::encode_to(&result, output); From bb35aa4d1825eff7e336dff19dea8ae5f48e7bbc Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 12:10:33 +0000 Subject: [PATCH 051/121] query -> view function renaming WIP --- .../src/construct_runtime/expand/metadata.rs | 6 ++-- .../src/pallet/expand/view_functions.rs | 36 +++++++++---------- .../src/pallet/parse/view_functions.rs | 12 +++---- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 6900deea03c9..ca26218a6c11 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -78,13 +78,13 @@ pub fn expand_runtime_metadata( }) .collect::>(); - let queries = pallet_declarations.iter().map(|decl| { + let view_functions = pallet_declarations.iter().map(|decl| { let name = &decl.name; let path = &decl.path; let instance = decl.instance.as_ref().into_iter(); quote! { - #path::Pallet::<#runtime #(, #path::#instance)*>::pallet_queries_metadata( + #path::Pallet::<#runtime #(, #path::#instance)*>::pallet_view_functions_metadata( ::core::stringify!(#name) ) } @@ -164,7 +164,7 @@ pub fn expand_runtime_metadata( ty: #scrate::__private::scale_info::meta_type::< <#runtime as #system_path::Config>::RuntimeViewFunction >(), - interfaces: #scrate::__private::sp_std::vec![ #(#queries),* ], + interfaces: #scrate::__private::sp_std::vec![ #(#view_functions),* ], } } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index feb1062afef5..5e4c3cd9a655 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -85,14 +85,14 @@ fn expand_view_function( let type_use_gen = &def.type_use_generics(span); let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" }; - let query_struct_ident = view_fn.query_struct_ident(); + let view_function_struct_ident = view_fn.view_function_struct_ident(); let view_fn_name = &view_fn.name; let (arg_names, arg_types) = view_fn.args_names_types(); let return_type = &view_fn.return_type; let docs = &view_fn.docs; - let query_id_suffix_bytes = view_fn - .query_id_suffix_bytes() + let view_function_id_suffix_bytes = view_fn + .view_function_id_suffix_bytes() .map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), Span::call_site())); quote::quote! { @@ -110,15 +110,15 @@ fn expand_view_function( #[codec(encode_bound())] #[codec(decode_bound())] #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)] - pub struct #query_struct_ident<#type_decl_bounded_gen> #where_clause { + pub struct #view_function_struct_ident<#type_decl_bounded_gen> #where_clause { #( pub #arg_names: #arg_types, )* _marker: ::core::marker::PhantomData<(#type_use_gen,)>, } - impl<#type_impl_gen> #query_struct_ident<#type_use_gen> #where_clause { - /// Create a new [`#query_struct_ident`] instance. + impl<#type_impl_gen> #view_function_struct_ident<#type_use_gen> #where_clause { + /// Create a new [`#view_function_struct_ident`] instance. pub fn new(#( #arg_names: #arg_types, )*) -> Self { Self { #( #arg_names, )* @@ -127,11 +127,11 @@ fn expand_view_function( } } - impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdSuffix for #query_struct_ident<#type_use_gen> #where_clause { - const SUFFIX: [::core::primitive::u8; 16usize] = [ #( #query_id_suffix_bytes ),* ]; + impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdSuffix for #view_function_struct_ident<#type_use_gen> #where_clause { + const SUFFIX: [::core::primitive::u8; 16usize] = [ #( #view_function_id_suffix_bytes ),* ]; } - impl<#type_impl_gen> #frame_support::traits::ViewFunction for #query_struct_ident<#type_use_gen> #where_clause { + impl<#type_impl_gen> #frame_support::traits::ViewFunction for #view_function_struct_ident<#type_use_gen> #where_clause { fn id() -> #frame_support::__private::ViewFunctionId { #frame_support::__private::ViewFunctionId { prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdPrefix>::prefix(), @@ -161,10 +161,10 @@ fn impl_dispatch_view_function( let type_use_gen = &def.type_use_generics(span); let query_match_arms = view_fns.iter().map(|view_fn| { - let query_struct_ident = view_fn.query_struct_ident(); + let view_function_struct_ident = view_fn.view_function_struct_ident(); quote::quote! { - <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdSuffix>::SUFFIX => { - <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::execute(input, output) + <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdSuffix>::SUFFIX => { + <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::execute(input, output) } } }); @@ -200,8 +200,8 @@ fn impl_query_metadata( let type_impl_gen = &def.type_impl_generics(span); let type_use_gen = &def.type_use_generics(span); - let queries = view_fns.iter().map(|view_fn| { - let query_struct_ident = view_fn.query_struct_ident(); + let view_functions = view_fns.iter().map(|view_fn| { + let view_function_struct_ident = view_fn.view_function_struct_ident(); let name = &view_fn.name; let args: Vec = vec![]; // todo @@ -211,10 +211,10 @@ fn impl_query_metadata( quote::quote! { #frame_support::__private::metadata_ir::ViewFunctionMetadataIR { name: ::core::stringify!(#name), - id: <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::id().into(), + id: <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::id().into(), args: #frame_support::__private::sp_std::vec![ #( #args ),* ], output: #frame_support::__private::scale_info::meta_type::< - <#query_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::ReturnType + <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::ReturnType >(), docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], } @@ -227,12 +227,12 @@ fn impl_query_metadata( quote::quote! { impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { #[doc(hidden)] - pub fn pallet_queries_metadata(name: &'static ::core::primitive::str) + pub fn pallet_view_functions_metadata(name: &'static ::core::primitive::str) -> #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR { #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR { name, - queries: #frame_support::__private::sp_std::vec![ #( #queries ),* ], + queries: #frame_support::__private::sp_std::vec![ #( #view_functions ),* ], docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], } } diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 2c24c875569b..98faecc2b706 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -27,7 +27,7 @@ pub struct ViewFunctionsImplDef { pub attr_span: proc_macro2::Span, /// Docs, specified on the impl Block. pub docs: Vec, - /// The view function query definitions. + /// The view function definitions. pub view_functions: Vec, } @@ -97,14 +97,14 @@ impl TryFrom for ViewFunctionDef { } impl ViewFunctionDef { - pub fn query_struct_ident(&self) -> syn::Ident { + pub fn view_function_struct_ident(&self) -> syn::Ident { syn::Ident::new( - &format!("{}Query", self.name.to_string().to_pascal_case()), + &format!("{}ViewFunction", self.name.to_string().to_pascal_case()), self.name.span(), ) } - pub fn query_id_suffix_bytes(&self) -> [u8; 16] { + pub fn view_function_id_suffix_bytes(&self) -> [u8; 16] { let mut output = [0u8; 16]; // concatenate the signature string @@ -117,8 +117,8 @@ impl ViewFunctionDef { .join(","); let return_type = &self.return_type; let view_fn_signature = format!( - "{query_name}({arg_types}) -> {return_type}", - query_name = &self.name, + "{view_function_name}({arg_types}) -> {return_type}", + view_function_name = &self.name, return_type = quote::quote!(#return_type), ); From f5c498e45eace74bb84a8fb61e4664d4dfe8d0d5 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 12:50:28 +0000 Subject: [PATCH 052/121] query -> view function renaming WIP --- .../src/construct_runtime/expand/metadata.rs | 2 +- .../src/pallet/expand/view_functions.rs | 16 ++++++++-------- .../frame/support/src/traits/view_function.rs | 2 +- substrate/primitives/metadata-ir/src/lib.rs | 2 +- substrate/primitives/metadata-ir/src/types.rs | 14 +++++++------- substrate/primitives/metadata-ir/src/v15.rs | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index ca26218a6c11..87e0311ea38a 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -160,7 +160,7 @@ pub fn expand_runtime_metadata( event_enum_ty: #scrate::__private::scale_info::meta_type::(), error_enum_ty: #scrate::__private::scale_info::meta_type::(), }, - query: #scrate::__private::metadata_ir::RuntimeViewFunctionIR { + view_functions: #scrate::__private::metadata_ir::RuntimeViewFunctionsIR { ty: #scrate::__private::scale_info::meta_type::< <#runtime as #system_path::Config>::RuntimeViewFunction >(), diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 5e4c3cd9a655..97c8bc276f23 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -30,25 +30,25 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { None => (def.item.span(), def.config.where_clause.clone(), Vec::new(), Vec::new()), }; - let query_prefix_impl = expand_query_prefix_impl(def, span, where_clause.as_ref()); + let view_function_prefix_impl = expand_view_function_prefix_impl(def, span, where_clause.as_ref()); let view_fn_impls = view_fns .iter() .map(|view_fn| expand_view_function(def, span, where_clause.as_ref(), view_fn)); let impl_dispatch_view_function = impl_dispatch_view_function(def, span, where_clause.as_ref(), &view_fns); - let impl_query_metadata = - impl_query_metadata(def, span, where_clause.as_ref(), &view_fns, &docs); + let impl_view_function_metadata = + impl_view_function_metadata(def, span, where_clause.as_ref(), &view_fns, &docs); quote::quote! { - #query_prefix_impl + #view_function_prefix_impl #( #view_fn_impls )* #impl_dispatch_view_function - #impl_query_metadata + #impl_view_function_metadata } } -fn expand_query_prefix_impl( +fn expand_view_function_prefix_impl( def: &Def, span: Span, where_clause: Option<&syn::WhereClause>, @@ -141,7 +141,7 @@ fn expand_view_function( type ReturnType = #return_type; - fn query(self) -> Self::ReturnType { + fn view_function(self) -> Self::ReturnType { let Self { #( #arg_names, )* _marker } = self; #pallet_ident::<#type_use_gen> :: #view_fn_name( #( #arg_names, )* ) } @@ -188,7 +188,7 @@ fn impl_dispatch_view_function( } } -fn impl_query_metadata( +fn impl_view_function_metadata( def: &Def, span: Span, where_clause: Option<&syn::WhereClause>, diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 8c59c1e664ce..256106796ce1 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -52,7 +52,7 @@ pub trait ViewFunction: DecodeAll { fn id() -> ViewFunctionId; type ReturnType: Encode; - fn query(self) -> Self::ReturnType; + fn view_function(self) -> Self::ReturnType; fn execute( input: &mut &[u8], diff --git a/substrate/primitives/metadata-ir/src/lib.rs b/substrate/primitives/metadata-ir/src/lib.rs index 2adf28bcd7a2..9240c30a0cbb 100644 --- a/substrate/primitives/metadata-ir/src/lib.rs +++ b/substrate/primitives/metadata-ir/src/lib.rs @@ -95,7 +95,7 @@ mod test { event_enum_ty: meta_type::<()>(), error_enum_ty: meta_type::<()>(), }, - query: RuntimeViewFunctionIR { ty: meta_type::<()>(), interfaces: vec![] }, + view_functions: RuntimeViewFunctionsIR { ty: meta_type::<()>(), interfaces: vec![] }, } } diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index 2be5043b6db6..90eda75a7349 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -42,7 +42,7 @@ pub struct MetadataIR { /// The outer enums types as found in the runtime. pub outer_enums: OuterEnumsIR, /// Metadata of view function queries - pub query: RuntimeViewFunctionIR, + pub view_functions: RuntimeViewFunctionsIR, } /// Metadata of a runtime trait. @@ -122,16 +122,16 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { /// Metadata of the the runtime query dispatch. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct RuntimeViewFunctionIR { +pub struct RuntimeViewFunctionsIR { /// The type implementing the runtime query dispatch. pub ty: T::Type, /// The query interfaces metadata. - pub interfaces: Vec>, + pub interfaces: Vec>, } /// Metadata of a runtime query interface. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct QueryInterfaceIR { +pub struct ViewFunctionsInterfaceIR { /// Name of the query interface. pub name: T::String, /// Queries belonging to the query interface. @@ -140,11 +140,11 @@ pub struct QueryInterfaceIR { pub docs: Vec, } -impl IntoPortable for QueryInterfaceIR { - type Output = QueryInterfaceIR; +impl IntoPortable for ViewFunctionsInterfaceIR { + type Output = ViewFunctionsInterfaceIR; fn into_portable(self, registry: &mut Registry) -> Self::Output { - QueryInterfaceIR { + ViewFunctionsInterfaceIR { name: self.name.into_portable(registry), queries: registry.map_into_portable(self.queries), docs: registry.map_into_portable(self.docs), diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 475188b1a4c4..05371ca80b09 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -41,9 +41,9 @@ impl From for RuntimeMetadataV15 { let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); // todo: add tests. - let query_interfaces = registry.map_into_portable(ir.query.interfaces.into_iter()); + let query_interfaces = registry.map_into_portable(ir.view_functions.interfaces.into_iter()); let queries_custom_metadata = CustomValueMetadata { - ty: ir.query.ty, + ty: ir.view_functions.ty, value: codec::Encode::encode(&query_interfaces), }; let mut custom_map = scale_info::prelude::collections::BTreeMap::new(); From 6acc73f6ed6de114a3d8f17aaa8db367b9196534 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:18:52 +0000 Subject: [PATCH 053/121] more query -> view function renaming --- substrate/primitives/metadata-ir/src/v15.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 05371ca80b09..73c906e6c4a8 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -41,13 +41,13 @@ impl From for RuntimeMetadataV15 { let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); // todo: add tests. - let query_interfaces = registry.map_into_portable(ir.view_functions.interfaces.into_iter()); - let queries_custom_metadata = CustomValueMetadata { + let view_function_interfaces = registry.map_into_portable(ir.view_functions.interfaces.into_iter()); + let view_functions_custom_metadata = CustomValueMetadata { ty: ir.view_functions.ty, - value: codec::Encode::encode(&query_interfaces), + value: codec::Encode::encode(&view_function_interfaces), }; let mut custom_map = scale_info::prelude::collections::BTreeMap::new(); - custom_map.insert("queries", queries_custom_metadata); + custom_map.insert("view_functions", view_functions_custom_metadata); let custom = CustomMetadata { map: custom_map }.into_portable(&mut registry); Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom } From 282ce5e3bdea080700969eb0efb5c7ddbc225f03 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:28:24 +0000 Subject: [PATCH 054/121] more query -> view function renaming --- substrate/bin/node/runtime/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 0d784d09af2e..42b1f688595a 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -2924,8 +2924,8 @@ impl_runtime_apis! { } impl sp_api::RuntimeViewFunction for Runtime { - fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError> { - Runtime::execute_query(query_id, query) + fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + Runtime::execute_view_function(id, input) } } From 39d6e357a1ee129d39d19eb19a51cdb816f00ad8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:28:49 +0000 Subject: [PATCH 055/121] fmt --- .../support/procedural/src/pallet/expand/view_functions.rs | 3 ++- substrate/primitives/metadata-ir/src/v15.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 97c8bc276f23..ab4ebf27b9e5 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -30,7 +30,8 @@ pub fn expand_view_functions(def: &Def) -> TokenStream { None => (def.item.span(), def.config.where_clause.clone(), Vec::new(), Vec::new()), }; - let view_function_prefix_impl = expand_view_function_prefix_impl(def, span, where_clause.as_ref()); + let view_function_prefix_impl = + expand_view_function_prefix_impl(def, span, where_clause.as_ref()); let view_fn_impls = view_fns .iter() diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 73c906e6c4a8..ac1cc93f2784 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -41,7 +41,8 @@ impl From for RuntimeMetadataV15 { let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); // todo: add tests. - let view_function_interfaces = registry.map_into_portable(ir.view_functions.interfaces.into_iter()); + let view_function_interfaces = + registry.map_into_portable(ir.view_functions.interfaces.into_iter()); let view_functions_custom_metadata = CustomValueMetadata { ty: ir.view_functions.ty, value: codec::Encode::encode(&view_function_interfaces), From d35e6dfea173e08d98fa68b9c2a8e003e8ea4470 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:40:04 +0000 Subject: [PATCH 056/121] invoke --- .../support/procedural/src/pallet/expand/view_functions.rs | 2 +- substrate/frame/support/src/traits/view_function.rs | 6 +++--- substrate/primitives/api/src/lib.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index ab4ebf27b9e5..c9365d1d8717 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -142,7 +142,7 @@ fn expand_view_function( type ReturnType = #return_type; - fn view_function(self) -> Self::ReturnType { + fn invoke(self) -> Self::ReturnType { let Self { #( #arg_names, )* _marker } = self; #pallet_ident::<#type_use_gen> :: #view_fn_name( #( #arg_names, )* ) } diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 256106796ce1..5a142dcc2bf6 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -52,14 +52,14 @@ pub trait ViewFunction: DecodeAll { fn id() -> ViewFunctionId; type ReturnType: Encode; - fn view_function(self) -> Self::ReturnType; + fn invoke(self) -> Self::ReturnType; fn execute( input: &mut &[u8], output: &mut O, ) -> Result<(), ViewFunctionDispatchError> { - let query = Self::decode_all(input)?; - let result = query.query(); + let view_function = Self::decode_all(input)?; + let result = view_function.invoke(); Encode::encode_to(&result, output); Ok(()) } diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index 5e3a5cd4326d..a366100f246b 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -848,7 +848,7 @@ decl_runtime_apis! { /// API for executing view function queriess pub trait RuntimeViewFunction where { /// Execute a view function query. - fn execute_view_function(query_id: QueryId, query: Vec) -> Result, ViewFunctionDispatchError>; + fn execute_view_function(query_id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError>; } } From 9e3bc13fa6c0e806c4110e2604c009d02655431d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:43:36 +0000 Subject: [PATCH 057/121] even more query -> view function renaming --- substrate/frame/support/src/traits.rs | 4 +++- substrate/frame/support/src/traits/view_function.rs | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 53aa0f8b0d3e..f59508341444 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -140,7 +140,9 @@ pub use proving::*; mod try_runtime; mod view_function; -pub use view_function::{DispatchViewFunction, QueryIdPrefix, QueryIdSuffix, ViewFunction}; +pub use view_function::{ + DispatchViewFunction, ViewFunction, ViewFunctionIdPrefix, ViewFunctionIdSuffix, +}; #[cfg(feature = "try-runtime")] pub use try_runtime::{ diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 5a142dcc2bf6..9a4de07cf465 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -39,11 +39,11 @@ impl DispatchViewFunction for () { } } -pub trait QueryIdPrefix { +pub trait ViewFunctionIdPrefix { fn prefix() -> [u8; 16]; } -pub trait QueryIdSuffix { +pub trait ViewFunctionIdSuffix { const SUFFIX: [u8; 16]; } From 82e9fbc3a56aae8f8ebc8d67c5af416b52945f79 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 22 Nov 2024 17:56:51 +0000 Subject: [PATCH 058/121] final query -> view function renaming? --- substrate/frame/examples/view-functions/src/tests.rs | 10 +++++----- .../procedural/src/pallet/expand/view_functions.rs | 4 ++-- substrate/primitives/metadata-ir/src/types.rs | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 9b7257fef712..4745729f1393 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -33,7 +33,7 @@ fn pallet_get_value_query() { pallet::SomeValue::::set(some_value); assert_eq!(some_value, Pallet::::get_value()); - let query = pallet::GetValueQuery::::new(); + let query = pallet::GetValueViewFunction::::new(); test_dispatch_view_function(&query, some_value); }); } @@ -46,7 +46,7 @@ fn pallet_get_value_with_arg_query() { pallet::SomeMap::::set(some_key, some_value); assert_eq!(some_value, Pallet::::get_value_with_arg(some_key)); - let query = pallet::GetValueWithArgQuery::::new(some_key); + let query = pallet::GetValueWithArgViewFunction::::new(some_key); test_dispatch_view_function(&query, some_value); }); } @@ -62,10 +62,10 @@ fn pallet_multiple_instances() { pallet2::SomeValue::::set(instance_value); pallet2::SomeValue::::set(instance1_value); - let query = pallet2::GetValueQuery::::new(); + let query = pallet2::GetValueViewFunction::::new(); test_dispatch_view_function(&query, instance_value); - let query_instance1 = pallet2::GetValueQuery::::new(); + let query_instance1 = pallet2::GetValueViewFunction::::new(); test_dispatch_view_function(&query_instance1, instance1_value); }); } @@ -76,7 +76,7 @@ where V: Decode + Eq + PartialEq + std::fmt::Debug, { let input = query.encode(); - let output = Runtime::execute_query(Q::id(), input).unwrap(); + let output = Runtime::execute_view_function(Q::id(), input).unwrap(); let query_result = V::decode(&mut &output[..]).unwrap(); assert_eq!(expected, query_result,); diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index c9365d1d8717..0ef1e710b149 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -229,9 +229,9 @@ fn impl_view_function_metadata( impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { #[doc(hidden)] pub fn pallet_view_functions_metadata(name: &'static ::core::primitive::str) - -> #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR + -> #frame_support::__private::metadata_ir::ViewFunctionsInterfaceIR { - #frame_support::__private::metadata_ir::ViewFunctionInterfaceIR { + #frame_support::__private::metadata_ir::ViewFunctionsInterfaceIR { name, queries: #frame_support::__private::sp_std::vec![ #( #view_functions ),* ], docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index 90eda75a7349..fa88fd006893 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -135,7 +135,7 @@ pub struct ViewFunctionsInterfaceIR { /// Name of the query interface. pub name: T::String, /// Queries belonging to the query interface. - pub queries: Vec>, + pub queries: Vec>, /// Query interface documentation. pub docs: Vec, } @@ -152,9 +152,9 @@ impl IntoPortable for ViewFunctionsInterfaceIR { } } -/// Metadata of a runtime method. +/// Metadata of a runtime view function. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct QueryMetadataIR { +pub struct ViewFunctionMetadataIR { /// Query name. pub name: T::String, /// Query id. @@ -167,11 +167,11 @@ pub struct QueryMetadataIR { pub docs: Vec, } -impl IntoPortable for QueryMetadataIR { - type Output = QueryMetadataIR; +impl IntoPortable for ViewFunctionMetadataIR { + type Output = ViewFunctionMetadataIR; fn into_portable(self, registry: &mut Registry) -> Self::Output { - QueryMetadataIR { + ViewFunctionMetadataIR { name: self.name.into_portable(registry), id: self.id, args: registry.map_into_portable(self.args), From 09d3e75fd90fc5dbc29cc87d3439162d6a0c6ca9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Nov 2024 10:05:47 +0000 Subject: [PATCH 059/121] Warnings/docs --- substrate/frame/examples/view-functions/src/tests.rs | 2 +- substrate/primitives/core/src/lib.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 4745729f1393..11d334819ec0 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -24,7 +24,7 @@ use crate::{ pallet2, }; use codec::{Decode, Encode}; -use frame_support::traits::{DispatchViewFunction, ViewFunction}; +use frame_support::traits::ViewFunction; #[test] fn pallet_get_value_query() { diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 992cda2cf939..4210651c48f2 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -327,12 +327,14 @@ pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 { #[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum Void {} -/// todo: [AJ] docs +/// Error type for view function dispatching. #[derive(Encode, Decode, RuntimeDebug, TypeInfo)] - pub enum ViewFunctionDispatchError { + /// View functions are not implemented for this runtime. NotImplemented, + /// A view function with the given `ViewFunctionId` was not found NotFound(ViewFunctionId), + /// Failed to decode the view function input. Codec, } From b12000183a584e48c4cb676005acd54288a9f7b5 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Nov 2024 10:29:25 +0000 Subject: [PATCH 060/121] Manual formatting --- .../procedural/src/pallet/expand/view_functions.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 0ef1e710b149..779790c1e463 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -175,10 +175,11 @@ fn impl_dispatch_view_function( for #pallet_ident<#type_use_gen> #where_clause { #[deny(unreachable_patterns)] - fn dispatch_view_function< - O: #frame_support::__private::codec::Output, - > - (id: & #frame_support::__private::ViewFunctionId, input: &mut &[u8], output: &mut O) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> + fn dispatch_view_function( + id: & #frame_support::__private::ViewFunctionId, + input: &mut &[u8], + output: &mut O + ) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> { match id.suffix { #( #query_match_arms )* From 49a5d737d42683c03fd597eb227445e8b3611b13 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Nov 2024 11:30:13 +0000 Subject: [PATCH 061/121] More warnings, format --- .../support/procedural/src/pallet/expand/view_functions.rs | 4 ++-- substrate/primitives/core/src/lib.rs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 779790c1e463..7759ea4e4a7f 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -176,8 +176,8 @@ fn impl_dispatch_view_function( { #[deny(unreachable_patterns)] fn dispatch_view_function( - id: & #frame_support::__private::ViewFunctionId, - input: &mut &[u8], + id: & #frame_support::__private::ViewFunctionId, + input: &mut &[u8], output: &mut O ) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> { diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 4210651c48f2..79f25552fc16 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -344,10 +344,15 @@ impl From for ViewFunctionDispatchError { } } -/// todo: [AJ] docs +/// The unique identifier for a view function. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct ViewFunctionId { + /// The part of the id for dispatching view functions from the top level of the runtime. + /// + /// Specifies which view function grouping this view function belongs to. This could be a group + /// of view functions associated with a pallet, or a pallet agnostic group of view functions. pub prefix: [u8; 16], + /// The part of the id for dispatching to a view function within a group. pub suffix: [u8; 16], } From cf9c01e0b9acbdb04b7db5b39d29c673e39abaf2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Nov 2024 11:46:10 +0000 Subject: [PATCH 062/121] Rename view function interface to groups --- .../src/construct_runtime/expand/metadata.rs | 2 +- .../src/pallet/expand/view_functions.rs | 6 ++-- substrate/primitives/metadata-ir/src/lib.rs | 2 +- substrate/primitives/metadata-ir/src/types.rs | 28 ++++++++++--------- substrate/primitives/metadata-ir/src/v15.rs | 2 +- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 87e0311ea38a..e7d44c97c3d1 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -164,7 +164,7 @@ pub fn expand_runtime_metadata( ty: #scrate::__private::scale_info::meta_type::< <#runtime as #system_path::Config>::RuntimeViewFunction >(), - interfaces: #scrate::__private::sp_std::vec![ #(#view_functions),* ], + groups: #scrate::__private::sp_std::vec![ #(#view_functions),* ], } } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 7759ea4e4a7f..0ad967e1a4e8 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -230,11 +230,11 @@ fn impl_view_function_metadata( impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { #[doc(hidden)] pub fn pallet_view_functions_metadata(name: &'static ::core::primitive::str) - -> #frame_support::__private::metadata_ir::ViewFunctionsInterfaceIR + -> #frame_support::__private::metadata_ir::ViewFunctionGroupIR { - #frame_support::__private::metadata_ir::ViewFunctionsInterfaceIR { + #frame_support::__private::metadata_ir::ViewFunctionGroupIR { name, - queries: #frame_support::__private::sp_std::vec![ #( #view_functions ),* ], + view_functions: #frame_support::__private::sp_std::vec![ #( #view_functions ),* ], docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], } } diff --git a/substrate/primitives/metadata-ir/src/lib.rs b/substrate/primitives/metadata-ir/src/lib.rs index 9240c30a0cbb..4f0afb042790 100644 --- a/substrate/primitives/metadata-ir/src/lib.rs +++ b/substrate/primitives/metadata-ir/src/lib.rs @@ -95,7 +95,7 @@ mod test { event_enum_ty: meta_type::<()>(), error_enum_ty: meta_type::<()>(), }, - view_functions: RuntimeViewFunctionsIR { ty: meta_type::<()>(), interfaces: vec![] }, + view_functions: RuntimeViewFunctionsIR { ty: meta_type::<()>(), groups: vec![] }, } } diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index fa88fd006893..bd805cf4c9e7 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -120,33 +120,35 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { } } -/// Metadata of the the runtime query dispatch. +/// Metadata of the top level runtime view function dispatch. #[derive(Clone, PartialEq, Eq, Encode, Debug)] pub struct RuntimeViewFunctionsIR { /// The type implementing the runtime query dispatch. pub ty: T::Type, - /// The query interfaces metadata. - pub interfaces: Vec>, + /// The view function groupings metadata. + pub groups: Vec>, } -/// Metadata of a runtime query interface. +/// Metadata of a runtime view function group. +/// +/// For example, view functions associated with a pallet would form a view function group. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct ViewFunctionsInterfaceIR { - /// Name of the query interface. +pub struct ViewFunctionGroupIR { + /// Name of the view function group. pub name: T::String, - /// Queries belonging to the query interface. - pub queries: Vec>, - /// Query interface documentation. + /// View functions belonging to the group. + pub view_functions: Vec>, + /// View function group documentation. pub docs: Vec, } -impl IntoPortable for ViewFunctionsInterfaceIR { - type Output = ViewFunctionsInterfaceIR; +impl IntoPortable for ViewFunctionGroupIR { + type Output = ViewFunctionGroupIR; fn into_portable(self, registry: &mut Registry) -> Self::Output { - ViewFunctionsInterfaceIR { + ViewFunctionGroupIR { name: self.name.into_portable(registry), - queries: registry.map_into_portable(self.queries), + view_functions: registry.map_into_portable(self.view_functions), docs: registry.map_into_portable(self.docs), } } diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index ac1cc93f2784..5181e4b59aac 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -42,7 +42,7 @@ impl From for RuntimeMetadataV15 { // todo: add tests. let view_function_interfaces = - registry.map_into_portable(ir.view_functions.interfaces.into_iter()); + registry.map_into_portable(ir.view_functions.groups.into_iter()); let view_functions_custom_metadata = CustomValueMetadata { ty: ir.view_functions.ty, value: codec::Encode::encode(&view_function_interfaces), From cb9767641f2c8fa5050f111c0841077b4e157889 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Nov 2024 12:30:39 +0000 Subject: [PATCH 063/121] Add PR doc --- prdoc/pr_4722.prdoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 prdoc/pr_4722.prdoc diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc new file mode 100644 index 000000000000..a7aaff5db8cc --- /dev/null +++ b/prdoc/pr_4722.prdoc @@ -0,0 +1,18 @@ +# 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: Implement pallet view functions + +doc: + - audience: Runtime Dev + description: | + Read-only view functions can now be defined on pallets. These functions provide an interface for querying state, + from both outside and inside the runtime. Common queries can be defined on pallets, without users having to + access the storage directly. + - audience: Runtime User + description: | + Querying the runtime state is now easier with the introduction of pallet view functions. Clients can call commonly + defined view functions rather than accessing the storage directly. These are similar to the Runtime APIs, but + are defined within the runtime itself. + +crates: [ ] From b87bc0ef02996cdffc1e0ca648f5156e70094698 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Nov 2024 09:50:55 +0000 Subject: [PATCH 064/121] Renaming and implement view fn args --- .../src/pallet/expand/view_functions.rs | 16 +++++++++++++++- substrate/primitives/metadata-ir/src/types.rs | 10 +++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 0ad967e1a4e8..3aa0b1c93e89 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -205,7 +205,21 @@ fn impl_view_function_metadata( let view_functions = view_fns.iter().map(|view_fn| { let view_function_struct_ident = view_fn.view_function_struct_ident(); let name = &view_fn.name; - let args: Vec = vec![]; // todo + let args = view_fn.args.iter().filter_map(|fn_arg| { + match fn_arg { + syn::FnArg::Receiver(_) => None, + syn::FnArg::Typed(typed) => { + let pat = &typed.pat; + let ty = &typed.ty; + Some(quote::quote! { + #frame_support::__private::metadata_ir::ViewFunctionArgMetadataIR { + name: ::core::stringify!(#pat), + ty: #frame_support::__private::scale_info::meta_type::<#ty>(), + } + }) + } + } + }); let no_docs = vec![]; let doc = if cfg!(feature = "no-metadata-docs") { &no_docs } else { &view_fn.docs }; diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index bd805cf4c9e7..8a74507b2b3f 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -162,7 +162,7 @@ pub struct ViewFunctionMetadataIR { /// Query id. pub id: [u8; 32], /// Query args. - pub args: Vec>, + pub args: Vec>, /// Query output. pub output: T::Type, /// Query documentation. @@ -185,18 +185,18 @@ impl IntoPortable for ViewFunctionMetadataIR { /// Metadata of a runtime method argument. #[derive(Clone, PartialEq, Eq, Encode, Debug)] -pub struct QueryArgMetadataIR { +pub struct ViewFunctionArgMetadataIR { /// Query argument name. pub name: T::String, /// Query argument type. pub ty: T::Type, } -impl IntoPortable for QueryArgMetadataIR { - type Output = QueryArgMetadataIR; +impl IntoPortable for ViewFunctionArgMetadataIR { + type Output = ViewFunctionArgMetadataIR; fn into_portable(self, registry: &mut Registry) -> Self::Output { - QueryArgMetadataIR { + ViewFunctionArgMetadataIR { name: self.name.into_portable(registry), ty: registry.register_type(&self.ty), } From 5b4e2f1bf8d619fb10a837889cc46b2ed2ecd604 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Nov 2024 10:06:20 +0000 Subject: [PATCH 065/121] Docs --- .../support/procedural/src/pallet/parse/view_functions.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 98faecc2b706..7b0400726409 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -19,7 +19,7 @@ use frame_support_procedural_tools::get_doc_literals; use inflector::Inflector; use syn::spanned::Spanned; -/// todo: docs +/// Parsed representation of an impl block annotated with `pallet::view_functions_experimental`. pub struct ViewFunctionsImplDef { /// The where_clause used. pub where_clause: Option, @@ -69,6 +69,7 @@ impl ViewFunctionsImplDef { } } +/// Parsed representation of a view function definition. #[derive(Clone)] pub struct ViewFunctionDef { pub name: syn::Ident, From 18b7d6defa096c1daae3ba2ed234f6dae756bd73 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Nov 2024 12:08:49 +0000 Subject: [PATCH 066/121] Metadata tests and remove spaces from types. --- Cargo.lock | 3 + .../frame/examples/view-functions/Cargo.toml | 5 + .../frame/examples/view-functions/src/lib.rs | 1 - .../frame/examples/view-functions/src/mock.rs | 55 -------- .../examples/view-functions/src/tests.rs | 119 +++++++++++++++++- .../src/pallet/parse/view_functions.rs | 4 +- substrate/primitives/metadata-ir/src/types.rs | 10 +- substrate/primitives/metadata-ir/src/v15.rs | 9 +- 8 files changed, 136 insertions(+), 70 deletions(-) delete mode 100644 substrate/frame/examples/view-functions/src/mock.rs diff --git a/Cargo.lock b/Cargo.lock index 5164fffe92d4..a47dc550ac13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13369,13 +13369,16 @@ name = "pallet-example-view-functions" version = "1.0.0" dependencies = [ "frame-benchmarking 28.0.0", + "frame-metadata 16.0.0", "frame-support 28.0.0", "frame-system 28.0.0", "log", "parity-scale-codec", + "pretty_assertions", "scale-info", "sp-core 28.0.0", "sp-io 30.0.0", + "sp-metadata-ir 0.6.0", "sp-runtime 31.0.1", "sp-std 14.0.0", ] diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml index add2159e1f26..d94a0dd5159e 100644 --- a/substrate/frame/examples/view-functions/Cargo.toml +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -17,17 +17,22 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false } log = { workspace = true } scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } +frame-metadata = { features = ["current"], workspace = true } frame-support = { path = "../../support", default-features = false } frame-system = { path = "../../system", default-features = false } sp-io = { path = "../../../primitives/io", default-features = false } +sp-metadata-ir = { path = "../../../primitives/metadata-ir", default-features = false } sp-runtime = { path = "../../../primitives/runtime", default-features = false } sp-std = { path = "../../../primitives/std", default-features = false } sp-core = { default-features = false, path = "../../../primitives/core" } frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true } +[dev-dependencies] +pretty_assertions = { version = "1.3.0" } + [features] default = ["std"] std = [ diff --git a/substrate/frame/examples/view-functions/src/lib.rs b/substrate/frame/examples/view-functions/src/lib.rs index 8357fcf34f08..e842a718ad33 100644 --- a/substrate/frame/examples/view-functions/src/lib.rs +++ b/substrate/frame/examples/view-functions/src/lib.rs @@ -19,7 +19,6 @@ //! work. #![cfg_attr(not(feature = "std"), no_std)] -pub mod mock; pub mod tests; use frame_support::Parameter; diff --git a/substrate/frame/examples/view-functions/src/mock.rs b/substrate/frame/examples/view-functions/src/mock.rs deleted file mode 100644 index 549c613cb944..000000000000 --- a/substrate/frame/examples/view-functions/src/mock.rs +++ /dev/null @@ -1,55 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! Mock runtime for `view-functions-example` tests. -#![cfg(test)] - -use crate::{pallet, pallet2}; -use frame_support::derive_impl; -use sp_runtime::testing::TestXt; - -pub type AccountId = u32; -pub type Balance = u32; - -type Block = frame_system::mocking::MockBlock; -frame_support::construct_runtime!( - pub enum Runtime { - System: frame_system, - ViewFunctionsExample: pallet, - ViewFunctionsInstance: pallet2, - ViewFunctionsInstance1: pallet2::, - } -); - -pub type Extrinsic = TestXt; - -#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] -impl frame_system::Config for Runtime { - type Block = Block; -} - -impl pallet::Config for Runtime {} -impl pallet2::Config for Runtime {} - -impl pallet2::Config for Runtime {} - -pub fn new_test_ext() -> sp_io::TestExternalities { - use sp_runtime::BuildStorage; - - let t = RuntimeGenesisConfig { system: Default::default() }.build_storage().unwrap(); - t.into() -} diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 11d334819ec0..dbe3b144b99c 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -19,12 +19,60 @@ #![cfg(test)] use crate::{ - mock::*, pallet::{self, Pallet}, pallet2, }; use codec::{Decode, Encode}; -use frame_support::traits::ViewFunction; +use scale_info::{ + form::PortableForm, + meta_type, +}; + +use frame_metadata::RuntimeMetadata; +use frame_support::{ + derive_impl, + traits::ViewFunction, + pallet_prelude::PalletInfoAccess, +}; +use sp_io::hashing::twox_128; +use sp_runtime::testing::TestXt; +use sp_metadata_ir::{ + ViewFunctionGroupIR, + ViewFunctionMetadataIR, + ViewFunctionArgMetadataIR, +}; + +pub type AccountId = u32; +pub type Balance = u32; + +type Block = frame_system::mocking::MockBlock; +frame_support::construct_runtime!( + pub enum Runtime { + System: frame_system, + ViewFunctionsExample: pallet, + ViewFunctionsInstance: pallet2, + ViewFunctionsInstance1: pallet2::, + } +); + +pub type Extrinsic = TestXt; + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type Block = Block; +} + +impl pallet::Config for Runtime {} +impl pallet2::Config for Runtime {} + +impl pallet2::Config for Runtime {} + +pub fn new_test_ext() -> sp_io::TestExternalities { + use sp_runtime::BuildStorage; + + let t = RuntimeGenesisConfig { system: Default::default() }.build_storage().unwrap(); + t.into() +} #[test] fn pallet_get_value_query() { @@ -70,6 +118,73 @@ fn pallet_multiple_instances() { }); } +#[test] +fn metadata_ir_definitions() { + new_test_ext().execute_with(|| { + let metadata_ir = Runtime::metadata_ir(); + let pallet1 = metadata_ir.view_functions.groups.iter() + .find(|pallet| pallet.name == "ViewFunctionsExample").unwrap(); + + fn view_fn_id(preifx_hash: [u8; 16], view_fn_signature: &str) -> [u8; 32] { + let mut id = [0u8; 32]; + id[..16].copy_from_slice(&preifx_hash); + id[16..].copy_from_slice(&twox_128(view_fn_signature.as_bytes())); + id + } + + + let get_value_id = view_fn_id( + ::name_hash(), + "get_value() -> Option", + ); + + let get_value_with_arg_id = view_fn_id( + ::name_hash(), + "get_value_with_arg(u32) -> Option", + ); + + pretty_assertions::assert_eq!( + pallet1.view_functions, + vec![ + ViewFunctionMetadataIR { + name: "get_value", + id: get_value_id, + args: vec![], + output: meta_type::>(), + docs: vec![" Query value no args."], + }, + ViewFunctionMetadataIR { + name: "get_value_with_arg", + id: get_value_with_arg_id, + args: vec![ + ViewFunctionArgMetadataIR { + name: "key", + ty: meta_type::(), + }, + ], + output: meta_type::>(), + docs: vec![" Query value with args."], + }, + ] + ); + }); +} + +#[test] +fn metadata_encoded_to_custom_value() { + new_test_ext().execute_with(|| { ; + let metadata = sp_metadata_ir::into_latest(Runtime::metadata_ir()); + // metadata is currently experimental so lives as a custom value. + let frame_metadata::RuntimeMetadata::V15(v15) = metadata.1 else { + panic!("Expected metadata v15") + }; + let custom_value = v15.custom.map.get("view_functions_experimental").expect("Expected custom value"); + let view_function_groups: Vec> = + Decode::decode(&mut &custom_value.value[..]).unwrap(); + assert_eq!(view_function_groups.len(), 4); + }); +} + fn test_dispatch_view_function(query: &Q, expected: V) where Q: ViewFunction + Encode, diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 7b0400726409..51df02c7a6a8 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -113,14 +113,14 @@ impl ViewFunctionDef { .args_names_types() .1 .iter() - .map(|ty| quote::quote!(#ty).to_string()) + .map(|ty| quote::quote!(#ty).to_string().replace(" ", "")) .collect::>() .join(","); let return_type = &self.return_type; + let return_type = quote::quote!(#return_type).to_string().replace(" ", ""); let view_fn_signature = format!( "{view_function_name}({arg_types}) -> {return_type}", view_function_name = &self.name, - return_type = quote::quote!(#return_type), ); // hash the signature string diff --git a/substrate/primitives/metadata-ir/src/types.rs b/substrate/primitives/metadata-ir/src/types.rs index 8a74507b2b3f..97cefc11c844 100644 --- a/substrate/primitives/metadata-ir/src/types.rs +++ b/substrate/primitives/metadata-ir/src/types.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use codec::{Compact, Encode}; +use codec::{Compact, Decode, Encode}; use scale_info::{ form::{Form, MetaForm, PortableForm}, prelude::{collections::BTreeMap, vec::Vec}, @@ -121,7 +121,7 @@ impl IntoPortable for RuntimeApiMethodParamMetadataIR { } /// Metadata of the top level runtime view function dispatch. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)] pub struct RuntimeViewFunctionsIR { /// The type implementing the runtime query dispatch. pub ty: T::Type, @@ -132,7 +132,7 @@ pub struct RuntimeViewFunctionsIR { /// Metadata of a runtime view function group. /// /// For example, view functions associated with a pallet would form a view function group. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)] pub struct ViewFunctionGroupIR { /// Name of the view function group. pub name: T::String, @@ -155,7 +155,7 @@ impl IntoPortable for ViewFunctionGroupIR { } /// Metadata of a runtime view function. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)] pub struct ViewFunctionMetadataIR { /// Query name. pub name: T::String, @@ -184,7 +184,7 @@ impl IntoPortable for ViewFunctionMetadataIR { } /// Metadata of a runtime method argument. -#[derive(Clone, PartialEq, Eq, Encode, Debug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, Debug)] pub struct ViewFunctionArgMetadataIR { /// Query argument name. pub name: T::String, diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 5181e4b59aac..0420cf9422eb 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -40,15 +40,14 @@ impl From for RuntimeMetadataV15 { registry.map_into_portable(ir.apis.into_iter().map(Into::::into)); let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); - // todo: add tests. - let view_function_interfaces = + let view_function_groups = registry.map_into_portable(ir.view_functions.groups.into_iter()); let view_functions_custom_metadata = CustomValueMetadata { ty: ir.view_functions.ty, - value: codec::Encode::encode(&view_function_interfaces), + value: codec::Encode::encode(&view_function_groups), }; - let mut custom_map = scale_info::prelude::collections::BTreeMap::new(); - custom_map.insert("view_functions", view_functions_custom_metadata); + let mut custom_map = alloc::collections::BTreeMap::new(); + custom_map.insert("view_functions_experimental", view_functions_custom_metadata); let custom = CustomMetadata { map: custom_map }.into_portable(&mut registry); Self { types: registry.into(), pallets, extrinsic, ty, apis, outer_enums, custom } From 25479e22c7fcf27706264f5e709b9c45df890cd8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Nov 2024 12:10:15 +0000 Subject: [PATCH 067/121] Fmt --- .../examples/view-functions/src/tests.rs | 41 ++++++++----------- substrate/primitives/metadata-ir/src/v15.rs | 3 +- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index dbe3b144b99c..6e0101ed2094 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -23,24 +23,13 @@ use crate::{ pallet2, }; use codec::{Decode, Encode}; -use scale_info::{ - form::PortableForm, - meta_type, -}; +use scale_info::{form::PortableForm, meta_type}; use frame_metadata::RuntimeMetadata; -use frame_support::{ - derive_impl, - traits::ViewFunction, - pallet_prelude::PalletInfoAccess, -}; +use frame_support::{derive_impl, pallet_prelude::PalletInfoAccess, traits::ViewFunction}; use sp_io::hashing::twox_128; +use sp_metadata_ir::{ViewFunctionArgMetadataIR, ViewFunctionGroupIR, ViewFunctionMetadataIR}; use sp_runtime::testing::TestXt; -use sp_metadata_ir::{ - ViewFunctionGroupIR, - ViewFunctionMetadataIR, - ViewFunctionArgMetadataIR, -}; pub type AccountId = u32; pub type Balance = u32; @@ -122,8 +111,12 @@ fn pallet_multiple_instances() { fn metadata_ir_definitions() { new_test_ext().execute_with(|| { let metadata_ir = Runtime::metadata_ir(); - let pallet1 = metadata_ir.view_functions.groups.iter() - .find(|pallet| pallet.name == "ViewFunctionsExample").unwrap(); + let pallet1 = metadata_ir + .view_functions + .groups + .iter() + .find(|pallet| pallet.name == "ViewFunctionsExample") + .unwrap(); fn view_fn_id(preifx_hash: [u8; 16], view_fn_signature: &str) -> [u8; 32] { let mut id = [0u8; 32]; @@ -132,7 +125,6 @@ fn metadata_ir_definitions() { id } - let get_value_id = view_fn_id( ::name_hash(), "get_value() -> Option", @@ -156,12 +148,7 @@ fn metadata_ir_definitions() { ViewFunctionMetadataIR { name: "get_value_with_arg", id: get_value_with_arg_id, - args: vec![ - ViewFunctionArgMetadataIR { - name: "key", - ty: meta_type::(), - }, - ], + args: vec![ViewFunctionArgMetadataIR { name: "key", ty: meta_type::() },], output: meta_type::>(), docs: vec![" Query value with args."], }, @@ -172,13 +159,17 @@ fn metadata_ir_definitions() { #[test] fn metadata_encoded_to_custom_value() { - new_test_ext().execute_with(|| { ; + new_test_ext().execute_with(|| { let metadata = sp_metadata_ir::into_latest(Runtime::metadata_ir()); // metadata is currently experimental so lives as a custom value. let frame_metadata::RuntimeMetadata::V15(v15) = metadata.1 else { panic!("Expected metadata v15") }; - let custom_value = v15.custom.map.get("view_functions_experimental").expect("Expected custom value"); + let custom_value = v15 + .custom + .map + .get("view_functions_experimental") + .expect("Expected custom value"); let view_function_groups: Vec> = Decode::decode(&mut &custom_value.value[..]).unwrap(); assert_eq!(view_function_groups.len(), 4); diff --git a/substrate/primitives/metadata-ir/src/v15.rs b/substrate/primitives/metadata-ir/src/v15.rs index 0420cf9422eb..3bc9445819d8 100644 --- a/substrate/primitives/metadata-ir/src/v15.rs +++ b/substrate/primitives/metadata-ir/src/v15.rs @@ -40,8 +40,7 @@ impl From for RuntimeMetadataV15 { registry.map_into_portable(ir.apis.into_iter().map(Into::::into)); let outer_enums = Into::::into(ir.outer_enums).into_portable(&mut registry); - let view_function_groups = - registry.map_into_portable(ir.view_functions.groups.into_iter()); + let view_function_groups = registry.map_into_portable(ir.view_functions.groups.into_iter()); let view_functions_custom_metadata = CustomValueMetadata { ty: ir.view_functions.ty, value: codec::Encode::encode(&view_function_groups), From 15d74eaa3e6f3c0640fdfabf9fa0d7a67553aff1 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 16:29:05 +0100 Subject: [PATCH 068/121] fix `Cargo.toml` duplicate dependency --- substrate/frame/examples/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/frame/examples/Cargo.toml b/substrate/frame/examples/Cargo.toml index ce702e1084ca..40d6959378b8 100644 --- a/substrate/frame/examples/Cargo.toml +++ b/substrate/frame/examples/Cargo.toml @@ -27,7 +27,6 @@ pallet-example-single-block-migrations = { workspace = true } pallet-example-split = { workspace = true } pallet-example-tasks = { workspace = true } pallet-example-view-functions = { workspace = true } -pallet-example-authorization-tx-extension = { workspace = true } [features] default = ["std"] From 6a042fb50ad24539a796123e3ae93ebc5e05813f Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 16:42:51 +0100 Subject: [PATCH 069/121] format and fix toml files + Cargo.lock --- Cargo.lock | 2 +- Cargo.toml | 1 + cumulus/bin/pov-validator/Cargo.toml | 2 +- substrate/frame/examples/view-functions/Cargo.toml | 6 ++++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed2f96464fe2..92212f973290 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13499,7 +13499,7 @@ name = "pallet-example-view-functions" version = "1.0.0" dependencies = [ "frame-benchmarking 28.0.0", - "frame-metadata 16.0.0", + "frame-metadata 18.0.0", "frame-support 28.0.0", "frame-system 28.0.0", "log", diff --git a/Cargo.toml b/Cargo.toml index 7b86163fabc4..cdf947f1df5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -362,6 +362,7 @@ members = [ "substrate/frame/examples/single-block-migrations", "substrate/frame/examples/split", "substrate/frame/examples/tasks", + "substrate/frame/examples/view-functions", "substrate/frame/executive", "substrate/frame/fast-unstake", "substrate/frame/glutton", diff --git a/cumulus/bin/pov-validator/Cargo.toml b/cumulus/bin/pov-validator/Cargo.toml index d7af29a6bcb2..a919e3f68eac 100644 --- a/cumulus/bin/pov-validator/Cargo.toml +++ b/cumulus/bin/pov-validator/Cargo.toml @@ -19,8 +19,8 @@ sc-executor.workspace = true sp-core.workspace = true sp-io.workspace = true sp-maybe-compressed-blob.workspace = true -tracing-subscriber.workspace = true tracing.workspace = true +tracing-subscriber.workspace = true [lints] workspace = true diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml index d94a0dd5159e..628bca447a1d 100644 --- a/substrate/frame/examples/view-functions/Cargo.toml +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -15,18 +15,18 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false } +frame-metadata = { features = ["current"], workspace = true } log = { workspace = true } scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } -frame-metadata = { features = ["current"], workspace = true } frame-support = { path = "../../support", default-features = false } frame-system = { path = "../../system", default-features = false } +sp-core = { default-features = false, path = "../../../primitives/core" } sp-io = { path = "../../../primitives/io", default-features = false } sp-metadata-ir = { path = "../../../primitives/metadata-ir", default-features = false } sp-runtime = { path = "../../../primitives/runtime", default-features = false } sp-std = { path = "../../../primitives/std", default-features = false } -sp-core = { default-features = false, path = "../../../primitives/core" } frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true } @@ -46,6 +46,8 @@ std = [ "sp-io/std", "sp-runtime/std", "sp-std/std", + "sp-metadata-ir/std", + "frame-metadata/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", From c6a87fc32b1f4885ec82990c83114775e50a8df9 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 16:56:44 +0100 Subject: [PATCH 070/121] fix `Cargo.toml` of `pallet-example-view-functions` --- .../frame/examples/view-functions/Cargo.toml | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml index 628bca447a1d..4bc2bd7c39b3 100644 --- a/substrate/frame/examples/view-functions/Cargo.toml +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -4,6 +4,7 @@ version = "1.0.0" authors.workspace = true edition.workspace = true license.workspace = true +homepage.workspace = true repository.workspace = true description = "Pallet to demonstrate the usage of view functions to query pallet state" @@ -14,21 +15,21 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false } +codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, workspace = true } frame-metadata = { features = ["current"], workspace = true } log = { workspace = true } -scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } +scale-info = { version = "2.11.1", default-features = false, features = ["derive"], workspace = true } -frame-support = { path = "../../support", default-features = false } -frame-system = { path = "../../system", default-features = false } +frame-support = { path = "../../support", default-features = false, workspace = true } +frame-system = { path = "../../system", default-features = false, workspace = true } -sp-core = { default-features = false, path = "../../../primitives/core" } -sp-io = { path = "../../../primitives/io", default-features = false } -sp-metadata-ir = { path = "../../../primitives/metadata-ir", default-features = false } -sp-runtime = { path = "../../../primitives/runtime", default-features = false } -sp-std = { path = "../../../primitives/std", default-features = false } +sp-core = { default-features = false, path = "../../../primitives/core", workspace = true } +sp-io = { path = "../../../primitives/io", default-features = false, workspace = true } +sp-metadata-ir = { path = "../../../primitives/metadata-ir", default-features = false, workspace = true } +sp-runtime = { path = "../../../primitives/runtime", default-features = false, workspace = true } +sp-std = { path = "../../../primitives/std", default-features = false, workspace = true } -frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true } +frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true, workspace = true } [dev-dependencies] pretty_assertions = { version = "1.3.0" } From 9e92079d531063c58aa7087fc876fd179c2bdc7a Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 17:11:07 +0100 Subject: [PATCH 071/121] TOML lint + `cargo clippy` --- substrate/frame/examples/view-functions/Cargo.toml | 6 +++--- .../support/procedural/src/pallet/expand/view_functions.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml index 4bc2bd7c39b3..39e0b217a95f 100644 --- a/substrate/frame/examples/view-functions/Cargo.toml +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -39,16 +39,16 @@ default = ["std"] std = [ "codec/std", "frame-benchmarking?/std", + "frame-metadata/std", "frame-support/std", "frame-system/std", "log/std", "scale-info/std", "sp-core/std", "sp-io/std", + "sp-metadata-ir/std", "sp-runtime/std", "sp-std/std", - "sp-metadata-ir/std", - "frame-metadata/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -60,4 +60,4 @@ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime", -] \ No newline at end of file +] diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 3aa0b1c93e89..aede71df0be4 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -22,7 +22,7 @@ use syn::spanned::Spanned; pub fn expand_view_functions(def: &Def) -> TokenStream { let (span, where_clause, view_fns, docs) = match def.view_functions.as_ref() { Some(view_fns) => ( - view_fns.attr_span.clone(), + view_fns.attr_span, view_fns.where_clause.clone(), view_fns.view_functions.clone(), view_fns.docs.clone(), From c2e175a7804c2d826fe0152eb4456aa13f4c4030 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 17:14:47 +0100 Subject: [PATCH 072/121] add pallets to prdoc --- prdoc/pr_4722.prdoc | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index a7aaff5db8cc..f44ad667e729 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -9,10 +9,29 @@ doc: Read-only view functions can now be defined on pallets. These functions provide an interface for querying state, from both outside and inside the runtime. Common queries can be defined on pallets, without users having to access the storage directly. + - audience: Runtime User description: | Querying the runtime state is now easier with the introduction of pallet view functions. Clients can call commonly defined view functions rather than accessing the storage directly. These are similar to the Runtime APIs, but are defined within the runtime itself. -crates: [ ] +crates: +- name: frame-support + bump: major +- name: sp-core + bump: minor +- name: sp-api + bump: minor +- name: sp-metadata-ir + bump: minor +- name: frame-system + bump: major +- name: frame-support-procedural + bump: major +- name: westend-runtime + bump: minor +- name: pallet-example-view-functions + bump: major +- name: cumulus-pov-validator + bump: minor From cdca6f5c62acc09213573dadbee6a1458a424bc8 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 18:55:45 +0100 Subject: [PATCH 073/121] Add `RuntimeViewFunction` to `DefaultConfig` --- substrate/frame/support/procedural/examples/proc_main/main.rs | 2 ++ .../frame/support/procedural/examples/proc_main/runtime.rs | 3 ++- substrate/frame/support/test/tests/pallet.rs | 3 ++- substrate/frame/support/test/tests/runtime_legacy_ordering.rs | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 4bdfc76dd92f..f742b19e64d3 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,6 +58,8 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + #[inject_runtime_type] + type RuntimeViewFunction = (); type DbWeight = (); } } diff --git a/substrate/frame/support/procedural/examples/proc_main/runtime.rs b/substrate/frame/support/procedural/examples/proc_main/runtime.rs index 109ca4f6dc48..8de560555895 100644 --- a/substrate/frame/support/procedural/examples/proc_main/runtime.rs +++ b/substrate/frame/support/procedural/examples/proc_main/runtime.rs @@ -99,7 +99,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index 68cda6009f9b..e45ff64e4c26 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -830,7 +830,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs index 7b92073a82b1..1594356ad8fe 100644 --- a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs +++ b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs @@ -296,7 +296,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; From e71d7d7e630ee2b907d8940cca0a65a94e1eaca2 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 7 Jan 2025 19:35:35 +0100 Subject: [PATCH 074/121] Add `RuntimeViewFunction`s to runtime config --- cumulus/pallets/weight-reclaim/src/tests.rs | 3 ++- substrate/frame/support/src/dispatch.rs | 1 + .../test/tests/runtime_ui/invalid_runtime_type_derive.stderr | 2 +- templates/parachain/pallets/template/src/mock.rs | 3 ++- templates/parachain/runtime/src/lib.rs | 3 ++- templates/solochain/pallets/template/src/mock.rs | 3 ++- 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cumulus/pallets/weight-reclaim/src/tests.rs b/cumulus/pallets/weight-reclaim/src/tests.rs index b87c107c7ec7..ce647445b332 100644 --- a/cumulus/pallets/weight-reclaim/src/tests.rs +++ b/cumulus/pallets/weight-reclaim/src/tests.rs @@ -89,7 +89,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index daa670d3131e..81b020584fe6 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -1374,6 +1374,7 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; + type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr index 0b128c3dd457..daa6721ff051 100644 --- a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr +++ b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr @@ -1,4 +1,4 @@ -error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask` +error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask`, `RuntimeViewFunction` --> tests/runtime_ui/invalid_runtime_type_derive.rs:21:23 | 21 | #[runtime::derive(RuntimeInfo)] diff --git a/templates/parachain/pallets/template/src/mock.rs b/templates/parachain/pallets/template/src/mock.rs index b924428d4145..3eeb9604f015 100644 --- a/templates/parachain/pallets/template/src/mock.rs +++ b/templates/parachain/pallets/template/src/mock.rs @@ -18,7 +18,8 @@ mod test_runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; diff --git a/templates/parachain/runtime/src/lib.rs b/templates/parachain/runtime/src/lib.rs index 0be27ecce739..f312e9f80192 100644 --- a/templates/parachain/runtime/src/lib.rs +++ b/templates/parachain/runtime/src/lib.rs @@ -262,7 +262,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/solochain/pallets/template/src/mock.rs b/templates/solochain/pallets/template/src/mock.rs index 1b86cd9b7709..44085bc3bff1 100644 --- a/templates/solochain/pallets/template/src/mock.rs +++ b/templates/solochain/pallets/template/src/mock.rs @@ -18,7 +18,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; From 05f7caade6a78e9c2163807f6b2284eedabd64a9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 8 Jan 2025 10:25:30 +0000 Subject: [PATCH 075/121] Spelling --- substrate/primitives/api/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index a366100f246b..75acd2dc1cae 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -845,7 +845,7 @@ decl_runtime_apis! { fn metadata_versions() -> alloc::vec::Vec; } - /// API for executing view function queriess + /// API for executing view functions pub trait RuntimeViewFunction where { /// Execute a view function query. fn execute_view_function(query_id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError>; From 7a82560a5c7e483248d4c4e437c7d61b01214e30 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 11:32:48 +0100 Subject: [PATCH 076/121] fix `RuntimeViewFunction` --- .../src/reference_docs/chain_spec_runtime/src/runtime.rs | 9 ++++++++- .../frame/support/procedural/examples/proc_main/main.rs | 5 ++++- templates/solochain/runtime/src/configs/mod.rs | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs index 282fc1ff489c..3ffa3d61263f 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs @@ -57,7 +57,14 @@ type SignedExtra = (); mod runtime { /// The main runtime type. #[runtime::runtime] - #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeTask, + RuntimeViewFunction + )] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index f742b19e64d3..14544ed1aa52 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -82,6 +82,8 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] + type RuntimeViewFunction; + #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -236,7 +238,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/configs/mod.rs b/templates/solochain/runtime/src/configs/mod.rs index e34b3cb82158..21b795808299 100644 --- a/templates/solochain/runtime/src/configs/mod.rs +++ b/templates/solochain/runtime/src/configs/mod.rs @@ -42,7 +42,7 @@ use sp_version::RuntimeVersion; use super::{ AccountId, Aura, Balance, Balances, Block, BlockNumber, Hash, Nonce, PalletInfo, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, - System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, + RuntimeViewFunction, System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, }; const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); From 87807aae9b44b284bc2c7878ce4945932c22a228 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 12:17:07 +0100 Subject: [PATCH 077/121] fix --- .../frame/support/procedural/examples/proc_main/main.rs | 4 ++-- substrate/frame/support/src/tests/mod.rs | 5 +++-- templates/parachain/runtime/src/configs/mod.rs | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 14544ed1aa52..957d32a46314 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,9 +58,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); - type DbWeight = (); } } @@ -82,7 +82,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction; + type RuntimeViewFunction: crate::traits::DispatchViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 4ae3574352bd..5fa576b5234e 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -51,9 +51,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); - type DbWeight = (); } } @@ -241,7 +241,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index 1e9155f59a57..c86cb264e1ab 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,9 +61,10 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, - System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, - MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, + Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, + EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, + SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; From d3bf83cebccb18abae3630e013afe075569ced7a Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 12:48:07 +0100 Subject: [PATCH 078/121] undo changes --- .../src/reference_docs/chain_spec_runtime/src/runtime.rs | 9 +-------- substrate/frame/support/src/dispatch.rs | 1 - .../frame/support/test/tests/runtime_legacy_ordering.rs | 3 +-- substrate/frame/system/src/lib.rs | 2 -- 4 files changed, 2 insertions(+), 13 deletions(-) diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs index 3ffa3d61263f..282fc1ff489c 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs @@ -57,14 +57,7 @@ type SignedExtra = (); mod runtime { /// The main runtime type. #[runtime::runtime] - #[runtime::derive( - RuntimeCall, - RuntimeEvent, - RuntimeError, - RuntimeOrigin, - RuntimeTask, - RuntimeViewFunction - )] + #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index 81b020584fe6..daa670d3131e 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -1374,7 +1374,6 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs index 1594356ad8fe..7b92073a82b1 100644 --- a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs +++ b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs @@ -296,8 +296,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 17648fc97313..ea0ca4415219 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -335,8 +335,6 @@ pub mod pallet { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - #[inject_runtime_type] - type RuntimeViewFunction = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = TestBlockHashCount>; type OnSetCode = (); From ccbac76bed654557ef62ad7d12a11b1ddc93255a Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:11:38 +0100 Subject: [PATCH 079/121] Revert "undo changes" This reverts commit d3bf83cebccb18abae3630e013afe075569ced7a. --- .../src/reference_docs/chain_spec_runtime/src/runtime.rs | 9 ++++++++- substrate/frame/support/src/dispatch.rs | 1 + .../frame/support/test/tests/runtime_legacy_ordering.rs | 3 ++- substrate/frame/system/src/lib.rs | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs index 282fc1ff489c..3ffa3d61263f 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs @@ -57,7 +57,14 @@ type SignedExtra = (); mod runtime { /// The main runtime type. #[runtime::runtime] - #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeTask, + RuntimeViewFunction + )] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index daa670d3131e..81b020584fe6 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -1374,6 +1374,7 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; + type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs index 7b92073a82b1..1594356ad8fe 100644 --- a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs +++ b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs @@ -296,7 +296,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index ea0ca4415219..17648fc97313 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -335,6 +335,8 @@ pub mod pallet { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + #[inject_runtime_type] + type RuntimeViewFunction = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = TestBlockHashCount>; type OnSetCode = (); From 03c1c459a5ecd0866568fde30b902a1b490d0616 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:17:28 +0100 Subject: [PATCH 080/121] revert last 3 commits --- .../frame/support/procedural/examples/proc_main/main.rs | 4 ++-- substrate/frame/support/src/tests/mod.rs | 5 ++--- templates/parachain/runtime/src/configs/mod.rs | 7 +++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 957d32a46314..14544ed1aa52 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,9 +58,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); + type DbWeight = (); } } @@ -82,7 +82,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction: crate::traits::DispatchViewFunction; + type RuntimeViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 5fa576b5234e..4ae3574352bd 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -51,9 +51,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); + type DbWeight = (); } } @@ -241,8 +241,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index c86cb264e1ab..1e9155f59a57 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,10 +61,9 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, - Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, - EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, - SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, + System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, + MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; From d513cd14eed71ac63ab12efa8880ce5aad375d77 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:18:16 +0100 Subject: [PATCH 081/121] revert last commits --- .../frame/support/procedural/examples/proc_main/main.rs | 4 ++-- substrate/frame/support/src/tests/mod.rs | 5 +++-- templates/parachain/runtime/src/configs/mod.rs | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 14544ed1aa52..957d32a46314 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,9 +58,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); - type DbWeight = (); } } @@ -82,7 +82,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction; + type RuntimeViewFunction: crate::traits::DispatchViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 4ae3574352bd..5fa576b5234e 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -51,9 +51,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); - type DbWeight = (); } } @@ -241,7 +241,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index 1e9155f59a57..c86cb264e1ab 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,9 +61,10 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, - System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, - MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, + Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, + EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, + SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; From 627c275342dcba04389af24d25f422d2ba70291c Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:19:11 +0100 Subject: [PATCH 082/121] Revert "fix" This reverts commit 87807aae9b44b284bc2c7878ce4945932c22a228. --- .../frame/support/procedural/examples/proc_main/main.rs | 4 ++-- substrate/frame/support/src/tests/mod.rs | 5 ++--- templates/parachain/runtime/src/configs/mod.rs | 7 +++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 957d32a46314..14544ed1aa52 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,9 +58,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); + type DbWeight = (); } } @@ -82,7 +82,7 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction: crate::traits::DispatchViewFunction; + type RuntimeViewFunction; #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 5fa576b5234e..4ae3574352bd 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -51,9 +51,9 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - type DbWeight = (); #[inject_runtime_type] type RuntimeViewFunction = (); + type DbWeight = (); } } @@ -241,8 +241,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index c86cb264e1ab..1e9155f59a57 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,10 +61,9 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, - Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, - EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, - SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, + System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, + MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; From 227deb667c1538926d7d373fde34cfcec1438e9b Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:19:32 +0100 Subject: [PATCH 083/121] Revert "fix `RuntimeViewFunction`" This reverts commit 7a82560a5c7e483248d4c4e437c7d61b01214e30. --- .../src/reference_docs/chain_spec_runtime/src/runtime.rs | 9 +-------- .../frame/support/procedural/examples/proc_main/main.rs | 5 +---- templates/solochain/runtime/src/configs/mod.rs | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs index 3ffa3d61263f..282fc1ff489c 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs @@ -57,14 +57,7 @@ type SignedExtra = (); mod runtime { /// The main runtime type. #[runtime::runtime] - #[runtime::derive( - RuntimeCall, - RuntimeEvent, - RuntimeError, - RuntimeOrigin, - RuntimeTask, - RuntimeViewFunction - )] + #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 14544ed1aa52..f742b19e64d3 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -82,8 +82,6 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction; - #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -238,8 +236,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/configs/mod.rs b/templates/solochain/runtime/src/configs/mod.rs index 21b795808299..e34b3cb82158 100644 --- a/templates/solochain/runtime/src/configs/mod.rs +++ b/templates/solochain/runtime/src/configs/mod.rs @@ -42,7 +42,7 @@ use sp_version::RuntimeVersion; use super::{ AccountId, Aura, Balance, Balances, Block, BlockNumber, Hash, Nonce, PalletInfo, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, - RuntimeViewFunction, System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, + System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, }; const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); From d234fdd4d33930c895dfcff07cd45b4854cc7203 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:20:47 +0100 Subject: [PATCH 084/121] Revert "Add `RuntimeViewFunction`s to runtime config" This reverts commit e71d7d7e630ee2b907d8940cca0a65a94e1eaca2. --- cumulus/pallets/weight-reclaim/src/tests.rs | 3 +-- substrate/frame/support/src/dispatch.rs | 1 - .../test/tests/runtime_ui/invalid_runtime_type_derive.stderr | 2 +- templates/parachain/pallets/template/src/mock.rs | 3 +-- templates/parachain/runtime/src/lib.rs | 3 +-- templates/solochain/pallets/template/src/mock.rs | 3 +-- 6 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cumulus/pallets/weight-reclaim/src/tests.rs b/cumulus/pallets/weight-reclaim/src/tests.rs index ce647445b332..b87c107c7ec7 100644 --- a/cumulus/pallets/weight-reclaim/src/tests.rs +++ b/cumulus/pallets/weight-reclaim/src/tests.rs @@ -89,8 +89,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Test; diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index 81b020584fe6..daa670d3131e 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -1374,7 +1374,6 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr index daa6721ff051..0b128c3dd457 100644 --- a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr +++ b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr @@ -1,4 +1,4 @@ -error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask`, `RuntimeViewFunction` +error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask` --> tests/runtime_ui/invalid_runtime_type_derive.rs:21:23 | 21 | #[runtime::derive(RuntimeInfo)] diff --git a/templates/parachain/pallets/template/src/mock.rs b/templates/parachain/pallets/template/src/mock.rs index 3eeb9604f015..b924428d4145 100644 --- a/templates/parachain/pallets/template/src/mock.rs +++ b/templates/parachain/pallets/template/src/mock.rs @@ -18,8 +18,7 @@ mod test_runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Test; diff --git a/templates/parachain/runtime/src/lib.rs b/templates/parachain/runtime/src/lib.rs index f312e9f80192..0be27ecce739 100644 --- a/templates/parachain/runtime/src/lib.rs +++ b/templates/parachain/runtime/src/lib.rs @@ -262,8 +262,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/templates/solochain/pallets/template/src/mock.rs b/templates/solochain/pallets/template/src/mock.rs index 44085bc3bff1..1b86cd9b7709 100644 --- a/templates/solochain/pallets/template/src/mock.rs +++ b/templates/solochain/pallets/template/src/mock.rs @@ -18,8 +18,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Test; From 68061a9bdc4e4059c0038031f5a5db1fdf78d447 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 13:21:05 +0100 Subject: [PATCH 085/121] Revert "Add `RuntimeViewFunction` to `DefaultConfig`" This reverts commit cdca6f5c62acc09213573dadbee6a1458a424bc8. --- substrate/frame/support/procedural/examples/proc_main/main.rs | 2 -- .../frame/support/procedural/examples/proc_main/runtime.rs | 3 +-- substrate/frame/support/test/tests/pallet.rs | 3 +-- substrate/frame/support/test/tests/runtime_legacy_ordering.rs | 3 +-- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index f742b19e64d3..4bdfc76dd92f 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,8 +58,6 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - #[inject_runtime_type] - type RuntimeViewFunction = (); type DbWeight = (); } } diff --git a/substrate/frame/support/procedural/examples/proc_main/runtime.rs b/substrate/frame/support/procedural/examples/proc_main/runtime.rs index 8de560555895..109ca4f6dc48 100644 --- a/substrate/frame/support/procedural/examples/proc_main/runtime.rs +++ b/substrate/frame/support/procedural/examples/proc_main/runtime.rs @@ -99,8 +99,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index e45ff64e4c26..68cda6009f9b 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -830,8 +830,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs index 1594356ad8fe..7b92073a82b1 100644 --- a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs +++ b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs @@ -296,8 +296,7 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask, - RuntimeViewFunction + RuntimeTask )] pub struct Runtime; From bab4cb049ff17a41dc101f88f038475112503bbf Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 8 Jan 2025 15:37:52 +0100 Subject: [PATCH 086/121] fix toml formatting --- cumulus/bin/pov-validator/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/bin/pov-validator/Cargo.toml b/cumulus/bin/pov-validator/Cargo.toml index a919e3f68eac..d7af29a6bcb2 100644 --- a/cumulus/bin/pov-validator/Cargo.toml +++ b/cumulus/bin/pov-validator/Cargo.toml @@ -19,8 +19,8 @@ sc-executor.workspace = true sp-core.workspace = true sp-io.workspace = true sp-maybe-compressed-blob.workspace = true -tracing.workspace = true tracing-subscriber.workspace = true +tracing.workspace = true [lints] workspace = true From 7b853880abb780b1aac14fd6d328b72cda4e31c9 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Thu, 9 Jan 2025 19:38:29 +0100 Subject: [PATCH 087/121] Add `RuntimeViewFunction` type to all runtime configs --- cumulus/pallets/weight-reclaim/src/tests.rs | 3 ++- .../src/reference_docs/chain_spec_runtime/src/runtime.rs | 9 ++++++++- .../frame/support/procedural/examples/proc_main/main.rs | 7 ++++++- .../support/procedural/examples/proc_main/runtime.rs | 3 ++- substrate/frame/support/src/dispatch.rs | 1 + substrate/frame/support/src/tests/mod.rs | 3 ++- substrate/frame/support/test/tests/pallet.rs | 3 ++- substrate/frame/support/test/tests/runtime.rs | 3 ++- .../frame/support/test/tests/runtime_legacy_ordering.rs | 3 ++- .../tests/runtime_ui/invalid_runtime_type_derive.stderr | 2 +- templates/parachain/pallets/template/src/mock.rs | 3 ++- templates/parachain/runtime/src/configs/mod.rs | 7 ++++--- templates/parachain/runtime/src/lib.rs | 3 ++- templates/solochain/runtime/src/configs/mod.rs | 2 +- 14 files changed, 37 insertions(+), 15 deletions(-) diff --git a/cumulus/pallets/weight-reclaim/src/tests.rs b/cumulus/pallets/weight-reclaim/src/tests.rs index b87c107c7ec7..ce647445b332 100644 --- a/cumulus/pallets/weight-reclaim/src/tests.rs +++ b/cumulus/pallets/weight-reclaim/src/tests.rs @@ -89,7 +89,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs index 282fc1ff489c..3ffa3d61263f 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs @@ -57,7 +57,14 @@ type SignedExtra = (); mod runtime { /// The main runtime type. #[runtime::runtime] - #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeTask, + RuntimeViewFunction + )] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index 4bdfc76dd92f..aea4e0562a2f 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,6 +58,8 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); + #[inject_runtime_type] + type RuntimeViewFunction = (); type DbWeight = (); } } @@ -80,6 +82,8 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] + type RuntimeViewFunction: crate::traits::DispatchViewFunction; + #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -234,7 +238,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/procedural/examples/proc_main/runtime.rs b/substrate/frame/support/procedural/examples/proc_main/runtime.rs index 109ca4f6dc48..8de560555895 100644 --- a/substrate/frame/support/procedural/examples/proc_main/runtime.rs +++ b/substrate/frame/support/procedural/examples/proc_main/runtime.rs @@ -99,7 +99,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index daa670d3131e..81b020584fe6 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -1374,6 +1374,7 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; + type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 4ae3574352bd..0d3475f9bfcd 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -241,7 +241,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/pallet.rs b/substrate/frame/support/test/tests/pallet.rs index 68cda6009f9b..e45ff64e4c26 100644 --- a/substrate/frame/support/test/tests/pallet.rs +++ b/substrate/frame/support/test/tests/pallet.rs @@ -830,7 +830,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/runtime.rs b/substrate/frame/support/test/tests/runtime.rs index 5335e08837e4..cbcdf8d27b39 100644 --- a/substrate/frame/support/test/tests/runtime.rs +++ b/substrate/frame/support/test/tests/runtime.rs @@ -296,7 +296,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs index 7b92073a82b1..1594356ad8fe 100644 --- a/substrate/frame/support/test/tests/runtime_legacy_ordering.rs +++ b/substrate/frame/support/test/tests/runtime_legacy_ordering.rs @@ -296,7 +296,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr index 0b128c3dd457..daa6721ff051 100644 --- a/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr +++ b/substrate/frame/support/test/tests/runtime_ui/invalid_runtime_type_derive.stderr @@ -1,4 +1,4 @@ -error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask` +error: expected one of: `RuntimeCall`, `RuntimeEvent`, `RuntimeError`, `RuntimeOrigin`, `RuntimeFreezeReason`, `RuntimeHoldReason`, `RuntimeSlashReason`, `RuntimeLockId`, `RuntimeTask`, `RuntimeViewFunction` --> tests/runtime_ui/invalid_runtime_type_derive.rs:21:23 | 21 | #[runtime::derive(RuntimeInfo)] diff --git a/templates/parachain/pallets/template/src/mock.rs b/templates/parachain/pallets/template/src/mock.rs index b924428d4145..3eeb9604f015 100644 --- a/templates/parachain/pallets/template/src/mock.rs +++ b/templates/parachain/pallets/template/src/mock.rs @@ -18,7 +18,8 @@ mod test_runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index 1e9155f59a57..c86cb264e1ab 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,9 +61,10 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, - System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, - MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, + Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, + EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, + SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; diff --git a/templates/parachain/runtime/src/lib.rs b/templates/parachain/runtime/src/lib.rs index 0be27ecce739..f312e9f80192 100644 --- a/templates/parachain/runtime/src/lib.rs +++ b/templates/parachain/runtime/src/lib.rs @@ -262,7 +262,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Runtime; diff --git a/templates/solochain/runtime/src/configs/mod.rs b/templates/solochain/runtime/src/configs/mod.rs index e34b3cb82158..21b795808299 100644 --- a/templates/solochain/runtime/src/configs/mod.rs +++ b/templates/solochain/runtime/src/configs/mod.rs @@ -42,7 +42,7 @@ use sp_version::RuntimeVersion; use super::{ AccountId, Aura, Balance, Balances, Block, BlockNumber, Hash, Nonce, PalletInfo, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, - System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, + RuntimeViewFunction, System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, }; const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); From f176933f2c9bc7b853fb258e1898b370f734e5df Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 10 Jan 2025 07:44:42 +0100 Subject: [PATCH 088/121] add missing `RuntimeViewFunction` --- templates/solochain/pallets/template/src/mock.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/solochain/pallets/template/src/mock.rs b/templates/solochain/pallets/template/src/mock.rs index 1b86cd9b7709..44085bc3bff1 100644 --- a/templates/solochain/pallets/template/src/mock.rs +++ b/templates/solochain/pallets/template/src/mock.rs @@ -18,7 +18,8 @@ mod runtime { RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, - RuntimeTask + RuntimeTask, + RuntimeViewFunction )] pub struct Test; From 502c5be4147d8fd1951aab7444d14cd297c75980 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 10 Jan 2025 17:24:35 +0100 Subject: [PATCH 089/121] small fix --- substrate/frame/support/test/src/lib.rs | 2 ++ .../tests/derive_impl_ui/inject_runtime_type_invalid.stderr | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/substrate/frame/support/test/src/lib.rs b/substrate/frame/support/test/src/lib.rs index b080740b0a4b..15897193f042 100644 --- a/substrate/frame/support/test/src/lib.rs +++ b/substrate/frame/support/test/src/lib.rs @@ -52,6 +52,8 @@ pub mod pallet { type RuntimeCall; /// Contains an aggregation of all tasks in this runtime. type RuntimeTask; + /// Type for dispatching queries. + type RuntimeViewFunction; /// The runtime event type. type RuntimeEvent: Parameter + Member diff --git a/substrate/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr b/substrate/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr index c7159b34afb3..aafc6b5a2c87 100644 --- a/substrate/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr +++ b/substrate/frame/support/test/tests/derive_impl_ui/inject_runtime_type_invalid.stderr @@ -1,4 +1,4 @@ -error: `#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeTask`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo` +error: `#[inject_runtime_type]` can only be attached to `RuntimeCall`, `RuntimeEvent`, `RuntimeTask`, `RuntimeViewFunction`, `RuntimeOrigin`, `RuntimeParameters` or `PalletInfo` --> tests/derive_impl_ui/inject_runtime_type_invalid.rs:32:5 | 32 | type RuntimeInfo = (); From c44d40e3a0b42c17a0b1b396689661063ff219fa Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 13:38:00 +0100 Subject: [PATCH 090/121] Propagate runtime attributes to view function macros --- .../src/construct_runtime/expand/metadata.rs | 9 +++++++++ .../src/construct_runtime/expand/view_function.rs | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 2a573a967fbf..67648cbbf326 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -82,8 +82,17 @@ pub fn expand_runtime_metadata( let name = &decl.name; let path = &decl.path; let instance = decl.instance.as_ref().into_iter(); + let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { + let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) + .expect("was successfully parsed before; qed"); + quote! { + #acc + #attr + } + }); quote! { + #attr #path::Pallet::<#runtime #(, #path::#instance)*>::pallet_view_functions_metadata( ::core::stringify!(#name) ) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index 9e0b13aa9125..687194b3d4b3 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -16,6 +16,7 @@ // limitations under the License use crate::construct_runtime::Pallet; +use core::str::FromStr; use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; /// Expands implementation of runtime level `DispatchViewFunction`. @@ -28,7 +29,16 @@ pub fn expand_outer_query( let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; + let attr = pallet.cfg_pattern.iter().fold(TokenStream2::new(), |acc, pattern| { + let attr = TokenStream2::from_str(&format!("#[cfg({})]", pattern.original())) + .expect("was successfully parsed before; qed"); + quote::quote! { + #acc + #attr + } + }); quote::quote! { + #attr if id.prefix == <#pallet_name as #scrate::traits::ViewFunctionIdPrefix>::prefix() { return <#pallet_name as #scrate::traits::DispatchViewFunction>::dispatch_view_function(id, input, output) } From 0819dac3c8c93505245fb4b83928fb5ca9e3d5a3 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 13:49:46 +0100 Subject: [PATCH 091/121] nit --- substrate/frame/examples/view-functions/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 6e0101ed2094..51c35f4924cc 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -25,7 +25,6 @@ use crate::{ use codec::{Decode, Encode}; use scale_info::{form::PortableForm, meta_type}; -use frame_metadata::RuntimeMetadata; use frame_support::{derive_impl, pallet_prelude::PalletInfoAccess, traits::ViewFunction}; use sp_io::hashing::twox_128; use sp_metadata_ir::{ViewFunctionArgMetadataIR, ViewFunctionGroupIR, ViewFunctionMetadataIR}; From 9793b2685ed8ef0e319d6ebadc14f342a52078f5 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 14:46:46 +0100 Subject: [PATCH 092/121] update errors msg in tests with view function errors --- .../deprecated_where_block.stderr | 34 +++++++++++++++++++ .../invalid_module_details_keyword.stderr | 2 +- .../invalid_module_entry.stderr | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/deprecated_where_block.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/deprecated_where_block.stderr index 726b09cf54c9..faa9cb558c26 100644 --- a/substrate/frame/support/test/tests/construct_runtime_ui/deprecated_where_block.stderr +++ b/substrate/frame/support/test/tests/construct_runtime_ui/deprecated_where_block.stderr @@ -561,6 +561,15 @@ note: the trait `Config` must be implemented | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this error originates in the macro `frame_support::construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `Runtime: Config` is not satisfied + --> tests/construct_runtime_ui/deprecated_where_block.rs:26:3 + | +26 | System: frame_system::{Pallet, Call, Storage, Config, Event}, + | ^^^^^^ the trait `Config` is not implemented for `Runtime`, which is required by `Pallet: ViewFunctionIdPrefix` + | + = help: the trait `ViewFunctionIdPrefix` is implemented for `Pallet` + = note: required for `Pallet` to implement `ViewFunctionIdPrefix` + error[E0599]: the function or associated item `storage_metadata` exists for struct `Pallet`, but its trait bounds were not satisfied --> tests/construct_runtime_ui/deprecated_where_block.rs:20:1 | @@ -736,6 +745,31 @@ note: the trait `Config` must be implemented | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this error originates in the macro `frame_support::construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0599]: the function or associated item `pallet_view_functions_metadata` exists for struct `Pallet`, but its trait bounds were not satisfied + --> tests/construct_runtime_ui/deprecated_where_block.rs:20:1 + | +20 | construct_runtime! { + | __^ + | | _| + | || +21 | || pub struct Runtime where + | ||______________________- doesn't satisfy `Runtime: Config` +22 | | Block = Block, +23 | | NodeBlock = Block, +... | +27 | | } +28 | | } + | |__^ function or associated item cannot be called on `Pallet` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `Runtime: Config` +note: the trait `Config` must be implemented + --> $WORKSPACE/substrate/frame/system/src/lib.rs + | + | pub trait Config: 'static + Eq + Clone { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `frame_support::construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `Runtime: Config` is not satisfied --> tests/construct_runtime_ui/deprecated_where_block.rs:20:1 | diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr index feb61793151d..8feac638630a 100644 --- a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr +++ b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr @@ -1,4 +1,4 @@ -error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason` +error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason`, `Query` --> tests/construct_runtime_ui/invalid_module_details_keyword.rs:23:20 | 23 | system: System::{enum}, diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr index 97943dfc1763..d7b5f3868e9a 100644 --- a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr +++ b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr @@ -1,4 +1,4 @@ -error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason` +error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason`, `Query` --> tests/construct_runtime_ui/invalid_module_entry.rs:24:23 | 24 | Balance: balances::{Unexpected}, From cf064414f6dceb857d21eb2068b8158e52f89d8e Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 14:55:03 +0100 Subject: [PATCH 093/121] fix prdoc --- prdoc/pr_4722.prdoc | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index f44ad667e729..ebe9fcc1ae6c 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -17,21 +17,21 @@ doc: are defined within the runtime itself. crates: -- name: frame-support - bump: major -- name: sp-core - bump: minor -- name: sp-api - bump: minor -- name: sp-metadata-ir - bump: minor -- name: frame-system - bump: major -- name: frame-support-procedural - bump: major -- name: westend-runtime - bump: minor -- name: pallet-example-view-functions - bump: major -- name: cumulus-pov-validator - bump: minor + - name: frame-support + bump: major + - name: sp-core + bump: minor + - name: sp-api + bump: minor + - name: sp-metadata-ir + bump: minor + - name: frame-system + bump: major + - name: frame-support-procedural + bump: major + - name: westend-runtime + bump: minor + - name: pallet-example-view-functions + bump: major + - name: cumulus-pov-validator + bump: minor From a372eecdd97e000f43d328dc87e200b982deb3ac Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 15:11:27 +0100 Subject: [PATCH 094/121] prdoc nit --- prdoc/pr_4722.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index ebe9fcc1ae6c..4989478d7cca 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -34,4 +34,4 @@ crates: - name: pallet-example-view-functions bump: major - name: cumulus-pov-validator - bump: minor + bump: minor \ No newline at end of file From e0758a23e84968fa52868a8ec982b1b98a417d00 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 13 Jan 2025 17:40:22 +0100 Subject: [PATCH 095/121] fix prdoc bumps --- prdoc/pr_4722.prdoc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index 4989478d7cca..8f0f1c9866e6 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -18,20 +18,22 @@ doc: crates: - name: frame-support - bump: major + bump: minor + - name: frame-system + bump: minor - name: sp-core bump: minor - name: sp-api bump: minor - name: sp-metadata-ir - bump: minor - - name: frame-system bump: major - name: frame-support-procedural - bump: major + bump: patch - name: westend-runtime bump: minor - name: pallet-example-view-functions - bump: major + bump: patch - name: cumulus-pov-validator - bump: minor \ No newline at end of file + bump: none + - name: cumulus-pallet-weight-reclaim + bump: none \ No newline at end of file From 73b41aa0a35d48b5a0eaf236497b0756efa0bd74 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 14 Jan 2025 16:24:55 +0100 Subject: [PATCH 096/121] fix semver bumps --- prdoc/pr_4722.prdoc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index 8f0f1c9866e6..402bcd35e065 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -20,7 +20,7 @@ crates: - name: frame-support bump: minor - name: frame-system - bump: minor + bump: major - name: sp-core bump: minor - name: sp-api @@ -29,11 +29,11 @@ crates: bump: major - name: frame-support-procedural bump: patch - - name: westend-runtime - bump: minor - name: pallet-example-view-functions bump: patch - name: cumulus-pov-validator bump: none - name: cumulus-pallet-weight-reclaim - bump: none \ No newline at end of file + bump: minor + - name: westend-runtime + bump: minor \ No newline at end of file From 9b62146adca757c815d5db41ccca6f8e739e5f53 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 14 Jan 2025 18:03:33 +0100 Subject: [PATCH 097/121] Add `wasm32-unknown-unknown` target in `check-semver` CI --- .github/workflows/check-semver.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check-semver.yml b/.github/workflows/check-semver.yml index 0da3e54ef60b..d063a89f852e 100644 --- a/.github/workflows/check-semver.yml +++ b/.github/workflows/check-semver.yml @@ -76,6 +76,7 @@ jobs: if: ${{ !contains(github.event.pull_request.labels.*.name, 'R0-silent') }} run: | rustup default $TOOLCHAIN + rustup target add wasm32-unknown-unknown --toolchain $TOOLCHAIN rustup component add rust-src --toolchain $TOOLCHAIN - name: install parity-publish From e2ac06c0d7452c63f777196c47dd3d5c655cef5c Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 15 Jan 2025 11:51:01 +0100 Subject: [PATCH 098/121] upgrade `parity-publish` version in `check-semver.yml` --- .github/workflows/check-semver.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-semver.yml b/.github/workflows/check-semver.yml index d063a89f852e..df1a1c8be603 100644 --- a/.github/workflows/check-semver.yml +++ b/.github/workflows/check-semver.yml @@ -82,7 +82,7 @@ jobs: - name: install parity-publish if: ${{ !contains(github.event.pull_request.labels.*.name, 'R0-silent') }} # Set the target dir to cache the build. - run: CARGO_TARGET_DIR=./target/ cargo install parity-publish@0.10.3 --locked -q + run: CARGO_TARGET_DIR=./target/ cargo install parity-publish@0.10.4 --locked -q - name: check semver if: ${{ !contains(github.event.pull_request.labels.*.name, 'R0-silent') }} From 258373ce05b783d1b193609c599b2d0474cdb261 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Wed, 15 Jan 2025 11:57:01 +0100 Subject: [PATCH 099/121] undo last change --- .github/workflows/check-semver.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-semver.yml b/.github/workflows/check-semver.yml index df1a1c8be603..d063a89f852e 100644 --- a/.github/workflows/check-semver.yml +++ b/.github/workflows/check-semver.yml @@ -82,7 +82,7 @@ jobs: - name: install parity-publish if: ${{ !contains(github.event.pull_request.labels.*.name, 'R0-silent') }} # Set the target dir to cache the build. - run: CARGO_TARGET_DIR=./target/ cargo install parity-publish@0.10.4 --locked -q + run: CARGO_TARGET_DIR=./target/ cargo install parity-publish@0.10.3 --locked -q - name: check semver if: ${{ !contains(github.event.pull_request.labels.*.name, 'R0-silent') }} From b63e77a26eebfe9c7817847955c67502785234b5 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Thu, 16 Jan 2025 11:52:04 +0100 Subject: [PATCH 100/121] update `rust-cache` version too --- .github/workflows/publish-check-compile.yml | 2 +- .github/workflows/publish-check-crates.yml | 2 +- .github/workflows/publish-claim-crates.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish-check-compile.yml b/.github/workflows/publish-check-compile.yml index ce1b2cb231d0..289f7ed18996 100644 --- a/.github/workflows/publish-check-compile.yml +++ b/.github/workflows/publish-check-compile.yml @@ -26,7 +26,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 + uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 with: cache-on-failure: true diff --git a/.github/workflows/publish-check-crates.yml b/.github/workflows/publish-check-crates.yml index 3150cb9dd405..52d138bfe318 100644 --- a/.github/workflows/publish-check-crates.yml +++ b/.github/workflows/publish-check-crates.yml @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 + uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 with: cache-on-failure: true diff --git a/.github/workflows/publish-claim-crates.yml b/.github/workflows/publish-claim-crates.yml index a6efc8a5599e..342a38ec5d8c 100644 --- a/.github/workflows/publish-claim-crates.yml +++ b/.github/workflows/publish-claim-crates.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 + uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 with: cache-on-failure: true From 578fa91698356e51cb154b9ed1a5afe53aeca337 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Thu, 16 Jan 2025 11:53:43 +0100 Subject: [PATCH 101/121] Revert "update `rust-cache` version too" This reverts commit b63e77a26eebfe9c7817847955c67502785234b5. --- .github/workflows/publish-check-compile.yml | 2 +- .github/workflows/publish-check-crates.yml | 2 +- .github/workflows/publish-claim-crates.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish-check-compile.yml b/.github/workflows/publish-check-compile.yml index 289f7ed18996..ce1b2cb231d0 100644 --- a/.github/workflows/publish-check-compile.yml +++ b/.github/workflows/publish-check-compile.yml @@ -26,7 +26,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 + uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: cache-on-failure: true diff --git a/.github/workflows/publish-check-crates.yml b/.github/workflows/publish-check-crates.yml index 52d138bfe318..3150cb9dd405 100644 --- a/.github/workflows/publish-check-crates.yml +++ b/.github/workflows/publish-check-crates.yml @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 + uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: cache-on-failure: true diff --git a/.github/workflows/publish-claim-crates.yml b/.github/workflows/publish-claim-crates.yml index 342a38ec5d8c..a6efc8a5599e 100644 --- a/.github/workflows/publish-claim-crates.yml +++ b/.github/workflows/publish-claim-crates.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@6d193bf28034eafb982f37bd894289fe649468fc # v4.1.7 - name: Rust Cache - uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7 + uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2.7.5 with: cache-on-failure: true From f3f321c9a79b2f76e0e2a052f0f2e182b70edad1 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 08:55:59 +0100 Subject: [PATCH 102/121] remove `RuntimeViewFunction` runtime type from `system` pallet --- prdoc/pr_4722.prdoc | 2 -- .../support/procedural/examples/proc_main/main.rs | 4 ---- .../src/construct_runtime/expand/metadata.rs | 4 +--- substrate/frame/support/src/dispatch.rs | 3 --- substrate/frame/support/src/storage/generator/mod.rs | 2 -- substrate/frame/support/src/tests/mod.rs | 4 ---- substrate/frame/support/test/src/lib.rs | 2 -- substrate/frame/system/src/lib.rs | 10 ---------- 8 files changed, 1 insertion(+), 30 deletions(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index 402bcd35e065..0c0792b7a028 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -19,8 +19,6 @@ doc: crates: - name: frame-support bump: minor - - name: frame-system - bump: major - name: sp-core bump: minor - name: sp-api diff --git a/substrate/frame/support/procedural/examples/proc_main/main.rs b/substrate/frame/support/procedural/examples/proc_main/main.rs index aea4e0562a2f..946bd5ff03ed 100644 --- a/substrate/frame/support/procedural/examples/proc_main/main.rs +++ b/substrate/frame/support/procedural/examples/proc_main/main.rs @@ -58,8 +58,6 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - #[inject_runtime_type] - type RuntimeViewFunction = (); type DbWeight = (); } } @@ -82,8 +80,6 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction: crate::traits::DispatchViewFunction; - #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index 67648cbbf326..b1f1514b9aa4 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -172,9 +172,7 @@ pub fn expand_runtime_metadata( error_enum_ty: #scrate::__private::scale_info::meta_type::(), }, view_functions: #scrate::__private::metadata_ir::RuntimeViewFunctionsIR { - ty: #scrate::__private::scale_info::meta_type::< - <#runtime as #system_path::Config>::RuntimeViewFunction - >(), + ty: #scrate::__private::scale_info::meta_type::(), groups: #scrate::__private::sp_std::vec![ #(#view_functions),* ], } } diff --git a/substrate/frame/support/src/dispatch.rs b/substrate/frame/support/src/dispatch.rs index 81b020584fe6..990996830030 100644 --- a/substrate/frame/support/src/dispatch.rs +++ b/substrate/frame/support/src/dispatch.rs @@ -732,7 +732,6 @@ mod weight_tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; - type RuntimeViewFunction; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -843,7 +842,6 @@ mod weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } @@ -1374,7 +1372,6 @@ mod extension_weight_tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeViewFunction = RuntimeViewFunction; type DbWeight = DbWeight; type PalletInfo = PalletInfo; } diff --git a/substrate/frame/support/src/storage/generator/mod.rs b/substrate/frame/support/src/storage/generator/mod.rs index bc6e2b2278eb..b0b1bda24bb7 100644 --- a/substrate/frame/support/src/storage/generator/mod.rs +++ b/substrate/frame/support/src/storage/generator/mod.rs @@ -65,7 +65,6 @@ mod tests { type RuntimeOrigin; type RuntimeCall; type RuntimeTask; - type RuntimeViewFunction; type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; } @@ -133,7 +132,6 @@ mod tests { type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type RuntimeTask = RuntimeTask; - type RuntimeViewFunction = RuntimeViewFunction; type PalletInfo = PalletInfo; type DbWeight = (); } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index 0d3475f9bfcd..b10e719b9ac3 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -51,8 +51,6 @@ pub mod frame_system { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - #[inject_runtime_type] - type RuntimeViewFunction = (); type DbWeight = (); } } @@ -75,8 +73,6 @@ pub mod frame_system { #[pallet::no_default_bounds] type RuntimeTask: crate::traits::tasks::Task; #[pallet::no_default_bounds] - type RuntimeViewFunction: crate::traits::DispatchViewFunction; - #[pallet::no_default_bounds] type PalletInfo: crate::traits::PalletInfo; type DbWeight: Get; #[pallet::constant] diff --git a/substrate/frame/support/test/src/lib.rs b/substrate/frame/support/test/src/lib.rs index 15897193f042..b080740b0a4b 100644 --- a/substrate/frame/support/test/src/lib.rs +++ b/substrate/frame/support/test/src/lib.rs @@ -52,8 +52,6 @@ pub mod pallet { type RuntimeCall; /// Contains an aggregation of all tasks in this runtime. type RuntimeTask; - /// Type for dispatching queries. - type RuntimeViewFunction; /// The runtime event type. type RuntimeEvent: Parameter + Member diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 17648fc97313..894e1898ed15 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -335,8 +335,6 @@ pub mod pallet { type PalletInfo = (); #[inject_runtime_type] type RuntimeTask = (); - #[inject_runtime_type] - type RuntimeViewFunction = (); type BaseCallFilter = frame_support::traits::Everything; type BlockHashCount = TestBlockHashCount>; type OnSetCode = (); @@ -429,10 +427,6 @@ pub mod pallet { #[inject_runtime_type] type RuntimeTask = (); - /// The query dispatch type, injected by `construct_runtime!`. - #[inject_runtime_type] - type RuntimeViewFunction = (); - /// Converts a module to the index of the module, injected by `construct_runtime!`. #[inject_runtime_type] type PalletInfo = (); @@ -523,10 +517,6 @@ pub mod pallet { #[pallet::no_default_bounds] type RuntimeTask: Task; - /// Type for dispatching queries. - #[pallet::no_default_bounds] - type RuntimeViewFunction: frame_support::traits::DispatchViewFunction; - /// This stores the number of previous transactions associated with a sender account. type Nonce: Parameter + Member From abe6f5cc629e7adbe2495169c7252cc73f7cb573 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 09:27:36 +0100 Subject: [PATCH 103/121] remove unused import --- templates/solochain/runtime/src/configs/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/solochain/runtime/src/configs/mod.rs b/templates/solochain/runtime/src/configs/mod.rs index 21b795808299..e34b3cb82158 100644 --- a/templates/solochain/runtime/src/configs/mod.rs +++ b/templates/solochain/runtime/src/configs/mod.rs @@ -42,7 +42,7 @@ use sp_version::RuntimeVersion; use super::{ AccountId, Aura, Balance, Balances, Block, BlockNumber, Hash, Nonce, PalletInfo, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, - RuntimeViewFunction, System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, + System, EXISTENTIAL_DEPOSIT, SLOT_DURATION, VERSION, }; const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); From 013e7891cc24ebe9a9342d3a9cb11c11f94ab35e Mon Sep 17 00:00:00 2001 From: Giuseppe Re Date: Fri, 17 Jan 2025 09:29:41 +0100 Subject: [PATCH 104/121] Update substrate/frame/support/procedural/src/pallet/parse/view_functions.rs Co-authored-by: Guillaume Thiolliere --- .../support/procedural/src/pallet/parse/view_functions.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 51df02c7a6a8..9f1ddb21ad11 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -33,9 +33,7 @@ pub struct ViewFunctionsImplDef { impl ViewFunctionsImplDef { pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result { - let item_impl = if let syn::Item::Impl(item) = item { - item - } else { + let syn::Item::Impl(item_impl) = item else { return Err(syn::Error::new( item.span(), "Invalid pallet::view_functions_experimental, expected item impl", From d7d614de52409113b4b54ed44ac5364091793f78 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 10:09:06 +0100 Subject: [PATCH 105/121] remove deprecated use of `sp-std` --- substrate/frame/examples/view-functions/Cargo.toml | 2 -- substrate/primitives/api/Cargo.toml | 2 -- substrate/primitives/api/src/lib.rs | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/substrate/frame/examples/view-functions/Cargo.toml b/substrate/frame/examples/view-functions/Cargo.toml index 39e0b217a95f..54d832264391 100644 --- a/substrate/frame/examples/view-functions/Cargo.toml +++ b/substrate/frame/examples/view-functions/Cargo.toml @@ -27,7 +27,6 @@ sp-core = { default-features = false, path = "../../../primitives/core", workspa sp-io = { path = "../../../primitives/io", default-features = false, workspace = true } sp-metadata-ir = { path = "../../../primitives/metadata-ir", default-features = false, workspace = true } sp-runtime = { path = "../../../primitives/runtime", default-features = false, workspace = true } -sp-std = { path = "../../../primitives/std", default-features = false, workspace = true } frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true, workspace = true } @@ -48,7 +47,6 @@ std = [ "sp-io/std", "sp-metadata-ir/std", "sp-runtime/std", - "sp-std/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", diff --git a/substrate/primitives/api/Cargo.toml b/substrate/primitives/api/Cargo.toml index fead69828772..7295adbc11ca 100644 --- a/substrate/primitives/api/Cargo.toml +++ b/substrate/primitives/api/Cargo.toml @@ -30,7 +30,6 @@ sp-metadata-ir = { optional = true, workspace = true } sp-runtime = { workspace = true } sp-runtime-interface = { workspace = true } sp-state-machine = { optional = true, workspace = true } -sp-std = { workspace = true } sp-trie = { optional = true, workspace = true } sp-version = { workspace = true } thiserror = { optional = true, workspace = true } @@ -53,7 +52,6 @@ std = [ "sp-runtime-interface/std", "sp-runtime/std", "sp-state-machine/std", - "sp-std/std", "sp-test-primitives/std", "sp-trie/std", "sp-version/std", diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index ac4d1b2c0aff..dcf24c88cc36 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -113,6 +113,7 @@ pub mod __private { pub use sp_runtime_interface::polkavm::{polkavm_abi, polkavm_export}; } +use alloc::vec::Vec; #[cfg(feature = "std")] pub use sp_core::traits::CallContext; use sp_core::{OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; @@ -127,7 +128,6 @@ use sp_runtime::{traits::Block as BlockT, ExtrinsicInclusionMode}; pub use sp_state_machine::StorageProof; #[cfg(feature = "std")] use sp_state_machine::{backend::AsTrieBackend, Backend as StateBackend, OverlayedChanges}; -use sp_std::vec::Vec; use sp_version::RuntimeVersion; #[cfg(feature = "std")] use std::cell::RefCell; From f9be388922426f99fd98e91b40b14b6c60f4fb2c Mon Sep 17 00:00:00 2001 From: Giuseppe Re Date: Fri, 17 Jan 2025 10:10:56 +0100 Subject: [PATCH 106/121] Update substrate/frame/support/src/traits/view_function.rs Co-authored-by: Guillaume Thiolliere --- substrate/frame/support/src/traits/view_function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 9a4de07cf465..d40b901f7285 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -47,7 +47,7 @@ pub trait ViewFunctionIdSuffix { const SUFFIX: [u8; 16]; } -/// implemented for each pallet view function method +/// Automatically implemented for each pallet view function method by the macro [`pallet`](crate::pallet). pub trait ViewFunction: DecodeAll { fn id() -> ViewFunctionId; type ReturnType: Encode; From 051dcddd1a0bf086ec8267bb83d0198294202611 Mon Sep 17 00:00:00 2001 From: Giuseppe Re Date: Fri, 17 Jan 2025 10:11:11 +0100 Subject: [PATCH 107/121] Update substrate/frame/support/src/traits/view_function.rs Co-authored-by: Guillaume Thiolliere --- substrate/frame/support/src/traits/view_function.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index d40b901f7285..749ff5326c82 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -43,6 +43,7 @@ pub trait ViewFunctionIdPrefix { fn prefix() -> [u8; 16]; } +/// Automatically implemented for each pallet view function method by the macro [`pallet`](crate::pallet). pub trait ViewFunctionIdSuffix { const SUFFIX: [u8; 16]; } From dec2467f69322f379accfc8d46d82040713f12d3 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 10:23:05 +0100 Subject: [PATCH 108/121] update `Cargo.lock` --- Cargo.lock | 2 -- 1 file changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be1d3d5f58b0..8deb7675724e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13651,7 +13651,6 @@ dependencies = [ "sp-io 30.0.0", "sp-metadata-ir 0.6.0", "sp-runtime 31.0.1", - "sp-std 14.0.0", ] [[package]] @@ -25639,7 +25638,6 @@ dependencies = [ "sp-runtime 31.0.1", "sp-runtime-interface 24.0.0", "sp-state-machine 0.35.0", - "sp-std 14.0.0", "sp-test-primitives", "sp-trie 29.0.0", "sp-version 29.0.0", From e8ff55de7d051fcafdc9cd1509994295192e760d Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 10:33:01 +0100 Subject: [PATCH 109/121] `cargo +nightly fmt --all` --- substrate/frame/support/src/traits/view_function.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 749ff5326c82..e3f4d2726da9 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -43,12 +43,14 @@ pub trait ViewFunctionIdPrefix { fn prefix() -> [u8; 16]; } -/// Automatically implemented for each pallet view function method by the macro [`pallet`](crate::pallet). +/// Automatically implemented for each pallet view function method by the macro +/// [`pallet`](crate::pallet). pub trait ViewFunctionIdSuffix { const SUFFIX: [u8; 16]; } -/// Automatically implemented for each pallet view function method by the macro [`pallet`](crate::pallet). +/// Automatically implemented for each pallet view function method by the macro +/// [`pallet`](crate::pallet). pub trait ViewFunction: DecodeAll { fn id() -> ViewFunctionId; type ReturnType: Encode; From 82f08eafabf0644a9b9ac3e23b1da1f6494ddf4e Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 11:09:34 +0100 Subject: [PATCH 110/121] update prdoc semver bump --- prdoc/pr_4722.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index 0c0792b7a028..4ae845ac549f 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -32,6 +32,6 @@ crates: - name: cumulus-pov-validator bump: none - name: cumulus-pallet-weight-reclaim - bump: minor + bump: patch - name: westend-runtime bump: minor \ No newline at end of file From 98d3063955027b0b3df7ed8573c44094aa0763bf Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 11:57:13 +0100 Subject: [PATCH 111/121] add `get_attributes()` method for pallet macros --- .../src/construct_runtime/expand/call.rs | 11 +---------- .../src/construct_runtime/expand/config.rs | 10 +--------- .../src/construct_runtime/expand/inherent.rs | 10 +--------- .../src/construct_runtime/expand/metadata.rs | 19 ++----------------- .../src/construct_runtime/expand/origin.rs | 19 ++----------------- .../construct_runtime/expand/outer_enums.rs | 19 ++----------------- .../src/construct_runtime/expand/task.rs | 10 +--------- .../src/construct_runtime/expand/unsigned.rs | 10 +--------- .../construct_runtime/expand/view_function.rs | 10 +--------- .../procedural/src/construct_runtime/mod.rs | 11 +---------- .../procedural/src/construct_runtime/parse.rs | 13 +++++++++++++ 11 files changed, 26 insertions(+), 116 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/call.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/call.rs index f055e8ce28e9..411d74ecbb3d 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/call.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/call.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::Pallet; use proc_macro2::TokenStream; use quote::quote; -use std::str::FromStr; use syn::Ident; pub fn expand_outer_dispatch( @@ -40,15 +39,7 @@ pub fn expand_outer_dispatch( let name = &pallet_declaration.name; let path = &pallet_declaration.path; let index = pallet_declaration.index; - let attr = - pallet_declaration.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet_declaration.get_attributes(); variant_defs.extend(quote! { #attr diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/config.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/config.rs index dbbe6ba6e6c3..7a51ba6ecf1d 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/config.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/config.rs @@ -19,7 +19,6 @@ use crate::construct_runtime::Pallet; use inflector::Inflector; use proc_macro2::TokenStream; use quote::{format_ident, quote, ToTokens}; -use std::str::FromStr; use syn::Ident; pub fn expand_outer_config( @@ -41,14 +40,7 @@ pub fn expand_outer_config( let field_name = &Ident::new(&pallet_name.to_string().to_snake_case(), decl.name.span()); let part_is_generic = !pallet_entry.generics.params.is_empty(); - let attr = &decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = &decl.get_attributes(); types.extend(expand_config_types(attr, runtime, decl, &config, part_is_generic)); fields.extend(quote!(#attr pub #field_name: #config,)); diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/inherent.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/inherent.rs index e34c6ac5016a..e25492802c32 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/inherent.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/inherent.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::Pallet; use proc_macro2::TokenStream; use quote::quote; -use std::str::FromStr; use syn::Ident; pub fn expand_outer_inherent( @@ -36,14 +35,7 @@ pub fn expand_outer_inherent( if pallet_decl.exists_part("Inherent") { let name = &pallet_decl.name; let path = &pallet_decl.path; - let attr = pallet_decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet_decl.get_attributes(); pallet_names.push(name); pallet_attrs.push(attr); diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs index b1f1514b9aa4..d246c0062864 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::{parse::PalletPath, Pallet}; use proc_macro2::TokenStream; use quote::quote; -use std::str::FromStr; use syn::Ident; pub fn expand_runtime_metadata( @@ -51,14 +50,7 @@ pub fn expand_runtime_metadata( let errors = expand_pallet_metadata_errors(runtime, decl); let associated_types = expand_pallet_metadata_associated_types(runtime, decl); let docs = expand_pallet_metadata_docs(runtime, decl); - let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = decl.get_attributes(); let deprecation_info = expand_pallet_metadata_deprecation(runtime, decl); quote! { #attr @@ -82,14 +74,7 @@ pub fn expand_runtime_metadata( let name = &decl.name; let path = &decl.path; let instance = decl.instance.as_ref().into_iter(); - let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = decl.get_attributes(); quote! { #attr diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/origin.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/origin.rs index 1c4ab436ad92..4742e68e2e2a 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/origin.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/origin.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::{Pallet, SYSTEM_PALLET_NAME}; use proc_macro2::TokenStream; use quote::quote; -use std::str::FromStr; use syn::{Generics, Ident}; pub fn expand_outer_origin( @@ -335,14 +334,7 @@ fn expand_origin_caller_variant( let part_is_generic = !generics.params.is_empty(); let variant_name = &pallet.name; let path = &pallet.path; - let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet.get_attributes(); match instance { Some(inst) if part_is_generic => quote! { @@ -387,14 +379,7 @@ fn expand_origin_pallet_conversions( }; let doc_string = get_intra_doc_string(" Convert to runtime origin using", &path.module_name()); - let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet.get_attributes(); quote! { #attr diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs index 80b242ccbe49..80d3a5af2662 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/outer_enums.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::Pallet; use proc_macro2::{Span, TokenStream}; use quote::{quote, ToTokens}; -use std::str::FromStr; use syn::{Generics, Ident}; /// Represents the types supported for creating an outer enum. @@ -185,14 +184,7 @@ fn expand_enum_variant( let path = &pallet.path; let variant_name = &pallet.name; let part_is_generic = !generics.params.is_empty(); - let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet.get_attributes(); match instance { Some(inst) if part_is_generic => quote! { @@ -224,14 +216,7 @@ fn expand_enum_conversion( enum_name_ident: &Ident, ) -> TokenStream { let variant_name = &pallet.name; - let attr = pallet.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet.get_attributes(); quote! { #attr diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/task.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/task.rs index 1302f86455f2..b9b8efb8c006 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/task.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/task.rs @@ -16,7 +16,6 @@ // limitations under the License use crate::construct_runtime::Pallet; -use core::str::FromStr; use proc_macro2::{Ident, TokenStream as TokenStream2}; use quote::quote; @@ -42,14 +41,7 @@ pub fn expand_outer_task( let instance = decl.instance.as_ref().map(|instance| quote!(, #path::#instance)); let task_type = quote!(#path::Task<#runtime_name #instance>); - let attr = decl.cfg_pattern.iter().fold(TokenStream2::new(), |acc, pattern| { - let attr = TokenStream2::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = decl.get_attributes(); from_impls.push(quote! { #attr diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/unsigned.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/unsigned.rs index 33aadba0d1f1..737a39ea681e 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/unsigned.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/unsigned.rs @@ -18,7 +18,6 @@ use crate::construct_runtime::Pallet; use proc_macro2::TokenStream; use quote::quote; -use std::str::FromStr; use syn::Ident; pub fn expand_outer_validate_unsigned( @@ -34,14 +33,7 @@ pub fn expand_outer_validate_unsigned( if pallet_decl.exists_part("ValidateUnsigned") { let name = &pallet_decl.name; let path = &pallet_decl.path; - let attr = pallet_decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { - let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }); + let attr = pallet_decl.get_attributes(); pallet_names.push(name); pallet_attrs.push(attr); diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index 687194b3d4b3..fbbdeb41e2d3 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -16,7 +16,6 @@ // limitations under the License use crate::construct_runtime::Pallet; -use core::str::FromStr; use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; /// Expands implementation of runtime level `DispatchViewFunction`. @@ -29,14 +28,7 @@ pub fn expand_outer_query( let prefix_conditionals = pallet_decls.iter().map(|pallet| { let pallet_name = &pallet.name; - let attr = pallet.cfg_pattern.iter().fold(TokenStream2::new(), |acc, pattern| { - let attr = TokenStream2::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote::quote! { - #acc - #attr - } - }); + let attr = pallet.get_attributes(); quote::quote! { #attr if id.prefix == <#pallet_name as #scrate::traits::ViewFunctionIdPrefix>::prefix() { diff --git a/substrate/frame/support/procedural/src/construct_runtime/mod.rs b/substrate/frame/support/procedural/src/construct_runtime/mod.rs index 54f126b5f09f..c6018e048f2f 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/mod.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/mod.rs @@ -653,16 +653,7 @@ pub(crate) fn decl_pallet_runtime_setup( .collect::>(); let pallet_attrs = pallet_declarations .iter() - .map(|pallet| { - pallet.cfg_pattern.iter().fold(TokenStream2::new(), |acc, pattern| { - let attr = TokenStream2::from_str(&format!("#[cfg({})]", pattern.original())) - .expect("was successfully parsed before; qed"); - quote! { - #acc - #attr - } - }) - }) + .map(|pallet| pallet.get_attributes()) .collect::>(); quote!( diff --git a/substrate/frame/support/procedural/src/construct_runtime/parse.rs b/substrate/frame/support/procedural/src/construct_runtime/parse.rs index fb18cbf0de79..d5c7608d6572 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/parse.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/parse.rs @@ -15,6 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use core::str::FromStr; use frame_support_procedural_tools::syn_ext as ext; use proc_macro2::{Span, TokenStream}; use quote::ToTokens; @@ -615,6 +616,18 @@ impl Pallet { pub fn exists_part(&self, name: &str) -> bool { self.find_part(name).is_some() } + + // Get runtime attributes for the pallet, mostly used for macros + pub fn get_attributes(&self) -> TokenStream { + self.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| { + let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original())) + .expect("was successfully parsed before; qed"); + quote::quote! { + #acc + #attr + } + }) + } } /// Result of a conversion of a declaration of pallets. From 502afaf500d753f9c16578ba3a8992b90b256fb1 Mon Sep 17 00:00:00 2001 From: Giuseppe Re Date: Fri, 17 Jan 2025 11:59:44 +0100 Subject: [PATCH 112/121] Update substrate/frame/support/src/traits/view_function.rs Co-authored-by: Guillaume Thiolliere --- substrate/frame/support/src/traits/view_function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index e3f4d2726da9..1c8eaf741025 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -20,7 +20,7 @@ use codec::{DecodeAll, Encode, Output}; use sp_core::{ViewFunctionDispatchError, ViewFunctionId}; -/// implemented by the runtime dispatching by prefix and then the pallet dispatching by suffix +/// Implemented by both pallets and the runtime. The runtime is dispatching by prefix using the pallet implementation of `ViewFunctionIdPrefix` then the pallet is dispatching by suffix using the methods implementation of `ViewFunctionIdSuffix`. pub trait DispatchViewFunction { fn dispatch_view_function( id: &ViewFunctionId, From e0b76edb65b73b17fc8bc9988b5c706393b43119 Mon Sep 17 00:00:00 2001 From: Giuseppe Re Date: Fri, 17 Jan 2025 12:00:02 +0100 Subject: [PATCH 113/121] Update substrate/frame/support/src/traits/view_function.rs Co-authored-by: Guillaume Thiolliere --- substrate/frame/support/src/traits/view_function.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index 1c8eaf741025..f7145d9505f1 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -39,6 +39,7 @@ impl DispatchViewFunction for () { } } +/// Automatically implemented for each pallet by the macro [`pallet`](crate::pallet). pub trait ViewFunctionIdPrefix { fn prefix() -> [u8; 16]; } From 73f62e4506de6fc574efd85bf679200cc79fbf32 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 12:27:15 +0100 Subject: [PATCH 114/121] fmt --- substrate/frame/support/src/traits/view_function.rs | 4 +++- templates/parachain/runtime/src/configs/mod.rs | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/traits/view_function.rs index f7145d9505f1..7b18999ea290 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/traits/view_function.rs @@ -20,7 +20,9 @@ use codec::{DecodeAll, Encode, Output}; use sp_core::{ViewFunctionDispatchError, ViewFunctionId}; -/// Implemented by both pallets and the runtime. The runtime is dispatching by prefix using the pallet implementation of `ViewFunctionIdPrefix` then the pallet is dispatching by suffix using the methods implementation of `ViewFunctionIdSuffix`. +/// Implemented by both pallets and the runtime. The runtime is dispatching by prefix using the +/// pallet implementation of `ViewFunctionIdPrefix` then the pallet is dispatching by suffix using +/// the methods implementation of `ViewFunctionIdSuffix`. pub trait DispatchViewFunction { fn dispatch_view_function( id: &ViewFunctionId, diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index c86cb264e1ab..1e9155f59a57 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -61,10 +61,9 @@ use super::{ weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, AccountId, Aura, Balance, Balances, Block, BlockNumber, CollatorSelection, ConsensusHook, Hash, MessageQueue, Nonce, PalletInfo, ParachainSystem, Runtime, RuntimeCall, RuntimeEvent, - RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, RuntimeViewFunction, - Session, SessionKeys, System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, - EXISTENTIAL_DEPOSIT, HOURS, MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, - SLOT_DURATION, VERSION, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, Session, SessionKeys, + System, WeightToFee, XcmpQueue, AVERAGE_ON_INITIALIZE_RATIO, EXISTENTIAL_DEPOSIT, HOURS, + MAXIMUM_BLOCK_WEIGHT, MICRO_UNIT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, VERSION, }; use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin}; From 8e34251abc29d6f4b502b7741cb7a1fdca7d6994 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Fri, 17 Jan 2025 16:50:15 +0100 Subject: [PATCH 115/121] remove `sp-std` from view functions macro --- .../src/construct_runtime/expand/view_function.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index fbbdeb41e2d3..b707fc3cb838 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -65,10 +65,10 @@ pub fn expand_outer_query( /// Convenience function for query execution from the runtime API. pub fn execute_view_function( id: #scrate::__private::ViewFunctionId, - input: #scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, - ) -> Result<#scrate::__private::sp_std::vec::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> + input: #scrate::__private::Vec<::core::primitive::u8>, + ) -> Result<#scrate::__private::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> { - let mut output = #scrate::__private::sp_std::vec![]; + let mut output = #scrate::__private::vec![]; <#runtime_view_function as #scrate::traits::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; Ok(output) } From de5834ca82d064f8ae66958a732cc00a03de704b Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 20 Jan 2025 16:20:43 +0100 Subject: [PATCH 116/121] Change `panic`s into compile errors --- .../src/pallet/expand/view_functions.rs | 12 ++++-- .../src/pallet/parse/view_functions.rs | 40 +++++++++++++------ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index aede71df0be4..4662825e2c90 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -88,12 +88,18 @@ fn expand_view_function( let view_function_struct_ident = view_fn.view_function_struct_ident(); let view_fn_name = &view_fn.name; - let (arg_names, arg_types) = view_fn.args_names_types(); + let (arg_names, arg_types) = match view_fn.args_names_types() { + Ok((arg_names, arg_types)) => (arg_names, arg_types), + Err(e) => return e.into_compile_error(), + }; let return_type = &view_fn.return_type; let docs = &view_fn.docs; - let view_function_id_suffix_bytes = view_fn - .view_function_id_suffix_bytes() + let view_function_id_suffix_bytes_raw = match view_fn.view_function_id_suffix_bytes() { + Ok(view_function_id_suffix_bytes_raw) => view_function_id_suffix_bytes_raw, + Err(e) => return e.into_compile_error(), + }; + let view_function_id_suffix_bytes = view_function_id_suffix_bytes_raw .map(|byte| syn::LitInt::new(&format!("0x{:X}_u8", byte), Span::call_site())); quote::quote! { diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 9f1ddb21ad11..858392d80142 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -56,6 +56,11 @@ impl ViewFunctionsImplDef { let view_fn_def = ViewFunctionDef::try_from(method.clone())?; view_functions.push(view_fn_def) + } else { + return Err(syn::Error::new( + item.span(), + "Invalid pallet::view_functions_experimental, expected a function", + )) } } Ok(Self { @@ -103,12 +108,12 @@ impl ViewFunctionDef { ) } - pub fn view_function_id_suffix_bytes(&self) -> [u8; 16] { + pub fn view_function_id_suffix_bytes(&self) -> Result<[u8; 16], syn::Error> { let mut output = [0u8; 16]; // concatenate the signature string let arg_types = self - .args_names_types() + .args_names_types()? .1 .iter() .map(|ty| quote::quote!(#ty).to_string().replace(" ", "")) @@ -124,19 +129,30 @@ impl ViewFunctionDef { // hash the signature string let hash = sp_crypto_hashing::twox_128(view_fn_signature.as_bytes()); output.copy_from_slice(&hash[..]); - output + Ok(output) } - pub fn args_names_types(&self) -> (Vec, Vec) { - self.args + pub fn args_names_types(&self) -> Result<(Vec, Vec), syn::Error> { + Ok(self + .args .iter() - .map(|arg| match arg { - syn::FnArg::Typed(pat_type) => match &*pat_type.pat { - syn::Pat::Ident(ident) => (ident.ident.clone(), *pat_type.ty.clone()), - _ => panic!("Unsupported pattern in view function argument"), - }, - _ => panic!("Unsupported argument in view function"), + .map(|arg| { + let syn::FnArg::Typed(pat_type) = arg else { + return Err(syn::Error::new( + arg.span(), + "Unsupported argument in view function", + )); + }; + let syn::Pat::Ident(ident) = &*pat_type.pat else { + return Err(syn::Error::new( + pat_type.pat.span(), + "Unsupported pattern in view function argument", + )); + }; + Ok((ident.ident.clone(), *pat_type.ty.clone())) }) - .unzip() + .collect::, syn::Error>>()? + .into_iter() + .unzip()) } } From f4e8aa2c20f56478d2273342ff6a5a6b88e50519 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 20 Jan 2025 17:18:23 +0100 Subject: [PATCH 117/121] Add view functions to Runtime APIs in `westend` runtime + `solochain` and `parachain` templates --- polkadot/runtime/westend/src/lib.rs | 10 +++++++++- templates/parachain/runtime/src/apis.rs | 8 +++++++- templates/solochain/runtime/src/apis.rs | 8 +++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 7da19cba18a5..a8bbba43eab0 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -94,7 +94,9 @@ use sp_consensus_beefy::{ ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature}, mmr::{BeefyDataProvider, MmrLeafVersion}, }; -use sp_core::{ConstU8, OpaqueMetadata, RuntimeDebug, H256}; +use sp_core::{ + ConstU8, OpaqueMetadata, RuntimeDebug, ViewFunctionDispatchError, ViewFunctionId, H256, +}; use sp_runtime::{ generic, impl_opaque_keys, traits::{ @@ -1964,6 +1966,12 @@ sp_api::impl_runtime_apis! { } } + impl sp_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + Runtime::execute_view_function(id, input) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) diff --git a/templates/parachain/runtime/src/apis.rs b/templates/parachain/runtime/src/apis.rs index 05a508ca655f..dbf8efa4d63b 100644 --- a/templates/parachain/runtime/src/apis.rs +++ b/templates/parachain/runtime/src/apis.rs @@ -35,7 +35,7 @@ use frame_support::{ use pallet_aura::Authorities; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; use sp_runtime::{ traits::Block as BlockT, transaction_validity::{TransactionSource, TransactionValidity}, @@ -114,6 +114,12 @@ impl_runtime_apis! { } } + impl sp_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + Runtime::execute_view_function(id, input) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) diff --git a/templates/solochain/runtime/src/apis.rs b/templates/solochain/runtime/src/apis.rs index 06c645fa0c53..44731056c67d 100644 --- a/templates/solochain/runtime/src/apis.rs +++ b/templates/solochain/runtime/src/apis.rs @@ -32,7 +32,7 @@ use frame_support::{ use pallet_grandpa::AuthorityId as GrandpaId; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; use sp_runtime::{ traits::{Block as BlockT, NumberFor}, transaction_validity::{TransactionSource, TransactionValidity}, @@ -75,6 +75,12 @@ impl_runtime_apis! { } } + impl sp_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + Runtime::execute_view_function(id, input) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) From 85ab0b7a012eecbd6ea3ab87d400041f6636db99 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 20 Jan 2025 17:48:42 +0100 Subject: [PATCH 118/121] remove unused pallet-level `Query` type --- .../frame/support/procedural/src/construct_runtime/parse.rs | 6 ------ .../invalid_module_details_keyword.stderr | 2 +- .../tests/construct_runtime_ui/invalid_module_entry.stderr | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/substrate/frame/support/procedural/src/construct_runtime/parse.rs b/substrate/frame/support/procedural/src/construct_runtime/parse.rs index d5c7608d6572..2df08123821a 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/parse.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/parse.rs @@ -46,7 +46,6 @@ mod keyword { syn::custom_keyword!(Task); syn::custom_keyword!(LockId); syn::custom_keyword!(SlashReason); - syn::custom_keyword!(Query); syn::custom_keyword!(exclude_parts); syn::custom_keyword!(use_parts); syn::custom_keyword!(expanded); @@ -395,7 +394,6 @@ pub enum PalletPartKeyword { Task(keyword::Task), LockId(keyword::LockId), SlashReason(keyword::SlashReason), - Query(keyword::Query), } impl Parse for PalletPartKeyword { @@ -430,8 +428,6 @@ impl Parse for PalletPartKeyword { Ok(Self::LockId(input.parse()?)) } else if lookahead.peek(keyword::SlashReason) { Ok(Self::SlashReason(input.parse()?)) - } else if lookahead.peek(keyword::Query) { - Ok(Self::Query(input.parse()?)) } else { Err(lookahead.error()) } @@ -456,7 +452,6 @@ impl PalletPartKeyword { Self::Task(_) => "Task", Self::LockId(_) => "LockId", Self::SlashReason(_) => "SlashReason", - Self::Query(_) => "Query", } } @@ -488,7 +483,6 @@ impl ToTokens for PalletPartKeyword { Self::Task(inner) => inner.to_tokens(tokens), Self::LockId(inner) => inner.to_tokens(tokens), Self::SlashReason(inner) => inner.to_tokens(tokens), - Self::Query(inner) => inner.to_tokens(tokens), } } } diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr index 8feac638630a..feb61793151d 100644 --- a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr +++ b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_details_keyword.stderr @@ -1,4 +1,4 @@ -error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason`, `Query` +error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason` --> tests/construct_runtime_ui/invalid_module_details_keyword.rs:23:20 | 23 | system: System::{enum}, diff --git a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr index d7b5f3868e9a..97943dfc1763 100644 --- a/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr +++ b/substrate/frame/support/test/tests/construct_runtime_ui/invalid_module_entry.stderr @@ -1,4 +1,4 @@ -error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason`, `Query` +error: expected one of: `Pallet`, `Call`, `Storage`, `Event`, `Error`, `Config`, `Origin`, `Inherent`, `ValidateUnsigned`, `FreezeReason`, `HoldReason`, `Task`, `LockId`, `SlashReason` --> tests/construct_runtime_ui/invalid_module_entry.rs:24:23 | 24 | Balance: balances::{Unexpected}, From 08f711f5f604cbbc7d363d0041fc391341244927 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Mon, 27 Jan 2025 16:35:04 +0100 Subject: [PATCH 119/121] move `view_functions` declaration and traits to `frame_support` --- polkadot/runtime/westend/src/lib.rs | 8 +-- substrate/bin/node/runtime/src/lib.rs | 6 +- .../examples/view-functions/src/tests.rs | 2 +- .../construct_runtime/expand/view_function.rs | 18 +++--- .../src/pallet/expand/view_functions.rs | 30 +++++----- substrate/frame/support/src/lib.rs | 3 +- substrate/frame/support/src/traits.rs | 6 -- .../view_function.rs => view_functions.rs} | 60 ++++++++++++++++++- substrate/primitives/api/src/lib.rs | 9 +-- substrate/primitives/core/src/lib.rs | 38 ------------ templates/parachain/runtime/src/apis.rs | 6 +- templates/solochain/runtime/src/apis.rs | 6 +- 12 files changed, 98 insertions(+), 94 deletions(-) rename substrate/frame/support/src/{traits/view_function.rs => view_functions.rs} (56%) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index d27b70685301..ac94fa996a87 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -94,9 +94,7 @@ use sp_consensus_beefy::{ ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature}, mmr::{BeefyDataProvider, MmrLeafVersion}, }; -use sp_core::{ - ConstU8, OpaqueMetadata, RuntimeDebug, ViewFunctionDispatchError, ViewFunctionId, H256, -}; +use sp_core::{ConstU8, OpaqueMetadata, RuntimeDebug, H256}; use sp_runtime::{ generic, impl_opaque_keys, traits::{ @@ -1976,8 +1974,8 @@ sp_api::impl_runtime_apis! { } } - impl sp_api::RuntimeViewFunction for Runtime { - fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + impl frame_support::view_functions::runtime_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: frame_support::view_functions::ViewFunctionId, input: Vec) -> Result, frame_support::view_functions::ViewFunctionDispatchError> { Runtime::execute_view_function(id, input) } } diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 766a8fd0a639..ba2b854afe65 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -103,7 +103,7 @@ use sp_consensus_beefy::{ mmr::MmrLeafVersion, }; use sp_consensus_grandpa::AuthorityId as GrandpaId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, H160}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ curve::PiecewiseLinear, @@ -3014,8 +3014,8 @@ impl_runtime_apis! { } } - impl sp_api::RuntimeViewFunction for Runtime { - fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + impl frame_support::view_functions::runtime_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: frame_support::view_functions::ViewFunctionId, input: Vec) -> Result, frame_support::view_functions::ViewFunctionDispatchError> { Runtime::execute_view_function(id, input) } } diff --git a/substrate/frame/examples/view-functions/src/tests.rs b/substrate/frame/examples/view-functions/src/tests.rs index 51c35f4924cc..25f5f094651d 100644 --- a/substrate/frame/examples/view-functions/src/tests.rs +++ b/substrate/frame/examples/view-functions/src/tests.rs @@ -25,7 +25,7 @@ use crate::{ use codec::{Decode, Encode}; use scale_info::{form::PortableForm, meta_type}; -use frame_support::{derive_impl, pallet_prelude::PalletInfoAccess, traits::ViewFunction}; +use frame_support::{derive_impl, pallet_prelude::PalletInfoAccess, view_functions::ViewFunction}; use sp_io::hashing::twox_128; use sp_metadata_ir::{ViewFunctionArgMetadataIR, ViewFunctionGroupIR, ViewFunctionMetadataIR}; use sp_runtime::testing::TestXt; diff --git a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs index b707fc3cb838..094dcca4a5b5 100644 --- a/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs +++ b/substrate/frame/support/procedural/src/construct_runtime/expand/view_function.rs @@ -31,8 +31,8 @@ pub fn expand_outer_query( let attr = pallet.get_attributes(); quote::quote! { #attr - if id.prefix == <#pallet_name as #scrate::traits::ViewFunctionIdPrefix>::prefix() { - return <#pallet_name as #scrate::traits::DispatchViewFunction>::dispatch_view_function(id, input, output) + if id.prefix == <#pallet_name as #scrate::view_functions::ViewFunctionIdPrefix>::prefix() { + return <#pallet_name as #scrate::view_functions::DispatchViewFunction>::dispatch_view_function(id, input, output) } } }); @@ -49,27 +49,27 @@ pub fn expand_outer_query( pub enum #runtime_view_function {} const _: () = { - impl #scrate::traits::DispatchViewFunction for #runtime_view_function { + impl #scrate::view_functions::DispatchViewFunction for #runtime_view_function { fn dispatch_view_function( - id: & #scrate::__private::ViewFunctionId, + id: & #scrate::view_functions::ViewFunctionId, input: &mut &[u8], output: &mut O - ) -> Result<(), #scrate::__private::ViewFunctionDispatchError> + ) -> Result<(), #scrate::view_functions::ViewFunctionDispatchError> { #( #prefix_conditionals )* - Err(#scrate::__private::ViewFunctionDispatchError::NotFound(id.clone())) + Err(#scrate::view_functions::ViewFunctionDispatchError::NotFound(id.clone())) } } impl #runtime_name { /// Convenience function for query execution from the runtime API. pub fn execute_view_function( - id: #scrate::__private::ViewFunctionId, + id: #scrate::view_functions::ViewFunctionId, input: #scrate::__private::Vec<::core::primitive::u8>, - ) -> Result<#scrate::__private::Vec<::core::primitive::u8>, #scrate::__private::ViewFunctionDispatchError> + ) -> Result<#scrate::__private::Vec<::core::primitive::u8>, #scrate::view_functions::ViewFunctionDispatchError> { let mut output = #scrate::__private::vec![]; - <#runtime_view_function as #scrate::traits::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; + <#runtime_view_function as #scrate::view_functions::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?; Ok(output) } } diff --git a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs index 4662825e2c90..587e74a2ac18 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/view_functions.rs @@ -61,7 +61,7 @@ fn expand_view_function_prefix_impl( let type_use_gen = &def.type_use_generics(span); quote::quote! { - impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdPrefix for #pallet_ident<#type_use_gen> #where_clause { + impl<#type_impl_gen> #frame_support::view_functions::ViewFunctionIdPrefix for #pallet_ident<#type_use_gen> #where_clause { fn prefix() -> [::core::primitive::u8; 16usize] { < ::PalletInfo @@ -134,15 +134,15 @@ fn expand_view_function( } } - impl<#type_impl_gen> #frame_support::traits::ViewFunctionIdSuffix for #view_function_struct_ident<#type_use_gen> #where_clause { + impl<#type_impl_gen> #frame_support::view_functions::ViewFunctionIdSuffix for #view_function_struct_ident<#type_use_gen> #where_clause { const SUFFIX: [::core::primitive::u8; 16usize] = [ #( #view_function_id_suffix_bytes ),* ]; } - impl<#type_impl_gen> #frame_support::traits::ViewFunction for #view_function_struct_ident<#type_use_gen> #where_clause { - fn id() -> #frame_support::__private::ViewFunctionId { - #frame_support::__private::ViewFunctionId { - prefix: <#pallet_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdPrefix>::prefix(), - suffix: ::SUFFIX, + impl<#type_impl_gen> #frame_support::view_functions::ViewFunction for #view_function_struct_ident<#type_use_gen> #where_clause { + fn id() -> #frame_support::view_functions::ViewFunctionId { + #frame_support::view_functions::ViewFunctionId { + prefix: <#pallet_ident<#type_use_gen> as #frame_support::view_functions::ViewFunctionIdPrefix>::prefix(), + suffix: ::SUFFIX, } } @@ -170,26 +170,26 @@ fn impl_dispatch_view_function( let query_match_arms = view_fns.iter().map(|view_fn| { let view_function_struct_ident = view_fn.view_function_struct_ident(); quote::quote! { - <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunctionIdSuffix>::SUFFIX => { - <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::execute(input, output) + <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunctionIdSuffix>::SUFFIX => { + <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::execute(input, output) } } }); quote::quote! { - impl<#type_impl_gen> #frame_support::traits::DispatchViewFunction + impl<#type_impl_gen> #frame_support::view_functions::DispatchViewFunction for #pallet_ident<#type_use_gen> #where_clause { #[deny(unreachable_patterns)] fn dispatch_view_function( - id: & #frame_support::__private::ViewFunctionId, + id: & #frame_support::view_functions::ViewFunctionId, input: &mut &[u8], output: &mut O - ) -> Result<(), #frame_support::__private::ViewFunctionDispatchError> + ) -> Result<(), #frame_support::view_functions::ViewFunctionDispatchError> { match id.suffix { #( #query_match_arms )* - _ => Err(#frame_support::__private::ViewFunctionDispatchError::NotFound(id.clone())), + _ => Err(#frame_support::view_functions::ViewFunctionDispatchError::NotFound(id.clone())), } } } @@ -233,10 +233,10 @@ fn impl_view_function_metadata( quote::quote! { #frame_support::__private::metadata_ir::ViewFunctionMetadataIR { name: ::core::stringify!(#name), - id: <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::id().into(), + id: <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::id().into(), args: #frame_support::__private::sp_std::vec![ #( #args ),* ], output: #frame_support::__private::scale_info::meta_type::< - <#view_function_struct_ident<#type_use_gen> as #frame_support::traits::ViewFunction>::ReturnType + <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::ReturnType >(), docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ], } diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index fda8e96677f5..97d16e2a06d2 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -55,7 +55,7 @@ pub mod __private { pub use scale_info; pub use serde; pub use serde_json; - pub use sp_core::{Get, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, Void}; + pub use sp_core::{Get, OpaqueMetadata, Void}; pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; #[cfg(feature = "std")] @@ -87,6 +87,7 @@ pub mod storage; #[cfg(test)] mod tests; pub mod traits; +pub mod view_functions; pub mod weights; #[doc(hidden)] pub mod unsigned { diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index 492c196d020e..4a83c809a6a5 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -139,12 +139,6 @@ pub use proving::*; #[cfg(feature = "try-runtime")] mod try_runtime; - -mod view_function; -pub use view_function::{ - DispatchViewFunction, ViewFunction, ViewFunctionIdPrefix, ViewFunctionIdSuffix, -}; - #[cfg(feature = "try-runtime")] pub use try_runtime::{ Select as TryStateSelect, TryDecodeEntireStorage, TryDecodeEntireStorageError, TryState, diff --git a/substrate/frame/support/src/traits/view_function.rs b/substrate/frame/support/src/view_functions.rs similarity index 56% rename from substrate/frame/support/src/traits/view_function.rs rename to substrate/frame/support/src/view_functions.rs index 7b18999ea290..dd23fad94a4f 100644 --- a/substrate/frame/support/src/traits/view_function.rs +++ b/substrate/frame/support/src/view_functions.rs @@ -17,8 +17,48 @@ //! Traits for querying pallet view functions. -use codec::{DecodeAll, Encode, Output}; -use sp_core::{ViewFunctionDispatchError, ViewFunctionId}; +use alloc::vec::Vec; +use codec::{Decode, DecodeAll, Encode, Output}; +use scale_info::TypeInfo; +use sp_runtime::RuntimeDebug; + +/// The unique identifier for a view function. +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct ViewFunctionId { + /// The part of the id for dispatching view functions from the top level of the runtime. + /// + /// Specifies which view function grouping this view function belongs to. This could be a group + /// of view functions associated with a pallet, or a pallet agnostic group of view functions. + pub prefix: [u8; 16], + /// The part of the id for dispatching to a view function within a group. + pub suffix: [u8; 16], +} + +impl From for [u8; 32] { + fn from(value: ViewFunctionId) -> Self { + let mut output = [0u8; 32]; + output[..16].copy_from_slice(&value.prefix); + output[16..].copy_from_slice(&value.suffix); + output + } +} + +/// Error type for view function dispatching. +#[derive(Encode, Decode, RuntimeDebug, TypeInfo)] +pub enum ViewFunctionDispatchError { + /// View functions are not implemented for this runtime. + NotImplemented, + /// A view function with the given `ViewFunctionId` was not found + NotFound(ViewFunctionId), + /// Failed to decode the view function input. + Codec, +} + +impl From for ViewFunctionDispatchError { + fn from(_: codec::Error) -> Self { + ViewFunctionDispatchError::Codec + } +} /// Implemented by both pallets and the runtime. The runtime is dispatching by prefix using the /// pallet implementation of `ViewFunctionIdPrefix` then the pallet is dispatching by suffix using @@ -70,3 +110,19 @@ pub trait ViewFunction: DecodeAll { Ok(()) } } + +pub mod runtime_api { + use super::*; + + sp_api::decl_runtime_apis! { + #[api_version(1)] + /// Runtime API for executing view functions + pub trait RuntimeViewFunction { + /// Execute a view function query. + fn execute_view_function( + query_id: ViewFunctionId, + input: Vec, + ) -> Result, ViewFunctionDispatchError>; + } + } +} diff --git a/substrate/primitives/api/src/lib.rs b/substrate/primitives/api/src/lib.rs index dcf24c88cc36..8909d2b2e486 100644 --- a/substrate/primitives/api/src/lib.rs +++ b/substrate/primitives/api/src/lib.rs @@ -113,10 +113,9 @@ pub mod __private { pub use sp_runtime_interface::polkavm::{polkavm_abi, polkavm_export}; } -use alloc::vec::Vec; #[cfg(feature = "std")] pub use sp_core::traits::CallContext; -use sp_core::{OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; +use sp_core::OpaqueMetadata; #[cfg(feature = "std")] use sp_externalities::{Extension, Extensions}; #[cfg(feature = "std")] @@ -844,12 +843,6 @@ decl_runtime_apis! { /// This can be used to call `metadata_at_version`. fn metadata_versions() -> alloc::vec::Vec; } - - /// API for executing view functions - pub trait RuntimeViewFunction where { - /// Execute a view function query. - fn execute_view_function(query_id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError>; - } } sp_core::generate_feature_enabled_macro!(std_enabled, feature = "std", $); diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 79f25552fc16..454f61df7941 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -327,44 +327,6 @@ pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 { #[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum Void {} -/// Error type for view function dispatching. -#[derive(Encode, Decode, RuntimeDebug, TypeInfo)] -pub enum ViewFunctionDispatchError { - /// View functions are not implemented for this runtime. - NotImplemented, - /// A view function with the given `ViewFunctionId` was not found - NotFound(ViewFunctionId), - /// Failed to decode the view function input. - Codec, -} - -impl From for ViewFunctionDispatchError { - fn from(_: codec::Error) -> Self { - ViewFunctionDispatchError::Codec - } -} - -/// The unique identifier for a view function. -#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct ViewFunctionId { - /// The part of the id for dispatching view functions from the top level of the runtime. - /// - /// Specifies which view function grouping this view function belongs to. This could be a group - /// of view functions associated with a pallet, or a pallet agnostic group of view functions. - pub prefix: [u8; 16], - /// The part of the id for dispatching to a view function within a group. - pub suffix: [u8; 16], -} - -impl From for [u8; 32] { - fn from(value: ViewFunctionId) -> Self { - let mut output = [0u8; 32]; - output[..16].copy_from_slice(&value.prefix); - output[16..].copy_from_slice(&value.suffix); - output - } -} - /// Macro for creating `Maybe*` marker traits. /// /// Such a maybe-marker trait requires the given bound when `feature = std` and doesn't require diff --git a/templates/parachain/runtime/src/apis.rs b/templates/parachain/runtime/src/apis.rs index dbf8efa4d63b..d7da43b86af1 100644 --- a/templates/parachain/runtime/src/apis.rs +++ b/templates/parachain/runtime/src/apis.rs @@ -35,7 +35,7 @@ use frame_support::{ use pallet_aura::Authorities; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ traits::Block as BlockT, transaction_validity::{TransactionSource, TransactionValidity}, @@ -114,8 +114,8 @@ impl_runtime_apis! { } } - impl sp_api::RuntimeViewFunction for Runtime { - fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + impl frame_support::view_functions::runtime_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: frame_support::view_functions::ViewFunctionId, input: Vec) -> Result, frame_support::view_functions::ViewFunctionDispatchError> { Runtime::execute_view_function(id, input) } } diff --git a/templates/solochain/runtime/src/apis.rs b/templates/solochain/runtime/src/apis.rs index 44731056c67d..9dc588c43a2d 100644 --- a/templates/solochain/runtime/src/apis.rs +++ b/templates/solochain/runtime/src/apis.rs @@ -32,7 +32,7 @@ use frame_support::{ use pallet_grandpa::AuthorityId as GrandpaId; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ traits::{Block as BlockT, NumberFor}, transaction_validity::{TransactionSource, TransactionValidity}, @@ -75,8 +75,8 @@ impl_runtime_apis! { } } - impl sp_api::RuntimeViewFunction for Runtime { - fn execute_view_function(id: ViewFunctionId, input: Vec) -> Result, ViewFunctionDispatchError> { + impl frame_support::view_functions::runtime_api::RuntimeViewFunction for Runtime { + fn execute_view_function(id: frame_support::view_functions::ViewFunctionId, input: Vec) -> Result, frame_support::view_functions::ViewFunctionDispatchError> { Runtime::execute_view_function(id, input) } } From 06a30bead2621c46d5efa95ae8fc9be43befea16 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 28 Jan 2025 11:55:47 +0100 Subject: [PATCH 120/121] fix prdoc --- prdoc/pr_4722.prdoc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/prdoc/pr_4722.prdoc b/prdoc/pr_4722.prdoc index 4ae845ac549f..a5bdbbeb3df9 100644 --- a/prdoc/pr_4722.prdoc +++ b/prdoc/pr_4722.prdoc @@ -19,10 +19,6 @@ doc: crates: - name: frame-support bump: minor - - name: sp-core - bump: minor - - name: sp-api - bump: minor - name: sp-metadata-ir bump: major - name: frame-support-procedural From 00a0712a8355701d036859f15654fa32b48b9f83 Mon Sep 17 00:00:00 2001 From: giuseppere Date: Tue, 28 Jan 2025 12:03:29 +0100 Subject: [PATCH 121/121] Fix error message for missing output --- .../support/procedural/src/pallet/parse/view_functions.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs index 858392d80142..766bcb13da8b 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/view_functions.rs @@ -85,10 +85,7 @@ impl TryFrom for ViewFunctionDef { type Error = syn::Error; fn try_from(method: syn::ImplItemFn) -> Result { let syn::ReturnType::Type(_, type_) = method.sig.output else { - return Err(syn::Error::new( - method.sig.output.span(), - "view functions must return a value", - )) + return Err(syn::Error::new(method.sig.span(), "view functions must return a value")) }; Ok(Self {