wallet_rpc_payments: implement multithreading

This commit is contained in:
gdmojo 2021-01-31 11:48:05 +00:00
parent cb70ae9450
commit f346e3ec0e
4 changed files with 79 additions and 43 deletions

View File

@ -276,7 +276,7 @@ namespace
const char* USAGE_PUBLIC_NODES("public_nodes"); const char* USAGE_PUBLIC_NODES("public_nodes");
const char* USAGE_WELCOME("welcome"); const char* USAGE_WELCOME("welcome");
const char* USAGE_RPC_PAYMENT_INFO("rpc_payment_info"); const char* USAGE_RPC_PAYMENT_INFO("rpc_payment_info");
const char* USAGE_START_MINING_FOR_RPC("start_mining_for_rpc"); const char* USAGE_START_MINING_FOR_RPC("start_mining_for_rpc [<number_of_threads>]");
const char* USAGE_STOP_MINING_FOR_RPC("stop_mining_for_rpc"); const char* USAGE_STOP_MINING_FOR_RPC("stop_mining_for_rpc");
const char* USAGE_SHOW_QR_CODE("show_qr_code [<subaddress_index>]"); const char* USAGE_SHOW_QR_CODE("show_qr_code [<subaddress_index>]");
const char* USAGE_VERSION("version"); const char* USAGE_VERSION("version");
@ -2360,6 +2360,24 @@ bool simple_wallet::start_mining_for_rpc(const std::vector<std::string> &args)
if (!try_connect_to_daemon()) if (!try_connect_to_daemon())
return true; return true;
bool ok = true;
if(args.size() >= 1)
{
uint16_t num = 0;
ok = string_tools::get_xtype_from_string(num, args[0]);
m_rpc_payment_threads = num;
}
else
{
m_rpc_payment_threads = 0;
}
if (!ok)
{
PRINT_USAGE(USAGE_START_MINING_FOR_RPC);
return true;
}
LOCK_IDLE_SCOPE(); LOCK_IDLE_SCOPE();
bool payment_required; bool payment_required;
@ -9272,7 +9290,7 @@ bool simple_wallet::check_rpc_payment()
fail_msg_writer() << tr("Error mining to daemon: ") << error; fail_msg_writer() << tr("Error mining to daemon: ") << error;
m_cmd_binder.print_prompt(); m_cmd_binder.print_prompt();
}; };
bool ret = m_wallet->search_for_rpc_payment(target, startfunc, contfunc, foundfunc, errorfunc); bool ret = m_wallet->search_for_rpc_payment(target, m_rpc_payment_threads, startfunc, contfunc, foundfunc, errorfunc);
if (!ret) if (!ret)
{ {
fail_msg_writer() << tr("Failed to start mining for RPC payment"); fail_msg_writer() << tr("Failed to start mining for RPC payment");

View File

@ -463,6 +463,7 @@ namespace cryptonote
std::atomic<bool> m_need_payment; std::atomic<bool> m_need_payment;
boost::posix_time::ptime m_last_rpc_payment_mining_time; boost::posix_time::ptime m_last_rpc_payment_mining_time;
bool m_rpc_payment_mining_requested; bool m_rpc_payment_mining_requested;
uint32_t m_rpc_payment_threads = 0;
bool m_daemon_rpc_payment_message_displayed; bool m_daemon_rpc_payment_message_displayed;
float m_rpc_payment_hash_rate; float m_rpc_payment_hash_rate;
std::atomic<bool> m_suspend_rpc_payment_mining; std::atomic<bool> m_suspend_rpc_payment_mining;

View File

@ -1426,7 +1426,7 @@ private:
bool get_rpc_payment_info(bool mining, bool &payment_required, uint64_t &credits, uint64_t &diff, uint64_t &credits_per_hash_found, cryptonote::blobdata &hashing_blob, uint64_t &height, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, uint32_t &cookie); bool get_rpc_payment_info(bool mining, bool &payment_required, uint64_t &credits, uint64_t &diff, uint64_t &credits_per_hash_found, cryptonote::blobdata &hashing_blob, uint64_t &height, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, uint32_t &cookie);
bool daemon_requires_payment(); bool daemon_requires_payment();
bool make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credits, uint64_t &balance); bool make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credits, uint64_t &balance);
bool search_for_rpc_payment(uint64_t credits_target, const std::function<bool(uint64_t, uint64_t)> &startfunc, const std::function<bool(unsigned)> &contfunc, const std::function<bool(uint64_t)> &foundfunc = NULL, const std::function<void(const std::string&)> &errorfunc = NULL); bool search_for_rpc_payment(uint64_t credits_target, uint32_t n_threads, const std::function<bool(uint64_t, uint64_t)> &startfunc, const std::function<bool(unsigned)> &contfunc, const std::function<bool(uint64_t)> &foundfunc = NULL, const std::function<void(const std::string&)> &errorfunc = NULL);
template<typename T> void handle_payment_changes(const T &res, std::true_type) { template<typename T> void handle_payment_changes(const T &res, std::true_type) {
if (res.status == CORE_RPC_STATUS_OK || res.status == CORE_RPC_STATUS_PAYMENT_REQUIRED) if (res.status == CORE_RPC_STATUS_OK || res.status == CORE_RPC_STATUS_PAYMENT_REQUIRED)
m_rpc_payment_state.credits = res.credits; m_rpc_payment_state.credits = res.credits;

View File

@ -42,6 +42,7 @@
#include "cryptonote_basic/blobdatatype.h" #include "cryptonote_basic/blobdatatype.h"
#include "common/i18n.h" #include "common/i18n.h"
#include "common/util.h" #include "common/util.h"
#include "common/threadpool.h"
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2.rpc_payments" #define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2.rpc_payments"
@ -101,7 +102,7 @@ bool wallet2::make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credit
return true; return true;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool wallet2::search_for_rpc_payment(uint64_t credits_target, const std::function<bool(uint64_t, uint64_t)> &startfunc, const std::function<bool(unsigned)> &contfunc, const std::function<bool(uint64_t)> &foundfunc, const std::function<void(const std::string&)> &errorfunc) bool wallet2::search_for_rpc_payment(uint64_t credits_target, uint32_t n_threads, const std::function<bool(uint64_t, uint64_t)> &startfunc, const std::function<bool(unsigned)> &contfunc, const std::function<bool(uint64_t)> &foundfunc, const std::function<void(const std::string&)> &errorfunc)
{ {
bool need_payment = false; bool need_payment = false;
bool payment_required; bool payment_required;
@ -139,27 +140,42 @@ bool wallet2::search_for_rpc_payment(uint64_t credits_target, const std::functio
continue; continue;
} }
crypto::hash hash; if(n_threads == 0)
const uint32_t local_nonce = nonce++; // wrapping's OK n_threads = boost::thread::hardware_concurrency();
*(uint32_t*)(hashing_blob.data() + 39) = SWAP32LE(local_nonce);
std::vector<crypto::hash> hash(n_threads);
tools::threadpool& tpool = tools::threadpool::getInstance();
tools::threadpool::waiter waiter(tpool);
const uint32_t local_nonce = nonce += n_threads; // wrapping's OK
for (size_t i = 0; i < n_threads; i++)
{
tpool.submit(&waiter, [&, i] {
*(uint32_t*)(hashing_blob.data() + 39) = SWAP32LE(local_nonce-i);
const uint8_t major_version = hashing_blob[0]; const uint8_t major_version = hashing_blob[0];
if (major_version >= RX_BLOCK_VERSION) if (major_version >= RX_BLOCK_VERSION)
{ {
const int miners = 1; const int miners = 1;
crypto::rx_slow_hash(height, seed_height, seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash.data, miners, 0); crypto::rx_slow_hash(height, seed_height, seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash[i].data, miners, 0);
} }
else else
{ {
int cn_variant = hashing_blob[0] >= 7 ? hashing_blob[0] - 6 : 0; int cn_variant = hashing_blob[0] >= 7 ? hashing_blob[0] - 6 : 0;
crypto::cn_slow_hash(hashing_blob.data(), hashing_blob.size(), hash, cn_variant, height); crypto::cn_slow_hash(hashing_blob.data(), hashing_blob.size(), hash[i], cn_variant, height);
} }
++n_hashes; });
if (cryptonote::check_hash(hash, diff)) }
waiter.wait();
n_hashes += n_threads;
for(size_t i=0; i < n_threads; i++)
{
if (cryptonote::check_hash(hash[i], diff))
{ {
uint64_t credits, balance; uint64_t credits, balance;
try try
{ {
make_rpc_payment(local_nonce, cookie, credits, balance); make_rpc_payment(local_nonce-i, cookie, credits, balance);
if (credits != credits_per_hash_found) if (credits != credits_per_hash_found)
{ {
MERROR("Found nonce, but daemon did not credit us with the expected amount"); MERROR("Found nonce, but daemon did not credit us with the expected amount");
@ -167,7 +183,7 @@ bool wallet2::search_for_rpc_payment(uint64_t credits_target, const std::functio
errorfunc("Found nonce, but daemon did not credit us with the expected amount"); errorfunc("Found nonce, but daemon did not credit us with the expected amount");
return false; return false;
} }
MDEBUG("Found nonce " << local_nonce << " at diff " << diff << ", gets us " << credits_per_hash_found << ", now " << balance << " credits"); MDEBUG("Found nonce " << local_nonce-i << " at diff " << diff << ", gets us " << credits_per_hash_found << ", now " << balance << " credits");
if (!foundfunc(credits)) if (!foundfunc(credits))
break; break;
} }
@ -185,6 +201,7 @@ bool wallet2::search_for_rpc_payment(uint64_t credits_target, const std::functio
} }
} }
} }
}
return true; return true;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------