-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathjson_stream.go
More file actions
239 lines (223 loc) · 6.68 KB
/
json_stream.go
File metadata and controls
239 lines (223 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package logger
import (
"context"
"encoding/hex"
"github.com/holiman/uint256"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/execution/tracing"
"github.com/erigontech/erigon/execution/tracing/tracers"
"github.com/erigontech/erigon/execution/types"
"github.com/erigontech/erigon/execution/types/accounts"
"github.com/erigontech/erigon/execution/vm"
"github.com/erigontech/erigon/rpc/jsonstream"
)
// JsonStreamLogger is an EVM state logger and implements Tracer.
//
// JsonStreamLogger can capture state based on the given Log configuration and also keeps
// a track record of modified storage which is used in reporting snapshots of the
// contract their storage.
type JsonStreamLogger struct {
ctx context.Context
cfg LogConfig
stream jsonstream.Stream
hexEncodeBuf [128]byte
firstCapture bool
locations common.Hashes // For sorting
storage map[accounts.Address]Storage
env *tracing.VMContext
}
// NewStructLogger returns a new logger
func NewJsonStreamLogger(cfg *LogConfig, ctx context.Context, stream jsonstream.Stream) *JsonStreamLogger {
logger := &JsonStreamLogger{
ctx: ctx,
stream: stream,
storage: make(map[accounts.Address]Storage),
firstCapture: true,
}
if cfg != nil {
logger.cfg = *cfg
}
return logger
}
func (l *JsonStreamLogger) Tracer() *tracers.Tracer {
return &tracers.Tracer{
Hooks: &tracing.Hooks{
OnTxStart: l.OnTxStart,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
},
}
}
func (l *JsonStreamLogger) OnTxStart(env *tracing.VMContext, tx types.Transaction, from accounts.Address) {
l.env = env
}
// hexWithPrefix encodes b as a 0x-prefixed hex string using the internal buffer.
func (l *JsonStreamLogger) hexWithPrefix(b []byte) string {
l.hexEncodeBuf[0] = '0'
l.hexEncodeBuf[1] = 'x'
n := hex.Encode(l.hexEncodeBuf[2:], b)
return string(l.hexEncodeBuf[:2+n])
}
// formatMemoryWord encodes a memory chunk as a 0x-prefixed 64-char hex string,
// padding the last word to 32 bytes if needed.
func (l *JsonStreamLogger) formatMemoryWord(chunk []byte) string {
if len(chunk) == 32 {
return l.hexWithPrefix(chunk)
}
var word [32]byte
copy(word[:], chunk)
return l.hexWithPrefix(word[:])
}
func (l *JsonStreamLogger) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
// no log entry are producer
if l.firstCapture {
l.stream.WriteObjectStart()
l.stream.WriteObjectField("structLogs")
l.stream.WriteArrayStart()
}
}
// OnOpcode also tracks SLOAD/SSTORE ops to track storage change.
func (l *JsonStreamLogger) OnOpcode(pc uint64, typ byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
contractAddr := scope.Address()
memory := scope.MemoryData()
stack := scope.StackData()
op := vm.OpCode(typ)
select {
case <-l.ctx.Done():
return
default:
}
if !l.firstCapture {
l.stream.WriteMore()
} else {
l.stream.WriteObjectStart()
l.stream.WriteObjectField("structLogs")
l.stream.WriteArrayStart()
l.firstCapture = false
}
var outputStorage bool
if !l.cfg.DisableStorage {
// initialise new changed values storage container for this contract
// if not present.
if l.storage[contractAddr] == nil {
l.storage[contractAddr] = make(Storage)
}
// capture SLOAD opcodes and record the read entry in the local storage
if op == vm.SLOAD && len(stack) >= 1 {
var (
address = accounts.InternKey(stack[len(stack)-1].Bytes32())
value uint256.Int
)
value, _ = l.env.IntraBlockState.GetState(contractAddr, address)
l.storage[contractAddr][address.Value()] = value.Bytes32()
outputStorage = true
}
// capture SSTORE opcodes and record the written entry in the local storage.
if op == vm.SSTORE && len(stack) >= 2 {
var (
value = common.Hash(stack[len(stack)-2].Bytes32())
address = common.Hash(stack[len(stack)-1].Bytes32())
)
l.storage[contractAddr][address] = value
outputStorage = true
}
}
// create a new snapshot of the EVM.
l.stream.WriteObjectStart()
l.stream.WriteObjectField("pc")
l.stream.WriteUint64(pc)
l.stream.WriteMore()
l.stream.WriteObjectField("op")
l.stream.WriteString(op.String())
l.stream.WriteMore()
l.stream.WriteObjectField("gas")
l.stream.WriteUint64(gas)
l.stream.WriteMore()
l.stream.WriteObjectField("gasCost")
l.stream.WriteUint64(cost)
l.stream.WriteMore()
l.stream.WriteObjectField("depth")
l.stream.WriteInt(depth)
refund := l.env.IntraBlockState.GetRefund()
if refund.Total() != 0 {
l.stream.WriteMore()
l.stream.WriteObjectField("refund")
l.stream.WriteUint64(l.env.IntraBlockState.GetRefund().Total())
}
if err != nil {
l.stream.WriteMore()
l.stream.WriteObjectField("error")
l.stream.WriteString(err.Error())
}
if !l.cfg.DisableStack {
l.stream.WriteMore()
l.stream.WriteObjectField("stack")
l.stream.WriteArrayStart()
for i, stackValue := range stack {
if i > 0 {
l.stream.WriteMore()
}
l.stream.WriteString(stackValue.Hex())
}
l.stream.WriteArrayEnd()
}
if !l.cfg.DisableMemory {
memData := memory
l.stream.WriteMore()
l.stream.WriteObjectField("memory")
l.stream.WriteArrayStart()
for i := 0; i < len(memData); i += 32 {
end := i + 32
if end > len(memData) {
end = len(memData)
}
if i > 0 {
l.stream.WriteMore()
}
l.stream.WriteString(l.formatMemoryWord(memData[i:end]))
}
l.stream.WriteArrayEnd()
}
if outputStorage {
l.stream.WriteMore()
l.stream.WriteObjectField("storage")
l.stream.WriteObjectStart()
first := true
// Sort storage by locations for easier comparison with geth
if l.locations != nil {
l.locations = l.locations[:0]
}
s := l.storage[contractAddr]
for loc := range s {
l.locations = append(l.locations, loc)
}
l.locations.Sort()
for _, loc := range l.locations {
value := s[loc]
if first {
first = false
} else {
l.stream.WriteMore()
}
l.stream.WriteObjectField(l.hexWithPrefix(loc[:]))
l.stream.WriteString(l.hexWithPrefix(value[:]))
}
l.stream.WriteObjectEnd()
}
l.stream.WriteObjectEnd()
}