fix: Remove google.golang.org/protobuf Any prefix (#14119)

* refactor: Remove Any's ypes.googleapis.com prefix

* Add docs

* go mod tidy

* Update
This commit is contained in:
Amaury
2022-12-01 18:09:01 +00:00
committed by GitHub
parent 4cfc79ad4e
commit 0c65734bbe
5 changed files with 52 additions and 21 deletions
+28
View File
@@ -244,6 +244,34 @@ A real-life example of encoding the pubkey as `Any` inside the Validator struct
https://github.com/cosmos/cosmos-sdk/blob/v0.46.0/x/staking/types/validator.go#L40-L61
```
#### `Any`'s TypeURL
When packing a protobuf message inside an `Any`, the message's type is uniquely defined by its type URL, which is the message's fully qualified name prefixed by a `/` (slash) character. In some implementations of `Any`, like the gogoproto one, there's generally [a resolvable prefix, e.g. `type.googleapis.com`](https://github.com/gogo/protobuf/blob/b03c65ea87cdc3521ede29f62fe3ce239267c1bc/protobuf/google/protobuf/any.proto#L87-L91). However, in the Cosmos SDK, we made the decision to not include such prefix, to have shorter type URLs. The Cosmos SDK's own `Any` implementation can be found in `github.com/cosmos/cosmos-sdk/codec/types`.
The Cosmos SDK is also switching away from gogoproto to the official `google.golang.org/protobuf` (known as the Protobuf API v2). Its default `Any` implementation also contains the [`type.googleapis.com`](https://github.com/protocolbuffers/protobuf-go/blob/v1.28.1/types/known/anypb/any.pb.go#L266) prefix. To maintain compatibility with the SDK, the following methods from `"google.golang.org/protobuf/types/known/anypb"` should not be used:
- `anypb.New`
- `anypb.MarshalFrom`
- `anypb.Any#MarshalFrom`
Instead, the Cosmos SDK provides helper functions in `"github.com/cosmos/cosmos-proto/any"`, which create an official `anypb.Any` without inserting the prefixes:
- `any.New`
- `any.MarshalFrom`
For example, to pack a `sdk.Msg` called `internalMsg`, use:
```diff
import (
- "google.golang.org/protobuf/types/known/anypb"
+ "github.com/cosmos/cosmos-proto/any"
)
- anyMsg, err := anypb.New(internalMsg.Message().Interface())
+ anyMsg, err := any.New(internalMsg.Message().Interface())
- fmt.Println(anyMsg.TypeURL) // type.googleapis.com/cosmos.bank.v1beta1.MsgSend
+ fmt.Println(anyMsg.TypeURL) // /cosmos.bank.v1beta1.MsgSend
```
## FAQ
### How to create modules using protobuf encoding