Adds grpc tests for x/staking (#7204)
* added staking grpc tests * updated tests * added todos * lint * lint * review changes * review chnages Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
parent
12d95de096
commit
e9a0e82895
|
@ -1,15 +1,20 @@
|
||||||
package rest_test
|
package rest_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gogo/protobuf/proto"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/testutil"
|
"github.com/cosmos/cosmos-sdk/testutil"
|
||||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
||||||
|
"github.com/cosmos/cosmos-sdk/types/query"
|
||||||
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||||
|
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/client/testutil"
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,13 +29,31 @@ func (s *IntegrationTestSuite) SetupSuite() {
|
||||||
s.T().Log("setting up integration test suite")
|
s.T().Log("setting up integration test suite")
|
||||||
|
|
||||||
cfg := network.DefaultConfig()
|
cfg := network.DefaultConfig()
|
||||||
cfg.NumValidators = 1
|
cfg.NumValidators = 2
|
||||||
|
|
||||||
s.cfg = cfg
|
s.cfg = cfg
|
||||||
s.network = network.New(s.T(), cfg)
|
s.network = network.New(s.T(), cfg)
|
||||||
|
|
||||||
_, err := s.network.WaitForHeight(1)
|
_, err := s.network.WaitForHeight(1)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
unbond, err := sdk.ParseCoin("10stake")
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
val2 := s.network.Validators[1]
|
||||||
|
|
||||||
|
// redelegate
|
||||||
|
_, err = stakingtestutil.MsgRedelegateExec(val.ClientCtx, val.Address, val.ValAddress, val2.ValAddress, unbond)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
_, err = s.network.WaitForHeight(1)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
// unbonding
|
||||||
|
_, err = stakingtestutil.MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
_, err = s.network.WaitForHeight(1)
|
||||||
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IntegrationTestSuite) TearDownSuite() {
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
||||||
|
@ -43,33 +66,23 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() {
|
||||||
baseURL := val.APIAddress
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
url string
|
url string
|
||||||
headers map[string]string
|
error bool
|
||||||
error bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"test query validators gRPC route with invalid status",
|
"test query validators gRPC route with invalid status",
|
||||||
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators?status=active", baseURL),
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators?status=active", baseURL),
|
||||||
map[string]string{
|
|
||||||
grpctypes.GRPCBlockHeightHeader: "1",
|
|
||||||
},
|
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"test query validators gRPC route without status query param",
|
"test query validators gRPC route without status query param",
|
||||||
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators", baseURL),
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators", baseURL),
|
||||||
map[string]string{
|
|
||||||
grpctypes.GRPCBlockHeightHeader: "1",
|
|
||||||
},
|
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"test query validators gRPC route with valid status",
|
"test query validators gRPC route with valid status",
|
||||||
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators?status=%s", baseURL, sdk.Bonded.String()),
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators?status=%s", baseURL, sdk.Bonded.String()),
|
||||||
map[string]string{
|
|
||||||
grpctypes.GRPCBlockHeightHeader: "1",
|
|
||||||
},
|
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -77,7 +90,7 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
s.Run(tc.name, func() {
|
s.Run(tc.name, func() {
|
||||||
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
|
resp, err := rest.GetRequest(tc.url)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
var valRes types.QueryValidatorsResponse
|
var valRes types.QueryValidatorsResponse
|
||||||
|
@ -96,6 +109,748 @@ func (s *IntegrationTestSuite) TestQueryValidatorsGRPCHandler() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryValidatorGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s", baseURL, "wrongValidatorAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s", baseURL, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s", baseURL, valAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var validator types.QueryValidatorResponse
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &validator)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().NotNil(validator.Validator)
|
||||||
|
s.Require().Equal(s.network.Validators[0].ValAddress, validator.Validator.OperatorAddress)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryValidatorDelegationsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
headers map[string]string
|
||||||
|
error bool
|
||||||
|
respType proto.Message
|
||||||
|
expectedResp proto.Message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations", baseURL, "wrongValAddress"),
|
||||||
|
map[string]string{},
|
||||||
|
true,
|
||||||
|
&types.QueryValidatorDelegationsResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations", baseURL, ""),
|
||||||
|
map[string]string{},
|
||||||
|
true,
|
||||||
|
&types.QueryValidatorDelegationsResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request(height specific)",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations", baseURL, valAddressBase64),
|
||||||
|
map[string]string{
|
||||||
|
grpctypes.GRPCBlockHeightHeader: "1",
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
&types.QueryValidatorDelegationsResponse{},
|
||||||
|
&types.QueryValidatorDelegationsResponse{
|
||||||
|
DelegationResponses: types.DelegationResponses{
|
||||||
|
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
|
||||||
|
},
|
||||||
|
Pagination: &query.PageResponse{Total: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Equal(tc.expectedResp.String(), tc.respType.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryValidatorUnbondingDelegationsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/unbonding_delegations", baseURL, "wrongValAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/unbonding_delegations", baseURL, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/unbonding_delegations", baseURL, valAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var ubds types.QueryValidatorUnbondingDelegationsResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &ubds)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Len(ubds.UnbondingResponses, 1)
|
||||||
|
s.Require().Equal(ubds.UnbondingResponses[0].ValidatorAddress, val.ValAddress)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryDelegationGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
val2 := s.network.Validators[1]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val2.Address)
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
accAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
respType proto.Message
|
||||||
|
expectedResp proto.Message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", baseURL, "wrongValAddress", accAddressBase64),
|
||||||
|
true,
|
||||||
|
&types.QueryDelegationResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wrong account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", baseURL, valAddressBase64, "wrongAccAddress"),
|
||||||
|
true,
|
||||||
|
&types.QueryDelegationResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", baseURL, "", accAddressBase64),
|
||||||
|
true,
|
||||||
|
&types.QueryDelegationResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", baseURL, valAddressBase64, ""),
|
||||||
|
true,
|
||||||
|
&types.QueryDelegationResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", baseURL, valAddressBase64, accAddressBase64),
|
||||||
|
false,
|
||||||
|
&types.QueryDelegationResponse{},
|
||||||
|
&types.QueryDelegationResponse{
|
||||||
|
DelegationResponse: &types.DelegationResponse{
|
||||||
|
Delegation: types.Delegation{
|
||||||
|
DelegatorAddress: val.Address,
|
||||||
|
ValidatorAddress: val2.ValAddress,
|
||||||
|
Shares: sdk.NewDec(10),
|
||||||
|
},
|
||||||
|
Balance: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Equal(tc.expectedResp.String(), tc.respType.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryUnbondingDelegationGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
accAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", baseURL, "wrongValAddress", accAddressBase64),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wrong account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", baseURL, valAddressBase64, "wrongAccAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", baseURL, "", accAddressBase64),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", baseURL, valAddressBase64, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", baseURL, valAddressBase64, accAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var ubd types.QueryUnbondingDelegationResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &ubd)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Equal(ubd.Unbond.DelegatorAddress, val.Address)
|
||||||
|
s.Require().Equal(ubd.Unbond.ValidatorAddress, val.ValAddress)
|
||||||
|
s.Require().Len(ubd.Unbond.Entries, 1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
delAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
headers map[string]string
|
||||||
|
error bool
|
||||||
|
respType proto.Message
|
||||||
|
expectedResp proto.Message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", baseURL, "wrongValAddress"),
|
||||||
|
map[string]string{},
|
||||||
|
true,
|
||||||
|
&types.QueryDelegatorDelegationsResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", baseURL, ""),
|
||||||
|
map[string]string{},
|
||||||
|
true,
|
||||||
|
&types.QueryDelegatorDelegationsResponse{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request (height specific)",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", baseURL, delAddressBase64),
|
||||||
|
map[string]string{
|
||||||
|
grpctypes.GRPCBlockHeightHeader: "1",
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
&types.QueryDelegatorDelegationsResponse{},
|
||||||
|
&types.QueryDelegatorDelegationsResponse{
|
||||||
|
DelegationResponses: types.DelegationResponses{
|
||||||
|
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
|
||||||
|
},
|
||||||
|
Pagination: &query.PageResponse{Total: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Equal(tc.expectedResp.String(), tc.respType.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryDelegatorUnbondingDelegationsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
delAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
ubdsLength int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/unbonding_delegations", baseURL, "wrongValAddress"),
|
||||||
|
true,
|
||||||
|
0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/unbonding_delegations", baseURL, ""),
|
||||||
|
true,
|
||||||
|
0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/unbonding_delegations", baseURL, delAddressBase64),
|
||||||
|
false,
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var ubds types.QueryDelegatorUnbondingDelegationsResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &ubds)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Len(ubds.UnbondingResponses, tc.ubdsLength)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryRedelegationsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
val2 := s.network.Validators[1]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
delAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
srcValBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
dstValBase64 := base64.URLEncoding.EncodeToString(val2.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations", baseURL, "wrongValAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations", baseURL, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations", baseURL, delAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request with src address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations?src_validator_addr=%s", baseURL, delAddressBase64, srcValBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request with dst address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations?dst_validator_addr=%s", baseURL, delAddressBase64, dstValBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request with dst address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/redelegations?src_validator_addr=%s&dst_validator_addr=%s", baseURL, delAddressBase64, srcValBase64, dstValBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
var redelegations types.QueryRedelegationsResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &redelegations)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
s.Require().Len(redelegations.RedelegationResponses, 1)
|
||||||
|
s.Require().Equal(redelegations.RedelegationResponses[0].Redelegation.DelegatorAddress, val.Address)
|
||||||
|
s.Require().Equal(redelegations.RedelegationResponses[0].Redelegation.ValidatorSrcAddress, val.ValAddress)
|
||||||
|
s.Require().Equal(redelegations.RedelegationResponses[0].Redelegation.ValidatorDstAddress, val2.ValAddress)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryDelegatorValidatorsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
delAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong delegator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators", baseURL, "wrongDelAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no delegator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators", baseURL, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators", baseURL, delAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var validators types.QueryDelegatorValidatorsResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &validators)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Len(validators.Validators, len(s.network.Validators))
|
||||||
|
s.Require().Equal(int(validators.Pagination.Total), len(s.network.Validators))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryDelegatorValidatorGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
valAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
// TODO: need to pass bech32 string instead of base64 encoding string.
|
||||||
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/7195
|
||||||
|
accAddressBase64 := base64.URLEncoding.EncodeToString(val.Address)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators/%s", baseURL, "wrongValAddress", accAddressBase64),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wrong account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators/%s", baseURL, valAddressBase64, "wrongAccAddress"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no validator address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators/%s", baseURL, "", accAddressBase64),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no account address",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators/%s", baseURL, valAddressBase64, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/validators/%s", baseURL, valAddressBase64, accAddressBase64),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var validator types.QueryDelegatorValidatorResponse
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &validator)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().NotNil(validator)
|
||||||
|
s.Require().Equal(validator.Validator.OperatorAddress, val.ValAddress)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryHistoricalInfoGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wrong height",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/historical_info/%s", baseURL, "-1"),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with no height",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/historical_info/%s", baseURL, ""),
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid request",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/historical_info/%s", baseURL, "2"),
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
var historical_info types.QueryHistoricalInfoResponse
|
||||||
|
|
||||||
|
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, &historical_info)
|
||||||
|
|
||||||
|
if tc.error {
|
||||||
|
s.Require().Error(err)
|
||||||
|
} else {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().NotNil(historical_info)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryParamsGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
respType proto.Message
|
||||||
|
expected proto.Message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"gRPC request params",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/params", baseURL),
|
||||||
|
&types.QueryParamsResponse{},
|
||||||
|
&types.QueryParamsResponse{
|
||||||
|
Params: types.DefaultParams(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType))
|
||||||
|
s.Require().Equal(tc.expected, tc.respType)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IntegrationTestSuite) TestQueryPoolGRPC() {
|
||||||
|
val := s.network.Validators[0]
|
||||||
|
baseURL := val.APIAddress
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
url string
|
||||||
|
respType proto.Message
|
||||||
|
expected proto.Message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"gRPC request params",
|
||||||
|
fmt.Sprintf("%s/cosmos/staking/v1beta1/pool", baseURL),
|
||||||
|
&types.QueryPoolResponse{},
|
||||||
|
&types.QueryPoolResponse{
|
||||||
|
Pool: types.Pool{
|
||||||
|
NotBondedTokens: sdk.NewInt(10),
|
||||||
|
BondedTokens: sdk.NewInt(199999990),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
resp, err := rest.GetRequest(tc.url)
|
||||||
|
s.Run(tc.name, func() {
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(resp, tc.respType))
|
||||||
|
s.Require().Equal(tc.expected, tc.respType)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntegrationTestSuite(t *testing.T) {
|
func TestIntegrationTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(IntegrationTestSuite))
|
suite.Run(t, new(IntegrationTestSuite))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||||
|
"github.com/cosmos/cosmos-sdk/testutil"
|
||||||
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commonArgs = []string{
|
||||||
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||||
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
|
||||||
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgRedelegateExec creates a redelegate message.
|
||||||
|
func MsgRedelegateExec(clientCtx client.Context, from, src, dst, amount fmt.Stringer,
|
||||||
|
extraArgs ...string) (testutil.BufferWriter, error) {
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
src.String(),
|
||||||
|
dst.String(),
|
||||||
|
amount.String(),
|
||||||
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, from.String()),
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, commonArgs...)
|
||||||
|
return clitestutil.ExecTestCLICmd(clientCtx, stakingcli.NewRedelegateCmd(), args)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgUnbondExec creates a unbond message.
|
||||||
|
func MsgUnbondExec(clientCtx client.Context, from fmt.Stringer, valAddress,
|
||||||
|
amount fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) {
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
valAddress.String(),
|
||||||
|
amount.String(),
|
||||||
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, from.String()),
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, commonArgs...)
|
||||||
|
return clitestutil.ExecTestCLICmd(clientCtx, stakingcli.NewUnbondCmd(), args)
|
||||||
|
}
|
Loading…
Reference in New Issue