Skip to content

Commit

Permalink
Some cleanup in span/connection_context + few more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
vtnerd committed Feb 3, 2025
1 parent 5e31c0a commit ed70c16
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 35 deletions.
20 changes: 20 additions & 0 deletions src/cryptonote_basic/connection_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "connection_context.h"

#include <boost/optional/optional.hpp>
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "p2p/p2p_protocol_defs.h"

Expand Down Expand Up @@ -69,4 +70,23 @@ namespace cryptonote
};
return std::numeric_limits<size_t>::max();
}

void cryptonote_connection_context::set_state_normal()
{
m_state = state_normal;
m_expected_heights_start = 0;
m_needed_objects.clear();
m_needed_objects.shrink_to_fit();
m_expected_heights.clear();
m_expected_heights.shrink_to_fit();
m_requested_objects.clear();
}

boost::optional<crypto::hash> cryptonote_connection_context::get_expected_hash(const uint64_t height) const
{
const auto difference = height - m_expected_heights_start;
if (height < m_expected_heights_start || m_expected_heights.size() < difference)
return boost::none;
return m_expected_heights[difference];
}
} // cryptonote
10 changes: 9 additions & 1 deletion src/cryptonote_basic/connection_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <atomic>
#include <algorithm>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/optional/optional_fwd.hpp>
#include "net/net_utils_base.h"
#include "crypto/hash.h"

Expand All @@ -42,7 +43,7 @@ namespace cryptonote
struct cryptonote_connection_context: public epee::net_utils::connection_context_base
{
cryptonote_connection_context(): m_state(state_before_handshake), m_remote_blockchain_height(0), m_last_response_height(0),
m_last_request_time(boost::date_time::not_a_date_time), m_callback_request_count(0),
m_expected_heights_start(0), m_last_request_time(boost::date_time::not_a_date_time), m_callback_request_count(0),
m_last_known_hash(crypto::null_hash), m_pruning_seed(0), m_rpc_port(0), m_rpc_credits_per_hash(0), m_anchor(false), m_score(0),
m_expect_response(0), m_expect_height(0), m_num_requested(0) {}

Expand Down Expand Up @@ -92,11 +93,18 @@ namespace cryptonote
//! \return Maximum number of bytes permissible for `command`.
static size_t get_max_bytes(int command) noexcept;

//! Use this instead of `m_state = state_normal`.
void set_state_normal();

boost::optional<crypto::hash> get_expected_hash(uint64_t height) const;

state m_state;
std::vector<std::pair<crypto::hash, uint64_t>> m_needed_objects;
std::vector<crypto::hash> m_expected_heights;
std::unordered_set<crypto::hash> m_requested_objects;
uint64_t m_remote_blockchain_height;
uint64_t m_last_response_height;
uint64_t m_expected_heights_start;
boost::posix_time::ptime m_last_request_time;
copyable_atomic m_callback_request_count; //in debug purpose: problem with double callback rise
crypto::hash m_last_known_hash;
Expand Down
16 changes: 13 additions & 3 deletions src/cryptonote_protocol/block_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ void block_queue::add_blocks(uint64_t height, std::vector<cryptonote::block_comp
blocks.insert(span(height, std::move(bcel), connection_id, addr, rate, size));
if (has_hashes)
{
for (const crypto::hash &h: hashes)
for (std::size_t i = 0; i < hashes.size(); ++i)
{
requested_hashes.insert(h);
have_blocks.insert(h);
requested_hashes.insert(hashes[i]);
have_blocks.emplace(hashes[i], height + i);
}
set_span_hashes(height, connection_id, hashes);
}
Expand Down Expand Up @@ -219,6 +219,16 @@ bool block_queue::have(const crypto::hash &hash) const
return have_blocks.find(hash) != have_blocks.end();
}

std::uint64_t block_queue::have_height(const crypto::hash &hash) const
{
boost::unique_lock<boost::recursive_mutex> lock(mutex);
const auto elem = have_blocks.find(hash);
if (elem == have_blocks.end())
return std::numeric_limits<std::uint64_t>::max();
return elem->second;
}


std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, const epee::net_utils::network_address &addr, bool sync_pruned_blocks, uint32_t local_pruning_seed, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<std::pair<crypto::hash, uint64_t>> &block_hashes, boost::posix_time::ptime time)
{
boost::unique_lock<boost::recursive_mutex> lock(mutex);
Expand Down
3 changes: 2 additions & 1 deletion src/cryptonote_protocol/block_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace cryptonote
bool foreach(std::function<bool(const span&)> f) const;
bool requested(const crypto::hash &hash) const;
bool have(const crypto::hash &hash) const;
std::uint64_t have_height(const crypto::hash &hash) const;

private:
void erase_block(block_map::iterator j);
Expand All @@ -107,6 +108,6 @@ namespace cryptonote
block_map blocks;
mutable boost::recursive_mutex mutex;
std::unordered_set<crypto::hash> requested_hashes;
std::unordered_set<crypto::hash> have_blocks;
std::unordered_map<crypto::hash, std::uint64_t> have_blocks;
};
}
1 change: 1 addition & 0 deletions src/cryptonote_protocol/cryptonote_protocol_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ namespace cryptonote
bool should_ask_for_pruned_data(cryptonote_connection_context& context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const;
void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
void drop_connection_with_score(cryptonote_connection_context &context, unsigned int score, bool flush_all_spans);
void drop_connection(const boost::uuids::uuid&);
void drop_connections(const epee::net_utils::network_address address);
bool kick_idle_peers();
bool check_standby_peers();
Expand Down
Loading

0 comments on commit ed70c16

Please sign in to comment.