Implement pallet view function queries#4722
Merged
re-gius merged 144 commits intoparitytech:masterfrom Jan 28, 2025
Merged
Conversation
ordian
added a commit
that referenced
this pull request
Feb 3, 2025
* master: Remove warnings by cleaning up the `Cargo.toml` (#7416) [Backport] Version bumps from stable2412-1 + prdocs reorg (#7401) fix pre-dispatch PoV underweight for ParasInherent (#7378) malus-collator: implement malicious collator submitting same collation to all backing groups (#6924) `fatxpool`: use tracing for logging (#6897) Improvements for Weekly bench (#7390) Replace derivative dependency with derive-where (#7324) Add support for feature `pallet_balances/insecure_zero_ed` in benchmarks and testing (#7379) Fix Snowbridge benchmark tests (#7296) Bridges small nits/improvements (#7383) Migrating cumulus-pallet-session-benchmarking to Benchmarking V2 (#6564) [pallet-revive] implement the block author API (#7198) Use checked math in frame-balances named_reserve (#7365) move installation of frame-omni-bencher into a cmd.py itself (#7372) remove old bench & revert the frame-weight-template (#7362) ci: fix workflow permissions (#7366) [net/libp2p] Use raw `Identify` observed addresses to discover external addresses (#7338) Improve `set_validation_data` error message. (#7359) Implement pallet view function queries (#4722)
Closed
|
This pull request has been mentioned on Polkadot Forum. There might be relevant details there: https://forum.polkadot.network/t/stabilizing-v16-metadata/12352/1 |
github-merge-queue bot
pushed a commit
that referenced
this pull request
Apr 17, 2025
Pallet view functions are no longer marked as experimental, and their use is suggested starting from this PR. Your feedback is more than welcome. See [docs](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.view_functions_experimental.html) for a quick introduction. For more context, you can look at: - #4722 - #7412 - #7830 : discussion on possible changes to pallet view functions --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Krayt78
pushed a commit
to Krayt78/polkadot-sdk
that referenced
this pull request
Apr 18, 2025
Pallet view functions are no longer marked as experimental, and their use is suggested starting from this PR. Your feedback is more than welcome. See [docs](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.view_functions_experimental.html) for a quick introduction. For more context, you can look at: - paritytech#4722 - paritytech#7412 - paritytech#7830 : discussion on possible changes to pallet view functions --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This was referenced Apr 22, 2025
6 tasks
castillax
pushed a commit
that referenced
this pull request
May 12, 2025
Pallet view functions are no longer marked as experimental, and their use is suggested starting from this PR. Your feedback is more than welcome. See [docs](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.view_functions_experimental.html) for a quick introduction. For more context, you can look at: - #4722 - #7412 - #7830 : discussion on possible changes to pallet view functions --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
7 tasks
This was referenced Jun 10, 2025
16 tasks
This was referenced Jul 16, 2025
fellowship-merge-bot bot
pushed a commit
to polkadot-fellows/runtimes
that referenced
this pull request
Oct 22, 2025
FRAME "View Functions" (paritytech/polkadot-sdk#216) support was implemented and released in Polkadot SDK with paritytech/polkadot-sdk#4722, but it was never exposed on the System Chains' runtimes. This PR adds the missing `execute_view_function()` to all System Chains' Runtime APIs.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #216.
This PR allows pallets to define a
view_functionsimpl like so:QueryIdEach 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 known limitation that only the type names themselves are taken into account: in the case of type aliases the signature may have the same underlying types but a different id; for generics the concrete types may be different but the signatures will remain the same.The existing Runtime
Calldispatchables are addressed by their concatenated indicespallet_index ++ call_index, and the dispatching is handled by the SCALE decoding of theRuntimeCallEnum::PalletVariant(PalletCallEnum::dispatchable_variant(payload)). Forview_functionsthe runtime/pallet generated enum structure is replaced by implementing theDispatchQuerytrait on the outer (runtime) scope, dispatching to a pallet based on the id prefix, and the inner (pallet) scope dispatching to the specific function based on the id suffix.Future implementations could also modify/extend this scheme and routing to pallet agnostic queries.
Executing externally
These view functions can be executed externally via the system runtime api:
XCQCurrently there is work going on by @xlc to implement
XCQwhich may eventually supersede this work.It may be that we still need the fixed function local query dispatching in addition to XCQ, in the same way that we have chain specific runtime dispatchables and XCM.
I have kept this in mind and the high level query API is agnostic to the underlying query dispatch and execution. I am just providing the implementation for the
view_functiondefinition.Metadata
Currently I am utilizing the
customsection of the frame metadata, to avoid modifying the official metadata format until this is standardized.vs
runtime_apiThere are similarities with
runtime_apis, some differences being:QueryIdwill change if the signature changes.Calling from contracts
Future work would be to add
weightannotations to the view function queries, and a host function topallet_contractsto allow executing these queries from contracts.TODO
runtime_api