Skip to content

Commit

Permalink
Donate Miner Function Courtesy of AngryWasp
Browse files Browse the repository at this point in the history
  • Loading branch information
hooftly committed May 18, 2019
1 parent 95b956d commit 73fa39a
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 14 deletions.
76 changes: 72 additions & 4 deletions src/cryptonote_basic/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ namespace cryptonote
{
const command_line::arg_descriptor<std::string> arg_extra_messages = {"extra-messages-file", "Specify file for extra messages to include into coinbase transactions", "", true};
const command_line::arg_descriptor<std::string> arg_start_mining = {"start-mining", "Specify wallet address to mining for", "", true};
const command_line::arg_descriptor<uint32_t> arg_mining_threads = {"mining-threads", "Specify mining threads count", 0, true};
const command_line::arg_descriptor<uint32_t> arg_donate_mining = {"donate-level", "Specify a percentage of blocks to mine to the development wallet", miner::MINING_DEFAULT_DONATION_LEVEL, true};
const command_line::arg_descriptor<uint32_t> arg_mining_threads = {"mining-threads", "Specify mining threads count", 0, true};
const command_line::arg_descriptor<bool> arg_bg_mining_enable = {"bg-mining-enable", "enable/disable background mining", true, true};
const command_line::arg_descriptor<bool> arg_bg_mining_ignore_battery = {"bg-mining-ignore-battery", "if true, assumes plugged in when unable to query system power status", false, true};
const command_line::arg_descriptor<uint64_t> arg_bg_mining_min_idle_interval_seconds = {"bg-mining-min-idle-interval", "Specify min lookback interval in seconds for determining idle state", miner::BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS, true};
const command_line::arg_descriptor<uint16_t> arg_bg_mining_idle_threshold_percentage = {"bg-mining-idle-threshold", "Specify minimum avg idle percentage over lookback interval", miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE, true};
const command_line::arg_descriptor<uint16_t> arg_bg_mining_miner_target_percentage = {"bg-mining-miner-target", "Specify maximum percentage cpu use by miner(s)", miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE, true};
const command_line::arg_descriptor<uint16_t> arg_bg_mining_idle_threshold_percentage = {"bg-mining-idle-threshold", "Specify minimum avg idle percentage over lookback interval", miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE, true};
const command_line::arg_descriptor<uint16_t> arg_bg_mining_miner_target_percentage = {"bg-mining-miner-target", "Specify maximum percentage cpu use by miner(s)", miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE, true};
}


Expand All @@ -108,6 +109,9 @@ namespace cryptonote
m_height(0),
m_pausers_count(0),
m_threads_total(0),
m_donate_blocks(MINING_DEFAULT_DONATION_LEVEL),
m_block_counter(0),
m_dev_mine_time(false),
m_starter_nonce(0),
m_last_hr_merge_time(0),
m_hashes(0),
Expand All @@ -131,6 +135,8 @@ namespace cryptonote
catch (...) { /* ignore */ }
}
//-----------------------------------------------------------------------------------------------------
bool m_last_dev_mine_time = false;
uint64_t last_height = 0;
bool miner::set_block_template(const block& bl, const difficulty_type& di, uint64_t height, uint64_t block_reward)
{
CRITICAL_REGION_LOCAL(m_template_lock);
Expand All @@ -140,6 +146,38 @@ namespace cryptonote
m_block_reward = block_reward;
++m_template_no;
m_starter_nonce = crypto::rand<uint32_t>();

if (height != last_height)
{
m_block_counter++;

//reset counter
if (m_block_counter > 100)
m_block_counter = 0;

//if counter is within dev mining window
m_dev_mine_time = (m_block_counter >= (100 - m_donate_blocks));

#if defined(ONE_TIME_NOTIFY)
if (m_dev_mine_time != m_last_dev_mine_time)
{
uint32_t remaining = 100 - m_block_counter;
if (m_dev_mine_time)
MGUSER_YELLOW("Mining to the dev fund for the next " << remaining << " blocks");
else
MGUSER_YELLOW("Resumed mining to your regular mining address");

m_last_dev_mine_time = m_dev_mine_time;
}
#else
uint32_t remaining = 100 - m_block_counter;
if (m_dev_mine_time)
MGUSER_YELLOW("Mining to the dev fund for the next " << remaining << " blocks");
#endif

last_height = height;
}

return true;
}
//-----------------------------------------------------------------------------------------------------
Expand All @@ -164,7 +202,9 @@ namespace cryptonote
extra_nonce = m_extra_messages[m_config.current_extra_message_index];
}

