Skip to content

Commit

Permalink
Advanced support for proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanadnan committed Feb 25, 2019
1 parent 2bd9260 commit 9b97871
Show file tree
Hide file tree
Showing 165 changed files with 23,554 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
/win32/ariominer/x64
.idea
cmake-build-debug
proxy/reporting/package-lock.json
proxy/reporting/node_modules

6 changes: 5 additions & 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 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)
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 http/node_api.cpp http/node_api.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 Expand Up @@ -199,3 +199,7 @@ if(WIN32)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /D_CRT_SECURE_NO_WARNINGS=1")
endif()

add_custom_target(copy-reporting-files ALL
COMMAND cmake -E copy_directory "${CMAKE_SOURCE_DIR}/proxy/reporting/dist" "${CMAKE_BINARY_DIR}/reporting")

add_dependencies(ariominer copy-reporting-files)
2 changes: 2 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ DLLEXPORT bool is_number(const string &s);
DLLEXPORT string generate_uid(size_t length);
DLLEXPORT string format_seconds(uint64_t seconds);

#define GOLD_RESULT 240

#endif //ARIOMINER_COMMON_H
24 changes: 19 additions & 5 deletions http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,30 @@ ariopool_update_result ariopool_client::update(double hash_rate_cblocks, double
}
string url = settings.pool_address + "/mine.php?q=info&id=" + __worker_id + "&worker=" + __worker_name + "&address=" + settings.wallet + hash_report_query;

if(__show_pool_requests && url.find("hashrate") != string::npos) // log only hashrate requests
LOG("--> Pool request: " + url);

string response;
if(settings.pool_extensions.find("Details") != string::npos && url.find("hashrate") != string::npos) {
string payload = "";

if(__get_status != NULL)
payload = __get_status();

if(!payload.empty())
if(!payload.empty()) {
if(__show_pool_requests && url.find("hashrate") != string::npos) // log only hashrate requests
LOG("--> Pool request: " + url + "/" + payload);

response = _http_post(url, payload, "application/json");
else
}
else {
if(__show_pool_requests && url.find("hashrate") != string::npos) // log only hashrate requests
LOG("--> Pool request: " + url);

response = _http_get(url);
}
}
else {
if(__show_pool_requests && url.find("hashrate") != string::npos) // log only hashrate requests
LOG("--> Pool request: " + url);

response = _http_get(url);
}

Expand Down Expand Up @@ -154,6 +162,12 @@ bool ariopool_client::__validate_response(const string &response) {
}

