refactor: reuse nodeconfig instead of loading it from DB
This commit is contained in:
parent
16197dc807
commit
b537ff9f02
|
@ -53,6 +53,8 @@ var (
|
|||
ErrRPCClientUnavailable = errors.New("JSON-RPC client is unavailable")
|
||||
// ErrDBNotAvailable is returned if a method is called before the DB is available for usage
|
||||
ErrDBNotAvailable = errors.New("DB is unavailable")
|
||||
// ErrConfigNotAvailable is returned if a method is called before the nodeconfig is set
|
||||
ErrConfigNotAvailable = errors.New("NodeConfig is not available")
|
||||
)
|
||||
|
||||
var _ StatusBackend = (*GethStatusBackend)(nil)
|
||||
|
@ -63,6 +65,8 @@ type GethStatusBackend struct {
|
|||
// rootDataDir is the same for all networks.
|
||||
rootDataDir string
|
||||
appDB *sql.DB
|
||||
config *params.NodeConfig
|
||||
|
||||
statusNode *node.StatusNode
|
||||
personalAPI *personal.PublicAPI
|
||||
multiaccountsDB *multiaccounts.Database
|
||||
|
@ -276,19 +280,19 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf, err := b.loadNodeConfig()
|
||||
if err != nil {
|
||||
|
||||
if b.loadNodeConfig(nil) != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logSettings := logutils.LogSettings{
|
||||
Enabled: conf.LogEnabled,
|
||||
MobileSystem: conf.LogMobileSystem,
|
||||
Level: conf.LogLevel,
|
||||
File: conf.LogFile,
|
||||
MaxSize: conf.LogMaxSize,
|
||||
MaxBackups: conf.LogMaxBackups,
|
||||
CompressRotated: conf.LogCompressRotated,
|
||||
Enabled: b.config.LogEnabled,
|
||||
MobileSystem: b.config.LogMobileSystem,
|
||||
Level: b.config.LogLevel,
|
||||
File: b.config.LogFile,
|
||||
MaxSize: b.config.LogMaxSize,
|
||||
MaxBackups: b.config.LogMaxBackups,
|
||||
CompressRotated: b.config.LogCompressRotated,
|
||||
}
|
||||
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
|
||||
return err
|
||||
|
@ -311,7 +315,7 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.StartNode(conf)
|
||||
err = b.StartNode(b.config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -344,12 +348,7 @@ func (b *GethStatusBackend) StartNodeWithKey(acc multiaccounts.Account, password
|
|||
return err
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) mergeConfig(n *params.NodeConfig) (*params.NodeConfig, error) {
|
||||
conf, err := nodecfg.GetNodeConfig(b.appDB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) OverwriteNodeConfigValues(conf *params.NodeConfig, n *params.NodeConfig) (*params.NodeConfig, error) {
|
||||
// Overwrite db configuration (only adds new values)
|
||||
if err := mergo.Merge(conf, n); err != nil {
|
||||
return nil, err
|
||||
|
@ -359,34 +358,28 @@ func (b *GethStatusBackend) mergeConfig(n *params.NodeConfig) (*params.NodeConfi
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return b.loadNodeConfig()
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, password string, nodecfg *params.NodeConfig) error {
|
||||
func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, password string, inputNodeCfg *params.NodeConfig) error {
|
||||
err := b.ensureAppDBOpened(acc, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf, err := b.loadNodeConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nodecfg != nil {
|
||||
conf, err = b.mergeConfig(nodecfg)
|
||||
err = b.loadNodeConfig(inputNodeCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
logSettings := logutils.LogSettings{
|
||||
Enabled: conf.LogEnabled,
|
||||
MobileSystem: conf.LogMobileSystem,
|
||||
Level: conf.LogLevel,
|
||||
File: conf.LogFile,
|
||||
MaxSize: conf.LogMaxSize,
|
||||
MaxBackups: conf.LogMaxBackups,
|
||||
CompressRotated: conf.LogCompressRotated,
|
||||
Enabled: b.config.LogEnabled,
|
||||
MobileSystem: b.config.LogMobileSystem,
|
||||
Level: b.config.LogLevel,
|
||||
File: b.config.LogFile,
|
||||
MaxSize: b.config.LogMaxSize,
|
||||
MaxBackups: b.config.LogMaxBackups,
|
||||
CompressRotated: b.config.LogCompressRotated,
|
||||
}
|
||||
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
|
||||
return err
|
||||
|
@ -415,7 +408,7 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
|
|||
MainAccount: walletAddr,
|
||||
}
|
||||
|
||||
err = b.StartNode(conf)
|
||||
err = b.StartNode(b.config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -677,12 +670,20 @@ func (b *GethStatusBackend) saveAccountsAndSettings(settings settings.Settings,
|
|||
return accdb.SaveAccounts(subaccs)
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) loadNodeConfig() (*params.NodeConfig, error) {
|
||||
func (b *GethStatusBackend) loadNodeConfig(inputNodeCfg *params.NodeConfig) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
conf, err := nodecfg.GetNodeConfig(b.appDB)
|
||||
|
||||
conf, err := nodecfg.GetNodeConfigFromDB(b.appDB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if inputNodeCfg != nil {
|
||||
conf, err = b.OverwriteNodeConfigValues(conf, inputNodeCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Start WakuV1 if WakuV2 is not enabled
|
||||
|
@ -701,7 +702,9 @@ func (b *GethStatusBackend) loadNodeConfig() (*params.NodeConfig, error) {
|
|||
}
|
||||
conf.KeyStoreDir = filepath.Join(b.rootDataDir, conf.KeyStoreDir)
|
||||
|
||||
return conf, nil
|
||||
b.config = conf
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) saveNodeConfig(n *params.NodeConfig) error {
|
||||
|
@ -715,10 +718,10 @@ func (b *GethStatusBackend) saveNodeConfig(n *params.NodeConfig) error {
|
|||
}
|
||||
|
||||
func (b *GethStatusBackend) GetNodeConfig() (*params.NodeConfig, error) {
|
||||
if b.appDB == nil {
|
||||
return nil, ErrDBNotAvailable
|
||||
if b.config == nil {
|
||||
return nil, ErrConfigNotAvailable
|
||||
}
|
||||
return b.loadNodeConfig()
|
||||
return b.config, nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
|
||||
|
@ -812,11 +815,11 @@ func (b *GethStatusBackend) RestartNode() error {
|
|||
return node.ErrNoRunningNode
|
||||
}
|
||||
|
||||
newcfg := *(b.statusNode.Config())
|
||||
if err := b.stopNode(); err != nil {
|
||||
return err
|
||||
}
|
||||
return b.startNode(&newcfg)
|
||||
|
||||
return b.startNode(b.config)
|
||||
}
|
||||
|
||||
// ResetChainData remove chain data from data directory.
|
||||
|
@ -824,16 +827,16 @@ func (b *GethStatusBackend) RestartNode() error {
|
|||
func (b *GethStatusBackend) ResetChainData() error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
newcfg := *(b.statusNode.Config())
|
||||
|
||||
if err := b.stopNode(); err != nil {
|
||||
return err
|
||||
}
|
||||
// config is cleaned when node is stopped
|
||||
if err := b.statusNode.ResetChainData(&newcfg); err != nil {
|
||||
if err := b.statusNode.ResetChainData(b.config); err != nil {
|
||||
return err
|
||||
}
|
||||
signal.SendChainDataRemoved()
|
||||
return b.startNode(&newcfg)
|
||||
return b.startNode(b.config)
|
||||
}
|
||||
|
||||
// CallRPC executes public RPC requests on node's in-proc RPC server.
|
||||
|
|
|
@ -41,7 +41,7 @@ func TestGetNodeConfig(t *testing.T) {
|
|||
nodeConfig := randomNodeConfig()
|
||||
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
|
||||
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfig(db)
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, nodeConfig, dbNodeConfig)
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func TestSaveNodeConfig(t *testing.T) {
|
|||
newNodeConfig := randomNodeConfig()
|
||||
require.NoError(t, nodecfg.SaveNodeConfig(db, newNodeConfig))
|
||||
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfig(db)
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, *newNodeConfig, *dbNodeConfig)
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ func TestMigrateNodeConfig(t *testing.T) {
|
|||
err = nodecfg.MigrateNodeConfig(db)
|
||||
require.NoError(t, err)
|
||||
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfig(db)
|
||||
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, nodeConfig, dbNodeConfig)
|
||||
|
||||
|
|
|
@ -247,7 +247,7 @@ func Login(accountData, password string) string {
|
|||
|
||||
// Login loads a key file (for a given address), tries to decrypt it using the password,
|
||||
// to verify ownership if verified, purges all the previous identities from Whisper,
|
||||
// and injects verified key as shh identity. It then updates the accounts node configuration
|
||||
// and injects verified key as shh identity. It then updates the accounts node db configuration
|
||||
// mergin the values received in the configJSON parameter
|
||||
func LoginWithConfig(accountData, password, configJSON string) string {
|
||||
err := login(accountData, password, configJSON)
|
||||
|
|
|
@ -744,7 +744,7 @@ func MigrateNodeConfig(db *sql.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetNodeConfig(db *sql.DB) (*params.NodeConfig, error) {
|
||||
func GetNodeConfigFromDB(db *sql.DB) (*params.NodeConfig, error) {
|
||||
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -41,7 +41,7 @@ func (s *Service) APIs() []rpc.API {
|
|||
{
|
||||
Namespace: "settings",
|
||||
Version: "0.1.0",
|
||||
Service: NewSettingsAPI(s.db),
|
||||
Service: NewSettingsAPI(s.db, s.config),
|
||||
},
|
||||
{
|
||||
Namespace: "accounts",
|
||||
|
|
|
@ -9,13 +9,14 @@ import (
|
|||
"github.com/status-im/status-go/params"
|
||||
)
|
||||
|
||||
func NewSettingsAPI(db *accounts.Database) *SettingsAPI {
|
||||
return &SettingsAPI{db}
|
||||
func NewSettingsAPI(db *accounts.Database, config *params.NodeConfig) *SettingsAPI {
|
||||
return &SettingsAPI{db, config}
|
||||
}
|
||||
|
||||
// SettingsAPI is class with methods available over RPC.
|
||||
type SettingsAPI struct {
|
||||
db *accounts.Database
|
||||
config *params.NodeConfig
|
||||
}
|
||||
|
||||
func (api *SettingsAPI) SaveSetting(ctx context.Context, typ string, val interface{}) error {
|
||||
|
@ -31,10 +32,12 @@ func (api *SettingsAPI) GetSettings(ctx context.Context) (settings.Settings, err
|
|||
return api.db.GetSettings()
|
||||
}
|
||||
|
||||
// NodeConfig returns the currently used node configuration
|
||||
func (api *SettingsAPI) NodeConfig(ctx context.Context) (*params.NodeConfig, error) {
|
||||
return nodecfg.GetNodeConfig(api.db.DB())
|
||||
return api.config, nil
|
||||
}
|
||||
|
||||
// Saves the nodeconfig in the database. The node must be restarted for the changes to be applied
|
||||
func (api *SettingsAPI) SaveNodeConfig(ctx context.Context, n *params.NodeConfig) error {
|
||||
return nodecfg.SaveNodeConfig(api.db.DB(), n)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue