Adapt pallet-contracts to WeightV2 (#12421)

* Replace contract access weight by proper PoV component

* Return the whole weight struct from dry-runs

* Fixup `seal_call` and `seal_instantiate`

* Fix duplicate extrinsics

* Remove ContractAccessWeight from runtime

* Fix doc link

* Remove leftover debugging output
This commit is contained in:
Alexander Theißen
2022-10-05 13:10:20 +02:00
committed by GitHub
parent 8f048cf522
commit b135a0fae4
8 changed files with 96 additions and 211 deletions
+19 -17
View File
@@ -26,8 +26,8 @@ use crate::{
tests::test_utils::{get_contract, get_contract_checked},
wasm::{PrefabWasmModule, ReturnCode as RuntimeReturnCode},
weights::WeightInfo,
BalanceOf, Code, CodeStorage, Config, ContractInfoOf, DefaultAddressGenerator,
DefaultContractAccessWeight, DeletionQueue, Error, Pallet, Schedule,
BalanceOf, Code, CodeStorage, Config, ContractInfoOf, DefaultAddressGenerator, DeletionQueue,
Error, Pallet, Schedule,
};
use assert_matches::assert_matches;
use codec::Encode;
@@ -404,7 +404,6 @@ impl Config for Test {
type DepositPerByte = DepositPerByte;
type DepositPerItem = DepositPerItem;
type AddressGenerator = DefaultAddressGenerator;
type ContractAccessWeight = DefaultContractAccessWeight<BlockWeights>;
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
}
@@ -414,7 +413,7 @@ pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]);
pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]);
pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(u64::MAX);
pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(256 * 1024);
pub struct ExtBuilder {
existential_deposit: u64,
@@ -674,7 +673,7 @@ fn run_out_of_gas() {
RuntimeOrigin::signed(ALICE),
addr, // newly created account
0,
Weight::from_ref_time(1_000_000_000_000),
Weight::from_ref_time(1_000_000_000_000).set_proof_size(u64::MAX),
None,
vec![],
),
@@ -1760,7 +1759,7 @@ fn chain_extension_works() {
false,
);
assert_ok!(result.result);
assert_eq!(result.gas_consumed, gas_consumed + 42);
assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 42);
let result = Contracts::bare_call(
ALICE,
addr.clone(),
@@ -1771,7 +1770,7 @@ fn chain_extension_works() {
false,
);
assert_ok!(result.result);
assert_eq!(result.gas_consumed, gas_consumed + 95);
assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 95);
// 3 = diverging chain extension call that sets flags to 0x1 and returns a fixed buffer
let result = Contracts::bare_call(
@@ -2409,10 +2408,11 @@ fn reinstrument_does_charge() {
let result2 =
Contracts::bare_call(ALICE, addr.clone(), 0, GAS_LIMIT, None, zero.clone(), false);
assert!(!result2.result.unwrap().did_revert());
assert!(result2.gas_consumed > result1.gas_consumed);
assert!(result2.gas_consumed.ref_time() > result1.gas_consumed.ref_time());
assert_eq!(
result2.gas_consumed,
result1.gas_consumed + <Test as Config>::WeightInfo::reinstrument(code_len).ref_time(),
result2.gas_consumed.ref_time(),
result1.gas_consumed.ref_time() +
<Test as Config>::WeightInfo::reinstrument(code_len).ref_time(),
);
});
}
@@ -2536,7 +2536,7 @@ fn gas_estimation_nested_call_fixed_limit() {
assert_ok!(&result.result);
// We have a subcall with a fixed gas limit. This constitutes precharging.
assert!(result.gas_required > result.gas_consumed);
assert!(result.gas_required.ref_time() > result.gas_consumed.ref_time());
// Make the same call using the estimated gas. Should succeed.
assert_ok!(
@@ -2544,7 +2544,7 @@ fn gas_estimation_nested_call_fixed_limit() {
ALICE,
addr_caller,
0,
Weight::from_ref_time(result.gas_required).set_proof_size(u64::MAX),
result.gas_required,
Some(result.storage_deposit.charge_or_zero()),
input,
false,
@@ -2557,6 +2557,7 @@ fn gas_estimation_nested_call_fixed_limit() {
#[test]
#[cfg(feature = "unstable-interface")]
fn gas_estimation_call_runtime() {
use codec::Decode;
let (caller_code, caller_hash) = compile_module::<Test>("call_runtime").unwrap();
let (callee_code, callee_hash) = compile_module::<Test>("dummy").unwrap();
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
@@ -2591,7 +2592,7 @@ fn gas_estimation_call_runtime() {
let call = RuntimeCall::Contracts(crate::Call::call {
dest: addr_callee,
value: 0,
gas_limit: GAS_LIMIT / 3,
gas_limit: GAS_LIMIT.set_ref_time(GAS_LIMIT.ref_time() / 3),
storage_deposit_limit: None,
data: vec![],
});
@@ -2604,9 +2605,10 @@ fn gas_estimation_call_runtime() {
call.encode(),
false,
);
assert_ok!(&result.result);
assert!(result.gas_required > result.gas_consumed);
// contract encodes the result of the dispatch runtime
let outcome = u32::decode(&mut result.result.unwrap().data.as_ref()).unwrap();
assert_eq!(outcome, 0);
assert!(result.gas_required.ref_time() > result.gas_consumed.ref_time());
// Make the same call using the required gas. Should succeed.
assert_ok!(
@@ -2614,7 +2616,7 @@ fn gas_estimation_call_runtime() {
ALICE,
addr_caller,
0,
Weight::from_ref_time(result.gas_required).set_proof_size(u64::MAX),
result.gas_required,
None,
call.encode(),
false,