From 5255833ce6dff565b0ce668635c0de9e479f6f86 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 27 Jun 2022 16:36:18 -0500 Subject: [PATCH 01/12] feat(gov): app-wiring in gov WIP --- x/distribution/module.go | 3 +++ x/gov/module.go | 34 ++++++++++++++++++++++++++++++++-- x/gov/types/v1beta1/content.go | 6 ++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/x/distribution/module.go b/x/distribution/module.go index 24c783f3b229..c1203a03542c 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -240,6 +241,7 @@ type distrOutputs struct { DistrKeeper keeper.Keeper Module runtime.AppModuleWrapper Hooks staking.StakingHooksWrapper + GovHandler v1beta1.RouteHandlerWrapper } func provideModule(in distrInputs) distrOutputs { @@ -250,5 +252,6 @@ func provideModule(in distrInputs) distrOutputs { DistrKeeper: k, Module: runtime.WrapAppModule(m), Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, + GovHandler: v1beta1.RouteHandlerWrapper{Handler: NewCommunityPoolSpendProposalHandler(k)}, } } diff --git a/x/gov/module.go b/x/gov/module.go index b93dab8f1f0a..5751b02b8fdd 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -4,11 +4,13 @@ package gov import ( "context" + "cosmossdk.io/core/appmodule" "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/runtime" "math/rand" - "github.com/grpc-ecosystem/grpc-gateway/runtime" + gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -74,7 +76,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod } // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the gov module. -func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { +func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) { if err := v1.RegisterQueryHandlerClient(context.Background(), mux, v1.NewQueryClient(clientCtx)); err != nil { panic(err) } @@ -128,6 +130,34 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, } } +func init() { + appmodule.Register( + //&modulev1.Module{} + appmodule.Provide(provideModuleBasic, provideModule), + appmodule.Invoke(invokeAddRoutes)) +} + +func provideModuleBasic() runtime.AppModuleBasicWrapper { + return runtime.WrapAppModuleBasic(AppModuleBasic{}) +} + +func provideModule(cdc codec.Codec, ak types.AccountKeeper, bk types.BankKeeper) (runtime.AppModuleWrapper, v1beta1.RouteHandlerWrapper) { + // TODO + keeper := nil + return runtime.WrapAppModule(NewAppModule(cdc, keeper, ak, bk)), v1beta1.RouteHandlerWrapper{Handler: v1beta1.ProposalHandler} +} + +func invokeAddRoutes( // keeper GovKeeper, + routes map[string]v1beta1.RouteHandlerWrapper) { + router := v1beta1.NewRouter() + for s, wrapper := range routes { + router.AddRoute(s, wrapper.Handler) + } + // TODO + // set legacyHandler on govKeeper after construction. requires refactor. legacyRouter must not be sealed after construction + // but should be sealed after this operation +} + // Name returns the gov module's name. func (AppModule) Name() string { return types.ModuleName diff --git a/x/gov/types/v1beta1/content.go b/x/gov/types/v1beta1/content.go index a0ce70457aab..4c16b476b31d 100644 --- a/x/gov/types/v1beta1/content.go +++ b/x/gov/types/v1beta1/content.go @@ -18,3 +18,9 @@ type Content interface { // Handler defines a function that handles a proposal after it has passed the // governance process. type Handler func(ctx sdk.Context, content Content) error + +// RouteHandlerWrapper is a wrapper for modules to inject RouteHandlers using depinject. +type RouteHandlerWrapper struct{ Handler } + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (RouteHandlerWrapper) IsOnePerModuleType() {} From b41e56a2f98537aedf0877a290749477452e0451 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Tue, 28 Jun 2022 11:50:30 -0500 Subject: [PATCH 02/12] Return gov handler from params module as output --- api/cosmos/gov/module/v1/module.pulsar.go | 560 ++++++++++++++++++++++ proto/cosmos/gov/module/v1/module.proto | 14 + x/gov/keeper/keeper.go | 33 +- x/gov/module.go | 40 +- x/gov/types/v1beta1/content.go | 10 +- x/params/module.go | 5 +- 6 files changed, 629 insertions(+), 33 deletions(-) create mode 100644 api/cosmos/gov/module/v1/module.pulsar.go create mode 100644 proto/cosmos/gov/module/v1/module.proto diff --git a/api/cosmos/gov/module/v1/module.pulsar.go b/api/cosmos/gov/module/v1/module.pulsar.go new file mode 100644 index 000000000000..2e48605d2448 --- /dev/null +++ b/api/cosmos/gov/module/v1/module.pulsar.go @@ -0,0 +1,560 @@ +// Code generated by protoc-gen-go-pulsar. DO NOT EDIT. +package modulev1 + +import ( + _ "cosmossdk.io/api/cosmos/app/v1alpha1" + fmt "fmt" + runtime "github.com/cosmos/cosmos-proto/runtime" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoiface "google.golang.org/protobuf/runtime/protoiface" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + io "io" + reflect "reflect" + sync "sync" +) + +var ( + md_Module protoreflect.MessageDescriptor + fd_Module_max_metadata_len protoreflect.FieldDescriptor +) + +func init() { + file_cosmos_gov_module_v1_module_proto_init() + md_Module = File_cosmos_gov_module_v1_module_proto.Messages().ByName("Module") + fd_Module_max_metadata_len = md_Module.Fields().ByName("max_metadata_len") +} + +var _ protoreflect.Message = (*fastReflection_Module)(nil) + +type fastReflection_Module Module + +func (x *Module) ProtoReflect() protoreflect.Message { + return (*fastReflection_Module)(x) +} + +func (x *Module) slowProtoReflect() protoreflect.Message { + mi := &file_cosmos_gov_module_v1_module_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +var _fastReflection_Module_messageType fastReflection_Module_messageType +var _ protoreflect.MessageType = fastReflection_Module_messageType{} + +type fastReflection_Module_messageType struct{} + +func (x fastReflection_Module_messageType) Zero() protoreflect.Message { + return (*fastReflection_Module)(nil) +} +func (x fastReflection_Module_messageType) New() protoreflect.Message { + return new(fastReflection_Module) +} +func (x fastReflection_Module_messageType) Descriptor() protoreflect.MessageDescriptor { + return md_Module +} + +// Descriptor returns message descriptor, which contains only the protobuf +// type information for the message. +func (x *fastReflection_Module) Descriptor() protoreflect.MessageDescriptor { + return md_Module +} + +// Type returns the message type, which encapsulates both Go and protobuf +// type information. If the Go type information is not needed, +// it is recommended that the message descriptor be used instead. +func (x *fastReflection_Module) Type() protoreflect.MessageType { + return _fastReflection_Module_messageType +} + +// New returns a newly allocated and mutable empty message. +func (x *fastReflection_Module) New() protoreflect.Message { + return new(fastReflection_Module) +} + +// Interface unwraps the message reflection interface and +// returns the underlying ProtoMessage interface. +func (x *fastReflection_Module) Interface() protoreflect.ProtoMessage { + return (*Module)(x) +} + +// Range iterates over every populated field in an undefined order, +// calling f for each field descriptor and value encountered. +// Range returns immediately if f returns false. +// While iterating, mutating operations may only be performed +// on the current field descriptor. +func (x *fastReflection_Module) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) { + if x.MaxMetadataLen != uint64(0) { + value := protoreflect.ValueOfUint64(x.MaxMetadataLen) + if !f(fd_Module_max_metadata_len, value) { + return + } + } +} + +// Has reports whether a field is populated. +// +// Some fields have the property of nullability where it is possible to +// distinguish between the default value of a field and whether the field +// was explicitly populated with the default value. Singular message fields, +// member fields of a oneof, and proto2 scalar fields are nullable. Such +// fields are populated only if explicitly set. +// +// In other cases (aside from the nullable cases above), +// a proto3 scalar field is populated if it contains a non-zero value, and +// a repeated field is populated if it is non-empty. +func (x *fastReflection_Module) Has(fd protoreflect.FieldDescriptor) bool { + switch fd.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + return x.MaxMetadataLen != uint64(0) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", fd.FullName())) + } +} + +// Clear clears the field such that a subsequent Has call reports false. +// +// Clearing an extension field clears both the extension type and value +// associated with the given field number. +// +// Clear is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_Module) Clear(fd protoreflect.FieldDescriptor) { + switch fd.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + x.MaxMetadataLen = uint64(0) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", fd.FullName())) + } +} + +// Get retrieves the value for a field. +// +// For unpopulated scalars, it returns the default value, where +// the default value of a bytes scalar is guaranteed to be a copy. +// For unpopulated composite types, it returns an empty, read-only view +// of the value; to obtain a mutable reference, use Mutable. +func (x *fastReflection_Module) Get(descriptor protoreflect.FieldDescriptor) protoreflect.Value { + switch descriptor.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + value := x.MaxMetadataLen + return protoreflect.ValueOfUint64(value) + default: + if descriptor.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", descriptor.FullName())) + } +} + +// Set stores the value for a field. +// +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType. +// When setting a composite type, it is unspecified whether the stored value +// aliases the source's memory in any way. If the composite value is an +// empty, read-only value, then it panics. +// +// Set is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_Module) Set(fd protoreflect.FieldDescriptor, value protoreflect.Value) { + switch fd.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + x.MaxMetadataLen = value.Uint() + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", fd.FullName())) + } +} + +// Mutable returns a mutable reference to a composite type. +// +// If the field is unpopulated, it may allocate a composite value. +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType +// if not already stored. +// It panics if the field does not contain a composite type. +// +// Mutable is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_Module) Mutable(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + panic(fmt.Errorf("field max_metadata_len of message cosmos.gov.module.v1.Module is not mutable")) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", fd.FullName())) + } +} + +// NewField returns a new value that is assignable to the field +// for the given descriptor. For scalars, this returns the default value. +// For lists, maps, and messages, this returns a new, empty, mutable value. +func (x *fastReflection_Module) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.gov.module.v1.Module.max_metadata_len": + return protoreflect.ValueOfUint64(uint64(0)) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.gov.module.v1.Module")) + } + panic(fmt.Errorf("message cosmos.gov.module.v1.Module does not contain field %s", fd.FullName())) + } +} + +// WhichOneof reports which field within the oneof is populated, +// returning nil if none are populated. +// It panics if the oneof descriptor does not belong to this message. +func (x *fastReflection_Module) WhichOneof(d protoreflect.OneofDescriptor) protoreflect.FieldDescriptor { + switch d.FullName() { + default: + panic(fmt.Errorf("%s is not a oneof field in cosmos.gov.module.v1.Module", d.FullName())) + } + panic("unreachable") +} + +// GetUnknown retrieves the entire list of unknown fields. +// The caller may only mutate the contents of the RawFields +// if the mutated bytes are stored back into the message with SetUnknown. +func (x *fastReflection_Module) GetUnknown() protoreflect.RawFields { + return x.unknownFields +} + +// SetUnknown stores an entire list of unknown fields. +// The raw fields must be syntactically valid according to the wire format. +// An implementation may panic if this is not the case. +// Once stored, the caller must not mutate the content of the RawFields. +// An empty RawFields may be passed to clear the fields. +// +// SetUnknown is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_Module) SetUnknown(fields protoreflect.RawFields) { + x.unknownFields = fields +} + +// IsValid reports whether the message is valid. +// +// An invalid message is an empty, read-only value. +// +// An invalid message often corresponds to a nil pointer of the concrete +// message type, but the details are implementation dependent. +// Validity is not part of the protobuf data model, and may not +// be preserved in marshaling or other operations. +func (x *fastReflection_Module) IsValid() bool { + return x != nil +} + +// ProtoMethods returns optional fastReflectionFeature-path implementations of various operations. +// This method may return nil. +// +// The returned methods type is identical to +// "google.golang.org/protobuf/runtime/protoiface".Methods. +// Consult the protoiface package documentation for details. +func (x *fastReflection_Module) ProtoMethods() *protoiface.Methods { + size := func(input protoiface.SizeInput) protoiface.SizeOutput { + x := input.Message.Interface().(*Module) + if x == nil { + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: 0, + } + } + options := runtime.SizeInputToOptions(input) + _ = options + var n int + var l int + _ = l + if x.MaxMetadataLen != 0 { + n += 1 + runtime.Sov(uint64(x.MaxMetadataLen)) + } + if x.unknownFields != nil { + n += len(x.unknownFields) + } + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: n, + } + } + + marshal := func(input protoiface.MarshalInput) (protoiface.MarshalOutput, error) { + x := input.Message.Interface().(*Module) + if x == nil { + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + options := runtime.MarshalInputToOptions(input) + _ = options + size := options.Size(x) + dAtA := make([]byte, size) + i := len(dAtA) + _ = i + var l int + _ = l + if x.unknownFields != nil { + i -= len(x.unknownFields) + copy(dAtA[i:], x.unknownFields) + } + if x.MaxMetadataLen != 0 { + i = runtime.EncodeVarint(dAtA, i, uint64(x.MaxMetadataLen)) + i-- + dAtA[i] = 0x8 + } + if input.Buf != nil { + input.Buf = append(input.Buf, dAtA...) + } else { + input.Buf = dAtA + } + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + unmarshal := func(input protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) { + x := input.Message.Interface().(*Module) + if x == nil { + return protoiface.UnmarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Flags: input.Flags, + }, nil + } + options := runtime.UnmarshalInputToOptions(input) + _ = options + dAtA := input.Buf + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: Module: wiretype end group for non-group") + } + if fieldNum <= 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: Module: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field MaxMetadataLen", wireType) + } + x.MaxMetadataLen = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + x.MaxMetadataLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := runtime.Skip(dAtA[iNdEx:]) + if err != nil { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if (iNdEx + skippy) > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + if !options.DiscardUnknown { + x.unknownFields = append(x.unknownFields, dAtA[iNdEx:iNdEx+skippy]...) + } + iNdEx += skippy + } + } + + if iNdEx > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, nil + } + return &protoiface.Methods{ + NoUnkeyedLiterals: struct{}{}, + Flags: protoiface.SupportMarshalDeterministic | protoiface.SupportUnmarshalDiscardUnknown, + Size: size, + Marshal: marshal, + Unmarshal: unmarshal, + Merge: nil, + CheckInitialized: nil, + } +} + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.0 +// protoc (unknown) +// source: cosmos/gov/module/v1/module.proto + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Module is the config object of the gov module. +type Module struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + MaxMetadataLen uint64 `protobuf:"varint,1,opt,name=max_metadata_len,json=maxMetadataLen,proto3" json:"max_metadata_len,omitempty"` +} + +func (x *Module) Reset() { + *x = Module{} + if protoimpl.UnsafeEnabled { + mi := &file_cosmos_gov_module_v1_module_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Module) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Module) ProtoMessage() {} + +// Deprecated: Use Module.ProtoReflect.Descriptor instead. +func (*Module) Descriptor() ([]byte, []int) { + return file_cosmos_gov_module_v1_module_proto_rawDescGZIP(), []int{0} +} + +func (x *Module) GetMaxMetadataLen() uint64 { + if x != nil { + return x.MaxMetadataLen + } + return 0 +} + +var File_cosmos_gov_module_v1_module_proto protoreflect.FileDescriptor + +var file_cosmos_gov_module_v1_module_proto_rawDesc = []byte{ + 0x0a, 0x21, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x67, 0x6f, 0x76, 0x2f, 0x6d, 0x6f, 0x64, + 0x75, 0x6c, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x14, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x67, 0x6f, 0x76, 0x2e, + 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x1a, 0x20, 0x63, 0x6f, 0x73, 0x6d, 0x6f, + 0x73, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x2f, 0x6d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x5e, 0x0a, 0x06, 0x4d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x28, 0x0a, 0x10, 0x6d, 0x61, 0x78, 0x5f, 0x6d, 0x65, 0x74, + 0x61, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x6c, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, + 0x0e, 0x6d, 0x61, 0x78, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x4c, 0x65, 0x6e, 0x3a, + 0x2a, 0xba, 0xc0, 0x96, 0xda, 0x01, 0x24, 0x0a, 0x22, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, + 0x73, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x78, 0x2f, 0x67, 0x6f, 0x76, 0x42, 0xca, 0x01, 0x0a, 0x18, + 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x67, 0x6f, 0x76, 0x2e, 0x6d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x0b, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, + 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2f, 0x67, 0x6f, 0x76, 0x2f, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x6d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x47, 0x4d, 0xaa, 0x02, 0x14, + 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x47, 0x6f, 0x76, 0x2e, 0x4d, 0x6f, 0x64, 0x75, 0x6c, + 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x14, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x47, 0x6f, + 0x76, 0x5c, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x20, 0x43, 0x6f, + 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x47, 0x6f, 0x76, 0x5c, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x5c, + 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, + 0x17, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, 0x47, 0x6f, 0x76, 0x3a, 0x3a, 0x4d, 0x6f, + 0x64, 0x75, 0x6c, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_cosmos_gov_module_v1_module_proto_rawDescOnce sync.Once + file_cosmos_gov_module_v1_module_proto_rawDescData = file_cosmos_gov_module_v1_module_proto_rawDesc +) + +func file_cosmos_gov_module_v1_module_proto_rawDescGZIP() []byte { + file_cosmos_gov_module_v1_module_proto_rawDescOnce.Do(func() { + file_cosmos_gov_module_v1_module_proto_rawDescData = protoimpl.X.CompressGZIP(file_cosmos_gov_module_v1_module_proto_rawDescData) + }) + return file_cosmos_gov_module_v1_module_proto_rawDescData +} + +var file_cosmos_gov_module_v1_module_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_cosmos_gov_module_v1_module_proto_goTypes = []interface{}{ + (*Module)(nil), // 0: cosmos.gov.module.v1.Module +} +var file_cosmos_gov_module_v1_module_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_cosmos_gov_module_v1_module_proto_init() } +func file_cosmos_gov_module_v1_module_proto_init() { + if File_cosmos_gov_module_v1_module_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_cosmos_gov_module_v1_module_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Module); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_cosmos_gov_module_v1_module_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_cosmos_gov_module_v1_module_proto_goTypes, + DependencyIndexes: file_cosmos_gov_module_v1_module_proto_depIdxs, + MessageInfos: file_cosmos_gov_module_v1_module_proto_msgTypes, + }.Build() + File_cosmos_gov_module_v1_module_proto = out.File + file_cosmos_gov_module_v1_module_proto_rawDesc = nil + file_cosmos_gov_module_v1_module_proto_goTypes = nil + file_cosmos_gov_module_v1_module_proto_depIdxs = nil +} diff --git a/proto/cosmos/gov/module/v1/module.proto b/proto/cosmos/gov/module/v1/module.proto new file mode 100644 index 000000000000..e8f7635f2320 --- /dev/null +++ b/proto/cosmos/gov/module/v1/module.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package cosmos.gov.module.v1; + +import "cosmos/app/v1alpha1/module.proto"; + +// Module is the config object of the gov module. +message Module { + option (cosmos.app.v1alpha1.module) = { + go_import: "github.com/cosmos/cosmos-sdk/x/gov" + }; + + uint64 max_metadata_len = 1; +} \ No newline at end of file diff --git a/x/gov/keeper/keeper.go b/x/gov/keeper/keeper.go index d7496837c51e..2134b2c81527 100644 --- a/x/gov/keeper/keeper.go +++ b/x/gov/keeper/keeper.go @@ -55,34 +55,27 @@ type Keeper struct { func NewKeeper( cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace types.ParamSubspace, authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper, - legacyRouter v1beta1.Router, router *baseapp.MsgServiceRouter, - config types.Config, + router *baseapp.MsgServiceRouter, config types.Config, ) Keeper { // ensure governance module account is set if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil { panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) } - // It is vital to seal the governance proposal router here as to not allow - // further handlers to be registered after the keeper is created since this - // could create invalid or non-deterministic behavior. - legacyRouter.Seal() - // If MaxMetadataLen not set by app developer, set to default value. if config.MaxMetadataLen == 0 { config.MaxMetadataLen = types.DefaultConfig().MaxMetadataLen } return Keeper{ - storeKey: key, - paramSpace: paramSpace, - authKeeper: authKeeper, - bankKeeper: bankKeeper, - sk: sk, - cdc: cdc, - legacyRouter: legacyRouter, - router: router, - config: config, + storeKey: key, + paramSpace: paramSpace, + authKeeper: authKeeper, + bankKeeper: bankKeeper, + sk: sk, + cdc: cdc, + router: router, + config: config, } } @@ -97,6 +90,14 @@ func (keeper *Keeper) SetHooks(gh types.GovHooks) *Keeper { return keeper } +func (keeper *Keeper) SetLegacyRouter(router v1beta1.Router) { + // It is vital to seal the governance proposal router here as to not allow + // further handlers to be registered after the keeper is created since this + // could create invalid or non-deterministic behavior. + router.Seal() + keeper.legacyRouter = router +} + // Logger returns a module-specific logger. func (keeper Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+types.ModuleName) diff --git a/x/gov/module.go b/x/gov/module.go index 5751b02b8fdd..d06099db8518 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -4,10 +4,13 @@ package gov import ( "context" + modulev1 "cosmossdk.io/api/cosmos/gov/module/v1" "cosmossdk.io/core/appmodule" "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/runtime" + store "github.com/cosmos/cosmos-sdk/store/types" "math/rand" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -132,7 +135,7 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, func init() { appmodule.Register( - //&modulev1.Module{} + &modulev1.Module{}, appmodule.Provide(provideModuleBasic, provideModule), appmodule.Invoke(invokeAddRoutes)) } @@ -141,21 +144,34 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper { return runtime.WrapAppModuleBasic(AppModuleBasic{}) } -func provideModule(cdc codec.Codec, ak types.AccountKeeper, bk types.BankKeeper) (runtime.AppModuleWrapper, v1beta1.RouteHandlerWrapper) { - // TODO - keeper := nil - return runtime.WrapAppModule(NewAppModule(cdc, keeper, ak, bk)), v1beta1.RouteHandlerWrapper{Handler: v1beta1.ProposalHandler} +func provideModule( + config *modulev1.Module, + cdc codec.Codec, + key *store.KVStoreKey, + subSpace types.ParamSubspace, + msgServiceRouter *baseapp.MsgServiceRouter, + ak types.AccountKeeper, + bk types.BankKeeper, + sk types.StakingKeeper) (runtime.AppModuleWrapper, v1beta1.RoutedHandler) { + + kConfig := types.DefaultConfig() + if config.MaxMetadataLen != 0 { + kConfig.MaxMetadataLen = config.MaxMetadataLen + } + + k := keeper.NewKeeper(cdc, key, subSpace, ak, bk, sk, msgServiceRouter, kConfig) + m := NewAppModule(cdc, k, ak, bk) + return runtime.WrapAppModule(m), v1beta1.RoutedHandler{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey} } -func invokeAddRoutes( // keeper GovKeeper, - routes map[string]v1beta1.RouteHandlerWrapper) { +func invokeAddRoutes( + keeper keeper.Keeper, + routes []v1beta1.RoutedHandler) { router := v1beta1.NewRouter() - for s, wrapper := range routes { - router.AddRoute(s, wrapper.Handler) + for _, r := range routes { + router.AddRoute(r.RouteKey, r.Handler) } - // TODO - // set legacyHandler on govKeeper after construction. requires refactor. legacyRouter must not be sealed after construction - // but should be sealed after this operation + keeper.SetLegacyRouter(router) } // Name returns the gov module's name. diff --git a/x/gov/types/v1beta1/content.go b/x/gov/types/v1beta1/content.go index 4c16b476b31d..dcfeacffd939 100644 --- a/x/gov/types/v1beta1/content.go +++ b/x/gov/types/v1beta1/content.go @@ -19,8 +19,10 @@ type Content interface { // governance process. type Handler func(ctx sdk.Context, content Content) error -// RouteHandlerWrapper is a wrapper for modules to inject RouteHandlers using depinject. -type RouteHandlerWrapper struct{ Handler } +type RoutedHandler struct { + Handler Handler + RouteKey string +} -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (RouteHandlerWrapper) IsOnePerModuleType() {} +// ManyPerContainer implements the depinject.ManyPerContainer interface. +func (RoutedHandler) ManyPerContainer() {} diff --git a/x/params/module.go b/x/params/module.go index 09d8664fae8c..31d44045c28c 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,6 +3,7 @@ package params import ( "context" "encoding/json" + govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -187,6 +188,7 @@ type paramsOutputs struct { ParamsKeeper keeper.Keeper BaseAppOption runtime.BaseAppOption Module runtime.AppModuleWrapper + GovHandler govv1beta1.RoutedHandler } func provideModule(in paramsInputs) paramsOutputs { @@ -195,8 +197,9 @@ func provideModule(in paramsInputs) paramsOutputs { app.SetParamStore(k.Subspace(baseapp.Paramspace).WithKeyTable(types.ConsensusParamsKeyTable())) } m := runtime.WrapAppModule(NewAppModule(k)) + govHandler := govv1beta1.RoutedHandler{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)} - return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m} + return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m, GovHandler: govHandler} } func provideSubSpace(key depinject.ModuleKey, k keeper.Keeper) types.Subspace { From 7338ccc68eaf79c077c1e82f6ec53e52290cf366 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Tue, 28 Jun 2022 11:57:11 -0500 Subject: [PATCH 03/12] fix x/distribution types --- simapp/app.go | 10 +++++----- x/distribution/module.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index fe7ed8ec60ac..cf68ab16de42 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -253,15 +253,15 @@ func NewSimApp( AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)). AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)) - govConfig := govtypes.DefaultConfig() + //govConfig := govtypes.DefaultConfig() /* Example of setting gov params: govConfig.MaxMetadataLen = 10000 */ - govKeeper := govkeeper.NewKeeper( - app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, - app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig, - ) + //govKeeper := govkeeper.NewKeeper( + // app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, + // app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig, + //) app.GovKeeper = *govKeeper.SetHooks( govtypes.NewMultiGovHooks( diff --git a/x/distribution/module.go b/x/distribution/module.go index c1203a03542c..d80042294a5c 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -241,7 +241,7 @@ type distrOutputs struct { DistrKeeper keeper.Keeper Module runtime.AppModuleWrapper Hooks staking.StakingHooksWrapper - GovHandler v1beta1.RouteHandlerWrapper + GovHandler govv1beta1.RoutedHandler } func provideModule(in distrInputs) distrOutputs { @@ -252,6 +252,6 @@ func provideModule(in distrInputs) distrOutputs { DistrKeeper: k, Module: runtime.WrapAppModule(m), Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, - GovHandler: v1beta1.RouteHandlerWrapper{Handler: NewCommunityPoolSpendProposalHandler(k)}, + GovHandler: govv1beta1.RoutedHandler{Handler: NewCommunityPoolSpendProposalHandler(k), RouteKey: types.RouterKey}, } } From 1ca6333f8fbabaafc15c3a81f1f8f49bd65df376 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Tue, 28 Jun 2022 16:48:43 -0500 Subject: [PATCH 04/12] wip x/gov --- depinject/container.go | 4 +-- simapp/app.go | 30 +--------------- simapp/app.yaml | 4 +++ x/distribution/module.go | 4 +-- x/gov/module.go | 61 +++++++++++++++++++++++++++++---- x/gov/types/expected_keepers.go | 5 +++ x/gov/types/v1beta1/content.go | 6 ++-- x/params/module.go | 23 ++++++++++--- x/upgrade/module.go | 5 ++- 9 files changed, 93 insertions(+), 49 deletions(-) diff --git a/depinject/container.go b/depinject/container.go index 706a7573d171..08fa30363b10 100644 --- a/depinject/container.go +++ b/depinject/container.go @@ -95,8 +95,6 @@ func (c *container) call(provider *ProviderDescriptor, moduleKey *moduleKey) ([] } func (c *container) getResolver(typ reflect.Type, key *moduleKey) (resolver, error) { - c.logf("Resolving %v", typ) - pr, err := c.getExplicitResolver(typ, key) if err != nil { return nil, err @@ -417,7 +415,7 @@ func (c *container) resolve(in ProviderInput, moduleKey *moduleKey, caller Locat markGraphNodeAsFailed(typeGraphNode) return reflect.Value{}, errors.Errorf("can't resolve type %v for %s:\n%s", - in.Type, caller, c.formatResolveStack()) + fullyQualifiedTypeName(in.Type), caller, c.formatResolveStack()) } res, err := vr.resolve(c, moduleKey, caller) diff --git a/simapp/app.go b/simapp/app.go index cf68ab16de42..0a2dde7c2602 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -68,8 +68,6 @@ import ( govclient "github.com/cosmos/cosmos-sdk/x/gov/client" govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "github.com/cosmos/cosmos-sdk/x/group" groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper" groupmodule "github.com/cosmos/cosmos-sdk/x/group/module" @@ -83,7 +81,6 @@ import ( paramsclient "github.com/cosmos/cosmos-sdk/x/params/client" paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" - paramproposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal" "github.com/cosmos/cosmos-sdk/x/slashing" slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" @@ -227,14 +224,13 @@ func NewSimApp( &app.EvidenceKeeper, &app.DistrKeeper, &app.UpgradeKeeper, + &app.GovKeeper, ); err != nil { panic(err) } app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...) - app.keys = sdk.NewKVStoreKeys(govtypes.StoreKey) - // configure state listening capabilities using AppOptions // we are doing nothing with the returned streamingServices and waitGroup in this case if _, _, err := streaming.LoadStreamingServices(app.App.BaseApp, appOpts, app.appCodec, app.keys); err != nil { @@ -247,28 +243,6 @@ func NewSimApp( app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName, ) - // register the proposal types - govRouter := govv1beta1.NewRouter() - govRouter.AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler). - AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)). - AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). - AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)) - //govConfig := govtypes.DefaultConfig() - /* - Example of setting gov params: - govConfig.MaxMetadataLen = 10000 - */ - //govKeeper := govkeeper.NewKeeper( - // app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper, - // app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig, - //) - - app.GovKeeper = *govKeeper.SetHooks( - govtypes.NewMultiGovHooks( - // register the governance hooks - ), - ) - /**** Module Options ****/ // Sets the version setter for the upgrade module @@ -282,7 +256,6 @@ func NewSimApp( // must be passed by reference here. if err := app.RegisterModules( crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants), - gov.NewAppModule(app.appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), ); err != nil { panic(err) } @@ -438,6 +411,5 @@ func GetMaccPerms() map[string][]string { // initParamsKeeper init params keeper and its subspaces func initParamsKeeper(paramsKeeper paramskeeper.Keeper) { - paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable()) paramsKeeper.Subspace(crisistypes.ModuleName) } diff --git a/simapp/app.yaml b/simapp/app.yaml index 277b236d006b..1a2700c516a8 100644 --- a/simapp/app.yaml +++ b/simapp/app.yaml @@ -141,3 +141,7 @@ modules: - name: upgrade config: "@type": cosmos.upgrade.module.v1.Module + + - name: gov + config: + "@type": cosmos.gov.module.v1.Module \ No newline at end of file diff --git a/x/distribution/module.go b/x/distribution/module.go index d80042294a5c..bb70f2571d57 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -241,7 +241,7 @@ type distrOutputs struct { DistrKeeper keeper.Keeper Module runtime.AppModuleWrapper Hooks staking.StakingHooksWrapper - GovHandler govv1beta1.RoutedHandler + GovHandler govv1beta1.HandlerRoute } func provideModule(in distrInputs) distrOutputs { @@ -252,6 +252,6 @@ func provideModule(in distrInputs) distrOutputs { DistrKeeper: k, Module: runtime.WrapAppModule(m), Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, - GovHandler: govv1beta1.RoutedHandler{Handler: NewCommunityPoolSpendProposalHandler(k), RouteKey: types.RouterKey}, + GovHandler: govv1beta1.HandlerRoute{Handler: NewCommunityPoolSpendProposalHandler(k), RouteKey: types.RouterKey}, } } diff --git a/x/gov/module.go b/x/gov/module.go index d06099db8518..e9e5e6bbe871 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -9,9 +9,14 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/runtime" store "github.com/cosmos/cosmos-sdk/store/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "golang.org/x/exp/maps" + "golang.org/x/exp/slices" "math/rand" + "sort" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -136,14 +141,22 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, func init() { appmodule.Register( &modulev1.Module{}, - appmodule.Provide(provideModuleBasic, provideModule), - appmodule.Invoke(invokeAddRoutes)) + appmodule.Provide(provideModuleBasic, provideKeyTable, provideModule), + appmodule.Invoke(invokeAddRoutes, invokeSetHooks)) } func provideModuleBasic() runtime.AppModuleBasicWrapper { return runtime.WrapAppModuleBasic(AppModuleBasic{}) } +func provideKeyTable(k depinject.ModuleKey) *paramtypes.KeyTable { + if k.Name() == "gov" { + return nil + } + kt := v1.ParamKeyTable() + return &kt +} + func provideModule( config *modulev1.Module, cdc codec.Codec, @@ -152,7 +165,7 @@ func provideModule( msgServiceRouter *baseapp.MsgServiceRouter, ak types.AccountKeeper, bk types.BankKeeper, - sk types.StakingKeeper) (runtime.AppModuleWrapper, v1beta1.RoutedHandler) { + sk types.StakingKeeper) (runtime.AppModuleWrapper, keeper.Keeper, v1beta1.HandlerRoute) { kConfig := types.DefaultConfig() if config.MaxMetadataLen != 0 { @@ -161,12 +174,22 @@ func provideModule( k := keeper.NewKeeper(cdc, key, subSpace, ak, bk, sk, msgServiceRouter, kConfig) m := NewAppModule(cdc, k, ak, bk) - return runtime.WrapAppModule(m), v1beta1.RoutedHandler{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey} + hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey} + + return runtime.WrapAppModule(m), k, hr } -func invokeAddRoutes( - keeper keeper.Keeper, - routes []v1beta1.RoutedHandler) { +func invokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { + if keeper == nil || routes == nil { + return + } + + // Default route order is a lexical sort by RouteKey. + // TODO order by configuration + slices.SortFunc(routes, func(x, y v1beta1.HandlerRoute) bool { + return x.RouteKey < y.RouteKey + }) + router := v1beta1.NewRouter() for _, r := range routes { router.AddRoute(r.RouteKey, r.Handler) @@ -174,6 +197,30 @@ func invokeAddRoutes( keeper.SetLegacyRouter(router) } +func invokeSetHooks(keeper *keeper.Keeper, govHooks map[string]types.GovHooksWrapper) error { + if keeper == nil || govHooks == nil { + return nil + } + + // Default ordering + // TODO supply ordering by configuration + modNames := maps.Keys(govHooks) + order := modNames + sort.Strings(order) + + var multiHooks types.MultiGovHooks + for _, modName := range order { + hook, ok := govHooks[modName] + if !ok { + return fmt.Errorf("can't find staking hooks for module %s", modName) + } + multiHooks = append(multiHooks, hook) + } + + keeper.SetHooks(multiHooks) + return nil +} + // Name returns the gov module's name. func (AppModule) Name() string { return types.ModuleName diff --git a/x/gov/types/expected_keepers.go b/x/gov/types/expected_keepers.go index 556387c62ac4..70f0e5fbe994 100644 --- a/x/gov/types/expected_keepers.go +++ b/x/gov/types/expected_keepers.go @@ -62,3 +62,8 @@ type GovHooks interface { AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) // Must be called when proposal fails to reach min deposit AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) // Must be called when proposal's finishes it's voting period } + +type GovHooksWrapper struct{ GovHooks } + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (GovHooksWrapper) IsOnePerModuleType() {} diff --git a/x/gov/types/v1beta1/content.go b/x/gov/types/v1beta1/content.go index dcfeacffd939..64d4ef4f19ae 100644 --- a/x/gov/types/v1beta1/content.go +++ b/x/gov/types/v1beta1/content.go @@ -19,10 +19,10 @@ type Content interface { // governance process. type Handler func(ctx sdk.Context, content Content) error -type RoutedHandler struct { +type HandlerRoute struct { Handler Handler RouteKey string } -// ManyPerContainer implements the depinject.ManyPerContainer interface. -func (RoutedHandler) ManyPerContainer() {} +// IsManyPerContainerType implements the depinject.ManyPerContainerType interface. +func (HandlerRoute) IsManyPerContainerType() {} diff --git a/x/params/module.go b/x/params/module.go index 31d44045c28c..4fe637f97527 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -188,7 +188,7 @@ type paramsOutputs struct { ParamsKeeper keeper.Keeper BaseAppOption runtime.BaseAppOption Module runtime.AppModuleWrapper - GovHandler govv1beta1.RoutedHandler + GovHandler govv1beta1.HandlerRoute } func provideModule(in paramsInputs) paramsOutputs { @@ -197,11 +197,26 @@ func provideModule(in paramsInputs) paramsOutputs { app.SetParamStore(k.Subspace(baseapp.Paramspace).WithKeyTable(types.ConsensusParamsKeyTable())) } m := runtime.WrapAppModule(NewAppModule(k)) - govHandler := govv1beta1.RoutedHandler{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)} + govHandler := govv1beta1.HandlerRoute{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)} return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m, GovHandler: govHandler} } -func provideSubSpace(key depinject.ModuleKey, k keeper.Keeper) types.Subspace { - return k.Subspace(key.Name()) +// TODO +// optional paramtypes.KeyTable? +type subSpaceInputs struct { + depinject.In + + Key depinject.ModuleKey + Keeper keeper.Keeper + KeyTable *types.KeyTable `optional:"true"` +} + +func provideSubSpace(in subSpaceInputs) types.Subspace { + if in.KeyTable == nil { + return in.Keeper.Subspace(in.Key.Name()) + } else { + kt := *in.KeyTable + return in.Keeper.Subspace(in.Key.Name()).WithKeyTable(kt) + } } diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 471c50dc7409..74df9aafa733 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -3,6 +3,7 @@ package upgrade import ( "context" "encoding/json" + govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cast" @@ -183,6 +184,7 @@ type upgradeOutputs struct { UpgradeKeeper keeper.Keeper Module runtime.AppModuleWrapper + GovHandler govv1beta1.HandlerRoute } func provideModule(in upgradeInputs) upgradeOutputs { @@ -202,6 +204,7 @@ func provideModule(in upgradeInputs) upgradeOutputs { // set the governance module account as the authority for conducting upgrades k := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, homePath, nil, authtypes.NewModuleAddress(govtypes.ModuleName).String()) m := NewAppModule(k) + gh := govv1beta1.HandlerRoute{RouteKey: types.RouterKey, Handler: NewSoftwareUpgradeProposalHandler(k)} - return upgradeOutputs{UpgradeKeeper: k, Module: runtime.WrapAppModule(m)} + return upgradeOutputs{UpgradeKeeper: k, Module: runtime.WrapAppModule(m), GovHandler: gh} } From 96483083e0242a5777af5fb3154ae881f13b0c76 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 29 Jun 2022 20:31:49 -0500 Subject: [PATCH 05/12] refactor usages of gov keeper to pointer - post construction mutation is required to overcome circular dependencies --- simapp/app.go | 2 +- x/gov/abci.go | 2 +- x/gov/genesis.go | 4 ++-- x/gov/keeper/grpc_query.go | 4 ++-- x/gov/keeper/hooks_test.go | 2 +- x/gov/keeper/invariants.go | 6 +++--- x/gov/keeper/keeper.go | 4 ++-- x/gov/keeper/migrations.go | 4 ++-- x/gov/keeper/msg_server.go | 4 ++-- x/gov/keeper/querier.go | 18 +++++++++--------- x/gov/module.go | 29 ++++++++++++++--------------- x/gov/simulation/operations.go | 31 +++++++++---------------------- x/params/module.go | 11 +++++++++++ 13 files changed, 59 insertions(+), 62 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 0a2dde7c2602..e5526b70877c 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -165,7 +165,7 @@ type SimApp struct { SlashingKeeper slashingkeeper.Keeper MintKeeper mintkeeper.Keeper DistrKeeper distrkeeper.Keeper - GovKeeper govkeeper.Keeper + GovKeeper *govkeeper.Keeper CrisisKeeper crisiskeeper.Keeper UpgradeKeeper upgradekeeper.Keeper ParamsKeeper paramskeeper.Keeper diff --git a/x/gov/abci.go b/x/gov/abci.go index b7120537564a..3019404c7520 100644 --- a/x/gov/abci.go +++ b/x/gov/abci.go @@ -12,7 +12,7 @@ import ( ) // EndBlocker called every block, process inflation, update validator set. -func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) { +func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) logger := keeper.Logger(ctx) diff --git a/x/gov/genesis.go b/x/gov/genesis.go index b332cb293afe..53f2a1fa7ebd 100644 --- a/x/gov/genesis.go +++ b/x/gov/genesis.go @@ -10,7 +10,7 @@ import ( ) // InitGenesis - store genesis parameters -func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, data *v1.GenesisState) { +func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, data *v1.GenesisState) { k.SetProposalID(ctx, data.StartingProposalId) k.SetDepositParams(ctx, *data.DepositParams) k.SetVotingParams(ctx, *data.VotingParams) @@ -55,7 +55,7 @@ func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k } // ExportGenesis - output genesis parameters -func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *v1.GenesisState { +func ExportGenesis(ctx sdk.Context, k *keeper.Keeper) *v1.GenesisState { startingProposalID, _ := k.GetProposalID(ctx) depositParams := k.GetDepositParams(ctx) votingParams := k.GetVotingParams(ctx) diff --git a/x/gov/keeper/grpc_query.go b/x/gov/keeper/grpc_query.go index 3ae97a3d96a1..b2e8f17f8c75 100644 --- a/x/gov/keeper/grpc_query.go +++ b/x/gov/keeper/grpc_query.go @@ -280,10 +280,10 @@ func (q Keeper) TallyResult(c context.Context, req *v1.QueryTallyResultRequest) var _ v1beta1.QueryServer = legacyQueryServer{} type legacyQueryServer struct { - keeper Keeper + keeper *Keeper } -func NewLegacyQueryServer(k Keeper) v1beta1.QueryServer { +func NewLegacyQueryServer(k *Keeper) v1beta1.QueryServer { return &legacyQueryServer{keeper: k} } diff --git a/x/gov/keeper/hooks_test.go b/x/gov/keeper/hooks_test.go index 19be117333ba..81ac2bbaf6e1 100644 --- a/x/gov/keeper/hooks_test.go +++ b/x/gov/keeper/hooks_test.go @@ -56,7 +56,7 @@ func TestHooks(t *testing.T) { govHooksReceiver := MockGovHooksReceiver{} keeper.UnsafeSetHooks( - &app.GovKeeper, types.NewMultiGovHooks(&govHooksReceiver), + app.GovKeeper, types.NewMultiGovHooks(&govHooksReceiver), ) require.False(t, govHooksReceiver.AfterProposalSubmissionValid) diff --git a/x/gov/keeper/invariants.go b/x/gov/keeper/invariants.go index 6e0f9d4e0a51..0cfcdf32e49f 100644 --- a/x/gov/keeper/invariants.go +++ b/x/gov/keeper/invariants.go @@ -11,12 +11,12 @@ import ( ) // RegisterInvariants registers all governance invariants -func RegisterInvariants(ir sdk.InvariantRegistry, keeper Keeper, bk types.BankKeeper) { +func RegisterInvariants(ir sdk.InvariantRegistry, keeper *Keeper, bk types.BankKeeper) { ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(keeper, bk)) } // AllInvariants runs all invariants of the governance module -func AllInvariants(keeper Keeper, bk types.BankKeeper) sdk.Invariant { +func AllInvariants(keeper *Keeper, bk types.BankKeeper) sdk.Invariant { return func(ctx sdk.Context) (string, bool) { return ModuleAccountInvariant(keeper, bk)(ctx) } @@ -24,7 +24,7 @@ func AllInvariants(keeper Keeper, bk types.BankKeeper) sdk.Invariant { // ModuleAccountInvariant checks that the module account coins reflects the sum of // deposit amounts held on store -func ModuleAccountInvariant(keeper Keeper, bk types.BankKeeper) sdk.Invariant { +func ModuleAccountInvariant(keeper *Keeper, bk types.BankKeeper) sdk.Invariant { return func(ctx sdk.Context) (string, bool) { var expectedDeposits sdk.Coins diff --git a/x/gov/keeper/keeper.go b/x/gov/keeper/keeper.go index 2134b2c81527..514ec2b96936 100644 --- a/x/gov/keeper/keeper.go +++ b/x/gov/keeper/keeper.go @@ -56,7 +56,7 @@ func NewKeeper( cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace types.ParamSubspace, authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper, router *baseapp.MsgServiceRouter, config types.Config, -) Keeper { +) *Keeper { // ensure governance module account is set if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil { panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) @@ -67,7 +67,7 @@ func NewKeeper( config.MaxMetadataLen = types.DefaultConfig().MaxMetadataLen } - return Keeper{ + return &Keeper{ storeKey: key, paramSpace: paramSpace, authKeeper: authKeeper, diff --git a/x/gov/keeper/migrations.go b/x/gov/keeper/migrations.go index fbcbfc55852c..6131d5601f4b 100644 --- a/x/gov/keeper/migrations.go +++ b/x/gov/keeper/migrations.go @@ -8,11 +8,11 @@ import ( // Migrator is a struct for handling in-place store migrations. type Migrator struct { - keeper Keeper + keeper *Keeper } // NewMigrator returns a new Migrator. -func NewMigrator(keeper Keeper) Migrator { +func NewMigrator(keeper *Keeper) Migrator { return Migrator{keeper: keeper} } diff --git a/x/gov/keeper/msg_server.go b/x/gov/keeper/msg_server.go index da5992afb0c1..3982451dd0f7 100644 --- a/x/gov/keeper/msg_server.go +++ b/x/gov/keeper/msg_server.go @@ -17,12 +17,12 @@ import ( ) type msgServer struct { - Keeper + *Keeper } // NewMsgServerImpl returns an implementation of the gov MsgServer interface // for the provided Keeper. -func NewMsgServerImpl(keeper Keeper) v1.MsgServer { +func NewMsgServerImpl(keeper *Keeper) v1.MsgServer { return &msgServer{Keeper: keeper} } diff --git a/x/gov/keeper/querier.go b/x/gov/keeper/querier.go index d528f33cf7ec..e5aacbfb4464 100644 --- a/x/gov/keeper/querier.go +++ b/x/gov/keeper/querier.go @@ -14,7 +14,7 @@ import ( ) // NewQuerier creates a new gov Querier instance -func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { +func NewQuerier(keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { switch path[0] { case v1.QueryParams: @@ -47,7 +47,7 @@ func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier } } -func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { switch path[0] { case v1.ParamDeposit: bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetDepositParams(ctx)) @@ -75,7 +75,7 @@ func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper K } } -func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryProposalParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -95,7 +95,7 @@ func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper return bz, nil } -func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryDepositParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -111,7 +111,7 @@ func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper return bz, nil } -func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryVoteParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -127,7 +127,7 @@ func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Kee return bz, nil } -func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryProposalParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -147,7 +147,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper return bz, nil } -func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryProposalParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -183,7 +183,7 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke return bz, nil } -func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryProposalVotesParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { @@ -210,7 +210,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke return bz, nil } -func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { +func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper *Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) { var params v1.QueryProposalsParams err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms) if err != nil { diff --git a/x/gov/module.go b/x/gov/module.go index e9e5e6bbe871..c78453c0d052 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -9,10 +9,11 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/runtime" store "github.com/cosmos/cosmos-sdk/store/types" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client" + paramsclient "github.com/cosmos/cosmos-sdk/x/params/client" + upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" "golang.org/x/exp/maps" "golang.org/x/exp/slices" "math/rand" @@ -123,13 +124,13 @@ func (a AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry type AppModule struct { AppModuleBasic - keeper keeper.Keeper + keeper *keeper.Keeper accountKeeper types.AccountKeeper bankKeeper types.BankKeeper } // NewAppModule creates a new AppModule object -func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { +func NewAppModule(cdc codec.Codec, keeper *keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, @@ -141,20 +142,18 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, func init() { appmodule.Register( &modulev1.Module{}, - appmodule.Provide(provideModuleBasic, provideKeyTable, provideModule), + appmodule.Provide(provideModuleBasic, provideModule), appmodule.Invoke(invokeAddRoutes, invokeSetHooks)) } func provideModuleBasic() runtime.AppModuleBasicWrapper { - return runtime.WrapAppModuleBasic(AppModuleBasic{}) -} - -func provideKeyTable(k depinject.ModuleKey) *paramtypes.KeyTable { - if k.Name() == "gov" { - return nil - } - kt := v1.ParamKeyTable() - return &kt + return runtime.WrapAppModuleBasic(NewAppModuleBasic( + []govclient.ProposalHandler{ + paramsclient.ProposalHandler, + distrclient.ProposalHandler, + upgradeclient.LegacyProposalHandler, + upgradeclient.LegacyCancelProposalHandler}, + )) } func provideModule( @@ -165,7 +164,7 @@ func provideModule( msgServiceRouter *baseapp.MsgServiceRouter, ak types.AccountKeeper, bk types.BankKeeper, - sk types.StakingKeeper) (runtime.AppModuleWrapper, keeper.Keeper, v1beta1.HandlerRoute) { + sk types.StakingKeeper) (runtime.AppModuleWrapper, *keeper.Keeper, v1beta1.HandlerRoute) { kConfig := types.DefaultConfig() if config.MaxMetadataLen != 0 { diff --git a/x/gov/simulation/operations.go b/x/gov/simulation/operations.go index 591eb47abc00..eb928dc5b51e 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -36,10 +36,7 @@ const ( ) // WeightedOperations returns all the operations from the module with their respective weights -func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper, - bk types.BankKeeper, k keeper.Keeper, wContents []simtypes.WeightedProposalContent, -) simulation.WeightedOperations { +func WeightedOperations(appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, wContents []simtypes.WeightedProposalContent) simulation.WeightedOperations { var ( weightMsgDeposit int weightMsgVote int @@ -103,9 +100,7 @@ func WeightedOperations( // SimulateMsgSubmitProposal simulates creating a msg Submit Proposal // voting on the proposal, and subsequently slashing the proposal. It is implemented using // future operations. -func SimulateMsgSubmitProposal( - ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, contentSim simtypes.ContentSimulatorFn, -) simtypes.Operation { +func SimulateMsgSubmitProposal(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, contentSim simtypes.ContentSimulatorFn) simtypes.Operation { // The states are: // column 1: All validators vote // column 2: 90% vote @@ -223,7 +218,7 @@ func SimulateMsgSubmitProposal( } // SimulateMsgDeposit generates a MsgDeposit with random values. -func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -273,13 +268,11 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke } // SimulateMsgVote generates a MsgVote with random values. -func SimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation { return operationSimulateMsgVote(ak, bk, k, simtypes.Account{}, -1) } -func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, - simAccount simtypes.Account, proposalIDInt int64, -) simtypes.Operation { +func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, simAccount simtypes.Account, proposalIDInt int64) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -327,13 +320,11 @@ func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k kee } // SimulateMsgVoteWeighted generates a MsgVoteWeighted with random values. -func SimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation { return operationSimulateMsgVoteWeighted(ak, bk, k, simtypes.Account{}, -1) } -func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, - simAccount simtypes.Account, proposalIDInt int64, -) simtypes.Operation { +func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, simAccount simtypes.Account, proposalIDInt int64) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -384,9 +375,7 @@ func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeepe // deposit amount between (0, min(balance, minDepositAmount)) // This is to simulate multiple users depositing to get the // proposal above the minimum deposit amount -func randomDeposit(r *rand.Rand, ctx sdk.Context, - ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, addr sdk.AccAddress, -) (deposit sdk.Coins, skip bool, err error) { +func randomDeposit(r *rand.Rand, ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, addr sdk.AccAddress) (deposit sdk.Coins, skip bool, err error) { account := ak.GetAccount(ctx, addr) spendable := bk.SpendableCoins(ctx, account.GetAddress()) @@ -420,9 +409,7 @@ func randomDeposit(r *rand.Rand, ctx sdk.Context, // (defined in gov GenesisState) and the latest proposal ID // that matches a given Status. // It does not provide a default ID. -func randomProposalID(r *rand.Rand, k keeper.Keeper, - ctx sdk.Context, status v1.ProposalStatus, -) (proposalID uint64, found bool) { +func randomProposalID(r *rand.Rand, k *keeper.Keeper, ctx sdk.Context, status v1.ProposalStatus) (proposalID uint64, found bool) { proposalID, _ = k.GetProposalID(ctx) switch { diff --git a/x/params/module.go b/x/params/module.go index 4fe637f97527..fad902b9325f 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,6 +3,7 @@ package params import ( "context" "encoding/json" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" @@ -166,6 +167,7 @@ func init() { provideModuleBasic, provideModule, provideSubSpace, + provideKeyTable, )) } @@ -212,6 +214,15 @@ type subSpaceInputs struct { KeyTable *types.KeyTable `optional:"true"` } +func provideKeyTable(k depinject.ModuleKey) *types.KeyTable { + // TODO there must be a better way? + if k.Name() != "gov" { + return nil + } + kt := v1.ParamKeyTable() + return &kt +} + func provideSubSpace(in subSpaceInputs) types.Subspace { if in.KeyTable == nil { return in.Keeper.Subspace(in.Key.Name()) From 638a31be55bbeebd5c62bf123940857412caa908 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 10:09:24 -0500 Subject: [PATCH 06/12] Add gov module to golang app config --- simapp/app_config.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/simapp/app_config.go b/simapp/app_config.go index 504dd84a5259..3aa53ec44e0e 100644 --- a/simapp/app_config.go +++ b/simapp/app_config.go @@ -35,6 +35,7 @@ import ( evidencemodulev1 "cosmossdk.io/api/cosmos/evidence/module/v1" feegrantmodulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1" genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1" + govmodulev1 "cosmossdk.io/api/cosmos/gov/module/v1" groupmodulev1 "cosmossdk.io/api/cosmos/group/module/v1" mintmodulev1 "cosmossdk.io/api/cosmos/mint/module/v1" nftmodulev1 "cosmossdk.io/api/cosmos/nft/module/v1" @@ -190,6 +191,10 @@ var AppConfig = appconfig.Compose(&appv1alpha1.Config{ Name: feegrant.ModuleName, Config: appconfig.WrapAny(&feegrantmodulev1.Module{}), }, + { + Name: govtypes.ModuleName, + Config: appconfig.WrapAny(&govmodulev1.Module{}), + }, }, }) From 16bf8a071771112cf9e5cc50de6364969dd94e59 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 10:51:48 -0500 Subject: [PATCH 07/12] Remove unneeded dependencies --- x/gov/module.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/x/gov/module.go b/x/gov/module.go index c78453c0d052..ebfb6e7d59bc 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -11,9 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/runtime" store "github.com/cosmos/cosmos-sdk/store/types" - distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client" - paramsclient "github.com/cosmos/cosmos-sdk/x/params/client" - upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" "golang.org/x/exp/maps" "golang.org/x/exp/slices" "math/rand" @@ -147,13 +144,7 @@ func init() { } func provideModuleBasic() runtime.AppModuleBasicWrapper { - return runtime.WrapAppModuleBasic(NewAppModuleBasic( - []govclient.ProposalHandler{ - paramsclient.ProposalHandler, - distrclient.ProposalHandler, - upgradeclient.LegacyProposalHandler, - upgradeclient.LegacyCancelProposalHandler}, - )) + return runtime.WrapAppModuleBasic(AppModuleBasic{}) } func provideModule( From 4c79c5e6299d3a79781aa1e80e90233d697affb5 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 18:09:12 -0500 Subject: [PATCH 08/12] remove initParamsKeeper from simapp/app.go --- simapp/app.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index fc6ebd7512a3..dd3f5b0c2b33 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -230,8 +230,6 @@ func NewSimApp( tmos.Exit(err.Error()) } - initParamsKeeper(app.ParamsKeeper) - /**** Module Options ****/ // Sets the version setter for the upgrade module @@ -385,7 +383,3 @@ func GetMaccPerms() map[string][]string { } return dupMaccPerms } - -// initParamsKeeper init params keeper and its subspaces -func initParamsKeeper(paramsKeeper paramskeeper.Keeper) { -} From 8ade79733e06b98139446ba04341cbc7f1ad5f59 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 18:25:05 -0500 Subject: [PATCH 09/12] Changelog entry & refactor module input/output --- CHANGELOG.md | 1 + x/gov/module.go | 47 ++++++++++++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d89a1597fc..37e7732b74e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/gov) [#12368](https://github.com/cosmos/cosmos-sdk/pull/12369) Gov keeper is now passed by reference instead of copy to make post-construction mutation of Hooks and Proposal Handlers possible at a framework level. * (simapp) [#12270](https://github.com/cosmos/cosmos-sdk/pull/12270) Remove `invCheckPeriod uint` attribute from `SimApp` struct as per migration of `x/crisis` to app wiring * (simapp) [#12334](https://github.com/cosmos/cosmos-sdk/pull/12334) Move `simapp.ConvertAddrsToValAddrs` and `simapp.CreateTestPubKeys ` to respectively `simtestutil.ConvertAddrsToValAddrs` and `simtestutil.CreateTestPubKeys` (`testutil/sims`) * (simapp) [#12312](https://github.com/cosmos/cosmos-sdk/pull/12312) Move `simapp.EmptyAppOptions` to `simtestutil.EmptyAppOptions` (`testutil/sims`) diff --git a/x/gov/module.go b/x/gov/module.go index ebfb6e7d59bc..69e69f846a75 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/runtime" store "github.com/cosmos/cosmos-sdk/store/types" "golang.org/x/exp/maps" @@ -147,26 +148,38 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper { return runtime.WrapAppModuleBasic(AppModuleBasic{}) } -func provideModule( - config *modulev1.Module, - cdc codec.Codec, - key *store.KVStoreKey, - subSpace types.ParamSubspace, - msgServiceRouter *baseapp.MsgServiceRouter, - ak types.AccountKeeper, - bk types.BankKeeper, - sk types.StakingKeeper) (runtime.AppModuleWrapper, *keeper.Keeper, v1beta1.HandlerRoute) { +type govInputs struct { + depinject.In + Config *modulev1.Module + Cdc codec.Codec + Key *store.KVStoreKey + Subspace types.ParamSubspace + MsgServiceRouter *baseapp.MsgServiceRouter + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + StakingKeeper types.StakingKeeper +} + +type govOutputs struct { + depinject.Out + + Module runtime.AppModuleWrapper + Keeper *keeper.Keeper + HandlerRoute v1beta1.HandlerRoute +} + +func provideModule(in govInputs) govOutputs { kConfig := types.DefaultConfig() - if config.MaxMetadataLen != 0 { - kConfig.MaxMetadataLen = config.MaxMetadataLen + if in.Config.MaxMetadataLen != 0 { + kConfig.MaxMetadataLen = in.Config.MaxMetadataLen } - k := keeper.NewKeeper(cdc, key, subSpace, ak, bk, sk, msgServiceRouter, kConfig) - m := NewAppModule(cdc, k, ak, bk) + k := keeper.NewKeeper(in.Cdc, in.Key, in.Subspace, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.MsgServiceRouter, kConfig) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper) hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: types.RouterKey} - return runtime.WrapAppModule(m), k, hr + return govOutputs{Module: runtime.WrapAppModule(m), Keeper: k, HandlerRoute: hr} } func invokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { @@ -175,7 +188,7 @@ func invokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { } // Default route order is a lexical sort by RouteKey. - // TODO order by configuration + // Explicit ordering can be added to the module config if required. slices.SortFunc(routes, func(x, y v1beta1.HandlerRoute) bool { return x.RouteKey < y.RouteKey }) @@ -192,8 +205,8 @@ func invokeSetHooks(keeper *keeper.Keeper, govHooks map[string]types.GovHooksWra return nil } - // Default ordering - // TODO supply ordering by configuration + // Default ordering is lexical by module name. + // Explicit ordering can be added to the module config if required. modNames := maps.Keys(govHooks) order := modNames sort.Strings(order) From 924d49ba99ddcd62fa847c96c7e94810a734e5fc Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 18:38:54 -0500 Subject: [PATCH 10/12] Move provideKeyTable back to x/gov --- x/gov/module.go | 11 ++++++++++- x/params/module.go | 11 ----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/x/gov/module.go b/x/gov/module.go index 69e69f846a75..dfee2099bb40 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/runtime" store "github.com/cosmos/cosmos-sdk/store/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "golang.org/x/exp/maps" "golang.org/x/exp/slices" "math/rand" @@ -140,7 +141,7 @@ func NewAppModule(cdc codec.Codec, keeper *keeper.Keeper, ak types.AccountKeeper func init() { appmodule.Register( &modulev1.Module{}, - appmodule.Provide(provideModuleBasic, provideModule), + appmodule.Provide(provideModuleBasic, provideModule, provideKeyTable), appmodule.Invoke(invokeAddRoutes, invokeSetHooks)) } @@ -182,6 +183,14 @@ func provideModule(in govInputs) govOutputs { return govOutputs{Module: runtime.WrapAppModule(m), Keeper: k, HandlerRoute: hr} } +func provideKeyTable(k depinject.ModuleKey) *paramtypes.KeyTable { + if k.Name() != types.ModuleName { + return nil + } + kt := v1.ParamKeyTable() + return &kt +} + func invokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { if keeper == nil || routes == nil { return diff --git a/x/params/module.go b/x/params/module.go index fad902b9325f..4fe637f97527 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,7 +3,6 @@ package params import ( "context" "encoding/json" - v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" @@ -167,7 +166,6 @@ func init() { provideModuleBasic, provideModule, provideSubSpace, - provideKeyTable, )) } @@ -214,15 +212,6 @@ type subSpaceInputs struct { KeyTable *types.KeyTable `optional:"true"` } -func provideKeyTable(k depinject.ModuleKey) *types.KeyTable { - // TODO there must be a better way? - if k.Name() != "gov" { - return nil - } - kt := v1.ParamKeyTable() - return &kt -} - func provideSubSpace(in subSpaceInputs) types.Subspace { if in.KeyTable == nil { return in.Keeper.Subspace(in.Key.Name()) From 830f48f46a571ee94bd866d022ad2061a0e3a3e9 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 19:05:21 -0500 Subject: [PATCH 11/12] better impl of keyTable provider --- x/gov/module.go | 8 ++------ x/params/module.go | 27 +++++++++++++++++---------- x/params/types/table.go | 3 +++ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/x/gov/module.go b/x/gov/module.go index dfee2099bb40..edef90247130 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -183,12 +183,8 @@ func provideModule(in govInputs) govOutputs { return govOutputs{Module: runtime.WrapAppModule(m), Keeper: k, HandlerRoute: hr} } -func provideKeyTable(k depinject.ModuleKey) *paramtypes.KeyTable { - if k.Name() != types.ModuleName { - return nil - } - kt := v1.ParamKeyTable() - return &kt +func provideKeyTable() paramtypes.KeyTable { + return v1.ParamKeyTable() } func invokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { diff --git a/x/params/module.go b/x/params/module.go index 4fe637f97527..daa69de61e5f 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,6 +3,7 @@ package params import ( "context" "encoding/json" + "fmt" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" @@ -202,21 +203,27 @@ func provideModule(in paramsInputs) paramsOutputs { return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m, GovHandler: govHandler} } -// TODO -// optional paramtypes.KeyTable? type subSpaceInputs struct { depinject.In - Key depinject.ModuleKey - Keeper keeper.Keeper - KeyTable *types.KeyTable `optional:"true"` + Key depinject.ModuleKey + Keeper keeper.Keeper + KeyTables map[string]types.KeyTable } -func provideSubSpace(in subSpaceInputs) types.Subspace { - if in.KeyTable == nil { - return in.Keeper.Subspace(in.Key.Name()) +// TODO pull up to configuration +var keyTableSubspacesConfig = map[string]string{"gov": "gov"} + +func provideSubSpace(in subSpaceInputs) (types.Subspace, error) { + moduleName := in.Key.Name() + var _, exists = keyTableSubspacesConfig[moduleName] + if !exists { + return in.Keeper.Subspace(moduleName), nil } else { - kt := *in.KeyTable - return in.Keeper.Subspace(in.Key.Name()).WithKeyTable(kt) + var kt, ok = in.KeyTables[moduleName] + if !ok { + return *new(types.Subspace), fmt.Errorf("a KeyTable is required for module %s but no provider was found", moduleName) + } + return in.Keeper.Subspace(moduleName).WithKeyTable(kt), nil } } diff --git a/x/params/types/table.go b/x/params/types/table.go index ef09c39d886e..9daf24888b7d 100644 --- a/x/params/types/table.go +++ b/x/params/types/table.go @@ -16,6 +16,9 @@ type KeyTable struct { m map[string]attribute } +// IsOnePerModuleType implements depinject.OnePerModuleType +func (KeyTable) IsOnePerModuleType() {} + func NewKeyTable(pairs ...ParamSetPair) KeyTable { keyTable := KeyTable{ m: make(map[string]attribute), From 2453e17ad7495e0ea426038eda39abb99322e5ad Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 30 Jun 2022 19:12:34 -0500 Subject: [PATCH 12/12] clean up subspace provider --- x/params/module.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/x/params/module.go b/x/params/module.go index daa69de61e5f..ef64bcbf93e2 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,7 +3,6 @@ package params import ( "context" "encoding/json" - "fmt" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" "math/rand" @@ -166,7 +165,7 @@ func init() { appmodule.Provide( provideModuleBasic, provideModule, - provideSubSpace, + provideSubspace, )) } @@ -203,7 +202,7 @@ func provideModule(in paramsInputs) paramsOutputs { return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m, GovHandler: govHandler} } -type subSpaceInputs struct { +type subspaceInputs struct { depinject.In Key depinject.ModuleKey @@ -211,19 +210,12 @@ type subSpaceInputs struct { KeyTables map[string]types.KeyTable } -// TODO pull up to configuration -var keyTableSubspacesConfig = map[string]string{"gov": "gov"} - -func provideSubSpace(in subSpaceInputs) (types.Subspace, error) { +func provideSubspace(in subspaceInputs) types.Subspace { moduleName := in.Key.Name() - var _, exists = keyTableSubspacesConfig[moduleName] + var kt, exists = in.KeyTables[moduleName] if !exists { - return in.Keeper.Subspace(moduleName), nil + return in.Keeper.Subspace(moduleName) } else { - var kt, ok = in.KeyTables[moduleName] - if !ok { - return *new(types.Subspace), fmt.Errorf("a KeyTable is required for module %s but no provider was found", moduleName) - } - return in.Keeper.Subspace(moduleName).WithKeyTable(kt), nil + return in.Keeper.Subspace(moduleName).WithKeyTable(kt) } }