Skip to content

Commit 01db05e

Browse files
offchain-worker: Do not intialize the entire System again (#10235)
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](#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. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> (cherry picked from commit 405e0bd)
1 parent 9f87984 commit 01db05e

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

prdoc/pr_10235.prdoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
title: 'offchain-worker: Do not intialize the entire `System` again'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
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.
6+
7+
The pull request ensures that we are actually testing this behavior of the offchain worker now.
8+
crates:
9+
- name: frame-executive
10+
bump: patch
11+
- name: frame-system
12+
bump: patch

substrate/frame/executive/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,14 @@ where
931931
// OffchainWorker RuntimeApi should skip initialization.
932932
let digests = header.digest().clone();
933933

934-
<frame_system::Pallet<System>>::initialize(header.number(), header.parent_hash(), &digests);
934+
// Let's deposit all the logs we are not yet aware of. These are the logs set by the `node`.
935+
let existing_digest = frame_system::Pallet::<System>::digest();
936+
for digest in digests.logs().iter().filter(|d| !existing_digest.logs.contains(d)) {
937+
frame_system::Pallet::<System>::deposit_log(digest.clone());
938+
}
939+
940+
// Initialize the intra block entropy, which is maybe used by offchain workers.
941+
frame_system::Pallet::<System>::initialize_intra_block_entropy(header.parent_hash());
935942

936943
// Frame system only inserts the parent hash into the block hashes as normally we don't know
937944
// the hash for the header before. However, here we are aware of the hash and we can add it

substrate/frame/executive/src/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,14 @@ fn all_weights_are_recorded_correctly() {
11011101
fn offchain_worker_works_as_expected() {
11021102
new_test_ext(1).execute_with(|| {
11031103
let parent_hash = sp_core::H256::from([69u8; 32]);
1104+
1105+
// Emulate block production before running the offchain worker.
1106+
System::initialize(&1, &parent_hash, &Digest::default());
1107+
System::finalize();
1108+
11041109
let mut digest = Digest::default();
1110+
// As `Seal` is added by the node after the block was build, it was not part of
1111+
// `System::initialize` above.
11051112
digest.push(DigestItem::Seal([1, 2, 3, 4], vec![5, 6, 7, 8]));
11061113

11071114
let header = Header::new(1, H256::default(), H256::default(), parent_hash, digest.clone());

substrate/frame/system/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,8 +1916,7 @@ impl<T: Config> Pallet<T> {
19161916
// populate environment
19171917
ExecutionPhase::<T>::put(Phase::Initialization);
19181918
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
1919-
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
1920-
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
1919+
Self::initialize_intra_block_entropy(parent_hash);
19211920
<Number<T>>::put(number);
19221921
<Digest<T>>::put(digest);
19231922
<ParentHash<T>>::put(parent_hash);
@@ -1928,6 +1927,14 @@ impl<T: Config> Pallet<T> {
19281927
BlockWeight::<T>::kill();
19291928
}
19301929

1930+
/// Initialize [`INTRABLOCK_ENTROPY`](well_known_keys::INTRABLOCK_ENTROPY).
1931+
///
1932+
/// Normally this is called internally [`initialize`](Self::initialize) at block initiation.
1933+
pub fn initialize_intra_block_entropy(parent_hash: &T::Hash) {
1934+
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
1935+
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
1936+
}
1937+
19311938
/// Log the entire resouce usage report up until this point.
19321939
///
19331940
/// Uses `crate::LOG_TARGET`, level `debug` and prints the weight and block length usage.

substrate/test-utils/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ impl_runtime_apis! {
743743
}.into(),
744744
);
745745
sp_io::offchain::submit_transaction(ext.encode()).unwrap();
746+
Executive::offchain_worker(header);
746747
}
747748
}
748749

0 commit comments

Comments
 (0)