Crate jam_pvm_common

Source
Expand description

The main JAM PVM API for creating authorizers and services on JAM. This includes a trait-based invocation entry-point API as well as host-calls functions and types for working with them.

In order to create a PVM executable containing a JAM service or authorizer, you must implement the Service or Authorizer in some type and then pass your type into the declare_service! or declare_authorizer! macro, respectively. This will generate the necessary entry-points for the PVM to call into your implementation.

§Example Service

extern crate alloc;
use alloc::vec::Vec;
use jam_pvm_common::{declare_service, Service, accumulate::set_storage};
use jam_types::*;

struct MyService;
declare_service!(MyService);

impl Service for MyService {
    fn refine(
        _id: ServiceId,
        payload: WorkPayload,
        _package_info: PackageInfo,
        _extrinsics: Vec<Vec<u8>>,
    ) -> WorkOutput {
        [&b"Hello "[..], payload.take().as_slice()].concat().into()
    }
    fn accumulate(_slot: Slot, _id: ServiceId, items: Vec<AccumulateItem>) -> Option<Hash> {
        for item in items.into_iter() {
            if let Ok(data) = item.result {
                set_storage(item.package.as_slice(), &data).expect("not enough balance?!");
            }
        }
        None
    }
    fn on_transfer(_slot: Slot, _id: ServiceId, _items: Vec<TransferRecord>) {}
}

§Host-calls

The host-calls available to a service or authorizer are split into four modules:

Each module contains a set of functions that can be called from the respective entry-point function. These functions are used to interact with the PVM and the blockchain state.

§Logging

Five logging macros are provided similar to those of the log crate, debug, info, warn, error, and trace. These macros are used with the non-standard PolkaJam log host-call and the format macro. The host environment is responsible for forwarding these logs to the appropriate destination.

§Features

  • authorizer: Enables the authorizer API.
  • service: Enables the service API.
  • logging: Enables the logging service; without the logging macros will evaluate any operands but otherwise have no effect.

Modules§

Macros§

  • Log a message with the debug level. Regular formatting may be used.
  • Declare that this crate is a JAM authorizer characterized by $auth_impl and create necessary entry points.
  • Declare that this crate is a JAM service characterized by $service_impl and create necessary entry points.
  • Log a message with the error level. Regular formatting may be used.
  • Log a message with the info level. Regular formatting may be used.
  • Log a message with the trace level. Regular formatting may be used.
  • Log a message with the warn level. Regular formatting may be used.

Enums§

Traits§

  • The invocation trait for a JAM authorizer.
  • The invocation trait for a JAM service.

Type Aliases§