@@ -20,7 +20,6 @@ import (
2020 "bytes"
2121 "compress/gzip"
2222 "context"
23- "errors"
2423 "fmt"
2524 "io"
2625 "io/ioutil"
@@ -41,6 +40,7 @@ import (
4140 "github.com/ethereum/go-ethereum/params"
4241 "github.com/ethereum/go-ethereum/rlp"
4342 "github.com/ethereum/go-ethereum/rpc"
43+ "github.com/ethereum/go-ethereum/trie"
4444)
4545
4646const defaultTraceTimeout = 5 * time .Second
@@ -526,59 +526,67 @@ func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, txHash common.
526526 if tx == nil {
527527 return nil , fmt .Errorf ("transaction %x not found" , txHash )
528528 }
529+ msg , context , statedb , err := api .computeTxEnv (blockHash , int (txIndex ))
530+ if err != nil {
531+ return nil , err
532+ }
533+
534+ // Run the transaction with tracing enabled.
535+ vmenv := vm .NewEVM (context , statedb , api .config , vm.Config {Debug : true , Tracer : tracer })
536+ ret , gas , err := core .ApplyMessage (vmenv , msg , new (core.GasPool ).AddGas (tx .Gas ()))
537+ if err != nil {
538+ return nil , fmt .Errorf ("tracing failed: %v" , err )
539+ }
540+ switch tracer := tracer .(type ) {
541+ case * vm.StructLogger :
542+ return & ethapi.ExecutionResult {
543+ Gas : gas ,
544+ ReturnValue : fmt .Sprintf ("%x" , ret ),
545+ StructLogs : ethapi .FormatLogs (tracer .StructLogs ()),
546+ }, nil
547+ case * ethapi.JavascriptTracer :
548+ return tracer .GetResult ()
549+ default :
550+ panic (fmt .Sprintf ("bad tracer type %T" , tracer ))
551+ }
552+ }
553+
554+ // computeTxEnv returns the execution environment of a certain transaction.
555+ func (api * PrivateDebugAPI ) computeTxEnv (blockHash common.Hash , txIndex int ) (core.Message , vm.Context , * state.StateDB , error ) {
556+ // Create the parent state.
529557 block := api .eth .BlockChain ().GetBlockByHash (blockHash )
530558 if block == nil {
531- return nil , fmt .Errorf ("block %x not found" , blockHash )
559+ return nil , vm. Context {}, nil , fmt .Errorf ("block %x not found" , blockHash )
532560 }
533- // Create the state database to mutate and eventually trace
534561 parent := api .eth .BlockChain ().GetBlock (block .ParentHash (), block .NumberU64 ()- 1 )
535562 if parent == nil {
536- return nil , fmt .Errorf ("block parent %x not found" , block .ParentHash ())
563+ return nil , vm. Context {}, nil , fmt .Errorf ("block parent %x not found" , block .ParentHash ())
537564 }
538- stateDb , err := api .eth .BlockChain ().StateAt (parent .Root ())
565+ statedb , err := api .eth .BlockChain ().StateAt (parent .Root ())
539566 if err != nil {
540- return nil , err
567+ return nil , vm. Context {}, nil , err
541568 }
569+ txs := block .Transactions ()
542570
571+ // Recompute transactions up to the target index.
543572 signer := types .MakeSigner (api .config , block .Number ())
544- // Mutate the state and trace the selected transaction
545- for idx , tx := range block .Transactions () {
573+ for idx , tx := range txs {
546574 // Assemble the transaction call message
547- msg , err := tx .AsMessage (signer )
548- if err != nil {
549- return nil , fmt .Errorf ("sender retrieval failed: %v" , err )
550- }
575+ msg , _ := tx .AsMessage (signer )
551576 context := core .NewEVMContext (msg , block .Header (), api .eth .BlockChain (), nil )
552-
553- // Mutate the state if we haven't reached the tracing transaction yet
554- if uint64 (idx ) < txIndex {
555- vmenv := vm .NewEVM (context , stateDb , api .config , vm.Config {})
556- _ , _ , err := core .ApplyMessage (vmenv , msg , new (core.GasPool ).AddGas (tx .Gas ()))
557- if err != nil {
558- return nil , fmt .Errorf ("mutation failed: %v" , err )
559- }
560- stateDb .DeleteSuicides ()
561- continue
577+ if idx == txIndex {
578+ return msg , context , statedb , nil
562579 }
563580
564- vmenv := vm .NewEVM (context , stateDb , api .config , vm.Config {Debug : true , Tracer : tracer })
565- ret , gas , err := core .ApplyMessage (vmenv , msg , new (core.GasPool ).AddGas (tx .Gas ()))
581+ vmenv := vm .NewEVM (context , statedb , api .config , vm.Config {})
582+ gp := new (core.GasPool ).AddGas (tx .Gas ())
583+ _ , _ , err := core .ApplyMessage (vmenv , msg , gp )
566584 if err != nil {
567- return nil , fmt .Errorf ("tracing failed: %v" , err )
568- }
569-
570- switch tracer := tracer .(type ) {
571- case * vm.StructLogger :
572- return & ethapi.ExecutionResult {
573- Gas : gas ,
574- ReturnValue : fmt .Sprintf ("%x" , ret ),
575- StructLogs : ethapi .FormatLogs (tracer .StructLogs ()),
576- }, nil
577- case * ethapi.JavascriptTracer :
578- return tracer .GetResult ()
585+ return nil , vm.Context {}, nil , fmt .Errorf ("tx %x failed: %v" , tx .Hash (), err )
579586 }
587+ statedb .DeleteSuicides ()
580588 }
581- return nil , errors . New ( "database inconsistency" )
589+ return nil , vm. Context {}, nil , fmt . Errorf ( "tx index %d out of range for block %x" , txIndex , blockHash )
582590}
583591
584592// Preimage is a debug API function that returns the preimage for a sha3 hash, if known.
@@ -592,3 +600,48 @@ func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hex
592600func (api * PrivateDebugAPI ) GetBadBlocks (ctx context.Context ) ([]core.BadBlockArgs , error ) {
593601 return api .eth .BlockChain ().BadBlocks ()
594602}
603+
604+ // StorageRangeResult is the result of a debug_storageRangeAt API call.
605+ type StorageRangeResult struct {
606+ Storage storageMap `json:"storage"`
607+ NextKey * common.Hash `json:"nextKey"` // nil if Storage includes the last key in the trie.
608+ }
609+
610+ type storageMap map [common.Hash ]storageEntry
611+
612+ type storageEntry struct {
613+ Key * common.Hash `json:"key"`
614+ Value common.Hash `json:"value"`
615+ }
616+
617+ // StorageRangeAt returns the storage at the given block height and transaction index.
618+ func (api * PrivateDebugAPI ) StorageRangeAt (ctx context.Context , blockHash common.Hash , txIndex int , contractAddress common.Address , keyStart hexutil.Bytes , maxResult int ) (StorageRangeResult , error ) {
619+ _ , _ , statedb , err := api .computeTxEnv (blockHash , txIndex )
620+ if err != nil {
621+ return StorageRangeResult {}, err
622+ }
623+ st := statedb .StorageTrie (contractAddress )
624+ if st == nil {
625+ return StorageRangeResult {}, fmt .Errorf ("account %x doesn't exist" , contractAddress )
626+ }
627+ return storageRangeAt (st , keyStart , maxResult ), nil
628+ }
629+
630+ func storageRangeAt (st * trie.SecureTrie , start []byte , maxResult int ) StorageRangeResult {
631+ it := trie .NewIterator (st .NodeIterator (start ))
632+ result := StorageRangeResult {Storage : storageMap {}}
633+ for i := 0 ; i < maxResult && it .Next (); i ++ {
634+ e := storageEntry {Value : common .BytesToHash (it .Value )}
635+ if preimage := st .GetKey (it .Key ); preimage != nil {
636+ preimage := common .BytesToHash (preimage )
637+ e .Key = & preimage
638+ }
639+ result .Storage [common .BytesToHash (it .Key )] = e
640+ }
641+ // Add the 'next key' so clients can continue downloading.
642+ if it .Next () {
643+ next := common .BytesToHash (it .Key )
644+ result .NextKey = & next
645+ }
646+ return result
647+ }
0 commit comments