mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 02:51:08 +00:00
seal: Add automated weights for contract API calls (#7017)
* 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 <sergei@parity.io> * 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 <sergei@parity.io> * 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 <sergei@parity.io> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
committed by
GitHub
parent
e3682fa2f4
commit
443725f0f6
@@ -36,6 +36,8 @@ use self::runtime::{to_execution_result, Runtime};
|
||||
use self::code_cache::load as load_code;
|
||||
|
||||
pub use self::code_cache::save as save_code;
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
pub use self::code_cache::save_raw as save_code_raw;
|
||||
pub use self::runtime::ReturnCode;
|
||||
|
||||
/// A prepared wasm module ready for execution.
|
||||
@@ -64,17 +66,17 @@ pub struct WasmExecutable {
|
||||
}
|
||||
|
||||
/// Loader which fetches `WasmExecutable` from the code cache.
|
||||
pub struct WasmLoader<'a> {
|
||||
schedule: &'a Schedule,
|
||||
pub struct WasmLoader<'a, T: Trait> {
|
||||
schedule: &'a Schedule<T>,
|
||||
}
|
||||
|
||||
impl<'a> WasmLoader<'a> {
|
||||
pub fn new(schedule: &'a Schedule) -> Self {
|
||||
impl<'a, T: Trait> WasmLoader<'a, T> {
|
||||
pub fn new(schedule: &'a Schedule<T>) -> Self {
|
||||
WasmLoader { schedule }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Trait> crate::exec::Loader<T> for WasmLoader<'a> {
|
||||
impl<'a, T: Trait> crate::exec::Loader<T> for WasmLoader<'a, T> {
|
||||
type Executable = WasmExecutable;
|
||||
|
||||
fn load_init(&self, code_hash: &CodeHash<T>) -> Result<WasmExecutable, &'static str> {
|
||||
@@ -94,17 +96,17 @@ impl<'a, T: Trait> crate::exec::Loader<T> for WasmLoader<'a> {
|
||||
}
|
||||
|
||||
/// Implementation of `Vm` that takes `WasmExecutable` and executes it.
|
||||
pub struct WasmVm<'a> {
|
||||
schedule: &'a Schedule,
|
||||
pub struct WasmVm<'a, T: Trait> {
|
||||
schedule: &'a Schedule<T>,
|
||||
}
|
||||
|
||||
impl<'a> WasmVm<'a> {
|
||||
pub fn new(schedule: &'a Schedule) -> Self {
|
||||
impl<'a, T: Trait> WasmVm<'a, T> {
|
||||
pub fn new(schedule: &'a Schedule<T>) -> Self {
|
||||
WasmVm { schedule }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Trait> crate::exec::Vm<T> for WasmVm<'a> {
|
||||
impl<'a, T: Trait> crate::exec::Vm<T> for WasmVm<'a, T> {
|
||||
type Executable = WasmExecutable;
|
||||
|
||||
fn execute<E: Ext<T = T>>(
|
||||
@@ -186,7 +188,6 @@ mod tests {
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct TerminationEntry {
|
||||
beneficiary: u64,
|
||||
gas_left: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
@@ -194,7 +195,6 @@ mod tests {
|
||||
to: u64,
|
||||
value: u64,
|
||||
data: Vec<u8>,
|
||||
gas_left: u64,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -247,13 +247,11 @@ mod tests {
|
||||
&mut self,
|
||||
to: &u64,
|
||||
value: u64,
|
||||
gas_meter: &mut GasMeter<Test>,
|
||||
) -> Result<(), DispatchError> {
|
||||
self.transfers.push(TransferEntry {
|
||||
to: *to,
|
||||
value,
|
||||
data: Vec::new(),
|
||||
gas_left: gas_meter.gas_left(),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -261,14 +259,13 @@ mod tests {
|
||||
&mut self,
|
||||
to: &u64,
|
||||
value: u64,
|
||||
gas_meter: &mut GasMeter<Test>,
|
||||
_gas_meter: &mut GasMeter<Test>,
|
||||
data: Vec<u8>,
|
||||
) -> ExecResult {
|
||||
self.transfers.push(TransferEntry {
|
||||
to: *to,
|
||||
value,
|
||||
data: data,
|
||||
gas_left: gas_meter.gas_left(),
|
||||
});
|
||||
// Assume for now that it was just a plain transfer.
|
||||
// TODO: Add tests for different call outcomes.
|
||||
@@ -277,11 +274,9 @@ mod tests {
|
||||
fn terminate(
|
||||
&mut self,
|
||||
beneficiary: &u64,
|
||||
gas_meter: &mut GasMeter<Test>,
|
||||
) -> Result<(), DispatchError> {
|
||||
self.terminations.push(TerminationEntry {
|
||||
beneficiary: *beneficiary,
|
||||
gas_left: gas_meter.gas_left(),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -372,16 +367,14 @@ mod tests {
|
||||
&mut self,
|
||||
to: &u64,
|
||||
value: u64,
|
||||
gas_meter: &mut GasMeter<Test>,
|
||||
) -> Result<(), DispatchError> {
|
||||
(**self).transfer(to, value, gas_meter)
|
||||
(**self).transfer(to, value)
|
||||
}
|
||||
fn terminate(
|
||||
&mut self,
|
||||
beneficiary: &u64,
|
||||
gas_meter: &mut GasMeter<Test>,
|
||||
) -> Result<(), DispatchError> {
|
||||
(**self).terminate(beneficiary, gas_meter)
|
||||
(**self).terminate(beneficiary)
|
||||
}
|
||||
fn call(
|
||||
&mut self,
|
||||
@@ -461,7 +454,7 @@ mod tests {
|
||||
let wasm = wat::parse_str(wat).unwrap();
|
||||
let schedule = crate::Schedule::default();
|
||||
let prefab_module =
|
||||
prepare_contract::<super::runtime::Env>(&wasm, &schedule).unwrap();
|
||||
prepare_contract::<super::runtime::Env, E::T>(&wasm, &schedule).unwrap();
|
||||
|
||||
let exec = WasmExecutable {
|
||||
// Use a "call" convention.
|
||||
@@ -523,7 +516,6 @@ mod tests {
|
||||
to: 7,
|
||||
value: 153,
|
||||
data: Vec::new(),
|
||||
gas_left: 9989000000,
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -587,7 +579,6 @@ mod tests {
|
||||
to: 9,
|
||||
value: 6,
|
||||
data: vec![1, 2, 3, 4],
|
||||
gas_left: 9984500000,
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -658,7 +649,7 @@ mod tests {
|
||||
code_hash: [0x11; 32].into(),
|
||||
endowment: 3,
|
||||
data: vec![1, 2, 3, 4],
|
||||
gas_left: 9971500000,
|
||||
gas_left: 9392302058,
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -699,7 +690,6 @@ mod tests {
|
||||
&mock_ext.terminations,
|
||||
&[TerminationEntry {
|
||||
beneficiary: 0x09,
|
||||
gas_left: 9994500000,
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -763,7 +753,6 @@ mod tests {
|
||||
to: 9,
|
||||
value: 6,
|
||||
data: vec![1, 2, 3, 4],
|
||||
gas_left: 228,
|
||||
}]
|
||||
);
|
||||
}
|
||||
@@ -1470,7 +1459,7 @@ mod tests {
|
||||
vec![0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00])
|
||||
]);
|
||||
|
||||
assert_eq!(gas_meter.gas_left(), 9967000000);
|
||||
assert_eq!(gas_meter.gas_left(), 9834099446);
|
||||
}
|
||||
|
||||
const CODE_DEPOSIT_EVENT_MAX_TOPICS: &str = r#"
|
||||
|
||||
Reference in New Issue
Block a user