Skip to content

Commit

Permalink
Merge PR #2033: staking lcd uses owner address not pubkey address
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski authored and cwgoes committed Aug 15, 2018
1 parent a4bcbc9 commit 5fff217
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 76 deletions.
5 changes: 1 addition & 4 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,8 @@ func TestValidatorQuery(t *testing.T) {
require.Equal(t, 1, len(pks))

validator1Owner := sdk.AccAddress(pks[0].Address())

validator := getValidator(t, port, validator1Owner)
bech32ValAddress, err := sdk.Bech32ifyValPub(pks[0])
require.NoError(t, err)
assert.Equal(t, validator.PubKey, bech32ValAddress, "The returned validator does not hold the correct data")
assert.Equal(t, validator.Owner, validator1Owner, "The returned validator does not hold the correct data")
}

func TestBonding(t *testing.T) {
Expand Down
29 changes: 22 additions & 7 deletions x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,25 +507,40 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.Handler
valAddress, err := sdk.AccAddressFromBech32(bech32validatorAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Error: %s", err.Error())))
w.Write([]byte(fmt.Sprintf("error: %s", err.Error())))
return
}

kvs, err := cliCtx.QuerySubspace(stake.ValidatorsKey, storeName)
key := stake.GetValidatorKey(valAddress)

res, err := cliCtx.QueryStore(key, storeName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error: %s", err.Error())))
w.Write([]byte(fmt.Sprintf("couldn't query validator, error: %s", err.Error())))
return
}

// the query will return empty if there is no data for this record
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}

validator, err := getValidator(valAddress, kvs, cdc)
validator, err := types.UnmarshalValidator(cdc, valAddress, res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("couldn't query validator. Error: %s", err.Error())))
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

output, err = cdc.MarshalJSON(validator)
bech32Validator, err := validator.Bech32Validator()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

output, err = cdc.MarshalJSON(bech32Validator)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error: %s", err.Error())))
Expand Down
86 changes: 21 additions & 65 deletions x/stake/client/rest/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rest

import (
"bytes"
"fmt"
"net/http"

Expand All @@ -12,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake/tags"
"github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/pkg/errors"
rpcclient "github.com/tendermint/tendermint/rpc/client"
)

Expand All @@ -26,33 +24,36 @@ func contains(stringSlice []string, txType string) bool {
return false
}

