2022-11-03 04:32:15 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
2022-11-30 01:31:39 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
|
2022-11-03 04:32:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
|
|
|
)
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func (api *OtterscanAPIImpl) TraceTransaction(ctx context.Context, hash libcommon.Hash) ([]*TraceEntry, error) {
|
2022-11-03 04:32:15 +00:00
|
|
|
tx, err := api.db.BeginRo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
tracer := NewTransactionTracer(ctx)
|
|
|
|
if _, err := api.runTracer(ctx, tx, hash, tracer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracer.Results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TraceEntry struct {
|
2023-01-13 18:12:18 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Depth int `json:"depth"`
|
|
|
|
From libcommon.Address `json:"from"`
|
|
|
|
To libcommon.Address `json:"to"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Input hexutil.Bytes `json:"input"`
|
2022-11-03 04:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionTracer struct {
|
|
|
|
DefaultTracer
|
|
|
|
ctx context.Context
|
|
|
|
Results []*TraceEntry
|
2022-12-18 16:11:31 +00:00
|
|
|
depth int // computed from CaptureStart, CaptureEnter, and CaptureExit calls
|
2022-11-03 04:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTransactionTracer(ctx context.Context) *TransactionTracer {
|
|
|
|
return &TransactionTracer{
|
|
|
|
ctx: ctx,
|
|
|
|
Results: make([]*TraceEntry, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func (t *TransactionTracer) captureStartOrEnter(typ vm.OpCode, from, to libcommon.Address, precompile bool, input []byte, value *uint256.Int) {
|
2022-11-03 04:32:15 +00:00
|
|
|
if precompile {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inputCopy := make([]byte, len(input))
|
|
|
|
copy(inputCopy, input)
|
|
|
|
_value := new(big.Int)
|
2022-12-20 06:29:12 +00:00
|
|
|
if value != nil {
|
|
|
|
_value.Set(value.ToBig())
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.CALL {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"CALL", t.depth, from, to, (*hexutil.Big)(_value), inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.STATICCALL {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"STATICCALL", t.depth, from, to, nil, inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.DELEGATECALL {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"DELEGATECALL", t.depth, from, to, nil, inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.CALLCODE {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"CALLCODE", t.depth, from, to, (*hexutil.Big)(_value), inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.CREATE {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"CREATE", t.depth, from, to, (*hexutil.Big)(value.ToBig()), inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 03:12:08 +00:00
|
|
|
if typ == vm.CREATE2 {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.Results = append(t.Results, &TraceEntry{"CREATE2", t.depth, from, to, (*hexutil.Big)(value.ToBig()), inputCopy})
|
2022-11-03 04:32:15 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-26 04:56:39 +00:00
|
|
|
|
|
|
|
if typ == vm.SELFDESTRUCT {
|
|
|
|
last := t.Results[len(t.Results)-1]
|
|
|
|
t.Results = append(t.Results, &TraceEntry{"SELFDESTRUCT", last.Depth + 1, from, to, (*hexutil.Big)(value.ToBig()), nil})
|
|
|
|
}
|
2022-11-03 04:32:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 22:28:50 +00:00
|
|
|
func (t *TransactionTracer) CaptureStart(env vm.VMInterface, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.depth = 0
|
2022-12-19 03:12:08 +00:00
|
|
|
t.captureStartOrEnter(vm.CALL, from, to, precompile, input, value)
|
2022-12-18 16:11:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func (t *TransactionTracer) CaptureEnter(typ vm.OpCode, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.depth++
|
2022-12-19 03:12:08 +00:00
|
|
|
t.captureStartOrEnter(typ, from, to, precompile, input, value)
|
2022-12-18 16:11:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 05:43:08 +00:00
|
|
|
func (t *TransactionTracer) CaptureExit(output []byte, usedGas uint64, err error) {
|
2022-12-18 16:11:31 +00:00
|
|
|
t.depth--
|
|
|
|
}
|