@@ -1427,6 +1427,145 @@ func (api *PrivateDebugAPI) SetHead(number uint64) {
14271427 api .eth .BlockChain ().SetHead (number )
14281428}
14291429
1430+ // StructLogRes stores a structured log emitted by the evm while replaying a
1431+ // transaction in debug mode
1432+ type structLogRes struct {
1433+ Pc uint64 `json:"pc"`
1434+ Op string `json:"op"`
1435+ Gas * big.Int `json:"gas"`
1436+ GasCost * big.Int `json:"gasCost"`
1437+ Error error `json:"error"`
1438+ Stack []string `json:"stack"`
1439+ Memory map [string ]string `json:"memory"`
1440+ Storage map [string ]string `json:"storage"`
1441+ }
1442+
1443+ // TransactionExecutionRes groups all structured logs emitted by the evm
1444+ // while replaying a transaction in debug mode as well as the amount of
1445+ // gas used and the return value
1446+ type TransactionExecutionResult struct {
1447+ Gas * big.Int `json:"gas"`
1448+ ReturnValue string `json:"returnValue"`
1449+ StructLogs []structLogRes `json:"structLogs"`
1450+ }
1451+
1452+ func (s * PrivateDebugAPI ) doReplayTransaction (txHash common.Hash ) ([]vm.StructLog , []byte , * big.Int , error ) {
1453+ // Retrieve the tx from the chain
1454+ tx , _ , blockIndex , _ := core .GetTransaction (s .eth .ChainDb (), txHash )
1455+
1456+ if tx == nil {
1457+ return nil , nil , nil , fmt .Errorf ("Transaction not found" )
1458+ }
1459+
1460+ block := s .eth .BlockChain ().GetBlockByNumber (blockIndex - 1 )
1461+ if block == nil {
1462+ return nil , nil , nil , fmt .Errorf ("Unable to retrieve prior block" )
1463+ }
1464+
1465+ // Create the state database
1466+ stateDb , err := state .New (block .Root (), s .eth .ChainDb ())
1467+ if err != nil {
1468+ return nil , nil , nil , err
1469+ }
1470+
1471+ txFrom , err := tx .From ()
1472+
1473+ if err != nil {
1474+ return nil , nil , nil , fmt .Errorf ("Unable to create transaction sender" )
1475+ }
1476+ from := stateDb .GetOrNewStateObject (txFrom )
1477+ msg := callmsg {
1478+ from : from ,
1479+ to : tx .To (),
1480+ gas : tx .Gas (),
1481+ gasPrice : tx .GasPrice (),
1482+ value : tx .Value (),
1483+ data : tx .Data (),
1484+ }
1485+
1486+ vmenv := core .NewEnv (stateDb , s .eth .BlockChain (), msg , block .Header ())
1487+ gp := new (core.GasPool ).AddGas (block .GasLimit ())
1488+ vm .GenerateStructLogs = true
1489+ defer func () { vm .GenerateStructLogs = false }()
1490+
1491+ ret , gas , err := core .ApplyMessage (vmenv , msg , gp )
1492+ if err != nil {
1493+ return nil , nil , nil , fmt .Errorf ("Error executing transaction %v" , err )
1494+ }
1495+
1496+ return vmenv .StructLogs (), ret , gas , nil
1497+ }
1498+
1499+ // Executes a transaction and returns the structured logs of the evm
1500+ // gathered during the execution
1501+ func (s * PrivateDebugAPI ) ReplayTransaction (txHash common.Hash , stackDepth int , memorySize int , storageSize int ) (* TransactionExecutionResult , error ) {
1502+
1503+ structLogs , ret , gas , err := s .doReplayTransaction (txHash )
1504+
1505+ if err != nil {
1506+ return nil , err
1507+ }
1508+
1509+ res := TransactionExecutionResult {
1510+ Gas : gas ,
1511+ ReturnValue : fmt .Sprintf ("%x" , ret ),
1512+ StructLogs : make ([]structLogRes , len (structLogs )),
1513+ }
1514+
1515+ for index , trace := range structLogs {
1516+
1517+ stackLength := len (trace .Stack )
1518+
1519+ // Return full stack by default
1520+ if stackDepth != - 1 && stackDepth < stackLength {
1521+ stackLength = stackDepth
1522+ }
1523+
1524+ res .StructLogs [index ] = structLogRes {
1525+ Pc : trace .Pc ,
1526+ Op : trace .Op .String (),
1527+ Gas : trace .Gas ,
1528+ GasCost : trace .GasCost ,
1529+ Error : trace .Err ,
1530+ Stack : make ([]string , stackLength ),
1531+ Memory : make (map [string ]string ),
1532+ Storage : make (map [string ]string ),
1533+ }
1534+
1535+ for i := 0 ; i < stackLength ; i ++ {
1536+ res .StructLogs [index ].Stack [i ] = fmt .Sprintf ("%x" , common .LeftPadBytes (trace .Stack [i ].Bytes (), 32 ))
1537+ }
1538+
1539+ addr := 0
1540+ memorySizeLocal := memorySize
1541+
1542+ // Return full memory by default
1543+ if memorySize == - 1 {
1544+ memorySizeLocal = len (trace .Memory )
1545+ }
1546+
1547+ for i := 0 ; i + 16 <= len (trace .Memory ) && addr < memorySizeLocal ; i += 16 {
1548+ res .StructLogs [index ].Memory [fmt .Sprintf ("%04d" , addr * 16 )] = fmt .Sprintf ("%x" , trace .Memory [i :i + 16 ])
1549+ addr ++
1550+ }
1551+
1552+ storageLength := len (trace .Stack )
1553+ if storageSize != - 1 && storageSize < storageLength {
1554+ storageLength = storageSize
1555+ }
1556+
1557+ i := 0
1558+ for storageIndex , storageValue := range trace .Storage {
1559+ if i >= storageLength {
1560+ break
1561+ }
1562+ res .StructLogs [index ].Storage [fmt .Sprintf ("%x" , storageIndex )] = fmt .Sprintf ("%x" , storageValue )
1563+ i ++
1564+ }
1565+ }
1566+ return & res , nil
1567+ }
1568+
14301569// PublicNetAPI offers network related RPC methods
14311570type PublicNetAPI struct {
14321571 net * p2p.Server
0 commit comments