Skip to content

Commit

Permalink
Merge PR cosmos#6076: Proto Any init + evidence
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored May 5, 2020
1 parent 7e72e5b commit 3862172
Show file tree
Hide file tree
Showing 82 changed files with 2,158 additions and 1,317 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ proto-all: proto-tools proto-gen proto-lint proto-check-breaking
proto-gen:
@./scripts/protocgen.sh

# This generates the SDK's custom wrapper for google.protobuf.Any. It should only be run manually when needed
proto-gen-any:
@./scripts/protocgen-any.sh

proto-lint:
@buf check lint --error-format=json

Expand Down
2 changes: 2 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
build:
roots:
- .
excludes:
- third_party/proto/google
lint:
use:
- DEFAULT
Expand Down
10 changes: 10 additions & 0 deletions codec/amino_codec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package codec

import (
"fmt"

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

// AminoCodec defines a codec that utilizes Amino for both binary and JSON
// encoding.
type AminoCodec struct {
Expand Down Expand Up @@ -57,3 +63,7 @@ func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr interface{}) error {
func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
ac.amino.MustUnmarshalJSON(bz, ptr)
}

func (*AminoCodec) UnpackAny(*types.Any, interface{}) error {
return fmt.Errorf("AminoCodec can't handle unpack protobuf Any's")
}
3 changes: 3 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/binary"
"io"

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

"github.com/gogo/protobuf/proto"
)

Expand Down Expand Up @@ -31,6 +33,7 @@ type (
MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)

JSONMarshaler
types.AnyUnpacker
}

JSONMarshaler interface {
Expand Down
10 changes: 8 additions & 2 deletions codec/hybrid_codec.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package codec

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

// HybridCodec defines a codec that utilizes Protobuf for binary encoding
// and Amino for JSON encoding.
type HybridCodec struct {
proto Marshaler
amino Marshaler
}

func NewHybridCodec(amino *Codec) Marshaler {
func NewHybridCodec(amino *Codec, unpacker types.AnyUnpacker) Marshaler {
return &HybridCodec{
proto: NewProtoCodec(),
proto: NewProtoCodec(unpacker),
amino: NewAminoCodec(amino),
}
}
Expand Down Expand Up @@ -61,3 +63,7 @@ func (hc *HybridCodec) UnmarshalJSON(bz []byte, ptr interface{}) error {
func (hc *HybridCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
hc.amino.MustUnmarshalJSON(bz, ptr)
}

func (hc *HybridCodec) UnpackAny(any *types.Any, iface interface{}) error {
return hc.proto.UnpackAny(any, iface)
}
4 changes: 2 additions & 2 deletions codec/hybrid_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestHybridCodec(t *testing.T) {
}{
{
"valid encoding and decoding",
codec.NewHybridCodec(createTestCodec()),
codec.NewHybridCodec(createTestCodec(), createTestInterfaceRegistry()),
&testdata.Dog{Name: "rufus"},
&testdata.Dog{},
false,
false,
},
{
"invalid decode type",
codec.NewHybridCodec(createTestCodec()),
codec.NewHybridCodec(createTestCodec(), createTestInterfaceRegistry()),
&testdata.Dog{Name: "rufus"},
&testdata.Cat{},
false,
Expand Down
26 changes: 21 additions & 5 deletions codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import (
"fmt"
"strings"

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

"github.com/gogo/protobuf/jsonpb"
)

// ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON
// encoding.
type ProtoCodec struct{}
type ProtoCodec struct {
anyUnpacker types.AnyUnpacker
}

func NewProtoCodec() Marshaler {
return &ProtoCodec{}
func NewProtoCodec(anyUnpacker types.AnyUnpacker) Marshaler {
return &ProtoCodec{anyUnpacker: anyUnpacker}
}

func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) {
Expand Down Expand Up @@ -58,7 +62,15 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
}

func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
return ptr.Unmarshal(bz)
err := ptr.Unmarshal(bz)
if err != nil {
return err
}
err = types.UnpackInterfaces(ptr, pc.anyUnpacker)
if err != nil {
return err
}
return nil
}

func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) {
Expand All @@ -80,7 +92,7 @@ func (pc *ProtoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshale
}

bz = bz[n:]
return ptr.Unmarshal(bz)
return pc.UnmarshalBinaryBare(bz, ptr)
}

func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
Expand Down Expand Up @@ -121,3 +133,7 @@ func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
panic(err)
}
}

func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error {
return pc.anyUnpacker.UnpackAny(any, iface)
}
16 changes: 14 additions & 2 deletions codec/proto_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ import (

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

func createTestInterfaceRegistry() types.InterfaceRegistry {
interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry.RegisterInterface("testdata.Animal",
(*testdata.Animal)(nil),
&testdata.Dog{},
&testdata.Cat{},
)

return interfaceRegistry
}

func TestProtoCodec(t *testing.T) {
testCases := []struct {
name string
Expand All @@ -20,15 +32,15 @@ func TestProtoCodec(t *testing.T) {
}{
{
"valid encoding and decoding",
codec.NewProtoCodec(),
codec.NewProtoCodec(createTestInterfaceRegistry()),
&testdata.Dog{Name: "rufus"},
&testdata.Dog{},
false,
false,
},
{
"invalid decode type",
codec.NewProtoCodec(),
codec.NewProtoCodec(createTestInterfaceRegistry()),
&testdata.Dog{Name: "rufus"},
&testdata.Cat{},
false,
Expand Down
9 changes: 9 additions & 0 deletions codec/testdata/animal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package testdata

import (
"fmt"

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

type Animal interface {
Expand All @@ -18,3 +20,10 @@ func (c Cat) Greet() string {
func (d Dog) Greet() string {
return fmt.Sprintf("Roof, my name is %s", d.Name)
}

var _ types.UnpackInterfacesMessage = HasAnimal{}

func (m HasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal Animal
return unpacker.UnpackAny(m.Animal, &animal)
}
Loading

0 comments on commit 3862172

Please sign in to comment.