Skip to content

Commit

Permalink
codec: Rename codec and marshaler interfaces (cosmos#9226)
Browse files Browse the repository at this point in the history
* codec: Rename codec and marshaler interfaces, ref: 8413

* codec: remove BinaryBare

* changelog update

* Update comments and documentation

* adding doc string comments

* Update CHANGELOG.md

Co-authored-by: Federico Kunze <[email protected]>

* Update codec/codec.go

Co-authored-by: Marko <[email protected]>

Co-authored-by: Federico Kunze <[email protected]>
Co-authored-by: Amaury <[email protected]>
Co-authored-by: Marko <[email protected]>
  • Loading branch information
4 people authored Apr 29, 2021
1 parent c94e9eb commit be4a965
Show file tree
Hide file tree
Showing 162 changed files with 824 additions and 621 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (auth/tx) [\#8926](https://github.com/cosmos/cosmos-sdk/pull/8926) The `ProtoTxProvider` interface used as a workaround for transaction simulation has been removed.
* (x/bank) [\#8798](https://github.com/cosmos/cosmos-sdk/pull/8798) `GetTotalSupply` is removed in favour of `GetPaginatedTotalSupply`
* (x/bank/types) [\#9061](https://github.com/cosmos/cosmos-sdk/pull/9061) `AddressFromBalancesStore` now returns an error for invalid key instead of panic.
* (codec) [\#9061](https://github.com/cosmos/cosmos-sdk/pull/9226) Rename codec interfaces and methods, to follow a general Go interfaces:
* `codec.Marshaler``codec.Codec` (this defines objects which serialize other objects)
* `codec.BinaryMarshaler``codec.BinaryCodec`
* `codec.JSONMarshaler``codec.JSONCodec`
* Removed `BinaryBare` suffix from `BinaryCodec` methods (`MarshalBinaryBare`, `UnmarshalBinaryBare`, ...)
* Removed `Binary` infix from `BinaryCodec` methods (`MarshalBinaryLengthPrefixed`, `UnmarshalBinaryLengthPrefixed`, ...)



Expand Down
30 changes: 15 additions & 15 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options
tx.Msgs = append(tx.Msgs, msgKeyValue{Key: key, Value: value})
keyCounter++
}
txBytes, err := codec.MarshalBinaryBare(tx)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)
resp := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, resp.IsOK(), "%v", resp.String())
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestTxDecoder(t *testing.T) {

app := newBaseApp(t.Name())
tx := newTxCounter(1, 0)
txBytes := codec.MustMarshalBinaryBare(tx)
txBytes := codec.MustMarshal(tx)

dTx, err := app.txDecoder(txBytes)
require.NoError(t, err)
Expand Down Expand Up @@ -804,7 +804,7 @@ func testTxDecoder(cdc *codec.LegacyAmino) sdk.TxDecoder {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty")
}

err := cdc.UnmarshalBinaryBare(txBytes, &tx)
err := cdc.Unmarshal(txBytes, &tx)
if err != nil {
return nil, sdkerrors.ErrTxDecode
}
Expand Down Expand Up @@ -935,7 +935,7 @@ func TestCheckTx(t *testing.T) {

for i := int64(0); i < nTxs; i++ {
tx := newTxCounter(i, 0) // no messages
txBytes, err := codec.MarshalBinaryBare(tx)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)
r := app.CheckTx(abci.RequestCheckTx{Tx: txBytes})
require.Empty(t, r.GetEvents())
Expand Down Expand Up @@ -991,7 +991,7 @@ func TestDeliverTx(t *testing.T) {
counter := int64(blockN*txPerHeight + i)
tx := newTxCounter(counter, counter)

txBytes, err := codec.MarshalBinaryBare(tx)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
header := tmproto.Header{Height: 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
tx := newTxCounter(0, 0, 1, 2)
txBytes, err := codec.MarshalBinaryBare(tx)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)
res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
Expand All @@ -1061,7 +1061,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
tx = newTxCounter(1, 3)
tx.Msgs = append(tx.Msgs, msgCounter2{0})
tx.Msgs = append(tx.Msgs, msgCounter2{1})
txBytes, err = codec.MarshalBinaryBare(tx)
txBytes, err = codec.Marshal(tx)
require.NoError(t, err)
res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
Expand Down Expand Up @@ -1123,7 +1123,7 @@ func TestSimulateTx(t *testing.T) {
app.BeginBlock(abci.RequestBeginBlock{Header: header})

tx := newTxCounter(count, count)
txBytes, err := cdc.MarshalBinaryBare(tx)
txBytes, err := cdc.Marshal(tx)
require.Nil(t, err)

// simulate a message, check gas reported
Expand Down Expand Up @@ -1252,7 +1252,7 @@ func TestRunInvalidTransaction(t *testing.T) {
registerTestCodec(newCdc)
newCdc.RegisterConcrete(&msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode", nil)

txBytes, err := newCdc.MarshalBinaryBare(tx)
txBytes, err := newCdc.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand Down Expand Up @@ -1520,7 +1520,7 @@ func TestBaseAppAnteHandler(t *testing.T) {
// the next txs ante handler execution (anteHandlerTxTest).
tx := newTxCounter(0, 0)
tx.setFailOnAnte(true)
txBytes, err := cdc.MarshalBinaryBare(tx)
txBytes, err := cdc.Marshal(tx)
require.NoError(t, err)
res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.Empty(t, res.Events)
Expand All @@ -1535,7 +1535,7 @@ func TestBaseAppAnteHandler(t *testing.T) {
tx = newTxCounter(0, 0)
tx.setFailOnHandler(true)

txBytes, err = cdc.MarshalBinaryBare(tx)
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand All @@ -1551,7 +1551,7 @@ func TestBaseAppAnteHandler(t *testing.T) {
// implicitly checked by previous tx executions
tx = newTxCounter(1, 0)

txBytes, err = cdc.MarshalBinaryBare(tx)
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand Down Expand Up @@ -1624,15 +1624,15 @@ func TestGasConsumptionBadTx(t *testing.T) {

tx := newTxCounter(5, 0)
tx.setFailOnAnte(true)
txBytes, err := cdc.MarshalBinaryBare(tx)
txBytes, err := cdc.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.False(t, res.IsOK(), fmt.Sprintf("%v", res))

// require next tx to fail due to black gas limit
tx = newTxCounter(5, 0)
txBytes, err = cdc.MarshalBinaryBare(tx)
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand Down Expand Up @@ -1992,7 +1992,7 @@ func TestWithRouter(t *testing.T) {
counter := int64(blockN*txPerHeight + i)
tx := newTxCounter(counter, counter)

txBytes, err := codec.MarshalBinaryBare(tx)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
Expand Down
4 changes: 2 additions & 2 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Context struct {
FromAddress sdk.AccAddress
Client rpcclient.Client
ChainID string
JSONMarshaler codec.JSONMarshaler
JSONMarshaler codec.JSONCodec
InterfaceRegistry codectypes.InterfaceRegistry
Input io.Reader
Keyring keyring.Keyring
Expand Down Expand Up @@ -73,7 +73,7 @@ func (ctx Context) WithInput(r io.Reader) Context {
}

// WithJSONMarshaler returns a copy of the Context with an updated JSONMarshaler.
func (ctx Context) WithJSONMarshaler(m codec.JSONMarshaler) Context {
func (ctx Context) WithJSONMarshaler(m codec.JSONCodec) Context {
ctx.JSONMarshaler = m
return ctx
}
Expand Down
28 changes: 14 additions & 14 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,69 +77,69 @@ func (cdc *LegacyAmino) jsonUnmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino})
}

func (cdc *LegacyAmino) MarshalBinaryBare(o interface{}) ([]byte, error) {
func (cdc *LegacyAmino) Marshal(o interface{}) ([]byte, error) {
err := cdc.marshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalBinaryBare(o)
}

func (cdc *LegacyAmino) MustMarshalBinaryBare(o interface{}) []byte {
bz, err := cdc.MarshalBinaryBare(o)
func (cdc *LegacyAmino) MustMarshal(o interface{}) []byte {
bz, err := cdc.Marshal(o)
if err != nil {
panic(err)
}
return bz
}

func (cdc *LegacyAmino) MarshalBinaryLengthPrefixed(o interface{}) ([]byte, error) {
func (cdc *LegacyAmino) MarshalLengthPrefixed(o interface{}) ([]byte, error) {
err := cdc.marshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalBinaryLengthPrefixed(o)
}

func (cdc *LegacyAmino) MustMarshalBinaryLengthPrefixed(o interface{}) []byte {
bz, err := cdc.MarshalBinaryLengthPrefixed(o)
func (cdc *LegacyAmino) MustMarshalLengthPrefixed(o interface{}) []byte {
bz, err := cdc.MarshalLengthPrefixed(o)
if err != nil {
panic(err)
}
return bz
}

func (cdc *LegacyAmino) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
func (cdc *LegacyAmino) Unmarshal(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalBinaryBare(bz, ptr)
if err != nil {
return err
}
return cdc.unmarshalAnys(ptr)
}

func (cdc *LegacyAmino) MustUnmarshalBinaryBare(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryBare(bz, ptr)
func (cdc *LegacyAmino) MustUnmarshal(bz []byte, ptr interface{}) {
err := cdc.Unmarshal(bz, ptr)
if err != nil {
panic(err)
}
}

func (cdc *LegacyAmino) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error {
func (cdc *LegacyAmino) UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
if err != nil {
return err
}
return cdc.unmarshalAnys(ptr)
}

func (cdc *LegacyAmino) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryLengthPrefixed(bz, ptr)
func (cdc *LegacyAmino) MustUnmarshalLengthPrefixed(bz []byte, ptr interface{}) {
err := cdc.UnmarshalLengthPrefixed(bz, ptr)
if err != nil {
panic(err)
}
}

// MarshalJSON implements codec.Marshaler interface
// MarshalJSON implements codec.Codec interface
func (cdc *LegacyAmino) MarshalJSON(o interface{}) ([]byte, error) {
err := cdc.jsonMarshalAnys(o)
if err != nil {
Expand All @@ -156,7 +156,7 @@ func (cdc *LegacyAmino) MustMarshalJSON(o interface{}) []byte {
return bz
}

// UnmarshalJSON implements codec.Marshaler interface
// UnmarshalJSON implements codec.Codec interface
func (cdc *LegacyAmino) UnmarshalJSON(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalJSON(bz, ptr)
if err != nil {
Expand Down
58 changes: 29 additions & 29 deletions codec/amino_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,51 @@ type AminoCodec struct {
*LegacyAmino
}

var _ Marshaler = &AminoCodec{}
var _ Codec = &AminoCodec{}

// NewAminoCodec returns a reference to a new AminoCodec
func NewAminoCodec(codec *LegacyAmino) *AminoCodec {
return &AminoCodec{LegacyAmino: codec}
}

// MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method.
func (ac *AminoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) {
return ac.LegacyAmino.MarshalBinaryBare(o)
// Marshal implements BinaryMarshaler.Marshal method.
func (ac *AminoCodec) Marshal(o ProtoMarshaler) ([]byte, error) {
return ac.LegacyAmino.Marshal(o)
}

// MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method.
func (ac *AminoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte {
return ac.LegacyAmino.MustMarshalBinaryBare(o)
// MustMarshal implements BinaryMarshaler.MustMarshal method.
func (ac *AminoCodec) MustMarshal(o ProtoMarshaler) []byte {
return ac.LegacyAmino.MustMarshal(o)
}

// MarshalBinaryLengthPrefixed implements BinaryMarshaler.MarshalBinaryLengthPrefixed method.
func (ac *AminoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
return ac.LegacyAmino.MarshalBinaryLengthPrefixed(o)
// MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.
func (ac *AminoCodec) MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
return ac.LegacyAmino.MarshalLengthPrefixed(o)
}

// MustMarshalBinaryLengthPrefixed implements BinaryMarshaler.MustMarshalBinaryLengthPrefixed method.
func (ac *AminoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
return ac.LegacyAmino.MustMarshalBinaryLengthPrefixed(o)
// MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method.
func (ac *AminoCodec) MustMarshalLengthPrefixed(o ProtoMarshaler) []byte {
return ac.LegacyAmino.MustMarshalLengthPrefixed(o)
}

// UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method.
func (ac *AminoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
return ac.LegacyAmino.UnmarshalBinaryBare(bz, ptr)
// Unmarshal implements BinaryMarshaler.Unmarshal method.
func (ac *AminoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error {
return ac.LegacyAmino.Unmarshal(bz, ptr)
}

// MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method.
func (ac *AminoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) {
ac.LegacyAmino.MustUnmarshalBinaryBare(bz, ptr)
// MustUnmarshal implements BinaryMarshaler.MustUnmarshal method.
func (ac *AminoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler) {
ac.LegacyAmino.MustUnmarshal(bz, ptr)
}

// UnmarshalBinaryLengthPrefixed implements BinaryMarshaler.UnmarshalBinaryLengthPrefixed method.
func (ac *AminoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
return ac.LegacyAmino.UnmarshalBinaryLengthPrefixed(bz, ptr)
// UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.
func (ac *AminoCodec) UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
return ac.LegacyAmino.UnmarshalLengthPrefixed(bz, ptr)
}

// MustUnmarshalBinaryLengthPrefixed implements BinaryMarshaler.MustUnmarshalBinaryLengthPrefixed method.
func (ac *AminoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
ac.LegacyAmino.MustUnmarshalBinaryLengthPrefixed(bz, ptr)
// MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method.
func (ac *AminoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
ac.LegacyAmino.MustUnmarshalLengthPrefixed(bz, ptr)
}

// MarshalJSON implements JSONMarshaler.MarshalJSON method,
Expand Down Expand Up @@ -83,23 +83,23 @@ func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) {

// MarshalInterface is a convenience function for amino marshaling interfaces.
// The `i` must be an interface.
// NOTE: to marshal a concrete type, you should use MarshalBinaryBare instead
// NOTE: to marshal a concrete type, you should use Marshal instead
func (ac *AminoCodec) MarshalInterface(i proto.Message) ([]byte, error) {
if err := assertNotNil(i); err != nil {
return nil, err
}
return ac.LegacyAmino.MarshalBinaryBare(i)
return ac.LegacyAmino.Marshal(i)
}

// UnmarshalInterface is a convenience function for amino unmarshaling interfaces.
// `ptr` must be a pointer to an interface.
// NOTE: to unmarshal a concrete type, you should use UnmarshalBinaryBare instead
// NOTE: to unmarshal a concrete type, you should use Unmarshal instead
//
// Example:
// var x MyInterface
// err := cdc.UnmarshalInterface(bz, &x)
func (ac *AminoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error {
return ac.LegacyAmino.UnmarshalBinaryBare(bz, ptr)
return ac.LegacyAmino.Unmarshal(bz, ptr)
}

// MarshalInterfaceJSON is a convenience function for amino marshaling interfaces.
Expand Down
4 changes: 2 additions & 2 deletions codec/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestMarshalProtoPubKey(t *testing.T) {

// **** test binary serialization ****

bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny)
bz, err = ccfg.Marshaler.Marshal(pkAny)
require.NoError(err)

var pkAny3 codectypes.Any
err = ccfg.Marshaler.UnmarshalBinaryBare(bz, &pkAny3)
err = ccfg.Marshaler.Unmarshal(bz, &pkAny3)
require.NoError(err)
err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI)
require.NoError(err)
Expand Down
Loading

0 comments on commit be4a965

Please sign in to comment.