Skip to content

Commit

Permalink
[status-im/status-mobile#9942] Upgradable paths in configs
Browse files Browse the repository at this point in the history
Storing absolute path for different configs breaks compatibility on iOS
as app's dir is changed after upgrade. The solution is to store relative
paths and to concatenate it with `backend.rootDataDir`. The only
exception is `LogFile` as it is stored outside `backend.rootDataDir` on
Android. `LogDir` config was added to allow adding of custom dir for log
file.
Configs concerned:
`DataDir`
`LogDir`
`LogFile`
`KeystoreDir`
`BackupDisabledDataDir`
  • Loading branch information
rasom committed Jan 31, 2020
1 parent c2f22f1 commit 992168b
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 23 deletions.
9 changes: 6 additions & 3 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
if err != nil {
return err
}
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
if err := logutils.OverrideRootLogWithConfig(conf, b.rootDataDir, false); err != nil {
return err
}
accountsDB := accounts.NewDB(b.appDB)
Expand Down Expand Up @@ -261,7 +261,7 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
if err != nil {
return err
}
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
if err := logutils.OverrideRootLogWithConfig(conf, b.rootDataDir, false); err != nil {
return err
}
accountsDB := accounts.NewDB(b.appDB)
Expand Down Expand Up @@ -442,6 +442,9 @@ func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
if manager == nil {
return errors.New("ethereum accounts.Manager is nil")
}

config.DataDir = filepath.Join(b.rootDataDir, config.DataDir)
config.KeyStoreDir = filepath.Join(b.rootDataDir, config.KeyStoreDir)
if err = b.statusNode.StartWithOptions(config, node.StartOptions{
Services: services,
// The peers discovery protocols are started manually after
Expand Down Expand Up @@ -882,7 +885,7 @@ func (b *GethStatusBackend) injectAccountIntoServices() error {
return err
}

if err := st.InitProtocol(identity, b.appDB); err != nil {
if err := st.InitProtocol(identity, b.appDB, b.rootDataDir); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions api/nimbus_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (b *nimbusStatusBackend) startNodeWithKey(acc multiaccounts.Account, passwo
if err != nil {
return err
}
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
if err := logutils.OverrideRootLogWithConfig(conf, b.rootDataDir, false); err != nil {
return err
}
accountsDB := accounts.NewDB(b.appDB)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (b *nimbusStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pa
if err != nil {
return err
}
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
if err := logutils.OverrideRootLogWithConfig(conf, b.rootDataDir, false); err != nil {
return err
}
accountsDB := accounts.NewDB(b.appDB)
Expand Down Expand Up @@ -884,7 +884,7 @@ func (b *nimbusStatusBackend) injectAccountIntoServices() error {
return err
}

if err := st.InitProtocol(identity, b.appDB); err != nil {
if err := st.InitProtocol(identity, b.appDB, b.rootDataDir); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/statusd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func setupLogging(config *params.NodeConfig) {
}

colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLogWithConfig(config, colors); err != nil {
if err := logutils.OverrideRootLogWithConfig(config, "", colors); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func StartNode(configJSON *C.char) *C.char {
return makeJSONResponse(err)
}

if err := logutils.OverrideRootLogWithConfig(config, false); err != nil {
if err := logutils.OverrideRootLogWithConfig(config, statusBackend.rootDataDir, false); err != nil {
return makeJSONResponse(err)
}

Expand Down
13 changes: 11 additions & 2 deletions logutils/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logutils

import (
"os"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/log"
Expand All @@ -15,15 +16,23 @@ func OverrideWithStdLogger(config *params.NodeConfig) error {
}

// OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it.
func OverrideRootLogWithConfig(config *params.NodeConfig, colors bool) error {
func OverrideRootLogWithConfig(config *params.NodeConfig, rootDataDir string, colors bool) error {
if !config.LogEnabled {
return nil
}
if config.LogMobileSystem {
return OverrideWithStdLogger(config)
}

var logFile string
if len(config.LogDir) == 0 {
logFile = filepath.Join(rootDataDir, config.LogFile)
} else {
logFile = filepath.Join(config.LogDir, config.LogFile)
}

return OverrideRootLog(config.LogEnabled, config.LogLevel, FileOptions{
Filename: config.LogFile,
Filename: logFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
Compress: config.LogCompressRotated,
Expand Down
5 changes: 4 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ type NodeConfig struct {
// LogMobileSystem enables log redirection to android/ios system logger.
LogMobileSystem bool

// LogFile is a folder which contains LogFile
LogDir string

// LogFile is filename where exposed logs get written to
LogFile string

Expand Down Expand Up @@ -648,7 +651,7 @@ func NewNodeConfig(dataDir string, networkID uint64) (*NodeConfig, error) {
var keyStoreDir, wnodeDir string

if dataDir != "" {
keyStoreDir = filepath.Join(dataDir, "keystore")
keyStoreDir = "keystore"
}
if dataDir != "" {
wnodeDir = filepath.Join(dataDir, "wnode")
Expand Down
2 changes: 1 addition & 1 deletion params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewNodeConfigWithDefaults(t *testing.T) {
)
require.NoError(t, err)
assert.Equal(t, "/some/data/path", c.DataDir)
assert.Equal(t, "/some/data/path/keystore", c.KeyStoreDir)
assert.Equal(t, "keystore", c.KeyStoreDir)
assert.Equal(t, true, c.EnableNTPSync)
// assert Whisper
assert.Equal(t, true, c.WhisperConfig.Enabled)
Expand Down
5 changes: 3 additions & 2 deletions services/ext/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *Service) GetPeer(rawURL string) (*enode.Node, error) {
return enode.ParseV4(rawURL)
}

func (s *Service) InitProtocol(identity *ecdsa.PrivateKey, db *sql.DB) error { // nolint: gocyclo
func (s *Service) InitProtocol(identity *ecdsa.PrivateKey, db *sql.DB, rootDataDir string) error { // nolint: gocyclo
if !s.config.PFSEnabled {
return nil
}
Expand All @@ -131,7 +131,8 @@ func (s *Service) InitProtocol(identity *ecdsa.PrivateKey, db *sql.DB) error { /

s.identity = identity

dataDir := filepath.Clean(s.config.BackupDisabledDataDir)
backupDir := filepath.Join(rootDataDir, s.config.BackupDisabledDataDir)
dataDir := filepath.Clean(backupDir)

if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
return err
Expand Down
13 changes: 6 additions & 7 deletions services/shhext/api_geth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"math"
"net"
"os"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -138,7 +137,7 @@ func TestRequestMessagesErrors(t *testing.T) {
handler := ext.NewHandlerMock(1)
config := params.ShhextConfig{
InstallationID: "1",
BackupDisabledDataDir: os.TempDir(),
BackupDisabledDataDir: "/",
PFSEnabled: true,
}
nodeWrapper := ext.NewTestNodeWrapper(shh, nil)
Expand Down Expand Up @@ -184,12 +183,12 @@ func TestRequestMessagesErrors(t *testing.T) {
}

func TestInitProtocol(t *testing.T) {
directory, err := ioutil.TempDir("", "status-go-testing")
_, err := ioutil.TempDir("", "status-go-testing")
require.NoError(t, err)

config := params.ShhextConfig{
InstallationID: "2",
BackupDisabledDataDir: directory,
BackupDisabledDataDir: "/",
PFSEnabled: true,
MailServerConfirmations: true,
ConnectionTarget: 10,
Expand All @@ -210,7 +209,7 @@ func TestInitProtocol(t *testing.T) {
sqlDB, err := sqlite.OpenDB(fmt.Sprintf("%s/db.sql", tmpdir), "password")
require.NoError(t, err)

err = service.InitProtocol(privateKey, sqlDB)
err = service.InitProtocol(privateKey, sqlDB, "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -251,7 +250,7 @@ func (s *ShhExtSuite) createAndAddNode() {
// set up protocol
config := params.ShhextConfig{
InstallationID: strconv.Itoa(idx),
BackupDisabledDataDir: s.dir,
BackupDisabledDataDir: "/",
PFSEnabled: true,
MailServerConfirmations: true,
ConnectionTarget: 10,
Expand All @@ -264,7 +263,7 @@ func (s *ShhExtSuite) createAndAddNode() {
s.Require().NoError(err)
privateKey, err := crypto.GenerateKey()
s.NoError(err)
err = service.InitProtocol(privateKey, sqlDB)
err = service.InitProtocol(privateKey, sqlDB, "")
s.NoError(err)
err = stack.Register(func(n *node.ServiceContext) (node.Service, error) {
return service, nil
Expand Down
4 changes: 2 additions & 2 deletions services/wakuext/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestInitProtocol(t *testing.T) {
sqlDB, err := sqlite.OpenDB(fmt.Sprintf("%s/db.sql", tmpdir), "password")
require.NoError(t, err)

err = service.InitProtocol(privateKey, sqlDB)
err = service.InitProtocol(privateKey, sqlDB, "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *ShhExtSuite) createAndAddNode() {
s.Require().NoError(err)
privateKey, err := crypto.GenerateKey()
s.NoError(err)
err = service.InitProtocol(privateKey, sqlDB)
err = service.InitProtocol(privateKey, sqlDB, "")
s.NoError(err)
err = stack.Register(func(n *node.ServiceContext) (node.Service, error) {
return service, nil
Expand Down

0 comments on commit 992168b

Please sign in to comment.