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

Proto Any init + evidence #6076

Merged
merged 52 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
f8ddff6
Convert evidence state proto to use Any for benchmarking
aaronc Apr 23, 2020
62efb2e
Revert changes for benchmarking
aaronc Apr 23, 2020
59eec83
Add safe Any implementation
aaronc Apr 24, 2020
27ba717
Have Any Pack/Unpack working
aaronc Apr 25, 2020
e21f8a2
Add Any value cache
aaronc Apr 25, 2020
f1b5f18
Implement Any Rehydrate
aaronc Apr 25, 2020
9e92ca9
WIP on UnpackInterfaces
aaronc Apr 26, 2020
ef82e23
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/pro…
aaronc Apr 29, 2020
82a7a8a
WIP on ADR 019 Any init
aaronc Apr 29, 2020
08a02d3
Merge branch 'master' into aaronc/proto-any-init
aaronc Apr 29, 2020
d9ef343
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/pro…
aaronc Apr 29, 2020
a3ee74b
WIP on fixing tests
aaronc Apr 29, 2020
22828ad
Fix buf.yaml
aaronc Apr 29, 2020
86d42c1
WIP on test fixes
aaronc Apr 29, 2020
09051fc
Remove duplicate Any type registration
aaronc Apr 29, 2020
a92a8e9
reorganize, nolint
aaronc Apr 29, 2020
3ef37a0
Fix tests, reorganization
aaronc Apr 30, 2020
57c9842
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/pro…
aaronc Apr 30, 2020
bf2491d
Doc and linting updates
aaronc Apr 30, 2020
1e174a3
Doc and linting updates
aaronc Apr 30, 2020
3c0bfaf
Test fix
aaronc Apr 30, 2020
3835634
wsl
aaronc Apr 30, 2020
83e1be2
Improve tests
aaronc Apr 30, 2020
3fa3d97
Improve tests
aaronc Apr 30, 2020
68ee2c8
Merge branch 'master' into aaronc/proto-any-init
Apr 30, 2020
7ef3a22
Update scripts/protocgen-any.sh
aaronc May 1, 2020
eeb871d
Merge branch 'master' into aaronc/proto-any-init
aaronc May 1, 2020
2ad95ff
Refactor evidence Codec from std to x/evidence
aaronc May 1, 2020
d7d5506
Merge branch 'aaronc/proto-any-init' of github.com:cosmos/cosmos-sdk …
aaronc May 1, 2020
8fe1ad2
Address review feedback
aaronc May 1, 2020
ef0c11d
Fix bug
aaronc May 1, 2020
84560f1
Address review feedback
aaronc May 1, 2020
99c0034
fix tests
aaronc May 1, 2020
4f24c55
Remove GetCodec from evidence Keeper
aaronc May 1, 2020
1c93275
Change evidence NewKeeper to just take an evidence Codec
aaronc May 1, 2020
b5a95a8
Make NewAnyWithValue require a proto.Message
aaronc May 1, 2020
de52d69
Fix error
aaronc May 1, 2020
3ada5b1
Update codec/types/any.go
aaronc May 1, 2020
9015a45
Update codec/types/interface_registry.go
aaronc May 1, 2020
6306127
Update x/evidence/types/codec.go
aaronc May 1, 2020
96cbff7
Update x/evidence/types/codec.go
aaronc May 1, 2020
ba19f15
Update x/evidence/types/codec.go
aaronc May 1, 2020
be25032
Update codec/types/any.go
aaronc May 1, 2020
2293862
Merge branch 'master' into aaronc/proto-any-init
alexanderbez May 1, 2020
0d050d8
fix tests
aaronc May 1, 2020
cd60b8f
Merge branch 'master' into aaronc/proto-any-init
alexanderbez May 2, 2020
a42a387
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/pro…
aaronc May 4, 2020
b1a5d8e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/pro…
aaronc May 4, 2020
b167e09
Merge branch 'master' into aaronc/proto-any-init
aaronc May 4, 2020
49e0538
Merge branch 'master' into aaronc/proto-any-init
alexanderbez May 4, 2020
3a5931c
Merge branch 'master' into aaronc/proto-any-init
aaronc May 5, 2020
70eba6f
Merge branch 'master' into aaronc/proto-any-init
alexanderbez May 5, 2020
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,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