Skip to content

Commit

Permalink
Adding destructor observer
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Sep 12, 2023
1 parent 1e51a7f commit fe957ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions nano/core_test/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ TEST (election, construction)
node, nano::dev::genesis, [] (auto const &) {}, [] (auto const &) {}, nano::election_behavior::normal);
}

// This tests the election destruction event notification
// Since the notification is signalled in the destructor, it needs to be freed
TEST (election, destructor_observer)
{
std::atomic<bool> destroyed{ false };
{
nano::test::system system (1);
auto & node = *system.nodes[0];
auto election = std::make_shared<nano::election> (node, nano::dev::genesis, [] (auto const &) {}, [] (auto const &) {}, nano::election_behavior::normal);
election->destructor_observers.add ([&destroyed] (auto const & qualified_root) {
destroyed = true;
});
}
ASSERT_TRUE (destroyed);
}

TEST (election, behavior)
{
nano::test::system system (1);
Expand Down
5 changes: 5 additions & 0 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ nano::election::election (nano::node & node_a, std::shared_ptr<nano::block> cons
last_blocks.emplace (block_a->hash (), block_a);
}

nano::election::~election ()
{
destructor_observers.notify (qualified_root);
}

void nano::election::confirm_once (nano::unique_lock<nano::mutex> & lock_a, nano::election_status_type type_a)
{
debug_assert (lock_a.owns_lock ());
Expand Down
3 changes: 3 additions & 0 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ class election final : public std::enable_shared_from_this<nano::election>

public: // Interface
election (nano::node &, std::shared_ptr<nano::block> const & block, std::function<void (std::shared_ptr<nano::block> const &)> const & confirmation_action, std::function<void (nano::account const &)> const & vote_action, nano::election_behavior behavior);
~election ();
nano::observer_set<nano::qualified_root const &> destructor_observers;

public:
std::shared_ptr<nano::block> find (nano::block_hash const &) const;
/*
* Process vote. Internally uses cooldown to throttle non-final votes
Expand Down

0 comments on commit fe957ad

Please sign in to comment.