aboutsummaryrefslogtreecommitdiff
path: root/src/miner
diff options
context:
space:
mode:
authorAntonio Juarez <antonio.maria.juarez@live.com>2014-03-03 22:07:58 +0000
committerAntonio Juarez <antonio.maria.juarez@live.com>2014-03-03 22:07:58 +0000
commit296ae46ed8f8f6e5f986f978febad302e3df231a (patch)
tree1629164454a239308f33c9e12afb22e7f3cd8eeb /src/miner
parentchanged name (diff)
downloadmonero-296ae46ed8f8f6e5f986f978febad302e3df231a.tar.xz
moved all stuff to github
Diffstat (limited to 'src/miner')
-rw-r--r--src/miner/simpleminer.cpp209
-rw-r--r--src/miner/simpleminer.h33
-rw-r--r--src/miner/simpleminer_protocol_defs.h104
-rw-r--r--src/miner/target_helper.h18
4 files changed, 364 insertions, 0 deletions
diff --git a/src/miner/simpleminer.cpp b/src/miner/simpleminer.cpp
new file mode 100644
index 000000000..a9f0ceab0
--- /dev/null
+++ b/src/miner/simpleminer.cpp
@@ -0,0 +1,209 @@
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+
+#include "common/command_line.h"
+#include "misc_log_ex.h"
+#include "simpleminer.h"
+#include "target_helper.h"
+#include "net/http_server_handlers_map2.h"
+#include "simpleminer_protocol_defs.h"
+#include "storages/http_abstract_invoke.h"
+#include "string_tools.h"
+#include "cryptonote_core/account.h"
+#include "cryptonote_core/cryptonote_format_utils.h"
+
+using namespace epee;
+namespace po = boost::program_options;
+
+int main(int argc, char** argv)
+{
+ string_tools::set_module_name_and_folder(argv[0]);
+
+ //set up logging options
+ log_space::get_set_log_detalisation_level(true, LOG_LEVEL_4);
+ log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
+ log_space::log_singletone::add_logger(LOGGER_FILE,
+ log_space::log_singletone::get_default_log_file().c_str(),
+ log_space::log_singletone::get_default_log_folder().c_str());
+
+ po::options_description desc("Allowed options");
+ command_line::add_arg(desc, command_line::arg_help);
+ mining::simpleminer::init_options(desc);
+
+ //uint32_t t = mining::get_target_for_difficulty(700000);
+
+ po::variables_map vm;
+ bool r = command_line::handle_error_helper(desc, [&]()
+ {
+ po::store(po::parse_command_line(argc, argv, desc), vm);
+ po::notify(vm);
+
+ if (command_line::get_arg(vm, command_line::arg_help))
+ {
+ std::cout << desc << std::endl;
+ return false;
+ }
+
+ return true;
+ });
+ if (!r)
+ return 1;
+
+ mining::simpleminer miner;
+ r = miner.init(vm);
+ r = r && miner.run(); // Never returns...
+
+ return 0;
+}
+
+
+
+namespace mining
+{
+ const command_line::arg_descriptor<std::string, true> arg_pool_addr = {"pool-addr", ""};
+ const command_line::arg_descriptor<std::string, true> arg_login = {"login", ""};
+ const command_line::arg_descriptor<std::string, true> arg_pass = {"pass", ""};
+
+ //-----------------------------------------------------------------------------------------------------
+ void simpleminer::init_options(boost::program_options::options_description& desc)
+ {
+ command_line::add_arg(desc, arg_pool_addr);
+ command_line::add_arg(desc, arg_login);
+ command_line::add_arg(desc, arg_pass);
+ }
+ bool simpleminer::init(const boost::program_options::variables_map& vm)
+ {
+ std::string pool_addr = command_line::get_arg(vm, arg_pool_addr);
+ //parse ip and address
+ std::string::size_type p = pool_addr.find(':');
+ CHECK_AND_ASSERT_MES(p != std::string::npos && (p + 1 != pool_addr.size()), false, "Wrong srv address syntax");
+ m_pool_ip = pool_addr.substr(0, p);
+ m_pool_port = pool_addr.substr(p + 1, pool_addr.size());
+ m_login = command_line::get_arg(vm, arg_login);
+ m_pass = command_line::get_arg(vm, arg_pass);
+ return true;
+ }
+
+ bool simpleminer::text_job_details_to_native_job_details(const job_details& job, simpleminer::job_details_native& native_details)
+ {
+ bool r = epee::string_tools::parse_hexstr_to_binbuff(job.blob, native_details.blob);
+ CHECK_AND_ASSERT_MES(r, false, "wrong buffer sent from pool server");
+ r = epee::string_tools::parse_tpod_from_hex_string(job.target, native_details.target);
+ CHECK_AND_ASSERT_MES(r, false, "wrong buffer sent from pool server");
+ native_details.job_id = job.job_id;
+ return true;
+ }
+
+ bool simpleminer::run()
+ {
+ std::string pool_session_id;
+ simpleminer::job_details_native job = AUTO_VAL_INIT(job);
+ uint64_t last_job_ticks = 0;
+
+ while(true)
+ {
+ //-----------------
+ last_job_ticks = epee::misc_utils::get_tick_count();
+ if(!m_http_client.is_connected())
+ {
+ LOG_PRINT_L0("Connecting " << m_pool_ip << ":" << m_pool_port << "....");
+ if(!m_http_client.connect(m_pool_ip, m_pool_port, 1000))
+ {
+ LOG_PRINT_L0("Failed to connect " << m_pool_ip << ":" << m_pool_port << ", sleep....");
+ epee::misc_utils::sleep_no_w(1000);
+ continue;
+ }
+ //DO AUTH
+ LOG_PRINT_L0("Connected " << m_pool_ip << ":" << m_pool_port << " OK");
+ COMMAND_RPC_LOGIN::request req = AUTO_VAL_INIT(req);
+ req.login = m_login;
+ req.pass = m_pass;
+ COMMAND_RPC_LOGIN::response resp = AUTO_VAL_INIT(resp);
+ if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_LOGIN>("/", req, resp, m_http_client))
+ {
+ LOG_PRINT_L0("Failed to invoke login " << m_pool_ip << ":" << m_pool_port << ", disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ continue;
+ }
+ if(resp.status != "OK" || resp.id.empty())
+ {
+ LOG_PRINT_L0("Failed to login " << m_pool_ip << ":" << m_pool_port << ", disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ continue;
+ }
+ pool_session_id = resp.id;
+ //78
+ if(!text_job_details_to_native_job_details(resp.job, job))
+ {
+ LOG_PRINT_L0("Failed to text_job_details_to_native_job_details(), disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ continue;
+ }
+ last_job_ticks = epee::misc_utils::get_tick_count();
+
+ }
+ while(epee::misc_utils::get_tick_count() - last_job_ticks < 20000)
+ {
+ //uint32_t c = (*((uint32_t*)&job.blob.data()[39]));
+ ++(*((uint32_t*)&job.blob.data()[39]));
+ crypto::hash h = cryptonote::null_hash;
+ crypto::cn_slow_hash(job.blob.data(), job.blob.size(), h);
+ if( ((uint32_t*)&h)[7] < job.target )
+ {
+ //found!
+
+ COMMAND_RPC_SUBMITSHARE::request submit_request = AUTO_VAL_INIT(submit_request);
+ COMMAND_RPC_SUBMITSHARE::response submit_response = AUTO_VAL_INIT(submit_response);
+ submit_request.id = pool_session_id;
+ submit_request.job_id = job.job_id;
+ submit_request.nonce = epee::string_tools::pod_to_hex((*((uint32_t*)&job.blob.data()[78])));
+ LOG_PRINT_L0("Share found: nonce=" << submit_request.nonce << " for job=" << job.job_id << ", submitting...");
+ if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_SUBMITSHARE>("/", submit_request, submit_response, m_http_client))
+ {
+ LOG_PRINT_L0("Failed to submit share! disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ break;
+ }
+ if(submit_response.status != "OK")
+ {
+ LOG_PRINT_L0("Failed to submit share! (submitted share rejected) disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ break;
+ }
+ LOG_PRINT_GREEN("Share submitted successfully!", LOG_LEVEL_0);
+ break;
+ }
+ }
+ //get next job
+ COMMAND_RPC_GETJOB::request getjob_request = AUTO_VAL_INIT(getjob_request);
+ COMMAND_RPC_GETJOB::response getjob_response = AUTO_VAL_INIT(getjob_response);
+ getjob_request.id = pool_session_id;
+ LOG_PRINT_L0("Getting next job...");
+ if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_GETJOB>("/", getjob_request, getjob_response, m_http_client))
+ {
+ LOG_PRINT_L0("Can't get new job! Disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ break;
+ }
+ if(!text_job_details_to_native_job_details(getjob_response, job))
+ {
+ LOG_PRINT_L0("Failed to text_job_details_to_native_job_details(), disconnect and sleep....");
+ m_http_client.disconnect();
+ epee::misc_utils::sleep_no_w(1000);
+ continue;
+ }
+ last_job_ticks = epee::misc_utils::get_tick_count();
+ }
+
+ return true;
+
+ }
+}
diff --git a/src/miner/simpleminer.h b/src/miner/simpleminer.h
new file mode 100644
index 000000000..803ea12fb
--- /dev/null
+++ b/src/miner/simpleminer.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#pragma once
+#include "net/http_client.h"
+#include "cryptonote_protocol/blobdatatype.h"
+#include "simpleminer_protocol_defs.h"
+namespace mining
+{
+ class simpleminer
+ {
+ public:
+ static void init_options(boost::program_options::options_description& desc);
+ bool init(const boost::program_options::variables_map& vm);
+ bool run();
+ private:
+ struct job_details_native
+ {
+ cryptonote::blobdata blob;
+ uint32_t target;
+ std::string job_id;
+ };
+
+ static bool text_job_details_to_native_job_details(const job_details& job, job_details_native& native_details);
+
+ std::string m_pool_ip;
+ std::string m_pool_port;
+ std::string m_login;
+ std::string m_pass;
+ epee::net_utils::http::http_simple_client m_http_client;
+ };
+}
diff --git a/src/miner/simpleminer_protocol_defs.h b/src/miner/simpleminer_protocol_defs.h
new file mode 100644
index 000000000..9b70f8cbf
--- /dev/null
+++ b/src/miner/simpleminer_protocol_defs.h
@@ -0,0 +1,104 @@
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#pragma once
+#include "cryptonote_protocol/cryptonote_protocol_defs.h"
+#include "cryptonote_core/cryptonote_basic.h"
+#include "crypto/hash.h"
+#include "net/rpc_method_name.h"
+
+namespace mining
+{
+ //-----------------------------------------------
+#define CORE_RPC_STATUS_OK "OK"
+
+
+ struct job_details
+ {
+ std::string blob;
+ std::string target;
+ std::string job_id;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(blob)
+ KV_SERIALIZE(target)
+ KV_SERIALIZE(job_id)
+ END_KV_SERIALIZE_MAP()
+ };
+
+
+ struct COMMAND_RPC_LOGIN
+ {
+ RPC_METHOD_NAME("login");
+
+ struct request
+ {
+ std::string login;
+ std::string pass;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(login)
+ KV_SERIALIZE(pass)
+ END_KV_SERIALIZE_MAP()
+ };
+
+
+ struct response
+ {
+ std::string status;
+ std::string id;
+ job_details job;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(id)
+ KV_SERIALIZE(job)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
+ struct COMMAND_RPC_GETJOB
+ {
+ RPC_METHOD_NAME("getjob");
+
+ struct request
+ {
+ std::string id;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(id)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ typedef job_details response;
+ };
+
+ struct COMMAND_RPC_SUBMITSHARE
+ {
+ RPC_METHOD_NAME("submit");
+
+ struct request
+ {
+ std::string id;
+ std::string nonce;
+ std::string job_id;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(id)
+ KV_SERIALIZE(nonce)
+ KV_SERIALIZE(job_id)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+}
+
diff --git a/src/miner/target_helper.h b/src/miner/target_helper.h
new file mode 100644
index 000000000..5ac6eed14
--- /dev/null
+++ b/src/miner/target_helper.h
@@ -0,0 +1,18 @@
+// Copyright (c) 2012-2013 The Cryptonote developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#pragma once
+#include "cryptonote_core/difficulty.h"
+
+
+namespace mining
+{
+ inline uint32_t get_target_for_difficulty(cryptonote::difficulty_type difficulty)
+ {
+ if(!difficulty)
+ return 0xffffffff;
+ return 0xffffffff/static_cast<uint32_t>(difficulty);
+ }
+
+}