if(!m_phandler->get_block_template(bl, m_mine_address, di, height, expected_reward, extra_nonce))
account_public_address adr = m_dev_mine_time ? m_donate_mine_address : m_mine_address;

if(!m_phandler->get_block_template(bl, adr, di, height, expected_reward, extra_nonce))
{
LOG_ERROR("Failed to get_block_template(), stopping mining");
return false;
Expand Down Expand Up @@ -278,6 +318,7 @@ namespace cryptonote
{
command_line::add_arg(desc, arg_extra_messages);
command_line::add_arg(desc, arg_start_mining);
command_line::add_arg(desc, arg_donate_mining);
command_line::add_arg(desc, arg_mining_threads);
command_line::add_arg(desc, arg_bg_mining_enable);
command_line::add_arg(desc, arg_bg_mining_ignore_battery);
Expand Down Expand Up @@ -321,6 +362,12 @@ namespace cryptonote
return false;
}
m_mine_address = info.address;

if(!cryptonote::get_account_address_from_str(info, nettype, DONATION_ADDR))
{
return false;
}
m_donate_mine_address = info.address;
m_threads_total = 0;
m_do_mining = true;
if(command_line::has_arg(vm, arg_mining_threads))
Expand All @@ -329,6 +376,11 @@ namespace cryptonote
}
}

if(command_line::has_arg(vm, arg_donate_mining))
{
m_donate_blocks = command_line::get_arg(vm, arg_donate_mining);
}

// Background mining parameters
// Let init set all parameters even if background mining is not enabled, they can start later with params set
if(command_line::has_arg(vm, arg_bg_mining_enable))
Expand Down Expand Up @@ -413,6 +465,8 @@ namespace cryptonote
MINFO("Ignoring battery");
}

set_donate_blocks(m_donate_blocks);

