Skip to content

Commit

Permalink
count gas refund diff for manual correction in case opcode execution …
Browse files Browse the repository at this point in the history
…returns ErrWriteProtection
  • Loading branch information
dustinxie committed Nov 23, 2022
1 parent dcc10c7 commit 9e576f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ type BlockContext struct {
// All fields can change between transactions.
type TxContext struct {
// Message information
Origin common.Address // Provides information for ORIGIN
GasPrice *big.Int // Provides information for GASPRICE
Origin common.Address // Provides information for ORIGIN
GasPrice *big.Int // Provides information for GASPRICE
DeltaRefundByDynamicGas int64 // difference of refund due to dynamicGas
}

// EVM is the Ethereum Virtual Machine base object and provides
Expand Down
11 changes: 9 additions & 2 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// For optimisation reason we're using uint64 as the program counter.
// It's theoretically possible to go above 2^64. The YP defines the PC
// to be uint256. Practically much less so feasible.
pc = uint64(0) // program counter
cost uint64
pc = uint64(0) // program counter
cost uint64
refundBeforeDynamicGas int64
refundDiff int64
// copies used by tracer
pcCopy uint64 // needed for the deferred EVMLogger
gasCopy uint64 // for EVMLogger to log gas remaining before execution
Expand Down Expand Up @@ -218,7 +220,9 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// Consume the gas and return an error if not enough gas is available.
// cost is explicitly set so that the capture state defer method can get the proper cost
var dynamicCost uint64
refundBeforeDynamicGas = int64(in.evm.StateDB.GetRefund())

This comment has been minimized.

Copy link
@CoderZhi

CoderZhi Nov 30, 2022

this could be a local parameter

dynamicCost, err = operation.dynamicGas(in.evm, contract, stack, mem, memorySize)
refundDiff = int64(in.evm.StateDB.GetRefund()) - refundBeforeDynamicGas
cost += dynamicCost // for tracing
if err != nil || !contract.UseGas(dynamicCost) {
return nil, ErrOutOfGas
Expand All @@ -234,6 +238,9 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// execute the operation
res, err = operation.execute(&pc, in, callContext)
if err != nil {
if err == ErrWriteProtection {
in.evm.TxContext.DeltaRefundByDynamicGas += refundDiff
}
break
}
pc++
Expand Down

0 comments on commit 9e576f7

Please sign in to comment.