Skip to content

Commit

Permalink
Move recently_cemented_cache and recently_confirmed_cache to their ow…
Browse files Browse the repository at this point in the history
…n files. (#4606)
  • Loading branch information
clemahieu authored May 7, 2024
1 parent f4ff734 commit 7f33efd
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 170 deletions.
4 changes: 4 additions & 0 deletions nano/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ add_library(
portmapping.cpp
process_live_dispatcher.cpp
process_live_dispatcher.hpp
recently_cemented_cache.cpp
recently_cemented_cache.hpp
recently_confirmed_cache.cpp
recently_confirmed_cache.hpp
repcrawler.hpp
repcrawler.cpp
rep_tiers.hpp
Expand Down
104 changes: 0 additions & 104 deletions nano/node/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,110 +662,6 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (ac
return composite;
}

/*
* class recently_confirmed
*/

nano::recently_confirmed_cache::recently_confirmed_cache (std::size_t max_size_a) :
max_size{ max_size_a }
{
}

void nano::recently_confirmed_cache::put (const nano::qualified_root & root, const nano::block_hash & hash)
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.get<tag_sequence> ().emplace_back (root, hash);
if (confirmed.size () > max_size)
{
confirmed.get<tag_sequence> ().pop_front ();
}
}

void nano::recently_confirmed_cache::erase (const nano::block_hash & hash)
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.get<tag_hash> ().erase (hash);
}

void nano::recently_confirmed_cache::clear ()
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.clear ();
}

bool nano::recently_confirmed_cache::exists (const nano::block_hash & hash) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.get<tag_hash> ().find (hash) != confirmed.get<tag_hash> ().end ();
}

bool nano::recently_confirmed_cache::exists (const nano::qualified_root & root) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.get<tag_root> ().find (root) != confirmed.get<tag_root> ().end ();
}

std::size_t nano::recently_confirmed_cache::size () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.size ();
}

nano::recently_confirmed_cache::entry_t nano::recently_confirmed_cache::back () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.back ();
}

std::unique_ptr<nano::container_info_component> nano::recently_confirmed_cache::collect_container_info (const std::string & name)
{
nano::unique_lock<nano::mutex> lock{ mutex };

auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "confirmed", confirmed.size (), sizeof (decltype (confirmed)::value_type) }));
return composite;
}

/*
* class recently_cemented
*/

nano::recently_cemented_cache::recently_cemented_cache (std::size_t max_size_a) :
max_size{ max_size_a }
{
}

void nano::recently_cemented_cache::put (const nano::election_status & status)
{
nano::lock_guard<nano::mutex> guard{ mutex };
cemented.push_back (status);
if (cemented.size () > max_size)
{
cemented.pop_front ();
}
}

nano::recently_cemented_cache::queue_t nano::recently_cemented_cache::list () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return cemented;
}

std::size_t nano::recently_cemented_cache::size () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return cemented.size ();
}

std::unique_ptr<nano::container_info_component> nano::recently_cemented_cache::collect_container_info (const std::string & name)
{
nano::unique_lock<nano::mutex> lock{ mutex };

auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "cemented", cemented.size (), sizeof (decltype (cemented)::value_type) }));
return composite;
}

/*
* active_elections_config
*/
Expand Down
68 changes: 2 additions & 66 deletions nano/node/active_elections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <nano/node/election_behavior.hpp>
#include <nano/node/election_insertion_result.hpp>
#include <nano/node/election_status.hpp>
#include <nano/node/recently_cemented_cache.hpp>
#include <nano/node/recently_confirmed_cache.hpp>
#include <nano/node/vote_with_weight_info.hpp>
#include <nano/secure/common.hpp>

Expand Down Expand Up @@ -62,72 +64,6 @@ class active_elections_config final
std::size_t confirmation_cache{ 65536 };
};

class recently_confirmed_cache final
{
public:
using entry_t = std::pair<nano::qualified_root, nano::block_hash>;

explicit recently_confirmed_cache (std::size_t max_size);

void put (nano::qualified_root const &, nano::block_hash const &);
void erase (nano::block_hash const &);
void clear ();
std::size_t size () const;

bool exists (nano::qualified_root const &) const;
bool exists (nano::block_hash const &) const;

public: // Tests
entry_t back () const;

private:
// clang-format off
class tag_hash {};
class tag_root {};
class tag_sequence {};

using ordered_recent_confirmations = boost::multi_index_container<entry_t,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequence>>,
mi::hashed_unique<mi::tag<tag_root>,
mi::member<entry_t, nano::qualified_root, &entry_t::first>>,
mi::hashed_unique<mi::tag<tag_hash>,
mi::member<entry_t, nano::block_hash, &entry_t::second>>>>;
// clang-format on
ordered_recent_confirmations confirmed;

std::size_t const max_size;

mutable nano::mutex mutex;

public: // Container info
std::unique_ptr<container_info_component> collect_container_info (std::string const &);
};

