jam_pvm_common/authorizer.rs
1use jam_types::{AuthOutput, AuthParam, CoreIndex, WorkPackage};
2
3/// Declare that this crate is a JAM authorizer characterized by `$auth_impl` and create necessary
4/// entry points.
5///
6/// - `$auth_impl` must implement the [Authorizer] trait.
7#[macro_export]
8macro_rules! declare_authorizer {
9 ($auth_impl: ty) => {
10 #[polkavm_derive::polkavm_export]
11 extern "C" fn is_authorized_ext(ptr: u32, size: u32) -> (u64, u64) {
12 let (param, package, core_index): (AuthParam, WorkPackage, CoreIndex) =
13 $crate::mem::decode_buf(ptr, size);
14 let result =
15 <$auth_impl as $crate::Authorizer>::is_authorized(param, package, core_index);
16 ((&result).as_ptr() as u64, result.len() as u64)
17 }
18 };
19}
20
21/// The invocation trait for a JAM authorizer.
22///
23/// The [declare_authorizer] macro requires that its parameter implement this trait.
24pub trait Authorizer {
25 /// The single entry-point of this PVM module, this determines whether a given Work Package
26 /// should be authorized to run on a given core.
27 ///
28 /// - `param`: The authorizer-parameter which parameterizes this logic in some way. (This can
29 /// also be found in the Work Package itself, but it provided here for convenience.)
30 /// - `package`: The Work Package to be authorized. It is guaranteed that the
31 /// `package.authorizer.code_hash` identifies this Authorizer logic. The Work Package includes
32 /// the `authorization` field which is freely settable by the Work Package builder in order to
33 /// authorize the package against this (parameterized) authorizer.
34 /// - `core_index`: The index of the core on which the Work Package will be executed.
35 ///
36 /// Returns the authorization output, an opaque blob which will be passed into both Refine and
37 /// Accumulate for all Work Items in `package`. If `package` is not authorized, then this should
38 /// panic instead.
39 fn is_authorized(param: AuthParam, package: WorkPackage, core_index: CoreIndex) -> AuthOutput;
40}