Only one CoreContext for all REST commands

This commit is contained in:
Christopher Goes 2018-04-25 16:49:31 +02:00
parent 228bc4add9
commit fd40d39556
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
13 changed files with 167 additions and 157 deletions

View File

@ -13,6 +13,7 @@ import (
cmn "github.com/tendermint/tmlibs/common"
client "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
keys "github.com/cosmos/cosmos-sdk/client/keys"
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
tx "github.com/cosmos/cosmos-sdk/client/tx"
@ -73,12 +74,14 @@ func createHandler(cdc *wire.Codec) http.Handler {
panic(err)
}
ctx := context.NewCoreContextFromViper()
// TODO make more functional? aka r = keys.RegisterRoutes(r)
keys.RegisterRoutes(r)
rpc.RegisterRoutes(r)
tx.RegisterRoutes(r, cdc)
auth.RegisterRoutes(r, cdc, "main")
bank.RegisterRoutes(r, cdc, kb)
ibc.RegisterRoutes(r, cdc, kb)
rpc.RegisterRoutes(ctx, r)
tx.RegisterRoutes(ctx, r, cdc)
auth.RegisterRoutes(ctx, r, cdc, "main")
bank.RegisterRoutes(ctx, r, cdc, kb)
ibc.RegisterRoutes(ctx, r, cdc, kb)
return r
}

View File

