Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move codec.RegisterCrypto and codec.Cdc to new packages #6330

Merged
merged 15 commits into from
Jun 4, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
```
* (client/rpc) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `RegisterRoutes` of rpc is moved from package client to client/rpc and client/rpc.RegisterRPCRoutes is removed.
* (client/lcd) [\#6290](https://github.com/cosmos/cosmos-sdk/pull/6290) `CliCtx` of struct `RestServer` in package client/lcd has been renamed to `ClientCtx`.
* (codec) [\#6330](https://github.com/cosmos/cosmos-sdk/pull/6330) `codec.RegisterCrypto` has been moved to the `crypto/codec` package and the global `codec.Cdc` Amino instance has been deprecated and moved to the `codec/legacy_global` package.

### Features

Expand Down
3 changes: 2 additions & 1 deletion client/keys/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package keys

import (
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

// KeysCdc defines codec to be used with key operations
var KeysCdc *codec.Codec

func init() {
KeysCdc = codec.New()
codec.RegisterCrypto(KeysCdc)
cryptocodec.RegisterCrypto(KeysCdc)
KeysCdc.Seal()
}

Expand Down
6 changes: 3 additions & 3 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
"github.com/cosmos/cosmos-sdk/types/rest"

tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
Expand Down Expand Up @@ -65,10 +65,10 @@ func getBlock(clientCtx client.Context, height *int64) ([]byte, error) {
}

if clientCtx.Indent {
return codec.Cdc.MarshalJSONIndent(res, "", " ")
return legacy_global.Cdc.MarshalJSONIndent(res, "", " ")
}

return codec.Cdc.MarshalJSON(res)
return legacy_global.Cdc.MarshalJSON(res)
}

// get the current blockchain height
Expand Down
6 changes: 3 additions & 3 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"

Expand Down Expand Up @@ -55,9 +55,9 @@ func printNodeStatus(_ *cobra.Command, _ []string) error {

var output []byte
if clientCtx.Indent {
output, err = codec.Cdc.MarshalJSONIndent(status, "", " ")
output, err = legacy_global.Cdc.MarshalJSONIndent(status, "", " ")
} else {
output, err = codec.Cdc.MarshalJSON(status)
output, err = legacy_global.Cdc.MarshalJSON(status)
}
if err != nil {
return err
Expand Down
23 changes: 2 additions & 21 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ import (
"encoding/json"
"fmt"

amino "github.com/tendermint/go-amino"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
tmtypes "github.com/tendermint/tendermint/types"

amino "github.com/tendermint/go-amino"

"github.com/cosmos/cosmos-sdk/codec/types"
)

// Cdc defines a global generic sealed Amino codec to be used throughout sdk. It
// has all Tendermint crypto and evidence types registered.
//
// TODO: Consider removing this global.
var Cdc *Codec

func init() {
Cdc = New()
RegisterCrypto(Cdc)
RegisterEvidences(Cdc)
Cdc.Seal()
}

// deprecated: Codec defines a wrapper for an Amino codec that properly handles protobuf
// types with Any's
type Codec struct {
Expand All @@ -41,12 +28,6 @@ func New() *Codec {
return &Codec{amino.NewCodec()}
}

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *Codec) {
cryptoamino.RegisterAmino(cdc.Amino)
}

// RegisterEvidences registers Tendermint evidence types with the provided Amino
// codec.
func RegisterEvidences(cdc *Codec) {
Expand Down
19 changes: 19 additions & 0 deletions codec/legacy_global/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package legacy_global
aaronc marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/cosmos/cosmos-sdk/codec"
codec2 "github.com/cosmos/cosmos-sdk/crypto/codec"
)

// Deprecated: Cdc defines a global generic sealed Amino codec to be used throughout sdk. It
// has all Tendermint crypto and evidence types registered.
//
// TODO: remove this global.
var Cdc *codec.Codec

func init() {
Cdc = codec.New()
codec2.RegisterCrypto(Cdc)
codec.RegisterEvidences(Cdc)
Cdc.Seal()
}
12 changes: 12 additions & 0 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package codec

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
)

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *codec.Codec) {
multisig.RegisterCodec(cdc.Amino)
}
14 changes: 9 additions & 5 deletions crypto/types/multisig/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ const (
var cdc = amino.NewCodec()

func init() {
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
cdc.RegisterConcrete(PubKey{},
RegisterCodec(cdc)
}

func RegisterCodec(amino *amino.Codec) {
amino.RegisterInterface((*crypto.PubKey)(nil), nil)
amino.RegisterConcrete(PubKey{},
PubKeyAminoRoute, nil)
cdc.RegisterConcrete(ed25519.PubKeyEd25519{},
amino.RegisterConcrete(ed25519.PubKeyEd25519{},
ed25519.PubKeyAminoName, nil)
cdc.RegisterConcrete(sr25519.PubKeySr25519{},
amino.RegisterConcrete(sr25519.PubKeySr25519{},
sr25519.PubKeyAminoName, nil)
cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{},
amino.RegisterConcrete(secp256k1.PubKeySecp256k1{},
secp256k1.PubKeyAminoName, nil)
}
3 changes: 2 additions & 1 deletion server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
tversion "github.com/tendermint/tendermint/version"

"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -127,7 +128,7 @@ against which this app has been compiled.

func printlnJSON(v interface{}) error {
cdc := codec.New()
codec.RegisterCrypto(cdc)
cryptocodec.RegisterCrypto(cdc)
marshalled, err := cdc.MarshalJSON(v)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion std/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package std
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
Expand Down Expand Up @@ -36,7 +37,7 @@ func MakeCodec(bm module.BasicManager) *codec.Codec {
bm.RegisterCodec(cdc)
vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
cryptocodec.RegisterCrypto(cdc)

return cdc
}
Expand Down
3 changes: 2 additions & 1 deletion types/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -180,7 +181,7 @@ func CheckNotFoundError(w http.ResponseWriter, err error) bool {
func WriteErrorResponse(w http.ResponseWriter, status int, err string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write(codec.Cdc.MustMarshalJSON(NewErrorResponse(0, err)))
_, _ = w.Write(legacy_global.Cdc.MustMarshalJSON(NewErrorResponse(0, err)))
}

// WriteSimulationResponse prepares and writes an HTTP
Expand Down
3 changes: 2 additions & 1 deletion types/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestProcessPostResponse(t *testing.T) {

acc := mockAccount{addr, coins, pubKey, accNumber, sequence}
cdc := codec.New()
codec.RegisterCrypto(cdc)
cryptocodec.RegisterCrypto(cdc)
cdc.RegisterConcrete(&mockAccount{}, "cosmos-sdk/mockAccount", nil)
ctx = ctx.WithCodec(cdc)

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

yaml "gopkg.in/yaml.v2"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"

ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/codec/legacy_global"
"github.com/cosmos/cosmos-sdk/codec/types"
)

func (gi GasInfo) String() string {
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewABCIMessageLog(i uint16, log string, events Events) ABCIMessageLog {
// String implements the fmt.Stringer interface for the ABCIMessageLogs type.
func (logs ABCIMessageLogs) String() (str string) {
if logs != nil {
raw, err := codec.Cdc.MarshalJSON(logs)
raw, err := legacy_global.Cdc.MarshalJSON(logs)
if err == nil {
str = string(raw)
}
Expand Down
4 changes: 2 additions & 2 deletions types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/tendermint/tendermint/libs/bytes"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -31,7 +31,7 @@ func TestABCIMessageLog(t *testing.T) {
msgLog := sdk.NewABCIMessageLog(0, "", events)

msgLogs := sdk.ABCIMessageLogs{msgLog}
bz, err := codec.Cdc.MarshalJSON(msgLogs)
bz, err := legacy_global.Cdc.MarshalJSON(msgLogs)
require.NoError(t, err)
require.Equal(t, string(bz), msgLogs.String())
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/ante/basic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ante

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -131,7 +131,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
PubKey: pubkey.Bytes(),
}

sigBz := codec.Cdc.MustMarshalBinaryBare(simSig)
sigBz := legacy_global.Cdc.MustMarshalBinaryBare(simSig)
cost := sdk.Gas(len(sigBz) + 6)

// If the pubkey is a multi-signature pubkey, then we estimate for the maximum
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 @@ -9,7 +9,7 @@ import (
"github.com/tendermint/tendermint/crypto/multisig"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -305,7 +305,7 @@ func DefaultSigVerificationGasConsumer(

case multisig.PubKeyMultisigThreshold:
var multisignature multisig.Multisignature
codec.Cdc.MustUnmarshalBinaryBare(sig, &multisignature)
legacy_global.Cdc.MustUnmarshalBinaryBare(sig, &multisignature)

ConsumeMultisignatureVerificationGas(meter, multisignature, pubkey, params)
return nil
Expand Down
3 changes: 2 additions & 1 deletion x/auth/client/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
Expand Down Expand Up @@ -158,7 +159,7 @@ func writeToNewTempFile(t *testing.T, data string) *os.File {
func makeCodec() *codec.Codec {
var cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
cryptocodec.RegisterCrypto(cdc)
authtypes.RegisterCodec(cdc)
cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
return cdc
Expand Down
3 changes: 2 additions & 1 deletion x/auth/legacy/v0_39/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34"
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
Expand All @@ -14,7 +15,7 @@ import (

func TestMigrate(t *testing.T) {
v039Codec := codec.New()
codec.RegisterCrypto(v039Codec)
cryptocodec.RegisterCrypto(v039Codec)
v038auth.RegisterCodec(v039Codec)

coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
Expand Down
3 changes: 2 additions & 1 deletion x/auth/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

// RegisterCodec registers the account interfaces and concrete types on the
Expand Down Expand Up @@ -41,5 +42,5 @@ var (

func init() {
RegisterCodec(amino)
codec.RegisterCrypto(amino)
cryptocodec.RegisterCrypto(amino)
}
5 changes: 3 additions & 2 deletions x/auth/types/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
yaml "gopkg.in/yaml.v2"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy_global"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (fee StdFee) Bytes() []byte {
fee.Amount = sdk.NewCoins()
}

bz, err := codec.Cdc.MarshalJSON(fee)
bz, err := legacy_global.Cdc.MarshalJSON(fee)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -295,7 +296,7 @@ func StdSignBytes(chainID string, accnum uint64, sequence uint64, fee StdFee, ms
msgsBytes = append(msgsBytes, json.RawMessage(msg.GetSignBytes()))
}

bz, err := codec.Cdc.MarshalJSON(StdSignDoc{
bz, err := legacy_global.Cdc.MarshalJSON(StdSignDoc{
AccountNumber: accnum,
ChainID: chainID,
Fee: json.RawMessage(fee.Bytes()),
Expand Down
Loading