Added submitblock and commented Json_rpc_http_server.

This commit is contained in:
Oran Juice 2014-11-10 20:03:16 +05:30
parent 43286b46e5
commit f3ac82b28e
No known key found for this signature in database
GPG Key ID: 71C5AF46CCB28124
3 changed files with 135 additions and 11 deletions

View File

@ -582,6 +582,60 @@ namespace RPC
return response.length(); return response.length();
} }
int submitblock(char *buf, int len, struct ns_rpc_request *req)
{
CHECK_CORE_BUSY();
if (req->params == NULL)
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_params,
"Parameters missing.", "{}");
}
rapidjson::Document request_json;
char request_buf[1000];
strncpy(request_buf, req->params[0].ptr, req->params[0].len);
request_buf[req->params[0].len] = '\0';
if (request_json.Parse(request_buf).HasParseError())
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::parse_error,
"Invalid JSON passed", "{}");
}
if (!request_json.HasMember("block") || !request_json["block"].IsString())
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_params,
"Incorrect block", "{}");
}
std::string string_blockblob = request_json["block"].GetString();
cryptonote::blobdata blockblob;
if (!string_tools::parse_hexstr_to_binbuff(string_blockblob, blockblob))
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_request,
"Incorrect block", "{}");
}
// Fixing of high orphan issue for most pools
cryptonote::block b = AUTO_VAL_INIT(b);
if (!parse_and_validate_block_from_blob(blockblob, b))
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_request,
"Wrong block blob", "{}");
}
// Fix from Boolberry neglects to check block
// size, do that with the function below
if (!core->check_incoming_block_size(blockblob))
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_request,
"Block blob size is too big, rejecting block", "{}");
}
if (!core->handle_block_found(b))
{
return ns_rpc_create_error(buf, len, req, RPC::Json_rpc_http_server::invalid_request,
"Block not accepted.", "{}");
}
return ns_rpc_create_reply(buf, len, req, "{s:s}", "status", CORE_RPC_STATUS_OK);
}
const char *method_names[] = { const char *method_names[] = {
"getheight", "getheight",
"getblocks", "getblocks",
@ -592,6 +646,7 @@ namespace RPC
"getblockcount", "getblockcount",
"getblockhash", "getblockhash",
"getblocktemplate", "getblocktemplate",
"submitblock",
NULL NULL
}; };
@ -605,6 +660,7 @@ namespace RPC
getblockcount, getblockcount,
getblockhash, getblockhash,
getblocktemplate, getblocktemplate,
submitblock,
NULL NULL
}; };

View File

@ -1,7 +1,16 @@
/*!
* \file json_rpc_http_server.h
* \brief Header for Json_rpc_http_server class
*/
#include "json_rpc_http_server.h" #include "json_rpc_http_server.h"
#include <iostream> #include <iostream>
/*!
* \namespace RPC
* \brief RPC related utilities
*/
namespace RPC namespace RPC
{ {
int Json_rpc_http_server::parse_error = -32700; int Json_rpc_http_server::parse_error = -32700;
@ -9,6 +18,12 @@ namespace RPC
int Json_rpc_http_server::invalid_params = -32602; int Json_rpc_http_server::invalid_params = -32602;
int Json_rpc_http_server::internal_error = -32603; int Json_rpc_http_server::internal_error = -32603;
/**
* \brief Constructor
* \param ip IP address to bind
* \param port Port number to bind
* \param ev_handler Event handler function pointer
*/
Json_rpc_http_server::Json_rpc_http_server(const std::string &ip, const std::string &port, Json_rpc_http_server::Json_rpc_http_server(const std::string &ip, const std::string &port,
void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data)) void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data))
{ {
@ -18,11 +33,18 @@ namespace RPC
m_ev_handler = ev_handler; m_ev_handler = ev_handler;
} }
/**
* \brief Destructor
*/
Json_rpc_http_server::~Json_rpc_http_server() Json_rpc_http_server::~Json_rpc_http_server()
{ {
stop(); stop();
} }
/*!
* \brief Starts the server
* \return True if start was successful
*/
bool Json_rpc_http_server::start() bool Json_rpc_http_server::start()
{ {
m_is_running = true; m_is_running = true;
@ -33,17 +55,25 @@ namespace RPC
return false; return false;
} }
ns_set_protocol_http_websocket(nc); ns_set_protocol_http_websocket(nc);
// Start a new thread so it doesn't block.
server_thread = new boost::thread(&Json_rpc_http_server::poll, this); server_thread = new boost::thread(&Json_rpc_http_server::poll, this);
return true; return true;
} }
/*!
* \brief Repeated loops processing requests if any.
*/
void Json_rpc_http_server::poll() void Json_rpc_http_server::poll()
{ {
// Loop until the server is running and poll.
while (m_is_running) { while (m_is_running) {
ns_mgr_poll(&mgr, 1000); ns_mgr_poll(&mgr, 1000);
} }
} }
/*!
* \brief Stops the server
*/
void Json_rpc_http_server::stop() void Json_rpc_http_server::stop()
{ {
m_is_running = false; m_is_running = false;

View File

@ -1,3 +1,8 @@
/*!
* \file json_rpc_http_server.h
* \brief Header for Json_rpc_http_server class
*/
#ifndef JSON_RPC_HTTP_SERVER_H #ifndef JSON_RPC_HTTP_SERVER_H
#define JSON_RPC_HTTP_SERVER_H #define JSON_RPC_HTTP_SERVER_H
@ -5,29 +10,62 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <string> #include <string>
/*!
* \namespace RPC
* \brief RPC related utilities
*/
namespace RPC namespace RPC
{ {
/*!
* \class Json_rpc_http_server
* \brief JSON HTTP RPC Server implemented with net_skeleton (aka fossa).
*
* Provides a higher level interface to C-like net_skeleton.
*/
class Json_rpc_http_server class Json_rpc_http_server
{ {
struct ns_mgr mgr; struct ns_mgr mgr; /*!< Connection manager */
struct ns_connection *nc; struct ns_connection *nc; /*!< Connection pointer */
boost::thread *server_thread; boost::thread *server_thread; /*!< Server runs on this thread */
/*!
* \brief Repeated loops processing requests if any.
*/
void poll(); void poll();
std::string m_ip; std::string m_ip; /*!< IP address where its listening */
std::string m_port; std::string m_port; /*!< Port where its listening */
bool m_is_running; bool m_is_running; /*!< Whether the server is currently running */
void (*m_ev_handler)(struct ns_connection *nc, int ev, void *ev_data); void (*m_ev_handler)(struct ns_connection *nc, int ev, void *ev_data); /*!< Server event handler function pointer */
public: public:
/**
* \brief Constructor
* \param ip IP address to bind
* \param port Port number to bind
* \param ev_handler Event handler function pointer
*/
Json_rpc_http_server(const std::string &ip, const std::string &port, Json_rpc_http_server(const std::string &ip, const std::string &port,
void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data)); void (*ev_handler)(struct ns_connection *nc, int ev, void *ev_data));
/**
* \brief Destructor
*/
~Json_rpc_http_server(); ~Json_rpc_http_server();
/*!
* \brief Starts the server
* \return True if start was successful
*/
bool start(); bool start();
/*!
* \brief Stops the server
*/
void stop(); void stop();
static int parse_error; static int parse_error; /*!< JSON request passed couldn't be parsed */
static int invalid_request; static int invalid_request; /*!< JSON request invalid */
static int invalid_params; static int invalid_params; /*!< JSON request had faulty/missing params */
static int internal_error; static int internal_error; /*!< JSON request resulted in an internal error */
}; };
} }