@ -29,9 +29,8 @@ func blockCommand() *cobra.Command {
return cmd
}
func getBlock(height *int64) ([]byte, error) {
func getBlock(ctx context.CoreContext, height *int64) ([]byte, error) {
// get the node
ctx := context.NewCoreContextFromViper()
node, err := ctx.GetNode()
if err != nil {
return nil, err
@ -56,8 +55,8 @@ func getBlock(height *int64) ([]byte, error) {
}
// get the current blockchain height
func GetChainHeight() (int64, error) {
node, err := context.NewCoreContextFromViper().GetNode()
func GetChainHeight(ctx context.CoreContext) (int64, error) {
node, err := ctx.GetNode()
if err != nil {
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 {
return err
}
@ -96,42 +95,46 @@ func printBlock(cmd *cobra.Command, args []string) error {
// REST
// REST handler to get a block
func BlockRequestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, err := strconv.ParseInt(vars["height"], 10, 64)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'."))
return
func BlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, err := strconv.ParseInt(vars["height"], 10, 64)
if err != nil {
w.WriteHeader(400)
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
func LatestBlockRequestHandler(w http.ResponseWriter, r *http.Request) {
height, err := GetChainHeight()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
func LatestBlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
height, err := GetChainHeight(ctx)
if err != nil {
w.WriteHeader(500)
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)
}

View File

@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
)
const (
@ -45,11 +46,11 @@ func initClientCommand() *cobra.Command {
}
// Register REST endpoints
func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/node_info", NodeInfoRequestHandler).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandler).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler).Methods("GET")
r.HandleFunc("/blocks/{height}", BlockRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandler).Methods("GET")
func RegisterRoutes(ctx context.CoreContext, r *mux.Router) {
r.HandleFunc("/node_info", NodeInfoRequestHandler(ctx)).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandler(ctx)).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler(ctx)).Methods("GET")
r.HandleFunc("/blocks/{height}", BlockRequestHandler(ctx)).Methods("GET")
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandler(ctx)).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandler(ctx)).Methods("GET")
}

View File

@ -22,9 +22,9 @@ func statusCommand() *cobra.Command {
return cmd
}
func getNodeStatus() (*ctypes.ResultStatus, error) {
func getNodeStatus(ctx context.CoreContext) (*ctypes.ResultStatus, error) {
// get the node
node, err := context.NewCoreContextFromViper().GetNode()
node, err := ctx.GetNode()
if err != nil {
return &ctypes.ResultStatus{}, err
}
@ -34,7 +34,7 @@ func getNodeStatus() (*ctypes.ResultStatus, error) {
// CMD
func printNodeStatus(cmd *cobra.Command, args []string) error {
status, err := getNodeStatus()
status, err := getNodeStatus(context.NewCoreContextFromViper())
if err != nil {
return err
}
@ -52,38 +52,42 @@ func printNodeStatus(cmd *cobra.Command, args []string) error {
// REST
// REST handler for node info
func NodeInfoRequestHandler(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
func NodeInfoRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(ctx)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
nodeInfo := status.NodeInfo
output, err := cdc.MarshalJSON(nodeInfo)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
nodeInfo := status.NodeInfo
output, err := cdc.MarshalJSON(nodeInfo)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
w.Write(output)
}
// REST handler for node syncing
func NodeSyncingRequestHandler(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
func NodeSyncingRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(ctx)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
syncing := status.SyncInfo.Syncing
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
syncing := status.SyncInfo.Syncing
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(strconv.FormatBool(syncing)))
}
w.Write([]byte(strconv.FormatBool(syncing)))
}

View File

@ -26,9 +26,9 @@ func validatorCommand() *cobra.Command {
return cmd
}
func getValidators(height *int64) ([]byte, error) {
func getValidators(ctx context.CoreContext, height *int64) ([]byte, error) {
// get the node
node, err := context.NewCoreContextFromViper().GetNode()
node, err := ctx.GetNode()
if err != nil {
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 {
return err
}
@ -73,42 +73,46 @@ func printValidators(cmd *cobra.Command, args []string) error {
// REST
// Validator Set at a height REST handler
func ValidatorSetRequestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, err := strconv.ParseInt(vars["height"], 10, 64)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
return
func ValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, err := strconv.ParseInt(vars["height"], 10, 64)
if err != nil {
w.WriteHeader(400)
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
func LatestValidatorSetRequestHandler(w http.ResponseWriter, r *http.Request) {
height, err := GetChainHeight()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
func LatestValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
height, err := GetChainHeight(ctx)
if err != nil {
w.WriteHeader(500)
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)
}

View File

@ -13,23 +13,25 @@ type BroadcastTxBody struct {
}
// BroadcastTx REST Handler
func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
var m BroadcastTxBody
func BroadcastTxRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var m BroadcastTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
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)))
}

View File

@ -34,7 +34,7 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
hashHexStr := args[0]
trustNode := viper.GetBool(client.FlagTrustNode)
output, err := queryTx(cdc, hashHexStr, trustNode)
output, err := queryTx(context.NewCoreContextFromViper(), cdc, hashHexStr, trustNode)
if err != nil {
return err
}
@ -51,14 +51,14 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
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)
if err != nil {
return nil, err
}
// get the node
node, err := context.NewCoreContextFromViper().GetNode()
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
@ -109,7 +109,7 @@ func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) {
// REST
// 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) {
vars := mux.Vars(r)
hashHexStr := vars["hash"]
@ -119,7 +119,7 @@ func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Requ
trustNode = true
}
output, err := queryTx(cdc, hashHexStr, trustNode)
output, err := queryTx(ctx, cdc, hashHexStr, trustNode)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))

View File

@ -4,6 +4,7 @@ import (
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -16,8 +17,8 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
}
// register REST routes
func RegisterRoutes(r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc)).Methods("GET")
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc, ctx)).Methods("GET")
// r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET")
// r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
// r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")

View File

@ -29,7 +29,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
tags := viper.GetStringSlice(flagTags)
output, err := searchTx(cdc, tags)
output, err := searchTx(context.NewCoreContextFromViper(), cdc, tags)
if err != nil {
return err
}
@ -47,7 +47,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
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 {
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 ")
// get the node
node, err := context.NewCoreContextFromViper().GetNode()
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
@ -94,7 +94,7 @@ func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error)
// REST
// 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) {
tag := r.FormValue("tag")
if tag == "" {
@ -104,7 +104,7 @@ func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Req
}
tags := []string{tag}
output, err := searchTx(cdc, tags)
output, err := searchTx(ctx, cdc, tags)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))

View File

@ -14,9 +14,7 @@ import (
)
// register REST routes
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) {
ctx := context.NewCoreContextFromViper()
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, storeName string) {
r.HandleFunc(
"/accounts/{address}",
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx),

View File

@ -16,9 +16,7 @@ import (
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
ctx := context.NewCoreContextFromViper()
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb, ctx)).Methods("POST")
}

View File

@ -15,9 +15,7 @@ import (
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
ctx := context.NewCoreContextFromViper()
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb, ctx)).Methods("POST")
}

View File

@ -15,9 +15,7 @@ import (
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
ctx := context.NewCoreContextFromViper()
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET")
}