fix(config)_: add API calls to set new waku node and max backups

* add waku nodeaddr and max log backups validation
* Allow to set new waku node
* Allow to set custom max backups
* add tests
This commit is contained in:
Andrey Bocharnikov 2024-05-24 14:54:12 +04:00
parent 3abf6d7a4e
commit fe25d97052
7 changed files with 194 additions and 0 deletions

View File

@ -802,6 +802,16 @@ func SetLogLevel(db *sql.DB, logLevel string) error {
return err
}
func SetMaxLogBackups(db *sql.DB, maxLogBackups uint) error {
_, err := db.Exec(`UPDATE log_config SET max_backups = ?`, maxLogBackups)
return err
}
func SaveNewWakuNode(db *sql.DB, nodeAddress string) error {
_, err := db.Exec(`INSERT OR REPLACE INTO cluster_nodes (node, type, synthetic_id) VALUES (?, ?, 'id')`, nodeAddress, WakuNodes)
return err
}
func SetWakuV2CustomNodes(db *sql.DB, customNodes map[string]string) error {
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {

View File

@ -20,10 +20,21 @@ func (m *Messenger) SetLogLevel(request *requests.SetLogLevel) error {
return nodecfg.SetLogLevel(m.database, request.LogLevel)
}
func (m *Messenger) SetMaxLogBackups(request *requests.SetMaxLogBackups) error {
return nodecfg.SetMaxLogBackups(m.database, request.MaxLogBackups)
}
func (m *Messenger) SetCustomNodes(request *requests.SetCustomNodes) error {
return nodecfg.SetWakuV2CustomNodes(m.database, request.CustomNodes)
}
func (m *Messenger) SaveNewWakuNode(request *requests.SaveNewWakuNode) error {
if err := request.Validate(); err != nil {
return err
}
return nodecfg.SaveNewWakuNode(m.database, request.NodeAddress)
}
func (m *Messenger) SetCustomizationColor(ctx context.Context, request *requests.SetCustomizationColor) error {
if err := request.Validate(); err != nil {
return err

View File

@ -0,0 +1,41 @@
package protocol
import (
"testing"
"github.com/status-im/status-go/protocol/requests"
"github.com/stretchr/testify/suite"
)
func TestMessengerValidateRequestSuite(t *testing.T) {
suite.Run(t, new(MessengerValidateRequestSuite))
}
type MessengerValidateRequestSuite struct {
MessengerBaseTestSuite
}
func (s *MessengerValidateRequestSuite) TestSaveNewWakuNodeRequestValidate_Enrtree() {
r := requests.SaveNewWakuNode{NodeAddress: "enrtree://AIRVQ5DDA4FFWLRBCHJWUWOO6X6S4ZTZ5B667LQ6AJU6PEYDLRD5O@sandbox.waku.nodes.status.im"}
err := s.m.SaveNewWakuNode(&r)
s.Require().NoError(err)
}
func (s *MessengerValidateRequestSuite) TestSaveNewWakuNodeRequestValidate_Multiaddr() {
r := requests.SaveNewWakuNode{NodeAddress: "/ip4/127.0.0.1/tcp/8080"}
err := s.m.SaveNewWakuNode(&r)
s.Require().NoError(err)
}
func (s *MessengerValidateRequestSuite) TestSaveNewWakuNodeRequestValidate_MultiaddrFail() {
r := requests.SaveNewWakuNode{NodeAddress: "/0.0.0.0"}
err := s.m.SaveNewWakuNode(&r)
s.Require().Error(err)
}
func (s *MessengerValidateRequestSuite) TestSaveNewWakuNodeRequestValidate_HttpFail() {
r := requests.SaveNewWakuNode{NodeAddress: "https://google.com"}
err := s.m.SaveNewWakuNode(&r)
s.Require().Error(err)
}

View File

@ -0,0 +1,94 @@
package protocol
import (
"database/sql"
"testing"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/nodecfg"
"github.com/stretchr/testify/suite"
)
func TestNodeConfigPersistence(t *testing.T) {
suite.Run(t, new(NodeConfigPersistenceTestSuite))
}
type NodeConfigPersistenceTestSuite struct {
suite.Suite
db *sql.DB
config *params.NodeConfig
}
const (
testWakuNodeEnrtree = "enrtree://AL65EKLJAUXKKPG43HVTML5EFFWEZ7L4LOKTLZCLJASG4DSESQZEC@prod.status.nodes.status.im"
testWakuNodeMultiaddr = "/ip4/127.0.0.1/tcp/34012"
)
func (s *NodeConfigPersistenceTestSuite) SetupTest() {
db, err := openTestDB()
s.Require().NoError(err)
s.db = db
s.config, err = nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
// write value to the db, otherwise log_config table won't be created
err = nodecfg.SaveNodeConfig(s.db, s.config)
s.Require().NoError(err)
}
func (s *NodeConfigPersistenceTestSuite) Test_SaveNewWakuNode() {
// GIVEN
wakuNodesBeforeChanges := s.config.ClusterConfig.WakuNodes
// WHEN
err := nodecfg.SaveNewWakuNode(s.db, testWakuNodeEnrtree)
s.Require().NoError(err)
err = nodecfg.SaveNewWakuNode(s.db, testWakuNodeMultiaddr)
s.Require().NoError(err)
// THEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().Len(dbNodeConfig.ClusterConfig.WakuNodes, len(wakuNodesBeforeChanges)+2)
s.Require().Contains(dbNodeConfig.ClusterConfig.WakuNodes, testWakuNodeEnrtree)
s.Require().Contains(dbNodeConfig.ClusterConfig.WakuNodes, testWakuNodeMultiaddr)
}
func (s *NodeConfigPersistenceTestSuite) Test_SaveMaxLogBackups() {
// GIVEN
maxLogBackupsBeforeChanges := s.config.LogMaxBackups
// WHEN
err := nodecfg.SetMaxLogBackups(s.db, uint(maxLogBackupsBeforeChanges+10))
s.Require().NoError(err)
// THEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().Equal(maxLogBackupsBeforeChanges+10, dbNodeConfig.LogMaxBackups)
}
func (s *NodeConfigPersistenceTestSuite) Test_SetLogLevelError() {
// WHEN
err := nodecfg.SetLogLevel(s.db, "ERROR")
s.Require().NoError(err)
// THEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().Equal("ERROR", dbNodeConfig.LogLevel)
}
func (s *NodeConfigPersistenceTestSuite) Test_SetLogLevelDebug() {
// WHEN
err := nodecfg.SetLogLevel(s.db, "DEBUG")
s.Require().NoError(err)
// THEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(s.db)
s.Require().NoError(err)
s.Require().Equal("DEBUG", dbNodeConfig.LogLevel)
}

View File

@ -0,0 +1,25 @@
package requests
import (
"strings"
"github.com/multiformats/go-multiaddr"
)
type SaveNewWakuNode struct {
NodeAddress string `json:"nodeAddress"`
}
func (r *SaveNewWakuNode) Validate() error {
if strings.HasPrefix(r.NodeAddress, "enrtree://") {
return nil
}
// It is a normal multiaddress
_, err := multiaddr.NewMultiaddr(r.NodeAddress)
if err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,5 @@
package requests
type SetMaxLogBackups struct {
MaxLogBackups uint `json:"maxLogBackups" validate:"omitempty,gte=0"`
}

View File

@ -1773,10 +1773,18 @@ func (api *PublicAPI) SetLogLevel(request *requests.SetLogLevel) error {
return api.service.messenger.SetLogLevel(request)
}
func (api *PublicAPI) SetMaxLogBackups(request *requests.SetMaxLogBackups) error {
return api.service.messenger.SetMaxLogBackups(request)
}
func (api *PublicAPI) SetCustomNodes(request *requests.SetCustomNodes) error {
return api.service.messenger.SetCustomNodes(request)
}
func (api *PublicAPI) SaveNewWakuNode(request *requests.SaveNewWakuNode) error {
return api.service.messenger.SaveNewWakuNode(request)
}
func (api *PublicAPI) SetCustomizationColor(ctx context.Context, request *requests.SetCustomizationColor) error {
return api.service.messenger.SetCustomizationColor(ctx, request)
}