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

Commit 9279d0b

Browse files
Alex Sedjordy25519
Alex Sed
authored andcommitted
Uncouple emptying gas_meter from updating the gas spent for the block (#68)
* Uncouple emptying gas_meter from updating the gas spent for the block.
1 parent 9469920 commit 9279d0b

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
@@ -262,6 +262,24 @@ impl GasHandler<GasTest> for TestGasHandler {
262262
}
263263
}
264264

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

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

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

frame/contracts/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,11 @@ decl_module! {
588588
if let Ok(code_hash) = result {
589589
Self::deposit_event(RawEvent::CodeStored(code_hash));
590590
}
591+
// Increase total spent gas.
592+
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
593+
// also has Gas type.
594+
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_meter.spent());
595+
591596
T::GasHandler::empty_unused_gas(&origin, gas_meter);
592597

593598
result.map(|_| ()).map_err(Into::into)
@@ -752,6 +757,11 @@ impl<T: Trait> Module<T> {
752757
DirectAccountDb.commit(ctx.overlay.into_change_set());
753758
}
754759

760+
// Increase total spent gas.
761+
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
762+
// also has Gas type.
763+
GasSpent::mutate(|block_gas_spent| *block_gas_spent += gas_meter.spent());
764+
755765
// Handle unused gas of the gas meter. Default behaviour is to refund cost of the unused gas.
756766
//
757767
// NOTE: This should go after the commit to the storage, since the storage changes

0 commit comments

Comments
 (0)