Skip to content

Commit 4c0883e

Browse files
holimanfjl
authored andcommitted
core/vm: adds refund as part of the json standard trace (#17910)
This adds the global accumulated refund counter to the standard json output as a numeric json value. Previously this was not very interesting since it was not used much, but with the new sstore gas changes the value is a lot more interesting from a consensus investigation perspective.
1 parent 3088c12 commit 4c0883e

6 files changed

Lines changed: 79 additions & 51 deletions

File tree

‎cmd/evm/json_logger.go‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create
4545
// CaptureState outputs state information on the logger.
4646
func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
4747
log := vm.StructLog{
48-
Pc: pc,
49-
Op: op,
50-
Gas: gas,
51-
GasCost: cost,
52-
MemorySize: memory.Len(),
53-
Storage: nil,
54-
Depth: depth,
55-
Err: err,
48+
Pc: pc,
49+
Op: op,
50+
Gas: gas,
51+
GasCost: cost,
52+
MemorySize: memory.Len(),
53+
Storage: nil,
54+
Depth: depth,
55+
RefundCounter: env.StateDB.GetRefund(),
56+
Err: err,
5657
}
5758
if !l.cfg.DisableMemory {
5859
log.Memory = memory.Data()

‎core/vm/gen_structlog.go‎

Lines changed: 30 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/vm/logger.go‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ type LogConfig struct {
5656
// StructLog is emitted to the EVM each cycle and lists information about the current internal state
5757
// prior to the execution of the statement.
5858
type StructLog struct {
59-
Pc uint64 `json:"pc"`
60-
Op OpCode `json:"op"`
61-
Gas uint64 `json:"gas"`
62-
GasCost uint64 `json:"gasCost"`
63-
Memory []byte `json:"memory"`
64-
MemorySize int `json:"memSize"`
65-
Stack []*big.Int `json:"stack"`
66-
Storage map[common.Hash]common.Hash `json:"-"`
67-
Depth int `json:"depth"`
68-
Err error `json:"-"`
59+
Pc uint64 `json:"pc"`
60+
Op OpCode `json:"op"`
61+
Gas uint64 `json:"gas"`
62+
GasCost uint64 `json:"gasCost"`
63+
Memory []byte `json:"memory"`
64+
MemorySize int `json:"memSize"`
65+
Stack []*big.Int `json:"stack"`
66+
Storage map[common.Hash]common.Hash `json:"-"`
67+
Depth int `json:"depth"`
68+
RefundCounter uint64 `json:"refund"`
69+
Err error `json:"-"`
6970
}
7071

7172
// overrides for gencodec
@@ -177,7 +178,7 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
177178
storage = l.changedValues[contract.Address()].Copy()
178179
}
179180
// create a new snaptshot of the EVM.
180-
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, storage, depth, err}
181+
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, storage, depth, env.StateDB.GetRefund(), err}
181182

182183
l.logs = append(l.logs, log)
183184
return nil

‎core/vm/logger_test.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/state"
2425
"github.com/ethereum/go-ethereum/params"
2526
)
2627

@@ -41,19 +42,23 @@ func (d *dummyContractRef) SetBalance(*big.Int) {}
4142
func (d *dummyContractRef) SetNonce(uint64) {}
4243
func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
4344

45+
type dummyStatedb struct {
46+
state.StateDB
47+
}
48+
49+
func (dummyStatedb) GetRefund() uint64 { return 1337 }
50+
4451
func TestStoreCapture(t *testing.T) {
4552
var (
46-
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
53+
env = NewEVM(Context{}, &dummyStatedb{}, params.TestChainConfig, Config{})
4754
logger = NewStructLogger(nil)
4855
mem = NewMemory()
4956
stack = newstack()
5057
contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
5158
)
5259
stack.push(big.NewInt(1))
5360
stack.push(big.NewInt(0))
54-
5561
var index common.Hash
56-
5762
logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
5863
if len(logger.changedValues[contract.Address()]) == 0 {
5964
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))

‎eth/tracers/tracer.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ type Tracer struct {
290290
contractWrapper *contractWrapper // Wrapper around the contract object
291291
dbWrapper *dbWrapper // Wrapper around the VM environment
292292

293-
pcValue *uint // Swappable pc value wrapped by a log accessor
294-
gasValue *uint // Swappable gas value wrapped by a log accessor
295-
costValue *uint // Swappable cost value wrapped by a log accessor
296-
depthValue *uint // Swappable depth value wrapped by a log accessor
297-
errorValue *string // Swappable error value wrapped by a log accessor
293+
pcValue *uint // Swappable pc value wrapped by a log accessor
294+
gasValue *uint // Swappable gas value wrapped by a log accessor
295+
costValue *uint // Swappable cost value wrapped by a log accessor
296+
depthValue *uint // Swappable depth value wrapped by a log accessor
297+
errorValue *string // Swappable error value wrapped by a log accessor
298+
refundValue *uint // Swappable refund value wrapped by a log accessor
298299

299300
ctx map[string]interface{} // Transaction context gathered throughout execution
300301
err error // Error, if one has occurred
@@ -323,6 +324,7 @@ func New(code string) (*Tracer, error) {
323324
gasValue: new(uint),
324325
costValue: new(uint),
325326
depthValue: new(uint),
327+
refundValue: new(uint),
326328
}
327329
// Set up builtins for this environment
328330
tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
@@ -442,6 +444,9 @@ func New(code string) (*Tracer, error) {
442444
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.depthValue); return 1 })
443445
tracer.vm.PutPropString(logObject, "getDepth")
444446

447+
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.refundValue); return 1 })
448+
tracer.vm.PutPropString(logObject, "getRefund")
449+
445450
tracer.vm.PushGoFunction(func(ctx *duktape.Context) int {
446451
if tracer.errorValue != nil {
447452
ctx.PushString(*tracer.errorValue)
@@ -527,6 +532,7 @@ func (jst *Tracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
527532
*jst.gasValue = uint(gas)
528533
*jst.costValue = uint(cost)
529534
*jst.depthValue = uint(depth)
535+
*jst.refundValue = uint(env.StateDB.GetRefund())
530536

531537
jst.errorValue = nil
532538
if err != nil {

‎eth/tracers/tracer_test.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/core/state"
2829
"github.com/ethereum/go-ethereum/core/vm"
2930
"github.com/ethereum/go-ethereum/params"
3031
)
@@ -43,8 +44,14 @@ func (account) ReturnGas(*big.Int) {}
4344
func (account) SetCode(common.Hash, []byte) {}
4445
func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
4546

47+
type dummyStatedb struct {
48+
state.StateDB
49+
}
50+
51+
func (dummyStatedb) GetRefund() uint64 { return 1337 }
52+
4653
func runTrace(tracer *Tracer) (json.RawMessage, error) {
47-
env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
54+
env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
4855

4956
contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
5057
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
@@ -126,7 +133,7 @@ func TestHaltBetweenSteps(t *testing.T) {
126133
t.Fatal(err)
127134
}
128135

129-
env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
136+
env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
130137
contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
131138

132139
tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)

0 commit comments

Comments
 (0)