Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions core/sr-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ runtime_io = { package = "sr-io", path = "../sr-io", default-features = false }
log = { version = "0.4.8", optional = true }
paste = "0.1.6"
rand = { version = "0.7.2", optional = true }
externalities = { package = "substrate-externalities", path = "../externalities", optional = true }
impl-trait-for-tuples = "0.1.2"

[dev-dependencies]
Expand All @@ -38,5 +37,4 @@ std = [
"primitives/std",
"app-crypto/std",
"rand",
"externalities",
]
3 changes: 0 additions & 3 deletions core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ pub use sr_arithmetic::helpers_128bit;
/// Re-export big_uint stiff.
pub use sr_arithmetic::biguint;

#[cfg(feature = "std")]
pub use externalities::set_and_run_with_externalities;

/// An abstraction over justification for a block's validity under a consensus algorithm.
///
/// Essentially a finality proof. The exact formulation will vary between consensus
Expand Down
5 changes: 2 additions & 3 deletions core/sr-primitives/src/offchain/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ impl<'a> HeadersIterator<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::set_and_run_with_externalities;
use runtime_io::TestExternalities;
use substrate_offchain::testing;
use primitives::offchain::OffchainExt;
Expand All @@ -523,7 +522,7 @@ mod tests {
let mut t = TestExternalities::default();
t.register_extension(OffchainExt::new(offchain));

set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
let request: Request = Request::get("http://localhost:1234");
let pending = request
.add_header("X-Auth", "hunter2")
Expand Down Expand Up @@ -564,7 +563,7 @@ mod tests {
let mut t = TestExternalities::default();
t.register_extension(OffchainExt::new(offchain));

set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
let pending = Request::default()
.method(Method::Post)
.url("http://localhost:1234")
Expand Down
7 changes: 7 additions & 0 deletions core/state-machine/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ impl<H: Hasher<Out=H256>, N: ChangesTrieBlockNumber> TestExternalities<H, N> {

self.backend.update(top.chain(children).collect())
}

/// Execute the given closure while `self` is set as externalities.
///
/// Returns the result of the given closure.
pub fn execute_with<R>(&mut self, execute: impl FnOnce() -> R) -> R {
externalities::set_and_run_with_externalities(self, execute)
}
}

impl<H: Hasher<Out=H256>, N: ChangesTrieBlockNumber> std::fmt::Debug for TestExternalities<H, N> {
Expand Down
25 changes: 8 additions & 17 deletions core/test-runtime/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ mod tests {

use runtime_io::TestExternalities;
use substrate_test_runtime_client::{AccountKeyring, Sr25519Keyring};
use sr_primitives::set_and_run_with_externalities;
use crate::{Header, Transfer, WASM_BINARY};
use primitives::{NeverNativeValue, map, traits::CodeExecutor};
use substrate_executor::{NativeExecutor, WasmExecutionMethod, native_executor_instance};
Expand Down Expand Up @@ -371,18 +370,14 @@ mod tests {
extrinsics: vec![],
};

set_and_run_with_externalities(&mut new_test_ext(), || polish_block(&mut b));
new_test_ext().execute_with(|| polish_block(&mut b));

block_executor(b, &mut new_test_ext());
}

#[test]
fn block_import_works_native() {
block_import_works(|b, ext| {
set_and_run_with_externalities(ext, || {
execute_block(b);
});
});
block_import_works(|b, ext| ext.execute_with(|| execute_block(b)));
}

#[test]
Expand Down Expand Up @@ -420,7 +415,7 @@ mod tests {
};

let mut dummy_ext = new_test_ext();
set_and_run_with_externalities(&mut dummy_ext, || polish_block(&mut b1));
dummy_ext.execute_with(|| polish_block(&mut b1));

let mut b2 = Block {
header: Header {
Expand All @@ -446,26 +441,26 @@ mod tests {
],
};

set_and_run_with_externalities(&mut dummy_ext, || polish_block(&mut b2));
dummy_ext.execute_with(|| polish_block(&mut b2));
drop(dummy_ext);

let mut t = new_test_ext();

set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(balance_of(AccountKeyring::Alice.into()), 111);
assert_eq!(balance_of(AccountKeyring::Bob.into()), 0);
});

block_executor(b1, &mut t);

set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(balance_of(AccountKeyring::Alice.into()), 42);
assert_eq!(balance_of(AccountKeyring::Bob.into()), 69);
});

block_executor(b2, &mut t);

