2020-06-28 06:10:27 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-29 15:50:24 +00:00
|
|
|
|
2021-05-06 17:37:38 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/tracers"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
|
|
|
"github.com/ledgerwatch/erigon/internal/ethapi"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/adapter"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/transactions"
|
2020-06-28 06:10:27 +00:00
|
|
|
)
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// TraceTransaction implements debug_traceTransaction. Returns Geth style transaction traces.
|
2021-05-06 17:37:38 +00:00
|
|
|
func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash common.Hash, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err := api.dbReader.BeginRo(ctx)
|
2020-10-10 12:24:56 +00:00
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2020-10-10 12:24:56 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2020-06-28 06:10:27 +00:00
|
|
|
// Retrieve the transaction and assemble its EVM context
|
2021-05-01 07:42:23 +00:00
|
|
|
txn, blockHash, _, txIndex := rawdb.ReadTransaction(tx, hash)
|
2020-10-10 12:24:56 +00:00
|
|
|
if txn == nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return fmt.Errorf("transaction %#x not found", hash)
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
2020-10-10 12:24:56 +00:00
|
|
|
getter := adapter.NewBlockGetter(tx)
|
2020-11-08 05:46:53 +00:00
|
|
|
|
2021-01-02 19:28:22 +00:00
|
|
|
chainConfig, err := api.chainConfig(tx)
|
2020-11-08 05:46:53 +00:00
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2020-11-08 05:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 07:23:21 +00:00
|
|
|
getHeader := func(hash common.Hash, number uint64) *types.Header {
|
|
|
|
return rawdb.ReadHeader(tx, hash, number)
|
|
|
|
}
|
|
|
|
msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, getter, chainConfig, getHeader, ethash.NewFaker(), tx, blockHash, txIndex)
|
2020-06-28 06:10:27 +00:00
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
|
|
|
// Trace the transaction and return
|
2021-05-06 17:37:38 +00:00
|
|
|
return transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream)
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
2021-01-18 11:13:19 +00:00
|
|
|
|
2021-05-06 17:37:38 +00:00
|
|
|
func (api *PrivateDebugAPIImpl) TraceCall(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
|
2021-04-03 06:26:00 +00:00
|
|
|
dbtx, err := api.dbReader.BeginRo(ctx)
|
2021-01-18 11:13:19 +00:00
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
defer dbtx.Rollback()
|
|
|
|
|
|
|
|
chainConfig, err := api.chainConfig(dbtx)
|
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 12:15:19 +00:00
|
|
|
blockNumber, hash, err := rpchelper.GetBlockNumber(blockNrOrHash, dbtx, api.filters)
|
2021-01-18 11:13:19 +00:00
|
|
|
if err != nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
var stateReader state.StateReader
|
|
|
|
if num, ok := blockNrOrHash.Number(); ok && num == rpc.LatestBlockNumber {
|
2021-04-05 13:04:58 +00:00
|
|
|
stateReader = state.NewPlainStateReader(dbtx)
|
2021-01-18 11:13:19 +00:00
|
|
|
} else {
|
2021-04-26 05:37:48 +00:00
|
|
|
stateReader = state.NewPlainKvState(dbtx, blockNumber)
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
header := rawdb.ReadHeader(ethdb.NewRoTxDb(dbtx), hash, blockNumber)
|
2021-01-18 11:13:19 +00:00
|
|
|
if header == nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
return fmt.Errorf("block %d(%x) not found", blockNumber, hash)
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
ibs := state.New(stateReader)
|
|
|
|
msg := args.ToMessage(api.GasCap)
|
2021-03-14 18:52:15 +00:00
|
|
|
blockCtx, txCtx := transactions.GetEvmContext(msg, header, blockNrOrHash.RequireCanonical, dbtx)
|
2021-01-18 11:13:19 +00:00
|
|
|
// Trace the transaction and return
|
2021-05-06 17:37:38 +00:00
|
|
|
return transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream)
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|