refactor: reuse nodeconfig instead of loading it from DB

This commit is contained in:
Richard Ramos 2022-02-27 10:46:17 -04:00
parent 16197dc807
commit b537ff9f02
6 changed files with 64 additions and 58 deletions

View File

@ -53,6 +53,8 @@ var (
ErrRPCClientUnavailable = errors.New("JSON-RPC client is unavailable") ErrRPCClientUnavailable = errors.New("JSON-RPC client is unavailable")
// ErrDBNotAvailable is returned if a method is called before the DB is available for usage // ErrDBNotAvailable is returned if a method is called before the DB is available for usage
ErrDBNotAvailable = errors.New("DB is unavailable") 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) var _ StatusBackend = (*GethStatusBackend)(nil)
@ -61,8 +63,10 @@ var _ StatusBackend = (*GethStatusBackend)(nil)
type GethStatusBackend struct { type GethStatusBackend struct {
mu sync.Mutex mu sync.Mutex
// rootDataDir is the same for all networks. // rootDataDir is the same for all networks.
rootDataDir string rootDataDir string
appDB *sql.DB appDB *sql.DB
config *params.NodeConfig
statusNode *node.StatusNode statusNode *node.StatusNode
personalAPI *personal.PublicAPI personalAPI *personal.PublicAPI
multiaccountsDB *multiaccounts.Database multiaccountsDB *multiaccounts.Database
@ -276,19 +280,19 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
if err != nil { if err != nil {
return err return err
} }
conf, err := b.loadNodeConfig()
if err != nil { if b.loadNodeConfig(nil) != nil {
return err return err
} }
logSettings := logutils.LogSettings{ logSettings := logutils.LogSettings{
Enabled: conf.LogEnabled, Enabled: b.config.LogEnabled,
MobileSystem: conf.LogMobileSystem, MobileSystem: b.config.LogMobileSystem,
Level: conf.LogLevel, Level: b.config.LogLevel,
File: conf.LogFile, File: b.config.LogFile,
MaxSize: conf.LogMaxSize, MaxSize: b.config.LogMaxSize,
MaxBackups: conf.LogMaxBackups, MaxBackups: b.config.LogMaxBackups,
CompressRotated: conf.LogCompressRotated, CompressRotated: b.config.LogCompressRotated,
} }
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil { if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
return err return err
@ -311,7 +315,7 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
if err != nil { if err != nil {
return err return err
} }
err = b.StartNode(conf) err = b.StartNode(b.config)
if err != nil { if err != nil {
return err return err
} }
@ -344,12 +348,7 @@ func (b *GethStatusBackend) StartNodeWithKey(acc multiaccounts.Account, password
return err return err
} }
func (b *GethStatusBackend) mergeConfig(n *params.NodeConfig) (*params.NodeConfig, error) { func (b *GethStatusBackend) OverwriteNodeConfigValues(conf *params.NodeConfig, n *params.NodeConfig) (*params.NodeConfig, error) {
conf, err := nodecfg.GetNodeConfig(b.appDB)
if err != nil {
return nil, err
}
// Overwrite db configuration (only adds new values) // Overwrite db configuration (only adds new values)
if err := mergo.Merge(conf, n); err != nil { if err := mergo.Merge(conf, n); err != nil {
return nil, err return nil, err
@ -359,34 +358,28 @@ func (b *GethStatusBackend) mergeConfig(n *params.NodeConfig) (*params.NodeConfi
return nil, err 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) err := b.ensureAppDBOpened(acc, password)
if err != nil { if err != nil {
return err return err
} }
conf, err := b.loadNodeConfig()
err = b.loadNodeConfig(inputNodeCfg)
if err != nil { if err != nil {
return err return err
} }
if nodecfg != nil {
conf, err = b.mergeConfig(nodecfg)
if err != nil {
return err
}
}
logSettings := logutils.LogSettings{ logSettings := logutils.LogSettings{
Enabled: conf.LogEnabled, Enabled: b.config.LogEnabled,
MobileSystem: conf.LogMobileSystem, MobileSystem: b.config.LogMobileSystem,
Level: conf.LogLevel, Level: b.config.LogLevel,
File: conf.LogFile, File: b.config.LogFile,
MaxSize: conf.LogMaxSize, MaxSize: b.config.LogMaxSize,
MaxBackups: conf.LogMaxBackups, MaxBackups: b.config.LogMaxBackups,
CompressRotated: conf.LogCompressRotated, CompressRotated: b.config.LogCompressRotated,
} }
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil { if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
return err return err
@ -415,7 +408,7 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
MainAccount: walletAddr, MainAccount: walletAddr,
} }
err = b.StartNode(conf) err = b.StartNode(b.config)
if err != nil { if err != nil {
return err return err
} }
@ -677,12 +670,20 @@ func (b *GethStatusBackend) saveAccountsAndSettings(settings settings.Settings,
return accdb.SaveAccounts(subaccs) return accdb.SaveAccounts(subaccs)
} }
func (b *GethStatusBackend) loadNodeConfig() (*params.NodeConfig, error) { func (b *GethStatusBackend) loadNodeConfig(inputNodeCfg *params.NodeConfig) error {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
conf, err := nodecfg.GetNodeConfig(b.appDB)
conf, err := nodecfg.GetNodeConfigFromDB(b.appDB)
if err != nil { 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 // 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) conf.KeyStoreDir = filepath.Join(b.rootDataDir, conf.KeyStoreDir)
return conf, nil b.config = conf
return nil
} }
func (b *GethStatusBackend) saveNodeConfig(n *params.NodeConfig) error { 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) { func (b *GethStatusBackend) GetNodeConfig() (*params.NodeConfig, error) {
if b.appDB == nil { if b.config == nil {
return nil, ErrDBNotAvailable return nil, ErrConfigNotAvailable
} }
return b.loadNodeConfig() return b.config, nil
} }
func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) { func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
@ -812,11 +815,11 @@ func (b *GethStatusBackend) RestartNode() error {
return node.ErrNoRunningNode return node.ErrNoRunningNode
} }
newcfg := *(b.statusNode.Config())
if err := b.stopNode(); err != nil { if err := b.stopNode(); err != nil {
return err return err
} }
return b.startNode(&newcfg)
return b.startNode(b.config)
} }
// ResetChainData remove chain data from data directory. // ResetChainData remove chain data from data directory.
@ -824,16 +827,16 @@ func (b *GethStatusBackend) RestartNode() error {
func (b *GethStatusBackend) ResetChainData() error { func (b *GethStatusBackend) ResetChainData() error {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
newcfg := *(b.statusNode.Config())
if err := b.stopNode(); err != nil { if err := b.stopNode(); err != nil {
return err return err
} }
// config is cleaned when node is stopped // 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 return err
} }
signal.SendChainDataRemoved() signal.SendChainDataRemoved()
return b.startNode(&newcfg) return b.startNode(b.config)
} }
// CallRPC executes public RPC requests on node's in-proc RPC server. // CallRPC executes public RPC requests on node's in-proc RPC server.

View File

@ -41,7 +41,7 @@ func TestGetNodeConfig(t *testing.T) {
nodeConfig := randomNodeConfig() nodeConfig := randomNodeConfig()
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig)) require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
dbNodeConfig, err := nodecfg.GetNodeConfig(db) dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, nodeConfig, dbNodeConfig) require.Equal(t, nodeConfig, dbNodeConfig)
} }
@ -55,7 +55,7 @@ func TestSaveNodeConfig(t *testing.T) {
newNodeConfig := randomNodeConfig() newNodeConfig := randomNodeConfig()
require.NoError(t, nodecfg.SaveNodeConfig(db, newNodeConfig)) require.NoError(t, nodecfg.SaveNodeConfig(db, newNodeConfig))
dbNodeConfig, err := nodecfg.GetNodeConfig(db) dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, *newNodeConfig, *dbNodeConfig) require.Equal(t, *newNodeConfig, *dbNodeConfig)
} }
@ -92,7 +92,7 @@ func TestMigrateNodeConfig(t *testing.T) {
err = nodecfg.MigrateNodeConfig(db) err = nodecfg.MigrateNodeConfig(db)
require.NoError(t, err) require.NoError(t, err)
dbNodeConfig, err := nodecfg.GetNodeConfig(db) dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, nodeConfig, dbNodeConfig) require.Equal(t, nodeConfig, dbNodeConfig)

