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 and update codec.MarshalAny functions to codec.Marshaler interface #8080

Merged
merged 22 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change order of args in UnmarshalInterface to a canonical one
  • Loading branch information
robert-zaremba committed Dec 4, 2020
commit 54b1de38a2fb5bae92f2a7c6d4007fdb4287b422
20 changes: 6 additions & 14 deletions codec/amino_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codec

import (
"errors"
"fmt"

"github.com/gogo/protobuf/proto"
)
Expand Down Expand Up @@ -95,19 +94,12 @@ func (ac *AminoCodec) MarshalInterface(i interface{}) ([]byte, error) {
}

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

// TODO remove
func assertProtoMarshaler(i interface{}) error {
return nil
// TODO: check this
_, ok := i.(ProtoMarshaler)
if !ok {
return fmt.Errorf("can't amino marshal %T; expecting ProtoMarshaler", i)
}
return nil
}
8 changes: 4 additions & 4 deletions codec/amino_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func TestAminoMarsharlInterface(t *testing.T) {
require.NoError(err)
// require.EqualError(err, "can't proto marshal testdata.Dog; expecting ProtoMarshaler")
require.PanicsWithValue("Unmarshal expects a pointer", func() {
cdc.UnmarshalInterface(animal, bz)
cdc.UnmarshalInterface(bz, animal)
})
var dog2 testdata.Dog
require.NoError(cdc.UnmarshalInterface(&dog2, bz))
require.NoError(cdc.UnmarshalInterface(bz, &dog2))
require.Equal(dog, dog2)
require.NoError(cdc.UnmarshalInterface(&animal, bz))
require.NoError(cdc.UnmarshalInterface(bz, &animal))
require.Equal(dog, animal)
var cat testdata.Cat
require.Error(cdc.UnmarshalInterface(&cat, bz))
require.Error(cdc.UnmarshalInterface(bz, &cat))
}

func TestAminoCodec(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions codec/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ func TestMarshalAny(t *testing.T) {
var animal testdata.Animal

// empty registry should fail
err = cdc.UnmarshalInterface(&animal, bz)
err = cdc.UnmarshalInterface(bz, &animal)
require.Error(t, err)

// wrong type registration should fail
registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{})
err = cdc.UnmarshalInterface(&animal, bz)
err = cdc.UnmarshalInterface(bz, &animal)
require.Error(t, err)

// should pass
registry = NewTestInterfaceRegistry()
cdc = codec.NewProtoCodec(registry)
err = cdc.UnmarshalInterface(&animal, bz)
err = cdc.UnmarshalInterface(bz, &animal)
require.NoError(t, err)
require.Equal(t, kitty, animal)

// nil should fail
registry = NewTestInterfaceRegistry()
err = cdc.UnmarshalInterface(nil, bz)
err = cdc.UnmarshalInterface(bz, nil)
require.Error(t, err)
}

Expand Down
8 changes: 4 additions & 4 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ type (
UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)

MarshalInterface(x interface{}) ([]byte, error)
UnmarshalInterface(x interface{}, bz []byte) error
MarshalInterface(i interface{}) ([]byte, error)
UnmarshalInterface(bz []byte, ptr interface{}) error

types.AnyUnpacker
}

