forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
307 lines (266 loc) · 8.43 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package runtime
import (
"fmt"
"os"
"slices"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/msgservice"
"github.com/cosmos/cosmos-sdk/x/genutil"
)
// appModule defines runtime as an AppModule
type appModule struct {
app *App
}
func (m appModule) IsOnePerModuleType() {}
func (m appModule) IsAppModule() {}
func (m appModule) RegisterServices(configurator module.Configurator) { //nolint:staticcheck // SA1019: Configurator is deprecated but still used in runtime v1.
err := m.app.registerRuntimeServices(configurator)
if err != nil {
panic(err)
}
}
func (m appModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{
"autocli": {
Service: autocliv1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "AppOptions",
Short: "Query the custom autocli options",
},
},
},
"reflection": {
Service: reflectionv1.ReflectionService_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "FileDescriptors",
Short: "Query the app's protobuf file descriptors",
},
},
},
},
},
}
}
var (
_ appmodule.AppModule = appModule{}
_ module.HasServices = appModule{}
)
// BaseAppOption is a depinject.AutoGroupType which can be used to pass
// BaseApp options into the depinject. It should be used carefully.
type BaseAppOption func(*baseapp.BaseApp)
// IsManyPerContainerType indicates that this is a depinject.ManyPerContainerType.
func (b BaseAppOption) IsManyPerContainerType() {}
func init() {
appconfig.RegisterModule(&runtimev1alpha1.Module{},
appconfig.Provide(
ProvideApp,
// to decouple runtime from sdk/codec ProvideInterfaceReistry can be registered from the app
// i.e. in the call to depinject.Inject(...)
codec.ProvideInterfaceRegistry,
codec.ProvideLegacyAmino,
codec.ProvideProtoCodec,
codec.ProvideAddressCodec,
ProvideKVStoreKey,
ProvideKVStoreFactory,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
ProvideGenesisTxHandler,
ProvideEnvironment,
ProvideTransientStoreService,
ProvideModuleManager,
ProvideCometService,
ProvideModuleConfigMaps,
ProvideModuleScopedConfigMap,
),
appconfig.Invoke(SetupAppBuilder),
)
}
func ProvideApp(
interfaceRegistry codectypes.InterfaceRegistry,
amino registry.AminoRegistrar,
protoCodec *codec.ProtoCodec,
) (
*AppBuilder,
*baseapp.MsgServiceRouter,
*baseapp.GRPCQueryRouter,
appmodule.AppModule,
protodesc.Resolver,
protoregistry.MessageTypeResolver,
) {
protoFiles := proto.HybridResolver
protoTypes := protoregistry.GlobalTypes
// At startup, check that all proto annotations are correct.
if err := msgservice.ValidateProtoAnnotations(protoFiles); err != nil {
// Once we switch to using protoreflect-based ante handlers, we might
// want to panic here instead of logging a warning.
_, _ = fmt.Fprintln(os.Stderr, err.Error())
}
std.RegisterInterfaces(interfaceRegistry)
std.RegisterLegacyAminoCodec(amino)
msgServiceRouter := baseapp.NewMsgServiceRouter()
grpcQueryRouter := baseapp.NewGRPCQueryRouter()
app := &App{
storeKeys: nil,
interfaceRegistry: interfaceRegistry,
cdc: protoCodec,
amino: amino,
msgServiceRouter: msgServiceRouter,
grpcQueryRouter: grpcQueryRouter,
}
appBuilder := &AppBuilder{app: app}
return appBuilder, msgServiceRouter, grpcQueryRouter, appModule{app}, protoFiles, protoTypes
}
type AppInputs struct {
depinject.In
Logger log.Logger
Config *runtimev1alpha1.Module
AppBuilder *AppBuilder
ModuleManager *module.Manager
BaseAppOptions []BaseAppOption
InterfaceRegistry codectypes.InterfaceRegistry
LegacyAmino registry.AminoRegistrar
AppOptions server.DynamicConfig `optional:"true"` // can be nil in client wiring
}
func SetupAppBuilder(inputs AppInputs) {
app := inputs.AppBuilder.app
app.baseAppOptions = inputs.BaseAppOptions
app.config = inputs.Config
app.logger = inputs.Logger
app.ModuleManager = inputs.ModuleManager
app.ModuleManager.RegisterInterfaces(inputs.InterfaceRegistry)
app.ModuleManager.RegisterLegacyAminoCodec(inputs.LegacyAmino)
if inputs.AppOptions != nil {
inputs.AppBuilder.appOptions = inputs.AppOptions
}
}
func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) {
wrapper.app.storeKeys = append(wrapper.app.storeKeys, key)
}
func storeKeyOverride(config *runtimev1alpha1.Module, moduleName string) *runtimev1alpha1.StoreKeyConfig {
for _, cfg := range config.OverrideStoreKeys {
if cfg.ModuleName == moduleName {
return cfg
}
}
return nil
}
func ProvideKVStoreKey(
config *runtimev1alpha1.Module,
key depinject.ModuleKey,
app *AppBuilder,
) *storetypes.KVStoreKey {
if slices.Contains(config.SkipStoreKeys, key.Name()) {
return nil
}
override := storeKeyOverride(config, key.Name())
var storeKeyName string
if override != nil {
storeKeyName = override.KvStoreKey
} else {
storeKeyName = key.Name()
}
storeKey := storetypes.NewKVStoreKey(storeKeyName)
registerStoreKey(app, storeKey)
return storeKey
}
func ProvideTransientStoreKey(
config *runtimev1alpha1.Module,
key depinject.ModuleKey,
app *AppBuilder,
) *storetypes.TransientStoreKey {
if slices.Contains(config.SkipStoreKeys, key.Name()) {
return nil
}
storeKey := storetypes.NewTransientStoreKey(fmt.Sprintf("transient:%s", key.Name()))
registerStoreKey(app, storeKey)
return storeKey
}
func ProvideMemoryStoreKey(
config *runtimev1alpha1.Module,
key depinject.ModuleKey,
app *AppBuilder,
) *storetypes.MemoryStoreKey {
if slices.Contains(config.SkipStoreKeys, key.Name()) {
return nil
}
storeKey := storetypes.NewMemoryStoreKey(fmt.Sprintf("memory:%s", key.Name()))
registerStoreKey(app, storeKey)
return storeKey
}
func ProvideModuleManager(modules map[string]appmodule.AppModule) *module.Manager {
return module.NewManagerFromMap(modules)
}
func ProvideGenesisTxHandler(appBuilder *AppBuilder) genutil.TxHandler {
return appBuilder.app
}
func ProvideEnvironment(
logger log.Logger,
config *runtimev1alpha1.Module,
key depinject.ModuleKey,
app *AppBuilder,
msgServiceRouter *baseapp.MsgServiceRouter,
queryServiceRouter *baseapp.GRPCQueryRouter,
) (store.KVStoreService, store.MemoryStoreService, appmodule.Environment) {
var (
kvService store.KVStoreService = failingStoreService{}
memKvService store.MemoryStoreService = failingStoreService{}
)
// skips modules that have no store
if !slices.Contains(config.SkipStoreKeys, key.Name()) {
storeKey := ProvideKVStoreKey(config, key, app)
kvService = kvStoreService{key: storeKey}
memStoreKey := ProvideMemoryStoreKey(config, key, app)
memKvService = memStoreService{key: memStoreKey}
}
return kvService, memKvService, NewEnvironment(
kvService,
logger.With(log.ModuleKey, fmt.Sprintf("x/%s", key.Name())),
EnvWithMsgRouterService(msgServiceRouter),
EnvWithQueryRouterService(queryServiceRouter),
EnvWithMemStoreService(memKvService),
)
}
func ProvideTransientStoreService(
config *runtimev1alpha1.Module,
key depinject.ModuleKey,
app *AppBuilder,
) store.TransientStoreService {
storeKey := ProvideTransientStoreKey(config, key, app)
if storeKey == nil {
return failingStoreService{}
}
return transientStoreService{key: storeKey}
}
func ProvideCometService() comet.Service {
return NewContextAwareCometInfoService()
}
func ProvideKVStoreFactory(app *AppBuilder) store.KVStoreServiceFactory {
return func(key []byte) store.KVStoreService {
sk := storetypes.NewKVStoreKey(string(key))
registerStoreKey(app, sk)
return kvStoreService{key: sk}
}
}