View File

@ -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, // 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, // 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 // mergin the values received in the configJSON parameter
func LoginWithConfig(accountData, password, configJSON string) string { func LoginWithConfig(accountData, password, configJSON string) string {
err := login(accountData, password, configJSON) err := login(accountData, password, configJSON)

View File

@ -744,7 +744,7 @@ func MigrateNodeConfig(db *sql.DB) error {
return nil 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{}) tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -41,7 +41,7 @@ func (s *Service) APIs() []rpc.API {
{ {
Namespace: "settings", Namespace: "settings",
Version: "0.1.0", Version: "0.1.0",
Service: NewSettingsAPI(s.db), Service: NewSettingsAPI(s.db, s.config),
}, },
{ {
Namespace: "accounts", Namespace: "accounts",

View File

@ -9,13 +9,14 @@ import (
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
) )
func NewSettingsAPI(db *accounts.Database) *SettingsAPI { func NewSettingsAPI(db *accounts.Database, config *params.NodeConfig) *SettingsAPI {
return &SettingsAPI{db} return &SettingsAPI{db, config}
} }
// SettingsAPI is class with methods available over RPC. // SettingsAPI is class with methods available over RPC.
type SettingsAPI struct { type SettingsAPI struct {
db *accounts.Database db *accounts.Database
config *params.NodeConfig
} }
func (api *SettingsAPI) SaveSetting(ctx context.Context, typ string, val interface{}) error { 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() return api.db.GetSettings()
} }
// NodeConfig returns the currently used node configuration
func (api *SettingsAPI) NodeConfig(ctx context.Context) (*params.NodeConfig, error) { 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 { func (api *SettingsAPI) SaveNodeConfig(ctx context.Context, n *params.NodeConfig) error {
return nodecfg.SaveNodeConfig(api.db.DB(), n) return nodecfg.SaveNodeConfig(api.db.DB(), n)
} }