Skip to content

Commit 3bbeb94

Browse files
authored
eth: make traceChain avoid OOM on long-running tracing (#23736)
This PR changes long-running chain tracing, so that it at some points releases the memory trie db, and switch over to a fresh disk-backed trie.
1 parent 53b94f1 commit 3bbeb94

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

eth/api_backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ func (b *EthAPIBackend) StartMining(threads int) error {
352352
return b.eth.StartMining(threads)
353353
}
354354

355-
func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error) {
356-
return b.eth.stateAtBlock(block, reexec, base, checkLive)
355+
func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error) {
356+
return b.eth.stateAtBlock(block, reexec, base, checkLive, preferDisk)
357357
}
358358

359359
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {

eth/state_accessor.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ import (
3535
// are attempted to be reexecuted to generate the desired state. The optional
3636
// base layer statedb can be passed then it's regarded as the statedb of the
3737
// parent block.
38-
func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (statedb *state.StateDB, err error) {
38+
// Parameters:
39+
// - block: The block for which we want the state (== state at the stateRoot of the parent)
40+
// - reexec: The maximum number of blocks to reprocess trying to obtain the desired state
41+
// - base: If the caller is tracing multiple blocks, the caller can provide the parent state
42+
// continuously from the callsite.
43+
// - checklive: if true, then the live 'blockchain' state database is used. If the caller want to
44+
// perform Commit or other 'save-to-disk' changes, this should be set to false to avoid
45+
// storing trash persistently
46+
// - preferDisk: this arg can be used by the caller to signal that even though the 'base' is provided,
47+
// it would be preferrable to start from a fresh state, if we have it on disk.
48+
func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (statedb *state.StateDB, err error) {
3949
var (
4050
current *types.Block
4151
database state.Database
@@ -50,6 +60,15 @@ func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state
5060
}
5161
}
5262
if base != nil {
63+
if preferDisk {
64+
// Create an ephemeral trie.Database for isolating the live one. Otherwise
65+
// the internal junks created by tracing will be persisted into the disk.
66+
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
67+
if statedb, err = state.New(block.Root(), database, nil); err == nil {
68+
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
69+
return statedb, nil
70+
}
71+
}
5372
// The optional base statedb is given, mark the start point as parent block
5473
statedb, database, report = base, base.Database(), false
5574
current = eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
@@ -152,7 +171,7 @@ func (eth *Ethereum) stateAtTransaction(block *types.Block, txIndex int, reexec
152171
}
153172
// Lookup the statedb of parent block from the live database,
154173
// otherwise regenerate it on the flight.
155-
statedb, err := eth.stateAtBlock(parent, reexec, nil, true)
174+
statedb, err := eth.stateAtBlock(parent, reexec, nil, true, false)
156175
if err != nil {
157176
return nil, vm.BlockContext{}, nil, err
158177
}

eth/tracers/api.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ const (
5353
// and reexecute to produce missing historical state necessary to run a specific
5454
// trace.
5555
defaultTraceReexec = uint64(128)
56+
57+
// defaultTracechainMemLimit is the size of the triedb, at which traceChain
58+
// switches over and tries to use a disk-backed database instead of building
59+
// on top of memory.
60+
// For non-archive nodes, this limit _will_ be overblown, as disk-backed tries
61+
// will only be found every ~15K blocks or so.
62+
defaultTracechainMemLimit = common.StorageSize(500 * 1024 * 1024)
5663
)
5764

5865
// Backend interface provides the common API services (that are provided by
@@ -67,7 +74,10 @@ type Backend interface {
6774
ChainConfig() *params.ChainConfig
6875
Engine() consensus.Engine
6976
ChainDb() ethdb.Database
70-
StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error)
77+
// StateAtBlock returns the state corresponding to the stateroot of the block.
78+
// N.B: For executing transactions on block N, the required stateRoot is block N-1,
79+
// so this method should be called with the parent.
80+
StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error)
7181
StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error)
7282
}
7383

@@ -320,6 +330,7 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config
320330
}
321331
close(results)
322332
}()
333+
var preferDisk bool
323334
// Feed all the blocks both into the tracer, as well as fast process concurrently
324335
for number = start.NumberU64(); number < end.NumberU64(); number++ {
325336
// Stop tracing if interruption was requested
@@ -349,18 +360,24 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config
349360
}
350361
// Prepare the statedb for tracing. Don't use the live database for
351362
// tracing to avoid persisting state junks into the database.
352-
statedb, err = api.backend.StateAtBlock(localctx, block, reexec, statedb, false)
363+
statedb, err = api.backend.StateAtBlock(localctx, block, reexec, statedb, false, preferDisk)
353364
if err != nil {
354365
failed = err
355366
break
356367
}
357-
if statedb.Database().TrieDB() != nil {
368+
if trieDb := statedb.Database().TrieDB(); trieDb != nil {
358369
// Hold the reference for tracer, will be released at the final stage
359-
statedb.Database().TrieDB().Reference(block.Root(), common.Hash{})
370+
trieDb.Reference(block.Root(), common.Hash{})
360371

361372
// Release the parent state because it's already held by the tracer
362373
if parent != (common.Hash{}) {
363-
statedb.Database().TrieDB().Dereference(parent)
374+
trieDb.Dereference(parent)
375+
}
376+
// Prefer disk if the trie db memory grows too much
377+
s1, s2 := trieDb.Size()
378+
if !preferDisk && (s1+s2) > defaultTracechainMemLimit {
379+
log.Info("Switching to prefer-disk mode for tracing", "size", s1+s2)
380+
preferDisk = true
364381
}
365382
}
366383
parent = block.Root()
@@ -496,7 +513,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
496513
if config != nil && config.Reexec != nil {
497514
reexec = *config.Reexec
498515
}
499-
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true)
516+
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
500517
if err != nil {
501518
return nil, err
502519
}
@@ -557,7 +574,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
557574
if config != nil && config.Reexec != nil {
558575
reexec = *config.Reexec
559576
}
560-
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true)
577+
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
561578
if err != nil {
562579
return nil, err
563580
}
@@ -646,7 +663,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
646663
if config != nil && config.Reexec != nil {
647664
reexec = *config.Reexec
648665
}
649-
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true)
666+
statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
650667
if err != nil {
651668
return nil, err
652669
}
@@ -810,7 +827,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
810827
if config != nil && config.Reexec != nil {
811828
reexec = *config.Reexec
812829
}
813-
statedb, err := api.backend.StateAtBlock(ctx, block, reexec, nil, true)
830+
statedb, err := api.backend.StateAtBlock(ctx, block, reexec, nil, true, false)
814831
if err != nil {
815832
return nil, err
816833
}

eth/tracers/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (b *testBackend) ChainDb() ethdb.Database {
138138
return b.chaindb
139139
}
140140

141-
func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error) {
141+
func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) {
142142
statedb, err := b.chain.StateAt(block.Root())
143143
if err != nil {
144144
return nil, errStateNotFound

eth/tracers/tracer.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,10 @@ func New(code string, ctx *Context) (*Tracer, error) {
553553
tracer.vm.Pop()
554554
hasExit := tracer.vm.GetPropString(tracer.tracerObject, "exit")
555555
tracer.vm.Pop()
556-
557556
if hasEnter != hasExit {
558557
return nil, fmt.Errorf("trace object must expose either both or none of enter() and exit()")
559558
}
560-
if !hasStep {
561-
// If there's no step function, the enter and exit must be present
562-
if !hasEnter {
563-
return nil, fmt.Errorf("trace object must expose either step() or both enter() and exit()")
564-
}
565-
}
566-
tracer.traceCallFrames = hasEnter
559+
tracer.traceCallFrames = hasEnter && hasExit
567560
tracer.traceSteps = hasStep
568561

569562
// Tracer is valid, inject the big int library to access large numbers

les/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (b *LesApiBackend) CurrentHeader() *types.Header {
324324
return b.eth.blockchain.CurrentHeader()
325325
}
326326

327-
func (b *LesApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error) {
327+
func (b *LesApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) {
328328
return b.eth.stateAtBlock(ctx, block, reexec)
329329
}
330330

0 commit comments

Comments
 (0)