-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.cpp
470 lines (344 loc) · 13.7 KB
/
entrypoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <jni.h>
#include <android/log.h>
#include <LLC/include/random.h>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <boost/filesystem.hpp>
#include <LLP/include/global.h>
#include <LLP/include/port.h>
#include <LLP/types/apinode.h>
#include <LLP/types/rpcnode.h>
#include <LLP/types/miner.h>
#include <LLP/include/lisp.h>
#include <LLP/include/port.h>
#include <LLD/include/global.h>
#include <TAO/API/include/global.h>
#include <TAO/API/include/cmd.h>
#include <TAO/Ledger/include/create.h>
#include <TAO/Ledger/include/chainstate.h>
#include <TAO/Ledger/include/dispatch.h>
#include <TAO/Ledger/types/stake_minter.h>
#include <TAO/Ledger/include/timelocks.h>
#include <Util/include/convert.h>
#include <Util/include/filesystem.h>
#include <Util/include/signals.h>
#include <Util/include/daemon.h>
#include <Util/include/config.h>
#include <Util/include/encoding.h>
#include <Legacy/include/ambassador.h>
using namespace std;
#define LOG_D(...) __android_log_print(ANDROID_LOG_DEBUG, "Entrypoint", __VA_ARGS__)
extern "C" {
jint JNI_Onload(JavaVM* vm, void* reserved) {
LOG_D ("JNI_OnLoad");
JNIEnv* env;
if(vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK ) {
LOG_D("Initial Get env error");
return JNI_ERR;
}
else {
return JNI_VERSION_1_6;
}
}
/** Startup
*
* Wrap all our initialization logic here that needs to run in the background after startup.
*
**/
void Startup()
{
/* Add our connections from commandline. */
LLP::MakeConnections<LLP::TimeNode> (LLP::TIME_SERVER);
LLP::MakeConnections<LLP::TritiumNode>(LLP::TRITIUM_SERVER);
/* Run our autologin scripts now. */
if(config::GetBoolArg("-autocreate", false) || config::GetBoolArg("-autologin", false))
{
/* Check that we are in single-user mode. */
if(config::fMultiuser.load())
{
/* Output our new warning message if the API was disabled. */
debug::log(0, ANSI_COLOR_BRIGHT_RED, "-autocreate and -autologin DISABLED", ANSI_COLOR_RESET);
debug::log(0, ANSI_COLOR_BRIGHT_YELLOW, "You cannot use -multiuser=1 with -auto(type) arguments.", ANSI_COLOR_RESET);
return;
}
/* Create a JSON encoding and call the main API endpoint. */
encoding::json jParams =
{
{ "username", config::GetArg("-username", "") },
{ "password", config::GetArg("-password", "") },
{ "pin", config::GetArg("-pin", "") }
};
/* Handle for -autocreate if specified. */
std::string strCreate = "create/master";
if(config::GetBoolArg("-autocreate", false))
{
try { TAO::API::Commands::Invoke("profiles", strCreate, jParams); }
catch(const TAO::API::Exception e){ debug::notice(FUNCTION, "::autocreate:", e.what()); }
}
/* Handle for -autologin if specified. */
if(config::GetBoolArg("-autologin", false))
{
try
{
/* Create our local session first. */
std::string strLogin = "create/local";
TAO::API::Commands::Invoke("sessions", strLogin, jParams);
/* Wait for dynamic indexing services. */
std::string strStatus = "status/local";
while(!config::fShutdown.load())
{
/* Check our current status against indexing services. */
const encoding::json jStatus =
TAO::API::Commands::Invoke("sessions", strStatus, jParams);
/* Break once we have indexed sigchain. */
if(!jStatus["indexing"].get<bool>()) //basic spin-lock
break;
/* Make sure we don't spin at 100% of a CPU core. */
runtime::sleep(1);
}
/* Create a JSON encoding and call the main API endpoint. */
encoding::json jUnlock =
{
{ "pin", config::GetArg("-pin", "") },
{ "notifications", "1" },
{ "mining", "1" },
{ "staking", "1" }
};
/* Unlock our local session now. */
std::string strUnlock = "unlock/local";
TAO::API::Commands::Invoke("sessions", strUnlock, jUnlock);
}
catch(const TAO::API::Exception e){ debug::notice(FUNCTION, "::autologin: ", e.what()); }
}
}
}
JNIEXPORT jstring JNICALL
Java_io_nexus_wallet_android_MainActivity_startNexusCore(JNIEnv *env, jobject thiz, jstring homepath, jstring apiUser ,jstring apiPassword , jobjectArray params) {
{
const char *res;
const char *inApiUser;
const char *inApiPassword;
jboolean isCopy;
res = env->GetStringUTFChars(homepath, &isCopy);
inApiUser = env->GetStringUTFChars(apiUser,&isCopy);
inApiPassword = env->GetStringUTFChars(apiPassword,&isCopy);
setenv("HOME", res, 0);
(env)->ReleaseStringUTFChars(homepath, res);
int defaultArgSize = 2;
int size = env->GetArrayLength(params);
int argc = size + defaultArgSize;
char *argv[argc];
//Set Default params
argv[0] = NULL;
argv[1] = strdup("-client=1");
for (int i = 0; i < size; ++i) {
jstring string = (jstring) env->GetObjectArrayElement(params, i);
const char *myarray = env->GetStringUTFChars(string, 0);
LOG_D("@@:: Incoming Params: %s", myarray);
argv[defaultArgSize + i] = strdup(myarray);
env->ReleaseStringUTFChars(string, myarray);
env->DeleteLocalRef(string);
}
/* Setup the timer timer. */
runtime::timer timer;
timer.Start();
/* Handle all the signals with signal handler method. */
SetupSignals();
LOG_D("Home Env: %s", std::string(getenv("HOME")).c_str());
ofstream nexusConfFile;
fstream nxsPolicyFile;
struct stat statbuf;
string nxsFolder = std::string(getenv("HOME")) + "/Nexus";
string nxsPolicy = std::string(nxsFolder + "/db-policies.txt" );
string nxsConf = std::string(nxsFolder + "/nexus.conf");
LOG_D("Nexus Folder: %s", nxsFolder.c_str());
LOG_D("Nexus Conf Location: %s", nxsConf.c_str());
int nxsPolicyExists = stat(nxsPolicy.c_str(), &statbuf);
int nxsFolderExists = stat(nxsFolder.c_str(), &statbuf);
int nxsConfExists = stat(nxsConf.c_str(), &statbuf);
int CURRENT_DB_POLICY = 4;
LOG_D("Is Nexus Folder Already there? : %d", nxsFolderExists);
LOG_D("Is Nexus Conf There? : %d", nxsConfExists);
LOG_D("NXS POLICY FOund? : %d", nxsPolicyExists);
if (nxsFolderExists < 0) {
mode_t m = S_IRWXU | S_IRWXG | S_IRWXO;
int status = mkdir((std::string(getenv("HOME"))).c_str(), m);
LOG_D("Make Home Directory Status: %d", status);
status = mkdir(nxsFolder.c_str(), m);
LOG_D("Make Nexus Folder Status: %d", status);
}
if (nxsPolicyExists < 0)
{
// If No policy is there but the nexus folder is, then we need to delete
if (nxsConfExists >= 0)
{
LOG_D("Deleting DB");
boost::filesystem::remove_all(nxsFolder + "/client");
}
}
else {
nxsPolicyFile.open (nxsPolicy, std::fstream::in);
if (nxsPolicyFile.is_open()) {
LOG_D("Open success");
}
std::string line;
std::vector<std::string> nxsPolicyFileLines;
while (std::getline(nxsPolicyFile, line)) {
nxsPolicyFileLines.push_back(line);
}
nxsPolicyFile.close();
if (nxsPolicyFileLines.size() > 0) {
string dbpolicy = nxsPolicyFileLines[0];
string delimiter = "dbpolicy:"; // A bit overkill but will be useful in the future
dbpolicy.erase(0, dbpolicy.find(delimiter) + delimiter.length());
if (stoi(dbpolicy) < CURRENT_DB_POLICY)
{
LOG_D("Deleting DB");
boost::filesystem::remove_all(nxsFolder + "/client");
}
}
}
nxsPolicyFile.open (nxsPolicy, std::fstream::out | std::fstream::trunc);
//TODO: Replace with vector write
nxsPolicyFile << "dbpolicy:" << std::to_string(CURRENT_DB_POLICY) << '\n';
nxsPolicyFile.close();
nexusConfFile.open(nxsConf, std::fstream::in | std::fstream::out | std::fstream::app);
if (nxsConfExists < 0) {
LOG_D("!! WRITING FILE");
// If file does not exist, write to it.
string fileContent = "apiuser=" + string(inApiUser) +"\napipassword=" + string(inApiPassword);
nexusConfFile
<< fileContent ;
} else {
LOG_D("!! FILE EXISTS");
}
env->ReleaseStringUTFChars(apiPassword,inApiPassword);
env->ReleaseStringUTFChars(apiUser,inApiUser);
nexusConfFile.close();
LOG_D("Listing All Params sent to core: ");
for (int i = 0; i < argc; i++) {
LOG_D("argv[%d] = %s\n", i, argv[i]);
}
/* Read the configuration file. Pass argc and argv for possible -datadir setting */
config::ReadConfigFile(config::mapArgs, config::mapMultiArgs, argc, argv);
/* Parse out the parameters */
config::ParseParameters(argc, argv);
/* Once we have read in the CLI paramters and config file, cache the args into global variables*/
config::CacheArgs();
/* Initalize the debug logger. */
debug::Initialize();
/* Handle Commandline switch */
for(int i = 1; i < argc; ++i)
{
/* Handle for commandline API/RPC */
if(!convert::IsSwitchChar(argv[i][0]))
{
//Mobile does not support comandline switching
return env->NewStringUTF(0);
}
}
/* Log the startup information now. */
debug::LogStartup(argc, argv);
/* Run the process as Daemon RPC/LLP Server if Flagged. */
if(config::fDaemon)
{
debug::log(0, FUNCTION, "-daemon flag enabled. Running in background");
Daemonize();
}
/* Create directories if they don't exist yet. */
if(!::filesystem::exists(config::GetDataDir()) &&
::filesystem::create_directory(config::GetDataDir()))
{
debug::log(0, FUNCTION, "Generated Path ", config::GetDataDir());
}
/* Check for failures or shutdown. */
bool fFailed = config::fShutdown.load();
if(!fFailed)
{
/* Initialize LLD. */
LLD::Initialize();
/* Initialize dispatch relays. */
TAO::Ledger::Dispatch::Initialize();
/* Initialize ChainState. */
TAO::Ledger::ChainState::Initialize();
/* Run our LLD indexing operations. */
LLD::Indexing();
/* Initialize the Lower Level Protocol. */
LLP::Initialize();
/* Startup performance metric. */
debug::log(0, FUNCTION, "Started up in ", timer.ElapsedMilliseconds(), "ms");
/* Set the initialized flags. */
config::fInitialized.store(true);
/* Kick off our startup thread for post-startup processing. */
std::thread tStartup = std::thread(Startup);
/* Initialize generator thread. */
std::thread thread;
if(config::fHybrid.load())
thread = std::thread(TAO::Ledger::ThreadGenerator);
/* Wait for shutdown. */
if(!config::GetBoolArg(std::string("-gdb")))
{
std::mutex SHUTDOWN_MUTEX;
std::unique_lock<std::mutex> SHUTDOWN_LOCK(SHUTDOWN_MUTEX);
SHUTDOWN.wait(SHUTDOWN_LOCK, []{ return config::fShutdown.load(); });
}
/* GDB mode waits for keyboard input to initiate clean shutdown. */
else
{
getchar();
config::fShutdown = true;
}
/* Wait for our startup thread to finish. */
tStartup.join();
/* Stop stake minter if running. Minter ignores request if not running, so safe to just call both */
TAO::Ledger::StakeMinter::GetInstance().Stop();
/* Wait for the private condition. */
if(config::fHybrid.load())
{
TAO::Ledger::PRIVATE_CONDITION.notify_all();
thread.join();
}
/* Shutdown dispatch. */
TAO::Ledger::Dispatch::Shutdown();
}
/* Shutdown metrics. */
timer.Reset();
/* Release network triggers. */
LLP::Release();
/* Shutdown the API subsystems. */
TAO::API::Shutdown();
/* Shutdown network subsystem. */
LLP::Shutdown();
/* Shutdown LLL sub-systems. */
LLD::Shutdown();
/* Startup performance metric. */
debug::log(0, FUNCTION, "Closed in ", timer.ElapsedMilliseconds(), "ms");
/* Close the debug log file once and for all. */
debug::Shutdown();
unsetenv("HOME");
return env->NewStringUTF("Shutdown");
}
}
JNIEXPORT jint JNICALL
Java_io_nexus_wallet_android_MainActivity_ShutDownNexusCore(JNIEnv *env, jobject thiz)
{
{
LOG_D("### Set Shutdown Flag");
Shutdown();
return 0;
}
}
JNIEXPORT jint JNICALL
Java_io_nexus_wallet_android_MainActivity_CloseListenSocket(JNIEnv *env, jobject thiz)
{
//LLP::CloseListening();
return 0;
}
JNIEXPORT jint JNICALL
Java_io_nexus_wallet_android_MainActivity_OpenListenSocket(JNIEnv *env, jobject thiz)
{
//LLP::OpenListening();
return 0;
}
}