github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/api_tracer.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The Spectrum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eth
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"errors"
    23  	"fmt"
    24  	"github.com/SmartMeshFoundation/Spectrum/eth/tracers"
    25  	"github.com/SmartMeshFoundation/Spectrum/internal/ethapi"
    26  	"io/ioutil"
    27  	"runtime"
    28  	"sync"
    29  	"sync/atomic"
    30  	"time"
    31  
    32  	"github.com/SmartMeshFoundation/Spectrum/common"
    33  	"github.com/SmartMeshFoundation/Spectrum/common/hexutil"
    34  	"github.com/SmartMeshFoundation/Spectrum/core"
    35  	"github.com/SmartMeshFoundation/Spectrum/core/state"
    36  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    37  	"github.com/SmartMeshFoundation/Spectrum/core/vm"
    38  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    39  	"github.com/SmartMeshFoundation/Spectrum/log"
    40  	"github.com/SmartMeshFoundation/Spectrum/rlp"
    41  	"github.com/SmartMeshFoundation/Spectrum/rpc"
    42  	"github.com/SmartMeshFoundation/Spectrum/trie"
    43  )
    44  
    45  const (
    46  	// defaultTraceTimeout is the amount of time a single transaction can execute
    47  	// by default before being forcefully aborted.
    48  	defaultTraceTimeout = 5 * time.Second
    49  
    50  	// defaultTraceReexec is the number of blocks the tracer is willing to go back
    51  	// and reexecute to produce missing historical state necessary to run a specific
    52  	// trace.
    53  	defaultTraceReexec = uint64(128)
    54  )
    55  
    56  // TraceConfig holds extra parameters to trace functions.
    57  type TraceConfig struct {
    58  	*vm.LogConfig
    59  	Tracer  *string
    60  	Timeout *string
    61  	Reexec  *uint64
    62  }
    63  
    64  // txTraceResult is the result of a single transaction trace.
    65  type txTraceResult struct {
    66  	Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
    67  	Error  string      `json:"error,omitempty"`  // Trace failure produced by the tracer
    68  }
    69  
    70  // blockTraceTask represents a single block trace task when an entire chain is
    71  // being traced.
    72  type blockTraceTask struct {
    73  	statedb *state.StateDB   // Intermediate state prepped for tracing
    74  	block   *types.Block     // Block to trace the transactions from
    75  	results []*txTraceResult // Trace results procudes by the task
    76  }
    77  
    78  // blockTraceResult represets the results of tracing a single block when an entire
    79  // chain is being traced.
    80  type blockTraceResult struct {
    81  	Block  hexutil.Uint64   `json:"block"`  // Block number corresponding to this trace
    82  	Hash   common.Hash      `json:"hash"`   // Block hash corresponding to this trace
    83  	Traces []*txTraceResult `json:"traces"` // Trace results produced by the task
    84  }
    85  
    86  // txTraceTask represents a single transaction trace task when an entire block
    87  // is being traced.
    88  type txTraceTask struct {
    89  	statedb *state.StateDB // Intermediate state prepped for tracing
    90  	index   int            // Transaction offset in the block
    91  }
    92  
    93  // ephemeralDatabase is a memory wrapper around a proper database, which acts as
    94  // an ephemeral write layer. This construct is used by the chain tracer to write
    95  // state tries for intermediate blocks without serializing to disk, but at the
    96  // same time to allow disk fallback for reads that do no hit the memory layer.
    97  type ephemeralDatabase struct {
    98  	diskdb ethdb.Database     // Persistent disk database to fall back to with reads
    99  	memdb  *ethdb.MemDatabase // Ephemeral memory database for primary reads and writes
   100  }
   101  
   102  func (db *ephemeralDatabase) Put(key []byte, value []byte) error { return db.memdb.Put(key, value) }
   103  func (db *ephemeralDatabase) Delete(key []byte) error            { return errors.New("delete not supported") }
   104  func (db *ephemeralDatabase) Close()                             { db.memdb.Close() }
   105  func (db *ephemeralDatabase) NewBatch() ethdb.Batch {
   106  	return db.memdb.NewBatch()
   107  }
   108  func (db *ephemeralDatabase) Has(key []byte) (bool, error) {
   109  	if has, _ := db.memdb.Has(key); has {
   110  		return has, nil
   111  	}
   112  	return db.diskdb.Has(key)
   113  }
   114  func (db *ephemeralDatabase) Get(key []byte) ([]byte, error) {
   115  	if blob, _ := db.memdb.Get(key); blob != nil {
   116  		return blob, nil
   117  	}
   118  	return db.diskdb.Get(key)
   119  }
   120  
   121  // Prune does a state sync into a new memory write layer and replaces the old one.
   122  // This allows us to discard entries that are no longer referenced from the current
   123  // state.
   124  func (db *ephemeralDatabase) Prune(root common.Hash) {
   125  	// Pull the still relevant state data into memory
   126  	sync := state.NewStateSync(root, db.diskdb)
   127  	for sync.Pending() > 0 {
   128  		hash := sync.Missing(1)[0]
   129  
   130  		// Move the next trie node from the memory layer into a sync struct
   131  		node, err := db.memdb.Get(hash[:])
   132  		if err != nil {
   133  			panic(err) // memdb must have the data
   134  		}
   135  		if _, _, err := sync.Process([]trie.SyncResult{{Hash: hash, Data: node}}); err != nil {
   136  			panic(err) // it's not possible to fail processing a node
   137  		}
   138  	}
   139  	// Discard the old memory layer and write a new one
   140  	db.memdb, _ = ethdb.NewMemDatabaseWithCap(db.memdb.Len())
   141  	if _, err := sync.Commit(db); err != nil {
   142  		panic(err) // writing into a memdb cannot fail
   143  	}
   144  }
   145  
   146  // TraceChain returns the structured logs created during the execution of EVM
   147  // between two blocks (excluding start) and returns them as a JSON object.
   148  func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) {
   149  	// Fetch the block interval that we want to trace
   150  	var from, to *types.Block
   151  
   152  	switch start {
   153  	case rpc.PendingBlockNumber:
   154  		from = api.eth.miner.PendingBlock()
   155  	case rpc.LatestBlockNumber:
   156  		from = api.eth.blockchain.CurrentBlock()
   157  	default:
   158  		from = api.eth.blockchain.GetBlockByNumber(uint64(start))
   159  	}
   160  	switch end {
   161  	case rpc.PendingBlockNumber:
   162  		to = api.eth.miner.PendingBlock()
   163  	case rpc.LatestBlockNumber:
   164  		to = api.eth.blockchain.CurrentBlock()
   165  	default:
   166  		to = api.eth.blockchain.GetBlockByNumber(uint64(end))
   167  	}
   168  	// Trace the chain if we've found all our blocks
   169  	if from == nil {
   170  		return nil, fmt.Errorf("starting block #%d not found", start)
   171  	}
   172  	if to == nil {
   173  		return nil, fmt.Errorf("end block #%d not found", end)
   174  	}
   175  	return api.traceChain(ctx, from, to, config)
   176  }
   177  
   178  // traceChain configures a new tracer according to the provided configuration, and
   179  // executes all the transactions contained within. The return value will be one item
   180  // per transaction, dependent on the requestd tracer.
   181  func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Block, config *TraceConfig) (*rpc.Subscription, error) {
   182  	// Tracing a chain is a **long** operation, only do with subscriptions
   183  	notifier, supported := rpc.NotifierFromContext(ctx)
   184  	if !supported {
   185  		return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
   186  	}
   187  	sub := notifier.CreateSubscription()
   188  
   189  	// Ensure we have a valid starting state before doing any work
   190  	origin := start.NumberU64()
   191  
   192  	memdb, _ := ethdb.NewMemDatabase()
   193  	db := &ephemeralDatabase{
   194  		diskdb: api.eth.ChainDb(),
   195  		memdb:  memdb,
   196  	}
   197  	if number := start.NumberU64(); number > 0 {
   198  		start = api.eth.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1)
   199  		if start == nil {
   200  			return nil, fmt.Errorf("parent block #%d not found", number-1)
   201  		}
   202  	}
   203  	statedb, err := state.New(start.Root(), state.NewDatabase(db))
   204  	if err != nil {
   205  		// If the starting state is missing, allow some number of blocks to be reexecuted
   206  		reexec := defaultTraceReexec
   207  		if config != nil && config.Reexec != nil {
   208  			reexec = *config.Reexec
   209  		}
   210  		// Find the most recent block that has the state available
   211  		for i := uint64(0); i < reexec; i++ {
   212  			start = api.eth.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1)
   213  			if start == nil {
   214  				break
   215  			}
   216  			if statedb, err = state.New(start.Root(), state.NewDatabase(db)); err == nil {
   217  				break
   218  			}
   219  		}
   220  		// If we still don't have the state available, bail out
   221  		if err != nil {
   222  			switch err.(type) {
   223  			case *trie.MissingNodeError:
   224  				return nil, errors.New("required historical state unavailable")
   225  			default:
   226  				return nil, err
   227  			}
   228  		}
   229  	}
   230  	// Execute all the transaction contained within the chain concurrently for each block
   231  	blocks := int(end.NumberU64() - origin)
   232  
   233  	threads := runtime.NumCPU()
   234  	if threads > blocks {
   235  		threads = blocks
   236  	}
   237  	var (
   238  		pend    = new(sync.WaitGroup)
   239  		tasks   = make(chan *blockTraceTask, threads)
   240  		results = make(chan *blockTraceTask, threads)
   241  	)
   242  	for th := 0; th < threads; th++ {
   243  		pend.Add(1)
   244  		go func() {
   245  			defer pend.Done()
   246  
   247  			// Fetch and execute the next block trace tasks
   248  			for task := range tasks {
   249  				signer := types.MakeSigner(api.config, task.block.Number())
   250  
   251  				// Trace all the transactions contained within
   252  				for i, tx := range task.block.Transactions() {
   253  					msg, _ := tx.AsMessage(signer)
   254  					vmctx := core.NewEVMContext(msg, task.block.Header(), api.eth.blockchain, nil)
   255  					txctx := &tracers.Context{
   256  						BlockHash: task.block.Hash(),
   257  						TxIndex:   i,
   258  						TxHash:    tx.Hash(),
   259  					}
   260  					res, err := api.traceTx(ctx, msg, txctx, vmctx, task.statedb, config)
   261  					if err != nil {
   262  						task.results[i] = &txTraceResult{Error: err.Error()}
   263  						log.Warn("Tracing failed", "err", err)
   264  						break
   265  					}
   266  					task.statedb.DeleteSuicides()
   267  					task.results[i] = &txTraceResult{Result: res}
   268  				}
   269  				// Stream the result back to the user or abort on teardown
   270  				select {
   271  				case results <- task:
   272  				case <-notifier.Closed():
   273  					return
   274  				}
   275  			}
   276  		}()
   277  	}
   278  	// Start a goroutine to feed all the blocks into the tracers
   279  	begin := time.Now()
   280  	complete := start.NumberU64()
   281  
   282  	go func() {
   283  		var (
   284  			logged time.Time
   285  			number uint64
   286  			traced uint64
   287  			failed error
   288  		)
   289  		// Ensure everything is properly cleaned up on any exit path
   290  		defer func() {
   291  			close(tasks)
   292  			pend.Wait()
   293  
   294  			switch {
   295  			case failed != nil:
   296  				log.Warn("Chain tracing failed", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin), "err", failed)
   297  			case number < end.NumberU64():
   298  				log.Warn("Chain tracing aborted", "start", start.NumberU64(), "end", end.NumberU64(), "abort", number, "transactions", traced, "elapsed", time.Since(begin))
   299  			default:
   300  				log.Info("Chain tracing finished", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin))
   301  			}
   302  			close(results)
   303  		}()
   304  		// Feed all the blocks both into the tracer, as well as fast process concurrently
   305  		for number = start.NumberU64() + 1; number <= end.NumberU64(); number++ {
   306  			// Stop tracing if interruption was requested
   307  			select {
   308  			case <-notifier.Closed():
   309  				return
   310  			default:
   311  			}
   312  			// Print progress logs if long enough time elapsed
   313  			if time.Since(logged) > 8*time.Second {
   314  				if number > origin {
   315  					log.Info("Tracing chain segment", "start", origin, "end", end.NumberU64(), "current", number, "transactions", traced, "elapsed", time.Since(begin))
   316  				} else {
   317  					log.Info("Preparing state for chain trace", "block", number, "start", origin, "elapsed", time.Since(begin))
   318  				}
   319  				logged = time.Now()
   320  			}
   321  			// Retrieve the next block to trace
   322  			block := api.eth.blockchain.GetBlockByNumber(number)
   323  			if block == nil {
   324  				failed = fmt.Errorf("block #%d not found", number)
   325  				break
   326  			}
   327  			// Send the block over to the concurrent tracers (if not in the fast-forward phase)
   328  			if number > origin {
   329  				txs := block.Transactions()
   330  
   331  				select {
   332  				case tasks <- &blockTraceTask{statedb: statedb.Copy(), block: block, results: make([]*txTraceResult, len(txs))}:
   333  				case <-notifier.Closed():
   334  					return
   335  				}
   336  				traced += uint64(len(txs))
   337  			} else {
   338  				atomic.StoreUint64(&complete, number)
   339  			}
   340  			// Generate the next state snapshot fast without tracing
   341  			_, _, _, err := api.eth.blockchain.Processor().Process(block, statedb, vm.Config{})
   342  			if err != nil {
   343  				failed = err
   344  				break
   345  			}
   346  			// Finalize the state so any modifications are written to the trie
   347  			root, err := statedb.CommitTo(db, true)
   348  			if err != nil {
   349  				failed = err
   350  				break
   351  			}
   352  			if err := statedb.Reset(root); err != nil {
   353  				failed = err
   354  				break
   355  			}
   356  			// After every N blocks, prune the database to only retain relevant data
   357  			if (number-start.NumberU64())%4096 == 0 {
   358  				// Wait until currently pending trace jobs finish
   359  				for atomic.LoadUint64(&complete) != number {
   360  					select {
   361  					case <-time.After(100 * time.Millisecond):
   362  					case <-notifier.Closed():
   363  						return
   364  					}
   365  				}
   366  				// No more concurrent access at this point, prune the database
   367  				var (
   368  					nodes = db.memdb.Len()
   369  					start = time.Now()
   370  				)
   371  				db.Prune(root)
   372  				log.Info("Pruned tracer state entries", "deleted", nodes-db.memdb.Len(), "left", db.memdb.Len(), "elapsed", time.Since(start))
   373  
   374  				statedb, _ = state.New(root, state.NewDatabase(db))
   375  			}
   376  		}
   377  	}()
   378  
   379  	// Keep reading the trace results and stream the to the user
   380  	go func() {
   381  		var (
   382  			done = make(map[uint64]*blockTraceResult)
   383  			next = origin + 1
   384  		)
   385  		for res := range results {
   386  			// Queue up next received result
   387  			result := &blockTraceResult{
   388  				Block:  hexutil.Uint64(res.block.NumberU64()),
   389  				Hash:   res.block.Hash(),
   390  				Traces: res.results,
   391  			}
   392  			done[uint64(result.Block)] = result
   393  
   394  			// Stream completed traces to the user, aborting on the first error
   395  			for result, ok := done[next]; ok; result, ok = done[next] {
   396  				if len(result.Traces) > 0 || next == end.NumberU64() {
   397  					notifier.Notify(sub.ID, result)
   398  				}
   399  				atomic.StoreUint64(&complete, next)
   400  				delete(done, next)
   401  				next++
   402  			}
   403  		}
   404  	}()
   405  	return sub, nil
   406  }
   407  
   408  // TraceBlockByNumber returns the structured logs created during the execution of
   409  // EVM and returns them as a JSON object.
   410  func (api *PrivateDebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) {
   411  	// Fetch the block that we want to trace
   412  	var block *types.Block
   413  
   414  	switch number {
   415  	case rpc.PendingBlockNumber:
   416  		block = api.eth.miner.PendingBlock()
   417  	case rpc.LatestBlockNumber:
   418  		block = api.eth.blockchain.CurrentBlock()
   419  	default:
   420  		block = api.eth.blockchain.GetBlockByNumber(uint64(number))
   421  	}
   422  	// Trace the block if it was found
   423  	if block == nil {
   424  		return nil, fmt.Errorf("block #%d not found", number)
   425  	}
   426  	return api.traceBlock(ctx, block, config)
   427  }
   428  
   429  // TraceBlockByHash returns the structured logs created during the execution of
   430  // EVM and returns them as a JSON object.
   431  func (api *PrivateDebugAPI) TraceBlockByHash(ctx context.Context, hash common.Hash, config *TraceConfig) ([]*txTraceResult, error) {
   432  	block := api.eth.blockchain.GetBlockByHash(hash)
   433  	if block == nil {
   434  		return nil, fmt.Errorf("block #%x not found", hash)
   435  	}
   436  	return api.traceBlock(ctx, block, config)
   437  }
   438  
   439  // TraceBlock returns the structured logs created during the execution of EVM
   440  // and returns them as a JSON object.
   441  func (api *PrivateDebugAPI) TraceBlock(ctx context.Context, blob []byte, config *TraceConfig) ([]*txTraceResult, error) {
   442  	block := new(types.Block)
   443  	if err := rlp.Decode(bytes.NewReader(blob), block); err != nil {
   444  		return nil, fmt.Errorf("could not decode block: %v", err)
   445  	}
   446  	return api.traceBlock(ctx, block, config)
   447  }
   448  
   449  // TraceBlockFromFile returns the structured logs created during the execution of
   450  // EVM and returns them as a JSON object.
   451  func (api *PrivateDebugAPI) TraceBlockFromFile(ctx context.Context, file string, config *TraceConfig) ([]*txTraceResult, error) {
   452  	blob, err := ioutil.ReadFile(file)
   453  	if err != nil {
   454  		return nil, fmt.Errorf("could not read file: %v", err)
   455  	}
   456  	return api.TraceBlock(ctx, blob, config)
   457  }
   458  
   459  // traceBlock configures a new tracer according to the provided configuration, and
   460  // executes all the transactions contained within. The return value will be one item
   461  // per transaction, dependent on the requestd tracer.
   462  func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block, config *TraceConfig) ([]*txTraceResult, error) {
   463  	// Create the parent state database
   464  	if err := api.eth.engine.VerifyHeader(api.eth.blockchain, block.Header(), true); err != nil {
   465  		return nil, err
   466  	}
   467  	parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
   468  	if parent == nil {
   469  		return nil, fmt.Errorf("parent %x not found", block.ParentHash())
   470  	}
   471  	reexec := defaultTraceReexec
   472  	if config != nil && config.Reexec != nil {
   473  		reexec = *config.Reexec
   474  	}
   475  	statedb, err := api.computeStateDB(parent, reexec)
   476  	if err != nil {
   477  		return nil, err
   478  	}
   479  	// Execute all the transaction contained within the block concurrently
   480  	var (
   481  		signer = types.MakeSigner(api.config, block.Number())
   482  
   483  		txs     = block.Transactions()
   484  		results = make([]*txTraceResult, len(txs))
   485  
   486  		pend = new(sync.WaitGroup)
   487  		jobs = make(chan *txTraceTask, len(txs))
   488  	)
   489  	threads := runtime.NumCPU()
   490  	if threads > len(txs) {
   491  		threads = len(txs)
   492  	}
   493  	blockHash := block.Hash()
   494  	for th := 0; th < threads; th++ {
   495  		pend.Add(1)
   496  		go func() {
   497  			defer pend.Done()
   498  
   499  			// Fetch and execute the next transaction trace tasks
   500  			for task := range jobs {
   501  				msg, _ := txs[task.index].AsMessage(signer)
   502  				vmctx := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
   503  
   504  				txctx := &tracers.Context{
   505  					BlockHash: blockHash,
   506  					TxIndex:   task.index,
   507  					TxHash:    txs[task.index].Hash(),
   508  				}
   509  
   510  				res, err := api.traceTx(ctx, msg, txctx, vmctx, task.statedb, config)
   511  				if err != nil {
   512  					results[task.index] = &txTraceResult{Error: err.Error()}
   513  					continue
   514  				}
   515  				results[task.index] = &txTraceResult{Result: res}
   516  			}
   517  		}()
   518  	}
   519  	// Feed the transactions into the tracers and return
   520  	var failed error
   521  	for i, tx := range txs {
   522  		// Send the trace task over for execution
   523  		jobs <- &txTraceTask{statedb: statedb.Copy(), index: i}
   524  
   525  		// Generate the next state snapshot fast without tracing
   526  		msg, _ := tx.AsMessage(signer)
   527  		vmctx := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
   528  
   529  		vmenv := vm.NewEVM(vmctx, statedb, api.config, vm.Config{})
   530  		if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()), block.Number()); err != nil {
   531  			failed = err
   532  			break
   533  		}
   534  		// Finalize the state so any modifications are written to the trie
   535  		statedb.Finalise(true)
   536  	}
   537  	close(jobs)
   538  	pend.Wait()
   539  
   540  	// If execution failed in between, abort
   541  	if failed != nil {
   542  		return nil, failed
   543  	}
   544  	return results, nil
   545  }
   546  
   547  // computeStateDB retrieves the state database associated with a certain block.
   548  // If no state is locally available for the given block, a number of blocks are
   549  // attempted to be reexecuted to generate the desired state.
   550  func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*state.StateDB, error) {
   551  	// If we have the state fully available, use that
   552  	statedb, err := api.eth.blockchain.StateAt(block.Root())
   553  	if err == nil {
   554  		return statedb, nil
   555  	}
   556  	// Otherwise try to reexec blocks until we find a state or reach our limit
   557  	origin := block.NumberU64()
   558  
   559  	memdb, _ := ethdb.NewMemDatabase()
   560  	db := &ephemeralDatabase{
   561  		diskdb: api.eth.ChainDb(),
   562  		memdb:  memdb,
   563  	}
   564  	for i := uint64(0); i < reexec; i++ {
   565  		block = api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
   566  		if block == nil {
   567  			break
   568  		}
   569  		if statedb, err = state.New(block.Root(), state.NewDatabase(db)); err == nil {
   570  			break
   571  		}
   572  	}
   573  	if err != nil {
   574  		switch err.(type) {
   575  		case *trie.MissingNodeError:
   576  			return nil, errors.New("required historical state unavailable")
   577  		default:
   578  			return nil, err
   579  		}
   580  	}
   581  	// State was available at historical point, regenerate
   582  	var (
   583  		start  = time.Now()
   584  		logged time.Time
   585  	)
   586  	for block.NumberU64() < origin {
   587  		// Print progress logs if long enough time elapsed
   588  		if time.Since(logged) > 8*time.Second {
   589  			log.Info("Regenerating historical state", "block", block.NumberU64()+1, "target", origin, "elapsed", time.Since(start))
   590  			logged = time.Now()
   591  		}
   592  		// Retrieve the next block to regenerate and process it
   593  		if block = api.eth.blockchain.GetBlockByNumber(block.NumberU64() + 1); block == nil {
   594  			return nil, fmt.Errorf("block #%d not found", block.NumberU64()+1)
   595  		}
   596  		_, _, _, err := api.eth.blockchain.Processor().Process(block, statedb, vm.Config{})
   597  		if err != nil {
   598  			return nil, err
   599  		}
   600  		// Finalize the state so any modifications are written to the trie
   601  		root, err := statedb.CommitTo(db, true)
   602  		if err != nil {
   603  			return nil, err
   604  		}
   605  		if err := statedb.Reset(root); err != nil {
   606  			return nil, err
   607  		}
   608  		// After every N blocks, prune the database to only retain relevant data
   609  		if block.NumberU64()%4096 == 0 || block.NumberU64() == origin {
   610  			var (
   611  				nodes = db.memdb.Len()
   612  				begin = time.Now()
   613  			)
   614  			db.Prune(root)
   615  			log.Info("Pruned tracer state entries", "deleted", nodes-db.memdb.Len(), "left", db.memdb.Len(), "elapsed", time.Since(begin))
   616  
   617  			statedb, _ = state.New(root, state.NewDatabase(db))
   618  		}
   619  	}
   620  	log.Info("Historical state regenerated", "block", block.NumberU64(), "elapsed", time.Since(start))
   621  	return statedb, nil
   622  }
   623  
   624  // TraceTransaction returns the structured logs created during the execution of EVM
   625  // and returns them as a JSON object.
   626  func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) {
   627  	// Retrieve the transaction and assemble its EVM context
   628  	tx, blockHash, _, index := core.GetTransaction(api.eth.ChainDb(), hash)
   629  	if tx == nil {
   630  		return nil, fmt.Errorf("transaction %x not found", hash)
   631  	}
   632  	reexec := defaultTraceReexec
   633  	if config != nil && config.Reexec != nil {
   634  		reexec = *config.Reexec
   635  	}
   636  	msg, vmctx, statedb, err := api.computeTxEnv(blockHash, int(index), reexec)
   637  	if err != nil {
   638  		return nil, err
   639  	}
   640  	txctx := &tracers.Context{
   641  		BlockHash: blockHash,
   642  		TxIndex:   int(index),
   643  		TxHash:    hash,
   644  	}
   645  	// Trace the transaction and return
   646  	return api.traceTx(ctx, msg, txctx, vmctx, statedb, config)
   647  }
   648  
   649  // traceTx configures a new tracer according to the provided configuration, and
   650  // executes the given message in the provided environment. The return value will
   651  // be tracer dependent.
   652  func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, txctx *tracers.Context, vmctx vm.Context, statedb *state.StateDB, config *TraceConfig) (interface{}, error) {
   653  	// Assemble the structured logger or the JavaScript tracer
   654  	var (
   655  		tracer vm.EVMLogger
   656  		err    error
   657  	)
   658  	switch {
   659  	case config != nil && config.Tracer != nil:
   660  		// Define a meaningful timeout of a single transaction trace
   661  		timeout := defaultTraceTimeout
   662  		if config.Timeout != nil {
   663  			if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
   664  				return nil, err
   665  			}
   666  		}
   667  		if t, err := tracers.New(*config.Tracer, txctx); err != nil {
   668  			return nil, err
   669  		} else {
   670  			deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
   671  			go func() {
   672  				<-deadlineCtx.Done()
   673  				if deadlineCtx.Err() == context.DeadlineExceeded {
   674  					t.Stop(errors.New("execution timeout"))
   675  				}
   676  			}()
   677  			defer cancel()
   678  			tracer = t
   679  		}
   680  
   681  	case config == nil:
   682  		tracer = vm.NewStructLogger(nil)
   683  
   684  	default:
   685  		tracer = vm.NewStructLogger(config.LogConfig)
   686  	}
   687  	// Run the transaction with tracing enabled.
   688  	vmenv := vm.NewEVM(vmctx, statedb, api.config, vm.Config{Debug: true, Tracer: tracer})
   689  
   690  	ret, gas, failed, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), api.eth.BlockChain().CurrentBlock().Number())
   691  	if err != nil {
   692  		return nil, fmt.Errorf("tracing failed: %v", err)
   693  	}
   694  	// Depending on the tracer type, format and return the output
   695  	switch tracer := tracer.(type) {
   696  	case *vm.StructLogger:
   697  		return &ethapi.ExecutionResult{
   698  			Gas:         gas,
   699  			Failed:      failed,
   700  			ReturnValue: fmt.Sprintf("%x", ret),
   701  			StructLogs:  ethapi.FormatLogs(tracer.StructLogs()),
   702  		}, nil
   703  
   704  	case tracers.Tracer:
   705  		return tracer.GetResult()
   706  
   707  	default:
   708  		panic(fmt.Sprintf("bad tracer type %T", tracer))
   709  	}
   710  	return nil, nil
   711  }
   712  
   713  // computeTxEnv returns the execution environment of a certain transaction.
   714  func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, reexec uint64) (core.Message, vm.Context, *state.StateDB, error) {
   715  	// Create the parent state database
   716  	block := api.eth.blockchain.GetBlockByHash(blockHash)
   717  	if block == nil {
   718  		return nil, vm.Context{}, nil, fmt.Errorf("block %x not found", blockHash)
   719  	}
   720  	parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
   721  	if parent == nil {
   722  		return nil, vm.Context{}, nil, fmt.Errorf("parent %x not found", block.ParentHash())
   723  	}
   724  	statedb, err := api.computeStateDB(parent, reexec)
   725  	if err != nil {
   726  		return nil, vm.Context{}, nil, err
   727  	}
   728  	// Recompute transactions up to the target index.
   729  	signer := types.MakeSigner(api.config, block.Number())
   730  
   731  	for idx, tx := range block.Transactions() {
   732  		// Assemble the transaction call message and return if the requested offset
   733  		msg, _ := tx.AsMessage(signer)
   734  		context := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
   735  		if idx == txIndex {
   736  			return msg, context, statedb, nil
   737  		}
   738  		// Not yet the searched for transaction, execute on top of the current state
   739  		vmenv := vm.NewEVM(context, statedb, api.config, vm.Config{})
   740  		if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()), block.Number()); err != nil {
   741  			return nil, vm.Context{}, nil, fmt.Errorf("tx %x failed: %v", tx.Hash(), err)
   742  		}
   743  		statedb.DeleteSuicides()
   744  	}
   745  	return nil, vm.Context{}, nil, fmt.Errorf("tx index %d out of range for block %x", txIndex, blockHash)
   746  }