From 7c6364d37f6153d49febf1c0db851ab3666304c0 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 28 Jul 2020 14:10:42 +0300 Subject: [PATCH] [#9986] Allow to disable fetching of tx history on mobile network --- .env | 1 + .env.jenkins | 3 +- .../status/ethereum/module/StatusModule.java | 12 +++ .../ios/RCTStatus/RCTStatus.m | 14 ++++ src/status_im/ethereum/subscriptions.cljs | 40 +++++----- src/status_im/native_module/core.cljs | 8 ++ src/status_im/network/net_info.cljs | 24 ++++-- src/status_im/subs.cljs | 4 +- .../ui/screens/wallet/accounts/views.cljs | 39 +++++++++- src/status_im/utils/config.cljs | 3 +- src/status_im/wallet/core.cljs | 75 ++++++++++++++++++- status-go-version.json | 6 +- 12 files changed, 196 insertions(+), 33 deletions(-) diff --git a/.env b/.env index a6b21c1d2b10..ffbea0e33faf 100644 --- a/.env +++ b/.env @@ -22,3 +22,4 @@ STATUS_GO_ENABLE_NIMBUS=0 KEYCARD_TEST_MENU=0 QR_READ_TEST_MENU=1 ENABLE_ROOT_ALERT=1 +DISABLE_WALLET_ON_MOBILE_NETWORK=1 diff --git a/.env.jenkins b/.env.jenkins index 394258ef65ec..8f79bf85d9e4 100644 --- a/.env.jenkins +++ b/.env.jenkins @@ -19,4 +19,5 @@ PARTITIONED_TOPIC=0 CONTRACT_NODES=1 STATUS_GO_ENABLE_NIMBUS=0 KEYCARD_TEST_MENU=0 -ENABLE_ROOT_ALERT=1 \ No newline at end of file +ENABLE_ROOT_ALERT=1 +DISABLE_WALLET_ON_MOBILE_NETWORK=1 diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index 507573c0a021..03819714b90c 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -1099,6 +1099,18 @@ public void appStateChange(final String type) { Statusgo.appStateChange(type); } + @ReactMethod + public void stopWallet() { + Log.d(TAG, "StopWallet"); + Statusgo.stopWallet(); + } + + @ReactMethod + public void startWallet() { + Log.d(TAG, "StartWallet"); + Statusgo.startWallet(); + } + @ReactMethod public void setBlankPreviewFlag(final Boolean blankPreview) { final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.reactContext); diff --git a/modules/react-native-status/ios/RCTStatus/RCTStatus.m b/modules/react-native-status/ios/RCTStatus/RCTStatus.m index edd87695869e..9048ebd11919 100644 --- a/modules/react-native-status/ios/RCTStatus/RCTStatus.m +++ b/modules/react-native-status/ios/RCTStatus/RCTStatus.m @@ -740,6 +740,20 @@ - (void) migrateKeystore:(NSString *)accountData StatusgoAppStateChange(type); } +RCT_EXPORT_METHOD(stopWallet) { +#if DEBUG + NSLog(@"StopWallet() method called"); +#endif + StatusgoStopWallet(); +} + +RCT_EXPORT_METHOD(startWallet) { +#if DEBUG + NSLog(@"StartWallet() method called"); +#endif + StatusgoStartWallet(); +} + RCT_EXPORT_METHOD(setBlankPreviewFlag:(BOOL *)newValue) { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; diff --git a/src/status_im/ethereum/subscriptions.cljs b/src/status_im/ethereum/subscriptions.cljs index 0bcf6c8c9876..3f16b43980bf 100644 --- a/src/status_im/ethereum/subscriptions.cljs +++ b/src/status_im/ethereum/subscriptions.cljs @@ -1,6 +1,7 @@ (ns status-im.ethereum.subscriptions (:require [status-im.ethereum.eip55 :as eip55] [status-im.wallet.db :as wallet] + [status-im.wallet.core :as wallet.core] [status-im.ethereum.transactions.core :as transactions] [status-im.utils.fx :as fx] [taoensso.timbre :as log])) @@ -59,24 +60,27 @@ (log/debug "[wallet-subs] recent-history-fetching-ended" "accounts" accounts "block" blockNumber) - {:db (-> db - (update-in [:wallet :accounts] - wallet/remove-transactions-since-block blockNumber) - (transactions/update-fetching-status accounts :recent? false)) - :transactions/get-transfers - {:chain-tokens (:wallet/all-tokens db) - :addresses (reduce - (fn [v address] - (let [normalized-address - (eip55/address->checksum address)] - (if (contains? v normalized-address) - v - (conj v address)))) - [] - accounts) - :before-block blockNumber - :limit 20 - :historical? true}}) + (fx/merge + cofx + {:db (-> db + (update-in [:wallet :accounts] + wallet/remove-transactions-since-block blockNumber) + (transactions/update-fetching-status accounts :recent? false)) + :transactions/get-transfers + {:chain-tokens (:wallet/all-tokens db) + :addresses (reduce + (fn [v address] + (let [normalized-address + (eip55/address->checksum address)] + (if (contains? v normalized-address) + v + (conj v address)))) + [] + accounts) + :before-block blockNumber + :limit 20 + :historical? true}} + (wallet.core/restart-wallet-service true))) (fx/defn new-wallet-event [cofx {:keys [type blockNumber accounts newTransactions] :as event}] diff --git a/src/status_im/native_module/core.cljs b/src/status_im/native_module/core.cljs index f79dc81d8c5a..501d3d9999a3 100644 --- a/src/status_im/native_module/core.cljs +++ b/src/status_im/native_module/core.cljs @@ -263,6 +263,14 @@ (log/debug "[native-module] app-state-change") (.appStateChange ^js (status) state)) +(defn stop-wallet [] + (log/debug "[native-module] stop-wallet") + (.stopWallet ^js (status))) + +(defn start-wallet [] + (log/debug "[native-module] start-wallet") + (.startWallet ^js (status))) + (defn set-blank-preview-flag [flag] (log/debug "[native-module] set-blank-preview-flag") (.setBlankPreviewFlag ^js (status) flag)) diff --git a/src/status_im/network/net_info.cljs b/src/status_im/network/net_info.cljs index 8a588825ab98..2028efa1f705 100644 --- a/src/status_im/network/net_info.cljs +++ b/src/status_im/network/net_info.cljs @@ -4,7 +4,8 @@ [status-im.ui.screens.mobile-network-settings.events :as mobile-network] [status-im.utils.fx :as fx] [status-im.wallet.core :as wallet] - ["@react-native-community/netinfo" :default net-info])) + ["@react-native-community/netinfo" :default net-info] + [taoensso.timbre :as log])) (fx/defn change-network-status [{:keys [db] :as cofx} is-connected?] @@ -24,14 +25,23 @@ (fx/defn handle-network-info-change {:events [::network-info-changed]} [{:keys [db] :as cofx} {:keys [isConnected type details] :as state}] - (let [old-network-status (:network-status db) - old-network-type (:network/type db) - connectivity-status (if isConnected :online :offline)] + (let [old-network-status (:network-status db) + old-network-type (:network/type db) + connectivity-status (if isConnected :online :offline) + status-changed? (= connectivity-status old-network-status) + type-changed? (= type old-network-type)] + (log/debug "[net-info]" + "old-network-status" old-network-status + "old-network-type" old-network-type + "connectivity-status" connectivity-status + "type" type + "details" details) (fx/merge cofx - (when-not (= connectivity-status old-network-status) + (when-not status-changed? (change-network-status isConnected)) - (when-not (= type old-network-type) - (change-network-type old-network-type type (:is-connection-expensive details)))))) + (when-not type-changed? + (change-network-type old-network-type type (:is-connection-expensive details))) + (wallet/restart-wallet-service type-changed?)))) (defn add-net-info-listener [] (when net-info diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index d0f9936e98d8..471ce0651964 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -59,6 +59,7 @@ ;;general (reg-root-key-sub :sync-state :sync-state) (reg-root-key-sub :network-status :network-status) +(reg-root-key-sub :network/type :network/type) (reg-root-key-sub :peers-count :peers-count) (reg-root-key-sub :about-app/node-info :node-info) (reg-root-key-sub :peers-summary :peers-summary) @@ -152,7 +153,8 @@ (reg-root-key-sub :wallet.transactions :wallet.transactions) (reg-root-key-sub :wallet/custom-token-screen :wallet/custom-token-screen) (reg-root-key-sub :wallet/prepare-transaction :wallet/prepare-transaction) - +(reg-root-key-sub :wallet-service/manual-setting :wallet-service/manual-setting) +(reg-root-key-sub :wallet-service/state :wallet-service/state) ;;commands (reg-root-key-sub :commands/select-account :commands/select-account) diff --git a/src/status_im/ui/screens/wallet/accounts/views.cljs b/src/status_im/ui/screens/wallet/accounts/views.cljs index 4aedabc29b25..c2be953261b3 100644 --- a/src/status_im/ui/screens/wallet/accounts/views.cljs +++ b/src/status_im/ui/screens/wallet/accounts/views.cljs @@ -12,7 +12,9 @@ [status-im.ui.screens.wallet.accounts.styles :as styles] [status-im.utils.utils :as utils.utils] [status-im.wallet.utils :as wallet.utils] - [status-im.keycard.login :as keycard.login]) + [status-im.keycard.login :as keycard.login] + [status-im.wallet.core :as wallet.core] + [status-im.utils.config :as config]) (:require-macros [status-im.utils.views :as views])) (views/defview account-card [{:keys [name color address type] :as account}] @@ -180,6 +182,39 @@ [quo/text {:color :secondary} (i18n/label :t/wallet-total-value)]])])) +(defn wallet-service-state [] + (let [status @(re-frame/subscribe [:network-status]) + type @(re-frame/subscribe [:network/type]) + setting @(re-frame/subscribe [:wallet-service/manual-setting]) + state @(re-frame/subscribe [:wallet-service/state])] + [react/view + {:style {:flex 1 + :flex-direction :row}} + [react/text {:style {:flex 1}} (str "setting: " setting)] + [react/text + {:style {:background-color :grey + :flex 1}} + status] + [react/text type] + [react/text + {:style {:background-color :grey + :flex 1}} + (str "service state: " state)] + [react/text + {:on-press #(re-frame/dispatch + [::wallet.core/toggle-force-stop-wallet-service]) + :style {:color (when (= setting :off) :red) + :background-color :green + :flex 1}} + "force stop"] + [react/text + {:on-press #(re-frame/dispatch + [::wallet.core/toggle-force-start-wallet-service]) + :style {:color (when (= setting :on) :red) + :background-color :yellow + :flex 1}} + "force start"]])) + (defn accounts-overview [] (fn [] (let [{:keys [mnemonic]} @(re-frame/subscribe [:multiaccount])] @@ -194,6 +229,8 @@ {:content (sheets/accounts-options mnemonic)}]) :icon :main-icons/more :accessibility-label :accounts-more-options}]} + (when config/disable-wallet-on-mobile-network? + [wallet-service-state]) [accounts] [assets] [react/view {:height 68}]] diff --git a/src/status_im/utils/config.cljs b/src/status_im/utils/config.cljs index 2d5d932b1b01..d0b8d302d5cb 100644 --- a/src/status_im/utils/config.cljs +++ b/src/status_im/utils/config.cljs @@ -39,7 +39,8 @@ (def commands-enabled? (enabled? (get-config :COMMANDS_ENABLED "0"))) (def keycard-test-menu-enabled? (enabled? (get-config :KEYCARD_TEST_MENU "0"))) (def qr-test-menu-enabled? (enabled? (get-config :QR_READ_TEST_MENU "0"))) - +(def disable-wallet-on-mobile-network? + (enabled? (get-config :DISABLE_WALLET_ON_MOBILE_NETWORK))) ;; CONFIG VALUES (def log-level (string/upper-case (get-config :LOG_LEVEL ""))) diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index 29b6d4798fcf..bdfee7888b94 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -24,7 +24,8 @@ [status-im.ethereum.stateofus :as stateofus] [status-im.ui.components.bottom-sheet.core :as bottom-sheet] [status-im.wallet.prices :as prices] - [status-im.wallet.utils :as wallet.utils])) + [status-im.wallet.utils :as wallet.utils] + [status-im.native-module.core :as status])) (defn get-balance [{:keys [address on-success on-error]}] @@ -601,3 +602,75 @@ :cancel-button-text (i18n/label :t/no) :on-accept #(re-frame/dispatch [:wallet.accounts/delete-account account]) :on-cancel #()}}) + +(re-frame/reg-fx + ::stop-wallet + (fn [] + (log/info "stop-wallet fx") + (status/stop-wallet))) + +(re-frame/reg-fx + ::start-wallet + (fn [] + (log/info "start-wallet fx") + (status/start-wallet))) + +(fx/defn stop-wallet + [{:keys [db] :as cofx}] + (let [] + {:db (assoc db :wallet-service/state :stopped) + ::stop-wallet nil})) + +(fx/defn start-wallet + [{:keys [db] :as cofx}] + (let [] + {:db (assoc db :wallet-service/state :started) + ::start-wallet nil})) + +(fx/defn restart-wallet-service + [{:keys [db] :as cofx} type-changed?] + (when (:multiaccount db) + (let [network-status (:network-status db) + network-type (:network/type db) + setting (:wallet-service/manual-setting db) + state (:wallet-service/state db)] + (log/info "restart-wallet-service" + "setting?" setting + "state" state + "network-type" network-type + "network-status" network-status) + (when (and config/disable-wallet-on-mobile-network? + (= network-status :online) + (nil? setting)) + (cond + (and (not= network-type "wifi") + (not= state :stopped)) + (stop-wallet cofx) + + (and (= network-type "wifi") + (= state :stopped)) + (start-wallet cofx)))))) + +(fx/defn toggle-force-stop-wallet-service + {:events [::toggle-force-stop-wallet-service]} + [{:keys [db] :as cofx}] + (let [setting (:wallet-service/manual-setting db) + new-setting (when (not= setting :off) :off)] + (fx/merge + cofx + {:db (assoc db :wallet-service/manual-setting new-setting)} + (if (= new-setting :off) + (stop-wallet) + (restart-wallet-service true))))) + +(fx/defn toggle-force-start-wallet-service + {:events [::toggle-force-start-wallet-service]} + [{:keys [db] :as cofx}] + (let [setting (:wallet-service/manual-setting db) + new-setting (when (not= setting :on) :on)] + (fx/merge + cofx + {:db (assoc db :wallet-service/manual-setting new-setting)} + (if (= new-setting :on) + (start-wallet) + (restart-wallet-service true))))) diff --git a/status-go-version.json b/status-go-version.json index 190af1b54b35..ca14f43bb30f 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -2,7 +2,7 @@ "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh ' instead", "owner": "status-im", "repo": "status-go", - "version": "v0.56.1", - "commit-sha1": "4574ab4c22ee6b662a7f87c39d0f714998c567dc", - "src-sha256": "0jd684lv7x93gxgvvhsv4ihxr5sw2rck2divsxfiql5g2c1v4alg" + "version": "stop-wallet", + "commit-sha1": "9d655f94598672b3d021da9cff911d4a3b103b2b", + "src-sha256": "074vk3qi03js8g5vn6r10q6vmwqi97nind195y1ipjggj0pk1lx5" }