fix fleet settings not being saved to node config

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-10-22 02:05:15 +02:00
parent 463b4111b2
commit 3528dd809a
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
7 changed files with 96 additions and 73 deletions

View File

@ -3,27 +3,15 @@
[status-im.multiaccounts.update.core :as multiaccounts.update] [status-im.multiaccounts.update.core :as multiaccounts.update]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.node.core :as node]
[status-im.utils.config :as config] [status-im.utils.config :as config]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[status-im.utils.fx :as fx]) [status-im.utils.fx :as fx]))
(:require-macros [status-im.utils.slurp :refer [slurp]]))
(defn current-fleet
[db]
(keyword (get-in db [:multiaccount :settings :fleet] config/fleet)))
(defn current-fleet-sub [settings] (defn current-fleet-sub [settings]
(keyword (or (get settings :fleet) (keyword (or (get settings :fleet)
config/fleet))) config/fleet)))
(def default-fleets (slurp "resources/config/fleets.json"))
(defn fleets [{:keys [custom-fleets]}]
(as-> [(default-fleets)] $
(mapv #(:fleets (types/json->clj %)) $)
(conj $ custom-fleets)
(reduce merge $)))
(defn format-mailserver (defn format-mailserver
[mailserver address] [mailserver address]
{:id mailserver {:id mailserver
@ -42,16 +30,18 @@
(reduce (fn [acc [fleet node-by-type]] (reduce (fn [acc [fleet node-by-type]]
(assoc acc fleet (format-mailservers (:mail node-by-type)))) (assoc acc fleet (format-mailservers (:mail node-by-type))))
{} {}
(fleets db))) (node/fleets db)))
(fx/defn show-save-confirmation (fx/defn show-save-confirmation
[{:keys [db] :as cofx} fleet] [{:keys [db] :as cofx} fleet]
{:ui/show-confirmation {:title (i18n/label :t/close-app-title) {:ui/show-confirmation
:content (i18n/label :t/change-fleet {:title (i18n/label :t/close-app-title)
{:fleet fleet}) :content (i18n/label :t/change-fleet
:confirm-button-text (i18n/label :t/close-app-button) {:fleet fleet})
:on-accept #(re-frame/dispatch [:fleet.ui/save-fleet-confirmed (keyword fleet)]) :confirm-button-text (i18n/label :t/close-app-button)
:on-cancel nil}}) :on-accept
#(re-frame/dispatch [:fleet.ui/save-fleet-confirmed (keyword fleet)])
:on-cancel nil}})
(defn nodes->fleet [nodes] (defn nodes->fleet [nodes]
(letfn [(format-nodes [nodes] (letfn [(format-nodes [nodes]
@ -79,9 +69,15 @@
(fx/defn save (fx/defn save
[{:keys [db now] :as cofx} fleet] [{:keys [db now] :as cofx} fleet]
(let [settings (get-in db [:multiaccount :settings])] (let [settings (get-in db [:multiaccount :settings])
(multiaccounts.update/update-settings cofx new-settings (if fleet
(if fleet (assoc settings :fleet fleet)
(assoc settings :fleet fleet) (dissoc settings :fleet))]
(dissoc settings :fleet)) (fx/merge cofx
{:success-event [:multiaccounts.update.callback/save-settings-success]}))) (multiaccounts.update/update-settings new-settings {})
(node/prepare-new-config
{:on-success
#(when (not= fleet
(:fleet settings))
(re-frame/dispatch
[:multiaccounts.update.callback/save-settings-success]))}))))

View File

@ -13,9 +13,9 @@
(assoc settings :log-level log-level) (assoc settings :log-level log-level)
(dissoc settings :log-level))] (dissoc settings :log-level))]
(fx/merge cofx (fx/merge cofx
(multiaccounts.update/update-settings new-settings (multiaccounts.update/update-settings new-settings {})
{}) (node/prepare-new-config {:on-success #(when (not= (node/get-log-level settings)
(node/prepare-new-config {:on-success #(when (not= (node/get-log-level settings) (node/get-log-level new-settings)) (node/get-log-level new-settings))
(re-frame/dispatch [:logout]))})))) (re-frame/dispatch [:logout]))}))))
(fx/defn show-change-log-level-confirmation (fx/defn show-change-log-level-confirmation

View File

@ -4,7 +4,7 @@
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.data-store.mailservers :as data-store.mailservers] [status-im.data-store.mailservers :as data-store.mailservers]
[status-im.ethereum.json-rpc :as json-rpc] [status-im.ethereum.json-rpc :as json-rpc]
[status-im.fleet.core :as fleet] [status-im.node.core :as node]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.mailserver.constants :as constants] [status-im.mailserver.constants :as constants]
[status-im.mailserver.topics :as mailserver.topics] [status-im.mailserver.topics :as mailserver.topics]
@ -44,13 +44,13 @@
(= (:mailserver/current-id db) id)) (= (:mailserver/current-id db) id))
(defn fetch [{:keys [db] :as cofx} id] (defn fetch [{:keys [db] :as cofx} id]
(get-in db [:mailserver/mailservers (fleet/current-fleet db) id])) (get-in db [:mailserver/mailservers (node/current-fleet-key db) id]))
(defn fetch-current [{:keys [db] :as cofx}] (defn fetch-current [{:keys [db] :as cofx}]
(fetch cofx (:mailserver/current-id db))) (fetch cofx (:mailserver/current-id db)))
(defn preferred-mailserver-id [{:keys [db] :as cofx}] (defn preferred-mailserver-id [{:keys [db] :as cofx}]
(get-in db [:multiaccount :settings :mailserver (fleet/current-fleet db)])) (get-in db [:multiaccount :settings :mailserver (node/current-fleet-key db)]))
(defn- round-robin (defn- round-robin
"Find the choice and pick the next one, default to first if not found" "Find the choice and pick the next one, default to first if not found"
@ -71,7 +71,7 @@
"Use the preferred mailserver if set & exists, otherwise picks one randomly "Use the preferred mailserver if set & exists, otherwise picks one randomly
if current-id is not set, else round-robin" if current-id is not set, else round-robin"
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [current-fleet (fleet/current-fleet db) (let [current-fleet (node/current-fleet-key db)
current-id (:mailserver/current-id db) current-id (:mailserver/current-id db)
preference (preferred-mailserver-id cofx) preference (preferred-mailserver-id cofx)
choices (-> db :mailserver/mailservers current-fleet keys)] choices (-> db :mailserver/mailservers current-fleet keys)]
@ -162,7 +162,7 @@
(fx/defn generate-mailserver-symkey (fx/defn generate-mailserver-symkey
[{:keys [db] :as cofx} {:keys [password id] :as mailserver}] [{:keys [db] :as cofx} {:keys [password id] :as mailserver}]
(let [current-fleet (fleet/current-fleet db)] (let [current-fleet (node/current-fleet-key db)]
{:db (assoc-in db [:mailserver/mailservers current-fleet id :generating-sym-key?] true) {:db (assoc-in db [:mailserver/mailservers current-fleet id :generating-sym-key?] true)
:shh/generate-sym-key-from-password :shh/generate-sym-key-from-password
[{:password password [{:password password
@ -414,7 +414,7 @@
add sym-key to the mailserver in app-db and request messages if add sym-key to the mailserver in app-db and request messages if
mailserver is ready" mailserver is ready"
[{:keys [db] :as cofx} {:keys [id]} sym-key-id] [{:keys [db] :as cofx} {:keys [id]} sym-key-id]
(let [current-fleet (fleet/current-fleet db)] (let [current-fleet (node/current-fleet-key db)]
(fx/merge cofx (fx/merge cofx
{:db (-> db {:db (-> db
(assoc-in [:mailserver/mailservers current-fleet id :sym-key-id] sym-key-id) (assoc-in [:mailserver/mailservers current-fleet id :sym-key-id] sym-key-id)
@ -427,7 +427,7 @@
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(when-not (zero? (:peers-count db)) (when-not (zero? (:peers-count db))
(if-let [preferred-mailserver (preferred-mailserver-id cofx)] (if-let [preferred-mailserver (preferred-mailserver-id cofx)]
(let [current-fleet (fleet/current-fleet db)] (let [current-fleet (node/current-fleet-key db)]
{:db {:db
(update-mailserver-state db :error) (update-mailserver-state db :error)
:ui/show-confirmation :ui/show-confirmation
@ -976,7 +976,7 @@
[{{:mailserver.edit/keys [mailserver] :keys [multiaccount] :as db} :db [{{:mailserver.edit/keys [mailserver] :keys [multiaccount] :as db} :db
random-id-generator :random-id-generator :as cofx}] random-id-generator :random-id-generator :as cofx}]
(let [{:keys [name url id]} mailserver (let [{:keys [name url id]} mailserver
current-fleet (fleet/current-fleet db) current-fleet (node/current-fleet-key db)
mailserver (build mailserver (build
(or (:value id) (or (:value id)
(keyword (string/replace (random-id-generator) "-" ""))) (keyword (string/replace (random-id-generator) "-" "")))
@ -1008,7 +1008,7 @@
[{:keys [db] :as cofx} id] [{:keys [db] :as cofx} id]
(if (can-delete? cofx id) (if (can-delete? cofx id)
{:db (update-in db {:db (update-in db
[:mailserver/mailservers (fleet/current-fleet db)] [:mailserver/mailservers (node/current-fleet-key db)]
dissoc id) dissoc id)
::json-rpc/call ::json-rpc/call
[{:method "mailservers_deleteMailserver" [{:method "mailservers_deleteMailserver"
@ -1020,7 +1020,7 @@
(fx/defn show-connection-confirmation (fx/defn show-connection-confirmation
[{:keys [db]} mailserver-id] [{:keys [db]} mailserver-id]
(let [current-fleet (fleet/current-fleet db)] (let [current-fleet (node/current-fleet-key db)]
{:ui/show-confirmation {:ui/show-confirmation
{:title (i18n/label :t/close-app-title) {:title (i18n/label :t/close-app-title)
:content (i18n/label :t/connect-mailserver-content :content (i18n/label :t/connect-mailserver-content
@ -1058,7 +1058,7 @@
(fx/defn unpin (fx/defn unpin
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [current-fleet (fleet/current-fleet db) (let [current-fleet (node/current-fleet-key db)
settings (get-in db [:multiaccount :settings])] settings (get-in db [:multiaccount :settings])]
(fx/merge cofx (fx/merge cofx
(multiaccounts.update/update-settings (update settings :mailserver dissoc current-fleet) (multiaccounts.update/update-settings (update settings :mailserver dissoc current-fleet)
@ -1067,7 +1067,7 @@
(fx/defn pin (fx/defn pin
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [current-fleet (fleet/current-fleet db) (let [current-fleet (node/current-fleet-key db)
mailserver-id (:mailserver/current-id db) mailserver-id (:mailserver/current-id db)
settings (get-in db [:multiaccount :settings])] settings (get-in db [:multiaccount :settings])]
(fx/merge cofx (fx/merge cofx

View File

@ -11,6 +11,7 @@
[status-im.fleet.core :as fleet] [status-im.fleet.core :as fleet]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.node.core :as node]
[status-im.protocol.core :as protocol] [status-im.protocol.core :as protocol]
[status-im.stickers.core :as stickers] [status-im.stickers.core :as stickers]
[status-im.ui.screens.mobile-network-settings.events :as mobile-network] [status-im.ui.screens.mobile-network-settings.events :as mobile-network]
@ -34,7 +35,7 @@
(def contract-fleet? #{:eth.contract}) (def contract-fleet? #{:eth.contract})
(defn fetch-nodes [current-fleet resolve reject] (defn fetch-nodes [current-fleet resolve reject]
(let [default-nodes (-> (fleet/fleets {}) (let [default-nodes (-> (node/fleets {})
(get-in [:eth.beta :mail]) (get-in [:eth.beta :mail])
vals)] vals)]
(if config/contract-nodes-enabled? (if config/contract-nodes-enabled?

View File

@ -2,12 +2,12 @@
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.ethereum.json-rpc :as json-rpc] [status-im.ethereum.json-rpc :as json-rpc]
[status-im.fleet.core :as fleet]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.utils.config :as config] [status-im.utils.config :as config]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.utils.platform :as utils.platform] [status-im.utils.platform :as utils.platform]
[status-im.utils.types :as types])) [status-im.utils.types :as types])
(:require-macros [status-im.utils.slurp :refer [slurp]]))
(defn- add-custom-bootnodes [config network all-bootnodes] (defn- add-custom-bootnodes [config network all-bootnodes]
(let [bootnodes (as-> all-bootnodes $ (let [bootnodes (as-> all-bootnodes $
@ -28,7 +28,8 @@
:LogEnabled true))) :LogEnabled true)))
(defn get-network-genesis-hash-prefix (defn get-network-genesis-hash-prefix
"returns the hex representation of the first 8 bytes of a network's genesis hash" "returns the hex representation of the first 8 bytes of
a network's genesis hash"
[network] [network]
(case network (case network
1 "d4e56740f876aef8" 1 "d4e56740f876aef8"
@ -74,13 +75,30 @@
(if utils.platform/desktop? "" (if utils.platform/desktop? ""
config/log-level-status-go))) config/log-level-status-go)))
(def default-fleets (slurp "resources/config/fleets.json"))
(defn fleets [{:keys [custom-fleets]}]
(as-> [(default-fleets)] $
(mapv #(:fleets (types/json->clj %)) $)
(conj $ custom-fleets)
(reduce merge $)))
(defn current-fleet-key [db]
(keyword (get-in db [:multiaccount :settings :fleet]
config/fleet)))
(defn get-current-fleet
[db]
(get (fleets db)
(current-fleet-key db)))
(defn- get-multiaccount-node-config (defn- get-multiaccount-node-config
[{:keys [multiaccount :networks/networks :networks/current-network] [{:keys [multiaccount :networks/networks :networks/current-network]
:or {current-network config/default-network :or {current-network config/default-network
networks constants/default-networks} networks constants/default-networks}
:as db}] :as db}]
(let [current-fleet-key (fleet/current-fleet db) (let [current-fleet-key (current-fleet-key db)
current-fleet (get (fleet/fleets db) current-fleet-key) current-fleet (get-current-fleet db)
rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet))) rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet)))
{:keys [installation-id settings bootnodes] {:keys [installation-id settings bootnodes]
:or {settings constants/default-multiaccount-settings}} multiaccount :or {settings constants/default-multiaccount-settings}} multiaccount
@ -98,9 +116,14 @@
:Rendezvous (not (empty? rendezvous-nodes)) :Rendezvous (not (empty? rendezvous-nodes))
:ClusterConfig {:Enabled true :ClusterConfig {:Enabled true
:Fleet (name current-fleet-key) :Fleet (name current-fleet-key)
:BootNodes (pick-nodes 4 (vals (:boot current-fleet))) :BootNodes
:TrustedMailServers (pick-nodes 6 (vals (:mail current-fleet))) (pick-nodes 4 (vals (:boot current-fleet)))
:StaticNodes (into (pick-nodes 2 (vals (:whisper current-fleet))) (vals (:static current-fleet))) :TrustedMailServers
(pick-nodes 6 (vals (:mail current-fleet)))
:StaticNodes
(into (pick-nodes 2
(vals (:whisper current-fleet)))
(vals (:static current-fleet)))
:RendezvousNodes rendezvous-nodes}) :RendezvousNodes rendezvous-nodes})
:always :always
@ -112,15 +135,16 @@
:LightClient true :LightClient true
:MinimumPoW 0.001 :MinimumPoW 0.001
:EnableNTPSync true} :EnableNTPSync true}
:ShhextConfig {:BackupDisabledDataDir (utils.platform/no-backup-directory) :ShhextConfig
:InstallationID installation-id {:BackupDisabledDataDir (utils.platform/no-backup-directory)
:MaxMessageDeliveryAttempts config/max-message-delivery-attempts :InstallationID installation-id
:MailServerConfirmations config/mailserver-confirmations-enabled? :MaxMessageDeliveryAttempts config/max-message-delivery-attempts
:DataSyncEnabled (boolean datasync?) :MailServerConfirmations config/mailserver-confirmations-enabled?
:DisableGenericDiscoveryTopic (boolean disable-discovery-topic?) :DataSyncEnabled (boolean datasync?)
:SendV1Messages (boolean v1-messages?) :DisableGenericDiscoveryTopic (boolean disable-discovery-topic?)
:PFSEnabled true} :SendV1Messages (boolean v1-messages?)
:RequireTopics (get-topics current-network) :PFSEnabled true}
:RequireTopics (get-topics current-network)
:StatusAccountsConfig {:Enabled true}) :StatusAccountsConfig {:Enabled true})
(and (and
@ -138,8 +162,9 @@
(fx/defn save-new-config (fx/defn save-new-config
"Saves a new status-go config for the current account "Saves a new status-go config for the current account
This RPC method is the only way to change the node config of an account. This RPC method is the only way to change the node config of an account.
NOTE: it is better used indirectly through `prepare-new-config`, which will take NOTE: it is better used indirectly through `prepare-new-config`,
care of building up the proper config based on settings in app-db" which will take care of building up the proper config based on settings in
app-db"
{:events [::save-new-config]} {:events [::save-new-config]}
[{:keys [db]} config {:keys [on-success]}] [{:keys [db]} config {:keys [on-success]}]
{::json-rpc/call [{:method "settings_saveNodeConfig" {::json-rpc/call [{:method "settings_saveNodeConfig"
@ -150,9 +175,10 @@
"Use this function to apply settings to the current account node config" "Use this function to apply settings to the current account node config"
[{:keys [db]} {:keys [on-success]}] [{:keys [db]} {:keys [on-success]}]
{::prepare-new-config [(get-new-config db) {::prepare-new-config [(get-new-config db)
#(re-frame/dispatch [::save-new-config % {:on-success on-success}])]}) #(re-frame/dispatch
[::save-new-config % {:on-success on-success}])]})
(re-frame/reg-fx (re-frame/reg-fx
::prepare-new-config ::prepare-new-config
(fn [[config callback]] (fn [[config callback]]
(status/prepare-dir-and-update-config config callback))) (status/prepare-dir-and-update-config config callback)))

View File

@ -7,7 +7,7 @@
[status-im.ui.components.status-bar.view :as status-bar] [status-im.ui.components.status-bar.view :as status-bar]
[status-im.ui.components.toolbar.view :as toolbar] [status-im.ui.components.toolbar.view :as toolbar]
[status-im.ui.screens.fleet-settings.styles :as styles] [status-im.ui.screens.fleet-settings.styles :as styles]
[status-im.fleet.core :as fleet-core] [status-im.node.core :as node]
[status-im.utils.platform :as platform]) [status-im.utils.platform :as platform])
(:require-macros [status-im.utils.views :as views])) (:require-macros [status-im.utils.views :as views]))
@ -35,7 +35,7 @@
fleet]]]]))) fleet]]]])))
(defn fleets [custom-fleets] (defn fleets [custom-fleets]
(map name (keys (fleet-core/fleets {:custom-fleets custom-fleets})))) (map name (keys (node/fleets {:custom-fleets custom-fleets}))))
(views/defview fleet-settings [] (views/defview fleet-settings []
(views/letsubs [custom-fleets [:fleets/custom-fleets] (views/letsubs [custom-fleets [:fleets/custom-fleets]

View File

@ -1,7 +1,8 @@
(ns status-im.test.fleet.core (ns status-im.test.fleet.core
(:require [cljs.test :refer-macros [deftest is testing]] (:require [cljs.test :refer-macros [deftest is testing]]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.fleet.core :as fleet])) [status-im.fleet.core :as fleet]
[status-im.node.core :as node]))
(deftest fleets-test (deftest fleets-test
(testing "not passing any extra fleet" (testing "not passing any extra fleet"
@ -12,17 +13,17 @@
:eth.test :eth.test
:eth.contract} :eth.contract}
(into #{} (into #{}
(keys (fleet/fleets {}))))))) (keys (node/fleets {})))))))
(testing "passing a custom fleet" (testing "passing a custom fleet"
(testing "it sets the custom fleet" (testing "it sets the custom fleet"
(is (= {:mail {"a" "a"} (is (= {:mail {"a" "a"}
:whisper {"w" "w"} :whisper {"w" "w"}
:boot {"b" "b"}} :boot {"b" "b"}}
(:custom-fleet (:custom-fleet
(fleet/fleets {:custom-fleets {:custom-fleet (node/fleets {:custom-fleets {:custom-fleet
{:mail {"a" "a"} {:mail {"a" "a"}
:whisper {"w" "w"} :whisper {"w" "w"}
:boot {"b" "b"}}}}))))))) :boot {"b" "b"}}}})))))))
(deftest set-nodes-test (deftest set-nodes-test
(testing "set-nodes" (testing "set-nodes"
@ -47,4 +48,3 @@
:password constants/mailserver-password :password constants/mailserver-password
:address "c"}} :address "c"}}
actual-mailservers)))))) actual-mailservers))))))