diff --git a/rai/core/core.cpp b/rai/core/core.cpp index c97261f6..21f5bdbb 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -2634,21 +2634,10 @@ void rai::bulk_pull_client::received_type () } } -rai::block_path::block_path (std::vector > & path_a, std::unordered_map > & blocks_a) : +rai::block_path::block_path (std::vector > & path_a, std::function (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 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 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 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 (); } diff --git a/rai/core/core.hpp b/rai/core/core.hpp index 8107ceb1..10ba7c1d 100644 --- a/rai/core/core.hpp +++ b/rai/core/core.hpp @@ -403,14 +403,13 @@ namespace rai { class block_path : public rai::block_visitor { public: - block_path (std::vector > &, std::unordered_map > &); + block_path (std::vector > &, std::function (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 > & path; - std::unordered_map > & blocks_m; std::function (rai::block_hash const &)> retrieve; }; class bootstrap_client : public std::enable_shared_from_this diff --git a/rai/core_test/block_path.cpp b/rai/core_test/block_path.cpp index 09080940..696e6714 100644 --- a/rai/core_test/block_path.cpp +++ b/rai/core_test/block_path.cpp @@ -5,7 +5,17 @@ TEST (block_path, zero) { std::vector > path; std::unordered_map > blocks; - rai::block_path block_path (path, blocks); + rai::block_path block_path (path, [&blocks] (rai::block_hash const & hash_a) + { + std::unique_ptr 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 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 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 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 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 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 ());