mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
runtime-interface: Implement register_only functions (#10640)
* runtime-interface: Implement `register_only` functions The runtime interface supports versioning of functions. Currently, if you add a new function it will be used by the runtime automatically. This results in requiring all nodes of a network to upgrade before the runtime is upgraded, otherwise they will fail to instantiate the new runtime because of missing host functions. This pr introduces `register_only` functions. This can be used when a new runtime interface function should be introduced, but the actual usage can be deferred. This means that nodes will have the host function for this, but the runtime will still use the old version of the function when being compiled for wasm. However, when a runtime is enacted that uses the new host function, the "old nodes" will already have the host function and will continue to work. * Update primitives/runtime-interface/src/lib.rs Co-authored-by: cheme <emericchevalier.pro@gmail.com> * Update primitives/runtime-interface/proc-macro/src/utils.rs Co-authored-by: cheme <emericchevalier.pro@gmail.com> * FMT Co-authored-by: cheme <emericchevalier.pro@gmail.com>
This commit is contained in:
@@ -153,6 +153,22 @@ pub use sp_std;
|
||||
/// [17].to_vec()
|
||||
/// }
|
||||
///
|
||||
/// /// Call function, different version and only being registered.
|
||||
/// ///
|
||||
/// /// This `register_only` version is only being registered, aka exposed to the runtime,
|
||||
/// /// but the runtime will still use the version 2 of this function. This is useful for when
|
||||
/// /// new host functions should be introduced. Adding new host functions requires that all
|
||||
/// /// nodes have the host functions available, because otherwise they fail at instantiation
|
||||
/// /// of the runtime. With `register_only` the function will not be used when compiling the
|
||||
/// /// runtime, but it will already be there for a future version of the runtime that will
|
||||
/// /// switch to using these host function.
|
||||
/// #[version(3, register_only)]
|
||||
/// fn call(data: &[u8]) -> Vec<u8> {
|
||||
/// // Here you could call some rather complex code that only compiles on native or
|
||||
/// // is way faster in native than executing it in wasm.
|
||||
/// [18].to_vec()
|
||||
/// }
|
||||
///
|
||||
/// /// A function can take a `&self` or `&mut self` argument to get access to the
|
||||
/// /// `Externalities`. (The generated method does not require
|
||||
/// /// this argument, so the function can be called just with the `optional` argument)
|
||||
@@ -177,12 +193,14 @@ pub use sp_std;
|
||||
/// trait Interface {
|
||||
/// fn call_version_1(data: &[u8]) -> Vec<u8>;
|
||||
/// fn call_version_2(data: &[u8]) -> Vec<u8>;
|
||||
/// fn call_version_3(data: &[u8]) -> Vec<u8>;
|
||||
/// fn set_or_clear_version_1(&mut self, optional: Option<Vec<u8>>);
|
||||
/// }
|
||||
///
|
||||
/// impl Interface for &mut dyn sp_externalities::Externalities {
|
||||
/// fn call_version_1(data: &[u8]) -> Vec<u8> { Vec::new() }
|
||||
/// fn call_version_2(data: &[u8]) -> Vec<u8> { [17].to_vec() }
|
||||
/// fn call_version_3(data: &[u8]) -> Vec<u8> { [18].to_vec() }
|
||||
/// fn set_or_clear_version_1(&mut self, optional: Option<Vec<u8>>) {
|
||||
/// match optional {
|
||||
/// Some(value) => self.set_storage([1, 2, 3, 4].to_vec(), value),
|
||||
@@ -204,6 +222,10 @@ pub use sp_std;
|
||||
/// <&mut dyn sp_externalities::Externalities as Interface>::call_version_2(data)
|
||||
/// }
|
||||
///
|
||||
/// fn call_version_3(data: &[u8]) -> Vec<u8> {
|
||||
/// <&mut dyn sp_externalities::Externalities as Interface>::call_version_3(data)
|
||||
/// }
|
||||
///
|
||||
/// pub fn set_or_clear(optional: Option<Vec<u8>>) {
|
||||
/// set_or_clear_version_1(optional)
|
||||
/// }
|
||||
@@ -285,8 +307,8 @@ pub use sp_std;
|
||||
/// This instructs the macro to make two significant changes to the generated code:
|
||||
///
|
||||
/// 1. The generated functions are not callable from the native side.
|
||||
/// 2. The trait as shown above is not implemented for `Externalities` and is instead
|
||||
/// implemented for `FunctionExecutor` (from `sp-wasm-interface`).
|
||||
/// 2. The trait as shown above is not implemented for [`Externalities`] and is instead
|
||||
/// implemented for `FunctionExecutor` (from `sp-wasm-interface`).
|
||||
///
|
||||
/// # Disable tracing
|
||||
/// By addding `no_tracing` to the list of options you can prevent the wasm-side interface from
|
||||
|
||||
Reference in New Issue
Block a user