moneroexamples 7 years ago
parent 61cd869127
commit 6da5b3637e

@ -7,7 +7,12 @@ set(PROJECT_NAME
project(${PROJECT_NAME}) project(${PROJECT_NAME})
set(CMAKE_CXX_FLAGS set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++14") "${CMAKE_CXX_FLAGS} -std=c++14 -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS
"${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -DLEAK_SANITIZER")
#
if (WIN32) if (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj -O3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj -O3")
@ -102,16 +107,15 @@ set(LIBRARIES
cryptonote_basic cryptonote_basic
daemonizer daemonizer
cncrypto cncrypto
blocks blocks
lmdb lmdb
ringct ringct
common common
mnemonics mnemonics
epee
easylogging easylogging
checkpoints checkpoints
version version
epee
${Boost_LIBRARIES} ${Boost_LIBRARIES}
pthread pthread
unbound unbound

@ -11,6 +11,7 @@
#include <regex> #include <regex>
using boost::filesystem::path; using boost::filesystem::path;
using xmreg::remove_bad_chars;
using namespace std; using namespace std;
@ -276,18 +277,18 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/block/<string>") CROW_ROUTE(app, "/block/<string>")
([&](const crow::request& req, string block_hash) { ([&](const crow::request& req, string block_hash) {
return crow::response(xmrblocks.show_block(block_hash)); return crow::response(xmrblocks.show_block(remove_bad_chars(block_hash)));
}); });
CROW_ROUTE(app, "/tx/<string>") CROW_ROUTE(app, "/tx/<string>")
([&](const crow::request& req, string tx_hash) { ([&](const crow::request& req, string tx_hash) {
return crow::response(xmrblocks.show_tx(tx_hash)); return crow::response(xmrblocks.show_tx(remove_bad_chars(tx_hash)));
}); });
CROW_ROUTE(app, "/tx/<string>/<uint>") CROW_ROUTE(app, "/tx/<string>/<uint>")
([&](string tx_hash, uint16_t with_ring_signatures) ([&](string tx_hash, uint16_t with_ring_signatures)
{ {
return xmrblocks.show_tx(tx_hash, with_ring_signatures); return xmrblocks.show_tx(remove_bad_chars(tx_hash), with_ring_signatures);
}); });
CROW_ROUTE(app, "/myoutputs").methods("POST"_method) CROW_ROUTE(app, "/myoutputs").methods("POST"_method)
@ -304,13 +305,13 @@ main(int ac, const char* av[])
return string("xmr address, viewkey or tx hash not provided"); return string("xmr address, viewkey or tx hash not provided");
} }
string tx_hash = post_body["tx_hash"]; string tx_hash = remove_bad_chars(post_body["tx_hash"]);
string xmr_address = post_body["xmr_address"]; string xmr_address = remove_bad_chars(post_body["xmr_address"]);
string viewkey = post_body["viewkey"]; string viewkey = remove_bad_chars(post_body["viewkey"]);
// this will be only not empty when checking raw tx data // this will be only not empty when checking raw tx data
// using tx pusher // using tx pusher
string raw_tx_data = post_body["raw_tx_data"]; string raw_tx_data = remove_bad_chars(post_body["raw_tx_data"]);
string domain = get_domain(req); string domain = get_domain(req);
@ -326,8 +327,10 @@ main(int ac, const char* av[])
string domain = get_domain(req); string domain = get_domain(req);
return xmrblocks.show_my_outputs(tx_hash, xmr_address, return xmrblocks.show_my_outputs(remove_bad_chars(tx_hash),
viewkey, string {}, remove_bad_chars(xmr_address),
remove_bad_chars(viewkey),
string {},
domain); domain);
}); });
@ -345,18 +348,21 @@ main(int ac, const char* av[])
"tx hash not provided"); "tx hash not provided");
} }
string tx_hash = post_body["txhash"]; string tx_hash = remove_bad_chars(post_body["txhash"]);
string tx_prv_key = post_body["txprvkey"]; string tx_prv_key = remove_bad_chars(post_body["txprvkey"]);
string xmr_address = post_body["xmraddress"]; string xmr_address = remove_bad_chars(post_body["xmraddress"]);
// this will be only not empty when checking raw tx data // this will be only not empty when checking raw tx data
// using tx pusher // using tx pusher
string raw_tx_data = post_body["raw_tx_data"]; string raw_tx_data = remove_bad_chars(post_body["raw_tx_data"]);
string domain = get_domain(req); string domain = get_domain(req);
return xmrblocks.show_prove(tx_hash, xmr_address, return xmrblocks.show_prove(tx_hash,
tx_prv_key, raw_tx_data, domain); xmr_address,
tx_prv_key,
raw_tx_data,
domain);
}); });
@ -366,8 +372,11 @@ main(int ac, const char* av[])
string domain = get_domain(req); string domain = get_domain(req);
return xmrblocks.show_prove(tx_hash, xmr_address, return xmrblocks.show_prove(remove_bad_chars(tx_hash),
tx_prv_key, string {}, domain); remove_bad_chars(xmr_address),
remove_bad_chars(tx_prv_key),
string {},
domain);
}); });
if (enable_pusher) if (enable_pusher)
@ -388,8 +397,8 @@ main(int ac, const char* av[])
return string("Raw tx data or action not provided"); return string("Raw tx data or action not provided");
} }
string raw_tx_data = post_body["rawtxdata"]; string raw_tx_data = remove_bad_chars(post_body["rawtxdata"]);
string action = post_body["action"]; string action = remove_bad_chars(post_body["action"]);
if (action == "check") if (action == "check")
return xmrblocks.show_checkrawtx(raw_tx_data, action); return xmrblocks.show_checkrawtx(raw_tx_data, action);
@ -423,8 +432,8 @@ main(int ac, const char* av[])
return string("Viewkey not provided. Cant decrypt key image file without it"); return string("Viewkey not provided. Cant decrypt key image file without it");
} }
string raw_data = post_body["rawkeyimgsdata"]; string raw_data = remove_bad_chars(post_body["rawkeyimgsdata"]);
string viewkey = post_body["viewkey"]; string viewkey = remove_bad_chars(post_body["viewkey"]);
return xmrblocks.show_checkrawkeyimgs(raw_data, viewkey); return xmrblocks.show_checkrawkeyimgs(raw_data, viewkey);
}); });
@ -455,8 +464,8 @@ main(int ac, const char* av[])
"key image file without it"); "key image file without it");
} }
string raw_data = post_body["rawoutputkeysdata"]; string raw_data = remove_bad_chars(post_body["rawoutputkeysdata"]);
string viewkey = post_body["viewkey"]; string viewkey = remove_bad_chars(post_body["viewkey"]);
return xmrblocks.show_checkcheckrawoutput(raw_data, viewkey); return xmrblocks.show_checkcheckrawoutput(raw_data, viewkey);
}); });
@ -465,7 +474,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/search").methods("GET"_method) CROW_ROUTE(app, "/search").methods("GET"_method)
([&](const crow::request& req) { ([&](const crow::request& req) {
return xmrblocks.search(string(req.url_params.get("value"))); return xmrblocks.search(remove_bad_chars(string(req.url_params.get("value"))));
}); });
CROW_ROUTE(app, "/mempool") CROW_ROUTE(app, "/mempool")
@ -496,7 +505,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/api/transaction/<string>") CROW_ROUTE(app, "/api/transaction/<string>")
([&](const crow::request &req, string tx_hash) { ([&](const crow::request &req, string tx_hash) {
myxmr::jsonresponse r{xmrblocks.json_transaction(tx_hash)}; myxmr::jsonresponse r{xmrblocks.json_transaction(remove_bad_chars(tx_hash))};
return r; return r;
}); });
@ -504,7 +513,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/api/rawtransaction/<string>") CROW_ROUTE(app, "/api/rawtransaction/<string>")
([&](const crow::request &req, string tx_hash) { ([&](const crow::request &req, string tx_hash) {
myxmr::jsonresponse r{xmrblocks.json_rawtransaction(tx_hash)}; myxmr::jsonresponse r{xmrblocks.json_rawtransaction(remove_bad_chars(tx_hash))};
return r; return r;
}); });
@ -512,7 +521,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/api/block/<string>") CROW_ROUTE(app, "/api/block/<string>")
([&](const crow::request &req, string block_no_or_hash) { ([&](const crow::request &req, string block_no_or_hash) {
myxmr::jsonresponse r{xmrblocks.json_block(block_no_or_hash)}; myxmr::jsonresponse r{xmrblocks.json_block(remove_bad_chars(block_no_or_hash))};
return r; return r;
}); });
@ -520,7 +529,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/api/rawblock/<string>") CROW_ROUTE(app, "/api/rawblock/<string>")
([&](const crow::request &req, string block_no_or_hash) { ([&](const crow::request &req, string block_no_or_hash) {
myxmr::jsonresponse r{xmrblocks.json_rawblock(block_no_or_hash)}; myxmr::jsonresponse r{xmrblocks.json_rawblock(remove_bad_chars(block_no_or_hash))};
return r; return r;
}); });
@ -534,7 +543,8 @@ main(int ac, const char* av[])
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ? string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "25"; req.url_params.get("limit") : "25";
myxmr::jsonresponse r{xmrblocks.json_transactions(page, limit)}; myxmr::jsonresponse r{xmrblocks.json_transactions(
remove_bad_chars(page), remove_bad_chars(limit))};
return r; return r;
}); });
@ -551,7 +561,8 @@ main(int ac, const char* av[])
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ? string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "100000000"; req.url_params.get("limit") : "100000000";
myxmr::jsonresponse r{xmrblocks.json_mempool(page, limit)}; myxmr::jsonresponse r{xmrblocks.json_mempool(
remove_bad_chars(page), remove_bad_chars(limit))};
return r; return r;
}); });
@ -559,7 +570,7 @@ main(int ac, const char* av[])
CROW_ROUTE(app, "/api/search/<string>") CROW_ROUTE(app, "/api/search/<string>")
([&](const crow::request &req, string search_value) { ([&](const crow::request &req, string search_value) {
myxmr::jsonresponse r{xmrblocks.json_search(search_value)}; myxmr::jsonresponse r{xmrblocks.json_search(remove_bad_chars(search_value))};
return r; return r;
}); });
@ -605,7 +616,11 @@ main(int ac, const char* av[])
cerr << "Cant parse tx_prove as bool. Using default value" << endl; cerr << "Cant parse tx_prove as bool. Using default value" << endl;
} }
myxmr::jsonresponse r{xmrblocks.json_outputs(tx_hash, address, viewkey, tx_prove)}; myxmr::jsonresponse r{xmrblocks.json_outputs(
remove_bad_chars(tx_hash),
remove_bad_chars(address),
remove_bad_chars(viewkey),
tx_prove)};
return r; return r;
}); });
@ -635,7 +650,11 @@ main(int ac, const char* av[])
cerr << "Cant parse tx_prove as bool. Using default value" << endl; cerr << "Cant parse tx_prove as bool. Using default value" << endl;
} }
myxmr::jsonresponse r{xmrblocks.json_outputsblocks(limit, address, viewkey, in_mempool_aswell)}; myxmr::jsonresponse r{xmrblocks.json_outputsblocks(
remove_bad_chars(limit),
remove_bad_chars(address),
remove_bad_chars(viewkey),
in_mempool_aswell)};
return r; return r;
}); });

