Skip to content

Commit 4ab5e43

Browse files
author
Pedro Pombeiro
committed
Replace call to GenerateConfig with local JSON. Closes #5739
Signed-off-by: Pedro Pombeiro <[email protected]>
1 parent e69fb18 commit 4ab5e43

File tree

10 files changed

+203
-201
lines changed

10 files changed

+203
-201
lines changed

modules/react-native-status/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies {
1616
implementation 'com.instabug.library:instabug:3+'
1717
implementation 'status-im:function:0.0.1'
1818

19-
String statusGoVersion = 'v0.14.1'
19+
String statusGoVersion = 'v0.15.0'
2020
final String statusGoGroup = 'status-im', statusGoName = 'status-go'
2121

2222
// Check if the local status-go jar exists, and compile against that if it does

modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java

+41-77
Original file line numberDiff line numberDiff line change
@@ -151,66 +151,20 @@ private static String prepareLogsFile() {
151151
return null;
152152
}
153153

154-
private String generateConfig(final JSONObject defaultConfig, final String root, final String keystoreDir, final String fleet) throws JSONException {
154+
private String updateConfig(final String jsonConfigString, final String absRootDirPath, final String absKeystoreDirPath) throws JSONException {
155+
final JSONObject jsonConfig = new JSONObject(jsonConfigString);
155156
// retrieve parameters from app config, that will be applied onto the Go-side config later on
156-
final String dataDir = root + defaultConfig.get("DataDir");
157-
final int networkId = defaultConfig.getInt("NetworkId");
158-
final Object upstreamConfig = defaultConfig.opt("UpstreamConfig");
159-
final Boolean logEnabled = defaultConfig.getBoolean("LogEnabled");
160-
final String logLevel = defaultConfig.optString("LogLevel", "ERROR");
161-
162-
// retrieve config from Go side, in order to use as the basis of the config
163-
JSONObject jsonConfig = new JSONObject(
164-
Statusgo.GenerateConfig(dataDir, fleet, networkId));
165-
166-
jsonConfig.put("NetworkId", networkId);
167-
jsonConfig.put("DataDir", dataDir);
168-
jsonConfig.put("KeyStoreDir", keystoreDir);
169-
170-
if (upstreamConfig != null) {
171-
Log.d(TAG, "UpstreamConfig is not null");
172-
jsonConfig.put("UpstreamConfig", upstreamConfig);
173-
}
174-
157+
final String absDataDirPath = pathCombine(absRootDirPath, jsonConfig.getString("DataDir"));
158+
final Boolean logEnabled = jsonConfig.getBoolean("LogEnabled");
175159
final String gethLogFilePath = logEnabled ? prepareLogsFile() : null;
176-
jsonConfig.put("LogEnabled", logEnabled);
177-
jsonConfig.put("LogFile", gethLogFilePath);
178-
jsonConfig.put("LogLevel", TextUtils.isEmpty(logLevel) ? "ERROR" : logLevel);
179160

180-
// Setting up whisper config
181-
JSONObject whisperConfig = jsonConfig.optJSONObject("WhisperConfig");
182-
if (whisperConfig == null) {
183-
whisperConfig = new JSONObject();
184-
}
185-
whisperConfig.put("LightClient", true);
186-
jsonConfig.put("WhisperConfig", whisperConfig);
187-
188-
// Setting up cluster config
189-
JSONObject clusterConfig = jsonConfig.optJSONObject("ClusterConfig");
190-
if (clusterConfig != null) {
191-
Log.d(TAG, "ClusterConfig is not null");
192-
clusterConfig.put("Fleet", fleet);
193-
jsonConfig.put("ClusterConfig", clusterConfig);
194-
} else {
195-
Log.w(TAG, "ClusterConfig: Cannot find ClusterConfig: doesn't exist or not a JSON object");
196-
Log.w(TAG, "ClusterConfig: Fleet will be set to defaults");
197-
}
161+
jsonConfig.put("DataDir", absDataDirPath);
162+
jsonConfig.put("KeyStoreDir", absKeystoreDirPath);
163+
jsonConfig.put("LogFile", gethLogFilePath);
198164

199165
return jsonConfig.toString();
200166
}
201167

202-
private String generateConfigFromDefaultConfig(final String root, final String keystoreDir, final String fleet, final String defaultConfig) {
203-
try {
204-
JSONObject customConfig = new JSONObject(defaultConfig);
205-
206-
return generateConfig(customConfig, root, keystoreDir, fleet);
207-
} catch (JSONException e) {
208-
Log.d(TAG, "Something went wrong " + e.getMessage());
209-
Log.d(TAG, "Default configuration will be used: ropsten, beta fleet");
210-
return Statusgo.GenerateConfig(this.getTestnetDataDir(root), "eth.beta", TESTNET_NETWORK_ID);
211-
}
212-
}
213-
214168
private static void prettyPrintConfig(final String config) {
215169
Log.d(TAG, "startNode() with config (see below)");
216170
String configOutput = config;
@@ -227,16 +181,22 @@ private static void prettyPrintConfig(final String config) {
227181
Log.d(TAG, "******************* ENDOF NODE CONFIG *************************");
228182
}
229183

230-
private String getTestnetDataDir(final String root) {
231-
return root + "/ethereum/testnet";
184+
private String getTestnetDataDir(final String absRootDirPath) {
185+
return pathCombine(absRootDirPath, "ethereum/testnet");
232186
}
233187

234-
private void doStartNode(final String defaultConfig, final String fleet) {
188+
private String pathCombine(final String path1, final String path2) {
189+
// Replace this logic with Paths.get(path1, path2) once API level 26+ becomes the minimum supported API level
190+
final File file = new File(path1, path2);
191+
return file.getAbsolutePath();
192+
}
193+
194+
private void doStartNode(final String jsonConfigString) {
235195

236196
Activity currentActivity = getCurrentActivity();
237197

238-
final String root = currentActivity.getApplicationInfo().dataDir;
239-
final String dataFolder = this.getTestnetDataDir(root);
198+
final String absRootDirPath = currentActivity.getApplicationInfo().dataDir;
199+
final String dataFolder = this.getTestnetDataDir(absRootDirPath);
240200
Log.d(TAG, "Starting Geth node in folder: " + dataFolder);
241201

242202
try {
@@ -247,11 +207,11 @@ private void doStartNode(final String defaultConfig, final String fleet) {
247207
Log.e(TAG, "error making folder: " + dataFolder, e);
248208
}
249209

250-
final String ropstenFlagPath = root + "/ropsten_flag";
210+
final String ropstenFlagPath = pathCombine(absRootDirPath, "ropsten_flag");
251211
final File ropstenFlag = new File(ropstenFlagPath);
252212
if (!ropstenFlag.exists()) {
253213
try {
254-
final String chaindDataFolderPath = dataFolder + "/StatusIM/lightchaindata";
214+
final String chaindDataFolderPath = pathCombine(dataFolder, "StatusIM/lightchaindata");
255215
final File lightChainFolder = new File(chaindDataFolderPath);
256216
if (lightChainFolder.isDirectory()) {
257217
String[] children = lightChainFolder.list();
@@ -266,10 +226,9 @@ private void doStartNode(final String defaultConfig, final String fleet) {
266226
}
267227
}
268228

269-
270229
String testnetDataDir = dataFolder;
271-
String oldKeystoreDir = testnetDataDir + "/keystore";
272-
String newKeystoreDir = root + "/keystore";
230+
String oldKeystoreDir = pathCombine(testnetDataDir, "keystore");
231+
String newKeystoreDir = pathCombine(absRootDirPath, "keystore");
273232
final File oldKeystore = new File(oldKeystoreDir);
274233
if (oldKeystore.exists()) {
275234
try {
@@ -288,29 +247,34 @@ private void doStartNode(final String defaultConfig, final String fleet) {
288247
}
289248
}
290249

291-
final String config = this.generateConfigFromDefaultConfig(root, newKeystoreDir, fleet, defaultConfig);
250+
try {
251+
final String updatedJsonConfigString = this.updateConfig(jsonConfigString, absRootDirPath, newKeystoreDir);
292252

293-
prettyPrintConfig(config);
253+
prettyPrintConfig(updatedJsonConfigString);
294254

295-
String res = Statusgo.StartNode(config);
296-
if (res.startsWith("{\"error\":\"\"")) {
297-
Log.d(TAG, "StartNode result: " + res);
298-
}
299-
else {
300-
Log.e(TAG, "StartNode failed: " + res);
255+
String res = Statusgo.StartNode(updatedJsonConfigString);
256+
if (res.startsWith("{\"error\":\"\"")) {
257+
Log.d(TAG, "StartNode result: " + res);
258+
Log.d(TAG, "Geth node started");
259+
}
260+
else {
261+
Log.e(TAG, "StartNode failed: " + res);
262+
}
263+
status.sendMessage();
264+
} catch (JSONException e) {
265+
Log.e(TAG, "updateConfig failed: " + e.getMessage());
266+
System.exit(1);
301267
}
302-
Log.d(TAG, "Geth node started");
303-
status.sendMessage();
304268
}
305269

306270
private String getOldExternalDir() {
307271
File extStore = Environment.getExternalStorageDirectory();
308-
return extStore.exists() ? extStore.getAbsolutePath() + "/ethereum/testnet" : getNewInternalDir();
272+
return extStore.exists() ? pathCombine(extStore.getAbsolutePath(), "ethereum/testnet") : getNewInternalDir();
309273
}
310274

311275
private String getNewInternalDir() {
312276
Activity currentActivity = getCurrentActivity();
313-
return currentActivity.getApplicationInfo().dataDir + "/ethereum/testnet";
277+
return pathCombine(currentActivity.getApplicationInfo().dataDir, "ethereum/testnet");
314278
}
315279

316280
private void deleteDirectory(File folder) {
@@ -384,7 +348,7 @@ public void moveToInternalStorage(Callback callback) {
384348
}
385349

386350
@ReactMethod
387-
public void startNode(final String config, final String fleet) {
351+
public void startNode(final String config) {
388352
Log.d(TAG, "startNode");
389353
if (!checkAvailability()) {
390354
return;
@@ -393,7 +357,7 @@ public void startNode(final String config, final String fleet) {
393357
Runnable r = new Runnable() {
394358
@Override
395359
public void run() {
396-
doStartNode(config, fleet);
360+
doStartNode(config);
397361
}
398362
};
399363

modules/react-native-status/desktop/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ExternalProject_Add(StatusGo_ep
3737
PREFIX ${StatusGo_PREFIX}
3838
SOURCE_DIR ${StatusGo_SOURCE_DIR}
3939
GIT_REPOSITORY https://github.com/status-im/status-go.git
40-
GIT_TAG v0.14.1
40+
GIT_TAG v0.15.0
4141
BUILD_BYPRODUCTS ${StatusGo_STATIC_LIB}
4242
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/${CONFIGURE_SCRIPT} ${GO_ROOT_PATH} ${StatusGo_ROOT} ${StatusGo_SOURCE_DIR}
4343
BUILD_COMMAND ""

0 commit comments

Comments
 (0)