-
Notifications
You must be signed in to change notification settings - Fork 298
/
module.go
361 lines (320 loc) · 12.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package module
import (
"encoding/json"
"fmt"
"sort"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
sdkmodule "github.com/cosmos/cosmos-sdk/types/module"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Manager defines a module manager that provides the high level utility for managing and executing
// operations for a group of modules. This implemention maps the state machine version to different
// versions of the module. It also provides a way to run migrations between different versions of a
// module.
type Manager struct {
versionedModules map[uint64]map[string]sdkmodule.AppModule
allModules []sdkmodule.AppModule
firstVersion uint64
lastVersion uint64
OrderInitGenesis []string
OrderExportGenesis []string
OrderBeginBlockers []string
OrderEndBlockers []string
OrderMigrations []string
}
type VersionedModule struct {
module sdkmodule.AppModule
// fromVersion and toVersion indicate the continuous range of app versions that the particular
// module is part of. The range is inclusive. `fromVersion` should not be smaller than `toVersion`
// 0 is not a valid app version
fromVersion, toVersion uint64
}
func NewVersionedModule(module sdkmodule.AppModule, fromVersion, toVersion uint64) VersionedModule {
return VersionedModule{
module: module,
fromVersion: fromVersion,
toVersion: toVersion,
}
}
// NewManager creates a new Manager object
func NewManager(modules ...VersionedModule) (*Manager, error) {
moduleMap := make(map[uint64]map[string]sdkmodule.AppModule)
allModules := make([]sdkmodule.AppModule, len(modules))
modulesStr := make([]string, 0, len(modules))
// firstVersion and lastVersion are quicker ways of working out the range of
// versions the state machine supports
firstVersion, lastVersion := uint64(0), uint64(0)
for idx, module := range modules {
if module.fromVersion == 0 {
return nil, sdkerrors.ErrInvalidVersion.Wrapf("v0 is not a valid version for module %s", module.module.Name())
}
if module.fromVersion > module.toVersion {
return nil, sdkerrors.ErrLogic.Wrapf("fromVersion can not be greater than toVersion for module %s", module.module.Name())
}
for version := module.fromVersion; version <= module.toVersion; version++ {
if moduleMap[version] == nil {
moduleMap[version] = make(map[string]sdkmodule.AppModule)
}
moduleMap[version][module.module.Name()] = module.module
}
allModules[idx] = module.module
modulesStr = append(modulesStr, module.module.Name())
if firstVersion == 0 || module.fromVersion < firstVersion {
firstVersion = module.fromVersion
}
if lastVersion == 0 || module.toVersion > lastVersion {
lastVersion = module.toVersion
}
}
return &Manager{
versionedModules: moduleMap,
allModules: allModules,
firstVersion: firstVersion,
lastVersion: lastVersion,
OrderInitGenesis: modulesStr,
OrderExportGenesis: modulesStr,
OrderBeginBlockers: modulesStr,
OrderEndBlockers: modulesStr,
}, nil
}
// SetOrderInitGenesis sets the order of init genesis calls
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames)
m.OrderInitGenesis = moduleNames
}
// SetOrderExportGenesis sets the order of export genesis calls
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames)
m.OrderExportGenesis = moduleNames
}
// SetOrderBeginBlockers sets the order of begin-blocker calls
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames)
m.OrderBeginBlockers = moduleNames
}
// SetOrderEndBlockers sets the order of end-blocker calls
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
m.OrderEndBlockers = moduleNames
}
// SetOrderMigrations sets the order of migrations to be run. If not set
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderMigrations", moduleNames)
m.OrderMigrations = moduleNames
}
// RegisterInvariants registers all module invariants
func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) {
for _, module := range m.allModules {
module.RegisterInvariants(ir)
}
}
// RegisterServices registers all module services
func (m *Manager) RegisterServices(cfg sdkmodule.Configurator) {
for _, module := range m.allModules {
module.RegisterServices(cfg)
}
}
// InitGenesis performs init genesis functionality for modules. Exactly one
// module must return a non-empty validator set update to correctly initialize
// the chain.
func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData map[string]json.RawMessage, appVersion uint64) abci.ResponseInitChain {
var validatorUpdates []abci.ValidatorUpdate
ctx.Logger().Info("initializing blockchain state from genesis.json")
modules, versionSupported := m.versionedModules[appVersion]
if !versionSupported {
modules = m.versionedModules[appconsts.LatestVersion]
}
for _, moduleName := range m.OrderInitGenesis {
if genesisData[moduleName] == nil {
continue
}
if modules[moduleName] == nil {
continue
}
ctx.Logger().Debug("running initialization for module", "module", moduleName)
moduleValUpdates := modules[moduleName].InitGenesis(ctx, cdc, genesisData[moduleName])
// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
if len(moduleValUpdates) > 0 {
if len(validatorUpdates) > 0 {
panic("validator InitGenesis updates already set by a previous module")
}
validatorUpdates = moduleValUpdates
}
}
// a chain must initialize with a non-empty validator set
if len(validatorUpdates) == 0 {
panic(fmt.Sprintf("validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the DefaultPowerReduction (%d)", sdk.DefaultPowerReduction))
}
return abci.ResponseInitChain{
Validators: validatorUpdates,
}
}
// ExportGenesis performs export genesis functionality for modules
func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec, version uint64) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)
modules := m.versionedModules[version]
for _, moduleName := range m.OrderExportGenesis {
genesisData[moduleName] = modules[moduleName].ExportGenesis(ctx, cdc)
}
return genesisData
}
// assertNoForgottenModules checks that we didn't forget any modules in the
// SetOrder* functions.
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string) {
ms := make(map[string]bool)
for _, m := range moduleNames {
ms[m] = true
}
var missing []string
for _, m := range m.allModules {
if _, ok := ms[m.Name()]; !ok {
missing = append(missing, m.Name())
}
}
if len(missing) != 0 {
panic(fmt.Sprintf(
"%s: all modules must be defined when setting %s, missing: %v", setOrderFnName, setOrderFnName, missing))
}
}
// MigrationHandler is the migration function that each module registers.
type MigrationHandler func(sdk.Context) error
// VersionMap is a map of moduleName -> version
type VersionMap map[string]uint64
// RunMigrations performs in-place store migrations for all modules. This
// function MUST be called when the state machine changes appVersion
func (m Manager) RunMigrations(ctx sdk.Context, cfg sdkmodule.Configurator, fromVersion, toVersion uint64) error {
c, ok := cfg.(configurator)
if !ok {
return sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", configurator{}, cfg)
}
modules := m.OrderMigrations
if modules == nil {
modules = DefaultMigrationsOrder(m.ModuleNames(toVersion))
}
currentVersionModules, exists := m.versionedModules[fromVersion]
if !exists {
return sdkerrors.ErrInvalidVersion.Wrapf("fromVersion %d not supported", fromVersion)
}
nextVersionModules, exists := m.versionedModules[toVersion]
if !exists {
return sdkerrors.ErrInvalidVersion.Wrapf("toVersion %d not supported", toVersion)
}
for _, moduleName := range modules {
_, currentModuleExists := currentVersionModules[moduleName]
nextModule, nextModuleExists := nextVersionModules[moduleName]
// if the module exists for both upgrades
if currentModuleExists && nextModuleExists {
err := c.runModuleMigrations(ctx, moduleName, fromVersion, toVersion)
if err != nil {
return err
}
} else if !currentModuleExists && nextModuleExists {
ctx.Logger().Info(fmt.Sprintf("adding a new module: %s", moduleName))
moduleValUpdates := nextModule.InitGenesis(ctx, c.cdc, nextModule.DefaultGenesis(c.cdc))
// The module manager assumes only one module will update the
// validator set, and it can't be a new module.
if len(moduleValUpdates) > 0 {
return sdkerrors.ErrLogic.Wrap("validator InitGenesis update is already set by another module")
}
}
// TODO: handle the case where a module is no longer supported (i.e. removed from the state machine)
}
return nil
}
// BeginBlock performs begin block functionality for all modules. It creates a
// child context with an event manager to aggregate events emitted from all
// modules.
func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
ctx = ctx.WithEventManager(sdk.NewEventManager())
modules := m.versionedModules[ctx.BlockHeader().Version.App]
if modules == nil {
modules = m.versionedModules[appconsts.LatestVersion]
}
for _, moduleName := range m.OrderBeginBlockers {
module, ok := modules[moduleName].(sdkmodule.BeginBlockAppModule)
if ok {
module.BeginBlock(ctx, req)
}
}
return abci.ResponseBeginBlock{
Events: ctx.EventManager().ABCIEvents(),
}
}
// EndBlock performs end block functionality for all modules. It creates a
// child context with an event manager to aggregate events emitted from all
// modules.
func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
ctx = ctx.WithEventManager(sdk.NewEventManager())
validatorUpdates := []abci.ValidatorUpdate{}
modules := m.versionedModules[ctx.BlockHeader().Version.App]
if modules == nil {
modules = m.versionedModules[appconsts.LatestVersion]
}
for _, moduleName := range m.OrderEndBlockers {
module, ok := modules[moduleName].(sdkmodule.EndBlockAppModule)
if !ok {
continue
}
moduleValUpdates := module.EndBlock(ctx, req)
// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
if len(moduleValUpdates) > 0 {
if len(validatorUpdates) > 0 {
panic("validator EndBlock updates already set by a previous module")
}
validatorUpdates = moduleValUpdates
}
}
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
Events: ctx.EventManager().ABCIEvents(),
}
}
// ModuleNames returns list of all module names, without any particular order.
func (m *Manager) ModuleNames(version uint64) []string {
modules, ok := m.versionedModules[version]
if !ok {
return []string{}
}
ms := make([]string, len(modules))
i := 0
for m := range modules {
ms[i] = m
i++
}
return ms
}
func (m *Manager) SupportedVersions() []uint64 {
output := make([]uint64, 0, m.lastVersion-m.firstVersion+1)
for version := m.firstVersion; version <= m.lastVersion; version++ {
if _, ok := m.versionedModules[version]; ok {
output = append(output, version)
}
}
return output
}
// DefaultMigrationsOrder returns a default migrations order: ascending alphabetical by module name,
// except x/auth which will run last, see:
// https://github.com/cosmos/cosmos-sdk/issues/10591
func DefaultMigrationsOrder(modules []string) []string {
const authName = "auth"
out := make([]string, 0, len(modules))
hasAuth := false
for _, m := range modules {
if m == authName {
hasAuth = true
} else {
out = append(out, m)
}
}
sort.Strings(out)
if hasAuth {
out = append(out, authName)
}
return out
}