[#6952] Fix odd app behavior after quick logout

This commit changes the way how/when the node is started/stopped:
1. `node` is not started on the app startup
2. When the user presses "Sign in" button the node is started
   with user specific configs (`InstallationID`, custom bootnodes, etc),
   and only after that `Login` call is performed.
3. When the user creates a new account, at first the node is started
   with default params (the same as would be used when user signs into
   the app after account creation whithout changing any setting), then
   `CreateAccount` call happens, then `Login`.
4. When the user restores their account, the flow is the same as `3`
   but with `RecoverAccount` instead of `CreateAccount`
5. When the user logs out the node is stopped. That's it.
This commit is contained in:
Roman Volosovskyi 2018-12-07 22:55:33 +02:00
parent 52b7bcd291
commit 07e8f6908d
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
10 changed files with 106 additions and 104 deletions

View File

@ -19,7 +19,8 @@
[status-im.utils.signing-phrase.core :as signing-phrase] [status-im.utils.signing-phrase.core :as signing-phrase]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.utils.fx :as fx])) [status-im.utils.fx :as fx]
[status-im.node.core :as node]))
(defn get-signing-phrase [cofx] (defn get-signing-phrase [cofx]
(assoc cofx :signing-phrase (signing-phrase/generate))) (assoc cofx :signing-phrase (signing-phrase/generate)))
@ -33,10 +34,17 @@
#(re-frame/dispatch [:accounts.create.callback/create-account-success (types/json->clj %) password]))) #(re-frame/dispatch [:accounts.create.callback/create-account-success (types/json->clj %) password])))
;;;; Handlers ;;;; Handlers
(defn create-account
(defn create-account [{{:accounts/keys [create] :as db} :db}] [{:keys [db random-guid-generator] :as cofx}]
{:db (update db :accounts/create assoc :step :account-creating :error nil) (fx/merge
:accounts.create/create-account (:password create)}) cofx
{:db (-> db
(update :accounts/create assoc
:step :account-creating
:error nil)
(assoc :node/on-ready :create-account
:accounts/new-installation-id (random-guid-generator)))}
(node/initialize nil)))
(fx/defn add-account (fx/defn add-account
"Takes db and new account, creates map of effects describing adding account to database and realm" "Takes db and new account, creates map of effects describing adding account to database and realm"
@ -51,14 +59,13 @@
:data-store/base-tx [(accounts-store/save-account-tx enriched-account)]})) :data-store/base-tx [(accounts-store/save-account-tx enriched-account)]}))
(fx/defn on-account-created (fx/defn on-account-created
[{:keys [random-guid-generator [{:keys [signing-phrase
signing-phrase
status status
db] :as cofx} db] :as cofx}
{:keys [pubkey address mnemonic]} password seed-backed-up] {:keys [pubkey address mnemonic installation-id]} password seed-backed-up]
(let [normalized-address (utils.hex/normalize-hex address) (let [normalized-address (utils.hex/normalize-hex address)
account {:public-key pubkey account {:public-key pubkey
:installation-id (random-guid-generator) :installation-id (or installation-id (get-in db [:accounts/new-installation-id]))
:address normalized-address :address normalized-address
:name (gfycat/generate-gfy pubkey) :name (gfycat/generate-gfy pubkey)
:status status :status status
@ -72,11 +79,11 @@
(log/debug "account-created") (log/debug "account-created")
(when-not (string/blank? pubkey) (when-not (string/blank? pubkey)
(fx/merge cofx (fx/merge cofx
{:db (assoc db :accounts/login {:address normalized-address {:db (assoc db :accounts/login {:address normalized-address
:password password :password password
:processing true})} :processing true})}
(add-account account) (add-account account)
(accounts.login/user-login))))) (accounts.login/user-login true)))))
(defn reset-account-creation [{db :db}] (defn reset-account-creation [{db :db}]
{:db (update db :accounts/create assoc {:db (update db :accounts/create assoc

View File

@ -55,14 +55,16 @@
(models.wallet/update-wallet) (models.wallet/update-wallet)
(transactions/start-sync))) (transactions/start-sync)))
(fx/defn user-login [{:keys [db] :as cofx}] (fx/defn user-login [{:keys [db] :as cofx} create-database?]
(let [{:keys [address password save-password?]} (accounts.db/credentials cofx)] (let [{:keys [address password save-password?]} (accounts.db/credentials cofx)]
(fx/merge (fx/merge
cofx cofx
(merge (merge
{:db (assoc-in db [:accounts/login :processing] true) {:db (-> db
(assoc-in [:accounts/login :processing] true)
(assoc :node/on-ready :login))
:accounts.login/clear-web-data nil :accounts.login/clear-web-data nil
:data-store/change-account [address password false]} :data-store/change-account [address password create-database?]}
(when save-password? (when save-password?
{:keychain/save-user-password [address password]}))))) {:keychain/save-user-password [address password]})))))
@ -199,7 +201,7 @@
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:accounts/login :password] password)} {:db (assoc-in db [:accounts/login :password] password)}
(navigation/navigate-to-cofx :progress nil) (navigation/navigate-to-cofx :progress nil)
(user-login)) (user-login false))
(navigation/navigate-to-clean cofx :login nil))) (navigation/navigate-to-clean cofx :login nil)))
(re-frame/reg-fx (re-frame/reg-fx

View File

@ -2,20 +2,21 @@
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.transport.core :as transport] [status-im.transport.core :as transport]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.models.transactions :as transactions])) [status-im.models.transactions :as transactions]
[status-im.node.core :as node]
[status-im.init.core :as init]))
(fx/defn logout (fx/defn logout
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [{:transport/keys [chats]} db] (fx/merge cofx
(fx/merge cofx {:keychain/clear-user-password (get-in db [:account/account :address])
{:keychain/clear-user-password (get-in db [:account/account :address]) :dev-server/stop nil}
:dev-server/stop nil (transactions/stop-sync)
:keychain/get-encryption-key [:init.callback/get-encryption-key-success]} (transport/stop-whisper)
(transactions/stop-sync) (init/initialize-app-db)
(navigation/navigate-to-clean :login {}) (init/load-accounts-and-initialize-views)
(transport/stop-whisper)))) (node/stop)))
(fx/defn show-logout-confirmation [_] (fx/defn show-logout-confirmation [_]
{:ui/show-confirmation {:ui/show-confirmation

View File

@ -10,7 +10,8 @@
[status-im.utils.identicon :as identicon] [status-im.utils.identicon :as identicon]
[status-im.utils.security :as security] [status-im.utils.security :as security]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[status-im.utils.fx :as fx])) [status-im.utils.fx :as fx]
[status-im.node.core :as node]))
(defn check-password-errors [password] (defn check-password-errors [password]
(cond (string/blank? password) :required-field (cond (string/blank? password) :required-field
@ -80,10 +81,15 @@
{:db (dissoc db :accounts/recover)} {:db (dissoc db :accounts/recover)}
(validate-recover-result data password)))) (validate-recover-result data password))))
(fx/defn recover-account [{:keys [db]}] (fx/defn recover-account
(let [{:keys [password passphrase]} (:accounts/recover db)] [{:keys [db random-guid-generator] :as cofx}]
{:db (assoc-in db [:accounts/recover :processing?] true) (fx/merge
:accounts.recover/recover-account [(security/mask-data passphrase) password]})) cofx
{:db (-> db
(assoc-in [:accounts/recover :processing?] true)
(assoc :node/on-ready :recover-account)
(assoc :accounts/new-installation-id (random-guid-generator)))}
(node/initialize nil)))
(fx/defn recover-account-with-checks [{:keys [db] :as cofx}] (fx/defn recover-account-with-checks [{:keys [db] :as cofx}]
(let [{:keys [passphrase processing?]} (:accounts/recover db)] (let [{:keys [passphrase processing?]} (:accounts/recover db)]

View File

@ -118,10 +118,13 @@
(re-frame/inject-cofx :data-store/all-browsers) (re-frame/inject-cofx :data-store/all-browsers)
(re-frame/inject-cofx :data-store/all-dapp-permissions)] (re-frame/inject-cofx :data-store/all-dapp-permissions)]
(fn [{:keys [db] :as cofx} [_ address]] (fn [{:keys [db] :as cofx} [_ address]]
(fx/merge (let [{:node/keys [status]} db]
cofx (fx/merge
(node/initialize (get-in db [:accounts/login :address])) cofx
(init/initialize-account address)))) (if (= status :started)
(accounts.login/login)
(node/initialize (get-in db [:accounts/login :address])))
(init/initialize-account address)))))
(handlers/register-handler-fx (handlers/register-handler-fx
:init.callback/keychain-reset :init.callback/keychain-reset
@ -176,6 +179,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.create.ui/next-step-pressed :accounts.create.ui/next-step-pressed
[(re-frame/inject-cofx :random-guid-generator)]
(fn [cofx [_ step password password-confirm]] (fn [cofx [_ step password password-confirm]]
(accounts.create/next-step cofx step password password-confirm))) (accounts.create/next-step cofx step password password-confirm)))
@ -231,11 +235,13 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.recover.ui/sign-in-button-pressed :accounts.recover.ui/sign-in-button-pressed
[(re-frame/inject-cofx :random-guid-generator)]
(fn [cofx _] (fn [cofx _]
(accounts.recover/recover-account-with-checks cofx))) (accounts.recover/recover-account-with-checks cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.recover.ui/recover-account-confirmed :accounts.recover.ui/recover-account-confirmed
[(re-frame/inject-cofx :random-guid-generator)]
(fn [cofx _] (fn [cofx _]
(accounts.recover/recover-account cofx))) (accounts.recover/recover-account cofx)))
@ -252,7 +258,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.login.ui/password-input-submitted :accounts.login.ui/password-input-submitted
(fn [cofx _] (fn [cofx _]
(accounts.login/user-login cofx))) (accounts.login/user-login cofx false)))
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.login.callback/login-success :accounts.login.callback/login-success
@ -293,6 +299,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.logout.ui/logout-confirmed :accounts.logout.ui/logout-confirmed
[(re-frame/inject-cofx :data-store/get-all-accounts)]
(fn [cofx _] (fn [cofx _]
(accounts.logout/logout cofx))) (accounts.logout/logout cofx)))
@ -300,6 +307,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:accounts.update.callback/save-settings-success :accounts.update.callback/save-settings-success
[(re-frame/inject-cofx :data-store/get-all-accounts)]
(fn [cofx _] (fn [cofx _]
(accounts.logout/logout cofx))) (accounts.logout/logout cofx)))