JSONMarshaler interface {
MarshalJSON(o proto.Message) ([]byte, error)
MustMarshalJSON(o proto.Message) []byte
// MarshalInterfaceJSON(x interface{}) ([]byte, error)
// UnmarshalInterfaceJSON(x interface{}, bz []byte) error
// MarshalInterfaceJSON(i interface{}) ([]byte, error)
// UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error

UnmarshalJSON(bz []byte, ptr proto.Message) error
MustUnmarshalJSON(bz []byte, ptr proto.Message)
Expand Down
22 changes: 11 additions & 11 deletions codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) {
// MarshalInterface is a convenience function for proto marshalling interfaces. It
// packs the provided value, which must implement proto.Message,
// in an Any and then marshals it to bytes.
// NOTE: if you use a concret type, then you should use MarshalBinaryBare instead
// NOTE: if you use a concrete type, then you should use MarshalBinaryBare instead
// TODO: check if we can use proto.Message here
func (pc *ProtoCodec) MarshalInterface(i interface{}) ([]byte, error) {
msg, ok := i.(proto.Message)
Expand All @@ -183,10 +183,10 @@ func (pc *ProtoCodec) MarshalInterface(i interface{}) ([]byte, error) {
// be a pointer to a non empty interface with registered implementations.
// NOTE: if you use a concert type, then you should use UnarshalBinaryBare instead
//
// Ex:
// var x MyInterface
// err := UnmarshalInterface(unpacker, &x, bz)
func (pc *ProtoCodec) UnmarshalInterface(ptr interface{}, bz []byte) error {
// Example:
// var x MyInterface
// err := UnmarshalInterface(bz, &x, bz)
func (pc *ProtoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error {
any := &types.Any{}
err := pc.UnmarshalBinaryBare(bz, any)
if err != nil {
Expand All @@ -198,7 +198,7 @@ func (pc *ProtoCodec) UnmarshalInterface(ptr interface{}, bz []byte) error {

// MarshalInterfaceJSON is a convenience function for proto marshalling interfaces. It
// packs the provided value in an Any and then marshals it to bytes.
// NOTE: if you use a concert type, then you should use JSONMarshaler.MarshalJSON instead
// NOTE: if you use a conceret type, then you should use JSONMarshaler.MarshalJSON instead
func (pc *ProtoCodec) MarshalInterfaceJSON(x proto.Message) ([]byte, error) {
any, err := types.NewAnyWithValue(x)
if err != nil {
Expand All @@ -210,12 +210,12 @@ func (pc *ProtoCodec) MarshalInterfaceJSON(x proto.Message) ([]byte, error) {
// UnmarshalInterfaceJSON is a convenience function for proto unmarshaling interfaces.
// It unmarshals an Any from bz bytes and then unpacks it to the `iface`, which must
// be a pointer to a non empty interface, implementing proto.Message with registered implementations.
// NOTE: if you use a concert type, then you should use JSONMarshaler.UnarshalJSON instead
// NOTE: if you use a conceret type, then you should use JSONMarshaler.UnarshalJSON instead
//
// Ex:
// var x MyInterface // must implement proto.Message
// err := UnmarshalAny(unpacker, &x, bz)
func (pc *ProtoCodec) UnmarshalInterfaceJSON(iface interface{}, bz []byte) error {
// Example:
// var x MyInterface // must implement proto.Message
// err := UnmarshalAny(unpacker, &x, bz)
func (pc *ProtoCodec) UnmarshalInterfaceJSON(bz []byte, iface interface{}) error {
any := &types.Any{}
err := pc.UnmarshalJSON(bz, any)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/adr-019-protobuf-state-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func MarshalEvidence(cdc codec.BinaryMarshaler, e eviexported.Evidence) ([]byte,

func UnmarshalEvidence(cdc codec.BinaryMarshaler, bz []byte) (eviexported.Evidence, error) {
var evi eviexported.Evidence
err := cdc.UnmarshalInterface(c.interfaceContext, &evi, bz)
err := cdc.UnmarshalInterface(&evi, bz)
return err, nil
}
```
Expand Down
2 changes: 1 addition & 1 deletion x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (ak AccountKeeper) MarshalAccount(accountI types.AccountI) ([]byte, error)
// bytes of a Proto-based Account type
func (ak AccountKeeper) UnmarshalAccount(bz []byte) (types.AccountI, error) {
var acc types.AccountI
return acc, ak.cdc.UnmarshalInterface(&acc, bz)
return acc, ak.cdc.UnmarshalInterface(bz, &acc)
}

// GetCodec return codec.Marshaler object used by the keeper
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,5 @@ func (k BaseKeeper) MarshalSupply(supplyI exported.SupplyI) ([]byte, error) {
// bytes of a Proto-based Supply type
func (k BaseKeeper) UnmarshalSupply(bz []byte) (exported.SupplyI, error) {
var evi exported.SupplyI
return evi, k.cdc.UnmarshalInterface(&evi, bz)
return evi, k.cdc.UnmarshalInterface(bz, &evi)
}
2 changes: 1 addition & 1 deletion x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ func (k Keeper) MarshalEvidence(evidenceI exported.Evidence) ([]byte, error) {
// bytes of a Proto-based Evidence type
func (k Keeper) UnmarshalEvidence(bz []byte) (exported.Evidence, error) {
var evi exported.Evidence
return evi, k.cdc.UnmarshalInterface(&evi, bz)
return evi, k.cdc.UnmarshalInterface(bz, &evi)
}
4 changes: 2 additions & 2 deletions x/ibc/core/02-client/types/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func MarshalClientState(cdc codec.BinaryMarshaler, clientStateI exported.ClientS
// failure.
func UnmarshalClientState(cdc codec.BinaryMarshaler, bz []byte) (exported.ClientState, error) {
var clientState exported.ClientState
if err := cdc.UnmarshalInterface(&clientState, bz); err != nil {
if err := cdc.UnmarshalInterface(bz, &clientState); err != nil {
return nil, err
}

Expand Down Expand Up @@ -78,7 +78,7 @@ func MarshalConsensusState(cdc codec.BinaryMarshaler, cs exported.ConsensusState
// failure.
func UnmarshalConsensusState(cdc codec.BinaryMarshaler, bz []byte) (exported.ConsensusState, error) {
var consensusState exported.ConsensusState
if err := cdc.UnmarshalInterface(&consensusState, bz); err != nil {
if err := cdc.UnmarshalInterface(bz, &consensusState); err != nil {
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (suite *SoloMachineTestSuite) GetSequenceFromStore() uint64 {
suite.Require().NotNil(bz)

var clientState exported.ClientState
err := suite.chainA.Codec.UnmarshalInterface(&clientState, bz)
err := suite.chainA.Codec.UnmarshalInterface(bz, &clientState)
suite.Require().NoError(err)
return clientState.GetLatestHeight().GetRevisionHeight()
}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.P
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
}
var pk cryptotypes.PubKey
return pk, k.cdc.UnmarshalInterface(&pk, bz)
return pk, k.cdc.UnmarshalInterface(bz, &pk)
}

// Slash attempts to slash a validator. The slash is delegated to the staking
Expand Down
4 changes: 2 additions & 2 deletions x/staking/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMsgDecode(t *testing.T) {
pk1bz, err := cdc.MarshalInterface(pk1)
require.NoError(t, err)
var pkUnmarshaled cryptotypes.PubKey
err = cdc.UnmarshalInterface(&pkUnmarshaled, pk1bz)
err = cdc.UnmarshalInterface(pk1bz, &pkUnmarshaled)
require.NoError(t, err)
require.True(t, pk1.Equals(pkUnmarshaled.(*ed25519.PubKey)))

Expand All @@ -43,7 +43,7 @@ func TestMsgDecode(t *testing.T) {
require.NoError(t, err)

var msgUnmarshaled sdk.Msg
err = cdc.UnmarshalInterface(&msgUnmarshaled, msgSerialized)
err = cdc.UnmarshalInterface(msgSerialized, &msgUnmarshaled)
require.NoError(t, err)
msg2, ok := msgUnmarshaled.(*types.MsgCreateValidator)
require.True(t, ok)
Expand Down