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