@@ -1501,6 +1501,145 @@ func (api *PrivateDebugAPI) SetHead(number uint64) {
15011501 api .eth .BlockChain ().SetHead (number )
15021502}
15031503
1504+ // StructLogRes stores a structured log emitted by the evm while replaying a
1505+ // transaction in debug mode
1506+ type structLogRes struct {
1507+ Pc uint64 `json:"pc"`
1508+ Op string `json:"op"`
1509+ Gas * big.Int `json:"gas"`
1510+ GasCost * big.Int `json:"gasCost"`
1511+ Error error `json:"error"`
1512+ Stack []string `json:"stack"`
1513+ Memory map [string ]string `json:"memory"`
1514+ Storage map [string ]string `json:"storage"`
1515+ }
1516+
1517+ // TransactionExecutionRes groups all structured logs emitted by the evm
1518+ // while replaying a transaction in debug mode as well as the amount of
1519+ // gas used and the return value
1520+ type TransactionExecutionResult struct {
1521+ Gas * big.Int `json:"gas"`
1522+ ReturnValue string `json:"returnValue"`
1523+ StructLogs []structLogRes `json:"structLogs"`
1524+ }
1525+
1526+ func (s * PrivateDebugAPI ) doReplayTransaction (txHash common.Hash ) ([]vm.StructLog , []byte , * big.Int , error ) {
1527+ // Retrieve the tx from the chain
1528+ tx , _ , blockIndex , _ := core .GetTransaction (s .eth .ChainDb (), txHash )
1529+
1530+ if tx == nil {
1531+ return nil , nil , nil , fmt .Errorf ("Transaction not found" )
1532+ }
1533+
1534+ block := s .eth .BlockChain ().GetBlockByNumber (blockIndex - 1 )
1535+ if block == nil {
1536+ return nil , nil , nil , fmt .Errorf ("Unable to retrieve prior block" )
1537+ }
1538+
1539+ // Create the state database
1540+ stateDb , err := state .New (block .Root (), s .eth .ChainDb ())
1541+ if err != nil {
1542+ return nil , nil , nil , err
1543+ }
1544+
1545+ txFrom , err := tx .From ()
1546+
1547+ if err != nil {
1548+ return nil , nil , nil , fmt .Errorf ("Unable to create transaction sender" )
1549+ }
1550+ from := stateDb .GetOrNewStateObject (txFrom )
1551+ msg := callmsg {
1552+ from : from ,
1553+ to : tx .To (),
1554+ gas : tx .Gas (),
1555+ gasPrice : tx .GasPrice (),
1556+ value : tx .Value (),
1557+ data : tx .Data (),
1558+ }
1559+
1560+ vmenv := core .NewEnv (stateDb , s .eth .BlockChain (), msg , block .Header ())
1561+ gp := new (core.GasPool ).AddGas (block .GasLimit ())
1562+ vm .GenerateStructLogs = true
1563+ defer func () { vm .GenerateStructLogs = false }()
1564+
1565+ ret , gas , err := core .ApplyMessage (vmenv , msg , gp )
1566+ if err != nil {
1567+ return nil , nil , nil , fmt .Errorf ("Error executing transaction %v" , err )
1568+ }
1569+
1570+ return vmenv .StructLogs (), ret , gas , nil
1571+ }
1572+
1573+ // Executes a transaction and returns the structured logs of the evm
1574+ // gathered during the execution
1575+ func (s * PrivateDebugAPI ) ReplayTransaction (txHash common.Hash , stackDepth int , memorySize int , storageSize int ) (* TransactionExecutionResult , error ) {
1576+
1577+ structLogs , ret , gas , err := s .doReplayTransaction (txHash )
1578+
1579+ if err != nil {
1580+ return nil , err
1581+ }
1582+
1583+ res := TransactionExecutionResult {
1584+ Gas : gas ,
1585+ ReturnValue : fmt .Sprintf ("%x" , ret ),
1586+ StructLogs : make ([]structLogRes , len (structLogs )),
1587+ }
1588+
1589+ for index , trace := range structLogs {
1590+
1591+ stackLength := len (trace .Stack )
1592+
1593+ // Return full stack by default
1594+ if stackDepth != - 1 && stackDepth < stackLength {
1595+ stackLength = stackDepth
1596+ }
1597+
1598+ res .StructLogs [index ] = structLogRes {
1599+ Pc : trace .Pc ,
1600+ Op : trace .Op .String (),
1601+ Gas : trace .Gas ,
1602+ GasCost : trace .GasCost ,
1603+ Error : trace .Err ,
1604+ Stack : make ([]string , stackLength ),
1605+ Memory : make (map [string ]string ),
1606+ Storage : make (map [string ]string ),
1607+ }
1608+
1609+ for i := 0 ; i < stackLength ; i ++ {
1610+ res .StructLogs [index ].Stack [i ] = fmt .Sprintf ("%x" , common .LeftPadBytes (trace .Stack [i ].Bytes (), 32 ))
1611+ }
1612+
1613+ addr := 0
1614+ memorySizeLocal := memorySize
1615+
1616+ // Return full memory by default
1617+ if memorySize == - 1 {
1618+ memorySizeLocal = len (trace .Memory )
1619+ }
1620+
1621+ for i := 0 ; i + 16 <= len (trace .Memory ) && addr < memorySizeLocal ; i += 16 {
1622+ res .StructLogs [index ].Memory [fmt .Sprintf ("%04d" , addr * 16 )] = fmt .Sprintf ("%x" , trace .Memory [i :i + 16 ])
1623+ addr ++
1624+ }
1625+
1626+ storageLength := len (trace .Stack )
1627+ if storageSize != - 1 && storageSize < storageLength {
1628+ storageLength = storageSize
1629+ }
1630+
1631+ i := 0
1632+ for storageIndex , storageValue := range trace .Storage {
1633+ if i >= storageLength {
1634+ break
1635+ }
1636+ res .StructLogs [index ].Storage [fmt .Sprintf ("%x" , storageIndex )] = fmt .Sprintf ("%x" , storageValue )
1637+ i ++
1638+ }
1639+ }
1640+ return & res , nil
1641+ }
1642+
15041643// PublicNetAPI offers network related RPC methods
15051644type PublicNetAPI struct {
15061645 net * p2p.Server
0 commit comments