diff --git a/src/base/base.cmake b/src/base/base.cmake index c17183167..dcc104951 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -2,6 +2,7 @@ set(HEADERS_BASE src/base/io/Console.h src/base/io/json/Json.h src/base/io/json/JsonChain.h + src/base/io/json/JsonRequest.h src/base/io/log/backends/ConsoleLog.h src/base/io/log/backends/FileLog.h src/base/io/log/Log.h @@ -53,14 +54,15 @@ set(SOURCES_BASE src/base/io/Console.cpp src/base/io/json/Json.cpp src/base/io/json/JsonChain.cpp + src/base/io/json/JsonRequest.cpp src/base/io/log/backends/ConsoleLog.cpp src/base/io/log/backends/FileLog.cpp src/base/io/log/Log.cpp src/base/io/Watcher.cpp + src/base/kernel/Base.cpp src/base/kernel/config/BaseConfig.cpp src/base/kernel/config/BaseTransform.cpp src/base/kernel/Entry.cpp - src/base/kernel/Base.cpp src/base/kernel/Process.cpp src/base/kernel/Signals.cpp src/base/net/dns/Dns.cpp diff --git a/src/base/io/json/JsonRequest.cpp b/src/base/io/json/JsonRequest.cpp new file mode 100644 index 000000000..4556f3f08 --- /dev/null +++ b/src/base/io/json/JsonRequest.cpp @@ -0,0 +1,38 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/io/json/JsonRequest.h" +#include "rapidjson/document.h" + + +void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms) +{ + auto &allocator = doc.GetAllocator(); + + doc.AddMember("id", id, allocator); + doc.AddMember("jsonrpc", "2.0", allocator); + doc.AddMember("method", rapidjson::StringRef(method), allocator); + doc.AddMember("params", params, allocator); +} diff --git a/src/base/io/json/JsonRequest.h b/src/base/io/json/JsonRequest.h new file mode 100644 index 000000000..e98c0baec --- /dev/null +++ b/src/base/io/json/JsonRequest.h @@ -0,0 +1,45 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_JSONREQUEST_H +#define XMRIG_JSONREQUEST_H + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class JsonRequest +{ +public: + static void create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_JSONREQUEST_H */ diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index df6f4837e..1d448ddff 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -37,6 +37,7 @@ #endif +#include "base/io/json/JsonRequest.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IClientListener.h" #include "base/net/dns/Dns.h" @@ -167,10 +168,6 @@ int64_t xmrig::Client::submit(const JobResult &result) Document doc(kObjectType); auto &allocator = doc.GetAllocator(); - doc.AddMember("id", m_sequence, allocator); - doc.AddMember("jsonrpc", "2.0", allocator); - doc.AddMember("method", "submit", allocator); - Value params(kObjectType); params.AddMember("id", StringRef(m_rpcId.data()), allocator); params.AddMember("job_id", StringRef(result.jobId.data()), allocator); @@ -181,7 +178,7 @@ int64_t xmrig::Client::submit(const JobResult &result) params.AddMember("algo", StringRef(result.algorithm.shortName()), allocator); } - doc.AddMember("params", params, allocator); + JsonRequest::create(doc, m_sequence, "submit", params); # ifdef XMRIG_PROXY_PROJECT m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff(), result.id); @@ -584,14 +581,10 @@ void xmrig::Client::login() Document doc(kObjectType); auto &allocator = doc.GetAllocator(); - doc.AddMember("id", 1, allocator); - doc.AddMember("jsonrpc", "2.0", allocator); - doc.AddMember("method", "login", allocator); - Value params(kObjectType); params.AddMember("login", m_pool.user().toJSON(), allocator); params.AddMember("pass", m_pool.password().toJSON(), allocator); - params.AddMember("agent", StringRef(m_agent), allocator); + params.AddMember("agent", StringRef(m_agent), allocator); if (!m_pool.rigId().isNull()) { params.AddMember("rigid", m_pool.rigId().toJSON(), allocator); @@ -612,7 +605,7 @@ void xmrig::Client::login() m_listener->onLogin(this, doc, params); - doc.AddMember("params", params, allocator); + JsonRequest::create(doc, 1, "login", params); send(doc); } diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index 0b2a2ec18..b4492e7e3 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -30,6 +30,7 @@ #include "3rdparty/http-parser/http_parser.h" #include "base/io/json/Json.h" +#include "base/io/json/JsonRequest.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IClientListener.h" #include "base/net/http/HttpClient.h" @@ -96,16 +97,11 @@ int64_t xmrig::DaemonClient::submit(const JobResult &result) using namespace rapidjson; Document doc(kObjectType); - auto &allocator = doc.GetAllocator(); - - doc.AddMember("id", m_sequence, allocator); - doc.AddMember("jsonrpc", "2.0", allocator); - doc.AddMember("method", "submitblock", allocator); Value params(kArrayType); - params.PushBack(m_blocktemplate.toJSON(), allocator); + params.PushBack(m_blocktemplate.toJSON(), doc.GetAllocator()); - doc.AddMember("params", params, allocator); + JsonRequest::create(doc, m_sequence, "submitblock", params); # ifdef XMRIG_PROXY_PROJECT m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff(), result.id); @@ -251,15 +247,11 @@ int64_t xmrig::DaemonClient::getBlockTemplate() Document doc(kObjectType); auto &allocator = doc.GetAllocator(); - doc.AddMember("id", m_sequence, allocator); - doc.AddMember("jsonrpc", "2.0", allocator); - doc.AddMember("method", "getblocktemplate", allocator); - Value params(kObjectType); params.AddMember("wallet_address", m_pool.user().toJSON(), allocator); params.AddMember("reserve_size", 8, allocator); - doc.AddMember("params", params, allocator); + JsonRequest::create(doc, m_sequence, "getblocktemplate", params); send(HTTP_POST, "/json_rpc", doc);