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