Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
co-authored by
Julien Robert
parent
3da9f9c9ec
commit
8334eefaaf
+12
-2
@@ -12,16 +12,26 @@ import (
|
||||
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
)
|
||||
|
||||
type bankSendWrapper struct {
|
||||
*banktypes.MsgSend
|
||||
}
|
||||
|
||||
func (msg bankSendWrapper) GetSigners() []sdk.AccAddress {
|
||||
fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress)
|
||||
return []sdk.AccAddress{fromAddress}
|
||||
}
|
||||
|
||||
func BenchmarkLegacyGetSigners(b *testing.B) {
|
||||
_, _, addr := testdata.KeyTestPubAddr()
|
||||
msg := &banktypes.MsgSend{
|
||||
msg := bankSendWrapper{&banktypes.MsgSend{
|
||||
FromAddress: addr.String(),
|
||||
ToAddress: "",
|
||||
Amount: nil,
|
||||
}
|
||||
}}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
+22
-4
@@ -4,6 +4,7 @@ import (
|
||||
fmt "fmt"
|
||||
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
|
||||
@@ -63,13 +64,21 @@ func NewAnyWithValue(v proto.Message) (*Any, error) {
|
||||
return nil, errorsmod.Wrap(sdkerrors.ErrPackAny, "Expecting non nil value to create a new Any")
|
||||
}
|
||||
|
||||
bz, err := proto.Marshal(v)
|
||||
var (
|
||||
bz []byte
|
||||
err error
|
||||
)
|
||||
if msg, ok := v.(protov2.Message); ok {
|
||||
bz, err = protov2.Marshal(msg)
|
||||
} else {
|
||||
bz, err = proto.Marshal(v)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Any{
|
||||
TypeUrl: "/" + proto.MessageName(v),
|
||||
TypeUrl: MsgTypeURL(v),
|
||||
Value: bz,
|
||||
cachedValue: v,
|
||||
}, nil
|
||||
@@ -94,8 +103,17 @@ func UnsafePackAny(x interface{}) *Any {
|
||||
// the packed value so that it can be retrieved from GetCachedValue without
|
||||
// unmarshaling
|
||||
func (any *Any) pack(x proto.Message) error {
|
||||
any.TypeUrl = "/" + proto.MessageName(x)
|
||||
bz, err := proto.Marshal(x)
|
||||
any.TypeUrl = MsgTypeURL(x)
|
||||
|
||||
var (
|
||||
bz []byte
|
||||
err error
|
||||
)
|
||||
if msg, ok := x.(protov2.Message); ok {
|
||||
bz, err = protov2.Marshal(msg)
|
||||
} else {
|
||||
bz, err = proto.Marshal(x)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ func (registry *interfaceRegistry) EnsureRegistered(impl interface{}) error {
|
||||
// same typeURL.
|
||||
func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, impls ...proto.Message) {
|
||||
for _, impl := range impls {
|
||||
typeURL := "/" + proto.MessageName(impl)
|
||||
typeURL := MsgTypeURL(impl)
|
||||
registry.registerImpl(iface, typeURL, impl)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// MsgTypeURL returns the TypeURL of a `sdk.Msg`.
|
||||
func MsgTypeURL(msg proto.Message) string {
|
||||
if m, ok := msg.(protov2.Message); ok {
|
||||
return "/" + string(m.ProtoReflect().Descriptor().FullName())
|
||||
}
|
||||
|
||||
return "/" + proto.MessageName(msg)
|
||||
}
|
||||
Reference in New Issue
Block a user