Merge pull request #431

253ed76 core_rpc_server: add optional json decoded tx to COMMAND_RPC_GET_TRANSACTIONS (moneromooo-monero)
ca23c0e core_rpc_server: add a getblock RPC command, and fix print_block (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-10-15 19:34:51 +02:00
commit eef5fa446e
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
4 changed files with 110 additions and 8 deletions

View File

@ -408,8 +408,8 @@ bool t_rpc_command_executor::print_height() {
} }
bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) { bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) {
cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request req; cryptonote::COMMAND_RPC_GET_BLOCK::request req;
cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response res; cryptonote::COMMAND_RPC_GET_BLOCK::response res;
epee::json_rpc::error error_resp; epee::json_rpc::error error_resp;
req.hash = epee::string_tools::pod_to_hex(block_hash); req.hash = epee::string_tools::pod_to_hex(block_hash);
@ -418,14 +418,14 @@ bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) {
if (m_is_rpc) if (m_is_rpc)
{ {
if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyhash", fail_message.c_str())) if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str()))
{ {
return true; return true;
} }
} }
else else
{ {
if (!m_rpc_server->on_get_block_header_by_hash(req, res, error_resp)) if (!m_rpc_server->on_get_block(req, res, error_resp))
{ {
tools::fail_msg_writer() << fail_message.c_str(); tools::fail_msg_writer() << fail_message.c_str();
return true; return true;
@ -433,13 +433,14 @@ bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) {
} }
print_block_header(res.block_header); print_block_header(res.block_header);
tools::success_msg_writer() << res.json << ENDL;
return true; return true;
} }
bool t_rpc_command_executor::print_block_by_height(uint64_t height) { bool t_rpc_command_executor::print_block_by_height(uint64_t height) {
cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req; cryptonote::COMMAND_RPC_GET_BLOCK::request req;
cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res; cryptonote::COMMAND_RPC_GET_BLOCK::response res;
epee::json_rpc::error error_resp; epee::json_rpc::error error_resp;
req.height = height; req.height = height;
@ -448,14 +449,14 @@ bool t_rpc_command_executor::print_block_by_height(uint64_t height) {
if (m_is_rpc) if (m_is_rpc)
{ {
if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyheight", fail_message.c_str())) if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str()))
{ {
return true; return true;
} }
} }
else else
{ {
if (!m_rpc_server->on_get_block_header_by_height(req, res, error_resp)) if (!m_rpc_server->on_get_block(req, res, error_resp))
{ {
tools::fail_msg_writer() << fail_message.c_str(); tools::fail_msg_writer() << fail_message.c_str();
return true; return true;
@ -463,6 +464,7 @@ bool t_rpc_command_executor::print_block_by_height(uint64_t height) {
} }
print_block_header(res.block_header); print_block_header(res.block_header);
tools::success_msg_writer() << res.json << ENDL;
return true; return true;
} }

View File

@ -253,6 +253,8 @@ namespace cryptonote
{ {
blobdata blob = t_serializable_object_to_blob(tx); blobdata blob = t_serializable_object_to_blob(tx);
res.txs_as_hex.push_back(string_tools::buff_to_hex_nodelimer(blob)); res.txs_as_hex.push_back(string_tools::buff_to_hex_nodelimer(blob));
if (req.decode_as_json)
res.txs_as_json.push_back(obj_to_json_str(tx));
} }
BOOST_FOREACH(const auto& miss_tx, missed_txs) BOOST_FOREACH(const auto& miss_tx, missed_txs)
@ -772,6 +774,66 @@ namespace cryptonote
return true; return true;
} }
//------------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp){
if(!check_core_busy())
{
error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
error_resp.message = "Core is busy.";
return false;
}
crypto::hash block_hash;
if (!req.hash.empty())
{
bool hash_parsed = parse_hash256(req.hash, block_hash);
if(!hash_parsed)
{
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
error_resp.message = "Failed to parse hex representation of block hash. Hex = " + req.hash + '.';
return false;
}
}
else
{
if(m_core.get_current_blockchain_height() <= req.height)
{
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
error_resp.message = std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height());
return false;
}
block_hash = m_core.get_block_id_by_height(req.height);
}
block blk;
bool have_block = m_core.get_block_by_hash(block_hash, blk);
if (!have_block)
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: can't get block by hash. Hash = " + req.hash + '.';
return false;
}
if (blk.miner_tx.vin.front().type() != typeid(txin_gen))
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: coinbase transaction in the block has the wrong type";
return false;
}
uint64_t block_height = boost::get<txin_gen>(blk.miner_tx.vin.front()).height;
bool responce_filled = fill_block_header_responce(blk, false, block_height, block_hash, res.block_header);
if (!responce_filled)
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: can't produce valid response.";
return false;
}
for (size_t n = 0; n < blk.tx_hashes.size(); ++n)
{
res.tx_hashes.push_back(epee::string_tools::pod_to_hex(blk.tx_hashes[n]));
}
res.blob = t_serializable_object_to_blob(blk);
res.json = obj_to_json_str(blk);
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp) bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp)
{ {
if(!check_core_busy()) if(!check_core_busy())

View File

@ -101,6 +101,7 @@ namespace cryptonote
MAP_JON_RPC_WE("getlastblockheader", on_get_last_block_header, COMMAND_RPC_GET_LAST_BLOCK_HEADER) MAP_JON_RPC_WE("getlastblockheader", on_get_last_block_header, COMMAND_RPC_GET_LAST_BLOCK_HEADER)
MAP_JON_RPC_WE("getblockheaderbyhash", on_get_block_header_by_hash, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH) MAP_JON_RPC_WE("getblockheaderbyhash", on_get_block_header_by_hash, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH)
MAP_JON_RPC_WE("getblockheaderbyheight", on_get_block_header_by_height, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT) MAP_JON_RPC_WE("getblockheaderbyheight", on_get_block_header_by_height, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT)
MAP_JON_RPC_WE("getblock", on_get_block, COMMAND_RPC_GET_BLOCK)
MAP_JON_RPC_WE("get_connections", on_get_connections, COMMAND_RPC_GET_CONNECTIONS) MAP_JON_RPC_WE("get_connections", on_get_connections, COMMAND_RPC_GET_CONNECTIONS)
MAP_JON_RPC_WE("get_info", on_get_info_json, COMMAND_RPC_GET_INFO) MAP_JON_RPC_WE("get_info", on_get_info_json, COMMAND_RPC_GET_INFO)
END_JSON_RPC_MAP() END_JSON_RPC_MAP()
@ -136,6 +137,7 @@ namespace cryptonote
bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp); bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp);
bool 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, epee::json_rpc::error& error_resp); bool 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, epee::json_rpc::error& error_resp);
bool 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, epee::json_rpc::error& error_resp); bool 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, epee::json_rpc::error& error_resp);
bool on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp);
bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp); bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp);
bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp); bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp);
bool on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp); bool on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp);

