github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/rpc/namespaces/debug/api.go (about)

     1  package debug
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/spf13/viper"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	clientcontext "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  
    13  	"github.com/fibonacci-chain/fbc/app/rpc/backend"
    14  	"github.com/fibonacci-chain/fbc/app/rpc/monitor"
    15  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    16  
    17  	evmtypes "github.com/fibonacci-chain/fbc/x/evm/types"
    18  )
    19  
    20  const (
    21  	NameSpace = "debug"
    22  )
    23  
    24  // PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
    25  type PublicDebugAPI struct {
    26  	clientCtx clientcontext.CLIContext
    27  	logger    log.Logger
    28  	backend   backend.Backend
    29  	Metrics   *monitor.RpcMetrics
    30  }
    31  
    32  // NewPublicTxPoolAPI creates a new tx pool service that gives information about the transaction pool.
    33  func NewAPI(clientCtx clientcontext.CLIContext, log log.Logger, backend backend.Backend) *PublicDebugAPI {
    34  	api := &PublicDebugAPI{
    35  		clientCtx: clientCtx,
    36  		backend:   backend,
    37  		logger:    log.With("module", "json-rpc", "namespace", "debug"),
    38  	}
    39  	if viper.GetBool(monitor.FlagEnableMonitor) {
    40  		api.Metrics = monitor.MakeMonitorMetrics(NameSpace)
    41  	}
    42  	return api
    43  }
    44  
    45  // TraceTransaction returns the structured logs created during the execution of EVM
    46  // and returns them as a JSON object.
    47  func (api *PublicDebugAPI) TraceTransaction(txHash common.Hash, config evmtypes.TraceConfig) (interface{}, error) {
    48  	monitor := monitor.GetMonitor("debug_traceTransaction", api.logger, api.Metrics).OnBegin()
    49  	defer monitor.OnEnd()
    50  	err := evmtypes.TestTracerConfig(&config)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("tracer err : %s", err.Error())
    53  	}
    54  	configBytes, err := json.Marshal(config)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	queryParam := sdk.QueryTraceTx{
    59  		TxHash:      txHash,
    60  		ConfigBytes: configBytes,
    61  	}
    62  	queryBytes, err := json.Marshal(&queryParam)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	_, err = api.clientCtx.Client.Tx(txHash.Bytes(), false)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	resTrace, _, err := api.clientCtx.QueryWithData("app/trace", queryBytes)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	var res sdk.Result
    76  	if err := api.clientCtx.Codec.UnmarshalBinaryBare(resTrace, &res); err != nil {
    77  		return nil, err
    78  	}
    79  	var decodedResult interface{}
    80  	if err := json.Unmarshal(res.Data, &decodedResult); err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	return decodedResult, nil
    85  }