-
Notifications
You must be signed in to change notification settings - Fork 3
refactor "network module" #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1601005
getStatusMessage
turuslan d13da66
SimpleSubscription
turuslan f91218a
rename
turuslan 159b55f
move types
turuslan 0292c3c
split code
turuslan fb4c427
remove unused
turuslan cf049f3
qtils tag
turuslan 0db5e6b
Merge remote-tracking branch 'origin/master' into refactor/network
turuslan fa8ac0e
rename
turuslan 01c69cc
empty
turuslan df4f161
rename
turuslan 4e83dd3
deprecated md
turuslan 448cb65
Merge branch 'master' into refactor/network
kamilsa c17e7f3
pragma once
turuslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| /** | ||
| * Copyright Quadrivium LLC | ||
| * All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "modules/networking/block_request_protocol.hpp" | ||
|
|
||
| #include <libp2p/basic/read_varint.hpp> | ||
| #include <libp2p/basic/write_varint.hpp> | ||
| #include <libp2p/coro/spawn.hpp> | ||
| #include <libp2p/host/basic_host.hpp> | ||
|
|
||
| #include "blockchain/block_tree.hpp" | ||
| #include "modules/networking/ssz_snappy.hpp" | ||
|
|
||
| namespace lean::modules { | ||
| BlockRequestProtocol::BlockRequestProtocol( | ||
| std::shared_ptr<boost::asio::io_context> io_context, | ||
| std::shared_ptr<libp2p::host::BasicHost> host, | ||
| qtils::SharedRef<blockchain::BlockTree> block_tree) | ||
| : io_context_{std::move(io_context)}, | ||
| host_{std::move(host)}, | ||
| block_tree_{std::move(block_tree)} {} | ||
|
|
||
| libp2p::StreamProtocols BlockRequestProtocol::getProtocolIds() const { | ||
| return {"/leanconsensus/req/blocks_by_root/1/ssz_snappy"}; | ||
| } | ||
|
|
||
| void BlockRequestProtocol::handle(std::shared_ptr<libp2p::Stream> stream) { | ||
| libp2p::coroSpawn( | ||
| *io_context_, | ||
| [self{shared_from_this()}, stream]() -> libp2p::Coro<void> { | ||
| std::ignore = co_await self->coroRespond(stream); | ||
| }); | ||
| } | ||
|
|
||
| void BlockRequestProtocol::start() { | ||
| host_->listenProtocol(shared_from_this()); | ||
| } | ||
|
|
||
| libp2p::CoroOutcome<BlockResponse> BlockRequestProtocol::request( | ||
| libp2p::PeerId peer_id, BlockRequest request) { | ||
| BOOST_OUTCOME_CO_TRY(auto stream, | ||
| co_await host_->newStream(peer_id, getProtocolIds())); | ||
| BOOST_OUTCOME_CO_TRY( | ||
| co_await libp2p::writeVarintMessage(stream, encodeSszSnappy(request))); | ||
| qtils::ByteVec encoded; | ||
| BOOST_OUTCOME_CO_TRY(co_await libp2p::readVarintMessage(stream, encoded)); | ||
| BOOST_OUTCOME_CO_TRY(auto response, | ||
| decodeSszSnappy<BlockResponse>(encoded)); | ||
| co_return response; | ||
| } | ||
|
|
||
| libp2p::CoroOutcome<void> BlockRequestProtocol::coroRespond( | ||
| std::shared_ptr<libp2p::Stream> stream) { | ||
| qtils::ByteVec encoded; | ||
| BOOST_OUTCOME_CO_TRY(co_await libp2p::readVarintMessage(stream, encoded)); | ||
| BOOST_OUTCOME_CO_TRY(auto request, decodeSszSnappy<BlockRequest>(encoded)); | ||
| BlockResponse response; | ||
| for (auto &block_hash : request.blocks) { | ||
| BOOST_OUTCOME_CO_TRY(auto block, | ||
| block_tree_->tryGetSignedBlock(block_hash)); | ||
| if (block.has_value()) { | ||
| response.blocks.push_back(std::move(block.value())); | ||
| } | ||
| } | ||
| BOOST_OUTCOME_CO_TRY( | ||
| co_await libp2p::writeVarintMessage(stream, encodeSszSnappy(response))); | ||
| co_return outcome::success(); | ||
| } | ||
| } // namespace lean::modules |
This file contains hidden or 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 @@ | ||
| /** | ||
| * Copyright Quadrivium LLC | ||
| * All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include <libp2p/protocol/base_protocol.hpp> | ||
| #include <qtils/shared_ref.hpp> | ||
|
|
||
| #include "modules/networking/types.hpp" | ||
|
|
||
| namespace boost::asio { | ||
| class io_context; | ||
| } // namespace boost::asio | ||
|
|
||
| namespace libp2p::host { | ||
| class BasicHost; | ||
| } // namespace libp2p::host | ||
|
|
||
| namespace lean::blockchain { | ||
| class BlockTree; | ||
| } // namespace lean::blockchain | ||
|
|
||
| namespace lean::modules { | ||
| class BlockRequestProtocol | ||
| : public std::enable_shared_from_this<BlockRequestProtocol>, | ||
| public libp2p::protocol::BaseProtocol { | ||
| public: | ||
| BlockRequestProtocol(std::shared_ptr<boost::asio::io_context> io_context, | ||
| std::shared_ptr<libp2p::host::BasicHost> host, | ||
| qtils::SharedRef<blockchain::BlockTree> block_tree); | ||
|
|
||
| // BaseProtocol | ||
| libp2p::StreamProtocols getProtocolIds() const override; | ||
| void handle(std::shared_ptr<libp2p::Stream> stream) override; | ||
|
|
||
| void start(); | ||
|
|
||
| libp2p::CoroOutcome<BlockResponse> request(libp2p::PeerId peer_id, | ||
| BlockRequest request); | ||
|
|
||
| private: | ||
| libp2p::CoroOutcome<void> coroRespond( | ||
| std::shared_ptr<libp2p::Stream> stream); | ||
|
|
||
| std::shared_ptr<boost::asio::io_context> io_context_; | ||
| std::shared_ptr<libp2p::host::BasicHost> host_; | ||
| qtils::SharedRef<blockchain::BlockTree> block_tree_; | ||
| }; | ||
| } // namespace lean::modules |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.