forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar.go
110 lines (88 loc) · 3.25 KB
/
registrar.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
package registrar
import (
"github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Fueav/juno/node"
"github.com/Fueav/juno/modules/telemetry"
"github.com/Fueav/juno/logging"
"github.com/Fueav/juno/types/config"
"github.com/Fueav/juno/modules/pruning"
"github.com/Fueav/juno/modules"
"github.com/Fueav/juno/modules/messages"
"github.com/Fueav/juno/database"
)
// Context represents the context of the modules registrar
type Context struct {
JunoConfig config.Config
SDKConfig *sdk.Config
EncodingConfig *params.EncodingConfig
Database database.Database
Proxy node.Node
Logger logging.Logger
}
// NewContext allows to build a new Context instance
func NewContext(
parsingConfig config.Config, sdkConfig *sdk.Config, encodingConfig *params.EncodingConfig,
database database.Database, proxy node.Node, logger logging.Logger,
) Context {
return Context{
JunoConfig: parsingConfig,
SDKConfig: sdkConfig,
EncodingConfig: encodingConfig,
Database: database,
Proxy: proxy,
Logger: logger,
}
}
// Registrar represents a modules registrar. This allows to build a list of modules that can later be used by
// specifying their names inside the TOML configuration file.
type Registrar interface {
BuildModules(context Context) modules.Modules
}
// ------------------------------------------------------------------------------------------------------------------
var (
_ Registrar = &EmptyRegistrar{}
)
// EmptyRegistrar represents a Registrar which does not register any custom module
type EmptyRegistrar struct{}
// BuildModules implements Registrar
func (*EmptyRegistrar) BuildModules(_ Context) modules.Modules {
return nil
}
// ------------------------------------------------------------------------------------------------------------------
var (
_ Registrar = &DefaultRegistrar{}
)
// DefaultRegistrar represents a registrar that allows to handle the default Juno modules
type DefaultRegistrar struct {
parser messages.MessageAddressesParser
}
// NewDefaultRegistrar builds a new DefaultRegistrar
func NewDefaultRegistrar(parser messages.MessageAddressesParser) *DefaultRegistrar {
return &DefaultRegistrar{
parser: parser,
}
}
// BuildModules implements Registrar
func (r *DefaultRegistrar) BuildModules(ctx Context) modules.Modules {
return modules.Modules{
pruning.NewModule(ctx.JunoConfig, ctx.Database, ctx.Logger),
messages.NewModule(r.parser, ctx.EncodingConfig.Marshaler, ctx.Database),
telemetry.NewModule(ctx.JunoConfig),
}
}
// ------------------------------------------------------------------------------------------------------------------
// GetModules returns the list of module implementations based on the given module names.
// For each module name that is specified but not found, a warning log is printed.
func GetModules(mods modules.Modules, names []string, logger logging.Logger) []modules.Module {
var modulesImpls []modules.Module
for _, name := range names {
module, found := mods.FindByName(name)
if found {
modulesImpls = append(modulesImpls, module)
} else {
logger.Error("Module is required but not registered. Be sure to register it using registrar.RegisterModule", "module", name)
}
}
return modulesImpls
}