Electroneum
Loading...
Searching...
No Matches
tools Namespace Reference

Various Tools. More...

Namespaces

namespace  base58
namespace  dns_utils
namespace  detail
namespace  error
namespace  wallet_rpc

Classes

class  options
class  Combinator
class  scoped_ptr
struct  DNSResolverData
class  string_copy
class  DNSResolver
 Provides high-level access to DNS resolution. More...
struct  download_thread_control
class  t_http_connection
class  Notify
class  password_container
struct  login
class  PerformanceTimer
class  LoggingPerformanceTimer
class  t_rpc_client
class  scoped_message_writer
class  threadpool
 A global thread pool. More...
struct  close_file
 Functional class for closing C file handles. More...
class  private_file
 A file restricted to process owner AND process. Deletes file on destruction. More...
class  file_locker
class  signal_handler
 Defines a signal handler for win32 and *nix. More...
class  NodeRPCProxy
class  ringdb
class  gamma_picker
class  wallet_keys_unlocker
class  i_wallet2_callback
class  wallet_device_callback
struct  tx_dust_policy
class  hashchain
class  wallet2
struct  COMMAND_RPC_GET_ADDRESS_TXS
struct  COMMAND_RPC_GET_ADDRESS_INFO
struct  COMMAND_RPC_GET_UNSPENT_OUTS
struct  COMMAND_RPC_LOGIN
struct  COMMAND_RPC_IMPORT_WALLET_REQUEST
class  wallet_rpc_server

Typedefs

typedef class scoped_ptr< ub_result, ub_resolve_freeub_result_ptr
typedef std::shared_ptr< download_thread_controldownload_async_handle

Enumerations

enum  { EVARINT_OVERFLOW = -1 , EVARINT_REPRESENT = -2 }
 Error codes for varint. More...

Functions

