From 4caa78883639c391b9251a574d5a312f00b55d34 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 6 Nov 2024 11:03:27 +0800 Subject: [PATCH] chore_: rename StatusBackendClient.enabled to serverEnabled --- doc/use-status-backend-server.md | 6 ++--- .../status/ethereum/module/DatabaseManager.kt | 2 +- .../ethereum/module/StatusBackendClient.kt | 26 +++++++++---------- .../java/im/status/ethereum/module/Utils.kt | 4 +-- .../ios/RCTStatus/StatusBackendClient.h | 2 +- .../ios/RCTStatus/StatusBackendClient.m | 26 +++++++++---------- .../react-native-status/ios/RCTStatus/Utils.m | 6 ++--- shadow-cljs.edn | 2 +- src/status_im/config.cljs | 10 ++++--- .../setup/status_backend_client.cljs | 6 ++--- 10 files changed, 47 insertions(+), 43 deletions(-) diff --git a/doc/use-status-backend-server.md b/doc/use-status-backend-server.md index 7d159f262f4..8ecf278431f 100644 --- a/doc/use-status-backend-server.md +++ b/doc/use-status-backend-server.md @@ -1,13 +1,13 @@ ## Solution to use Status Backend Server -`StatusBackendClient` is the entry point to use Status Backend Server. Whether it's enabled or not is controlled by `STATUS_BACKEND_ENABLED` environment variable. We need always to call `status-im.setup.status-backend-client/init` whether `STATUS_BACKEND_ENABLED` is `1` or not. If it's not enabled, the invocation to functions in `native-module.core` will be delegated to built-in status-go library, otherwise it will be delegated to status-go running in status-backend server. Currently, all functions has usages in `native-module.core` should be supported delegated to. +`StatusBackendClient` is the entry point to use Status Backend Server. We need always to call `status-im.setup.status-backend-client/init` whether `STATUS_BACKEND_SERVER_ENABLED` is `1` or not. If it's not enabled, the invocation to functions in `native-module.core` will be delegated to built-in status-go library, otherwise it will be delegated to status-go running in status-backend server. Currently, all functions has usages in `native-module.core` should be supported delegated to. related [PR](https://github.com/status-im/status-mobile/pull/21550) ## Usage ### Add environment variables to your local machine: ```shell -# enable using status backend server or not -export STATUS_BACKEND_ENABLED=1 +# enable using status backend server or not, otherwise it will use built-in status-go library +export STATUS_BACKEND_SERVER_ENABLED=1 #The host should contain an IP address and a port separated by a colon. #The port comes from your running status backend server. diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/DatabaseManager.kt b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/DatabaseManager.kt index 7e408d1c48a..5188a260b0f 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/DatabaseManager.kt +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/DatabaseManager.kt @@ -20,7 +20,7 @@ class DatabaseManager(private val reactContext: ReactApplicationContext) : React private fun getExportDBFile(): File { StatusBackendClient.getInstance()?.let { - if (it.enabled) { + if (it.serverEnabled) { return File(it.rootDataDir, exportDBFileName) } } diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusBackendClient.kt b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusBackendClient.kt index d7cc2a14000..f2829100100 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusBackendClient.kt +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusBackendClient.kt @@ -33,7 +33,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R statusgoFunction: () -> String ) { val statusBackendClient = getInstance() - if (statusBackendClient?.enabled == true) { + if (statusBackendClient?.serverEnabled == true) { val result = statusBackendClient.request(endpoint, requestBody) result.onSuccess { response -> utils.handleStatusGoResponse(response, endpoint) @@ -54,7 +54,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R callback: Callback? ) { val statusBackendClient = getInstance() - if (statusBackendClient?.enabled == true) { + if (statusBackendClient?.serverEnabled == true) { val runnable = Runnable { val result = statusBackendClient.request(endpoint, requestBody) result.onSuccess { response -> @@ -77,7 +77,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R statusgoFunction: () -> String ): String { val statusBackendClient = getInstance() - return if (statusBackendClient?.enabled == true) { + return if (statusBackendClient?.serverEnabled == true) { val result = statusBackendClient.request(endpoint, requestBody) result.getOrElse { error -> Log.e(TAG, "request to $endpoint failed", error) @@ -109,32 +109,32 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R private var webSocket: WebSocket? = null - @Volatile var enabled = false + @Volatile var serverEnabled = false @Volatile private var statusGoEndpoint: String? = null @Volatile private var signalEndpoint: String? = null @Volatile var rootDataDir: String? = null @ReactMethod fun configStatusBackendServer( - enabled: Boolean, + serverEnabled: Boolean, statusGoEndpoint: String, signalEndpoint: String, rootDataDir: String ) { - configure(enabled, statusGoEndpoint, signalEndpoint, rootDataDir) + configure(serverEnabled, statusGoEndpoint, signalEndpoint, rootDataDir) } private fun configure( - enabled: Boolean, + serverEnabled: Boolean, statusGoEndpoint: String, signalEndpoint: String, rootDataDir: String ) { - Log.d(TAG, "configure: enabled=$enabled, statusGoEndpoint=$statusGoEndpoint, " + + Log.d(TAG, "configure: serverEnabled=$serverEnabled, statusGoEndpoint=$statusGoEndpoint, " + "signalEndpoint=$signalEndpoint, rootDataDir=$rootDataDir") - this.enabled = enabled - if (enabled) { + this.serverEnabled = serverEnabled + if (serverEnabled) { this.statusGoEndpoint = statusGoEndpoint this.signalEndpoint = signalEndpoint this.rootDataDir = rootDataDir @@ -148,7 +148,7 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R } private fun connectWebSocket() { - if (!enabled || signalEndpoint == null) { + if (!serverEnabled || signalEndpoint == null) { return } @@ -173,8 +173,8 @@ class StatusBackendClient(private val reactContext: ReactApplicationContext) : R } fun request(endpoint: String, body: String): Result { - if (!enabled || statusGoEndpoint == null) { - return Result.failure(IllegalStateException("Status backend is not enabled")) + if (!serverEnabled || statusGoEndpoint == null) { + return Result.failure(IllegalStateException("Status backend server is not enabled")) } val fullUrl = "$statusGoEndpoint$endpoint" diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Utils.kt b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Utils.kt index 8b6cd330860..ffcd560ebad 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Utils.kt +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Utils.kt @@ -26,7 +26,7 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas fun getNoBackupDirectory(): String { StatusBackendClient.getInstance()?.let { client -> - if (client.enabled && client.rootDataDir != null) { + if (client.serverEnabled && client.rootDataDir != null) { return client.rootDataDir!! } } @@ -40,7 +40,7 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas fun getPublicStorageDirectory(): File? { StatusBackendClient.getInstance()?.let { client -> - if (client.enabled && client.rootDataDir != null) { + if (client.serverEnabled && client.rootDataDir != null) { return File(client.rootDataDir!!) } } diff --git a/modules/react-native-status/ios/RCTStatus/StatusBackendClient.h b/modules/react-native-status/ios/RCTStatus/StatusBackendClient.h index cba750c066f..6a4bee04ea9 100644 --- a/modules/react-native-status/ios/RCTStatus/StatusBackendClient.h +++ b/modules/react-native-status/ios/RCTStatus/StatusBackendClient.h @@ -3,7 +3,7 @@ @interface StatusBackendClient : NSObject -@property (nonatomic) BOOL enabled; +@property (nonatomic) BOOL serverEnabled; @property (nonatomic, strong) NSString *statusGoEndpoint; @property (nonatomic, strong) NSString *signalEndpoint; @property (nonatomic, strong) NSString *rootDataDir; diff --git a/modules/react-native-status/ios/RCTStatus/StatusBackendClient.m b/modules/react-native-status/ios/RCTStatus/StatusBackendClient.m index acacc35c847..38fd25b0ce1 100644 --- a/modules/react-native-status/ios/RCTStatus/StatusBackendClient.m +++ b/modules/react-native-status/ios/RCTStatus/StatusBackendClient.m @@ -4,7 +4,7 @@ @implementation StatusBackendClient { NSURLSessionWebSocketTask *_webSocket; - BOOL _enabled; + BOOL _serverEnabled; NSString *_statusGoEndpoint; NSString *_signalEndpoint; NSString *_rootDataDir; @@ -35,7 +35,7 @@ - (instancetype)init { dispatch_once(&onceToken, ^{ sharedInstance = [super init]; if (sharedInstance) { - sharedInstance->_enabled = NO; + sharedInstance->_serverEnabled = NO; sharedInstance->_statusGoEndpoint = nil; sharedInstance->_signalEndpoint = nil; sharedInstance->_rootDataDir = nil; @@ -49,8 +49,8 @@ - (id)copyWithZone:(NSZone *)zone { return self; } -- (BOOL)enabled { - return _enabled; +- (BOOL)serverEnabled { + return _serverEnabled; } - (NSString *)statusGoEndpoint { @@ -66,7 +66,7 @@ - (NSString *)rootDataDir { } - (void)connectWebSocket { - if (!self.enabled || !self.signalEndpoint) { + if (!self.serverEnabled || !self.signalEndpoint) { return; } @@ -145,23 +145,23 @@ - (void)request:(NSString *)endpoint [task resume]; } -RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)enabled +RCT_EXPORT_METHOD(configStatusBackendServer:(BOOL)serverEnabled statusGoEndpoint:(NSString *)statusGoEndpoint signalEndpoint:(NSString *)signalEndpoint rootDataDir:(NSString *)rootDataDir) { - [self configureWithEnabled:enabled + [self configureWithEnabled:serverEnabled statusGoEndpoint:statusGoEndpoint signalEndpoint:signalEndpoint rootDataDir:rootDataDir]; } -- (void)configureWithEnabled:(BOOL)enabled +- (void)configureWithEnabled:(BOOL)serverEnabled statusGoEndpoint:(NSString *)statusGoEndpoint signalEndpoint:(NSString *)signalEndpoint rootDataDir:(NSString *)rootDataDir { - _enabled = enabled; + _serverEnabled = serverEnabled; - if (enabled) { + if (serverEnabled) { _statusGoEndpoint = statusGoEndpoint; _signalEndpoint = signalEndpoint; _rootDataDir = rootDataDir; @@ -178,7 +178,7 @@ + (void)executeStatusGoRequest:(NSString *)endpoint body:(NSString *)body statusgoFunction:(NSString * (^)(void))statusgoFunction { StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled) { + if (client.serverEnabled) { [client request:endpoint body:body callback:^(NSString *response, NSError *error) { [Utils handleStatusGoResponse:response source:endpoint error:error]; }]; @@ -193,7 +193,7 @@ + (void)executeStatusGoRequestWithCallback:(NSString *)endpoint statusgoFunction:(NSString * (^)(void))statusgoFunction callback:(RCTResponseSenderBlock)callback { StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled) { + if (client.serverEnabled) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [client request:endpoint body:body callback:^(NSString *response, NSError *error) { if (error) { @@ -222,7 +222,7 @@ + (NSString *)executeStatusGoRequestWithResult:(NSString *)endpoint body:(NSString *)body statusgoFunction:(NSString * (^)(void))statusgoFunction { StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled) { + if (client.serverEnabled) { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); __block NSString *resultString = @""; diff --git a/modules/react-native-status/ios/RCTStatus/Utils.m b/modules/react-native-status/ios/RCTStatus/Utils.m index 77ab68f4ed0..e64c6c38e46 100644 --- a/modules/react-native-status/ios/RCTStatus/Utils.m +++ b/modules/react-native-status/ios/RCTStatus/Utils.m @@ -16,7 +16,7 @@ + (NSURL *)getRootUrl { NSURL *rootUrl; StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled && client.rootDataDir) { + if (client.serverEnabled && client.rootDataDir) { rootUrl = [NSURL fileURLWithPath:client.rootDataDir]; } else { rootUrl = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; @@ -75,7 +75,7 @@ + (NSString *) getKeyUID:(NSString *)jsonString { + (NSString *) getExportDbFilePath { StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled && client.rootDataDir) { + if (client.serverEnabled && client.rootDataDir) { return [client.rootDataDir stringByAppendingPathComponent:@"export.db"]; } @@ -131,7 +131,7 @@ + (void) migrateKeystore:(NSString *)accountData RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(backupDisabledDataDir) { StatusBackendClient *client = [StatusBackendClient sharedInstance]; - if (client.enabled && client.rootDataDir) { + if (client.serverEnabled && client.rootDataDir) { return client.rootDataDir; } diff --git a/shadow-cljs.edn b/shadow-cljs.edn index a2fdc2b4c51..381b4e5cd1a 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -69,7 +69,7 @@ status-im.config/INFURA_TOKEN #shadow/env "INFURA_TOKEN" status-im.config/STATUS_BUILD_PROXY_USER #shadow/env "STATUS_BUILD_PROXY_USER" status-im.config/STATUS_BUILD_PROXY_PASSWORD #shadow/env "STATUS_BUILD_PROXY_PASSWORD" - status-im.config/STATUS_BACKEND_ENABLED #shadow/env "STATUS_BACKEND_ENABLED" + status-im.config/STATUS_BACKEND_SERVER_ENABLED #shadow/env "STATUS_BACKEND_SERVER_ENABLED" status-im.config/STATUS_BACKEND_SERVER_HOST #shadow/env "STATUS_BACKEND_SERVER_HOST" status-im.config/STATUS_BACKEND_SERVER_ROOT_DATA_DIR #shadow/env "STATUS_BACKEND_SERVER_ROOT_DATA_DIR" diff --git a/src/status_im/config.cljs b/src/status_im/config.cljs index 5df16a92269..b86519d4efa 100644 --- a/src/status_im/config.cljs +++ b/src/status_im/config.cljs @@ -135,9 +135,13 @@ ;; interfere with react-native debug tools, such as inspector and Perf monitor (def enable-alert-banner? (enabled? (get-config :ENABLE_ALERT_BANNER "0"))) -(goog-define STATUS_BACKEND_ENABLED "0") -;; e.g. 127.0.0.1:58935 +;; enable using status backend server or not, otherwise it will use built-in status-go library +;; see doc/use-status-backend-server.md for more details +(goog-define STATUS_BACKEND_SERVER_ENABLED "0") +;; The host should contain an IP address and a port separated by a colon. +;; The port comes from your running status backend server. +;; If you run it by PORT=60000 make run-status-backend , then host will likely be 127.0.0.1:60000 (goog-define STATUS_BACKEND_SERVER_HOST "") ;; /path/to/root/data/dir -;; make sure it exists +;; make sure it exists, it should be in absolute path (goog-define STATUS_BACKEND_SERVER_ROOT_DATA_DIR "") diff --git a/src/status_im/setup/status_backend_client.cljs b/src/status_im/setup/status_backend_client.cljs index 1805eedbd9b..e901b78cab7 100644 --- a/src/status_im/setup/status_backend_client.cljs +++ b/src/status_im/setup/status_backend_client.cljs @@ -3,16 +3,16 @@ [status-im.config :as config])) (def default-config - {:enabled? (config/enabled? config/STATUS_BACKEND_ENABLED) + {:server-enabled? (config/enabled? config/STATUS_BACKEND_SERVER_ENABLED) :status-go-endpoint (str "http://" config/STATUS_BACKEND_SERVER_HOST "/statusgo/") :signal-endpoint (str "ws://" config/STATUS_BACKEND_SERVER_HOST "/signals") :root-data-dir config/STATUS_BACKEND_SERVER_ROOT_DATA_DIR}) (defn set-config! - [{:keys [enabled? status-go-endpoint signal-endpoint root-data-dir]}] + [{:keys [server-enabled? status-go-endpoint signal-endpoint root-data-dir]}] (when-let [client (.-StatusBackendClient (.-NativeModules react-native))] (.configStatusBackendServer client - enabled? + server-enabled? status-go-endpoint signal-endpoint root-data-dir)))