-
Notifications
You must be signed in to change notification settings - Fork 77
/
setup.go
68 lines (53 loc) · 2.02 KB
/
setup.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
package types
import (
"fmt"
"reflect"
"github.com/forbole/juno/v5/parser"
nodebuilder "github.com/forbole/juno/v5/node/builder"
"github.com/forbole/juno/v5/types/config"
"github.com/forbole/juno/v5/database"
sdk "github.com/cosmos/cosmos-sdk/types"
modsregistrar "github.com/forbole/juno/v5/modules/registrar"
)
// GetParserContext setups all the things that can be used to later parse the chain state
func GetParserContext(cfg config.Config, parseConfig *Config) (*parser.Context, error) {
// Build the codec
encodingConfig := parseConfig.GetEncodingConfigBuilder()()
// Setup the SDK configuration
sdkConfig, sealed := getConfig()
if !sealed {
parseConfig.GetSetupConfig()(cfg, sdkConfig)
sdkConfig.Seal()
}
// Get the db
databaseCtx := database.NewContext(cfg.Database, encodingConfig, parseConfig.GetLogger())
db, err := parseConfig.GetDBBuilder()(databaseCtx)
if err != nil {
return nil, err
}
// Init the client
cp, err := nodebuilder.BuildNode(cfg.Node, encodingConfig)
if err != nil {
return nil, fmt.Errorf("failed to start client: %s", err)
}
// Setup the logging
err = parseConfig.GetLogger().SetLogFormat(cfg.Logging.LogFormat)
if err != nil {
return nil, fmt.Errorf("error while setting logging format: %s", err)
}
err = parseConfig.GetLogger().SetLogLevel(cfg.Logging.LogLevel)
if err != nil {
return nil, fmt.Errorf("error while setting logging level: %s", err)
}
// Get the modules
context := modsregistrar.NewContext(cfg, sdkConfig, encodingConfig, db, cp, parseConfig.GetLogger())
mods := parseConfig.GetRegistrar().BuildModules(context)
registeredModules := modsregistrar.GetModules(mods, cfg.Chain.Modules, parseConfig.GetLogger())
return parser.NewContext(encodingConfig, cp, db, parseConfig.GetLogger(), registeredModules), nil
}
// getConfig returns the SDK Config instance as well as if it's sealed or not
func getConfig() (config *sdk.Config, sealed bool) {
sdkConfig := sdk.GetConfig()
fv := reflect.ValueOf(sdkConfig).Elem().FieldByName("sealed")
return sdkConfig, fv.Bool()
}