Attribute Macro host

Source
#[host]
Expand description

Apply host state wrapper to host functions.

Supported meta attributes:

  • fallible - if the host function executes fallible call.
  • wgas - if the host function supports with-gas version.
  • cost - RuntimeCosts definition, for example #[host(cost = RuntimeCosts::Null)]
  • err - Structure definition with error code, for example #[host(err = ErrorWithHash)]

§Example

#[host(fallible, wgas, cost = RuntimeCosts::Reply(len))]
pub fn reply(
    ctx: &mut R,
    payload_ptr: u32,
    len: u32,
    value_ptr: u32,
    delay: u32,
) -> Result<(), R::Error> {
    let read_payload = ctx.register_read(payload_ptr, len);
    let value = ctx.register_and_read_value(value_ptr)?;
    let payload = ctx.read(read_payload)?.try_into()?;

    let state = ctx.host_state_mut();
    state.ext.reply(ReplyPacket::new(payload, value), delay)
        .map_err(Into::into)
}

will generate

pub fn reply(ctx: &mut R,
    payload_ptr: u32,
    len: u32,
    value_ptr: u32,
    delay: u32
) -> Result<(), R::Error> {
    syscall_trace!("reply", payload_ptr, len, value_ptr, delay, err_mid_ptr);

    ctx.run_fallible::<_, _, ErrorWithHash>(err_mid_ptr, RuntimeCosts::Reply(len), |ctx| {
        // ...
    })
}

pub fn reply_wgas(
    ctx: &mut R,
    payload_ptr: u32,
    len: u32,
    gas_limit: u64,
    value_ptr: u32,
    delay: u32
) -> Result<(), R::Error> {
    syscall_trace!("reply_wgas", payload_ptr, len, gas_limit, value_ptr, delay, err_mid_ptr);

    ctx.run_fallible::<_, _, ErrorWithHash>(err_mid_ptr, RuntimeCosts::ReplyWGas(len), |ctx| {
        // ...
    })
}