fix(config)_: fix TorrentConfig when loading node config from DB

* Fix TorrentConfig validation condition
* Add tests
* Should help existing users who cannot login to the app

fixes #14813
This commit is contained in:
Andrey Bocharnikov 2024-06-06 18:08:17 +04:00
parent ddc75815eb
commit 9ffe842acc
5 changed files with 79 additions and 8 deletions

View File

@ -28,9 +28,6 @@ const walletAccountDefaultName = "Account 1"
const keystoreRelativePath = "keystore" const keystoreRelativePath = "keystore"
const DefaultKeycardPairingDataFile = "/ethereum/mainnet_rpc/keycard/pairings.json" const DefaultKeycardPairingDataFile = "/ethereum/mainnet_rpc/keycard/pairings.json"
const DefaultArchivesRelativePath = "data/archivedata"
const DefaultTorrentTorrentsRelativePath = "data/torrents"
const DefaultDataDir = "/ethereum/mainnet_rpc" const DefaultDataDir = "/ethereum/mainnet_rpc"
const DefaultNodeName = "StatusIM" const DefaultNodeName = "StatusIM"
const DefaultLogFile = "geth.log" const DefaultLogFile = "geth.log"
@ -325,8 +322,8 @@ func defaultNodeConfig(installationID string, request *requests.CreateAccount, o
nodeConfig.TorrentConfig = params.TorrentConfig{ nodeConfig.TorrentConfig = params.TorrentConfig{
Enabled: false, Enabled: false,
Port: 0, Port: 0,
DataDir: filepath.Join(nodeConfig.RootDataDir, DefaultArchivesRelativePath), DataDir: filepath.Join(nodeConfig.RootDataDir, params.ArchivesRelativePath),
TorrentDir: filepath.Join(nodeConfig.RootDataDir, DefaultTorrentTorrentsRelativePath), TorrentDir: filepath.Join(nodeConfig.RootDataDir, params.TorrentTorrentsRelativePath),
} }
if request.TorrentConfigEnabled != nil { if request.TorrentConfigEnabled != nil {

View File

@ -253,3 +253,64 @@ func randomNodeConfig() *params.NodeConfig {
}, },
} }
} }
func TestConfigValidate(t *testing.T) {
// GIVEN
db, stop := setupTestDB(t)
defer stop()
tmpdir := t.TempDir()
nodeConfig, err := params.NewNodeConfig(tmpdir, 1777)
require.NoError(t, err)
require.NoError(t, nodeConfig.Validate())
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
// WHEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
// THEN
require.NoError(t, dbNodeConfig.Validate())
}
func TestRepairLoadedTorrentConfig(t *testing.T) {
// GIVEN
db, stop := setupTestDB(t)
defer stop()
tmpdir := t.TempDir()
nodeConfig, err := params.NewNodeConfig(tmpdir, 1777)
require.NoError(t, err)
require.NoError(t, nodeConfig.Validate())
// Write config to db
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
// WHEN: Corrupt the torrent config data as described in the ticket
// (https://github.com/status-im/status-desktop/issues/14643)
// Write invalid torrent config to database
nodeConfig.TorrentConfig.DataDir = ""
nodeConfig.TorrentConfig.TorrentDir = ""
nodeConfig.TorrentConfig.Enabled = true
require.Error(t, nodeConfig.Validate())
_, err = db.Exec(`INSERT OR REPLACE INTO torrent_config (
enabled, port, data_dir, torrent_dir, synthetic_id
) VALUES (?, ?, ?, ?, 'id')`,
nodeConfig.TorrentConfig.Enabled,
nodeConfig.TorrentConfig.Port,
nodeConfig.TorrentConfig.DataDir,
nodeConfig.TorrentConfig.TorrentDir,
)
require.NoError(t, err)
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
// THEN The invalid torrent config should be repaired
require.Error(t, dbNodeConfig.Validate())
require.NoError(t, dbNodeConfig.UpdateWithDefaults())
require.NoError(t, dbNodeConfig.Validate())
}

View File

@ -819,6 +819,16 @@ func (c *NodeConfig) UpdateWithDefaults() error {
c.WakuConfig.MinimumPoW = WakuMinimumPoW c.WakuConfig.MinimumPoW = WakuMinimumPoW
} }
// Ensure TorrentConfig is valid
if c.TorrentConfig.Enabled {
if c.TorrentConfig.DataDir == "" {
c.TorrentConfig.DataDir = filepath.Join(c.RootDataDir, ArchivesRelativePath)
}
if c.TorrentConfig.TorrentDir == "" {
c.TorrentConfig.TorrentDir = filepath.Join(c.RootDataDir, TorrentTorrentsRelativePath)
}
}
return c.setDefaultPushNotificationsServers() return c.setDefaultPushNotificationsServers()
} }
@ -1124,7 +1134,7 @@ func (c *TorrentConfig) Validate(validate *validator.Validate) error {
return err return err
} }
if c.Enabled && c.DataDir == "" || c.TorrentDir == "" { if c.Enabled && (c.DataDir == "" || c.TorrentDir == "") {
return fmt.Errorf("TorrentConfig.DataDir and TorrentConfig.TorrentDir cannot be \"\"") return fmt.Errorf("TorrentConfig.DataDir and TorrentConfig.TorrentDir cannot be \"\"")
} }
return nil return nil

View File

@ -6,6 +6,9 @@ const (
// StatusDatabase path relative to DataDir. // StatusDatabase path relative to DataDir.
StatusDatabase = "status-db" StatusDatabase = "status-db"
ArchivesRelativePath = "data/archivedata"
TorrentTorrentsRelativePath = "data/torrents"
// SendTransactionMethodName https://docs.walletconnect.com/advanced/rpc-reference/ethereum-rpc#eth_sendtransaction // SendTransactionMethodName https://docs.walletconnect.com/advanced/rpc-reference/ethereum-rpc#eth_sendtransaction
SendTransactionMethodName = "eth_sendTransaction" SendTransactionMethodName = "eth_sendTransaction"

View File

@ -347,8 +347,8 @@ func setDefaultNodeConfig(c *params.NodeConfig) error {
c.TorrentConfig = params.TorrentConfig{ c.TorrentConfig = params.TorrentConfig{
Enabled: specifiedTorrentConfigEnabled, Enabled: specifiedTorrentConfigEnabled,
Port: specifiedTorrentConfigPort, Port: specifiedTorrentConfigPort,
DataDir: filepath.Join(c.RootDataDir, api.DefaultArchivesRelativePath), DataDir: filepath.Join(c.RootDataDir, params.ArchivesRelativePath),
TorrentDir: filepath.Join(c.RootDataDir, api.DefaultTorrentTorrentsRelativePath), TorrentDir: filepath.Join(c.RootDataDir, params.TorrentTorrentsRelativePath),
} }
return nil return nil