/
util.cpp
538 lines (469 loc) · 15.7 KB
/
util.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or https://opensource.org/licenses/mit-license.php.
#include "amount.h"
#include "netbase.h" // for AddTimeData
#include "support/cleanse.h"
#include "sync.h"
#include "version.h"
#include "node/ui_interface.h"
#include "util.h"
#include <util/strencodings.h>
#include <util/string.h>
#include <boost/date_time/posix_time/posix_time.hpp> //For day of year
#include <cmath>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/newline.hpp>
#include <openssl/crypto.h>
#include <cstdarg>
#include <codecvt>
using namespace std;
bool fPrintToConsole = false;
bool fRequestShutdown = false;
std::atomic<bool> fShutdown = false;
bool fCommandLine = false;
bool fTestNet = false;
bool fNoListen = false;
bool fLogTimestamps = false;
CMedianFilter<int64_t> vTimeOffsets(200,0);
bool fReopenDebugLog = false;
bool fDevbuildCripple;
/** A map that contains all the currently held directory locks. After
* successful locking, these will be held here until the global destructor
* cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
* is called.
*/
static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks;
/** Mutex to protect dir_locks. */
static std::mutex cs_dir_locks;
int GetDayOfYear(int64_t timestamp)
{
try
{
boost::gregorian::date d=boost::posix_time::from_time_t(timestamp).date();
// boost::gregorian::date d(year, month, day);
int dayNumber = d.day_of_year();
return dayNumber;
}
catch (std::out_of_range& e)
{
// Alternatively catch bad_year etc exceptions.
return 0;
}
}
string FormatMoney(int64_t n, bool fPlus)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
int64_t n_abs = (n > 0 ? n : -n);
int64_t quotient = n_abs/COIN;
int64_t remainder = n_abs%COIN;
string str = strprintf("%" PRId64 ".%08" PRId64, quotient, remainder);
// Right-trim excess zeros before the decimal point:
int nTrim = 0;
for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i)
++nTrim;
if (nTrim)
str.erase(str.size()-nTrim, nTrim);
if (n < 0)
str.insert((unsigned int)0, 1, '-');
else if (fPlus && n > 0)
str.insert((unsigned int)0, 1, '+');
return str;
}
bool ParseMoney(const string& str, int64_t& nRet)
{
if (!ValidAsCString(str)) {
return false;
}
return ParseMoney(str.c_str(), nRet);
}
bool ParseMoney(const char* pszIn, int64_t& nRet)
{
string strWhole;
int64_t nUnits = 0;
const char* p = pszIn;
while (IsSpace(*p))
p++;
for (; *p; p++)
{
if (*p == '.')
{
p++;
int64_t nMult = CENT*10;
while (IsDigit(*p) && (nMult > 0))
{
nUnits += nMult * (*p++ - '0');
nMult /= 10;
}
break;
}
if (IsSpace(*p))
break;
if (!IsDigit(*p))
return false;
strWhole.insert(strWhole.end(), *p);
}
for (; *p; p++)
if (!IsSpace(*p))
return false;
if (strWhole.size() > 10) // guard against 63 bit overflow
return false;
if (nUnits < 0 || nUnits > COIN)
return false;
int64_t nWhole = 0;
// Because of the protection above, this assert should never fail.
assert(ParseInt64(strWhole, &nWhole));
int64_t nValue = nWhole*COIN + nUnits;
nRet = nValue;
return true;
}
bool WildcardMatch(const char* psz, const char* mask)
{
while (true)
{
switch (*mask)
{
case '\0':
return (*psz == '\0');
case '*':
return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
case '?':
if (*psz == '\0')
return false;
break;
default:
if (*psz != *mask)
return false;
break;
}
psz++;
mask++;
}
}
bool WildcardMatch(const string& str, const string& mask)
{
return WildcardMatch(str.c_str(), mask.c_str());
}
/**
* Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
* Specifically handles case where path p exists, but it wasn't possible for the user to
* write to the parent directory.
*/
bool TryCreateDirectories(const fs::path& p)
{
try
{
return fs::create_directories(p);
} catch (const fs::filesystem_error&) {
if (!fs::exists(p) || !fs::is_directory(p))
throw;
}
// create_directories didn't create the directory, it had to have existed already
return false;
}
bool DirIsWritable(const fs::path& directory)
{
fs::path tmpFile = directory / fs::unique_path();
FILE* file = fsbridge::fopen(tmpFile, "a");
if (!file) return false;
fclose(file);
remove(tmpFile);
return true;
}
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
{
std::lock_guard<std::mutex> ulock(cs_dir_locks);
fs::path pathLockFile = directory / lockfile_name;
// If a lock for this directory already exists in the map, don't try to re-lock it
if (dir_locks.count(pathLockFile.string())) {
return true;
}
// Create empty lock file if it doesn't exist.
FILE* file = fsbridge::fopen(pathLockFile, "a");
if (file) fclose(file);
auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
if (!lock->TryLock()) {
return error("Error while attempting to lock directory %s: %s", directory.string(), lock->GetReason());
}
if (!probe_only) {
// Lock successful and we're not just probing, put it into the map
dir_locks.emplace(pathLockFile.string(), std::move(lock));
}
return true;
}
std::string GetFileContents(const fs::path filepath)
{
if (!fs::exists(filepath)) {
LogPrintf("GetFileContents: file does not exist %s", filepath);
return "-1";
}
fsbridge::ifstream in(filepath, std::ios::in | std::ios::binary);
if (in.fail()) {
LogPrintf("GetFileContents: error opening file %s", filepath);
return "-1";
}
LogPrint(BCLog::LogFlags::NOISY, "loading file to string %s", filepath);
std::ostringstream out;
out << in.rdbuf();
// Immediately close instead of waiting for the destructor to decrease the
// chance of a race when calling this to read BOINC's client_state.xml:
in.close();
return out.str();
}
#ifndef UPGRADERFLAG
// avoid including unnecessary files for standalone upgrader
static int64_t nTimeOffset = 0;
int64_t GetTimeOffset()
{
return nTimeOffset;
}
int64_t GetAdjustedTime()
{
return GetTime() + GetTimeOffset();
}
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
{
// Ignore duplicates
static set<CNetAddr> setKnown;
if (!setKnown.insert(ip).second)
return;
// Add data
vTimeOffsets.input(nOffsetSample);
LogPrint(BCLog::LogFlags::NOISY, "Added time data, samples %d, offset %+" PRId64 " (%+" PRId64 " minutes)", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
{
// We believe the median of the other nodes 95% and our own node's time ("0" initial offset) 5%. This will also act to gently converge the network to consensus UTC, in case
// the entire network is displaced for some reason.
nTimeOffset = 0.95 * vTimeOffsets.median();
std::vector<int64_t> vSorted = vTimeOffsets.sorted();
// Only let other nodes change our time by so much
if (abs64(nTimeOffset) >= 70 * 60)
{
nTimeOffset = 0;
static bool fDone;
if (!fDone)
{
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
bool fMatch = false;
for (auto const& nOffset : vSorted)
if (nOffset != 0 && abs64(nOffset) < 5 * 60)
fMatch = true;
if (!fMatch)
{
fDone = true;
string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Gridcoin will not work properly.");
strMiscWarning = strMessage;
LogPrintf("*** %s", strMessage);
uiInterface.ThreadSafeMessageBox(strMessage+" ", string("Gridcoin"), CClientUIInterface::MSG_WARNING);
}
}
}
if (LogInstance().WillLogCategory(BCLog::LogFlags::NOISY)) {
for (auto const& n : vSorted)
LogPrintf("%+" PRId64 " ", n);
LogPrintf("| ");
}
LogPrint(BCLog::LogFlags::NOISY, "nTimeOffset = %+" PRId64 " (%+" PRId64 " minutes)", nTimeOffset, nTimeOffset/60);
}
}
#endif
double Round(double d, int place)
{
const double accuracy = std::pow(10, place);
return std::round(d * accuracy) / accuracy;
}
std::string RoundToString(double d, int place)
{
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::fixed << std::setprecision(place) << d;
return ss.str();
}
double RoundFromString(const std::string& s, int place)
{
try
{
double num = boost::lexical_cast<double>(s);
return Round(num, place);
}
catch(const boost::bad_lexical_cast& e)
{
return 0;
}
}
bool Contains(const std::string& data, const std::string& instring)
{
return data.find(instring) != std::string::npos;
}
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
size_t pos = 0;
size_t end = 0;
std::vector<std::string> elems;
while((end = s.find(delim, pos)) != std::string::npos)
{
elems.push_back(s.substr(pos, end - pos));
pos = end + delim.size();
}
// Append final value
elems.push_back(s.substr(pos, end - pos));
return elems;
}
#if HAVE_SYSTEM
void runCommand(std::string strCommand)
{
#ifndef WIN32
int nErr = ::system(strCommand.c_str());
#else
int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
#endif
if (nErr)
LogPrintf("runCommand error: system(%s) returned %d", strCommand, nErr);
}
#endif
void RenameThread(const char* name)
{
#if defined(PR_SET_NAME)
// Only the first 15 characters are used (16 - NUL terminator)
::prctl(PR_SET_NAME, name, 0, 0, 0);
#elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
// TODO : This is currently disabled because it needs to be verified to work
// on FreeBSD or OpenBSD first. When verified the '0 &&' part can be
// removed.
pthread_set_name_np(pthread_self(), name);
// This is XCode 10.6-and-later; bring back if we drop 10.5 support:
// #elif defined(MAC_OSX)
// pthread_setname_np(name);
#else
// Prevent warnings for unused parameters...
(void)name;
#endif
}
bool NewThread(void(*pfn)(void*), void* parg)
{
try
{
boost::thread(pfn, parg); // thread detaches when out of scope
} catch(boost::thread_resource_error &e) {
LogPrintf("Error creating thread: %s", e.what());
return false;
}
return true;
}
bool ThreadHandler::createThread(void(*pfn)(ThreadHandlerPtr), ThreadHandlerPtr parg, const std::string tname)
{
try
{
#if defined(__linux__) && !defined(__GLIBCXX__)
//
// Explicitly set the stack size for this thread to 2 MB.
//
// This supports compilation with musl libc which provides a default
// stack size of 128 KB. Gridcoin chose the scrypt algorithm to hash
// blocks early in the chain. The selected scrypt parameters require
// more than 128 KB of stack space, so we need to increase the stack
// size for threads that hash blocks.
//
// This function is used to create those threads. Since we will port
// Bitcoin's newer thread management utilities, I will not take time
// to generalize this patch. Ideally, we should specify a stack size
// suitable for the application instead of relying on the default of
// the libc implementation. For now, we will let glibc do its thing.
// 2 MB is the typical default for glibc on x86 platforms so this is
// the size we'll start with. After testing, we may choose a smaller
// stack size. This patch may apply to other libc implementations as
// well.
//
boost::thread::attributes attrs;
attrs.set_stack_size(2 << 20);
boost::thread *newThread = new boost::thread(attrs, std::bind(pfn, parg));
#else
boost::thread *newThread = new boost::thread(pfn, parg);
#endif
threadGroup.add_thread(newThread);
threadMap[tname] = newThread;
} catch(boost::thread_resource_error &e) {
LogPrintf("Error creating thread: %s", e.what());
return false;
}
return true;
}
bool ThreadHandler::createThread(void(*pfn)(void*), void* parg, const std::string tname)
{
try
{
#if defined(__linux__) && !defined(__GLIBCXX__)
//
// Explicitly set the stack size for this thread to 2 MB.
//
// This supports compilation with musl libc which provides a default
// stack size of 128 KB. Gridcoin chose the scrypt algorithm to hash
// blocks early in the chain. The selected scrypt parameters require
// more than 128 KB of stack space, so we need to increase the stack
// size for threads that hash blocks.
//
// This function is used to create those threads. Since we will port
// Bitcoin's newer thread management utilities, I will not take time
// to generalize this patch. Ideally, we should specify a stack size
// suitable for the application instead of relying on the default of
// the libc implementation. For now, we will let glibc do its thing.
// 2 MB is the typical default for glibc on x86 platforms so this is
// the size we'll start with. After testing, we may choose a smaller
// stack size. This patch may apply to other libc implementations as
// well.
//
boost::thread::attributes attrs;
attrs.set_stack_size(2 << 20);
boost::thread *newThread = new boost::thread(attrs, std::bind(pfn, parg));
#else
boost::thread *newThread = new boost::thread(pfn, parg);
#endif
threadGroup.add_thread(newThread);
threadMap[tname] = newThread;
} catch(boost::thread_resource_error &e) {
LogPrintf("Error creating thread: %s", e.what());
return false;
}
return true;
}
int ThreadHandler::numThreads()
{
return threadGroup.size();
}
bool ThreadHandler::threadExists(const string tname)
{
if(threadMap.count(tname) > 0)
return true;
else
return false;
}
void ThreadHandler::interruptAll(){
threadGroup.interrupt_all();
}
void ThreadHandler::removeByName(const std::string tname)
{
threadGroup.remove_thread(threadMap[tname]);
threadMap[tname]->join();
threadMap.erase(tname);
}
void ThreadHandler::removeAll()
{
LogPrintf("Wait for %d threads to join.",numThreads());
threadGroup.join_all();
for (auto it=threadMap.begin(); it!=threadMap.end(); ++it)
{
threadGroup.remove_thread(it->second);
}
threadMap.clear();
}
std::string TimestampToHRDate(double dtm)
{
if (dtm == 0) return "1-1-1970 00:00:00";
if (dtm > 9888888888) return "1-1-2199 00:00:00";
std::string sDt = DateTimeStrFormat("%m-%d-%Y %H:%M:%S",dtm);
return sDt;
}