-
Notifications
You must be signed in to change notification settings - Fork 16
/
Loader.cpp
executable file
·232 lines (178 loc) · 6.98 KB
/
Loader.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
#include <openssl/rand.h>
#include "Loader.h"
#include "Chain.h"
#include "FS/FS.h"
#include "CertStore/CertStore.h"
#include "PathSum/PathSum.h"
#include "Wallet.h"
#include "Consensus/VoteStore.h"
#include "Config.h"
bool Loader::createTouchFilesAndDirectories() {
// certs/
FS::createDirectory(FS::getCertDirectoryPath());
// certs/root/
FS::createDirectory(FS::concatPaths(FS::getCertDirectoryPath(), "root/"));
// certs/csca/
FS::createDirectory(FS::concatPaths(FS::getCertDirectoryPath(), "csca/"));
// certs/dsc/
FS::createDirectory(FS::concatPaths(FS::getCertDirectoryPath(), "dsc/"));
// x509/
FS::createDirectory(FS::getX509DirectoryPath());
// x509/root/
FS::createDirectory(FS::concatPaths(FS::getX509DirectoryPath(), "root/"));
// x509/csca/
FS::createDirectory(FS::concatPaths(FS::getX509DirectoryPath(), "csca/"));
// x509/dsc/
FS::createDirectory(FS::concatPaths(FS::getX509DirectoryPath(), "dsc/"));
// blockdat/
FS::createDirectory(FS::getBlockDatDirectoryPath());
// AddressStore.mdb
FS::createDirectory(FS::getAddressStorePath());
// BlockIndexStore.mdb
FS::createDirectory(FS::getBlockIndexStorePath());
// NTPSKStore.mdb
FS::createDirectory(FS::getNTPSKStorePath());
// DSCCounterStore.mdb
FS::createDirectory(FS::getDSCCounterStorePath());
// blockdat/00000000.dat
FS::touchFile(FS::getBlockDatPath());
// wallet.dat
FS::touchFile(FS::getWalletPath());
// bestHeaders.dat
FS::touchFile(FS::getBestBlockHeadersPath());
// headers.mdb
FS::createDirectory(FS::getBlockHeadersPath());
// myTransactions.mdb
FS::createDirectory(FS::getMyTransactionsPath());
// LOGS
FS::createDirectory(FS::getLogPath());
return true;
}
bool Loader::isLocked() {
return FS::fileExists(FS::getLockPath());
}
bool Loader::loadConfig() {
if(!FS::fileExists(FS::getConfigPath())) {
FS::createDirectory(FS::getConfigBasePath());
FS::touchFile(FS::getConfigPath());
uint32_t byteLength = 12;
unsigned char buf[byteLength];
RAND_bytes(buf, byteLength);
std::string password = Hexdump::ucharToHexString(buf, byteLength);
std::string config = "# the path needs to end with \"/\" if not set it will be the default one\n"
"blockchainPath = \n"
"\n"
"# DANGER do not change allowFrom unless you know what you are doing\n"
"# If you want to get remote access please consider unsing a VPN or an SSH Tunnel\n"
"allowFrom = 127.0.0.1\n"
"\n"
"# The number of addresses to generate from the wallet.dat seed\n"
"numberOfAdresses = 100\n"
"\n"
"# defines which events are logged\n"
"# levels are:\n"
"# - NOTICE\n"
"# - ERROR\n"
"logLevel = NOTICE\n"
"\n"
"# address for donations\n"
"donationAddress = \n"
"\n"
"# minting can be ON or OFF\n"
"minting = OFF\n"
"\n"
"# Indexate transactions\n"
"indexateTransactions = OFF\n"
"\n"
"# Get nodes from Gitub\n"
"nodesFromGithub = ON\n"
"\n"
"#password that needs to be send with each API request\n"
"apiKey = ";
//Log(LOG_LEVEL_INFO) << "Generated API password:" << password;
std::vector<unsigned char> configVector(config.c_str(), config.c_str() + config.length());
std::vector<unsigned char> configVector2 = FS::concatPaths(configVector, password.c_str());
FS::overwriteFile(FS::getConfigPath(), configVector2);
}
Config& config = Config::Instance();
return config.loadConfig();
}
bool Loader::loadDelegates() {
VoteStore& voteStore = VoteStore::Instance();
return voteStore.loadDelegates();
}
bool Loader::loadBestBlockHeaders() {
Chain& chain = Chain::Instance();
uint64_t pos = 0;
bool eof = false;
std::vector<BlockHeader> bestBlockHeaders;
while(!eof) {
BlockHeader header;
if(!FS::deserializeFromFile(FS::getBestBlockHeadersPath(), BLOCK_SIZE_MAX, pos, header, pos, eof)) {
break;
}
bestBlockHeaders.emplace_back(header);
chain.setCurrentBlockchainHeight(header.getBlockHeight());
}
if(bestBlockHeaders.empty()) {
Log(LOG_LEVEL_INFO) << "bestBlockHeaders is empty";
return false;
}
chain.setBestBlockHeaders(bestBlockHeaders);
Log(LOG_LEVEL_INFO) << "Loaded " << (uint64_t)bestBlockHeaders.size() << " best block header(s)";
return true;
}
bool Loader::loadCertStore() {
CertStore& certStore = CertStore::Instance();
certStore.loadFromFS();
Log(LOG_LEVEL_INFO) << "Loaded certStore";
return true;
}
bool Loader::loadPathSum() {
PathSum& pathSum = PathSum::Instance();
UAmount zeroBlockAmount;
pathSum.appendValue(zeroBlockAmount); // Block zero doesn't exist so we assign empty value
Chain& chain = Chain::Instance();
std::map<uint64_t, UAmount> pathSumList;
BlockHeader* header = chain.getBestBlockHeader();
if(header == nullptr) {
return true;
}
pathSumList.insert(std::pair<uint64_t, UAmount>(header->getBlockHeight(), header->getPayout()));
std::vector<unsigned char> previousHeaderHash = header->getPreviousHeaderHash();
// first we insert values in a temporary variable as we start with last block and pathSum requires
// a chronological order
while(true) {
//Log(LOG_LEVEL_INFO) << "previousHeaderHash: " << previousHeaderHash;
BlockHeader* found = chain.getBlockHeader(previousHeaderHash);
if(found == nullptr) {
break;
}
//Log(LOG_LEVEL_INFO) << "Temporary inserted Payout: " << found->getPayout();
pathSumList.insert(std::pair<uint64_t, UAmount>(found->getBlockHeight(), found->getPayout()));
previousHeaderHash = found->getPreviousHeaderHash();
delete found;
}
int i = 1;
for(std::map<uint64_t, UAmount>::iterator it = pathSumList.begin(); it != pathSumList.end(); ++it) {
pathSum.appendValue(it->second);
//Log(LOG_LEVEL_INFO) << "Inserted block: " << i << ":" << it->first << " p:" << it->second;
if(it->first != i) {
Log(LOG_LEVEL_INFO) << "Block height " << it->first << " and PathSum insert position " << i << " mismatch";
return false;
}
if(it->first == 269970) {
if(it->second.map[8] != 453617 && it->second.map[6] != 32517392) {
Log(LOG_LEVEL_CRITICAL_ERROR) << "failed the sanity check at height 269970, the PathSum is dirty";
return false;
}
}
i++;
}
return true;
}
bool Loader::loadWallet() {
Wallet& wallet = Wallet::Instance();
wallet.initWallet();
return true;
}