Pulling up retrieve function.

This commit is contained in:
clemahieu 2014-11-25 19:10:32 -06:00
commit 52be0619a9
3 changed files with 81 additions and 23 deletions

View file

@ -2634,21 +2634,10 @@ void rai::bulk_pull_client::received_type ()
}
}
rai::block_path::block_path (std::vector <std::unique_ptr <rai::block>> & path_a, std::unordered_map <rai::block_hash, std::unique_ptr <rai::block>> & blocks_a) :
rai::block_path::block_path (std::vector <std::unique_ptr <rai::block>> & path_a, std::function <std::unique_ptr <rai::block> (rai::block_hash const &)> const & retrieve_a) :
path (path_a),
blocks_m (blocks_a)
retrieve (retrieve_a)
{
retrieve = [this] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks_m.find (hash_a));
if (existing != blocks_m.end ())
{
result = std::move (existing->second);
blocks_m.erase (existing);
}
return result;
};
}
void rai::block_path::send_block (rai::send_block const & block_a)
@ -2662,7 +2651,7 @@ void rai::block_path::send_block (rai::send_block const & block_a)
void rai::block_path::receive_block (rai::receive_block const & block_a)
{
rai::block_path path_l (path, blocks_m);
rai::block_path path_l (path, retrieve);
path_l.generate (block_a.hashables.source);
auto block (retrieve (block_a.hashables.previous));
if (block != nullptr)
@ -2710,7 +2699,17 @@ void rai::bulk_pull_client::process_end ()
while (!blocks.empty ())
{
path.clear ();
rai::block_path filler (path, blocks);
rai::block_path filler (path, [this] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
filler.generate (blocks.begin ()->first);
while (!path.empty ())
{
@ -3486,7 +3485,17 @@ void rai::bulk_push_client::push ()
if (!blocks.empty ())
{
path.clear ();
rai::block_path filler (path, blocks);
rai::block_path filler (path, [this] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
filler.generate (blocks.begin ()->first);
push_block ();
}

View file

@ -403,14 +403,13 @@ namespace rai {
class block_path : public rai::block_visitor
{
public:
block_path (std::vector <std::unique_ptr <rai::block>> &, std::unordered_map <rai::block_hash, std::unique_ptr <rai::block>> &);
block_path (std::vector <std::unique_ptr <rai::block>> &, std::function <std::unique_ptr <rai::block> (rai::block_hash const &)> const &);
void generate (rai::block_hash const &);
void send_block (rai::send_block const &);
void receive_block (rai::receive_block const &);
void open_block (rai::open_block const &);
void change_block (rai::change_block const &);
std::vector <std::unique_ptr <rai::block>> & path;
std::unordered_map <rai::block_hash, std::unique_ptr <rai::block>> & blocks_m;
std::function <std::unique_ptr <rai::block> (rai::block_hash const &)> retrieve;
};
class bootstrap_client : public std::enable_shared_from_this <bootstrap_client>

View file

@ -5,7 +5,17 @@ TEST (block_path, zero)
{
std::vector <std::unique_ptr <rai::block>> path;
std::unordered_map <rai::block_hash, std::unique_ptr <rai::block>> blocks;
rai::block_path block_path (path, blocks);
rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
block_path.generate (0);
ASSERT_EQ (0, path.size ());
ASSERT_EQ (0, blocks.size ());
@ -18,7 +28,17 @@ TEST (block_path, one)
std::unique_ptr <rai::block> block1 (new rai::send_block);
auto hash1 (block1->hash ());
blocks [hash1] = block1->clone ();
rai::block_path block_path (path, blocks);
rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
block_path.generate (hash1);
ASSERT_EQ (1, path.size ());
ASSERT_EQ (0, blocks.size ());
@ -36,7 +56,17 @@ TEST (block_path, two)
block2->hashables.previous = hash1;
auto hash2 (block2->hash ());
blocks [hash2] = block2->clone ();
rai::block_path block_path (path, blocks);
rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
block_path.generate (hash2);
ASSERT_EQ (2, path.size ());
ASSERT_EQ (0, blocks.size ());
@ -61,7 +91,17 @@ TEST (block_path, receive_one)
block3->hashables.source = hash2;
auto hash3 (block3->hash ());
blocks [hash3] = block3->clone ();
rai::block_path block_path (path, blocks);
rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
block_path.generate (hash3);
ASSERT_EQ (3, path.size ());
ASSERT_EQ (0, blocks.size ());
@ -90,7 +130,17 @@ TEST (block_path, receive_two)
block3->hashables.source = hash2;
auto hash3 (block3->hash ());
blocks [hash3] = block3->clone ();
rai::block_path block_path (path, blocks);
rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a)
{
std::unique_ptr <rai::block> result;
auto existing (blocks.find (hash_a));
if (existing != blocks.end ())
{
result = std::move (existing->second);
blocks.erase (existing);
}
return result;
});
block_path.generate (hash3);
ASSERT_EQ (4, path.size ());
ASSERT_EQ (0, blocks.size ());