Skip to content

Commit

Permalink
chore(all)!: use gogoproto/any instead of codec/types/any (#21013)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Jul 22, 2024
1 parent 71d14f9 commit 9ab162d
Show file tree
Hide file tree
Showing 48 changed files with 222 additions and 191 deletions.
4 changes: 3 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ There is no longer a need for the Cosmos SDK to host these protos for itself and
That package containing proto v2 generated code, but the SDK now uses [buf generated go SDK instead](https://buf.build/docs/bsr/generated-sdks/go).
If you were depending on `cosmossdk.io/api/tendermint`, please use the buf generated go SDK instead, or ask CometBFT host the generated proto v2 code.

The `codectypes.Any` has moved to `github.com/cosmos/gogoproto/types/any`. Module developers can update the `buf.gen.gogo.yaml` configuration files by adjusting the corresponding `opt` option to `Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any` for directly mapping the`Any` type to its new location. This change is optional as `codectypes.Any` is aliased to `gogoproto.Any` in the SDK.
The `codectypes.Any` has moved to `github.com/cosmos/gogoproto/types/any`. Module developers need to update the `buf.gen.gogo.yaml` configuration files by adjusting the corresponding `opt` option to `Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any` for directly mapping the`Any` type to its new location. This change is optional, but recommended, as `codectypes.Any` is aliased to `gogoproto.Any` in the SDK.

Also, any usages of the interfaces `AnyUnpacker` and `UnpackInterfacesMessage` must be replaced with the interfaces of the same name in the `github.com/cosmos/gogoproto/types/any` package.

### Modules

Expand Down
7 changes: 4 additions & 3 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
gogogrpc "github.com/cosmos/gogoproto/grpc"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -20,8 +21,8 @@ import (
)

var (
_ ServiceServer = queryServer{}
_ codectypes.UnpackInterfacesMessage = &GetLatestValidatorSetResponse{}
_ ServiceServer = queryServer{}
_ gogoprotoany.UnpackInterfacesMessage = &GetLatestValidatorSetResponse{}
)

type (
Expand Down Expand Up @@ -112,7 +113,7 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa
return ValidatorsOutput(ctx, s.clientCtx, nil, page, limit)
}

func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
for _, val := range m.Validators {
err := unpacker.UnpackAny(val.PubKey, &pubKey)
Expand Down
23 changes: 23 additions & 0 deletions client/keys/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

func generatePubKeys(n int) []types.PubKey {
Expand Down Expand Up @@ -100,3 +102,24 @@ func TestProtoMarshalJSON(t *testing.T) {
require.Equal(ko.Address, expectedOutput)
require.Equal(ko.PubKey, string(bz))
}

func TestNestedMultisigOutput(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

sk := secp256k1.PrivKey{Key: []byte{154, 49, 3, 117, 55, 232, 249, 20, 205, 216, 102, 7, 136, 72, 177, 2, 131, 202, 234, 81, 31, 208, 46, 244, 179, 192, 167, 163, 142, 117, 246, 13}}
tmpKey := sk.PubKey()
multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey})
multisigPk2 := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey, multisigPk})

kb, err := keyring.New(t.Name(), keyring.BackendTest, t.TempDir(), nil, cdc)
require.NoError(t, err)

_, err = kb.SaveMultisig("multisig", multisigPk2)
require.NoError(t, err)

k, err := kb.Key("multisig")
require.NoError(t, err)

_, err = MkAccKeyOutput(k, addresscodec.NewBech32Codec("cosmos"))
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codec

import (
"github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"google.golang.org/grpc/encoding"
"google.golang.org/protobuf/reflect/protoreflect"

Expand Down Expand Up @@ -76,7 +77,7 @@ type (
// is not registered in codec, or is not compatible with the serialized data
UnmarshalInterface(bz []byte, ptr interface{}) error

types.AnyUnpacker
gogoprotoany.AnyUnpacker
}

JSONCodec interface {
Expand Down
44 changes: 4 additions & 40 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,18 @@ import (

"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/registry"
"cosmossdk.io/x/tx/signing"
)

// AnyUnpacker is an interface which allows safely unpacking types packed
// in Any's against a whitelist of registered types
type AnyUnpacker interface {
// UnpackAny unpacks the value in any to the interface pointer passed in as
// iface. Note that the type in any must have been registered in the
// underlying whitelist registry as a concrete type for that interface
// Ex:
// var msg sdk.Msg
// err := cdc.UnpackAny(any, &msg)
// ...
UnpackAny(any *Any, iface interface{}) error
}

// UnpackInterfacesMessage is meant to extend protobuf types (which implement
// proto.Message) to support a post-deserialization phase which unpacks
// types packed within Any's using the whitelist provided by AnyUnpacker
type UnpackInterfacesMessage interface {
// UnpackInterfaces is implemented in order to unpack values packed within
// Any's using the AnyUnpacker. It should generally be implemented as
// follows:
// func (s *MyStruct) UnpackInterfaces(unpacker AnyUnpacker) error {
// var x AnyInterface
// // where X is an Any field on MyStruct
// err := unpacker.UnpackAny(s.X, &x)
// if err != nil {
// return nil
// }
// // where Y is a field on MyStruct that implements UnpackInterfacesMessage itself
// err = s.Y.UnpackInterfaces(unpacker)
// if err != nil {
// return nil
// }
// return nil
// }
UnpackInterfaces(unpacker AnyUnpacker) error
}

// UnpackInterfaces is a convenience function that calls UnpackInterfaces
// on x if x implements UnpackInterfacesMessage
func UnpackInterfaces(x interface{}, unpacker AnyUnpacker) error {
if msg, ok := x.(UnpackInterfacesMessage); ok {
func UnpackInterfaces(x interface{}, unpacker gogoprotoany.AnyUnpacker) error {
if msg, ok := x.(gogoprotoany.UnpackInterfacesMessage); ok {
return msg.UnpackInterfaces(unpacker)
}
return nil
Expand All @@ -65,7 +29,7 @@ var protoMessageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
// InterfaceRegistry provides a mechanism for registering interfaces and
// implementations that can be safely unpacked from Any
type InterfaceRegistry interface {
AnyUnpacker
gogoprotoany.AnyUnpacker
jsonpb.AnyResolver
registry.InterfaceRegistrar

Expand Down
4 changes: 3 additions & 1 deletion crypto/keyring/legacy_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

gogoprotoany "github.com/cosmos/gogoproto/types/any"

"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
Expand Down Expand Up @@ -218,7 +220,7 @@ func (i LegacyMultiInfo) GetPath() (*hd.BIP44Params, error) {
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (i LegacyMultiInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (i LegacyMultiInfo) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
multiPK := i.PubKey.(*multisig.LegacyAminoPubKey)

return codectypes.UnpackInterfaces(multiPK, unpacker)
Expand Down
4 changes: 3 additions & 1 deletion crypto/keyring/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keyring
import (
"errors"

gogoprotoany "github.com/cosmos/gogoproto/types/any"

errorsmod "cosmossdk.io/errors"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -103,7 +105,7 @@ func (k Record) GetType() KeyType {
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (k *Record) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (k *Record) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var pk cryptotypes.PubKey
if err := unpacker.UnpackAny(k.PubKey, &pk); err != nil {
return err
Expand Down
12 changes: 4 additions & 8 deletions crypto/keys/multisig/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cmtcrypto "github.com/cometbft/cometbft/crypto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"

"github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -12,8 +13,8 @@ import (
)

var (
_ multisigtypes.PubKey = &LegacyAminoPubKey{}
_ types.UnpackInterfacesMessage = &LegacyAminoPubKey{}
_ multisigtypes.PubKey = &LegacyAminoPubKey{}
_ gogoprotoany.UnpackInterfacesMessage = &LegacyAminoPubKey{}
)

// NewLegacyAminoPubKey returns a new LegacyAminoPubKey.
Expand Down Expand Up @@ -149,7 +150,7 @@ func (m *LegacyAminoPubKey) Type() string {
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m *LegacyAminoPubKey) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m *LegacyAminoPubKey) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
for _, any := range m.PubKeys {
var pk cryptotypes.PubKey
err := unpacker.UnpackAny(any, &pk)
Expand All @@ -169,11 +170,6 @@ func packPubKeys(pubKeys []cryptotypes.PubKey) ([]*types.Any, error) {
return nil, err
}
anyPubKeys[i] = any

// sets the compat.aminoBz value
if err := anyPubKeys[i].UnmarshalAmino(pubKeys[i].Bytes()); err != nil {
return nil, err
}
}
return anyPubKeys, nil
}
10 changes: 5 additions & 5 deletions server/v2/cometbft/client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"strings"

"cosmossdk.io/server/v2/cometbft/client/rpc"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
gogogrpc "github.com/cosmos/gogoproto/grpc"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"cosmossdk.io/server/v2/cometbft/client/rpc"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -22,8 +22,8 @@ import (
)

var (
_ ServiceServer = queryServer{}
_ codectypes.UnpackInterfacesMessage = &GetLatestValidatorSetResponse{}
_ ServiceServer = queryServer{}
_ gogoprotoany.UnpackInterfacesMessage = &GetLatestValidatorSetResponse{}
)

const (
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s queryServer) GetLatestValidatorSet(
return ValidatorsOutput(ctx, s.client, nil, page, limit)
}

func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
for _, val := range m.Validators {
err := unpacker.UnpackAny(val.PubKey, &pubKey)
Expand Down
15 changes: 7 additions & 8 deletions testutil/testdata/animal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"fmt"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/codec/types"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
)

type Animal interface {
Expand All @@ -32,9 +31,9 @@ func (d Dog) Greet() string {
return fmt.Sprintf("Roof, my name is %s", d.Name)
}

var _ types.UnpackInterfacesMessage = HasAnimal{}
var _ gogoprotoany.UnpackInterfacesMessage = HasAnimal{}

func (m HasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m HasAnimal) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var animal Animal
return unpacker.UnpackAny(m.Animal, &animal)
}
Expand All @@ -59,9 +58,9 @@ func (m HasHasAnimal) TheHasAnimal() HasAnimalI {
return m.HasAnimal.GetCachedValue().(HasAnimalI)
}

var _ types.UnpackInterfacesMessage = HasHasAnimal{}
var _ gogoprotoany.UnpackInterfacesMessage = HasHasAnimal{}

func (m HasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m HasHasAnimal) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var animal HasAnimalI
return unpacker.UnpackAny(m.HasAnimal, &animal)
}
Expand All @@ -76,9 +75,9 @@ func (m HasHasHasAnimal) TheHasHasAnimal() HasHasAnimalI {
return m.HasHasAnimal.GetCachedValue().(HasHasAnimalI)
}

var _ types.UnpackInterfacesMessage = HasHasHasAnimal{}
var _ gogoprotoany.UnpackInterfacesMessage = HasHasHasAnimal{}

func (m HasHasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m HasHasHasAnimal) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var animal HasHasAnimalI
return unpacker.UnpackAny(m.HasHasAnimal, &animal)
}
9 changes: 5 additions & 4 deletions testutil/testdata/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

gogoprotoany "github.com/cosmos/gogoproto/types/any"
"github.com/cosmos/gogoproto/types/any/test"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -49,16 +50,16 @@ func (e QueryImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHe
return &SayHelloResponse{Greeting: greeting}, nil
}

var _ types.UnpackInterfacesMessage = &TestAnyRequest{}
var _ gogoprotoany.UnpackInterfacesMessage = &TestAnyRequest{}

func (m *TestAnyRequest) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m *TestAnyRequest) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
var animal test.Animal
return unpacker.UnpackAny(m.AnyAnimal, &animal)
}

var _ types.UnpackInterfacesMessage = &TestAnyResponse{}
var _ gogoprotoany.UnpackInterfacesMessage = &TestAnyResponse{}

func (m *TestAnyResponse) UnpackInterfaces(unpacker types.AnyUnpacker) error {
func (m *TestAnyResponse) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
return m.HasAnimal.UnpackInterfaces(unpacker)
}

Expand Down
7 changes: 4 additions & 3 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
gogoprotoany "github.com/cosmos/gogoproto/types/any"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -159,13 +160,13 @@ func ParseABCILogs(logs string) (res ABCIMessageLogs, err error) {
return res, err
}

var _, _ codectypes.UnpackInterfacesMessage = SearchTxsResult{}, TxResponse{}
var _, _ gogoprotoany.UnpackInterfacesMessage = SearchTxsResult{}, TxResponse{}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
//
// types.UnpackInterfaces needs to be called for each nested Tx because
// there are generally interfaces to unpack in Tx's
func (s SearchTxsResult) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (s SearchTxsResult) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
for _, tx := range s.Txs {
err := codectypes.UnpackInterfaces(tx, unpacker)
if err != nil {
Expand All @@ -176,7 +177,7 @@ func (s SearchTxsResult) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (r TxResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (r TxResponse) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
if r.Tx != nil {
var tx HasMsgs
return unpacker.UnpackAny(r.Tx, &tx)
Expand Down
Loading

0 comments on commit 9ab162d

Please sign in to comment.