Implement pallet view function queries (#4722)
Closes #216. This PR allows pallets to define a `view_functions` impl like so: ```rust #[pallet::view_functions] 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) } } ``` ### `QueryId` Each view function is uniquely identified by a `QueryId`, which for this implementation is generated by: ```twox_128(pallet_name) ++ twox_128("fn_name(fnarg_types) -> return_ty")``` The prefix `twox_128(pallet_name)` is the same as the storage prefix for pallets and take into account multiple instances of the same pallet. The suffix is generated from the fn type signature so is guaranteed to be unique for that pallet impl. For one of the view fns in the example above it would be `twox_128("get_value_with_arg(u32) -> Option<u32>")`. It is a kn...
parent
4302f74f
Showing
- .github/workflows/check-semver.yml 1 addition, 0 deletions.github/workflows/check-semver.yml
- Cargo.lock 19 additions, 0 deletionsCargo.lock
- Cargo.toml 2 additions, 0 deletionsCargo.toml
- cumulus/pallets/weight-reclaim/src/tests.rs 2 additions, 1 deletioncumulus/pallets/weight-reclaim/src/tests.rs
- docs/sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs 8 additions, 1 deletion.../sdk/src/reference_docs/chain_spec_runtime/src/runtime.rs
- polkadot/runtime/westend/src/lib.rs 8 additions, 1 deletionpolkadot/runtime/westend/src/lib.rs
- prdoc/pr_4722.prdoc 33 additions, 0 deletionsprdoc/pr_4722.prdoc
- substrate/bin/node/runtime/src/lib.rs 8 additions, 1 deletionsubstrate/bin/node/runtime/src/lib.rs
- substrate/frame/examples/Cargo.toml 3 additions, 0 deletionssubstrate/frame/examples/Cargo.toml
- substrate/frame/examples/src/lib.rs 3 additions, 0 deletionssubstrate/frame/examples/src/lib.rs
- substrate/frame/examples/view-functions/Cargo.toml 61 additions, 0 deletionssubstrate/frame/examples/view-functions/Cargo.toml
- substrate/frame/examples/view-functions/src/lib.rs 114 additions, 0 deletionssubstrate/frame/examples/view-functions/src/lib.rs
- substrate/frame/examples/view-functions/src/tests.rs 188 additions, 0 deletionssubstrate/frame/examples/view-functions/src/tests.rs
- substrate/frame/support/procedural/examples/proc_main/main.rs 2 additions, 1 deletion...trate/frame/support/procedural/examples/proc_main/main.rs
- substrate/frame/support/procedural/examples/proc_main/runtime.rs 2 additions, 1 deletion...te/frame/support/procedural/examples/proc_main/runtime.rs
- substrate/frame/support/procedural/src/construct_runtime/expand/call.rs 1 addition, 10 deletions...e/support/procedural/src/construct_runtime/expand/call.rs
- substrate/frame/support/procedural/src/construct_runtime/expand/config.rs 1 addition, 9 deletions...support/procedural/src/construct_runtime/expand/config.rs
- substrate/frame/support/procedural/src/construct_runtime/expand/inherent.rs 1 addition, 9 deletions...pport/procedural/src/construct_runtime/expand/inherent.rs
- substrate/frame/support/procedural/src/construct_runtime/expand/metadata.rs 19 additions, 9 deletions...pport/procedural/src/construct_runtime/expand/metadata.rs
- substrate/frame/support/procedural/src/construct_runtime/expand/mod.rs 2 additions, 0 deletions...me/support/procedural/src/construct_runtime/expand/mod.rs
Please register or sign in to comment