forked from cryptozoidberg/boolberry
-
Notifications
You must be signed in to change notification settings - Fork 4
/
simpleminer.cpp
793 lines (715 loc) · 28.3 KB
/
simpleminer.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// 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 "rpc/mining_protocol_defs.h"
#include "storages/http_abstract_invoke.h"
#include "string_tools.h"
#include "currency_core/account.h"
#include "currency_core/currency_format_utils.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "currency_core/miner_common.h"
#ifndef WIN32
#include <sys/mman.h>
#endif
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_2);
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;
while (1) {
mining::simpleminer miner;
r = miner.init(vm);
r = r && miner.run(); // Returns on too many failures
LOG_PRINT_L0("Excessive failures. Sleeping 10-ish seconds and restarting...");
epee::misc_utils::sleep_no_w(10000 + (crypto::rand<size_t>() % 5000));
}
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", ""};
const command_line::arg_descriptor<uint32_t> arg_mining_threads = { "mining-threads", "Specify mining threads count", 1, true };
const command_line::arg_descriptor<uint32_t> arg_platform = { "platform", "Specify OpenCL platform number (starting at 0)", 0, true };
const command_line::arg_descriptor<uint32_t> arg_device = { "device", "Specify OpenCL device number (starting at 0)", 0, true };
const command_line::arg_descriptor<std::string, true> arg_scratchpad_url = { "remote_scratchpad", "Specify URL to remote scratchpad"};
const command_line::arg_descriptor<std::string> arg_scratchpad_local = { "local_scratchpad", "Specify URL to remote scratchpad ", "", true };
static const int attempts_per_loop = 64 * 1024 * 4;
//-----------------------------------------------------------------------------------------------------
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);
command_line::add_arg(desc, arg_mining_threads);
command_line::add_arg(desc, arg_platform);
command_line::add_arg(desc, arg_device);
command_line::add_arg(desc, arg_scratchpad_url);
command_line::add_arg(desc, arg_scratchpad_local);
}
//-----------------------------------------------------------------------------------------------------
bool try_mkdir_chdir(const std::string& dirn)
{
boost::system::error_code ec;
if(boost::filesystem::exists(dirn, ec))
{
return true;
}
boost::filesystem::create_directories("/some/path", ec);
return !ec;
}
//-----------------------------------------------------------------------------------------------------
std::string get_default_local_cache_path()
{
#if defined(_WIN64) || defined(_WIN32)
const char* phome_var_name = "LOCALAPPDATA";
#else
const char* phome_var_name = "HOME";
#endif
if (!getenv(phome_var_name))
{
LOG_ERROR("Env variable " << phome_var_name << " not set");
return "";
}
std::string default_local_path = getenv(phome_var_name);
#if !defined(_WIN64) && !defined(_WIN32)
default_local_path += "/.cache";
#else
default_local_path += "/" CURRENCY_NAME;
#endif
if (!try_mkdir_chdir(default_local_path) )
{
LOG_ERROR("Failed to create a dir " << getenv(phome_var_name));
return "";
}
return default_local_path + "/" SCRATCHPAD_DEFAULT_FILENAME;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::reset_scratchpad()
{
m_blocks_addendums.clear();
m_hi = AUTO_VAL_INIT(m_hi);
m_scratchpad.clear();
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::store_scratchpad_to_file(const std::string& path)
{
std::string buff;
buff.resize(sizeof(export_scratchpad_file_header) + m_scratchpad.size()*32, 0);
uint8_t* pbuff = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buff.data()));
export_scratchpad_file_header* pheader = reinterpret_cast<export_scratchpad_file_header*>(pbuff);
pheader->current_hi.height = m_hi.height;
pheader->current_hi.prevhash = m_hi.id;
pheader->scratchpad_size = m_scratchpad.size()*4;
pbuff += sizeof(export_scratchpad_file_header);
memcpy(pbuff, &m_scratchpad[0], m_scratchpad.size()*32);
if(!file_io_utils::save_string_to_file(path, buff))
{
LOG_ERROR("Failed to save scratchpad local cache to " << path);
return false;
}
m_last_scratchpad_store_time = time(NULL);
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::load_scratchpad_from_file(const std::string& path)
{
reset_scratchpad();
if(!path.size())
{
LOG_ERROR("Empty scratchpad file path");
return false;
}
boost::system::error_code ec;
time_t file_time = boost::filesystem::last_write_time(path, ec);
if(ec)
{
LOG_PRINT_L0("Local scratchpad cache (" << path << ") not found");
return false;
}
if(time(NULL) - file_time > LOCAL_SCRATCHPAD_CACHE_EXPIRATION_INTERVAL )
{
/*scratchpad older than 5 days, better to redownload*/
LOG_PRINT_L0("Local scratchpad cache (" << path << ") is older that 5 days, downloading new...");
return false;
}
std::string buff;
if(!epee::file_io_utils::load_file_to_string(path, buff))
{
LOG_PRINT_L0("Local scratchpad cache (" << path << ") not found");
return false;
}
if(buff.size() < sizeof(export_scratchpad_file_header))
{
LOG_ERROR("Wrong scratchpad file size: " << buff.size() << " in file " << path);
return false;
}
const uint8_t* pbuff = reinterpret_cast<const uint8_t*>(buff.data());
const export_scratchpad_file_header* pscr_header = reinterpret_cast<const export_scratchpad_file_header*>(pbuff);
if(pscr_header->scratchpad_size*8 != buff.size()-sizeof(export_scratchpad_file_header) || pscr_header->scratchpad_size%4)
{
LOG_ERROR("File size and export_scratchpad_file_header mismatch, " << path);
return false;
}
/*
TODO: refactor copy addendums array
for(int i = 0; i != WILD_KECCAK_ADDENDUMS_ARRAY_SIZE; i++)
{
m_blocks_addendums.push_back(mining::addendum());
m_blocks_addendums.back().hi.block_id = pscr_header->add_arr[i].prev_hi
}*/
m_hi.height = pscr_header->current_hi.height;
m_hi.id = pscr_header->current_hi.prevhash;
m_scratchpad.resize(pscr_header->scratchpad_size/4);
pbuff+= sizeof(export_scratchpad_file_header);
memcpy(&m_scratchpad[0], pbuff, buff.size()-sizeof(export_scratchpad_file_header));
m_last_scratchpad_store_time = file_time;
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::init_scratchpad()
{
//let's try to lookup scratchpad in local cache and then, if it not there - try to fetch it from server
if(!load_scratchpad_from_file(m_scratchpad_local_path))
{
//failed, try to fetch it from server
if(!m_scratchpad_url.size())
{
LOG_ERROR("Local scratchpad chache not found, and scratchpad url not found");
return false;
}
epee::net_utils::http::http_simple_client http_client;
const epee::net_utils::http::http_response_info *prespinfo = NULL;
LOG_PRINT_L0("Downloading remote scrathcpad from " << m_scratchpad_url << "...");
if(!epee::net_utils::http::invoke_request(m_scratchpad_url, http_client, 10000, &prespinfo))
{
LOG_ERROR("Local scratchpad chache not found, and scratchpad url not found");
return false;
}
LOG_PRINT_L0("Remote scratchpad downloaded, size " << prespinfo->m_body.size()/1024 << "Kb");
if(!file_io_utils::save_string_to_file(m_scratchpad_local_path, prespinfo->m_body))
{
LOG_ERROR("Failed to store local scratchpad file to path " << m_scratchpad_local_path);
return false;
}
//let's try to load it once again
if(!load_scratchpad_from_file(m_scratchpad_local_path))
{
LOG_ERROR("Local scratchpad chache not found, and scratchpad url not found");
return false;
}
}
update_fast_scratchpad();
LOG_PRINT_L0("Scratchpad loaded okay, hashes count: " << m_scratchpad.size());
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
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_threads_total = 1;
if(command_line::has_arg(vm, arg_mining_threads))
{
m_threads_total = command_line::get_arg(vm, arg_mining_threads);
LOG_PRINT_L0("Mining with " << m_threads_total << " threads ");
}
m_pass = command_line::get_arg(vm, arg_pass);
m_hi = AUTO_VAL_INIT(m_hi);
m_last_job_ticks = 0;
m_last_scratchpad_store_time = 0;
m_fast_scratchpad_pages = 0;
m_fast_scratchpad = NULL;
if(command_line::has_arg(vm, arg_scratchpad_url))
{
m_scratchpad_url = command_line::get_arg(vm, arg_scratchpad_url);
}
if(command_line::has_arg(vm, arg_scratchpad_local))
{
m_scratchpad_local_path = command_line::get_arg(vm, arg_scratchpad_local);
}else
{
m_scratchpad_local_path = get_default_local_cache_path();
}
// Set-up OpenCL Platform
m_platform = 0;
if(command_line::has_arg(vm, arg_platform))
m_platform = command_line::get_arg(vm, arg_platform);
m_device = 0;
if(command_line::has_arg(vm, arg_device))
m_device = command_line::get_arg(vm, arg_device);
pOCL_Device = new OCL_Device(m_platform, m_device, attempts_per_loop, 0, "wild_keccak.cl");
if (!pOCL_Device->isOK())
exit(-1);
pOCL_Device->SetBuildOptions("");
pOCL_Device->PrintInfo();
if(!init_scratchpad())
{
LOG_ERROR("Failed to init scratchpad");
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::text_height_info_to_native_height_info(const height_info& hi, height_info_native& hi_native)
{
hi_native.height = hi.height;
bool r = string_tools::hex_to_pod(hi.block_id, hi_native.id);
CHECK_AND_ASSERT_MES(r, false, "wrong block_id: " << hi.block_id);
return true;
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::native_height_info_to_text_height_info(height_info& hi, const height_info_native& hi_native)
{
hi.height = hi_native.height;
hi.block_id = string_tools::pod_to_hex(hi_native.id);
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::get_xtype_from_string(native_details.difficulty, job.difficulty);
CHECK_AND_ASSERT_MES(r, false, "wrong buffer sent from pool server");
native_details.job_id = job.job_id;
return text_height_info_to_native_height_info(job.prev_hi, native_details.prev_hi);
}
//--------------------------------------------------------------------------------------------------------------------------------
void simpleminer::worker_thread(uint64_t start_nonce, uint32_t nonce_offset, std::atomic<uint32_t> *result, std::atomic<bool> *do_reset, std::atomic<bool> *done) {
// printf("Worker thread starting at %lu + %u\n", start_nonce, nonce_offset);
currency::blobdata blob = m_job.blob;
currency::difficulty_type difficulty = m_job.difficulty;
while (!*do_reset) {
m_hashes_done += attempts_per_loop;
LOG_PRINT_L3("Blob size " << blob.size() << ", scratchpad " << m_scratchpad.size() << ", start " << start_nonce << ", offset " << nonce_offset);
char* data = new char[blob.size()];
memcpy(data, blob.data(), blob.size());
pOCL_Device->CopyBufferToDevice(data, 0, blob.size());
#define OUTPUT_SIZE 16
uint64_t* output = new uint64_t[OUTPUT_SIZE];
memset(output, 0, OUTPUT_SIZE * sizeof(uint64_t));
pOCL_Device->CopyBufferToDevice(output, 1, OUTPUT_SIZE * sizeof(uint64_t));
pOCL_Device->run(start_nonce+nonce_offset, difficulty == 0 ? ULLONG_MAX : ULLONG_MAX / difficulty);
pOCL_Device->CopyBufferToHost(output, 1, OUTPUT_SIZE * sizeof(uint64_t));
for (uint64_t i = 0; i < output[OUTPUT_SIZE-1]; i++) {
uint64_t nounce = output[i];
LOG_PRINT_L2("GPU " << (nounce));
(*reinterpret_cast<uint64_t*>(&blob[1])) = nounce;
crypto::hash h = currency::null_hash;
currency::get_blob_longhash(blob, h, m_job.prev_hi.height+1, [&](uint64_t index) -> crypto::hash&
{
return m_fast_scratchpad[index%m_scratchpad.size()];
});
if( currency::check_hash(h, m_job.difficulty))
{
LOG_PRINT_L2("CPU " << (nounce));
(*result) = (uint32_t)(nounce - start_nonce);
(*done) = true;
(*do_reset) = true;
m_work_done_cond.notify_one();
return;
}
}
nonce_offset += (m_threads_total * attempts_per_loop);
}
}
//--------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::run()
{
m_job = AUTO_VAL_INIT(m_job);
uint64_t search_start = epee::misc_utils::get_tick_count();
uint64_t hashes_done = 0;
uint32_t job_submit_failures = 0;
bool re_get_scratchpad = false;
while(true)
{
bool job_received = false;
//-----------------
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;
}
m_http_client.get_socket().set_option(boost::asio::ip::tcp::no_delay(true));
//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";
native_height_info_to_text_height_info(req.hi, m_hi);
bool job_requested = m_hi.height ? true:false;
COMMAND_RPC_LOGIN::response resp = AUTO_VAL_INIT(resp);
if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_LOGIN>("/json_rpc", 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 != CORE_RPC_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;
}
m_pool_session_id = resp.id;
if (re_get_scratchpad || !m_hi.height || !m_scratchpad.size())
{
if (!reinit_scratchpad())
continue;
re_get_scratchpad = false;
}
else if(!apply_addendums(resp.job.addms))
{
LOG_PRINT_L0("Failed to apply_addendum, requesting full scratchpad...");
if (!reinit_scratchpad())
continue;
}
if (job_requested && resp.job.blob.empty() && resp.job.difficulty.empty() && resp.job.job_id.empty())
{
LOG_PRINT_L0("Job didn't change");
continue;
}
else if(job_requested && !text_job_details_to_native_job_details(resp.job, m_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;
}
if(job_requested)
{
m_last_job_ticks = epee::misc_utils::get_tick_count();
job_received = true;
}
}
if(!job_received)
{
uint64_t get_job_start_time = epee::misc_utils::get_tick_count();
if(!get_job()) /* Next version: Handle this asynchronously */
{
continue;
}
uint64_t get_job_end_time = epee::misc_utils::get_tick_count();
if ((get_job_end_time - get_job_start_time) > 1000)
{
LOG_PRINT_L0("slow pool response " << (get_job_end_time - get_job_start_time) << " ms");
}
}
update_fast_scratchpad();
uint64_t start_nonce = (*reinterpret_cast<uint64_t*>(&m_job.blob[1])) + 1000000;
std::list<boost::thread> threads;
std::atomic<uint32_t> results[128];
std::atomic<bool> do_reset(false);
std::atomic<bool> done(false);
if (m_threads_total > 1) {
LOG_PRINT_RED_L0("OpenCL simpleminer should be used 1 instance per GPU! Changing to 1 mining thread.");
m_threads_total = 1;
}
m_hashes_done = 0; /* Used to calculate offset to push back into job */
uint32_t nonce_offset = 0;
for (unsigned int i = 0; i < m_threads_total; i++)
{
results[i] = 0;
threads.push_back(boost::thread(&simpleminer::worker_thread, this, start_nonce, nonce_offset, &results[i], &do_reset, &done));
nonce_offset += attempts_per_loop;
}
while(!done && epee::misc_utils::get_tick_count() - m_last_job_ticks < 20000)
{
std::unique_lock<std::mutex> lck(m_work_mutex);
m_work_done_cond.wait_for(lck, std::chrono::seconds(1));
}
do_reset = true;
BOOST_FOREACH(boost::thread& th, threads)
{
th.join();
}
for (unsigned int i = 0; i < m_threads_total; i++) {
if (results[i] != 0) {
(*reinterpret_cast<uint64_t*>(&m_job.blob[1])) = (start_nonce + results[i]);
crypto::hash h = currency::null_hash;
currency::get_blob_longhash(m_job.blob, h, m_job.prev_hi.height+1, [&](uint64_t index) -> crypto::hash&
{
return m_scratchpad[index%m_scratchpad.size()];
});
if( currency::check_hash(h, m_job.difficulty))
{
//<< ", id" << currency::get_blob_hash(m_job.blob) << ENDL
//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 = m_pool_session_id;
submit_request.job_id = m_job.job_id;
submit_request.nonce = (*reinterpret_cast<uint64_t*>(&m_job.blob[1]));
submit_request.result = string_tools::buff_to_hex_nodelimer(std::string((char*) &h, HASH_SIZE));
LOG_PRINT_GREEN("Share found: nonce=" << submit_request.nonce << " for job=" << m_job.job_id << ", diff: " << m_job.difficulty << ENDL
<< ", PoW:" << h << ", height:" << m_job.prev_hi.height+1 << ", submitting...", LOG_LEVEL_0);
//LOG_PRINT_L0("Block hashing blob: " << string_tools::buff_to_hex_nodelimer(m_job.blob));
//LOG_PRINT_L0("scratch_pad: " << currency::dump_scratchpad(m_scratchpad));
if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_SUBMITSHARE>("/json_rpc", submit_request, submit_response, m_http_client))
{
/* Failed to submit a job. This can happen because of disconnection,
* server failure, or block expiry. In any event, try to get
* a new job. If the job fetch fails, get_job will disconnect
* and sleep for us */
LOG_PRINT_L0("Failed to submit share! Updating job.");
job_submit_failures++;
}
else if(submit_response.status != "OK")
{
LOG_PRINT_L0("Failed to submit share! (submitted share rejected). Updating job.");
job_submit_failures++;
}
else
{
LOG_PRINT_GREEN("Share submitted successfully!", LOG_LEVEL_0);
job_submit_failures = 0;
}
break; /* One submission per job id */
}
else
{
LOG_PRINT_L0("share did not pass diff revalidation");
}
}
}
start_nonce += m_hashes_done;
hashes_done += m_hashes_done;
m_hashes_done = 0;
(*reinterpret_cast<uint64_t*>(&m_job.blob[1])) = start_nonce;
if (job_submit_failures == 5)
{
LOG_PRINT_L0("Too many submission failures. Something is very wrong.");
free_fast_scratchpad();
return false;
}
if (job_submit_failures > 3)
{
re_get_scratchpad = true;
/* note fallthrough into next case to also reconnect */
}
if (job_submit_failures > 1)
{
m_http_client.disconnect();
epee::misc_utils::sleep_no_w(1000);
} else {
uint64_t loop_end_time = epee::misc_utils::get_tick_count();
uint64_t hash_rate = (hashes_done * 1000) / ((loop_end_time - search_start) + 1);
LOG_PRINT_L0("avg hr: " << hash_rate);
}
}
free_fast_scratchpad();
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::reinit_scratchpad()
{
init_scratchpad();
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
void simpleminer::free_fast_scratchpad()
{
if (m_fast_scratchpad)
{
if (m_fast_mmapped)
{
#ifndef WIN32
munmap(m_fast_scratchpad, m_fast_scratchpad_pages*4096);
#endif
} else {
free(m_fast_scratchpad);
}
m_fast_scratchpad = NULL;
}
}
void simpleminer::update_fast_scratchpad()
{
/* Check size - reallocate fast scratch if necessary */
size_t cur_scratchpad_size = m_scratchpad.size() * sizeof(crypto::hash);
size_t cur_scratchpad_pages = (cur_scratchpad_size / 4096) + 1;
if (cur_scratchpad_pages > m_fast_scratchpad_pages)
{
free_fast_scratchpad();
void *addr = nullptr;
size_t mapsize = cur_scratchpad_pages * 4096;
#ifdef MAP_HUGETLB
addr = MAP_FAILED;
addr = mmap(0, mapsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|MAP_POPULATE, 0, 0);
if (addr == MAP_FAILED)
{
LOG_PRINT_L0("Unable to mmap huge pages. Enable for faster operation.");
addr = nullptr;
}
#endif
if (addr == nullptr) {
addr = malloc(mapsize);
} else {
m_fast_mmapped = true;
}
m_fast_scratchpad_pages = (uint32_t)cur_scratchpad_pages;
m_fast_scratchpad = (crypto::hash *)addr;
}
memcpy(m_fast_scratchpad, &m_scratchpad[0], m_scratchpad.size() * sizeof(crypto::hash));
// Update OpenCL buffer
pOCL_Device->updateScratchpad(&m_scratchpad[0], m_scratchpad.size(), sizeof(crypto::hash));
}
//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::pop_addendum(const addendum& add)
{
std::vector<crypto::hash> add_vec;
bool r = currency::hexstr_to_addendum(add.addm, add_vec);
CHECK_AND_ASSERT_MES(r, false, "failed to hexstr_to_addendum");
CHECK_AND_ASSERT_MES(m_scratchpad.size() > add_vec.size(), false, "error: to big addendum or to small local scratchpad");
std::map<uint64_t, crypto::hash> patch;
currency::get_scratchpad_patch(m_scratchpad.size() - add_vec.size(),
0,
add_vec.size(),
add_vec,
patch);
r = currency::apply_scratchpad_patch(m_scratchpad, patch);
CHECK_AND_ASSERT_MES(r, false, "failed to apply patch");
m_scratchpad.resize(m_scratchpad.size() - add_vec.size());
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::push_addendum(const addendum& add)
{
std::vector<crypto::hash> add_vec;
bool r = currency::hexstr_to_addendum(add.addm, add_vec);
CHECK_AND_ASSERT_MES(r, false, "failed to hexstr_to_addendum");
std::map<uint64_t, crypto::hash> patch;
currency::get_scratchpad_patch(m_scratchpad.size(),
0,
add_vec.size(),
add_vec,
patch);
r = currency::apply_scratchpad_patch(m_scratchpad, patch);
CHECK_AND_ASSERT_MES(r, false, "failed to apply patch");
for(const auto& h: add_vec)
m_scratchpad.push_back(h);
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::apply_addendums(const std::list<addendum>& addms)
{
if(!addms.size())
return true;
crypto::hash pid = currency::null_hash;
bool r = string_tools::hex_to_pod(addms.begin()->prev_id, pid);
CHECK_AND_ASSERT_MES(r, false, "wrong prev_id");
if(pid != m_hi.id)
{
LOG_PRINT_L0("Split detected, looking for patch");
auto it = std::find_if(m_blocks_addendums.begin(), m_blocks_addendums.end(),[&](const mining::addendum& a){
if(a.hi.block_id == addms.begin()->prev_id)
return true;
else
return false;
});
if(it == m_blocks_addendums.end())
return false;
//unpatch
size_t count = 0;
for(auto it_to_patch = --m_blocks_addendums.end(); it_to_patch != it; --it)
{
r = pop_addendum(*it_to_patch);
CHECK_AND_ASSERT_MES(r, false, "failed to pop_addendum()");
++count;
}
m_blocks_addendums.erase(++it, m_blocks_addendums.end());
LOG_PRINT_L0("Numbers of blocks removed from scratchpad: " << count);
}
//append scratchpad with new blocks
for(const auto& a: addms)
{
r = push_addendum(a);
CHECK_AND_ASSERT_MES(r, false, "failed to push_addendum()");
}
LOG_PRINT_L0("Numbers of blocks added to scratchpad: " << addms.size());
text_height_info_to_native_height_info(addms.back().hi, m_hi);
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::get_job()
{
//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 = m_pool_session_id;
native_height_info_to_text_height_info(getjob_request.hi, m_hi);
LOG_PRINT_L0("Getting next job...");
if(!epee::net_utils::invoke_http_json_rpc<mining::COMMAND_RPC_GETJOB>("/json_rpc", 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);
return false;
}
if (getjob_response.jd.blob.empty() && getjob_response.jd.difficulty.empty() && getjob_response.jd.job_id.empty())
{
LOG_PRINT_L0("Job didn't change");
}
else if(!text_job_details_to_native_job_details(getjob_response.jd, m_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);
return false;
}
//apply addendum
if(!apply_addendums(getjob_response.jd.addms))
{
LOG_PRINT_L0("Failed to apply_addendum, requesting full scratchpad...");
reinit_scratchpad();
return true;
}
if(time(NULL) - m_last_scratchpad_store_time > LOCAL_SCRATCHPAD_CACHE_STORE_INTERVAL)
{
store_scratchpad_to_file(m_scratchpad_local_path);
}
m_last_job_ticks = epee::misc_utils::get_tick_count();
return true;
}
}