View File

@ -99,8 +99,7 @@
[cofx encryption-key] [cofx encryption-key]
(fx/merge cofx (fx/merge cofx
{:init/init-store encryption-key} {:init/init-store encryption-key}
(initialize-app-db) (initialize-app-db)))
(node/initialize nil)))
(fx/defn set-device-uuid (fx/defn set-device-uuid
[{:keys [db]} device-uuid] [{:keys [db]} device-uuid]

View File

@ -77,11 +77,13 @@
current-fleet-key (fleet/current-fleet db address) current-fleet-key (fleet/current-fleet db address)
current-fleet (get fleet/fleets current-fleet-key) current-fleet (get fleet/fleets current-fleet-key)
rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet))) rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet)))
{:keys [network {:keys [network installation-id settings bootnodes networks]}
installation-id (merge
settings {:network config/default-network
bootnodes :networks (:networks/networks db)
networks]} (get accounts address) :settings (constants/default-account-settings)
:installation-id (get db :accounts/new-installation-id)}
(get accounts address))
use-custom-bootnodes (get-in settings [:bootnodes network]) use-custom-bootnodes (get-in settings [:bootnodes network])
log-level (or (:log-level settings) log-level (or (:log-level settings)
config/log-level-status-go)] config/log-level-status-go)]
@ -122,13 +124,6 @@
:always :always
(add-log-level log-level)))) (add-log-level log-level))))
(defn get-node-config [db network]
(-> (get-in (:networks/networks db) [network :config])
(get-base-node-config)
(assoc :PFSEnabled false
:NoDiscovery true)
(add-log-level config/log-level-status-go)))
(fx/defn update-sync-state (fx/defn update-sync-state
[{:keys [db]} error sync-state] [{:keys [db]} error sync-state]
{:db (assoc db :node/chain-sync-state {:db (assoc db :node/chain-sync-state
@ -146,9 +141,7 @@
(let [network (if address (let [network (if address
(get-account-network db address) (get-account-network db address)
(:network db)) (:network db))
node-config (if address node-config (get-account-node-config db address)
(get-account-node-config db address)
(get-node-config db network))
node-config-json (types/clj->json node-config)] node-config-json (types/clj->json node-config)]
(log/info "Node config: " node-config-json) (log/info "Node config: " node-config-json)
{:db (assoc db {:db (assoc db
@ -156,7 +149,7 @@
:node/status :starting) :node/status :starting)
:node/start node-config-json})) :node/start node-config-json}))
(defn stop (fx/defn stop
[{:keys [db]}] [{:keys [db]}]
{:db (assoc db :node/status :stopping) {:db (assoc db :node/status :stopping)
:node/stop nil}) :node/stop nil})
@ -165,7 +158,7 @@
[{{:node/keys [status] :as db} :db :as cofx} address] [{{:node/keys [status] :as db} :db :as cofx} address]
(let [restart {:db (assoc db :node/restart? true :node/address address)}] (let [restart {:db (assoc db :node/restart? true :node/address address)}]
(case status (case status
:started (stop cofx) :started nil
:starting (do :starting (do
(when utils.platform/desktop? (when utils.platform/desktop?
(status/stop-node)) (status/stop-node))

View File

@ -8,11 +8,13 @@
[status-im.transport.message.core :as transport.message] [status-im.transport.message.core :as transport.message]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[taoensso.timbre :as log])) [taoensso.timbre :as log]
[status-im.utils.security :as security]))
(fx/defn status-node-started (fx/defn status-node-started
[{db :db :as cofx}] [{db :db :as cofx}]
(let [{:node/keys [restart? address]} db (let [{:node/keys [restart? address on-ready]
:accounts/keys [create]} db
can-login? (and (not restart?) can-login? (and (not restart?)
(:password (accounts.db/credentials cofx)))] (:password (accounts.db/credentials cofx)))]
(fx/merge cofx (fx/merge cofx
@ -24,14 +26,19 @@
(when restart? (when restart?
(node/initialize address)) (node/initialize address))
(when can-login? (when can-login?
(accounts.login/login))))) (accounts.login/login))
(when (= :create-account on-ready)
(fn [_]
{:accounts.create/create-account (:password create)}))
(when (= :recover-account on-ready)
(fn [{:keys [db]}]
(let [{:keys [password passphrase]} (:accounts/recover db)]
{:accounts.recover/recover-account
[(security/mask-data passphrase) password]}))))))
(fx/defn status-node-stopped (fx/defn status-node-stopped
[{db :db :as cofx}] [{db :db}]
(let [{:keys [address]} (accounts.db/credentials cofx)] {:db (assoc db :node/status :stopped)})
(fx/merge cofx
{:db (assoc db :node/status :stopped)}
(node/start address))))
(fx/defn status-module-initialized [{:keys [db]}] (fx/defn status-module-initialized [{:keys [db]}]
{:db (assoc db :status-module-initialized? true) {:db (assoc db :status-module-initialized? true)

View File

@ -95,29 +95,24 @@
(models/validate-password {:db {:accounts/recover {:password "thisisapaswoord"}}})))) (models/validate-password {:db {:accounts/recover {:password "thisisapaswoord"}}}))))
(deftest recover-account (deftest recover-account
(let [new-cofx (models/recover-account {:db {:accounts/recover (let [new-cofx (models/recover-account {:random-guid-generator (constantly "random")
:db {:accounts/recover
{:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater"
:password "thisisapaswoord"}}})] :password "thisisapaswoord"}}})]
(is (= {:accounts/recover {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" (is (contains? new-cofx :node/start))
:password "thisisapaswoord" (is (= "random" (get-in new-cofx [:db :accounts/new-installation-id])))
:processing? true}} (is (= :recover-account (get-in new-cofx [:db :node/on-ready])))))
(:db new-cofx)))
(is (= security/MaskedData
(-> new-cofx :accounts.recover/recover-account first type)))
(is (= "thisisapaswoord" (-> new-cofx :accounts.recover/recover-account second)))))
(deftest recover-account-with-checks (deftest recover-account-with-checks
(let [new-cofx (models/recover-account-with-checks {:db {:accounts/recover (let [new-cofx (models/recover-account-with-checks {:random-guid-generator (constantly "random")
:db {:accounts/recover
{:passphrase "game buzz method pretty olympic fat quit display velvet unveil marine crater" {:passphrase "game buzz method pretty olympic fat quit display velvet unveil marine crater"
:password "thisisapaswoord"}}})] :password "thisisapaswoord"}}})]
(is (= {:accounts/recover {:passphrase "game buzz method pretty olympic fat quit display velvet unveil marine crater" (is (contains? new-cofx :node/start))
:password "thisisapaswoord" (is (= "random" (get-in new-cofx [:db :accounts/new-installation-id])))
:processing? true}} (is (= :recover-account (get-in new-cofx [:db :node/on-ready]))))
(:db new-cofx))) (let [new-cofx (models/recover-account-with-checks {:random-guid-generator (constantly "random")
(is (= security/MaskedData :db {:accounts/recover
(-> new-cofx :accounts.recover/recover-account first type)))
(is (= "thisisapaswoord" (-> new-cofx :accounts.recover/recover-account second))))
(let [new-cofx (models/recover-account-with-checks {:db {:accounts/recover
{:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater"
:password "thisisapaswoord"}}})] :password "thisisapaswoord"}}})]
(is (= (i18n/label :recovery-typo-dialog-title) (-> new-cofx :ui/show-confirmation :title))) (is (= (i18n/label :recovery-typo-dialog-title) (-> new-cofx :ui/show-confirmation :title)))

View File

@ -13,34 +13,18 @@
(let [address "a" (let [address "a"
cofx {:db {:accounts/accounts {address {:installation-id "id"}}}}] cofx {:db {:accounts/accounts {address {:installation-id "id"}}}}]
(testing "installation-id" (testing "installation-id"
(testing "the user is not logged in" (let [actual (parse-node-config (node/start cofx address))]
(let [actual (parse-node-config (node/start cofx nil))] (is (= "id" (:InstallationID actual)))))
(is (not (:InstallationID actual)))))
(testing "the user is logged in"
(let [actual (parse-node-config (node/start cofx address))]
(is (= "id" (:InstallationID actual))))))
(testing "pfs & group chats disabled" (testing "pfs & group chats disabled"
(with-redefs [config/pfs-encryption-enabled? false (with-redefs [config/pfs-encryption-enabled? false
config/group-chats-enabled? false] config/group-chats-enabled? false]
(testing "the user is not logged in" (let [actual (parse-node-config (node/start cofx address))]
(let [actual (parse-node-config (node/start cofx nil))] (is (not (:PFSEnabled actual)))))
(is (not (:PFSEnabled actual)))))
(testing "the user is logged in"
(let [actual (parse-node-config (node/start cofx address))]
(is (not (:PFSEnabled actual))))))
(testing "pfs is enabled" (testing "pfs is enabled"
(with-redefs [config/pfs-encryption-enabled? true] (with-redefs [config/pfs-encryption-enabled? true]
(testing "the user is not logged in" (let [actual (parse-node-config (node/start cofx address))]
(let [actual (parse-node-config (node/start cofx nil))] (is (:PFSEnabled actual)))))
(is (not (:PFSEnabled actual)))))
(testing "the user is logged in"
(let [actual (parse-node-config (node/start cofx address))]
(is (:PFSEnabled actual))))))
(testing "group chats is enabled" (testing "group chats is enabled"
(with-redefs [config/group-chats-enabled? true] (with-redefs [config/group-chats-enabled? true]
(testing "the user is not logged in" (let [actual (parse-node-config (node/start cofx address))]
(let [actual (parse-node-config (node/start cofx nil))] (is (:PFSEnabled actual))))))))
(is (not (:PFSEnabled actual)))))
(testing "the user is logged in"
(let [actual (parse-node-config (node/start cofx address))]
(is (:PFSEnabled actual)))))))))