Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080)
* Changelog update * Rename codec.MarshalAny * move codec.MarshalInterface to codec.Marshaler * fix tests * Update amino_codec for compliance with MarshalerInterface * update tests and comments * add tests * change order of args in UnmarshalInterface to a canonical one * uplift MarshalInterface to take ProtoMessage as an argument * wip * add nil check * make tests working * tests cleanup * add support for *JSON methods * Update changelog * linter fixes * fix test types * update evidence genesis_test * adding test * review updates Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot]
parent
da6b7f7755
commit
b219c54c2d
@@ -6,6 +6,7 @@
|
||||
- 2020 Feb 24: Updates to handle messages with interface fields
|
||||
- 2020 Apr 27: Convert usages of `oneof` for interfaces to `Any`
|
||||
- 2020 May 15: Describe `cosmos_proto` extensions and amino compatibility
|
||||
- 2020 Dec 4: Move and rename `MarshalAny` and `UnmarshalAny` into the `codec.Marshaler` interface.
|
||||
|
||||
## Status
|
||||
|
||||
@@ -221,23 +222,20 @@ every module that implements it in order to populate the `InterfaceRegistry`.
|
||||
|
||||
### Using `Any` to encode state
|
||||
|
||||
The SDK will provide support methods `MarshalAny` and `UnmarshalAny` to allow
|
||||
easy encoding of state to `Any` in `Codec` implementations. Ex:
|
||||
The SDK will provide support methods `MarshalInterface` and `UnmarshalInterface` to hide a complexity of wrapping interface types into `Any` and allow easy serialization.
|
||||
|
||||
```go
|
||||
import "github.com/cosmos/cosmos-sdk/codec"
|
||||
|
||||
func (c *Codec) MarshalEvidence(evidenceI eviexported.Evidence) ([]byte, error) {
|
||||
return codec.MarshalAny(evidenceI)
|
||||
// note: eviexported.Evidence is an interface type
|
||||
func MarshalEvidence(cdc codec.BinaryMarshaler, e eviexported.Evidence) ([]byte, error) {
|
||||
return cdc.MarshalInterface(e)
|
||||
}
|
||||
|
||||
func (c *Codec) UnmarshalEvidence(bz []byte) (eviexported.Evidence, error) {
|
||||
func UnmarshalEvidence(cdc codec.BinaryMarshaler, bz []byte) (eviexported.Evidence, error) {
|
||||
var evi eviexported.Evidence
|
||||
err := codec.UnmarshalAny(c.interfaceContext, &evi, bz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return evi, nil
|
||||
err := cdc.UnmarshalInterface(&evi, bz)
|
||||
return err, nil
|
||||
}
|
||||
```
|
||||
|
||||
@@ -375,4 +373,3 @@ seamless.
|
||||
|
||||
1. https://github.com/cosmos/cosmos-sdk/issues/4977
|
||||
2. https://github.com/cosmos/cosmos-sdk/issues/5444
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ Protobuf types can be defined to encode:
|
||||
- [`Msg`s](../building-modules/messages-and-queries.md#messages)
|
||||
- [Query services](../building-modules/query-services.md)
|
||||
- [genesis](../building-modules/genesis.md)
|
||||
|
||||
|
||||
**Naming and conventions**
|
||||
|
||||
We encourage developers to follow industry guidelines: [Protocol Buffers style guide](https://developers.google.com/protocol-buffers/docs/style)
|
||||
@@ -95,11 +95,9 @@ may simply migrate any existing types that
|
||||
are encoded and persisted via their concrete Amino codec to Protobuf (see 1. for further guidelines) and accept a `Marshaler` as the codec which is implemented via the `ProtoCodec`
|
||||
without any further customization.
|
||||
|
||||
However, if modules are to handle type interfaces, module-level .proto files should define messages which encode interfaces
|
||||
using [`google.protobuf.Any`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto).
|
||||
However, if a module type composes an interface, it must wrap it in the `skd.Any` (from `/types` package) type. To do that, a module-level .proto file must use [`google.protobuf.Any`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto) for respective message type interface types.
|
||||
|
||||
For example, we can define `MsgSubmitEvidence` as follows where `Evidence` is
|
||||
an interface:
|
||||
For example, in the `x/evidence` module defines an `Evidence` interface, which is used by the `MsgSubmitEvidence`. The structure definition must use `sdk.Any` to wrap the evidence file. In the proto file we define it as follows:
|
||||
|
||||
```protobuf
|
||||
// proto/cosmos/evidence/v1beta1/tx.proto
|
||||
@@ -110,8 +108,7 @@ message MsgSubmitEvidence {
|
||||
}
|
||||
```
|
||||
|
||||
The SDK provides support methods `MarshalAny` and `UnmarshalAny` to allow
|
||||
easy encoding of state to `Any`.
|
||||
The SDK `codec.Marshaler` interface provides support methods `MarshalInterface` and `UnmarshalInterface` to easy encoding of state to `Any`.
|
||||
|
||||
Module should register interfaces using `InterfaceRegistry` which provides a mechanism for registering interfaces: `RegisterInterface(protoName string, iface interface{})` and implementations: `RegisterImplementations(iface interface{}, impls ...proto.Message)` that can be safely unpacked from Any, similarly to type registration with Amino:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user