Only one CoreContext for all REST commands
This commit is contained in:
parent
228bc4add9
commit
fd40d39556
|
@ -13,6 +13,7 @@ import (
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
|
|
||||||
client "github.com/cosmos/cosmos-sdk/client"
|
client "github.com/cosmos/cosmos-sdk/client"
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
||||||
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
|
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
|
||||||
tx "github.com/cosmos/cosmos-sdk/client/tx"
|
tx "github.com/cosmos/cosmos-sdk/client/tx"
|
||||||
|
@ -73,12 +74,14 @@ func createHandler(cdc *wire.Codec) http.Handler {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := context.NewCoreContextFromViper()
|
||||||
|
|
||||||
// TODO make more functional? aka r = keys.RegisterRoutes(r)
|
// TODO make more functional? aka r = keys.RegisterRoutes(r)
|
||||||
keys.RegisterRoutes(r)
|
keys.RegisterRoutes(r)
|
||||||
rpc.RegisterRoutes(r)
|
rpc.RegisterRoutes(ctx, r)
|
||||||
tx.RegisterRoutes(r, cdc)
|
tx.RegisterRoutes(ctx, r, cdc)
|
||||||
auth.RegisterRoutes(r, cdc, "main")
|
auth.RegisterRoutes(ctx, r, cdc, "main")
|
||||||
bank.RegisterRoutes(r, cdc, kb)
|
bank.RegisterRoutes(ctx, r, cdc, kb)
|
||||||
ibc.RegisterRoutes(r, cdc, kb)
|
ibc.RegisterRoutes(ctx, r, cdc, kb)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,9 +29,8 @@ func blockCommand() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlock(height *int64) ([]byte, error) {
|
func getBlock(ctx context.CoreContext, height *int64) ([]byte, error) {
|
||||||
// get the node
|
// get the node
|
||||||
ctx := context.NewCoreContextFromViper()
|
|
||||||
node, err := ctx.GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -56,8 +55,8 @@ func getBlock(height *int64) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the current blockchain height
|
// get the current blockchain height
|
||||||
func GetChainHeight() (int64, error) {
|
func GetChainHeight(ctx context.CoreContext) (int64, error) {
|
||||||
node, err := context.NewCoreContextFromViper().GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
@ -85,7 +84,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := getBlock(height)
|
output, err := getBlock(context.NewCoreContextFromViper(), height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -96,42 +95,46 @@ func printBlock(cmd *cobra.Command, args []string) error {
|
||||||
// REST
|
// REST
|
||||||
|
|
||||||
// REST handler to get a block
|
// REST handler to get a block
|
||||||
func BlockRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func BlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
vars := mux.Vars(r)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
vars := mux.Vars(r)
|
||||||
if err != nil {
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
||||||
w.WriteHeader(400)
|
if err != nil {
|
||||||
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'."))
|
w.WriteHeader(400)
|
||||||
return
|
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chainHeight, err := GetChainHeight(ctx)
|
||||||
|
if height > chainHeight {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output, err := getBlock(ctx, &height)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(output)
|
||||||
}
|
}
|
||||||
chainHeight, err := GetChainHeight()
|
|
||||||
if height > chainHeight {
|
|
||||||
w.WriteHeader(404)
|
|
||||||
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output, err := getBlock(&height)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write(output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// REST handler to get the latest block
|
// REST handler to get the latest block
|
||||||
func LatestBlockRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func LatestBlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
height, err := GetChainHeight()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
height, err := GetChainHeight(ctx)
|
||||||
w.WriteHeader(500)
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.WriteHeader(500)
|
||||||
return
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output, err := getBlock(ctx, &height)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(output)
|
||||||
}
|
}
|
||||||
output, err := getBlock(&height)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write(output)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/client"
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -45,11 +46,11 @@ func initClientCommand() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register REST endpoints
|
// Register REST endpoints
|
||||||
func RegisterRoutes(r *mux.Router) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router) {
|
||||||
r.HandleFunc("/node_info", NodeInfoRequestHandler).Methods("GET")
|
r.HandleFunc("/node_info", NodeInfoRequestHandler(ctx)).Methods("GET")
|
||||||
r.HandleFunc("/syncing", NodeSyncingRequestHandler).Methods("GET")
|
r.HandleFunc("/syncing", NodeSyncingRequestHandler(ctx)).Methods("GET")
|
||||||
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler).Methods("GET")
|
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler(ctx)).Methods("GET")
|
||||||
r.HandleFunc("/blocks/{height}", BlockRequestHandler).Methods("GET")
|
r.HandleFunc("/blocks/{height}", BlockRequestHandler(ctx)).Methods("GET")
|
||||||
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandler).Methods("GET")
|
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandler(ctx)).Methods("GET")
|
||||||
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandler).Methods("GET")
|
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandler(ctx)).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,9 @@ func statusCommand() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNodeStatus() (*ctypes.ResultStatus, error) {
|
func getNodeStatus(ctx context.CoreContext) (*ctypes.ResultStatus, error) {
|
||||||
// get the node
|
// get the node
|
||||||
node, err := context.NewCoreContextFromViper().GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &ctypes.ResultStatus{}, err
|
return &ctypes.ResultStatus{}, err
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func getNodeStatus() (*ctypes.ResultStatus, error) {
|
||||||
// CMD
|
// CMD
|
||||||
|
|
||||||
func printNodeStatus(cmd *cobra.Command, args []string) error {
|
func printNodeStatus(cmd *cobra.Command, args []string) error {
|
||||||
status, err := getNodeStatus()
|
status, err := getNodeStatus(context.NewCoreContextFromViper())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -52,38 +52,42 @@ func printNodeStatus(cmd *cobra.Command, args []string) error {
|
||||||
// REST
|
// REST
|
||||||
|
|
||||||
// REST handler for node info
|
// REST handler for node info
|
||||||
func NodeInfoRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func NodeInfoRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
|
||||||
status, err := getNodeStatus()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
status, err := getNodeStatus(ctx)
|
||||||
w.WriteHeader(500)
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.WriteHeader(500)
|
||||||
return
|
w.Write([]byte(err.Error()))
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
nodeInfo := status.NodeInfo
|
nodeInfo := status.NodeInfo
|
||||||
output, err := cdc.MarshalJSON(nodeInfo)
|
output, err := cdc.MarshalJSON(nodeInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
w.Write(output)
|
||||||
}
|
}
|
||||||
w.Write(output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// REST handler for node syncing
|
// REST handler for node syncing
|
||||||
func NodeSyncingRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func NodeSyncingRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
|
||||||
status, err := getNodeStatus()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
status, err := getNodeStatus(ctx)
|
||||||
w.WriteHeader(500)
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.WriteHeader(500)
|
||||||
return
|
w.Write([]byte(err.Error()))
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
syncing := status.SyncInfo.Syncing
|
syncing := status.SyncInfo.Syncing
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
w.Write([]byte(strconv.FormatBool(syncing)))
|
||||||
}
|
}
|
||||||
w.Write([]byte(strconv.FormatBool(syncing)))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,9 +26,9 @@ func validatorCommand() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func getValidators(height *int64) ([]byte, error) {
|
func getValidators(ctx context.CoreContext, height *int64) ([]byte, error) {
|
||||||
// get the node
|
// get the node
|
||||||
node, err := context.NewCoreContextFromViper().GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ func printValidators(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := getValidators(height)
|
output, err := getValidators(context.NewCoreContextFromViper(), height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -73,42 +73,46 @@ func printValidators(cmd *cobra.Command, args []string) error {
|
||||||
// REST
|
// REST
|
||||||
|
|
||||||
// Validator Set at a height REST handler
|
// Validator Set at a height REST handler
|
||||||
func ValidatorSetRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func ValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
vars := mux.Vars(r)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
vars := mux.Vars(r)
|
||||||
if err != nil {
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
||||||
w.WriteHeader(400)
|
if err != nil {
|
||||||
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
|
w.WriteHeader(400)
|
||||||
return
|
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chainHeight, err := GetChainHeight(ctx)
|
||||||
|
if height > chainHeight {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output, err := getValidators(ctx, &height)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(output)
|
||||||
}
|
}
|
||||||
chainHeight, err := GetChainHeight()
|
|
||||||
if height > chainHeight {
|
|
||||||
w.WriteHeader(404)
|
|
||||||
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output, err := getValidators(&height)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write(output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Latest Validator Set REST handler
|
// Latest Validator Set REST handler
|
||||||
func LatestValidatorSetRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func LatestValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
height, err := GetChainHeight()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
height, err := GetChainHeight(ctx)
|
||||||
w.WriteHeader(500)
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.WriteHeader(500)
|
||||||
return
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output, err := getValidators(ctx, &height)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(output)
|
||||||
}
|
}
|
||||||
output, err := getValidators(&height)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write(output)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,23 +13,25 @@ type BroadcastTxBody struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastTx REST Handler
|
// BroadcastTx REST Handler
|
||||||
func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func BroadcastTxRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
var m BroadcastTxBody
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var m BroadcastTxBody
|
||||||
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
err := decoder.Decode(&m)
|
err := decoder.Decode(&m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := ctx.BroadcastTx([]byte(m.TxBytes))
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte(string(res.Height)))
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := context.NewCoreContextFromViper().BroadcastTx([]byte(m.TxBytes))
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write([]byte(string(res.Height)))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
hashHexStr := args[0]
|
hashHexStr := args[0]
|
||||||
trustNode := viper.GetBool(client.FlagTrustNode)
|
trustNode := viper.GetBool(client.FlagTrustNode)
|
||||||
|
|
||||||
output, err := queryTx(cdc, hashHexStr, trustNode)
|
output, err := queryTx(context.NewCoreContextFromViper(), cdc, hashHexStr, trustNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -51,14 +51,14 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryTx(cdc *wire.Codec, hashHexStr string, trustNode bool) ([]byte, error) {
|
func queryTx(ctx context.CoreContext, cdc *wire.Codec, hashHexStr string, trustNode bool) ([]byte, error) {
|
||||||
hash, err := hex.DecodeString(hashHexStr)
|
hash, err := hex.DecodeString(hashHexStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the node
|
// get the node
|
||||||
node, err := context.NewCoreContextFromViper().GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) {
|
||||||
// REST
|
// REST
|
||||||
|
|
||||||
// transaction query REST handler
|
// transaction query REST handler
|
||||||
func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
func QueryTxRequestHandler(cdc *wire.Codec, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
hashHexStr := vars["hash"]
|
hashHexStr := vars["hash"]
|
||||||
|
@ -119,7 +119,7 @@ func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Requ
|
||||||
trustNode = true
|
trustNode = true
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := queryTx(cdc, hashHexStr, trustNode)
|
output, err := queryTx(ctx, cdc, hashHexStr, trustNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,8 +17,8 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// register REST routes
|
// register REST routes
|
||||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) {
|
||||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc)).Methods("GET")
|
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc, ctx)).Methods("GET")
|
||||||
// r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET")
|
// r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET")
|
||||||
// r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
|
// r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
|
||||||
// r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")
|
// r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")
|
||||||
|
|
|
@ -29,7 +29,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
tags := viper.GetStringSlice(flagTags)
|
tags := viper.GetStringSlice(flagTags)
|
||||||
|
|
||||||
output, err := searchTx(cdc, tags)
|
output, err := searchTx(context.NewCoreContextFromViper(), cdc, tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchTx(cdc *wire.Codec, tags []string) ([]byte, error) {
|
func searchTx(ctx context.CoreContext, cdc *wire.Codec, tags []string) ([]byte, error) {
|
||||||
if len(tags) == 0 {
|
if len(tags) == 0 {
|
||||||
return nil, errors.New("Must declare at least one tag to search")
|
return nil, errors.New("Must declare at least one tag to search")
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ func searchTx(cdc *wire.Codec, tags []string) ([]byte, error) {
|
||||||
query := strings.Join(tags, " AND ")
|
query := strings.Join(tags, " AND ")
|
||||||
|
|
||||||
// get the node
|
// get the node
|
||||||
node, err := context.NewCoreContextFromViper().GetNode()
|
node, err := ctx.GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error)
|
||||||
// REST
|
// REST
|
||||||
|
|
||||||
// Search Tx REST Handler
|
// Search Tx REST Handler
|
||||||
func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
func SearchTxRequestHandler(ctx context.CoreContext, cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tag := r.FormValue("tag")
|
tag := r.FormValue("tag")
|
||||||
if tag == "" {
|
if tag == "" {
|
||||||
|
@ -104,7 +104,7 @@ func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Req
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := []string{tag}
|
tags := []string{tag}
|
||||||
output, err := searchTx(cdc, tags)
|
output, err := searchTx(ctx, cdc, tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
|
|
@ -14,9 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// register REST routes
|
// register REST routes
|
||||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, storeName string) {
|
||||||
ctx := context.NewCoreContextFromViper()
|
|
||||||
|
|
||||||
r.HandleFunc(
|
r.HandleFunc(
|
||||||
"/accounts/{address}",
|
"/accounts/{address}",
|
||||||
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx),
|
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx),
|
||||||
|
|
|
@ -16,9 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterRoutes - Central function to define routes that get registered by the main application
|
// RegisterRoutes - Central function to define routes that get registered by the main application
|
||||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
||||||
ctx := context.NewCoreContextFromViper()
|
|
||||||
|
|
||||||
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb, ctx)).Methods("POST")
|
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb, ctx)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterRoutes - Central function to define routes that get registered by the main application
|
// RegisterRoutes - Central function to define routes that get registered by the main application
|
||||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
||||||
ctx := context.NewCoreContextFromViper()
|
|
||||||
|
|
||||||
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb, ctx)).Methods("POST")
|
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb, ctx)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterRoutes - Central function to define routes that get registered by the main application
|
// RegisterRoutes - Central function to define routes that get registered by the main application
|
||||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
||||||
ctx := context.NewCoreContextFromViper()
|
|
||||||
|
|
||||||
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET")
|
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue