607 lines
22 KiB
C++
607 lines
22 KiB
C++
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
||
|
//
|
||
|
// This file is part of Bytecoin.
|
||
|
//
|
||
|
// Bytecoin is free software: you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// Bytecoin 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 Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
#include "RpcServer.h"
|
||
|
|
||
|
#include <future>
|
||
|
#include <unordered_map>
|
||
|
|
||
|
// CryptoNote
|
||
|
#include "cryptonote_core/cryptonote_core.h"
|
||
|
#include "cryptonote_core/miner.h"
|
||
|
#include "p2p/net_node.h"
|
||
|
|
||
|
#include "core_rpc_server_error_codes.h"
|
||
|
#include "JsonRpc.h"
|
||
|
|
||
|
#undef ERROR
|
||
|
|
||
|
using namespace Logging;
|
||
|
|
||
|
namespace CryptoNote {
|
||
|
|
||
|
namespace {
|
||
|
|
||
|
template <typename Command>
|
||
|
RpcServer::HandlerFunction binMethod(bool (RpcServer::*handler)(typename Command::request const&, typename Command::response&)) {
|
||
|
return [handler](RpcServer* obj, const HttpRequest& request, HttpResponse& response) {
|
||
|
|
||
|
boost::value_initialized<typename Command::request> req;
|
||
|
boost::value_initialized<typename Command::response> res;
|
||
|
|
||
|
if (!epee::serialization::load_t_from_binary(static_cast<typename Command::request&>(req), request.getBody())) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool result = (obj->*handler)(req, res);
|
||
|
response.setBody(epee::serialization::store_t_to_binary(res.data()));
|
||
|
return result;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
template <typename Command>
|
||
|
RpcServer::HandlerFunction jsonMethod(bool (RpcServer::*handler)(typename Command::request const&, typename Command::response&)) {
|
||
|
return [handler](RpcServer* obj, const HttpRequest& request, HttpResponse& response) {
|
||
|
|
||
|
boost::value_initialized<typename Command::request> req;
|
||
|
boost::value_initialized<typename Command::response> res;
|
||
|
|
||
|
if (!epee::serialization::load_t_from_json(static_cast<typename Command::request&>(req), request.getBody())) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool result = (obj->*handler)(req, res);
|
||
|
response.setBody(epee::serialization::store_t_to_json(res.data()));
|
||
|
return result;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
std::unordered_map<std::string, RpcServer::HandlerFunction> RpcServer::s_handlers = {
|
||
|
|
||
|
// binary handlers
|
||
|
{ "/getblocks.bin", binMethod<COMMAND_RPC_GET_BLOCKS_FAST>(&RpcServer::on_get_blocks) },
|
||
|
{ "/queryblocks.bin", binMethod<COMMAND_RPC_QUERY_BLOCKS>(&RpcServer::on_query_blocks) },
|
||
|
{ "/get_o_indexes.bin", binMethod<COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES>(&RpcServer::on_get_indexes) },
|
||
|
{ "/getrandom_outs.bin", binMethod<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS>(&RpcServer::on_get_random_outs) },
|
||
|
|
||
|
// json handlers
|
||
|
{ "/getinfo", jsonMethod<COMMAND_RPC_GET_INFO>(&RpcServer::on_get_info) },
|
||
|
{ "/getheight", jsonMethod<COMMAND_RPC_GET_HEIGHT>(&RpcServer::on_get_height) },
|
||
|
{ "/gettransactions", jsonMethod<COMMAND_RPC_GET_TRANSACTIONS>(&RpcServer::on_get_transactions)},
|
||
|
{ "/sendrawtransaction", jsonMethod<COMMAND_RPC_SEND_RAW_TX>(&RpcServer::on_send_raw_tx) },
|
||
|
{ "/start_mining", jsonMethod<COMMAND_RPC_START_MINING>(&RpcServer::on_start_mining) },
|
||
|
{ "/stop_mining", jsonMethod<COMMAND_RPC_STOP_MINING>(&RpcServer::on_stop_mining) },
|
||
|
{ "/stop_daemon", jsonMethod<COMMAND_RPC_STOP_DAEMON>(&RpcServer::on_stop_daemon) },
|
||
|
|
||
|
// json rpc
|
||
|
{ "/json_rpc", std::bind(&RpcServer::processJsonRpcRequest, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) }
|
||
|
};
|
||
|
|
||
|
RpcServer::RpcServer(System::Dispatcher& dispatcher, Logging::ILogger& log, core& c, node_server& p2p) :
|
||
|
HttpServer(dispatcher, log), logger(log, "RpcServer"), m_core(c), m_p2p(p2p) {
|
||
|
}
|
||
|
|
||
|
void RpcServer::processRequest(const HttpRequest& request, HttpResponse& response) {
|
||
|
auto url = request.getUrl();
|
||
|
|
||
|
auto it = s_handlers.find(url);
|
||
|
if (it == s_handlers.end()) {
|
||
|
response.setStatus(HttpResponse::STATUS_404);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (url != "/json_rpc" && !checkCoreReady()) {
|
||
|
response.setStatus(HttpResponse::STATUS_500);
|
||
|
response.setBody("Core is busy");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
it->second(this, request, response);
|
||
|
}
|
||
|
|
||
|
bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse& response) {
|
||
|
|
||
|
using namespace JsonRpc;
|
||
|
|
||
|
response.addHeader("Content-Type", "application/json");
|
||
|
|
||
|
JsonRpcRequest jsonRequest;
|
||
|
JsonRpcResponse jsonResponse;
|
||
|
|
||
|
try {
|
||
|
|
||
|
jsonRequest.parseRequest(request.getBody());
|
||
|
jsonResponse.setId(jsonRequest.getId()); // copy id
|
||
|
|
||
|
static std::unordered_map<std::string, JsonMemberMethod> jsonRpcHandlers = {
|
||
|
{ "getblockcount", makeMemberMethod(&RpcServer::on_getblockcount) },
|
||
|
{ "on_getblockhash", makeMemberMethod(&RpcServer::on_getblockhash) },
|
||
|
{ "getblocktemplate", makeMemberMethod(&RpcServer::on_getblocktemplate) },
|
||
|
{ "getcurrencyid", makeMemberMethod(&RpcServer::on_get_currency_id) },
|
||
|
{ "submitblock", makeMemberMethod(&RpcServer::on_submitblock) },
|
||
|
{ "getlastblockheader", makeMemberMethod(&RpcServer::on_get_last_block_header) },
|
||
|
{ "getblockheaderbyhash", makeMemberMethod(&RpcServer::on_get_block_header_by_hash) },
|
||
|
{ "getblockheaderbyheight", makeMemberMethod(&RpcServer::on_get_block_header_by_height) }
|
||
|
};
|
||
|
|
||
|
auto it = jsonRpcHandlers.find(jsonRequest.getMethod());
|
||
|
if (it == jsonRpcHandlers.end()) {
|
||
|
throw JsonRpcError(JsonRpc::errMethodNotFound);
|
||
|
}
|
||
|
|
||
|
if (jsonRequest.getMethod() != "getcurrencyid" && !checkCoreReady()) {
|
||
|
throw JsonRpcError(CORE_RPC_ERROR_CODE_CORE_BUSY, "Core is busy");
|
||
|
}
|
||
|
|
||
|
it->second(this, jsonRequest, jsonResponse);
|
||
|
|
||
|
} catch (const JsonRpcError& err) {
|
||
|
jsonResponse.setError(err);
|
||
|
}
|
||
|
|
||
|
response.setBody(jsonResponse.getBody());
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
#define CHECK_CORE_READY()
|
||
|
|
||
|
bool RpcServer::checkCoreReady() {
|
||
|
return m_core.is_ready() && m_p2p.get_payload_object().is_synchronized();
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Binary handlers
|
||
|
//
|
||
|
|
||
|
bool RpcServer::on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res) {
|
||
|
|
||
|
std::list<std::pair<Block, std::list<Transaction>>> bs;
|
||
|
if (!m_core.find_blockchain_supplement(req.block_ids, bs, res.current_height, res.start_height, COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT)) {
|
||
|
res.status = "Failed";
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
for (auto& b : bs) {
|
||
|
res.blocks.resize(res.blocks.size() + 1);
|
||
|
res.blocks.back().block = block_to_blob(b.first);
|
||
|
for (auto& t : b.second) {
|
||
|
res.blocks.back().txs.push_back(tx_to_blob(t));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_query_blocks(const COMMAND_RPC_QUERY_BLOCKS::request& req, COMMAND_RPC_QUERY_BLOCKS::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
|
||
|
if (!m_core.queryBlocks(req.block_ids, req.timestamp, res.start_height, res.current_height, res.full_offset, res.items)) {
|
||
|
res.status = "Failed to perform query";
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
|
||
|
if (!m_core.get_tx_outputs_gindexs(req.txid, res.o_indexes)) {
|
||
|
res.status = "Failed";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
logger(TRACE) << "COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES: [" << res.o_indexes.size() << "]";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_random_outs(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
res.status = "Failed";
|
||
|
if (!m_core.get_random_outs_for_amounts(req, res)) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
|
||
|
std::stringstream ss;
|
||
|
typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount outs_for_amount;
|
||
|
typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry out_entry;
|
||
|
|
||
|
std::for_each(res.outs.begin(), res.outs.end(), [&](outs_for_amount& ofa) {
|
||
|
ss << "[" << ofa.amount << "]:";
|
||
|
|
||
|
assert(ofa.outs.size() && "internal error: ofa.outs.size() is empty");
|
||
|
|
||
|
std::for_each(ofa.outs.begin(), ofa.outs.end(), [&](out_entry& oe)
|
||
|
{
|
||
|
ss << oe.global_amount_index << " ";
|
||
|
});
|
||
|
ss << ENDL;
|
||
|
});
|
||
|
std::string s = ss.str();
|
||
|
logger(TRACE) << "COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS: " << ENDL << s;
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// JSON handlers
|
||
|
//
|
||
|
|
||
|
bool RpcServer::on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res) {
|
||
|
res.height = m_core.get_current_blockchain_height();
|
||
|
res.difficulty = m_core.get_blockchain_storage().get_difficulty_for_next_block();
|
||
|
res.tx_count = m_core.get_blockchain_storage().get_total_transactions() - res.height; //without coinbase
|
||
|
res.tx_pool_size = m_core.get_pool_transactions_count();
|
||
|
res.alt_blocks_count = m_core.get_blockchain_storage().get_alternative_blocks_count();
|
||
|
uint64_t total_conn = m_p2p.get_connections_count();
|
||
|
res.outgoing_connections_count = m_p2p.get_outgoing_connections_count();
|
||
|
res.incoming_connections_count = total_conn - res.outgoing_connections_count;
|
||
|
res.white_peerlist_size = m_p2p.get_peerlist_manager().get_white_peers_count();
|
||
|
res.grey_peerlist_size = m_p2p.get_peerlist_manager().get_gray_peers_count();
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
res.height = m_core.get_current_blockchain_height();
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
std::vector<crypto::hash> vh;
|
||
|
for (const auto& tx_hex_str : req.txs_hashes) {
|
||
|
blobdata b;
|
||
|
if (!hexToBlob(tx_hex_str, b))
|
||
|
{
|
||
|
res.status = "Failed to parse hex representation of transaction hash";
|
||
|
return true;
|
||
|
}
|
||
|
if (b.size() != sizeof(crypto::hash))
|
||
|
{
|
||
|
res.status = "Failed, size of data mismatch";
|
||
|
}
|
||
|
vh.push_back(*reinterpret_cast<const crypto::hash*>(b.data()));
|
||
|
}
|
||
|
std::list<crypto::hash> missed_txs;
|
||
|
std::list<Transaction> txs;
|
||
|
m_core.get_transactions(vh, txs, missed_txs);
|
||
|
|
||
|
for (auto& tx : txs) {
|
||
|
blobdata blob = t_serializable_object_to_blob(tx);
|
||
|
res.txs_as_hex.push_back(blobToHex(blob));
|
||
|
}
|
||
|
|
||
|
for (const auto& miss_tx : missed_txs) {
|
||
|
res.missed_tx.push_back(Common::podToHex(miss_tx));
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
|
||
|
std::string tx_blob;
|
||
|
if (!hexToBlob(req.tx_as_hex, tx_blob))
|
||
|
{
|
||
|
logger(INFO) << "[on_send_raw_tx]: Failed to parse tx from hexbuff: " << req.tx_as_hex;
|
||
|
res.status = "Failed";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
tx_verification_context tvc = AUTO_VAL_INIT(tvc);
|
||
|
if (!m_core.handle_incoming_tx(tx_blob, tvc, false))
|
||
|
{
|
||
|
logger(INFO) << "[on_send_raw_tx]: Failed to process tx";
|
||
|
res.status = "Failed";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (tvc.m_verifivation_failed)
|
||
|
{
|
||
|
logger(INFO) << "[on_send_raw_tx]: tx verification failed";
|
||
|
res.status = "Failed";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (!tvc.m_should_be_relayed)
|
||
|
{
|
||
|
logger(INFO) << "[on_send_raw_tx]: tx accepted, but not relayed";
|
||
|
res.status = "Not relayed";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
NOTIFY_NEW_TRANSACTIONS::request r;
|
||
|
r.txs.push_back(tx_blob);
|
||
|
m_core.get_protocol()->relay_transactions(r);
|
||
|
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
AccountPublicAddress adr;
|
||
|
if (!m_core.currency().parseAccountAddressString(req.miner_address, adr)) {
|
||
|
res.status = "Failed, wrong address";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
boost::thread::attributes attrs;
|
||
|
attrs.set_stack_size(THREAD_STACK_SIZE);
|
||
|
|
||
|
if (!m_core.get_miner().start(adr, static_cast<size_t>(req.threads_count), attrs)) {
|
||
|
res.status = "Failed, mining not started";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
if (!m_core.get_miner().stop()) {
|
||
|
res.status = "Failed, mining not stopped";
|
||
|
return true;
|
||
|
}
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMMAND_RPC_STOP_DAEMON::response& res) {
|
||
|
CHECK_CORE_READY();
|
||
|
if (m_core.currency().isTestnet()) {
|
||
|
m_p2p.send_stop_signal();
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
} else {
|
||
|
res.status = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
// JSON RPC methods
|
||
|
//------------------------------------------------------------------------------------------------------------------------------
|
||
|
bool RpcServer::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res) {
|
||
|
res.count = m_core.get_current_blockchain_height();
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res) {
|
||
|
if (req.size() != 1) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_WRONG_PARAM, "Wrong parameters, expected height" };
|
||
|
}
|
||
|
|
||
|
uint64_t h = req[0];
|
||
|
if (m_core.get_current_blockchain_height() <= h) {
|
||
|
throw JsonRpc::JsonRpcError{
|
||
|
CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT,
|
||
|
std::string("To big height: ") + std::to_string(h) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height())
|
||
|
};
|
||
|
}
|
||
|
|
||
|
res = Common::podToHex(m_core.get_block_id_by_height(h));
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
namespace {
|
||
|
uint64_t slow_memmem(void* start_buff, size_t buflen, void* pat, size_t patlen)
|
||
|
{
|
||
|
void* buf = start_buff;
|
||
|
void* end = (char*)buf + buflen - patlen;
|
||
|
while ((buf = memchr(buf, ((char*)pat)[0], buflen)))
|
||
|
{
|
||
|
if (buf>end)
|
||
|
return 0;
|
||
|
if (memcmp(buf, pat, patlen) == 0)
|
||
|
return (char*)buf - (char*)start_buff;
|
||
|
buf = (char*)buf + 1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res) {
|
||
|
if (req.reserve_size > TX_EXTRA_NONCE_MAX_COUNT) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE, "To big reserved size, maximum 255" };
|
||
|
}
|
||
|
|
||
|
CryptoNote::AccountPublicAddress acc = AUTO_VAL_INIT(acc);
|
||
|
|
||
|
if (!req.wallet_address.size() || !m_core.currency().parseAccountAddressString(req.wallet_address, acc)) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_WRONG_WALLET_ADDRESS, "Failed to parse wallet address" };
|
||
|
}
|
||
|
|
||
|
Block b = AUTO_VAL_INIT(b);
|
||
|
CryptoNote::blobdata blob_reserve;
|
||
|
blob_reserve.resize(req.reserve_size, 0);
|
||
|
if (!m_core.get_block_template(b, acc, res.difficulty, res.height, blob_reserve)) {
|
||
|
logger(ERROR) << "Failed to create block template";
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: failed to create block template" };
|
||
|
}
|
||
|
|
||
|
blobdata block_blob = t_serializable_object_to_blob(b);
|
||
|
crypto::public_key tx_pub_key = CryptoNote::get_tx_pub_key_from_extra(b.minerTx);
|
||
|
if (tx_pub_key == null_pkey) {
|
||
|
logger(ERROR) << "Failed to find tx pub key in coinbase extra";
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: failed to find tx pub key in coinbase extra" };
|
||
|
}
|
||
|
|
||
|
if (0 < req.reserve_size) {
|
||
|
res.reserved_offset = slow_memmem((void*)block_blob.data(), block_blob.size(), &tx_pub_key, sizeof(tx_pub_key));
|
||
|
if (!res.reserved_offset) {
|
||
|
logger(ERROR) << "Failed to find tx pub key in blockblob";
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: failed to create block template" };
|
||
|
}
|
||
|
res.reserved_offset += sizeof(tx_pub_key) + 3; //3 bytes: tag for TX_EXTRA_TAG_PUBKEY(1 byte), tag for TX_EXTRA_NONCE(1 byte), counter in TX_EXTRA_NONCE(1 byte)
|
||
|
if (res.reserved_offset + req.reserve_size > block_blob.size()) {
|
||
|
logger(ERROR) << "Failed to calculate offset for reserved bytes";
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: failed to create block template" };
|
||
|
}
|
||
|
} else {
|
||
|
res.reserved_offset = 0;
|
||
|
}
|
||
|
|
||
|
res.blocktemplate_blob = blobToHex(block_blob);
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_currency_id(const COMMAND_RPC_GET_CURRENCY_ID::request& /*req*/, COMMAND_RPC_GET_CURRENCY_ID::response& res) {
|
||
|
crypto::hash currencyId = m_core.currency().genesisBlockHash();
|
||
|
blobdata blob = t_serializable_object_to_blob(currencyId);
|
||
|
res.currency_id_blob = blobToHex(blob);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res) {
|
||
|
if (req.size() != 1) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_WRONG_PARAM, "Wrong param" };
|
||
|
}
|
||
|
|
||
|
blobdata blockblob;
|
||
|
if (!hexToBlob(req[0], blockblob)) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB, "Wrong block blob" };
|
||
|
}
|
||
|
|
||
|
CryptoNote::block_verification_context bvc = AUTO_VAL_INIT(bvc);
|
||
|
|
||
|
System::Event event(m_dispatcher);
|
||
|
auto resultFuture = std::async(std::launch::async, [this, &event, &bvc, &blockblob]{
|
||
|
m_core.handle_incoming_block_blob(blockblob, bvc, true, true);
|
||
|
m_dispatcher.remoteSpawn([&event]() { event.set(); });
|
||
|
});
|
||
|
|
||
|
event.wait();
|
||
|
resultFuture.get();
|
||
|
|
||
|
if (!bvc.m_added_to_main_chain) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_BLOCK_NOT_ACCEPTED, "Block not accepted" };
|
||
|
}
|
||
|
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
namespace {
|
||
|
uint64_t get_block_reward(const Block& blk) {
|
||
|
uint64_t reward = 0;
|
||
|
for (const TransactionOutput& out : blk.minerTx.vout) {
|
||
|
reward += out.amount;
|
||
|
}
|
||
|
return reward;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void RpcServer::fill_block_header_responce(const Block& blk, bool orphan_status, uint64_t height, const crypto::hash& hash, block_header_responce& responce) {
|
||
|
responce.major_version = blk.majorVersion;
|
||
|
responce.minor_version = blk.minorVersion;
|
||
|
responce.timestamp = blk.timestamp;
|
||
|
responce.prev_hash = Common::podToHex(blk.prevId);
|
||
|
responce.nonce = blk.nonce;
|
||
|
responce.orphan_status = orphan_status;
|
||
|
responce.height = height;
|
||
|
responce.depth = m_core.get_current_blockchain_height() - height - 1;
|
||
|
responce.hash = Common::podToHex(hash);
|
||
|
responce.difficulty = m_core.get_blockchain_storage().block_difficulty(height);
|
||
|
responce.reward = get_block_reward(blk);
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res) {
|
||
|
uint64_t last_block_height;
|
||
|
crypto::hash last_block_hash;
|
||
|
|
||
|
if (!m_core.get_blockchain_top(last_block_height, last_block_hash)) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: can't get last block hash." };
|
||
|
}
|
||
|
|
||
|
Block last_block;
|
||
|
if (!m_core.get_block_by_hash(last_block_hash, last_block)) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: can't get last block hash." };
|
||
|
}
|
||
|
|
||
|
fill_block_header_responce(last_block, false, last_block_height, last_block_hash, res.block_header);
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res) {
|
||
|
crypto::hash block_hash;
|
||
|
|
||
|
if (!parse_hash256(req.hash, block_hash)) {
|
||
|
throw JsonRpc::JsonRpcError{
|
||
|
CORE_RPC_ERROR_CODE_WRONG_PARAM,
|
||
|
"Failed to parse hex representation of block hash. Hex = " + req.hash + '.' };
|
||
|
}
|
||
|
|
||
|
Block blk;
|
||
|
if (!m_core.get_block_by_hash(block_hash, blk)) {
|
||
|
throw JsonRpc::JsonRpcError{
|
||
|
CORE_RPC_ERROR_CODE_INTERNAL_ERROR,
|
||
|
"Internal error: can't get block by hash. Hash = " + req.hash + '.' };
|
||
|
}
|
||
|
|
||
|
if (blk.minerTx.vin.front().type() != typeid(TransactionInputGenerate)) {
|
||
|
throw JsonRpc::JsonRpcError{
|
||
|
CORE_RPC_ERROR_CODE_INTERNAL_ERROR,
|
||
|
"Internal error: coinbase transaction in the block has the wrong type" };
|
||
|
}
|
||
|
|
||
|
uint64_t block_height = boost::get<TransactionInputGenerate>(blk.minerTx.vin.front()).height;
|
||
|
fill_block_header_responce(blk, false, block_height, block_hash, res.block_header);
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool RpcServer::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res) {
|
||
|
if (m_core.get_current_blockchain_height() <= req.height) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT,
|
||
|
std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height()) };
|
||
|
}
|
||
|
|
||
|
crypto::hash block_hash = m_core.get_block_id_by_height(req.height);
|
||
|
Block blk;
|
||
|
if (!m_core.get_block_by_hash(block_hash, blk)) {
|
||
|
throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR,
|
||
|
"Internal error: can't get block by height. Height = " + std::to_string(req.height) + '.' };
|
||
|
}
|
||
|
|
||
|
fill_block_header_responce(blk, false, req.height, block_hash, res.block_header);
|
||
|
res.status = CORE_RPC_STATUS_OK;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|