Attribute Macro pbc_contract_codegen::zk_on_variables_opened
source · #[zk_on_variables_opened]
Expand description
Secret variable opened 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 contract opens one or more secret
variables; this can only happen after the use of
ZkStateChange::OpenVariables
.
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 opening of ZK variables.
Annotated function must have a signature of following format:
#[zk_on_variables_opened]
pub fn zk_on_variables_opened(
context: ContractContext,
state: ContractState,
zk_state: ZkState<Metadata>,
opened_variables: Vec<SecretVarId>,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>)
Where opened_variables
is a Vec
of the opened variables.
§Example
Common usages include post-processing of computation results; for example
#[zk_on_variables_opened]
pub fn zk_on_sum_variable_opened(
context: ContractContext,
mut state: ContractState,
zk_state: ZkState<Metadata>,
opened_variables: Vec<SecretVarId>,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>) {
let result_var_id: SecretVarId = *opened_variables.get(0).unwrap();
let result_var: ZkClosed<Metadata> = zk_state.get_variable(result_var_id).unwrap();
let result: Vec<u8> = result_var.data.as_ref().unwrap().clone();
state.push(result);
(state, vec![], vec![])
}