forked from hyperledger-iroha/iroha-dco
-
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.
Merge pull request hyperledger-iroha#385 from hyperledger/feature/net…
…work-stubs Network stubs
- Loading branch information
Showing
13 changed files
with
246 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_library(networking | ||
impl/network_api.cpp | ||
) | ||
|
||
target_link_libraries(networking PUBLIC | ||
rxcpp | ||
dao | ||
) |
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,17 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include <network/network_api.h> |
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,87 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#ifndef IROHA_NETWORK_H | ||
#define IROHA_NETWORK_H | ||
|
||
#include "dao/dao.hpp" | ||
#include "rxcpp/rx-observable.hpp" | ||
|
||
namespace iroha { | ||
namespace network { | ||
|
||
/** | ||
* Interface provides methods for fetching consensus-related data. | ||
*/ | ||
class ConsensusListener { | ||
public: | ||
/** | ||
* Event is triggered when proposal arrives from network. | ||
* @return observable with Proposals. | ||
* (List of Proposals) | ||
*/ | ||
virtual rxcpp::observable<iroha::dao::Proposal> on_proposal() = 0; | ||
|
||
/** | ||
* Event is triggered when commit block arrives. | ||
* @return observable with sequence of committed blocks. | ||
* In common case observable<Block> will contain one element. | ||
* But there are scenarios when consensus provide many blocks, e.g. | ||
* on peer startup - peer will get all actual blocks. | ||
*/ | ||
virtual rxcpp::observable<rxcpp::observable<iroha::dao::Block>> | ||
on_commit() = 0; | ||
}; | ||
|
||
/** | ||
* Interface for downloading blocks from a network | ||
*/ | ||
class BlockLoaderApi { | ||
public: | ||
/** | ||
* Method requests missed blocks from external peer starting from it's top | ||
* block. | ||
* Note, that blocks will be in order: from the newest | ||
* to your actual top block. | ||
* This order is required for verify blocks before storing in a ledger. | ||
* @param target_peer - peer for requesting blocks | ||
* @param topBlock - your last actual block | ||
* @return observable with blocks | ||
*/ | ||
virtual rxcpp::observable<iroha::dao::Block> requestBlocks( | ||
iroha::dao::Peer &target_peer, iroha::dao::Block &topBlock) = 0; | ||
}; | ||
|
||
/** | ||
* Interface for propagating transaction in a network | ||
*/ | ||
class TransactionPropagator { | ||
public: | ||
/** | ||
* Method spreads transaction to other members of a network | ||
* @param tx - transaction for propagation | ||
*/ | ||
virtual void propagate_transaction(iroha::dao::Transaction &tx) = 0; | ||
}; | ||
|
||
/** | ||
* Public API interface for communication between current peer and other | ||
* peers in a network | ||
*/ | ||
class PeerCommunicationService : public TransactionPropagator, | ||
public ConsensusListener {}; | ||
} | ||
} | ||
#endif // IROHA_NETWORK_H |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ ADD_LIBRARY(stateful_validator STATIC validator.cpp) | |
|
||
target_link_libraries(stateful_validator | ||
schema | ||
optional | ||
) |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ add_library(dao | |
|
||
target_link_libraries(dao | ||
schema | ||
optional | ||
) |
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 was deleted.
Oops, something went wrong.
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,110 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "rxcpp/rx.hpp" | ||
namespace Rx { | ||
using namespace rxcpp; | ||
using namespace rxcpp::sources; | ||
using namespace rxcpp::operators; | ||
using namespace rxcpp::util; | ||
} | ||
using namespace Rx; | ||
|
||
#include <regex> | ||
#include <random> | ||
using namespace std; | ||
using namespace std::chrono; | ||
|
||
TEST(rxcppTest, usage_observable_test) { | ||
random_device rd; // non-deterministic generator | ||
mt19937 gen(rd()); | ||
uniform_int_distribution<> dist(4, 18); | ||
|
||
// for testing purposes, produce byte stream that from lines of text | ||
auto bytes = range(0, 10) | | ||
flat_map([&](int i){ | ||
auto body = from((uint8_t)('A' + i)) | | ||
repeat(dist(gen)) | | ||
as_dynamic(); | ||
auto delim = from((uint8_t)'\r'); | ||
return from(body, delim) | concat(); | ||
}) | | ||
window(17) | | ||
flat_map([](observable<uint8_t> w){ | ||
return w | | ||
reduce( | ||
vector<uint8_t>(), | ||
[](vector<uint8_t> v, uint8_t b){ | ||
v.push_back(b); | ||
return v; | ||
}) | | ||
as_dynamic(); | ||
}) | | ||
tap([](vector<uint8_t>& v){ | ||
// print input packet of bytes | ||
copy(v.begin(), v.end(), ostream_iterator<long>(cout, " ")); | ||
cout << endl; | ||
}); | ||
|
||
// | ||
// recover lines of text from byte stream | ||
// | ||
|
||
auto removespaces = [](string s){ | ||
s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end()); | ||
return s; | ||
}; | ||
|
||
// create strings split on \r | ||
auto strings = bytes | | ||
concat_map([](vector<uint8_t> v){ | ||
string s(v.begin(), v.end()); | ||
regex delim(R"/(\r)/"); | ||
cregex_token_iterator cursor(&s[0], &s[0] + s.size(), delim, {-1, 0}); | ||
cregex_token_iterator end; | ||
vector<string> splits(cursor, end); | ||
return iterate(move(splits)); | ||
}) | | ||
filter([](const string& s){ | ||
return !s.empty(); | ||
}) | | ||
publish() | | ||
ref_count(); | ||
|
||
// filter to last string in each line | ||
auto closes = strings | | ||
filter( | ||
[](const string& s){ | ||
return s.back() == '\r'; | ||
}) | | ||
Rx::map([](const string&){return 0;}); | ||
|
||
// group strings by line | ||
auto linewindows = strings | | ||
window_toggle(closes | start_with(0), [=](int){return closes;}); | ||
|
||
// reduce the strings for a line into one string | ||
auto lines = linewindows | | ||
flat_map([&](observable<string> w) { | ||
return w | start_with<string>("") | sum() | Rx::map(removespaces); | ||
}); | ||
|
||
// print result | ||
lines | | ||
subscribe<string>(println(cout)); | ||
} |