aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core
diff options
context:
space:
mode:
authorj-berman <justinberman@protonmail.com>2025-12-11 21:32:23 -0800
committerj-berman <justinberman@protonmail.com>2025-12-11 21:37:20 -0800
commitd8d3cf97300480b9d3942c667978dd4860b76feb (patch)
treeb9a58cca4fb8e9fd5576d1ef7b62415a119c803f /src/cryptonote_core
parentae08557f713a41f3f7267fcb08bbbe87f18d7932 (diff)
downloadmonzero-core-d8d3cf97300480b9d3942c667978dd4860b76feb.tar.gz
monzero-core-d8d3cf97300480b9d3942c667978dd4860b76feb.tar.xz
monzero-core-d8d3cf97300480b9d3942c667978dd4860b76feb.zip
p2p: fix race causing dropped connections during sync
Without this commit: 1) read height from DB 2) add block to chain in separate thread 3) read chain for block id's and request them from peer 4) ERR in handle_response_chain_entry, peer's first block is the one that was added to the chain, which has block idx=height from step 1. This commit reads the chain for height and highest block id's in one go while holding the m_blockchain_lock to avoid the race.
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r--src/cryptonote_core/blockchain.cpp4
-rw-r--r--src/cryptonote_core/blockchain.h3
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp4
-rw-r--r--src/cryptonote_core/cryptonote_core.h2
4 files changed, 7 insertions, 6 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index a6c2e3faf..3a34089f5 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -728,13 +728,13 @@ crypto::hash Blockchain::get_tail_id() const
* powers of 2 less recent from there, so 13, 17, 25, etc...
*
*/
-bool Blockchain::get_short_chain_history(std::list<crypto::hash>& ids) const
+bool Blockchain::get_short_chain_history(std::list<crypto::hash>& ids, uint64_t& current_height) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
uint64_t i = 0;
uint64_t current_multiplier = 1;
- uint64_t sz = m_db->height();
+ uint64_t sz = current_height = m_db->height();
if(!sz)
return true;
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index f16bc7415..0fd81f4ba 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -445,10 +445,11 @@ namespace cryptonote
* powers of 2 less recent from there, so 13, 17, 25, etc...
*
* @param ids return-by-reference list to put the resulting hashes in
+ * @param current_height the current blockchain height, return-by-reference
*
* @return true
*/
- bool get_short_chain_history(std::list<crypto::hash>& ids) const;
+ bool get_short_chain_history(std::list<crypto::hash>& ids, uint64_t& current_height) const;
/**
* @brief get recent block hashes for a foreign chain
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index 300fb2553..d30bc72a7 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -1536,9 +1536,9 @@ namespace cryptonote
return m_mempool.get_pool_for_rpc(tx_infos, key_image_infos);
}
//-----------------------------------------------------------------------------------------------
- bool core::get_short_chain_history(std::list<crypto::hash>& ids) const
+ bool core::get_short_chain_history(std::list<crypto::hash>& ids, uint64_t& current_height) const
{
- return m_blockchain_storage.get_short_chain_history(ids);
+ return m_blockchain_storage.get_short_chain_history(ids, current_height);
}
//-----------------------------------------------------------------------------------------------
bool core::handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NOTIFY_RESPONSE_GET_OBJECTS::request& rsp, cryptonote_connection_context& context)
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index 362060b54..ab9a0ee12 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -580,7 +580,7 @@ namespace cryptonote
*
* @note see Blockchain::get_short_chain_history
*/
- bool get_short_chain_history(std::list<crypto::hash>& ids) const;
+ bool get_short_chain_history(std::list<crypto::hash>& ids, uint64_t& current_height) const;
/**
* @copydoc Blockchain::find_blockchain_supplement(const std::list<crypto::hash>&, NOTIFY_RESPONSE_CHAIN_ENTRY::request&) const