-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4167 from clemahieu/block_origin_remove
Add batch block processing result observer to block_processor
- Loading branch information
Showing
18 changed files
with
364 additions
and
144 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
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
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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <nano/node/block_arrival.hpp> | ||
#include <nano/node/block_broadcast.hpp> | ||
#include <nano/node/blockprocessor.hpp> | ||
#include <nano/node/network.hpp> | ||
|
||
nano::block_broadcast::block_broadcast (nano::network & network, nano::block_arrival & block_arrival) : | ||
network{ network }, | ||
block_arrival{ block_arrival } | ||
{ | ||
} | ||
|
||
void nano::block_broadcast::connect (nano::block_processor & block_processor, bool enabled) | ||
{ | ||
if (!enabled) | ||
{ | ||
return; | ||
} | ||
block_processor.processed.add ([this] (auto const & result, auto const & block) { | ||
switch (result.code) | ||
{ | ||
case nano::process_result::progress: | ||
observe (block); | ||
break; | ||
default: | ||
break; | ||
} | ||
erase (block); | ||
}); | ||
} | ||
|
||
void nano::block_broadcast::observe (std::shared_ptr<nano::block> block) | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
auto existing = local.find (block); | ||
auto local_l = existing != local.end (); | ||
lock.unlock (); | ||
if (local_l) | ||
{ | ||
// Block created on this node | ||
// Perform more agressive initial flooding | ||
network.flood_block_initial (block); | ||
} | ||
else | ||
{ | ||
if (block_arrival.recent (block->hash ())) | ||
{ | ||
// Block arrived from realtime traffic, do normal gossip. | ||
network.flood_block (block, nano::transport::buffer_drop_policy::limiter); | ||
} | ||
else | ||
{ | ||
// Block arrived from bootstrap | ||
// Don't broadcast blocks we're bootstrapping | ||
} | ||
} | ||
} | ||
|
||
void nano::block_broadcast::set_local (std::shared_ptr<nano::block> block) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
local.insert (block); | ||
} | ||
|
||
void nano::block_broadcast::erase (std::shared_ptr<nano::block> block) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
local.erase (block); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/blocks.hpp> | ||
|
||
#include <memory> | ||
#include <unordered_set> | ||
|
||
namespace nano | ||
{ | ||
class block_arrival; | ||
class block_processor; | ||
class network; | ||
// This class tracks blocks that originated from this node. | ||
class block_broadcast | ||
{ | ||
public: | ||
block_broadcast (nano::network & network, nano::block_arrival & block_arrival); | ||
// Add batch_processed observer to block_processor if enabled | ||
void connect (nano::block_processor & block_processor, bool enabled); | ||
// Block_processor observer | ||
void observe (std::shared_ptr<nano::block> block); | ||
// Mark a block as originating locally | ||
void set_local (std::shared_ptr<nano::block> block); | ||
void erase (std::shared_ptr<nano::block> block); | ||
|
||
private: | ||
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; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <nano/node/blocking_observer.hpp> | ||
#include <nano/node/blockprocessor.hpp> | ||
|
||
void nano::blocking_observer::connect (nano::block_processor & block_processor) | ||
{ | ||
block_processor.processed.add ([this] (auto const & result, auto const & block) { | ||
observe (result, block); | ||
}); | ||
} | ||
|
||
void nano::blocking_observer::stop () | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
stopped = true; | ||
auto discard = std::move (blocking); | ||
// Signal broken promises outside lock | ||
lock.unlock (); | ||
discard.clear (); // ~promise future_error | ||
} | ||
|
||
void nano::blocking_observer::observe (nano::process_return const & result, std::shared_ptr<nano::block> block) | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
auto existing = blocking.find (block); | ||
if (existing != blocking.end ()) | ||
{ | ||
auto promise = std::move (existing->second); | ||
blocking.erase (existing); | ||
// Signal promise outside of lock | ||
lock.unlock (); | ||
promise.set_value (result); | ||
} | ||
} | ||
|
||
std::future<nano::process_return> nano::blocking_observer::insert (std::shared_ptr<nano::block> block) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
if (stopped) | ||
{ | ||
std::promise<nano::process_return> promise; | ||
return promise.get_future (); // ~promise future_error | ||
} | ||
auto iterator = blocking.emplace (block, std::promise<nano::process_return>{}); | ||
return iterator->second.get_future (); | ||
} | ||
|
||
bool nano::blocking_observer::exists (std::shared_ptr<nano::block> block) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
auto existing = blocking.find (block); | ||
return existing != blocking.end (); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/locks.hpp> | ||
#include <nano/secure/common.hpp> | ||
|
||
#include <future> | ||
#include <memory> | ||
#include <unordered_map> | ||
|
||
namespace nano | ||
{ | ||
class block; | ||
class block_processor; | ||
// Observer that facilitates a blocking call to block processing which is done asynchronosly by the block_processor | ||
class blocking_observer | ||
{ | ||
public: | ||
void connect (nano::block_processor & block_processor); | ||
// Stop the observer and trigger broken promise exceptions | ||
void stop (); | ||
// Block processor observer | ||
void observe (nano::process_return const & result, std::shared_ptr<nano::block> block); | ||
[[nodiscard]] std::future<nano::process_return> insert (std::shared_ptr<nano::block> block); | ||
bool exists (std::shared_ptr<nano::block> block); | ||
|
||
private: | ||
std::unordered_multimap<std::shared_ptr<nano::block>, std::promise<nano::process_return>> blocking; | ||
bool stopped{ false }; | ||
nano::mutex mutex; | ||
}; | ||
} |
Oops, something went wrong.