Skip to content

Commit 15780ea

Browse files
author
Peter Pratscher
committed
core/vm, rpc/api: renamed to debug.replayTransaction, migrated to new RPC, integrated feedback
Integrated code review suggestions Integrated last review comments
1 parent 528dcc3 commit 15780ea

4 files changed

Lines changed: 147 additions & 1 deletion

File tree

‎core/vm/common.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
// Global Debug flag indicating Debug VM (full logging)
2929
var Debug bool
3030

31+
var GenerateStructLogs bool = false
32+
3133
// Type is the VM type accepted by **NewVm**
3234
type Type byte
3335

‎core/vm/vm.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, contract *Co
367367
// log emits a log event to the environment for each opcode encountered. This is not to be confused with the
368368
// LOG* opcode.
369369
func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, contract *Contract, err error) {
370-
if Debug {
370+
if Debug || GenerateStructLogs {
371371
mem := make([]byte, len(memory.Data()))
372372
copy(mem, memory.Data())
373373

‎eth/api.go‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
14311570
type PublicNetAPI struct {
14321571
net *p2p.Server

‎rpc/javascript.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ web3._extend({
399399
call: 'debug_writeMemProfile',
400400
params: 1
401401
}),
402+
new web3._extend.Method({
403+
name: 'replayTransaction',
404+
call: 'debug_replayTransaction',
405+
params: 4
406+
})
402407
],
403408
properties:
404409
[

0 commit comments

Comments
 (0)