aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_protocol
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptonote_protocol')
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.h2
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.inl6
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler_common.h5
-rw-r--r--src/cryptonote_protocol/levin_notify.cpp58
4 files changed, 59 insertions, 12 deletions
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h
index 89860fe41..61aac6d81 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h
@@ -104,7 +104,7 @@ namespace cryptonote
bool get_payload_sync_data(CORE_SYNC_DATA& hshd);
bool on_callback(cryptonote_connection_context& context);
t_core& get_core(){return m_core;}
- bool is_synchronized(){return m_synchronized;}
+ virtual bool is_synchronized() const final { return !no_sync() && m_synchronized; }
void log_connections();
std::list<connection_info> get_connections();
const block_queue &get_block_queue() const { return m_block_queue; }
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
index 20bcd7f3b..a72b7db79 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -430,7 +430,7 @@ namespace cryptonote
MLOGIF_P2P_MESSAGE(crypto::hash hash; cryptonote::block b; bool ret = cryptonote::parse_and_validate_block_from_blob(arg.b.block, b, &hash);, ret, "Received NOTIFY_NEW_BLOCK " << hash << " (height " << arg.current_blockchain_height << ", " << arg.b.txs.size() << " txes)");
if(context.m_state != cryptonote_connection_context::state_normal)
return 1;
- if(!is_synchronized() || m_no_sync) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
+ if(!is_synchronized()) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
{
LOG_DEBUG_CC(context, "Received new block while syncing, ignored");
return 1;
@@ -501,7 +501,7 @@ namespace cryptonote
MLOGIF_P2P_MESSAGE(crypto::hash hash; cryptonote::block b; bool ret = cryptonote::parse_and_validate_block_from_blob(arg.b.block, b, &hash);, ret, "Received NOTIFY_NEW_FLUFFY_BLOCK " << hash << " (height " << arg.current_blockchain_height << ", " << arg.b.txs.size() << " txes)");
if(context.m_state != cryptonote_connection_context::state_normal)
return 1;
- if(!is_synchronized() || m_no_sync) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
+ if(!is_synchronized()) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
{
LOG_DEBUG_CC(context, "Received new block while syncing, ignored");
return 1;
@@ -929,7 +929,7 @@ namespace cryptonote
// while syncing, core will lock for a long time, so we ignore
// those txes as they aren't really needed anyway, and avoid a
// long block before replying
- if(!is_synchronized() || m_no_sync)
+ if(!is_synchronized())
{
LOG_DEBUG_CC(context, "Received new tx while syncing, ignored");
return 1;
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler_common.h b/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
index 1c7635fd8..79c2edf1d 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
@@ -40,6 +40,7 @@ namespace cryptonote
/************************************************************************/
struct i_cryptonote_protocol
{
+ virtual bool is_synchronized() const = 0;
virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context)=0;
virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay)=0;
//virtual bool request_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context)=0;
@@ -50,6 +51,10 @@ namespace cryptonote
/************************************************************************/
struct cryptonote_protocol_stub: public i_cryptonote_protocol
{
+ virtual bool is_synchronized() const final
+ {
+ return false;
+ }
virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context)
{
return false;
diff --git a/src/cryptonote_protocol/levin_notify.cpp b/src/cryptonote_protocol/levin_notify.cpp
index b6b1268cf..75ba68d3b 100644
--- a/src/cryptonote_protocol/levin_notify.cpp
+++ b/src/cryptonote_protocol/levin_notify.cpp
@@ -105,8 +105,44 @@ namespace levin
return std::chrono::steady_clock::duration{crypto::rand_range(rep(0), range.count())};
}
- //! \return Outgoing connections supporting fragments in `connections` filtered by remote blockchain height.
- std::vector<boost::uuids::uuid> get_out_connections(connections& p2p, uint64_t min_blockchain_height)
+ uint64_t get_median_remote_height(connections& p2p)
+ {
+ std::vector<uint64_t> remote_heights;
+ remote_heights.reserve(connection_id_reserve_size);
+ p2p.foreach_connection([&remote_heights] (detail::p2p_context& context) {
+ if (!context.m_is_income)
+ {
+ remote_heights.emplace_back(context.m_remote_blockchain_height);
+ }
+ return true;
+ });
+
+ if (remote_heights.empty())
+ {
+ return 0;
+ }
+
+ const size_t n = remote_heights.size() / 2;
+ std::sort(remote_heights.begin(), remote_heights.end());
+ if (remote_heights.size() % 2 != 0)
+ {
+ return remote_heights[n];
+ }
+ return remote_heights[n-1];
+ }
+
+ uint64_t get_blockchain_height(connections& p2p, const i_core_events* core)
+ {
+ const uint64_t local_blockchain_height = core->get_current_blockchain_height();
+ if (core->is_synchronized())
+ {
+ return local_blockchain_height;
+ }
+ return std::max(local_blockchain_height, get_median_remote_height(p2p));
+ }
+
+ //! \return Outgoing connections supporting fragments in `connections` filtered by blockchain height.
+ std::vector<boost::uuids::uuid> get_out_connections(connections& p2p, uint64_t blockchain_height)
{
std::vector<boost::uuids::uuid> outs;
outs.reserve(connection_id_reserve_size);
@@ -115,15 +151,21 @@ namespace levin
the reserve call so a strand is not used. Investigate if there is lots
of waiting in here. */
- p2p.foreach_connection([&outs, min_blockchain_height] (detail::p2p_context& context) {
- if (!context.m_is_income && context.m_remote_blockchain_height >= min_blockchain_height)
+ p2p.foreach_connection([&outs, blockchain_height] (detail::p2p_context& context) {
+ if (!context.m_is_income && context.m_remote_blockchain_height >= blockchain_height)
outs.emplace_back(context.m_connection_id);
return true;
});
+ MDEBUG("Found " << outs.size() << " out connections having height >= " << blockchain_height);
return outs;
}
+ std::vector<boost::uuids::uuid> get_out_connections(connections& p2p, const i_core_events* core)
+ {
+ return get_out_connections(p2p, get_blockchain_height(p2p, core));
+ }
+
std::string make_tx_payload(std::vector<blobdata>&& txs, const bool pad, const bool fluff)
{
NOTIFY_NEW_TRANSACTIONS::request request{};
@@ -540,7 +582,7 @@ namespace levin
}
// connection list may be outdated, try again
- update_channels::run(zone_, get_out_connections(*zone_->p2p, core_->get_target_blockchain_height()));
+ update_channels::run(zone_, get_out_connections(*zone_->p2p, core_));
}
MERROR("Unable to send transaction(s) via Dandelion++ stem");
@@ -644,7 +686,7 @@ namespace levin
{
channel.active = nullptr;
channel.connection = boost::uuids::nil_uuid();
- auto height = core_->get_target_blockchain_height();
+ auto height = get_blockchain_height(*zone_->p2p, core_);
auto connections = get_out_connections(*zone_->p2p, height);
if (connections.empty())
@@ -680,7 +722,7 @@ namespace levin
const bool fluffing = crypto::rand_idx(unsigned(100)) < CRYPTONOTE_DANDELIONPP_FLUFF_PROBABILITY;
const auto start = std::chrono::steady_clock::now();
- auto connections = get_out_connections(*(zone_->p2p), core_->get_target_blockchain_height());
+ auto connections = get_out_connections(*(zone_->p2p), core_);
zone_->strand.dispatch(
change_channels{zone_, net::dandelionpp::connection_map{std::move(connections), count_}, fluffing}
);
@@ -731,7 +773,7 @@ namespace levin
return;
zone_->strand.dispatch(
- update_channels{zone_, get_out_connections(*(zone_->p2p), core_->get_target_blockchain_height())}
+ update_channels{zone_, get_out_connections(*(zone_->p2p), core_)}
);
}