From 6ad16e6c9019d1a1bbd0fd4cc9922105d45fc9f1 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 21 May 2018 10:52:06 +0900 Subject: [PATCH] Changes to /stake/candidates REST handler --- client/lcd/lcd_test.go | 2 +- x/stake/client/rest/query.go | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 157a38681..578490ca1 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -387,7 +387,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { config.Consensus.SkipTimeoutCommit = false logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - // logger = log.NewFilter(logger, log.AllowError()) + logger = log.NewFilter(logger, log.AllowError()) privValidatorFile := config.PrivValidatorFile() privVal := pvm.LoadOrGenFilePV(privValidatorFile) db := dbm.NewMemDB() diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 576bad3cc..4decfdbfd 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -79,25 +79,28 @@ func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreC // candidatesHandlerFn - http request handler to query list of candidates func candidatesHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - res, err := ctx.Query(stake.CandidatesKey, storeName) + res, err := ctx.QuerySubspace(cdc, stake.CandidatesKey, storeName) if err != nil { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("Couldn't query bond. Error: %s", err.Error()))) + w.Write([]byte(fmt.Sprintf("Couldn't query candidates. Error: %s", err.Error()))) return } - // the query will return empty if there is no data for this bond if len(res) == 0 { w.WriteHeader(http.StatusNoContent) return } - var candidates []stake.Candidate - err = cdc.UnmarshalBinary(res, &candidates) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("Couldn't decode candidates. Error: %s", err.Error()))) - return + candidates := make(stake.Candidates, 0, len(res)) + for _, kv := range res { + var candidate stake.Candidate + err = cdc.UnmarshalBinary(kv.Value, &candidate) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("Couldn't decode candidate. Error: %s", err.Error()))) + return + } + candidates = append(candidates, candidate) } output, err := cdc.MarshalJSON(candidates)