Merge pull request #228 from status-im/feature/statusgo-refactor

Feature/statusgo refactor

Former-commit-id: fd329ee732
This commit is contained in:
Roman Volosovskyi 2016-09-15 17:03:10 +03:00 committed by GitHub
commit 6815221480
6 changed files with 45 additions and 98 deletions

View File

@ -14,5 +14,5 @@ android {
dependencies { dependencies {
compile 'com.facebook.react:react-native:+' 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')
} }

View File

@ -125,12 +125,7 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
// Geth // Geth
@ReactMethod @ReactMethod
public void startNode(Callback callback, Callback onAlreadyRunning) { public void startNode(Callback callback) {
if (StatusService.isRunning()) {
onAlreadyRunning.invoke();
return;
}
if (!checkAvailability()) { if (!checkAvailability()) {
callback.invoke(false); callback.invoke(false);

View File

@ -8,7 +8,7 @@ import android.util.Log;
import java.lang.ref.WeakReference; 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; import java.io.File;
@ -16,7 +16,7 @@ public class StatusService extends Service {
private static final String TAG = "StatusService"; private static final String TAG = "StatusService";
private static boolean isStatusInitialized = false; private static boolean isNodeInitialized = false;
private final Handler handler = new Handler(); private final Handler handler = new Handler();
private static String dataFolder; private static String dataFolder;
@ -76,7 +76,7 @@ public class StatusService extends Service {
super.onDestroy(); super.onDestroy();
//TODO: stop geth //TODO: stop geth
stopNode(null); stopNode(null);
isStatusInitialized = false; //isNodeInitialized = false;
Log.d(TAG, "Status Service stopped !"); Log.d(TAG, "Status Service stopped !");
} }
@ -87,11 +87,10 @@ public class StatusService extends Service {
} }
private boolean handleMessage(Message message) { private boolean handleMessage(Message message) {
Log.d(TAG, "Received service message." + message.toString());
switch (message.what) { switch (message.what) {
case StatusMessages.MSG_START_NODE: case StatusMessages.MSG_START_NODE:
Log.d(TAG, "Received start node message." + message.toString());
startNode(message); startNode(message);
break; break;
@ -135,80 +134,31 @@ public class StatusService extends Service {
} }
private void startNode(Message message) { 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<Void, Void, Void> { applicationMessenger = message.replyTo;
if (!isNodeInitialized) {
String callbackIdentifier; File extStore = Environment.getExternalStorageDirectory();
Messenger messenger; dataFolder = extStore.exists() ?
extStore.getAbsolutePath() + "/ethereum" :
getApplicationInfo().dataDir + "/ethereum";
Log.d(TAG, "Starting Geth node in folder: " + dataFolder);
StartTask(Messenger messenger, String callbackIdentifier) { try {
final File newFile = new File(dataFolder);
this.messenger = messenger; // todo handle error?
this.callbackIdentifier = callbackIdentifier; newFile.mkdir();
} } catch (Exception e) {
Log.e(TAG, "error making folder: " + dataFolder, e);
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");
} }
};
new Thread(new Runnable() { Statusgo.StartNode(dataFolder);
public void run() { Log.d(TAG, "Geth node started");
Statusgo.StartNode(dataFolder); Log.w(TAG, "adding peer");
} Statusgo.AddPeer("enode://409772c7dea96fa59a912186ad5bcdb5e51b80556b3fe447d940f99d9eaadb51d4f0ffedb68efad232b52475dd7bd59b51cee99968b3cc79e2d5684b33c4090c@139.162.166.59:30303");
}).start(); isNodeInitialized = true;
}
handler.postDelayed(addPeer, 5000); createAndSendReply(message, StatusMessages.MSG_START_NODE, null);
} }
private void stopNode(Message message) { private void stopNode(Message message) {
@ -281,7 +231,7 @@ public class StatusService extends Service {
Bundle data = message.getData(); Bundle data = message.getData();
String js = data.getString("js"); String js = data.getString("js");
Statusgo.initJail(js); Statusgo.InitJail(js);
Bundle replyData = new Bundle(); Bundle replyData = new Bundle();
createAndSendReply(message, StatusMessages.MSG_JAIL_INIT, replyData); createAndSendReply(message, StatusMessages.MSG_JAIL_INIT, replyData);
@ -293,7 +243,7 @@ public class StatusService extends Service {
String chatId = data.getString("chatId"); String chatId = data.getString("chatId");
String js = data.getString("js"); String js = data.getString("js");
String result = Statusgo.parse(chatId, js); String result = Statusgo.Parse(chatId, js);
Bundle replyData = new Bundle(); Bundle replyData = new Bundle();
replyData.putString("data", result); replyData.putString("data", result);
@ -307,15 +257,15 @@ public class StatusService extends Service {
String path = data.getString("path"); String path = data.getString("path");
String params = data.getString("params"); String params = data.getString("params");
String result = Statusgo.call(chatId, path, params); String result = Statusgo.Call(chatId, path, params);
Bundle replyData = new Bundle(); Bundle replyData = new Bundle();
replyData.putString("data", result); replyData.putString("data", result);
createAndSendReply(message, StatusMessages.MSG_JAIL_CALL, replyData); createAndSendReply(message, StatusMessages.MSG_JAIL_CALL, replyData);
} }
public static boolean isRunning() { public static boolean isNodeInitialized() {
return isStatusInitialized; return isNodeInitialized;
} }
private static void createAndSendReply(Message message, int replyIdMessage, Bundle replyData) { private static void createAndSendReply(Message message, int replyIdMessage, Bundle replyData) {

View File

@ -17,9 +17,9 @@
(.addListener r/device-event-emitter "gethEvent" (.addListener r/device-event-emitter "gethEvent"
#(dispatch [:signal-event (.-jsonEvent %)])) #(dispatch [:signal-event (.-jsonEvent %)]))
(defn start-node [on-result on-already-running] (defn start-node [on-result]
(when status (when status
(.startNode status on-result on-already-running))) (.startNode status on-result)))
(defn create-account [password on-result] (defn create-account [password on-result]
(when status (when status

View File

@ -24,7 +24,8 @@
status-im.accounts.handlers status-im.accounts.handlers
status-im.protocol.handlers status-im.protocol.handlers
[status-im.utils.datetime :as time] [status-im.utils.datetime :as time]
status-im.transactions.handlers)) status-im.transactions.handlers
[status-im.utils.types :as t]))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;
@ -112,8 +113,16 @@
(u/side-effect! (u/side-effect!
(fn [db _] (fn [db _]
(log/debug "Starting node") (log/debug "Starting node")
(status/start-node (fn [result] (node-started db result)) (status/start-node (fn [result] (node-started db result))))))
#(log/debug "Geth already initialized")))))
(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 (register-handler :crypt-initialized
(u/side-effect! (u/side-effect!

View File

@ -80,13 +80,6 @@
(fn [db [_ hash]] (fn [db [_ hash]]
(remove-pending-message 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 (register-handler :transaction-queued
(after #(dispatch [:navigate-to :confirm])) (after #(dispatch [:navigate-to :confirm]))
(fn [db [_ {:keys [hash args]}]] (fn [db [_ {:keys [hash args]}]]