Move Externalities into its own crate (#3775)

* Move `Externalities` into `substrate-externalities`

- `Externalities` now support generic extensions
- Split of `primtives-storage` for storage primitive types

* Move the externalities scoping into `substrate-externalities`

* Fix compilation

* Review feedback

* Adds macro for declaring extensions

* Fix benchmarks

* Introduce `ExtensionStore` trait

* Last review comments

* Implement it for `ExtensionStore`
This commit is contained in:
Bastian Köcher
2019-10-09 15:50:30 +02:00
committed by GitHub
parent 984c6ac839
commit 8a39be474e
95 changed files with 1600 additions and 1420 deletions
+14 -12
View File
@@ -240,12 +240,14 @@ impl<T: Trait> Module<T> {
mod tests {
use super::*;
use runtime_io::with_externalities;
use support::{impl_outer_origin, assert_ok, assert_noop, parameter_types};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sr_primitives::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sr_primitives::{
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
impl_outer_origin! {
pub enum Origin for Test {}
@@ -289,13 +291,13 @@ mod tests {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
#[test]
fn issuing_asset_units_to_issuer_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
});
@@ -303,7 +305,7 @@ mod tests {
#[test]
fn querying_total_supply_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
@@ -320,7 +322,7 @@ mod tests {
#[test]
fn transferring_amount_above_available_balance_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
@@ -331,7 +333,7 @@ mod tests {
#[test]
fn transferring_amount_less_than_available_balance_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
@@ -345,7 +347,7 @@ mod tests {
#[test]
fn transferring_less_than_one_unit_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 0), "transfer amount should be non-zero");
@@ -354,7 +356,7 @@ mod tests {
#[test]
fn transferring_more_units_than_total_supply_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 101), "origin account balance must be greater than or equal to the transfer amount");
@@ -363,7 +365,7 @@ mod tests {
#[test]
fn destroying_asset_balance_with_positive_balance_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::destroy(Origin::signed(1), 0));
@@ -372,7 +374,7 @@ mod tests {
#[test]
fn destroying_asset_balance_with_zero_balance_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 2), 0);
assert_noop!(Assets::destroy(Origin::signed(2), 0), "origin balance should be non-zero");
+2 -2
View File
@@ -26,7 +26,7 @@ use sr_primitives::{
};
use support::{impl_outer_origin, parameter_types};
use runtime_io;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
impl_outer_origin!{
pub enum Origin for Test {}
@@ -73,7 +73,7 @@ impl Trait for Test {
type AuthorityId = AuthorityId;
}
pub fn new_test_ext(authorities: Vec<u64>) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext(authorities: Vec<u64>) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test>{
authorities: authorities.into_iter().map(|a| UintAuthorityId(a).to_public_key()).collect(),
+2 -2
View File
@@ -18,12 +18,12 @@
#![cfg(test)]
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
use crate::mock::{Aura, new_test_ext};
#[test]
fn initial_values() {
with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
assert_eq!(Aura::last(), 0u64);
assert_eq!(Aura::authorities().len(), 4);
});
+13 -13
View File
@@ -135,12 +135,12 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
mod tests {
use super::*;
use app_crypto::Pair;
use primitives::testing::KeyStore;
use primitives::{crypto::key_types, sr25519, traits::BareCryptoStore, H256};
use runtime_io::{with_externalities, TestExternalities};
use sr_primitives::testing::{Header, UintAuthorityId};
use sr_primitives::traits::{ConvertInto, IdentityLookup, OpaqueKeys};
use sr_primitives::Perbill;
use primitives::{testing::KeyStore, crypto::key_types, sr25519, H256, traits::KeystoreExt};
use runtime_io::TestExternalities;
use sr_primitives::{
testing::{Header, UintAuthorityId}, traits::{ConvertInto, IdentityLookup, OpaqueKeys},
Perbill, set_and_run_with_externalities,
};
use support::{impl_outer_origin, parameter_types};
type AuthorityDiscovery = Module<Test>;
@@ -261,9 +261,9 @@ mod tests {
// Create externalities.
let mut externalities = TestExternalities::new(t);
externalities.set_keystore(key_store);
externalities.register_extension(KeystoreExt(key_store));
with_externalities(&mut externalities, || {
set_and_run_with_externalities(&mut externalities, || {
assert_eq!(
authority_id,
AuthorityDiscovery::authority_id().expect("Retrieving public key.")
@@ -298,9 +298,9 @@ mod tests {
// Create externalities.
let mut externalities = TestExternalities::new(t);
externalities.set_keystore(key_store);
externalities.register_extension(KeystoreExt(key_store));
with_externalities(&mut externalities, || {
set_and_run_with_externalities(&mut externalities, || {
assert_eq!(None, AuthorityDiscovery::authority_id());
});
}
@@ -335,9 +335,9 @@ mod tests {
// Create externalities.
let mut externalities = TestExternalities::new(t);
externalities.set_keystore(key_store);
externalities.register_extension(KeystoreExt(key_store));
with_externalities(&mut externalities, || {
set_and_run_with_externalities(&mut externalities, || {
let payload = String::from("test payload").into_bytes();
let (sig, authority_id) = AuthorityDiscovery::sign(&payload).expect("signature");
@@ -350,7 +350,7 @@ mod tests {
assert!(!AuthorityDiscovery::verify(
&String::from("other payload").into_bytes(),
sig,
authority_id
authority_id,
))
});
}
+9 -10
View File
@@ -412,12 +412,11 @@ impl<T: Trait> ProvideInherent for Module<T> {
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use sr_primitives::traits::{BlakeTwo256, IdentityLookup};
use sr_primitives::testing::Header;
use sr_primitives::generic::DigestItem;
use sr_primitives::Perbill;
use primitives::H256;
use sr_primitives::{
set_and_run_with_externalities, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
generic::DigestItem, Perbill,
};
use support::{parameter_types, impl_outer_origin, ConsensusEngineId};
impl_outer_origin!{
@@ -535,7 +534,7 @@ mod tests {
)
}
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
t.into()
}
@@ -543,7 +542,7 @@ mod tests {
#[test]
fn prune_old_uncles_works() {
use UncleEntryItem::*;
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let hash = Default::default();
let author = Default::default();
let uncles = vec![
@@ -562,7 +561,7 @@ mod tests {
#[test]
fn rejects_bad_uncles() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let author_a = 69;
struct CanonChain {
@@ -675,7 +674,7 @@ mod tests {
#[test]
fn sets_author_lazily() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let author = 42;
let mut header = seal_header(
create_header(1, Default::default(), [1; 32].into()),
+1 -1
View File
@@ -101,7 +101,7 @@ impl Trait for Test {
type EpochChangeTrigger = crate::ExternalTrigger;
}
pub fn new_test_ext(authorities: Vec<DummyValidatorId>) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext(authorities: Vec<DummyValidatorId>) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig {
authorities: authorities.into_iter().map(|a| (UintAuthorityId(a).to_public_key(), 1)).collect(),
+7 -6
View File
@@ -17,9 +17,10 @@
//! Consensus extension module tests for BABE consensus.
use super::*;
use runtime_io::with_externalities;
use mock::{new_test_ext, Babe, Test};
use sr_primitives::{traits::OnFinalize, testing::{Digest, DigestItem}};
use sr_primitives::{
set_and_run_with_externalities, traits::OnFinalize, testing::{Digest, DigestItem},
};
use session::ShouldEndSession;
const EMPTY_RANDOMNESS: [u8; 32] = [
@@ -53,14 +54,14 @@ fn empty_randomness_is_correct() {
#[test]
fn initial_values() {
with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
assert_eq!(Babe::authorities().len(), 4)
})
}
#[test]
fn check_module() {
with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
assert!(!Babe::should_end_session(0), "Genesis does not change sessions");
assert!(!Babe::should_end_session(200000),
"BABE does not include the block number in epoch calculations");
@@ -71,7 +72,7 @@ type System = system::Module<Test>;
#[test]
fn first_block_epoch_zero_start() {
with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
let genesis_slot = 100;
let first_vrf = [1; 32];
let pre_digest = make_pre_digest(
@@ -119,7 +120,7 @@ fn first_block_epoch_zero_start() {
#[test]
fn authority_index() {
with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
assert_eq!(
Babe::find_author((&[(BABE_ENGINE_ID, &[][..])]).into_iter().cloned()), None,
"Trivially invalid authorities are ignored")
+2 -2
View File
@@ -20,7 +20,7 @@
use sr_primitives::{Perbill, traits::{Convert, IdentityLookup}, testing::Header,
weights::{DispatchInfo, Weight}};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use runtime_io;
use support::{impl_outer_origin, parameter_types};
use support::traits::Get;
@@ -179,7 +179,7 @@ impl ExtBuilder {
TRANSACTION_BYTE_FEE.with(|v| *v.borrow_mut() = self.transaction_byte_fee);
WEIGHT_TO_FEE.with(|v| *v.borrow_mut() = self.weight_to_fee);
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
self.set_associated_consts();
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
GenesisConfig::<Runtime> {
+45 -45
View File
@@ -20,7 +20,7 @@
use super::*;
use mock::{Balances, ExtBuilder, Runtime, System, info_from_weight, CALL};
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
use support::{
assert_noop, assert_ok, assert_err,
traits::{LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons,
@@ -34,7 +34,7 @@ const ID_3: LockIdentifier = *b"3 ";
#[test]
fn basic_locking_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
assert_eq!(Balances::free_balance(&1), 10);
Balances::set_lock(ID_1, &1, 9, u64::max_value(), WithdrawReasons::all());
assert_noop!(
@@ -46,7 +46,7 @@ fn basic_locking_should_work() {
#[test]
fn partial_locking_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
});
@@ -54,7 +54,7 @@ fn partial_locking_should_work() {
#[test]
fn lock_removal_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
Balances::remove_lock(ID_1, &1);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
@@ -63,7 +63,7 @@ fn lock_removal_should_work() {
#[test]
fn lock_replacement_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
@@ -72,7 +72,7 @@ fn lock_replacement_should_work() {
#[test]
fn double_locking_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
Balances::set_lock(ID_2, &1, 5, u64::max_value(), WithdrawReasons::all());
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
@@ -81,7 +81,7 @@ fn double_locking_should_work() {
#[test]
fn combination_locking_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, u64::max_value(), 0, WithdrawReasons::none());
Balances::set_lock(ID_2, &1, 0, u64::max_value(), WithdrawReasons::none());
Balances::set_lock(ID_3, &1, 0, 0, WithdrawReasons::all());
@@ -91,7 +91,7 @@ fn combination_locking_should_work() {
#[test]
fn lock_value_extension_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6),
@@ -112,7 +112,7 @@ fn lock_value_extension_should_work() {
#[test]
fn lock_reasons_should_work() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(1)
.monied(true).transaction_fees(0, 1, 0)
@@ -163,7 +163,7 @@ fn lock_reasons_should_work() {
#[test]
fn lock_block_number_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 1),
@@ -177,7 +177,7 @@ fn lock_block_number_should_work() {
#[test]
fn lock_block_number_extension_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6),
@@ -199,7 +199,7 @@ fn lock_block_number_extension_should_work() {
#[test]
fn lock_reasons_extension_should_work() {
with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
Balances::set_lock(ID_1, &1, 10, 10, WithdrawReason::Transfer.into());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6),
@@ -220,7 +220,7 @@ fn lock_reasons_extension_should_work() {
#[test]
fn default_indexing_on_new_accounts_should_not_work2() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(10)
.creation_fee(50)
@@ -242,7 +242,7 @@ fn default_indexing_on_new_accounts_should_not_work2() {
#[test]
fn reserved_balance_should_prevent_reclaim_count() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(256 * 1)
.monied(true)
@@ -281,7 +281,7 @@ fn reserved_balance_should_prevent_reclaim_count() {
#[test]
fn reward_should_work() {
with_externalities(&mut ExtBuilder::default().monied(true).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().monied(true).build(), || {
assert_eq!(Balances::total_balance(&1), 10);
assert_ok!(Balances::deposit_into_existing(&1, 10).map(drop));
assert_eq!(Balances::total_balance(&1), 20);
@@ -291,7 +291,7 @@ fn reward_should_work() {
#[test]
fn dust_account_removal_should_work() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(100)
.monied(true)
@@ -311,7 +311,7 @@ fn dust_account_removal_should_work() {
#[test]
fn dust_account_removal_should_work2() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(100)
.creation_fee(50)
@@ -332,7 +332,7 @@ fn dust_account_removal_should_work2() {
#[test]
fn balance_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 42);
assert_eq!(Balances::free_balance(&1), 42);
assert_eq!(Balances::reserved_balance(&1), 0);
@@ -345,7 +345,7 @@ fn balance_works() {
#[test]
fn balance_transfer_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::transfer(Some(1).into(), 2, 69));
assert_eq!(Balances::total_balance(&1), 42);
@@ -355,7 +355,7 @@ fn balance_transfer_works() {
#[test]
fn force_transfer_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_noop!(
Balances::force_transfer(Some(2).into(), 1, 2, 69),
@@ -369,7 +369,7 @@ fn force_transfer_works() {
#[test]
fn reserving_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_eq!(Balances::total_balance(&1), 111);
@@ -386,7 +386,7 @@ fn reserving_balance_should_work() {
#[test]
fn balance_transfer_when_reserved_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 69));
assert_noop!(
@@ -398,7 +398,7 @@ fn balance_transfer_when_reserved_should_not_work() {
#[test]
fn deducting_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 69));
assert_eq!(Balances::free_balance(&1), 42);
@@ -407,7 +407,7 @@ fn deducting_balance_should_work() {
#[test]
fn refunding_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 42);
Balances::set_reserved_balance(&1, 69);
Balances::unreserve(&1, 69);
@@ -418,7 +418,7 @@ fn refunding_balance_should_work() {
#[test]
fn slashing_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 69));
assert!(Balances::slash(&1, 69).1.is_zero());
@@ -430,7 +430,7 @@ fn slashing_balance_should_work() {
#[test]
fn slashing_incomplete_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 42);
assert_ok!(Balances::reserve(&1, 21));
assert_eq!(Balances::slash(&1, 69).1, 27);
@@ -442,7 +442,7 @@ fn slashing_incomplete_balance_should_work() {
#[test]
fn unreserving_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
Balances::unreserve(&1, 42);
@@ -453,7 +453,7 @@ fn unreserving_balance_should_work() {
#[test]
fn slashing_reserved_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
assert_eq!(Balances::slash_reserved(&1, 42).1, 0);
@@ -465,7 +465,7 @@ fn slashing_reserved_balance_should_work() {
#[test]
fn slashing_incomplete_reserved_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 42));
assert_eq!(Balances::slash_reserved(&1, 69).1, 27);
@@ -477,7 +477,7 @@ fn slashing_incomplete_reserved_balance_should_work() {
#[test]
fn transferring_reserved_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 110);
let _ = Balances::deposit_creating(&2, 1);
assert_ok!(Balances::reserve(&1, 110));
@@ -491,7 +491,7 @@ fn transferring_reserved_balance_should_work() {
#[test]
fn transferring_reserved_balance_to_nonexistent_should_fail() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
assert_noop!(Balances::repatriate_reserved(&1, &2, 42), "beneficiary account must pre-exist");
@@ -500,7 +500,7 @@ fn transferring_reserved_balance_to_nonexistent_should_fail() {
#[test]
fn transferring_incomplete_reserved_balance_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let _ = Balances::deposit_creating(&1, 110);
let _ = Balances::deposit_creating(&2, 1);
assert_ok!(Balances::reserve(&1, 41));
@@ -514,7 +514,7 @@ fn transferring_incomplete_reserved_balance_should_work() {
#[test]
fn transferring_too_high_value_should_not_panic() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
<FreeBalance<Runtime>>::insert(1, u64::max_value());
<FreeBalance<Runtime>>::insert(2, 1);
@@ -530,7 +530,7 @@ fn transferring_too_high_value_should_not_panic() {
#[test]
fn account_create_on_free_too_low_with_other() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
let _ = Balances::deposit_creating(&1, 100);
@@ -547,7 +547,7 @@ fn account_create_on_free_too_low_with_other() {
#[test]
fn account_create_on_free_too_low() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
// No-op.
@@ -560,7 +560,7 @@ fn account_create_on_free_too_low() {
#[test]
fn account_removal_on_free_too_low() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
@@ -590,7 +590,7 @@ fn account_removal_on_free_too_low() {
#[test]
fn transfer_overflow_isnt_exploitable() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().creation_fee(50).build(),
|| {
// Craft a value that will overflow if summed with `creation_fee`.
@@ -606,7 +606,7 @@ fn transfer_overflow_isnt_exploitable() {
#[test]
fn check_vesting_status() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(256)
.monied(true)
@@ -669,7 +669,7 @@ fn check_vesting_status() {
#[test]
fn unvested_balance_should_not_transfer() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(10)
.monied(true)
@@ -691,7 +691,7 @@ fn unvested_balance_should_not_transfer() {
#[test]
fn vested_balance_should_transfer() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(10)
.monied(true)
@@ -710,7 +710,7 @@ fn vested_balance_should_transfer() {
#[test]
fn extra_balance_should_transfer() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(10)
.monied(true)
@@ -740,7 +740,7 @@ fn extra_balance_should_transfer() {
#[test]
fn liquid_funds_should_transfer_with_delayed_vesting() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(256)
.monied(true)
@@ -770,7 +770,7 @@ fn liquid_funds_should_transfer_with_delayed_vesting() {
#[test]
fn signed_extension_take_fees_work() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(10)
.transaction_fees(10, 1, 5)
@@ -788,7 +788,7 @@ fn signed_extension_take_fees_work() {
#[test]
fn signed_extension_take_fees_is_bounded() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.existential_deposit(1000)
.transaction_fees(0, 0, 1)
@@ -810,7 +810,7 @@ fn signed_extension_take_fees_is_bounded() {
#[test]
fn burn_must_work() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.monied(true)
.build(),
+14 -14
View File
@@ -382,10 +382,10 @@ mod tests {
use support::{Hashable, assert_ok, assert_noop, parameter_types};
use system::{EventRecord, Phase};
use hex_literal::hex;
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use sr_primitives::{
Perbill, traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, testing::Header, BuildStorage
set_and_run_with_externalities, Perbill,
traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, testing::Header, BuildStorage,
};
use crate as collective;
@@ -439,7 +439,7 @@ mod tests {
}
);
fn make_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn make_ext() -> runtime_io::TestExternalities {
GenesisConfig {
collective_Instance1: Some(collective::GenesisConfig {
members: vec![1, 2, 3],
@@ -451,7 +451,7 @@ mod tests {
#[test]
fn motions_basic_environment_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
assert_eq!(Collective::members(), vec![1, 2, 3]);
assert_eq!(Collective::proposals(), Vec::<H256>::new());
@@ -464,7 +464,7 @@ mod tests {
#[test]
fn removal_of_old_voters_votes_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
@@ -498,7 +498,7 @@ mod tests {
#[test]
fn removal_of_old_voters_votes_works_with_set_members() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
@@ -532,7 +532,7 @@ mod tests {
#[test]
fn propose_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = proposal.blake2_256().into();
@@ -561,7 +561,7 @@ mod tests {
#[test]
fn motions_ignoring_non_collective_proposals_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
assert_noop!(
@@ -573,7 +573,7 @@ mod tests {
#[test]
fn motions_ignoring_non_collective_votes_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
@@ -584,7 +584,7 @@ mod tests {
#[test]
fn motions_ignoring_bad_index_collective_vote_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(3);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
@@ -595,7 +595,7 @@ mod tests {
#[test]
fn motions_revoting_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
@@ -640,7 +640,7 @@ mod tests {
#[test]
fn motions_disapproval_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
@@ -683,7 +683,7 @@ mod tests {
#[test]
fn motions_approval_works() {
with_externalities(&mut make_ext(), || {
set_and_run_with_externalities(&mut make_ext(), || {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
+28 -32
View File
@@ -803,16 +803,12 @@ mod tests {
BalanceOf, ExecFeeToken, ExecutionContext, Ext, Loader, TransferFeeKind, TransferFeeToken,
Vm, ExecResult, RawEvent, DeferredAction,
};
use crate::account_db::AccountDb;
use crate::exec::{ExecReturnValue, ExecError, STATUS_SUCCESS};
use crate::gas::GasMeter;
use crate::tests::{ExtBuilder, Test};
use crate::{CodeHash, Config};
use runtime_io::with_externalities;
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap;
use std::marker::PhantomData;
use crate::{
account_db::AccountDb, gas::GasMeter, tests::{ExtBuilder, Test},
exec::{ExecReturnValue, ExecError, STATUS_SUCCESS}, CodeHash, Config,
};
use sr_primitives::set_and_run_with_externalities;
use std::{cell::RefCell, rc::Rc, collections::HashMap, marker::PhantomData};
use assert_matches::assert_matches;
const ALICE: u64 = 1;
@@ -937,7 +933,7 @@ mod tests {
exec_success()
});
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, exec_ch).unwrap();
@@ -957,7 +953,7 @@ mod tests {
let dest = BOB;
// This test verifies that base fee for call is taken.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let vm = MockVm::new();
let loader = MockLoader::empty();
let cfg = Config::preload();
@@ -975,7 +971,7 @@ mod tests {
});
// This test verifies that base fee for instantiation is taken.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let mut loader = MockLoader::empty();
let code = loader.insert(|_| exec_success());
@@ -1005,7 +1001,7 @@ mod tests {
let vm = MockVm::new();
let loader = MockLoader::empty();
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
ctx.overlay.set_balance(&origin, 100);
@@ -1037,7 +1033,7 @@ mod tests {
|_| Ok(ExecReturnValue { status: 1, data: Vec::new() })
);
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, return_ch).unwrap();
@@ -1065,7 +1061,7 @@ mod tests {
// This test sends 50 units of currency to a non-existent account.
// This should lead to creation of a new account thus
// a fee should be charged.
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let vm = MockVm::new();
@@ -1094,7 +1090,7 @@ mod tests {
// This one is similar to the previous one but transfer to an existing account.
// In this test we expect that a regular transfer fee is charged.
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let vm = MockVm::new();
@@ -1123,7 +1119,7 @@ mod tests {
// This test sends 50 units of currency as an endownment to a newly
// instantiated contract.
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let mut loader = MockLoader::empty();
@@ -1164,7 +1160,7 @@ mod tests {
let vm = MockVm::new();
let loader = MockLoader::empty();
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
ctx.overlay.set_balance(&origin, 0);
@@ -1198,7 +1194,7 @@ mod tests {
|_| Ok(ExecReturnValue { status: STATUS_SUCCESS, data: vec![1, 2, 3, 4] })
);
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, return_ch).unwrap();
@@ -1229,7 +1225,7 @@ mod tests {
|_| Ok(ExecReturnValue { status: 1, data: vec![1, 2, 3, 4] })
);
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, return_ch).unwrap();
@@ -1257,7 +1253,7 @@ mod tests {
});
// This one tests passing the input data into a contract via call.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, input_data_ch).unwrap();
@@ -1282,7 +1278,7 @@ mod tests {
});
// This one tests passing the input data into a contract via instantiate.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
@@ -1326,7 +1322,7 @@ mod tests {
exec_success()
});
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, recurse_ch).unwrap();
@@ -1370,7 +1366,7 @@ mod tests {
exec_success()
});
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(origin, &cfg, &vm, &loader);
@@ -1412,7 +1408,7 @@ mod tests {
exec_success()
});
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
ctx.overlay.instantiate_contract(&BOB, bob_ch).unwrap();
@@ -1436,7 +1432,7 @@ mod tests {
let mut loader = MockLoader::empty();
let dummy_ch = loader.insert(|_| exec_success());
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let cfg = Config::preload();
@@ -1464,7 +1460,7 @@ mod tests {
|_| Ok(ExecReturnValue { status: STATUS_SUCCESS, data: vec![80, 65, 83, 83] })
);
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let cfg = Config::preload();
@@ -1507,7 +1503,7 @@ mod tests {
|_| Ok(ExecReturnValue { status: 1, data: vec![70, 65, 73, 76] })
);
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let cfg = Config::preload();
@@ -1555,7 +1551,7 @@ mod tests {
}
});
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let cfg = Config::preload();
@@ -1617,7 +1613,7 @@ mod tests {
}
});
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(15).build(),
|| {
let cfg = Config::preload();
@@ -1653,7 +1649,7 @@ mod tests {
exec_success()
});
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let cfg = Config::preload();
let mut ctx = ExecutionContext::top_level(ALICE, &cfg, &vm, &loader);
+26 -29
View File
@@ -19,21 +19,18 @@
#![allow(unused)]
use crate::account_db::{AccountDb, DirectAccountDb, OverlayAccountDb};
use crate::{
BalanceOf, ComputeDispatchFee, ContractAddressFor, ContractInfo, ContractInfoOf, GenesisConfig,
Module, RawAliveContractInfo, RawEvent, Trait, TrieId, TrieIdFromParentCounter, Schedule,
TrieIdGenerator, CheckBlockGasLimit,
TrieIdGenerator, CheckBlockGasLimit, account_db::{AccountDb, DirectAccountDb, OverlayAccountDb},
};
use assert_matches::assert_matches;
use hex_literal::*;
use codec::{Decode, Encode, KeyedVec};
use runtime_io;
use runtime_io::with_externalities;
use sr_primitives::{
Perbill, BuildStorage, transaction_validity::{InvalidTransaction, ValidTransaction},
traits::{BlakeTwo256, Hash, IdentityLookup, SignedExtension},
weights::{DispatchInfo, DispatchClass},
weights::{DispatchInfo, DispatchClass}, set_and_run_with_externalities,
testing::{Digest, DigestItem, Header, UintAuthorityId, H256},
};
use support::{
@@ -41,7 +38,7 @@ use support::{
storage::child, StorageMap, StorageValue, traits::{Currency, Get},
};
use std::{cell::RefCell, sync::atomic::{AtomicUsize, Ordering}};
use primitives::{storage::well_known_keys, Blake2Hasher};
use primitives::storage::well_known_keys;
use system::{self, EventRecord, Phase};
mod contract {
@@ -275,7 +272,7 @@ impl ExtBuilder {
INSTANTIATION_FEE.with(|v| *v.borrow_mut() = self.instantiation_fee);
BLOCK_GAS_LIMIT.with(|v| *v.borrow_mut() = self.block_gas_limit);
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
self.set_associated_consts();
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test> {
@@ -307,7 +304,7 @@ fn compile_module<T>(wabt_module: &str)
// Then we check that the all unused gas is refunded.
#[test]
fn refunds_unused_gas() {
with_externalities(&mut ExtBuilder::default().gas_price(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().gas_price(2).build(), || {
Balances::deposit_creating(&ALICE, 100_000_000);
assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, Vec::new()));
@@ -319,7 +316,7 @@ fn refunds_unused_gas() {
#[test]
fn account_removal_removes_storage() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
let trie_id1 = <Test as Trait>::TrieIdGenerator::trie_id(&1);
@@ -419,7 +416,7 @@ const CODE_RETURN_FROM_START_FN: &str = r#"
fn instantiate_and_call_and_deposit_event() {
let (wasm, code_hash) = compile_module::<Test>(CODE_RETURN_FROM_START_FN).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -502,7 +499,7 @@ fn dispatch_call() {
let (wasm, code_hash) = compile_module::<Test>(CODE_DISPATCH_CALL).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -623,7 +620,7 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() {
let (wasm, code_hash) = compile_module::<Test>(CODE_DISPATCH_CALL_THEN_TRAP).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -826,7 +823,7 @@ fn test_set_rent_code_and_hash() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -855,7 +852,7 @@ fn storage_size() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
// Storage size
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -885,7 +882,7 @@ fn storage_size() {
fn deduct_blocks() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -982,7 +979,7 @@ fn claim_surcharge_malus() {
fn claim_surcharge(blocks: u64, trigger_call: impl Fn() -> bool, removes: bool) {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1018,7 +1015,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
// Balance reached and superior to subsistence threshold
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1057,7 +1054,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
);
// Allowance exceeded
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1095,7 +1092,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
);
// Balance reached and inferior to subsistence threshold
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1142,7 +1139,7 @@ fn call_removed_contract() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();
// Balance reached and superior to subsistence threshold
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1230,7 +1227,7 @@ const CODE_CHECK_DEFAULT_RENT_ALLOWANCE: &str = r#"
fn default_rent_allowance_on_instantiate() {
let (wasm, code_hash) = compile_module::<Test>(CODE_CHECK_DEFAULT_RENT_ALLOWANCE).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1347,7 +1344,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
let (restoration_wasm, restoration_code_hash) =
compile_module::<Test>(CODE_RESTORATION).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -1533,7 +1530,7 @@ const CODE_STORAGE_SIZE: &str = r#"
fn storage_max_value_limit() {
let (wasm, code_hash) = compile_module::<Test>(CODE_STORAGE_SIZE).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -1900,7 +1897,7 @@ fn deploy_and_call_other_contract() {
let (callee_wasm, callee_code_hash) = compile_module::<Test>(CODE_RETURN_WITH_DATA).unwrap();
let (caller_wasm, caller_code_hash) = compile_module::<Test>(CODE_CALLER_CONTRACT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -2031,7 +2028,7 @@ const CODE_SELF_DESTRUCT: &str = r#"
#[test]
fn self_destruct_by_draining_balance() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SELF_DESTRUCT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -2070,7 +2067,7 @@ fn self_destruct_by_draining_balance() {
#[test]
fn cannot_self_destruct_while_live() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SELF_DESTRUCT).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -2272,7 +2269,7 @@ fn destroy_contract_and_transfer_funds() {
let (callee_wasm, callee_code_hash) = compile_module::<Test>(CODE_SELF_DESTRUCT).unwrap();
let (caller_wasm, caller_code_hash) = compile_module::<Test>(CODE_DESTROY_AND_TRANSFER).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
// Create
@@ -2371,7 +2368,7 @@ const CODE_SELF_DESTRUCTING_CONSTRUCTOR: &str = r#"
#[test]
fn cannot_self_destruct_in_constructor() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SELF_DESTRUCTING_CONSTRUCTOR).unwrap();
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().existential_deposit(50).build(),
|| {
Balances::deposit_creating(&ALICE, 1_000_000);
@@ -2395,7 +2392,7 @@ fn cannot_self_destruct_in_constructor() {
#[test]
fn check_block_gas_limit_works() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().block_gas_limit(50).build(),
|| {
let info = DispatchInfo { weight: 100, class: DispatchClass::Normal };
-1
View File
@@ -38,7 +38,6 @@ impl<T> OnMembersChanged<T> for () {
mod tests {
// These re-exports are here for a reason, edit with care
pub use super::*;
pub use runtime_io::with_externalities;
use support::{impl_outer_origin, impl_outer_event, impl_outer_dispatch, parameter_types};
use support::traits::Get;
pub use primitives::{H256, Blake2Hasher, u32_trait::{_1, _2, _3, _4}};
+40 -39
View File
@@ -971,14 +971,15 @@ impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use support::{
impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types,
traits::Contains
};
use primitives::{H256, Blake2Hasher};
use sr_primitives::{traits::{BlakeTwo256, IdentityLookup, Bounded}, testing::Header};
use sr_primitives::Perbill;
use primitives::H256;
use sr_primitives::{
set_and_run_with_externalities, traits::{BlakeTwo256, IdentityLookup, Bounded},
testing::Header, Perbill,
};
use balances::BalanceLock;
use system::EnsureSignedBy;
@@ -1084,7 +1085,7 @@ mod tests {
type CooloffPeriod = CooloffPeriod;
}
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test>{
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
@@ -1100,7 +1101,7 @@ mod tests {
#[test]
fn params_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Democracy::referendum_count(), 0);
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::total_issuance(), 210);
@@ -1132,7 +1133,7 @@ mod tests {
#[test]
fn external_and_public_interleaving_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
@@ -1245,7 +1246,7 @@ mod tests {
#[test]
fn emergency_cancel_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
@@ -1274,7 +1275,7 @@ mod tests {
#[test]
fn veto_external_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
@@ -1334,7 +1335,7 @@ mod tests {
#[test]
fn external_referendum_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_noop!(Democracy::external_propose(
Origin::signed(1),
@@ -1363,7 +1364,7 @@ mod tests {
#[test]
fn external_majority_referendum_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_noop!(Democracy::external_propose_majority(
Origin::signed(1),
@@ -1388,7 +1389,7 @@ mod tests {
#[test]
fn external_default_referendum_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_noop!(Democracy::external_propose_default(
Origin::signed(3),
@@ -1413,7 +1414,7 @@ mod tests {
#[test]
fn fast_track_referendum_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
let h = BlakeTwo256::hash_of(&set_balance_proposal(2));
assert_noop!(Democracy::fast_track(Origin::signed(5), h, 3, 2), "no proposal made");
@@ -1437,7 +1438,7 @@ mod tests {
#[test]
fn fast_track_referendum_fails_when_no_simple_majority() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
let h = BlakeTwo256::hash_of(&set_balance_proposal(2));
assert_ok!(Democracy::external_propose(
@@ -1453,7 +1454,7 @@ mod tests {
#[test]
fn locked_for_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 2));
assert_ok!(propose_set_balance(1, 4, 4));
@@ -1466,7 +1467,7 @@ mod tests {
#[test]
fn single_proposal_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
assert!(Democracy::referendum_info(0).is_none());
@@ -1513,7 +1514,7 @@ mod tests {
#[test]
fn cancel_queued_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1537,7 +1538,7 @@ mod tests {
#[test]
fn proxy_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Democracy::proxy(10), None);
assert_ok!(Democracy::set_proxy(Origin::signed(1), 10));
assert_eq!(Democracy::proxy(10), Some(1));
@@ -1567,7 +1568,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_proxy() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1587,7 +1588,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_delegation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1612,7 +1613,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_cyclic_delegation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1639,7 +1640,7 @@ mod tests {
#[test]
/// If transactor already voted, delegated vote is overwriten.
fn single_proposal_should_work_with_vote_and_delegation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1665,7 +1666,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_undelegation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1694,7 +1695,7 @@ mod tests {
#[test]
/// If transactor voted, delegated vote is overwriten.
fn single_proposal_should_work_with_delegation_and_vote() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1725,7 +1726,7 @@ mod tests {
#[test]
fn deposit_for_proposals_should_be_taken() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
@@ -1740,7 +1741,7 @@ mod tests {
#[test]
fn deposit_for_proposals_should_be_returned() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
@@ -1756,7 +1757,7 @@ mod tests {
#[test]
fn proposal_with_deposit_below_minimum_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 0), "value too low");
});
@@ -1764,7 +1765,7 @@ mod tests {
#[test]
fn poor_proposer_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 11), "proposer\'s balance too low");
});
@@ -1772,7 +1773,7 @@ mod tests {
#[test]
fn poor_seconder_should_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
assert_ok!(propose_set_balance(2, 2, 11));
assert_noop!(Democracy::second(Origin::signed(1), 0), "seconder\'s balance too low");
@@ -1781,7 +1782,7 @@ mod tests {
#[test]
fn runners_up_should_come_after() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 2));
assert_ok!(propose_set_balance(1, 4, 4));
@@ -1797,7 +1798,7 @@ mod tests {
#[test]
fn simple_passing_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1820,7 +1821,7 @@ mod tests {
#[test]
fn cancel_referendum_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1840,7 +1841,7 @@ mod tests {
#[test]
fn simple_failing_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1863,7 +1864,7 @@ mod tests {
#[test]
fn controversial_voting_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1889,7 +1890,7 @@ mod tests {
#[test]
fn delayed_enactment_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1917,7 +1918,7 @@ mod tests {
#[test]
fn controversial_low_turnout_voting_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1939,7 +1940,7 @@ mod tests {
#[test]
fn passing_low_turnout_voting_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::total_issuance(), 210);
@@ -1965,7 +1966,7 @@ mod tests {
#[test]
fn lock_voting_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(0);
let r = Democracy::inject_referendum(
1,
@@ -2025,7 +2026,7 @@ mod tests {
#[test]
fn lock_voting_should_work_with_delegation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
+43 -43
View File
@@ -591,10 +591,10 @@ mod tests {
use super::*;
use std::cell::RefCell;
use srml_support::{assert_ok, assert_noop, parameter_types, assert_eq_uvec};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use sr_primitives::{Perbill, testing::Header, BuildStorage,
traits::{BlakeTwo256, IdentityLookup, Block as BlockT}
use primitives::H256;
use sr_primitives::{
Perbill, testing::Header, BuildStorage,
traits::{BlakeTwo256, IdentityLookup, Block as BlockT}, set_and_run_with_externalities
};
use crate as elections;
@@ -730,7 +730,7 @@ mod tests {
self.desired_runners_up = count;
self
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
VOTING_BOND.with(|v| *v.borrow_mut() = self.voter_bond);
GenesisConfig {
balances: Some(balances::GenesisConfig::<Test>{
@@ -770,7 +770,7 @@ mod tests {
#[test]
fn params_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::desired_members(), 2);
assert_eq!(Elections::term_duration(), 5);
@@ -790,7 +790,7 @@ mod tests {
#[test]
fn simple_candidate_submission_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert!(Elections::is_candidate(&1).is_err());
assert!(Elections::is_candidate(&2).is_err());
@@ -817,7 +817,7 @@ mod tests {
#[test]
fn simple_candidate_submission_with_no_votes_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_ok!(Elections::submit_candidacy(Origin::signed(1)));
@@ -844,7 +844,7 @@ mod tests {
#[test]
fn dupe_candidate_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_ok!(Elections::submit_candidacy(Origin::signed(1)));
assert_eq!(Elections::candidates(), vec![1]);
@@ -858,7 +858,7 @@ mod tests {
#[test]
fn member_candidacy_submission_should_not_work() {
// critically important to make sure that outgoing candidates and losers are not mixed up.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::vote(Origin::signed(2), vec![5], 20));
@@ -878,7 +878,7 @@ mod tests {
#[test]
fn poor_candidate_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_noop!(
Elections::submit_candidacy(Origin::signed(7)),
@@ -889,7 +889,7 @@ mod tests {
#[test]
fn simple_voting_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_eq!(balances(&2), (20, 0));
@@ -903,7 +903,7 @@ mod tests {
#[test]
fn can_vote_with_custom_stake() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_eq!(balances(&2), (20, 0));
@@ -917,7 +917,7 @@ mod tests {
#[test]
fn can_update_votes_and_stake() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(balances(&2), (20, 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
@@ -938,7 +938,7 @@ mod tests {
#[test]
fn cannot_vote_for_no_candidate() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_noop!(
Elections::vote(Origin::signed(2), vec![], 20),
"cannot vote when no candidates or members exist"
@@ -949,7 +949,7 @@ mod tests {
#[test]
fn can_vote_for_old_members_even_when_no_new_candidates() {
// let allowed_votes = candidates_count as usize + Self::members().len()
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -967,7 +967,7 @@ mod tests {
#[test]
fn cannot_vote_for_more_than_candidates() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -980,7 +980,7 @@ mod tests {
#[test]
fn cannot_vote_for_less_than_ed() {
with_externalities(&mut ExtBuilder::default().voter_bond(8).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voter_bond(8).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -993,7 +993,7 @@ mod tests {
#[test]
fn can_vote_for_more_than_total_balance_but_moot() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1006,7 +1006,7 @@ mod tests {
#[test]
fn remove_voter_should_work() {
with_externalities(&mut ExtBuilder::default().voter_bond(8).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voter_bond(8).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::vote(Origin::signed(2), vec![5], 20));
@@ -1031,14 +1031,14 @@ mod tests {
#[test]
fn non_voter_remove_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_noop!(Elections::remove_voter(Origin::signed(3)), "must be a voter");
});
}
#[test]
fn dupe_remove_should_fail() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::vote(Origin::signed(2), vec![5], 20));
@@ -1051,7 +1051,7 @@ mod tests {
#[test]
fn removed_voter_should_not_be_counted() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1071,7 +1071,7 @@ mod tests {
#[test]
fn reporter_must_be_voter() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_noop!(
Elections::report_defunct_voter(Origin::signed(1), 2),
"reporter must be a voter",
@@ -1081,7 +1081,7 @@ mod tests {
#[test]
fn can_detect_defunct_voter() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1116,7 +1116,7 @@ mod tests {
#[test]
fn report_voter_should_work_and_earn_reward() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1148,8 +1148,8 @@ mod tests {
#[test]
fn report_voter_should_slash_when_bad_report() {
with_externalities(&mut ExtBuilder::default().build(), || {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1180,7 +1180,7 @@ mod tests {
#[test]
fn simple_voting_rounds_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1215,7 +1215,7 @@ mod tests {
#[test]
fn defunct_voter_will_be_counted() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
// This guy's vote is pointless for this round.
@@ -1243,7 +1243,7 @@ mod tests {
#[test]
fn only_desired_seats_are_chosen() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1264,7 +1264,7 @@ mod tests {
#[test]
fn phragmen_should_not_self_vote() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1279,7 +1279,7 @@ mod tests {
#[test]
fn runners_up_should_be_kept() {
with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1306,7 +1306,7 @@ mod tests {
#[test]
fn runners_up_should_be_next_candidates() {
with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1333,7 +1333,7 @@ mod tests {
#[test]
fn runners_up_lose_bond_once_outgoing() {
with_externalities(&mut ExtBuilder::default().desired_runners_up(1).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().desired_runners_up(1).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(2)));
@@ -1364,7 +1364,7 @@ mod tests {
#[test]
fn current_members_are_always_implicitly_next_candidate() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1400,7 +1400,7 @@ mod tests {
fn election_state_is_uninterrupted() {
// what I mean by uninterrupted:
// given no input or stimulants the same members are re-elected.
with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().desired_runners_up(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1433,7 +1433,7 @@ mod tests {
#[test]
fn remove_members_triggers_election() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
@@ -1459,7 +1459,7 @@ mod tests {
#[test]
fn seats_should_be_released_when_no_vote() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1493,7 +1493,7 @@ mod tests {
#[test]
fn outgoing_will_get_the_bond_back() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(balances(&5), (50, 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
@@ -1519,7 +1519,7 @@ mod tests {
#[test]
fn losers_will_lose_the_bond() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
@@ -1542,7 +1542,7 @@ mod tests {
#[test]
fn incoming_outgoing_are_reported() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(5)));
@@ -1587,7 +1587,7 @@ mod tests {
#[test]
fn invalid_votes_are_moot() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(4)));
assert_ok!(Elections::submit_candidacy(Origin::signed(3)));
+5 -7
View File
@@ -23,11 +23,9 @@ use support::{
StorageValue, StorageMap, parameter_types, assert_ok,
traits::{Get, ChangeMembers, Currency}
};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use sr_primitives::{
Perbill, BuildStorage,
testing::Header,
Perbill, BuildStorage, set_and_run_with_externalities, testing::Header,
traits::{BlakeTwo256, IdentityLookup, Block as BlockT},
};
use crate as elections;
@@ -213,7 +211,7 @@ impl ExtBuilder {
self.desired_seats = seats;
self
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
VOTER_BOND.with(|v| *v.borrow_mut() = self.voter_bond);
VOTING_FEE.with(|v| *v.borrow_mut() = self.voting_fee);
PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow_mut() = self.bad_presentation_punishment);
@@ -283,9 +281,9 @@ pub(crate) fn locks(who: &u64) -> Vec<u64> {
Balances::locks(who).iter().map(|l| l.amount).collect::<Vec<u64>>()
}
pub(crate) fn new_test_ext_with_candidate_holes() -> runtime_io::TestExternalities<Blake2Hasher> {
pub(crate) fn new_test_ext_with_candidate_holes() -> runtime_io::TestExternalities {
let mut t = ExtBuilder::default().build();
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
<elections::Candidates<Test>>::put(vec![0, 0, 1]);
elections::CandidateCount::put(1);
<elections::RegisterInfoOf<Test>>::insert(1, (0, 2));
+57 -57
View File
@@ -22,11 +22,11 @@ use crate::mock::*;
use crate::*;
use support::{assert_ok, assert_err, assert_noop};
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
#[test]
fn params_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::next_vote_from(1), 4);
assert_eq!(Elections::next_vote_from(4), 4);
@@ -53,7 +53,7 @@ fn params_should_work() {
#[test]
fn chunking_bool_to_flag_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::bool_to_flag(vec![]), vec![]);
assert_eq!(Elections::bool_to_flag(vec![false]), vec![0]);
assert_eq!(Elections::bool_to_flag(vec![true]), vec![1]);
@@ -98,7 +98,7 @@ fn chunking_bool_to_flag_should_work() {
#[test]
fn chunking_voter_set_growth_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
// create 65. 64 (set0) + 1 (set1)
@@ -122,7 +122,7 @@ fn chunking_voter_set_growth_should_work() {
#[test]
fn chunking_voter_set_reclaim_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
(1..=129).for_each(|i| vote(i, 0));
@@ -159,7 +159,7 @@ fn chunking_voter_set_reclaim_should_work() {
#[test]
fn chunking_approvals_set_growth_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
// create candidates and voters.
(1..=250).for_each(|i| create_candidate(i, (i-1) as u32));
(1..=250).for_each(|i| vote(i, i as usize));
@@ -221,7 +221,7 @@ fn chunking_approvals_set_growth_should_work() {
#[test]
fn chunking_cell_status_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
(1..=63).for_each(|i| vote(i, 0));
@@ -240,7 +240,7 @@ fn chunking_cell_status_works() {
#[test]
fn chunking_voter_index_does_not_take_holes_into_account() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
// create 65. 64 (set0) + 1 (set1)
@@ -265,7 +265,7 @@ fn chunking_voter_index_does_not_take_holes_into_account() {
#[test]
fn chunking_approval_storage_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(3), 1));
@@ -285,7 +285,7 @@ fn chunking_approval_storage_should_work() {
#[test]
fn voting_initial_set_approvals_ignores_voter_index() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
// Last argument is essentially irrelevant. You might get or miss a tip.
@@ -299,7 +299,7 @@ fn voting_initial_set_approvals_ignores_voter_index() {
}
#[test]
fn voting_bad_approval_index_slashes_voters_and_bond_reduces_stake() {
with_externalities(&mut ExtBuilder::default().voting_fee(5).voter_bond(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voting_fee(5).voter_bond(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
(1..=63).for_each(|i| vote(i, 0));
@@ -329,7 +329,7 @@ fn voting_bad_approval_index_slashes_voters_and_bond_reduces_stake() {
#[test]
fn voting_subsequent_set_approvals_checks_voter_index() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(3), vec![], 0, 0, 30));
@@ -353,7 +353,7 @@ fn voting_subsequent_set_approvals_checks_voter_index() {
#[test]
fn voting_cannot_lock_less_than_limit() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_noop!(
@@ -366,7 +366,7 @@ fn voting_cannot_lock_less_than_limit() {
#[test]
fn voting_locking_more_than_total_balance_is_moot() {
with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_eq!(balances(&3), (30, 0));
@@ -382,7 +382,7 @@ fn voting_locking_more_than_total_balance_is_moot() {
#[test]
fn voting_locking_stake_and_reserving_bond_works() {
with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
assert_eq!(balances(&2), (20, 0));
@@ -408,7 +408,7 @@ fn voting_locking_stake_and_reserving_bond_works() {
#[test]
fn voting_without_any_candidate_count_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates().len(), 0);
@@ -422,7 +422,7 @@ fn voting_without_any_candidate_count_should_not_work() {
#[test]
fn voting_setting_an_approval_vote_count_more_than_candidate_count_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
@@ -437,7 +437,7 @@ fn voting_setting_an_approval_vote_count_more_than_candidate_count_should_not_wo
#[test]
fn voting_resubmitting_approvals_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
@@ -456,7 +456,7 @@ fn voting_resubmitting_approvals_should_work() {
#[test]
fn voting_retracting_voter_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
@@ -501,7 +501,7 @@ fn voting_retracting_voter_should_work() {
#[test]
fn voting_invalid_retraction_index_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(3), 0));
@@ -514,7 +514,7 @@ fn voting_invalid_retraction_index_should_not_work() {
#[test]
fn voting_overflow_retraction_index_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(3), 0));
@@ -525,7 +525,7 @@ fn voting_overflow_retraction_index_should_not_work() {
#[test]
fn voting_non_voter_retraction_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(3), 0));
@@ -536,7 +536,7 @@ fn voting_non_voter_retraction_should_not_work() {
#[test]
fn retracting_inactive_voter_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -570,7 +570,7 @@ fn retracting_inactive_voter_should_work() {
#[test]
fn retracting_inactive_voter_with_other_candidates_in_slots_should_work() {
with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().voter_bond(2).build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -605,7 +605,7 @@ fn retracting_inactive_voter_with_other_candidates_in_slots_should_work() {
#[test]
fn retracting_inactive_voter_with_bad_reporter_index_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -634,7 +634,7 @@ fn retracting_inactive_voter_with_bad_reporter_index_should_not_work() {
#[test]
fn retracting_inactive_voter_with_bad_target_index_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -663,7 +663,7 @@ fn retracting_inactive_voter_with_bad_target_index_should_not_work() {
#[test]
fn retracting_active_voter_should_slash_reporter() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(3), 1));
@@ -711,7 +711,7 @@ fn retracting_active_voter_should_slash_reporter() {
#[test]
fn retracting_inactive_voter_by_nonvoter_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -740,7 +740,7 @@ fn retracting_inactive_voter_by_nonvoter_should_not_work() {
#[test]
fn candidacy_simple_candidate_submission_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_eq!(Elections::candidate_reg_info(1), None);
@@ -768,7 +768,7 @@ fn candidacy_simple_candidate_submission_should_work() {
fn candidacy_submission_using_free_slot_should_work() {
let mut t = new_test_ext_with_candidate_holes();
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), vec![0, 0, 1]);
@@ -784,7 +784,7 @@ fn candidacy_submission_using_free_slot_should_work() {
fn candidacy_submission_using_alternative_free_slot_should_work() {
let mut t = new_test_ext_with_candidate_holes();
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), vec![0, 0, 1]);
@@ -800,7 +800,7 @@ fn candidacy_submission_using_alternative_free_slot_should_work() {
fn candidacy_submission_not_using_free_slot_should_not_work() {
let mut t = new_test_ext_with_candidate_holes();
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
System::set_block_number(1);
assert_noop!(
Elections::submit_candidacy(Origin::signed(4), 3),
@@ -811,7 +811,7 @@ fn candidacy_submission_not_using_free_slot_should_not_work() {
#[test]
fn candidacy_bad_candidate_slot_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_noop!(
@@ -823,7 +823,7 @@ fn candidacy_bad_candidate_slot_submission_should_not_work() {
#[test]
fn candidacy_non_free_candidate_slot_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0));
@@ -837,7 +837,7 @@ fn candidacy_non_free_candidate_slot_submission_should_not_work() {
#[test]
fn candidacy_dupe_candidate_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0));
@@ -851,7 +851,7 @@ fn candidacy_dupe_candidate_submission_should_not_work() {
#[test]
fn candidacy_poor_candidate_submission_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_eq!(Elections::candidates(), Vec::<u64>::new());
assert_noop!(
@@ -863,7 +863,7 @@ fn candidacy_poor_candidate_submission_should_not_work() {
#[test]
fn election_voting_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
@@ -892,7 +892,7 @@ fn election_voting_should_work() {
#[test]
fn election_proxy_voting_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(1);
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
@@ -933,7 +933,7 @@ fn election_proxy_voting_should_work() {
#[test]
fn election_simple_tally_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
@@ -972,7 +972,7 @@ fn election_simple_tally_should_work() {
#[test]
fn election_seats_should_be_released() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 1));
@@ -1006,7 +1006,7 @@ fn election_seats_should_be_released() {
#[test]
fn election_presentations_with_zero_staked_deposit_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -1022,7 +1022,7 @@ fn election_presentations_with_zero_staked_deposit_should_not_work() {
#[test]
fn election_double_presentations_should_be_punished() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert!(Balances::can_slash(&4, 10));
System::set_block_number(4);
@@ -1045,7 +1045,7 @@ fn election_double_presentations_should_be_punished() {
#[test]
fn election_presenting_for_double_election_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_eq!(Elections::submit_candidacy(Origin::signed(2), 0), Ok(()));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -1072,7 +1072,7 @@ fn election_presenting_for_double_election_should_not_work() {
#[test]
fn election_presenting_loser_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0));
assert_ok!(Elections::set_approvals(Origin::signed(6), vec![true], 0, 0, 60));
@@ -1105,7 +1105,7 @@ fn election_presenting_loser_should_not_work() {
#[test]
fn election_presenting_loser_first_should_not_matter() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0));
assert_ok!(Elections::set_approvals(Origin::signed(6), vec![true], 0, 0, 60));
@@ -1137,7 +1137,7 @@ fn election_presenting_loser_first_should_not_matter() {
#[test]
fn election_present_outside_of_presentation_period_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
assert_noop!(
@@ -1149,7 +1149,7 @@ fn election_present_outside_of_presentation_period_should_not_work() {
#[test]
fn election_present_with_invalid_vote_index_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 1));
@@ -1165,7 +1165,7 @@ fn election_present_with_invalid_vote_index_should_not_work() {
#[test]
fn election_present_when_presenter_is_poor_should_not_work() {
let test_present = |p| {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.voting_fee(5)
.voter_bond(2)
.bad_presentation_punishment(p)
@@ -1199,7 +1199,7 @@ fn election_present_when_presenter_is_poor_should_not_work() {
#[test]
fn election_invalid_present_tally_should_slash() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
assert_eq!(Balances::total_balance(&4), 40);
@@ -1219,7 +1219,7 @@ fn election_invalid_present_tally_should_slash() {
#[test]
fn election_runners_up_should_be_kept() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
@@ -1280,7 +1280,7 @@ fn election_runners_up_should_be_kept() {
#[test]
fn election_second_tally_should_use_runners_up() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0));
assert_ok!(Elections::set_approvals(Origin::signed(6), vec![true], 0, 0, 60));
@@ -1335,7 +1335,7 @@ fn election_second_tally_should_use_runners_up() {
#[test]
fn election_loser_candidates_bond_gets_slashed() {
with_externalities(&mut ExtBuilder::default().desired_seats(1).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().desired_seats(1).build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
@@ -1374,7 +1374,7 @@ fn election_loser_candidates_bond_gets_slashed() {
#[test]
fn pot_accumulating_weight_and_decaying_should_work() {
with_externalities(&mut ExtBuilder::default().balance_factor(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().balance_factor(10).build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
@@ -1502,7 +1502,7 @@ fn pot_accumulating_weight_and_decaying_should_work() {
#[test]
fn pot_winning_resets_accumulated_pot() {
with_externalities(&mut ExtBuilder::default().balance_factor(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().balance_factor(10).build(), || {
System::set_block_number(4);
assert!(!Elections::presentation_active());
@@ -1564,7 +1564,7 @@ fn pot_winning_resets_accumulated_pot() {
#[test]
fn pot_resubmitting_approvals_stores_pot() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.voter_bond(0)
.voting_fee(0)
.balance_factor(10)
@@ -1629,7 +1629,7 @@ fn pot_resubmitting_approvals_stores_pot() {
#[test]
fn pot_get_offset_should_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Elections::get_offset(100, 0), 0);
assert_eq!(Elections::get_offset(100, 1), 96);
assert_eq!(Elections::get_offset(100, 2), 96 + 93);
@@ -1653,7 +1653,7 @@ fn pot_get_offset_should_work() {
#[test]
fn pot_get_offset_with_zero_decay() {
with_externalities(&mut ExtBuilder::default().decay_ratio(0).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().decay_ratio(0).build(), || {
assert_eq!(Elections::get_offset(100, 0), 0);
assert_eq!(Elections::get_offset(100, 1), 0);
assert_eq!(Elections::get_offset(100, 2), 0);
+6 -9
View File
@@ -634,15 +634,12 @@ mod tests {
use super::*;
use support::{assert_ok, impl_outer_origin, parameter_types};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sr_primitives::{
Perbill,
set_and_run_with_externalities, Perbill, weights::GetDispatchInfo, testing::Header,
traits::{BlakeTwo256, OnInitialize, OnFinalize, IdentityLookup},
weights::GetDispatchInfo,
testing::Header
};
impl_outer_origin! {
@@ -707,7 +704,7 @@ mod tests {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
// We use default for brevity, but you can configure as desired if needed.
balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
@@ -722,7 +719,7 @@ mod tests {
#[test]
fn it_works_for_optional_value() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// Check that GenesisBuilder works properly.
assert_eq!(Example::dummy(), Some(42));
@@ -743,7 +740,7 @@ mod tests {
#[test]
fn it_works_for_default_value() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Example::foo(), 24);
assert_ok!(Example::accumulate_foo(Origin::signed(1), 1));
assert_eq!(Example::foo(), 25);
@@ -752,7 +749,7 @@ mod tests {
#[test]
fn signed_ext_watch_dummy_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let call = <Call<Test>>::set_dummy(10);
let info = DispatchInfo::default();
+13 -13
View File
@@ -293,12 +293,12 @@ where
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use sr_primitives::{
generic::Era, Perbill, DispatchError, weights::Weight, testing::{Digest, Header, Block},
traits::{Bounded, Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
transaction_validity::{InvalidTransaction, UnknownTransaction}, ApplyError,
set_and_run_with_externalities,
};
use support::{
impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch,
@@ -419,8 +419,8 @@ mod tests {
}.assimilate_storage(&mut t).unwrap();
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(2, 69)));
let weight = xt.get_dispatch_info().weight as u64;
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new(t);
with_externalities(&mut t, || {
let mut t = runtime_io::TestExternalities::new(t);
set_and_run_with_externalities(&mut t, || {
Executive::initialize_block(&Header::new(
1,
H256::default(),
@@ -435,7 +435,7 @@ mod tests {
});
}
fn new_test_ext(balance_factor: u64) -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext(balance_factor: u64) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
balances::GenesisConfig::<Runtime> {
balances: vec![(1, 111 * balance_factor)],
@@ -446,7 +446,7 @@ mod tests {
#[test]
fn block_import_works() {
with_externalities(&mut new_test_ext(1), || {
set_and_run_with_externalities(&mut new_test_ext(1), || {
Executive::execute_block(Block {
header: Header {
parent_hash: [69u8; 32].into(),
@@ -463,7 +463,7 @@ mod tests {
#[test]
#[should_panic]
fn block_import_of_bad_state_root_fails() {
with_externalities(&mut new_test_ext(1), || {
set_and_run_with_externalities(&mut new_test_ext(1), || {
Executive::execute_block(Block {
header: Header {
parent_hash: [69u8; 32].into(),
@@ -480,7 +480,7 @@ mod tests {
#[test]
#[should_panic]
fn block_import_of_bad_extrinsic_root_fails() {
with_externalities(&mut new_test_ext(1), || {
set_and_run_with_externalities(&mut new_test_ext(1), || {
Executive::execute_block(Block {
header: Header {
parent_hash: [69u8; 32].into(),
@@ -499,7 +499,7 @@ mod tests {
let mut t = new_test_ext(1);
// bad nonce check!
let xt = sr_primitives::testing::TestXt(sign_extra(1, 30, 0), Call::Balances(BalancesCall::transfer(33, 69)));
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
Executive::initialize_block(&Header::new(
1,
H256::default(),
@@ -521,7 +521,7 @@ mod tests {
let encoded_len = encoded.len() as Weight;
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get();
let num_to_exhaust_block = limit / encoded_len;
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
Executive::initialize_block(&Header::new(
1,
H256::default(),
@@ -557,7 +557,7 @@ mod tests {
let x2 = sr_primitives::testing::TestXt(sign_extra(1, 2, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let len = xt.clone().encode().len() as u32;
let mut t = new_test_ext(1);
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), 0);
assert_eq!(<system::Module<Runtime>>::all_extrinsics_weight(), 0);
@@ -581,7 +581,7 @@ mod tests {
let xt = sr_primitives::testing::TestXt(None, Call::Balances(BalancesCall::set_balance(33, 69, 69)));
let mut t = new_test_ext(1);
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
assert_eq!(Executive::validate_transaction(xt.clone()), Ok(Default::default()));
assert_eq!(
Executive::apply_extrinsic(xt),
@@ -599,7 +599,7 @@ mod tests {
let id: LockIdentifier = *b"0 ";
let execute_with_lock = |lock: WithdrawReasons| {
let mut t = new_test_ext(1);
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
<balances::Module<Runtime> as LockableCurrency<u64>>::set_lock(
id,
&1,
+8 -7
View File
@@ -247,11 +247,12 @@ impl<T: Trait> ProvideInherent for Module<T> {
mod tests {
use super::*;
use runtime_io::{with_externalities, TestExternalities};
use runtime_io::TestExternalities;
use primitives::H256;
use sr_primitives::traits::{BlakeTwo256, IdentityLookup, OnFinalize, Header as HeaderT};
use sr_primitives::testing::Header;
use sr_primitives::Perbill;
use sr_primitives::{
set_and_run_with_externalities, testing::Header, Perbill,
traits::{BlakeTwo256, IdentityLookup, OnFinalize, Header as HeaderT},
};
use support::{assert_ok, impl_outer_origin, parameter_types};
use srml_system as system;
use std::cell::RefCell;
@@ -321,7 +322,7 @@ mod tests {
#[test]
fn median_works() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
FinalityTracker::update_hint(Some(500));
assert_eq!(FinalityTracker::median(), 250);
assert!(NOTIFICATIONS.with(|n| n.borrow().is_empty()));
@@ -331,7 +332,7 @@ mod tests {
#[test]
fn notifies_when_stalled() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
let mut parent_hash = System::parent_hash();
for i in 2..106 {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
@@ -350,7 +351,7 @@ mod tests {
#[test]
fn recent_notifications_prevent_stalling() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
let mut parent_hash = System::parent_hash();
for i in 2..106 {
System::initialize(&i, &parent_hash, &Default::default(), &Default::default());
+3 -3
View File
@@ -25,7 +25,7 @@ use sr_primitives::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use primitives::{Blake2Hasher, H256};
use primitives::H256;
use support::{parameter_types, impl_outer_event, impl_outer_origin};
use super::*;
@@ -118,7 +118,7 @@ impl ExtBuilder {
}
// builds genesis config
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
@@ -137,7 +137,7 @@ impl ExtBuilder {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext() -> runtime_io::TestExternalities {
system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap()
+46 -46
View File
@@ -22,14 +22,14 @@
use super::*;
use crate::mock::{new_test_ext, ExtBuilder, GenericAsset, Origin, System, Test, TestEvent};
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
use support::{assert_noop, assert_ok};
#[test]
fn issuing_asset_units_to_issuer_should_work() {
let balance = 100;
with_externalities(&mut ExtBuilder::default().free_balance((16000, 1, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((16000, 1, 100)).build(), || {
let default_permission = PermissionLatest {
update: Owner::Address(1),
mint: Owner::Address(1),
@@ -51,7 +51,7 @@ fn issuing_asset_units_to_issuer_should_work() {
#[test]
fn issuing_with_next_asset_id_overflow_should_not_work() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
NextAssetId::<Test>::put(u32::max_value());
@@ -79,7 +79,7 @@ fn issuing_with_next_asset_id_overflow_should_not_work() {
fn querying_total_supply_should_work() {
let asset_id = 1000;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -127,7 +127,7 @@ fn querying_total_supply_should_work() {
fn transferring_amount_should_work() {
let asset_id = 1000;
let free_balance = 100;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -168,7 +168,7 @@ fn transferring_amount_should_work() {
#[test]
fn transferring_amount_should_fail_when_transferring_more_than_free_balance() {
let asset_id = 1000;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -195,7 +195,7 @@ fn transferring_amount_should_fail_when_transferring_more_than_free_balance() {
fn transferring_less_than_one_unit_should_not_work() {
let asset_id = 1000;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -233,7 +233,7 @@ fn self_transfer_should_fail() {
let asset_id = 1000;
let balance = 100;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -259,7 +259,7 @@ fn self_transfer_should_fail() {
#[test]
fn transferring_more_units_than_total_supply_should_not_work() {
let asset_id = 1000;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let default_permission = PermissionLatest {
@@ -286,7 +286,7 @@ fn transferring_more_units_than_total_supply_should_not_work() {
// Ensures it uses fake money for staking asset id.
#[test]
fn staking_asset_id_should_return_0() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(GenericAsset::staking_asset_id(), 16000);
});
}
@@ -294,7 +294,7 @@ fn staking_asset_id_should_return_0() {
// Ensures it uses fake money for spending asset id.
#[test]
fn spending_asset_id_should_return_10() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(GenericAsset::spending_asset_id(), 16001);
});
}
@@ -305,7 +305,7 @@ fn spending_asset_id_should_return_10() {
// -Â total_balance should return 0
#[test]
fn total_balance_should_be_zero() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(GenericAsset::total_balance(&0, &0), 0);
});
}
@@ -323,7 +323,7 @@ fn total_balance_should_be_equal_to_account_balance() {
mint: Owner::Address(1),
burn: Owner::Address(1),
};
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
assert_ok!(GenericAsset::create(
@@ -348,7 +348,7 @@ fn total_balance_should_be_equal_to_account_balance() {
// -Â free_balance should return 50.
#[test]
fn free_balance_should_only_return_account_free_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 70);
assert_eq!(GenericAsset::free_balance(&1, &0), 50);
});
@@ -363,7 +363,7 @@ fn free_balance_should_only_return_account_free_balance() {
// -Â total_balance should equals to account balance + free balance.
#[test]
fn total_balance_should_be_equal_to_sum_of_account_balance_and_free_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 70);
assert_eq!(GenericAsset::total_balance(&1, &0), 120);
});
@@ -378,7 +378,7 @@ fn total_balance_should_be_equal_to_sum_of_account_balance_and_free_balance() {
// - reserved_balance should return 70.
#[test]
fn reserved_balance_should_only_return_account_reserved_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 50)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 70);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 70);
});
@@ -394,7 +394,7 @@ fn reserved_balance_should_only_return_account_reserved_balance() {
// - reserved_balance = amount
#[test]
fn set_reserved_balance_should_add_balance_as_reserved() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
GenericAsset::set_reserved_balance(&1, &0, 50);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 50);
});
@@ -410,7 +410,7 @@ fn set_reserved_balance_should_add_balance_as_reserved() {
// - New free_balance should replace older free_balance.
#[test]
fn set_free_balance_should_add_amount_as_free_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_free_balance(&1, &0, 50);
assert_eq!(GenericAsset::free_balance(&1, &0), 50);
});
@@ -429,7 +429,7 @@ fn set_free_balance_should_add_amount_as_free_balance() {
// - new reserved_balance = original free balance + reserved amount
#[test]
fn reserve_should_moves_amount_from_balance_to_reserved_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
assert_ok!(GenericAsset::reserve(&1, &0, 70));
assert_eq!(GenericAsset::free_balance(&1, &0), 30);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 70);
@@ -448,7 +448,7 @@ fn reserve_should_moves_amount_from_balance_to_reserved_balance() {
// - Should throw an error.
#[test]
fn reserve_should_not_moves_amount_from_balance_to_reserved_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
assert_noop!(GenericAsset::reserve(&1, &0, 120), "not enough free funds");
assert_eq!(GenericAsset::free_balance(&1, &0), 100);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 0);
@@ -466,7 +466,7 @@ fn reserve_should_not_moves_amount_from_balance_to_reserved_balance() {
// - unreserved should return 20.
#[test]
fn unreserve_should_return_substratced_value_from_unreserved_amount_by_actual_acount_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::unreserve(&1, &0, 120), 20);
});
@@ -483,7 +483,7 @@ fn unreserve_should_return_substratced_value_from_unreserved_amount_by_actual_ac
// - unreserved should return None.
#[test]
fn unreserve_should_return_none() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::unreserve(&1, &0, 50), 0);
});
@@ -500,7 +500,7 @@ fn unreserve_should_return_none() {
// - free_balance should be 200.
#[test]
fn unreserve_should_increase_free_balance_by_reserved_balance() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
GenericAsset::unreserve(&1, &0, 120);
assert_eq!(GenericAsset::free_balance(&1, &0), 200);
@@ -518,7 +518,7 @@ fn unreserve_should_increase_free_balance_by_reserved_balance() {
// - reserved_balance should be 0.
#[test]
fn unreserve_should_deduct_reserved_balance_by_reserved_amount() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_free_balance(&1, &0, 100);
GenericAsset::unreserve(&1, &0, 120);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 0);
@@ -536,7 +536,7 @@ fn unreserve_should_deduct_reserved_balance_by_reserved_amount() {
// - slash should return None.
#[test]
fn slash_should_return_slash_reserved_amount() {
with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().free_balance((1, 0, 100)).build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::slash(&1, &0, 70), None);
});
@@ -550,7 +550,7 @@ fn slash_should_return_slash_reserved_amount() {
// - Should return slashed_reserved - reserved_balance.
#[test]
fn slash_reserved_should_deducts_up_to_amount_from_reserved_balance() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::slash_reserved(&1, &0, 150), Some(50));
});
@@ -564,7 +564,7 @@ fn slash_reserved_should_deducts_up_to_amount_from_reserved_balance() {
// - Should return None.
#[test]
fn slash_reserved_should_return_none() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::slash_reserved(&1, &0, 100), None);
});
@@ -579,7 +579,7 @@ fn slash_reserved_should_return_none() {
// - Should not return None.
#[test]
fn repatriate_reserved_return_amount_substracted_by_slash_amount() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 130), 30);
});
@@ -594,7 +594,7 @@ fn repatriate_reserved_return_amount_substracted_by_slash_amount() {
// - Should return None.
#[test]
fn repatriate_reserved_return_none() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 90), 0);
});
@@ -608,7 +608,7 @@ fn repatriate_reserved_return_none() {
// - Should create a new reserved asset.
#[test]
fn create_reserved_should_create_a_default_account_with_the_balance_given() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let default_permission = PermissionLatest {
update: Owner::Address(1),
mint: Owner::Address(1),
@@ -643,7 +643,7 @@ fn create_reserved_should_create_a_default_account_with_the_balance_given() {
// - Should throw a permission error
#[test]
fn mint_should_throw_permission_error() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
let origin = 1;
let asset_id = 4;
let to_account = 2;
@@ -666,7 +666,7 @@ fn mint_should_throw_permission_error() {
// - Should not change `origins` free_balance.
#[test]
fn mint_should_increase_asset() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -707,7 +707,7 @@ fn mint_should_increase_asset() {
// - Should throw a permission error.
#[test]
fn burn_should_throw_permission_error() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -733,7 +733,7 @@ fn burn_should_throw_permission_error() {
// - Should not change `origin`'s free_balance.
#[test]
fn burn_should_burn_an_asset() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -779,7 +779,7 @@ fn burn_should_burn_an_asset() {
// - The account origin should have burn, mint and update permissions.
#[test]
fn check_permission_should_return_correct_permission() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -825,7 +825,7 @@ fn check_permission_should_return_correct_permission() {
// - The account origin should not have burn, mint and update permissions.
#[test]
fn check_permission_should_return_false_for_no_permission() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -871,7 +871,7 @@ fn check_permission_should_return_false_for_no_permission() {
// - The account origin should have update and mint permissions.
#[test]
fn update_permission_should_change_permission() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -923,7 +923,7 @@ fn update_permission_should_change_permission() {
// - Should throw an error stating "Origin does not have enough permission to update permissions."
#[test]
fn update_permission_should_throw_error_when_lack_of_permissions() {
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().free_balance((16000, 1, 100000)).build(),
|| {
let origin = 1;
@@ -974,7 +974,7 @@ fn update_permission_should_throw_error_when_lack_of_permissions() {
// - Permissions must have burn, mint and updatePermission for the given asset_id.
#[test]
fn create_asset_works_with_given_asset_id_and_from_account() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let origin = 1;
let from_account: Option<<Test as system::Trait>::AccountId> = Some(1);
@@ -1011,7 +1011,7 @@ fn create_asset_works_with_given_asset_id_and_from_account() {
// - `create_asset` should not work.
#[test]
fn create_asset_with_non_reserved_asset_id_should_not_work() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let origin = 1;
let from_account: Option<<Test as system::Trait>::AccountId> = Some(1);
@@ -1045,7 +1045,7 @@ fn create_asset_with_non_reserved_asset_id_should_not_work() {
// - `create_asset` should not work.
#[test]
fn create_asset_with_a_taken_asset_id_should_not_work() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let origin = 1;
let from_account: Option<<Test as system::Trait>::AccountId> = Some(1);
@@ -1090,7 +1090,7 @@ fn create_asset_with_a_taken_asset_id_should_not_work() {
// - Should create a reserved token.
#[test]
fn create_asset_should_create_a_reserved_asset_when_from_account_is_none() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let origin = 1;
let from_account: Option<<Test as system::Trait>::AccountId> = None;
@@ -1133,7 +1133,7 @@ fn create_asset_should_create_a_reserved_asset_when_from_account_is_none() {
// - Should not create a `reserved_asset`.
#[test]
fn create_asset_should_create_a_user_asset() {
with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().next_asset_id(10).build(), || {
let origin = 1;
let from_account: Option<<Test as system::Trait>::AccountId> = None;
@@ -1180,7 +1180,7 @@ fn update_permission_should_raise_event() {
burn: Owner::Address(origin),
};
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
@@ -1223,7 +1223,7 @@ fn mint_should_raise_event() {
let to = 2;
let amount = 100;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
@@ -1262,7 +1262,7 @@ fn burn_should_raise_event() {
};
let amount = 100;
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
+2 -2
View File
@@ -21,7 +21,7 @@
use sr_primitives::{Perbill, DigestItem, traits::IdentityLookup, testing::{Header, UintAuthorityId}};
use runtime_io;
use support::{impl_outer_origin, impl_outer_event, parameter_types};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use codec::{Encode, Decode};
use crate::{AuthorityId, GenesisConfig, Trait, Module, ConsensusLog};
use substrate_finality_grandpa_primitives::GRANDPA_ENGINE_ID;
@@ -82,7 +82,7 @@ pub fn to_authorities(vec: Vec<(u64, u64)>) -> Vec<(AuthorityId, u64)> {
.collect()
}
pub fn new_test_ext(authorities: Vec<(u64, u64)>) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext(authorities: Vec<(u64, u64)>) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig {
authorities: to_authorities(authorities),
+7 -9
View File
@@ -18,9 +18,7 @@
#![cfg(test)]
use sr_primitives::testing::Digest;
use sr_primitives::traits::{Header, OnFinalize};
use runtime_io::with_externalities;
use sr_primitives::{set_and_run_with_externalities, testing::Digest, traits::{Header, OnFinalize}};
use crate::mock::*;
use system::{EventRecord, Phase};
use codec::{Decode, Encode};
@@ -29,7 +27,7 @@ use super::*;
#[test]
fn authorities_change_logged() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 0, None).unwrap();
@@ -57,7 +55,7 @@ fn authorities_change_logged() {
#[test]
fn authorities_change_logged_after_delay() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
Grandpa::on_finalize(1);
@@ -90,7 +88,7 @@ fn authorities_change_logged_after_delay() {
#[test]
fn cannot_schedule_change_when_one_pending() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
assert!(<PendingChange<Test>>::exists());
@@ -133,7 +131,7 @@ fn new_decodes_from_old() {
#[test]
fn dispatch_forced_change() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
Grandpa::schedule_change(
to_authorities(vec![(4, 1), (5, 1), (6, 1)]),
@@ -205,7 +203,7 @@ fn dispatch_forced_change() {
#[test]
fn schedule_pause_only_when_live() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
// we schedule a pause at block 1 with delay of 1
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
Grandpa::schedule_pause(1).unwrap();
@@ -240,7 +238,7 @@ fn schedule_pause_only_when_live() {
#[test]
fn schedule_resume_only_when_paused() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
set_and_run_with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
// the set is currently live, resuming it is an error
+2 -2
View File
@@ -25,7 +25,7 @@ use sr_primitives::Perbill;
use sr_staking_primitives::{SessionIndex, offence::ReportOffence};
use sr_primitives::testing::{Header, UintAuthorityId, TestXt};
use sr_primitives::traits::{IdentityLookup, BlakeTwo256, ConvertInto};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use support::{impl_outer_origin, impl_outer_dispatch, parameter_types};
use {runtime_io, system};
@@ -85,7 +85,7 @@ impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
}
}
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext() -> runtime_io::TestExternalities {
let t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
t.into()
}
+8 -9
View File
@@ -21,10 +21,9 @@
use super::*;
use crate::mock::*;
use offchain::testing::TestOffchainExt;
use primitives::offchain::OpaquePeerId;
use runtime_io::with_externalities;
use primitives::offchain::{OpaquePeerId, OffchainExt};
use support::{dispatch, assert_noop};
use sr_primitives::testing::UintAuthorityId;
use sr_primitives::{set_and_run_with_externalities, testing::UintAuthorityId};
#[test]
@@ -49,7 +48,7 @@ fn test_unresponsiveness_slash_fraction() {
#[test]
fn should_report_offline_validators() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let block = 1;
System::set_block_number(block);
@@ -125,7 +124,7 @@ fn heartbeat(
#[test]
fn should_mark_online_validator_when_heartbeat_is_received() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
@@ -160,7 +159,7 @@ fn should_mark_online_validator_when_heartbeat_is_received() {
#[test]
fn late_heartbeat_should_fail() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 4, 4, 5, 6]));
@@ -181,9 +180,9 @@ fn late_heartbeat_should_fail() {
fn should_generate_heartbeats() {
let mut ext = new_test_ext();
let (offchain, state) = TestOffchainExt::new();
ext.set_offchain_externalities(offchain);
ext.register_extension(OffchainExt::new(offchain));
with_externalities(&mut ext, || {
set_and_run_with_externalities(&mut ext, || {
// given
let block = 1;
System::set_block_number(block);
@@ -219,7 +218,7 @@ fn should_generate_heartbeats() {
#[test]
fn should_cleanup_received_heartbeats_on_session_end() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
advance_session();
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3]));
+2 -2
View File
@@ -22,7 +22,7 @@ use std::collections::HashSet;
use ref_thread_local::{ref_thread_local, RefThreadLocal};
use sr_primitives::testing::Header;
use sr_primitives::Perbill;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use support::{impl_outer_origin, parameter_types};
use {runtime_io, system};
use crate::{GenesisConfig, Module, Trait, IsDeadAccount, OnNewAccount, ResolveHint};
@@ -96,7 +96,7 @@ impl Trait for Runtime {
type Event = ();
}
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext() -> runtime_io::TestExternalities {
{
let mut h = ALIVE.borrow_mut();
h.clear();
+5 -5
View File
@@ -20,11 +20,11 @@
use super::*;
use crate::mock::{Indices, new_test_ext, make_account, kill_account, TestIsDeadAccount};
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
#[test]
fn indexing_lookup_should_work() {
with_externalities(
set_and_run_with_externalities(
&mut new_test_ext(),
|| {
assert_eq!(Indices::lookup_index(0), Some(1));
@@ -38,7 +38,7 @@ fn indexing_lookup_should_work() {
#[test]
fn default_indexing_on_new_accounts_should_work() {
with_externalities(
set_and_run_with_externalities(
&mut new_test_ext(),
|| {
assert_eq!(Indices::lookup_index(4), None);
@@ -50,7 +50,7 @@ fn default_indexing_on_new_accounts_should_work() {
#[test]
fn reclaim_indexing_on_new_accounts_should_work() {
with_externalities(
set_and_run_with_externalities(
&mut new_test_ext(),
|| {
assert_eq!(Indices::lookup_index(1), Some(2));
@@ -66,7 +66,7 @@ fn reclaim_indexing_on_new_accounts_should_work() {
#[test]
fn alive_account_should_prevent_reclaim() {
with_externalities(
set_and_run_with_externalities(
&mut new_test_ext(),
|| {
assert!(!TestIsDeadAccount::is_dead_account(&2));
+9 -9
View File
@@ -193,12 +193,12 @@ mod tests {
use std::cell::RefCell;
use support::{assert_ok, assert_noop, impl_outer_origin, parameter_types};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sr_primitives::{
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
use system::EnsureSignedBy;
@@ -281,7 +281,7 @@ mod tests {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
// We use default for brevity, but you can configure as desired if needed.
GenesisConfig::<Test>{
@@ -293,7 +293,7 @@ mod tests {
#[test]
fn query_membership_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Membership::members(), vec![10, 20, 30]);
assert_eq!(MEMBERS.with(|m| m.borrow().clone()), vec![10, 20, 30]);
});
@@ -301,7 +301,7 @@ mod tests {
#[test]
fn add_member_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Membership::add_member(Origin::signed(5), 15), "bad origin");
assert_noop!(Membership::add_member(Origin::signed(1), 10), "already a member");
assert_ok!(Membership::add_member(Origin::signed(1), 15));
@@ -312,7 +312,7 @@ mod tests {
#[test]
fn remove_member_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Membership::remove_member(Origin::signed(5), 20), "bad origin");
assert_noop!(Membership::remove_member(Origin::signed(2), 15), "not a member");
assert_ok!(Membership::remove_member(Origin::signed(2), 20));
@@ -323,7 +323,7 @@ mod tests {
#[test]
fn swap_member_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Membership::swap_member(Origin::signed(5), 10, 25), "bad origin");
assert_noop!(Membership::swap_member(Origin::signed(3), 15, 25), "not a member");
assert_noop!(Membership::swap_member(Origin::signed(3), 10, 30), "already a member");
@@ -337,7 +337,7 @@ mod tests {
#[test]
fn reset_members_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Membership::reset_members(Origin::signed(1), vec![20, 40, 30]), "bad origin");
assert_ok!(Membership::reset_members(Origin::signed(4), vec![20, 40, 30]));
assert_eq!(Membership::members(), vec![20, 30, 40]);
+2 -2
View File
@@ -28,7 +28,7 @@ use sr_staking_primitives::{
};
use sr_primitives::testing::Header;
use sr_primitives::traits::{IdentityLookup, BlakeTwo256};
use substrate_primitives::{H256, Blake2Hasher};
use substrate_primitives::H256;
use support::{impl_outer_origin, impl_outer_event, parameter_types, StorageMap, StorageDoubleMap};
use {runtime_io, system};
@@ -103,7 +103,7 @@ impl_outer_event! {
}
}
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext() -> runtime_io::TestExternalities {
let t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
t.into()
}
+8 -8
View File
@@ -24,11 +24,11 @@ use crate::mock::{
offence_reports,
};
use system::{EventRecord, Phase};
use runtime_io::with_externalities;
use sr_primitives::set_and_run_with_externalities;
#[test]
fn should_report_an_authority_and_trigger_on_offence() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -51,7 +51,7 @@ fn should_report_an_authority_and_trigger_on_offence() {
#[test]
fn should_calculate_the_fraction_correctly() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -83,7 +83,7 @@ fn should_calculate_the_fraction_correctly() {
#[test]
fn should_not_report_the_same_authority_twice_in_the_same_slot() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -113,7 +113,7 @@ fn should_not_report_the_same_authority_twice_in_the_same_slot() {
#[test]
fn should_report_in_different_time_slot() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -143,7 +143,7 @@ fn should_report_in_different_time_slot() {
#[test]
fn should_deposit_event() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -171,7 +171,7 @@ fn should_deposit_event() {
#[test]
fn doesnt_deposit_event_for_dups() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -208,7 +208,7 @@ fn doesnt_deposit_event_for_dups() {
fn should_properly_count_offences() {
// We report two different authorities for the same issue. Ultimately, the 1st authority
// should have `count` equal 2 and the count of the 2nd one should be equal to 1.
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
@@ -161,10 +161,12 @@ impl<T: Trait> Module<T> {
#[cfg(test)]
mod tests {
use super::*;
use primitives::{H256, Blake2Hasher};
use sr_primitives::{Perbill, traits::{BlakeTwo256, OnInitialize, Header as _, IdentityLookup}, testing::Header};
use primitives::H256;
use sr_primitives::{
Perbill, traits::{BlakeTwo256, OnInitialize, Header as _, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
use support::{impl_outer_origin, parameter_types};
use runtime_io::with_externalities;
#[derive(Clone, PartialEq, Eq)]
pub struct Test;
@@ -202,7 +204,7 @@ mod tests {
type System = system::Module<Test>;
type Randomness = Module<Test>;
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
t.into()
}
@@ -229,7 +231,7 @@ mod tests {
#[test]
fn test_random_material_parital() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let genesis_hash = System::parent_hash();
setup_blocks(38);
@@ -243,7 +245,7 @@ mod tests {
#[test]
fn test_random_material_filled() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let genesis_hash = System::parent_hash();
setup_blocks(81);
@@ -258,7 +260,7 @@ mod tests {
#[test]
fn test_random_material_filled_twice() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let genesis_hash = System::parent_hash();
setup_blocks(162);
@@ -273,7 +275,7 @@ mod tests {
#[test]
fn test_random() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
setup_blocks(162);
assert_eq!(System::block_number(), 162);
+2 -2
View File
@@ -20,7 +20,7 @@ use super::*;
use std::cell::RefCell;
use support::{impl_outer_origin, parameter_types};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sr_primitives::{
@@ -132,7 +132,7 @@ impl Trait for Test {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
// We use default for brevity, but you can configure as desired if needed.
balances::GenesisConfig::<Test> {
+17 -18
View File
@@ -20,8 +20,7 @@ use super::*;
use mock::*;
use support::{assert_ok, assert_noop};
use runtime_io::with_externalities;
use sr_primitives::traits::OnInitialize;
use sr_primitives::{set_and_run_with_externalities, traits::OnInitialize};
type ScoredPool = Module<Test>;
type System = system::Module<Test>;
@@ -32,7 +31,7 @@ const INDEX_ERR: &str = "index does not match requested account";
#[test]
fn query_membership_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(ScoredPool::members(), vec![20, 40]);
assert_eq!(Balances::reserved_balance(&31), CandidateDeposit::get());
assert_eq!(Balances::reserved_balance(&40), CandidateDeposit::get());
@@ -42,7 +41,7 @@ fn query_membership_works() {
#[test]
fn submit_candidacy_must_not_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(
ScoredPool::submit_candidacy(Origin::signed(99)),
"balance too low to submit candidacy"
@@ -56,7 +55,7 @@ fn submit_candidacy_must_not_work() {
#[test]
fn submit_candidacy_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 15;
@@ -71,7 +70,7 @@ fn submit_candidacy_works() {
#[test]
fn scoring_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 15;
let score = 99;
@@ -89,7 +88,7 @@ fn scoring_works() {
#[test]
fn scoring_same_element_with_same_score_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 31;
let index = find_in_pool(who).expect("entity must be in pool") as u32;
@@ -109,7 +108,7 @@ fn scoring_same_element_with_same_score_works() {
#[test]
fn kicking_works_only_for_authorized() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let who = 40;
let index = find_in_pool(who).expect("entity must be in pool") as u32;
assert_noop!(ScoredPool::kick(Origin::signed(99), who, index), "bad origin");
@@ -118,7 +117,7 @@ fn kicking_works_only_for_authorized() {
#[test]
fn kicking_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 40;
assert_eq!(Balances::reserved_balance(&who), CandidateDeposit::get());
@@ -138,7 +137,7 @@ fn kicking_works() {
#[test]
fn unscored_entities_must_not_be_used_for_filling_members() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
// we submit a candidacy, score will be `None`
assert_ok!(ScoredPool::submit_candidacy(Origin::signed(15)));
@@ -163,7 +162,7 @@ fn unscored_entities_must_not_be_used_for_filling_members() {
#[test]
fn refreshing_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 15;
assert_ok!(ScoredPool::submit_candidacy(Origin::signed(who)));
@@ -181,7 +180,7 @@ fn refreshing_works() {
#[test]
fn refreshing_happens_every_period() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
System::set_block_number(1);
assert_ok!(ScoredPool::submit_candidacy(Origin::signed(15)));
@@ -201,7 +200,7 @@ fn refreshing_happens_every_period() {
#[test]
fn withdraw_candidacy_must_only_work_for_members() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let who = 77;
let index = 0;
assert_noop!( ScoredPool::withdraw_candidacy(Origin::signed(who), index), INDEX_ERR);
@@ -210,7 +209,7 @@ fn withdraw_candidacy_must_only_work_for_members() {
#[test]
fn oob_index_should_abort() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let who = 40;
let oob_index = ScoredPool::pool().len() as u32;
assert_noop!(ScoredPool::withdraw_candidacy(Origin::signed(who), oob_index), OOB_ERR);
@@ -221,7 +220,7 @@ fn oob_index_should_abort() {
#[test]
fn index_mismatches_should_abort() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let who = 40;
let index = 3;
assert_noop!(ScoredPool::withdraw_candidacy(Origin::signed(who), index), INDEX_ERR);
@@ -232,7 +231,7 @@ fn index_mismatches_should_abort() {
#[test]
fn withdraw_unscored_candidacy_must_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 5;
@@ -247,7 +246,7 @@ fn withdraw_unscored_candidacy_must_work() {
#[test]
fn withdraw_scored_candidacy_must_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 40;
assert_eq!(Balances::reserved_balance(&who), CandidateDeposit::get());
@@ -265,7 +264,7 @@ fn withdraw_scored_candidacy_must_work() {
#[test]
fn candidacy_resubmitting_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// given
let who = 15;
+7 -6
View File
@@ -312,9 +312,10 @@ impl<T: Trait, D: AsRef<[u8]>> support::traits::KeyOwnerProofSystem<(KeyTypeId,
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use primitives::{Blake2Hasher, crypto::key_types::DUMMY};
use sr_primitives::{traits::OnInitialize, testing::UintAuthorityId};
use primitives::crypto::key_types::DUMMY;
use sr_primitives::{
traits::OnInitialize, testing::UintAuthorityId, set_and_run_with_externalities,
};
use crate::mock::{
NEXT_VALIDATORS, force_new_session,
set_next_validators, Test, System, Session,
@@ -323,7 +324,7 @@ mod tests {
type Historical = Module<Test>;
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
crate::GenesisConfig::<Test> {
keys: NEXT_VALIDATORS.with(|l|
@@ -335,7 +336,7 @@ mod tests {
#[test]
fn generated_proof_is_good() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
set_next_validators(vec![1, 2]);
force_new_session();
@@ -376,7 +377,7 @@ mod tests {
#[test]
fn prune_up_to_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
for i in 1..101u64 {
set_next_validators(vec![i]);
force_new_session();
+13 -15
View File
@@ -680,11 +680,9 @@ impl<T: Trait, Inner: FindAuthor<u32>> FindAuthor<T::ValidatorId>
mod tests {
use super::*;
use support::assert_ok;
use runtime_io::with_externalities;
use primitives::{Blake2Hasher, crypto::key_types::DUMMY};
use primitives::crypto::key_types::DUMMY;
use sr_primitives::{
traits::OnInitialize,
testing::UintAuthorityId,
traits::OnInitialize, set_and_run_with_externalities, testing::UintAuthorityId,
};
use mock::{
NEXT_VALIDATORS, SESSION_CHANGED, TEST_SESSION_CHANGED, authorities, force_new_session,
@@ -692,7 +690,7 @@ mod tests {
reset_before_session_end_called, before_session_end_called,
};
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
keys: NEXT_VALIDATORS.with(|l|
@@ -710,7 +708,7 @@ mod tests {
#[test]
fn simple_setup_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
assert_eq!(Session::validators(), vec![1, 2, 3]);
});
@@ -718,7 +716,7 @@ mod tests {
#[test]
fn put_get_keys() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Session::put_keys(&10, &UintAuthorityId(10).into());
assert_eq!(Session::load_keys(&10), Some(UintAuthorityId(10).into()));
})
@@ -727,7 +725,7 @@ mod tests {
#[test]
fn keys_cleared_on_kill() {
let mut ext = new_test_ext();
with_externalities(&mut ext, || {
set_and_run_with_externalities(&mut ext, || {
assert_eq!(Session::validators(), vec![1, 2, 3]);
assert_eq!(Session::load_keys(&1), Some(UintAuthorityId(1).into()));
@@ -744,7 +742,7 @@ mod tests {
fn authorities_should_track_validators() {
reset_before_session_end_called();
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
set_next_validators(vec![1, 2]);
force_new_session();
initialize_block(1);
@@ -795,7 +793,7 @@ mod tests {
#[test]
fn should_work_with_early_exit() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
set_session_length(10);
initialize_block(1);
@@ -818,7 +816,7 @@ mod tests {
#[test]
fn session_change_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// Block 1: No change
initialize_block(1);
assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
@@ -848,7 +846,7 @@ mod tests {
#[test]
fn duplicates_are_not_allowed() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
Session::on_initialize(1);
assert!(Session::set_keys(Origin::signed(4), UintAuthorityId(1).into(), vec![]).is_err());
@@ -863,7 +861,7 @@ mod tests {
fn session_changed_flag_works() {
reset_before_session_end_called();
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
TEST_SESSION_CHANGED.with(|l| *l.borrow_mut() = true);
force_new_session();
@@ -952,7 +950,7 @@ mod tests {
#[test]
fn session_keys_generate_output_works_as_set_keys_input() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let new_keys = mock::MockSessionKeys::generate(None);
assert_ok!(
Session::set_keys(
@@ -966,7 +964,7 @@ mod tests {
#[test]
fn return_true_if_more_than_third_is_disabled() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
set_next_validators(vec![1, 2, 3, 4, 5, 6, 7]);
force_new_session();
initialize_block(1);
+3 -3
View File
@@ -22,7 +22,7 @@ use sr_primitives::curve::PiecewiseLinear;
use sr_primitives::traits::{IdentityLookup, Convert, OpaqueKeys, OnInitialize, SaturatedConversion};
use sr_primitives::testing::{Header, UintAuthorityId};
use sr_staking_primitives::SessionIndex;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use runtime_io;
use support::{assert_ok, impl_outer_origin, parameter_types, StorageLinkedMap};
use support::traits::{Currency, Get, FindAuthor};
@@ -274,7 +274,7 @@ impl ExtBuilder {
pub fn set_associated_consts(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
pub fn build(self) -> runtime_io::TestExternalities {
self.set_associated_consts();
let mut storage = system::GenesisConfig::default().build_storage::<Test>().unwrap();
let balance_factor = if self.existential_deposit > 0 {
@@ -341,7 +341,7 @@ impl ExtBuilder {
}.assimilate_storage(&mut storage);
let mut ext = storage.into();
runtime_io::with_externalities(&mut ext, || {
sr_primitives::set_and_run_with_externalities(&mut ext, || {
let validators = Session::validators();
SESSION.with(|x|
*x.borrow_mut() = (validators.clone(), HashSet::new())
+45 -47
View File
@@ -18,16 +18,14 @@
use super::*;
use mock::*;
use runtime_io::with_externalities;
use sr_primitives::{assert_eq_error_rate, traits::OnInitialize};
use sr_primitives::{assert_eq_error_rate, traits::OnInitialize, set_and_run_with_externalities};
use sr_staking_primitives::offence::{OffenceDetails, OnOffenceHandler};
use support::{assert_ok, assert_noop, assert_eq_uvec};
use support::traits::{Currency, ReservableCurrency};
use support::{assert_ok, assert_noop, assert_eq_uvec, traits::{Currency, ReservableCurrency}};
#[test]
fn basic_setup_works() {
// Verifies initial conditions of mock
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
// Account 11 is stashed and locked, and account 10 is the controller
@@ -109,7 +107,7 @@ fn basic_setup_works() {
#[test]
fn change_controller_works() {
with_externalities(&mut ExtBuilder::default().build(),
set_and_run_with_externalities(&mut ExtBuilder::default().build(),
|| {
assert_eq!(Staking::bonded(&11), Some(10));
@@ -136,7 +134,7 @@ fn rewards_should_work() {
// * rewards get recorded per session
// * rewards get paid per Era
// * Check that nominators are also rewarded
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -217,7 +215,7 @@ fn multi_era_reward_should_work() {
// Should check that:
// The value of current_session_reward is set at the end of each era, based on
// slot_stake and session_reward.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -260,7 +258,7 @@ fn staking_should_work() {
// * new validators can be added to the default set
// * new ones will be chosen per era
// * either one can unlock the stash and back-down from being a validator via `chill`ing.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.fair(false) // to give 20 more staked value
.build(),
@@ -322,7 +320,7 @@ fn staking_should_work() {
#[test]
fn less_than_needed_candidates_works() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.minimum_validator_count(1)
.validator_count(4)
.nominate(false)
@@ -349,7 +347,7 @@ fn less_than_needed_candidates_works() {
#[test]
fn no_candidate_emergency_condition() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.minimum_validator_count(10)
.validator_count(15)
.num_validators(4)
@@ -415,7 +413,7 @@ fn nominating_and_rewards_should_work() {
// 10 with stake 400.0 20 with stake 600.0 30 with stake 0
// 4 has load 0.0005555555555555556 and supported
// 10 with stake 600.0 20 with stake 400.0 40 with stake 0.0
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.validator_pool(true)
.build(),
@@ -599,7 +597,7 @@ fn nominators_also_get_slashed() {
// 10 - is the controller of 11
// 11 - is the stash.
// 2 - is the nominator of 20, 10
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
assert_eq!(Staking::validator_count(), 2);
// Set payee to controller
@@ -659,7 +657,7 @@ fn double_staking_should_fail() {
// * an account already bonded as stash cannot be be stashed again.
// * an account already bonded as stash cannot nominate.
// * an account already bonded as controller can nominate.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
let arbitrary_value = 5;
@@ -684,7 +682,7 @@ fn double_staking_should_fail() {
fn double_controlling_should_fail() {
// should test (in the same order):
// * an account already bonded as controller CANNOT be reused as the controller of another account.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
let arbitrary_value = 5;
@@ -703,7 +701,7 @@ fn double_controlling_should_fail() {
#[test]
fn session_and_eras_work() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
assert_eq!(Staking::current_era(), 0);
@@ -747,7 +745,7 @@ fn session_and_eras_work() {
#[test]
fn forcing_new_era_works() {
with_externalities(&mut ExtBuilder::default().build(),|| {
set_and_run_with_externalities(&mut ExtBuilder::default().build(),|| {
// normal flow of session.
assert_eq!(Staking::current_era(), 0);
start_session(0);
@@ -786,7 +784,7 @@ fn forcing_new_era_works() {
#[test]
fn cannot_transfer_staked_balance() {
// Tests that a stash account cannot transfer funds
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
// Confirm account 11 is stashed
assert_eq!(Staking::bonded(&11), Some(10));
// Confirm account 11 has some free balance
@@ -811,7 +809,7 @@ fn cannot_transfer_staked_balance_2() {
// Tests that a stash account cannot transfer funds
// Same test as above but with 20, and more accurate.
// 21 has 2000 free balance but 1000 at stake
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.fair(true)
.build(),
@@ -834,7 +832,7 @@ fn cannot_transfer_staked_balance_2() {
#[test]
fn cannot_reserve_staked_balance() {
// Checks that a bonded account cannot reserve balance from free balance
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
// Confirm account 11 is stashed
assert_eq!(Staking::bonded(&11), Some(10));
// Confirm account 11 has some free balance
@@ -854,7 +852,7 @@ fn cannot_reserve_staked_balance() {
#[test]
fn reward_destination_works() {
// Rewards go to the correct destination as determined in Payee
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
// Check that account 11 is a validator
assert!(Staking::current_elected().contains(&11));
// Check the balance of the validator account
@@ -946,7 +944,7 @@ fn validator_payment_prefs_work() {
// Test that validator preferences are correctly honored
// Note: unstake threshold is being directly tested in slashing tests.
// This test will focus on validator payment.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
// Initial config
@@ -998,7 +996,7 @@ fn bond_extra_works() {
// Tests that extra `free_balance` in the stash can be added to stake
// NOTE: this tests only verifies `StakingLedger` for correct updates
// See `bond_extra_and_withdraw_unbonded_works` for more details and updates on `Exposure`.
with_externalities(&mut ExtBuilder::default().build(),
set_and_run_with_externalities(&mut ExtBuilder::default().build(),
|| {
// Check that account 10 is a validator
assert!(<Validators<Test>>::exists(11));
@@ -1044,7 +1042,7 @@ fn bond_extra_and_withdraw_unbonded_works() {
// * It can add extra funds to the bonded account.
// * it can unbond a portion of its funds from the stash account.
// * Once the unbonding period is done, it can actually take the funds out of the stash.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -1131,7 +1129,7 @@ fn bond_extra_and_withdraw_unbonded_works() {
#[test]
fn too_many_unbond_calls_should_not_work() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
// locked at era 0 until 3
for _ in 0..MAX_UNLOCKING_CHUNKS-1 {
assert_ok!(Staking::unbond(Origin::signed(10), 1));
@@ -1160,7 +1158,7 @@ fn too_many_unbond_calls_should_not_work() {
fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment() {
// Test that slot_stake is determined by the least staked validator
// Test that slot_stake is the maximum punishment that can happen to a validator
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.fair(false)
.build(),
@@ -1213,7 +1211,7 @@ fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment(
fn on_free_balance_zero_stash_removes_validator() {
// Tests that validator storage items are cleaned up when stash is empty
// Tests that storage items are untouched when controller is empty
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.existential_deposit(10)
.build(),
|| {
@@ -1266,7 +1264,7 @@ fn on_free_balance_zero_stash_removes_validator() {
fn on_free_balance_zero_stash_removes_nominator() {
// Tests that nominator storage items are cleaned up when stash is empty
// Tests that storage items are untouched when controller is empty
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.existential_deposit(10)
.build(),
|| {
@@ -1322,7 +1320,7 @@ fn on_free_balance_zero_stash_removes_nominator() {
#[test]
fn switching_roles() {
// Test that it should be possible to switch between roles (nominator, validator, idle) with minimal overhead.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -1391,7 +1389,7 @@ fn switching_roles() {
#[test]
fn wrong_vote_is_null() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.validator_pool(true)
.build(),
@@ -1419,7 +1417,7 @@ fn wrong_vote_is_null() {
fn bond_with_no_staked_value() {
// Behavior when someone bonds with no staked value.
// Particularly when she votes and the candidate is elected.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.validator_count(3)
.existential_deposit(5)
.nominate(false)
@@ -1467,7 +1465,7 @@ fn bond_with_no_staked_value() {
fn bond_with_little_staked_value_bounded_by_slot_stake() {
// Behavior when someone bonds with little staked value.
// Particularly when she votes and the candidate is elected.
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.validator_count(3)
.nominate(false)
.minimum_validator_count(1)
@@ -1517,7 +1515,7 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() {
#[cfg(feature = "equalize")]
#[test]
fn phragmen_linear_worse_case_equalize() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.validator_pool(true)
.fair(true)
@@ -1562,7 +1560,7 @@ fn phragmen_linear_worse_case_equalize() {
#[test]
fn new_era_elects_correct_number_of_validators() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(true)
.validator_pool(true)
.fair(true)
@@ -1583,7 +1581,7 @@ fn new_era_elects_correct_number_of_validators() {
#[test]
fn phragmen_should_not_overflow_validators() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -1609,7 +1607,7 @@ fn phragmen_should_not_overflow_validators() {
#[test]
fn phragmen_should_not_overflow_nominators() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -1634,7 +1632,7 @@ fn phragmen_should_not_overflow_nominators() {
#[test]
fn phragmen_should_not_overflow_ultimate() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
@@ -1656,7 +1654,7 @@ fn phragmen_should_not_overflow_ultimate() {
#[test]
fn reward_validator_slashing_validator_doesnt_overflow() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
let stake = u32::max_value() as u64 * 2;
@@ -1689,7 +1687,7 @@ fn reward_validator_slashing_validator_doesnt_overflow() {
#[test]
fn reward_from_authorship_event_handler_works() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
use authorship::EventHandler;
@@ -1716,7 +1714,7 @@ fn reward_from_authorship_event_handler_works() {
#[test]
fn add_reward_points_fns_works() {
with_externalities(&mut ExtBuilder::default()
set_and_run_with_externalities(&mut ExtBuilder::default()
.build(),
|| {
let validators = <Module<Test>>::current_elected();
@@ -1744,7 +1742,7 @@ fn add_reward_points_fns_works() {
#[test]
fn unbonded_balance_is_not_slashable() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
// total amount staked is slashable.
assert_eq!(Staking::slashable_balance_of(&11), 1000);
@@ -1759,7 +1757,7 @@ fn unbonded_balance_is_not_slashable() {
fn era_is_always_same_length() {
// This ensures that the sessions is always of the same length if there is no forcing no
// session changes.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
start_era(1);
assert_eq!(Staking::current_era_start_session_index(), SessionsPerEra::get());
@@ -1779,7 +1777,7 @@ fn era_is_always_same_length() {
#[test]
fn offence_forces_new_era() {
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
Staking::on_offence(
&[OffenceDetails {
offender: (
@@ -1799,7 +1797,7 @@ fn offence_forces_new_era() {
fn slashing_performed_according_exposure() {
// This test checks that slashing is performed according the exposure (or more precisely,
// historical exposure), not the current balance.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Staking::stakers(&11).own, 1000);
// Handle an offence with a historical exposure.
@@ -1827,7 +1825,7 @@ fn slashing_performed_according_exposure() {
fn reporters_receive_their_slice() {
// This test verifies that the reporters of the offence receive their slice from the slashed
// amount.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
// The reporters' reward is calculated from the total exposure.
#[cfg(feature = "equalize")]
let initial_balance = 1250;
@@ -1857,7 +1855,7 @@ fn reporters_receive_their_slice() {
#[test]
fn invulnerables_are_not_slashed() {
// For invulnerable validators no slashing is performed.
with_externalities(
set_and_run_with_externalities(
&mut ExtBuilder::default().invulnerables(vec![11]).build(),
|| {
#[cfg(feature = "equalize")]
@@ -1894,7 +1892,7 @@ fn invulnerables_are_not_slashed() {
#[test]
fn dont_slash_if_fraction_is_zero() {
// Don't slash if the fraction is zero.
with_externalities(&mut ExtBuilder::default().build(), || {
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
assert_eq!(Balances::free_balance(&11), 1000);
Staking::on_offence(
+11 -12
View File
@@ -236,13 +236,12 @@ pub use serde::{Serialize, Deserialize};
mod tests {
use super::*;
use codec::{Codec, EncodeLike};
use runtime_io::with_externalities;
use primitives::Blake2Hasher;
pub use srml_metadata::{
use sr_primitives::set_and_run_with_externalities;
use srml_metadata::{
DecodeDifferent, StorageEntryMetadata, StorageMetadata, StorageEntryType,
StorageEntryModifier, DefaultByte, DefaultByteGetter, StorageHasher
StorageEntryModifier, DefaultByteGetter, StorageHasher,
};
pub use rstd::marker::PhantomData;
use rstd::marker::PhantomData;
pub trait Trait {
type BlockNumber: Codec + EncodeLike + Default;
@@ -281,7 +280,7 @@ mod tests {
type Origin = u32;
}
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
GenesisConfig::default().build_storage().unwrap().into()
}
@@ -289,7 +288,7 @@ mod tests {
#[test]
fn linked_map_issue_3318() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
OptionLinkedMap::insert(1, 1);
assert_eq!(OptionLinkedMap::get(1), Some(1));
OptionLinkedMap::insert(1, 2);
@@ -299,7 +298,7 @@ mod tests {
#[test]
fn linked_map_swap_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
OptionLinkedMap::insert(0, 0);
OptionLinkedMap::insert(1, 1);
OptionLinkedMap::insert(2, 2);
@@ -328,7 +327,7 @@ mod tests {
#[test]
fn linked_map_basic_insert_remove_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// initialized during genesis
assert_eq!(Map::get(&15u32), 42u64);
@@ -354,7 +353,7 @@ mod tests {
#[test]
fn linked_map_enumeration_and_head_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Map::head(), Some(15));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(15, 42)]);
// insert / remove
@@ -406,7 +405,7 @@ mod tests {
#[test]
fn double_map_basic_insert_remove_remove_prefix_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
type DoubleMap = DataDM;
// initialized during genesis
assert_eq!(DoubleMap::get(&15u32, &16u32), 42u64);
@@ -446,7 +445,7 @@ mod tests {
#[test]
fn double_map_append_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
type DoubleMap = AppendableDM<Test>;
let key1 = 17u32;
@@ -758,8 +758,8 @@ mod test3 {
#[cfg(test)]
#[allow(dead_code)]
mod test_append_and_len {
use crate::storage::{StorageValue};
use runtime_io::{with_externalities, TestExternalities};
use runtime_io::TestExternalities;
use sr_primitives::set_and_run_with_externalities;
use codec::{Encode, Decode};
pub trait Trait {
@@ -801,7 +801,7 @@ mod test_append_and_len {
#[test]
fn default_for_option() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
assert_eq!(OptionVec::get(), None);
assert_eq!(JustVec::get(), vec![]);
});
@@ -809,7 +809,7 @@ mod test_append_and_len {
#[test]
fn append_works() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
let _ = MapVec::append(1, [1, 2, 3].iter());
let _ = MapVec::append(1, [4, 5].iter());
assert_eq!(MapVec::get(1), vec![1, 2, 3, 4, 5]);
@@ -822,7 +822,7 @@ mod test_append_and_len {
#[test]
fn append_works_for_default() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
assert_eq!(JustVecWithDefault::get(), vec![6, 9]);
let _ = JustVecWithDefault::append([1].iter());
assert_eq!(JustVecWithDefault::get(), vec![6, 9, 1]);
@@ -839,7 +839,7 @@ mod test_append_and_len {
#[test]
fn append_or_put_works() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
let _ = MapVec::append_or_insert(1, &[1, 2, 3][..]);
let _ = MapVec::append_or_insert(1, &[4, 5][..]);
assert_eq!(MapVec::get(1), vec![1, 2, 3, 4, 5]);
@@ -856,7 +856,7 @@ mod test_append_and_len {
#[test]
fn len_works() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
JustVec::put(&vec![1, 2, 3, 4]);
OptionVec::put(&vec![1, 2, 3, 4, 5]);
MapVec::insert(1, &vec![1, 2, 3, 4, 5, 6]);
@@ -871,7 +871,7 @@ mod test_append_and_len {
#[test]
fn len_works_for_default() {
with_externalities(&mut TestExternalities::default(), || {
set_and_run_with_externalities(&mut TestExternalities::default(), || {
// vec
assert_eq!(JustVec::get(), vec![]);
assert_eq!(JustVec::decode_len(), Ok(0));
+2
View File
@@ -10,6 +10,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
runtime-io ={ package = "sr-io", path = "../../../core/sr-io", default-features = false }
support = { package = "srml-support", version = "2", path = "../", default-features = false }
inherents = { package = "substrate-inherents", path = "../../../core/inherents", default-features = false }
sr-primitives = { package = "sr-primitives", path = "../../../core/sr-primitives", default-features = false }
primitives = { package = "substrate-primitives", path = "../../../core/primitives", default-features = false }
trybuild = "1.0.14"
pretty_assertions = "0.6.1"
@@ -23,4 +24,5 @@ std = [
"support/std",
"inherents/std",
"primitives/std",
"sr-primitives/std",
]
@@ -15,20 +15,22 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#![recursion_limit="128"]
use runtime_io::with_externalities;
use sr_primitives::{
generic, BuildStorage, traits::{BlakeTwo256, Block as _, Verify},
set_and_run_with_externalities,
};
use support::{
Parameter, traits::Get, parameter_types,
sr_primitives::{generic, BuildStorage, traits::{BlakeTwo256, Block as _, Verify}},
metadata::{
DecodeDifferent, StorageMetadata, StorageEntryModifier, StorageEntryType, DefaultByteGetter,
StorageEntryMetadata, StorageHasher
StorageEntryMetadata, StorageHasher,
},
StorageValue, StorageMap, StorageLinkedMap, StorageDoubleMap,
};
use inherents::{
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
};
use primitives::{H256, sr25519, Blake2Hasher};
use primitives::{H256, sr25519};
mod system;
@@ -275,7 +277,7 @@ pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
GenesisConfig{
module1_Instance1: Some(module1::GenesisConfig {
value: 3,
@@ -329,7 +331,7 @@ fn storage_instance_independance() {
#[test]
fn storage_with_instance_basic_operation() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
type Value = module2::Value<Runtime, module2::Instance1>;
type Map = module2::Map<module2::Instance1>;
type LinkedMap = module2::LinkedMap<module2::Instance1>;
+6 -5
View File
@@ -17,9 +17,10 @@
use criterion::{Criterion, criterion_group, criterion_main, black_box};
use srml_system as system;
use support::{decl_module, decl_event, impl_outer_origin, impl_outer_event};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use sr_primitives::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use primitives::H256;
use sr_primitives::{
set_and_run_with_externalities, Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
};
mod module {
use super::*;
@@ -82,13 +83,13 @@ impl module::Trait for Runtime {
type Event = Event;
}
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into()
}
fn deposit_events(n: usize) {
let mut t = new_test_ext();
with_externalities(&mut t, || {
set_and_run_with_externalities(&mut t, || {
for _ in 0..n {
module::Module::<Runtime>::deposit_event(
module::Event::Complex(vec![1, 2, 3], 2, 3, 899)
+19 -17
View File
@@ -120,7 +120,7 @@ use codec::{Encode, Decode};
use runtime_io::TestExternalities;
#[cfg(any(feature = "std", test))]
use primitives::{ChangesTrieConfiguration, Blake2Hasher};
use primitives::ChangesTrieConfiguration;
pub mod offchain;
@@ -695,7 +695,7 @@ impl<T: Trait> Module<T> {
/// Get the basic externalities for this module, useful for tests.
#[cfg(any(feature = "std", test))]
pub fn externalities() -> TestExternalities<Blake2Hasher> {
pub fn externalities() -> TestExternalities {
TestExternalities::new((map![
<BlockHash<T>>::hashed_key_for(T::BlockNumber::zero()) => [69u8; 32].encode(),
<Number<T>>::hashed_key().to_vec() => T::BlockNumber::one().encode(),
@@ -1090,9 +1090,11 @@ impl<T: Trait> Lookup for ChainContext<T> {
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use primitives::H256;
use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header, DispatchError};
use sr_primitives::{
traits::{BlakeTwo256, IdentityLookup}, testing::Header, DispatchError,
set_and_run_with_externalities,
};
use support::{impl_outer_origin, parameter_types};
impl_outer_origin! {
@@ -1141,7 +1143,7 @@ mod tests {
const CALL: &<Test as Trait>::Call = &();
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
@@ -1162,7 +1164,7 @@ mod tests {
#[test]
fn deposit_event_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
System::initialize(&1, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
System::note_finished_extrinsics();
System::deposit_event(1u16);
@@ -1199,7 +1201,7 @@ mod tests {
#[test]
fn deposit_event_topics() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
const BLOCK_NUMBER: u64 = 1;
System::initialize(&BLOCK_NUMBER, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
@@ -1259,7 +1261,7 @@ mod tests {
#[test]
fn prunes_block_hash_mappings() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// simulate import of 15 blocks
for n in 1..=15 {
System::initialize(
@@ -1292,7 +1294,7 @@ mod tests {
#[test]
fn signed_ext_check_nonce_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
<AccountNonce<Test>>::insert(1, 1);
let info = DispatchInfo::default();
let len = 0_usize;
@@ -1310,7 +1312,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_works_normal_tx() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let normal_limit = normal_weight_limit();
let small = DispatchInfo { weight: 100, ..Default::default() };
let medium = DispatchInfo {
@@ -1337,7 +1339,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_fee_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let free = DispatchInfo { weight: 0, ..Default::default() };
let len = 0_usize;
@@ -1350,7 +1352,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_max_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let max = DispatchInfo { weight: Weight::max_value(), ..Default::default() };
let len = 0_usize;
let normal_limit = normal_weight_limit();
@@ -1364,7 +1366,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_works_operational_tx() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let normal = DispatchInfo { weight: 100, ..Default::default() };
let op = DispatchInfo { weight: 100, class: DispatchClass::Operational };
let len = 0_usize;
@@ -1387,7 +1389,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_priority_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal };
let op = DispatchInfo { weight: 100, class: DispatchClass::Operational };
let len = 0_usize;
@@ -1408,7 +1410,7 @@ mod tests {
#[test]
fn signed_ext_check_weight_block_size_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let normal = DispatchInfo::default();
let normal_limit = normal_weight_limit() as usize;
let reset_check_weight = |tx, s, f| {
@@ -1432,7 +1434,7 @@ mod tests {
#[test]
fn signed_ext_check_era_should_work() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// future
assert_eq!(
CheckEra::<Test>::from(Era::mortal(4, 2)).additional_signed().err().unwrap(),
@@ -1448,7 +1450,7 @@ mod tests {
#[test]
fn signed_ext_check_era_should_change_longevity() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal };
let len = 0_usize;
let ext = (
+8 -5
View File
@@ -322,9 +322,12 @@ mod tests {
use super::*;
use support::{impl_outer_origin, assert_ok, parameter_types};
use runtime_io::{with_externalities, TestExternalities};
use runtime_io::TestExternalities;
use primitives::H256;
use sr_primitives::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sr_primitives::{
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
impl_outer_origin! {
pub enum Origin for Test {}
@@ -369,7 +372,7 @@ mod tests {
#[test]
fn timestamp_works() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
Timestamp::set_timestamp(42);
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
assert_eq!(Timestamp::now(), 69);
@@ -380,7 +383,7 @@ mod tests {
#[should_panic(expected = "Timestamp must be updated only once in the block")]
fn double_timestamp_should_fail() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
Timestamp::set_timestamp(42);
assert_ok!(Timestamp::dispatch(Call::set(69), Origin::NONE));
let _ = Timestamp::dispatch(Call::set(70), Origin::NONE);
@@ -391,7 +394,7 @@ mod tests {
#[should_panic(expected = "Timestamp must increment by at least <MinimumPeriod> between sequential blocks")]
fn block_period_minimum_enforced() {
let t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
with_externalities(&mut TestExternalities::new(t), || {
set_and_run_with_externalities(&mut TestExternalities::new(t), || {
Timestamp::set_timestamp(42);
let _ = Timestamp::dispatch(Call::set(46), Origin::NONE);
});
+19 -21
View File
@@ -355,13 +355,11 @@ impl<T: Trait> OnDilution<BalanceOf<T>> for Module<T> {
mod tests {
use super::*;
use runtime_io::with_externalities;
use support::{assert_noop, assert_ok, impl_outer_origin, parameter_types};
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use sr_primitives::{
traits::{BlakeTwo256, OnFinalize, IdentityLookup},
testing::Header,
assert_eq_error_rate,
traits::{BlakeTwo256, OnFinalize, IdentityLookup}, set_and_run_with_externalities,
testing::Header, assert_eq_error_rate,
};
impl_outer_origin! {
@@ -437,7 +435,7 @@ mod tests {
type Balances = balances::Module<Test>;
type Treasury = Module<Test>;
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test>{
balances: vec![(0, 100), (1, 99), (2, 1)],
@@ -448,7 +446,7 @@ mod tests {
#[test]
fn genesis_config_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Treasury::pot(), 0);
assert_eq!(Treasury::proposal_count(), 0);
});
@@ -456,7 +454,7 @@ mod tests {
#[test]
fn minting_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
// Check that accumulate works when we have Some value in Dummy already.
Treasury::on_dilution(100, 100);
assert_eq!(Treasury::pot(), 100);
@@ -467,7 +465,7 @@ mod tests {
fn minting_works_2() {
let tests = [(1, 10), (1, 20), (40, 130), (2, 66), (2, 67), (2, 100), (2, 101), (2, 134)];
for &(minted, portion) in &tests {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let init_total_issuance = Balances::total_issuance();
Treasury::on_dilution(minted, portion);
@@ -491,7 +489,7 @@ mod tests {
#[test]
fn spend_proposal_takes_min_deposit() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3));
assert_eq!(Balances::free_balance(&0), 99);
assert_eq!(Balances::reserved_balance(&0), 1);
@@ -500,7 +498,7 @@ mod tests {
#[test]
fn spend_proposal_takes_proportional_deposit() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
assert_eq!(Balances::free_balance(&0), 95);
assert_eq!(Balances::reserved_balance(&0), 5);
@@ -509,14 +507,14 @@ mod tests {
#[test]
fn spend_proposal_fails_when_proposer_poor() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Treasury::propose_spend(Origin::signed(2), 100, 3), "Proposer's balance too low");
});
}
#[test]
fn accepted_spend_proposal_ignored_outside_spend_period() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
@@ -530,7 +528,7 @@ mod tests {
#[test]
fn unused_pot_should_diminish() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
let init_total_issuance = Balances::total_issuance();
Treasury::on_dilution(100, 100);
assert_eq!(Balances::total_issuance(), init_total_issuance + 100);
@@ -543,7 +541,7 @@ mod tests {
#[test]
fn rejected_spend_proposal_ignored_on_spend_period() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
@@ -557,7 +555,7 @@ mod tests {
#[test]
fn reject_already_rejected_spend_proposal_fails() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
@@ -568,21 +566,21 @@ mod tests {
#[test]
fn reject_non_existant_spend_proposal_fails() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), "No proposal at that index");
});
}
#[test]
fn accept_non_existant_spend_proposal_fails() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), "No proposal at that index");
});
}
#[test]
fn accept_already_rejected_spend_proposal_fails() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
@@ -593,7 +591,7 @@ mod tests {
#[test]
fn accepted_spend_proposal_enacted_on_spend_period() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_eq!(Treasury::pot(), 100);
@@ -608,7 +606,7 @@ mod tests {
#[test]
fn pot_underflow_should_not_diminish() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 150, 3));
+5 -5
View File
@@ -64,10 +64,10 @@ mod tests {
use super::*;
use support::{assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch};
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use primitives::H256;
use sr_primitives::{
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
set_and_run_with_externalities,
};
impl_outer_origin! {
@@ -139,7 +139,7 @@ mod tests {
type Balances = balances::Module<Test>;
type Utility = Module<Test>;
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn new_test_ext() -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 0)],
@@ -150,7 +150,7 @@ mod tests {
#[test]
fn batch_works() {
with_externalities(&mut new_test_ext(), || {
set_and_run_with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 0);
assert_noop!(Utility::batch(Origin::signed(1), vec![