From fe25d97052e66d5364ceddec867bf2a8b6e836a7 Mon Sep 17 00:00:00 2001 From: Andrey Bocharnikov Date: Fri, 24 May 2024 14:54:12 +0400 Subject: [PATCH] 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 --- nodecfg/node_config.go | 10 +++ protocol/messenger_settings.go | 11 +++ protocol/messenger_validate_requests_test.go | 41 +++++++++ protocol/node_config_persistence_test.go | 94 ++++++++++++++++++++ protocol/requests/save_new_waku_node.go | 25 ++++++ protocol/requests/set_max_log_backups.go | 5 ++ services/ext/api.go | 8 ++ 7 files changed, 194 insertions(+) create mode 100644 protocol/messenger_validate_requests_test.go create mode 100644 protocol/node_config_persistence_test.go create mode 100644 protocol/requests/save_new_waku_node.go create mode 100644 protocol/requests/set_max_log_backups.go diff --git a/nodecfg/node_config.go b/nodecfg/node_config.go index edb7aa19e..681037348 100644 --- a/nodecfg/node_config.go +++ b/nodecfg/node_config.go @@ -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 { diff --git a/protocol/messenger_settings.go b/protocol/messenger_settings.go index bb0515642..a423f0f44 100644 --- a/protocol/messenger_settings.go +++ b/protocol/messenger_settings.go @@ -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 diff --git a/protocol/messenger_validate_requests_test.go b/protocol/messenger_validate_requests_test.go new file mode 100644 index 000000000..7dbe200ab --- /dev/null +++ b/protocol/messenger_validate_requests_test.go @@ -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) +} diff --git a/protocol/node_config_persistence_test.go b/protocol/node_config_persistence_test.go new file mode 100644 index 000000000..a8c3f12a2 --- /dev/null +++ b/protocol/node_config_persistence_test.go @@ -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) +} diff --git a/protocol/requests/save_new_waku_node.go b/protocol/requests/save_new_waku_node.go new file mode 100644 index 000000000..9d0155a77 --- /dev/null +++ b/protocol/requests/save_new_waku_node.go @@ -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 +} diff --git a/protocol/requests/set_max_log_backups.go b/protocol/requests/set_max_log_backups.go new file mode 100644 index 000000000..061bfd4a8 --- /dev/null +++ b/protocol/requests/set_max_log_backups.go @@ -0,0 +1,5 @@ +package requests + +type SetMaxLogBackups struct { + MaxLogBackups uint `json:"maxLogBackups" validate:"omitempty,gte=0"` +} diff --git a/services/ext/api.go b/services/ext/api.go index 2365a363f..40f1bce05 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -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) }