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

fix: deterministic protov2 marshal (backport #18403) #18406

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions baseapp/internal/protocompat/protocompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

var (
gogoType = reflect.TypeOf((*gogoproto.Message)(nil)).Elem()
protov2Type = reflect.TypeOf((*proto2.Message)(nil)).Elem()
gogoType = reflect.TypeOf((*gogoproto.Message)(nil)).Elem()
protov2Type = reflect.TypeOf((*proto2.Message)(nil)).Elem()
protov2MarshalOpts = proto2.MarshalOptions{Deterministic: true}
)

type Handler = func(ctx context.Context, request, response protoiface.MessageV1) error
Expand Down Expand Up @@ -96,7 +97,7 @@ func makeProtoV2HybridHandler(prefMethod protoreflect.MethodDescriptor, cdc code
// the response is a protov2 message, so we cannot just return it.
// since the request came as gogoproto, we expect the response
// to also be gogoproto.
respBytes, err := proto2.Marshal(resp.(proto2.Message))
respBytes, err := protov2MarshalOpts.Marshal(resp.(proto2.Message))
if err != nil {
return err
}
Expand Down Expand Up @@ -137,7 +138,7 @@ func makeGogoHybridHandler(prefMethod protoreflect.MethodDescriptor, cdc codec.B
switch m := inReq.(type) {
case proto2.Message:
// we need to marshal and unmarshal the request.
requestBytes, err := proto2.Marshal(m)
requestBytes, err := protov2MarshalOpts.Marshal(m)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion codec/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ type collValue2[T any, PT protoMessageV2[T]] struct {
}

func (c collValue2[T, PT]) Encode(value PT) ([]byte, error) {
return protov2.Marshal(value)
protov2MarshalOpts := protov2.MarshalOptions{Deterministic: true}
return protov2MarshalOpts.Marshal(value)
}

func (c collValue2[T, PT]) Decode(b []byte) (PT, error) {
Expand Down
3 changes: 2 additions & 1 deletion codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ type grpcProtoCodec struct {
func (g grpcProtoCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case proto.Message:
return proto.Marshal(m)
protov2MarshalOpts := proto.MarshalOptions{Deterministic: true}
return protov2MarshalOpts.Marshal(m)
case gogoproto.Message:
return g.cdc.Marshal(m)
default:
Expand Down
6 changes: 4 additions & 2 deletions codec/types/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func NewAnyWithValue(v proto.Message) (*Any, error) {
err error
)
if msg, ok := v.(protov2.Message); ok {
bz, err = protov2.Marshal(msg)
protov2MarshalOpts := protov2.MarshalOptions{Deterministic: true}
bz, err = protov2MarshalOpts.Marshal(msg)
} else {
bz, err = proto.Marshal(v)
}
Expand Down Expand Up @@ -110,7 +111,8 @@ func (any *Any) pack(x proto.Message) error {
err error
)
if msg, ok := x.(protov2.Message); ok {
bz, err = protov2.Marshal(msg)
protov2MarshalOpts := protov2.MarshalOptions{Deterministic: true}
bz, err = protov2MarshalOpts.Marshal(msg)
} else {
bz, err = proto.Marshal(x)
}
Expand Down
Loading