diff --git a/CMakeLists.txt b/CMakeLists.txt index e0dd437..747fcba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ set(SOURCE hash/argon2/defs.h hash/argon2/argon2profile_4_4_16384.c hash/argon2/argon2profile_1_1_524288.c hash/argon2/blake2/blake2b.c hash/argon2/blake2/blake2.h - hash/gpu/opencl_kernel.cpp hash/gpu/opencl_kernel.h crypt/base64.cpp crypt/base64.h crypt/random_generator.cpp crypt/random_generator.h autotune/autotune.cpp autotune/autotune.h) + hash/gpu/opencl_kernel.cpp hash/gpu/opencl_kernel.h crypt/base64.cpp crypt/base64.h crypt/random_generator.cpp crypt/random_generator.h autotune/autotune.cpp autotune/autotune.h app/runner.h) set(ARGON2_FILL_BLOCKS_SRC hash/cpu/argon2_opt/implementation.c diff --git a/app/main.cpp b/app/main.cpp index 1db7ae6..def4f6a 100755 --- a/app/main.cpp +++ b/app/main.cpp @@ -4,12 +4,28 @@ #include "../common/common.h" #include "arguments.h" +#include "runner.h" #include "../miner/miner.h" #include "../autotune/autotune.h" #include "../proxy/proxy.h" +runner *main_app = NULL; + +void shutdown(int s){ + if(main_app != NULL) { + main_app->stop(); + } +} + int main(int argc, char *argv[]) { srand((uint32_t)time(NULL)); + struct sigaction sigIntHandler; + + sigIntHandler.sa_handler = shutdown; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, NULL); arguments args(argc, argv); @@ -27,14 +43,17 @@ int main(int argc, char *argv[]) { if(args.is_miner()) { miner m(args); + main_app = &m; m.run(); } else if(args.is_autotune()) { autotune a(args); + main_app = &a; a.run(); } else if(args.is_proxy()) { proxy p(args); + main_app = &p; p.run(); } diff --git a/app/runner.h b/app/runner.h new file mode 100644 index 0000000..3d31788 --- /dev/null +++ b/app/runner.h @@ -0,0 +1,14 @@ +// +// Created by Haifa Bogdan Adnan on 30/08/2018. +// + +#ifndef ARIOMINER_RUNNER_H +#define ARIOMINER_RUNNER_H + +class runner { +public: + virtual void run() = 0; + virtual void stop() = 0; +}; + +#endif //ARIOMINER_RUNNER_H diff --git a/autotune/autotune.cpp b/autotune/autotune.cpp index fef2aa8..2b14e0b 100644 --- a/autotune/autotune.cpp +++ b/autotune/autotune.cpp @@ -9,7 +9,9 @@ #include "autotune.h" -autotune::autotune(arguments &args) : __args(args) { } +autotune::autotune(arguments &args) : __args(args) { + __running = false; +} autotune::~autotune() { } @@ -27,7 +29,13 @@ void autotune::run() { double best_intensity = 0; double best_hashrate = 0; + __running = true; + for(double intensity = __args.gpu_intensity_start(); intensity <= __args.gpu_intensity_stop(); intensity += __args.gpu_intensity_step()) { + if(!__running) { + break; + } + cout << fixed << setprecision(2) <<"Intensity " << intensity << ": " << flush; if(__args.argon2_profile() == "1_1_524288") { __args.gpu_intensity_cblocks().clear(); @@ -62,5 +70,16 @@ void autotune::run() { cout << fixed << setprecision(2) << hashrate << " h/s" <::iterator it = hashers.begin();it != hashers.end();++it) { + if((*it)->get_type() == "GPU") { + (*it)->cleanup(); + } + } + cout << fixed << setprecision(2) << "Best intensity is " << best_intensity << ", running at " << best_hashrate << " h/s." << endl; -} \ No newline at end of file +} + +void autotune::stop() { + cout << endl << "Received termination request, please wait for cleanup ... " << endl; + __running = false; +} diff --git a/autotune/autotune.h b/autotune/autotune.h index f716347..1fe06f6 100644 --- a/autotune/autotune.h +++ b/autotune/autotune.h @@ -5,15 +5,18 @@ #ifndef ARIOMINER_AUTOTUNE_H #define ARIOMINER_AUTOTUNE_H +#include "../app/runner.h" -class autotune { +class autotune : public runner { public: autotune(arguments &args); ~autotune(); - void run(); + virtual void run(); + virtual void stop(); private: arguments &__args; + bool __running; }; diff --git a/common/common.h b/common/common.h index 1c0a811..f5d0493 100755 --- a/common/common.h +++ b/common/common.h @@ -32,6 +32,7 @@ #include #include #include +#include #else #include #include diff --git a/miner/miner.cpp b/miner/miner.cpp index 228a7e3..73bfa15 100755 --- a/miner/miner.cpp +++ b/miner/miner.cpp @@ -22,6 +22,7 @@ miner::miner(arguments &args) : __args(args), __client(args) { __confirmed = 0; __rejected = 0; __begin_time = time(NULL); + __running = false; vector hashers = hasher::get_hashers(); for(vector::iterator it = hashers.begin();it != hashers.end();++it) { @@ -46,64 +47,69 @@ miner::~miner() { } void miner::run() { - uint64_t last_update, last_report; + uint64_t last_update, last_report; last_update = last_report = 0; - vector hashers = hasher::get_active_hashers(); + vector hashers = hasher::get_active_hashers(); + + __running = true; - while (true) { - for(vector::iterator it = hashers.begin();it != hashers.end();++it) { + while (__running) { + for (vector::iterator it = hashers.begin(); it != hashers.end(); ++it) { vector hashes = (*it)->get_hashes(); - for(vector::iterator hash=hashes.begin();hash != hashes.end();hash++) { - if(hash->block != __blk) //the block expired + for (vector::iterator hash = hashes.begin(); hash != hashes.end(); hash++) { + if (hash->block != __blk) //the block expired continue; // LOG(hash->hash); string duration = __calc_duration(hash->base, hash->hash); uint64_t result = __calc_compare(duration); - if(result > 0 && result <= __limit) { - if(__args.is_verbose()) LOG("--> Submitting nonce: " + hash->nonce + " / " + hash->hash.substr(30)); + if (result > 0 && result <= __limit) { + if (__args.is_verbose()) + LOG("--> Submitting nonce: " + hash->nonce + " / " + hash->hash.substr(30)); ariopool_submit_result reply = __client.submit(hash->hash, hash->nonce, __public_key); - if(reply.success) { - if(result <= GOLD_RESULT) { - if(__args.is_verbose()) LOG("--> Block found."); + if (reply.success) { + if (result <= GOLD_RESULT) { + if (__args.is_verbose()) LOG("--> Block found."); __found++; - } - else { - if(__args.is_verbose()) LOG("--> Nonce confirmed."); + } else { + if (__args.is_verbose()) LOG("--> Nonce confirmed."); __confirmed++; } - } - else { - if(__args.is_verbose()) { + } else { + if (__args.is_verbose()) { LOG("--> The nonce did not confirm."); LOG("--> Pool response: "); LOG(reply.pool_response); } __rejected++; - if(hash->realloc_flag != NULL) + if (hash->realloc_flag != NULL) *(hash->realloc_flag) = true; } } } } - if(microseconds() - last_update > __args.update_interval()) { - if(__update_pool_data()) { - for(vector::iterator it = hashers.begin();it != hashers.end();++it) { + if (microseconds() - last_update > __args.update_interval()) { + if (__update_pool_data()) { + for (vector::iterator it = hashers.begin(); it != hashers.end(); ++it) { (*it)->set_input(__public_key, __blk, __difficulty, __argon2profile, __recommendation); } } last_update = microseconds(); } - if(microseconds() - last_report > __args.report_interval()) { + if (microseconds() - last_report > __args.report_interval()) { __display_report(); last_report = microseconds(); } this_thread::sleep_for(chrono::milliseconds(100)); } + + for (vector::iterator it = hashers.begin(); it != hashers.end(); ++it) { + (*it)->cleanup(); + } } string miner::__calc_duration(const string &base, const string &hash) { @@ -256,3 +262,8 @@ void miner::__display_report() { LOG(ss.str()); } + +void miner::stop() { + cout << endl << "Received termination request, please wait for cleanup ... " << endl; + __running = false; +} diff --git a/miner/miner.h b/miner/miner.h index f46ae8a..4c96afb 100755 --- a/miner/miner.h +++ b/miner/miner.h @@ -8,13 +8,15 @@ #define GOLD_RESULT 240 #include "../http/client.h" +#include "../app/runner.h" -class miner { +class miner : public runner { public: miner(arguments &args); ~miner(); - void run(); + virtual void run(); + virtual void stop(); private: string __calc_duration(const string &base, const string &hash); @@ -36,6 +38,8 @@ class miner { time_t __begin_time; + bool __running; + arguments &__args; ariopool_client __client; }; diff --git a/proxy/proxy.cpp b/proxy/proxy.cpp index 7c413f2..710de43 100644 --- a/proxy/proxy.cpp +++ b/proxy/proxy.cpp @@ -12,4 +12,6 @@ proxy::proxy(arguments &args) { } proxy::~proxy() { } -void proxy::run() { } \ No newline at end of file +void proxy::run() { } + +void proxy::stop() { } \ No newline at end of file diff --git a/proxy/proxy.h b/proxy/proxy.h index e10a81f..710ef17 100644 --- a/proxy/proxy.h +++ b/proxy/proxy.h @@ -5,13 +5,15 @@ #ifndef PROJECT_PROXY_H #define PROJECT_PROXY_H +#include "../app/runner.h" -class proxy { +class proxy : public runner { public: proxy(arguments &args); ~proxy(); - void run(); + virtual void run(); + virtual void stop(); };