From fd40d39556da9c4e702a6654aa64cb5df3a6b73a Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 25 Apr 2018 16:49:31 +0200 Subject: [PATCH] Only one CoreContext for all REST commands --- client/lcd/root.go | 13 +++--- client/rpc/block.go | 79 ++++++++++++++++++----------------- client/rpc/root.go | 15 +++---- client/rpc/status.go | 64 +++++++++++++++------------- client/rpc/validators.go | 76 +++++++++++++++++---------------- client/tx/broadcast.go | 36 ++++++++-------- client/tx/query.go | 10 ++--- client/tx/root.go | 5 ++- client/tx/search.go | 10 ++--- x/auth/client/rest/query.go | 4 +- x/bank/client/rest/sendtx.go | 4 +- x/ibc/client/rest/transfer.go | 4 +- x/stake/client/rest/query.go | 4 +- 13 files changed, 167 insertions(+), 157 deletions(-) diff --git a/client/lcd/root.go b/client/lcd/root.go index a607504cd9..b166d4ce46 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -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 } diff --git a/client/rpc/block.go b/client/rpc/block.go index 200dc668e6..46f6cdbd80 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -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) } diff --git a/client/rpc/root.go b/client/rpc/root.go index 43e9097f0e..81be7ddf43 100644 --- a/client/rpc/root.go +++ b/client/rpc/root.go @@ -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") } diff --git a/client/rpc/status.go b/client/rpc/status.go index 3b143d9c5b..919fea4ce1 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -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))) } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 6937e4a899..768cad7bcf 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -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) } diff --git a/client/tx/broadcast.go b/client/tx/broadcast.go index 9e9eaae70d..d325ff15b5 100644 --- a/client/tx/broadcast.go +++ b/client/tx/broadcast.go @@ -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))) } diff --git a/client/tx/query.go b/client/tx/query.go index cc252bacc8..2c53622e13 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -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())) diff --git a/client/tx/root.go b/client/tx/root.go index 2a87113b38..caae53620b 100644 --- a/client/tx/root.go +++ b/client/tx/root.go @@ -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") diff --git a/client/tx/search.go b/client/tx/search.go index 1687d3bbd9..29b330a308 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -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())) diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index e16884fc6b..fd51789c1c 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -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), diff --git a/x/bank/client/rest/sendtx.go b/x/bank/client/rest/sendtx.go index 83b62d03d7..15256ce093 100644 --- a/x/bank/client/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -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") } diff --git a/x/ibc/client/rest/transfer.go b/x/ibc/client/rest/transfer.go index 9e567df016..033ef6be53 100644 --- a/x/ibc/client/rest/transfer.go +++ b/x/ibc/client/rest/transfer.go @@ -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") } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 0e148345b0..96bdcc95de 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -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") }