mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 07:01:03 +00:00
Offchain Workers: Example Pallet (#4989)
* Example of offchain worker pallet. * Fix compilation issues. * Use serde_json to parse JSON. * Add some basic tests. * Working on docs. * Fix compilation * Finish docs for signed. * Work on unsigned send. * Add some tests and missing docs. * Add example of StorageValueRef * Add weight. * Extra \n * Fix im-online test. * Bump runtime. * Fix tests. * Apply suggestions from code review Co-Authored-By: Joshy Orndorff <JoshOrndorff@users.noreply.github.com> Co-Authored-By: Gavin Wood <gavin@parity.io> * Address review comments. Co-authored-by: Joshy Orndorff <JoshOrndorff@users.noreply.github.com> Co-authored-by: Gavin Wood <github@gavwood.com>
This commit is contained in:
@@ -341,8 +341,6 @@ decl_module! {
|
||||
|
||||
// Runs after every block.
|
||||
fn offchain_worker(now: T::BlockNumber) {
|
||||
debug::RuntimeLogger::init();
|
||||
|
||||
// Only send messages if we are a potential validator.
|
||||
if sp_io::offchain::is_validator() {
|
||||
for res in Self::send_heartbeats(now).into_iter().flatten() {
|
||||
|
||||
@@ -192,6 +192,8 @@ fn late_heartbeat_should_fail() {
|
||||
|
||||
#[test]
|
||||
fn should_generate_heartbeats() {
|
||||
use sp_runtime::traits::OffchainWorker;
|
||||
|
||||
let mut ext = new_test_ext();
|
||||
let (offchain, _state) = TestOffchainExt::new();
|
||||
let (pool, state) = TestTransactionPoolExt::new();
|
||||
@@ -202,6 +204,7 @@ fn should_generate_heartbeats() {
|
||||
// given
|
||||
let block = 1;
|
||||
System::set_block_number(block);
|
||||
UintAuthorityId::set_all_keys(vec![0, 1, 2]);
|
||||
// buffer new validators
|
||||
Session::rotate_session();
|
||||
// enact the change and buffer another one
|
||||
@@ -209,17 +212,13 @@ fn should_generate_heartbeats() {
|
||||
Session::rotate_session();
|
||||
|
||||
// when
|
||||
UintAuthorityId::set_all_keys(vec![0, 1, 2]);
|
||||
ImOnline::send_heartbeats(2)
|
||||
.unwrap()
|
||||
// make sure to consume the iterator and check there are no errors.
|
||||
.collect::<Result<Vec<_>, _>>().unwrap();
|
||||
|
||||
ImOnline::offchain_worker(block);
|
||||
|
||||
// then
|
||||
let transaction = state.write().transactions.pop().unwrap();
|
||||
// All validators have `0` as their session key, so we generate 3 transactions.
|
||||
// All validators have `0` as their session key, so we generate 2 transactions.
|
||||
assert_eq!(state.read().transactions.len(), 2);
|
||||
|
||||
// check stuff about the transaction.
|
||||
let ex: Extrinsic = Decode::decode(&mut &*transaction).unwrap();
|
||||
let heartbeat = match ex.call {
|
||||
@@ -228,7 +227,7 @@ fn should_generate_heartbeats() {
|
||||
};
|
||||
|
||||
assert_eq!(heartbeat, Heartbeat {
|
||||
block_number: 2,
|
||||
block_number: block,
|
||||
network_state: sp_io::offchain::network_state().unwrap(),
|
||||
session_index: 2,
|
||||
authority_index: 2,
|
||||
|
||||
Reference in New Issue
Block a user