2020-06-28 06:10:27 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-29 15:50:24 +00:00
|
|
|
|
2021-07-11 04:05:56 +00:00
|
|
|
"github.com/holiman/uint256"
|
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"
|
2022-03-16 16:21:40 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/tracers"
|
2021-08-10 02:48:56 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/internal/ethapi"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/transactions"
|
2020-06-28 06:10:27 +00:00
|
|
|
)
|
|
|
|
|
2022-02-19 08:15:27 +00:00
|
|
|
// TraceBlockByNumber implements debug_traceBlockByNumber. Returns Geth style block traces.
|
|
|
|
func (api *PrivateDebugAPIImpl) TraceBlockByNumber(ctx context.Context, blockNum rpc.BlockNumber, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
|
|
|
|
return api.traceBlock(ctx, rpc.BlockNumberOrHashWithNumber(blockNum), config, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceBlockByHash implements debug_traceBlockByHash. Returns Geth style block traces.
|
|
|
|
func (api *PrivateDebugAPIImpl) TraceBlockByHash(ctx context.Context, hash common.Hash, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
|
|
|
|
return api.traceBlock(ctx, rpc.BlockNumberOrHashWithHash(hash, true), config, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig, stream *jsoniter.Stream) error {
|
|
|
|
tx, err := api.db.BeginRo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
var block *types.Block
|
|
|
|
if number, ok := blockNrOrHash.Number(); ok {
|
|
|
|
block, err = api.blockByRPCNumber(number, tx)
|
|
|
|
} else if hash, ok := blockNrOrHash.Hash(); ok {
|
|
|
|
block, err = api.blockByHashWithSenders(tx, hash)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid arguments; neither block nor hash specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chainConfig, err := api.chainConfig(tx)
|
|
|
|
if err != nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contractHasTEVM := func(contractHash common.Hash) (bool, error) { return false, nil }
|
|
|
|
if api.TevmEnabled {
|
|
|
|
contractHasTEVM = ethdb.GetHasTEVM(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeader := func(hash common.Hash, number uint64) *types.Header {
|
|
|
|
return rawdb.ReadHeader(tx, hash, number)
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:21:40 +00:00
|
|
|
_, blockCtx, _, ibs, reader, err := transactions.ComputeTxEnv(ctx, block, chainConfig, getHeader, contractHasTEVM, ethash.NewFaker(), tx, block.Hash(), 0)
|
2022-02-19 08:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signer := types.MakeSigner(chainConfig, block.NumberU64())
|
2022-05-26 16:20:34 +00:00
|
|
|
rules := chainConfig.Rules(block.NumberU64())
|
2022-02-19 08:15:27 +00:00
|
|
|
stream.WriteArrayStart()
|
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
|
|
|
stream.WriteNil()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
ibs.Prepare(tx.Hash(), block.Hash(), idx)
|
2022-05-26 16:20:34 +00:00
|
|
|
msg, _ := tx.AsMessage(*signer, block.BaseFee(), rules)
|
2022-03-16 16:21:40 +00:00
|
|
|
txCtx := vm.TxContext{
|
|
|
|
TxHash: tx.Hash(),
|
|
|
|
Origin: msg.From(),
|
|
|
|
GasPrice: msg.GasPrice().ToBig(),
|
|
|
|
}
|
2022-02-19 08:15:27 +00:00
|
|
|
|
|
|
|
transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream)
|
2022-05-26 16:20:34 +00:00
|
|
|
_ = ibs.FinalizeTx(rules, reader)
|
2022-02-19 08:15:27 +00:00
|
|
|
if idx != len(block.Transactions())-1 {
|
|
|
|
stream.WriteMore()
|
|
|
|
}
|
|
|
|
stream.Flush()
|
|
|
|
}
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
stream.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-06-11 08:34:47 +00:00
|
|
|
tx, err := api.db.BeginRo(ctx)
|
2020-10-10 12:24:56 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
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
|
2022-01-07 13:52:38 +00:00
|
|
|
blockNum, ok, err := api.txnLookup(ctx, tx, hash)
|
2022-01-06 11:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
if !ok {
|
2022-01-06 11:22:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
block, err := api.blockByNumberWithSenders(tx, blockNum)
|
2021-07-11 05:25:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
if block == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blockHash := block.Hash()
|
|
|
|
var txnIndex uint64
|
|
|
|
var txn types.Transaction
|
|
|
|
for i, transaction := range block.Transactions() {
|
|
|
|
if transaction.Hash() == hash {
|
|
|
|
txnIndex = uint64(i)
|
|
|
|
txn = transaction
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 12:24:56 +00:00
|
|
|
if txn == nil {
|
2022-02-07 21:30:46 +00:00
|
|
|
var borTx *types.Transaction
|
|
|
|
borTx, _, _, _, err = rawdb.ReadBorTransaction(tx, hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if borTx != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
2021-05-06 17:37:38 +00:00
|
|
|
return fmt.Errorf("transaction %#x not found", hash)
|
2020-06-28 06:10:27 +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-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
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)
|
|
|
|
}
|
2021-10-06 09:09:47 +00:00
|
|
|
contractHasTEVM := func(contractHash common.Hash) (bool, error) { return false, nil }
|
|
|
|
if api.TevmEnabled {
|
|
|
|
contractHasTEVM = ethdb.GetHasTEVM(tx)
|
|
|
|
}
|
2022-01-07 13:52:38 +00:00
|
|
|
msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, block, chainConfig, getHeader, contractHasTEVM, ethash.NewFaker(), tx, blockHash, txnIndex)
|
2020-06-28 06:10:27 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
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-06-11 08:34:47 +00:00
|
|
|
dbtx, err := api.db.BeginRo(ctx)
|
2021-01-18 11:13:19 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
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-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 23:42:14 +00:00
|
|
|
blockNumber, hash, latest, err := rpchelper.GetBlockNumber(blockNrOrHash, dbtx, api.filters)
|
2021-01-18 11:13:19 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
|
|
|
var stateReader state.StateReader
|
2022-02-23 23:42:14 +00:00
|
|
|
if latest {
|
2021-09-29 01:36:25 +00:00
|
|
|
cacheView, err := api.stateCache.View(ctx, dbtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stateReader = state.NewCachedReader2(cacheView, dbtx)
|
2021-01-18 11:13:19 +00:00
|
|
|
} else {
|
2021-07-24 07:14:11 +00:00
|
|
|
stateReader = state.NewPlainState(dbtx, blockNumber)
|
2021-01-18 11:13:19 +00:00
|
|
|
}
|
2021-05-31 10:12:48 +00:00
|
|
|
header := rawdb.ReadHeader(dbtx, hash, blockNumber)
|
2021-01-18 11:13:19 +00:00
|
|
|
if header == nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
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)
|
2021-07-11 04:05:56 +00:00
|
|
|
|
2022-03-01 15:40:24 +00:00
|
|
|
if config != nil && config.StateOverrides != nil {
|
|
|
|
if err := config.StateOverrides.Override(ibs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 04:05:56 +00:00
|
|
|
var baseFee *uint256.Int
|
|
|
|
if header != nil && header.BaseFee != nil {
|
|
|
|
var overflow bool
|
|
|
|
baseFee, overflow = uint256.FromBig(header.BaseFee)
|
|
|
|
if overflow {
|
|
|
|
return fmt.Errorf("header.BaseFee uint256 overflow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg, err := args.ToMessage(api.GasCap, baseFee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-10 02:48:56 +00:00
|
|
|
|
2021-10-06 09:09:47 +00:00
|
|
|
contractHasTEVM := func(contractHash common.Hash) (bool, error) { return false, nil }
|
|
|
|
if api.TevmEnabled {
|
|
|
|
contractHasTEVM = ethdb.GetHasTEVM(dbtx)
|
|
|
|
}
|
|
|
|
blockCtx, txCtx := transactions.GetEvmContext(msg, header, blockNrOrHash.RequireCanonical, dbtx, contractHasTEVM)
|
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
|
|
|
}
|