refactor!: turn MsgsV2 into ReflectMessages to make it less confusing (#19839)
Co-authored-by: marbar3778 <marbar3778@yahoo.com> Co-authored-by: sontrinh16 <trinhleson2000@gmail.com> Co-authored-by: Marko <marko@baricevic.me> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
marbar3778
sontrinh16
Marko
coderabbitai[bot]
parent
2c3fd19099
commit
53925ef5fd
+12
-10
@@ -3,7 +3,7 @@ package codec
|
||||
import (
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
"google.golang.org/grpc/encoding"
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/types"
|
||||
)
|
||||
@@ -24,17 +24,19 @@ type (
|
||||
InterfaceRegistry() types.InterfaceRegistry
|
||||
|
||||
// GetMsgAnySigners returns the signers of the given message encoded in a protobuf Any
|
||||
// as well as the decoded google.golang.org/protobuf/proto.Message that was used to
|
||||
// extract the signers so that this can be used in other contexts.
|
||||
GetMsgAnySigners(msg *types.Any) ([][]byte, protov2.Message, error)
|
||||
// as well as the decoded protoreflect.Message that was used to extract the
|
||||
// signers so that this can be used in other context where proto reflection
|
||||
// is needed.
|
||||
GetMsgAnySigners(msg *types.Any) ([][]byte, protoreflect.Message, error)
|
||||
|
||||
// GetMsgV2Signers returns the signers of the given message.
|
||||
GetMsgV2Signers(msg protov2.Message) ([][]byte, error)
|
||||
// GetMsgSigners returns the signers of the given message plus the
|
||||
// decoded protoreflect.Message that was used to extract the
|
||||
// signers so that this can be used in other context where proto reflection
|
||||
// is needed.
|
||||
GetMsgSigners(msg proto.Message) ([][]byte, protoreflect.Message, error)
|
||||
|
||||
// GetMsgV1Signers returns the signers of the given message plus the
|
||||
// decoded google.golang.org/protobuf/proto.Message that was used to extract the
|
||||
// signers so that this can be used in other contexts.
|
||||
GetMsgV1Signers(msg proto.Message) ([][]byte, protov2.Message, error)
|
||||
// GetReflectMsgSigners returns the signers of the given reflected proto message.
|
||||
GetReflectMsgSigners(msg protoreflect.Message) ([][]byte, error)
|
||||
|
||||
// mustEmbedCodec requires that all implementations of Codec embed an official implementation from the codec
|
||||
// package. This allows new methods to be added to the Codec interface without breaking backwards compatibility.
|
||||
|
||||
@@ -299,7 +299,7 @@ func (pc *ProtoCodec) InterfaceRegistry() types.InterfaceRegistry {
|
||||
return pc.interfaceRegistry
|
||||
}
|
||||
|
||||
func (pc ProtoCodec) GetMsgAnySigners(msg *types.Any) ([][]byte, proto.Message, error) {
|
||||
func (pc ProtoCodec) GetMsgAnySigners(msg *types.Any) ([][]byte, protoreflect.Message, error) {
|
||||
msgv2, err := anyutil.Unpack(&anypb.Any{
|
||||
TypeUrl: msg.TypeUrl,
|
||||
Value: msg.Value,
|
||||
@@ -309,17 +309,17 @@ func (pc ProtoCodec) GetMsgAnySigners(msg *types.Any) ([][]byte, proto.Message,
|
||||
}
|
||||
|
||||
signers, err := pc.interfaceRegistry.SigningContext().GetSigners(msgv2)
|
||||
return signers, msgv2, err
|
||||
return signers, msgv2.ProtoReflect(), err
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) GetMsgV2Signers(msg proto.Message) ([][]byte, error) {
|
||||
return pc.interfaceRegistry.SigningContext().GetSigners(msg)
|
||||
func (pc *ProtoCodec) GetReflectMsgSigners(msg protoreflect.Message) ([][]byte, error) {
|
||||
return pc.interfaceRegistry.SigningContext().GetSigners(msg.Interface())
|
||||
}
|
||||
|
||||
func (pc *ProtoCodec) GetMsgV1Signers(msg gogoproto.Message) ([][]byte, proto.Message, error) {
|
||||
func (pc *ProtoCodec) GetMsgSigners(msg gogoproto.Message) ([][]byte, protoreflect.Message, error) {
|
||||
if msgV2, ok := msg.(proto.Message); ok {
|
||||
signers, err := pc.interfaceRegistry.SigningContext().GetSigners(msgV2)
|
||||
return signers, msgV2, err
|
||||
return signers, msgV2.ProtoReflect(), err
|
||||
}
|
||||
a, err := types.NewAnyWithValue(msg)
|
||||
if err != nil {
|
||||
|
||||
@@ -191,12 +191,12 @@ func TestGetSigners(t *testing.T) {
|
||||
msgSendV1 := &countertypes.MsgIncreaseCounter{Signer: testAddrStr, Count: 1}
|
||||
msgSendV2 := &counterv1.MsgIncreaseCounter{Signer: testAddrStr, Count: 1}
|
||||
|
||||
signers, msgSendV2Copy, err := cdc.GetMsgV1Signers(msgSendV1)
|
||||
signers, msgSendV2Copy, err := cdc.GetMsgSigners(msgSendV1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, [][]byte{testAddr}, signers)
|
||||
require.True(t, protov2.Equal(msgSendV2, msgSendV2Copy))
|
||||
require.True(t, protov2.Equal(msgSendV2, msgSendV2Copy.Interface()))
|
||||
|
||||
signers, err = cdc.GetMsgV2Signers(msgSendV2)
|
||||
signers, err = cdc.GetReflectMsgSigners(msgSendV2.ProtoReflect())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, [][]byte{testAddr}, signers)
|
||||
|
||||
@@ -205,7 +205,7 @@ func TestGetSigners(t *testing.T) {
|
||||
signers, msgSendV2Copy, err = cdc.GetMsgAnySigners(msgSendAny)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, [][]byte{testAddr}, signers)
|
||||
require.True(t, protov2.Equal(msgSendV2, msgSendV2Copy))
|
||||
require.True(t, protov2.Equal(msgSendV2, msgSendV2Copy.Interface()))
|
||||
}
|
||||
|
||||
type testAddressCodec struct{}
|
||||
|
||||
Reference in New Issue
Block a user