func getDelegatorValidator(cliCtx context.CLIContext, cdc *wire.Codec, delegatorAddr sdk.AccAddress, validatorAccAddr sdk.AccAddress) (
validator types.BechValidator, httpStatusCode int, errMsg string, err error) {
func getDelegatorValidator(cliCtx context.CLIContext, cdc *wire.Codec, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) (
bech32Validator types.BechValidator, httpStatusCode int, errMsg string, err error) {

keyDel := stake.GetDelegationKey(delegatorAddr, validatorAccAddr)

res, err := cliCtx.QueryStore(keyDel, storeName)
key := stake.GetDelegationKey(delegatorAddr, validatorAddr)
res, err := cliCtx.QueryStore(key, storeName)
if err != nil {
return types.BechValidator{}, http.StatusInternalServerError, "couldn't query delegation. Error: ", err
}

if len(res) == 0 {
return types.BechValidator{}, http.StatusNoContent, "", nil
}

kvs, err := cliCtx.QuerySubspace(stake.ValidatorsKey, storeName)
key = stake.GetValidatorKey(validatorAddr)
res, err = cliCtx.QueryStore(key, storeName)
if err != nil {
return types.BechValidator{}, http.StatusInternalServerError, "Error: ", err
return types.BechValidator{}, http.StatusInternalServerError, "couldn't query validator. Error: ", err
}
if len(kvs) == 0 {
if len(res) == 0 {
return types.BechValidator{}, http.StatusNoContent, "", nil
}

validator, errVal := getValidatorFromAccAdrr(validatorAccAddr, kvs, cdc)
if errVal != nil {
return types.BechValidator{}, http.StatusInternalServerError, "Couldn't get info from validator. Error: ", errVal
validator, err := types.UnmarshalValidator(cdc, validatorAddr, res)
if err != nil {
return types.BechValidator{}, http.StatusBadRequest, "", err
}
bech32Validator, err = validator.Bech32Validator()
if err != nil {
return types.BechValidator{}, http.StatusBadRequest, "", err
}
return validator, http.StatusOK, "", nil

return bech32Validator, http.StatusOK, "", nil
}

func getDelegatorDelegations(cliCtx context.CLIContext, cdc *wire.Codec, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) (
Expand Down Expand Up @@ -104,8 +105,8 @@ func getDelegatorUndelegations(cliCtx context.CLIContext, cdc *wire.Codec, deleg
func getDelegatorRedelegations(cliCtx context.CLIContext, cdc *wire.Codec, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) (
regelegations types.Redelegation, httpStatusCode int, errMsg string, err error) {

keyRedelegateTo := stake.GetREDsByDelToValDstIndexKey(delegatorAddr, validatorAddr)
marshalledRedelegations, err := cliCtx.QueryStore(keyRedelegateTo, storeName)
key := stake.GetREDsByDelToValDstIndexKey(delegatorAddr, validatorAddr)
marshalledRedelegations, err := cliCtx.QueryStore(key, storeName)
if err != nil {
return types.Redelegation{}, http.StatusInternalServerError, "couldn't query redelegation. Error: ", err
}
Expand All @@ -114,7 +115,7 @@ func getDelegatorRedelegations(cliCtx context.CLIContext, cdc *wire.Codec, deleg
return types.Redelegation{}, http.StatusNoContent, "", nil
}

redelegations, err := types.UnmarshalRED(cdc, keyRedelegateTo, marshalledRedelegations)
redelegations, err := types.UnmarshalRED(cdc, key, marshalledRedelegations)
if err != nil {
return types.Redelegation{}, http.StatusInternalServerError, "couldn't unmarshall redelegations. Error: ", err
}
Expand Down Expand Up @@ -156,55 +157,10 @@ func getValidators(validatorKVs []sdk.KVPair, cdc *wire.Codec) ([]types.BechVali
return validators, nil
}

// gets a validator given a ValAddress
func getValidator(address sdk.AccAddress, validatorKVs []sdk.KVPair, cdc *wire.Codec) (stake.BechValidator, error) {
// parse out the validators
for _, kv := range validatorKVs {
addr := kv.Key[1:]
validator, err := types.UnmarshalValidator(cdc, addr, kv.Value)
if err != nil {
return stake.BechValidator{}, err
}

ownerAddress := validator.PubKey.Address()
if bytes.Equal(ownerAddress.Bytes(), address.Bytes()) {
bech32Validator, err := validator.Bech32Validator()
if err != nil {
return stake.BechValidator{}, err
}

return bech32Validator, nil
}
}
return stake.BechValidator{}, errors.Errorf("Couldn't find validator")
}

// gets a validator given an AccAddress
func getValidatorFromAccAdrr(address sdk.AccAddress, validatorKVs []sdk.KVPair, cdc *wire.Codec) (stake.BechValidator, error) {
// parse out the validators
for _, kv := range validatorKVs {
addr := kv.Key[1:]
validator, err := types.UnmarshalValidator(cdc, addr, kv.Value)
if err != nil {
return stake.BechValidator{}, err
}

ownerAddress := validator.PubKey.Address()
if bytes.Equal(ownerAddress.Bytes(), address.Bytes()) {
bech32Validator, err := validator.Bech32Validator()
if err != nil {
return stake.BechValidator{}, err
}

return bech32Validator, nil
}
}
return stake.BechValidator{}, errors.Errorf("Couldn't find validator")
}

// gets all Bech32 validators from a key
func getBech32Validators(storeName string, cliCtx context.CLIContext, cdc *wire.Codec) (
validators []types.BechValidator, httpStatusCode int, errMsg string, err error) {

// Get all validators using key
kvs, err := cliCtx.QuerySubspace(stake.ValidatorsKey, storeName)
if err != nil {
Expand Down

0 comments on commit 5fff217

Please sign in to comment.