-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement pallet view function queries #4722
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
Changes from 118 commits
264b902
987802c
4f7a513
e8af3c2
7038710
9267519
c6a4e42
140f7d9
23b9ebf
86c5f2f
f42c5c4
ac6643e
2ea6158
8b56a84
06f9ffc
9ed181b
cd52010
44707b3
afa1490
f97a40c
ddff4d0
538ea8a
db616f5
fd3318b
a49e5f3
dd651ac
c7ff6c9
5a8e559
e71b9de
22dd94b
117ac87
def7807
fbd4d3a
04d763e
86f35eb
885fd3a
3b563fb
43de841
ebcf283
f0b1f36
c6dd8a5
81150ba
11cb3db
18b7deb
21eeba7
7b82da6
fd0896c
0bf818a
7c71b8a
60d78fc
19d6f65
f8e9d2b
b93cbf4
b58b9ae
c2c3883
0e92e5b
62674bd
bb35aa4
f5c498e
6acc73f
282ce5e
39d6e35
d35e6df
9e3bc13
82e9fbc
09d3e75
b120001
49a5d73
cf9c01e
9628070
cb97676
d355f30
f88b646
b87bc0e
5b4e2f1
18b7d6d
25479e2
402c29d
24d00d9
3fd9b5d
15d74ea
6a042fb
c6a87fc
9e92079
c2e175a
cdca6f5
2512093
e71d7d7
05f7caa
fc1d7aa
7a82560
87807aa
d3bf83c
ccbac76
03c1c45
d513cd1
627c275
227deb6
d234fdd
68061a9
bab4cb0
7b85388
f176933
90d45f6
502c5be
a4d8b0c
c44d40e
0819dac
9793b26
9a50968
cf06441
a372eec
e0758a2
73b41aa
9b62146
320d7f4
e2ac06c
258373c
b63e77a
578fa91
f3f321c
c642149
abe6f5c
013e789
d7d614d
f9be388
051dcdd
dec2467
e8ff55d
82f08ea
98d3063
502afaf
e0b76ed
73f62e4
8e34251
de5834c
f4e8aa2
85ab0b7
b04abd3
08f711f
5e730ac
06a30be
00a0712
9e07f97
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # 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: | ||
| - name: frame-support | ||
| bump: minor | ||
| - name: frame-system | ||
| bump: major | ||
| - name: sp-core | ||
| bump: minor | ||
| - name: sp-api | ||
| bump: minor | ||
| - name: sp-metadata-ir | ||
| bump: major | ||
| - name: frame-support-procedural | ||
| bump: patch | ||
| - name: pallet-example-view-functions | ||
| bump: patch | ||
| - name: cumulus-pov-validator | ||
| bump: none | ||
| - name: cumulus-pallet-weight-reclaim | ||
| bump: minor | ||
| - name: westend-runtime | ||
| bump: minor |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,7 @@ use sp_consensus_beefy::{ | |
| mmr::MmrLeafVersion, | ||
| }; | ||
| use sp_consensus_grandpa::AuthorityId as GrandpaId; | ||
| use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160}; | ||
| use sp_core::{crypto::KeyTypeId, OpaqueMetadata, ViewFunctionDispatchError, ViewFunctionId, H160}; | ||
| use sp_inherents::{CheckInherentsResult, InherentData}; | ||
| use sp_runtime::{ | ||
| curve::PiecewiseLinear, | ||
|
|
@@ -2386,7 +2386,8 @@ mod runtime { | |
| RuntimeHoldReason, | ||
| RuntimeSlashReason, | ||
| RuntimeLockId, | ||
| RuntimeTask | ||
| RuntimeTask, | ||
| RuntimeViewFunction | ||
| )] | ||
| pub struct Runtime; | ||
|
|
||
|
|
@@ -2930,6 +2931,12 @@ impl_runtime_apis! { | |
| } | ||
| } | ||
|
|
||
| impl sp_api::RuntimeViewFunction<Block> for Runtime { | ||
| fn execute_view_function(id: ViewFunctionId, input: Vec<u8>) -> Result<Vec<u8>, ViewFunctionDispatchError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| Runtime::execute_view_function(id, input) | ||
| } | ||
| } | ||
|
|
||
| impl sp_block_builder::BlockBuilder<Block> for Runtime { | ||
| fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult { | ||
| Executive::apply_extrinsic(extrinsic) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| [package] | ||
| name = "pallet-example-view-functions" | ||
| 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" | ||
|
|
||
| [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, workspace = true } | ||
| frame-metadata = { features = ["current"], workspace = true } | ||
| log = { workspace = true } | ||
| scale-info = { version = "2.11.1", default-features = false, features = ["derive"], workspace = true } | ||
|
|
||
| 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", 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, workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| pretty_assertions = { version = "1.3.0" } | ||
|
|
||
| [features] | ||
| 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", | ||
| ] | ||
| 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", | ||
| ] |
| 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) | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.