pub struct DelegateCtx { /* private fields */ }Expand description
Opaque handle to the delegate’s execution environment.
Provides access to:
- Temporary context: State shared within a single message batch (reset between calls)
- Persistent secrets: Encrypted storage that survives across all invocations
- Contract state (V2): Direct synchronous access to local contract state
§Context Methods
§Secret Methods
§Contract Methods (V2)
§Delegate Management Methods (V2)
Implementations§
Source§impl DelegateCtx
impl DelegateCtx
Sourcepub fn read(&self) -> Vec<u8> ⓘ
pub fn read(&self) -> Vec<u8> ⓘ
Read the current context bytes.
Returns an empty Vec if no context has been written.
Sourcepub fn read_into(&self, buf: &mut [u8]) -> usize
pub fn read_into(&self, buf: &mut [u8]) -> usize
Read context into a provided buffer.
Returns the number of bytes actually read.
Sourcepub fn write(&mut self, data: &[u8]) -> bool
pub fn write(&mut self, data: &[u8]) -> bool
Write new context bytes, replacing any existing content.
Returns true on success, false on error.
Sourcepub fn get_secret_len(&self, key: &[u8]) -> Option<usize>
pub fn get_secret_len(&self, key: &[u8]) -> Option<usize>
Get the length of a secret without retrieving its value.
Returns None if the secret does not exist.
Sourcepub fn get_secret(&self, key: &[u8]) -> Option<Vec<u8>>
pub fn get_secret(&self, key: &[u8]) -> Option<Vec<u8>>
Get a secret by key.
Returns None if the secret does not exist.
Sourcepub fn set_secret(&mut self, key: &[u8], value: &[u8]) -> bool
pub fn set_secret(&mut self, key: &[u8], value: &[u8]) -> bool
Store a secret.
Returns true on success, false on error.
Sourcepub fn has_secret(&self, key: &[u8]) -> bool
pub fn has_secret(&self, key: &[u8]) -> bool
Check if a secret exists.
Sourcepub fn remove_secret(&mut self, key: &[u8]) -> bool
pub fn remove_secret(&mut self, key: &[u8]) -> bool
Remove a secret.
Returns true if the secret was removed, false if it didn’t exist.
Sourcepub fn list_secrets(&self, prefix: &[u8]) -> Vec<Vec<u8>>
pub fn list_secrets(&self, prefix: &[u8]) -> Vec<Vec<u8>>
Enumerate the keys of every secret this delegate has stored whose raw
key begins with prefix (pass an empty slice to list all keys).
Returns the matching raw keys (the same byte strings originally passed
to set_secret). Order is unspecified. The host
caps the number of keys returned; if storage holds more matching keys
than the cap, the list is truncated (callers needing exhaustive
enumeration should narrow the prefix).
This closes the gap that previously forced apps storing an open-ended
key family (e.g. room:<owner_vk>) to maintain their own key registry:
after a delegate-WASM rebuild the delegate can now rediscover what it
has stored instead of probing a hardcoded key set.
Sourcepub fn get_contract_state(&self, instance_id: &[u8; 32]) -> Option<Vec<u8>>
pub fn get_contract_state(&self, instance_id: &[u8; 32]) -> Option<Vec<u8>>
Get contract state by instance ID.
Returns Some(state_bytes) if the contract exists locally,
None if not found or on error.
Uses a two-step protocol: first queries the state length, then reads the state bytes into an allocated buffer.
Sourcepub fn put_contract_state(
&mut self,
instance_id: &[u8; 32],
state: &[u8],
) -> bool
pub fn put_contract_state( &mut self, instance_id: &[u8; 32], state: &[u8], ) -> bool
Store (PUT) contract state by instance ID.
The contract’s code must already be registered in the runtime’s contract
store. Returns true on success, false on error.
Sourcepub fn update_contract_state(
&mut self,
instance_id: &[u8; 32],
state: &[u8],
) -> bool
pub fn update_contract_state( &mut self, instance_id: &[u8; 32], state: &[u8], ) -> bool
Update contract state by instance ID.
Like put_contract_state, but only succeeds if the contract already has
stored state. This performs a full state replacement (not a delta-based
update through the contract’s update_state logic). Returns true on
success, false if no prior state exists or on other errors.
Sourcepub fn subscribe_contract(&mut self, instance_id: &[u8; 32]) -> bool
pub fn subscribe_contract(&mut self, instance_id: &[u8; 32]) -> bool
Subscribe to contract updates by instance ID.
Registers interest in receiving notifications when the contract’s state changes. Currently validates that the contract is known and returns success; actual notification delivery is a follow-up.
Returns true on success, false if the contract is unknown or on error.
Sourcepub fn create_delegate(
&mut self,
wasm_code: &[u8],
params: &[u8],
cipher: &[u8; 32],
nonce: &[u8; 24],
) -> Result<([u8; 32], [u8; 32]), i32>
pub fn create_delegate( &mut self, wasm_code: &[u8], params: &[u8], cipher: &[u8; 32], nonce: &[u8; 24], ) -> Result<([u8; 32], [u8; 32]), i32>
Create a new child delegate from WASM bytecode and parameters.
This V2 host function allows a delegate to spawn new delegates at runtime. The child delegate is registered in the node’s delegate store and secret store with the provided cipher and nonce.
Returns Ok((key_hash, code_hash)) where both are 32-byte arrays identifying
the newly created delegate. Returns Err(error_code) on failure.
§Resource Limits
- Maximum creation depth: 4 (prevents fork bombs)
- Maximum creations per process() call: 8
§Error Codes
-1: Called outside process() context-4: Invalid parameter-9: WASM memory bounds violation-20: Depth limit exceeded-21: Per-call creation limit exceeded-23: Invalid WASM module-24: Store registration failed