[contracts] Port host functions to Weight V2 and storage deposit limit (#13565)

* added [unstable][seal2] call()

* updated test to cover new seal_call proof_limit

* docs updated

* add [seal2][unstable] instantiate() and test

* add [seal2][unstable] weight_to_fee() + docs and test

* add [seal2][unstable] gas_left() + docs and test

* update benchmarks

* add DefaultDepositLimit to pallet Config

* specify deposit limit for nested call

add test for nested call deposit limit

save: separate deposit limit for nested calls

* specify deposit limit for nested instantiate

save: works with test

cleaned up debugging outputs

* update benchmarks

* added missing fixtures

* fix benches

* pass explicit deposit limit to storage bench

* explicit deposit limit for another set_storage bench

* add more deposit limit for storage benches

* moving to simplified benchmarks

* moved to simplified benchmarks

* fix seal_weight_to_fee bench

* fix seal_instantiate benchmark

* doc typo fix

* default dl for benchmarking

more dl for tests

dl for tests to max

deposit_limit fix in instantiate bench

fix instantiate bench

fix instantiate benchmark

fix instantiate bench again

remove dbg

fix seal bench again

fixing it still

seal_instantiate zero deposit

less runs to check if deposit enough

try

try 2

try 3

try 4

* max_runtime_mem to Schedule limits

* add default deposit limit fallback check to test

* weight params renaming

* fmt

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

Co-authored-by: PG Herveou <pgherveou@gmail.com>

* prettify inputs in tests

* typestate param refactored

---------

Co-authored-by: PG Herveou <pgherveou@gmail.com>
This commit is contained in:
Sasha Gryaznov
2023-04-26 14:27:13 +03:00
committed by GitHub
parent 01c66da036
commit 60310de7d6
16 changed files with 949 additions and 275 deletions
+27 -21
View File
@@ -370,7 +370,7 @@ mod tests {
gas::GasMeter,
storage::WriteOutcome,
tests::{RuntimeCall, Test, ALICE, BOB},
BalanceOf, CodeHash, Error, OldWeight, Pallet as Contracts,
BalanceOf, CodeHash, Error, Pallet as Contracts,
};
use assert_matches::assert_matches;
use frame_support::{
@@ -470,6 +470,7 @@ mod tests {
fn call(
&mut self,
_gas_limit: Weight,
_deposit_limit: BalanceOf<Self::T>,
to: AccountIdOf<Self::T>,
value: u64,
data: Vec<u8>,
@@ -489,6 +490,7 @@ mod tests {
fn instantiate(
&mut self,
gas_limit: Weight,
_deposit_limit: BalanceOf<Self::T>,
code_hash: CodeHash<Test>,
value: u64,
data: Vec<u8>,
@@ -587,7 +589,11 @@ mod tests {
16_384
}
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T> {
BalanceOf::<Self::T>::from(1312_u32).saturating_mul(weight.ref_time().into())
BalanceOf::<Self::T>::from(1312_u32)
.saturating_mul(weight.ref_time().into())
.saturating_add(
BalanceOf::<Self::T>::from(103_u32).saturating_mul(weight.proof_size()),
)
}
fn schedule(&self) -> &Schedule<Self::T> {
&self.schedule
@@ -1589,7 +1595,7 @@ mod tests {
const CODE_GAS_PRICE: &str = r#"
(module
(import "seal0" "seal_weight_to_fee" (func $seal_weight_to_fee (param i64 i32 i32)))
(import "seal1" "weight_to_fee" (func $seal_weight_to_fee (param i64 i64 i32 i32)))
(import "env" "memory" (memory 1 1))
;; size of our buffer is 32 bytes
@@ -1606,7 +1612,7 @@ mod tests {
(func (export "call")
;; This stores the gas price in the buffer
(call $seal_weight_to_fee (i64.const 2) (i32.const 0) (i32.const 32))
(call $seal_weight_to_fee (i64.const 2) (i64.const 1) (i32.const 0) (i32.const 32))
;; assert len == 8
(call $assert
@@ -1616,11 +1622,11 @@ mod tests {
)
)
;; assert that contents of the buffer is equal to the i64 value of 2 * 1312.
;; assert that contents of the buffer is equal to the i64 value of 2 * 1312 + 103 = 2727.
(call $assert
(i64.eq
(i64.load (i32.const 0))
(i64.const 2624)
(i64.const 2727)
)
)
)
@@ -1635,12 +1641,12 @@ mod tests {
const CODE_GAS_LEFT: &str = r#"
(module
(import "seal0" "seal_gas_left" (func $seal_gas_left (param i32 i32)))
(import "seal1" "gas_left" (func $seal_gas_left (param i32 i32)))
(import "seal0" "seal_return" (func $seal_return (param i32 i32 i32)))
(import "env" "memory" (memory 1 1))
;; size of our buffer is 32 bytes
(data (i32.const 32) "\20")
;; Make output buffer size 20 bytes
(data (i32.const 20) "\14")
(func $assert (param i32)
(block $ok
@@ -1652,19 +1658,19 @@ mod tests {
)
(func (export "call")
;; This stores the gas left in the buffer
(call $seal_gas_left (i32.const 0) (i32.const 32))
;; This stores the weight left to the buffer
(call $seal_gas_left (i32.const 0) (i32.const 20))
;; assert len == 8
;; Assert len <= 16 (max encoded Weight len)
(call $assert
(i32.eq
(i32.load (i32.const 32))
(i32.const 8)
(i32.le_u
(i32.load (i32.const 20))
(i32.const 16)
)
)
;; return gas left
(call $seal_return (i32.const 0) (i32.const 0) (i32.const 8))
;; Return weight left and its encoded value len
(call $seal_return (i32.const 0) (i32.const 0) (i32.load (i32.const 20)))
(unreachable)
)
@@ -1679,11 +1685,11 @@ mod tests {
let output = execute(CODE_GAS_LEFT, vec![], &mut ext).unwrap();
let gas_left = OldWeight::decode(&mut &*output.data).unwrap();
let weight_left = Weight::decode(&mut &*output.data).unwrap();
let actual_left = ext.gas_meter.gas_left();
// TODO: account for proof size weight
assert!(gas_left < gas_limit.ref_time(), "gas_left must be less than initial");
assert!(gas_left > actual_left.ref_time(), "gas_left must be greater than final");
assert!(weight_left.all_lt(gas_limit), "gas_left must be less than initial");
assert!(weight_left.all_gt(actual_left), "gas_left must be greater than final");
}
/// Test that [`frame_support::weights::OldWeight`] en/decodes the same as our