Skip to content

Commit

Permalink
use newQuery...Params
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Kunze committed Nov 12, 2018
1 parent 182f111 commit 7dfeb7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
12 changes: 3 additions & 9 deletions x/stake/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code
}

cliCtx := context.NewCLIContext().WithCodec(cdc)
params := stake.QueryValidatorParams{
ValidatorAddr: valAddr,
}
params := stake.NewQueryValidatorParams(valAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -164,9 +162,7 @@ func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cob
}

cliCtx := context.NewCLIContext().WithCodec(cdc)
params := stake.QueryValidatorParams{
ValidatorAddr: valAddr,
}
params := stake.NewQueryValidatorParams(valAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -303,9 +299,7 @@ func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra
return err
}

params := stake.QueryValidatorParams{
ValidatorAddr: validatorAddr,
}
params := stake.NewQueryValidatorParams(validatorAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down
13 changes: 3 additions & 10 deletions x/stake/client/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func queryBonds(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) ht
return
}

params := stake.QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}
params := stake.NewQueryBondsParams(delegatorAddr, validatorAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -93,9 +90,7 @@ func queryDelegator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
return
}

params := stake.QueryDelegatorParams{
DelegatorAddr: delegatorAddr,
}
params := stake.NewQueryDelegatorParams(delegatorAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -123,9 +118,7 @@ func queryValidator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
return
}

params := stake.QueryValidatorParams{
ValidatorAddr: validatorAddr,
}
params := stake.NewQueryValidatorParams(validatorAddr)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions x/stake/querier/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ type QueryBondsParams struct {
ValidatorAddr sdk.ValAddress
}

// creates a new QueryDelegatorParams
func NewQueryDelegatorParams(delegatorAddr sdk.AccAddress) QueryDelegatorParams {
return QueryDelegatorParams{
DelegatorAddr: delegatorAddr,
}
}

// creates a new QueryValidatorParams
func NewQueryValidatorParams(validatorAddr sdk.ValAddress) QueryValidatorParams {
return QueryValidatorParams{
ValidatorAddr: validatorAddr,
}
}

// creates a new QueryBondsParams
func NewQueryBondsParams(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) QueryBondsParams {
return QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}
}

func queryValidators(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
stakeParams := k.GetParams(ctx)
validators := k.GetValidators(ctx, stakeParams.MaxValidators)
Expand Down
37 changes: 9 additions & 28 deletions x/stake/querier/queryable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ var (
pk1, pk2 = keep.PKs[0], keep.PKs[1]
)

func newTestDelegatorQuery(delegatorAddr sdk.AccAddress) QueryDelegatorParams {
return QueryDelegatorParams{
DelegatorAddr: delegatorAddr,
}
}

func newTestValidatorQuery(validatorAddr sdk.ValAddress) QueryValidatorParams {
return QueryValidatorParams{
ValidatorAddr: validatorAddr,
}
}

func newTestBondQuery(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) QueryBondsParams {
return QueryBondsParams{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
}
}

func TestNewQuerier(t *testing.T) {
cdc := codec.New()
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
Expand Down Expand Up @@ -72,7 +53,7 @@ func TestNewQuerier(t *testing.T) {
_, err = querier(ctx, []string{"parameters"}, query)
require.Nil(t, err)

queryValParams := newTestValidatorQuery(addrVal1)
queryValParams := NewQueryValidatorParams(addrVal1)
bz, errRes := cdc.MarshalJSON(queryValParams)
require.Nil(t, errRes)

Expand All @@ -91,7 +72,7 @@ func TestNewQuerier(t *testing.T) {
_, err = querier(ctx, []string{"validatorRedelegations"}, query)
require.Nil(t, err)

queryDelParams := newTestDelegatorQuery(addrAcc2)
queryDelParams := NewQueryDelegatorParams(addrAcc2)
bz, errRes = cdc.MarshalJSON(queryDelParams)
require.Nil(t, errRes)

Expand Down Expand Up @@ -163,7 +144,7 @@ func TestQueryValidators(t *testing.T) {
require.ElementsMatch(t, queriedValidators, validatorsResp)

// Query each validator
queryParams := newTestValidatorQuery(addrVal1)
queryParams := NewQueryValidatorParams(addrVal1)
bz, errRes := cdc.MarshalJSON(queryParams)
require.Nil(t, errRes)

Expand Down Expand Up @@ -203,7 +184,7 @@ func TestQueryDelegation(t *testing.T) {
keeper.ApplyAndReturnValidatorSetUpdates(ctx)

// Query Delegator bonded validators
queryParams := newTestDelegatorQuery(addrAcc2)
queryParams := NewQueryDelegatorParams(addrAcc2)
bz, errRes := cdc.MarshalJSON(queryParams)
require.Nil(t, errRes)

Expand Down Expand Up @@ -231,7 +212,7 @@ func TestQueryDelegation(t *testing.T) {
require.NotNil(t, err)

// Query bonded validator
queryBondParams := newTestBondQuery(addrAcc2, addrVal1)
queryBondParams := NewQueryBondsParams(addrAcc2, addrVal1)
bz, errRes = cdc.MarshalJSON(queryBondParams)
require.Nil(t, errRes)

Expand Down Expand Up @@ -298,7 +279,7 @@ func TestQueryDelegation(t *testing.T) {

// Query validator delegations

bz, errRes = cdc.MarshalJSON(newTestValidatorQuery(addrVal1))
bz, errRes = cdc.MarshalJSON(NewQueryValidatorParams(addrVal1))
require.Nil(t, errRes)

query = abci.RequestQuery{
Expand All @@ -318,7 +299,7 @@ func TestQueryDelegation(t *testing.T) {
// Query unbonging delegation
keeper.BeginUnbonding(ctx, addrAcc2, val1.OperatorAddr, sdk.NewDec(10))

queryBondParams = newTestBondQuery(addrAcc2, addrVal1)
queryBondParams = NewQueryBondsParams(addrAcc2, addrVal1)
bz, errRes = cdc.MarshalJSON(queryBondParams)
require.Nil(t, errRes)

Expand Down Expand Up @@ -387,7 +368,7 @@ func TestQueryRedelegations(t *testing.T) {
require.True(t, found)

// delegator redelegations
queryDelegatorParams := newTestDelegatorQuery(addrAcc2)
queryDelegatorParams := NewQueryDelegatorParams(addrAcc2)
bz, errRes := cdc.MarshalJSON(queryDelegatorParams)
require.Nil(t, errRes)

Expand All @@ -406,7 +387,7 @@ func TestQueryRedelegations(t *testing.T) {
require.Equal(t, redelegation, redsRes[0])

// validator redelegations
queryValidatorParams := newTestValidatorQuery(val1.GetOperator())
queryValidatorParams := NewQueryValidatorParams(val1.GetOperator())
bz, errRes = cdc.MarshalJSON(queryValidatorParams)
require.Nil(t, errRes)

Expand Down
5 changes: 4 additions & 1 deletion x/stake/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ var (
NewMsgBeginUnbonding = types.NewMsgBeginUnbonding
NewMsgBeginRedelegate = types.NewMsgBeginRedelegate

NewQuerier = querier.NewQuerier
NewQuerier = querier.NewQuerier
NewQueryDelegatorParams = querier.NewQueryDelegatorParams
NewQueryValidatorParams = querier.NewQueryValidatorParams
NewQueryBondsParams = querier.NewQueryBondsParams
)

const (
Expand Down

0 comments on commit 7dfeb7d

Please sign in to comment.