Skip to content

Commit

Permalink
fix: deterministic protov2 marshal (backport #18403) (#18406)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
mergify[bot] and julienrbrt committed Nov 7, 2023
1 parent d1f41e5 commit 5bed3ae
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
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

0 comments on commit 5bed3ae

Please sign in to comment.