Skip to content

Commit

Permalink
Use new any type
Browse files Browse the repository at this point in the history
  • Loading branch information
raynaudoe committed Mar 26, 2024
1 parent 56d4c20 commit df6c467
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 39 deletions.
3 changes: 2 additions & 1 deletion baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
gogoprototypes "github.com/cosmos/gogoproto/types/any"
"io"

cmttypes "github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion codec/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"fmt"
gogoprototypes "github.com/cosmos/gogoproto/types/any"
"strings"

"github.com/cosmos/cosmos-proto/anyutil"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions crypto/keyring/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keyring

import (
"errors"
gogoprototypes "github.com/cosmos/gogoproto/types/any"

errorsmod "cosmossdk.io/errors"

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/keys/multisig/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/keys/multisig/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multisig

import (
fmt "fmt"
gogoprototypes "github.com/cosmos/gogoproto/types/any"

cmtcrypto "github.com/cometbft/cometbft/crypto"

Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 8 additions & 7 deletions testutil/testdata/codec.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
8 changes: 5 additions & 3 deletions testutil/testdata/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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)
}

Expand Down
5 changes: 3 additions & 2 deletions types/tx/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion x/auth/migrations/legacytx/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/auth/signing/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/tx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit df6c467

Please sign in to comment.