diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 5b3d8f20e9df7..a93a3b68be469 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -606,13 +606,18 @@ fn deploying_wasm_contract_should_work() { CheckedExtrinsic { signed: Some((charlie(), signed_extra(0, 0))), function: Call::Contracts( - pallet_contracts::Call::put_code::(10_000, transfer_code) + pallet_contracts::Call::put_code::(500_000_000, transfer_code) ), }, CheckedExtrinsic { signed: Some((charlie(), signed_extra(1, 0))), function: Call::Contracts( - pallet_contracts::Call::instantiate::(1 * DOLLARS, 10_000, transfer_ch, Vec::new()) + pallet_contracts::Call::instantiate::( + 1 * DOLLARS, + 500_000_000, + transfer_ch, + Vec::new() + ) ), }, CheckedExtrinsic { @@ -621,7 +626,7 @@ fn deploying_wasm_contract_should_work() { pallet_contracts::Call::call::( pallet_indices::address::Address::Id(addr.clone()), 10, - 10_000, + 500_000_000, vec![0x00, 0x01, 0x02, 0x03] ) ), diff --git a/frame/contracts/src/exec.rs b/frame/contracts/src/exec.rs index 027a10bb4de02..67809bbd4026b 100644 --- a/frame/contracts/src/exec.rs +++ b/frame/contracts/src/exec.rs @@ -896,6 +896,7 @@ mod tests { use crate::{ account_db::AccountDb, gas::GasMeter, tests::{ExtBuilder, Test}, exec::{ExecReturnValue, ExecError, STATUS_SUCCESS}, CodeHash, Config, + gas::Gas, }; use std::{cell::RefCell, rc::Rc, collections::HashMap, marker::PhantomData}; use assert_matches::assert_matches; @@ -905,6 +906,8 @@ mod tests { const BOB: u64 = 2; const CHARLIE: u64 = 3; + const GAS_LIMIT: Gas = 10_000_000_000; + impl<'a, T, V, L> ExecutionContext<'a, T, V, L> where T: crate::Trait { @@ -1010,7 +1013,7 @@ mod tests { #[test] fn it_works() { let value = Default::default(); - let mut gas_meter = GasMeter::::new(10000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let data = vec![]; let vm = MockVm::new(); @@ -1051,7 +1054,7 @@ mod tests { ctx.overlay.set_balance(&origin, 100); ctx.overlay.set_balance(&dest, 0); - let mut gas_meter = GasMeter::::new(1000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let result = ctx.call(dest, 0, &mut gas_meter, vec![]); assert_matches!(result, Ok(_)); @@ -1071,7 +1074,7 @@ mod tests { ctx.overlay.set_balance(&origin, 100); - let mut gas_meter = GasMeter::::new(1000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let result = ctx.instantiate(1, &mut gas_meter, &code, vec![]); assert_matches!(result, Ok(_)); @@ -1100,7 +1103,7 @@ mod tests { let output = ctx.call( dest, 55, - &mut GasMeter::::new(1000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ).unwrap(); @@ -1133,7 +1136,7 @@ mod tests { let output = ctx.call( dest, 55, - &mut GasMeter::::new(1000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ).unwrap(); @@ -1159,7 +1162,7 @@ mod tests { ctx.overlay.set_balance(&origin, 100); ctx.overlay.set_balance(&dest, 0); - let mut gas_meter = GasMeter::::new(1000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let result = ctx.call(dest, 50, &mut gas_meter, vec![]); assert_matches!(result, Ok(_)); @@ -1184,7 +1187,7 @@ mod tests { ctx.overlay.set_balance(&origin, 100); ctx.overlay.set_balance(&dest, 15); - let mut gas_meter = GasMeter::::new(1000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let result = ctx.call(dest, 50, &mut gas_meter, vec![]); assert_matches!(result, Ok(_)); @@ -1212,7 +1215,7 @@ mod tests { ctx.overlay.set_balance(&origin, 100); ctx.overlay.set_balance(&dest, 15); - let mut gas_meter = GasMeter::::new(1000); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); let result = ctx.instantiate(50, &mut gas_meter, &code, vec![]); assert_matches!(result, Ok(_)); @@ -1246,7 +1249,7 @@ mod tests { let result = ctx.call( dest, 100, - &mut GasMeter::::new(1000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1283,7 +1286,7 @@ mod tests { let result = ctx.call( dest, 0, - &mut GasMeter::::new(1000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1314,7 +1317,7 @@ mod tests { let result = ctx.call( dest, 0, - &mut GasMeter::::new(1000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1342,7 +1345,7 @@ mod tests { let result = ctx.call( BOB, 0, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), vec![1, 2, 3, 4], ); assert_matches!(result, Ok(_)); @@ -1367,7 +1370,7 @@ mod tests { let result = ctx.instantiate( 1, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &input_data_ch, vec![1, 2, 3, 4], ); @@ -1417,7 +1420,7 @@ mod tests { let result = ctx.call( BOB, value, - &mut GasMeter::::new(100000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1463,7 +1466,7 @@ mod tests { let result = ctx.call( dest, 0, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1504,7 +1507,7 @@ mod tests { let result = ctx.call( BOB, 0, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), vec![], ); @@ -1526,7 +1529,7 @@ mod tests { assert_matches!( ctx.instantiate( 0, // <- zero endowment - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &dummy_ch, vec![], ), @@ -1552,7 +1555,7 @@ mod tests { let instantiated_contract_address = assert_matches!( ctx.instantiate( 100, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &dummy_ch, vec![], ), @@ -1592,7 +1595,7 @@ mod tests { let instantiated_contract_address = assert_matches!( ctx.instantiate( 100, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &dummy_ch, vec![], ), @@ -1637,7 +1640,7 @@ mod tests { ctx.overlay.instantiate_contract(&BOB, instantiator_ch).unwrap(); assert_matches!( - ctx.call(BOB, 20, &mut GasMeter::::new(1000), vec![]), + ctx.call(BOB, 20, &mut GasMeter::::new(GAS_LIMIT), vec![]), Ok(_) ); @@ -1697,7 +1700,7 @@ mod tests { ctx.overlay.instantiate_contract(&BOB, instantiator_ch).unwrap(); assert_matches!( - ctx.call(BOB, 20, &mut GasMeter::::new(1000), vec![]), + ctx.call(BOB, 20, &mut GasMeter::::new(GAS_LIMIT), vec![]), Ok(_) ); @@ -1734,7 +1737,7 @@ mod tests { assert_matches!( ctx.instantiate( 100, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &terminate_ch, vec![], ), @@ -1770,7 +1773,7 @@ mod tests { let result = ctx.instantiate( 1, - &mut GasMeter::::new(10000), + &mut GasMeter::::new(GAS_LIMIT), &rent_allowance_ch, vec![], ); diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index da9fd644b52f7..5eb243e7c925a 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -23,6 +23,7 @@ use crate::{ BalanceOf, ComputeDispatchFee, ContractAddressFor, ContractInfo, ContractInfoOf, GenesisConfig, Module, RawAliveContractInfo, RawEvent, Trait, TrieId, TrieIdFromParentCounter, Schedule, TrieIdGenerator, account_db::{AccountDb, DirectAccountDb, OverlayAccountDb}, + gas::Gas, }; use assert_matches::assert_matches; use hex_literal::*; @@ -214,6 +215,8 @@ const BOB: u64 = 2; const CHARLIE: u64 = 3; const DJANGO: u64 = 4; +const GAS_LIMIT: Gas = 10_000_000_000; + pub struct ExtBuilder { existential_deposit: u64, } @@ -268,10 +271,10 @@ fn returns_base_call_cost() { Balances::deposit_creating(&ALICE, 100_000_000); assert_eq!( - Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, Vec::new()), + Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, Vec::new()), Ok( PostDispatchInfo { - actual_weight: Some(135), + actual_weight: Some(67500000), } ) ); @@ -359,13 +362,13 @@ fn instantiate_and_call_and_deposit_event() { ExtBuilder::default().existential_deposit(100).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Check at the end to get hash on error easily let creation = Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], ); @@ -433,7 +436,7 @@ fn dispatch_call() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Let's keep this assert even though it's redundant. If you ever need to update the // wasm source this test will fail and will show you the actual hash. @@ -458,7 +461,7 @@ fn dispatch_call() { assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -467,7 +470,7 @@ fn dispatch_call() { Origin::signed(ALICE), BOB, // newly created account 0, - 1_000_000_000_000, + GAS_LIMIT, vec![], )); @@ -554,7 +557,7 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Let's keep this assert even though it's redundant. If you ever need to update the // wasm source this test will fail and will show you the actual hash. @@ -579,7 +582,7 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() { assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -591,7 +594,7 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() { Origin::signed(ALICE), BOB, // newly created account 0, - 1_000_000_000_000, + GAS_LIMIT, vec![], ), "contract trapped during execution" @@ -650,12 +653,12 @@ fn run_out_of_gas() { .execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -667,7 +670,7 @@ fn run_out_of_gas() { Origin::signed(ALICE), BOB, // newly created account 0, - 1000, + 67_500_000, vec![], ), "ran out of gas during contract execution" @@ -696,7 +699,7 @@ fn test_set_rent_code_and_hash() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // If you ever need to update the wasm source this test will fail // and will show you the actual hash. @@ -728,21 +731,21 @@ fn storage_size() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 30_000, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); let bob_contract = ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap(); assert_eq!(bob_contract.storage_size, ::StorageSizeOffset::get() + 4); - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::set_storage_4_byte())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::set_storage_4_byte())); let bob_contract = ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap(); assert_eq!(bob_contract.storage_size, ::StorageSizeOffset::get() + 4 + 4); - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::remove_storage_4_byte())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::remove_storage_4_byte())); let bob_contract = ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap(); assert_eq!(bob_contract.storage_size, ::StorageSizeOffset::get() + 4); }); @@ -765,11 +768,11 @@ fn deduct_blocks() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 30_000, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); @@ -781,7 +784,7 @@ fn deduct_blocks() { initialize_block(5); // Trigger rent through call - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null())); // Check result let rent = (8 + 4 - 3) // storage size = size_offset + deploy_set_storage - deposit_offset @@ -796,7 +799,7 @@ fn deduct_blocks() { initialize_block(12); // Trigger rent through call - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null())); // Check result let rent_2 = (8 + 4 - 2) // storage size = size_offset + deploy_set_storage - deposit_offset @@ -808,7 +811,7 @@ fn deduct_blocks() { assert_eq!(Balances::free_balance(BOB), 30_000 - rent - rent_2); // Second call on same block should have no effect on rent - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null())); let bob_contract = ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap(); assert_eq!(bob_contract.rent_allowance, 1_000 - rent - rent_2); @@ -821,7 +824,7 @@ fn deduct_blocks() { fn call_contract_removals() { removals(|| { // Call on already-removed account might fail, and this is fine. - Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()); + Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null()); true }); } @@ -859,11 +862,11 @@ fn claim_surcharge(blocks: u64, trigger_call: impl Fn() -> bool, removes: bool) ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); @@ -892,11 +895,11 @@ fn removals(trigger_call: impl Fn() -> bool) { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm.clone())); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm.clone())); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); @@ -928,11 +931,11 @@ fn removals(trigger_call: impl Fn() -> bool) { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm.clone())); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm.clone())); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 1_000, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(100u32).encode() // rent allowance )); @@ -963,11 +966,11 @@ fn removals(trigger_call: impl Fn() -> bool) { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm.clone())); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm.clone())); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 50+Balances::minimum_balance(), - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); @@ -977,7 +980,7 @@ fn removals(trigger_call: impl Fn() -> bool) { assert_eq!(Balances::free_balance(BOB), 50 + Balances::minimum_balance()); // Transfer funds - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000_000_000, call::transfer())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::transfer())); assert_eq!(ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap().rent_allowance, 1_000); assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance()); @@ -1007,23 +1010,23 @@ fn call_removed_contract() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm.clone())); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm.clone())); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, code_hash.into(), + GAS_LIMIT, code_hash.into(), ::Balance::from(1_000u32).encode() // rent allowance )); // Calling contract should succeed. - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null())); // Advance blocks initialize_block(10); // Calling contract should remove contract and fail. assert_err_ignore_postinfo!( - Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()), + Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null()), "contract has been evicted" ); // Calling a contract that is about to evict shall emit an event. @@ -1037,7 +1040,7 @@ fn call_removed_contract() { // Subsequent contract calls should also fail. assert_err_ignore_postinfo!( - Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()), + Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null()), "contract has been evicted" ); }) @@ -1051,11 +1054,11 @@ fn default_rent_allowance_on_instantiate() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 30_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1068,7 +1071,7 @@ fn default_rent_allowance_on_instantiate() { initialize_block(5); // Trigger rent through call - assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null())); + assert_ok!(Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null())); // Check contract is still alive let bob_contract = ContractInfoOf::::get(BOB).unwrap().get_alive(); @@ -1104,8 +1107,8 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, restoration_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, set_rent_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, restoration_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, set_rent_wasm)); // If you ever need to update the wasm source this test will fail // and will show you the actual hash. @@ -1137,7 +1140,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 30_000, - 100_000, + GAS_LIMIT, set_rent_code_hash.into(), ::Balance::from(0u32).encode() )); @@ -1150,7 +1153,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: if test_different_storage { assert_ok!(Contracts::call( Origin::signed(ALICE), - BOB, 0, 100_000, + BOB, 0, GAS_LIMIT, call::set_storage_4_byte()) ); } @@ -1164,7 +1167,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: // Call `BOB`, which makes it pay rent. Since the rent allowance is set to 0 // we expect that it will get removed leaving tombstone. assert_err_ignore_postinfo!( - Contracts::call(Origin::signed(ALICE), BOB, 0, 100_000, call::null()), + Contracts::call(Origin::signed(ALICE), BOB, 0, GAS_LIMIT, call::null()), "contract has been evicted" ); assert!(ContractInfoOf::::get(BOB).unwrap().get_tombstone().is_some()); @@ -1186,7 +1189,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: assert_ok!(Contracts::instantiate( Origin::signed(CHARLIE), 30_000, - 100_000, + GAS_LIMIT, restoration_code_hash.into(), ::Balance::from(0u32).encode() )); @@ -1206,7 +1209,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: Origin::signed(ALICE), DJANGO, 0, - 100_000, + GAS_LIMIT, vec![], )); @@ -1320,11 +1323,11 @@ fn storage_max_value_limit() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 30_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1338,7 +1341,7 @@ fn storage_max_value_limit() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, Encode::encode(&self::MaxValueSize::get()), )); @@ -1348,7 +1351,7 @@ fn storage_max_value_limit() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, Encode::encode(&(self::MaxValueSize::get() + 1)), ), "contract trapped during execution" @@ -1366,13 +1369,13 @@ fn deploy_and_call_other_contract() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, callee_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, caller_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, callee_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, caller_wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, caller_code_hash.into(), vec![], )); @@ -1383,7 +1386,7 @@ fn deploy_and_call_other_contract() { Origin::signed(ALICE), BOB, 0, - 200_000, + GAS_LIMIT, callee_code_hash.as_ref().to_vec(), )); }); @@ -1394,13 +1397,13 @@ fn cannot_self_destruct_through_draning() { let (wasm, code_hash) = compile_module::(&load_wasm("drain.wat")).unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Instantiate the BOB contract. assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1418,7 +1421,7 @@ fn cannot_self_destruct_through_draning() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, vec![], ), "contract trapped during execution" @@ -1432,13 +1435,13 @@ fn cannot_self_destruct_while_live() { .unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Instantiate the BOB contract. assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1456,7 +1459,7 @@ fn cannot_self_destruct_while_live() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, vec![0], ), "contract trapped during execution" @@ -1476,13 +1479,13 @@ fn self_destruct_works() { .unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Instantiate the BOB contract. assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1499,7 +1502,7 @@ fn self_destruct_works() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, vec![], ), Ok(_) @@ -1525,15 +1528,15 @@ fn destroy_contract_and_transfer_funds() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { // Create Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, callee_wasm)); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, caller_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, callee_wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, caller_wasm)); // This deploys the BOB contract, which in turn deploys the CHARLIE contract during // construction. assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 200_000, - 100_000, + GAS_LIMIT, caller_code_hash.into(), callee_code_hash.as_ref().to_vec(), )); @@ -1549,7 +1552,7 @@ fn destroy_contract_and_transfer_funds() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, CHARLIE.encode(), )); @@ -1564,7 +1567,7 @@ fn cannot_self_destruct_in_constructor() { compile_module::(&load_wasm("self_destructing_constructor.wat")).unwrap(); ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Fail to instantiate the BOB because the call that is issued in the deploy // function exhausts all balances which puts it below the existential deposit. @@ -1572,7 +1575,7 @@ fn cannot_self_destruct_in_constructor() { Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], ), @@ -1593,11 +1596,11 @@ fn get_runtime_storage() { 0x14144020u32.to_le_bytes().to_vec().as_ref() ); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1605,7 +1608,7 @@ fn get_runtime_storage() { Origin::signed(ALICE), BOB, 0, - 100_000, + GAS_LIMIT, vec![], )); }); @@ -1617,13 +1620,13 @@ fn crypto_hashes() { ExtBuilder::default().existential_deposit(50).build().execute_with(|| { Balances::deposit_creating(&ALICE, 1_000_000); - assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm)); + assert_ok!(Contracts::put_code(Origin::signed(ALICE), GAS_LIMIT, wasm)); // Instantiate the CRYPTO_HASHES contract. assert_ok!(Contracts::instantiate( Origin::signed(ALICE), 100_000, - 100_000, + GAS_LIMIT, code_hash.into(), vec![], )); @@ -1652,7 +1655,7 @@ fn crypto_hashes() { ALICE, BOB, 0, - 100_000, + GAS_LIMIT, params, ).unwrap(); assert_eq!(result.status, 0); diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index b2fbce09e737e..cb69cd689b265 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -163,6 +163,8 @@ mod tests { use assert_matches::assert_matches; use sp_runtime::DispatchError; + const GAS_LIMIT: Gas = 10_000_000_000; + #[derive(Debug, PartialEq, Eq)] struct DispatchEntry(Call); @@ -550,7 +552,7 @@ mod tests { CODE_TRANSFER, vec![], &mut mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( @@ -559,7 +561,7 @@ mod tests { to: 7, value: 153, data: Vec::new(), - gas_left: 49978, + gas_left: 9989000000, }] ); } @@ -610,7 +612,7 @@ mod tests { CODE_CALL, vec![], &mut mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( @@ -619,7 +621,7 @@ mod tests { to: 9, value: 6, data: vec![1, 2, 3, 4], - gas_left: 49971, + gas_left: 9985500000, }] ); } @@ -672,7 +674,7 @@ mod tests { CODE_INSTANTIATE, vec![], &mut mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( @@ -681,7 +683,7 @@ mod tests { code_hash: [0x11; 32].into(), endowment: 3, data: vec![1, 2, 3, 4], - gas_left: 49947, + gas_left: 9973500000, }] ); } @@ -715,14 +717,14 @@ mod tests { CODE_TERMINATE, vec![], &mut mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( &mock_ext.terminations, &[TerminationEntry { beneficiary: 0x09, - gas_left: 49989, + gas_left: 9994500000, }] ); } @@ -773,7 +775,7 @@ mod tests { &CODE_TRANSFER_LIMITED_GAS, vec![], &mut mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( @@ -866,7 +868,7 @@ mod tests { CODE_GET_STORAGE, vec![], mock_ext, - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!(output, ExecReturnValue { status: STATUS_SUCCESS, data: [0x22; 32].to_vec() }); @@ -930,7 +932,7 @@ mod tests { CODE_CALLER, vec![], MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); } @@ -992,7 +994,7 @@ mod tests { CODE_ADDRESS, vec![], MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); } @@ -1047,7 +1049,7 @@ mod tests { #[test] fn balance() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_BALANCE, vec![], @@ -1107,7 +1109,7 @@ mod tests { #[test] fn gas_price() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_GAS_PRICE, vec![], @@ -1165,7 +1167,7 @@ mod tests { #[test] fn gas_left() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let output = execute( CODE_GAS_LEFT, @@ -1175,7 +1177,7 @@ mod tests { ).unwrap(); let gas_left = Gas::decode(&mut output.data.as_slice()).unwrap(); - assert!(gas_left < 50_000, "gas_left must be less than initial"); + assert!(gas_left < GAS_LIMIT, "gas_left must be less than initial"); assert!(gas_left > gas_meter.gas_left(), "gas_left must be greater than final"); } @@ -1230,7 +1232,7 @@ mod tests { #[test] fn value_transferred() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_VALUE_TRANSFERRED, vec![], @@ -1266,7 +1268,7 @@ mod tests { CODE_DISPATCH_CALL, vec![], &mut mock_ext, - &mut GasMeter::new(1_000_000_000_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!( @@ -1306,7 +1308,7 @@ mod tests { CODE_RETURN_FROM_START_FN, vec![], MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!(output, ExecReturnValue { status: STATUS_SUCCESS, data: vec![1, 2, 3, 4] }); @@ -1363,7 +1365,7 @@ mod tests { #[test] fn now() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_TIMESTAMP_NOW, vec![], @@ -1422,7 +1424,7 @@ mod tests { #[test] fn minimum_balance() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_MINIMUM_BALANCE, vec![], @@ -1481,7 +1483,7 @@ mod tests { #[test] fn tombstone_deposit() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_TOMBSTONE_DEPOSIT, vec![], @@ -1549,7 +1551,7 @@ mod tests { #[test] fn random() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let output = execute( CODE_RANDOM, @@ -1594,7 +1596,7 @@ mod tests { #[test] fn deposit_event() { let mut mock_ext = MockExt::default(); - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let _ = execute( CODE_DEPOSIT_EVENT, vec![], @@ -1607,7 +1609,7 @@ mod tests { vec![0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00]) ]); - assert_eq!(gas_meter.gas_left(), 49934); + assert_eq!(gas_meter.gas_left(), 9967000000); } const CODE_DEPOSIT_EVENT_MAX_TOPICS: &str = r#" @@ -1640,7 +1642,7 @@ mod tests { #[test] fn deposit_event_max_topics() { // Checks that the runtime traps if there are more than `max_topic_events` topics. - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); assert_matches!( execute( @@ -1684,7 +1686,7 @@ mod tests { #[test] fn deposit_event_duplicates() { // Checks that the runtime traps if there are duplicates. - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); assert_matches!( execute( @@ -1755,7 +1757,7 @@ mod tests { CODE_BLOCK_NUMBER, vec![], MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); } @@ -1795,7 +1797,7 @@ mod tests { CODE_SIMPLE_ASSERT, input_data, MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!(output.data.len(), 0); @@ -1811,7 +1813,7 @@ mod tests { CODE_SIMPLE_ASSERT, input_data, MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).err().unwrap(); assert_eq!(error.buffer.capacity(), 1_234); @@ -1865,7 +1867,7 @@ mod tests { CODE_RETURN_WITH_DATA, hex!("00112233445566778899").to_vec(), MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!(output, ExecReturnValue { status: 0, data: hex!("445566778899").to_vec() }); @@ -1878,7 +1880,7 @@ mod tests { CODE_RETURN_WITH_DATA, hex!("112233445566778899").to_vec(), MockExt::default(), - &mut GasMeter::new(50_000), + &mut GasMeter::new(GAS_LIMIT), ).unwrap(); assert_eq!(output, ExecReturnValue { status: 17, data: hex!("5566778899").to_vec() }); @@ -1964,7 +1966,7 @@ mod tests { #[test] fn get_runtime_storage() { - let mut gas_meter = GasMeter::new(50_000); + let mut gas_meter = GasMeter::new(GAS_LIMIT); let mock_ext = MockExt::default(); // "\01\02\03\04" - Some(0x14144020)