Skip to content

Commit

Permalink
Make confirm_back () non-recursive (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiySW authored Feb 20, 2019
1 parent c0356a4 commit 42bc837
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3036,9 +3036,8 @@ void nano::election::compute_rep_votes (nano::transaction const & transaction_a)
}
}

void nano::election::confirm_once (nano::transaction const & transaction_a, uint8_t & depth_a)
void nano::election::confirm_once (nano::transaction const & transaction_a, bool confirmed_back)
{
depth_a++;
if (!confirmed.exchange (true))
{
status.election_end = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now ().time_since_epoch ());
Expand All @@ -3050,22 +3049,30 @@ void nano::election::confirm_once (nano::transaction const & transaction_a, uint
node_l->process_confirmed (winner_l);
confirmation_action_l (winner_l);
});
confirm_back (transaction_a, depth_a);
if (!confirmed_back)
{
confirm_back (transaction_a);
}
}
}

void nano::election::confirm_back (nano::transaction const & transaction_a, uint8_t & depth_a)
void nano::election::confirm_back (nano::transaction const & transaction_a)
{
std::vector<nano::block_hash> hashes = { status.winner->previous (), status.winner->source (), status.winner->link () };
for (auto & hash : hashes)
std::deque<nano::block_hash> hashes = { status.winner->previous (), status.winner->source (), status.winner->link () };
while (!hashes.empty ())
{
// Depth is limited to 200
if (!hash.is_zero () && !node.ledger.is_epoch_link (hash) && depth_a < 200)
auto hash (hashes.front ());
hashes.pop_front ();
if (!hash.is_zero () && !node.ledger.is_epoch_link (hash))
{
auto existing (node.active.blocks.find (hash));
if (existing != node.active.blocks.end () && !existing->second->confirmed && !existing->second->stopped && existing->second->blocks.size () == 1)
{
existing->second->confirm_once (transaction_a, depth_a);
release_assert (existing->second->status.winner->hash () == hash);
existing->second->confirm_once (transaction_a, true); // Avoid recursive actions
hashes.push_back (existing->second->status.winner->previous ());
hashes.push_back (existing->second->status.winner->source ());
hashes.push_back (existing->second->status.winner->link ());
}
}
}
Expand Down Expand Up @@ -3135,8 +3142,7 @@ void nano::election::confirm_if_quorum (nano::transaction const & transaction_a)
{
log_votes (tally_l);
}
uint8_t depth (0);
confirm_once (transaction_a, depth);
confirm_once (transaction_a);
}
}

Expand Down
4 changes: 2 additions & 2 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class election_vote_result
class election : public std::enable_shared_from_this<nano::election>
{
std::function<void(std::shared_ptr<nano::block>)> confirmation_action;
void confirm_once (nano::transaction const &, uint8_t &);
void confirm_back (nano::transaction const &, uint8_t &);
void confirm_once (nano::transaction const &, bool = false);
void confirm_back (nano::transaction const &);

public:
election (nano::node &, std::shared_ptr<nano::block>, std::function<void(std::shared_ptr<nano::block>)> const &);
Expand Down

0 comments on commit 42bc837

Please sign in to comment.