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

Update x/gov to use Any #6147

Merged
merged 52 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ce24456
Update x/gov to use Any
aaronc May 5, 2020
df49ab4
Fixes
aaronc May 5, 2020
51240fa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 6, 2020
3e5a787
Remove MsgSubmitProposalLegacy
aaronc May 6, 2020
5264182
Update CHANGELOG.md
aaronc May 6, 2020
b2ca3a4
Add RegisterInterfaces for x/distribution, x/params, & x/upgrade
aaronc May 6, 2020
8dcfffb
Fix query JSON issue
aaronc May 7, 2020
42092cf
Fix gov tests
aaronc May 8, 2020
8ea6a44
Revert custom Any Equals
aaronc May 8, 2020
06056b3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 8, 2020
7531d14
Re-remove types
aaronc May 8, 2020
ba6b151
Rename receivers
aaronc May 8, 2020
eab723e
Fix imports in gov
sahith-narahari May 11, 2020
3777926
Sort imports
sahith-narahari May 11, 2020
426c04f
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
sahith-narahari May 11, 2020
bf05676
Make amino JSON signing work with Any
aaronc May 14, 2020
a4a35df
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 14, 2020
519e1b9
Run proto-gen
aaronc May 14, 2020
84b4f63
Create full amino wrapper
aaronc May 14, 2020
5b2700c
Fix errors
aaronc May 14, 2020
06c2d03
Fixes
aaronc May 14, 2020
ad482e4
Fix tests
aaronc May 14, 2020
0ff09a4
Test fixes
aaronc May 14, 2020
7b632fa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 14, 2020
33b04ff
Fix tests
aaronc May 14, 2020
72a89a1
Linting
aaronc May 14, 2020
f6fa2ba
Update ADR 019 and CHANGELOG
aaronc May 15, 2020
f3dbd90
Updated ADR 019
aaronc May 15, 2020
8a53536
Extract Marshal/UnmarshalProposal
aaronc May 15, 2020
5d63dd6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 15, 2020
e5c90d2
fix error
aaronc May 15, 2020
ccf589a
lint
aaronc May 15, 2020
ba93a7e
linting
aaronc May 15, 2020
065f051
linting
aaronc May 15, 2020
3915704
Update client/keys/parse.go
aaronc May 15, 2020
83bfba7
linting
aaronc May 15, 2020
4836ffb
Merge branch 'master' into aaronc/x-gov-proto-any
alexanderbez May 16, 2020
fa169dc
Update docs/architecture/adr-019-protobuf-state-encoding.md
aaronc May 18, 2020
50d4b36
Update docs/architecture/adr-019-protobuf-state-encoding.md
aaronc May 18, 2020
34141d8
Address review feedback
aaronc May 18, 2020
63561a8
Add godocs
aaronc May 18, 2020
600ac6a
Merge branch 'master' into aaronc/x-gov-proto-any
aaronc May 18, 2020
22d8af7
Fix errors
aaronc May 18, 2020
5d289e1
Merge remote-tracking branch 'origin/aaronc/x-gov-proto-any' into aar…
aaronc May 18, 2020
a005332
fix errors
aaronc May 18, 2020
c1bad36
revert file
aaronc May 18, 2020
b58c42a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into aaronc/x-g…
aaronc May 19, 2020
c09e298
Address review feedback
aaronc May 19, 2020
0e159e7
Address review feedback
aaronc May 19, 2020
5ed861d
Stacktrace debug flag
aaronc May 19, 2020
2e14d48
Fix tests
aaronc May 19, 2020
72ead83
Address review feedback
aaronc May 19, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ information on how to implement the new `Keyring` interface.
* [\#5858](https://github.com/cosmos/cosmos-sdk/pull/5858) Make Keyring store keys by name and address's hexbytes representation.
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Remove APIs for getting and setting `x/evidence` parameters. `BaseApp` now uses a `ParamStore` to manage Tendermint consensus parameters which is managed via the `x/params` `Substore` type.
* (export) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) `AppExporter` now returns ABCI consensus parameters to be included in marshaled exported state. These parameters must be returned from the application via the `BaseApp`.
* (codec) `*codec.Codec` is now a wrapper around Amino which provides backwards compatibility with protobuf `Any`.
ALL legacy code should use `*codec.Codec` instead of `*amino.Codec` directly
* (x/gov) [\#6147](https://github.com/cosmos/cosmos-sdk/pull/6147) The `Content` field on `Proposal` and `MsgSubmitProposal`
is now `Any` in concordance with [ADR 019](docs/architecture/adr-019-protobuf-state-encoding.md) and `GetContent` should now
be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposal` constructor now may return an `error`

### Features

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test-race:
@VERSION=$(VERSION) go test -mod=readonly -race $(PACKAGES_NOSIMULATION)

test-integration: build-sim
BUILDDIR=$(BUILDDIR) go test -mod=readonly -p 4 -tags=-tags='ledger test_ledger_mock cli_test' -run ^TestCLI `go list ./.../cli/...`
BUILDDIR=$(BUILDDIR) go test -mod=readonly -p 4 -tags='ledger test_ledger_mock cli_test' -run ^TestCLI `go list ./.../cli/...`

.PHONY: test test-all test-ledger-mock test-ledger test-unit test-race

Expand Down
158 changes: 149 additions & 9 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
amino "github.com/tendermint/go-amino"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
tmtypes "github.com/tendermint/tendermint/types"

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

// Cdc defines a global generic sealed Amino codec to be used throughout sdk. It
Expand All @@ -17,29 +19,38 @@ import (
var Cdc *Codec

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

// Codec defines a wrapper for an Amino codec that properly handles protobuf
// types with Any's
type Codec struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this be deprecated in the future in favor of AminoCodec? are there any difference between the 2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ADR updates in this PR I noted that we would rename this to LegacyAmino. It is more or less deprecated. The difference between the two is that Marshaler expects types that implement ProtoMarshaler (i.e. generated by protoc). So any code using Marshaler is more or less ensured to be proto compatible. This legacy Codec is pure amino.

Amino *amino.Codec
}

// Codec defines a type alias for an Amino codec.
type Codec = amino.Codec
var _ JSONMarshaler = &Codec{}

func (cdc *Codec) Seal() {
cdc.Amino.Seal()
aaronc marked this conversation as resolved.
Show resolved Hide resolved
}

func New() *Codec {
return amino.NewCodec()
return &Codec{amino.NewCodec()}
}

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

// RegisterEvidences registers Tendermint evidence types with the provided Amino
// codec.
func RegisterEvidences(cdc *Codec) {
tmtypes.RegisterEvidences(cdc)
tmtypes.RegisterEvidences(cdc.Amino)
}

// MarshalJSONIndent provides a utility for indented JSON encoding of an object
Expand Down Expand Up @@ -68,3 +79,132 @@ func MustMarshalJSONIndent(m JSONMarshaler, obj interface{}) []byte {

return bz
}

func (cdc *Codec) marshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoPacker{Cdc: cdc.Amino})
}

func (cdc *Codec) unmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: cdc.Amino})
}

func (cdc *Codec) jsonMarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: cdc.Amino})
}

func (cdc *Codec) jsonUnmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino})
}

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

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

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

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

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

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

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

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

func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error) {
err := cdc.jsonMarshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalJSON(o)
}

func (cdc *Codec) MustMarshalJSON(o interface{}) []byte {
bz, err := cdc.MarshalJSON(o)
if err != nil {
panic(err)
}
return bz
}

func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalJSON(bz, ptr)
if err != nil {
return err
}
return cdc.jsonUnmarshalAnys(ptr)
}

func (cdc *Codec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
err := cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}

func (*Codec) UnpackAny(*types.Any, interface{}) error {
return fmt.Errorf("AminoCodec can't handle unpack protobuf Any's")
}

func (cdc *Codec) RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) {
cdc.Amino.RegisterInterface(ptr, iopts)
}

func (cdc *Codec) RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) {
cdc.Amino.RegisterConcrete(o, name, copts)
}

func (cdc *Codec) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
err := cdc.jsonMarshalAnys(o)
if err != nil {
panic(err)
}
return cdc.Amino.MarshalJSONIndent(o, prefix, indent)
}
114 changes: 13 additions & 101 deletions codec/amino_codec.go
Original file line number Diff line number Diff line change
@@ -1,133 +1,45 @@
package codec

import (
"fmt"

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

// AminoCodec defines a codec that utilizes Amino for both binary and JSON
// AminoCodec defines a codec that utilizes Codec for both binary and JSON
// encoding.
type AminoCodec struct {
amino *Codec
}

func NewAminoCodec(amino *Codec) Marshaler {
return &AminoCodec{amino}
}

func (ac *AminoCodec) marshalAnys(o ProtoMarshaler) error {
return types.UnpackInterfaces(o, types.AminoPacker{Cdc: ac.amino})
*Codec
}

func (ac *AminoCodec) unmarshalAnys(o ProtoMarshaler) error {
return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: ac.amino})
}
var _ Marshaler = &AminoCodec{}

func (ac *AminoCodec) jsonMarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: ac.amino})
}

func (ac *AminoCodec) jsonUnmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: ac.amino})
func NewAminoCodec(codec *Codec) *AminoCodec {
return &AminoCodec{Codec: codec}
}

func (ac *AminoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) {
err := ac.marshalAnys(o)
if err != nil {
return nil, err
}
return ac.amino.MarshalBinaryBare(o)
return ac.Codec.MarshalBinaryBare(o)
}

func (ac *AminoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte {
err := ac.marshalAnys(o)
if err != nil {
panic(err)
}
return ac.amino.MustMarshalBinaryBare(o)
return ac.Codec.MustMarshalBinaryBare(o)
}

func (ac *AminoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error) {
err := ac.marshalAnys(o)
if err != nil {
return nil, err
}
return ac.amino.MarshalBinaryLengthPrefixed(o)
return ac.Codec.MarshalBinaryLengthPrefixed(o)
}

func (ac *AminoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte {
err := ac.marshalAnys(o)
if err != nil {
panic(err)
}
return ac.amino.MustMarshalBinaryLengthPrefixed(o)
return ac.Codec.MustMarshalBinaryLengthPrefixed(o)
}

func (ac *AminoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error {
err := ac.amino.UnmarshalBinaryBare(bz, ptr)
if err != nil {
return err
}
return ac.unmarshalAnys(ptr)
return ac.Codec.UnmarshalBinaryBare(bz, ptr)
}

func (ac *AminoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) {
ac.amino.MustUnmarshalBinaryBare(bz, ptr)
err := ac.unmarshalAnys(ptr)
if err != nil {
panic(err)
}
ac.Codec.MustUnmarshalBinaryBare(bz, ptr)
}

func (ac *AminoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error {
err := ac.amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
if err != nil {
return err
}
return ac.unmarshalAnys(ptr)
return ac.Codec.UnmarshalBinaryLengthPrefixed(bz, ptr)
}

func (ac *AminoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) {
ac.amino.MustUnmarshalBinaryLengthPrefixed(bz, ptr)
err := ac.unmarshalAnys(ptr)
if err != nil {
panic(err)
}
}

func (ac *AminoCodec) MarshalJSON(o interface{}) ([]byte, error) {
err := ac.jsonMarshalAnys(o)
if err != nil {
return nil, err
}
return ac.amino.MarshalJSON(o)
}

func (ac *AminoCodec) MustMarshalJSON(o interface{}) []byte {
err := ac.jsonMarshalAnys(o)
if err != nil {
panic(err)
}
return ac.amino.MustMarshalJSON(o)
}

func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr interface{}) error {
err := ac.amino.UnmarshalJSON(bz, ptr)
if err != nil {
return err
}
return ac.jsonUnmarshalAnys(ptr)
}

func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
ac.amino.MustUnmarshalJSON(bz, ptr)
err := ac.jsonUnmarshalAnys(ptr)
if err != nil {
panic(err)
}
}

func (*AminoCodec) UnpackAny(*types.Any, interface{}) error {
return fmt.Errorf("AminoCodec can't handle unpack protobuf Any's")
ac.Codec.MustUnmarshalBinaryLengthPrefixed(bz, ptr)
}
5 changes: 2 additions & 3 deletions codec/amino_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"

"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"

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

func createTestCodec() *amino.Codec {
cdc := amino.NewCodec()
func createTestCodec() *codec.Codec {
cdc := codec.New()

cdc.RegisterInterface((*testdata.Animal)(nil), nil)
cdc.RegisterConcrete(testdata.Dog{}, "testdata/Dog", nil)
Expand Down
Loading