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<(), &'static str> { ... }
    fn post_upgrade() -> 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.

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.

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.

Execute some pre-checks prior to a runtime upgrade.

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.

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.

Implementors§