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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion frame/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ codec = { package = "parity-scale-codec", version = "2.2.0", default-features =
"max-encoded-len",
] }
log = { version = "0.4", default-features = false }
pwasm-utils = { version = "0.18", default-features = false }
pwasm-utils = { version = "0.18.2", default-features = false }
serde = { version = "1", optional = true, features = ["derive"] }
smallvec = { version = "1", default-features = false, features = [
"const_generics",
Expand Down
30 changes: 14 additions & 16 deletions frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use sp_std::{convert::TryInto, default::Default, vec, vec::Vec};
const API_BENCHMARK_BATCHES: u32 = 20;

/// How many batches we do per Instruction benchmark.
const INSTR_BENCHMARK_BATCHES: u32 = 1;
const INSTR_BENCHMARK_BATCHES: u32 = 50;

/// An instantiated and deployed contract.
struct Contract<T: Config> {
Expand Down Expand Up @@ -444,11 +444,8 @@ benchmarks! {

}: call(origin, instance.addr, 0u32.into(), Weight::max_value(), vec![])

// We cannot call seal_input multiple times. Therefore our weight determination is not
// as precise as with other APIs. Because this function can only be called once per
// contract it cannot be used for Dos.
seal_input {
let r in 0 .. 1;
let r in 0 .. API_BENCHMARK_BATCHES;
let code = WasmModule::<T>::from(ModuleDefinition {
memory: Some(ImportedMemory::max::<T>()),
imported_functions: vec![ImportedFunction {
Expand All @@ -463,7 +460,7 @@ benchmarks! {
value: 0u32.to_le_bytes().to_vec(),
},
],
call_body: Some(body::repeated(r, &[
call_body: Some(body::repeated(r * API_BENCHMARK_BATCH_SIZE, &[
Instruction::I32Const(4), // ptr where to store output
Instruction::I32Const(0), // ptr to length
Instruction::Call(0),
Expand Down Expand Up @@ -492,11 +489,10 @@ benchmarks! {
value: buffer_size.to_le_bytes().to_vec(),
},
],
call_body: Some(body::plain(vec![
call_body: Some(body::repeated(API_BENCHMARK_BATCH_SIZE, &[
Instruction::I32Const(4), // ptr where to store output
Instruction::I32Const(0), // ptr to length
Instruction::Call(0),
Instruction::End,
])),
.. Default::default()
});
Expand All @@ -505,7 +501,9 @@ benchmarks! {
let origin = RawOrigin::Signed(instance.caller.clone());
}: call(origin, instance.addr, 0u32.into(), Weight::max_value(), data)

// The same argument as for `seal_input` is true here.
// We cannot call `seal_return` multiple times. Therefore our weight determination is not
// as precise as with other APIs. Because this function can only be called once per
// contract it cannot be used as an attack vector.
seal_return {
let r in 0 .. 1;
let code = WasmModule::<T>::from(ModuleDefinition {
Expand Down Expand Up @@ -551,7 +549,7 @@ benchmarks! {
let origin = RawOrigin::Signed(instance.caller.clone());
}: call(origin, instance.addr, 0u32.into(), Weight::max_value(), vec![])

// The same argument as for `seal_input` is true here.
// The same argument as for `seal_return` is true here.
seal_terminate {
let r in 0 .. 1;
let beneficiary = account::<T::AccountId>("beneficiary", 0, 0);
Expand Down Expand Up @@ -1509,6 +1507,7 @@ benchmarks! {
}

// w_br = w_bench - 2 * w_param
// Block instructions are not counted.
instr_br {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
Expand All @@ -1533,17 +1532,16 @@ benchmarks! {
sbox.invoke();
}

// w_br_if = w_bench - 5 * w_param
// The two additional pushes + drop are only executed 50% of the time.
// Making it: 3 * w_param + (50% * 4 * w_param)
// w_br_if = w_bench - 3 * w_param
// Block instructions are not counted.
instr_br_if {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
call_body: Some(body::repeated_dyn(r * INSTR_BENCHMARK_BATCH_SIZE, vec![
Regular(Instruction::Block(BlockType::NoResult)),
Regular(Instruction::Block(BlockType::NoResult)),
Regular(Instruction::Block(BlockType::NoResult)),
RandomI32(0, 2),
Regular(Instruction::I32Const(1)),
Regular(Instruction::BrIf(1)),
RandomI64Repeated(1),
Regular(Instruction::Drop),
Expand All @@ -1562,11 +1560,11 @@ benchmarks! {
}

// w_br_table = w_bench - 3 * w_param
// 1 * w_param + 0.5 * 2 * w_param + 0.25 * 4 * w_param
// Block instructions are not counted.
instr_br_table {
let r in 0 .. INSTR_BENCHMARK_BATCHES;
let table = Box::new(BrTableData {
table: Box::new([0, 1, 2]),
table: Box::new([1, 1, 1]),
default: 1,
});
let mut sbox = Sandbox::from(&WasmModule::<T>::from(ModuleDefinition {
Expand Down
8 changes: 4 additions & 4 deletions frame/contracts/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const API_BENCHMARK_BATCH_SIZE: u32 = 100;

/// How many instructions are executed in a single batch. The reasoning is the same
/// as for `API_BENCHMARK_BATCH_SIZE`.
pub const INSTR_BENCHMARK_BATCH_SIZE: u32 = 1_000;
pub const INSTR_BENCHMARK_BATCH_SIZE: u32 = 100;

/// Definition of the cost schedule and other parameterizations for the wasm vm.
///
Expand Down Expand Up @@ -495,7 +495,7 @@ impl<T: Config> Default for InstructionWeights<T> {
select: cost_instr!(instr_select, 4),
r#if: cost_instr!(instr_if, 3),
br: cost_instr!(instr_br, 2),
br_if: cost_instr!(instr_br_if, 5),
br_if: cost_instr!(instr_br_if, 3),
br_table: cost_instr!(instr_br_table, 3),
br_table_per_entry: cost_instr!(instr_br_table_per_entry, 0),
call: cost_instr!(instr_call, 2),
Expand Down Expand Up @@ -559,8 +559,8 @@ impl<T: Config> Default for HostFnWeights<T> {
now: cost_batched!(seal_now),
weight_to_fee: cost_batched!(seal_weight_to_fee),
gas: cost_batched!(seal_gas),
input: cost!(seal_input),
input_per_byte: cost_byte!(seal_input_per_kb),
input: cost_batched!(seal_input),
input_per_byte: cost_byte_batched!(seal_input_per_kb),
r#return: cost!(seal_return),
return_per_byte: cost_byte!(seal_return_per_kb),
terminate: cost!(seal_terminate),
Expand Down
Loading