-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.cpp
executable file
·236 lines (192 loc) · 5.54 KB
/
main.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
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <thread>
#include "Transaction/Transaction.h"
#include "Wallet.h"
#include "CertStore/Cert.h"
#include "CertStore/CertStore.h"
#include "Tools/Log.h"
#include "BlockCreator/Mint.h"
#include "Loader.h"
#include "TxPool.h"
#include "API/ApiServer.h"
#include "Network/Server.h"
#include "Consensus/VoteStore.h"
#include "Test/Test.h"
#include "WebInterface/WebInterface.h"
#include "Config.h"
#include "App.h"
#include "Network/Network.h"
void startSync() {
Network &network = Network::Instance();
network.syncBlockchain();
}
void cleanTransactionPool() {
TxPool& txPool = TxPool::Instance();
while(true) {
#if defined(_WIN32)
Sleep(300000);
#else
sleep(300);
#endif
txPool.cleanTxPool();
}
}
void startMinting() {
Mint& mint = Mint::Instance();
Config& config = Config::Instance();
if (config.isMintingEnabled()) {
mint.startMinting();
mint.startMintingService();
}
}
void getMyIP() {
Network::getMyIP();
}
void startServer() {
Log(LOG_LEVEL_INFO) << "Start Server";
try
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), NET_PORT_INT);
Server &server = Server::Instance(io_service, endpoint);
io_service.run();
}
catch (std::exception& e)
{
Log(LOG_LEVEL_ERROR) << "Server Exception: " << e.what();
startServer();
}
}
void startApiServer() {
ApiServer* apiServer = new ApiServer();
apiServer->run();
}
void startWebInterface() {
WebInterface* webInterface = new WebInterface();
webInterface->run();
}
#if defined(__linux__)
void signalHandler(int signal) {
Log(LOG_LEVEL_INFO) << "received shutdown signal";
App& app = App::Instance();
app.terminate();
}
#endif
#if defined(_WIN32)
BOOL WINAPI signalHandler(DWORD signal) {
if (signal == CTRL_CLOSE_EVENT || signal == CTRL_LOGOFF_EVENT || signal == CTRL_SHUTDOWN_EVENT || signal == CTRL_C_EVENT) {
Log(LOG_LEVEL_INFO) << "received shutdown signal";
App& app = App::Instance();
app.terminate();
}
}
#endif
int main() {
Log(LOG_LEVEL_INFO) << "Starting UBIC version " << VERSION;
if(Loader::isLocked()) {
Log(LOG_LEVEL_CRITICAL_ERROR) << "Found lock file "
<< FS::getLockPath()
<< " this means that either another instance of UBIC is running or that it wasn't shutdown properly";
return 0;
}
#if defined(__linux__)
//daemonize
pid_t pid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
if (setsid() < 0)
exit(EXIT_FAILURE);
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
signal(SIGABRT, signalHandler);
signal(SIGKILL, signalHandler);
signal(SIGQUIT, signalHandler);
pid = getpid();
Log(LOG_LEVEL_INFO) << "Process ID:" << pid;
FS::touchFile(Hexdump::stringToCharVector("/var/ubic/ubic.pid"));
std::fstream fout("/var/ubic/ubic.pid", std::fstream::in | std::fstream::out | std::fstream::binary );
fout.seekp( 0 );
std::ostringstream oss;
oss << pid;
fout.write(oss.str().c_str(), oss.str().size());
fout.close();
fclose(stdout);
fclose(stderr);
#endif
#if defined(_WIN32)
SetConsoleCtrlHandler(signalHandler, TRUE);
#endif
#if defined(TEST_MODE)
App& app = App::Instance();
app.setIsStarting(true);
Log(LOG_LEVEL_INFO) << "Starting in test mode";
Test::sanitizeUbicFolder();
Loader::createTouchFilesAndDirectories();
Loader::loadConfig();
Config& config = Config::Instance();
config.setMintingStatus("ON"); // force mining to be on
Loader::loadDelegates();
Loader::loadBestBlockHeaders();
Loader::loadCertStore();
Loader::loadPathSum();
Loader::loadWallet();
app.setIsStarting(false);
VoteStore& voteStore = VoteStore::Instance();
if(voteStore.getActiveDelegates().empty()) { // if there are no validators we create them
Test::createValidators();
}
CertStore& certStore = CertStore::Instance();
if(certStore.getRootList().empty()) { // if there are no root certs we create one
Test::createRootCert();
}
Test::importCACerts();
Log(LOG_LEVEL_INFO) << "API KEY: " << config.getApiKey();
std::thread t1(&startApiServer);
std::thread t2(&startServer);
std::thread t3(&startWebInterface);
std::thread t4(&startMinting);
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
#else
App& app = App::Instance();
app.setIsStarting(true);
std::thread t0(&getMyIP);
std::thread t1(&startApiServer);
// @TODO add a lock
//Loader::lock();
Loader::createTouchFilesAndDirectories();
Loader::loadConfig();
Loader::loadDelegates();
Loader::loadBestBlockHeaders();
Loader::loadCertStore();
if(!Loader::loadPathSum()) {
return 0;
}
Loader::loadWallet();
app.setIsStarting(false);
Mint& mint = Mint::Instance();
TxPool& txPool = TxPool::Instance();
Wallet& wallet = Wallet::Instance();
Log(LOG_LEVEL_INFO) << "Final balance : " << wallet.getBalance();
std::thread t2(&startServer);
std::thread t3(&startWebInterface);
std::thread t4(&startMinting);
std::thread t5(&startSync);
std::thread t6(&cleanTransactionPool);
t0.join();
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
return 0;
#endif
}