-
Notifications
You must be signed in to change notification settings - Fork 766
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
base: master
Are you sure you want to change the base?
Conversation
@@ -843,6 +844,12 @@ decl_runtime_apis! { | |||
/// This can be used to call `metadata_at_version`. | |||
fn metadata_versions() -> alloc::vec::Vec<u32>; | |||
} | |||
|
|||
/// API for executing view function queriess | |||
pub trait RuntimeViewFunction where { |
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.
Please move to frame-support
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.
I can't see any other decl_runtime_apis!
in frame-support
. Is sp-api
not the place to have the common runtime api definitions?
I'm currently working on fixing CI - only small changes |
Closes #216.
This PR allows pallets to define a
view_functions
impl like so: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 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
Call
dispatchables 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_functions
the runtime/pallet generated enum structure is replaced by implementing theDispatchQuery
trait 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:
XCQ
Currently there is work going on by @xlc to implement
XCQ
which 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_function
definition.Metadata
Currently I am utilizing the
custom
section of the frame metadata, to avoid modifying the official metadata format until this is standardized.vs
runtime_api
There are similarities with
runtime_apis
, some differences being:QueryId
will change if the signature changes.Calling from contracts
Future work would be to add
weight
annotations to the view function queries, and a host function topallet_contracts
to allow executing these queries from contracts.TODO
runtime_api