GET_BLOCK_HEADER_BY_HEIGHT

This commit is contained in:
Sean Darcy 2021-11-11 11:16:40 +11:00
parent 32179ddd3e
commit 129e6f204c
6 changed files with 42 additions and 48 deletions

View File

@ -1760,15 +1760,10 @@ namespace cryptonote::rpc {
return;
}
//------------------------------------------------------------------------------------------------------------------------------
GET_BLOCK_HEADER_BY_HEIGHT::response core_rpc_server::invoke(GET_BLOCK_HEADER_BY_HEIGHT::request&& req, rpc_context context)
void core_rpc_server::invoke(GET_BLOCK_HEADER_BY_HEIGHT& get_block_header_by_height, rpc_context context)
{
GET_BLOCK_HEADER_BY_HEIGHT::response res{};
PERF_TIMER(on_get_block_header_by_height);
if (use_bootstrap_daemon_if_necessary<GET_BLOCK_HEADER_BY_HEIGHT>(req, res))
return res;
auto get = [this, curr_height=m_core.get_current_blockchain_height(), pow=req.fill_pow_hash && context.admin, tx_hashes=req.get_tx_hashes]
auto get = [this, curr_height=m_core.get_current_blockchain_height(), pow=get_block_header_by_height.request.fill_pow_hash && context.admin, tx_hashes=get_block_header_by_height.request.get_tx_hashes]
(uint64_t height, block_header_response& bhr) {
if (height >= curr_height)
throw rpc_error{ERROR_TOO_BIG_HEIGHT,
@ -1780,15 +1775,22 @@ namespace cryptonote::rpc {
fill_block_header_response(blk, false, height, get_block_hash(blk), bhr, pow, tx_hashes);
};
if (req.height)
get(*req.height, res.block_header.emplace());
if (!req.heights.empty())
res.block_headers.reserve(req.heights.size());
for (auto height : req.heights)
get(height, res.block_headers.emplace_back());
res.status = STATUS_OK;
return res;
block_header_response header;
if (get_block_header_by_height.request.height)
{
get(*get_block_header_by_height.request.height, header);
get_block_header_by_height.response["block_header"] = header;
}
std::vector<block_header_response> headers;
if (!get_block_header_by_height.request.heights.empty())
headers.reserve(get_block_header_by_height.request.heights.size());
for (auto height : get_block_header_by_height.request.heights)
get(height, headers.emplace_back());
get_block_header_by_height.response["status"] = STATUS_OK;
get_block_header_by_height.response["block_headers"] = headers;
return;
}
//------------------------------------------------------------------------------------------------------------------------------
GET_BLOCK::response core_rpc_server::invoke(GET_BLOCK::request&& req, rpc_context context)

View File

@ -255,6 +255,7 @@ namespace cryptonote::rpc {
void invoke(GET_SERVICE_NODE_BLACKLISTED_KEY_IMAGES& get_service_node_blacklisted_key_images, rpc_context context);
void invoke(RELAY_TX& relay_tx, rpc_context context);
void invoke(GET_BLOCK_HEADERS_RANGE& get_block_headers_range, rpc_context context);
void invoke(GET_BLOCK_HEADER_BY_HEIGHT& get_block_header_by_height, rpc_context context);
// Deprecated Monero NIH binary endpoints:
GET_ALT_BLOCKS_HASHES_BIN::response invoke(GET_ALT_BLOCKS_HASHES_BIN::request&& req, rpc_context context);
@ -270,7 +271,6 @@ namespace cryptonote::rpc {
// FIXME: unconverted JSON RPC endpoints:
SET_BOOTSTRAP_DAEMON::response invoke(SET_BOOTSTRAP_DAEMON::request&& req, rpc_context context);
GET_OUTPUT_DISTRIBUTION::response invoke(GET_OUTPUT_DISTRIBUTION::request&& req, rpc_context context, bool binary = false);
GET_BLOCK_HEADER_BY_HEIGHT::response invoke(GET_BLOCK_HEADER_BY_HEIGHT::request&& req, rpc_context context);
GET_BLOCK::response invoke(GET_BLOCK::request&& req, rpc_context context);
GET_OUTPUT_HISTOGRAM::response invoke(GET_OUTPUT_HISTOGRAM::request&& req, rpc_context context);
GET_ALTERNATE_CHAINS::response invoke(GET_ALTERNATE_CHAINS::request&& req, rpc_context context);

View File

@ -496,4 +496,11 @@ namespace cryptonote::rpc {
get_values(in, "fill_pow_hash", get_block_headers_range.request.fill_pow_hash);
get_values(in, "get_tx_hashes", get_block_headers_range.request.get_tx_hashes);
}
void parse_request(GET_BLOCK_HEADER_BY_HEIGHT& get_block_header_by_height, rpc_input in) {
get_values(in, "height", get_block_header_by_height.request.height);
get_values(in, "heights", get_block_header_by_height.request.heights);
get_values(in, "fill_pow_hash", get_block_header_by_height.request.fill_pow_hash);
get_values(in, "get_tx_hashes", get_block_header_by_height.request.get_tx_hashes);
}
}

View File

@ -42,4 +42,5 @@ namespace cryptonote::rpc {
void parse_request(SETBANS& set_bans, rpc_input in);
void parse_request(GET_STAKING_REQUIREMENT& get_staking_requirement, rpc_input in);
void parse_request(GET_BLOCK_HEADERS_RANGE& get_block_headers_range, rpc_input in);
void parse_request(GET_BLOCK_HEADER_BY_HEIGHT& get_block_header_by_height, rpc_input in);
}

View File

@ -103,22 +103,6 @@ KV_SERIALIZE_MAP_CODE_BEGIN(block_header_response)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(GET_BLOCK_HEADER_BY_HEIGHT::request)
KV_SERIALIZE(height)
KV_SERIALIZE(heights)
KV_SERIALIZE_OPT(fill_pow_hash, false);
KV_SERIALIZE_OPT(get_tx_hashes, false);
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(GET_BLOCK_HEADER_BY_HEIGHT::response)
KV_SERIALIZE(block_header)
KV_SERIALIZE(block_headers)
KV_SERIALIZE(status)
KV_SERIALIZE(untrusted)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(GET_BLOCK::request)
KV_SERIALIZE(hash)
KV_SERIALIZE(height)

View File

@ -819,31 +819,31 @@ namespace cryptonote::rpc {
} request;
};
OXEN_RPC_DOC_INTROSPECT
// Similar to get_block_header_by_hash above, this method includes a block's height as an input parameter to retrieve basic information about the block.
/// Similar to get_block_header_by_hash above, this method includes a block's height as an input parameter to retrieve basic information about the block.
///
/// Inputs:
///
/// - \p height A block height to look up; returned in `block_header`
/// - \p heights Block heights to retrieve; returned in `block_headers`
/// - \p fill_pow_hash Tell the daemon if it should fill out pow_hash field.
/// - \p get_tx_hashes If true (default false) then include the hashes of non-coinbase transactions
///
/// Output values available from a public RPC endpoint:
///
/// - \p status General RPC status string. `"OK"` means everything looks good.
/// - \p block_header Block header information for the requested `height` block
/// - \p block_headers Block header information for the requested `heights` blocks
struct GET_BLOCK_HEADER_BY_HEIGHT : PUBLIC
{
static constexpr auto names() { return NAMES("get_block_header_by_height", "getblockheaderbyheight"); }
struct request
struct request_parameters
{
std::optional<uint64_t> height; // A block height to look up; returned in `block_header`
std::vector<uint64_t> heights; // Block heights to retrieve; returned in `block_headers`
bool fill_pow_hash; // Tell the daemon if it should fill out pow_hash field.
bool get_tx_hashes; // If true (default false) then include the hashes of non-coinbase transactions
KV_MAP_SERIALIZABLE
};
struct response
{
std::string status; // General RPC error code. "OK" means everything looks good.
std::optional<block_header_response> block_header; // Block header information for the requested `height` block
std::vector<block_header_response> block_headers; // Block header information for the requested `heights` blocks
bool untrusted; // States if the result is obtained using the bootstrap mode, and is therefore not trusted (`true`), or when the daemon is fully synced (`false`).
KV_MAP_SERIALIZABLE
};
} request;
};
OXEN_RPC_DOC_INTROSPECT