set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(balance_of(AccountKeyring::Alice.into()), 0);
assert_eq!(balance_of(AccountKeyring::Bob.into()), 42);
assert_eq!(balance_of(AccountKeyring::Charlie.into()), 69);
Expand All @@ -474,11 +469,7 @@ mod tests {

#[test]
fn block_import_with_transaction_works_native() {
block_import_with_transaction_works(|b, ext| {
set_and_run_with_externalities(ext, || {
execute_block(b);
});
});
block_import_with_transaction_works(|b, ext| ext.execute_with(|| execute_block(b)));
}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions node-template/runtime/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ mod tests {
use primitives::H256;
use support::{impl_outer_origin, assert_ok, parameter_types};
use sr_primitives::{
set_and_run_with_externalities, traits::{BlakeTwo256, IdentityLookup}, testing::Header,
weights::Weight, Perbill,
traits::{BlakeTwo256, IdentityLookup}, testing::Header, weights::Weight, Perbill,
};

impl_outer_origin! {
Expand Down Expand Up @@ -122,7 +121,7 @@ mod tests {

#[test]
fn it_works_for_default_value() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
// Just a dummy test for the dummy funtion `do_something`
// calling the `do_something` function with a value 42
assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));
Expand Down
24 changes: 12 additions & 12 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
).0;
assert!(r.is_ok());

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
});
Expand Down Expand Up @@ -262,7 +262,7 @@ mod tests {
).0;
assert!(r.is_ok());

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
});
Expand Down Expand Up @@ -433,7 +433,7 @@ mod tests {
None,
).0.unwrap();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS);
let events = vec![
Expand Down Expand Up @@ -468,7 +468,7 @@ mod tests {
None,
).0.unwrap();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
// NOTE: fees differ slightly in tests that execute more than one block due to the
// weight update. Hence, using `assert_eq_error_rate`.
assert_eq_error_rate!(
Expand Down Expand Up @@ -540,7 +540,7 @@ mod tests {
None,
).0.unwrap();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS);
});
Expand All @@ -553,7 +553,7 @@ mod tests {
None,
).0.unwrap();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq_error_rate!(
Balances::total_balance(&alice()),
32 * DOLLARS - 2 * transfer_fee(&xt()),
Expand Down Expand Up @@ -715,7 +715,7 @@ mod tests {
None,
).0.unwrap();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
// Verify that the contract constructor worked well and code of TRANSFER contract is actually deployed.
assert_eq!(
&contracts::ContractInfoOf::<Runtime>::get(addr)
Expand Down Expand Up @@ -836,7 +836,7 @@ mod tests {
.expect("Extrinsic could be applied")
.expect("Extrinsic did not fail");

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS);
});
Expand Down Expand Up @@ -895,7 +895,7 @@ mod tests {

let mut prev_multiplier = WeightMultiplier::default();

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(System::next_weight_multiplier(), prev_multiplier);
});

Expand Down Expand Up @@ -947,7 +947,7 @@ mod tests {
).0.unwrap();

// weight multiplier is increased for next block.
sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
let fm = System::next_weight_multiplier();
println!("After a big block: {:?} -> {:?}", prev_multiplier, fm);
assert!(fm > prev_multiplier);
Expand All @@ -964,7 +964,7 @@ mod tests {
).0.unwrap();

// weight multiplier is increased for next block.
sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
let fm = System::next_weight_multiplier();
println!("After a small block: {:?} -> {:?}", prev_multiplier, fm);
assert!(fm < prev_multiplier);
Expand Down Expand Up @@ -1018,7 +1018,7 @@ mod tests {
).0;
assert!(r.is_ok());

sr_primitives::set_and_run_with_externalities(&mut t, || {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&bob()), (10 + 69) * DOLLARS);
// Components deducted from alice's balances:
// - Weight fee
Expand Down
21 changes: 9 additions & 12 deletions srml/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ mod tests {
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,
set_and_run_with_externalities,
};
use sr_primitives::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};

impl_outer_origin! {
pub enum Origin for Test {}
Expand Down Expand Up @@ -297,15 +294,15 @@ mod tests {

#[test]
fn issuing_asset_units_to_issuer_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
});
}

#[test]
fn querying_total_supply_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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));
Expand All @@ -322,7 +319,7 @@ mod tests {

#[test]
fn transferring_amount_above_available_balance_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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));
Expand All @@ -333,7 +330,7 @@ mod tests {

#[test]
fn transferring_amount_less_than_available_balance_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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));
Expand All @@ -347,7 +344,7 @@ mod tests {

#[test]
fn transferring_less_than_one_unit_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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");
Expand All @@ -356,7 +353,7 @@ mod tests {

#[test]
fn transferring_more_units_than_total_supply_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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");
Expand All @@ -365,7 +362,7 @@ mod tests {

#[test]
fn destroying_asset_balance_with_positive_balance_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
assert_ok!(Assets::issue(Origin::signed(1), 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::destroy(Origin::signed(1), 0));
Expand All @@ -374,7 +371,7 @@ mod tests {

#[test]
fn destroying_asset_balance_with_zero_balance_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
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");
Expand Down
3 changes: 1 addition & 2 deletions srml/aura/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

#![cfg(test)]

use sr_primitives::set_and_run_with_externalities;
use crate::mock::{Aura, new_test_ext};

#[test]
fn initial_values() {
set_and_run_with_externalities(&mut new_test_ext(vec![0, 1, 2, 3]), || {
new_test_ext(vec![0, 1, 2, 3]).execute_with(|| {
assert_eq!(Aura::last(), 0u64);
assert_eq!(Aura::authorities().len(), 4);
});
Expand Down
Loading