@@ -151,66 +151,20 @@ private static String prepareLogsFile() {
151
151
return null ;
152
152
}
153
153
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 );
155
156
// 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" );
175
159
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 );
179
160
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 );
198
164
199
165
return jsonConfig .toString ();
200
166
}
201
167
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
-
214
168
private static void prettyPrintConfig (final String config ) {
215
169
Log .d (TAG , "startNode() with config (see below)" );
216
170
String configOutput = config ;
@@ -227,16 +181,22 @@ private static void prettyPrintConfig(final String config) {
227
181
Log .d (TAG , "******************* ENDOF NODE CONFIG *************************" );
228
182
}
229
183
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") ;
232
186
}
233
187
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 ) {
235
195
236
196
Activity currentActivity = getCurrentActivity ();
237
197
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 );
240
200
Log .d (TAG , "Starting Geth node in folder: " + dataFolder );
241
201
242
202
try {
@@ -247,11 +207,11 @@ private void doStartNode(final String defaultConfig, final String fleet) {
247
207
Log .e (TAG , "error making folder: " + dataFolder , e );
248
208
}
249
209
250
- final String ropstenFlagPath = root + "/ ropsten_flag" ;
210
+ final String ropstenFlagPath = pathCombine ( absRootDirPath , " ropsten_flag") ;
251
211
final File ropstenFlag = new File (ropstenFlagPath );
252
212
if (!ropstenFlag .exists ()) {
253
213
try {
254
- final String chaindDataFolderPath = dataFolder + "/ StatusIM/lightchaindata" ;
214
+ final String chaindDataFolderPath = pathCombine ( dataFolder , " StatusIM/lightchaindata") ;
255
215
final File lightChainFolder = new File (chaindDataFolderPath );
256
216
if (lightChainFolder .isDirectory ()) {
257
217
String [] children = lightChainFolder .list ();
@@ -266,10 +226,9 @@ private void doStartNode(final String defaultConfig, final String fleet) {
266
226
}
267
227
}
268
228
269
-
270
229
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") ;
273
232
final File oldKeystore = new File (oldKeystoreDir );
274
233
if (oldKeystore .exists ()) {
275
234
try {
@@ -288,29 +247,34 @@ private void doStartNode(final String defaultConfig, final String fleet) {
288
247
}
289
248
}
290
249
291
- final String config = this .generateConfigFromDefaultConfig (root , newKeystoreDir , fleet , defaultConfig );
250
+ try {
251
+ final String updatedJsonConfigString = this .updateConfig (jsonConfigString , absRootDirPath , newKeystoreDir );
292
252
293
- prettyPrintConfig (config );
253
+ prettyPrintConfig (updatedJsonConfigString );
294
254
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 );
301
267
}
302
- Log .d (TAG , "Geth node started" );
303
- status .sendMessage ();
304
268
}
305
269
306
270
private String getOldExternalDir () {
307
271
File extStore = Environment .getExternalStorageDirectory ();
308
- return extStore .exists () ? extStore .getAbsolutePath () + "/ ethereum/testnet" : getNewInternalDir ();
272
+ return extStore .exists () ? pathCombine ( extStore .getAbsolutePath (), " ethereum/testnet") : getNewInternalDir ();
309
273
}
310
274
311
275
private String getNewInternalDir () {
312
276
Activity currentActivity = getCurrentActivity ();
313
- return currentActivity .getApplicationInfo ().dataDir + "/ ethereum/testnet" ;
277
+ return pathCombine ( currentActivity .getApplicationInfo ().dataDir , " ethereum/testnet") ;
314
278
}
315
279
316
280
private void deleteDirectory (File folder ) {
@@ -384,7 +348,7 @@ public void moveToInternalStorage(Callback callback) {
384
348
}
385
349
386
350
@ ReactMethod
387
- public void startNode (final String config , final String fleet ) {
351
+ public void startNode (final String config ) {
388
352
Log .d (TAG , "startNode" );
389
353
if (!checkAvailability ()) {
390
354
return ;
@@ -393,7 +357,7 @@ public void startNode(final String config, final String fleet) {
393
357
Runnable r = new Runnable () {
394
358
@ Override
395
359
public void run () {
396
- doStartNode (config , fleet );
360
+ doStartNode (config );
397
361
}
398
362
};
399
363
0 commit comments