return true;
}
//-----------------------------------------------------------------------------------------------------
Expand All @@ -431,6 +485,20 @@ namespace cryptonote
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
}
//-----------------------------------------------------------------------------------------------------
void miner::set_donate_blocks(uint32_t b)
{
if (b > 100)
b = 100;

m_donate_blocks = b;

MGUSER_YELLOW("Mining set to donate " << m_donate_blocks << "% to the dev fund");
if (m_donate_blocks > 0)
MGUSER_YELLOW("Thankyou for your support.");

m_block_counter = 0;
}
//-----------------------------------------------------------------------------------------------------
bool miner::stop()
{
MTRACE("Miner has received stop signal");
Expand Down
8 changes: 8 additions & 0 deletions src/cryptonote_basic/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ namespace cryptonote
uint8_t get_mining_target() const;
bool set_mining_target(uint8_t mining_target);
uint64_t get_block_reward() const { return m_block_reward; }
void set_donate_blocks(uint32_t b);
uint32_t get_donate_blocks() const { return m_donate_blocks; }

static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE = 90;
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE = 50;
Expand All @@ -101,6 +103,8 @@ namespace cryptonote
static constexpr uint64_t BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS = 400; // ramp up
static constexpr uint64_t BACKGROUND_MINING_MIN_MINER_EXTRA_SLEEP_MILLIS = 5;

static constexpr uint32_t MINING_DEFAULT_DONATION_LEVEL = 0;

private:
bool worker_thread();
bool request_block_template();
Expand All @@ -126,13 +130,17 @@ namespace cryptonote
uint64_t m_height;
volatile uint32_t m_thread_index;
volatile uint32_t m_threads_total;
uint32_t m_donate_blocks;
uint32_t m_block_counter;
bool m_dev_mine_time;
std::atomic<int32_t> m_pausers_count;
epee::critical_section m_miners_count_lock;

std::list<boost::thread> m_threads;
epee::critical_section m_threads_lock;
i_miner_handler* m_phandler;
account_public_address m_mine_address;
account_public_address m_donate_mine_address;
epee::math_helper::once_a_time_seconds<5> m_update_block_template_interval;
epee::math_helper::once_a_time_seconds<2> m_update_merge_hr_interval;
epee::math_helper::once_a_time_seconds<1> m_autodetect_interval;
Expand Down
2 changes: 2 additions & 0 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
#define HASH_OF_HASHES_STEP 256
#define DEFAULT_TXPOOL_MAX_SIZE 648000000ull // 3 days at 300000, in bytes

#define DONATION_ADDR "amitGvbEpdH6vcmWR3qXd9XXyud78DQfM84D2P9NMpJEV9iZFTTrmP5f6kUwWVmfNBXyMs4AufNUoUrGsqwfUn1L695cS6EkwC"

struct hard_fork
{
uint8_t version;
Expand Down
18 changes: 18 additions & 0 deletions src/daemon/command_parser_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,24 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg
return true;
}

bool t_command_parser_executor::donate_mining(const std::vector<std::string>& args)
{
if (args.empty())
{
std::cout << "expected: donate_level <blocks>" << std::endl;
return true;
}

uint32_t blocks_count = 0;
bool ok = epee::string_tools::get_xtype_from_string(blocks_count, args[0]);
blocks_count = ok ? blocks_count : 0;

if (blocks_count > 100)
blocks_count = 100;

return m_executor.donate_mining(blocks_count);
}

bool t_command_parser_executor::stop_mining(const std::vector<std::string>& args)
{
if (!args.empty()) return false;
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/command_parser_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class t_command_parser_executor final

bool start_mining(const std::vector<std::string>& args);

bool donate_mining(const std::vector<std::string>& args);

bool stop_mining(const std::vector<std::string>& args);

bool mining_status(const std::vector<std::string>& args);
Expand Down
6 changes: 6 additions & 0 deletions src/daemon/command_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ t_command_server::t_command_server(
, "start_mining <addr> [<threads>|auto] [do_background_mining] [ignore_battery]"
, "Start mining for specified address. Defaults to 1 thread and no background mining. Use \"auto\" to autodetect optimal number of threads."
);
m_command_lookup.set_handler(
"donate_level"
, std::bind(&t_command_parser_executor::donate_mining, &m_parser, p::_1)
, "donate_level <blocks>"
, "Amount of time to spend mining to the donation fund."
);
m_command_lookup.set_handler(
"stop_mining"
, std::bind(&t_command_parser_executor::stop_mining, &m_parser, p::_1)
Expand Down
29 changes: 28 additions & 1 deletion src/daemon/rpc_command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,33 @@ bool t_rpc_command_executor::start_mining(cryptonote::account_public_address add
return true;
}

bool t_rpc_command_executor::donate_mining(uint32_t blocks_count)
{
cryptonote::COMMAND_RPC_DONATE_MINING::request req;
cryptonote::COMMAND_RPC_DONATE_MINING::response res;
req.blocks = blocks_count;

std::string fail_message = "Donation level unchanged";

if (m_is_rpc)
{
if (m_rpc_client->rpc_request(req, res, "/set_donate_level", fail_message.c_str()))
{
tools::success_msg_writer() << "Mining started";
}
}
else
{
if (!m_rpc_server->on_set_donation_level(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << make_error(fail_message, res.status);
return true;
}
}

return true;
}

bool t_rpc_command_executor::stop_mining() {
cryptonote::COMMAND_RPC_STOP_MINING::request req;
cryptonote::COMMAND_RPC_STOP_MINING::response res;
Expand Down Expand Up @@ -2032,7 +2059,7 @@ bool t_rpc_command_executor::update()
return true;
}

tools::msg_writer() << "Update available: v" << res.version << ENDL << res.uri << ENDL << "SHA256: " << res.hash;
tools::msg_writer() << "Update available- v" << res.version << ":" << res.codename << ENDL << res.uri << ENDL << "Release note: " << res.release_note;
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/daemon/rpc_command_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class t_rpc_command_executor final {

bool start_mining(cryptonote::account_public_address address, uint64_t num_threads, cryptonote::network_type nettype, bool do_background_mining = false, bool ignore_battery = false);

bool donate_mining(uint32_t blocks_count);

bool stop_mining();

bool mining_status();
Expand Down
9 changes: 9 additions & 0 deletions src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,15 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_set_donation_level(const COMMAND_RPC_DONATE_MINING::request& req, COMMAND_RPC_DONATE_MINING::response& res, const connection_context *ctx)
{
PERF_TIMER(on_set_donation_level);
cryptonote::miner &miner= m_core.get_miner();
miner.set_donate_blocks(req.blocks);
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res, const connection_context *ctx)
{
PERF_TIMER(on_stop_mining);
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/core_rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ namespace cryptonote
MAP_URI_AUTO_JON2("/send_raw_transaction", on_send_raw_tx, COMMAND_RPC_SEND_RAW_TX)
MAP_URI_AUTO_JON2("/sendrawtransaction", on_send_raw_tx, COMMAND_RPC_SEND_RAW_TX)
MAP_URI_AUTO_JON2_IF("/start_mining", on_start_mining, COMMAND_RPC_START_MINING, !m_restricted)
MAP_URI_AUTO_JON2_IF("/set_donate_level", on_set_donation_level, COMMAND_RPC_DONATE_MINING, !m_restricted)
MAP_URI_AUTO_JON2_IF("/stop_mining", on_stop_mining, COMMAND_RPC_STOP_MINING, !m_restricted)
MAP_URI_AUTO_JON2_IF("/mining_status", on_mining_status, COMMAND_RPC_MINING_STATUS, !m_restricted)
MAP_URI_AUTO_JON2_IF("/save_bc", on_save_bc, COMMAND_RPC_SAVE_BC, !m_restricted)
Expand Down Expand Up @@ -181,6 +182,7 @@ namespace cryptonote
bool on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res, const connection_context *ctx = NULL);
bool on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res, const connection_context *ctx = NULL);
bool on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res, const connection_context *ctx = NULL);
bool on_set_donation_level(const COMMAND_RPC_DONATE_MINING::request& req, COMMAND_RPC_DONATE_MINING::response& res, const connection_context *ctx = NULL);
bool on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res, const connection_context *ctx = NULL);
bool on_get_outs_bin(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res, const connection_context *ctx = NULL);
bool on_get_outs(const COMMAND_RPC_GET_OUTPUTS::request& req, COMMAND_RPC_GET_OUTPUTS::response& res, const connection_context *ctx = NULL);
Expand Down
29 changes: 26 additions & 3 deletions src/rpc/core_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,29 @@ namespace cryptonote
typedef epee::misc_utils::struct_init<response_t> response;
};
//-----------------------------------------------
struct COMMAND_RPC_DONATE_MINING
{
struct request_t
{
uint32_t blocks;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(blocks)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;

struct response_t
{
std::string status;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(status)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
};
//-----------------------------------------------
struct COMMAND_RPC_GET_INFO
{
struct request_t
Expand Down Expand Up @@ -2030,16 +2053,16 @@ namespace cryptonote
bool update;
std::string version;
std::string codename;
std::string release_note;
std::string uri;
std::string hash;
std::string release_note;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(status)
KV_SERIALIZE(update)
KV_SERIALIZE(version)
KV_SERIALIZE(codename)
KV_SERIALIZE(uri)
KV_SERIALIZE(hash)
KV_SERIALIZE(release_note)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
Expand Down
Loading

0 comments on commit 73fa39a

Please sign in to comment.