From df6c4671d11a541ac88ddfdc0c02e3136016aac7 Mon Sep 17 00:00:00 2001 From: Ezequiel Raynaudo Date: Tue, 26 Mar 2024 10:07:29 -0300 Subject: [PATCH] Use new any type --- baseapp/msg_service_router.go | 3 ++- client/grpc/cmtservice/service.go | 3 ++- codec/amino.go | 9 +++++---- codec/json.go | 3 ++- codec/proto_codec.go | 7 ++++--- crypto/keyring/record.go | 5 +++-- crypto/keys/multisig/amino.go | 3 ++- crypto/keys/multisig/multisig.go | 3 ++- testutil/testdata/codec.go | 15 ++++++++------- testutil/testdata/grpc_query.go | 8 +++++--- types/tx/msgs.go | 5 +++-- x/auth/ante/sigverify.go | 4 ++-- x/auth/migrations/legacytx/stdtx.go | 3 ++- x/auth/signing/adapter.go | 4 ++-- x/auth/tx/builder.go | 5 +++-- x/auth/tx/query.go | 4 ++-- x/auth/tx/sigs.go | 4 ++-- x/auth/types/account.go | 3 ++- x/auth/types/genesis.go | 3 ++- 19 files changed, 55 insertions(+), 39 deletions(-) diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index 21ba4371e426..5b097c2bf27b 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -3,6 +3,7 @@ package baseapp import ( "context" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" abci "github.com/cometbft/cometbft/abci/types" gogogrpc "github.com/cosmos/gogoproto/grpc" @@ -222,7 +223,7 @@ func (msr *MsgServiceRouter) registerMsgServiceHandler(sd *grpc.ServiceDesc, met return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting proto.Message, got %T", resMsg) } - anyResp, err := codectypes.NewAnyWithValue(resMsg) + anyResp, err := gogoprototypes.NewAnyWithCacheWithValue(resMsg) if err != nil { return nil, err } diff --git a/client/grpc/cmtservice/service.go b/client/grpc/cmtservice/service.go index e26e65332806..7f64b9ae43d1 100644 --- a/client/grpc/cmtservice/service.go +++ b/client/grpc/cmtservice/service.go @@ -2,6 +2,7 @@ package cmtservice import ( "context" + gogoprototypes "github.com/cosmos/gogoproto/types/any" abci "github.com/cometbft/cometbft/abci/types" gogogrpc "github.com/cosmos/gogoproto/grpc" @@ -171,7 +172,7 @@ func ValidatorsOutput(ctx context.Context, clientCtx client.Context, height *int if err != nil { return nil, err } - anyPub, err := codectypes.NewAnyWithValue(pk) + anyPub, err := gogoprototypes.NewAnyWithCacheWithValue(pk) if err != nil { return nil, err } diff --git a/codec/amino.go b/codec/amino.go index f17dbcc4a43f..1f068589b69b 100644 --- a/codec/amino.go +++ b/codec/amino.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "io" cmttypes "github.com/cometbft/cometbft/types" @@ -61,19 +62,19 @@ func MustMarshalJSONIndent(cdc *LegacyAmino, obj interface{}) []byte { } func (cdc *LegacyAmino) marshalAnys(o interface{}) error { - return types.UnpackInterfaces(o, types.AminoPacker{Cdc: cdc.Amino}) + return types.UnpackInterfaces(o, gogoprototypes.AminoPacker{Cdc: cdc.Amino}) } func (cdc *LegacyAmino) unmarshalAnys(o interface{}) error { - return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: cdc.Amino}) + return types.UnpackInterfaces(o, gogoprototypes.AminoUnpacker{Cdc: cdc.Amino}) } func (cdc *LegacyAmino) jsonMarshalAnys(o interface{}) error { - return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: cdc.Amino}) + return types.UnpackInterfaces(o, gogoprototypes.AminoJSONPacker{Cdc: cdc.Amino}) } func (cdc *LegacyAmino) jsonUnmarshalAnys(o interface{}) error { - return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino}) + return types.UnpackInterfaces(o, gogoprototypes.AminoJSONUnpacker{Cdc: cdc.Amino}) } func (cdc *LegacyAmino) Marshal(o interface{}) ([]byte, error) { diff --git a/codec/json.go b/codec/json.go index 2e913bfd11d8..d89aeaa21fee 100644 --- a/codec/json.go +++ b/codec/json.go @@ -2,6 +2,7 @@ package codec import ( "bytes" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "github.com/cosmos/gogoproto/jsonpb" "github.com/cosmos/gogoproto/proto" @@ -20,7 +21,7 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e if resolver != nil { jm = &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: resolver} } - err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm}) + err := types.UnpackInterfaces(msg, gogoprototypes.ProtoJSONPacker{JSONPBMarshaler: jm}) if err != nil { return nil, err } diff --git a/codec/proto_codec.go b/codec/proto_codec.go index 74be56f35ea4..08d0e0ae4602 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "strings" "github.com/cosmos/cosmos-proto/anyutil" @@ -227,7 +228,7 @@ func (pc *ProtoCodec) MarshalInterface(i gogoproto.Message) ([]byte, error) { if err := assertNotNil(i); err != nil { return nil, err } - any, err := types.NewAnyWithValue(i) + any, err := gogoprototypes.NewAnyWithCacheWithValue(i) if err != nil { return nil, err } @@ -262,7 +263,7 @@ func (pc *ProtoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error { // packs the provided value in an Any and then marshals it to bytes. // NOTE: to marshal a concrete type, you should use MarshalJSON instead func (pc *ProtoCodec) MarshalInterfaceJSON(x gogoproto.Message) ([]byte, error) { - any, err := types.NewAnyWithValue(x) + any, err := gogoprototypes.NewAnyWithCacheWithValue(x) if err != nil { return nil, err } @@ -321,7 +322,7 @@ func (pc *ProtoCodec) GetMsgV1Signers(msg gogoproto.Message) ([][]byte, proto.Me signers, err := pc.interfaceRegistry.SigningContext().GetSigners(msgV2) return signers, msgV2, err } - a, err := types.NewAnyWithValue(msg) + a, err := gogoprototypes.NewAnyWithCacheWithValue(msg) if err != nil { return nil, nil, err } diff --git a/crypto/keyring/record.go b/crypto/keyring/record.go index 96141e4c906e..e9e86b6592b2 100644 --- a/crypto/keyring/record.go +++ b/crypto/keyring/record.go @@ -2,6 +2,7 @@ package keyring import ( "errors" + gogoprototypes "github.com/cosmos/gogoproto/types/any" errorsmod "cosmossdk.io/errors" @@ -21,7 +22,7 @@ var ( ) func newRecord(name string, pk cryptotypes.PubKey, item isRecord_Item) (*Record, error) { - any, err := codectypes.NewAnyWithValue(pk) + any, err := gogoprototypes.NewAnyWithCacheWithValue(pk) if err != nil { return nil, err } @@ -31,7 +32,7 @@ func newRecord(name string, pk cryptotypes.PubKey, item isRecord_Item) (*Record, // NewLocalRecord creates a new Record with local key item func NewLocalRecord(name string, priv cryptotypes.PrivKey, pk cryptotypes.PubKey) (*Record, error) { - any, err := codectypes.NewAnyWithValue(priv) + any, err := gogoprototypes.NewAnyWithCacheWithValue(priv) if err != nil { return nil, err } diff --git a/crypto/keys/multisig/amino.go b/crypto/keys/multisig/amino.go index 8fa596906d89..a9763203a7f2 100644 --- a/crypto/keys/multisig/amino.go +++ b/crypto/keys/multisig/amino.go @@ -2,6 +2,7 @@ package multisig import ( errorsmod "cosmossdk.io/errors" + gogoprototypes "github.com/cosmos/gogoproto/types/any" types "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -53,7 +54,7 @@ func tmToProto(tmPk tmMultisig) (*LegacyAminoPubKey, error) { var err error pks := make([]*types.Any, len(tmPk.PubKeys)) for i, pk := range tmPk.PubKeys { - pks[i], err = types.NewAnyWithValue(pk) + pks[i], err = gogoprototypes.NewAnyWithCacheWithValue(pk) if err != nil { return nil, err } diff --git a/crypto/keys/multisig/multisig.go b/crypto/keys/multisig/multisig.go index 10f7d2e04000..86901e3c421c 100644 --- a/crypto/keys/multisig/multisig.go +++ b/crypto/keys/multisig/multisig.go @@ -2,6 +2,7 @@ package multisig import ( fmt "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" cmtcrypto "github.com/cometbft/cometbft/crypto" @@ -164,7 +165,7 @@ func packPubKeys(pubKeys []cryptotypes.PubKey) ([]*types.Any, error) { anyPubKeys := make([]*types.Any, len(pubKeys)) for i := 0; i < len(pubKeys); i++ { - any, err := types.NewAnyWithValue(pubKeys[i]) + any, err := gogoprototypes.NewAnyWithCacheWithValue(pubKeys[i]) if err != nil { return nil, err } diff --git a/testutil/testdata/codec.go b/testutil/testdata/codec.go index 6e4dcc253b58..69cd3b9e6ad5 100644 --- a/testutil/testdata/codec.go +++ b/testutil/testdata/codec.go @@ -1,6 +1,7 @@ package testdata import ( + "github.com/cosmos/gogoproto/types/any/test" amino "github.com/tendermint/go-amino" "github.com/cosmos/cosmos-sdk/codec/types" @@ -42,15 +43,15 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { func NewTestAmino() *amino.Codec { cdc := amino.NewCodec() - cdc.RegisterInterface((*Animal)(nil), nil) - cdc.RegisterConcrete(&Dog{}, "testpb/Dog", nil) - cdc.RegisterConcrete(&Cat{}, "testpb/Cat", nil) + cdc.RegisterInterface((*test.Animal)(nil), nil) + cdc.RegisterConcrete(&test.Dog{}, "testpb/Dog", nil) + cdc.RegisterConcrete(&test.Cat{}, "testpb/Cat", nil) - cdc.RegisterInterface((*HasAnimalI)(nil), nil) - cdc.RegisterConcrete(&HasAnimal{}, "testpb/HasAnimal", nil) + cdc.RegisterInterface((*test.HasAnimalI)(nil), nil) + cdc.RegisterConcrete(&test.HasAnimal{}, "testpb/HasAnimal", nil) - cdc.RegisterInterface((*HasHasAnimalI)(nil), nil) - cdc.RegisterConcrete(&HasHasAnimal{}, "testpb/HasHasAnimal", nil) + cdc.RegisterInterface((*test.HasHasAnimalI)(nil), nil) + cdc.RegisterConcrete(&test.HasHasAnimal{}, "testpb/HasHasAnimal", nil) return cdc } diff --git a/testutil/testdata/grpc_query.go b/testutil/testdata/grpc_query.go index 510e554d433e..00232b5f7874 100644 --- a/testutil/testdata/grpc_query.go +++ b/testutil/testdata/grpc_query.go @@ -3,6 +3,8 @@ package testdata import ( "context" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" + "github.com/cosmos/gogoproto/types/any/test" "testing" "github.com/cosmos/gogoproto/proto" @@ -22,12 +24,12 @@ type QueryImpl struct{} var _ QueryServer = QueryImpl{} func (e QueryImpl) TestAny(_ context.Context, request *TestAnyRequest) (*TestAnyResponse, error) { - animal, ok := request.AnyAnimal.GetCachedValue().(Animal) + animal, ok := request.AnyAnimal.GetCachedValue().(test.Animal) if !ok { return nil, fmt.Errorf("expected Animal") } - any, err := types.NewAnyWithValue(animal.(proto.Message)) + any, err := gogoprototypes.NewAnyWithCacheWithValue(animal.(proto.Message)) if err != nil { return nil, err } @@ -50,7 +52,7 @@ func (e QueryImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHe var _ types.UnpackInterfacesMessage = &TestAnyRequest{} func (m *TestAnyRequest) UnpackInterfaces(unpacker types.AnyUnpacker) error { - var animal Animal + var animal test.Animal return unpacker.UnpackAny(m.AnyAnimal, &animal) } diff --git a/types/tx/msgs.go b/types/tx/msgs.go index a27aebb2056e..0b7f531211f5 100644 --- a/types/tx/msgs.go +++ b/types/tx/msgs.go @@ -2,6 +2,7 @@ package tx import ( fmt "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -18,7 +19,7 @@ type MsgResponse interface{} // SetMsg takes a sdk.Msg and turn them into Any. func SetMsg(msg sdk.Msg) (*types.Any, error) { - any, err := types.NewAnyWithValue(msg) + any, err := gogoprototypes.NewAnyWithCacheWithValue(msg) if err != nil { return nil, err } @@ -31,7 +32,7 @@ func SetMsgs(msgs []sdk.Msg) ([]*types.Any, error) { anys := make([]*types.Any, len(msgs)) for i, msg := range msgs { var err error - anys[i], err = types.NewAnyWithValue(msg) + anys[i], err = gogoprototypes.NewAnyWithCacheWithValue(msg) if err != nil { return nil, err } diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index facc160fcee3..bc66c5c76aa5 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" secp256k1dcrd "github.com/decred/dcrd/dcrec/secp256k1/v4" "google.golang.org/protobuf/types/known/anypb" @@ -17,7 +18,6 @@ import ( "cosmossdk.io/x/auth/types" txsigning "cosmossdk.io/x/tx/signing" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -337,7 +337,7 @@ func (svd SigVerificationDecorator) verifySig(ctx sdk.Context, tx sdk.Tx, acc sd accNum = 0 } - anyPk, _ := codectypes.NewAnyWithValue(pubKey) + anyPk, _ := gogoprototypes.NewAnyWithCacheWithValue(pubKey) signerData := txsigning.SignerData{ Address: acc.GetAddress().String(), diff --git a/x/auth/migrations/legacytx/stdtx.go b/x/auth/migrations/legacytx/stdtx.go index 900991c01450..3dbae86be78e 100644 --- a/x/auth/migrations/legacytx/stdtx.go +++ b/x/auth/migrations/legacytx/stdtx.go @@ -3,6 +3,7 @@ package legacytx import ( errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" + types "github.com/cosmos/gogoproto/types/any" "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -105,7 +106,7 @@ func (tx StdTx) GetMsgs() []sdk.Msg { return tx.Msgs } // so it can't be saved into protobuf configured storage. We are using it only for API // compatibility. func (tx *StdTx) AsAny() *codectypes.Any { - return codectypes.UnsafePackAny(tx) + return types.UnsafePackAnyWithCache(tx) } // GetMemo returns the memo diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index 42b1b82fdc48..f27f872e69cf 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -3,12 +3,12 @@ package signing import ( "context" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "google.golang.org/protobuf/types/known/anypb" txsigning "cosmossdk.io/x/tx/signing" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" ) @@ -43,7 +43,7 @@ func GetSignBytesAdapter( var pubKey *anypb.Any if signerData.PubKey != nil { - anyPk, err := codectypes.NewAnyWithValue(signerData.PubKey) + anyPk, err := gogoprototypes.NewAnyWithCacheWithValue(signerData.PubKey) if err != nil { return nil, err } diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 531e40a2f672..b256eede426b 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -2,6 +2,7 @@ package tx import ( "fmt" + gogoproto "github.com/cosmos/gogoproto/types/any" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" @@ -157,7 +158,7 @@ func (w *builder) getTx() (*gogoTxWrapper, error) { func msgsV1toAnyV2(msgs []sdk.Msg) ([]*anypb.Any, error) { anys := make([]*codectypes.Any, len(msgs)) for i, msg := range msgs { - anyMsg, err := codectypes.NewAnyWithValue(msg) + anyMsg, err := gogoproto.NewAnyWithCacheWithValue(msg) if err != nil { return nil, err } @@ -211,7 +212,7 @@ func (w *builder) SetSignatures(signatures ...signing.SignatureV2) error { ) modeInfo, rawSigs[i] = SignatureDataToModeInfoAndSig(sig.Data) if sig.PubKey != nil { - pubKey, err = codectypes.NewAnyWithValue(sig.PubKey) + pubKey, err = gogoproto.NewAnyWithCacheWithValue(sig.PubKey) if err != nil { return err } diff --git a/x/auth/tx/query.go b/x/auth/tx/query.go index ace17bfbe048..812b00f5a0d2 100644 --- a/x/auth/tx/query.go +++ b/x/auth/tx/query.go @@ -5,12 +5,12 @@ import ( "encoding/hex" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "time" coretypes "github.com/cometbft/cometbft/rpc/core/types" "github.com/cosmos/cosmos-sdk/client" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" querytypes "github.com/cosmos/cosmos-sdk/types/query" ) @@ -145,7 +145,7 @@ func mkTxResult(txConfig client.TxConfig, resTx *coretypes.ResultTx, resBlock *c if err != nil { return nil, err } - anyTx, err := codectypes.NewAnyWithValue(tx) + anyTx, err := gogoprototypes.NewAnyWithCacheWithValue(tx) if err != nil { return nil, err } diff --git a/x/auth/tx/sigs.go b/x/auth/tx/sigs.go index 1c182567f9cf..16e1037e7e2e 100644 --- a/x/auth/tx/sigs.go +++ b/x/auth/tx/sigs.go @@ -2,11 +2,11 @@ package tx import ( "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" @@ -115,7 +115,7 @@ func (g config) MarshalSignatureJSON(sigs []signing.SignatureV2) ([]byte, error) for i, sig := range sigs { descData := signing.SignatureDataToProto(sig.Data) - any, err := codectypes.NewAnyWithValue(sig.PubKey) + any, err := gogoprototypes.NewAnyWithCacheWithValue(sig.PubKey) if err != nil { return nil, err } diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 529fc28914b3..d97a2a45e37e 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "strings" "github.com/cometbft/cometbft/crypto" @@ -86,7 +87,7 @@ func (acc *BaseAccount) SetPubKey(pubKey cryptotypes.PubKey) error { acc.PubKey = nil return nil } - any, err := codectypes.NewAnyWithValue(pubKey) + any, err := gogoprototypes.NewAnyWithCacheWithValue(pubKey) if err == nil { acc.PubKey = any } diff --git a/x/auth/types/genesis.go b/x/auth/types/genesis.go index 82a45779c3cb..0d511b0383d0 100644 --- a/x/auth/types/genesis.go +++ b/x/auth/types/genesis.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + gogoprototypes "github.com/cosmos/gogoproto/types/any" "sort" proto "github.com/cosmos/gogoproto/proto" @@ -171,7 +172,7 @@ func PackAccounts(accounts GenesisAccounts) ([]*types.Any, error) { if !ok { return nil, fmt.Errorf("cannot proto marshal %T", acc) } - any, err := types.NewAnyWithValue(msg) + any, err := gogoprototypes.NewAnyWithCacheWithValue(msg) if err != nil { return nil, err }