api_tracer.go 25 KB

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