From 97e14988b5555e0deb4775063bc836d9efa9c637 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Mon, 24 May 2021 14:50:49 -0700 Subject: [PATCH 1/5] test unpack on validator --- server/grpc/server_test.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index e3bd71fe4feb..006f998d2566 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -1,10 +1,10 @@ -// +build norace - package grpc_test import ( "context" "fmt" + "github.com/cosmos/cosmos-sdk/simapp" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "testing" "time" @@ -34,6 +34,7 @@ import ( type IntegrationTestSuite struct { suite.Suite + app *simapp.SimApp cfg network.Config network *network.Network conn *grpc.ClientConn @@ -41,7 +42,7 @@ type IntegrationTestSuite struct { func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - + app := simapp.Setup(false) s.cfg = network.DefaultConfig() s.cfg.NumValidators = 1 s.network = network.New(s.T(), s.cfg) @@ -56,6 +57,7 @@ func (s *IntegrationTestSuite) SetupSuite() { grpc.WithInsecure(), // Or else we get "no transport security set" ) s.Require().NoError(err) + s.app = app } func (s *IntegrationTestSuite) TearDownSuite() { @@ -213,6 +215,25 @@ func (s *IntegrationTestSuite) TestGRPCServerInvalidHeaderHeights() { } } +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) + 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] From fcd99fc8ad6503fdd7b0254398058a7d165b6ff3 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Tue, 25 May 2021 08:25:42 -0700 Subject: [PATCH 2/5] *add line to docs about validator being encoded in any * add test to show how to unpack interfaces with validator object --- docs/core/encoding.md | 3 ++- server/grpc/server_test.go | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/core/encoding.md b/docs/core/encoding.md index 7b6b1274ca2a..bfafc13137a3 100644 --- a/docs/core/encoding.md +++ b/docs/core/encoding.md @@ -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` [object](https://github.com/cosmos/cosmos-sdk/blob/61bd6cbd12cdac19a514fcc610ee091c462edfc3/x/staking/types/staking.pb.go#L306-L337) which is fully encoded as `Any`. A real-life example of encoding the pubkey as `Any` inside the Validator struct in x/staking is shown in the following example: diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index 006f998d2566..0b034b64c028 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -1,3 +1,5 @@ +// +build norace + package grpc_test import ( @@ -42,7 +44,7 @@ type IntegrationTestSuite struct { func (s *IntegrationTestSuite) SetupSuite() { s.T().Log("setting up integration test suite") - app := simapp.Setup(false) + s.app = simapp.Setup(false) s.cfg = network.DefaultConfig() s.cfg.NumValidators = 1 s.network = network.New(s.T(), s.cfg) @@ -57,7 +59,6 @@ func (s *IntegrationTestSuite) SetupSuite() { grpc.WithInsecure(), // Or else we get "no transport security set" ) s.Require().NoError(err) - s.app = app } func (s *IntegrationTestSuite) TearDownSuite() { @@ -215,6 +216,8 @@ 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) @@ -229,6 +232,7 @@ func (s *IntegrationTestSuite) TestGRPCUnpacker() { // 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) From 110378fac5dbad830fbf1698948d748d8e12a35b Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 26 May 2021 08:03:42 -0700 Subject: [PATCH 3/5] Update docs/core/encoding.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- docs/core/encoding.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/encoding.md b/docs/core/encoding.md index bfafc13137a3..3897cb5bb682 100644 --- a/docs/core/encoding.md +++ b/docs/core/encoding.md @@ -198,7 +198,7 @@ The above `Profile` example is a fictive example used for educational purposes. - 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 `Validator` [object](https://github.com/cosmos/cosmos-sdk/blob/61bd6cbd12cdac19a514fcc610ee091c462edfc3/x/staking/types/staking.pb.go#L306-L337) which is fully encoded as `Any`. +- the `Validator` [object](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/x/staking/types/staking.pb.go#L306-L337) which is fully encoded as `Any`. A real-life example of encoding the pubkey as `Any` inside the Validator struct in x/staking is shown in the following example: From af30c0d375cd65c35e359dbd5305eec93001f5a8 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Wed, 26 May 2021 08:09:47 -0700 Subject: [PATCH 4/5] sort imports --- server/grpc/server_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index 0b034b64c028..7f3c7a742cb4 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -5,8 +5,6 @@ package grpc_test import ( "context" "fmt" - "github.com/cosmos/cosmos-sdk/simapp" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "testing" "time" @@ -14,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" @@ -22,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" @@ -31,6 +31,7 @@ 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 { From fa919fae9d03ea6a69483761ab5c3c959a98ce89 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 26 May 2021 10:09:21 -0700 Subject: [PATCH 5/5] Update docs/core/encoding.md Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> --- docs/core/encoding.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/encoding.md b/docs/core/encoding.md index 3897cb5bb682..b5d862c35c00 100644 --- a/docs/core/encoding.md +++ b/docs/core/encoding.md @@ -198,7 +198,7 @@ The above `Profile` example is a fictive example used for educational purposes. - 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 `Validator` [object](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/x/staking/types/staking.pb.go#L306-L337) which is fully encoded as `Any`. +- 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: