Skip to content

Commit 92b4cb2

Browse files
MysticRyuujins1na
andauthored
eth/tracers/logger: conform structLog tracing to spec (ethereum#34093)
This is a breaking change in the opcode (structLog) tracer. Several fields will have a slight formatting difference to conform to the newly established spec at: ethereum/execution-apis#762. The differences include: - `memory`: words will have the 0x prefix. Also last word of memory will be padded to 32-bytes. - `storage`: keys and values will have the 0x prefix. --------- Co-authored-by: Sina M <1591639+s1na@users.noreply.github.com>
1 parent 3da517e commit 92b4cb2

7 files changed

Lines changed: 269 additions & 65 deletions

File tree

eth/api_backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ func (b *EthAPIBackend) CurrentHeader() *types.Header {
484484
return b.eth.blockchain.CurrentHeader()
485485
}
486486

487-
func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) {
488-
return b.eth.stateAtBlock(ctx, block, reexec, base, readOnly, preferDisk)
487+
func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) {
488+
return b.eth.stateAtBlock(ctx, block, base, readOnly, preferDisk)
489489
}
490490

491-
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
492-
return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
491+
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
492+
return b.eth.stateAtTransaction(ctx, block, txIndex)
493493
}
494494

495495
func (b *EthAPIBackend) RPCTxSyncDefaultTimeout() time.Duration {

eth/api_debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (api *DebugAPI) StorageRangeAt(ctx context.Context, blockNrOrHash rpc.Block
222222
if block == nil {
223223
return StorageRangeResult{}, fmt.Errorf("block %v not found", blockNrOrHash)
224224
}
225-
_, _, statedb, release, err := api.eth.stateAtTransaction(ctx, block, txIndex, 0)
225+
_, _, statedb, release, err := api.eth.stateAtTransaction(ctx, block, txIndex)
226226
if err != nil {
227227
return StorageRangeResult{}, err
228228
}

eth/state_accessor.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ import (
3838
// for releasing state.
3939
var noopReleaser = tracers.StateReleaseFunc(func() {})
4040

41-
func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
41+
// reexecLimit is the maximum number of ancestor blocks to walk back when
42+
// attempting to reconstruct missing historical state for hash-scheme nodes.
43+
const reexecLimit = uint64(128)
44+
45+
func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
4246
var (
4347
current *types.Block
4448
database state.Database
@@ -99,7 +103,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
99103
}
100104
}
101105
// Database does not have the state for the given block, try to regenerate
102-
for i := uint64(0); i < reexec; i++ {
106+
for i := uint64(0); i < reexecLimit; i++ {
103107
if err := ctx.Err(); err != nil {
104108
return nil, nil, err
105109
}
@@ -120,7 +124,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
120124
if err != nil {
121125
switch err.(type) {
122126
case *trie.MissingNodeError:
123-
return nil, nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexec)
127+
return nil, nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexecLimit)
124128
default:
125129
return nil, nil, err
126130
}
@@ -190,10 +194,9 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
190194
}
191195

192196
// stateAtBlock retrieves the state database associated with a certain block.
193-
// If no state is locally available for the given block, a number of blocks
194-
// are attempted to be reexecuted to generate the desired state. The optional
195-
// base layer statedb can be provided which is regarded as the statedb of the
196-
// parent block.
197+
// If no state is locally available for the given block, up to reexecLimit ancestor
198+
// blocks are reexecuted to generate the desired state. The optional base layer
199+
// statedb can be provided which is regarded as the statedb of the parent block.
197200
//
198201
// An additional release function will be returned if the requested state is
199202
// available. Release is expected to be invoked when the returned state is no
@@ -202,7 +205,6 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
202205
//
203206
// Parameters:
204207
// - block: The block for which we want the state(state = block.Root)
205-
// - reexec: The maximum number of blocks to reprocess trying to obtain the desired state
206208
// - base: If the caller is tracing multiple blocks, the caller can provide the parent
207209
// state continuously from the callsite.
208210
// - readOnly: If true, then the live 'blockchain' state database is used. No mutation should
@@ -211,9 +213,9 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
211213
// - preferDisk: This arg can be used by the caller to signal that even though the 'base' is
212214
// provided, it would be preferable to start from a fresh state, if we have it
213215
// on disk.
214-
func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
216+
func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) {
215217
if eth.blockchain.TrieDB().Scheme() == rawdb.HashScheme {
216-
return eth.hashState(ctx, block, reexec, base, readOnly, preferDisk)
218+
return eth.hashState(ctx, block, base, readOnly, preferDisk)
217219
}
218220
return eth.pathState(block)
219221
}
@@ -225,7 +227,7 @@ func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, reexe
225227
// function will return the state of block after the pre-block operations have
226228
// been completed (e.g. updating system contracts), but before post-block
227229
// operations are completed (e.g. processing withdrawals).
228-
func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
230+
func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
229231
// Short circuit if it's genesis block.
230232
if block.NumberU64() == 0 {
231233
return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis")
@@ -237,7 +239,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
237239
}
238240
// Lookup the statedb of parent block from the live database,
239241
// otherwise regenerate it on the flight.
240-
statedb, release, err := eth.stateAtBlock(ctx, parent, reexec, nil, true, false)
242+
statedb, release, err := eth.stateAtBlock(ctx, parent, nil, true, false)
241243
if err != nil {
242244
return nil, vm.BlockContext{}, nil, nil, err
243245
}

eth/tracers/api.go

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ const (
5151
// by default before being forcefully aborted.
5252
defaultTraceTimeout = 5 * time.Second
5353

54-
// defaultTraceReexec is the number of blocks the tracer is willing to go back
55-
// and reexecute to produce missing historical state necessary to run a specific
56-
// trace.
57-
defaultTraceReexec = uint64(128)
58-
5954
// defaultTracechainMemLimit is the size of the triedb, at which traceChain
6055
// switches over and tries to use a disk-backed database instead of building
6156
// on top of memory.
@@ -89,8 +84,8 @@ type Backend interface {
8984
ChainConfig() *params.ChainConfig
9085
Engine() consensus.Engine
9186
ChainDb() ethdb.Database
92-
StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error)
93-
StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, StateReleaseFunc, error)
87+
StateAtBlock(ctx context.Context, block *types.Block, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error)
88+
StateAtTransaction(ctx context.Context, block *types.Block, txIndex int) (*types.Transaction, vm.BlockContext, *state.StateDB, StateReleaseFunc, error)
9489
}
9590

