Merge pull request #5374

a2561653 wallet: new option to start background mining (moneromooo-monero)
This commit is contained in:
Riccardo Spagni
2019-04-11 13:01:30 +02:00
10 changed files with 264 additions and 20 deletions
+7
View File
@@ -1046,6 +1046,7 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
m_segregation_height(0),
m_ignore_fractional_outputs(true),
m_track_uses(false),
m_setup_background_mining(BackgroundMiningMaybe),
m_is_initialized(false),
m_kdf_rounds(kdf_rounds),
is_old_file_format(false),
@@ -3552,6 +3553,9 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
value2.SetInt(m_track_uses ? 1 : 0);
json.AddMember("track_uses", value2, json.GetAllocator());
value2.SetInt(m_setup_background_mining);
json.AddMember("setup_background_mining", value2, json.GetAllocator());
value2.SetUint(m_subaddress_lookahead_major);
json.AddMember("subaddress_lookahead_major", value2, json.GetAllocator());
@@ -3703,6 +3707,7 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
m_segregation_height = 0;
m_ignore_fractional_outputs = true;
m_track_uses = false;
m_setup_background_mining = BackgroundMiningMaybe;
m_subaddress_lookahead_major = SUBADDRESS_LOOKAHEAD_MAJOR;
m_subaddress_lookahead_minor = SUBADDRESS_LOOKAHEAD_MINOR;
m_original_keys_available = false;
@@ -3857,6 +3862,8 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
m_ignore_fractional_outputs = field_ignore_fractional_outputs;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, track_uses, int, Int, false, false);
m_track_uses = field_track_uses;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, setup_background_mining, BackgroundMiningSetupType, Int, false, BackgroundMiningMaybe);
m_setup_background_mining = field_setup_background_mining;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, subaddress_lookahead_major, uint32_t, Uint, false, SUBADDRESS_LOOKAHEAD_MAJOR);
m_subaddress_lookahead_major = field_subaddress_lookahead_major;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, subaddress_lookahead_minor, uint32_t, Uint, false, SUBADDRESS_LOOKAHEAD_MINOR);
+9
View File
@@ -194,6 +194,12 @@ namespace tools
AskPasswordToDecrypt = 2,
};
enum BackgroundMiningSetupType {
BackgroundMiningMaybe = 0,
BackgroundMiningYes = 1,
BackgroundMiningNo = 2,
};
static const char* tr(const char* str);
static bool has_testnet_option(const boost::program_options::variables_map& vm);
@@ -1010,6 +1016,8 @@ namespace tools
void confirm_non_default_ring_size(bool always) { m_confirm_non_default_ring_size = always; }
bool track_uses() const { return m_track_uses; }
void track_uses(bool value) { m_track_uses = value; }
BackgroundMiningSetupType setup_background_mining() const { return m_setup_background_mining; }
void setup_background_mining(BackgroundMiningSetupType value) { m_setup_background_mining = value; }
const std::string & device_name() const { return m_device_name; }
void device_name(const std::string & device_name) { m_device_name = device_name; }
const std::string & device_derivation_path() const { return m_device_derivation_path; }
@@ -1451,6 +1459,7 @@ namespace tools
uint64_t m_segregation_height;
bool m_ignore_fractional_outputs;
bool m_track_uses;
BackgroundMiningSetupType m_setup_background_mining;
bool m_is_initialized;
NodeRPCProxy m_node_rpc_proxy;
std::unordered_set<crypto::hash> m_scanned_pool_txs[2];
+56
View File
@@ -279,6 +279,8 @@ namespace tools
m_auto_refresh_period = DEFAULT_AUTO_REFRESH_PERIOD;
m_last_auto_refresh_time = boost::posix_time::min_date_time;
check_background_mining();
m_net_server.set_threads_prefix("RPC");
auto rng = [](size_t len, uint8_t *ptr) { return crypto::rand(len, ptr); };
return epee::http_server_impl_base<wallet_rpc_server, connection_context>::init(
@@ -287,6 +289,60 @@ namespace tools
);
}
//------------------------------------------------------------------------------------------------------------------------------
void wallet_rpc_server::check_background_mining()
{
if (!m_wallet)
return;
tools::wallet2::BackgroundMiningSetupType setup = m_wallet->setup_background_mining();
if (setup == tools::wallet2::BackgroundMiningNo)
{
MLOG_RED(el::Level::Warning, "Background mining not enabled. Run \"set setup-background-mining 1\" in monero-wallet-cli to change.");
return;
}
if (!m_wallet->is_trusted_daemon())
{
MDEBUG("Using an untrusted daemon, skipping background mining check");
return;
}
cryptonote::COMMAND_RPC_MINING_STATUS::request req;
cryptonote::COMMAND_RPC_MINING_STATUS::response res;
bool r = m_wallet->invoke_http_json("/mining_status", req, res);
if (!r || res.status != CORE_RPC_STATUS_OK)
{
MERROR("Failed to query mining status: " << (r ? res.status : "No connection to daemon"));
return;
}
if (res.active || res.is_background_mining_enabled)
return;
if (setup == tools::wallet2::BackgroundMiningMaybe)
{
MINFO("The daemon is not set up to background mine.");
MINFO("With background mining enabled, the daemon will mine when idle and not on batttery.");
MINFO("Enabling this supports the network you are using, and makes you eligible for receiving new monero");
MINFO("Set setup-background-mining to 1 in monero-wallet-cli to change.");
return;
}
cryptonote::COMMAND_RPC_START_MINING::request req2;
cryptonote::COMMAND_RPC_START_MINING::response res2;
req2.miner_address = m_wallet->get_account().get_public_address_str(m_wallet->nettype());
req2.threads_count = 1;
req2.do_background_mining = true;
req2.ignore_battery = false;
r = m_wallet->invoke_http_json("/start_mining", req2, res);
if (!r || res2.status != CORE_RPC_STATUS_OK)
{
MERROR("Failed to setup background mining: " << (r ? res.status : "No connection to daemon"));
return;
}
MINFO("Background mining enabled. The daemon will mine when idle and not on batttery.");
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::not_open(epee::json_rpc::error& er)
{
er.code = WALLET_RPC_ERROR_CODE_NOT_OPEN;
+2
View File
@@ -254,6 +254,8 @@ namespace tools
bool validate_transfer(const std::list<wallet_rpc::transfer_destination>& destinations, const std::string& payment_id, std::vector<cryptonote::tx_destination_entry>& dsts, std::vector<uint8_t>& extra, bool at_least_one_destination, epee::json_rpc::error& er);
void check_background_mining();
wallet2 *m_wallet;
std::string m_wallet_dir;
tools::private_file rpc_login_file;