-
Notifications
You must be signed in to change notification settings - Fork 12
/
app_config.go
136 lines (109 loc) · 4.42 KB
/
app_config.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
package backend
import (
"fmt"
"code.vegaprotocol.io/vega/paths"
"code.vegaprotocol.io/vega/wallet/api"
netStoreV1 "code.vegaprotocol.io/vega/wallet/network/store/v1"
"code.vegaprotocol.io/vega/wallet/wallets"
"code.vegaprotocol.io/vegawallet-desktop/app"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.uber.org/zap"
)
const (
ReloadingEntireApplicationRequired = "reloading_entire_application_required"
)
type SearchForExistingConfigurationResponse struct {
Wallets []string `json:"wallets"`
Networks []string `json:"networks"`
}
// SearchForExistingConfiguration searches for existing wallets and networks.
// This endpoint should be used to help the user to restore existing wallet
// setup in the app.
func (h *Handler) SearchForExistingConfiguration() (SearchForExistingConfigurationResponse, error) {
h.log.Debug("Entering SearchForExistingConfiguration")
defer h.log.Debug("Leaving SearchForExistingConfiguration")
defaultCfg := app.Config{
VegaHome: "",
}
vegaPaths := paths.New(defaultCfg.VegaHome)
netStore, err := netStoreV1.InitialiseStore(vegaPaths)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the networks store: %v", err))
return SearchForExistingConfigurationResponse{}, fmt.Errorf("could not initialise the networks store: %w", err)
}
walletStore, err := wallets.InitialiseStoreFromPaths(vegaPaths, false)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the wallets store: %v", err))
return SearchForExistingConfigurationResponse{}, fmt.Errorf("could not initialise the wallets store: %w", err)
}
listWallets, _ := api.NewAdminListWallets(walletStore).Handle(h.ctx, nil)
listNetworks, _ := api.NewAdminListNetworks(netStore).Handle(h.ctx, nil)
networks := listNetworks.(api.AdminListNetworksResult).Networks
networkNames := make([]string, 0, len(networks))
for _, net := range networks {
networkNames = append(networkNames, net.Name)
}
return SearchForExistingConfigurationResponse{
Wallets: listWallets.(api.AdminListWalletsResult).Wallets,
Networks: networkNames,
}, nil
}
// GetAppConfig return the application configuration.
func (h *Handler) GetAppConfig() (app.Config, error) {
if err := h.ensureBackendStarted(); err != nil {
return app.Config{}, err
}
h.log.Debug("Entering GetAppConfig")
defer h.log.Debug("Leaving GetAppConfig")
if err := h.ensureAppIsInitialised(); err != nil {
return app.Config{}, err
}
return h.appConfig()
}
// UpdateAppConfig update the application configuration. This requires a restart
// to take effect.
func (h *Handler) UpdateAppConfig(updatedConfig app.Config) error {
if err := h.ensureBackendStarted(); err != nil {
return err
}
h.log.Debug("Entering UpdateAppConfig")
defer h.log.Debug("Leaving UpdateAppConfig")
if err := h.ensureAppIsInitialised(); err != nil {
return err
}
if err := updatedConfig.EnsureIsValid(); err != nil {
return err
}
if updatedConfig.DefaultNetwork != "" {
if exists, err := h.networkStore.NetworkExists(updatedConfig.DefaultNetwork); err != nil {
return fmt.Errorf("could not verify the network exists: %w", err)
} else if !exists {
return fmt.Errorf("the network %q does not exist", updatedConfig.DefaultNetwork)
}
}
existingConfig, err := h.appConfig()
if err != nil {
return err
}
if err := h.configLoader.SaveConfig(updatedConfig); err != nil {
h.log.Error("Could not save the application configuration", zap.Error(err))
return fmt.Errorf("could not save the application configuration: %w", err)
}
// The vega home dictates where to find the wallets, the networks, where to put
// the log files, etc. So, if it has been updated, we need to reload the entire
// application.
reloadEntireAppRequired := existingConfig.VegaHome != updatedConfig.VegaHome
if reloadEntireAppRequired {
runtime.EventsEmit(h.ctx, ReloadingEntireApplicationRequired)
if err := h.reloadBackendComponentsFromConfig(); err != nil {
h.log.Error("Could not reload the backend components after the application configuration update", zap.Error(err))
return fmt.Errorf("could not reload the backend components after the application configuration update: %w", err)
}
} else {
if err := h.updateBackendComponentsFromConfig(); err != nil {
h.log.Error("Could not update the backend components after the application configuration update", zap.Error(err))
return fmt.Errorf("could not update the backend components after the application configuration update: %w", err)
}
}
return nil
}