Skip to content

Commit

Permalink
Basic support for proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanadnan committed Feb 20, 2019
1 parent 6c0d008 commit 2bd9260
Show file tree
Hide file tree
Showing 12 changed files with 502 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(SOURCE
miner/mini-gmp/mini-gmp.h miner/mini-gmp/mini-gmp.c
autotune/autotune.cpp autotune/autotune.h
app/runner.h
miner/miner_api.cpp miner/miner_api.h miner/pool_settings_provider.cpp miner/pool_settings_provider.h http/simplejson/json.cpp)
miner/miner_api.cpp miner/miner_api.h http/pool_settings_provider.cpp http/pool_settings_provider.h http/simplejson/json.cpp proxy/proxy_server.cpp proxy/proxy_server.h proxy/index_html.cpp proxy/index_html.h)
set(SOURCE_COMMON app/arguments.cpp app/arguments.h common/common.h common/common.cpp common/dllimport.h common/dllexport.h
crypt/sha512.cpp crypt/sha512.h crypt/base64.cpp crypt/base64.h crypt/random_generator.cpp crypt/random_generator.h
common/cfgpath.h)
Expand Down
40 changes: 38 additions & 2 deletions app/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,44 @@ bool arguments::valid(string &error) {
return false;
}
}
else {
error = "Only miner or autotune mode are supported for the moment";
else if(__proxy_flag == 1) {
if(__proxy_port < 1024) {
error = "Proxy listening port must be at least 1024, lower port numbers are usually reserved by system and requires administrator privileges.";
return false;
}

if (__pool.empty()) {
error = "Pool address is mandatory.";
return false;
}

if (__pool.find("https://") == 0) {
error = "Only HTTP protocol is allowed for pool connection, HTTPS is not supported.";
return false;
}

if (__wallet.empty()) {
error = "Wallet is mandatory.";
return false;
}

if (__name.empty()) {
error = "Worker name is mandatory.";
return false;
}

if (__update_interval < 2000000) {
error = "Pool update interval must be at least 2 sec.";
return false;
}

if (__hash_report_interval < 60000000) {
error = "Reporting interval must be at least 1 min.";
return false;
}
}
else {
error = "You need to specify an operation mode (miner/autotune/proxy).";
return false;
}

Expand Down
47 changes: 46 additions & 1 deletion http/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define PROJECT_CLIENT_H

#include "http.h"
#include "../miner/pool_settings_provider.h"
#include "pool_settings_provider.h"

struct ariopool_result {
bool success;
Expand All @@ -22,6 +22,51 @@ struct ariopool_update_result : public ariopool_result {
string recommendation;
string version;
string extensions;

bool update(ariopool_update_result &src) {
if(block != src.block ||
difficulty != src.difficulty ||
limit != src.limit ||
public_key != src.public_key ||
height != src.height ||
argon2profile != src.argon2profile ||
recommendation != src.recommendation ||
version != src.version ||
extensions != src.extensions) {
block = src.block;
difficulty = src.difficulty;
limit = src.limit;
public_key = src.public_key;
height = src.height;
argon2profile = src.argon2profile;
recommendation = src.recommendation;
version = src.version;
extensions = src.extensions;

return true;
}

return false;
}

string response() {
stringstream ss;
string argon_mem = "524288";
string argon_threads = "1";
string argon_time = "1";
if(argon2profile == "4_4_16384") {
argon_mem = "16384";
argon_threads = "4";
argon_time = "4";
}

ss << "{ \"status\": \"ok\", \"data\": { \"recommendation\": \"" << recommendation << "\", \"argon_mem\": " << argon_mem
<< ", \"argon_threads\": " << argon_threads << ", \"argon_time\": " << argon_time <<", \"difficulty\": \"" << difficulty
<< "\", \"block\": \"" << block << "\", \"height\": " << height << ", \"public_key\": \"" << public_key
<< "\", \"limit\": " << limit << " }, \"coin\": \"arionum\" }";

return ss.str();
}
};

struct ariopool_submit_result : public ariopool_result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "../common/common.h"
#include "../common/cfgpath.h"
#include "../http/simplejson/json.h"
#include "simplejson/json.h"

#include "pool_settings_provider.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ARIOMINER_DEVFEE_CONFIG_H

#include "../app/arguments.h"
#include "../http/http.h"
#include "http.h"

struct pool_settings {
string wallet;
Expand Down
2 changes: 1 addition & 1 deletion miner/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ string miner::get_status() {
ss << "{ \"type\": \"" << (*h)->get_type() << "\", \"subtype\": \"" << (*h)->get_subtype() << "\", \"devices\": [ ";
map<int, device_info> devices = (*h)->get_device_infos();
for(map<int, device_info>::iterator d = devices.begin(); d != devices.end();) {
ss << "{ \"id\": " << d->first << ", \"bus_id\": \"" << d->second.bus_id << "\", \"name\": \"" << d->second.name << "\", \"cblocks_intensity\": " << d->second.cblocks_intensity <<
ss << "{ \"id\": \"" << d->first << "\", \"bus_id\": \"" << d->second.bus_id << "\", \"name\": \"" << d->second.name << "\", \"cblocks_intensity\": " << d->second.cblocks_intensity <<
", \"gblocks_intensity\": " << d->second.gblocks_intensity << ", \"cblocks_hashrate\": " << d->second.cblock_hashrate <<
", \"gblocks_hashrate\": " << d->second.gblock_hashrate << " }";
if((++d) != devices.end())
Expand Down
19 changes: 19 additions & 0 deletions proxy/index_html.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by Haifa Bogdan Adnan on 20/02/2019.
//

#include "../common/common.h"

#include "index_html.h"

string index_html = R"HTML(
<html>
<head>
<title>Test Page for ProxyReporting</title>
</head>
<body>
<h2>Test header</h2>
<p>test data</p>
</body>
</html>
)HTML";
10 changes: 10 additions & 0 deletions proxy/index_html.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by Haifa Bogdan Adnan on 20/02/2019.
//

#ifndef ARIOMINER_INDEX_HTML_H
#define ARIOMINER_INDEX_HTML_H

extern string index_html;

#endif //ARIOMINER_INDEX_HTML_H
117 changes: 114 additions & 3 deletions proxy/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,124 @@
#include "../common/common.h"

#include "../app/arguments.h"
#include "../http/client.h"

#include "proxy.h"
#include "proxy_server.h"

proxy::proxy(arguments &args) { }
proxy::proxy(arguments &args) : __args(args), __client(args, NULL) {
__running = false;
}

proxy::~proxy() { }

void proxy::run() { }
void proxy::run() {
__running = true;
uint64_t last_update = 0;

void proxy::stop() { }
if(__args.proxy_port() > 0) {
vector<string> options;
options.push_back("listening_ports");
options.push_back(to_string(__args.proxy_port()));
LOG("Starting proxy server on port " + to_string(__args.proxy_port()) + ".");
proxy_server server(options, *this, __args);
LOG("Waiting for clients...");
while(__running) {
if (microseconds() - last_update > __args.update_interval()) {
__update_pool_data();
last_update = microseconds();
}

this_thread::sleep_for(chrono::milliseconds(100));
}
}
else {
LOG("Proxy port not set, exiting.");
__running = false;
}
}

void proxy::stop() {
__running = false;
}

bool proxy::__update_pool_data() {
double hash_rate_cblocks = 0;
double hash_rate_gblocks = 0;

time_t timestamp = time(NULL);

__miner_clients_lock.lock();
for(map<string, miner_client>::iterator iter=__miner_clients.begin(); iter != __miner_clients.end(); iter++) {
if(timestamp - iter->second.timestamp < 1200) {
hash_rate_cblocks += iter->second.cblocks_hashrate;
hash_rate_gblocks += iter->second.gblocks_hashrate;
}
}
__miner_clients_lock.unlock();

ariopool_update_result new_settings = __client.update(hash_rate_cblocks, hash_rate_gblocks);

bool changed = false;
if(new_settings.success) {
__pool_block_settings_lock.lock();
changed = __pool_block_settings.update(new_settings);
__pool_block_settings_lock.unlock();
}

if(changed && __args.is_verbose()) {
stringstream ss;
ss << "-----------------------------------------------------------------------------------------------------------------------------------------" << endl;
ss << "--> Pool data updated Block: " << new_settings.block << endl;
ss << "--> " << ((new_settings.argon2profile == "1_1_524288") ? "CPU round" : (new_settings.recommendation == "pause" ? "Masternode round" : "GPU round"));
ss << " Height: " << new_settings.height << " Limit: " << new_settings.limit << " Difficulty: " << new_settings.difficulty << " Proxy: " << __args.name() << endl;
ss << "-----------------------------------------------------------------------------------------------------------------------------------------";

LOG(ss.str());

return true;
}

return false;
}

string proxy::process_info_request(const string &ip, const string &miner_id, const string &miner_name, double cblocks_hashrate, double gblocks_hashrate) {
string miner_key = miner_id + "_" + miner_name;

__miner_clients_lock.lock();
if(__miner_clients.find(miner_key) == __miner_clients.end()) {
__miner_clients.insert(make_pair(miner_key, miner_client()));
LOG("New client from " + ip + " id: " + miner_id + " worker: " + miner_name);
}
miner_client &client = __miner_clients[miner_key];
client.worker_name = miner_name;
if(cblocks_hashrate > 0)
client.cblocks_hashrate = cblocks_hashrate;
if(gblocks_hashrate > 0)
client.gblocks_hashrate = gblocks_hashrate;
client.timestamp = time(NULL);
__miner_clients_lock.unlock();

__pool_block_settings_lock.lock();
string response = __pool_block_settings.response();
__pool_block_settings_lock.unlock();

return response;
}

string proxy::process_submit_request(const string &ip, const string &miner_id, const string &miner_name, const string &argon, const string &nonce, const string &public_key) {
string hash;

__pool_block_settings_lock.lock();
int height = __pool_block_settings.height;
__pool_block_settings_lock.unlock();

if(height%2)
hash = "$argon2i$v=19$m=524288,t=1,p=1" + argon;
else
hash = "$argon2i$v=19$m=16384,t=4,p=4" + argon;

ariopool_submit_result result = __client.submit(hash, nonce, public_key);

return result.pool_response;
}
31 changes: 31 additions & 0 deletions proxy/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
#define PROJECT_PROXY_H

#include "../app/runner.h"
#include "../app/arguments.h"
#include "../http/client.h"

struct miner_client {
miner_client() {
cblocks_hashrate = 0;
gblocks_hashrate = 0;
timestamp = 0;
}
string worker_name;
double cblocks_hashrate;
double gblocks_hashrate;
time_t timestamp;
};

class proxy : public runner {
public:
Expand All @@ -14,6 +28,23 @@ class proxy : public runner {

virtual void run();
virtual void stop();

string process_info_request(const string &ip, const string &miner_id, const string &miner_name, double cblocks_hashrate, double gblocks_hashrate);
string process_submit_request(const string &ip, const string &miner_id, const string &miner_name, const string &argon, const string &nonce, const string &public_key);

private:
bool __update_pool_data();

mutex __pool_block_settings_lock;
ariopool_update_result __pool_block_settings;

mutex __miner_clients_lock;
map<string, miner_client> __miner_clients;

arguments &__args;
bool __running;

ariopool_client __client;
};


Expand Down
Loading

0 comments on commit 2bd9260

Please sign in to comment.