template<typename F>
void apply_permutation (std::vector< size_t > permutation, const F &swap)
template<typename T>
void apply_permutation (const std::vector< size_t > &permutation, std::vector< T > &v)
template<class t_object>
bool serialize_obj_to_file (t_object &obj, const std::string &file_path)
template<class t_object>
bool unserialize_obj_from_file (t_object &obj, const std::string &file_path)
uint64_t combinations_count (uint32_t k, uint32_t n)
boost::optional< std::string > ipv4_to_string (const char *src, size_t len)
boost::optional< std::string > ipv6_to_string (const char *src, size_t len)
boost::optional< std::string > txt_to_string (const char *src, size_t len)
bool download (const std::string &path, const std::string &url, std::function< bool(const std::string &, const std::string &, size_t, ssize_t)> cb)
download_async_handle download_async (const std::string &path, const std::string &url, std::function< void(const std::string &, const std::string &, bool)> result, std::function< bool(const std::string &, const std::string &, size_t, ssize_t)> progress)
bool download_finished (const download_async_handle &control)
bool download_error (const download_async_handle &control)
bool download_wait (const download_async_handle &control)
bool download_cancel (const download_async_handle &control)
uint64_t get_tick_count ()
uint64_t ticks_to_ns (uint64_t ticks)
void set_performance_timer_log_level (el::Level level)
uint64_t get_ticks_per_ns ()
uint32_t make_pruning_seed (uint32_t stripe, uint32_t log_stripes)
bool has_unpruned_block (uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
uint32_t get_pruning_stripe (uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
uint32_t get_pruning_seed (uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
uint64_t get_next_unpruned_block_height (uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
uint64_t get_next_pruned_block_height (uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
uint32_t get_random_stripe ()
constexpr uint32_t get_pruning_log_stripes (uint32_t pruning_seed)
uint32_t get_pruning_stripe (uint32_t pruning_seed)
scoped_message_writer success_msg_writer (bool color=true)
scoped_message_writer msg_writer (epee::console_colors color=epee::console_color_default)
scoped_message_writer fail_msg_writer ()
int spawn (const char *filename, const std::vector< std::string > &args, bool wait)
void set_stack_trace_log (const std::string &log)
void log_stack_trace (const char *msg)
bool check_updates (const std::string &software, const std::string &buildtag, std::string &version, std::string &hash)
std::string get_update_url (const std::string &software, const std::string &subdir, const std::string &buildtag, const std::string &version, bool user)
std::string get_nix_version_display_string ()
std::string get_os_version_string ()
 Returns the OS version string.
std::string get_default_data_dir ()
 Returns the default data directory.
bool create_directories_if_necessary (const std::string &path)
 creates directories for a path
std::error_code replace_file (const std::string &old_name, const std::string &new_name)
 std::rename wrapper for nix and something strange for windows.
bool sanitize_locale ()
bool disable_core_dumps ()
ssize_t get_lockable_memory ()
bool on_startup ()
void set_strict_default_file_permissions (bool strict)
boost::optional< bool > is_hdd (const char *file_path)
void set_max_concurrency (unsigned n)
unsigned get_max_concurrency ()
bool is_local_address (const std::string &address)
int vercmp (const char *v0, const char *v1)
bool sha256sum (const uint8_t *data, size_t len, crypto::hash &hash)
bool sha256sum (const std::string &filename, crypto::hash &hash)
boost::optional< std::pair< uint32_t, uint32_t > > parse_subaddress_lookahead (const std::string &str)
int display_simple_progress_spinner (int x)
std::string glob_to_regex (const std::string &val)
void closefrom (int fd)
std::string get_human_readable_timestamp (uint64_t ts)
std::string get_human_readable_bytes (uint64_t bytes)
template<typename OutputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type write_varint (OutputIt &&dest, T i)
 writes a varint to a stream.
template<typename T>
std::string get_varint_data (const T &v)
 Returns the string that represents the varint.
template<int bits, typename InputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type read_varint (InputIt &&first, InputIt &&last, T &write)
 reads in the varint that is pointed to by InputIt into write
template<typename InputIt, typename T>
int read_varint (InputIt &&first, InputIt &&last, T &i)
 Wrapper around the other read_varint, Sets template parameters for you.

Variables

el::Level performance_timer_log_level = el::Level::Info
const size_t MAX_SPLIT_ATTEMPTS = 30

Detailed Description

Various Tools.

Typedef Documentation

◆ download_async_handle

Definition at line 37 of file download.h.

◆ ub_result_ptr

Definition at line 220 of file dns_utils.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Error codes for varint.

Enumerator
EVARINT_OVERFLOW 
EVARINT_REPRESENT 

Definition at line 58 of file varint.h.

58 {
59 /* \brief Represents the overflow error */
61 /* \brief Represents a non conical represnetation */
63 };
@ EVARINT_REPRESENT
Definition varint.h:62
@ EVARINT_OVERFLOW
Definition varint.h:60

Function Documentation

◆ apply_permutation() [1/2]

template<typename T>
void tools::apply_permutation ( const std::vector< size_t > & permutation,
std::vector< T > & v )

Definition at line 64 of file apply_permutation.h.

65{
66 CHECK_AND_ASSERT_THROW_MES(permutation.size() == v.size(), "Mismatched vector sizes");
67 apply_permutation(permutation, [&v](size_t i0, size_t i1){ std::swap(v[i0], v[i1]); });
68}
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
void apply_permutation(std::vector< size_t > permutation, const F &swap)
Here is the call graph for this function:

◆ apply_permutation() [2/2]

template<typename F>
void tools::apply_permutation ( std::vector< size_t > permutation,
const F & swap )

Definition at line 43 of file apply_permutation.h.

44{
45 //sanity check
46 for (size_t n = 0; n < permutation.size(); ++n)
47 CHECK_AND_ASSERT_THROW_MES(std::find(permutation.begin(), permutation.end(), n) != permutation.end(), "Bad permutation");
48
49 for (size_t i = 0; i < permutation.size(); ++i)
50 {
51 size_t current = i;
52 while (i != permutation[current])
53 {
54 size_t next = permutation[current];
55 swap(current, next);
56 permutation[current] = current;
57 current = next;
58 }
59 permutation[current] = current;
60 }
61}
Here is the caller graph for this function:

◆ check_updates()

bool tools::check_updates ( const std::string & software,
const std::string & buildtag,
std::string & version,
std::string & hash )

Definition at line 41 of file updates.cpp.

42 {
43 std::vector<std::string> records;
44 bool found = false;
45
46 MDEBUG("Checking updates for " << buildtag << " " << software);
47
48 // All four ElectroneumPulse domains have DNSSEC on and valid
49 static const std::vector<std::string> dns_urls = {
50 "updates.electroneumpulse.com",
51 "updates.electroneumpulse.info",
52 "updates.electroneumpulse.net",
53 "updates.electroneumpulse.org"
54 };
55
56 if (!tools::dns_utils::load_txt_records_from_dns(records, dns_urls, "update"))
57 return false;
58
59 for (const auto& record : records)
60 {
61 std::vector<std::string> fields;
62 boost::split(fields, record, boost::is_any_of(":"));
63 if (fields.size() != 4)
64 {
65 MWARNING("Updates record does not have 4 fields: " << record);
66 continue;
67 }
68
69 if (software != fields[0] || buildtag != fields[1])
70 continue;
71
72 bool alnum = true;
73 for (auto c: fields[3])
74 if (!isalnum(c))
75 alnum = false;
76 if (fields[3].size() != 64 && !alnum)
77 {
78 MWARNING("Invalid hash: " << fields[3]);
79 continue;
80 }
81
82 // use highest version
83 if (found)
84 {
85 int cmp = vercmp(version.c_str(), fields[2].c_str());
86 if (cmp > 0)
87 continue;
88 if (cmp == 0 && hash != fields[3])
89 MWARNING("Two matches found for " << software << " version " << version << " on " << buildtag);
90 }
91
92 version = fields[2];
93 hash = fields[3];
94
95 MINFO("Found new version " << version << " with hash " << hash);
96 found = true;
97 }
98 return found;
99 }
uint8_t version
#define MWARNING(x)
Definition misc_log_ex.h:74
#define MDEBUG(x)
Definition misc_log_ex.h:76
#define MINFO(x)
Definition misc_log_ex.h:75
POD_CLASS hash
Definition hash.h:50
bool load_txt_records_from_dns(std::vector< std::string > &good_records, const std::vector< std::string > &dns_urls, std::string type)
int vercmp(const char *v0, const char *v1)
Definition util.cpp:915
Here is the call graph for this function:
Here is the caller graph for this function:

◆ closefrom()

void tools::closefrom ( int fd)

Definition at line 1058 of file util.cpp.

1059 {
1060#if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __DragonFly__
1061 ::closefrom(fd);
1062#else
1063#if defined __GLIBC__
1064 const int sc_open_max = sysconf(_SC_OPEN_MAX);
1065 const int MAX_FDS = std::min(65536, sc_open_max);
1066#else
1067 const int MAX_FDS = 65536;
1068#endif
1069 while (fd < MAX_FDS)
1070 {
1071 close(fd);
1072 ++fd;
1073 }
1074#endif
1075 }
void closefrom(int fd)
Definition util.cpp:1058
Here is the call graph for this function:
Here is the caller graph for this function:

◆ combinations_count()

uint64_t tools::combinations_count ( uint32_t k,
uint32_t n )

Definition at line 35 of file combinator.cpp.

36{
37 if (k > n) {
38 throw std::runtime_error("k must not be greater than n");
39 }
40
41 uint64_t c = 1;
42 for (uint64_t i = 1; i <= k; ++i) {
43 c *= n--;
44 c /= i;
45 }
46
47 return c;
48}
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the caller graph for this function:

◆ create_directories_if_necessary()

bool tools::create_directories_if_necessary ( const std::string & path)

creates directories for a path

wrapper around boost::filesyste::create_directories.
(ensure-directory-exists): greenspun's tenth rule in action!

Definition at line 625 of file util.cpp.

626 {
627 namespace fs = boost::filesystem;
628 boost::system::error_code ec;
629 fs::path fs_path(path);
630 if (fs::is_directory(fs_path, ec))
631 {
632 return true;
633 }
634
635 bool res = fs::create_directories(fs_path, ec);
636 if (res)
637 {
638 LOG_PRINT_L2("Created directory: " << path);
639 }
640 else
641 {
642 LOG_PRINT_L2("Can't create directory: " << path << ", err: "<< ec.message());
643 }
644
645 return res;
646 }
const char * res
#define LOG_PRINT_L2(x)
Here is the caller graph for this function:

◆ disable_core_dumps()

bool tools::disable_core_dumps ( )

Definition at line 748 of file util.cpp.

749 {
750#ifdef __GLIBC__
751 // disable core dumps in release mode
752 struct rlimit rlimit;
753 rlimit.rlim_cur = rlimit.rlim_max = 0;
754 if (setrlimit(RLIMIT_CORE, &rlimit))
755 {
756 MWARNING("Failed to disable core dumps");
757 return false;
758 }
759#endif
760 return true;
761 }
Here is the caller graph for this function:

◆ display_simple_progress_spinner()

int tools::display_simple_progress_spinner ( int x)

Definition at line 995 of file util.cpp.

995 {
996 std::string s;
997 x++;
998 if (x == 1) {
999 s = "|";
1000 } else if (x == 2) {
1001 s = "/";
1002 } else if (x == 3) {
1003 s = "-";
1004 } else {
1005 s = "\\";
1006 x = 0;
1007 }
1008 std::cout << "\r" << s << std::flush;
1009 return x;
1010 }

◆ download()

bool tools::download ( const std::string & path,
const std::string & url,
std::function< bool(const std::string &, const std::string &, size_t, ssize_t)> cb )

Definition at line 258 of file download.cpp.

259 {
260 bool success = false;
261 download_async_handle handle = download_async(path, url, [&success](const std::string&, const std::string&, bool result) {success = result;}, cb);
262 download_wait(handle);
263 return success;
264 }
std::shared_ptr< download_thread_control > download_async_handle
Definition download.h:37
download_async_handle download_async(const std::string &path, const std::string &url, std::function< void(const std::string &, const std::string &, bool)> result, std::function< bool(const std::string &, const std::string &, size_t, ssize_t)> progress)
Definition download.cpp:266
bool download_wait(const download_async_handle &control)
Definition download.cpp:287
Here is the call graph for this function:
Here is the caller graph for this function:

◆ download_async()

download_async_handle tools::download_async ( const std::string & path,
const std::string & url,
std::function< void(const std::string &, const std::string &, bool)> result,
std::function< bool(const std::string &, const std::string &, size_t, ssize_t)> progress )

Definition at line 266 of file download.cpp.

267 {
268 download_async_handle control = std::make_shared<download_thread_control>(path, url, result, progress);
269 control->thread = boost::thread([control](){ download_thread(control); });
270 return control;
271 }
Here is the caller graph for this function:

◆ download_cancel()

bool tools::download_cancel ( const download_async_handle & control)

Definition at line 299 of file download.cpp.

300 {
301 CHECK_AND_ASSERT_MES(control != 0, false, "NULL async download handle");
302 {
303 boost::lock_guard<boost::mutex> lock(control->mutex);
304 if (control->stopped)
305 return true;
306 control->stop = true;
307 }
308 control->thread.join();
309 return true;
310 }
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Here is the caller graph for this function:

◆ download_error()

bool tools::download_error ( const download_async_handle & control)

Definition at line 280 of file download.cpp.

281 {
282 CHECK_AND_ASSERT_MES(control != 0, false, "NULL async download handle");
283 boost::lock_guard<boost::mutex> lock(control->mutex);
284 return !control->success;
285 }

◆ download_finished()

bool tools::download_finished ( const download_async_handle & control)

Definition at line 273 of file download.cpp.

274 {
275 CHECK_AND_ASSERT_MES(control != 0, false, "NULL async download handle");
276 boost::lock_guard<boost::mutex> lock(control->mutex);
277 return control->stopped;
278 }

◆ download_wait()

bool tools::download_wait ( const download_async_handle & control)

Definition at line 287 of file download.cpp.

288 {
289 CHECK_AND_ASSERT_MES(control != 0, false, "NULL async download handle");
290 {
291 boost::lock_guard<boost::mutex> lock(control->mutex);
292 if (control->stopped)
293 return true;
294 }
295 control->thread.join();
296 return true;
297 }
Here is the caller graph for this function:

◆ fail_msg_writer()

scoped_message_writer tools::fail_msg_writer ( )
inline

Definition at line 131 of file scoped_message_writer.h.

132{
134}
@ Error
Information representing errors in application but application will keep running.
@ console_color_red

◆ get_default_data_dir()

std::string tools::get_default_data_dir ( )

Returns the default data directory.

Windows < Vista: C:\Documents and Settings\Username\Application Data\CRYPTONOTE_NAME

Windows >= Vista: C:\Users\Username\AppData\Roaming\CRYPTONOTE_NAME

Mac: ~/Library/Application Support/CRYPTONOTE_NAME

Unix: ~/.CRYPTONOTE_NAME

Definition at line 600 of file util.cpp.

601 {
602 /* Please for the love of god refactor the ifdefs out of this */
603
604 // namespace fs = boost::filesystem;
605 // Windows < Vista: C:\Documents and Settings\Username\Application Data\CRYPTONOTE_NAME
606 // Windows >= Vista: C:\Users\Username\AppData\Roaming\CRYPTONOTE_NAME
607 // Unix & Mac: ~/.CRYPTONOTE_NAME
608 std::string config_folder;
609
610#ifdef WIN32
611 config_folder = get_special_folder_path(CSIDL_COMMON_APPDATA, true) + "\\" + CRYPTONOTE_NAME;
612#else
613 std::string pathRet;
614 char* pszHome = getenv("HOME");
615 if (pszHome == NULL || strlen(pszHome) == 0)
616 pathRet = "/";
617 else
618 pathRet = pszHome;
619 config_folder = (pathRet + "/." + CRYPTONOTE_NAME);
620#endif
621
622 return config_folder;
623 }
#define CRYPTONOTE_NAME
Here is the caller graph for this function:

◆ get_human_readable_bytes()

std::string tools::get_human_readable_bytes ( uint64_t bytes)

Definition at line 1089 of file util.cpp.

1090 {
1091 // Use 1024 for "kilo", 1024*1024 for "mega" and so on instead of the more modern and standard-conforming
1092 // 1000, 1000*1000 and so on, to be consistent with other Electroneum code that also uses base 2 units
1093 struct byte_map
1094 {
1095 const char* const format;
1096 const std::uint64_t bytes;
1097 };
1098
1099 static constexpr const byte_map sizes[] =
1100 {
1101 {"%.0f B", 1024},
1102 {"%.2f KB", 1024 * 1024},
1103 {"%.2f MB", std::uint64_t(1024) * 1024 * 1024},
1104 {"%.2f GB", std::uint64_t(1024) * 1024 * 1024 * 1024},
1105 {"%.2f TB", std::uint64_t(1024) * 1024 * 1024 * 1024 * 1024}
1106 };
1107
1108 struct bytes_less
1109 {
1110 bool operator()(const byte_map& lhs, const byte_map& rhs) const noexcept
1111 {
1112 return lhs.bytes < rhs.bytes;
1113 }
1114 };
1115
1116 const auto size = std::upper_bound(
1117 std::begin(sizes), std::end(sizes) - 1, byte_map{"", bytes}, bytes_less{}
1118 );
1119 const std::uint64_t divisor = size->bytes / 1024;
1120 return (boost::format(size->format) % (double(bytes) / divisor)).str();
1121 }
Here is the caller graph for this function:

◆ get_human_readable_timestamp()

std::string tools::get_human_readable_timestamp ( uint64_t ts)

Definition at line 1077 of file util.cpp.

1078 {
1079 char buffer[64];
1080 if (ts < 1234567890)
1081 return "<unknown>";
1082 time_t tt = ts;
1083 struct tm tm;
1085 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
1086 return std::string(buffer);
1087 }
bool get_gmt_time(time_t t, struct tm &tm)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_lockable_memory()

ssize_t tools::get_lockable_memory ( )

Definition at line 763 of file util.cpp.

764 {
765#ifdef __GLIBC__
766 struct rlimit rlim;
767 if (getrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
768 {
769 MERROR("Failed to determine the lockable memory limit");
770 return -1;
771 }
772 return rlim.rlim_cur;
773#else
774 return -1;
775#endif
776 }
#define MERROR(x)
Definition misc_log_ex.h:73
Here is the caller graph for this function:

◆ get_max_concurrency()

unsigned tools::get_max_concurrency ( )

Definition at line 868 of file util.cpp.

869 {
870 boost::lock_guard<boost::mutex> lock(max_concurrency_lock);
871 return max_concurrency;
872 }
Here is the caller graph for this function:

◆ get_next_pruned_block_height()

uint64_t tools::get_next_pruned_block_height ( uint64_t block_height,
uint64_t blockchain_height,
uint32_t pruning_seed )

Definition at line 93 of file pruning.cpp.

94{
95 const uint32_t stripe = get_pruning_stripe(pruning_seed);
96 if (stripe == 0)
97 return blockchain_height;
98 if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
99 return blockchain_height;
100 const uint32_t seed_log_stripes = get_pruning_log_stripes(pruning_seed);
101 const uint64_t log_stripes = seed_log_stripes ? seed_log_stripes : CRYPTONOTE_PRUNING_LOG_STRIPES;
102 const uint64_t mask = (1ul << log_stripes) - 1;
103 const uint32_t block_pruning_seed = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & mask) + 1;
104 if (block_pruning_seed != stripe)
105 return block_height;
106 const uint32_t next_stripe = 1 + (block_pruning_seed & mask);
107 return get_next_unpruned_block_height(block_height, blockchain_height, tools::make_pruning_seed(next_stripe, log_stripes));
108}
#define CRYPTONOTE_PRUNING_LOG_STRIPES
#define CRYPTONOTE_PRUNING_TIP_BLOCKS
#define CRYPTONOTE_PRUNING_STRIPE_SIZE
uint64_t get_next_unpruned_block_height(uint64_t block_height, uint64_t blockchain_height, uint32_t pruning_seed)
Definition pruning.cpp:69
constexpr uint32_t get_pruning_log_stripes(uint32_t pruning_seed)
Definition pruning.h:40
uint32_t get_pruning_stripe(uint64_t block_height, uint64_t blockchain_height, uint32_t log_stripes)
Definition pruning.cpp:54
uint32_t make_pruning_seed(uint32_t stripe, uint32_t log_stripes)
Definition pruning.cpp:37
unsigned int uint32_t
Definition stdint.h:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_next_unpruned_block_height()

uint64_t tools::get_next_unpruned_block_height ( uint64_t block_height,
uint64_t blockchain_height,
uint32_t pruning_seed )

Definition at line 69 of file pruning.cpp.

70{
71 CHECK_AND_ASSERT_MES(block_height <= CRYPTONOTE_MAX_BLOCK_NUMBER+1, block_height, "block_height too large");
72 CHECK_AND_ASSERT_MES(blockchain_height <= CRYPTONOTE_MAX_BLOCK_NUMBER+1, block_height, "blockchain_height too large");
73 const uint32_t stripe = get_pruning_stripe(pruning_seed);
74 if (stripe == 0)
75 return block_height;
76 if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
77 return block_height;
78 const uint32_t seed_log_stripes = get_pruning_log_stripes(pruning_seed);
79 const uint64_t log_stripes = seed_log_stripes ? seed_log_stripes : CRYPTONOTE_PRUNING_LOG_STRIPES;
80 const uint64_t mask = (1ul << log_stripes) - 1;
81 const uint32_t block_pruning_stripe = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & mask) + 1;
82 if (block_pruning_stripe == stripe)
83 return block_height;
84 const uint64_t cycles = ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) >> log_stripes);
85 const uint64_t cycle_start = cycles + ((stripe > block_pruning_stripe) ? 0 : 1);
86 const uint64_t h = cycle_start * (CRYPTONOTE_PRUNING_STRIPE_SIZE << log_stripes) + (stripe - 1) * CRYPTONOTE_PRUNING_STRIPE_SIZE;
87 if (h + CRYPTONOTE_PRUNING_TIP_BLOCKS > blockchain_height)
88 return blockchain_height < CRYPTONOTE_PRUNING_TIP_BLOCKS ? 0 : blockchain_height - CRYPTONOTE_PRUNING_TIP_BLOCKS;
89 CHECK_AND_ASSERT_MES(h >= block_height, block_height, "h < block_height, unexpected");
90 return h;
91}
#define CRYPTONOTE_MAX_BLOCK_NUMBER
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_nix_version_display_string()

std::string tools::get_nix_version_display_string ( )

Definition at line 554 of file util.cpp.

555{
556 struct utsname un;
557
558 if(uname(&un) < 0)
559 return std::string("*nix: failed to get os version");
560 return std::string() + un.sysname + " " + un.version + " " + un.release;
561}
Here is the caller graph for this function:

◆ get_os_version_string()

std::string tools::get_os_version_string ( )

Returns the OS version string.

This is a wrapper around the primitives get_windows_version_display_string() and get_nix_version_display_string()

Definition at line 566 of file util.cpp.

567 {
568#ifdef WIN32
569 return get_windows_version_display_string();
570#else
572#endif
573 }
std::string get_nix_version_display_string()
Definition util.cpp:554
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pruning_log_stripes()

uint32_t tools::get_pruning_log_stripes ( uint32_t pruning_seed)
inlineconstexpr

Definition at line 40 of file pruning.h.

40{ return (pruning_seed >> PRUNING_SEED_LOG_STRIPES_SHIFT) & PRUNING_SEED_LOG_STRIPES_MASK; }
Here is the caller graph for this function:

◆ get_pruning_seed()

uint32_t tools::get_pruning_seed ( uint64_t block_height,
uint64_t blockchain_height,
uint32_t log_stripes )

Definition at line 61 of file pruning.cpp.

62{
63 const uint32_t stripe = get_pruning_stripe(block_height, blockchain_height, log_stripes);
64 if (stripe == 0)
65 return 0;
66 return make_pruning_seed(stripe, log_stripes);
67}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pruning_stripe() [1/2]

uint32_t tools::get_pruning_stripe ( uint32_t pruning_seed)
inline

Definition at line 41 of file pruning.h.

41{ if (pruning_seed == 0) return 0; return 1 + ((pruning_seed >> PRUNING_SEED_STRIPE_SHIFT) & PRUNING_SEED_STRIPE_MASK); }

◆ get_pruning_stripe() [2/2]

uint32_t tools::get_pruning_stripe ( uint64_t block_height,
uint64_t blockchain_height,
uint32_t log_stripes )

Definition at line 54 of file pruning.cpp.

55{
56 if (block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height)
57 return 0;
58 return ((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & (uint64_t)((1ul << log_stripes) - 1)) + 1;
59}
Here is the caller graph for this function:

◆ get_random_stripe()

uint32_t tools::get_random_stripe ( )

Definition at line 110 of file pruning.cpp.

111{
113}
std::enable_if< std::is_pod< T >::value, T >::type rand()
Definition crypto.h:216
Here is the call graph for this function:

◆ get_tick_count()

uint64_t tools::get_tick_count ( )

Definition at line 46 of file perf_timer.cpp.

47 {
48#if defined(__x86_64__)
49 uint32_t hi, lo;
50 __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
51 return (((uint64_t)hi) << 32) | (uint64_t)lo;
52#else
54#endif
55 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_ticks_per_ns()

uint64_t tools::get_ticks_per_ns ( )

◆ get_update_url()

std::string tools::get_update_url ( const std::string & software,
const std::string & subdir,
const std::string & buildtag,
const std::string & version,
bool user )

Definition at line 101 of file updates.cpp.

102 {
103 const char *base = user ? "http://s3-eu-west-1.amazonaws.com/updates.electroneum.com/releases/download/v" : "http://s3-eu-west-1.amazonaws.com/updates.electroneum.com/releases/download/v";
104#ifdef _WIN32
105 static const char *extension = strncmp(buildtag.c_str(), "install-", 8) ? ".zip" : ".exe";
106#else
107 static const char extension[] = ".tar.bz2";
108#endif
109
110 std::string url;
111
112 url = base;
113// if (!subdir.empty())
114// url += subdir + "/";
115 url = url + version + "/" + software + "-" + buildtag + "-v" + version + extension;
116 return url;
117 }
Here is the caller graph for this function:

◆ get_varint_data()

template<typename T>
std::string tools::get_varint_data ( const T & v)

Returns the string that represents the varint.

Definition at line 85 of file varint.h.

86 {
87 std::stringstream ss;
88 write_varint(std::ostreambuf_iterator<char>(ss), v);
89 return ss.str();
90 }
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type write_varint(OutputIt &&dest, T i)
writes a varint to a stream.
Definition varint.h:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ glob_to_regex()

std::string tools::glob_to_regex ( const std::string & val)

Definition at line 1012 of file util.cpp.

1013 {
1014 std::string newval;
1015
1016 bool escape = false;
1017 for (char c: val)
1018 {
1019 if (c == '*')
1020 newval += escape ? "*" : ".*";
1021 else if (c == '?')
1022 newval += escape ? "?" : ".";
1023 else if (c == '\\')
1024 newval += '\\', escape = !escape;
1025 else
1026 newval += c;
1027 }
1028 return newval;
1029 }
Here is the caller graph for this function:

◆ has_unpruned_block()

bool tools::has_unpruned_block ( uint64_t block_height,
uint64_t blockchain_height,
uint32_t pruning_seed )

Definition at line 44 of file pruning.cpp.

45{
46 const uint32_t stripe = get_pruning_stripe(pruning_seed);
47 if (stripe == 0)
48 return true;
49 const uint32_t log_stripes = get_pruning_log_stripes(pruning_seed);
50 uint32_t block_stripe = get_pruning_stripe(block_height, blockchain_height, log_stripes);
51 return block_stripe == 0 || block_stripe == stripe;
52}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ipv4_to_string()

boost::optional< std::string > tools::ipv4_to_string ( const char * src,
size_t len )

Definition at line 136 of file dns_utils.cpp.

137{
138 if (len < 4)
139 {
140 MERROR("Invalid IPv4 address: " << std::string(src, len));
141 return boost::none;
142 }
143
144 std::stringstream ss;
145 unsigned int bytes[4];
146 for (int i = 0; i < 4; i++)
147 {
148 unsigned char a = src[i];
149 bytes[i] = a;
150 }
151 ss << bytes[0] << "."
152 << bytes[1] << "."
153 << bytes[2] << "."
154 << bytes[3];
155 return ss.str();
156}
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
Here is the caller graph for this function:

◆ ipv6_to_string()

boost::optional< std::string > tools::ipv6_to_string ( const char * src,
size_t len )

Definition at line 160 of file dns_utils.cpp.

161{
162 if (len < 8)
163 {
164 MERROR("Invalid IPv4 address: " << std::string(src, len));
165 return boost::none;
166 }
167
168 std::stringstream ss;
169 unsigned int bytes[8];
170 for (int i = 0; i < 8; i++)
171 {
172 unsigned char a = src[i];
173 bytes[i] = a;
174 }
175 ss << bytes[0] << ":"
176 << bytes[1] << ":"
177 << bytes[2] << ":"
178 << bytes[3] << ":"
179 << bytes[4] << ":"
180 << bytes[5] << ":"
181 << bytes[6] << ":"
182 << bytes[7];
183 return ss.str();
184}
Here is the caller graph for this function:

◆ is_hdd()

boost::optional< bool > tools::is_hdd ( const char * file_path)

Definition at line 813 of file util.cpp.

814 {
815#ifdef __GLIBC__
816 struct stat st;
817 std::string prefix;
818 if(stat(file_path, &st) == 0)
819 {
820 std::ostringstream s;
821 s << "/sys/dev/block/" << major(st.st_dev) << ":" << minor(st.st_dev);
822 prefix = s.str();
823 }
824 else
825 {
826 return boost::none;
827 }
828 std::string attr_path = prefix + "/queue/rotational";
829 std::ifstream f(attr_path, std::ios_base::in);
830 if(not f.is_open())
831 {
832 attr_path = prefix + "/../queue/rotational";
833 f.open(attr_path, std::ios_base::in);
834 if(not f.is_open())
835 {
836 return boost::none;
837 }
838 }
839 unsigned short val = 0xdead;
840 f >> val;
841 if(not f.fail())
842 {
843 return (val == 1);
844 }
845 return boost::none;
846#else
847 return boost::none;
848#endif
849 }
Here is the caller graph for this function:

◆ is_local_address()

bool tools::is_local_address ( const std::string & address)

Definition at line 874 of file util.cpp.

875 {
876 // always assume Tor/I2P addresses to be untrusted by default
877 if (boost::ends_with(address, ".onion") || boost::ends_with(address, ".i2p"))
878 {
879 MDEBUG("Address '" << address << "' is Tor/I2P, non local");
880 return false;
881 }
882
883 // extract host
886 {
887 MWARNING("Failed to determine whether address '" << address << "' is local, assuming not");
888 return false;
889 }
890 if (u_c.host.empty())
891 {
892 MWARNING("Failed to determine whether address '" << address << "' is local, assuming not");
893 return false;
894 }
895
896 // resolve to IP
897 boost::asio::io_service io_service;
898 boost::asio::ip::tcp::resolver resolver(io_service);
899 boost::asio::ip::tcp::resolver::query query(u_c.host, "");
900 boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);
901 while (i != boost::asio::ip::tcp::resolver::iterator())
902 {
903 const boost::asio::ip::tcp::endpoint &ep = *i;
904 if (ep.address().is_loopback())
905 {
906 MDEBUG("Address '" << address << "' is local");
907 return true;
908 }
909 ++i;
910 }
911
912 MDEBUG("Address '" << address << "' is not local");
913 return false;
914 }
bool parse_url(const std::string url_str, http::url_content &content)
const char * address
Definition multisig.cpp:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_stack_trace()

void tools::log_stack_trace ( const char * msg)

Definition at line 121 of file stack_trace.cpp.

122{
123#ifdef USE_UNWIND
124 unw_context_t ctx;
125 unw_cursor_t cur;
126 unw_word_t ip, off;
127 unsigned level;
128 char sym[512], *dsym;
129 int status;
130 const char *log = stack_trace_log.empty() ? NULL : stack_trace_log.c_str();
131#endif
132
133 if (msg)
134 ST_LOG(msg);
135 ST_LOG("Unwound call stack:");
136
137#ifdef USE_UNWIND
138 if (unw_getcontext(&ctx) < 0) {
139 ST_LOG("Failed to create unwind context");
140 return;
141 }
142 if (unw_init_local(&cur, &ctx) < 0) {
143 ST_LOG("Failed to find the first unwind frame");
144 return;
145 }
146 for (level = 1; level < 999; ++level) { // 999 for safety
147 int ret = unw_step(&cur);
148 if (ret < 0) {
149 ST_LOG("Failed to find the next frame");
150 return;
151 }
152 if (ret == 0)
153 break;
154 if (unw_get_reg(&cur, UNW_REG_IP, &ip) < 0) {
155 ST_LOG(" " << std::setw(4) << level);
156 continue;
157 }
158 if (unw_get_proc_name(&cur, sym, sizeof(sym), &off) < 0) {
159 ST_LOG(" " << std::setw(4) << level << std::setbase(16) << std::setw(20) << "0x" << ip);
160 continue;
161 }
162 dsym = abi::__cxa_demangle(sym, NULL, NULL, &status);
163 ST_LOG(" " << std::setw(4) << level << std::setbase(16) << std::setw(20) << "0x" << ip << " " << (!status && dsym ? dsym : sym) << " + " << "0x" << off);
164 free(dsym);
165 }
166#else
167 std::stringstream ss;
168 ss << el::base::debug::StackTrace();
169 std::vector<std::string> lines;
170 std::string s = ss.str();
171 boost::split(lines, s, boost::is_any_of("\n"));
172 for (const auto &line: lines)
173 ST_LOG(line);
174#endif
175}
#define ST_LOG(x)

◆ make_pruning_seed()

uint32_t tools::make_pruning_seed ( uint32_t stripe,
uint32_t log_stripes )

Definition at line 37 of file pruning.cpp.

38{
39 CHECK_AND_ASSERT_THROW_MES(log_stripes <= PRUNING_SEED_LOG_STRIPES_MASK, "log_stripes out of range");
40 CHECK_AND_ASSERT_THROW_MES(stripe > 0 && stripe <= (1ul << log_stripes), "stripe out of range");
41 return (log_stripes << PRUNING_SEED_LOG_STRIPES_SHIFT) | ((stripe - 1) << PRUNING_SEED_STRIPE_SHIFT);
42}
Here is the caller graph for this function:

◆ msg_writer()

Definition at line 126 of file scoped_message_writer.h.

127{
128 return scoped_message_writer(color, false, std::string(), el::Level::Info);
129}
@ Info
Mainly useful to represent current progress of application.
Here is the caller graph for this function:

◆ on_startup()

bool tools::on_startup ( )

Definition at line 778 of file util.cpp.

779 {
780 mlog_configure("", true);
781
782 setup_crash_dump();
783
785
786#ifdef __GLIBC__
787 const char *ver = gnu_get_libc_version();
788 if (!strcmp(ver, "2.25"))
789 MCLOG_RED(el::Level::Warning, "global", "Running with glibc " << ver << ", hangs may occur - change glibc version if possible");
790#endif
791
792#if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_TEXT)
793 SSL_library_init();
794#else
795 OPENSSL_init_ssl(0, NULL);
796#endif
797
798 if (!unbound_built_with_threads())
799 MCLOG_RED(el::Level::Warning, "global", "libunbound was not built with threads enabled - crashes may occur");
800
801 return true;
802 }
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition mlog.cpp:148
#define MCLOG_RED(level, cat, x)
Definition misc_log_ex.h:58
@ Warning
Useful when application has potentially harmful situtaions.
bool sanitize_locale()
Definition util.cpp:690
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_subaddress_lookahead()

boost::optional< std::pair< uint32_t, uint32_t > > tools::parse_subaddress_lookahead ( const std::string & str)

Definition at line 977 of file util.cpp.

978 {
979 auto pos = str.find(":");
980 bool r = pos != std::string::npos;
981 uint32_t major;
982 r = r && epee::string_tools::get_xtype_from_string(major, str.substr(0, pos));
983 uint32_t minor;
984 r = r && epee::string_tools::get_xtype_from_string(minor, str.substr(pos + 1));
985 if (r)
986 {
987 return std::make_pair(major, minor);
988 }
989 else
990 {
991 return {};
992 }
993 }
PUSH_WARNINGS bool get_xtype_from_string(OUT XType &val, const std::string &str_id)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_varint() [1/2]

template<typename InputIt, typename T>
int tools::read_varint ( InputIt && first,
InputIt && last,
T & i )

Wrapper around the other read_varint, Sets template parameters for you.

Definition at line 126 of file varint.h.

126 {
127 return read_varint<std::numeric_limits<T>::digits>(std::forward<InputIt>(first), std::forward<InputIt>(last), i);
128 }
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type read_varint(InputIt &&first, InputIt &&last, T &write)
reads in the varint that is pointed to by InputIt into write
Definition varint.h:95
Here is the call graph for this function:

◆ read_varint() [2/2]

template<int bits, typename InputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type tools::read_varint ( InputIt && first,
InputIt && last,
T & write )

reads in the varint that is pointed to by InputIt into write

Definition at line 95 of file varint.h.

95 {
96 int read = 0;
97 write = 0;
98 for (int shift = 0;; shift += 7) {
99 if (first == last) {
100 return read;
101 }
102 unsigned char byte = *first;
103 ++first;
104 ++read;
105 if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
106 return EVARINT_OVERFLOW;
107 }
108 if (byte == 0 && shift != 0) {
109 return EVARINT_REPRESENT;
110 }
111
112 write |= static_cast<T>(byte & 0x7f) << shift; /* Does the actually placing into write, stripping the first bit */
113
114 /* If there is no next */
115 if ((byte & 0x80) == 0) {
116 break;
117 }
118 }
119 return read;
120 }
#define T(x)
Here is the caller graph for this function:

◆ replace_file()

std::error_code tools::replace_file ( const std::string & old_name,
const std::string & new_name )

std::rename wrapper for nix and something strange for windows.

Definition at line 648 of file util.cpp.

649 {
650 int code;
651#if defined(WIN32)
652 // Maximizing chances for success
653 std::wstring wide_replacement_name;
654 try { wide_replacement_name = string_tools::utf8_to_utf16(old_name); }
655 catch (...) { return std::error_code(GetLastError(), std::system_category()); }
656 std::wstring wide_replaced_name;
657 try { wide_replaced_name = string_tools::utf8_to_utf16(new_name); }
658 catch (...) { return std::error_code(GetLastError(), std::system_category()); }
659
660 DWORD attributes = ::GetFileAttributesW(wide_replaced_name.c_str());
661 if (INVALID_FILE_ATTRIBUTES != attributes)
662 {
663 ::SetFileAttributesW(wide_replaced_name.c_str(), attributes & (~FILE_ATTRIBUTE_READONLY));
664 }
665
666 bool ok = 0 != ::MoveFileExW(wide_replacement_name.c_str(), wide_replaced_name.c_str(), MOVEFILE_REPLACE_EXISTING);
667 code = ok ? 0 : static_cast<int>(::GetLastError());
668#else
669 bool ok = 0 == std::rename(old_name.c_str(), new_name.c_str());
670 code = ok ? 0 : errno;
671#endif
672 return std::error_code(code, std::system_category());
673 }
Here is the caller graph for this function:

◆ sanitize_locale()

bool tools::sanitize_locale ( )

Definition at line 690 of file util.cpp.

691 {
692 // boost::filesystem throws for "invalid" locales, such as en_US.UTF-8, or kjsdkfs,
693 // so reset it here before any calls to it
694 try
695 {
696 boost::filesystem::path p {std::string("test")};
697 p /= std::string("test");
698 }
699 catch (...)
700 {
701#if defined(__MINGW32__) || defined(__MINGW__)
702 putenv("LC_ALL=C");
703 putenv("LANG=C");
704#else
705 setenv("LC_ALL", "C", 1);
706 setenv("LANG", "C", 1);
707#endif
708 return true;
709 }
710 return false;
711 }
Here is the caller graph for this function:

◆ serialize_obj_to_file()

template<class t_object>
bool tools::serialize_obj_to_file ( t_object & obj,
const std::string & file_path )

Definition at line 43 of file boost_serialization_helper.h.

44 {
45 TRY_ENTRY();
46#if defined(_MSC_VER)
47 // Need to know HANDLE of file to call FlushFileBuffers
48 HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
49 if (INVALID_HANDLE_VALUE == data_file_handle)
50 return false;
51
52 int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0);
53 if (-1 == data_file_descriptor)
54 {
55 ::CloseHandle(data_file_handle);
56 return false;
57 }
58
59 const std::unique_ptr<FILE, tools::close_file> data_file_file{_fdopen(data_file_descriptor, "wb")};
60 if (nullptr == data_file_file)
61 {
62 // Call CloseHandle is not necessary
63 _close(data_file_descriptor);
64 return false;
65 }
66
67 // HACK: undocumented constructor, this code may not compile
68 std::ofstream data_file(data_file_file.get());
69 if (data_file.fail())
70 {
71 // Call CloseHandle and _close are not necessary
72 return false;
73 }
74#else
75 std::ofstream data_file;
76 data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
77 if (data_file.fail())
78 return false;
79#endif
80
81 boost::archive::portable_binary_oarchive a(data_file);
82 a << obj;
83 if (data_file.fail())
84 return false;
85
86 data_file.flush();
87#if defined(_MSC_VER)
88 // To make sure the file is fully stored on disk
89 ::FlushFileBuffers(data_file_handle);
90#endif
91
92 return true;
93 CATCH_ENTRY_L0("serialize_obj_to_file", false);
94 }
#define CATCH_ENTRY_L0(lacation, return_val)
#define TRY_ENTRY()
_W64 signed int intptr_t
Definition stdint.h:164

◆ set_max_concurrency()

void tools::set_max_concurrency ( unsigned n)

Definition at line 857 of file util.cpp.

858 {
859 if (n < 1)
860 n = boost::thread::hardware_concurrency();
861 unsigned hwc = boost::thread::hardware_concurrency();
862 if (n > hwc)
863 n = hwc;
864 boost::lock_guard<boost::mutex> lock(max_concurrency_lock);
865 max_concurrency = n;
866 }
Here is the caller graph for this function:

◆ set_performance_timer_log_level()

void tools::set_performance_timer_log_level ( el::Level level)

Definition at line 96 of file perf_timer.cpp.

97{
98 if (level != el::Level::Debug && level != el::Level::Trace && level != el::Level::Info
99 && level != el::Level::Warning && level != el::Level::Error && level != el::Level::Fatal)
100 {
101 MERROR("Wrong log level: " << el::LevelHelper::convertToString(level) << ", using Info");
102 level = el::Level::Info;
103 }
105}
static const char * convertToString(Level level)
Converts level to associated const char*.
@ Fatal
Severe error information that will presumably abort application.
@ Debug
Informational events most useful for developers to debug application.
@ Trace
Information that can be useful to back-trace certain events - mostly useful than debug logs.
el::Level performance_timer_log_level
Here is the call graph for this function:

◆ set_stack_trace_log()

void tools::set_stack_trace_log ( const std::string & log)

Definition at line 116 of file stack_trace.cpp.

117{
118 stack_trace_log = log;
119}
Here is the caller graph for this function:

◆ set_strict_default_file_permissions()

void tools::set_strict_default_file_permissions ( bool strict)

Definition at line 803 of file util.cpp.

804 {
805#if defined(__MINGW32__) || defined(__MINGW__)
806 // no clue about the odd one out
807#else
808 mode_t mode = strict ? 077 : 0;
809 umask(mode);
810#endif
811 }
Here is the caller graph for this function:

◆ sha256sum() [1/2]

bool tools::sha256sum ( const std::string & filename,
crypto::hash & hash )

Definition at line 945 of file util.cpp.

946 {
948 return false;
949 std::ifstream f;
950 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
951 f.open(filename, std::ios_base::binary | std::ios_base::in | std::ios::ate);
952 if (!f)
953 return false;
954 std::ifstream::pos_type file_size = f.tellg();
955 SHA256_CTX ctx;
956 if (!SHA256_Init(&ctx))
957 return false;
958 size_t size_left = file_size;
959 f.seekg(0, std::ios::beg);
960 while (size_left)
961 {
962 char buf[4096];
963 std::ifstream::pos_type read_size = size_left > sizeof(buf) ? sizeof(buf) : size_left;
964 f.read(buf, read_size);
965 if (!f || !f.good())
966 return false;
967 if (!SHA256_Update(&ctx, buf, read_size))
968 return false;
969 size_left -= read_size;
970 }
971 f.close();
972 if (!SHA256_Final((unsigned char*)hash.data, &ctx))
973 return false;
974 return true;
975 }
bool is_file_exist(const std::string &path)
const char * buf
Here is the call graph for this function:

◆ sha256sum() [2/2]

bool tools::sha256sum ( const uint8_t * data,
size_t len,
crypto::hash & hash )

Definition at line 933 of file util.cpp.

934 {
935 SHA256_CTX ctx;
936 if (!SHA256_Init(&ctx))
937 return false;
938 if (!SHA256_Update(&ctx, data, len))
939 return false;
940 if (!SHA256_Final((unsigned char*)hash.data, &ctx))
941 return false;
942 return true;
943 }
Here is the caller graph for this function:

◆ spawn()

int tools::spawn ( const char * filename,
const std::vector< std::string > & args,
bool wait )

Definition at line 51 of file spawn.cpp.

52{
53#ifdef _WIN32
54 std::string joined = boost::algorithm::join(args, " ");
55 char *commandLine = !joined.empty() ? &joined[0] : nullptr;
56 STARTUPINFOA si = {};
57 si.cb = sizeof(si);
58 PROCESS_INFORMATION pi;
59 if (!CreateProcessA(filename, commandLine, nullptr, nullptr, false, 0, nullptr, nullptr, &si, &pi))
60 {
61 MERROR("CreateProcess failed. Error code " << GetLastError());
62 return -1;
63 }
64
65 BOOST_SCOPE_EXIT(&pi)
66 {
67 CloseHandle(pi.hThread);
68 CloseHandle(pi.hProcess);
69 }
70 BOOST_SCOPE_EXIT_END
71
72 if (!wait)
73 {
74 return 0;
75 }
76
77 DWORD result = WaitForSingleObject(pi.hProcess, INFINITE);
78 if (result != WAIT_OBJECT_0)
79 {
80 MERROR("WaitForSingleObject failed. Result " << result << ", error code " << GetLastError());
81 return -1;
82 }
83
84 DWORD exitCode;
85 if (!GetExitCodeProcess(pi.hProcess, &exitCode))
86 {
87 MERROR("GetExitCodeProcess failed. Error code " << GetLastError());
88 return -1;
89 }
90
91 MINFO("Child exited with " << exitCode);
92 return static_cast<int>(exitCode);
93#else
94 std::vector<char*> argv(args.size() + 1);
95 for (size_t n = 0; n < args.size(); ++n)
96 argv[n] = (char*)args[n].c_str();
97 argv[args.size()] = NULL;
98
99 pid_t pid = fork();
100 if (pid < 0)
101 {
102 MERROR("Error forking: " << strerror(errno));
103 return -1;
104 }
105
106 // child
107 if (pid == 0)
108 {
110 close(0);
111 char *envp[] = {NULL};
112 execve(filename, argv.data(), envp);
113 MERROR("Failed to execve: " << strerror(errno));
114 return -1;
115 }
116
117 // parent
118 if (pid > 0)
119 {
120 if (!wait)
121 {
122 signal(SIGCHLD, SIG_IGN);
123 return 0;
124 }
125
126 while (1)
127 {
128 int wstatus = 0;
129 pid_t w = waitpid(pid, &wstatus, WUNTRACED | WCONTINUED);
130 if (w < 0) {
131 MERROR("Error waiting for child: " << strerror(errno));
132 return -1;
133 }
134 if (WIFEXITED(wstatus))
135 {
136 MINFO("Child exited with " << WEXITSTATUS(wstatus));
137 return WEXITSTATUS(wstatus);
138 }
139 if (WIFSIGNALED(wstatus))
140 {
141 MINFO("Child killed by " << WEXITSTATUS(wstatus));
142 return WEXITSTATUS(wstatus);
143 }
144 }
145 }
146 MERROR("Secret passage found");
147 return -1;
148#endif
149}
void fork(const std::string &pidfile)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ success_msg_writer()

scoped_message_writer tools::success_msg_writer ( bool color = true)
inline

Definition at line 121 of file scoped_message_writer.h.

122{
124}
@ console_color_default
@ console_color_green
Here is the caller graph for this function:

◆ ticks_to_ns()

uint64_t tools::ticks_to_ns ( uint64_t ticks)

Definition at line 79 of file perf_timer.cpp.

80 {
81#if defined(__x86_64__)
82 return 256 * ticks / ticks_per_ns;
83#else
84 return ticks;
85#endif
86 }
Here is the caller graph for this function:

◆ txt_to_string()

boost::optional< std::string > tools::txt_to_string ( const char * src,
size_t len )

Definition at line 186 of file dns_utils.cpp.

187{
188 if (len == 0)
189 return boost::none;
190 return std::string(src+1, len-1);
191}
Here is the caller graph for this function:

◆ unserialize_obj_from_file()

template<class t_object>
bool tools::unserialize_obj_from_file ( t_object & obj,
const std::string & file_path )

Definition at line 97 of file boost_serialization_helper.h.

98 {
99 TRY_ENTRY();
100
101 std::ifstream data_file;
102 data_file.open( file_path, std::ios_base::binary | std::ios_base::in);
103 if(data_file.fail())
104 return false;
105 try
106 {
107 // first try reading in portable mode
109 a >> obj;
110 }
111 catch(...)
112 {
113 // if failed, try reading in unportable mode
114 boost::filesystem::copy_file(file_path, file_path + ".unportable", boost::filesystem::copy_options::overwrite_existing);
115 data_file.close();
116 data_file.open( file_path, std::ios_base::binary | std::ios_base::in);
117 if(data_file.fail())
118 return false;
119 boost::archive::binary_iarchive a(data_file);
120 a >> obj;
121 }
122 return !data_file.fail();
123 CATCH_ENTRY_L0("unserialize_obj_from_file", false);
124 }
Here is the caller graph for this function:

◆ vercmp()

int tools::vercmp ( const char * v0,
const char * v1 )

Definition at line 915 of file util.cpp.

916 {
917 std::vector<std::string> f0, f1;
918 boost::split(f0, v0, boost::is_any_of(".-"));
919 boost::split(f1, v1, boost::is_any_of(".-"));
920 for (size_t i = 0; i < std::max(f0.size(), f1.size()); ++i) {
921 if (i >= f0.size())
922 return -1;
923 if (i >= f1.size())
924 return 1;
925 int f0i = atoi(f0[i].c_str()), f1i = atoi(f1[i].c_str());
926 int n = f0i - f1i;
927 if (n)
928 return n;
929 }
930 return 0;
931 }
Here is the caller graph for this function:

◆ write_varint()

template<typename OutputIt, typename T>
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type tools::write_varint ( OutputIt && dest,
T i )

writes a varint to a stream.

Definition at line 70 of file varint.h.

70 {
71 /* Make sure that there is one after this */
72 while (i >= 0x80) {
73 *dest = (static_cast<char>(i) & 0x7f) | 0x80;
74 ++dest;
75 i >>= 7; /* I should be in multiples of 7, this should just get the next part */
76 }
77 /* writes the last one to dest */
78 *dest = static_cast<char>(i);
79 dest++; /* Seems kinda pointless... */
80 }
CXA_THROW_INFO_T void(* dest)(void *))
Here is the caller graph for this function:

Variable Documentation

◆ MAX_SPLIT_ATTEMPTS

const size_t tools::MAX_SPLIT_ATTEMPTS = 30

Definition at line 991 of file wallet2.cpp.

◆ performance_timer_log_level

el::Level tools::performance_timer_log_level = el::Level::Info

Definition at line 92 of file perf_timer.cpp.