simplify 'gasUsed' and 'refundGas'

This commit is contained in:
andri lim 2020-01-30 22:54:29 +07:00 committed by zah
parent d9991b1e8b
commit 270854a5aa
2 changed files with 11 additions and 17 deletions

View File

@ -13,21 +13,22 @@ proc processTransaction*(tx: Transaction, sender: EthAddress, vmState: BaseVMSta
trace "Sender", sender
trace "txHash", rlpHash = tx.rlpHash
var gasUsed = 0.GasInt
if validateTransaction(vmState, tx, sender, fork):
gasUsed = tx.gasLimit
var c = setupComputation(vmState, tx, sender, fork)
vmState.mutateStateDB:
db.subBalance(sender, tx.gasLimit.u256 * tx.gasPrice.u256)
execComputation(c)
if not c.shouldBurnGas:
gasUsed = c.refundGas(tx, sender)
vmState.cumulativeGasUsed += gasUsed
result = tx.gasLimit
if not c.shouldBurnGas:
c.refundGas(tx, sender)
result -= c.gasMeter.gasRemaining
vmState.cumulativeGasUsed += result
vmState.mutateStateDB:
# miner fee
let txFee = gasUsed.u256 * tx.gasPrice.u256
let txFee = result.u256 * tx.gasPrice.u256
db.addBalance(vmState.blockHeader.coinbase, txFee)
for deletedAccount in vmState.suicides:
@ -42,7 +43,6 @@ proc processTransaction*(tx: Transaction, sender: EthAddress, vmState: BaseVMSta
db.deleteAccount(account)
vmState.accountDb.updateOriginalRoot()
result = gasUsed
type
# TODO: these types need to be removed

View File

@ -84,17 +84,11 @@ proc execComputation*(c: Computation) =
c.vmstate.status = c.isSuccess
proc refundGas*(c: Computation, tx: Transaction, sender: EthAddress): GasInt =
let
gasRemaining = c.gasMeter.gasRemaining
gasRefunded = c.getGasRefund()
gasUsed = tx.gasLimit - gasRemaining
gasRefund = min(gasRefunded, gasUsed div 2)
proc refundGas*(c: Computation, tx: Transaction, sender: EthAddress) =
let maxRefund = (tx.gasLimit - c.gasMeter.gasRemaining) div 2
c.gasMeter.returnGas min(c.getGasRefund(), maxRefund)
c.vmState.mutateStateDB:
db.addBalance(sender, (gasRemaining + gasRefund).u256 * tx.gasPrice.u256)
result = gasUsed - gasRefund
db.addBalance(sender, c.gasMeter.gasRemaining.u256 * tx.gasPrice.u256)
#[
method executeTransaction(vmState: BaseVMState, transaction: Transaction): (Computation, BlockHeader) {.base.}=