This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix test for Substrate#9898 #9907
Merged
emostov
merged 1 commit into
kiz-improve-ocw-resubmit
from
zeke-kiz-improve-ocw-resubmit
Oct 1, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1241,35 +1241,62 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn ocw_clears_cache_after_election() { | ||
| let (mut ext, _pool) = ExtBuilder::default().build_offchainify(0); | ||
| fn ocw_clears_cache_on_unsigned_phase_open() { | ||
| let (mut ext, pool) = ExtBuilder::default().build_offchainify(0); | ||
| ext.execute_with(|| { | ||
| roll_to(25); | ||
| assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25))); | ||
| const BLOCK: u64 = 25; | ||
| let block_plus = |delta: u64| BLOCK + delta; | ||
| let offchain_repeat = <Runtime as Config>::OffchainRepeat::get(); | ||
|
|
||
| // we must clear the offchain storage to ensure the offchain execution check doesn't get | ||
| // in the way. | ||
| let mut storage = StorageValueRef::persistent(&OFFCHAIN_LAST_BLOCK); | ||
| storage.clear(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clearing the storage here appears to be irrelevant because it starts out empty
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually always thought it to be an overkill and wanted to remove it :p |
||
| roll_to(BLOCK); | ||
| // we are on the first block of the unsigned phase | ||
| assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, BLOCK))); | ||
|
|
||
| assert!( | ||
| !ocw_solution_exists::<Runtime>(), | ||
| "no solution should be present before we mine one", | ||
| ); | ||
|
|
||
| // creates and cache a solution | ||
| MultiPhase::offchain_worker(25); | ||
| // create and cache a solution on the first block of the unsigned phase | ||
| MultiPhase::offchain_worker(BLOCK); | ||
| assert!( | ||
| ocw_solution_exists::<Runtime>(), | ||
| "a solution must be cached after running the worker", | ||
| ); | ||
|
|
||
| // after an election, the solution must be cleared | ||
| // record the submitted tx, | ||
| let tx_cache_1 = pool.read().transactions[0].clone(); | ||
| // and assume it has been processed. | ||
| pool.try_write().unwrap().transactions.clear(); | ||
|
|
||
| // after an election, the solution is not cleared | ||
| // we don't actually care about the result of the election | ||
| roll_to(26); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaict every call to |
||
| let _ = MultiPhase::do_elect(); | ||
| MultiPhase::offchain_worker(26); | ||
| assert!(!ocw_solution_exists::<Runtime>(), "elections must clear the ocw cache"); | ||
| MultiPhase::offchain_worker(block_plus(1)); | ||
| assert!(ocw_solution_exists::<Runtime>(), "elections does not clear the ocw cache"); | ||
|
|
||
| // submit a solution with the offchain worker after the repeat interval | ||
| MultiPhase::offchain_worker(block_plus(offchain_repeat + 1)); | ||
|
|
||
| // record the submitted tx, | ||
| let tx_cache_2 = pool.read().transactions[0].clone(); | ||
| // and assume it has been processed. | ||
| pool.try_write().unwrap().transactions.clear(); | ||
|
|
||
| // the OCW submitted the same solution twice since the cache was not cleared. | ||
| assert_eq!(tx_cache_1, tx_cache_2); | ||
|
|
||
| let current_block = block_plus(offchain_repeat * 2 + 2); | ||
| // force the unsigned phase to start on the current block. | ||
| CurrentPhase::<Runtime>::set(Phase::Unsigned((true, current_block))); | ||
|
|
||
| // clear the cache and create a solution since we are on the first block of the unsigned | ||
| // phase. | ||
| MultiPhase::offchain_worker(current_block); | ||
| let tx_cache_3 = pool.read().transactions[0].clone(); | ||
|
|
||
| // the submitted solution changes because the cache was cleared. | ||
| assert_eq!(tx_cache_1, tx_cache_3); | ||
| }) | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an idiom taken from the other tests in this file