View File

@ -95,9 +95,11 @@ namespace cryptonote
struct request struct request
{ {
std::list<std::string> txs_hashes; std::list<std::string> txs_hashes;
bool decode_as_json;
BEGIN_KV_SERIALIZE_MAP() BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(txs_hashes) KV_SERIALIZE(txs_hashes)
KV_SERIALIZE(decode_as_json)
END_KV_SERIALIZE_MAP() END_KV_SERIALIZE_MAP()
}; };
@ -106,11 +108,13 @@ namespace cryptonote
{ {
std::list<std::string> txs_as_hex; //transactions blobs as hex std::list<std::string> txs_as_hex; //transactions blobs as hex
std::list<std::string> missed_tx; //not found transactions std::list<std::string> missed_tx; //not found transactions
std::list<std::string> txs_as_json; //transactions decoded as json
std::string status; std::string status;
BEGIN_KV_SERIALIZE_MAP() BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(txs_as_hex) KV_SERIALIZE(txs_as_hex)
KV_SERIALIZE(missed_tx) KV_SERIALIZE(missed_tx)
KV_SERIALIZE(txs_as_json)
KV_SERIALIZE(status) KV_SERIALIZE(status)
END_KV_SERIALIZE_MAP() END_KV_SERIALIZE_MAP()
}; };
@ -536,6 +540,38 @@ namespace cryptonote
}; };
struct COMMAND_RPC_GET_BLOCK
{
struct request
{
std::string hash;
uint64_t height;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(hash)
KV_SERIALIZE(height)
END_KV_SERIALIZE_MAP()
};
struct response
{
std::string status;
block_header_responce block_header;
std::vector<std::string> tx_hashes;
std::string blob;
std::string json;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(block_header)
KV_SERIALIZE(tx_hashes)
KV_SERIALIZE(status)
KV_SERIALIZE(blob)
KV_SERIALIZE(json)
END_KV_SERIALIZE_MAP()
};
};
struct peer { struct peer {
uint64_t id; uint64_t id;
uint32_t ip; uint32_t ip;