Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

contracts: Reduce the API surface #8359

Merged
9 commits merged into from
Mar 24, 2021
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
7 changes: 3 additions & 4 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,9 @@ pub fn testnet_genesis(
phantom: Default::default(),
},
pallet_contracts: ContractsConfig {
current_schedule: pallet_contracts::Schedule {
enable_println, // this should only be enabled on development chains
..Default::default()
},
// println should only be enabled on development chains
current_schedule: pallet_contracts::Schedule::default()
.enable_println(enable_println),
},
pallet_sudo: SudoConfig {
key: root_key,
Expand Down
11 changes: 4 additions & 7 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,10 @@ fn deploying_wasm_contract_should_work() {
).0.unwrap();

t.execute_with(|| {
// Verify that the contract constructor worked well and code of TRANSFER contract is actually deployed.
assert_eq!(
&pallet_contracts::ContractInfoOf::<Runtime>::get(addr)
.and_then(|c| c.get_alive())
.unwrap()
.code_hash,
&transfer_ch
// Verify that the contract does exist by querying some of its storage items
// It does not matter that the storage item itself does not exist.
assert!(
&pallet_contracts::Pallet::<Runtime>::get_storage(addr, Default::default()).is_ok()
);
});
}
Expand Down
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,11 +744,11 @@ impl pallet_tips::Config for Runtime {
}

parameter_types! {
pub const TombstoneDeposit: Balance = deposit(
pub TombstoneDeposit: Balance = deposit(
1,
sp_std::mem::size_of::<pallet_contracts::ContractInfo<Runtime>>() as u32
<pallet_contracts::Pallet<Runtime>>::contract_info_size(),
);
pub const DepositPerContract: Balance = TombstoneDeposit::get();
pub DepositPerContract: Balance = TombstoneDeposit::get();
pub const DepositPerStorageByte: Balance = deposit(0, 1);
pub const DepositPerStorageItem: Balance = deposit(1, 0);
pub RentFraction: Perbill = Perbill::from_rational(1u32, 30 * DAYS);
Expand Down
3 changes: 3 additions & 0 deletions frame/contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ In other words: Upgrading this pallet will not break pre-existing contracts.

### Added

- Make storage and fields of `Schedule` private to the crate.
[1](https://github.com/paritytech/substrate/pull/8359)

- Add new version of `seal_random` which exposes additional information.
[1](https://github.com/paritytech/substrate/pull/8329)

Expand Down
8 changes: 3 additions & 5 deletions frame/contracts/src/benchmarking/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
//! we define this simple definition of a contract that can be passed to `create_code` that
//! compiles it down into a `WasmModule` that can be used as a contract's code.

use crate::Config;
use crate::Pallet as Contracts;

use crate::{Config, CurrentSchedule};
use parity_wasm::elements::{
Instruction, Instructions, FuncBody, ValueType, BlockType, Section, CustomSection,
};
Expand Down Expand Up @@ -225,7 +223,7 @@ where
if def.inject_stack_metering {
code = inject_limiter(
code,
Contracts::<T>::current_schedule().limits.stack_height
<CurrentSchedule<T>>::get().limits.stack_height
)
.unwrap();
}
Expand Down Expand Up @@ -505,5 +503,5 @@ where
T: Config,
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
Contracts::<T>::current_schedule().limits.memory_pages
<CurrentSchedule<T>>::get().limits.memory_pages
}
25 changes: 13 additions & 12 deletions frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use self::{
},
sandbox::Sandbox,
};
use codec::Encode;
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
use frame_system::{Pallet as System, RawOrigin};
use parity_wasm::elements::{Instruction, ValueType, BlockType};
Expand Down Expand Up @@ -313,7 +314,7 @@ benchmarks! {
let WasmModule { code, hash, .. } = WasmModule::<T>::sized(c * 1024);
Contracts::<T>::store_code_raw(code)?;
let mut module = PrefabWasmModule::from_storage_noinstr(hash)?;
let schedule = Contracts::<T>::current_schedule();
let schedule = <CurrentSchedule<T>>::get();
}: {
Contracts::<T>::reinstrument_module(&mut module, &schedule)?;
}
Expand Down Expand Up @@ -936,7 +937,7 @@ benchmarks! {
seal_random {
let r in 0 .. API_BENCHMARK_BATCHES;
let pages = code::max_pages::<T>();
let subject_len = Contracts::<T>::current_schedule().limits.subject_len;
let subject_len = <CurrentSchedule<T>>::get().limits.subject_len;
assert!(subject_len < 1024);
let code = WasmModule::<T>::from(ModuleDefinition {
memory: Some(ImportedMemory::max::<T>()),
Expand Down Expand Up @@ -992,7 +993,7 @@ benchmarks! {
// `t`: Number of topics
// `n`: Size of event payload in kb
seal_deposit_event_per_topic_and_kb {
let t in 0 .. Contracts::<T>::current_schedule().limits.event_topics;
let t in 0 .. <CurrentSchedule<T>>::get().limits.event_topics;
let n in 0 .. T::MaxValueSize::get() / 1024;
let mut topics = (0..API_BENCHMARK_BATCH_SIZE)
.map(|n| (n * t..n * t + t).map(|i| T::Hashing::hash_of(&i)).collect::<Vec<_>>().encode())
Expand Down Expand Up @@ -1922,7 +1923,7 @@ benchmarks! {

// w_br_table_per_entry = w_bench
instr_br_table_per_entry {
let e in 1 .. Contracts::<T>::current_schedule().limits.br_table_size;
let e in 1 .. <CurrentSchedule<T>>::get().limits.br_table_size;
let entry: Vec<u32> = [0, 1].iter()
.cloned()
.cycle()
Expand Down Expand Up @@ -1978,7 +1979,7 @@ benchmarks! {
// w_call_indrect = w_bench - 3 * w_param
instr_call_indirect {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let num_elements = Contracts::<T>::current_schedule().limits.table_size;
let num_elements = <CurrentSchedule<T>>::get().limits.table_size;
use self::code::TableSegment;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
// We need to make use of the stack here in order to trigger stack height
Expand Down Expand Up @@ -2008,8 +2009,8 @@ benchmarks! {
// linearly depend on the amount of parameters to this function.
// Please note that this is not necessary with a direct call.
instr_call_indirect_per_param {
let p in 0 .. Contracts::<T>::current_schedule().limits.parameters;
let num_elements = Contracts::<T>::current_schedule().limits.table_size;
let p in 0 .. <CurrentSchedule<T>>::get().limits.parameters;
let num_elements = <CurrentSchedule<T>>::get().limits.table_size;
use self::code::TableSegment;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
// We need to make use of the stack here in order to trigger stack height
Expand Down Expand Up @@ -2039,7 +2040,7 @@ benchmarks! {
// w_local_get = w_bench - 1 * w_param
instr_local_get {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let max_locals = Contracts::<T>::current_schedule().limits.stack_height;
let max_locals = <CurrentSchedule<T>>::get().limits.stack_height;
let mut call_body = body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
RandomGetLocal(0, max_locals),
Regular(Instruction::Drop),
Expand All @@ -2056,7 +2057,7 @@ benchmarks! {
// w_local_set = w_bench - 1 * w_param
instr_local_set {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let max_locals = Contracts::<T>::current_schedule().limits.stack_height;
let max_locals = <CurrentSchedule<T>>::get().limits.stack_height;
let mut call_body = body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
RandomI64Repeated(1),
RandomSetLocal(0, max_locals),
Expand All @@ -2073,7 +2074,7 @@ benchmarks! {
// w_local_tee = w_bench - 2 * w_param
instr_local_tee {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let max_locals = Contracts::<T>::current_schedule().limits.stack_height;
let max_locals = <CurrentSchedule<T>>::get().limits.stack_height;
let mut call_body = body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
RandomI64Repeated(1),
RandomTeeLocal(0, max_locals),
Expand All @@ -2091,7 +2092,7 @@ benchmarks! {
// w_global_get = w_bench - 1 * w_param
instr_global_get {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let max_globals = Contracts::<T>::current_schedule().limits.globals;
let max_globals = <CurrentSchedule<T>>::get().limits.globals;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
call_body: Some(body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
RandomGetGlobal(0, max_globals),
Expand All @@ -2107,7 +2108,7 @@ benchmarks! {
// w_global_set = w_bench - 1 * w_param
instr_global_set {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let max_globals = Contracts::<T>::current_schedule().limits.globals;
let max_globals = <CurrentSchedule<T>>::get().limits.globals;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
call_body: Some(body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
RandomI64Repeated(1),
Expand Down
12 changes: 10 additions & 2 deletions frame/contracts/src/chain_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
//! induces. In order to be able to charge the correct weight for the functions defined
//! by a chain extension benchmarks must be written, too. In the near future this crate
//! will provide the means for easier creation of those specialized benchmarks.
//!
//! # Example
//!
//! The ink! repository maintains an
//! [end-to-end example](https://github.com/paritytech/ink/tree/master/examples/rand-extension)
//! on how to use a chain extension in order to provide new features to ink! contracts.

use crate::{
Error,
Expand Down Expand Up @@ -141,8 +147,8 @@ pub enum RetVal {

/// Grants the chain extension access to its parameters and execution environment.
///
/// It uses the typestate pattern to enforce the correct usage of the parameters passed
/// to the chain extension.
/// It uses [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html)
/// to enforce the correct usage of the parameters passed to the chain extension.
pub struct Environment<'a, 'b, E: Ext, S: state::State> {
/// The actual data of this type.
inner: Inner<'a, 'b, E>,
Expand Down Expand Up @@ -376,6 +382,8 @@ mod state {
pub trait BufIn: State {}
pub trait BufOut: State {}

/// The initial state of an [`Environment`](`super::Environment`).
/// See [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html).
pub enum Init {}
pub enum OnlyIn {}
pub enum PrimInBufOut {}
Expand Down
38 changes: 19 additions & 19 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ mod tests {
test_utils::{place_contract, set_balance, get_balance},
},
exec::ExportedFunction::*,
Error, Weight,
Error, Weight, CurrentSchedule,
};
use sp_runtime::DispatchError;
use assert_matches::assert_matches;
Expand Down Expand Up @@ -1139,7 +1139,7 @@ mod tests {
});

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
place_contract(&BOB, exec_ch);

Expand Down Expand Up @@ -1189,7 +1189,7 @@ mod tests {
);

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(origin.clone(), &schedule);
place_contract(&BOB, return_ch);
set_balance(&origin, 100);
Expand Down Expand Up @@ -1249,7 +1249,7 @@ mod tests {
);

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(origin, &schedule);
place_contract(&BOB, return_ch);

Expand Down Expand Up @@ -1278,7 +1278,7 @@ mod tests {
);

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(origin, &schedule);
place_contract(&BOB, return_ch);

Expand All @@ -1304,7 +1304,7 @@ mod tests {

// This one tests passing the input data into a contract via call.
ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
place_contract(&BOB, input_data_ch);

Expand All @@ -1327,7 +1327,7 @@ mod tests {

// This one tests passing the input data into a contract via instantiate.
ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let subsistence = Contracts::<Test>::subsistence_threshold();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
Expand Down Expand Up @@ -1380,7 +1380,7 @@ mod tests {
});

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
set_balance(&BOB, 1);
place_contract(&BOB, recurse_ch);
Expand Down Expand Up @@ -1428,7 +1428,7 @@ mod tests {
});

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(origin.clone(), &schedule);
place_contract(&dest, bob_ch);
place_contract(&CHARLIE, charlie_ch);
Expand Down Expand Up @@ -1466,7 +1466,7 @@ mod tests {
});

ExtBuilder::default().build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
place_contract(&BOB, bob_ch);
place_contract(&CHARLIE, charlie_ch);
Expand All @@ -1487,7 +1487,7 @@ mod tests {
let dummy_ch = MockLoader::insert(Constructor, |_, _| exec_success());

ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
let executable = MockExecutable::from_storage(
Expand Down Expand Up @@ -1515,7 +1515,7 @@ mod tests {
);

ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
let executable = MockExecutable::from_storage(
Expand Down Expand Up @@ -1551,7 +1551,7 @@ mod tests {
);

ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
let executable = MockExecutable::from_storage(
Expand Down Expand Up @@ -1599,7 +1599,7 @@ mod tests {
});

ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
set_balance(&ALICE, Contracts::<Test>::subsistence_threshold() * 100);
place_contract(&BOB, instantiator_ch);
Expand Down Expand Up @@ -1648,7 +1648,7 @@ mod tests {
});

ExtBuilder::default().existential_deposit(15).build().execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
set_balance(&ALICE, 1000);
set_balance(&BOB, 100);
Expand Down Expand Up @@ -1676,7 +1676,7 @@ mod tests {
.existential_deposit(15)
.build()
.execute_with(|| {
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
let executable = MockExecutable::from_storage(
Expand Down Expand Up @@ -1715,7 +1715,7 @@ mod tests {

ExtBuilder::default().build().execute_with(|| {
let subsistence = Contracts::<Test>::subsistence_threshold();
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
let executable = MockExecutable::from_storage(
Expand Down Expand Up @@ -1747,7 +1747,7 @@ mod tests {

ExtBuilder::default().build().execute_with(|| {
let subsistence = Contracts::<Test>::subsistence_threshold();
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
set_balance(&ALICE, subsistence * 10);
Expand Down Expand Up @@ -1795,7 +1795,7 @@ mod tests {

ExtBuilder::default().build().execute_with(|| {
let subsistence = Contracts::<Test>::subsistence_threshold();
let schedule = Contracts::current_schedule();
let schedule = <CurrentSchedule<Test>>::get();
let mut ctx = MockContext::top_level(ALICE, &schedule);
let mut gas_meter = GasMeter::<Test>::new(GAS_LIMIT);
set_balance(&ALICE, subsistence * 100);
Expand Down
Loading