Attribute Macro pbc_contract_codegen::zk_on_compute_complete
source · #[zk_on_compute_complete]
Expand description
Computation complete zero-knowledge contract annotation
OPTIONAL HOOK: This is an optional hook, and is not required for a well-formed zero-knowledge contract. The default behaviour is to do nothing.
If the annotated function panics changes to the contract state will be rolled back.
Annotated function is automatically called when a zero-knowledge computation is finished; this
can only happen after the use of
ZkStateChange::StartComputation
.
This hook is exclusively called by the blockchain itself, and cannot be called manually from
the dashboard, nor from another contract.
Allows the contract to automatically react to ZK computation completion.
Must have a signature of the following format:
#[zk_on_compute_complete(shortname = 0x13)]
pub fn function_name(
context: ContractContext,
state: ContractState,
zk_state: ZkState<Metadata>,
created_variables: Vec<SecretVarId>,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>)
with following constraints:
ContractState
must be the type annotated withstate
, and must have anpbc_traits::ReadWriteState
implementation.- The
Metadata
type given toZkState
andZkInputDef
must be identical both for individual functions, and across the entire contract. - This function is only available with the
zk
feature enabled.
The function receives:
ContractState
: The previous states.ZkState<Metadata>
: The current state of the zk computation.ContractContext
: The current contract context.Vec<SecretVarId>
: Ids of the computation’s output variables.
The function must return a tuple containing:
- New public state.
- Vector of
EventGroup
; a list of interactions with other contracts. Vec<ZkStateChange>
declaring how to change the zk contract state.
§Example
A commonly used pattern is to open the output variables given to zk_on_compute_complete
, as
demonstrated in the following example:
type ContractState = u32;
type Metadata = u32;
#[zk_on_compute_complete(shortname=0x13)]
pub fn zk_on_compute_complete(
context: ContractContext,
state: ContractState,
zk_state: ZkState<Metadata>,
created_variables: Vec<SecretVarId>,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>) {
(state, vec![], vec![ZkStateChange::OpenVariables { variables: created_variables }])
}