jpsock changes

This commit is contained in:
Unknown
2017-11-14 15:57:19 +00:00
parent 288363814f
commit 8130f656b6
3 changed files with 78 additions and 9 deletions
+1 -1
View File
@@ -193,7 +193,7 @@ const std::string jconf::GetCurrency()
bool jconf::IsCurrencyMonero()
{
if(xmrstak::strcmp_i(::jconf::inst()->GetCurrency(), "monero"))
if(xmrstak::strcmp_i(GetCurrency(), "monero"))
{
return true;
}
+65 -7
View File
@@ -23,6 +23,7 @@
#include <stdarg.h>
#include <assert.h>
#include <algorithm>
#include "jpsock.hpp"
#include "socks.hpp"
@@ -33,9 +34,6 @@
#include "xmrstak/misc/jext.hpp"
#include "xmrstak/version.hpp"
#define AGENTID_STR XMR_STAK_NAME "/" XMR_STAK_VERSION
using namespace rapidjson;
struct jpsock::call_rsp
@@ -366,10 +364,11 @@ bool jpsock::process_pool_job(const opq_json_val* params)
if (!params->val->IsObject())
return set_socket_error("PARSE error: Job error 1");
const Value * blob, *jobid, *target;
const Value *blob, *jobid, *target, *motd;
jobid = GetObjectMember(*params->val, "job_id");
blob = GetObjectMember(*params->val, "blob");
target = GetObjectMember(*params->val, "target");
motd = GetObjectMember(*params->val, "motd");
if (jobid == nullptr || blob == nullptr || target == nullptr ||
!jobid->IsString() || !blob->IsString() || !target->IsString())
@@ -415,6 +414,12 @@ bool jpsock::process_pool_job(const opq_json_val* params)
else
return set_socket_error("PARSE error: Job error 5");
if(motd != nullptr && motd->IsString())
{
std::unique_lock<std::mutex>(motd_mutex);
pool_motd.assign(motd->GetString());
}
iJobDiff = t64_to_diff(oPoolJob.iTarget);
executor::inst()->push_event(ex_event(oPoolJob, pool_id));
@@ -426,6 +431,7 @@ bool jpsock::process_pool_job(const opq_json_val* params)
bool jpsock::connect(std::string& sConnectError)
{
ext_algo = ext_backend = ext_hashcount = ext_motd = false;
bHaveSocketError = false;
sSocketError.clear();
iJobDiff = 0;
@@ -527,6 +533,7 @@ bool jpsock::cmd_login()
const Value* id = GetObjectMember(*oResult.val, "id");
const Value* job = GetObjectMember(*oResult.val, "job");
const Value* ext = GetObjectMember(*oResult.val, "extensions");
if (id == nullptr || job == nullptr || !id->IsString())
{
@@ -545,6 +552,29 @@ bool jpsock::cmd_login()
memset(sMinerId, 0, sizeof(sMinerId));
memcpy(sMinerId, id->GetString(), id->GetStringLength());
if(ext != nullptr && ext->IsArray())
{
for(size_t i=0; i < ext->Size(); i++)
{
const Value& jextname = ext->GetArray()[i];
if(!jextname.IsString())
continue;
std::string tmp(jextname.GetString());
std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
if(tmp.compare("algo"))
ext_algo = true;
else if(tmp.compare("backend"))
ext_backend = true;
else if(tmp.compare("hashcount"))
ext_hashcount = true;
else if(tmp.compare("motd"))
ext_motd = true;
}
}
opq_json_val v(job);
if(!process_pool_job(&v))
{
@@ -558,20 +588,33 @@ bool jpsock::cmd_login()
return true;
}
bool jpsock::cmd_submit(const char* sJobId, uint32_t iNonce, const uint8_t* bResult)
bool jpsock::cmd_submit(const char* sJobId, uint32_t iNonce, const uint8_t* bResult, xmrstak::iBackend* bend, bool algo_full_cn)
{
char cmd_buffer[1024];
char sNonce[9];
char sResult[65];
/*Extensions*/
char sAlgo[64] = {0};
char sBackend[64] = {0};
char sHashcount[64] = {0};
if(ext_backend)
snprintf(sBackend, sizeof(sBackend), ",\"backend\":\"%s\"", "");
if(ext_hashcount)
snprintf(sHashcount, sizeof(sHashcount), ",\"hashcount\":\"%llu\"", int_port(bend->iHashCount.load(std::memory_order_relaxed)));
if(ext_algo)
snprintf(sAlgo, sizeof(sAlgo), ",\"algo\":\"%s\"", algo_full_cn ? "cryptonight" : "cryptonight-lite");
bin2hex((unsigned char*)&iNonce, 4, sNonce);
sNonce[8] = '\0';
bin2hex(bResult, 32, sResult);
sResult[64] = '\0';
snprintf(cmd_buffer, sizeof(cmd_buffer), "{\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"},\"id\":1}\n",
sMinerId, sJobId, sNonce, sResult);
snprintf(cmd_buffer, sizeof(cmd_buffer), "{\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"%s%s%s},\"id\":1}\n",
sMinerId, sJobId, sNonce, sResult, sBackend, sHashcount, sAlgo);
opq_json_val oResult(nullptr);
return cmd_ret_wait(cmd_buffer, oResult);
@@ -594,6 +637,21 @@ bool jpsock::get_current_job(pool_job& job)
return true;
}
bool jpsock::get_pool_motd(std::string& strin)
{
if(!ext_motd)
return false;
std::unique_lock<std::mutex>(motd_mutex);
if(pool_motd.size() > 0)
{
strin.assign(pool_motd);
return true;
}
return false;
}
inline unsigned char hf_hex2bin(char c, bool &err)
{
if (c >= '0' && c <= '9')
+12 -1
View File
@@ -1,5 +1,6 @@
#pragma once
#include "xmrstak/backend/iBackend.hpp"
#include "msgstruct.hpp"
#include <mutex>
@@ -33,7 +34,7 @@ public:
void disconnect(bool quiet = false);
bool cmd_login();
bool cmd_submit(const char* sJobId, uint32_t iNonce, const uint8_t* bResult);
bool cmd_submit(const char* sJobId, uint32_t iNonce, const uint8_t* bResult, xmrstak::iBackend* bend, bool algo_full_cn);
static bool hex2bin(const char* in, unsigned int len, unsigned char* out);
static void bin2hex(const unsigned char* in, unsigned int len, char* out);
@@ -58,6 +59,8 @@ public:
inline const char* get_tls_fp() { return tls_fp.c_str(); }
inline bool is_nicehash() { return nicehash; }
bool get_pool_motd(std::string& strin);
std::string&& get_call_error();
bool have_sock_error() { return bHaveSocketError; }
@@ -87,6 +90,14 @@ private:
bool pool;
bool nicehash;
bool ext_algo = false;
bool ext_backend = false;
bool ext_hashcount = false;
bool ext_motd = false;
std::string pool_motd;
std::mutex motd_mutex;
size_t connect_time = 0;
std::atomic<size_t> connect_attempts;
std::atomic<size_t> disconnect_time;