Skip to content

Commit b019018

Browse files
authored
core/vm, internal/ethapi: tracer no full storage, nicer json output (#15499)
* core/vm, internal/ethapi: tracer no full storage, nicer json output * core/vm, internal/ethapi: omit disabled trace fields
1 parent 87f5b41 commit b019018

File tree

3 files changed

+36
-71
lines changed

3 files changed

+36
-71
lines changed

core/vm/logger.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type LogConfig struct {
4545
DisableMemory bool // disable memory capture
4646
DisableStack bool // disable stack capture
4747
DisableStorage bool // disable storage capture
48-
FullStorage bool // show full storage (slow)
4948
Limit int // maximum length of output, but zero means unlimited
5049
}
5150

@@ -136,41 +135,24 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
136135
)
137136
l.changedValues[contract.Address()][address] = value
138137
}
139-
// copy a snapstot of the current memory state to a new buffer
138+
// Copy a snapstot of the current memory state to a new buffer
140139
var mem []byte
141140
if !l.cfg.DisableMemory {
142141
mem = make([]byte, len(memory.Data()))
143142
copy(mem, memory.Data())
144143
}
145-
146-
// copy a snapshot of the current stack state to a new buffer
144+
// Copy a snapshot of the current stack state to a new buffer
147145
var stck []*big.Int
148146
if !l.cfg.DisableStack {
149147
stck = make([]*big.Int, len(stack.Data()))
150148
for i, item := range stack.Data() {
151149
stck[i] = new(big.Int).Set(item)
152150
}
153151
}
154-
155-
// Copy the storage based on the settings specified in the log config. If full storage
156-
// is disabled (default) we can use the simple Storage.Copy method, otherwise we use
157-
// the state object to query for all values (slow process).
152+
// Copy a snapshot of the current storage to a new container
158153
var storage Storage
159154
if !l.cfg.DisableStorage {
160-
if l.cfg.FullStorage {
161-
storage = make(Storage)
162-
// Get the contract account and loop over each storage entry. This may involve looping over
163-
// the trie and is a very expensive process.
164-
165-
env.StateDB.ForEachStorage(contract.Address(), func(key, value common.Hash) bool {
166-
storage[key] = value
167-
// Return true, indicating we'd like to continue.
168-
return true
169-
})
170-
} else {
171-
// copy a snapshot of the current storage to a new container.
172-
storage = l.changedValues[contract.Address()].Copy()
173-
}
155+
storage = l.changedValues[contract.Address()].Copy()
174156
}
175157
// create a new snaptshot of the EVM.
176158
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, storage, depth, err}

core/vm/logger_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,8 @@ func TestStoreCapture(t *testing.T) {
6363
if len(logger.changedValues[contract.Address()]) == 0 {
6464
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
6565
}
66-
6766
exp := common.BigToHash(big.NewInt(1))
6867
if logger.changedValues[contract.Address()][index] != exp {
6968
t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
7069
}
7170
}
72-
73-
func TestStorageCapture(t *testing.T) {
74-
t.Skip("implementing this function is difficult. it requires all sort of interfaces to be implemented which isn't trivial. The value (the actual test) isn't worth it")
75-
var (
76-
ref = &dummyContractRef{}
77-
contract = NewContract(ref, ref, new(big.Int), 0)
78-
env = NewEVM(Context{}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
79-
logger = NewStructLogger(nil)
80-
mem = NewMemory()
81-
stack = newstack()
82-
)
83-
84-
logger.CaptureState(env, 0, STOP, 0, 0, mem, stack, contract, 0, nil)
85-
if ref.calledForEach {
86-
t.Error("didn't expect for each to be called")
87-
}
88-
89-
logger = NewStructLogger(&LogConfig{FullStorage: true})
90-
logger.CaptureState(env, 0, STOP, 0, 0, mem, stack, contract, 0, nil)
91-
if !ref.calledForEach {
92-
t.Error("expected for each to be called")
93-
}
94-
}

internal/ethapi/api.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -710,45 +710,52 @@ type ExecutionResult struct {
710710
// StructLogRes stores a structured log emitted by the EVM while replaying a
711711
// transaction in debug mode
712712
type StructLogRes struct {
713-
Pc uint64 `json:"pc"`
714-
Op string `json:"op"`
715-
Gas uint64 `json:"gas"`
716-
GasCost uint64 `json:"gasCost"`
717-
Depth int `json:"depth"`
718-
Error error `json:"error"`
719-
Stack []string `json:"stack"`
720-
Memory []string `json:"memory"`
721-
Storage map[string]string `json:"storage"`
713+
Pc uint64 `json:"pc"`
714+
Op string `json:"op"`
715+
Gas uint64 `json:"gas"`
716+
GasCost uint64 `json:"gasCost"`
717+
Depth int `json:"depth"`
718+
Error error `json:"error,omitempty"`
719+
Stack *[]string `json:"stack,omitempty"`
720+
Memory *[]string `json:"memory,omitempty"`
721+
Storage *map[string]string `json:"storage,omitempty"`
722722
}
723723

724724
// formatLogs formats EVM returned structured logs for json output
725-
func FormatLogs(structLogs []vm.StructLog) []StructLogRes {
726-
formattedStructLogs := make([]StructLogRes, len(structLogs))
727-
for index, trace := range structLogs {
728-
formattedStructLogs[index] = StructLogRes{
725+
func FormatLogs(logs []vm.StructLog) []StructLogRes {
726+
formatted := make([]StructLogRes, len(logs))
727+
for index, trace := range logs {
728+
formatted[index] = StructLogRes{
729729
Pc: trace.Pc,
730730
Op: trace.Op.String(),
731731
Gas: trace.Gas,
732732
GasCost: trace.GasCost,
733733
Depth: trace.Depth,
734734
Error: trace.Err,
735-
Stack: make([]string, len(trace.Stack)),
736-
Storage: make(map[string]string),
737735
}
738-
739-
for i, stackValue := range trace.Stack {
740-
formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32))
736+
if trace.Stack != nil {
737+
stack := make([]string, len(trace.Stack))
738+
for i, stackValue := range trace.Stack {
739+
stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32))
740+
}
741+
formatted[index].Stack = &stack
741742
}
742-
743-
for i := 0; i+32 <= len(trace.Memory); i += 32 {
744-
formattedStructLogs[index].Memory = append(formattedStructLogs[index].Memory, fmt.Sprintf("%x", trace.Memory[i:i+32]))
743+
if trace.Memory != nil {
744+
memory := make([]string, 0, (len(trace.Memory)+31)/32)
745+
for i := 0; i+32 <= len(trace.Memory); i += 32 {
746+
memory = append(memory, fmt.Sprintf("%x", trace.Memory[i:i+32]))
747+
}
748+
formatted[index].Memory = &memory
745749
}
746-
747-
for i, storageValue := range trace.Storage {
748-
formattedStructLogs[index].Storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue)
750+
if trace.Storage != nil {
751+
storage := make(map[string]string)
752+
for i, storageValue := range trace.Storage {
753+
storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue)
754+
}
755+
formatted[index].Storage = &storage
749756
}
750757
}
751-
return formattedStructLogs
758+
return formatted
752759
}
753760

754761
// rpcOutputBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are

0 commit comments

Comments
 (0)