ic_cdk

Attribute Macro post_upgrade

Source
#[post_upgrade]
Expand description

Register the canister_post_upgrade entry point of a canister.

This attribute macro will export the function canister_post_upgrade in the canister module.

The function under this attribute must have no return value.

Each canister can only have one canister_post_upgrade entry point.

ยงExample

#[post_upgrade]
fn post_upgrade_function() {
    // ...
}

The post_upgrade function may accept an argument.

The argument must implement the CandidType trait.

And it should match the initialization parameters of the service constructor in the Candid interface. Therefore, the init function and the post_upgrade function should take the same argument type.

#[derive(Clone, Debug, CandidType, Deserialize)]
struct InitArg {
    foo: u8,
}

#[post_upgrade]
fn post_upgrade_function(arg: InitArg) {
    // ...
}

In this case, the argument will be read from ic0.msg_arg_data_size/copy and passed to the post_upgrade function upon successful deserialization.