pub trait Hooks<BlockNumber> {
    fn on_finalize(_n: BlockNumber) { ... }
    fn on_idle(_n: BlockNumber, _remaining_weight: Weight) -> Weight { ... }
    fn on_initialize(_n: BlockNumber) -> Weight { ... }
    fn on_runtime_upgrade() -> Weight { ... }
    fn try_state(_n: BlockNumber) -> Result<(), &'static str> { ... }
    fn pre_upgrade() -> Result<Vec<u8>, &'static str> { ... }
    fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> { ... }
    fn offchain_worker(_n: BlockNumber) { ... }
    fn integrity_test() { ... }
Expand description

The pallet hooks trait. Implementing this lets you express some logic to execute.

Provided Methods

The block is being finalized. Implement to have something happen.

This will be run when the block is being finalized (before on_finalize). Implement to have something happen using the remaining weight. Will not fire if the remaining weight is 0. Return the weight used, the hook will subtract it from current weight used and pass the result to the next on_idle hook if it exists.

The block is being initialized. Implement to have something happen.

Return the non-negotiable weight consumed in the block.

Perform a module upgrade.

NOTE: this doesn’t include all pallet logic triggered on runtime upgrade. For instance it doesn’t include the write of the pallet version in storage. The final complete logic triggered on runtime upgrade is given by implementation of OnRuntimeUpgrade trait by Pallet.


This function will be called before we initialized any runtime state, aka on_initialize wasn’t called yet. So, information like the block number and any other block local data are not accessible.

Return the non-negotiable weight consumed for runtime upgrade.

While this function can be freely implemented, using on_runtime_upgrade from inside the pallet is discouraged and might get deprecated in the future. Alternatively, export the same logic as a free-function from your pallet, and pass it to type Executive from the top-level runtime.

Execute the sanity checks of this pallet, per block.

It should focus on certain checks to ensure that the state is sensible. This is never executed in a consensus code-path, therefore it can consume as much weight as it needs.

This hook should not alter any storage.

Execute some pre-checks prior to a runtime upgrade.

Return a Vec<u8> that can contain arbitrary encoded data (usually some pre-upgrade state), which will be passed to post_upgrade after upgrading for post-check. An empty vector should be returned if there is no such need.

This hook is never meant to be executed on-chain but is meant to be used by testing tools.

Execute some post-checks after a runtime upgrade.

The state parameter is the Vec<u8> returned by pre_upgrade before upgrading, which can be used for post-check. NOTE: if pre_upgrade is not implemented an empty vector will be passed in, in such case post_upgrade should ignore it.

This hook is never meant to be executed on-chain but is meant to be used by testing tools.

Implementing this function on a module allows you to perform long-running tasks that make (by default) validators generate transactions that feed results of those long-running computations back on chain.

NOTE: This function runs off-chain, so it can access the block state, but cannot preform any alterations. More specifically alterations are not forbidden, but they are not persisted in any way after the worker has finished.

This function is being called after every block import (when fully synced).

Implement this and use any of the Offchain sp_io set of APIs to perform off-chain computations, calls and submit transactions with results to trigger any on-chain changes. Any state alterations are lost and are not persisted.

Run integrity test.

The test is not executed in a externalities provided environment.