-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
566 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "network/peer_view.hpp" | ||
#include "blockchain/block_tree.hpp" | ||
#include "common/visitor.hpp" | ||
|
||
namespace kagome::network { | ||
|
||
PeerView::PeerView( | ||
const primitives::events::ChainSubscriptionEnginePtr &chain_events_engine, | ||
std::shared_ptr<application::AppStateManager> asmgr, | ||
std::shared_ptr<blockchain::BlockTree> block_tree) | ||
: chain_events_engine_{chain_events_engine}, | ||
my_view_update_observable_{ | ||
std::make_shared<MyViewSubscriptionEngine>()}, | ||
remote_view_update_observable_{ | ||
std::make_shared<PeerViewSubscriptionEngine>()}, | ||
block_tree_{std::move(block_tree)} { | ||
BOOST_ASSERT(chain_events_engine_); | ||
BOOST_ASSERT(block_tree_); | ||
asmgr->takeControl(*this); | ||
} | ||
|
||
bool PeerView::start() { | ||
return true; | ||
} | ||
|
||
void PeerView::stop() { | ||
if (chain_sub_) { | ||
chain_sub_->unsubscribe(); | ||
} | ||
chain_sub_.reset(); | ||
} | ||
|
||
bool PeerView::prepare() { | ||
chain_sub_ = std::make_shared<primitives::events::ChainEventSubscriber>( | ||
chain_events_engine_); | ||
chain_sub_->subscribe(chain_sub_->generateSubscriptionSetId(), | ||
primitives::events::ChainEventType::kNewHeads); | ||
chain_sub_->setCallback( | ||
[wptr{weak_from_this()}]( | ||
auto /*set_id*/, | ||
auto && /*internal_obj*/, | ||
auto /*event_type*/, | ||
const primitives::events::ChainEventParams &event) { | ||
if (auto self = wptr.lock()) { | ||
if (auto const value = | ||
if_type<const primitives::events::HeadsEventParams>( | ||
event)) { | ||
self->updateMyView( | ||
View{.heads_ = self->block_tree_->getLeaves(), | ||
.finalized_number_ = | ||
self->block_tree_->getLastFinalized().number}); | ||
} | ||
} | ||
}); | ||
return true; | ||
} | ||
|
||
PeerView::MyViewSubscriptionEnginePtr PeerView::getMyViewObservable() { | ||
BOOST_ASSERT(my_view_update_observable_); | ||
return my_view_update_observable_; | ||
} | ||
|
||
PeerView::PeerViewSubscriptionEnginePtr PeerView::getRemoteViewObservable() { | ||
BOOST_ASSERT(remote_view_update_observable_); | ||
return remote_view_update_observable_; | ||
} | ||
|
||
void PeerView::updateMyView(network::View &&view) { | ||
BOOST_ASSERT(my_view_update_observable_); | ||
std::sort(view.heads_.begin(), view.heads_.end()); | ||
if (my_view_ != view) { | ||
my_view_ = std::move(view); | ||
|
||
BOOST_ASSERT(my_view_); | ||
my_view_update_observable_->notify(EventType::kViewUpdated, *my_view_); | ||
} | ||
} | ||
|
||
void PeerView::updateRemoteView(const PeerId &peer_id, network::View &&view) { | ||
auto it = remote_view_.find(peer_id); | ||
if (it == remote_view_.end() || it->second != view) { | ||
auto &ref = remote_view_[peer_id]; | ||
ref = std::move(view); | ||
remote_view_update_observable_->notify( | ||
EventType::kViewUpdated, peer_id, ref); | ||
} | ||
} | ||
|
||
std::optional<std::reference_wrapper<const View>> PeerView::getMyView() | ||
const { | ||
return my_view_; | ||
} | ||
|
||
} // namespace kagome::network |
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,80 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#ifndef KAGOME_PEER_VIEW | ||
#define KAGOME_PEER_VIEW | ||
|
||
#include <libp2p/peer/peer_id.hpp> | ||
#include <memory> | ||
#include <unordered_map> | ||
|
||
#include "application/app_state_manager.hpp" | ||
#include "blockchain/block_tree.hpp" | ||
#include "network/types/collator_messages.hpp" | ||
#include "outcome/outcome.hpp" | ||
#include "primitives/event_types.hpp" | ||
#include "subscription/subscriber.hpp" | ||
#include "subscription/subscription_engine.hpp" | ||
#include "utils/non_copyable.hpp" | ||
|
||
namespace kagome::network { | ||
|
||
class PeerView : public NonCopyable, | ||
public NonMovable, | ||
public std::enable_shared_from_this<PeerView> { | ||
public: | ||
enum struct EventType : uint32_t { kViewUpdated }; | ||
|
||
using PeerId = libp2p::peer::PeerId; | ||
|
||
using MyViewSubscriptionEngine = | ||
subscription::SubscriptionEngine<EventType, bool, network::View>; | ||
using MyViewSubscriptionEnginePtr = | ||
std::shared_ptr<MyViewSubscriptionEngine>; | ||
using MyViewSubscriber = MyViewSubscriptionEngine::SubscriberType; | ||
using MyViewSubscriberPtr = std::shared_ptr<MyViewSubscriber>; | ||
|
||
using PeerViewSubscriptionEngine = subscription:: | ||
SubscriptionEngine<EventType, bool, PeerId, network::View>; | ||
using PeerViewSubscriptionEnginePtr = | ||
std::shared_ptr<PeerViewSubscriptionEngine>; | ||
using PeerViewSubscriber = PeerViewSubscriptionEngine::SubscriberType; | ||
using PeerViewSubscriberPtr = std::shared_ptr<PeerViewSubscriber>; | ||
|
||
PeerView(const primitives::events::ChainSubscriptionEnginePtr | ||
&chain_events_engine, | ||
std::shared_ptr<application::AppStateManager> asmgr, | ||
std::shared_ptr<blockchain::BlockTree> block_tree); | ||
virtual ~PeerView() = default; | ||
|
||
/** | ||
* Object lifetime control subsystem. | ||
*/ | ||
bool start(); | ||
void stop(); | ||
bool prepare(); | ||
|
||
MyViewSubscriptionEnginePtr getMyViewObservable(); | ||
PeerViewSubscriptionEnginePtr getRemoteViewObservable(); | ||
|
||
void updateRemoteView(const PeerId &peer_id, network::View &&view); | ||
std::optional<std::reference_wrapper<const View>> getMyView() const; | ||
|
||
private: | ||
void updateMyView(network::View &&view); | ||
|
||
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_; | ||
std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_; | ||
|
||
MyViewSubscriptionEnginePtr my_view_update_observable_; | ||
PeerViewSubscriptionEnginePtr remote_view_update_observable_; | ||
|
||
std::optional<View> my_view_; | ||
std::unordered_map<PeerId, View> remote_view_; | ||
std::shared_ptr<blockchain::BlockTree> block_tree_; | ||
}; | ||
|
||
} // namespace kagome::network | ||
|
||
#endif // KAGOME_PEER_VIEW |
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
Oops, something went wrong.