9691
// API is the collection of tracing APIs exposed over the private debugging endpoint.
@@ -156,7 +151,6 @@ type TraceConfig struct {
156151
*logger.Config
157152
Tracer *string
158153
Timeout *string
159-
Reexec *uint64
160154
// Config specific to given tracer. Note struct logger
161155
// config are historically embedded in main object.
162156
TracerConfig json.RawMessage
@@ -174,7 +168,6 @@ type TraceCallConfig struct {
174168
// StdTraceConfig holds extra parameters to standard-json trace functions.
175169
type StdTraceConfig struct {
176170
logger.Config
177-
Reexec *uint64
178171
TxHash common.Hash
179172
}
180173

@@ -245,10 +238,6 @@ func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, conf
245238
// transaction, dependent on the requested tracer.
246239
// The tracing procedure should be aborted in case the closed signal is received.
247240
func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed <-chan error) chan *blockTraceResult {
248-
reexec := defaultTraceReexec
249-
if config != nil && config.Reexec != nil {
250-
reexec = *config.Reexec
251-
}
252241
blocks := int(end.NumberU64() - start.NumberU64())
253242
threads := runtime.NumCPU()
254243
if threads > blocks {
@@ -374,7 +363,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed
374363
s1, s2, s3 := statedb.Database().TrieDB().Size()
375364
preferDisk = s1+s2+s3 > defaultTracechainMemLimit
376365
}
377-
statedb, release, err = api.backend.StateAtBlock(ctx, block, reexec, statedb, false, preferDisk)
366+
statedb, release, err = api.backend.StateAtBlock(ctx, block, statedb, false, preferDisk)
378367
if err != nil {
379368
failed = err
380369
break
@@ -522,11 +511,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
522511
if err != nil {
523512
return nil, err
524513
}
525-
reexec := defaultTraceReexec
526-
if config != nil && config.Reexec != nil {
527-
reexec = *config.Reexec
528-
}
529-
statedb, release, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
514+
statedb, release, err := api.backend.StateAtBlock(ctx, parent, nil, true, false)
530515
if err != nil {
531516
return nil, err
532517
}
@@ -591,11 +576,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
591576
if err != nil {
592577
return nil, err
593578
}
594-
reexec := defaultTraceReexec
595-
if config != nil && config.Reexec != nil {
596-
reexec = *config.Reexec
597-
}
598-
statedb, release, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
579+
statedb, release, err := api.backend.StateAtBlock(ctx, parent, nil, true, false)
599580
if err != nil {
600581
return nil, err
601582
}
@@ -743,11 +724,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
743724
if err != nil {
744725
return nil, err
745726
}
746-
reexec := defaultTraceReexec
747-
if config != nil && config.Reexec != nil {
748-
reexec = *config.Reexec
749-
}
750-
statedb, release, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
727+
statedb, release, err := api.backend.StateAtBlock(ctx, parent, nil, true, false)
751728
if err != nil {
752729
return nil, err
753730
}
@@ -877,15 +854,11 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *
877854
if blockNumber == 0 {
878855
return nil, errors.New("genesis is not traceable")
879856
}
880-
reexec := defaultTraceReexec
881-
if config != nil && config.Reexec != nil {
882-
reexec = *config.Reexec
883-
}
884857
block, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(blockNumber), blockHash)
885858
if err != nil {
886859
return nil, err
887860
}
888-
tx, vmctx, statedb, release, err := api.backend.StateAtTransaction(ctx, block, int(index), reexec)
861+
tx, vmctx, statedb, release, err := api.backend.StateAtTransaction(ctx, block, int(index))
889862
if err != nil {
890863
return nil, err
891864
}
@@ -939,15 +912,10 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
939912
return nil, err
940913
}
941914
// try to recompute the state
942-
reexec := defaultTraceReexec
943-
if config != nil && config.Reexec != nil {
944-
reexec = *config.Reexec
945-
}
946-
947915
if config != nil && config.TxIndex != nil {
948-
_, _, statedb, release, err = api.backend.StateAtTransaction(ctx, block, int(*config.TxIndex), reexec)
916+
_, _, statedb, release, err = api.backend.StateAtTransaction(ctx, block, int(*config.TxIndex))
949917
} else {
950-
statedb, release, err = api.backend.StateAtBlock(ctx, block, reexec, nil, true, false)
918+
statedb, release, err = api.backend.StateAtBlock(ctx, block, nil, true, false)
951919
}
952920
if err != nil {
953921
return nil, err

0 commit comments

Comments
 (0)