Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_idle hook and offchain_worker example #6416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions substrate/frame/examples/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,28 @@ pub mod pallet {
// Perform necessary data/state clean up here.
}

// A runtime code run after every block and have access to extended set of APIs.
//
// For instance you can generate extrinsics for the upcoming produced block.
fn offchain_worker(_n: BlockNumberFor<T>) {
// We don't do anything here.
// but we could dispatch extrinsic (transaction/unsigned/inherent) using
// sp_io::submit_extrinsic.
// To see example on offchain worker, please refer to example-offchain-worker pallet
// accompanied in this repository.
}
}
// A runtime code run after every block and has access to an extended set of APIs.
//
// For instance, you can generate extrinsics for the upcoming produced block.
fn offchain_worker(_n: BlockNumberFor<T>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be inside the pallet? Did you test that it works as expected?

// We don't do anything here.
// But we could dispatch extrinsics (transaction/unsigned/inherent) using
// sp_io::submit_extrinsic.
// To see an example on offchain worker usage, please refer to the example-offchain-worker pallet
// in this repository.
}

// The on_idle hook is called when the system is idle, typically during periods when no extrinsics are being processed.
fn on_idle(n: T::BlockNumber) -> Weight {
// Log the block number at which the on_idle hook is being called
log::info!("on_idle called at block number {:?}", n);

// Return weight for the operation; this indicates the computational cost
// In this case, we return Weight::zero() because we are not performing any heavy operations
Weight::zero()
}



// The call declaration. This states the entry points that we handle. The
// macro takes care of the marshalling of arguments and dispatch.
Expand Down Expand Up @@ -531,3 +542,4 @@ where
}
impl_tx_ext_default!(<T as frame_system::Config>::RuntimeCall; weight prepare);
}

Loading