@ -5458,7 +5458,7 @@ namespace xmreg
{"has_payment_id8" , txd.payment_id8 != null_hash8}, {"has_payment_id8" , txd.payment_id8 != null_hash8},
{"confirmations" , txd.no_confirmations}, {"confirmations" , txd.no_confirmations},
{"payment_id" , pid_str}, {"payment_id" , pid_str},
{"payment_id_as_ascii" , std::regex_replace(txd.payment_id_as_ascii, e, " ")}, {"payment_id_as_ascii" , remove_bad_chars(txd.payment_id_as_ascii)},
{"payment_id8" , pid8_str}, {"payment_id8" , pid8_str},
{"extra" , txd.get_extra_str()}, {"extra" , txd.get_extra_str()},
{"with_ring_signatures" , static_cast<bool>( {"with_ring_signatures" , static_cast<bool>(

@ -41,16 +41,16 @@
namespace xmreg namespace xmreg
{ {
using namespace cryptonote; using namespace cryptonote;
using namespace crypto; using namespace crypto;
using namespace std; using namespace std;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using json = nlohmann::json; using json = nlohmann::json;
struct outputs_visitor struct outputs_visitor
{ {
std::vector<crypto::public_key >& m_output_keys; std::vector<crypto::public_key >& m_output_keys;
const Blockchain& m_bch; const Blockchain& m_bch;
@ -65,17 +65,17 @@ namespace xmreg
m_output_keys.push_back(pubkey); m_output_keys.push_back(pubkey);
return true; return true;
} }
}; };
template <typename T> template <typename T>
bool bool
parse_str_secret_key(const string& key_str, T& secret_key); parse_str_secret_key(const string& key_str, T& secret_key);
template <typename T> template <typename T>
bool bool
parse_str_secret_key(const string& key_str, std::vector<T>& secret_keys) parse_str_secret_key(const string& key_str, std::vector<T>& secret_keys)
{ {
const size_t num_keys = key_str.size() / 64; const size_t num_keys = key_str.size() / 64;
if (num_keys * 64 != key_str.size()) if (num_keys * 64 != key_str.size())
return false; return false;
@ -86,193 +86,193 @@ namespace xmreg
return false; return false;
} }
return true; return true;
} }
bool bool
get_tx_pub_key_from_str_hash(Blockchain& core_storage, get_tx_pub_key_from_str_hash(Blockchain& core_storage,
const string& hash_str, const string& hash_str,
transaction& tx); transaction& tx);
bool bool
parse_str_address(const string& address_str, parse_str_address(const string& address_str,
address_parse_info& address_info, address_parse_info& address_info,
bool testnet = false); bool testnet = false);
inline bool inline bool
is_separator(char c); is_separator(char c);
string string
print_address(const address_parse_info& address, print_address(const address_parse_info& address,
bool testnet = false); bool testnet = false);
string string
print_sig (const signature& sig); print_sig (const signature& sig);
string string
remove_trailing_path_separator(const string& in_path); remove_trailing_path_separator(const string& in_path);
bf::path bf::path
remove_trailing_path_separator(const bf::path& in_path); remove_trailing_path_separator(const bf::path& in_path);
string string
timestamp_to_str_gm(time_t timestamp, const char* format = "%F %T"); timestamp_to_str_gm(time_t timestamp, const char* format = "%F %T");
ostream& ostream&
operator<< (ostream& os, const address_parse_info& addr_info); operator<< (ostream& os, const address_parse_info& addr_info);
string string
get_default_lmdb_folder(bool testnet = false); get_default_lmdb_folder(bool testnet = false);
bool bool
generate_key_image(const crypto::key_derivation& derivation, generate_key_image(const crypto::key_derivation& derivation,
const std::size_t output_index, const std::size_t output_index,
const crypto::secret_key& sec_key, const crypto::secret_key& sec_key,
const crypto::public_key& pub_key, const crypto::public_key& pub_key,
crypto::key_image& key_img); crypto::key_image& key_img);
bool bool
get_blockchain_path(const boost::optional<string>& bc_path, get_blockchain_path(const boost::optional<string>& bc_path,
bf::path& blockchain_path, bf::path& blockchain_path,
bool testnet = false); bool testnet = false);
uint64_t uint64_t
sum_money_in_outputs(const transaction& tx); sum_money_in_outputs(const transaction& tx);
pair<uint64_t, uint64_t> pair<uint64_t, uint64_t>
sum_money_in_outputs(const string& json_str); sum_money_in_outputs(const string& json_str);
pair<uint64_t, uint64_t> pair<uint64_t, uint64_t>
sum_money_in_outputs(const json& _json); sum_money_in_outputs(const json& _json);
array<uint64_t, 4> array<uint64_t, 4>
summary_of_in_out_rct( summary_of_in_out_rct(
const transaction& tx, const transaction& tx,
vector<pair<txout_to_key, uint64_t>>& output_pub_keys, vector<pair<txout_to_key, uint64_t>>& output_pub_keys,
vector<txin_to_key>& input_key_imgs); vector<txin_to_key>& input_key_imgs);
// this version for mempool txs from json // this version for mempool txs from json
array<uint64_t, 6> array<uint64_t, 6>
summary_of_in_out_rct(const json& _json); summary_of_in_out_rct(const json& _json);
uint64_t uint64_t
sum_money_in_inputs(const transaction& tx); sum_money_in_inputs(const transaction& tx);
pair<uint64_t, uint64_t> pair<uint64_t, uint64_t>
sum_money_in_inputs(const string& json_str); sum_money_in_inputs(const string& json_str);
pair<uint64_t, uint64_t> pair<uint64_t, uint64_t>
sum_money_in_inputs(const json& _json); sum_money_in_inputs(const json& _json);
uint64_t uint64_t
count_nonrct_inputs(const transaction& tx); count_nonrct_inputs(const transaction& tx);
uint64_t uint64_t
count_nonrct_inputs(const string& json_str); count_nonrct_inputs(const string& json_str);
uint64_t uint64_t
count_nonrct_inputs(const json& _json); count_nonrct_inputs(const json& _json);
array<uint64_t, 2> array<uint64_t, 2>
sum_money_in_tx(const transaction& tx); sum_money_in_tx(const transaction& tx);
array<uint64_t, 2> array<uint64_t, 2>
sum_money_in_txs(const vector<transaction>& txs); sum_money_in_txs(const vector<transaction>& txs);
uint64_t uint64_t
sum_fees_in_txs(const vector<transaction>& txs); sum_fees_in_txs(const vector<transaction>& txs);
uint64_t uint64_t
get_mixin_no(const transaction& tx); get_mixin_no(const transaction& tx);
vector<uint64_t> vector<uint64_t>
get_mixin_no(const string& json_str); get_mixin_no(const string& json_str);
vector<uint64_t> vector<uint64_t>
get_mixin_no(const json& _json); get_mixin_no(const json& _json);
vector<uint64_t> vector<uint64_t>
get_mixin_no_in_txs(const vector<transaction>& txs); get_mixin_no_in_txs(const vector<transaction>& txs);
vector<pair<txout_to_key, uint64_t>> vector<pair<txout_to_key, uint64_t>>
get_ouputs(const transaction& tx); get_ouputs(const transaction& tx);
vector<tuple<txout_to_key, uint64_t, uint64_t>> vector<tuple<txout_to_key, uint64_t, uint64_t>>
get_ouputs_tuple(const transaction& tx); get_ouputs_tuple(const transaction& tx);
vector<txin_to_key> vector<txin_to_key>
get_key_images(const transaction& tx); get_key_images(const transaction& tx);
bool bool
get_payment_id(const vector<uint8_t>& extra, get_payment_id(const vector<uint8_t>& extra,
crypto::hash& payment_id, crypto::hash& payment_id,
crypto::hash8& payment_id8); crypto::hash8& payment_id8);
bool bool
get_payment_id(const transaction& tx, get_payment_id(const transaction& tx,
crypto::hash& payment_id, crypto::hash& payment_id,
crypto::hash8& payment_id8); crypto::hash8& payment_id8);
inline double inline double
get_xmr(uint64_t core_amount) get_xmr(uint64_t core_amount)
{ {
return static_cast<double>(core_amount) / 1e12; return static_cast<double>(core_amount) / 1e12;
} }
array<size_t, 5> array<size_t, 5>
timestamp_difference(uint64_t t1, uint64_t t2); timestamp_difference(uint64_t t1, uint64_t t2);
string string
read(string filename); read(string filename);
pair<string, double> pair<string, double>
timestamps_time_scale(const vector<uint64_t>& timestamps, timestamps_time_scale(const vector<uint64_t>& timestamps,
uint64_t timeN, uint64_t resolution = 80, uint64_t timeN, uint64_t resolution = 80,
uint64_t time0 = 1397818193 /* timestamp of the second block */); uint64_t time0 = 1397818193 /* timestamp of the second block */);
bool bool
decode_ringct(const rct::rctSig & rv, decode_ringct(const rct::rctSig & rv,
const crypto::public_key pub, const crypto::public_key pub,
const crypto::secret_key &sec, const crypto::secret_key &sec,
unsigned int i, unsigned int i,
rct::key & mask, rct::key & mask,
uint64_t & amount); uint64_t & amount);
bool bool
decode_ringct(const rct::rctSig & rv, decode_ringct(const rct::rctSig & rv,
const crypto::key_derivation &derivation, const crypto::key_derivation &derivation,
unsigned int i, unsigned int i,
rct::key & mask, rct::key & mask,
uint64_t & amount); uint64_t & amount);
bool bool
url_decode(const std::string& in, std::string& out); url_decode(const std::string& in, std::string& out);
map<std::string, std::string> map<std::string, std::string>
parse_crow_post_data(const string& req_body); parse_crow_post_data(const string& req_body);
// from wallet2::decrypt // from wallet2::decrypt
string string
decrypt(const std::string &ciphertext, decrypt(const std::string &ciphertext,
const crypto::secret_key &skey, const crypto::secret_key &skey,
bool authenticated = true); bool authenticated = true);
// based on // based on
// crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const // crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const
public_key public_key
get_tx_pub_key_from_received_outs(const transaction &tx); get_tx_pub_key_from_received_outs(const transaction &tx);
static static
string string
xmr_amount_to_str(const uint64_t& xmr_amount, xmr_amount_to_str(const uint64_t& xmr_amount,
string _format="{:0.12f}", string _format="{:0.12f}",
bool zero_to_question_mark=true) bool zero_to_question_mark=true)
{ {
string amount_str = "?"; string amount_str = "?";
if (!zero_to_question_mark) if (!zero_to_question_mark)
@ -288,17 +288,17 @@ namespace xmreg
} }
return amount_str; return amount_str;
} }
bool bool
is_output_ours(const size_t& output_index, is_output_ours(const size_t& output_index,
const transaction& tx, const transaction& tx,
const public_key& pub_tx_key, const public_key& pub_tx_key,
const secret_key& private_view_key, const secret_key& private_view_key,
const public_key& public_spend_key); const public_key& public_spend_key);
bool bool
get_real_output_for_key_image(const key_image& ki, get_real_output_for_key_image(const key_image& ki,
const transaction& tx, const transaction& tx,
const secret_key& private_view_key, const secret_key& private_view_key,
const public_key& public_spend_key, const public_key& public_spend_key,
@ -306,12 +306,12 @@ namespace xmreg
public_key output_pub_key); public_key output_pub_key);
// based on http://stackoverflow.com/a/9943098/248823 // based on http://stackoverflow.com/a/9943098/248823
template<typename Iterator, typename Func> template<typename Iterator, typename Func>
void chunks(Iterator begin, void chunks(Iterator begin,
Iterator end, Iterator end,
iterator_traits<string::iterator>::difference_type k, iterator_traits<string::iterator>::difference_type k,
Func f) Func f)
{ {
Iterator chunk_begin; Iterator chunk_begin;
Iterator chunk_end; Iterator chunk_end;
chunk_end = chunk_begin = begin; chunk_end = chunk_begin = begin;
@ -326,33 +326,44 @@ namespace xmreg
chunk_begin = chunk_end; chunk_begin = chunk_end;
} }
while(std::distance(chunk_begin,end) > 0); while(std::distance(chunk_begin,end) > 0);
} }
bool /*
make_tx_from_json(const string& json_str, transaction& tx); * Remove all characters in in_str that match the given
* regular expression
*/
inline string
remove_bad_chars(string const& in_str, std::regex const& rgx = std::regex {"[^a-zA-Z0-9]"})
{
return std::regex_replace(in_str, rgx, "");
}
string
make_printable(const string& in_s);
string bool
get_human_readable_timestamp(uint64_t ts); make_tx_from_json(const string& json_str, transaction& tx);
string
make_printable(const string& in_s);
string
get_human_readable_timestamp(uint64_t ts);
// Get the median of an unordered set of numbers of arbitrary // Get the median of an unordered set of numbers of arbitrary
// type without modifying the underlying dataset. // type without modifying the underlying dataset.
// taken from http://stackoverflow.com/a/19695285 // taken from http://stackoverflow.com/a/19695285
template <typename It> template <typename It>
typename std::iterator_traits<It>::value_type typename std::iterator_traits<It>::value_type
calc_median(It it_begin, It it_end) calc_median(It it_begin, It it_end)
{ {
using T = typename std::iterator_traits<It>::value_type; using T = typename std::iterator_traits<It>::value_type;
std::vector<T> data(it_begin, it_end); std::vector<T> data(it_begin, it_end);
std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end()); std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end());
return data[data.size() / 2]; return data[data.size() / 2];
} }
void void
pause_execution(uint64_t no_seconds, const string& text = "now"); pause_execution(uint64_t no_seconds, const string& text = "now");
} }

Loading…
Cancel
Save