From 0738de17f4ab37157dd199df583ef0acdea93245 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 25 May 2018 00:14:10 +0900 Subject: [PATCH] Removed candidates endpoint and addressed some comments --- client/lcd/lcd_test.go | 56 +++++++++--------------------------- x/stake/client/rest/query.go | 48 ++++--------------------------- 2 files changed, 19 insertions(+), 85 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 578490ca13..7d1dab6762 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -311,13 +311,6 @@ func TestTxs(t *testing.T) { // assert.NotEqual(t, "[]", body) } -func TestCandidates(t *testing.T) { - candidates := getCandidates(t) - assert.Equal(t, len(candidates), 2) - assert.Equal(t, hex.EncodeToString(candidates[0].Address), candidateAddr1) - assert.Equal(t, hex.EncodeToString(candidates[1].Address), candidateAddr2) -} - func TestBond(t *testing.T) { // create bond TX @@ -334,7 +327,7 @@ func TestBond(t *testing.T) { assert.Equal(t, int64(9999900), coins.AmountOf(stakeDenom)) // query candidate - bond := getDelegatorBond(t, sendAddr, candidateAddr1) + bond := getDelegation(t, sendAddr, candidateAddr1) assert.Equal(t, "100/1", bond.Shares.String()) } @@ -354,7 +347,7 @@ func TestUnbond(t *testing.T) { assert.Equal(t, int64(9999911), coins.AmountOf(stakeDenom)) // query candidate - bond := getDelegatorBond(t, sendAddr, candidateAddr1) + bond := getDelegation(t, sendAddr, candidateAddr1) assert.Equal(t, "99/1", bond.Shares.String()) } @@ -423,13 +416,10 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { }, StakeData: stake.GenesisState{ Pool: stake.Pool{ - TotalSupply: 1650, - BondedShares: sdk.NewRat(200, 1), - UnbondedShares: sdk.ZeroRat(), - BondedPool: 200, - UnbondedPool: 0, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), + BondedShares: sdk.NewRat(200, 1), + UnbondedShares: sdk.ZeroRat(), + Inflation: sdk.NewRat(7, 100), + PrevBondedShares: sdk.ZeroRat(), }, Params: stake.Params{ InflationRateChange: sdk.NewRat(13, 100), @@ -439,30 +429,20 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { MaxValidators: 100, BondDenom: stakeDenom, }, - Candidates: []stake.Candidate{ + Validators: []stake.Validator{ { - Status: 1, - Address: genDoc.Validators[0].PubKey.Address(), - PubKey: genDoc.Validators[0].PubKey, - Assets: sdk.NewRat(1000, 1), - Liabilities: sdk.ZeroRat(), + Owner: genDoc.Validators[0].PubKey.Address(), + PubKey: genDoc.Validators[0].PubKey, Description: stake.Description{ Moniker: "validator1", }, - ValidatorBondHeight: 0, - ValidatorBondCounter: 0, }, { - Status: 1, - Address: genDoc.Validators[1].PubKey.Address(), - PubKey: genDoc.Validators[1].PubKey, - Assets: sdk.NewRat(100, 1), - Liabilities: sdk.ZeroRat(), + Owner: genDoc.Validators[1].PubKey.Address(), + PubKey: genDoc.Validators[1].PubKey, Description: stake.Description{ Moniker: "validator2", }, - ValidatorBondHeight: 0, - ValidatorBondCounter: 0, }, }, }, @@ -602,26 +582,16 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad return resultTx } -func getDelegatorBond(t *testing.T, delegatorAddr, candidateAddr string) stake.DelegatorBond { +func getDelegation(t *testing.T, delegatorAddr, candidateAddr string) stake.Delegation { // get the account to get the sequence res, body := request(t, port, "GET", "/stake/"+delegatorAddr+"/bonding_status/"+candidateAddr, nil) require.Equal(t, http.StatusOK, res.StatusCode, body) - var bond stake.DelegatorBond + var bond stake.Delegation err := cdc.UnmarshalJSON([]byte(body), &bond) require.Nil(t, err) return bond } -func getCandidates(t *testing.T) []stake.Candidate { - // get the account to get the sequence - res, body := request(t, port, "GET", "/stake/candidates", nil) - require.Equal(t, http.StatusOK, res.StatusCode, body) - var candidates []stake.Candidate - err := cdc.UnmarshalJSON([]byte(body), &candidates) - require.Nil(t, err) - return candidates -} - func doBond(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) { // get the account to get the sequence acc := getAccount(t, sendAddr) diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 4decfdbfd8..02cc732c9d 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -14,8 +14,10 @@ import ( ) func registerQueryRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) { - r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", bondingStatusHandlerFn("stake", cdc, ctx)).Methods("GET") - r.HandleFunc("/stake/candidates", candidatesHandlerFn("stake", cdc, ctx)).Methods("GET") + r.HandleFunc( + "/stake/{delegator}/bonding_status/{validator}", + bondingStatusHandlerFn("stake", cdc, ctx), + ).Methods("GET") } // bondingStatusHandlerFn - http request handler to query delegator bonding status @@ -24,7 +26,7 @@ func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreC // read parameters vars := mux.Vars(r) delegator := vars["delegator"] - candidate := vars["candidate"] + validator := vars["validator"] bz, err := hex.DecodeString(delegator) if err != nil { @@ -34,7 +36,7 @@ func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreC } delegatorAddr := sdk.Address(bz) - bz, err = hex.DecodeString(candidate) + bz, err = hex.DecodeString(validator) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) @@ -75,41 +77,3 @@ func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreC w.Write(output) } } - -// 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.QuerySubspace(cdc, stake.CandidatesKey, storeName) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("Couldn't query candidates. Error: %s", err.Error()))) - return - } - - if len(res) == 0 { - w.WriteHeader(http.StatusNoContent) - 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) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } - - w.Write(output) - } -}