Merge pull request #2552 from nanocurrency/confirmed_function
Encapsulating election::confirmed so its implementation can vary.
This commit is contained in:
		
				commit
				
					
						efe79639ec
					
				
			
		
					 5 changed files with 23 additions and 17 deletions
				
			
		| 
						 | 
				
			
			@ -212,7 +212,7 @@ TEST (node, node_receive_quorum)
 | 
			
		|||
		nano::lock_guard<std::mutex> guard (node1.active.mutex);
 | 
			
		||||
		auto info (node1.active.roots.find (nano::qualified_root (previous, previous)));
 | 
			
		||||
		ASSERT_NE (node1.active.roots.end (), info);
 | 
			
		||||
		ASSERT_FALSE (info->election->confirmed);
 | 
			
		||||
		ASSERT_FALSE (info->election->confirmed ());
 | 
			
		||||
		ASSERT_EQ (1, info->election->last_votes.size ());
 | 
			
		||||
	}
 | 
			
		||||
	nano::system system2 (1);
 | 
			
		||||
| 
						 | 
				
			
			@ -2574,7 +2574,7 @@ TEST (node, confirm_quorum)
 | 
			
		|||
	nano::lock_guard<std::mutex> guard (node1.active.mutex);
 | 
			
		||||
	auto info (node1.active.roots.find (nano::qualified_root (send1->hash (), send1->hash ())));
 | 
			
		||||
	ASSERT_NE (node1.active.roots.end (), info);
 | 
			
		||||
	ASSERT_FALSE (info->election->confirmed);
 | 
			
		||||
	ASSERT_FALSE (info->election->confirmed ());
 | 
			
		||||
	ASSERT_EQ (1, info->election->last_votes.size ());
 | 
			
		||||
	ASSERT_EQ (0, node1.balance (nano::test_genesis_key.pub));
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -159,7 +159,7 @@ void nano::active_transactions::block_cemented_callback (std::shared_ptr<nano::b
 | 
			
		|||
			if (existing != election_winner_details.end ())
 | 
			
		||||
			{
 | 
			
		||||
				auto election = existing->second;
 | 
			
		||||
				if (election->confirmed && !election->stopped && election->status.winner->hash () == hash)
 | 
			
		||||
				if (election->confirmed () && !election->stopped && election->status.winner->hash () == hash)
 | 
			
		||||
				{
 | 
			
		||||
					add_confirmed (existing->second->status, block_a->qualified_root ());
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -310,7 +310,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
 | 
			
		|||
	{
 | 
			
		||||
		auto election_l (i->election);
 | 
			
		||||
		auto root_l (i->root);
 | 
			
		||||
		if (election_l->confirmed || (election_l->confirmation_request_count != 0 && !node.ledger.could_fit (transaction_l, *election_l->status.winner)))
 | 
			
		||||
		if (election_l->confirmed () || (election_l->confirmation_request_count != 0 && !node.ledger.could_fit (transaction_l, *election_l->status.winner)))
 | 
			
		||||
		{
 | 
			
		||||
			election_l->stop ();
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -744,7 +744,7 @@ void nano::active_transactions::adjust_difficulty (nano::block_hash const & hash
 | 
			
		|||
		if (processed_blocks.find (hash) == processed_blocks.end ())
 | 
			
		||||
		{
 | 
			
		||||
			auto existing (blocks.find (hash));
 | 
			
		||||
			if (existing != blocks.end () && !existing->second->confirmed && !existing->second->stopped && existing->second->status.winner->hash () == hash)
 | 
			
		||||
			if (existing != blocks.end () && !existing->second->confirmed () && !existing->second->stopped && existing->second->status.winner->hash () == hash)
 | 
			
		||||
			{
 | 
			
		||||
				auto previous (existing->second->status.winner->previous ());
 | 
			
		||||
				if (!previous.is_zero ())
 | 
			
		||||
| 
						 | 
				
			
			@ -829,7 +829,7 @@ void nano::active_transactions::update_active_difficulty (nano::unique_lock<std:
 | 
			
		|||
		auto cutoff (std::chrono::steady_clock::now () - election_request_delay - 1s);
 | 
			
		||||
		for (auto it (sorted_roots.begin ()), end (sorted_roots.end ()); it != end && count++ < node.config.active_elections_size; ++it)
 | 
			
		||||
		{
 | 
			
		||||
			if (!it->election->confirmed && !it->election->stopped && it->election->election_start < cutoff)
 | 
			
		||||
			if (!it->election->confirmed () && !it->election->stopped && it->election->election_start < cutoff)
 | 
			
		||||
			{
 | 
			
		||||
				active_root_difficulties.push_back (it->adjusted_difficulty);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -927,7 +927,7 @@ bool nano::active_transactions::publish (std::shared_ptr<nano::block> block_a)
 | 
			
		|||
	{
 | 
			
		||||
		auto election (existing->election);
 | 
			
		||||
		result = election->publish (block_a);
 | 
			
		||||
		if (!result && !election->confirmed)
 | 
			
		||||
		if (!result && !election->confirmed ())
 | 
			
		||||
		{
 | 
			
		||||
			blocks.emplace (block_a->hash (), election);
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -943,7 +943,7 @@ boost::optional<nano::election_status_type> nano::active_transactions::confirm_b
 | 
			
		|||
	auto existing (blocks.find (hash));
 | 
			
		||||
	if (existing != blocks.end ())
 | 
			
		||||
	{
 | 
			
		||||
		if (!existing->second->confirmed && !existing->second->stopped && existing->second->status.winner->hash () == hash)
 | 
			
		||||
		if (!existing->second->confirmed () && !existing->second->stopped && existing->second->status.winner->hash () == hash)
 | 
			
		||||
		{
 | 
			
		||||
			existing->second->confirm_once (nano::election_status_type::active_confirmation_height);
 | 
			
		||||
			return nano::election_status_type::active_confirmation_height;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,11 +11,11 @@ nano::election_vote_result::election_vote_result (bool replay_a, bool processed_
 | 
			
		|||
 | 
			
		||||
nano::election::election (nano::node & node_a, std::shared_ptr<nano::block> block_a, bool const skip_delay_a, std::function<void(std::shared_ptr<nano::block>)> const & confirmation_action_a) :
 | 
			
		||||
confirmation_action (confirmation_action_a),
 | 
			
		||||
confirmed_m (false),
 | 
			
		||||
node (node_a),
 | 
			
		||||
election_start (std::chrono::steady_clock::now ()),
 | 
			
		||||
status ({ block_a, 0, std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now ().time_since_epoch ()), std::chrono::duration_values<std::chrono::milliseconds>::zero (), 0, 1, 0, nano::election_status_type::ongoing }),
 | 
			
		||||
skip_delay (skip_delay_a),
 | 
			
		||||
confirmed (false),
 | 
			
		||||
stopped (false)
 | 
			
		||||
{
 | 
			
		||||
	last_votes.emplace (node.network_params.random.not_an_account, nano::vote_info{ std::chrono::steady_clock::now (), 0, block_a->hash () });
 | 
			
		||||
| 
						 | 
				
			
			@ -26,7 +26,7 @@ stopped (false)
 | 
			
		|||
void nano::election::confirm_once (nano::election_status_type type_a)
 | 
			
		||||
{
 | 
			
		||||
	assert (!node.active.mutex.try_lock ());
 | 
			
		||||
	if (!confirmed.exchange (true))
 | 
			
		||||
	if (!confirmed_m.exchange (true))
 | 
			
		||||
	{
 | 
			
		||||
		status.election_end = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now ().time_since_epoch ());
 | 
			
		||||
		status.election_duration = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::steady_clock::now () - election_start);
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +51,7 @@ void nano::election::confirm_once (nano::election_status_type type_a)
 | 
			
		|||
void nano::election::stop ()
 | 
			
		||||
{
 | 
			
		||||
	assert (!node.active.mutex.try_lock ());
 | 
			
		||||
	if (!stopped && !confirmed)
 | 
			
		||||
	if (!stopped && !confirmed ())
 | 
			
		||||
	{
 | 
			
		||||
		stopped = true;
 | 
			
		||||
		status.election_end = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now ().time_since_epoch ());
 | 
			
		||||
| 
						 | 
				
			
			@ -63,6 +63,11 @@ void nano::election::stop ()
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool nano::election::confirmed ()
 | 
			
		||||
{
 | 
			
		||||
	return confirmed_m;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool nano::election::have_quorum (nano::tally_t const & tally_a, nano::uint128_t tally_sum) const
 | 
			
		||||
{
 | 
			
		||||
	bool result = false;
 | 
			
		||||
| 
						 | 
				
			
			@ -189,7 +194,7 @@ nano::election_vote_result nano::election::vote (nano::account rep, uint64_t seq
 | 
			
		|||
		{
 | 
			
		||||
			node.stats.inc (nano::stat::type::election, nano::stat::detail::vote_new);
 | 
			
		||||
			last_votes[rep] = { std::chrono::steady_clock::now (), sequence, block_hash };
 | 
			
		||||
			if (!confirmed)
 | 
			
		||||
			if (!confirmed ())
 | 
			
		||||
			{
 | 
			
		||||
				confirm_if_quorum ();
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -260,7 +265,7 @@ void nano::election::update_dependent ()
 | 
			
		|||
	for (auto & block_search : blocks_search)
 | 
			
		||||
	{
 | 
			
		||||
		auto existing (node.active.blocks.find (block_search));
 | 
			
		||||
		if (existing != node.active.blocks.end () && !existing->second->confirmed && !existing->second->stopped)
 | 
			
		||||
		if (existing != node.active.blocks.end () && !existing->second->confirmed () && !existing->second->stopped)
 | 
			
		||||
		{
 | 
			
		||||
			if (existing->second->dependent_blocks.find (hash) == existing->second->dependent_blocks.end ())
 | 
			
		||||
			{
 | 
			
		||||
| 
						 | 
				
			
			@ -287,7 +292,7 @@ void nano::election::clear_blocks ()
 | 
			
		|||
		auto erased (node.active.blocks.erase (hash));
 | 
			
		||||
		(void)erased;
 | 
			
		||||
		// clear_blocks () can be called in active_transactions::publish () before blocks insertion if election was confirmed
 | 
			
		||||
		assert (erased == 1 || confirmed);
 | 
			
		||||
		assert (erased == 1 || confirmed ());
 | 
			
		||||
		node.active.erase_inactive_votes_cache (hash);
 | 
			
		||||
		// Notify observers about dropped elections & blocks lost confirmed elections
 | 
			
		||||
		if (stopped || hash != winner_hash)
 | 
			
		||||
| 
						 | 
				
			
			@ -308,7 +313,7 @@ void nano::election::insert_inactive_votes_cache (nano::block_hash const & hash_
 | 
			
		|||
			node.stats.inc (nano::stat::type::election, nano::stat::detail::vote_cached);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (!confirmed && !cache.voters.empty ())
 | 
			
		||||
	if (!confirmed () && !cache.voters.empty ())
 | 
			
		||||
	{
 | 
			
		||||
		auto delay (std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now () - cache.arrival));
 | 
			
		||||
		if (delay > late_blocks_delay)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,6 +51,7 @@ public:
 | 
			
		|||
class election final : public std::enable_shared_from_this<nano::election>
 | 
			
		||||
{
 | 
			
		||||
	std::function<void(std::shared_ptr<nano::block>)> confirmation_action;
 | 
			
		||||
	std::atomic<bool> confirmed_m;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
	election (nano::node &, std::shared_ptr<nano::block>, bool const, std::function<void(std::shared_ptr<nano::block>)> const &);
 | 
			
		||||
| 
						 | 
				
			
			@ -69,13 +70,13 @@ public:
 | 
			
		|||
	void clear_blocks ();
 | 
			
		||||
	void insert_inactive_votes_cache (nano::block_hash const &);
 | 
			
		||||
	void stop ();
 | 
			
		||||
	bool confirmed ();
 | 
			
		||||
	nano::node & node;
 | 
			
		||||
	std::unordered_map<nano::account, nano::vote_info> last_votes;
 | 
			
		||||
	std::unordered_map<nano::block_hash, std::shared_ptr<nano::block>> blocks;
 | 
			
		||||
	std::chrono::steady_clock::time_point election_start;
 | 
			
		||||
	nano::election_status status;
 | 
			
		||||
	bool skip_delay;
 | 
			
		||||
	std::atomic<bool> confirmed;
 | 
			
		||||
	bool stopped;
 | 
			
		||||
	std::unordered_map<nano::block_hash, nano::uint128_t> last_tally;
 | 
			
		||||
	unsigned confirmation_request_count{ 0 };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1747,7 +1747,7 @@ void nano::json_handler::confirmation_active ()
 | 
			
		|||
		nano::lock_guard<std::mutex> lock (node.active.mutex);
 | 
			
		||||
		for (auto i (node.active.roots.begin ()), n (node.active.roots.end ()); i != n; ++i)
 | 
			
		||||
		{
 | 
			
		||||
			if (i->election->confirmation_request_count >= announcements && !i->election->confirmed && !i->election->stopped)
 | 
			
		||||
			if (i->election->confirmation_request_count >= announcements && !i->election->confirmed () && !i->election->stopped)
 | 
			
		||||
			{
 | 
			
		||||
				boost::property_tree::ptree entry;
 | 
			
		||||
				entry.put ("", i->root.to_string ());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue