Extract synchronize function to send one at a time.

This commit is contained in:
clemahieu 2015-01-05 20:21:43 -06:00
commit a1e911341b
2 changed files with 35 additions and 17 deletions

View file

@ -3446,29 +3446,45 @@ bool rai::block_synchronization::add_dependency (rai::block const & block_a)
return visitor.result;
}
bool rai::block_synchronization::fill_dependencies ()
{
auto result (false);
auto done (false);
while (!result && !done)
{
auto block (retrieve (blocks.top ()));
if (block != nullptr)
{
done = add_dependency (*block);
}
else
{
result = true;
}
}
return result;
}
bool rai::block_synchronization::synchronize_one ()
{
auto result (fill_dependencies ());
if (!result)
{
auto block (retrieve (blocks.top ()));
assert (block != nullptr);
target (*block);
blocks.pop ();
}
return result;
}
bool rai::block_synchronization::synchronize (rai::block_hash const & hash_a)
{
auto result (false);
blocks.push (hash_a);
while (!result && !blocks.empty ())
{
auto block (retrieve (blocks.top ()));
if (block != nullptr)
{
if (add_dependency (*block))
{
target (*block);
blocks.pop ();
}
else
{
// Dependency was added to 'blocks'
}
}
else
{
result = true;
}
result = synchronize_one ();
}
return result;
}

View file

@ -418,6 +418,8 @@ public:
virtual std::unique_ptr <rai::block> retrieve (rai::block_hash const &) = 0;
// return true if all dependencies are synchronized
bool add_dependency (rai::block const &);
bool fill_dependencies ();
bool synchronize_one ();
bool synchronize (rai::block_hash const &);
std::stack <rai::block_hash> blocks;
std::unordered_set <rai::block_hash> sent;