From 457197c23c232132236e59fca025318cd4872467 Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 15 Sep 2016 16:48:53 +0300 Subject: [PATCH] updated the statusgo refactoring changes Former-commit-id: fe1f0b689f038de739fe43ee9a55f515342a7b37 --- .../react-native-status/android/build.gradle | 2 +- .../com/statusim/module/StatusModule.java | 7 +- .../com/statusim/module/StatusService.java | 108 +++++------------- src/status_im/components/status.cljs | 4 +- src/status_im/handlers.cljs | 15 ++- src/status_im/transactions/handlers.cljs | 7 -- 6 files changed, 45 insertions(+), 98 deletions(-) diff --git a/modules/react-native-status/android/build.gradle b/modules/react-native-status/android/build.gradle index abdaecb7c1..75fe4ed72b 100644 --- a/modules/react-native-status/android/build.gradle +++ b/modules/react-native-status/android/build.gradle @@ -14,5 +14,5 @@ android { dependencies { compile 'com.facebook.react:react-native:+' - compile(group: 'status-im', name: 'status-go', version: '0.1.0-module', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: '0.1.1', ext: 'aar') } \ No newline at end of file diff --git a/modules/react-native-status/android/src/main/java/com/statusim/module/StatusModule.java b/modules/react-native-status/android/src/main/java/com/statusim/module/StatusModule.java index 959e72061b..e4991ea0ba 100644 --- a/modules/react-native-status/android/src/main/java/com/statusim/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/com/statusim/module/StatusModule.java @@ -125,12 +125,7 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL // Geth @ReactMethod - public void startNode(Callback callback, Callback onAlreadyRunning) { - - if (StatusService.isRunning()) { - onAlreadyRunning.invoke(); - return; - } + public void startNode(Callback callback) { if (!checkAvailability()) { callback.invoke(false); diff --git a/modules/react-native-status/android/src/main/java/com/statusim/module/StatusService.java b/modules/react-native-status/android/src/main/java/com/statusim/module/StatusService.java index dd6d58c3d0..3d8ca9a205 100644 --- a/modules/react-native-status/android/src/main/java/com/statusim/module/StatusService.java +++ b/modules/react-native-status/android/src/main/java/com/statusim/module/StatusService.java @@ -8,7 +8,7 @@ import android.util.Log; import java.lang.ref.WeakReference; -import com.github.status_im.status_go.Statusgo; +import com.github.status_im.status_go.cmd.Statusgo; import java.io.File; @@ -16,7 +16,7 @@ public class StatusService extends Service { private static final String TAG = "StatusService"; - private static boolean isStatusInitialized = false; + private static boolean isNodeInitialized = false; private final Handler handler = new Handler(); private static String dataFolder; @@ -76,7 +76,7 @@ public class StatusService extends Service { super.onDestroy(); //TODO: stop geth stopNode(null); - isStatusInitialized = false; + //isNodeInitialized = false; Log.d(TAG, "Status Service stopped !"); } @@ -87,11 +87,10 @@ public class StatusService extends Service { } private boolean handleMessage(Message message) { - + Log.d(TAG, "Received service message." + message.toString()); switch (message.what) { case StatusMessages.MSG_START_NODE: - Log.d(TAG, "Received start node message." + message.toString()); startNode(message); break; @@ -135,80 +134,31 @@ public class StatusService extends Service { } private void startNode(Message message) { - - if (!isStatusInitialized) { - isStatusInitialized = true; - Log.d(TAG, "Client messenger1: " + message.replyTo.toString()); - Bundle data = message.getData(); - String callbackIdentifier = data.getString(StatusConnector.CALLBACK_IDENTIFIER); - Log.d(TAG, "Callback identifier: " + callbackIdentifier); - new StartTask(message.replyTo, callbackIdentifier).execute(); - } - } - private class StartTask extends AsyncTask { + applicationMessenger = message.replyTo; + if (!isNodeInitialized) { - String callbackIdentifier; - Messenger messenger; + File extStore = Environment.getExternalStorageDirectory(); + dataFolder = extStore.exists() ? + extStore.getAbsolutePath() + "/ethereum" : + getApplicationInfo().dataDir + "/ethereum"; + Log.d(TAG, "Starting Geth node in folder: " + dataFolder); - StartTask(Messenger messenger, String callbackIdentifier) { - - this.messenger = messenger; - this.callbackIdentifier = callbackIdentifier; - } - - protected Void doInBackground(Void... args) { - - startGeth(); - return null; - } - - protected void onPostExecute(Void results) { - onGethStarted(messenger, callbackIdentifier); - } - } - - private void onGethStarted(Messenger messenger, String callbackIdentifier) { - - Log.d(TAG, "Geth Node started"); - Message replyMessage = Message.obtain(null, StatusMessages.MSG_START_NODE, 0, 0, null); - Bundle replyData = new Bundle(); - Log.d(TAG, "Callback identifier: " + callbackIdentifier); - replyData.putString(StatusConnector.CALLBACK_IDENTIFIER, callbackIdentifier); - replyMessage.setData(replyData); - sendReply(messenger, replyMessage); - } - - private void startGeth() { - - File extStore = Environment.getExternalStorageDirectory(); - dataFolder = extStore.exists() ? - extStore.getAbsolutePath() + "/ethereum" : - getApplicationInfo().dataDir + "/ethereum"; - Log.d(TAG, "Starting background Geth Service in folder: " + dataFolder); - - try { - final File newFile = new File(dataFolder); - // todo handle error? - newFile.mkdir(); - } catch (Exception e) { - Log.e(TAG, "error making folder: " + dataFolder, e); - } - - final Runnable addPeer = new Runnable() { - public void run() { - Log.w("Geth", "adding peer"); - Statusgo.addPeer("enode://409772c7dea96fa59a912186ad5bcdb5e51b80556b3fe447d940f99d9eaadb51d4f0ffedb68efad232b52475dd7bd59b51cee99968b3cc79e2d5684b33c4090c@139.162.166.59:30303"); + try { + final File newFile = new File(dataFolder); + // todo handle error? + newFile.mkdir(); + } catch (Exception e) { + Log.e(TAG, "error making folder: " + dataFolder, e); } - }; - new Thread(new Runnable() { - public void run() { - Statusgo.StartNode(dataFolder); - } - }).start(); - - handler.postDelayed(addPeer, 5000); + Statusgo.StartNode(dataFolder); + Log.d(TAG, "Geth node started"); + Log.w(TAG, "adding peer"); + Statusgo.AddPeer("enode://409772c7dea96fa59a912186ad5bcdb5e51b80556b3fe447d940f99d9eaadb51d4f0ffedb68efad232b52475dd7bd59b51cee99968b3cc79e2d5684b33c4090c@139.162.166.59:30303"); + isNodeInitialized = true; + } + createAndSendReply(message, StatusMessages.MSG_START_NODE, null); } private void stopNode(Message message) { @@ -281,7 +231,7 @@ public class StatusService extends Service { Bundle data = message.getData(); String js = data.getString("js"); - Statusgo.initJail(js); + Statusgo.InitJail(js); Bundle replyData = new Bundle(); createAndSendReply(message, StatusMessages.MSG_JAIL_INIT, replyData); @@ -293,7 +243,7 @@ public class StatusService extends Service { String chatId = data.getString("chatId"); String js = data.getString("js"); - String result = Statusgo.parse(chatId, js); + String result = Statusgo.Parse(chatId, js); Bundle replyData = new Bundle(); replyData.putString("data", result); @@ -307,15 +257,15 @@ public class StatusService extends Service { String path = data.getString("path"); String params = data.getString("params"); - String result = Statusgo.call(chatId, path, params); + String result = Statusgo.Call(chatId, path, params); Bundle replyData = new Bundle(); replyData.putString("data", result); createAndSendReply(message, StatusMessages.MSG_JAIL_CALL, replyData); } - public static boolean isRunning() { - return isStatusInitialized; + public static boolean isNodeInitialized() { + return isNodeInitialized; } private static void createAndSendReply(Message message, int replyIdMessage, Bundle replyData) { diff --git a/src/status_im/components/status.cljs b/src/status_im/components/status.cljs index ff21d8a5dd..3449b36bd5 100644 --- a/src/status_im/components/status.cljs +++ b/src/status_im/components/status.cljs @@ -17,9 +17,9 @@ (.addListener r/device-event-emitter "gethEvent" #(dispatch [:signal-event (.-jsonEvent %)])) -(defn start-node [on-result on-already-running] +(defn start-node [on-result] (when status - (.startNode status on-result on-already-running))) + (.startNode status on-result))) (defn create-account [password on-result] (when status diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 88f71b8881..02b05fee42 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -24,7 +24,8 @@ status-im.accounts.handlers status-im.protocol.handlers [status-im.utils.datetime :as time] - status-im.transactions.handlers)) + status-im.transactions.handlers + [status-im.utils.types :as t])) ;; -- Middleware ------------------------------------------------------------ ;; @@ -112,8 +113,16 @@ (u/side-effect! (fn [db _] (log/debug "Starting node") - (status/start-node (fn [result] (node-started db result)) - #(log/debug "Geth already initialized"))))) + (status/start-node (fn [result] (node-started db result)))))) + +(register-handler :signal-event + (u/side-effect! + (fn [_ [_ event-str]] + (let [{:keys [type event]} (t/json->clj event-str)] + (case type + "transaction.queued" (dispatch [:transaction-queued event]) + "node.started" (log/debug "Event *node.started* received") + (log/debug "Event " type " not handled")))))) (register-handler :crypt-initialized (u/side-effect! diff --git a/src/status_im/transactions/handlers.cljs b/src/status_im/transactions/handlers.cljs index 74b6ff5933..a8bb07f8c8 100644 --- a/src/status_im/transactions/handlers.cljs +++ b/src/status_im/transactions/handlers.cljs @@ -80,13 +80,6 @@ (fn [db [_ hash]] (remove-pending-message db hash))) -(register-handler :signal-event - (u/side-effect! - (fn [_ [_ event-str]] - (let [{:keys [type event]} (t/json->clj event-str)] - (case type - "sendTransactionQueued" (dispatch [:transaction-queued event])))))) - (register-handler :transaction-queued (after #(dispatch [:navigate-to :confirm])) (fn [db [_ {:keys [hash args]}]]