-
Notifications
You must be signed in to change notification settings - Fork 16
CSUB-202 OCW Lock #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
CSUB-202 OCW Lock #388
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
636fe52
offchain task status prefix
CAGS295 d3fe8f6
move ocw tests
CAGS295 48d846e
OCW Lock
CAGS295 088cb78
remove trivial concurrent test
CAGS295 39cd394
bump spec_version
CAGS295 0a824d8
use sp_std::vec instead of alloc::vec
CAGS295 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#![cfg(test)] | ||
|
||
use super::{StorageLock, Task}; | ||
use crate::{ | ||
mock::{roll_by_with_ocw, set_rpc_uri, AccountId, ExtBuilder, MockedRpcRequests, Origin, Test}, | ||
ocw::{ | ||
errors::{OffchainError, VerificationFailureCause as Cause}, | ||
rpc::{EthTransaction, EthTransactionReceipt, JsonRpcResponse}, | ||
EncodeLike, ETH_CONFIRMATIONS, | ||
}, | ||
pallet::{Config, Store}, | ||
tests::{generate_address_with_proof, RefstrExt}, | ||
types::CollectedCoinsId, | ||
ExternalAddress, Pallet as Creditcoin, | ||
}; | ||
use assert_matches::assert_matches; | ||
use codec::Decode; | ||
use ethereum_types::{H160, U64}; | ||
use frame_support::{assert_noop, assert_ok, once_cell::sync::Lazy}; | ||
use frame_system::Pallet as System; | ||
use pallet_timestamp::Pallet as Timestamp; | ||
use sp_core::offchain::Duration; | ||
use sp_io::offchain; | ||
use sp_runtime::{ | ||
offchain::{ | ||
storage::StorageValueRef, | ||
storage_lock::{BlockAndTime, Lockable, Time}, | ||
}, | ||
traits::{BlockNumberProvider, IdentifyAccount}, | ||
}; | ||
use std::convert::{TryFrom, TryInto}; | ||
use std::sync::{ | ||
atomic::{AtomicU8, Ordering}, | ||
Arc, | ||
}; | ||
use std::thread; | ||
|
||
#[test] | ||
fn lock_released_when_guard_is_dropped() { | ||
let ext = ExtBuilder::default(); | ||
ext.build_offchain_and_execute_with_state(|state, _| { | ||
let key = b"id_1"; | ||
let mut l1 = StorageLock::<'_, Time>::new(key); | ||
let g = l1.try_lock(); | ||
assert!(g.is_ok()); | ||
drop(g); | ||
assert!(state.read().persistent_storage.get(key).is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn lock_guard_is_kept_alive() { | ||
let ext = ExtBuilder::default(); | ||
ext.build_offchain_and_execute_with_state(|_, _| { | ||
let mut l1 = StorageLock::<'_, Time>::new(b"id_1"); | ||
let g = l1.try_lock(); | ||
g.expect("ok").forget(); | ||
let g = l1.try_lock(); | ||
assert!(g.is_err()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn lock_expires() { | ||
let ext = ExtBuilder::default(); | ||
ext.build_offchain_and_execute_with_state(|_, _| { | ||
System::<Test>::set_block_number(1); | ||
let mut l1 = StorageLock::<'_, BlockAndTime<System<Test>>>::with_block_and_time_deadline( | ||
b"id_1", | ||
1, | ||
Duration::from_millis(0), | ||
); | ||
let g = l1.try_lock().expect("ok"); | ||
g.forget(); | ||
System::<Test>::set_block_number(3); | ||
let sleep_until = offchain::timestamp().add(Duration::from_millis(1)); | ||
offchain::sleep_until(sleep_until); | ||
let g = l1.try_lock(); | ||
assert!(g.is_ok()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn lock_mutual_exclusion() { | ||
let ext = ExtBuilder::default(); | ||
ext.build_offchain_and_execute_with_state(|state, _| { | ||
let mut l1 = StorageLock::<'_, Time>::new(b"id_1"); | ||
let mut l2 = StorageLock::<'_, Time>::new(b"id_2"); | ||
|
||
let g1 = l1.try_lock().expect("ok"); | ||
g1.forget(); | ||
let g1 = l1.try_lock(); | ||
assert!(g1.is_err()); | ||
//won't release because it was not a guard. | ||
drop(g1); | ||
let g2 = l2.try_lock().expect("ok"); | ||
drop(g2); | ||
let g2 = l2.try_lock(); | ||
assert!(g2.is_ok()); | ||
|
||
let g1 = l1.try_lock(); | ||
assert!(g1.is_err()); | ||
drop(g2); | ||
let deadline = state.read().persistent_storage.get(b"id_2"); | ||
assert!(deadline.is_none()); | ||
let deadline = state.read().persistent_storage.get(b"id_1"); | ||
assert!(deadline.is_some()); | ||
}); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.