/*
* Helper container for storing recently cemented elections (a block from election might be confirmed but not yet cemented by confirmation height processor)
*/
class recently_cemented_cache final
{
public:
using queue_t = std::deque<nano::election_status>;

explicit recently_cemented_cache (std::size_t max_size);

void put (nano::election_status const &);
queue_t list () const;
std::size_t size () const;

private:
queue_t cemented;
std::size_t const max_size;

mutable nano::mutex mutex;

public: // Container info
std::unique_ptr<container_info_component> collect_container_info (std::string const &);
};

/**
* Core class for determining consensus
* Holds all active blocks i.e. recently added blocks that need confirmation
Expand Down
42 changes: 42 additions & 0 deletions nano/node/recently_cemented_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <nano/lib/utility.hpp>
#include <nano/node/recently_cemented_cache.hpp>

/*
* class recently_cemented
*/

nano::recently_cemented_cache::recently_cemented_cache (std::size_t max_size_a) :
max_size{ max_size_a }
{
}

void nano::recently_cemented_cache::put (const nano::election_status & status)
{
nano::lock_guard<nano::mutex> guard{ mutex };
cemented.push_back (status);
if (cemented.size () > max_size)
{
cemented.pop_front ();
}
}

nano::recently_cemented_cache::queue_t nano::recently_cemented_cache::list () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return cemented;
}

std::size_t nano::recently_cemented_cache::size () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return cemented.size ();
}

std::unique_ptr<nano::container_info_component> nano::recently_cemented_cache::collect_container_info (const std::string & name)
{
nano::unique_lock<nano::mutex> lock{ mutex };

auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "cemented", cemented.size (), sizeof (decltype (cemented)::value_type) }));
return composite;
}
38 changes: 38 additions & 0 deletions nano/node/recently_cemented_cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <nano/lib/locks.hpp>
#include <nano/node/election_status.hpp>

#include <deque>

namespace nano
{
class container_info_component;
}

namespace nano
{
/*
* Helper container for storing recently cemented elections (a block from election might be confirmed but not yet cemented by confirmation height processor)
*/
class recently_cemented_cache final
{
public:
using queue_t = std::deque<nano::election_status>;

explicit recently_cemented_cache (std::size_t max_size);

void put (nano::election_status const &);
queue_t list () const;
std::size_t size () const;

private:
queue_t cemented;
std::size_t const max_size;

mutable nano::mutex mutex;

public: // Container info
std::unique_ptr<container_info_component> collect_container_info (std::string const &);
};
}
66 changes: 66 additions & 0 deletions nano/node/recently_confirmed_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <nano/lib/utility.hpp>
#include <nano/node/recently_confirmed_cache.hpp>

/*
* class recently_confirmed
*/

nano::recently_confirmed_cache::recently_confirmed_cache (std::size_t max_size_a) :
max_size{ max_size_a }
{
}

void nano::recently_confirmed_cache::put (const nano::qualified_root & root, const nano::block_hash & hash)
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.get<tag_sequence> ().emplace_back (root, hash);
if (confirmed.size () > max_size)
{
confirmed.get<tag_sequence> ().pop_front ();
}
}

void nano::recently_confirmed_cache::erase (const nano::block_hash & hash)
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.get<tag_hash> ().erase (hash);
}

void nano::recently_confirmed_cache::clear ()
{
nano::lock_guard<nano::mutex> guard{ mutex };
confirmed.clear ();
}

bool nano::recently_confirmed_cache::exists (const nano::block_hash & hash) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.get<tag_hash> ().find (hash) != confirmed.get<tag_hash> ().end ();
}

bool nano::recently_confirmed_cache::exists (const nano::qualified_root & root) const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.get<tag_root> ().find (root) != confirmed.get<tag_root> ().end ();
}

std::size_t nano::recently_confirmed_cache::size () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.size ();
}

nano::recently_confirmed_cache::entry_t nano::recently_confirmed_cache::back () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return confirmed.back ();
}

std::unique_ptr<nano::container_info_component> nano::recently_confirmed_cache::collect_container_info (const std::string & name)
{
nano::unique_lock<nano::mutex> lock{ mutex };

auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "confirmed", confirmed.size (), sizeof (decltype (confirmed)::value_type) }));
return composite;
}
Loading

0 comments on commit 7f33efd

Please sign in to comment.