What changed, and why it matters
This patch changes how Monero nodes decide when to switch back to a slower, safer database synchronization mode after a peer disconnects. Previously, the node could leave fast sync mode based on the highest reported blockchain height from any remaining peer, including peers on anonymous networks like Tor or I2P. Now, the node only uses publicly reachable peers to decide whether to re-enable safe sync. This reduces the chance that a single anonymous peer can trick a node into staying in fast sync, which could theoretically make certain denial-of-service or chain-manipulation attacks easier.
Review the new public_target condition for completeness and edge cases, run the new unit tests, and consider whether additional documentation or a security advisory is warranted if this addresses a known attack vector.
Security signals we found
Change in consensus-adjacent sync logic
Network-zone-aware access control (public vs hidden peers)
Safe sync mode re-enablement condition tightened
New unit tests explicitly model Tor/I2P vs public peer behavior
Potential mitigation against anonymous peer influencing sync mode
Evidence from the diff
The commit modifies on_connection_close in cryptonote_protocol_handler.inl to track two target heights: target (overall maximum among remaining synchronizing peers) and public_target (maximum among public-zone peers only). The decision to call m_core.safesyncmode(true) is now gated on public_target rather than the global target. The previous logic enabled safe sync whenever the target dropped close to the current height; the new logic enables safe sync when a public peer disconnects or the overall target drops, but only if no remaining public peer is far ahead. Unit tests verify that hidden peers retain the sync target but still restore safe sync, that public peers retain the sync target, and that hidden peers cannot keep safe sync disabled when public peers are close to the local height.
Changed components
src/cryptonote_protocol/cryptonote_protocol_handler.inltests/unit_tests/node_server.cppMonero P2P protocol handlerNode safe sync mode logicInspect captured patch +207 / −3
### src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -2954,25 +2954,31 @@ skip:
template<class t_core>
void t_cryptonote_protocol_handler<t_core>::on_connection_close(cryptonote_connection_context &context)
{
- uint64_t target = 0;
+ uint64_t target = 0, public_target = 0;
m_p2p->for_each_connection([&](const connection_context& cntxt, nodetool::peerid_type peer_id, uint32_t support_flags) {
if (cntxt.m_state >= cryptonote_connection_context::state_synchronizing && cntxt.m_connection_id != context.m_connection_id)
+ {
target = std::max(target, cntxt.m_remote_blockchain_height);
+ if (cntxt.m_remote_address.get_zone() == epee::net_utils::zone::public_)
+ public_target = std::max(public_target, cntxt.m_remote_blockchain_height);
+ }
return true;
});
const uint64_t previous_target = m_core.get_target_blockchain_height();
if (target < previous_target)
{
MINFO("Target height decreasing from " << previous_target << " to " << target);
m_core.set_target_blockchain_height(target);
- if (target < m_core.get_current_blockchain_height() + 5)
- m_core.safesyncmode(true);
if (target == 0 && context.m_state > cryptonote_connection_context::state_before_handshake && !m_stopping)
{
MCWARNING("global", "monerod is now disconnected from the network");
m_ask_for_txpool_complement = true;
}
}
+ if ((target < previous_target || (context.m_remote_address.get_zone() == epee::net_utils::zone::public_ &&
+ context.m_state >= cryptonote_connection_context::state_synchronizing)) &&
+ public_target < m_core.get_current_blockchain_height() + 5)
+ m_core.safesyncmode(true);
m_block_queue.flush_spans(context.m_connection_id, false);
MLOG_PEER_STATE("closed");
### tests/unit_tests/node_server.cpp
@@ -35,6 +35,7 @@
#include "cryptonote_core/i_core_events.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.inl"
+#include "net/i2p_address.h"
#include "net/tor_address.h"
#include "unit_tests_utils.h"
#include <algorithm>
@@ -1895,5 +1896,202 @@ TEST(regtest, isolates_p2p_state_from_mainnet_data_dir)
EXPECT_TRUE(regtest_public.gray.empty());
}
+namespace
+{
+ class target_test_core : public test_core
+ {
+ public:
+ uint64_t target_height = 0;
+ bool safe_sync = false;
+ bool known_block = false;
+ bool synchronized = false;
+
+ uint64_t get_target_blockchain_height() const { return target_height; }
+ void set_target_blockchain_height(uint64_t height) { target_height = height; }
+ void safesyncmode(bool safe) { safe_sync = safe; }
+ bool have_block(const crypto::hash&, int* where = nullptr) const
+ {
+ if (known_block && where)
+ *where = HAVE_BLOCK_MAIN_CHAIN;
+ return known_block;
+ }
+ void on_synchronized() { synchronized = true; }
+ };
+
+ struct target_test_p2p : nodetool::p2p_endpoint_stub<cryptonote::cryptonote_connection_context>
+ {
+ std::vector<cryptonote::cryptonote_connection_context*> connections;
+
+ void for_each_connection(std::function<bool(cryptonote::cryptonote_connection_context&, nodetool::peerid_type, uint32_t)> f) override
+ {
+ for (auto* connection : connections)
+ if (!f(*connection, 0, 0))
+ break;
+ }
+ };
+
+ void set_test_address(cryptonote::cryptonote_connection_context& context, boost::uuids::uuid id,
+ const epee::net_utils::network_address& address)
+ {
+ static_cast<epee::net_utils::connection_context_base&>(context) =
+ epee::net_utils::connection_context_base{id, address, false, false};
+ }
+}
+
+TEST(cryptonote_protocol_handler, hidden_peer_retains_target_but_restores_safe_sync)
+{
+ target_test_core core;
+ target_test_p2p p2p;
+ cryptonote::t_cryptonote_protocol_handler<target_test_core> handler{core, &p2p};
+ ASSERT_TRUE(handler.init(make_regtest_options(".")));
+ cryptonote::cryptonote_connection_context hidden, public_peer;
+ boost::uuids::uuid hidden_id{}, public_id{};
+ hidden_id.data[0] = 1;
+ public_id.data[0] = 2;
+ set_test_address(hidden, hidden_id, net::tor_address::unknown());
+ set_test_address(public_peer, public_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 1, 18080));
+ p2p.connections = {&hidden, &public_peer};
+
+ cryptonote::CORE_SYNC_DATA data{};
+ data.current_height = 8;
+ ASSERT_TRUE(handler.process_payload_sync_data(data, hidden, true));
+ ASSERT_EQ(hidden.m_state, cryptonote::cryptonote_connection_context::state_normal);
+ EXPECT_EQ(core.target_height, 0u);
+
+ data.current_height = 10;
+ ASSERT_TRUE(handler.process_payload_sync_data(data, public_peer, true));
+ ASSERT_EQ(public_peer.m_state, cryptonote::cryptonote_connection_context::state_synchronizing);
+ ASSERT_EQ(core.target_height, 10u);
+
+ handler.on_connection_close(public_peer);
+ EXPECT_EQ(core.target_height, 8u);
+ EXPECT_TRUE(core.safe_sync);
+ EXPECT_EQ(hidden.m_remote_blockchain_height, 8u);
+}
+
+TEST(cryptonote_protocol_handler, public_peer_retains_sync_target)
+{
+ for (const auto state : {cryptonote::cryptonote_connection_context::state_normal,
+ cryptonote::cryptonote_connection_context::state_synchronizing})
+ {
+ SCOPED_TRACE(static_cast<int>(state));
+ target_test_core core;
+ target_test_p2p p2p;
+ cryptonote::t_cryptonote_protocol_handler<target_test_core> handler{core, &p2p};
+ ASSERT_TRUE(handler.init(make_regtest_options(".")));
+ cryptonote::cryptonote_connection_context tor, i2p, public_peer, closing;
+ boost::uuids::uuid tor_id{}, i2p_id{}, public_id{}, closing_id{};
+ tor_id.data[0] = 1;
+ i2p_id.data[0] = 2;
+ public_id.data[0] = 3;
+ closing_id.data[0] = 4;
+ set_test_address(tor, tor_id, MONERO_UNWRAP(net::tor_address::make("vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", 18080)));
+ set_test_address(i2p, i2p_id, MONERO_UNWRAP(net::i2p_address::make("vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopn.b32.i2p")));
+ set_test_address(public_peer, public_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 1, 18080));
+ set_test_address(closing, closing_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 2, 18080));
+ tor.set_state_normal();
+ i2p.set_state_normal();
+ public_peer.m_state = state;
+ closing.m_state = cryptonote::cryptonote_connection_context::state_synchronizing;
+ tor.m_remote_blockchain_height = 12;
+ i2p.m_remote_blockchain_height = 13;
+ public_peer.m_remote_blockchain_height = 9;
+ closing.m_remote_blockchain_height = core.target_height = 14;
+ p2p.connections = {&tor, &i2p, &public_peer, &closing};
+
+ handler.on_connection_close(closing);
+ EXPECT_EQ(core.target_height, 13u);
+ EXPECT_FALSE(core.safe_sync);
+ }
+}
+
+TEST(cryptonote_protocol_handler, higher_hidden_peer_blocks_known_public_top)
+{
+ target_test_core core;
+ target_test_p2p p2p;
+ cryptonote::t_cryptonote_protocol_handler<target_test_core> handler{core, &p2p};
+ ASSERT_TRUE(handler.init(make_regtest_options(".")));
+ cryptonote::cryptonote_connection_context tor, i2p, public_peer, new_public_peer, closing;
+ boost::uuids::uuid tor_id{}, i2p_id{}, public_id{}, new_public_id{}, closing_id{};
+ tor_id.data[0] = 1;
+ i2p_id.data[0] = 2;
+ public_id.data[0] = 3;
+ closing_id.data[0] = 4;
+ new_public_id.data[0] = 5;
+ set_test_address(tor, tor_id, MONERO_UNWRAP(net::tor_address::make("vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", 18080)));
+ set_test_address(i2p, i2p_id, MONERO_UNWRAP(net::i2p_address::make("vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopn.b32.i2p")));
+ set_test_address(public_peer, public_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 1, 18080));
+ set_test_address(new_public_peer, new_public_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 3, 18080));
+ set_test_address(closing, closing_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 2, 18080));
+ tor.set_state_normal();
+ i2p.set_state_normal();
+ closing.m_state = cryptonote::cryptonote_connection_context::state_synchronizing;
+ tor.m_remote_blockchain_height = 8;
+ i2p.m_remote_blockchain_height = 9;
+ closing.m_remote_blockchain_height = core.target_height = 10;
+ p2p.connections = {&tor, &i2p, &public_peer, &closing};
+
+ handler.on_connection_close(closing);
+ ASSERT_EQ(core.target_height, 9u);
+ EXPECT_TRUE(core.safe_sync);
+ p2p.connections = {&tor, &i2p, &public_peer};
+
+ core.known_block = true;
+ cryptonote::CORE_SYNC_DATA data{};
+ data.current_height = core.get_current_blockchain_height();
+ ASSERT_TRUE(handler.process_payload_sync_data(data, public_peer, true));
+ EXPECT_FALSE(core.synchronized);
+ EXPECT_FALSE(handler.is_synchronized());
+
+ p2p.connections = {&tor, &public_peer};
+ handler.on_connection_close(i2p);
+ EXPECT_EQ(core.target_height, 8u);
+ p2p.connections = {&public_peer};
+ handler.on_connection_close(tor);
+ ASSERT_EQ(core.target_height, 1u);
+ EXPECT_FALSE(handler.is_synchronized());
+ p2p.connections = {&public_peer, &new_public_peer};
+ ASSERT_TRUE(handler.process_payload_sync_data(data, new_public_peer, true));
+ EXPECT_TRUE(core.synchronized);
+ EXPECT_TRUE(handler.is_synchronized());
+}
+
+TEST(cryptonote_protocol_handler, public_sync_below_hidden_target_keeps_safe_mode)
+{
+ target_test_core core;
+ target_test_p2p p2p;
+ cryptonote::t_cryptonote_protocol_handler<target_test_core> handler{core, &p2p};
+ ASSERT_TRUE(handler.init(make_regtest_options(".")));
+ cryptonote::cryptonote_connection_context hidden, closing, public_peer;
+ boost::uuids::uuid hidden_id{}, closing_id{}, public_id{};
+ hidden_id.data[0] = 1;
+ closing_id.data[0] = 2;
+ public_id.data[0] = 3;
+ set_test_address(hidden, hidden_id, MONERO_UNWRAP(net::tor_address::make("vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", 18080)));
+ set_test_address(closing, closing_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 1, 18080));
+ set_test_address(public_peer, public_id, MAKE_IPV4_ADDRESS_PORT(203, 0, 113, 2, 18080));
+ hidden.set_state_normal();
+ hidden.m_remote_blockchain_height = 101;
+ p2p.connections = {&hidden, &closing};
+ core.safe_sync = true;
+
+ cryptonote::CORE_SYNC_DATA data{};
+ data.current_height = 101;
+ ASSERT_TRUE(handler.process_payload_sync_data(data, closing, true));
+ ASSERT_EQ(core.target_height, 101u);
+ ASSERT_FALSE(core.safe_sync);
+
+ handler.on_connection_close(closing);
+ ASSERT_EQ(core.target_height, 101u);
+ ASSERT_TRUE(core.safe_sync);
+
+ p2p.connections = {&hidden, &public_peer};
+ data.current_height = 81;
+ ASSERT_TRUE(handler.process_payload_sync_data(data, public_peer, true));
+ EXPECT_EQ(public_peer.m_state, cryptonote::cryptonote_connection_context::state_synchronizing);
+ EXPECT_EQ(core.target_height, 101u);
+ EXPECT_TRUE(core.safe_sync);
+}
+
namespace nodetool { template class node_server<cryptonote::t_cryptonote_protocol_handler<test_core>>; }
namespace cryptonote { template class t_cryptonote_protocol_handler<test_core>; }Why this scored 51/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.