Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions prdoc/pr_10235.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: 'offchain-worker: Do not intialize the entire `System` again'
doc:
- audience: Runtime Dev
description: |-
When calling `offchain-worker` we were initializing the entire `System` again with the same block we are running on top of. However, with [the change to require strictly increasing block numbers](https://github.com/paritytech/polkadot-sdk/pull/10180) the offchain-worker was failing. This is now solved by just registering the missing digests. The rest of the changes done by `initialize` are not important for offchain workers.

The pull request ensures that we are actually testing this behavior of the offchain worker now.
crates:
- name: frame-executive
bump: patch
- name: frame-system
bump: patch
9 changes: 8 additions & 1 deletion substrate/frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,14 @@ where
// OffchainWorker RuntimeApi should skip initialization.
let digests = header.digest().clone();

<frame_system::Pallet<System>>::initialize(header.number(), header.parent_hash(), &digests);
// Let's deposit all the logs we are not yet aware of. These are the logs set by the `node`.
let existing_digest = frame_system::Pallet::<System>::digest();
for digest in digests.logs().iter().filter(|d| !existing_digest.logs.contains(d)) {
frame_system::Pallet::<System>::deposit_log(digest.clone());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what about well_known_keys::INTRABLOCK_ENTROPY it gets removed in finalize, maybe we should bring it back, some offchain worker could read into it.


// Initialize the intra block entropy, which is maybe used by offchain workers.
frame_system::Pallet::<System>::initialize_intra_block_entropy(header.parent_hash());

// Frame system only inserts the parent hash into the block hashes as normally we don't know
// the hash for the header before. However, here we are aware of the hash and we can add it
Expand Down
7 changes: 7 additions & 0 deletions substrate/frame/executive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,14 @@ fn all_weights_are_recorded_correctly() {
fn offchain_worker_works_as_expected() {
new_test_ext(1).execute_with(|| {
let parent_hash = sp_core::H256::from([69u8; 32]);

// Emulate block production before running the offchain worker.
System::initialize(&1, &parent_hash, &Digest::default());
System::finalize();

let mut digest = Digest::default();
// As `Seal` is added by the node after the block was build, it was not part of
// `System::initialize` above.
digest.push(DigestItem::Seal([1, 2, 3, 4], vec![5, 6, 7, 8]));

let header = Header::new(1, H256::default(), H256::default(), parent_hash, digest.clone());
Expand Down
11 changes: 9 additions & 2 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,8 +1918,7 @@ impl<T: Config> Pallet<T> {
// populate environment
ExecutionPhase::<T>::put(Phase::Initialization);
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
Self::initialize_intra_block_entropy(parent_hash);
<Number<T>>::put(number);
<Digest<T>>::put(digest);
<ParentHash<T>>::put(parent_hash);
Expand All @@ -1929,6 +1928,14 @@ impl<T: Config> Pallet<T> {
BlockWeight::<T>::kill();
}

/// Initialize [`INTRABLOCK_ENTROPY`](well_known_keys::INTRABLOCK_ENTROPY).
///
/// Normally this is called internally [`initialize`](Self::initialize) at block initiation.
pub fn initialize_intra_block_entropy(parent_hash: &T::Hash) {
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
}

/// Log the entire resouce usage report up until this point.
///
/// Uses `crate::LOG_TARGET`, level `debug` and prints the weight and block length usage.
Expand Down
1 change: 1 addition & 0 deletions substrate/test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ impl_runtime_apis! {
}.into(),
);
sp_io::offchain::submit_transaction(ext.encode()).unwrap();
Executive::offchain_worker(header);
}
}

Expand Down
Loading