forked from nanocurrency/nano-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
block_broadcast
logic cleaner and more robust
- Loading branch information
1 parent
8cae757
commit e22434b
Showing
3 changed files
with
96 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,85 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/blocks.hpp> | ||
#include <nano/lib/locks.hpp> | ||
|
||
#include <boost/multi_index/hashed_index.hpp> | ||
#include <boost/multi_index/ordered_index.hpp> | ||
#include <boost/multi_index_container.hpp> | ||
|
||
#include <memory> | ||
#include <thread> | ||
#include <unordered_set> | ||
|
||
namespace mi = boost::multi_index; | ||
|
||
namespace nano | ||
{ | ||
class block_arrival; | ||
class block_processor; | ||
class network; | ||
// This class tracks blocks that originated from this node. | ||
} | ||
|
||
namespace nano | ||
{ | ||
/** | ||
* Broadcasts blocks to the network | ||
* Tracks local blocks for more aggressive propagation | ||
*/ | ||
class block_broadcast | ||
{ | ||
public: | ||
block_broadcast (nano::network & network, nano::block_arrival & block_arrival, bool enabled = false); | ||
// Add batch_processed observer to block_processor if enabled | ||
void connect (nano::block_processor & block_processor); | ||
block_broadcast (nano::block_processor &, nano::network &, nano::block_arrival &, bool enabled = false); | ||
|
||
// Mark a block as originating locally | ||
void set_local (std::shared_ptr<nano::block> block); | ||
void erase (std::shared_ptr<nano::block> block); | ||
void track_local (nano::block_hash const &); | ||
|
||
private: // Dependencies | ||
nano::block_processor & block_processor; | ||
nano::network & network; | ||
nano::block_arrival & block_arrival; | ||
|
||
private: | ||
// Block_processor observer | ||
void observe (std::shared_ptr<nano::block> block); | ||
void observe (std::shared_ptr<nano::block> const & block); | ||
|
||
nano::network & network; | ||
nano::block_arrival & block_arrival; | ||
std::unordered_set<std::shared_ptr<nano::block>> local; // Blocks originated on this node | ||
nano::mutex mutex; | ||
bool enabled; | ||
void run (); | ||
|
||
private: | ||
class hash_tracker | ||
{ | ||
public: | ||
void add (nano::block_hash const &); | ||
void erase (nano::block_hash const &); | ||
bool contains (nano::block_hash const &) const; | ||
|
||
private: | ||
mutable nano::mutex mutex; | ||
|
||
// clang-format off | ||
class tag_sequenced {}; | ||
class tag_hash {}; | ||
|
||
using ordered_hashes = boost::multi_index_container<nano::block_hash, | ||
mi::indexed_by< | ||
mi::sequenced<mi::tag<tag_sequenced>>, | ||
mi::hashed_unique<mi::tag<tag_hash>, | ||
mi::identity<nano::block_hash>> | ||
>>; | ||
// clang-format on | ||
|
||
// Blocks originated on this node | ||
ordered_hashes hashes; | ||
|
||
static std::size_t constexpr max_size = 1024 * 128; | ||
}; | ||
|
||
hash_tracker local; | ||
|
||
bool enabled{ false }; | ||
bool stopped{ false }; | ||
nano::condition_variable condition; | ||
mutable nano::mutex mutex; | ||
std::thread thread; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters