-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Update x/gov to use Any #6147
Changes from 46 commits
ce24456
df49ab4
51240fa
3e5a787
5264182
b2ca3a4
8dcfffb
42092cf
8ea6a44
06056b3
7531d14
ba6b151
eab723e
3777926
426c04f
bf05676
a4a35df
519e1b9
84b4f63
5b2700c
06c2d03
ad482e4
0ff09a4
7b632fa
33b04ff
72a89a1
f6fa2ba
f3dbd90
8a53536
5d63dd6
e5c90d2
ccf589a
ba93a7e
065f051
3915704
83bfba7
4836ffb
fa169dc
50d4b36
34141d8
63561a8
600ac6a
22d8af7
5d289e1
a005332
c1bad36
b58c42a
c09e298
0e159e7
5ed861d
2e14d48
72ead83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -23,23 +25,33 @@ func init() { | |
Cdc = cdc.Seal() | ||
} | ||
|
||
// Codec defines a type alias for an Amino codec. | ||
type Codec = amino.Codec | ||
// Codec defines a wrapper for an Amino codec that properly handles protobuf | ||
// types with Any's | ||
type Codec struct { | ||
Amino *amino.Codec | ||
} | ||
|
||
var _ JSONMarshaler = &Codec{} | ||
|
||
func (cdc *Codec) Seal() *Codec { | ||
cdc.Amino.Seal() | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return cdc | ||
} | ||
|
||
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 | ||
|
@@ -68,3 +80,135 @@ 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 { | ||
err := cdc.marshalAnys(o) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it would be easier to read and maintain the code if you would just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I think I understand what you mean and addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ups, should have been "call |
||
if err != nil { | ||
panic(err) | ||
} | ||
return cdc.Amino.MustMarshalBinaryBare(o) | ||
} | ||
|
||
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 { | ||
err := cdc.marshalAnys(o) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return cdc.Amino.MustMarshalBinaryLengthPrefixed(o) | ||
} | ||
|
||
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{}) { | ||
cdc.Amino.MustUnmarshalBinaryBare(bz, ptr) | ||
err := cdc.unmarshalAnys(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{}) { | ||
cdc.Amino.MustUnmarshalBinaryLengthPrefixed(bz, ptr) | ||
err := cdc.unmarshalAnys(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 { | ||
err := cdc.jsonMarshalAnys(o) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return cdc.Amino.MustMarshalJSON(o) | ||
} | ||
|
||
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{}) { | ||
cdc.Amino.MustUnmarshalJSON(bz, ptr) | ||
err := cdc.jsonUnmarshalAnys(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) | ||
} |
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) | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 thatMarshaler
expects types that implementProtoMarshaler
(i.e. generated by protoc). So any code usingMarshaler
is more or less ensured to be proto compatible. This legacyCodec
is pure amino.