pub trait Hooks<BlockNumber> {
    // Provided methods
    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 offchain_worker(_n: BlockNumber) { ... }
    fn integrity_test() { ... }
}
Expand description

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

Provided Methods§

source

fn on_finalize(_n: BlockNumber)

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

source

fn on_idle(_n: BlockNumber, _remaining_weight: Weight) -> Weight

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.

source

fn on_initialize(_n: BlockNumber) -> Weight

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

Return the non-negotiable weight consumed in the block.

source

fn on_runtime_upgrade() -> Weight

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.

Warning

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.

source

fn offchain_worker(_n: BlockNumber)

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.

source

fn integrity_test()

Run integrity test.

The test is not executed in a externalities provided environment.

Implementors§