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

Commit

Permalink
seal: Add automated weights for contract API calls (#7017)
Browse files Browse the repository at this point in the history
* seal: Add capability to put uninstrumented code (for benchmarks)

Benchmarks should only measure the overhead of the API calls itself.
For that reason we want to run them without instrumentation.

* seal: Cap the the data length for deposited events

Data used in events has storage implications for archive nodes.
Those need to keep the events in storage forever. For that reason
we want to limit the amount of storage that can be used inside events.

* seal: Fix error reporting in the case out of bound sandbox access

* seal: Refactor existing benchmarks

* seal: Convert benchmark file to tabs

* seal: Add benchmarks for functions called by contracts

* seal: Create a default schedule from benchmark generated WeightInfo

* seal: Make use of WeightInfo in extrinsic weight annotations

* seal: Replace the old schedule by the benchmark generated one

* Review: Fix copy paste typo in schedule construction

* Review: Fix stale docs

* Fix whitespace errors

Co-authored-by: Sergei Shulepov <[email protected]>

* Review: Use checked_div in order to be more defensive

* Review: Rename no_charge to already_charged

* Review: Whitelist caller of extrinsics

* Review: Remove trailing whitespace

* Review: Remove confusing "self::" syntax

* Review: Add docs for the benchmark prepration submodule

* Review: Move code generation functions to own module

* Review: Refactor and document benchmark helper functions

* Remove additional empty line

* Added missing comment on caller_funding

* Update frame/contracts/src/benchmarking/code.rs

Co-authored-by: Sergei Shulepov <[email protected]>

* Fix missing sp_std::prelude import in code.rs

* cargo run --release --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark --chain dev --steps 50 --repeat 20 --extrinsic * --execution=wasm --wasm-execution=compiled --output ./bin/node/runtime/src/weights --header ./HEADER --pallet pallet_contracts --heap-pages 4096

* Use weights from the benchmark machine for the substrate node

* Remove prefixes from Schedule members

* Data lengths in the WeightInfo Trait are specified in kilobytes

* Rename ApiWeights to HostFunctionWeights

Co-authored-by: Sergei Shulepov <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2020
1 parent 258558a commit 0aa4ab4
Show file tree
Hide file tree
Showing 20 changed files with 3,460 additions and 920 deletions.
15 changes: 11 additions & 4 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ impl pallet_contracts::Trait for Runtime {
type MaxDepth = pallet_contracts::DefaultMaxDepth;
type MaxValueSize = pallet_contracts::DefaultMaxValueSize;
type WeightPrice = pallet_transaction_payment::Module<Self>;
type WeightInfo = weights::pallet_contracts::WeightInfo<Self>;
}

impl pallet_sudo::Trait for Runtime {
Expand Down Expand Up @@ -929,7 +930,7 @@ construct_runtime!(
FinalityTracker: pallet_finality_tracker::{Module, Call, Inherent},
Grandpa: pallet_grandpa::{Module, Call, Storage, Config, Event, ValidateUnsigned},
Treasury: pallet_treasury::{Module, Call, Storage, Config, Event<T>},
Contracts: pallet_contracts::{Module, Call, Config, Storage, Event<T>},
Contracts: pallet_contracts::{Module, Call, Config<T>, Storage, Event<T>},
Sudo: pallet_sudo::{Module, Call, Config<T>, Storage, Event<T>},
ImOnline: pallet_im_online::{Module, Call, Storage, Event<T>, ValidateUnsigned, Config<T>},
AuthorityDiscovery: pallet_authority_discovery::{Module, Call, Config},
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
pub mod frame_system;
pub mod pallet_balances;
pub mod pallet_collective;
pub mod pallet_contracts;
pub mod pallet_democracy;
pub mod pallet_elections_phragmen;
pub mod pallet_identity;
Expand Down
294 changes: 294 additions & 0 deletions bin/node/runtime/src/weights/pallet_contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
// This file is part of Substrate.

// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Weights for pallet_contracts
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
//! DATE: 2020-10-06, STEPS: [50], REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::Weight};
use sp_std::marker::PhantomData;

pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Trait> pallet_contracts::WeightInfo for WeightInfo<T> {
fn update_schedule() -> Weight {
(33_207_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn put_code(n: u32, ) -> Weight {
(0 as Weight)
.saturating_add((144_833_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn instantiate(n: u32, ) -> Weight {
(223_974_000 as Weight)
.saturating_add((1_007_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
fn call() -> Weight {
(210_638_000 as Weight)
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn claim_surcharge() -> Weight {
(508_079_000 as Weight)
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn seal_caller(r: u32, ) -> Weight {
(143_336_000 as Weight)
.saturating_add((397_788_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_address(r: u32, ) -> Weight {
(147_296_000 as Weight)
.saturating_add((396_962_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_gas_left(r: u32, ) -> Weight {
(141_677_000 as Weight)
.saturating_add((393_308_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_balance(r: u32, ) -> Weight {
(157_556_000 as Weight)
.saturating_add((879_861_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
}
fn seal_value_transferred(r: u32, ) -> Weight {
(148_867_000 as Weight)
.saturating_add((391_678_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_minimum_balance(r: u32, ) -> Weight {
(147_252_000 as Weight)
.saturating_add((393_977_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_tombstone_deposit(r: u32, ) -> Weight {
(144_208_000 as Weight)
.saturating_add((394_625_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_rent_allowance(r: u32, ) -> Weight {
(135_320_000 as Weight)
.saturating_add((925_541_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_block_number(r: u32, ) -> Weight {
(145_849_000 as Weight)
.saturating_add((390_065_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_now(r: u32, ) -> Weight {
(146_363_000 as Weight)
.saturating_add((391_772_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_weight_to_fee(r: u32, ) -> Weight {
(129_872_000 as Weight)
.saturating_add((670_744_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
}
fn seal_gas(r: u32, ) -> Weight {
(130_985_000 as Weight)
.saturating_add((198_427_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_input(r: u32, ) -> Weight {
(138_647_000 as Weight)
.saturating_add((8_363_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_input_per_kb(n: u32, ) -> Weight {
(149_418_000 as Weight)
.saturating_add((272_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_return(r: u32, ) -> Weight {
(129_116_000 as Weight)
.saturating_add((5_745_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_return_per_kb(n: u32, ) -> Weight {
(139_601_000 as Weight)
.saturating_add((680_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_terminate(r: u32, ) -> Weight {
(138_548_000 as Weight)
.saturating_add((355_473_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(r as Weight)))
}
fn seal_restore_to(r: u32, ) -> Weight {
(239_880_000 as Weight)
.saturating_add((138_305_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().reads((3 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(r as Weight)))
}
fn seal_restore_to_per_delta(d: u32, ) -> Weight {
(40_572_000 as Weight)
.saturating_add((3_748_632_000 as Weight).saturating_mul(d as Weight))
.saturating_add(T::DbWeight::get().reads(7 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(d as Weight)))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
.saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(d as Weight)))
}
fn seal_random(r: u32, ) -> Weight {
(148_156_000 as Weight)
.saturating_add((1_036_452_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
}
fn seal_deposit_event(r: u32, ) -> Weight {
(176_039_000 as Weight)
.saturating_add((1_497_705_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_deposit_event_per_topic_and_kb(t: u32, n: u32, ) -> Weight {
(1_923_547_000 as Weight)
.saturating_add((783_354_000 as Weight).saturating_mul(t as Weight))
.saturating_add((240_600_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(t as Weight)))
.saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(t as Weight)))
}
fn seal_set_rent_allowance(r: u32, ) -> Weight {
(151_095_000 as Weight)
.saturating_add((1_104_696_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
fn seal_set_storage(r: u32, ) -> Weight {
(0 as Weight)
.saturating_add((14_975_467_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(r as Weight)))
}
fn seal_set_storage_per_kb(n: u32, ) -> Weight {
(2_465_724_000 as Weight)
.saturating_add((203_125_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
fn seal_clear_storage(r: u32, ) -> Weight {
(0 as Weight)
.saturating_add((5_254_595_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(r as Weight)))
}
fn seal_get_storage(r: u32, ) -> Weight {
(60_303_000 as Weight)
.saturating_add((1_135_486_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight)))
}
fn seal_get_storage_per_kb(n: u32, ) -> Weight {
(931_900_000 as Weight)
.saturating_add((144_572_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
}
fn seal_transfer(r: u32, ) -> Weight {
(50_722_000 as Weight)
.saturating_add((6_701_164_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
.saturating_add(T::DbWeight::get().writes((100 as Weight).saturating_mul(r as Weight)))
}
fn seal_call(r: u32, ) -> Weight {
(0 as Weight)
.saturating_add((10_589_747_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().reads((100 as Weight).saturating_mul(r as Weight)))
}
fn seal_call_per_transfer_input_output_kb(t: u32, i: u32, o: u32, ) -> Weight {
(11_223_388_000 as Weight)
.saturating_add((4_965_182_000 as Weight).saturating_mul(t as Weight))
.saturating_add((50_603_000 as Weight).saturating_mul(i as Weight))
.saturating_add((72_972_000 as Weight).saturating_mul(o as Weight))
.saturating_add(T::DbWeight::get().reads(105 as Weight))
.saturating_add(T::DbWeight::get().reads((101 as Weight).saturating_mul(t as Weight)))
.saturating_add(T::DbWeight::get().writes((101 as Weight).saturating_mul(t as Weight)))
}
fn seal_instantiate(r: u32, ) -> Weight {
(0 as Weight)
.saturating_add((22_933_938_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().reads((300 as Weight).saturating_mul(r as Weight)))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
.saturating_add(T::DbWeight::get().writes((200 as Weight).saturating_mul(r as Weight)))
}
fn seal_instantiate_per_input_output_kb(i: u32, o: u32, ) -> Weight {
(20_986_307_000 as Weight)
.saturating_add((152_611_000 as Weight).saturating_mul(i as Weight))
.saturating_add((73_457_000 as Weight).saturating_mul(o as Weight))
.saturating_add(T::DbWeight::get().reads(207 as Weight))
.saturating_add(T::DbWeight::get().writes(202 as Weight))
}
fn seal_hash_sha2_256(r: u32, ) -> Weight {
(145_988_000 as Weight)
.saturating_add((343_540_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_sha2_256_per_kb(n: u32, ) -> Weight {
(719_758_000 as Weight)
.saturating_add((420_306_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_keccak_256(r: u32, ) -> Weight {
(116_261_000 as Weight)
.saturating_add((360_601_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_keccak_256_per_kb(n: u32, ) -> Weight {
(583_726_000 as Weight)
.saturating_add((333_091_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_blake2_256(r: u32, ) -> Weight {
(144_609_000 as Weight)
.saturating_add((332_388_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_blake2_256_per_kb(n: u32, ) -> Weight {
(612_987_000 as Weight)
.saturating_add((150_030_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_blake2_128(r: u32, ) -> Weight {
(142_085_000 as Weight)
.saturating_add((329_426_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
fn seal_hash_blake2_128_per_kb(n: u32, ) -> Weight {
(632_517_000 as Weight)
.saturating_add((149_974_000 as Weight).saturating_mul(n as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))
}
}
Loading

0 comments on commit 0aa4ab4

Please sign in to comment.