Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions frame/contracts/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ pub trait GasHandler<T: Trait> {
buy_gas::<T>(transactor, gas_limit)
}

/// This function should be called when the contract has stopped consuming gas and the gas_meter
/// is ready to be read. This will update the gas spent for the block and then empties the
/// unused gas.
fn update(transactor: &T::AccountId, gas_meter: GasMeter<T>) {
// Increase total spent gas.
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
// also has Gas type.
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_meter.spent());

Self::empty_unused_gas(transactor, gas_meter);
}

/// This function empties the remaining gas in the gas meter
/// Default behaviour will deposit currency into the user's balance to refund un-used gas
fn empty_unused_gas(
Expand Down Expand Up @@ -257,14 +269,8 @@ pub fn refund_unused_gas<T: Trait>(
transactor: &T::AccountId,
gas_meter: GasMeter<T>,
) {
let gas_spent = gas_meter.spent();
let gas_left = gas_meter.gas_left();

// Increase total spent gas.
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
// also has Gas type.
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_spent);

// Refund gas left by the price it was bought at.
let refund = gas_meter.gas_price * gas_left.unique_saturated_into();
let _imbalance = T::Currency::deposit_creating(transactor, refund);
Expand Down
4 changes: 2 additions & 2 deletions frame/contracts/src/gas_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn user_is_charged_on_empty_unused_gas() {

// Spend half the gas, then empty the gas meter, the user should be charged here
gas_meter.charge(&(), SimpleToken(gas_used));
TestGasHandler::empty_unused_gas(&ALICE, gas_meter);
TestGasHandler::update(&ALICE, gas_meter);

assert_eq!(Balances::free_balance(&ALICE), expected_remaining_balance);
});
Expand All @@ -327,7 +327,7 @@ fn user_is_charged_if_out_of_gas() {

// Spend more gas than the `gas_limit`, then empty the gas meter, the user should be charged here
gas_meter.charge(&(), SimpleToken(gas_limit + 1));
TestGasHandler::empty_unused_gas(&ALICE, gas_meter);
TestGasHandler::update(&ALICE, gas_meter);

assert_eq!(Balances::free_balance(&ALICE), expected_remaining_balance);
});
Expand Down
4 changes: 2 additions & 2 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ decl_module! {
if let Ok(code_hash) = result {
Self::deposit_event(RawEvent::CodeStored(code_hash));
}
T::GasHandler::empty_unused_gas(&origin, gas_meter);
T::GasHandler::update(&origin, gas_meter);

result.map(|_| ()).map_err(Into::into)
}
Expand Down Expand Up @@ -756,7 +756,7 @@ impl<T: Trait> Module<T> {
//
// NOTE: This should go after the commit to the storage, since the storage changes
// can alter the balance of the caller.
T::GasHandler::empty_unused_gas(&origin, gas_meter);
T::GasHandler::update(&origin, gas_meter);

// Execute deferred actions.
ctx.deferred.into_iter().for_each(|deferred| {
Expand Down