chore: run modernize (#24356)

This commit is contained in:
Alex | Interchain Labs
2025-04-03 10:42:20 -04:00
committed by GitHub
parent df07789843
commit 3c6deab626
185 changed files with 667 additions and 590 deletions
+75
View File
@@ -0,0 +1,75 @@
package types_test
import (
"fmt"
"runtime"
"testing"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
type errOnMarshal struct {
testdata.Dog
}
var _ proto.Message = (*errOnMarshal)(nil)
var errAlways = fmt.Errorf("always erroring")
func (eom *errOnMarshal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return nil, errAlways
}
var eom = &errOnMarshal{}
// Ensure that returning an error doesn't suddenly allocate and waste bytes.
// See https://github.com/cosmos/cosmos-sdk/issues/8537
func TestNewAnyWithCustomTypeURLWithErrorNoAllocation(t *testing.T) {
// This tests continues to fail inconsistently.
//
// Example: https://github.com/cosmos/cosmos-sdk/pull/9246/checks?check_run_id=2643313958#step:6:118
// Ref: https://github.com/cosmos/cosmos-sdk/issues/9010
t.SkipNow()
// make sure we're not in the middle of a GC.
runtime.GC()
var ms1, ms2 runtime.MemStats
runtime.ReadMemStats(&ms1)
any, err := types.NewAnyWithValue(eom)
runtime.ReadMemStats(&ms2)
// Ensure that no fresh allocation was made.
if diff := ms2.HeapAlloc - ms1.HeapAlloc; diff > 0 {
t.Errorf("Unexpected allocation of %d bytes", diff)
}
if err == nil {
t.Fatal("err wasn't returned")
}
if any != nil {
t.Fatalf("Unexpectedly got a non-nil Any value: %v", any)
}
}
var sink any
func BenchmarkNewAnyWithCustomTypeURLWithErrorReturned(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
any, err := types.NewAnyWithValue(eom)
if err == nil {
b.Fatal("err wasn't returned")
}
if any != nil {
b.Fatalf("Unexpectedly got a non-nil Any value: %v", any)
}
sink = any
}
if sink == nil {
b.Fatal("benchmark didn't run")
}
sink = (any)(nil)
}
+1 -1
View File
@@ -19,5 +19,5 @@ type AminoJSONUnpacker = gogoany.AminoJSONUnpacker
// AminoJSONPacker is an alias for github.com/cosmos/gogoproto/types/any.AminoJSONPacker.
type AminoJSONPacker = gogoany.AminoJSONPacker
// ProtoUnpacker is an alias for github.com/cosmos/gogoproto/types/any.ProtoJSONPacker.
// ProtoJSONPacker is an alias for github.com/cosmos/gogoproto/types/any.ProtoJSONPacker.
type ProtoJSONPacker = gogoany.ProtoJSONPacker
+9 -9
View File
@@ -54,14 +54,14 @@ type InterfaceRegistry interface {
//
// Ex:
// registry.RegisterInterface("cosmos.base.v1beta1.Msg", (*sdk.Msg)(nil))
RegisterInterface(protoName string, iface interface{}, impls ...proto.Message)
RegisterInterface(protoName string, iface any, impls ...proto.Message)
// RegisterImplementations registers impls as concrete implementations of
// the interface iface.
//
// Ex:
// registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
RegisterImplementations(iface interface{}, impls ...proto.Message)
RegisterImplementations(iface any, impls ...proto.Message)
// ListAllInterfaces list the type URLs of all registered interfaces.
ListAllInterfaces() []string
@@ -71,7 +71,7 @@ type InterfaceRegistry interface {
ListImplementations(ifaceTypeURL string) []string
// EnsureRegistered ensures there is a registered interface for the given concrete type.
EnsureRegistered(iface interface{}) error
EnsureRegistered(iface any) error
protodesc.Resolver
@@ -169,7 +169,7 @@ func NewInterfaceRegistryWithOptions(options InterfaceRegistryOptions) (Interfac
}, nil
}
func (registry *interfaceRegistry) RegisterInterface(protoName string, iface interface{}, impls ...proto.Message) {
func (registry *interfaceRegistry) RegisterInterface(protoName string, iface any, impls ...proto.Message) {
typ := reflect.TypeOf(iface)
if typ.Elem().Kind() != reflect.Interface {
panic(fmt.Errorf("%T is not an interface type", iface))
@@ -182,7 +182,7 @@ func (registry *interfaceRegistry) RegisterInterface(protoName string, iface int
// EnsureRegistered ensures there is a registered interface for the given concrete type.
//
// Returns an error if not, and nil if so.
func (registry *interfaceRegistry) EnsureRegistered(impl interface{}) error {
func (registry *interfaceRegistry) EnsureRegistered(impl any) error {
if reflect.ValueOf(impl).Kind() != reflect.Ptr {
return fmt.Errorf("%T is not a pointer", impl)
}
@@ -199,7 +199,7 @@ func (registry *interfaceRegistry) EnsureRegistered(impl interface{}) error {
//
// This function PANICs if different concrete types are registered under the
// same typeURL.
func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, impls ...proto.Message) {
func (registry *interfaceRegistry) RegisterImplementations(iface any, impls ...proto.Message) {
for _, impl := range impls {
typeURL := MsgTypeURL(impl)
registry.registerImpl(iface, typeURL, impl)
@@ -211,7 +211,7 @@ func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, im
//
// This function PANICs if different concrete types are registered under the
// same typeURL.
func (registry *interfaceRegistry) RegisterCustomTypeURL(iface interface{}, typeURL string, impl proto.Message) {
func (registry *interfaceRegistry) RegisterCustomTypeURL(iface any, typeURL string, impl proto.Message) {
registry.registerImpl(iface, typeURL, impl)
}
@@ -220,7 +220,7 @@ func (registry *interfaceRegistry) RegisterCustomTypeURL(iface interface{}, type
//
// This function PANICs if different concrete types are registered under the
// same typeURL.
func (registry *interfaceRegistry) registerImpl(iface interface{}, typeURL string, impl proto.Message) {
func (registry *interfaceRegistry) registerImpl(iface any, typeURL string, impl proto.Message) {
ityp := reflect.TypeOf(iface).Elem()
imap, found := registry.interfaceImpls[ityp]
if !found {
@@ -283,7 +283,7 @@ func (registry *interfaceRegistry) ListImplementations(ifaceName string) []strin
return keys
}
func (registry *interfaceRegistry) UnpackAny(any *Any, iface interface{}) error {
func (registry *interfaceRegistry) UnpackAny(any *Any, iface any) error {
unpacker := &statefulUnpacker{
registry: registry,
maxDepth: MaxUnpackAnyRecursionDepth,