Skip to content

Commit

Permalink
Sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed May 24, 2024
1 parent 10ca8a5 commit c7c3c70
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
12 changes: 8 additions & 4 deletions nano/node/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ nano::active_elections::active_elections (nano::node & node_a, nano::confirming_
confirming_set.batch_cemented.add ([this] (nano::confirming_set::cemented_notification const & notification) {
{
auto transaction = node.ledger.tx_begin_read ();
for (auto const & block : notification.cemented)
for (auto const & [block, is_root] : notification.cemented)
{
transaction.refresh_if_needed ();

block_cemented_callback (transaction, block);
block_cemented_callback (transaction, block, is_root);
}
}
for (auto const & hash : notification.already_cemented)
Expand Down Expand Up @@ -90,7 +90,7 @@ void nano::active_elections::stop ()
clear ();
}

void nano::active_elections::block_cemented_callback (nano::secure::transaction const & transaction, std::shared_ptr<nano::block> const & block)
void nano::active_elections::block_cemented_callback (nano::secure::transaction const & transaction, std::shared_ptr<nano::block> const & block, bool root)
{
debug_assert (node.block_confirmed (block->hash ()));
if (auto election_l = election (block->qualified_root ()))
Expand All @@ -106,8 +106,11 @@ void nano::active_elections::block_cemented_callback (nano::secure::transaction
status = election->get_status ();
votes = election->votes_with_weight ();
}
if (confirming_set.exists (block->hash ())) // TODO: This can be passed from the confirming_set

if (root)
{
// If the block is the root of the confirmation, the election must exist
release_assert (election, "election winner not found for confirmation root");
status.type = nano::election_status_type::active_confirmed_quorum;
}
else if (election)
Expand All @@ -118,6 +121,7 @@ void nano::active_elections::block_cemented_callback (nano::secure::transaction
{
status.type = nano::election_status_type::inactive_confirmation_height;
}

recently_cemented.put (status);

node.stats.inc (nano::stat::type::active_elections, nano::stat::detail::cemented);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_elections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class active_elections final
std::vector<std::shared_ptr<nano::election>> list_active_impl (std::size_t) const;
void activate_successors (nano::secure::transaction const &, std::shared_ptr<nano::block> const & block);
void notify_observers (nano::secure::transaction const &, nano::election_status const & status, std::vector<nano::vote_with_weight_info> const & votes) const;
void block_cemented_callback (nano::secure::transaction const &, std::shared_ptr<nano::block> const &);
void block_cemented_callback (nano::secure::transaction const &, std::shared_ptr<nano::block> const &, bool root);
void block_already_cemented_callback (nano::block_hash const &);

private: // Dependencies
Expand Down
18 changes: 11 additions & 7 deletions nano/node/confirming_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ nano::confirming_set::confirming_set (nano::ledger & ledger_a, nano::stats & sta
workers{ 1, nano::thread_role::name::confirmation_height_notifications }
{
batch_cemented.add ([this] (auto const & notification) {
for (auto const & i : notification.cemented)
for (auto const & [block, is_root] : notification.cemented)
{
stats.inc (nano::stat::type::confirming_set, nano::stat::detail::notify_cemented);
cemented_observers.notify (i);
cemented_observers.notify (block);
}
for (auto const & i : notification.already_cemented)
for (auto const & hash : notification.already_cemented)
{
stats.inc (nano::stat::type::confirming_set, nano::stat::detail::notify_already_confirmed);
block_already_cemented_observers.notify (i);
block_already_cemented_observers.notify (hash);
}
});
}
Expand Down Expand Up @@ -110,7 +110,7 @@ void nano::confirming_set::run_batch (std::unique_lock<std::mutex> & lock)
debug_assert (!mutex.try_lock ());
debug_assert (!set.empty ());

std::deque<std::shared_ptr<nano::block>> cemented;
std::deque<cemented_t> cemented;
std::deque<nano::block_hash> already;

// Move items in to back buffer and release lock so more items can be added to the front buffer
Expand All @@ -135,9 +135,13 @@ void nano::confirming_set::run_batch (std::unique_lock<std::mutex> & lock)
auto added = ledger.confirm (tx, item);
if (!added.empty ())
{
// Confirming this block may implicitly confirm more
cemented.insert (cemented.end (), added.begin (), added.end ());
stats.add (nano::stat::type::confirming_set, nano::stat::detail::cemented, added.size ());

// Confirming this block may implicitly confirm more
for (auto & block : added)
{
cemented.push_back ({ block, block->hash () == item });
}
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion nano/node/confirming_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class confirming_set final
std::unique_ptr<container_info_component> collect_container_info (std::string const & name) const;

public: // Events
using cemented_t = std::pair<std::shared_ptr<nano::block>, bool>; // bool indicates if block was the root of the confirmation

// Observers will be called once ledger has blocks marked as confirmed
struct cemented_notification
{
std::deque<std::shared_ptr<nano::block>> cemented;
std::deque<cemented_t> cemented;
std::deque<nano::block_hash> already_cemented;
};

Expand Down
11 changes: 8 additions & 3 deletions nano/node/recently_cemented_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/utility.hpp>
#include <nano/node/recently_cemented_cache.hpp>

#include <ranges>

/*
* class recently_cemented
*/
Expand All @@ -13,17 +16,19 @@ nano::recently_cemented_cache::recently_cemented_cache (std::size_t 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);
auto [it, inserted] = cemented.emplace_back (entry_t{ status.winner->hash (), status });
release_assert (inserted, "recently cemented hash conflict");
if (cemented.size () > max_size)
{
cemented.pop_front ();
}
}

nano::recently_cemented_cache::queue_t nano::recently_cemented_cache::list () const
std::deque<nano::election_status> nano::recently_cemented_cache::list () const
{
nano::lock_guard<nano::mutex> guard{ mutex };
return cemented;
auto r = cemented | std::views::transform ([] (auto const & entry) { return entry.second; });
return { r.begin (), r.end () };
}

std::size_t nano::recently_cemented_cache::size () const
Expand Down
28 changes: 24 additions & 4 deletions nano/node/recently_cemented_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

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

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index_container.hpp>

#include <deque>

namespace mi = boost::multi_index;

namespace nano
{
class container_info_component;
Expand All @@ -18,16 +27,27 @@ namespace nano
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::deque<nano::election_status> list () const;
std::size_t size () const;

private:
queue_t cemented;
using entry_t = std::pair<nano::block_hash, nano::election_status>;

// clang-format off
class tag_hash {};
class tag_sequence {};

using ordered_recent_cementations = boost::multi_index_container<entry_t,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequence>>,
mi::hashed_unique<mi::tag<tag_hash>,
mi::member<entry_t, nano::block_hash, &entry_t::first>>>>;
// clang-format on
ordered_recent_cementations cemented;

std::size_t const max_size;

mutable nano::mutex mutex;
Expand Down

0 comments on commit c7c3c70

Please sign in to comment.