Skip to content

Commit

Permalink
test: add test for unpacking interface on validator request (cosmos#9391
Browse files Browse the repository at this point in the history
)

* test unpack on validator

* *add line to docs about validator being encoded in any
* add test to show how to unpack interfaces with validator object

* Update docs/core/encoding.md

Co-authored-by: Amaury <[email protected]>

* sort imports

* Update docs/core/encoding.md

Co-authored-by: Ryan Christoffersen <[email protected]>

Co-authored-by: technicallyty <[email protected]>
Co-authored-by: Amaury <[email protected]>
Co-authored-by: Ryan Christoffersen <[email protected]>
  • Loading branch information
4 people authored May 26, 2021
1 parent 44a4138 commit a93ed22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/core/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ The above `Profile` example is a fictive example used for educational purposes.
- the `sdk.Msg` interface for encoding different `Msg`s in a transaction,
- the `AccountI` interface for encodinig different types of accounts (similar to the above example) in the x/auth query responses,
- the `Evidencei` interface for encoding different types of evidences in the x/evidence module,
- the `AuthorizationI` interface for encoding different types of x/authz authorizations.
- the `AuthorizationI` interface for encoding different types of x/authz authorizations,
- the [`Validator`](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/x/staking/types/staking.pb.go#L306-L337) struct that contains information about a validator.

A real-life example of encoding the pubkey as `Any` inside the Validator struct in x/staking is shown in the following example:

Expand Down
28 changes: 27 additions & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
Expand All @@ -20,6 +21,7 @@ import (
reflectionv1 "github.com/cosmos/cosmos-sdk/client/grpc/reflection"
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
reflectionv2 "github.com/cosmos/cosmos-sdk/server/grpc/reflection/v2alpha1"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -29,19 +31,21 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type IntegrationTestSuite struct {
suite.Suite

app *simapp.SimApp
cfg network.Config
network *network.Network
conn *grpc.ClientConn
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

s.app = simapp.Setup(false)
s.cfg = network.DefaultConfig()
s.cfg.NumValidators = 1
s.network = network.New(s.T(), s.cfg)
Expand Down Expand Up @@ -213,6 +217,28 @@ func (s *IntegrationTestSuite) TestGRPCServerInvalidHeaderHeights() {
}
}

// TestGRPCUnpacker - tests the grpc endpoint for Validator and using the interface registry unpack and extract the
// ConsAddr. (ref: https://github.com/cosmos/cosmos-sdk/issues/8045)
func (s *IntegrationTestSuite) TestGRPCUnpacker() {
ir := s.app.InterfaceRegistry()
queryClient := stakingtypes.NewQueryClient(s.conn)
validator, err := queryClient.Validator(context.Background(),
&stakingtypes.QueryValidatorRequest{ValidatorAddr: s.network.Validators[0].ValAddress.String()})
require.NoError(s.T(), err)

// no unpacked interfaces yet, so ConsAddr will be nil
nilAddr, err := validator.Validator.GetConsAddr()
require.Error(s.T(), err)
require.Nil(s.T(), nilAddr)

// unpack the interfaces and now ConsAddr is not nil
err = validator.Validator.UnpackInterfaces(ir)
require.NoError(s.T(), err)
addr, err := validator.Validator.GetConsAddr()
require.NotNil(s.T(), addr)
require.NoError(s.T(), err)
}

// mkTxBuilder creates a TxBuilder containing a signed tx from validator 0.
func (s IntegrationTestSuite) mkTxBuilder() client.TxBuilder {
val := s.network.Validators[0]
Expand Down

0 comments on commit a93ed22

Please sign in to comment.