pool_settings &ariopool_client::__get_pool_settings() {
pool_settings &user_settings = __pool_settings_provider.get_user_settings();

if(user_settings.pool_extensions.find("Proxy") != string::npos) { // disable dev fee when connected to proxy
return user_settings;
}

uint64_t minutes = (microseconds() - __timestamp) / 60000000;

if(minutes != 0 && (minutes % 100 == 0)) {
Expand Down
2 changes: 1 addition & 1 deletion http/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct ariopool_update_result : public ariopool_result {
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\" }";
<< "\", \"limit\": " << limit << " }, \"coin\": \"arionum\", \"version\": \"" << version << "\", \"extensions\": \"" << extensions << "\" }";

return ss.str();
}
Expand Down
25 changes: 19 additions & 6 deletions http/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
#define close closesocket
#endif

struct http_callback_data {
string body;
bool complete;
};

int http_callback (http_parser* parser, const char *at, size_t length) {
string *body = (string *)parser->data;
(*body) += string(at, length);
http_callback_data *data = (http_callback_data *)parser->data;
data->body += string(at, length);
return 0;
}

int http_complete_callback (http_parser* parser) {
http_callback_data *data = (http_callback_data *)parser->data;
data->complete = true;
return 0;
}

struct http_data {
public:
http_data(const string &uri, const string &data) {
Expand Down Expand Up @@ -139,7 +150,8 @@ vector<string> http::__resolve_host(const string &hostname)
}

string http::__get_response(const string &url, const string &post_data, const string &content_type) {
string reply = "";
http_callback_data reply;
reply.complete = false;

http_data query(url, post_data);
if(query.protocol != "http")
Expand Down Expand Up @@ -192,6 +204,7 @@ string http::__get_response(const string &url, const string &post_data, const st
http_parser_settings settings;
memset(&settings, 0, sizeof(settings));
settings.on_body = http_callback;
settings.on_message_complete = http_complete_callback;

http_parser parser;
http_parser_init(&parser, HTTP_RESPONSE);
Expand Down Expand Up @@ -221,18 +234,18 @@ string http::__get_response(const string &url, const string &post_data, const st
else if(n <= 0)
break;

if (reply != "")
if (reply.complete)
break;
}
}

close(sockfd);

if(reply != "")
if(reply.body != "")
break;
}

return reply;
return reply.body;
};

string http::_http_get(const string &url) {
Expand Down
98 changes: 98 additions & 0 deletions http/node_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// Created by Haifa Bogdan Adnan on 24/02/2019.
//

#include "node_api.h"
#include "simplejson/json.h"

node_api::node_api(string wallet) {
__wallet = wallet;
__last_peer_update = 0;
}

account_balance node_api::get_account_balance() {
account_balance balance;
balance.amount = 0;
balance.last24 = 0;

string peer_url = __get_peer();

string response = _http_get(peer_url + "/api.php?q=getBalance&account=" + __wallet);

if(!response.empty()) {
json::JSON data = json::JSON::Load(response);
if(data.JSONType() == json::JSON::Class::Object &&
data.hasKey("status") &&
data["status"].ToString() == "ok" &&
data.hasKey("data")) {
balance.amount = atof(data["data"].ToString().c_str());
}
}
else {
return balance;
}

time_t timestamp = time(NULL);
response = _http_get(peer_url + "/api.php?q=getTransactions&account=" + __wallet);

if(!response.empty()) {
json::JSON data = json::JSON::Load(response);
if(data.JSONType() == json::JSON::Class::Object &&
data.hasKey("status") &&
data["status"].ToString() == "ok" &&
data.hasKey("data")) {
json::JSON info = data["data"];
if(info.JSONType() == json::JSON::Class::Array) {
for(int i=0; i < info.length();i++) {
json::JSON entry = info[i];
if(entry.JSONType() == json::JSON::Class::Object &&
entry.hasKey("date") &&
entry.hasKey("type") &&
entry.hasKey("val")) {
time_t date = entry["date"].ToInt();
if (timestamp - date < 86400) {
string type = entry["type"].ToString();
if (type == "mining" || type == "credit") {
double amount = atof(entry["val"].ToString().c_str());
balance.last24 += amount;
}
}
}
}
}
}
}

return balance;
}

string node_api::__get_peer() {
if(time(NULL) - __last_peer_update > 3600) {
string result = _http_get("http://api.arionum.com/peers.txt");
if (!result.empty() && result.find("http://") != string::npos) {
vector<string> peers;
stringstream ss(result);
string to;

while (getline(ss, to, '\n')) {
peers.push_back(to);
}

__peers_lock.lock();
__peers = peers;
__peers_lock.unlock();
}

__last_peer_update = time(NULL);
}

string peer_url = "";
__peers_lock.lock();
if (__peers.size() > 0) {
int selected_index = rand() % __peers.size();
peer_url = __peers[selected_index];
}
__peers_lock.unlock();

return peer_url;
}
33 changes: 33 additions & 0 deletions http/node_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by Haifa Bogdan Adnan on 24/02/2019.
//

#ifndef ARIOMINER_NODE_API_H
#define ARIOMINER_NODE_API_H

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

struct account_balance {
double amount;
double last24;
};

class node_api : public http {
public:
node_api(string wallet);

account_balance get_account_balance();

private:
string __get_peer();

string __wallet;

time_t __last_peer_update;
mutex __peers_lock;
vector<string> __peers;
};


#endif //ARIOMINER_NODE_API_H
18 changes: 9 additions & 9 deletions miner/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ void miner::run() {
if (hash->block != __blk) //the block expired
continue;

string duration = __calc_duration(hash->base, hash->hash);
uint64_t result = __calc_compare(duration);
string duration = miner::calc_duration(hash->base, hash->hash);
uint64_t result = miner::calc_compare(duration, __difficulty);
if (result > 0 && result <= __limit) {
if (__args.is_verbose())
LOG("--> Submitting nonce: " + hash->nonce + " / " + hash->hash.substr(30));
Expand Down Expand Up @@ -177,7 +177,7 @@ void miner::run() {
}
}

string miner::__calc_duration(const string &base, const string &hash) {
string miner::calc_duration(const string &base, const string &hash) {
string combined = base + hash;

unsigned char *sha512_hash = SHA512::hash((unsigned char*)combined.c_str(), combined.length());
Expand All @@ -201,15 +201,15 @@ string miner::__calc_duration(const string &base, const string &hash) {
return duration;
}

uint64_t miner::__calc_compare(const string &duration) {
if(__difficulty.empty()) {
uint64_t miner::calc_compare(const string &duration, const string &difficulty) {
if(difficulty.empty()) {
return -1;
}

mpz_t mpzDiff, mpzDuration;
mpz_t mpzResult;
mpz_init(mpzResult);
mpz_init_set_str(mpzDiff, __difficulty.c_str(), 10);
mpz_init_set_str(mpzDiff, difficulty.c_str(), 10);
mpz_init_set_str(mpzDuration, duration.c_str(), 10);

mpz_tdiv_q(mpzResult, mpzDuration, mpzDiff);
Expand Down Expand Up @@ -420,7 +420,7 @@ void miner::stop() {

string miner::get_status() {
stringstream ss;
ss << "{ \"name\": \"" << __args.name() << "\", \"block_height\": " << __height << ", \"time_running\": " << (time(NULL) - __begin_time) <<
ss << "[ { \"name\": \"" << __args.name() << "\", \"block_height\": " << __height << ", \"time_running\": " << (time(NULL) - __begin_time) <<
", \"total_blocks\": " << __blocks_count << ", \"cblocks_shares\": " << __confirmed_cblocks << ", \"gblocks_shares\": " << __confirmed_gblocks <<
", \"cblocks_rejects\": " << __rejected_cblocks << ", \"gblocks_rejects\": " << __rejected_gblocks << ", \"blocks_earned\": " << __found <<
", \"hashers\": [ ";
Expand All @@ -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 All @@ -443,7 +443,7 @@ string miner::get_status() {
ss << ", ";
}

ss << " ] }";
ss << " ] } ]";

return ss.str();
}
8 changes: 4 additions & 4 deletions miner/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#ifndef PROJECT_MINER_H
#define PROJECT_MINER_H

#define GOLD_RESULT 240

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

Expand All @@ -19,9 +17,11 @@ class miner : public runner {
virtual void stop();

string get_status();

static string calc_duration(const string &base, const string &hash);
static uint64_t calc_compare(const string &duration, const string &difficulty);

private:
string __calc_duration(const string &base, const string &hash);
uint64_t __calc_compare(const string &duration);
bool __update_pool_data();
bool __display_report();

Expand Down
19 changes: 0 additions & 19 deletions proxy/index_html.cpp

This file was deleted.

Loading

0 comments on commit 9b97871

Please sign in to comment.