Skip to content

Commit

Permalink
Make core_rpc_server::on_get_info thread safe + ez concurrency wins
Browse files Browse the repository at this point in the history
- grab an lmdb read transaction guard to ensure the thread executing on_get_info reads consistent data from the db
+ other low hanging fruit
  • Loading branch information
j-berman committed Sep 12, 2021
1 parent 665bd89 commit d26c121
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ namespace cryptonote

CHECK_PAYMENT_MIN1(req, res, COST_PER_GET_INFO, false);

// ensures current thread's db reads execute in a single lmdb txn to make function safe from concurrent writes
db_rtxn_guard rtxn_guard(&m_core.get_blockchain_storage().get_db());

const bool restricted = m_restricted && ctx;

crypto::hash top_hash;
Expand Down Expand Up @@ -502,6 +505,8 @@ namespace cryptonote
res.busy_syncing = m_p2p.get_payload_object().is_busy_syncing();
res.synchronized = check_core_ready();

rtxn_guard.stop();

res.status = CORE_RPC_STATUS_OK;
return true;
}
Expand Down Expand Up @@ -559,7 +564,7 @@ namespace cryptonote
if (last_block_hash == req.block_ids.front())
{
res.start_height = 0;
res.current_height = m_core.get_current_blockchain_height();
res.current_height = last_block_height + 1;
res.status = CORE_RPC_STATUS_OK;
return true;
}
Expand Down Expand Up @@ -1653,10 +1658,11 @@ namespace cryptonote
return false;
}
uint64_t h = req[0];
if(m_core.get_current_blockchain_height() <= h)
uint64_t blockchain_height = m_core.get_current_blockchain_height();
if(blockchain_height <= h)
{
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
error_resp.message = std::string("Requested block height: ") + std::to_string(h) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
error_resp.message = std::string("Requested block height: ") + std::to_string(h) + " greater than current top block height: " + std::to_string(blockchain_height - 1);
}
res = string_tools::pod_to_hex(m_core.get_block_id_by_height(h));
return true;
Expand Down Expand Up @@ -2245,10 +2251,11 @@ namespace cryptonote
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT>(invoke_http_mode::JON_RPC, "getblockheaderbyheight", req, res, r))
return r;

if(m_core.get_current_blockchain_height() <= req.height)
uint64_t blockchain_height = m_core.get_current_blockchain_height();
if(blockchain_height <= req.height)
{
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(blockchain_height - 1);
return false;
}
CHECK_PAYMENT_MIN1(req, res, COST_PER_BLOCK_HEADER, false);
Expand Down Expand Up @@ -2295,10 +2302,11 @@ namespace cryptonote
}
else
{
if(m_core.get_current_blockchain_height() <= req.height)
uint64_t blockchain_height = m_core.get_current_blockchain_height();
if(blockchain_height <= req.height)
{
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(blockchain_height - 1);
return false;
}
block_hash = m_core.get_block_id_by_height(req.height);
Expand Down

0 comments on commit d26c121

Please sign in to comment.