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
+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 };