Merge pull request #7358

f346e3e wallet_rpc_payments: implement multithreading (gdmojo)
This commit is contained in:
luigi1111 2021-03-29 21:10:48 -04:00
commit ac091fb539
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
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");
@ -2361,6 +2361,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;
@ -9327,7 +9345,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

@ -464,6 +464,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

@ -1428,7 +1428,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,49 +140,65 @@ 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);
const uint8_t major_version = hashing_blob[0]; std::vector<crypto::hash> hash(n_threads);
if (major_version >= RX_BLOCK_VERSION) 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++)
{ {
const int miners = 1; tpool.submit(&waiter, [&, i] {
crypto::rx_slow_hash(height, seed_height, seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash.data, miners, 0); *(uint32_t*)(hashing_blob.data() + 39) = SWAP32LE(local_nonce-i);
} const uint8_t major_version = hashing_blob[0];
else if (major_version >= RX_BLOCK_VERSION)
{
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);
}
++n_hashes;
if (cryptonote::check_hash(hash, diff))
{
uint64_t credits, balance;
try
{
make_rpc_payment(local_nonce, cookie, credits, balance);
if (credits != credits_per_hash_found)
{ {
MERROR("Found nonce, but daemon did not credit us with the expected amount"); const int miners = 1;
if (errorfunc) crypto::rx_slow_hash(height, seed_height, seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash[i].data, miners, 0);
errorfunc("Found nonce, but daemon did not credit us with the expected amount");
return false;
} }
MDEBUG("Found nonce " << local_nonce << " at diff " << diff << ", gets us " << credits_per_hash_found << ", now " << balance << " credits"); else
if (!foundfunc(credits)) {
break; int cn_variant = hashing_blob[0] >= 7 ? hashing_blob[0] - 6 : 0;
} crypto::cn_slow_hash(hashing_blob.data(), hashing_blob.size(), hash[i], cn_variant, height);
catch (const tools::error::wallet_coded_rpc_error &e) }
});
}
waiter.wait();
n_hashes += n_threads;
for(size_t i=0; i < n_threads; i++)
{
if (cryptonote::check_hash(hash[i], diff))
{ {
MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon"); uint64_t credits, balance;
if (errorfunc) try
errorfunc("Found nonce, but daemon errored out with error " + std::to_string(e.code()) + ": " + e.status() + ", continuing"); {
} make_rpc_payment(local_nonce-i, cookie, credits, balance);
catch (const std::exception &e) if (credits != credits_per_hash_found)
{ {
MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon"); MERROR("Found nonce, but daemon did not credit us with the expected amount");
if (errorfunc) if (errorfunc)
errorfunc("Found nonce, but daemon errored out with: '" + std::string(e.what()) + "', continuing"); errorfunc("Found nonce, but daemon did not credit us with the expected amount");
return false;
}
MDEBUG("Found nonce " << local_nonce-i << " at diff " << diff << ", gets us " << credits_per_hash_found << ", now " << balance << " credits");
if (!foundfunc(credits))
break;
}
catch (const tools::error::wallet_coded_rpc_error &e)
{
MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon");
if (errorfunc)
errorfunc("Found nonce, but daemon errored out with error " + std::to_string(e.code()) + ": " + e.status() + ", continuing");
}
catch (const std::exception &e)
{
MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon");
if (errorfunc)
errorfunc("Found nonce, but daemon errored out with: '" + std::string(e.what()) + "', continuing");
}
} }
} }
} }