From fe957adcaecf1361afe49eeaaf9518b7d239aaab Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Thu, 7 Sep 2023 17:22:38 +0100 Subject: [PATCH] Adding destructor observer --- nano/core_test/election.cpp | 16 ++++++++++++++++ nano/node/election.cpp | 5 +++++ nano/node/election.hpp | 3 +++ 3 files changed, 24 insertions(+) diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index 94f33a034d..8cb7d0f322 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -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 destroyed{ false }; + { + nano::test::system system (1); + auto & node = *system.nodes[0]; + auto election = std::make_shared (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); diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 79243bfece..c8f3d5ee73 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -32,6 +32,11 @@ nano::election::election (nano::node & node_a, std::shared_ptr 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 & lock_a, nano::election_status_type type_a) { debug_assert (lock_a.owns_lock ()); diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 1acc6eb921..59abffd06b 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -138,7 +138,10 @@ class election final : public std::enable_shared_from_this public: // Interface election (nano::node &, std::shared_ptr const & block, std::function const &)> const & confirmation_action, std::function const & vote_action, nano::election_behavior behavior); + ~election (); + nano::observer_set destructor_observers; +public: std::shared_ptr find (nano::block_hash const &) const; /* * Process vote. Internally uses cooldown to throttle non-final votes