-
Notifications
You must be signed in to change notification settings - Fork 809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement pallet view function queries #4722
Merged
Merged
Changes from 79 commits
Commits
Show all changes
144 commits
Select commit
Hold shift + click to select a range
264b902
Add view functions parsing and definition
ascjones 987802c
Add to view functions mod definition
ascjones 4f7a513
Adding traits and types and wiring up expansion
ascjones e8af3c2
Use ToTokens for expansion
ascjones 7038710
fmtl
ascjones 9267519
Adding generics etc to query struct
ascjones c6a4e42
fmt
ascjones 140f7d9
example with args
ascjones 23b9ebf
add example for testing
ascjones 86c5f2f
wire up codegen for view functions
ascjones f42c5c4
Generate DispatchQuery impl
ascjones ac6643e
Wire up runtime PalletQuery
ascjones 2ea6158
Wire up query args
ascjones 8b56a84
Add get value with args query
ascjones 06f9ffc
decode_all
ascjones 9ed181b
Merge branch 'master' into aj/view-functions
ascjones cd52010
Docs
ascjones 44707b3
Generate suffix from view_fn signature
ascjones afa1490
separate suffix id
ascjones f97a40c
WIP expand runtime level query
ascjones ddff4d0
Implement `DispatchQuery` for pallets even without queries
ascjones 538ea8a
WIP
ascjones db616f5
Use pallet name hash as query id prefix
ascjones fd3318b
Commented out code
ascjones a49e5f3
Inject runtime type
ascjones dd651ac
tidy
ascjones c7ff6c9
Add pallet with instances
ascjones 5a8e559
Pallet instances tests
ascjones e71b9de
Refactor tests
ascjones 22dd94b
Move execute from codegen to trait
ascjones 117ac87
WIP query runtime api
ascjones def7807
WIP query metadata
ascjones fbd4d3a
Fix up metadata generation
ascjones 04d763e
Add queries section to custom metadata
ascjones 86f35eb
Comment
ascjones 885fd3a
fix metadata gen
ascjones 3b563fb
move runtime api and core types to primitives
ascjones 43de841
Add some RuntimeQuery types
ascjones ebcf283
Fix up westend runtime
ascjones f0b1f36
Add RuntimeQuery derives
ascjones c6dd8a5
Wire up runtime API query methods
ascjones 81150ba
Use Runtime::execute_query generated method
ascjones 11cb3db
Fmt
ascjones 18b7deb
Merge branch 'master' into aj/view-functions
ascjones 21eeba7
Merge branch 'master' into aj/view-functions
ascjones 7b82da6
Master.into()
kianenigma fd0896c
Merge branch 'master' into aj/view-functions
ascjones 0bf818a
Merge branch 'master' into aj/view-functions
jsdw 7c71b8a
Rename `view_functions` to `view_functions_experimental`
ascjones 60d78fc
Remove Vec import
ascjones 19d6f65
Resore Vec import
ascjones f8e9d2b
Merge branch 'master' into aj/view-functions
ascjones b93cbf4
QueryDispatchError -> ViewFunctionDispatchError
ascjones b58b9ae
query -> view function naming WIP
ascjones c2c3883
query -> view function naming WIP
ascjones 0e92e5b
query -> view function naming
ascjones 62674bd
fmt
ascjones bb35aa4
query -> view function renaming WIP
ascjones f5c498e
query -> view function renaming WIP
ascjones 6acc73f
more query -> view function renaming
ascjones 282ce5e
more query -> view function renaming
ascjones 39d6e35
fmt
ascjones d35e6df
invoke
ascjones 9e3bc13
even more query -> view function renaming
ascjones 82e9fbc
final query -> view function renaming?
ascjones 09d3e75
Warnings/docs
ascjones b120001
Manual formatting
ascjones 49a5d73
More warnings, format
ascjones cf9c01e
Rename view function interface to groups
ascjones 9628070
Merge branch 'master' into aj/view-functions
ascjones cb97676
Add PR doc
ascjones d355f30
Merge branch 'master' into aj/view-functions
ascjones f88b646
Merge branch 'master' into aj/view-functions
ascjones b87bc0e
Renaming and implement view fn args
ascjones 5b4e2f1
Docs
ascjones 18b7d6d
Metadata tests and remove spaces from types.
ascjones 25479e2
Fmt
ascjones 402c29d
Merge branch 'master' into aj/view-functions
ascjones 24d00d9
Merge branch 'master' into aj/view-functions
jsdw 3fd9b5d
Merge branch 'master' into aj/view-functions
re-gius 15d74ea
fix `Cargo.toml` duplicate dependency
re-gius 6a042fb
format and fix toml files + Cargo.lock
re-gius c6a87fc
fix `Cargo.toml` of `pallet-example-view-functions`
re-gius 9e92079
TOML lint + `cargo clippy`
re-gius c2e175a
add pallets to prdoc
re-gius cdca6f5
Add `RuntimeViewFunction` to `DefaultConfig`
re-gius 2512093
Merge branch 'master' into aj/view-functions
re-gius e71d7d7
Add `RuntimeViewFunction`s to runtime config
re-gius 05f7caa
Spelling
ascjones fc1d7aa
Merge branch 'master' into aj/view-functions
ascjones 7a82560
fix `RuntimeViewFunction`
re-gius 87807aa
fix
re-gius d3bf83c
undo changes
re-gius ccbac76
Revert "undo changes"
re-gius 03c1c45
revert last 3 commits
re-gius d513cd1
revert last commits
re-gius 627c275
Revert "fix"
re-gius 227deb6
Revert "fix `RuntimeViewFunction`"
re-gius d234fdd
Revert "Add `RuntimeViewFunction`s to runtime config"
re-gius 68061a9
Revert "Add `RuntimeViewFunction` to `DefaultConfig`"
re-gius bab4cb0
fix toml formatting
re-gius 7b85388
Add `RuntimeViewFunction` type to all runtime configs
re-gius f176933
add missing `RuntimeViewFunction`
re-gius 90d45f6
Merge branch 'master' into aj/view-functions
re-gius 502c5be
small fix
re-gius a4d8b0c
Merge branch 'master' into aj/view-functions
re-gius c44d40e
Propagate runtime attributes to view function macros
re-gius 0819dac
nit
re-gius 9793b26
update errors msg in tests with view function errors
re-gius 9a50968
Merge branch 'master' into aj/view-functions
re-gius cf06441
fix prdoc
re-gius a372eec
prdoc nit
re-gius e0758a2
fix prdoc bumps
re-gius 73b41aa
fix semver bumps
re-gius 9b62146
Add `wasm32-unknown-unknown` target in `check-semver` CI
re-gius 320d7f4
Merge branch 'master' into aj/view-functions
re-gius e2ac06c
upgrade `parity-publish` version in `check-semver.yml`
re-gius 258373c
undo last change
re-gius b63e77a
update `rust-cache` version too
re-gius 578fa91
Revert "update `rust-cache` version too"
re-gius f3f321c
remove `RuntimeViewFunction` runtime type from `system` pallet
re-gius c642149
Merge branch 'master' into aj/view-functions
re-gius abe6f5c
remove unused import
re-gius 013e789
Update substrate/frame/support/procedural/src/pallet/parse/view_funct…
re-gius d7d614d
remove deprecated use of `sp-std`
re-gius f9be388
Update substrate/frame/support/src/traits/view_function.rs
re-gius 051dcdd
Update substrate/frame/support/src/traits/view_function.rs
re-gius dec2467
update `Cargo.lock`
re-gius e8ff55d
`cargo +nightly fmt --all`
re-gius 82f08ea
update prdoc semver bump
re-gius 98d3063
add `get_attributes()` method for pallet macros
re-gius 502afaf
Update substrate/frame/support/src/traits/view_function.rs
re-gius e0b76ed
Update substrate/frame/support/src/traits/view_function.rs
re-gius 73f62e4
fmt
re-gius 8e34251
remove `sp-std` from view functions macro
re-gius de5834c
Change `panic`s into compile errors
re-gius f4e8aa2
Add view functions to Runtime APIs in `westend` runtime + `solochain`…
re-gius 85ab0b7
remove unused pallet-level `Query` type
re-gius b04abd3
Merge branch 'master' into aj/view-functions
re-gius 08f711f
move `view_functions` declaration and traits to `frame_support`
re-gius 5e730ac
Merge branch 'master' into aj/view-functions
re-gius 06a30be
fix prdoc
re-gius 00a0712
Fix error message for missing output
re-gius 9e07f97
Merge branch 'master' into aj/view-functions
re-gius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: [ ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
[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-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 = [ | ||
"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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// 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::view_functions_experimental` api for service | ||
//! work. | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub mod tests; | ||
|
||
use frame_support::Parameter; | ||
use scale_info::TypeInfo; | ||
|
||
pub struct SomeType1; | ||
impl From<SomeType1> 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::*; | ||
|
||
#[pallet::error] | ||
pub enum Error<T> {} | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::storage] | ||
pub type SomeValue<T: Config> = StorageValue<_, u32>; | ||
|
||
#[pallet::storage] | ||
pub type SomeMap<T: Config> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; | ||
|
||
#[pallet::view_functions_experimental] | ||
impl<T: Config> Pallet<T> | ||
where | ||
T::AccountId: From<SomeType1> + SomeAssociation1, | ||
{ | ||
/// Query value no args. | ||
pub fn get_value() -> Option<u32> { | ||
SomeValue::<T>::get() | ||
} | ||
|
||
/// Query value with args. | ||
pub fn get_value_with_arg(key: u32) -> Option<u32> { | ||
SomeMap::<T>::get(key) | ||
} | ||
} | ||
} | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet2 { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
|
||
#[pallet::error] | ||
pub enum Error<T, I = ()> {} | ||
|
||
#[pallet::config] | ||
pub trait Config<I: 'static = ()>: frame_system::Config {} | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>); | ||
|
||
#[pallet::storage] | ||
pub type SomeValue<T: Config<I>, I: 'static = ()> = StorageValue<_, u32>; | ||
|
||
#[pallet::storage] | ||
pub type SomeMap<T: Config<I>, I: 'static = ()> = | ||
StorageMap<_, Twox64Concat, u32, u32, OptionQuery>; | ||
|
||
#[pallet::view_functions_experimental] | ||
impl<T: Config<I>, I: 'static> Pallet<T, I> | ||
where | ||
T::AccountId: From<SomeType1> + SomeAssociation1, | ||
{ | ||
/// Query value no args. | ||
pub fn get_value() -> Option<u32> { | ||
SomeValue::<T, I>::get() | ||
} | ||
|
||
/// Query value with args. | ||
pub fn get_value_with_arg(key: u32) -> Option<u32> { | ||
SomeMap::<T, I>::get(key) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'm wondering whether it's worth scoping this call a bit more eg
execute_pallet_view_function
, in case we want to have anexecute_runtime_view_function
in the future or something? Or would you see runtime-wide view functions also being callable through this same interface?