aboutsummaryrefslogtreecommitdiff
path: root/src/miner/simpleminer.cpp
blob: fafe6b3f3bc5bc0c1560e8412ca30f37d17327b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2014-2015, The Monero Project
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#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, 20000))
        {
          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;
        req.agent = "simpleminer/0.1";
        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 (resp.job.blob.empty() && resp.job.target.empty() && resp.job.job_id.empty())
        {
            LOG_PRINT_L0("Job didn't change");
            continue;
        }
        else 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()[39])));
          submit_request.result = epee::string_tools::pod_to_hex(h);
          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);
        continue;
      }
      if (getjob_response.blob.empty() && getjob_response.target.empty() && getjob_response.job_id.empty())
      {
        LOG_PRINT_L0("Job didn't change");
        continue;
      }
      else 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;

  }
}