api_tracer.go 27 KB

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