Skip to content

Commit 129b323

Browse files
authored
Fix some weirdness in offchain_worker (#7541)
We call `offchain_worker` with the state of the imported block and pass the header of this block. However in the runtime we call all `offchain_worker` functions with the number of the parent block. Besides that we also pass all digests and not only the pre runtime digests. In the context where the offchain worker is executed we have all digests, so there is no real reason to only pass pre runtime digests. Another fix is that we also insert the hash of the current header into the block hash map.
1 parent 1ef0b4f commit 129b323

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/lib.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117

118118
use sp_std::{prelude::*, marker::PhantomData};
119119
use frame_support::{
120-
storage::StorageValue, weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
120+
StorageValue, StorageMap, weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
121121
traits::{OnInitialize, OnFinalize, OnRuntimeUpgrade, OffchainWorker},
122122
dispatch::PostDispatchInfo,
123123
};
@@ -453,7 +453,7 @@ where
453453
// We need to keep events available for offchain workers,
454454
// hence we initialize the block manually.
455455
// OffchainWorker RuntimeApi should skip initialization.
456-
let digests = Self::extract_pre_digest(header);
456+
let digests = header.digest().clone();
457457

458458
<frame_system::Module<System>>::initialize(
459459
header.number(),
@@ -463,15 +463,16 @@ where
463463
frame_system::InitKind::Inspection,
464464
);
465465

466+
// Frame system only inserts the parent hash into the block hashes as normally we don't know
467+
// the hash for the header before. However, here we are aware of the hash and we can add it
468+
// as well.
469+
frame_system::BlockHash::<System>::insert(header.number(), header.hash());
470+
466471
// Initialize logger, so the log messages are visible
467472
// also when running WASM.
468473
frame_support::debug::RuntimeLogger::init();
469474

470-
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(
471-
// to maintain backward compatibility we call module offchain workers
472-
// with parent block number.
473-
header.number().saturating_sub(1u32.into())
474-
)
475+
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(*header.number())
475476
}
476477
}
477478

@@ -481,7 +482,7 @@ mod tests {
481482
use super::*;
482483
use sp_core::H256;
483484
use sp_runtime::{
484-
generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block},
485+
generic::{Era, DigestItem}, Perbill, DispatchError, testing::{Digest, Header, Block},
485486
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup},
486487
transaction_validity::{
487488
InvalidTransaction, ValidTransaction, TransactionValidityError, UnknownTransaction
@@ -547,6 +548,10 @@ mod tests {
547548
sp_io::storage::set(super::TEST_KEY, "module".as_bytes());
548549
200
549550
}
551+
552+
fn offchain_worker(n: T::BlockNumber) {
553+
assert_eq!(T::BlockNumber::from(1u32), n);
554+
}
550555
}
551556
}
552557

@@ -1115,4 +1120,27 @@ mod tests {
11151120
);
11161121
});
11171122
}
1123+
1124+
#[test]
1125+
fn offchain_worker_works_as_expected() {
1126+
new_test_ext(1).execute_with(|| {
1127+
let parent_hash = sp_core::H256::from([69u8; 32]);
1128+
let mut digest = Digest::default();
1129+
digest.push(DigestItem::Seal([1, 2, 3, 4], vec![5, 6, 7, 8]));
1130+
1131+
let header = Header::new(
1132+
1,
1133+
H256::default(),
1134+
H256::default(),
1135+
parent_hash,
1136+
digest.clone(),
1137+
);
1138+
1139+
Executive::offchain_worker(&header);
1140+
1141+
assert_eq!(digest, System::digest());
1142+
assert_eq!(parent_hash, System::block_hash(0));
1143+
assert_eq!(header.hash(), System::block_hash(1));
1144+
});
1145+
}
11181146
}

0 commit comments

Comments
 (0)