forked from cerc-io/laconicd-deprecated
conflicts
This commit is contained in:
+61
-44
@@ -1,88 +1,105 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
|
||||
rpctypes "github.com/cosmos/ethermint/rpc/types"
|
||||
"github.com/cosmos/ethermint/x/evm/types"
|
||||
)
|
||||
|
||||
// GetQueryCmd defines evm module queries through the cli
|
||||
func GetQueryCmd(moduleName string, cdc *codec.Codec) *cobra.Command {
|
||||
evmQueryCmd := &cobra.Command{
|
||||
// GetQueryCmd returns the parent command for all x/bank CLi query commands.
|
||||
func GetQueryCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: types.ModuleName,
|
||||
Short: "Querying commands for the evm module",
|
||||
DisableFlagParsing: true,
|
||||
SuggestionsMinimumDistance: 2,
|
||||
RunE: client.ValidateCmd,
|
||||
}
|
||||
evmQueryCmd.AddCommand(flags.GetCommands(
|
||||
GetCmdGetStorageAt(moduleName, cdc),
|
||||
GetCmdGetCode(moduleName, cdc),
|
||||
)...)
|
||||
return evmQueryCmd
|
||||
|
||||
cmd.AddCommand(
|
||||
GetStorageCmd(),
|
||||
GetCodeCmd(),
|
||||
)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdGetStorageAt queries a key in an accounts storage
|
||||
func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "storage [account] [key]",
|
||||
Short: "Gets storage for an account at a given key",
|
||||
// GetStorageCmd queries a key in an accounts storage
|
||||
func GetStorageCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "storage [address] [key]",
|
||||
Short: "Gets storage for an account with a given key and height",
|
||||
Long: "Gets storage for an account with a given key and height. If the height is not provided, it will use the latest height from context.",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
account, err := accountToHex(args[0])
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not parse account address")
|
||||
return err
|
||||
}
|
||||
|
||||
queryClient := types.NewQueryClient(clientCtx)
|
||||
|
||||
address, err := accountToHex(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key := formatKeyToHash(args[1])
|
||||
|
||||
res, _, err := clientCtx.Query(
|
||||
fmt.Sprintf("custom/%s/storage/%s/%s", queryRoute, account, key))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not resolve: %s", err)
|
||||
req := &types.QueryStorageRequest{
|
||||
Address: address,
|
||||
Key: key,
|
||||
}
|
||||
var out types.QueryResStorage
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return clientCtx.PrintOutput(out)
|
||||
|
||||
res, err := queryClient.Storage(rpctypes.ContextWithHeight(clientCtx.Height), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintProto(res)
|
||||
},
|
||||
}
|
||||
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdGetCode queries the code field of a given address
|
||||
func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "code [account]",
|
||||
// GetCodeCmd queries the code field of a given address
|
||||
func GetCodeCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "code [address]",
|
||||
Short: "Gets code from an account",
|
||||
Long: "Gets code from an account. If the height is not provided, it will use the latest height from context.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
account, err := accountToHex(args[0])
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not parse account address")
|
||||
return err
|
||||
}
|
||||
|
||||
res, _, err := clientCtx.Query(
|
||||
fmt.Sprintf("custom/%s/code/%s", queryRoute, account))
|
||||
queryClient := types.NewQueryClient(clientCtx)
|
||||
|
||||
address, err := accountToHex(args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not resolve: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var out types.QueryResCode
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return clientCtx.PrintOutput(out)
|
||||
req := &types.QueryCodeRequest{
|
||||
Address: address,
|
||||
}
|
||||
|
||||
res, err := queryClient.Code(rpctypes.ContextWithHeight(clientCtx.Height), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintProto(res)
|
||||
},
|
||||
}
|
||||
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
+32
-33
@@ -1,90 +1,89 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
|
||||
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
||||
|
||||
rpctypes "github.com/cosmos/ethermint/rpc/types"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// RegisterRoutes - Central function to define routes that get registered by the main application
|
||||
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(cliCtx)).Methods("GET")
|
||||
r.HandleFunc("/txs", authrest.QueryTxsRequestHandlerFn(cliCtx)).Methods("GET") // default from auth
|
||||
r.HandleFunc("/txs", authrest.BroadcastTxRequest(cliCtx)).Methods("POST") // default from auth
|
||||
r.HandleFunc("/txs/encode", authrest.EncodeTxRequestHandlerFn(cliCtx)).Methods("POST") // default from auth
|
||||
r.HandleFunc("/txs/decode", authrest.DecodeTxRequestHandlerFn(cliCtx)).Methods("POST") // default from auth
|
||||
// RegisterTxRoutes - Central function to define routes that get registered by the main application
|
||||
func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) {
|
||||
r := clientrest.WithHTTPDeprecationHeaders(rtr)
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/txs", authrest.QueryTxsRequestHandlerFn(clientCtx)).Methods("GET")
|
||||
r.HandleFunc("/txs", authrest.BroadcastTxRequest(clientCtx)).Methods("POST")
|
||||
r.HandleFunc("/txs/encode", authrest.EncodeTxRequestHandlerFn(clientCtx)).Methods("POST")
|
||||
r.HandleFunc("/txs/decode", authrest.DecodeTxRequestHandlerFn(clientCtx)).Methods("POST")
|
||||
}
|
||||
|
||||
func QueryTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
|
||||
// by hash in a committed block.
|
||||
func QueryTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
hashHexStr := vars["hash"]
|
||||
|
||||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
||||
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ethHashPrefix := "0x"
|
||||
if strings.HasPrefix(hashHexStr, ethHashPrefix) {
|
||||
// eth Tx
|
||||
ethHashPrefixLength := len(ethHashPrefix)
|
||||
output, err := getEthTransactionByHash(cliCtx, hashHexStr[ethHashPrefixLength:])
|
||||
if err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
rest.PostProcessResponseBare(w, cliCtx, output)
|
||||
if !strings.HasPrefix(hashHexStr, ethHashPrefix) {
|
||||
authrest.QueryTxRequestHandlerFn(clientCtx)
|
||||
return
|
||||
}
|
||||
|
||||
output, err := utils.QueryTx(cliCtx, hashHexStr)
|
||||
// eth Tx
|
||||
ethHashPrefixLength := len(ethHashPrefix)
|
||||
output, err := getEthTransactionByHash(clientCtx, hashHexStr[ethHashPrefixLength:])
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), hashHexStr) {
|
||||
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
rest.PostProcessResponseBare(w, cliCtx, output)
|
||||
}
|
||||
|
||||
rest.PostProcessResponseBare(w, clientCtx, output)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTransactionByHash returns the transaction identified by hash.
|
||||
func getEthTransactionByHash(cliCtx context.CLIContext, hashHex string) ([]byte, error) {
|
||||
func getEthTransactionByHash(clientCtx client.Context, hashHex string) ([]byte, error) {
|
||||
hash, err := hex.DecodeString(hashHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := cliCtx.GetNode()
|
||||
node, err := clientCtx.GetNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx, err := node.Tx(hash, false)
|
||||
|
||||
tx, err := node.Tx(context.Background(), hash, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Can either cache or just leave this out if not necessary
|
||||
block, err := node.Block(&tx.Height)
|
||||
block, err := node.Block(context.Background(), &tx.Height)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockHash := common.BytesToHash(block.Block.Hash())
|
||||
|
||||
ethTx, err := rpctypes.RawTxToEthTx(cliCtx, tx.Tx)
|
||||
ethTx, err := rpctypes.RawTxToEthTx(clientCtx, tx.Tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user