Expand description
Host function API for delegates.
This module provides synchronous access to delegate context, secrets, and contract state via host functions, eliminating the need for message round-trips.
§Example
use freenet_stdlib::prelude::*;
#[delegate]
impl DelegateInterface for MyDelegate {
fn process(
ctx: &mut DelegateCtx,
_params: Parameters<'static>,
_attested: Option<&'static [u8]>,
message: InboundDelegateMsg,
) -> Result<Vec<OutboundDelegateMsg>, DelegateError> {
// Read/write temporary context
let data = ctx.read();
ctx.write(b"new state");
// Access persistent secrets
if let Some(key) = ctx.get_secret(b"private_key") {
// use key...
}
ctx.set_secret(b"new_secret", b"value");
// V2: Direct contract access (no round-trips!)
let contract_id = [0u8; 32]; // your contract instance ID
if let Some(state) = ctx.get_contract_state(&contract_id) {
// process state...
}
ctx.put_contract_state(&contract_id, b"new state");
Ok(vec![])
}
}§Context vs Secrets vs Contracts
-
Context (
read/write): Temporary state within a single message batch. Reset between separate runtime calls. Use for intermediate processing state. -
Secrets (
get_secret/set_secret): Persistent encrypted storage. Survives across all delegate invocations. Use for private keys, tokens, etc. -
Contracts (
get_contract_state/put_contract_state/update_contract_state/subscribe_contract): V2 host functions for direct contract state access. Synchronous local reads/writes — no request/response round-trips.
§Error Codes
Host functions return negative values to indicate errors:
| Code | Meaning |
|---|---|
| 0 | Success |
| -1 | Called outside process() context |
| -2 | Secret not found |
| -3 | Storage operation failed |
| -4 | Invalid parameter (e.g., negative length) |
| -5 | Context too large (exceeds i32::MAX) |
| -6 | Buffer too small |
| -7 | Contract not found in local store |
| -8 | Internal state store error |
| -9 | WASM memory bounds violation |
| -10 | Contract code not registered |
The wrapper methods in DelegateCtx handle these error codes and present
a more ergonomic API.
Modules§
- error_
codes - Error codes returned by host functions.
Structs§
- Delegate
Ctx - Opaque handle to the delegate’s execution environment.