api.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // Copyright 2021 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 tracers
  17. import (
  18. "bufio"
  19. "bytes"
  20. "context"
  21. "encoding/json"
  22. "errors"
  23. "fmt"
  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/consensus"
  31. "github.com/ethereum/go-ethereum/core"
  32. "github.com/ethereum/go-ethereum/core/rawdb"
  33. "github.com/ethereum/go-ethereum/core/state"
  34. "github.com/ethereum/go-ethereum/core/types"
  35. "github.com/ethereum/go-ethereum/core/vm"
  36. "github.com/ethereum/go-ethereum/eth/tracers/logger"
  37. "github.com/ethereum/go-ethereum/ethdb"
  38. "github.com/ethereum/go-ethereum/internal/ethapi"
  39. "github.com/ethereum/go-ethereum/log"
  40. "github.com/ethereum/go-ethereum/params"
  41. "github.com/ethereum/go-ethereum/rlp"
  42. "github.com/ethereum/go-ethereum/rpc"
  43. )
  44. const (
  45. // defaultTraceTimeout is the amount of time a single transaction can execute
  46. // by default before being forcefully aborted.
  47. defaultTraceTimeout = 5 * time.Second
  48. // defaultTraceReexec is the number of blocks the tracer is willing to go back
  49. // and reexecute to produce missing historical state necessary to run a specific
  50. // trace.
  51. defaultTraceReexec = uint64(128)
  52. // defaultTracechainMemLimit is the size of the triedb, at which traceChain
  53. // switches over and tries to use a disk-backed database instead of building
  54. // on top of memory.
  55. // For non-archive nodes, this limit _will_ be overblown, as disk-backed tries
  56. // will only be found every ~15K blocks or so.
  57. defaultTracechainMemLimit = common.StorageSize(500 * 1024 * 1024)
  58. )
  59. // Backend interface provides the common API services (that are provided by
  60. // both full and light clients) with access to necessary functions.
  61. type Backend interface {
  62. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  63. HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
  64. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  65. BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
  66. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  67. RPCGasCap() uint64
  68. ChainConfig() *params.ChainConfig
  69. Engine() consensus.Engine
  70. ChainDb() ethdb.Database
  71. // StateAtBlock returns the state corresponding to the stateroot of the block.
  72. // N.B: For executing transactions on block N, the required stateRoot is block N-1,
  73. // so this method should be called with the parent.
  74. StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error)
  75. StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error)
  76. }
  77. // API is the collection of tracing APIs exposed over the private debugging endpoint.
  78. type API struct {
  79. backend Backend
  80. }
  81. // NewAPI creates a new API definition for the tracing methods of the Ethereum service.
  82. func NewAPI(backend Backend) *API {
  83. return &API{backend: backend}
  84. }
  85. // chainContext constructs the context reader which is used by the evm for reading
  86. // the necessary chain context.
  87. func (api *API) chainContext(ctx context.Context) core.ChainContext {
  88. return ethapi.NewChainContext(ctx, api.backend)
  89. }
  90. // blockByNumber is the wrapper of the chain access function offered by the backend.
  91. // It will return an error if the block is not found.
  92. func (api *API) blockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
  93. block, err := api.backend.BlockByNumber(ctx, number)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if block == nil {
  98. return nil, fmt.Errorf("block #%d not found", number)
  99. }
  100. return block, nil
  101. }
  102. // blockByHash is the wrapper of the chain access function offered by the backend.
  103. // It will return an error if the block is not found.
  104. func (api *API) blockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  105. block, err := api.backend.BlockByHash(ctx, hash)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if block == nil {
  110. return nil, fmt.Errorf("block %s not found", hash.Hex())
  111. }
  112. return block, nil
  113. }
  114. // blockByNumberAndHash is the wrapper of the chain access function offered by
  115. // the backend. It will return an error if the block is not found.
  116. //
  117. // Note this function is friendly for the light client which can only retrieve the
  118. // historical(before the CHT) header/block by number.
  119. func (api *API) blockByNumberAndHash(ctx context.Context, number rpc.BlockNumber, hash common.Hash) (*types.Block, error) {
  120. block, err := api.blockByNumber(ctx, number)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if block.Hash() == hash {
  125. return block, nil
  126. }
  127. return api.blockByHash(ctx, hash)
  128. }
  129. // TraceConfig holds extra parameters to trace functions.
  130. type TraceConfig struct {
  131. *logger.Config
  132. Tracer *string
  133. Timeout *string
  134. Reexec *uint64
  135. // Config specific to given tracer. Note struct logger
  136. // config are historically embedded in main object.
  137. TracerConfig json.RawMessage
  138. }
  139. // TraceCallConfig is the config for traceCall API. It holds one more
  140. // field to override the state for tracing.
  141. type TraceCallConfig struct {
  142. TraceConfig
  143. StateOverrides *ethapi.StateOverride
  144. BlockOverrides *ethapi.BlockOverrides
  145. }
  146. // StdTraceConfig holds extra parameters to standard-json trace functions.
  147. type StdTraceConfig struct {
  148. logger.Config
  149. Reexec *uint64
  150. TxHash common.Hash
  151. }
  152. // txTraceResult is the result of a single transaction trace.
  153. type txTraceResult struct {
  154. Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
  155. Error string `json:"error,omitempty"` // Trace failure produced by the tracer
  156. }
  157. // blockTraceTask represents a single block trace task when an entire chain is
  158. // being traced.
  159. type blockTraceTask struct {
  160. statedb *state.StateDB // Intermediate state prepped for tracing
  161. block *types.Block // Block to trace the transactions from
  162. rootref common.Hash // Trie root reference held for this task
  163. results []*txTraceResult // Trace results produced by the task
  164. }
  165. // blockTraceResult represents the results of tracing a single block when an entire
  166. // chain is being traced.
  167. type blockTraceResult struct {
  168. Block hexutil.Uint64 `json:"block"` // Block number corresponding to this trace
  169. Hash common.Hash `json:"hash"` // Block hash corresponding to this trace
  170. Traces []*txTraceResult `json:"traces"` // Trace results produced by the task
  171. }
  172. // txTraceTask represents a single transaction trace task when an entire block
  173. // is being traced.
  174. type txTraceTask struct {
  175. statedb *state.StateDB // Intermediate state prepped for tracing
  176. index int // Transaction offset in the block
  177. }
  178. // TraceChain returns the structured logs created during the execution of EVM
  179. // between two blocks (excluding start) and returns them as a JSON object.
  180. func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) { // Fetch the block interval that we want to trace
  181. from, err := api.blockByNumber(ctx, start)
  182. if err != nil {
  183. return nil, err
  184. }
  185. to, err := api.blockByNumber(ctx, end)
  186. if err != nil {
  187. return nil, err
  188. }
  189. if from.Number().Cmp(to.Number()) >= 0 {
  190. return nil, fmt.Errorf("end block (#%d) needs to come after start block (#%d)", end, start)
  191. }
  192. return api.traceChain(ctx, from, to, config)
  193. }
  194. // traceChain configures a new tracer according to the provided configuration, and
  195. // executes all the transactions contained within. The return value will be one item
  196. // per transaction, dependent on the requested tracer.
  197. func (api *API) traceChain(ctx context.Context, start, end *types.Block, config *TraceConfig) (*rpc.Subscription, error) {
  198. // Tracing a chain is a **long** operation, only do with subscriptions
  199. notifier, supported := rpc.NotifierFromContext(ctx)
  200. if !supported {
  201. return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
  202. }
  203. sub := notifier.CreateSubscription()
  204. // Prepare all the states for tracing. Note this procedure can take very
  205. // long time. Timeout mechanism is necessary.
  206. reexec := defaultTraceReexec
  207. if config != nil && config.Reexec != nil {
  208. reexec = *config.Reexec
  209. }
  210. blocks := int(end.NumberU64() - start.NumberU64())
  211. threads := runtime.NumCPU()
  212. if threads > blocks {
  213. threads = blocks
  214. }
  215. var (
  216. pend = new(sync.WaitGroup)
  217. tasks = make(chan *blockTraceTask, threads)
  218. results = make(chan *blockTraceTask, threads)
  219. localctx = context.Background()
  220. )
  221. for th := 0; th < threads; th++ {
  222. pend.Add(1)
  223. go func() {
  224. defer pend.Done()
  225. // Fetch and execute the next block trace tasks
  226. for task := range tasks {
  227. signer := types.MakeSigner(api.backend.ChainConfig(), task.block.Number())
  228. blockCtx := core.NewEVMBlockContext(task.block.Header(), api.chainContext(localctx), nil)
  229. // Trace all the transactions contained within
  230. for i, tx := range task.block.Transactions() {
  231. msg, _ := tx.AsMessage(signer, task.block.BaseFee())
  232. txctx := &Context{
  233. BlockHash: task.block.Hash(),
  234. TxIndex: i,
  235. TxHash: tx.Hash(),
  236. }
  237. res, err := api.traceTx(localctx, msg, txctx, blockCtx, task.statedb, config)
  238. if err != nil {
  239. task.results[i] = &txTraceResult{Error: err.Error()}
  240. log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err)
  241. break
  242. }
  243. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  244. task.statedb.Finalise(api.backend.ChainConfig().IsEIP158(task.block.Number()))
  245. task.results[i] = &txTraceResult{Result: res}
  246. }
  247. // Stream the result back to the user or abort on teardown
  248. select {
  249. case results <- task:
  250. case <-notifier.Closed():
  251. return
  252. }
  253. }
  254. }()
  255. }
  256. // Start a goroutine to feed all the blocks into the tracers
  257. var (
  258. begin = time.Now()
  259. derefTodo []common.Hash // list of hashes to dereference from the db
  260. derefsMu sync.Mutex // mutex for the derefs
  261. )
  262. go func() {
  263. var (
  264. logged time.Time
  265. number uint64
  266. traced uint64
  267. failed error
  268. parent common.Hash
  269. statedb *state.StateDB
  270. )
  271. // Ensure everything is properly cleaned up on any exit path
  272. defer func() {
  273. close(tasks)
  274. pend.Wait()
  275. switch {
  276. case failed != nil:
  277. log.Warn("Chain tracing failed", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin), "err", failed)
  278. case number < end.NumberU64():
  279. log.Warn("Chain tracing aborted", "start", start.NumberU64(), "end", end.NumberU64(), "abort", number, "transactions", traced, "elapsed", time.Since(begin))
  280. default:
  281. log.Info("Chain tracing finished", "start", start.NumberU64(), "end", end.NumberU64(), "transactions", traced, "elapsed", time.Since(begin))
  282. }
  283. close(results)
  284. }()
  285. var preferDisk bool
  286. // Feed all the blocks both into the tracer, as well as fast process concurrently
  287. for number = start.NumberU64(); number < end.NumberU64(); number++ {
  288. // Stop tracing if interruption was requested
  289. select {
  290. case <-notifier.Closed():
  291. return
  292. default:
  293. }
  294. // clean out any derefs
  295. derefsMu.Lock()
  296. for _, h := range derefTodo {
  297. statedb.Database().TrieDB().Dereference(h)
  298. }
  299. derefTodo = derefTodo[:0]
  300. derefsMu.Unlock()
  301. // Print progress logs if long enough time elapsed
  302. if time.Since(logged) > 8*time.Second {
  303. logged = time.Now()
  304. log.Info("Tracing chain segment", "start", start.NumberU64(), "end", end.NumberU64(), "current", number, "transactions", traced, "elapsed", time.Since(begin))
  305. }
  306. // Retrieve the parent state to trace on top
  307. block, err := api.blockByNumber(localctx, rpc.BlockNumber(number))
  308. if err != nil {
  309. failed = err
  310. break
  311. }
  312. // Prepare the statedb for tracing. Don't use the live database for
  313. // tracing to avoid persisting state junks into the database.
  314. statedb, err = api.backend.StateAtBlock(localctx, block, reexec, statedb, false, preferDisk)
  315. if err != nil {
  316. failed = err
  317. break
  318. }
  319. if trieDb := statedb.Database().TrieDB(); trieDb != nil {
  320. // Hold the reference for tracer, will be released at the final stage
  321. trieDb.Reference(block.Root(), common.Hash{})
  322. // Release the parent state because it's already held by the tracer
  323. if parent != (common.Hash{}) {
  324. trieDb.Dereference(parent)
  325. }
  326. // Prefer disk if the trie db memory grows too much
  327. s1, s2 := trieDb.Size()
  328. if !preferDisk && (s1+s2) > defaultTracechainMemLimit {
  329. log.Info("Switching to prefer-disk mode for tracing", "size", s1+s2)
  330. preferDisk = true
  331. }
  332. }
  333. parent = block.Root()
  334. next, err := api.blockByNumber(localctx, rpc.BlockNumber(number+1))
  335. if err != nil {
  336. failed = err
  337. break
  338. }
  339. // Send the block over to the concurrent tracers (if not in the fast-forward phase)
  340. txs := next.Transactions()
  341. select {
  342. case tasks <- &blockTraceTask{statedb: statedb.Copy(), block: next, rootref: block.Root(), results: make([]*txTraceResult, len(txs))}:
  343. case <-notifier.Closed():
  344. return
  345. }
  346. traced += uint64(len(txs))
  347. }
  348. }()
  349. // Keep reading the trace results and stream the to the user
  350. go func() {
  351. var (
  352. done = make(map[uint64]*blockTraceResult)
  353. next = start.NumberU64() + 1
  354. )
  355. for res := range results {
  356. // Queue up next received result
  357. result := &blockTraceResult{
  358. Block: hexutil.Uint64(res.block.NumberU64()),
  359. Hash: res.block.Hash(),
  360. Traces: res.results,
  361. }
  362. // Schedule any parent tries held in memory by this task for dereferencing
  363. done[uint64(result.Block)] = result
  364. derefsMu.Lock()
  365. derefTodo = append(derefTodo, res.rootref)
  366. derefsMu.Unlock()
  367. // Stream completed traces to the user, aborting on the first error
  368. for result, ok := done[next]; ok; result, ok = done[next] {
  369. if len(result.Traces) > 0 || next == end.NumberU64() {
  370. notifier.Notify(sub.ID, result)
  371. }
  372. delete(done, next)
  373. next++
  374. }
  375. }
  376. }()
  377. return sub, nil
  378. }
  379. // TraceBlockByNumber returns the structured logs created during the execution of
  380. // EVM and returns them as a JSON object.
  381. func (api *API) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) {
  382. block, err := api.blockByNumber(ctx, number)
  383. if err != nil {
  384. return nil, err
  385. }
  386. return api.traceBlock(ctx, block, config)
  387. }
  388. // TraceBlockByHash returns the structured logs created during the execution of
  389. // EVM and returns them as a JSON object.
  390. func (api *API) TraceBlockByHash(ctx context.Context, hash common.Hash, config *TraceConfig) ([]*txTraceResult, error) {
  391. block, err := api.blockByHash(ctx, hash)
  392. if err != nil {
  393. return nil, err
  394. }
  395. return api.traceBlock(ctx, block, config)
  396. }
  397. // TraceBlock returns the structured logs created during the execution of EVM
  398. // and returns them as a JSON object.
  399. func (api *API) TraceBlock(ctx context.Context, blob hexutil.Bytes, config *TraceConfig) ([]*txTraceResult, error) {
  400. block := new(types.Block)
  401. if err := rlp.Decode(bytes.NewReader(blob), block); err != nil {
  402. return nil, fmt.Errorf("could not decode block: %v", err)
  403. }
  404. return api.traceBlock(ctx, block, config)
  405. }
  406. // TraceBlockFromFile returns the structured logs created during the execution of
  407. // EVM and returns them as a JSON object.
  408. func (api *API) TraceBlockFromFile(ctx context.Context, file string, config *TraceConfig) ([]*txTraceResult, error) {
  409. blob, err := os.ReadFile(file)
  410. if err != nil {
  411. return nil, fmt.Errorf("could not read file: %v", err)
  412. }
  413. return api.TraceBlock(ctx, blob, config)
  414. }
  415. // TraceBadBlock returns the structured logs created during the execution of
  416. // EVM against a block pulled from the pool of bad ones and returns them as a JSON
  417. // object.
  418. func (api *API) TraceBadBlock(ctx context.Context, hash common.Hash, config *TraceConfig) ([]*txTraceResult, error) {
  419. block := rawdb.ReadBadBlock(api.backend.ChainDb(), hash)
  420. if block == nil {
  421. return nil, fmt.Errorf("bad block %#x not found", hash)
  422. }
  423. return api.traceBlock(ctx, block, config)
  424. }
  425. // StandardTraceBlockToFile dumps the structured logs created during the
  426. // execution of EVM to the local file system and returns a list of files
  427. // to the caller.
  428. func (api *API) StandardTraceBlockToFile(ctx context.Context, hash common.Hash, config *StdTraceConfig) ([]string, error) {
  429. block, err := api.blockByHash(ctx, hash)
  430. if err != nil {
  431. return nil, err
  432. }
  433. return api.standardTraceBlockToFile(ctx, block, config)
  434. }
  435. // IntermediateRoots executes a block (bad- or canon- or side-), and returns a list
  436. // of intermediate roots: the stateroot after each transaction.
  437. func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config *TraceConfig) ([]common.Hash, error) {
  438. block, _ := api.blockByHash(ctx, hash)
  439. if block == nil {
  440. // Check in the bad blocks
  441. block = rawdb.ReadBadBlock(api.backend.ChainDb(), hash)
  442. }
  443. if block == nil {
  444. return nil, fmt.Errorf("block %#x not found", hash)
  445. }
  446. if block.NumberU64() == 0 {
  447. return nil, errors.New("genesis is not traceable")
  448. }
  449. parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
  450. if err != nil {
  451. return nil, err
  452. }
  453. reexec := defaultTraceReexec
  454. if config != nil && config.Reexec != nil {
  455. reexec = *config.Reexec
  456. }
  457. statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
  458. if err != nil {
  459. return nil, err
  460. }
  461. var (
  462. roots []common.Hash
  463. signer = types.MakeSigner(api.backend.ChainConfig(), block.Number())
  464. chainConfig = api.backend.ChainConfig()
  465. vmctx = core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
  466. deleteEmptyObjects = chainConfig.IsEIP158(block.Number())
  467. )
  468. for i, tx := range block.Transactions() {
  469. var (
  470. msg, _ = tx.AsMessage(signer, block.BaseFee())
  471. txContext = core.NewEVMTxContext(msg)
  472. vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{})
  473. )
  474. statedb.Prepare(tx.Hash(), i)
  475. if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil {
  476. log.Warn("Tracing intermediate roots did not complete", "txindex", i, "txhash", tx.Hash(), "err", err)
  477. // We intentionally don't return the error here: if we do, then the RPC server will not
  478. // return the roots. Most likely, the caller already knows that a certain transaction fails to
  479. // be included, but still want the intermediate roots that led to that point.
  480. // It may happen the tx_N causes an erroneous state, which in turn causes tx_N+M to not be
  481. // executable.
  482. // N.B: This should never happen while tracing canon blocks, only when tracing bad blocks.
  483. return roots, nil
  484. }
  485. // calling IntermediateRoot will internally call Finalize on the state
  486. // so any modifications are written to the trie
  487. roots = append(roots, statedb.IntermediateRoot(deleteEmptyObjects))
  488. }
  489. return roots, nil
  490. }
  491. // StandardTraceBadBlockToFile dumps the structured logs created during the
  492. // execution of EVM against a block pulled from the pool of bad ones to the
  493. // local file system and returns a list of files to the caller.
  494. func (api *API) StandardTraceBadBlockToFile(ctx context.Context, hash common.Hash, config *StdTraceConfig) ([]string, error) {
  495. block := rawdb.ReadBadBlock(api.backend.ChainDb(), hash)
  496. if block == nil {
  497. return nil, fmt.Errorf("bad block %#x not found", hash)
  498. }
  499. return api.standardTraceBlockToFile(ctx, block, config)
  500. }
  501. // traceBlock configures a new tracer according to the provided configuration, and
  502. // executes all the transactions contained within. The return value will be one item
  503. // per transaction, dependent on the requested tracer.
  504. func (api *API) traceBlock(ctx context.Context, block *types.Block, config *TraceConfig) ([]*txTraceResult, error) {
  505. if block.NumberU64() == 0 {
  506. return nil, errors.New("genesis is not traceable")
  507. }
  508. parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
  509. if err != nil {
  510. return nil, err
  511. }
  512. reexec := defaultTraceReexec
  513. if config != nil && config.Reexec != nil {
  514. reexec = *config.Reexec
  515. }
  516. statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
  517. if err != nil {
  518. return nil, err
  519. }
  520. // Execute all the transaction contained within the block concurrently
  521. var (
  522. signer = types.MakeSigner(api.backend.ChainConfig(), block.Number())
  523. txs = block.Transactions()
  524. results = make([]*txTraceResult, len(txs))
  525. pend = new(sync.WaitGroup)
  526. jobs = make(chan *txTraceTask, len(txs))
  527. )
  528. threads := runtime.NumCPU()
  529. if threads > len(txs) {
  530. threads = len(txs)
  531. }
  532. blockHash := block.Hash()
  533. for th := 0; th < threads; th++ {
  534. pend.Add(1)
  535. go func() {
  536. blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
  537. defer pend.Done()
  538. // Fetch and execute the next transaction trace tasks
  539. for task := range jobs {
  540. msg, _ := txs[task.index].AsMessage(signer, block.BaseFee())
  541. txctx := &Context{
  542. BlockHash: blockHash,
  543. TxIndex: task.index,
  544. TxHash: txs[task.index].Hash(),
  545. }
  546. res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config)
  547. if err != nil {
  548. results[task.index] = &txTraceResult{Error: err.Error()}
  549. continue
  550. }
  551. results[task.index] = &txTraceResult{Result: res}
  552. }
  553. }()
  554. }
  555. // Feed the transactions into the tracers and return
  556. var failed error
  557. blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
  558. for i, tx := range txs {
  559. // Send the trace task over for execution
  560. jobs <- &txTraceTask{statedb: statedb.Copy(), index: i}
  561. // Generate the next state snapshot fast without tracing
  562. msg, _ := tx.AsMessage(signer, block.BaseFee())
  563. statedb.Prepare(tx.Hash(), i)
  564. vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{})
  565. if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil {
  566. failed = err
  567. break
  568. }
  569. // Finalize the state so any modifications are written to the trie
  570. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  571. statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
  572. }
  573. close(jobs)
  574. pend.Wait()
  575. // If execution failed in between, abort
  576. if failed != nil {
  577. return nil, failed
  578. }
  579. return results, nil
  580. }
  581. // standardTraceBlockToFile configures a new tracer which uses standard JSON output,
  582. // and traces either a full block or an individual transaction. The return value will
  583. // be one filename per transaction traced.
  584. func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) {
  585. // If we're tracing a single transaction, make sure it's present
  586. if config != nil && config.TxHash != (common.Hash{}) {
  587. if !containsTx(block, config.TxHash) {
  588. return nil, fmt.Errorf("transaction %#x not found in block", config.TxHash)
  589. }
  590. }
  591. if block.NumberU64() == 0 {
  592. return nil, errors.New("genesis is not traceable")
  593. }
  594. parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
  595. if err != nil {
  596. return nil, err
  597. }
  598. reexec := defaultTraceReexec
  599. if config != nil && config.Reexec != nil {
  600. reexec = *config.Reexec
  601. }
  602. statedb, err := api.backend.StateAtBlock(ctx, parent, reexec, nil, true, false)
  603. if err != nil {
  604. return nil, err
  605. }
  606. // Retrieve the tracing configurations, or use default values
  607. var (
  608. logConfig logger.Config
  609. txHash common.Hash
  610. )
  611. if config != nil {
  612. logConfig = config.Config
  613. txHash = config.TxHash
  614. }
  615. logConfig.Debug = true
  616. // Execute transaction, either tracing all or just the requested one
  617. var (
  618. dumps []string
  619. signer = types.MakeSigner(api.backend.ChainConfig(), block.Number())
  620. chainConfig = api.backend.ChainConfig()
  621. vmctx = core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
  622. canon = true
  623. )
  624. // Check if there are any overrides: the caller may wish to enable a future
  625. // fork when executing this block. Note, such overrides are only applicable to the
  626. // actual specified block, not any preceding blocks that we have to go through
  627. // in order to obtain the state.
  628. // Therefore, it's perfectly valid to specify `"futureForkBlock": 0`, to enable `futureFork`
  629. if config != nil && config.Overrides != nil {
  630. // Copy the config, to not screw up the main config
  631. // Note: the Clique-part is _not_ deep copied
  632. chainConfigCopy := new(params.ChainConfig)
  633. *chainConfigCopy = *chainConfig
  634. chainConfig = chainConfigCopy
  635. if berlin := config.Config.Overrides.BerlinBlock; berlin != nil {
  636. chainConfig.BerlinBlock = berlin
  637. canon = false
  638. }
  639. }
  640. for i, tx := range block.Transactions() {
  641. // Prepare the transaction for un-traced execution
  642. var (
  643. msg, _ = tx.AsMessage(signer, block.BaseFee())
  644. txContext = core.NewEVMTxContext(msg)
  645. vmConf vm.Config
  646. dump *os.File
  647. writer *bufio.Writer
  648. err error
  649. )
  650. // If the transaction needs tracing, swap out the configs
  651. if tx.Hash() == txHash || txHash == (common.Hash{}) {
  652. // Generate a unique temporary file to dump it into
  653. prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
  654. if !canon {
  655. prefix = fmt.Sprintf("%valt-", prefix)
  656. }
  657. dump, err = os.CreateTemp(os.TempDir(), prefix)
  658. if err != nil {
  659. return nil, err
  660. }
  661. dumps = append(dumps, dump.Name())
  662. // Swap out the noop logger to the standard tracer
  663. writer = bufio.NewWriter(dump)
  664. vmConf = vm.Config{
  665. Debug: true,
  666. Tracer: logger.NewJSONLogger(&logConfig, writer),
  667. EnablePreimageRecording: true,
  668. }
  669. }
  670. // Execute the transaction and flush any traces to disk
  671. vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf)
  672. statedb.Prepare(tx.Hash(), i)
  673. _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
  674. if writer != nil {
  675. writer.Flush()
  676. }
  677. if dump != nil {
  678. dump.Close()
  679. log.Info("Wrote standard trace", "file", dump.Name())
  680. }
  681. if err != nil {
  682. return dumps, err
  683. }
  684. // Finalize the state so any modifications are written to the trie
  685. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  686. statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
  687. // If we've traced the transaction we were looking for, abort
  688. if tx.Hash() == txHash {
  689. break
  690. }
  691. }
  692. return dumps, nil
  693. }
  694. // containsTx reports whether the transaction with a certain hash
  695. // is contained within the specified block.
  696. func containsTx(block *types.Block, hash common.Hash) bool {
  697. for _, tx := range block.Transactions() {
  698. if tx.Hash() == hash {
  699. return true
  700. }
  701. }
  702. return false
  703. }
  704. // TraceTransaction returns the structured logs created during the execution of EVM
  705. // and returns them as a JSON object.
  706. func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) {
  707. _, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash)
  708. if err != nil {
  709. return nil, err
  710. }
  711. // It shouldn't happen in practice.
  712. if blockNumber == 0 {
  713. return nil, errors.New("genesis is not traceable")
  714. }
  715. reexec := defaultTraceReexec
  716. if config != nil && config.Reexec != nil {
  717. reexec = *config.Reexec
  718. }
  719. block, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(blockNumber), blockHash)
  720. if err != nil {
  721. return nil, err
  722. }
  723. msg, vmctx, statedb, err := api.backend.StateAtTransaction(ctx, block, int(index), reexec)
  724. if err != nil {
  725. return nil, err
  726. }
  727. txctx := &Context{
  728. BlockHash: blockHash,
  729. TxIndex: int(index),
  730. TxHash: hash,
  731. }
  732. return api.traceTx(ctx, msg, txctx, vmctx, statedb, config)
  733. }
  734. // TraceCall lets you trace a given eth_call. It collects the structured logs
  735. // created during the execution of EVM if the given transaction was added on
  736. // top of the provided block and returns them as a JSON object.
  737. func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig) (interface{}, error) {
  738. // Try to retrieve the specified block
  739. var (
  740. err error
  741. block *types.Block
  742. )
  743. if hash, ok := blockNrOrHash.Hash(); ok {
  744. block, err = api.blockByHash(ctx, hash)
  745. } else if number, ok := blockNrOrHash.Number(); ok {
  746. if number == rpc.PendingBlockNumber {
  747. // We don't have access to the miner here. For tracing 'future' transactions,
  748. // it can be done with block- and state-overrides instead, which offers
  749. // more flexibility and stability than trying to trace on 'pending', since
  750. // the contents of 'pending' is unstable and probably not a true representation
  751. // of what the next actual block is likely to contain.
  752. return nil, errors.New("tracing on top of pending is not supported")
  753. }
  754. block, err = api.blockByNumber(ctx, number)
  755. } else {
  756. return nil, errors.New("invalid arguments; neither block nor hash specified")
  757. }
  758. if err != nil {
  759. return nil, err
  760. }
  761. // try to recompute the state
  762. reexec := defaultTraceReexec
  763. if config != nil && config.Reexec != nil {
  764. reexec = *config.Reexec
  765. }
  766. statedb, err := api.backend.StateAtBlock(ctx, block, reexec, nil, true, false)
  767. if err != nil {
  768. return nil, err
  769. }
  770. vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
  771. // Apply the customization rules if required.
  772. if config != nil {
  773. if err := config.StateOverrides.Apply(statedb); err != nil {
  774. return nil, err
  775. }
  776. config.BlockOverrides.Apply(&vmctx)
  777. }
  778. // Execute the trace
  779. msg, err := args.ToMessage(api.backend.RPCGasCap(), block.BaseFee())
  780. if err != nil {
  781. return nil, err
  782. }
  783. var traceConfig *TraceConfig
  784. if config != nil {
  785. traceConfig = &TraceConfig{
  786. Config: config.Config,
  787. Tracer: config.Tracer,
  788. Timeout: config.Timeout,
  789. Reexec: config.Reexec,
  790. }
  791. }
  792. return api.traceTx(ctx, msg, new(Context), vmctx, statedb, traceConfig)
  793. }
  794. // traceTx configures a new tracer according to the provided configuration, and
  795. // executes the given message in the provided environment. The return value will
  796. // be tracer dependent.
  797. func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) {
  798. var (
  799. tracer Tracer
  800. err error
  801. timeout = defaultTraceTimeout
  802. txContext = core.NewEVMTxContext(message)
  803. )
  804. if config == nil {
  805. config = &TraceConfig{}
  806. }
  807. // Default tracer is the struct logger
  808. tracer = logger.NewStructLogger(config.Config)
  809. if config.Tracer != nil {
  810. tracer, err = New(*config.Tracer, txctx, config.TracerConfig)
  811. if err != nil {
  812. return nil, err
  813. }
  814. }
  815. // Define a meaningful timeout of a single transaction trace
  816. if config.Timeout != nil {
  817. if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
  818. return nil, err
  819. }
  820. }
  821. deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
  822. go func() {
  823. <-deadlineCtx.Done()
  824. if errors.Is(deadlineCtx.Err(), context.DeadlineExceeded) {
  825. tracer.Stop(errors.New("execution timeout"))
  826. }
  827. }()
  828. defer cancel()
  829. // Run the transaction with tracing enabled.
  830. vmenv := vm.NewEVM(vmctx, txContext, statedb, api.backend.ChainConfig(), vm.Config{Debug: true, Tracer: tracer, NoBaseFee: true})
  831. // Call Prepare to clear out the statedb access list
  832. statedb.Prepare(txctx.TxHash, txctx.TxIndex)
  833. if _, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas())); err != nil {
  834. return nil, fmt.Errorf("tracing failed: %w", err)
  835. }
  836. return tracer.GetResult()
  837. }
  838. // APIs return the collection of RPC services the tracer package offers.
  839. func APIs(backend Backend) []rpc.API {
  840. // Append all the local APIs and return
  841. return []rpc.API{
  842. {
  843. Namespace: "debug",
  844. Service: NewAPI(backend),
  845. },
  846. }
  847. }