Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit 8742ccb

Browse files
Merge remote-tracking branch 'origin/develop' into chore/bt_127_merge_substrate
* origin/develop: Uncouple emptying gas_meter from updating the gas spent for the block (#68)
2 parents dcb159a + d2319b9 commit 8742ccb

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

frame/contracts/src/gas.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use crate::{GasSpent, Module, Trait, BalanceOf};
17+
use crate::{Module, Trait, BalanceOf};
1818
use sp_std::convert::TryFrom;
1919
use sp_runtime::traits::{
2020
CheckedMul, Zero, SaturatedConversion, AtLeast32Bit, UniqueSaturatedInto,
2121
};
2222
use frame_support::{
23-
traits::{Currency, ExistenceRequirement, OnUnbalanced, WithdrawReason}, StorageValue,
24-
dispatch::DispatchError,
23+
traits::{Currency, ExistenceRequirement, OnUnbalanced, WithdrawReason}, dispatch::DispatchError,
2524
};
2625

2726
#[cfg(test)]
@@ -257,14 +256,8 @@ pub fn refund_unused_gas<T: Trait>(
257256
transactor: &T::AccountId,
258257
gas_meter: GasMeter<T>,
259258
) {
260-
let gas_spent = gas_meter.spent();
261259
let gas_left = gas_meter.gas_left();
262260

263-
// Increase total spent gas.
264-
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
265-
// also has Gas type.
266-
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_spent);
267-
268261
// Refund gas left by the price it was bought at.
269262
let refund = gas_meter.gas_price * gas_left.unique_saturated_into();
270263
let _imbalance = T::Currency::deposit_creating(transactor, refund);

frame/contracts/src/gas_tests.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ impl GasHandler<GasTest> for TestGasHandler {
263263
}
264264
}
265265

266+
pub struct NoChargeGasHandler;
267+
impl GasHandler<GasTest> for NoChargeGasHandler {
268+
fn fill_gas(
269+
_transactor: &<GasTest as system::Trait>::AccountId,
270+
gas_limit: Gas,
271+
) -> Result<GasMeter<GasTest>, DispatchError> {
272+
// fills the gas meter without charging the user
273+
Ok(GasMeter::with_limit(gas_limit, 1))
274+
}
275+
276+
fn empty_unused_gas(
277+
transactor: &<GasTest as system::Trait>::AccountId,
278+
gas_meter: GasMeter<GasTest>,
279+
) {
280+
// Do not charge the transactor. Give gas for free.
281+
}
282+
}
283+
266284
#[test]
267285
// Tests that the user is not charged when filling up gas meters
268286
fn customized_fill_gas_does_not_charge_the_user() {
@@ -274,9 +292,11 @@ fn customized_fill_gas_does_not_charge_the_user() {
274292
// Create test account
275293
Balances::deposit_creating(&ALICE, 1000);
276294

277-
// fill gas
278-
let gas_meter_result = TestGasHandler::fill_gas(&ALICE, 500);
279-
assert!(gas_meter_result.is_ok());
295+
let gas_limit = 500;
296+
let mut gas_meter = NoChargeGasHandler::fill_gas(&ALICE, gas_limit).unwrap();
297+
// Charge as if the whole gas_limit is used
298+
gas_meter.charge(&(), SimpleToken(gas_limit));
299+
NoChargeGasHandler::empty_unused_gas(&ALICE, gas_meter);
280300

281301
// Check the user is not charged
282302
assert_eq!(Balances::free_balance(&ALICE), 1000);

frame/contracts/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ decl_module! {
582582
if let Ok(code_hash) = result {
583583
Self::deposit_event(RawEvent::CodeStored(code_hash));
584584
}
585+
// Increase total spent gas.
586+
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
587+
// also has Gas type.
588+
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_meter.spent());
589+
585590
T::GasHandler::empty_unused_gas(&origin, gas_meter);
586591

587592
result.map(|_| ()).map_err(Into::into)
@@ -746,6 +751,11 @@ impl<T: Trait> Module<T> {
746751
DirectAccountDb.commit(ctx.overlay.into_change_set());
747752
}
748753

754+
// Increase total spent gas.
755+
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
756+
// also has Gas type.
757+
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_meter.spent());
758+
749759
// Handle unused gas of the gas meter. Default behaviour is to refund cost of the unused gas.
750760
//
751761
// NOTE: This should go after the commit to the storage, since the storage changes

0 commit comments

Comments
 (0)