Removed candidates endpoint and addressed some comments

This commit is contained in:
Matt Bell 2018-05-25 00:14:10 +09:00
parent 6ad16e6c90
commit 0738de17f4
2 changed files with 19 additions and 85 deletions

View File

@ -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)

View File

@ -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)
}
}