mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 15:46:43 +00:00
Change OnGasConsumed(gas, cost uint64, reason) to OnGasChange(old, new uint64, reason)
This way, we avoid having a `cost` that is negative which does not make sense for a `uint64`. Having the `old, new` also yields correct value and the delta can then be negative and be holded in a `int64`. # Conflicts: # core/vm/evm.go
This commit is contained in:
parent
47d9118557
commit
d0dbbaf6a5
13 changed files with 41 additions and 46 deletions
|
|
@ -265,7 +265,7 @@ func (st *StateTransition) buyGas() error {
|
|||
}
|
||||
|
||||
if st.evm.Config.Tracer != nil {
|
||||
st.evm.Config.Tracer.OnGasConsumed(0, -st.msg.GasLimit, vm.GasInitialBalance)
|
||||
st.evm.Config.Tracer.OnGasChange(0, st.msg.GasLimit, vm.GasInitialBalance)
|
||||
}
|
||||
|
||||
st.gasRemaining += st.msg.GasLimit
|
||||
|
|
@ -391,7 +391,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
|
|||
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas)
|
||||
}
|
||||
if t := st.evm.Config.Tracer; t != nil {
|
||||
t.OnGasConsumed(st.gasRemaining, gas, vm.GasChangeIntrinsicGas)
|
||||
t.OnGasChange(st.gasRemaining, st.gasRemaining-gas, vm.GasChangeIntrinsicGas)
|
||||
}
|
||||
st.gasRemaining -= gas
|
||||
|
||||
|
|
@ -459,7 +459,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
|
|||
}
|
||||
|
||||
if st.evm.Config.Tracer != nil {
|
||||
st.evm.Config.Tracer.OnGasConsumed(st.gasRemaining, -refund, vm.GasRefunded)
|
||||
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, vm.GasRefunded)
|
||||
}
|
||||
|
||||
st.gasRemaining += refund
|
||||
|
|
@ -469,7 +469,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
|
|||
st.state.AddBalance(st.msg.From, remaining, state.BalanceChangeGasRefund)
|
||||
|
||||
if st.evm.Config.Tracer != nil {
|
||||
st.evm.Config.Tracer.OnGasConsumed(st.gasRemaining, st.gasRemaining, vm.GasBuyBack)
|
||||
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, vm.GasBuyBack)
|
||||
}
|
||||
|
||||
// Also return remaining gas to the block gas counter so it is
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ func (c *Contract) UseGas(gas uint64, logger EVMLogger, reason GasChangeReason)
|
|||
return false
|
||||
}
|
||||
if logger != nil {
|
||||
logger.OnGasConsumed(c.Gas, gas, reason)
|
||||
logger.OnGasChange(c.Gas, c.Gas-gas, reason)
|
||||
}
|
||||
c.Gas -= gas
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uin
|
|||
return nil, 0, ErrOutOfGas
|
||||
}
|
||||
if logger != nil {
|
||||
logger.OnGasConsumed(suppliedGas, gasCost, GasChangePrecompiledContract)
|
||||
logger.OnGasChange(suppliedGas, suppliedGas-gasCost, GasChangePrecompiledContract)
|
||||
}
|
||||
suppliedGas -= gasCost
|
||||
output, err := p.Run(input)
|
||||
|
|
|
|||
|
|
@ -189,10 +189,9 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
|
|||
} else {
|
||||
// Handle tracer events for entering and exiting a call frame
|
||||
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
|
||||
evm.Config.Tracer.OnGasConsumed(0, -gas, GasInitialBalance)
|
||||
|
||||
evm.Config.Tracer.OnGasChange(0, gas, GasInitialBalance)
|
||||
defer func(startGas uint64) {
|
||||
evm.Config.Tracer.OnGasConsumed(leftOverGas, leftOverGas, GasBuyBack)
|
||||
evm.Config.Tracer.OnGasChange(leftOverGas, 0, GasBuyBack)
|
||||
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
|
||||
}(gas)
|
||||
}
|
||||
|
|
@ -242,7 +241,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
|
|||
evm.StateDB.RevertToSnapshot(snapshot)
|
||||
if err != ErrExecutionReverted {
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.OnGasConsumed(gas, gas, GasChangeFailedExecution)
|
||||
evm.Config.Tracer.OnGasChange(gas, 0, GasChangeFailedExecution)
|
||||
}
|
||||
|
||||
gas = 0
|
||||
|
|
@ -265,10 +264,9 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
|
|||
// Invoke tracer hooks that signal entering/exiting a call frame
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.CaptureEnter(CALLCODE, caller.Address(), addr, input, gas, value)
|
||||
evm.Config.Tracer.OnGasConsumed(0, -gas, GasInitialBalance)
|
||||
|
||||
evm.Config.Tracer.OnGasChange(0, gas, GasInitialBalance)
|
||||
defer func(startGas uint64) {
|
||||
evm.Config.Tracer.OnGasConsumed(leftOverGas, leftOverGas, GasBuyBack)
|
||||
evm.Config.Tracer.OnGasChange(leftOverGas, 0, GasBuyBack)
|
||||
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
|
||||
}(gas)
|
||||
}
|
||||
|
|
@ -301,7 +299,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
|
|||
evm.StateDB.RevertToSnapshot(snapshot)
|
||||
if err != ErrExecutionReverted {
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.OnGasConsumed(gas, gas, GasChangeFailedExecution)
|
||||
evm.Config.Tracer.OnGasChange(gas, 0, GasChangeFailedExecution)
|
||||
}
|
||||
|
||||
gas = 0
|
||||
|
|
@ -323,10 +321,9 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
|
|||
parent := caller.(*Contract)
|
||||
// DELEGATECALL inherits value from parent call
|
||||
evm.Config.Tracer.CaptureEnter(DELEGATECALL, caller.Address(), addr, input, gas, parent.value)
|
||||
evm.Config.Tracer.OnGasConsumed(0, -gas, GasInitialBalance)
|
||||
|
||||
evm.Config.Tracer.OnGasChange(0, gas, GasInitialBalance)
|
||||
defer func(startGas uint64) {
|
||||
evm.Config.Tracer.OnGasConsumed(leftOverGas, leftOverGas, GasBuyBack)
|
||||
evm.Config.Tracer.OnGasChange(leftOverGas, 0, GasBuyBack)
|
||||
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
|
||||
}(gas)
|
||||
}
|
||||
|
|
@ -351,7 +348,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
|
|||
evm.StateDB.RevertToSnapshot(snapshot)
|
||||
if err != ErrExecutionReverted {
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.OnGasConsumed(gas, gas, GasChangeFailedExecution)
|
||||
evm.Config.Tracer.OnGasChange(gas, 0, GasChangeFailedExecution)
|
||||
}
|
||||
|
||||
gas = 0
|
||||
|
|
@ -368,10 +365,9 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
|
|||
// Invoke tracer hooks that signal entering/exiting a call frame
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.CaptureEnter(STATICCALL, caller.Address(), addr, input, gas, nil)
|
||||
evm.Config.Tracer.OnGasConsumed(0, -gas, GasInitialBalance)
|
||||
|
||||
evm.Config.Tracer.OnGasChange(0, gas, GasInitialBalance)
|
||||
defer func(startGas uint64) {
|
||||
evm.Config.Tracer.OnGasConsumed(leftOverGas, leftOverGas, GasBuyBack)
|
||||
evm.Config.Tracer.OnGasChange(leftOverGas, 0, GasBuyBack)
|
||||
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
|
||||
}(gas)
|
||||
}
|
||||
|
|
@ -413,7 +409,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
|
|||
evm.StateDB.RevertToSnapshot(snapshot)
|
||||
if err != ErrExecutionReverted {
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.OnGasConsumed(gas, gas, GasChangeFailedExecution)
|
||||
evm.Config.Tracer.OnGasChange(gas, 0, GasChangeFailedExecution)
|
||||
}
|
||||
|
||||
gas = 0
|
||||
|
|
@ -444,10 +440,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
|
|||
}()
|
||||
} else {
|
||||
evm.Config.Tracer.CaptureEnter(typ, caller.Address(), address, codeAndHash.code, gas, value)
|
||||
evm.Config.Tracer.OnGasConsumed(0, -gas, GasInitialBalance)
|
||||
|
||||
evm.Config.Tracer.OnGasChange(0, gas, GasInitialBalance)
|
||||
defer func() {
|
||||
evm.Config.Tracer.OnGasConsumed(leftoverGas, leftoverGas, GasBuyBack)
|
||||
evm.Config.Tracer.OnGasChange(leftoverGas, 0, GasBuyBack)
|
||||
evm.Config.Tracer.CaptureExit(ret, gas-leftoverGas, err)
|
||||
}()
|
||||
}
|
||||
|
|
@ -474,7 +469,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
|
|||
contractHash := evm.StateDB.GetCodeHash(address)
|
||||
if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != types.EmptyCodeHash) {
|
||||
if evm.Config.Tracer != nil {
|
||||
evm.Config.Tracer.OnGasConsumed(gas, gas, GasChangeFailedExecution)
|
||||
evm.Config.Tracer.OnGasChange(gas, 0, GasChangeFailedExecution)
|
||||
}
|
||||
|
||||
return nil, common.Address{}, 0, ErrContractAddressCollision
|
||||
|
|
|
|||
|
|
@ -614,7 +614,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
|
|||
scope.Stack.push(&stackvalue)
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
@ -659,7 +659,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
|
|||
scope.Stack.push(&stackvalue)
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
@ -709,7 +709,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
|
|||
}
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
@ -749,7 +749,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
|
|||
}
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
@ -782,7 +782,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
|
|||
}
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
@ -815,7 +815,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
|
|||
}
|
||||
|
||||
if interpreter.evm.Config.Tracer != nil {
|
||||
interpreter.evm.Config.Tracer.OnGasConsumed(scope.Contract.Gas, -returnGas, GasChangeCallLeftOverRefunded)
|
||||
interpreter.evm.Config.Tracer.OnGasChange(scope.Contract.Gas, scope.Contract.Gas+returnGas, GasChangeCallLeftOverRefunded)
|
||||
}
|
||||
|
||||
scope.Contract.Gas += returnGas
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ type EVMLogger interface {
|
|||
CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error)
|
||||
CaptureKeccakPreimage(hash common.Hash, data []byte)
|
||||
// Misc
|
||||
OnGasConsumed(gas, amount uint64, reason GasChangeReason)
|
||||
OnGasChange(old, new uint64, reason GasChangeReason)
|
||||
}
|
||||
|
||||
// GasChangeReason is used to indicate the reason for a gas change, useful
|
||||
|
|
|
|||
|
|
@ -745,10 +745,10 @@ func (f *Firehose) OnNewAccount(a common.Address) {
|
|||
})
|
||||
}
|
||||
|
||||
func (f *Firehose) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {
|
||||
func (f *Firehose) OnGasChange(old, new uint64, reason vm.GasChangeReason) {
|
||||
f.ensureInBlockAndInTrx()
|
||||
|
||||
if amount == 0 {
|
||||
if old == new {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -768,7 +768,7 @@ func (f *Firehose) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason)
|
|||
}
|
||||
|
||||
activeCall := f.callStack.Peek()
|
||||
change := f.newGasChange("tracer", gas, gas-amount, gasChangeReasonFromChain(reason))
|
||||
change := f.newGasChange("tracer", old, new, gasChangeReasonFromChain(reason))
|
||||
|
||||
// There is an initial gas consumption happening will the call is not yet started, we track it manually
|
||||
if activeCall == nil {
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ func (*AccessListTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64,
|
|||
|
||||
func (*AccessListTracer) CaptureKeccakPreimage(hash common.Hash, data []byte) {}
|
||||
|
||||
func (*AccessListTracer) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {}
|
||||
func (*AccessListTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}
|
||||
|
||||
func (*AccessListTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {}
|
||||
|
||||
|
|
|
|||
|
|
@ -219,7 +219,7 @@ func (l *StructLogger) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, s
|
|||
// CaptureKeccakPreimage is called during the KECCAK256 opcode.
|
||||
func (l *StructLogger) CaptureKeccakPreimage(hash common.Hash, data []byte) {}
|
||||
|
||||
func (l *StructLogger) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {}
|
||||
func (l *StructLogger) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}
|
||||
|
||||
// CaptureEnd is called after the call finishes to finalize the tracing.
|
||||
func (l *StructLogger) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
||||
|
|
@ -410,7 +410,7 @@ func (t *mdLogger) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope
|
|||
|
||||
func (t *mdLogger) CaptureKeccakPreimage(hash common.Hash, data []byte) {}
|
||||
|
||||
func (t *mdLogger) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {}
|
||||
func (t *mdLogger) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}
|
||||
|
||||
func (t *mdLogger) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
||||
fmt.Fprintf(t.out, "\nOutput: `%#x`\nConsumed gas: `%d`\nError: `%v`\n",
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ func (l *JSONLogger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, sco
|
|||
// CaptureKeccakPreimage is called during the KECCAK256 opcode.
|
||||
func (l *JSONLogger) CaptureKeccakPreimage(hash common.Hash, data []byte) {}
|
||||
|
||||
func (l *JSONLogger) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {}
|
||||
func (l *JSONLogger) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}
|
||||
|
||||
// CaptureEnd is triggered at end of execution.
|
||||
func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
||||
|
|
|
|||
|
|
@ -96,9 +96,9 @@ func (t *muxTracer) CaptureKeccakPreimage(hash common.Hash, data []byte) {
|
|||
}
|
||||
|
||||
// CaptureGasConsumed is called when gas is consumed.
|
||||
func (t *muxTracer) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {
|
||||
func (t *muxTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {
|
||||
for _, t := range t.tracers {
|
||||
t.OnGasConsumed(gas, amount, reason)
|
||||
t.OnGasChange(old, new, reason)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,8 +58,8 @@ func (t *NoopTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, _ *
|
|||
// CaptureKeccakPreimage is called during the KECCAK256 opcode.
|
||||
func (t *NoopTracer) CaptureKeccakPreimage(hash common.Hash, data []byte) {}
|
||||
|
||||
// OnGasConsumed is called when gas is consumed.
|
||||
func (t *NoopTracer) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {}
|
||||
// OnGasChange is called when gas is either consumed or refunded.
|
||||
func (t *NoopTracer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {}
|
||||
|
||||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
||||
func (t *NoopTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
||||
|
|
|
|||
|
|
@ -120,6 +120,6 @@ func (p *Printer) OnNewAccount(a common.Address) {
|
|||
fmt.Printf("OnNewAccount: a=%v\n", a)
|
||||
}
|
||||
|
||||
func (p *Printer) OnGasConsumed(gas, amount uint64, reason vm.GasChangeReason) {
|
||||
fmt.Printf("OnGasConsumed: gas=%v, amount=%v\n", gas, amount)
|
||||
func (p *Printer) OnGasChange(old, new uint64, reason vm.GasChangeReason) {
|
||||
fmt.Printf("OnGasChange: old=%v, new=%v, diff=%v\n", old, new, new-old)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue