api_tracer.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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
  371. // EVM against a block pulled from the pool of bad ones and returns them as a JSON
  372. // object.
  373. func (api *PrivateDebugAPI) TraceBadBlock(ctx context.Context, hash common.Hash, config *TraceConfig) ([]*txTraceResult, error) {
  374. blocks := api.eth.blockchain.BadBlocks()
  375. for _, block := range blocks {
  376. if block.Hash() == hash {
  377. return api.traceBlock(ctx, block, config)
  378. }
  379. }
  380. return nil, fmt.Errorf("bad block %#x not found", hash)
  381. }
  382. // StandardTraceBlockToFile dumps the structured logs created during the
  383. // execution of EVM to the local file system and returns a list of files
  384. // to the caller.
  385. func (api *PrivateDebugAPI) StandardTraceBlockToFile(ctx context.Context, hash common.Hash, config *StdTraceConfig) ([]string, error) {
  386. block := api.eth.blockchain.GetBlockByHash(hash)
  387. if block == nil {
  388. return nil, fmt.Errorf("block %#x not found", hash)
  389. }
  390. return api.standardTraceBlockToFile(ctx, block, config)
  391. }
  392. // StandardTraceBadBlockToFile dumps the structured logs created during the
  393. // execution of EVM against a block pulled from the pool of bad ones to the
  394. // local file system and returns a list of files to the caller.
  395. func (api *PrivateDebugAPI) StandardTraceBadBlockToFile(ctx context.Context, hash common.Hash, config *StdTraceConfig) ([]string, error) {
  396. blocks := api.eth.blockchain.BadBlocks()
  397. for _, block := range blocks {
  398. if block.Hash() == hash {
  399. return api.standardTraceBlockToFile(ctx, block, config)
  400. }
  401. }
  402. return nil, fmt.Errorf("bad block %#x not found", hash)
  403. }
  404. // traceBlock configures a new tracer according to the provided configuration, and
  405. // executes all the transactions contained within. The return value will be one item
  406. // per transaction, dependent on the requestd tracer.
  407. func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block, config *TraceConfig) ([]*txTraceResult, error) {
  408. // Create the parent state database
  409. if err := api.eth.engine.VerifyHeader(api.eth.blockchain, block.Header(), true); err != nil {
  410. return nil, err
  411. }
  412. parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
  413. if parent == nil {
  414. return nil, fmt.Errorf("parent %#x not found", block.ParentHash())
  415. }
  416. reexec := defaultTraceReexec
  417. if config != nil && config.Reexec != nil {
  418. reexec = *config.Reexec
  419. }
  420. statedb, err := api.computeStateDB(parent, reexec)
  421. if err != nil {
  422. return nil, err
  423. }
  424. // Execute all the transaction contained within the block concurrently
  425. var (
  426. signer = types.MakeSigner(api.config, block.Number())
  427. txs = block.Transactions()
  428. results = make([]*txTraceResult, len(txs))
  429. pend = new(sync.WaitGroup)
  430. jobs = make(chan *txTraceTask, len(txs))
  431. )
  432. threads := runtime.NumCPU()
  433. if threads > len(txs) {
  434. threads = len(txs)
  435. }
  436. for th := 0; th < threads; th++ {
  437. pend.Add(1)
  438. go func() {
  439. defer pend.Done()
  440. // Fetch and execute the next transaction trace tasks
  441. for task := range jobs {
  442. msg, _ := txs[task.index].AsMessage(signer)
  443. vmctx := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
  444. res, err := api.traceTx(ctx, msg, vmctx, task.statedb, config)
  445. if err != nil {
  446. results[task.index] = &txTraceResult{Error: err.Error()}
  447. continue
  448. }
  449. results[task.index] = &txTraceResult{Result: res}
  450. }
  451. }()
  452. }
  453. // Feed the transactions into the tracers and return
  454. var failed error
  455. for i, tx := range txs {
  456. // Send the trace task over for execution
  457. jobs <- &txTraceTask{statedb: statedb.Copy(), index: i}
  458. // Generate the next state snapshot fast without tracing
  459. msg, _ := tx.AsMessage(signer)
  460. vmctx := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
  461. vmenv := vm.NewEVM(vmctx, statedb, api.config, vm.Config{})
  462. if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil {
  463. failed = err
  464. break
  465. }
  466. // Finalize the state so any modifications are written to the trie
  467. statedb.Finalise(true)
  468. }
  469. close(jobs)
  470. pend.Wait()
  471. // If execution failed in between, abort
  472. if failed != nil {
  473. return nil, failed
  474. }
  475. return results, nil
  476. }
  477. // standardTraceBlockToFile configures a new tracer which uses standard JSON output,
  478. // and traces either a full block or an individual transaction. The return value will
  479. // be one filename per transaction traced.
  480. func (api *PrivateDebugAPI) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) {
  481. // If we're tracing a single transaction, make sure it's present
  482. if config != nil && config.TxHash != (common.Hash{}) {
  483. var exists bool
  484. for _, tx := range block.Transactions() {
  485. if exists = (tx.Hash() == config.TxHash); exists {
  486. break
  487. }
  488. }
  489. if !exists {
  490. return nil, fmt.Errorf("transaction %#x not found in block", config.TxHash)
  491. }
  492. }
  493. // Create the parent state database
  494. if err := api.eth.engine.VerifyHeader(api.eth.blockchain, block.Header(), true); err != nil {
  495. return nil, err
  496. }
  497. parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
  498. if parent == nil {
  499. return nil, fmt.Errorf("parent %#x not found", block.ParentHash())
  500. }
  501. reexec := defaultTraceReexec
  502. if config != nil && config.Reexec != nil {
  503. reexec = *config.Reexec
  504. }
  505. statedb, err := api.computeStateDB(parent, reexec)
  506. if err != nil {
  507. return nil, err
  508. }
  509. // Retrieve the tracing configurations, or use default values
  510. var (
  511. logConfig vm.LogConfig
  512. txHash common.Hash
  513. )
  514. if config != nil {
  515. if config.LogConfig != nil {
  516. logConfig = *config.LogConfig
  517. }
  518. txHash = config.TxHash
  519. }
  520. logConfig.Debug = true
  521. // Execute transaction, either tracing all or just the requested one
  522. var (
  523. signer = types.MakeSigner(api.config, block.Number())
  524. dumps []string
  525. )
  526. for i, tx := range block.Transactions() {
  527. // Prepare the trasaction for un-traced execution
  528. var (
  529. msg, _ = tx.AsMessage(signer)
  530. vmctx = core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
  531. vmConf vm.Config
  532. dump *os.File
  533. err error
  534. )
  535. // If the transaction needs tracing, swap out the configs
  536. if tx.Hash() == txHash || txHash == (common.Hash{}) {
  537. // Generate a unique temporary file to dump it into
  538. prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
  539. dump, err = ioutil.TempFile(os.TempDir(), prefix)
  540. if err != nil {
  541. return nil, err
  542. }
  543. dumps = append(dumps, dump.Name())
  544. // Swap out the noop logger to the standard tracer
  545. vmConf = vm.Config{
  546. Debug: true,
  547. Tracer: vm.NewJSONLogger(&logConfig, bufio.NewWriter(dump)),
  548. EnablePreimageRecording: true,
  549. }
  550. }
  551. // Execute the transaction and flush any traces to disk
  552. vmenv := vm.NewEVM(vmctx, statedb, api.config, vmConf)
  553. _, _, _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
  554. if dump != nil {
  555. dump.Close()
  556. log.Info("Wrote standard trace", "file", dump.Name())
  557. }
  558. if err != nil {
  559. return dumps, err
  560. }
  561. // Finalize the state so any modifications are written to the trie
  562. statedb.Finalise(true)
  563. // If we've traced the transaction we were looking for, abort
  564. if tx.Hash() == txHash {
  565. break
  566. }
  567. }
  568. return dumps, nil
  569. }
  570. // computeStateDB retrieves the state database associated with a certain block.
  571. // If no state is locally available for the given block, a number of blocks are
  572. // attempted to be reexecuted to generate the desired state.
  573. func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*state.StateDB, error) {
  574. // If we have the state fully available, use that
  575. statedb, err := api.eth.blockchain.StateAt(block.Root())
  576. if err == nil {
  577. return statedb, nil
  578. }
  579. // Otherwise try to reexec blocks until we find a state or reach our limit
  580. origin := block.NumberU64()
  581. database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16)
  582. for i := uint64(0); i < reexec; i++ {
  583. block = api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
  584. if block == nil {
  585. break
  586. }
  587. if statedb, err = state.New(block.Root(), database); err == nil {
  588. break
  589. }
  590. }
  591. if err != nil {
  592. switch err.(type) {
  593. case *trie.MissingNodeError:
  594. return nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexec)
  595. default:
  596. return nil, err
  597. }
  598. }
  599. // State was available at historical point, regenerate
  600. var (
  601. start = time.Now()
  602. logged time.Time
  603. proot common.Hash
  604. )
  605. for block.NumberU64() < origin {
  606. // Print progress logs if long enough time elapsed
  607. if time.Since(logged) > 8*time.Second {
  608. log.Info("Regenerating historical state", "block", block.NumberU64()+1, "target", origin, "remaining", origin-block.NumberU64()-1, "elapsed", time.Since(start))
  609. logged = time.Now()
  610. }
  611. // Retrieve the next block to regenerate and process it
  612. if block = api.eth.blockchain.GetBlockByNumber(block.NumberU64() + 1); block == nil {
  613. return nil, fmt.Errorf("block #%d not found", block.NumberU64()+1)
  614. }
  615. _, _, _, err := api.eth.blockchain.Processor().Process(block, statedb, vm.Config{})
  616. if err != nil {
  617. return nil, fmt.Errorf("processing block %d failed: %v", block.NumberU64(), err)
  618. }
  619. // Finalize the state so any modifications are written to the trie
  620. root, err := statedb.Commit(api.eth.blockchain.Config().IsEIP158(block.Number()))
  621. if err != nil {
  622. return nil, err
  623. }
  624. if err := statedb.Reset(root); err != nil {
  625. return nil, fmt.Errorf("state reset after block %d failed: %v", block.NumberU64(), err)
  626. }
  627. database.TrieDB().Reference(root, common.Hash{})
  628. if proot != (common.Hash{}) {
  629. database.TrieDB().Dereference(proot)
  630. }
  631. proot = root
  632. }
  633. nodes, imgs := database.TrieDB().Size()
  634. log.Info("Historical state regenerated", "block", block.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs)
  635. return statedb, nil
  636. }
  637. // TraceTransaction returns the structured logs created during the execution of EVM
  638. // and returns them as a JSON object.
  639. func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) {
  640. // Retrieve the transaction and assemble its EVM context
  641. tx, blockHash, _, index := rawdb.ReadTransaction(api.eth.ChainDb(), hash)
  642. if tx == nil {
  643. return nil, fmt.Errorf("transaction %#x not found", hash)
  644. }
  645. reexec := defaultTraceReexec
  646. if config != nil && config.Reexec != nil {
  647. reexec = *config.Reexec
  648. }
  649. msg, vmctx, statedb, err := api.computeTxEnv(blockHash, int(index), reexec)
  650. if err != nil {
  651. return nil, err
  652. }
  653. // Trace the transaction and return
  654. return api.traceTx(ctx, msg, vmctx, statedb, config)
  655. }
  656. // traceTx configures a new tracer according to the provided configuration, and
  657. // executes the given message in the provided environment. The return value will
  658. // be tracer dependent.
  659. func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, vmctx vm.Context, statedb *state.StateDB, config *TraceConfig) (interface{}, error) {
  660. // Assemble the structured logger or the JavaScript tracer
  661. var (
  662. tracer vm.Tracer
  663. err error
  664. )
  665. switch {
  666. case config != nil && config.Tracer != nil:
  667. // Define a meaningful timeout of a single transaction trace
  668. timeout := defaultTraceTimeout
  669. if config.Timeout != nil {
  670. if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
  671. return nil, err
  672. }
  673. }
  674. // Constuct the JavaScript tracer to execute with
  675. if tracer, err = tracers.New(*config.Tracer); err != nil {
  676. return nil, err
  677. }
  678. // Handle timeouts and RPC cancellations
  679. deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
  680. go func() {
  681. <-deadlineCtx.Done()
  682. tracer.(*tracers.Tracer).Stop(errors.New("execution timeout"))
  683. }()
  684. defer cancel()
  685. case config == nil:
  686. tracer = vm.NewStructLogger(nil)
  687. default:
  688. tracer = vm.NewStructLogger(config.LogConfig)
  689. }
  690. // Run the transaction with tracing enabled.
  691. vmenv := vm.NewEVM(vmctx, statedb, api.config, vm.Config{Debug: true, Tracer: tracer})
  692. ret, gas, failed, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()))
  693. if err != nil {
  694. return nil, fmt.Errorf("tracing failed: %v", err)
  695. }
  696. // Depending on the tracer type, format and return the output
  697. switch tracer := tracer.(type) {
  698. case *vm.StructLogger:
  699. return &ethapi.ExecutionResult{
  700. Gas: gas,
  701. Failed: failed,
  702. ReturnValue: fmt.Sprintf("%x", ret),
  703. StructLogs: ethapi.FormatLogs(tracer.StructLogs()),
  704. }, nil
  705. case *tracers.Tracer:
  706. return tracer.GetResult()
  707. default:
  708. panic(fmt.Sprintf("bad tracer type %T", tracer))
  709. }
  710. }
  711. // computeTxEnv returns the execution environment of a certain transaction.
  712. func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, reexec uint64) (core.Message, vm.Context, *state.StateDB, error) {
  713. // Create the parent state database
  714. block := api.eth.blockchain.GetBlockByHash(blockHash)
  715. if block == nil {
  716. return nil, vm.Context{}, nil, fmt.Errorf("block %#x not found", blockHash)
  717. }
  718. parent := api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
  719. if parent == nil {
  720. return nil, vm.Context{}, nil, fmt.Errorf("parent %#x not found", block.ParentHash())
  721. }
  722. statedb, err := api.computeStateDB(parent, reexec)
  723. if err != nil {
  724. return nil, vm.Context{}, nil, err
  725. }
  726. // Recompute transactions up to the target index.
  727. signer := types.MakeSigner(api.config, block.Number())
  728. for idx, tx := range block.Transactions() {
  729. // Assemble the transaction call message and return if the requested offset
  730. msg, _ := tx.AsMessage(signer)
  731. context := core.NewEVMContext(msg, block.Header(), api.eth.blockchain, nil)
  732. if idx == txIndex {
  733. return msg, context, statedb, nil
  734. }
  735. // Not yet the searched for transaction, execute on top of the current state
  736. vmenv := vm.NewEVM(context, statedb, api.config, vm.Config{})
  737. if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
  738. return nil, vm.Context{}, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
  739. }
  740. // Ensure any modifications are committed to the state
  741. statedb.Finalise(true)
  742. }
  743. return nil, vm.Context{}, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, blockHash)
  744. }