Skip to content

Commit 405e0bd

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>
1 parent d5a8c14 commit 405e0bd

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
@@ -953,7 +953,14 @@ where
953953
// OffchainWorker RuntimeApi should skip initialization.
954954
let digests = header.digest().clone();
955955

956-
<frame_system::Pallet<System>>::initialize(header.number(), header.parent_hash(), &digests);
956+
// Let's deposit all the logs we are not yet aware of. These are the logs set by the `node`.
957+
let existing_digest = frame_system::Pallet::<System>::digest();
958+
for digest in digests.logs().iter().filter(|d| !existing_digest.logs.contains(d)) {
959+
frame_system::Pallet::<System>::deposit_log(digest.clone());
960+
}
961+
962+
// Initialize the intra block entropy, which is maybe used by offchain workers.
963+
frame_system::Pallet::<System>::initialize_intra_block_entropy(header.parent_hash());
957964

958965
// Frame system only inserts the parent hash into the block hashes as normally we don't know
959966
// 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
@@ -1113,7 +1113,14 @@ fn all_weights_are_recorded_correctly() {
11131113
fn offchain_worker_works_as_expected() {
11141114
new_test_ext(1).execute_with(|| {
11151115
let parent_hash = sp_core::H256::from([69u8; 32]);
1116+
1117+
// Emulate block production before running the offchain worker.
1118+
System::initialize(&1, &parent_hash, &Digest::default());
1119+
System::finalize();
1120+
11161121
let mut digest = Digest::default();
1122+
// As `Seal` is added by the node after the block was build, it was not part of
1123+
// `System::initialize` above.
11171124
digest.push(DigestItem::Seal([1, 2, 3, 4], vec![5, 6, 7, 8]));
11181125

11191126
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
@@ -1918,8 +1918,7 @@ impl<T: Config> Pallet<T> {
19181918
// populate environment
19191919
ExecutionPhase::<T>::put(Phase::Initialization);
19201920
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
1921-
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
1922-
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
1921+
Self::initialize_intra_block_entropy(parent_hash);
19231922
<Number<T>>::put(number);
19241923
<Digest<T>>::put(digest);
19251924
<ParentHash<T>>::put(parent_hash);
@@ -1929,6 +1928,14 @@ impl<T: Config> Pallet<T> {
19291928
BlockWeight::<T>::kill();
19301929
}
19311930

1931+
/// Initialize [`INTRABLOCK_ENTROPY`](well_known_keys::INTRABLOCK_ENTROPY).
1932+
///
1933+
/// Normally this is called internally [`initialize`](Self::initialize) at block initiation.
1934+
pub fn initialize_intra_block_entropy(parent_hash: &T::Hash) {
1935+
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
1936+
storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
1937+
}
1938+
19321939
/// Log the entire resouce usage report up until this point.
19331940
///
19341941
/// 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
@@ -742,6 +742,7 @@ impl_runtime_apis! {
742742
}.into(),
743743
);
744744
sp_io::offchain::submit_transaction(ext.encode()).unwrap();
745+
Executive::offchain_worker(header);
745746
}
746747
}
747748

0 commit comments

Comments
 (0)