Commit dd0f2571 authored by Pierre Kim's avatar Pierre Kim

Add RPC API 'f_blocks_list_json'

parent a64a9973
......@@ -28,6 +28,22 @@ struct STATUS_STRUCT {
}
};
struct f_block_short_response {
uint64_t timestamp;
uint32_t height;
std::string hash;
uint64_t tx_count;
uint64_t cumul_size;
void serialize(ISerializer &s) {
KV_MEMBER(timestamp)
KV_MEMBER(height)
KV_MEMBER(hash)
KV_MEMBER(cumul_size)
KV_MEMBER(tx_count)
}
};
struct COMMAND_RPC_GET_HEIGHT {
typedef EMPTY_STRUCT request;
......@@ -427,6 +443,26 @@ struct COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT {
typedef BLOCK_HEADER_RESPONSE response;
};
struct COMMAND_RPC_BLOCKS_LIST_JSON {
struct request {
uint64_t height;
void serialize(ISerializer &s) {
KV_MEMBER(height)
}
};
struct response {
std::vector<f_block_short_response> blocks; //transactions blobs as hex
std::string status;
void serialize(ISerializer &s) {
KV_MEMBER(blocks)
KV_MEMBER(status)
}
};
};
struct COMMAND_RPC_QUERY_BLOCKS {
struct request {
std::vector<Crypto::Hash> block_ids; //*first 10 blocks id goes sequential, next goes in pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */
......
......@@ -133,6 +133,7 @@ bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse&
jsonResponse.setId(jsonRequest.getId()); // copy id
static std::unordered_map<std::string, RpcServer::RpcHandler<JsonMemberMethod>> jsonRpcHandlers = {
{ "f_blocks_list_json",{ makeMemberMethod(&RpcServer::f_on_blocks_list_json), false } },
{ "getblockcount", { makeMemberMethod(&RpcServer::on_getblockcount), true } },
{ "on_getblockhash", { makeMemberMethod(&RpcServer::on_getblockhash), false } },
{ "getblocktemplate", { makeMemberMethod(&RpcServer::on_getblocktemplate), false } },
......@@ -648,6 +649,48 @@ bool RpcServer::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER
return true;
}
bool RpcServer::f_on_blocks_list_json(const COMMAND_RPC_BLOCKS_LIST_JSON::request& req, COMMAND_RPC_BLOCKS_LIST_JSON::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()) };
}
uint32_t print_blocks_count = 30;
uint32_t last_height = req.height - print_blocks_count;
if (req.height <= print_blocks_count) {
last_height = 0;
}
for (uint32_t i = req.height; i >= last_height; i--) {
Hash block_hash = m_core.getBlockIdByHeight(static_cast<uint32_t>(i));
Block blk;
if (!m_core.getBlockByHash(block_hash, blk)) {
throw JsonRpc::JsonRpcError{
CORE_RPC_ERROR_CODE_INTERNAL_ERROR,
"Internal error: can't get block by height. Height = " + std::to_string(i) + '.' };
}
//BlockDetails blkDetails = m_core.getBlockDetails(block_hash);
size_t blkSize;
m_core.getBlockSize(block_hash, blkSize);
f_block_short_response block_short;
block_short.cumul_size = blkSize;
block_short.timestamp = blk.timestamp;
block_short.height = i;
block_short.hash = Common::podToHex(block_hash);
block_short.tx_count = blk.transactionHashes.size() + 1;
res.blocks.push_back(block_short);
if (i == 0)
break;
}
res.status = CORE_RPC_STATUS_OK;
return true;
}
bool RpcServer::enableCors(const std::vector<std::string> domains) {
m_cors_domains = domains;
return true;
......
......@@ -64,6 +64,7 @@ private:
bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res);
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);
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);
bool f_on_blocks_list_json(const COMMAND_RPC_BLOCKS_LIST_JSON::request& req, COMMAND_RPC_BLOCKS_LIST_JSON::response& res);
void fill_block_header_response(const Block& blk, bool orphan_status, uint64_t height, const Crypto::Hash& hash, block_header_response& responce);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment