Skip to content

Commit

Permalink
Added cleanup procedure when shutting down miner.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanadnan committed Aug 30, 2018
1 parent 8eb1de9 commit e1797ed
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions app/main.cpp
Expand Up @@ -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);

Expand All @@ -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();
}

Expand Down
14 changes: 14 additions & 0 deletions 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
23 changes: 21 additions & 2 deletions autotune/autotune.cpp
Expand Up @@ -9,7 +9,9 @@

#include "autotune.h"

autotune::autotune(arguments &args) : __args(args) { }
autotune::autotune(arguments &args) : __args(args) {
__running = false;
}

autotune::~autotune() { }

Expand All @@ -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();
Expand Down Expand Up @@ -62,5 +70,16 @@ void autotune::run() {
cout << fixed << setprecision(2) << hashrate << " h/s" <<endl << flush;
}

for(vector<hasher*>::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;
}
}

void autotune::stop() {
cout << endl << "Received termination request, please wait for cleanup ... " << endl;
__running = false;
}
7 changes: 5 additions & 2 deletions autotune/autotune.h
Expand Up @@ -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;
};


Expand Down
1 change: 1 addition & 0 deletions common/common.h
Expand Up @@ -32,6 +32,7 @@
#include<netdb.h>
#include<arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#else
#include <WinSock2.h>
#include <WS2tcpip.h>
Expand Down
55 changes: 33 additions & 22 deletions miner/miner.cpp
Expand Up @@ -22,6 +22,7 @@ miner::miner(arguments &args) : __args(args), __client(args) {
__confirmed = 0;
__rejected = 0;
__begin_time = time(NULL);
__running = false;

vector<hasher*> hashers = hasher::get_hashers();
for(vector<hasher*>::iterator it = hashers.begin();it != hashers.end();++it) {
Expand All @@ -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<hasher*> hashers = hasher::get_active_hashers();
vector<hasher *> hashers = hasher::get_active_hashers();

__running = true;

while (true) {
for(vector<hasher*>::iterator it = hashers.begin();it != hashers.end();++it) {
while (__running) {
for (vector<hasher *>::iterator it = hashers.begin(); it != hashers.end(); ++it) {
vector<hash_data> hashes = (*it)->get_hashes();

for(vector<hash_data>::iterator hash=hashes.begin();hash != hashes.end();hash++) {
if(hash->block != __blk) //the block expired
for (vector<hash_data>::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<hasher*>::iterator it = hashers.begin();it != hashers.end();++it) {
if (microseconds() - last_update > __args.update_interval()) {
if (__update_pool_data()) {
for (vector<hasher *>::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<hasher *>::iterator it = hashers.begin(); it != hashers.end(); ++it) {
(*it)->cleanup();
}
}

string miner::__calc_duration(const string &base, const string &hash) {
Expand Down Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions miner/miner.h
Expand Up @@ -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);
Expand All @@ -36,6 +38,8 @@ class miner {

time_t __begin_time;

bool __running;

arguments &__args;
ariopool_client __client;
};
Expand Down
4 changes: 3 additions & 1 deletion proxy/proxy.cpp
Expand Up @@ -12,4 +12,6 @@ proxy::proxy(arguments &args) { }

proxy::~proxy() { }

void proxy::run() { }
void proxy::run() { }

void proxy::stop() { }
6 changes: 4 additions & 2 deletions proxy/proxy.h
Expand Up @@ -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();
};


Expand Down

0 comments on commit e1797ed

Please sign in to comment.