From 2c52a854ea5f08d4958e6c4133efc2b32ea60397 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Fri, 9 Feb 2018 13:38:26 +0300 Subject: [PATCH 01/23] add persitent storage for ordering service proposal height Signed-off-by: Alexey Chernyshov --- ...gres_ordering_service_persistent_state.hpp | 83 +++++++++++++++++++ irohad/ametsuchi/impl/storage_impl.cpp | 20 +++-- irohad/ametsuchi/impl/storage_impl.hpp | 13 +++ .../ordering_service_persistent_state.hpp | 45 ++++++++++ irohad/ametsuchi/storage.hpp | 8 ++ irohad/ametsuchi/wsv_query.hpp | 3 +- irohad/main/application.cpp | 7 +- irohad/main/impl/ordering_init.cpp | 23 +++-- irohad/main/impl/ordering_init.hpp | 18 ++-- .../ordering/impl/ordering_service_impl.cpp | 13 ++- .../ordering/impl/ordering_service_impl.hpp | 29 +++++-- .../irohad/ametsuchi/ametsuchi_mocks.hpp | 8 +- ...mock_ordering_service_persistent_state.hpp | 47 +++++++++++ .../ordering/ordering_gate_service_test.cpp | 9 +- .../irohad/ordering/ordering_service_test.cpp | 13 ++- 15 files changed, 299 insertions(+), 40 deletions(-) create mode 100644 irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp create mode 100644 irohad/ametsuchi/ordering_service_persistent_state.hpp create mode 100644 test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp new file mode 100644 index 0000000000..aa02561f06 --- /dev/null +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -0,0 +1,83 @@ +/** + * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved. + * http://soramitsu.co.jp + * + * 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_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP +#define IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP + +#include + +#include "ametsuchi/impl/postgres_wsv_common.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" + +namespace iroha { + namespace ametsuchi { + + class PostgresOrderingServicePersistentState + : public OrderingServicePersistentState { + public: + explicit PostgresOrderingServicePersistentState( + pqxx::nontransaction &transaction) + : transaction_(transaction), + log_(logger::log("PostgresOrderingServicePersistentState")), + execute_{ametsuchi::makeExecute(transaction_, log_)} {} + + /** + * Save proposal height that it can be restored + * after launch + */ + virtual bool saveProposalHeight(size_t height) { + return execute( + "UPDATE ordering_service_state " + "SET proposal_height = " + + transaction_.quote(height) + ";"); + } + + /** + * Load proposal height + */ + virtual boost::optional loadProposalHeight() const { + return execute_("SELECT * FROM ordering_service_state;") | + [&](const auto &result) -> boost::optional { + boost::optional res; + if (result.empty()) { + log_->info("There is no proposal_height in ordering_service_state"); + } else { + size_t height; + auto row = result.at(0); + row.at("proposal_height") >> height; + res = height; + } + return res; + }; + } + + private: + pqxx::nontransaction &transaction_; + logger::Logger log_; + + using ExecuteType = decltype(ametsuchi::makeExecute(transaction_, log_)); + ExecuteType execute_; + + // TODO: refactor to return Result when it is introduced IR-775 + bool execute(const std::string &statement) noexcept { + return static_cast(execute_(statement)); + } + }; + } // namespace ordering +} // namespace iroha + +#endif // IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/irohad/ametsuchi/impl/storage_impl.cpp b/irohad/ametsuchi/impl/storage_impl.cpp index 5f0ce170d3..d7c2e3c17d 100644 --- a/irohad/ametsuchi/impl/storage_impl.cpp +++ b/irohad/ametsuchi/impl/storage_impl.cpp @@ -19,11 +19,12 @@ #include "ametsuchi/impl/flat_file/flat_file.hpp" // for FlatFile #include "ametsuchi/impl/mutable_storage_impl.hpp" -#include "ametsuchi/impl/postgres_wsv_query.hpp" #include "ametsuchi/impl/postgres_block_query.hpp" +#include "ametsuchi/impl/postgres_wsv_query.hpp" #include "ametsuchi/impl/temporary_wsv_impl.hpp" #include "model/converters/json_common.hpp" #include "model/execution/command_executor_factory.hpp" // for CommandExecutorFactory +#include "postgres_ordering_service_persistent_state.hpp" namespace iroha { namespace ametsuchi { @@ -53,7 +54,10 @@ namespace iroha { wsv_transaction_(std::move(wsv_transaction)), wsv_(std::make_shared(*wsv_transaction_)), blocks_(std::make_shared(*wsv_transaction_, - *block_store_)) { + *block_store_)), + ordering_state_( + std::make_shared( + *wsv_transaction_)) { log_ = logger::log("StorageImpl"); wsv_transaction_->exec(init_); @@ -148,6 +152,7 @@ DROP TABLE IF EXISTS height_by_hash; DROP TABLE IF EXISTS height_by_account_set; DROP TABLE IF EXISTS index_by_creator_height; DROP TABLE IF EXISTS index_by_id_height_asset; +DROP TABLE IF EXISTS ordering_service_state; )"; // erase db @@ -167,8 +172,7 @@ DROP TABLE IF EXISTS index_by_id_height_asset; } nonstd::optional StorageImpl::initConnections( - std::string block_store_dir, - std::string postgres_options) { + std::string block_store_dir, std::string postgres_options) { auto log_ = logger::log("StorageImpl:initConnection"); log_->info("Start storage creation"); @@ -200,8 +204,7 @@ DROP TABLE IF EXISTS index_by_id_height_asset; } std::shared_ptr StorageImpl::create( - std::string block_store_dir, - std::string postgres_options) { + std::string block_store_dir, std::string postgres_options) { auto ctx = initConnections(block_store_dir, postgres_options); if (not ctx.has_value()) { return nullptr; @@ -236,5 +239,10 @@ DROP TABLE IF EXISTS index_by_id_height_asset; std::shared_ptr StorageImpl::getBlockQuery() const { return blocks_; } + + std::shared_ptr + StorageImpl::getOrderingServicePersistentState() const { + return ordering_state_; + } } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/storage_impl.hpp b/irohad/ametsuchi/impl/storage_impl.hpp index 5dba50327d..e654148ac4 100644 --- a/irohad/ametsuchi/impl/storage_impl.hpp +++ b/irohad/ametsuchi/impl/storage_impl.hpp @@ -25,13 +25,16 @@ #include #include + #include "logger/logger.hpp" #include "model/converters/json_block_factory.hpp" + namespace iroha { namespace ametsuchi { class FlatFile; + class OrderingServicePersistentState; struct ConnectionContext { ConnectionContext(std::unique_ptr block_store, @@ -66,6 +69,9 @@ namespace iroha { std::shared_ptr getBlockQuery() const override; + std::shared_ptr + getOrderingServicePersistentState() const override; + protected: StorageImpl(std::string block_store_dir, std::string postgres_options, @@ -95,6 +101,8 @@ namespace iroha { std::shared_ptr blocks_; + std::shared_ptr ordering_state_; + model::converters::JsonBlockFactory serializer_; // Allows multiple readers and a single writer @@ -184,6 +192,11 @@ CREATE TABLE IF NOT EXISTS index_by_id_height_asset ( asset_id text, index text ); +CREATE TABLE IF NOT EXISTS ordering_service_state ( + proposal_height bigserial +); +INSERT INTO ordering_service_state +VALUES (2); -- start height (1 for genesis) )"; }; } // namespace ametsuchi diff --git a/irohad/ametsuchi/ordering_service_persistent_state.hpp b/irohad/ametsuchi/ordering_service_persistent_state.hpp new file mode 100644 index 0000000000..dd72eee99c --- /dev/null +++ b/irohad/ametsuchi/ordering_service_persistent_state.hpp @@ -0,0 +1,45 @@ +/** + * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved. + * http://soramitsu.co.jp + * + * 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_ORDERING_SERVICE_PERSISTENT_STATE_HPP +#define IROHA_ORDERING_SERVICE_PERSISTENT_STATE_HPP + +#include + +namespace iroha { + namespace ametsuchi { + + /** + * Interface for Ordering Service persistence + */ + class OrderingServicePersistentState { + public: + /** + * Save proposal height that it can be restored + * after launch + */ + virtual bool saveProposalHeight(size_t height) = 0; + + /** + * Load proposal height + */ + virtual boost::optional loadProposalHeight() const = 0; + }; + } // namespace ordering +} // namespace iroha + +#endif // IROHA_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/irohad/ametsuchi/storage.hpp b/irohad/ametsuchi/storage.hpp index c88d4f23be..539785770a 100644 --- a/irohad/ametsuchi/storage.hpp +++ b/irohad/ametsuchi/storage.hpp @@ -31,6 +31,7 @@ namespace iroha { class BlockQuery; class WsvQuery; + class OrderingServicePersistentState; /** * Storage interface, which allows queries on current committed state, and @@ -42,6 +43,13 @@ namespace iroha { virtual std::shared_ptr getBlockQuery() const = 0; + /** + * Get storage for ordering storage state + * @return storage for ordering service state + */ + virtual std::shared_ptr + getOrderingServicePersistentState() const = 0; + /** * Raw insertion of blocks without validation * @param block - block for insertion diff --git a/irohad/ametsuchi/wsv_query.hpp b/irohad/ametsuchi/wsv_query.hpp index 2f435a6594..14bbd0d55f 100644 --- a/irohad/ametsuchi/wsv_query.hpp +++ b/irohad/ametsuchi/wsv_query.hpp @@ -18,10 +18,10 @@ #ifndef IROHA_WSV_QUERY_HPP #define IROHA_WSV_QUERY_HPP -#include #include #include #include +#include "common/types.hpp" namespace iroha { @@ -81,6 +81,7 @@ namespace iroha { * @return All roles currently in the system */ virtual nonstd::optional> getRoles() = 0; + /** * Get account by user account_id * @param account_id diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index c7e1bd43e4..e20a544f4a 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -156,8 +156,11 @@ void Irohad::initValidators() { * Initializing ordering gate */ void Irohad::initOrderingGate() { - ordering_gate = - ordering_init.initOrderingGate(wsv, max_proposal_size_, proposal_delay_); + ordering_gate = ordering_init.initOrderingGate( + wsv, + max_proposal_size_, + proposal_delay_, + storage->getOrderingServicePersistentState()); log_->info("[Init] => init ordering gate - [{}]", logger::logBool(ordering_gate)); } diff --git a/irohad/main/impl/ordering_init.cpp b/irohad/main/impl/ordering_init.cpp index 5ba8850e95..3b40954d3f 100644 --- a/irohad/main/impl/ordering_init.cpp +++ b/irohad/main/impl/ordering_init.cpp @@ -17,7 +17,7 @@ #include "main/impl/ordering_init.hpp" #include "model/peer.hpp" -#include "ordering/impl/ordering_service_transport_grpc.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" namespace iroha { namespace network { @@ -32,15 +32,23 @@ namespace iroha { std::shared_ptr wsv, size_t max_size, std::chrono::milliseconds delay_milliseconds, - std::shared_ptr transport) { + std::shared_ptr transport, + std::shared_ptr + persistent_state) { return std::make_shared( - wsv, max_size, delay_milliseconds.count(), transport); + wsv, + max_size, + delay_milliseconds.count(), + transport, + persistent_state); } std::shared_ptr OrderingInit::initOrderingGate( std::shared_ptr wsv, size_t max_size, - std::chrono::milliseconds delay_milliseconds) { + std::chrono::milliseconds delay_milliseconds, + std::shared_ptr + persistent_state) { auto ledger_peers = wsv->getLedgerPeers(); if (not ledger_peers or ledger_peers.value().empty()) { log_->error( @@ -53,8 +61,11 @@ namespace iroha { ordering_service_transport = std::make_shared(); - ordering_service = createService( - wsv, max_size, delay_milliseconds, ordering_service_transport); + ordering_service = createService(wsv, + max_size, + delay_milliseconds, + ordering_service_transport, + persistent_state); ordering_service_transport->subscribe(ordering_service); ordering_gate = createGate(ordering_gate_transport); return ordering_gate; diff --git a/irohad/main/impl/ordering_init.hpp b/irohad/main/impl/ordering_init.hpp index 3dddf5a83b..bd8828be1d 100644 --- a/irohad/main/impl/ordering_init.hpp +++ b/irohad/main/impl/ordering_init.hpp @@ -19,14 +19,18 @@ #define IROHA_ORDERING_INIT_HPP #include "ametsuchi/peer_query.hpp" +#include "logger/logger.hpp" #include "ordering/impl/ordering_gate_impl.hpp" #include "ordering/impl/ordering_gate_transport_grpc.hpp" -#include "ordering/impl/ordering_service_transport_grpc.hpp" - -#include "logger/logger.hpp" #include "ordering/impl/ordering_service_impl.hpp" +#include "ordering/impl/ordering_service_transport_grpc.hpp" namespace iroha { + + namespace ametsuchi { + class OrderingServicePersistentState; + } + namespace network { /** @@ -52,7 +56,9 @@ namespace iroha { std::shared_ptr wsv, size_t max_size, std::chrono::milliseconds delay_milliseconds, - std::shared_ptr transport); + std::shared_ptr transport, + std::shared_ptr + persistent_state); public: /** @@ -66,7 +72,9 @@ namespace iroha { std::shared_ptr initOrderingGate( std::shared_ptr wsv, size_t max_size, - std::chrono::milliseconds delay_milliseconds); + std::chrono::milliseconds delay_milliseconds, + std::shared_ptr + persistent_state); std::shared_ptr ordering_service; std::shared_ptr ordering_gate; diff --git a/irohad/ordering/impl/ordering_service_impl.cpp b/irohad/ordering/impl/ordering_service_impl.cpp index 0d9f9733cc..fe25af3431 100644 --- a/irohad/ordering/impl/ordering_service_impl.cpp +++ b/irohad/ordering/impl/ordering_service_impl.cpp @@ -16,6 +16,7 @@ */ #include "ordering/impl/ordering_service_impl.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" #include "model/peer.hpp" namespace iroha { @@ -24,13 +25,17 @@ namespace iroha { std::shared_ptr wsv, size_t max_size, size_t delay_milliseconds, - std::shared_ptr transport) + std::shared_ptr transport, + std::shared_ptr persistent_state) : wsv_(wsv), max_size_(max_size), delay_milliseconds_(delay_milliseconds), transport_(transport), - proposal_height(2) { + persistent_state_(persistent_state) { updateTimer(); + + // restore state of ordering service from persistent storage + proposal_height = persistent_state_->loadProposalHeight().value(); } void OrderingServiceImpl::onTransaction( @@ -53,6 +58,10 @@ namespace iroha { model::Proposal proposal(txs); proposal.height = proposal_height++; + // Save proposal height in persistent storage. + // In case of restart it reloads state. + persistent_state_->saveProposalHeight(proposal_height); + publishProposal(std::move(proposal)); } diff --git a/irohad/ordering/impl/ordering_service_impl.hpp b/irohad/ordering/impl/ordering_service_impl.hpp index 7108743293..55fbef255f 100644 --- a/irohad/ordering/impl/ordering_service_impl.hpp +++ b/irohad/ordering/impl/ordering_service_impl.hpp @@ -18,24 +18,25 @@ #ifndef IROHA_ORDERING_SERVICE_IMPL_HPP #define IROHA_ORDERING_SERVICE_IMPL_HPP -#include #include #include -#include "network/impl/async_grpc_client.hpp" -#include "network/ordering_service.hpp" -#include "network/ordering_service_transport.hpp" +#include +#include #include "ametsuchi/peer_query.hpp" -#include "ordering.grpc.pb.h" - -#include #include "model/converters/pb_transaction_factory.hpp" #include "model/proposal.hpp" #include "network/impl/async_grpc_client.hpp" +#include "network/ordering_service.hpp" +#include "network/ordering_service_transport.hpp" #include "ordering.grpc.pb.h" namespace iroha { + + namespace ametsuchi { + class OrderingServicePersistentState; + } namespace ordering { /** @@ -45,6 +46,7 @@ namespace iroha { * Sends proposal by given timer interval and proposal size * @param delay_milliseconds timer delay * @param max_size proposal size + * @param persistent_state - storage for persistent state of ordering service */ class OrderingServiceImpl : public network::OrderingService { public: @@ -52,7 +54,8 @@ namespace iroha { std::shared_ptr wsv, size_t max_size, size_t delay_milliseconds, - std::shared_ptr transport); + std::shared_ptr transport, + std::shared_ptr persistent_state); /** * Process transaction received from network @@ -102,6 +105,16 @@ namespace iroha { */ const size_t delay_milliseconds_; std::shared_ptr transport_; + + /** + * Persistense storage for proposal counter. + * In case of relaunch, ordering server will enumerate proposals consecutively. + */ + std::shared_ptr persistent_state_; + + /** + * Proposal counter + */ size_t proposal_height; }; } // namespace ordering diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index c7fc0e58c8..657ac8ae3f 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -28,11 +28,10 @@ #include "ametsuchi/temporary_wsv.hpp" #include "ametsuchi/wsv_query.hpp" #include "model/account.hpp" +#include "model/account_asset.hpp" #include "model/asset.hpp" -#include "model/peer.hpp" #include "model/domain.hpp" -#include "model/account_asset.hpp" - +#include "model/peer.hpp" #include @@ -189,6 +188,9 @@ namespace iroha { public: MOCK_CONST_METHOD0(getWsvQuery, std::shared_ptr(void)); MOCK_CONST_METHOD0(getBlockQuery, std::shared_ptr(void)); + MOCK_CONST_METHOD0( + getOrderingServicePersistentState, + std::shared_ptr(void)); MOCK_METHOD0(createTemporaryWsv, std::unique_ptr(void)); MOCK_METHOD0(createMutableStorage, std::unique_ptr(void)); MOCK_METHOD1(doCommit, void(MutableStorage *storage)); diff --git a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp new file mode 100644 index 0000000000..d8885d2033 --- /dev/null +++ b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp @@ -0,0 +1,47 @@ +/** + * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved. + * http://soramitsu.co.jp + * + * 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_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP +#define IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP + +#include "ametsuchi/ordering_service_persistent_state.hpp" + +class MockOrderingServicePersistentState + : public iroha::ametsuchi::OrderingServicePersistentState { + public: + /** + * Save proposal height + */ + virtual bool saveProposalHeight(size_t height) { + height_ = height; + return true; + } + + /** + * Load proposal height + */ + virtual boost::optional loadProposalHeight() const { + return boost::optional(height_); + } + + private: + /** + * Initial height is 2 (1 for genesis block). + */ + size_t height_ = 2; +}; + +#endif // IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index 4abf614df0..26a5381783 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -16,12 +16,13 @@ */ #include "framework/test_subscriber.hpp" +#include "mock_ordering_service_persistent_state.hpp" +#include "model/asset.hpp" #include "module/irohad/ametsuchi/ametsuchi_mocks.hpp" #include "ordering/impl/ordering_gate_impl.hpp" #include "ordering/impl/ordering_gate_transport_grpc.hpp" #include "ordering/impl/ordering_service_impl.hpp" #include "ordering/impl/ordering_service_transport_grpc.hpp" -#include "model/asset.hpp" using namespace iroha::ordering; using namespace iroha::model; @@ -41,6 +42,7 @@ class OrderingGateServiceTest : public ::testing::Test { gate_transport->subscribe(gate); service_transport = std::make_shared(); + persistent_state = std::make_shared(); counter = 2; } @@ -110,6 +112,7 @@ class OrderingGateServiceTest : public ::testing::Test { Peer peer; std::shared_ptr gate_transport; std::shared_ptr service_transport; + std::shared_ptr persistent_state; }; TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { @@ -122,7 +125,7 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { const size_t commit_delay = 400; service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport); + wsv, max_proposal, commit_delay, service_transport, persistent_state); service_transport->subscribe(service); start(); @@ -164,7 +167,7 @@ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { const size_t commit_delay = 1000; service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport); + wsv, max_proposal, commit_delay, service_transport, persistent_state); service_transport->subscribe(service); start(); diff --git a/test/module/irohad/ordering/ordering_service_test.cpp b/test/module/irohad/ordering/ordering_service_test.cpp index f93d97e954..2257742cd6 100644 --- a/test/module/irohad/ordering/ordering_service_test.cpp +++ b/test/module/irohad/ordering/ordering_service_test.cpp @@ -23,9 +23,11 @@ #include "module/irohad/ametsuchi/ametsuchi_mocks.hpp" #include "module/irohad/network/network_mocks.hpp" +#include "mock_ordering_service_persistent_state.hpp" #include "ordering/impl/ordering_gate_impl.hpp" #include "ordering/impl/ordering_gate_transport_grpc.hpp" #include "ordering/impl/ordering_service_impl.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" #include "ordering/impl/ordering_service_transport_grpc.hpp" using namespace iroha; @@ -35,12 +37,12 @@ using namespace iroha::network; using namespace iroha::ametsuchi; using namespace std::chrono_literals; -using ::testing::_; using ::testing::AtLeast; using ::testing::DoAll; using ::testing::Invoke; using ::testing::InvokeWithoutArgs; using ::testing::Return; +using ::testing::_; static logger::Logger log_ = logger::testLog("OrderingService"); @@ -72,9 +74,12 @@ class OrderingServiceTest : public ::testing::Test { void SetUp() override { wsv = std::make_shared(); fake_transport = std::make_shared(); + fake_persistent_state = + std::make_shared(); } std::shared_ptr fake_transport; + std::shared_ptr fake_persistent_state; std::condition_variable cv; std::mutex m; std::string address{"0.0.0.0:50051"}; @@ -90,7 +95,7 @@ TEST_F(OrderingServiceTest, SimpleTest) { const size_t commit_delay = 1000; auto ordering_service = std::make_shared( - wsv, max_proposal, commit_delay, fake_transport); + wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); EXPECT_CALL(*fake_transport, publishProposal(_, _)).Times(1); @@ -103,7 +108,7 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { const size_t commit_delay = 1000; auto ordering_service = std::make_shared( - wsv, max_proposal, commit_delay, fake_transport); + wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); // Init => proposal size 5 => 2 proposals after 10 transactions @@ -136,7 +141,7 @@ TEST_F(OrderingServiceTest, ValidWhenTimerStrategy) { const size_t commit_delay = 400; auto ordering_service = std::make_shared( - wsv, max_proposal, commit_delay, fake_transport); + wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); EXPECT_CALL(*fake_transport, publishProposal(_, _)) From d1b437eb3033127cca4005cb3535ad0cb7ba43e6 Mon Sep 17 00:00:00 2001 From: dumitru Date: Tue, 13 Feb 2018 17:08:42 +0300 Subject: [PATCH 02/23] Set temporary wsv interface to new model Signed-off-by: dumitru --- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 18 +++++++++++------- irohad/ametsuchi/impl/temporary_wsv_impl.hpp | 3 +-- irohad/ametsuchi/temporary_wsv.hpp | 8 +++++++- .../impl/stateful_validator_impl.cpp | 2 ++ 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index c7a47f0968..9111cab9d0 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -18,9 +18,10 @@ #include "ametsuchi/impl/temporary_wsv_impl.hpp" #include "ametsuchi/impl/postgres_wsv_command.hpp" #include "ametsuchi/impl/postgres_wsv_query.hpp" -#include "model/execution/command_executor_factory.hpp" -#include "model/account.hpp" #include "amount/amount.hpp" +#include "backend/protobuf/from_old_model.hpp" +#include "model/account.hpp" +#include "model/execution/command_executor_factory.hpp" namespace iroha { namespace ametsuchi { @@ -38,10 +39,12 @@ namespace iroha { } bool TemporaryWsvImpl::apply( - const model::Transaction &transaction, + const shared_model::interface::Transaction &tx, std::function apply_function) { - const auto &tx_creator = transaction.creator_account_id; + auto transaction = tx.makeOldModel(); + + const auto &tx_creator = transaction->creator_account_id; auto execute_command = [this, &tx_creator](auto command) { auto executor = command_executors_->getCommandExecutor(command); auto account = wsv_->getAccount(tx_creator).value(); @@ -59,15 +62,16 @@ namespace iroha { }; transaction_->exec("SAVEPOINT savepoint_;"); - auto result = apply_function(transaction, *wsv_) - && std::all_of(transaction.commands.begin(), - transaction.commands.end(), + auto result = apply_function(*transaction, *wsv_) + && std::all_of(transaction->commands.begin(), + transaction->commands.end(), execute_command); if (result) { transaction_->exec("RELEASE SAVEPOINT savepoint_;"); } else { transaction_->exec("ROLLBACK TO SAVEPOINT savepoint_;"); } + delete transaction; return result; } diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.hpp b/irohad/ametsuchi/impl/temporary_wsv_impl.hpp index 2e88da762e..36a78cd065 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.hpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.hpp @@ -31,7 +31,6 @@ namespace iroha { } namespace ametsuchi { - class TemporaryWsvImpl : public TemporaryWsv { public: TemporaryWsvImpl( @@ -39,7 +38,7 @@ namespace iroha { std::unique_ptr transaction, std::shared_ptr command_executors); - bool apply(const model::Transaction &transaction, + bool apply(const shared_model::interface::Transaction &, std::function function) override; diff --git a/irohad/ametsuchi/temporary_wsv.hpp b/irohad/ametsuchi/temporary_wsv.hpp index a731b4dd79..767eaba8f6 100644 --- a/irohad/ametsuchi/temporary_wsv.hpp +++ b/irohad/ametsuchi/temporary_wsv.hpp @@ -25,6 +25,12 @@ #include "model/block.hpp" #include "model/transaction.hpp" +namespace shared_model { + namespace interface { + class Transaction; + } // namespace interface +} // namespace shared_model + namespace iroha { namespace ametsuchi { @@ -47,7 +53,7 @@ namespace iroha { * otherwise. * @return True if transaction was successfully applied, false otherwise */ - virtual bool apply(const model::Transaction &transaction, + virtual bool apply(const shared_model::interface::Transaction &, std::function function) = 0; diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index c7d5746a53..a3514c4e94 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -15,6 +15,8 @@ * limitations under the License. */ +#include "validation/impl/stateful_validator_impl.hpp" +#include "backend/protobuf/from_old_model.hpp" #include #include From 8389e123a9ece1fa5c6f6310cb5f5abb30c1e0b7 Mon Sep 17 00:00:00 2001 From: dumitru Date: Thu, 15 Feb 2018 13:09:48 +0300 Subject: [PATCH 03/23] Change function parameter to accept new model Signed-off-by: dumitru --- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 6 +++--- irohad/ametsuchi/impl/temporary_wsv_impl.hpp | 7 ++++--- irohad/ametsuchi/temporary_wsv.hpp | 7 ++++--- .../validation/impl/stateful_validator_impl.cpp | 16 ++++++++++------ test/module/irohad/ametsuchi/ametsuchi_mocks.hpp | 6 +++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index 9111cab9d0..2bcc7540a6 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -40,8 +40,8 @@ namespace iroha { bool TemporaryWsvImpl::apply( const shared_model::interface::Transaction &tx, - std::function - apply_function) { + std::function apply_function) { auto transaction = tx.makeOldModel(); const auto &tx_creator = transaction->creator_account_id; @@ -62,7 +62,7 @@ namespace iroha { }; transaction_->exec("SAVEPOINT savepoint_;"); - auto result = apply_function(*transaction, *wsv_) + auto result = apply_function(tx, *wsv_) && std::all_of(transaction->commands.begin(), transaction->commands.end(), execute_command); diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.hpp b/irohad/ametsuchi/impl/temporary_wsv_impl.hpp index 36a78cd065..f35969205a 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.hpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.hpp @@ -38,9 +38,10 @@ namespace iroha { std::unique_ptr transaction, std::shared_ptr command_executors); - bool apply(const shared_model::interface::Transaction &, - std::function - function) override; + bool apply( + const shared_model::interface::Transaction &, + std::function function) override; ~TemporaryWsvImpl() override; diff --git a/irohad/ametsuchi/temporary_wsv.hpp b/irohad/ametsuchi/temporary_wsv.hpp index 767eaba8f6..3f431be90a 100644 --- a/irohad/ametsuchi/temporary_wsv.hpp +++ b/irohad/ametsuchi/temporary_wsv.hpp @@ -53,9 +53,10 @@ namespace iroha { * otherwise. * @return True if transaction was successfully applied, false otherwise */ - virtual bool apply(const shared_model::interface::Transaction &, - std::function function) = 0; + virtual bool apply( + const shared_model::interface::Transaction &, + std::function function) = 0; virtual ~TemporaryWsv() = default; }; diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index a3514c4e94..8394eee669 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -16,9 +16,9 @@ */ #include "validation/impl/stateful_validator_impl.hpp" -#include "backend/protobuf/from_old_model.hpp" #include #include +#include "backend/protobuf/from_old_model.hpp" #include "datetime/time.hpp" #include "model/account.hpp" @@ -36,18 +36,21 @@ namespace iroha { ametsuchi::TemporaryWsv &temporaryWsv) { log_->info("transactions in proposal: {}", proposal.transactions.size()); auto checking_transaction = [this](auto &tx, auto &queries) { - return (queries.getAccount(tx.creator_account_id) | + auto transaction = + std::unique_ptr(tx.makeOldModel()); + return (queries.getAccount(transaction->creator_account_id) | [&](const auto &account) { // Check if tx creator has account and has quorum to execute // transaction - return tx.signatures.size() >= account.quorum - ? queries.getSignatories(tx.creator_account_id) + return transaction->signatures.size() >= account.quorum + ? queries.getSignatories(transaction->creator_account_id) : nonstd::nullopt; } | [&](const auto &signatories) { // Check if signatures in transaction are account signatory - return this->signaturesSubset(tx.signatures, signatories) + return this->signaturesSubset(transaction->signatures, + signatories) ? nonstd::make_optional(signatories) : nonstd::nullopt; }) @@ -57,7 +60,8 @@ namespace iroha { // Filter only valid transactions auto filter = [&temporaryWsv, checking_transaction](auto &acc, const auto &tx) { - auto answer = temporaryWsv.apply(tx, checking_transaction); + auto transaction = shared_model::proto::from_old(tx); + auto answer = temporaryWsv.apply(transaction, checking_transaction); if (answer) { acc.push_back(tx); } diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index e215a833ca..6d8acf45ef 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -155,8 +155,8 @@ namespace iroha { MOCK_METHOD2( apply, bool(const model::Block &, - std::function)); + std::function< + bool(const model::Block &, WsvQuery &, const hash256_t &)>)); }; /** @@ -167,7 +167,7 @@ namespace iroha { expected::Result, std::string> createMockMutableStorage() { return expected::makeValue>( - std::move(std::make_unique())); + std::make_unique()); } class MockMutableFactory : public MutableFactory { From 37dce6eb58186c063c2b91d01d286d36233bd8d3 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Thu, 15 Feb 2018 11:56:55 +0300 Subject: [PATCH 04/23] add tests for persistent ordering service Signed-off-by: Alexey Chernyshov --- .../ametsuchi/impl/mutable_storage_impl.cpp | 14 +++++-- .../ametsuchi/impl/mutable_storage_impl.hpp | 6 +++ ...gres_ordering_service_persistent_state.hpp | 2 +- irohad/ametsuchi/impl/storage_impl.cpp | 41 +++++++++++-------- irohad/ametsuchi/impl/storage_impl.hpp | 5 ++- irohad/ametsuchi/mutable_storage.hpp | 4 ++ .../irohad/ametsuchi/ametsuchi_mocks.hpp | 5 ++- .../irohad/ametsuchi/ametsuchi_test.cpp | 22 ++++++++++ ...mock_ordering_service_persistent_state.hpp | 17 ++------ .../ordering/ordering_gate_service_test.cpp | 35 +++++++++++++--- .../irohad/ordering/ordering_service_test.cpp | 12 ++++++ 11 files changed, 119 insertions(+), 44 deletions(-) diff --git a/irohad/ametsuchi/impl/mutable_storage_impl.cpp b/irohad/ametsuchi/impl/mutable_storage_impl.cpp index 5394d96121..7f0ecd3646 100644 --- a/irohad/ametsuchi/impl/mutable_storage_impl.cpp +++ b/irohad/ametsuchi/impl/mutable_storage_impl.cpp @@ -18,11 +18,9 @@ #include "ametsuchi/impl/postgres_block_index.hpp" #include "ametsuchi/impl/postgres_wsv_command.hpp" #include "ametsuchi/impl/postgres_wsv_query.hpp" - #include "model/execution/command_executor_factory.hpp" - -#include "ametsuchi/wsv_command.hpp" #include "model/sha3_hash.hpp" +#include "postgres_ordering_service_persistent_state.hpp" namespace iroha { namespace ametsuchi { @@ -39,7 +37,10 @@ namespace iroha { block_index_(std::make_unique(*transaction_)), command_executors_(std::move(command_executors)), committed(false), - log_(logger::log("MutableStorage")) { + log_(logger::log("MutableStorage")), + ordering_state_( + std::make_shared( + *transaction_)) { transaction_->exec("BEGIN;"); } @@ -87,5 +88,10 @@ namespace iroha { transaction_->exec("ROLLBACK;"); } } + + std::shared_ptr + MutableStorageImpl::getOrderingServicePersistentState() const { + return ordering_state_; + } } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/mutable_storage_impl.hpp b/irohad/ametsuchi/impl/mutable_storage_impl.hpp index f38d7ededb..bca2b1b835 100644 --- a/irohad/ametsuchi/impl/mutable_storage_impl.hpp +++ b/irohad/ametsuchi/impl/mutable_storage_impl.hpp @@ -35,6 +35,7 @@ namespace iroha { class BlockIndex; class WsvCommand; + class OrderingServicePersistentState; class MutableStorageImpl : public MutableStorage { friend class StorageImpl; @@ -53,6 +54,9 @@ namespace iroha { ~MutableStorageImpl() override; + std::shared_ptr + getOrderingServicePersistentState() const override; + private: hash256_t top_hash_; // ordered collection is used to enforce block insertion order in @@ -66,6 +70,8 @@ namespace iroha { std::unique_ptr block_index_; std::shared_ptr command_executors_; + std::shared_ptr ordering_state_; + bool committed; logger::Logger log_; diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index aa02561f06..55562ea0f1 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -69,7 +69,7 @@ namespace iroha { pqxx::nontransaction &transaction_; logger::Logger log_; - using ExecuteType = decltype(ametsuchi::makeExecute(transaction_, log_)); + using ExecuteType = decltype(makeExecute(transaction_, log_)); ExecuteType execute_; // TODO: refactor to return Result when it is introduced IR-775 diff --git a/irohad/ametsuchi/impl/storage_impl.cpp b/irohad/ametsuchi/impl/storage_impl.cpp index dea9e8a883..5c17272824 100644 --- a/irohad/ametsuchi/impl/storage_impl.cpp +++ b/irohad/ametsuchi/impl/storage_impl.cpp @@ -56,15 +56,22 @@ namespace iroha { wsv_transaction_(std::move(wsv_transaction)), wsv_(std::make_shared(*wsv_transaction_)), blocks_(std::make_shared(*wsv_transaction_, - *block_store_)), - ordering_state_( - std::make_shared( - *wsv_transaction_)) { + *block_store_)) { log_ = logger::log("StorageImpl"); wsv_transaction_->exec(init_); wsv_transaction_->exec( "SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;"); + + auto storageResult = createMutableStorage(); + storageResult.match( + [&](expected::Value> + &mutable_storage) { + mutable_storage_ = std::move(mutable_storage.value); + }, + [&](expected::Error &error) { + log_->error(error.error); + }); } expected::Result, std::string> @@ -165,7 +172,6 @@ DROP TABLE IF EXISTS height_by_hash; DROP TABLE IF EXISTS height_by_account_set; DROP TABLE IF EXISTS index_by_creator_height; DROP TABLE IF EXISTS index_by_id_height_asset; -DROP TABLE IF EXISTS ordering_service_state; )"; // erase db @@ -184,15 +190,17 @@ DROP TABLE IF EXISTS ordering_service_state; block_store_->dropAll(); } - expected::Result StorageImpl::initConnections( - std::string block_store_dir, std::string postgres_options) { + expected::Result + StorageImpl::initConnections(std::string block_store_dir, + std::string postgres_options) { auto log_ = logger::log("StorageImpl:initConnection"); log_->info("Start storage creation"); auto block_store = FlatFile::create(block_store_dir); if (not block_store) { return expected::makeError( - (boost::format("Cannot create block store in {}") % block_store_dir).str()); + (boost::format("Cannot create block store in {}") % block_store_dir) + .str()); } log_->info("block store created"); @@ -210,10 +218,10 @@ DROP TABLE IF EXISTS ordering_service_state; *postgres_connection, "Storage"); log_->info("transaction to PostgreSQL initialized"); - return expected::makeValue(ConnectionContext( - std::move(*block_store), - std::move(postgres_connection), - std::move(wsv_transaction))); + return expected::makeValue( + ConnectionContext(std::move(*block_store), + std::move(postgres_connection), + std::move(wsv_transaction))); } expected::Result, std::string> @@ -222,7 +230,7 @@ DROP TABLE IF EXISTS ordering_service_state; auto ctx_result = initConnections(block_store_dir, postgres_options); expected::Result, std::string> storage; ctx_result.match( - [&](expected::Value &ctx){ + [&](expected::Value &ctx) { storage = expected::makeValue(std::shared_ptr( new StorageImpl(block_store_dir, postgres_options, @@ -230,10 +238,7 @@ DROP TABLE IF EXISTS ordering_service_state; std::move(ctx.value.pg_lazy), std::move(ctx.value.pg_nontx)))); }, - [&](expected::Error &error) { - storage = error; - } - ); + [&](expected::Error &error) { storage = error; }); return storage; } @@ -261,7 +266,7 @@ DROP TABLE IF EXISTS ordering_service_state; std::shared_ptr StorageImpl::getOrderingServicePersistentState() const { - return ordering_state_; + return mutable_storage_->getOrderingServicePersistentState(); } } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/storage_impl.hpp b/irohad/ametsuchi/impl/storage_impl.hpp index e8e3148600..95af45bbe2 100644 --- a/irohad/ametsuchi/impl/storage_impl.hpp +++ b/irohad/ametsuchi/impl/storage_impl.hpp @@ -103,8 +103,6 @@ namespace iroha { std::shared_ptr blocks_; - std::shared_ptr ordering_state_; - model::converters::JsonBlockFactory serializer_; // Allows multiple readers and a single writer @@ -112,6 +110,8 @@ namespace iroha { logger::Logger log_; + std::unique_ptr mutable_storage_; + protected: const std::string init_ = R"( CREATE TABLE IF NOT EXISTS role ( @@ -197,6 +197,7 @@ CREATE TABLE IF NOT EXISTS index_by_id_height_asset ( CREATE TABLE IF NOT EXISTS ordering_service_state ( proposal_height bigserial ); +DELETE FROM ordering_service_state; INSERT INTO ordering_service_state VALUES (2); -- start height (1 for genesis) )"; diff --git a/irohad/ametsuchi/mutable_storage.hpp b/irohad/ametsuchi/mutable_storage.hpp index c78423475a..d5d4e491fa 100644 --- a/irohad/ametsuchi/mutable_storage.hpp +++ b/irohad/ametsuchi/mutable_storage.hpp @@ -29,6 +29,7 @@ namespace iroha { namespace ametsuchi { class WsvQuery; + class OrderingServicePersistentState; /** * Mutable storage is used apply blocks to the storage. @@ -56,6 +57,9 @@ namespace iroha { const hash256_t &)> function) = 0; virtual ~MutableStorage() = default; + + virtual std::shared_ptr + getOrderingServicePersistentState() const = 0; }; } // namespace ametsuchi diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index 9eb5b29796..40b585f5f2 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -157,6 +157,9 @@ namespace iroha { bool(const model::Block &, std::function)); + MOCK_CONST_METHOD0( + getOrderingServicePersistentState, + std::shared_ptr(void)); }; /** @@ -167,7 +170,7 @@ namespace iroha { expected::Result, std::string> createMockMutableStorage() { return expected::makeValue>( - std::move(std::make_unique())); + std::make_unique()); } class MockMutableFactory : public MutableFactory { diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 7fdc6bada3..cb581b71bc 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -35,6 +35,7 @@ #include "model/permissions.hpp" #include "model/sha3_hash.hpp" #include "module/irohad/ametsuchi/ametsuchi_fixture.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" using namespace iroha::ametsuchi; using namespace iroha::model; @@ -866,3 +867,24 @@ TEST_F(AmetsuchiTest, FindTxByHashTest) { ASSERT_EQ(*blocks->getTxByHashSync(tx2hash), tx2); ASSERT_EQ(blocks->getTxByHashSync(tx3hash), boost::none); } + +TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { + std::shared_ptr storage; + auto storageResult = StorageImpl::create(block_store_path, pgopt_); + storageResult.match( + [&](iroha::expected::Value> &_storage) { + storage = _storage.value; + }, + [](iroha::expected::Error &error) { + FAIL() << "StorageImpl: " << error.error; + }); + ASSERT_TRUE(storage); + + auto ordering_state = storage->getOrderingServicePersistentState(); + + ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); + ASSERT_TRUE(ordering_state->saveProposalHeight(11)); + ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); + ASSERT_TRUE(ordering_state->saveProposalHeight(33)); + ASSERT_EQ(33, ordering_state->loadProposalHeight().value()); +} diff --git a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp index d8885d2033..34f544e56c 100644 --- a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp +++ b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp @@ -17,6 +17,8 @@ #ifndef IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP #define IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP +#include + #include "ametsuchi/ordering_service_persistent_state.hpp" class MockOrderingServicePersistentState @@ -25,23 +27,12 @@ class MockOrderingServicePersistentState /** * Save proposal height */ - virtual bool saveProposalHeight(size_t height) { - height_ = height; - return true; - } + MOCK_METHOD1(saveProposalHeight, bool(size_t height)); /** * Load proposal height */ - virtual boost::optional loadProposalHeight() const { - return boost::optional(height_); - } - - private: - /** - * Initial height is 2 (1 for genesis block). - */ - size_t height_ = 2; + MOCK_CONST_METHOD0(loadProposalHeight, boost::optional()); }; #endif // IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index 26a5381783..1197a6408e 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -42,11 +42,13 @@ class OrderingGateServiceTest : public ::testing::Test { gate_transport->subscribe(gate); service_transport = std::make_shared(); - persistent_state = std::make_shared(); counter = 2; } - void SetUp() override {} + void SetUp() override { + fake_persistent_state = + std::make_shared(); + } void start() { std::mutex mtx; @@ -112,7 +114,7 @@ class OrderingGateServiceTest : public ::testing::Test { Peer peer; std::shared_ptr gate_transport; std::shared_ptr service_transport; - std::shared_ptr persistent_state; + std::shared_ptr fake_persistent_state; }; TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { @@ -124,8 +126,19 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { const size_t max_proposal = 100; const size_t commit_delay = 400; + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) + .Times(1) + .WillOnce(Return(boost::optional(2))); + + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(3)) + .Times(1) + .WillOnce(Return(bool(true))); + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(4)) + .Times(1) + .WillOnce(Return(bool(true))); + service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport, persistent_state); + wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); service_transport->subscribe(service); start(); @@ -166,8 +179,20 @@ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { const size_t max_proposal = 5; const size_t commit_delay = 1000; + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) + .Times(1) + .WillOnce(Return(boost::optional(2))); + + + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(3)) + .Times(1) + .WillOnce(Return(bool(true))); + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(4)) + .Times(1) + .WillOnce(Return(bool(true))); + service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport, persistent_state); + wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); service_transport->subscribe(service); start(); diff --git a/test/module/irohad/ordering/ordering_service_test.cpp b/test/module/irohad/ordering/ordering_service_test.cpp index 2257742cd6..3fa3b5dbf6 100644 --- a/test/module/irohad/ordering/ordering_service_test.cpp +++ b/test/module/irohad/ordering/ordering_service_test.cpp @@ -94,6 +94,10 @@ TEST_F(OrderingServiceTest, SimpleTest) { const size_t max_proposal = 5; const size_t commit_delay = 1000; + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) + .Times(1) + .WillOnce(Return(boost::optional(2))); + auto ordering_service = std::make_shared( wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); @@ -107,6 +111,10 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { const size_t max_proposal = 5; const size_t commit_delay = 1000; + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) + .Times(1) + .WillOnce(Return(boost::optional(2))); + auto ordering_service = std::make_shared( wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); @@ -140,6 +148,10 @@ TEST_F(OrderingServiceTest, ValidWhenTimerStrategy) { const size_t max_proposal = 100; const size_t commit_delay = 400; + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) + .Times(1) + .WillOnce(Return(boost::optional(2))); + auto ordering_service = std::make_shared( wsv, max_proposal, commit_delay, fake_transport, fake_persistent_state); fake_transport->subscribe(ordering_service); From f48e7ed960a2a85f257859bfb8b52eedd1495da7 Mon Sep 17 00:00:00 2001 From: dumitru Date: Thu, 15 Feb 2018 17:22:59 +0300 Subject: [PATCH 05/23] Fix memory leaks Signed-off-by: dumitru --- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 5 ++--- irohad/ametsuchi/temporary_wsv.hpp | 2 -- irohad/validation/impl/stateful_validator_impl.cpp | 11 ++++++----- test/module/irohad/ametsuchi/ametsuchi_mocks.hpp | 3 ++- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index 2bcc7540a6..8aac990788 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -42,7 +42,7 @@ namespace iroha { const shared_model::interface::Transaction &tx, std::function apply_function) { - auto transaction = tx.makeOldModel(); + auto transaction = std::unique_ptr(tx.makeOldModel()); const auto &tx_creator = transaction->creator_account_id; auto execute_command = [this, &tx_creator](auto command) { @@ -63,7 +63,7 @@ namespace iroha { transaction_->exec("SAVEPOINT savepoint_;"); auto result = apply_function(tx, *wsv_) - && std::all_of(transaction->commands.begin(), + and std::all_of(transaction->commands.begin(), transaction->commands.end(), execute_command); if (result) { @@ -71,7 +71,6 @@ namespace iroha { } else { transaction_->exec("ROLLBACK TO SAVEPOINT savepoint_;"); } - delete transaction; return result; } diff --git a/irohad/ametsuchi/temporary_wsv.hpp b/irohad/ametsuchi/temporary_wsv.hpp index 3f431be90a..edd02851d3 100644 --- a/irohad/ametsuchi/temporary_wsv.hpp +++ b/irohad/ametsuchi/temporary_wsv.hpp @@ -22,8 +22,6 @@ #include "ametsuchi/wsv_command.hpp" #include "ametsuchi/wsv_query.hpp" -#include "model/block.hpp" -#include "model/transaction.hpp" namespace shared_model { namespace interface { diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index 8394eee669..df33cd123a 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -36,18 +36,19 @@ namespace iroha { ametsuchi::TemporaryWsv &temporaryWsv) { log_->info("transactions in proposal: {}", proposal.transactions.size()); auto checking_transaction = [this](auto &tx, auto &queries) { - auto transaction = - std::unique_ptr(tx.makeOldModel()); - return (queries.getAccount(transaction->creator_account_id) | + + return (queries.getAccount(tx.creatorAccountId()) | [&](const auto &account) { // Check if tx creator has account and has quorum to execute // transaction - return transaction->signatures.size() >= account.quorum - ? queries.getSignatories(transaction->creator_account_id) + return tx.signatures().size() >= account.quorum + ? queries.getSignatories(tx.creatorAccountId()) : nonstd::nullopt; } | [&](const auto &signatories) { + auto transaction = + std::unique_ptr(tx.makeOldModel()); // Check if signatures in transaction are account signatory return this->signaturesSubset(transaction->signatures, signatories) diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index 6d8acf45ef..ca2f6f9a4f 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -32,7 +32,8 @@ #include "model/asset.hpp" #include "model/domain.hpp" #include "model/peer.hpp" - +#include "model/transaction.hpp" +#include "model/block.hpp" #include #include "common/result.hpp" From 4a7bde9883e8ecfc74e030b59acc2c2d73e159f2 Mon Sep 17 00:00:00 2001 From: dumitru Date: Thu, 15 Feb 2018 18:15:54 +0300 Subject: [PATCH 06/23] Remove unused import and vars Signed-off-by: dumitru --- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 2 -- irohad/validation/impl/stateful_validator_impl.cpp | 7 +------ test/module/irohad/ametsuchi/ametsuchi_mocks.hpp | 4 ++-- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index 8aac990788..04d5adee9f 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -20,7 +20,6 @@ #include "ametsuchi/impl/postgres_wsv_query.hpp" #include "amount/amount.hpp" #include "backend/protobuf/from_old_model.hpp" -#include "model/account.hpp" #include "model/execution/command_executor_factory.hpp" namespace iroha { @@ -47,7 +46,6 @@ namespace iroha { const auto &tx_creator = transaction->creator_account_id; auto execute_command = [this, &tx_creator](auto command) { auto executor = command_executors_->getCommandExecutor(command); - auto account = wsv_->getAccount(tx_creator).value(); if (not executor->validate(*command, *wsv_, tx_creator)) { return false; } diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index df33cd123a..541ff7fc24 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -16,13 +16,8 @@ */ #include "validation/impl/stateful_validator_impl.hpp" -#include -#include #include "backend/protobuf/from_old_model.hpp" - -#include "datetime/time.hpp" #include "model/account.hpp" -#include "validation/impl/stateful_validator_impl.hpp" namespace iroha { namespace validation { @@ -48,7 +43,7 @@ namespace iroha { | [&](const auto &signatories) { auto transaction = - std::unique_ptr(tx.makeOldModel()); + std::unique_ptr(tx.makeOldModel()); // Check if signatures in transaction are account signatory return this->signaturesSubset(transaction->signatures, signatories) diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index ca2f6f9a4f..f706a74a94 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -19,6 +19,7 @@ #define IROHA_AMETSUCHI_MOCKS_HPP #include +#include #include "ametsuchi/block_query.hpp" #include "ametsuchi/mutable_factory.hpp" #include "ametsuchi/mutable_storage.hpp" @@ -30,11 +31,10 @@ #include "model/account.hpp" #include "model/account_asset.hpp" #include "model/asset.hpp" +#include "model/block.hpp" #include "model/domain.hpp" #include "model/peer.hpp" #include "model/transaction.hpp" -#include "model/block.hpp" -#include #include "common/result.hpp" From 2cc57cba9eeba1e8f875f9a8ce0e12751be863e5 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Fri, 16 Feb 2018 16:39:05 +0300 Subject: [PATCH 07/23] move persistent ordering service to new storage Signed-off-by: Alexey Chernyshov --- irohad/ametsuchi/CMakeLists.txt | 1 + .../ametsuchi/impl/mutable_storage_impl.cpp | 11 +- .../ametsuchi/impl/mutable_storage_impl.hpp | 6 - ...gres_ordering_service_persistent_state.cpp | 128 ++++++++++++++++++ ...gres_ordering_service_persistent_state.hpp | 83 +++++++----- irohad/ametsuchi/impl/storage_impl.cpp | 15 -- irohad/ametsuchi/impl/storage_impl.hpp | 20 +-- irohad/ametsuchi/mutable_storage.hpp | 4 - .../ordering_service_persistent_state.hpp | 5 + irohad/ametsuchi/storage.hpp | 8 -- irohad/main/application.cpp | 16 ++- irohad/main/application.hpp | 9 ++ irohad/main/irohad.cpp | 3 + .../ordering/impl/ordering_service_impl.cpp | 2 +- .../ordering/impl/ordering_service_impl.hpp | 21 +-- .../transfer_asset_inter_domain_test.cpp | 4 + .../pipeline/tx_pipeline_integration_test.cpp | 3 + .../irohad/ametsuchi/ametsuchi_mocks.hpp | 6 - .../irohad/ametsuchi/ametsuchi_test.cpp | 21 +-- ...mock_ordering_service_persistent_state.hpp | 5 + .../irohad/ordering/ordering_service_test.cpp | 6 + 21 files changed, 257 insertions(+), 120 deletions(-) create mode 100644 irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp diff --git a/irohad/ametsuchi/CMakeLists.txt b/irohad/ametsuchi/CMakeLists.txt index bf8f76a0ee..8fabe31f09 100644 --- a/irohad/ametsuchi/CMakeLists.txt +++ b/irohad/ametsuchi/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(ametsuchi impl/peer_query_wsv.cpp impl/postgres_block_query.cpp impl/postgres_block_index.cpp + impl/postgres_ordering_service_persistent_state.cpp ) target_link_libraries(ametsuchi diff --git a/irohad/ametsuchi/impl/mutable_storage_impl.cpp b/irohad/ametsuchi/impl/mutable_storage_impl.cpp index 7f0ecd3646..fe4af98a3b 100644 --- a/irohad/ametsuchi/impl/mutable_storage_impl.cpp +++ b/irohad/ametsuchi/impl/mutable_storage_impl.cpp @@ -20,7 +20,6 @@ #include "ametsuchi/impl/postgres_wsv_query.hpp" #include "model/execution/command_executor_factory.hpp" #include "model/sha3_hash.hpp" -#include "postgres_ordering_service_persistent_state.hpp" namespace iroha { namespace ametsuchi { @@ -37,10 +36,7 @@ namespace iroha { block_index_(std::make_unique(*transaction_)), command_executors_(std::move(command_executors)), committed(false), - log_(logger::log("MutableStorage")), - ordering_state_( - std::make_shared( - *transaction_)) { + log_(logger::log("MutableStorage")) { transaction_->exec("BEGIN;"); } @@ -88,10 +84,5 @@ namespace iroha { transaction_->exec("ROLLBACK;"); } } - - std::shared_ptr - MutableStorageImpl::getOrderingServicePersistentState() const { - return ordering_state_; - } } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/mutable_storage_impl.hpp b/irohad/ametsuchi/impl/mutable_storage_impl.hpp index bca2b1b835..f38d7ededb 100644 --- a/irohad/ametsuchi/impl/mutable_storage_impl.hpp +++ b/irohad/ametsuchi/impl/mutable_storage_impl.hpp @@ -35,7 +35,6 @@ namespace iroha { class BlockIndex; class WsvCommand; - class OrderingServicePersistentState; class MutableStorageImpl : public MutableStorage { friend class StorageImpl; @@ -54,9 +53,6 @@ namespace iroha { ~MutableStorageImpl() override; - std::shared_ptr - getOrderingServicePersistentState() const override; - private: hash256_t top_hash_; // ordered collection is used to enforce block insertion order in @@ -70,8 +66,6 @@ namespace iroha { std::unique_ptr block_index_; std::shared_ptr command_executors_; - std::shared_ptr ordering_state_; - bool committed; logger::Logger log_; diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp new file mode 100644 index 0000000000..5fbfa3607e --- /dev/null +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp @@ -0,0 +1,128 @@ +/** + * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved. + * http://soramitsu.co.jp + * + * 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 "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" +#include +#include +#include +#include "common/types.hpp" + +namespace iroha { + namespace ametsuchi { + + expected::Result, + std::string> + PostgresOrderingServicePersistentState::create( + std::string postgres_options) { + auto log_ = + logger::log("PostgresOrderingServicePersistentState:initConnection"); + log_->info("Start storage creation"); + + // create connection + auto postgres_connection = + std::make_unique(postgres_options); + try { + postgres_connection->activate(); + } catch (const pqxx::broken_connection &e) { + return expected::makeError( + (boost::format("Connection to PostgreSQL broken: {}") % e.what()) + .str()); + } + log_->info("connection to PostgreSQL completed"); + + // create transaction + auto postgres_transaction = std::make_unique( + *postgres_connection, "Storage"); + log_->info("transaction to PostgreSQL initialized"); + + expected::Result, + std::string> + storage; + storage = expected::makeValue( + std::shared_ptr( + new PostgresOrderingServicePersistentState( + std::move(postgres_connection), + std::move(postgres_transaction)))); + return storage; + } + + PostgresOrderingServicePersistentState:: + PostgresOrderingServicePersistentState( + std::unique_ptr postgres_connection, + std::unique_ptr postgres_transaction) + : postgres_connection_(std::move(postgres_connection)), + postgres_transaction_(std::move(postgres_transaction)), + log_(logger::log("PostgresOrderingServicePersistentState")), + execute_{ametsuchi::makeExecute(*postgres_transaction_, log_)} {} + + void PostgresOrderingServicePersistentState::initStorage() { + log_->info("Init storage"); + postgres_transaction_->exec( + "CREATE TABLE IF NOT EXISTS ordering_service_state (\n" + " proposal_height bigserial\n" + ");" + "INSERT INTO ordering_service_state\n" + "VALUES (2); -- expected height (1 is genesis)"); + } + + void PostgresOrderingServicePersistentState::dropStorgage() { + log_->info("Drop storage"); + postgres_transaction_->exec( + "DROP TABLE IF EXISTS ordering_service_state;"); + } + + bool PostgresOrderingServicePersistentState::saveProposalHeight( + size_t height) { + log_->info("Save proposal_height in ordering_service_state " + + std::to_string(height)); + auto res = execute( + "DELETE FROM ordering_service_state;\n" + "INSERT INTO ordering_service_state " + "VALUES (" + + postgres_transaction_->quote(height) + ");"); + return res; + } + + boost::optional + PostgresOrderingServicePersistentState::loadProposalHeight() const { + return execute_("SELECT * FROM ordering_service_state;") | + [&](const auto &result) -> boost::optional { + boost::optional res; + if (result.empty()) { + log_->error( + "There is no proposal_height in ordering_service_state. Use " + "default value 2."); + res = 2; + } else { + size_t height; + auto row = result.at(0); + row.at("proposal_height") >> height; + res = height; + log_->info("Load proposal_height in ordering_service_state " + + std::to_string(height)); + } + return res; + }; + } + + void PostgresOrderingServicePersistentState::reset() { + dropStorgage(); + initStorage(); + } + + } // namespace ametsuchi +} // namespace iroha diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index 55562ea0f1..87deb539d0 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -18,10 +18,11 @@ #ifndef IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP #define IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP -#include - -#include "ametsuchi/impl/postgres_wsv_common.hpp" +#include #include "ametsuchi/ordering_service_persistent_state.hpp" +#include "common/result.hpp" +#include "logger/logger.hpp" +#include "ametsuchi/impl/postgres_wsv_common.hpp" namespace iroha { namespace ametsuchi { @@ -29,55 +30,73 @@ namespace iroha { class PostgresOrderingServicePersistentState : public OrderingServicePersistentState { public: + /** + * Create the instance of PostgresOrderingServicePersistentState + * @param postgres_options postgres connection string + * @return new instace of PostgresOrderingServicePersistentState + */ + static expected::Result< + std::shared_ptr, + std::string> + create(std::string postgres_options); + + /** + * Constructor + * @param postgres_connection postgres connection object + * @param postgres_transaction postgres transaction object + */ explicit PostgresOrderingServicePersistentState( - pqxx::nontransaction &transaction) - : transaction_(transaction), - log_(logger::log("PostgresOrderingServicePersistentState")), - execute_{ametsuchi::makeExecute(transaction_, log_)} {} + std::unique_ptr postgres_connection, + std::unique_ptr postgres_transaction); + + /** + * Initialize storage. + * Create tables and fill with initial value. + */ + void initStorage(); + + /** + * Drop storage tables. + */ + void dropStorgage(); /** * Save proposal height that it can be restored * after launch */ - virtual bool saveProposalHeight(size_t height) { - return execute( - "UPDATE ordering_service_state " - "SET proposal_height = " - + transaction_.quote(height) + ";"); - } + virtual bool saveProposalHeight(size_t height); /** * Load proposal height */ - virtual boost::optional loadProposalHeight() const { - return execute_("SELECT * FROM ordering_service_state;") | - [&](const auto &result) -> boost::optional { - boost::optional res; - if (result.empty()) { - log_->info("There is no proposal_height in ordering_service_state"); - } else { - size_t height; - auto row = result.at(0); - row.at("proposal_height") >> height; - res = height; - } - return res; - }; - } + virtual boost::optional loadProposalHeight() const; + + /** + * Reset storage state to default + */ + virtual void reset(); private: - pqxx::nontransaction &transaction_; + /** + * Pg connection with direct transaction management + */ + std::unique_ptr postgres_connection_; + + /** + * Pg transaction + */ + std::unique_ptr postgres_transaction_; + logger::Logger log_; - using ExecuteType = decltype(makeExecute(transaction_, log_)); + using ExecuteType = decltype(makeExecute(*postgres_transaction_, log_)); ExecuteType execute_; - // TODO: refactor to return Result when it is introduced IR-775 bool execute(const std::string &statement) noexcept { return static_cast(execute_(statement)); } }; - } // namespace ordering + } // namespace ametsuchi } // namespace iroha #endif // IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/irohad/ametsuchi/impl/storage_impl.cpp b/irohad/ametsuchi/impl/storage_impl.cpp index 5c17272824..3b16ca7990 100644 --- a/irohad/ametsuchi/impl/storage_impl.cpp +++ b/irohad/ametsuchi/impl/storage_impl.cpp @@ -62,16 +62,6 @@ namespace iroha { wsv_transaction_->exec(init_); wsv_transaction_->exec( "SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;"); - - auto storageResult = createMutableStorage(); - storageResult.match( - [&](expected::Value> - &mutable_storage) { - mutable_storage_ = std::move(mutable_storage.value); - }, - [&](expected::Error &error) { - log_->error(error.error); - }); } expected::Result, std::string> @@ -263,10 +253,5 @@ DROP TABLE IF EXISTS index_by_id_height_asset; std::shared_ptr StorageImpl::getBlockQuery() const { return blocks_; } - - std::shared_ptr - StorageImpl::getOrderingServicePersistentState() const { - return mutable_storage_->getOrderingServicePersistentState(); - } } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/storage_impl.hpp b/irohad/ametsuchi/impl/storage_impl.hpp index 95af45bbe2..dc4ee39b43 100644 --- a/irohad/ametsuchi/impl/storage_impl.hpp +++ b/irohad/ametsuchi/impl/storage_impl.hpp @@ -18,23 +18,18 @@ #ifndef IROHA_STORAGE_IMPL_HPP #define IROHA_STORAGE_IMPL_HPP -#include "ametsuchi/storage.hpp" - #include -#include - #include #include - +#include +#include "ametsuchi/storage.hpp" #include "logger/logger.hpp" #include "model/converters/json_block_factory.hpp" - namespace iroha { namespace ametsuchi { class FlatFile; - class OrderingServicePersistentState; struct ConnectionContext { ConnectionContext(std::unique_ptr block_store, @@ -71,9 +66,6 @@ namespace iroha { std::shared_ptr getBlockQuery() const override; - std::shared_ptr - getOrderingServicePersistentState() const override; - protected: StorageImpl(std::string block_store_dir, std::string postgres_options, @@ -110,8 +102,6 @@ namespace iroha { logger::Logger log_; - std::unique_ptr mutable_storage_; - protected: const std::string init_ = R"( CREATE TABLE IF NOT EXISTS role ( @@ -194,12 +184,6 @@ CREATE TABLE IF NOT EXISTS index_by_id_height_asset ( asset_id text, index text ); -CREATE TABLE IF NOT EXISTS ordering_service_state ( - proposal_height bigserial -); -DELETE FROM ordering_service_state; -INSERT INTO ordering_service_state -VALUES (2); -- start height (1 for genesis) )"; }; } // namespace ametsuchi diff --git a/irohad/ametsuchi/mutable_storage.hpp b/irohad/ametsuchi/mutable_storage.hpp index d5d4e491fa..c78423475a 100644 --- a/irohad/ametsuchi/mutable_storage.hpp +++ b/irohad/ametsuchi/mutable_storage.hpp @@ -29,7 +29,6 @@ namespace iroha { namespace ametsuchi { class WsvQuery; - class OrderingServicePersistentState; /** * Mutable storage is used apply blocks to the storage. @@ -57,9 +56,6 @@ namespace iroha { const hash256_t &)> function) = 0; virtual ~MutableStorage() = default; - - virtual std::shared_ptr - getOrderingServicePersistentState() const = 0; }; } // namespace ametsuchi diff --git a/irohad/ametsuchi/ordering_service_persistent_state.hpp b/irohad/ametsuchi/ordering_service_persistent_state.hpp index dd72eee99c..f9f8a96156 100644 --- a/irohad/ametsuchi/ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/ordering_service_persistent_state.hpp @@ -38,6 +38,11 @@ namespace iroha { * Load proposal height */ virtual boost::optional loadProposalHeight() const = 0; + + /** + * Reset storage to default state + */ + virtual void reset() = 0; }; } // namespace ordering } // namespace iroha diff --git a/irohad/ametsuchi/storage.hpp b/irohad/ametsuchi/storage.hpp index e6ad6e1ae9..33a6314c17 100644 --- a/irohad/ametsuchi/storage.hpp +++ b/irohad/ametsuchi/storage.hpp @@ -32,7 +32,6 @@ namespace iroha { class BlockQuery; class WsvQuery; - class OrderingServicePersistentState; /** * Storage interface, which allows queries on current committed state, and @@ -44,13 +43,6 @@ namespace iroha { virtual std::shared_ptr getBlockQuery() const = 0; - /** - * Get storage for ordering storage state - * @return storage for ordering service state - */ - virtual std::shared_ptr - getOrderingServicePersistentState() const = 0; - /** * Raw insertion of blocks without validation * @param block - block for insertion diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index de86d1ad5d..a15013c909 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -16,6 +16,7 @@ */ #include "main/application.hpp" +#include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" using namespace iroha; using namespace iroha::ametsuchi; @@ -117,9 +118,22 @@ void Irohad::initStorage() { throw std::runtime_error(error.error); }); + auto orderingStorageResult = PostgresOrderingServicePersistentState::create(pg_conn_); + orderingStorageResult.match( + [&](expected::Value> &_storage) { + ordering_service_storage_ = _storage.value; + }, + [](expected::Error &error) { + throw std::runtime_error(error.error); + }); + log_->info("[Init] => storage", logger::logBool(storage)); } +void Irohad::resetOrderingService() { + ordering_service_storage_->reset(); +} + /** * Creating transaction, query and query response factories */ @@ -169,7 +183,7 @@ void Irohad::initOrderingGate() { wsv, max_proposal_size_, proposal_delay_, - storage->getOrderingServicePersistentState()); + ordering_service_storage_); log_->info("[Init] => init ordering gate - [{}]", logger::logBool(ordering_gate)); } diff --git a/irohad/main/application.hpp b/irohad/main/application.hpp index e80dc90f57..3bb430ab58 100644 --- a/irohad/main/application.hpp +++ b/irohad/main/application.hpp @@ -42,6 +42,7 @@ #include "validation/stateful_validator.hpp" #include "ametsuchi/impl/peer_query_wsv.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" #include "network/impl/peer_communication_service_impl.hpp" #include "synchronizer/impl/synchronizer_impl.hpp" #include "validation/impl/chain_validator_impl.hpp" @@ -79,6 +80,11 @@ class Irohad { */ virtual void init(); + /** + * Reset oredering service storage state to default + */ + void resetOrderingService(); + /** * Drop wsv and block store */ @@ -187,6 +193,9 @@ class Irohad { public: std::shared_ptr storage; + std::shared_ptr + ordering_service_storage_; + iroha::keypair_t keypair; }; diff --git a/irohad/main/irohad.cpp b/irohad/main/irohad.cpp index ac3d39fedf..6671b04acb 100644 --- a/irohad/main/irohad.cpp +++ b/irohad/main/irohad.cpp @@ -140,6 +140,9 @@ int main(int argc, char *argv[]) { // clear previous storage if any irohad.dropStorage(); + // reset ordering service persistent counter + irohad.resetOrderingService(); + log->info("Block is parsed"); // Applying transactions from genesis block to iroha storage diff --git a/irohad/ordering/impl/ordering_service_impl.cpp b/irohad/ordering/impl/ordering_service_impl.cpp index f5f4db355d..b9c42f9464 100644 --- a/irohad/ordering/impl/ordering_service_impl.cpp +++ b/irohad/ordering/impl/ordering_service_impl.cpp @@ -60,7 +60,7 @@ namespace iroha { proposal.height = proposal_height++; proposal.created_time = iroha::time::now(); - // Save proposal height in persistent storage. + // Save proposal height to the persistent storage. // In case of restart it reloads state. persistent_state_->saveProposalHeight(proposal_height); diff --git a/irohad/ordering/impl/ordering_service_impl.hpp b/irohad/ordering/impl/ordering_service_impl.hpp index 55fbef255f..5de0e65f43 100644 --- a/irohad/ordering/impl/ordering_service_impl.hpp +++ b/irohad/ordering/impl/ordering_service_impl.hpp @@ -18,12 +18,10 @@ #ifndef IROHA_ORDERING_SERVICE_IMPL_HPP #define IROHA_ORDERING_SERVICE_IMPL_HPP -#include -#include - #include +#include #include - +#include #include "ametsuchi/peer_query.hpp" #include "model/converters/pb_transaction_factory.hpp" #include "model/proposal.hpp" @@ -46,7 +44,8 @@ namespace iroha { * Sends proposal by given timer interval and proposal size * @param delay_milliseconds timer delay * @param max_size proposal size - * @param persistent_state - storage for persistent state of ordering service + * @param persistent_state - storage for persistent state of ordering + * service */ class OrderingServiceImpl : public network::OrderingService { public: @@ -55,7 +54,8 @@ namespace iroha { size_t max_size, size_t delay_milliseconds, std::shared_ptr transport, - std::shared_ptr persistent_state); + std::shared_ptr + persistent_state); /** * Process transaction received from network @@ -108,12 +108,15 @@ namespace iroha { /** * Persistense storage for proposal counter. - * In case of relaunch, ordering server will enumerate proposals consecutively. + * In case of relaunch, ordering server will enumerate proposals + * consecutively. */ - std::shared_ptr persistent_state_; + std::shared_ptr + persistent_state_; /** - * Proposal counter + * Proposal counter of expected proposal. Should be number of blocks in + * the ledger + 1. */ size_t proposal_height; }; diff --git a/test/integration/pipeline/transfer_asset_inter_domain_test.cpp b/test/integration/pipeline/transfer_asset_inter_domain_test.cpp index d5fa4b1dd4..02ed28a080 100644 --- a/test/integration/pipeline/transfer_asset_inter_domain_test.cpp +++ b/test/integration/pipeline/transfer_asset_inter_domain_test.cpp @@ -73,6 +73,10 @@ class TransferAssetInterDomainTest : public TxPipelineIntegrationTestFixture { ASSERT_TRUE(irohad->storage); irohad->storage->insertBlock(genesis_block); + + // reset ordering storage state + irohad->resetOrderingService(); + irohad->init(); irohad->run(); } diff --git a/test/integration/pipeline/tx_pipeline_integration_test.cpp b/test/integration/pipeline/tx_pipeline_integration_test.cpp index e7ecfa6030..405dc1fe57 100644 --- a/test/integration/pipeline/tx_pipeline_integration_test.cpp +++ b/test/integration/pipeline/tx_pipeline_integration_test.cpp @@ -60,6 +60,9 @@ class TxPipelineIntegrationTest : public TxPipelineIntegrationTestFixture { // insert genesis block irohad->storage->insertBlock(genesis_block); + // reset ordering storage state + irohad->resetOrderingService(); + // initialize irohad irohad->init(); diff --git a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp index 40b585f5f2..fa0ca3ca21 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_mocks.hpp @@ -157,9 +157,6 @@ namespace iroha { bool(const model::Block &, std::function)); - MOCK_CONST_METHOD0( - getOrderingServicePersistentState, - std::shared_ptr(void)); }; /** @@ -199,9 +196,6 @@ namespace iroha { public: MOCK_CONST_METHOD0(getWsvQuery, std::shared_ptr(void)); MOCK_CONST_METHOD0(getBlockQuery, std::shared_ptr(void)); - MOCK_CONST_METHOD0( - getOrderingServicePersistentState, - std::shared_ptr(void)); MOCK_METHOD0( createTemporaryWsv, expected::Result, std::string>(void)); diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index cb581b71bc..1e3bded43d 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -35,7 +35,7 @@ #include "model/permissions.hpp" #include "model/sha3_hash.hpp" #include "module/irohad/ametsuchi/ametsuchi_fixture.hpp" -#include "ametsuchi/ordering_service_persistent_state.hpp" +#include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" using namespace iroha::ametsuchi; using namespace iroha::model; @@ -869,22 +869,23 @@ TEST_F(AmetsuchiTest, FindTxByHashTest) { } TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { - std::shared_ptr storage; - auto storageResult = StorageImpl::create(block_store_path, pgopt_); - storageResult.match( - [&](iroha::expected::Value> &_storage) { - storage = _storage.value; + std::shared_ptr ordering_state; + auto orderingStorageResult = iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); + orderingStorageResult.match( + [&](iroha::expected::Value> &_storage) { + ordering_state = _storage.value; }, [](iroha::expected::Error &error) { - FAIL() << "StorageImpl: " << error.error; + FAIL() << "PostgresOrderingServicePersistentState: " << error.error; }); - ASSERT_TRUE(storage); - - auto ordering_state = storage->getOrderingServicePersistentState(); + ASSERT_TRUE(ordering_state); + ordering_state->reset(); ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); ASSERT_TRUE(ordering_state->saveProposalHeight(11)); ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); ASSERT_TRUE(ordering_state->saveProposalHeight(33)); ASSERT_EQ(33, ordering_state->loadProposalHeight().value()); + ordering_state->reset(); + ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); } diff --git a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp index 34f544e56c..c98a9ab13e 100644 --- a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp +++ b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp @@ -33,6 +33,11 @@ class MockOrderingServicePersistentState * Load proposal height */ MOCK_CONST_METHOD0(loadProposalHeight, boost::optional()); + + /** + * Reset state + */ + MOCK_METHOD0(reset, void()); }; #endif // IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/test/module/irohad/ordering/ordering_service_test.cpp b/test/module/irohad/ordering/ordering_service_test.cpp index 3fa3b5dbf6..970735dc57 100644 --- a/test/module/irohad/ordering/ordering_service_test.cpp +++ b/test/module/irohad/ordering/ordering_service_test.cpp @@ -111,6 +111,9 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { const size_t max_proposal = 5; const size_t commit_delay = 1000; + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)) + .Times(2); + EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) .Times(1) .WillOnce(Return(boost::optional(2))); @@ -142,6 +145,9 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { TEST_F(OrderingServiceTest, ValidWhenTimerStrategy) { // Init => proposal timer 400 ms => 10 tx by 50 ms => 2 proposals in 1 second + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)) + .Times(2); + EXPECT_CALL(*wsv, getLedgerPeers()) .WillRepeatedly(Return(std::vector{peer})); From e8cbdbccddfa79580b43ae11c79f896f7bc70e8e Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Mon, 19 Feb 2018 09:10:47 +0300 Subject: [PATCH 08/23] fix header order Signed-off-by: Alexey Chernyshov --- irohad/ametsuchi/impl/storage_impl.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/irohad/ametsuchi/impl/storage_impl.hpp b/irohad/ametsuchi/impl/storage_impl.hpp index dc4ee39b43..a46e750347 100644 --- a/irohad/ametsuchi/impl/storage_impl.hpp +++ b/irohad/ametsuchi/impl/storage_impl.hpp @@ -18,11 +18,12 @@ #ifndef IROHA_STORAGE_IMPL_HPP #define IROHA_STORAGE_IMPL_HPP +#include "ametsuchi/storage.hpp" + #include #include #include #include -#include "ametsuchi/storage.hpp" #include "logger/logger.hpp" #include "model/converters/json_block_factory.hpp" From 7dc617795c7b1996a8521a8af369bf24f335370a Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Mon, 19 Feb 2018 14:32:18 +0300 Subject: [PATCH 09/23] fix comments Signed-off-by: Alexey Chernyshov --- .../impl/postgres_ordering_service_persistent_state.hpp | 4 ++++ irohad/ametsuchi/ordering_service_persistent_state.hpp | 2 +- test/module/irohad/ametsuchi/ametsuchi_test.cpp | 5 +++++ .../module/irohad/ordering/ordering_gate_service_test.cpp | 8 ++++---- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index 87deb539d0..fa82a0fc20 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -27,6 +27,10 @@ namespace iroha { namespace ametsuchi { + /** + * Class implements OrderingServicePersistentState for persistent storage of + * Ordering Service with PostgreSQL. + */ class PostgresOrderingServicePersistentState : public OrderingServicePersistentState { public: diff --git a/irohad/ametsuchi/ordering_service_persistent_state.hpp b/irohad/ametsuchi/ordering_service_persistent_state.hpp index f9f8a96156..5e00b12430 100644 --- a/irohad/ametsuchi/ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/ordering_service_persistent_state.hpp @@ -24,7 +24,7 @@ namespace iroha { namespace ametsuchi { /** - * Interface for Ordering Service persistence + * Interface for Ordering Service persistence to store proposal's height in a persistent way */ class OrderingServicePersistentState { public: diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 1e3bded43d..0e974dbe55 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -868,6 +868,11 @@ TEST_F(AmetsuchiTest, FindTxByHashTest) { ASSERT_EQ(blocks->getTxByHashSync(tx3hash), boost::none); } +/** + * @given initialized storage for ordering service + * @when save proposal height + * @then load proposal height and ensure it is correct + */ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { std::shared_ptr ordering_state; auto orderingStorageResult = iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index 1197a6408e..62c9ca4c08 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -132,10 +132,10 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { EXPECT_CALL(*fake_persistent_state, saveProposalHeight(3)) .Times(1) - .WillOnce(Return(bool(true))); + .WillOnce(Return(true)); EXPECT_CALL(*fake_persistent_state, saveProposalHeight(4)) .Times(1) - .WillOnce(Return(bool(true))); + .WillOnce(Return(true)); service = std::make_shared( wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); @@ -186,10 +186,10 @@ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { EXPECT_CALL(*fake_persistent_state, saveProposalHeight(3)) .Times(1) - .WillOnce(Return(bool(true))); + .WillOnce(Return(true)); EXPECT_CALL(*fake_persistent_state, saveProposalHeight(4)) .Times(1) - .WillOnce(Return(bool(true))); + .WillOnce(Return(true)); service = std::make_shared( wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); From c80b29682c8b3a6a6f2578ce33014e7d3c15ff93 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Thu, 22 Feb 2018 10:52:48 +0300 Subject: [PATCH 10/23] rework ordering service state storage with makeExecuteResult Signed-off-by: Alexey Chernyshov --- ...gres_ordering_service_persistent_state.cpp | 54 ++++++++++--------- ...gres_ordering_service_persistent_state.hpp | 6 +-- irohad/ametsuchi/impl/postgres_wsv_common.hpp | 1 - .../integration_test_framework.cpp | 1 + 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp index 5fbfa3607e..f50a38e9c7 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp @@ -67,7 +67,7 @@ namespace iroha { : postgres_connection_(std::move(postgres_connection)), postgres_transaction_(std::move(postgres_transaction)), log_(logger::log("PostgresOrderingServicePersistentState")), - execute_{ametsuchi::makeExecute(*postgres_transaction_, log_)} {} + execute_{ametsuchi::makeExecuteResult(*postgres_transaction_)} {} void PostgresOrderingServicePersistentState::initStorage() { log_->info("Init storage"); @@ -89,34 +89,38 @@ namespace iroha { size_t height) { log_->info("Save proposal_height in ordering_service_state " + std::to_string(height)); - auto res = execute( - "DELETE FROM ordering_service_state;\n" - "INSERT INTO ordering_service_state " - "VALUES (" - + postgres_transaction_->quote(height) + ");"); - return res; + return execute_( + "DELETE FROM ordering_service_state;\n" + "INSERT INTO ordering_service_state " + "VALUES (" + + postgres_transaction_->quote(height) + ");") + .match([](expected::Value v) -> bool { return true; }, + [&](expected::Error e) -> bool { + log_->error(e.error); + return false; + }); } boost::optional PostgresOrderingServicePersistentState::loadProposalHeight() const { - return execute_("SELECT * FROM ordering_service_state;") | - [&](const auto &result) -> boost::optional { - boost::optional res; - if (result.empty()) { - log_->error( - "There is no proposal_height in ordering_service_state. Use " - "default value 2."); - res = 2; - } else { - size_t height; - auto row = result.at(0); - row.at("proposal_height") >> height; - res = height; - log_->info("Load proposal_height in ordering_service_state " - + std::to_string(height)); - } - return res; - }; + boost::optional height; + execute_("SELECT * FROM ordering_service_state;") + .match( + [&](expected::Value result) { + if (result.value.empty()) { + log_->error( + "There is no proposal_height in ordering_service_state. " + "Use default value 2."); + height = 2; + } else { + auto row = result.value.at(0); + height = row.at("proposal_height").as(); + log_->info("Load proposal_height in ordering_service_state " + + std::to_string(height.value())); + } + }, + [&](expected::Error e) { log_->error(e.error); }); + return height; } void PostgresOrderingServicePersistentState::reset() { diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index fa82a0fc20..2ea724426d 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -93,12 +93,8 @@ namespace iroha { logger::Logger log_; - using ExecuteType = decltype(makeExecute(*postgres_transaction_, log_)); + using ExecuteType = decltype(makeExecuteResult(*postgres_transaction_)); ExecuteType execute_; - - bool execute(const std::string &statement) noexcept { - return static_cast(execute_(statement)); - } }; } // namespace ametsuchi } // namespace iroha diff --git a/irohad/ametsuchi/impl/postgres_wsv_common.hpp b/irohad/ametsuchi/impl/postgres_wsv_common.hpp index 5647f48b9b..78486dcfc8 100644 --- a/irohad/ametsuchi/impl/postgres_wsv_common.hpp +++ b/irohad/ametsuchi/impl/postgres_wsv_common.hpp @@ -29,7 +29,6 @@ namespace iroha { /** * Return function which can execute SQL statements on provided transaction * @param transaction on which to apply statement. - * @param logger is used to report an error. * @return Result with pqxx::result in value case, or exception message * if exception was caught */ diff --git a/test/framework/integration_framework/integration_test_framework.cpp b/test/framework/integration_framework/integration_test_framework.cpp index c8d88ce6ab..c7f2dca176 100644 --- a/test/framework/integration_framework/integration_test_framework.cpp +++ b/test/framework/integration_framework/integration_test_framework.cpp @@ -76,6 +76,7 @@ namespace integration_framework { log_->info("created pipeline"); // iroha_instance_->clearLedger(); // log_->info("cleared ledger"); + iroha_instance_->instance_->resetOrderingService(); std::shared_ptr old_block(block.makeOldModel()); iroha_instance_->makeGenesis(*old_block); log_->info("added genesis block"); From f97ad92379b55d93f9497e75b1a3aaec29c61323 Mon Sep 17 00:00:00 2001 From: Dumitru Date: Thu, 22 Feb 2018 13:21:57 +0300 Subject: [PATCH 11/23] Delete unused old transaction Signed-off-by: Dumitru --- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index 04d5adee9f..f46ff52dfa 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -41,9 +41,7 @@ namespace iroha { const shared_model::interface::Transaction &tx, std::function apply_function) { - auto transaction = std::unique_ptr(tx.makeOldModel()); - - const auto &tx_creator = transaction->creator_account_id; + const auto &tx_creator = tx.creatorAccountId(); auto execute_command = [this, &tx_creator](auto command) { auto executor = command_executors_->getCommandExecutor(command); if (not executor->validate(*command, *wsv_, tx_creator)) { @@ -60,10 +58,20 @@ namespace iroha { }; transaction_->exec("SAVEPOINT savepoint_;"); + auto commands = + std::accumulate(tx.commands().begin(), + tx.commands().end(), + std::vector>{}, + [](auto &vec, const auto &cmd) { + auto curr = std::shared_ptr(cmd->makeOldModel()); + vec.push_back(curr); + return vec; + }); + auto result = apply_function(tx, *wsv_) - and std::all_of(transaction->commands.begin(), - transaction->commands.end(), - execute_command); + and std::all_of(commands.begin(), + commands.end(), + execute_command); if (result) { transaction_->exec("RELEASE SAVEPOINT savepoint_;"); } else { From 2e8e17acf1ec94f743a7bb13e6478bc4ba1728f9 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Thu, 22 Feb 2018 13:52:59 +0300 Subject: [PATCH 12/23] fix variable Signed-off-by: Alexey Chernyshov --- irohad/main/application.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index a15013c909..5d0bd4856f 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -118,8 +118,7 @@ void Irohad::initStorage() { throw std::runtime_error(error.error); }); - auto orderingStorageResult = PostgresOrderingServicePersistentState::create(pg_conn_); - orderingStorageResult.match( + PostgresOrderingServicePersistentState::create(pg_conn_).match( [&](expected::Value> &_storage) { ordering_service_storage_ = _storage.value; }, From d561e42abd58e89c389cce011af2738f882a4cf9 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Thu, 22 Feb 2018 17:44:20 +0300 Subject: [PATCH 13/23] clang-format Signed-off-by: Alexey Chernyshov --- ...gres_ordering_service_persistent_state.hpp | 2 +- irohad/ametsuchi/wsv_query.hpp | 2 +- irohad/main/application.cpp | 11 ++++------- irohad/main/impl/ordering_init.cpp | 2 +- .../ordering/impl/ordering_service_impl.cpp | 3 ++- .../integration_test_framework.hpp | 4 ++-- .../transfer_asset_inter_domain_test.cpp | 19 +++++++++---------- .../pipeline/tx_pipeline_integration_test.cpp | 19 +++++++++---------- .../irohad/ametsuchi/ametsuchi_test.cpp | 14 ++++++++------ .../ordering/ordering_gate_service_test.cpp | 15 ++++++++++----- .../irohad/ordering/ordering_service_test.cpp | 8 +++----- 11 files changed, 50 insertions(+), 49 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index 2ea724426d..dc7bd3d803 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -19,10 +19,10 @@ #define IROHA_POSTGRES_ORDERING_SERVICE_PERSISTENT_STATE_HPP #include +#include "ametsuchi/impl/postgres_wsv_common.hpp" #include "ametsuchi/ordering_service_persistent_state.hpp" #include "common/result.hpp" #include "logger/logger.hpp" -#include "ametsuchi/impl/postgres_wsv_common.hpp" namespace iroha { namespace ametsuchi { diff --git a/irohad/ametsuchi/wsv_query.hpp b/irohad/ametsuchi/wsv_query.hpp index 14bbd0d55f..bba0820bc0 100644 --- a/irohad/ametsuchi/wsv_query.hpp +++ b/irohad/ametsuchi/wsv_query.hpp @@ -31,7 +31,7 @@ namespace iroha { struct AccountAsset; struct Peer; struct Asset; - } + } // namespace model namespace ametsuchi { diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index 8816d1ade0..5ebfbce8d7 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -118,9 +118,9 @@ void Irohad::initStorage() { }); PostgresOrderingServicePersistentState::create(pg_conn_).match( - [&](expected::Value> &_storage) { - ordering_service_storage_ = _storage.value; - }, + [&](expected::Value< + std::shared_ptr> + &_storage) { ordering_service_storage_ = _storage.value; }, [](expected::Error &error) { throw std::runtime_error(error.error); }); @@ -165,10 +165,7 @@ void Irohad::initValidators() { */ void Irohad::initOrderingGate() { ordering_gate = ordering_init.initOrderingGate( - wsv, - max_proposal_size_, - proposal_delay_, - ordering_service_storage_); + wsv, max_proposal_size_, proposal_delay_, ordering_service_storage_); log_->info("[Init] => init ordering gate - [{}]", logger::logBool(ordering_gate)); } diff --git a/irohad/main/impl/ordering_init.cpp b/irohad/main/impl/ordering_init.cpp index 3b40954d3f..bea4b33d52 100644 --- a/irohad/main/impl/ordering_init.cpp +++ b/irohad/main/impl/ordering_init.cpp @@ -16,8 +16,8 @@ */ #include "main/impl/ordering_init.hpp" -#include "model/peer.hpp" #include "ametsuchi/ordering_service_persistent_state.hpp" +#include "model/peer.hpp" namespace iroha { namespace network { diff --git a/irohad/ordering/impl/ordering_service_impl.cpp b/irohad/ordering/impl/ordering_service_impl.cpp index a049ea7b86..ec2269c10f 100644 --- a/irohad/ordering/impl/ordering_service_impl.cpp +++ b/irohad/ordering/impl/ordering_service_impl.cpp @@ -27,7 +27,8 @@ namespace iroha { size_t max_size, size_t delay_milliseconds, std::shared_ptr transport, - std::shared_ptr persistent_state) + std::shared_ptr + persistent_state) : wsv_(wsv), max_size_(max_size), delay_milliseconds_(delay_milliseconds), diff --git a/test/framework/integration_framework/integration_test_framework.hpp b/test/framework/integration_framework/integration_test_framework.hpp index 2577633ccb..4df58fcc61 100644 --- a/test/framework/integration_framework/integration_test_framework.hpp +++ b/test/framework/integration_framework/integration_test_framework.hpp @@ -50,8 +50,8 @@ namespace integration_framework { using BlockType = std::shared_ptr; public: - IntegrationTestFramework(size_t maximum_block_size = 10): - maximum_block_size_(maximum_block_size) {} + IntegrationTestFramework(size_t maximum_block_size = 10) + : maximum_block_size_(maximum_block_size) {} IntegrationTestFramework &setInitialState( const shared_model::crypto::Keypair &keypair); IntegrationTestFramework &setInitialState( diff --git a/test/integration/pipeline/transfer_asset_inter_domain_test.cpp b/test/integration/pipeline/transfer_asset_inter_domain_test.cpp index 02ed28a080..034433b0e4 100644 --- a/test/integration/pipeline/transfer_asset_inter_domain_test.cpp +++ b/test/integration/pipeline/transfer_asset_inter_domain_test.cpp @@ -60,16 +60,15 @@ class TransferAssetInterDomainTest : public TxPipelineIntegrationTestFixture { manager = std::make_shared("node0"); auto keypair = getVal(manager->loadKeys()); - irohad = std::make_shared( - block_store_path, - pgopt_, - 0, - 10001, - 10, - 5000ms, - 5000ms, - 5000ms, - keypair); + irohad = std::make_shared(block_store_path, + pgopt_, + 0, + 10001, + 10, + 5000ms, + 5000ms, + 5000ms, + keypair); ASSERT_TRUE(irohad->storage); irohad->storage->insertBlock(genesis_block); diff --git a/test/integration/pipeline/tx_pipeline_integration_test.cpp b/test/integration/pipeline/tx_pipeline_integration_test.cpp index e7b767dd67..dd29f29522 100644 --- a/test/integration/pipeline/tx_pipeline_integration_test.cpp +++ b/test/integration/pipeline/tx_pipeline_integration_test.cpp @@ -44,16 +44,15 @@ class TxPipelineIntegrationTest : public TxPipelineIntegrationTestFixture { manager = std::make_shared("node0"); auto keypair = manager->loadKeys().value(); - irohad = std::make_shared( - block_store_path, - pgopt_, - 0, - 10001, - 10, - 5000ms, - 5000ms, - 5000ms, - keypair); + irohad = std::make_shared(block_store_path, + pgopt_, + 0, + 10001, + 10, + 5000ms, + 5000ms, + 5000ms, + keypair); ASSERT_TRUE(irohad->storage); diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 0e974dbe55..aa2ffca468 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -20,6 +20,7 @@ #include #include "ametsuchi/impl/postgres_block_query.hpp" +#include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" #include "ametsuchi/impl/postgres_wsv_query.hpp" #include "ametsuchi/impl/storage_impl.hpp" #include "ametsuchi/mutable_storage.hpp" @@ -35,7 +36,6 @@ #include "model/permissions.hpp" #include "model/sha3_hash.hpp" #include "module/irohad/ametsuchi/ametsuchi_fixture.hpp" -#include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" using namespace iroha::ametsuchi; using namespace iroha::model; @@ -874,12 +874,14 @@ TEST_F(AmetsuchiTest, FindTxByHashTest) { * @then load proposal height and ensure it is correct */ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { - std::shared_ptr ordering_state; - auto orderingStorageResult = iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); + std::shared_ptr + ordering_state; + auto orderingStorageResult = + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); orderingStorageResult.match( - [&](iroha::expected::Value> &_storage) { - ordering_state = _storage.value; - }, + [&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, [](iroha::expected::Error &error) { FAIL() << "PostgresOrderingServicePersistentState: " << error.error; }); diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index 62c9ca4c08..cc73000066 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -137,8 +137,11 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { .Times(1) .WillOnce(Return(true)); - service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); + service = std::make_shared(wsv, + max_proposal, + commit_delay, + service_transport, + fake_persistent_state); service_transport->subscribe(service); start(); @@ -183,7 +186,6 @@ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { .Times(1) .WillOnce(Return(boost::optional(2))); - EXPECT_CALL(*fake_persistent_state, saveProposalHeight(3)) .Times(1) .WillOnce(Return(true)); @@ -191,8 +193,11 @@ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { .Times(1) .WillOnce(Return(true)); - service = std::make_shared( - wsv, max_proposal, commit_delay, service_transport, fake_persistent_state); + service = std::make_shared(wsv, + max_proposal, + commit_delay, + service_transport, + fake_persistent_state); service_transport->subscribe(service); start(); diff --git a/test/module/irohad/ordering/ordering_service_test.cpp b/test/module/irohad/ordering/ordering_service_test.cpp index b20084e9ae..5ffe5d4b91 100644 --- a/test/module/irohad/ordering/ordering_service_test.cpp +++ b/test/module/irohad/ordering/ordering_service_test.cpp @@ -23,11 +23,11 @@ #include "module/irohad/ametsuchi/ametsuchi_mocks.hpp" #include "module/irohad/network/network_mocks.hpp" +#include "ametsuchi/ordering_service_persistent_state.hpp" #include "mock_ordering_service_persistent_state.hpp" #include "ordering/impl/ordering_gate_impl.hpp" #include "ordering/impl/ordering_gate_transport_grpc.hpp" #include "ordering/impl/ordering_service_impl.hpp" -#include "ametsuchi/ordering_service_persistent_state.hpp" #include "ordering/impl/ordering_service_transport_grpc.hpp" #include "module/shared_model/builders/protobuf/test_proposal_builder.hpp" @@ -123,8 +123,7 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { const size_t max_proposal = 5; const size_t commit_delay = 1000; - EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)) - .Times(2); + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)).Times(2); EXPECT_CALL(*fake_persistent_state, loadProposalHeight()) .Times(1) @@ -157,8 +156,7 @@ TEST_F(OrderingServiceTest, ValidWhenProposalSizeStrategy) { TEST_F(OrderingServiceTest, ValidWhenTimerStrategy) { // Init => proposal timer 400 ms => 10 tx by 50 ms => 2 proposals in 1 second - EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)) - .Times(2); + EXPECT_CALL(*fake_persistent_state, saveProposalHeight(_)).Times(2); EXPECT_CALL(*wsv, getLedgerPeers()) .WillRepeatedly(Return(std::vector{peer})); From 19e157166fafa0a1bb512b024a584c585f14f20f Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Thu, 22 Feb 2018 19:00:27 +0300 Subject: [PATCH 14/23] fix pr issues Signed-off-by: Alexey Chernyshov --- ...gres_ordering_service_persistent_state.cpp | 57 +++++++++++-------- ...gres_ordering_service_persistent_state.hpp | 8 +-- .../ordering_service_persistent_state.hpp | 2 +- irohad/main/application.cpp | 11 ++-- .../irohad/ametsuchi/ametsuchi_test.cpp | 39 +++++++++++++ ...mock_ordering_service_persistent_state.hpp | 2 +- .../ordering/ordering_gate_service_test.cpp | 11 ++++ 7 files changed, 94 insertions(+), 36 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp index f50a38e9c7..d992d8d078 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp @@ -27,11 +27,7 @@ namespace iroha { expected::Result, std::string> PostgresOrderingServicePersistentState::create( - std::string postgres_options) { - auto log_ = - logger::log("PostgresOrderingServicePersistentState:initConnection"); - log_->info("Start storage creation"); - + const std::string &postgres_options) { // create connection auto postgres_connection = std::make_unique(postgres_options); @@ -42,21 +38,16 @@ namespace iroha { (boost::format("Connection to PostgreSQL broken: {}") % e.what()) .str()); } - log_->info("connection to PostgreSQL completed"); // create transaction auto postgres_transaction = std::make_unique( *postgres_connection, "Storage"); - log_->info("transaction to PostgreSQL initialized"); - expected::Result, std::string> storage; - storage = expected::makeValue( - std::shared_ptr( - new PostgresOrderingServicePersistentState( + storage = expected::makeValue(std::make_shared( std::move(postgres_connection), - std::move(postgres_transaction)))); + std::move(postgres_transaction))); return storage; } @@ -69,20 +60,34 @@ namespace iroha { log_(logger::log("PostgresOrderingServicePersistentState")), execute_{ametsuchi::makeExecuteResult(*postgres_transaction_)} {} - void PostgresOrderingServicePersistentState::initStorage() { + bool PostgresOrderingServicePersistentState::initStorage() { + bool result = true; log_->info("Init storage"); - postgres_transaction_->exec( - "CREATE TABLE IF NOT EXISTS ordering_service_state (\n" - " proposal_height bigserial\n" - ");" - "INSERT INTO ordering_service_state\n" - "VALUES (2); -- expected height (1 is genesis)"); + try { + postgres_transaction_->exec( + "CREATE TABLE IF NOT EXISTS ordering_service_state (\n" + " proposal_height bigserial\n" + ");" + "INSERT INTO ordering_service_state\n" + "VALUES (2); -- expected height (1 is genesis)"); + } catch (const std::exception &e) { + log_->error(e.what()); + result = false; + } + return result; } - void PostgresOrderingServicePersistentState::dropStorgage() { + bool PostgresOrderingServicePersistentState::dropStorgage() { + bool result = true; log_->info("Drop storage"); - postgres_transaction_->exec( - "DROP TABLE IF EXISTS ordering_service_state;"); + try { + postgres_transaction_->exec( + "DROP TABLE IF EXISTS ordering_service_state;"); + } catch (const std::exception &e) { + log_->error(e.what()); + result = false; + } + return result; } bool PostgresOrderingServicePersistentState::saveProposalHeight( @@ -123,9 +128,11 @@ namespace iroha { return height; } - void PostgresOrderingServicePersistentState::reset() { - dropStorgage(); - initStorage(); + bool PostgresOrderingServicePersistentState::reset() { + bool result = true; + result &= dropStorgage(); + result &= initStorage(); + return result; } } // namespace ametsuchi diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index dc7bd3d803..b116037a9b 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -42,7 +42,7 @@ namespace iroha { static expected::Result< std::shared_ptr, std::string> - create(std::string postgres_options); + create(const std::string &postgres_options); /** * Constructor @@ -57,12 +57,12 @@ namespace iroha { * Initialize storage. * Create tables and fill with initial value. */ - void initStorage(); + bool initStorage(); /** * Drop storage tables. */ - void dropStorgage(); + bool dropStorgage(); /** * Save proposal height that it can be restored @@ -78,7 +78,7 @@ namespace iroha { /** * Reset storage state to default */ - virtual void reset(); + virtual bool reset(); private: /** diff --git a/irohad/ametsuchi/ordering_service_persistent_state.hpp b/irohad/ametsuchi/ordering_service_persistent_state.hpp index 5e00b12430..4b99c82447 100644 --- a/irohad/ametsuchi/ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/ordering_service_persistent_state.hpp @@ -42,7 +42,7 @@ namespace iroha { /** * Reset storage to default state */ - virtual void reset() = 0; + virtual bool reset() = 0; }; } // namespace ordering } // namespace iroha diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index 5ebfbce8d7..2f5c69749b 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -113,23 +113,24 @@ void Irohad::initStorage() { [&](expected::Value> &_storage) { storage = _storage.value; }, - [](expected::Error &error) { - throw std::runtime_error(error.error); + [&](expected::Error &error) { + log_->error(error.error); }); PostgresOrderingServicePersistentState::create(pg_conn_).match( [&](expected::Value< std::shared_ptr> &_storage) { ordering_service_storage_ = _storage.value; }, - [](expected::Error &error) { - throw std::runtime_error(error.error); + [&](expected::Error &error) { + log_->error(error.error); }); log_->info("[Init] => storage", logger::logBool(storage)); } void Irohad::resetOrderingService() { - ordering_service_storage_->reset(); + if (not ordering_service_storage_->reset()) + log_->error("cannot reset ordering service storage"); } /** diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index aa2ffca468..21c9558d0a 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -896,3 +896,42 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { ordering_state->reset(); ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); } + +/** + * @given initialized storage for ordering service + * @when save proposal height + * @then load proposal height and ensure it is correct + */ +TEST_F(AmetsuchiTest, OrderingServicePersistentStorageRestartTest) { + std::shared_ptr + ordering_state; + auto orderingStorageResult = + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); + orderingStorageResult.match( + [&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " << error.error; + }); + ASSERT_TRUE(ordering_state); + + ordering_state->reset(); + ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); + ASSERT_TRUE(ordering_state->saveProposalHeight(11)); + ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); + + // restart Ordering Service Storage + ordering_state.reset(); + orderingStorageResult = + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); + orderingStorageResult.match( + [&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " << error.error; + }); + ASSERT_TRUE(ordering_state); + ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); +} \ No newline at end of file diff --git a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp index c98a9ab13e..e205d00caa 100644 --- a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp +++ b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp @@ -37,7 +37,7 @@ class MockOrderingServicePersistentState /** * Reset state */ - MOCK_METHOD0(reset, void()); + MOCK_METHOD0(reset, bool()); }; #endif // IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index cc73000066..623dee72da 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -117,6 +117,11 @@ class OrderingGateServiceTest : public ::testing::Test { std::shared_ptr fake_persistent_state; }; +/** + * @given ordering service + * @when a bunch of transaction has arrived + * @then proposal is sent + */ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { // 8 transaction -> proposal -> 2 transaction -> proposal @@ -172,6 +177,12 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { } } +/** + * @given ordering service + * @when a bunch of transaction has arrived + * @then split transactions on to two proposal + * + */ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { // commits on the fulfilling proposal queue // 10 transaction -> proposal with 5 -> proposal with 5 From df63d9fa10e038152196ce483b30f7ca72e70c1e Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Fri, 23 Feb 2018 11:23:41 +0300 Subject: [PATCH 15/23] rework exec() with makeExecuteResult() Signed-off-by: Alexey Chernyshov --- ...gres_ordering_service_persistent_state.cpp | 52 ++++++++----------- irohad/ametsuchi/wsv_query.hpp | 1 + .../irohad/ametsuchi/ametsuchi_test.cpp | 2 +- .../ordering/ordering_gate_service_test.cpp | 1 - 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp index d992d8d078..52cae6818d 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp @@ -18,7 +18,6 @@ #include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp" #include #include -#include #include "common/types.hpp" namespace iroha { @@ -45,9 +44,9 @@ namespace iroha { expected::Result, std::string> storage; - storage = expected::makeValue(std::make_shared( - std::move(postgres_connection), - std::move(postgres_transaction))); + storage = expected::makeValue( + std::make_shared( + std::move(postgres_connection), std::move(postgres_transaction))); return storage; } @@ -61,33 +60,27 @@ namespace iroha { execute_{ametsuchi::makeExecuteResult(*postgres_transaction_)} {} bool PostgresOrderingServicePersistentState::initStorage() { - bool result = true; - log_->info("Init storage"); - try { - postgres_transaction_->exec( - "CREATE TABLE IF NOT EXISTS ordering_service_state (\n" - " proposal_height bigserial\n" - ");" - "INSERT INTO ordering_service_state\n" - "VALUES (2); -- expected height (1 is genesis)"); - } catch (const std::exception &e) { - log_->error(e.what()); - result = false; - } - return result; + return execute_( + "CREATE TABLE IF NOT EXISTS ordering_service_state (\n" + " proposal_height bigserial\n" + ");\n" + "INSERT INTO ordering_service_state\n" + "VALUES (2); -- expected height (1 is genesis)") + .match([](expected::Value v) -> bool { return true; }, + [&](expected::Error e) -> bool { + log_->error(e.error); + return false; + }); } bool PostgresOrderingServicePersistentState::dropStorgage() { - bool result = true; log_->info("Drop storage"); - try { - postgres_transaction_->exec( - "DROP TABLE IF EXISTS ordering_service_state;"); - } catch (const std::exception &e) { - log_->error(e.what()); - result = false; - } - return result; + return execute_("DROP TABLE IF EXISTS ordering_service_state;") + .match([](expected::Value v) -> bool { return true; }, + [&](expected::Error e) -> bool { + log_->error(e.error); + return false; + }); } bool PostgresOrderingServicePersistentState::saveProposalHeight( @@ -129,10 +122,7 @@ namespace iroha { } bool PostgresOrderingServicePersistentState::reset() { - bool result = true; - result &= dropStorgage(); - result &= initStorage(); - return result; + return dropStorgage() & initStorage(); } } // namespace ametsuchi diff --git a/irohad/ametsuchi/wsv_query.hpp b/irohad/ametsuchi/wsv_query.hpp index bba0820bc0..24ea298dcb 100644 --- a/irohad/ametsuchi/wsv_query.hpp +++ b/irohad/ametsuchi/wsv_query.hpp @@ -69,6 +69,7 @@ namespace iroha { */ virtual nonstd::optional> getAccountRoles( const std::string &account_id) = 0; + /** * Get all permissions of a role * @param role_name diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 21c9558d0a..4d74d3aa46 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -934,4 +934,4 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageRestartTest) { }); ASSERT_TRUE(ordering_state); ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); -} \ No newline at end of file +} diff --git a/test/module/irohad/ordering/ordering_gate_service_test.cpp b/test/module/irohad/ordering/ordering_gate_service_test.cpp index 623dee72da..90aaabf6cb 100644 --- a/test/module/irohad/ordering/ordering_gate_service_test.cpp +++ b/test/module/irohad/ordering/ordering_gate_service_test.cpp @@ -181,7 +181,6 @@ TEST_F(OrderingGateServiceTest, SplittingBunchTransactions) { * @given ordering service * @when a bunch of transaction has arrived * @then split transactions on to two proposal - * */ TEST_F(OrderingGateServiceTest, ProposalsReceivedWhenProposalSize) { // commits on the fulfilling proposal queue From 7d3123d32f5d3464320b1311a9e4b915ae966b3e Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Fri, 23 Feb 2018 11:43:11 +0300 Subject: [PATCH 16/23] add test for ordering service state with different connections Signed-off-by: Alexey Chernyshov --- .../irohad/ametsuchi/ametsuchi_test.cpp | 97 +++++++++++++------ 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 4d74d3aa46..fa6512554a 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -876,15 +876,14 @@ TEST_F(AmetsuchiTest, FindTxByHashTest) { TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { std::shared_ptr ordering_state; - auto orderingStorageResult = - iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); - orderingStorageResult.match( - [&](iroha::expected::Value> - &_storage) { ordering_state = _storage.value; }, - [](iroha::expected::Error &error) { - FAIL() << "PostgresOrderingServicePersistentState: " << error.error; - }); + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_) + .match([&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " + << error.error; + }); ASSERT_TRUE(ordering_state); ordering_state->reset(); @@ -905,15 +904,14 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { TEST_F(AmetsuchiTest, OrderingServicePersistentStorageRestartTest) { std::shared_ptr ordering_state; - auto orderingStorageResult = - iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); - orderingStorageResult.match( - [&](iroha::expected::Value> - &_storage) { ordering_state = _storage.value; }, - [](iroha::expected::Error &error) { - FAIL() << "PostgresOrderingServicePersistentState: " << error.error; - }); + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_) + .match([&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " + << error.error; + }); ASSERT_TRUE(ordering_state); ordering_state->reset(); @@ -923,15 +921,60 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageRestartTest) { // restart Ordering Service Storage ordering_state.reset(); - orderingStorageResult = - iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_); - orderingStorageResult.match( - [&](iroha::expected::Value> - &_storage) { ordering_state = _storage.value; }, - [](iroha::expected::Error &error) { - FAIL() << "PostgresOrderingServicePersistentState: " << error.error; - }); + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_) + .match([&](iroha::expected::Value> + &_storage) { ordering_state = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " + << error.error; + }); ASSERT_TRUE(ordering_state); ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); } + +/** + * @given 2 different initialized storages for ordering service + * @when save proposal height to the first one + * @then the state is consistent + */ +TEST_F(AmetsuchiTest, + OrderingServicePersistentStorageDifferentConnectionsTest) { + std::shared_ptr + ordering_state_1; + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_) + .match([&](iroha::expected::Value> + &_storage) { ordering_state_1 = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " + << error.error; + }); + ASSERT_TRUE(ordering_state_1); + + std::shared_ptr + ordering_state_2; + iroha::ametsuchi::PostgresOrderingServicePersistentState::create(pgopt_) + .match([&](iroha::expected::Value> + &_storage) { ordering_state_2 = _storage.value; }, + [](iroha::expected::Error &error) { + FAIL() << "PostgresOrderingServicePersistentState: " + << error.error; + }); + ASSERT_TRUE(ordering_state_2); + + ordering_state_2->reset(); + ASSERT_EQ(2, ordering_state_1->loadProposalHeight().value()); + ASSERT_EQ(2, ordering_state_2->loadProposalHeight().value()); + ASSERT_TRUE(ordering_state_1->saveProposalHeight(11)); + ASSERT_EQ(11, ordering_state_1->loadProposalHeight().value()); + ASSERT_EQ(11, ordering_state_2->loadProposalHeight().value()); + + ordering_state_2->reset(); + ASSERT_EQ(2, ordering_state_1->loadProposalHeight().value()); + ASSERT_EQ(2, ordering_state_2->loadProposalHeight().value()); + ASSERT_TRUE(ordering_state_2->saveProposalHeight(42)); + ASSERT_EQ(42, ordering_state_1->loadProposalHeight().value()); + ASSERT_EQ(42, ordering_state_2->loadProposalHeight().value()); +} From 4a97eb13e163c589392888b5f71a5a4cf72877e5 Mon Sep 17 00:00:00 2001 From: Bogdan Vaneev Date: Fri, 23 Feb 2018 14:29:49 +0200 Subject: [PATCH 17/23] Reduce parallelism for debug build Signed-off-by: Bogdan Vaneev --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b8f39dc50a..f78692cf47 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -48,7 +48,7 @@ jobs: - run: name: make command: | - cmake --build $IROHA_BUILD -- -j3 + cmake --build $IROHA_BUILD -- -j2 - run: ccache --show-stats - run: name: unit tests, generate xunit-*.xml @@ -112,7 +112,7 @@ jobs: - run: name: make and package command: | - cmake --build $IROHA_BUILD --target package -- -j4 + cmake --build $IROHA_BUILD --target package -- -j$(nproc --all) - run: ccache --show-stats - save_cache: key: build-linux-release-{{ arch }}-{{ .Branch }}-{{ epoch }} From 47a2177403b65d6021b6857171ba6d4a2ff684e2 Mon Sep 17 00:00:00 2001 From: Bogdan Vaneev Date: Fri, 23 Feb 2018 14:42:49 +0200 Subject: [PATCH 18/23] Pass cmake args - compiler generator - launcher C, CXX (for ccache) - identation fixes - build type Signed-off-by: Bogdan Vaneev --- cmake/Modules/Findbenchmark.cmake | 23 +++++++++-- cmake/Modules/Finded25519.cmake | 26 +++++++++---- cmake/Modules/Findgflags.cmake | 27 +++++++++---- cmake/Modules/Findgrpc.cmake | 65 +++++++++++++++++++++---------- cmake/Modules/Findgtest.cmake | 41 ++++++++++++------- cmake/Modules/Findoptional.cmake | 14 +++---- cmake/Modules/Findpq.cmake | 26 ++++++++----- cmake/Modules/Findpqxx.cmake | 30 +++++++++----- cmake/Modules/Findprotobuf.cmake | 32 ++++++++++----- cmake/Modules/Findrapidjson.cmake | 12 +++--- cmake/Modules/Findrxcpp.cmake | 12 +++--- cmake/Modules/Findspdlog.cmake | 12 +++--- cmake/Modules/Findtbb.cmake | 31 +++++++++------ cmake/dependencies.cmake | 6 +++ cmake/functions.cmake | 18 +++++++++ 15 files changed, 257 insertions(+), 118 deletions(-) diff --git a/cmake/Modules/Findbenchmark.cmake b/cmake/Modules/Findbenchmark.cmake index 31c1672b33..3472858bdf 100644 --- a/cmake/Modules/Findbenchmark.cmake +++ b/cmake/Modules/Findbenchmark.cmake @@ -12,17 +12,32 @@ find_package_handle_standard_args(benchmark DEFAULT_MSG benchmark_LIBRARY ) +iroha_get_lib_name(BENCHLIB benchmark STATIC) + if (NOT benchmark_FOUND) externalproject_add(google_benchmark GIT_REPOSITORY https://github.com/google/benchmark GIT_TAG v1.2.0 - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + CMAKE_ARGS + -G${CMAKE_GENERATOR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + BUILD_BYPRODUCTS + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${BENCHLIB} + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(google_benchmark source_dir binary_dir) set(benchmark_INCLUDE_DIR ${source_dir}/include) - set(benchmark_LIBRARY ${binary_dir}/src/libbenchmark.a) + + set(benchmark_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${BENCHLIB}) + file(MAKE_DIRECTORY ${benchmark_INCLUDE_DIR}) add_dependencies(benchmark google_benchmark) diff --git a/cmake/Modules/Finded25519.cmake b/cmake/Modules/Finded25519.cmake index 30810b1bea..6810bd4a6b 100644 --- a/cmake/Modules/Finded25519.cmake +++ b/cmake/Modules/Finded25519.cmake @@ -15,11 +15,28 @@ set(URL https://github.com/hyperledger/iroha-ed25519) set(VERSION e7188b8393dbe5ac54378610d53630bd4a180038) set_target_description(ed25519 "Digital signature algorithm" ${URL} ${VERSION}) +iroha_get_lib_name(EDLIB ed25519 STATIC) + if (NOT ed25519_FOUND) externalproject_add(hyperledger_ed25519 GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CMAKE_ARGS -DTESTING=OFF -DBUILD=STATIC + CMAKE_ARGS + -DEDIMPL=ref10 + -DHASH=sha3_brainhub + -DRANDOM=dev_urandom + + -DTESTING=OFF + -DBUILD_TESTING=OFF + -DBUILD=STATIC + + -G${CMAKE_GENERATOR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + BUILD_BYPRODUCTS + ${EP_PREFIX}/src/hyperledger_ed25519-build${XCODE_EXT}/${EDLIB} INSTALL_COMMAND "" # remove install step TEST_COMMAND "" # remove test step UPDATE_COMMAND "" # remove update step @@ -27,9 +44,8 @@ if (NOT ed25519_FOUND) externalproject_get_property(hyperledger_ed25519 binary_dir) externalproject_get_property(hyperledger_ed25519 source_dir) set(ed25519_INCLUDE_DIR ${source_dir}/include) - set(ed25519_LIBRARY ${binary_dir}/${CMAKE_STATIC_LIBRARY_PREFIX}ed25519${CMAKE_STATIC_LIBRARY_SUFFIX}) + set(ed25519_LIBRARY ${binary_dir}${XCODE_EXT}/${EDLIB}) file(MAKE_DIRECTORY ${ed25519_INCLUDE_DIR}) - link_directories(${binary_dir}) add_dependencies(ed25519 hyperledger_ed25519) endif () @@ -38,7 +54,3 @@ set_target_properties(ed25519 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${ed25519_INCLUDE_DIR} IMPORTED_LOCATION ${ed25519_LIBRARY} ) - -if(ENABLE_LIBS_PACKAGING) - add_install_step_for_lib(${ed25519_LIBRARY}) -endif() diff --git a/cmake/Modules/Findgflags.cmake b/cmake/Modules/Findgflags.cmake index 3d3c375cfb..91bed591f5 100644 --- a/cmake/Modules/Findgflags.cmake +++ b/cmake/Modules/Findgflags.cmake @@ -15,19 +15,32 @@ set(URL https://github.com/gflags/gflags.git) set(VERSION f8a0efe03aa69b3336d8e228b37d4ccb17324b88) set_target_description(gflags "Flag parsing engine" ${URL} ${VERSION}) +iroha_get_lib_name(GFLAGSLIB gflags STATIC) if (NOT gflags_FOUND) externalproject_add(gflags_gflags - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} - BUILD_BYPRODUCTS ${EP_PREFIX}/src/gflags_gflags-build/lib/libgflags.a - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} + CMAKE_ARGS + -G${CMAKE_GENERATOR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + BUILD_BYPRODUCTS + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GFLAGSLIB} + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(gflags_gflags binary_dir) set(gflags_INCLUDE_DIR ${binary_dir}/include) - set(gflags_LIBRARY ${binary_dir}/lib/libgflags.a) + + set(gflags_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GFLAGSLIB}) + file(MAKE_DIRECTORY ${gflags_INCLUDE_DIR}) add_dependencies(gflags gflags_gflags) diff --git a/cmake/Modules/Findgrpc.cmake b/cmake/Modules/Findgrpc.cmake index e05b1b6274..a032d8afce 100644 --- a/cmake/Modules/Findgrpc.cmake +++ b/cmake/Modules/Findgrpc.cmake @@ -34,34 +34,56 @@ set(URL https://github.com/grpc/grpc) set(VERSION bfcbad3b86c7912968dc8e64f2121c920dad4dfb) set_target_description(grpc "Remote Procedure Call library" ${URL} ${VERSION}) +iroha_get_lib_name(GPRLIB gpr SHARED) +iroha_get_lib_name(GRPCLIB grpc SHARED) +iroha_get_lib_name(GRPCPPLIB grpc++ SHARED) +iroha_get_lib_name(GRPCPPREFLIB grpc++_reflection SHARED) + if (NOT grpc_FOUND) find_package(Git REQUIRED) externalproject_add(grpc_grpc GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CMAKE_ARGS -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG -DProtobuf_DIR=${EP_PREFIX}/src/google_protobuf-build/lib/cmake/protobuf -DgRPC_ZLIB_PROVIDER=package -DBUILD_SHARED_LIBS=ON - PATCH_COMMAND ${GIT_EXECUTABLE} apply ${PROJECT_SOURCE_DIR}/patch/fix-protobuf-package-include.patch || true - BUILD_BYPRODUCTS ${EP_PREFIX}/src/grpc_grpc-build/grpc_cpp_plugin - ${EP_PREFIX}/src/grpc_grpc-build/${CMAKE_SHARED_LIBRARY_PREFIX}grpc${CMAKE_SHARED_LIBRARY_SUFFIX} - ${EP_PREFIX}/src/grpc_grpc-build/${CMAKE_SHARED_LIBRARY_PREFIX}grpc++${CMAKE_SHARED_LIBRARY_SUFFIX} - ${EP_PREFIX}/src/grpc_grpc-build/${CMAKE_SHARED_LIBRARY_PREFIX}grpc++_reflection${CMAKE_SHARED_LIBRARY_SUFFIX} + CMAKE_ARGS + -DgRPC_PROTOBUF_PROVIDER=package + -DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG + -DProtobuf_DIR=${EP_PREFIX}/src/google_protobuf-build/lib/cmake/protobuf + -DgRPC_ZLIB_PROVIDER=package + -DBUILD_SHARED_LIBS=ON + -G${CMAKE_GENERATOR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + PATCH_COMMAND + ${GIT_EXECUTABLE} apply ${PROJECT_SOURCE_DIR}/patch/fix-protobuf-package-include.patch || true + BUILD_BYPRODUCTS + ${CMAKE_BINARY_DIR}${XCODE_EXT}/grpc_cpp_plugin + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GPRLIB} + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCLIB} + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPLIB} +# ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPREFLIB} INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(grpc_grpc source_dir binary_dir) - set(grpc_INCLUDE_DIR ${source_dir}/include) - set(grpc_LIBRARY ${binary_dir}/${CMAKE_SHARED_LIBRARY_PREFIX}grpc${CMAKE_SHARED_LIBRARY_SUFFIX}) - set(grpc_grpc++_LIBRARY ${binary_dir}/${CMAKE_SHARED_LIBRARY_PREFIX}grpc++${CMAKE_SHARED_LIBRARY_SUFFIX}) - set(grpc_grpc++_reflection_LIBRARY ${binary_dir}/${CMAKE_SHARED_LIBRARY_PREFIX}grpc++_reflection${CMAKE_SHARED_LIBRARY_SUFFIX}) - set(grpc_CPP_PLUGIN ${binary_dir}/grpc_cpp_plugin) + set(grpc_INCLUDE_DIR ${source_dir}/include) + set(gpr_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GPRLIB}) + set(grpc_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCLIB}) + set(grpc_grpc++_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPLIB}) +# set(grpc_grpc++_reflection_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPREFLIB}) + set(grpc_CPP_PLUGIN ${CMAKE_BINARY_DIR}${XCODE_EXT}/grpc_cpp_plugin) + file(MAKE_DIRECTORY ${grpc_INCLUDE_DIR}) - link_directories(${binary_dir}) add_dependencies(grpc_grpc protobuf) add_dependencies(grpc grpc_grpc) add_dependencies(grpc++ grpc_grpc) - add_dependencies(grpc++_reflection grpc_grpc) +# add_dependencies(grpc++_reflection grpc_grpc) add_dependencies(grpc_cpp_plugin grpc_grpc protoc) endif () @@ -77,11 +99,11 @@ set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${grpc_grpc++_LIBRARY} ) -set_target_properties(grpc++_reflection PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${grpc_INCLUDE_DIR} - INTERFACE_LINK_LIBRARIES grpc++ - IMPORTED_LOCATION ${grpc_grpc++_reflection_LIBRARY} - ) +#set_target_properties(grpc++_reflection PROPERTIES +# INTERFACE_INCLUDE_DIRECTORIES ${grpc_INCLUDE_DIR} +# INTERFACE_LINK_LIBRARIES grpc++ +# IMPORTED_LOCATION ${grpc_grpc++_reflection_LIBRARY} +# ) set_target_properties(grpc_cpp_plugin PROPERTIES IMPORTED_LOCATION ${grpc_CPP_PLUGIN} @@ -92,7 +114,8 @@ set_target_properties(gpr PROPERTIES ) if(ENABLE_LIBS_PACKAGING) + add_install_step_for_lib(${gpr_LIBRARY}) add_install_step_for_lib(${grpc_LIBRARY}) add_install_step_for_lib(${grpc_grpc++_LIBRARY}) - add_install_step_for_lib(${gpr_LIBRARY}) +# add_install_step_for_lib(${grpc_grpc++_reflection_LIBRARY}) endif() diff --git a/cmake/Modules/Findgtest.cmake b/cmake/Modules/Findgtest.cmake index 22e6cd6b63..e607c0f54a 100644 --- a/cmake/Modules/Findgtest.cmake +++ b/cmake/Modules/Findgtest.cmake @@ -33,31 +33,44 @@ set(VERSION ec44c6c1675c25b9827aacd08c02433cccde7780) set_target_description(gtest "Unit testing library" ${URL} ${VERSION}) set_target_description(gmock "Mocking library" ${URL} ${VERSION}) +iroha_get_lib_name(GTESTMAINLIB gtest_main STATIC) +iroha_get_lib_name(GTESTLIB gtest STATIC) +iroha_get_lib_name(GMOCKMAINLIB gmock_main STATIC) +iroha_get_lib_name(GMOCKLIB gmock STATIC) + if (NOT gtest_FOUND) ExternalProject_Add(google_test GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -Dgtest_force_shared_crt=ON - -Dgtest_disable_pthreads=OFF - BUILD_BYPRODUCTS ${EP_PREFIX}/src/google_test-build/googlemock/gtest/libgtest_main.a - ${EP_PREFIX}/src/google_test-build/googlemock/gtest/libgtest.a - ${EP_PREFIX}/src/google_test-build/googlemock/libgmock_main.a - ${EP_PREFIX}/src/google_test-build/googlemock/libgmock.a + CMAKE_ARGS + -Dgtest_force_shared_crt=ON + -Dgtest_disable_pthreads=OFF + -G${CMAKE_GENERATOR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + BUILD_BYPRODUCTS + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GTESTMAINLIB} + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GTESTLIB} + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GMOCKMAINLIB} + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GMOCKLIB} INSTALL_COMMAND "" # remove install step - UPDATE_COMMAND "" # remove update step - TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step ) ExternalProject_Get_Property(google_test source_dir binary_dir) set(gtest_INCLUDE_DIR ${source_dir}/googletest/include) set(gmock_INCLUDE_DIR ${source_dir}/googlemock/include) - set(gtest_MAIN_LIBRARY ${binary_dir}/googlemock/gtest/libgtest_main.a) - set(gtest_LIBRARY ${binary_dir}/googlemock/gtest/libgtest.a) + set(gtest_MAIN_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GTESTMAINLIB}) + set(gtest_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GTESTLIB}) - set(gmock_MAIN_LIBRARY ${binary_dir}/googlemock/libgmock_main.a) - set(gmock_LIBRARY ${binary_dir}/googlemock/libgmock.a) + set(gmock_MAIN_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GMOCKMAINLIB}) + set(gmock_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GMOCKLIB}) file(MAKE_DIRECTORY ${gtest_INCLUDE_DIR}) file(MAKE_DIRECTORY ${gmock_INCLUDE_DIR}) diff --git a/cmake/Modules/Findoptional.cmake b/cmake/Modules/Findoptional.cmake index 26e5246e5e..b8dcecf0ba 100644 --- a/cmake/Modules/Findoptional.cmake +++ b/cmake/Modules/Findoptional.cmake @@ -14,17 +14,17 @@ set_target_description(optional "Data structure for optional types" ${URL} ${VER if (NOT optional_FOUND) externalproject_add(martinmoene_optional - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} CONFIGURE_COMMAND "" # remove configure step - BUILD_COMMAND "" # remove build step - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + BUILD_COMMAND "" # remove build step + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(martinmoene_optional source_dir) set(optional_INCLUDE_DIR ${source_dir}/include) - file(MAKE_DIRECTORY ${optional_INCLUDE_DIR}) + file(MAKE_DIRECTORY ${optional_INCLUDE_DIR}) add_dependencies(optional martinmoene_optional) endif () diff --git a/cmake/Modules/Findpq.cmake b/cmake/Modules/Findpq.cmake index f6acaaaf10..6991c0a52c 100644 --- a/cmake/Modules/Findpq.cmake +++ b/cmake/Modules/Findpq.cmake @@ -25,25 +25,33 @@ set(URL https://git.postgresql.org/git/postgresql.git) set(VERSION 029386ccbddd0a33d481b94e511f5219b03e6636) set_target_description(pq "C postgres client library" ${URL} ${VERSION}) +iroha_get_lib_name(PQLIB pq STATIC) if (NOT pq_FOUND) + set(pqlib_path ${EP_PREFIX}/src/postgres_postgres/src/interfaces/libpq/${PQLIB}) + set(pqconfig_path ${EP_PREFIX}/src/postgres_postgres/src/bin/pq_config/pq_config) + externalproject_add(postgres_postgres GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CONFIGURE_COMMAND ./configure --without-readline + CONFIGURE_COMMAND + ./configure --without-readline BUILD_IN_SOURCE 1 - BUILD_COMMAND $(MAKE) -C ./src/bin/pg_config && $(MAKE) -C ./src/interfaces/libpq - BUILD_BYPRODUCTS ${EP_PREFIX}/src/postgres_postgres/src/interfaces/libpq/libpq.a + BUILD_COMMAND + $(MAKE) -C ./src/bin/pg_config && $(MAKE) -C ./src/interfaces/libpq + BUILD_BYPRODUCTS + ${pqlib_path} + ${pqconfig_path} INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(postgres_postgres source_dir) set(postgres_INCLUDE_DIR ${source_dir}/src/include) - set(pq_INCLUDE_DIR ${source_dir}/src/interfaces/libpq) - set(pq_LIBRARY ${source_dir}/src/interfaces/libpq/libpq.a) - set(pg_config_EXECUTABLE ${source_dir}/src/bin/pg_config/pg_config) - file(MAKE_DIRECTORY ${pq_INCLUDE_DIR} ${postgres_INCLUDE_DIR}) + set(pq_INCLUDE_DIR ${source_dir}/src/interfaces/libpq) + set(pq_LIBRARY ${pqlib_path}) + set(pg_config_EXECUTABLE ${pqconfig_path}) + file(MAKE_DIRECTORY ${pq_INCLUDE_DIR} ${postgres_INCLUDE_DIR}) add_dependencies(pg_config postgres_postgres) add_dependencies(pq postgres_postgres pg_config) diff --git a/cmake/Modules/Findpqxx.cmake b/cmake/Modules/Findpqxx.cmake index 927e22e854..d62c1192a3 100644 --- a/cmake/Modules/Findpqxx.cmake +++ b/cmake/Modules/Findpqxx.cmake @@ -15,27 +15,37 @@ set(URL https://github.com/jtv/libpqxx.git) set(VERSION 5b17abce5ac2b1a2f8278718405b7ade8bb30ae9) set_target_description(pqxx "C++ bindings for postgres client library" ${URL} ${VERSION}) +iroha_get_lib_name(PQXX pqxx STATIC) + if (NOT pqxx_FOUND) externalproject_add(jtv_libpqxx GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env CXXFLAGS=${CMAKE_CXX_FLAGS} CPPFLAGS=-I${postgres_INCLUDE_DIR} PG_CONFIG=${pg_config_EXECUTABLE} ./configure --disable-documentation --with-postgres-include=${pq_INCLUDE_DIR} --with-postgres-lib=${pq_INCLUDE_DIR} + CONFIGURE_COMMAND + ${CMAKE_COMMAND} -E env + CPPFLAGS=-I${postgres_INCLUDE_DIR} + PG_CONFIG=${pg_config_EXECUTABLE} + ./configure + --disable-documentation + --with-postgres-include=${pq_INCLUDE_DIR} + --with-postgres-lib=${pq_INCLUDE_DIR} BUILD_IN_SOURCE 1 BUILD_COMMAND $(MAKE) - BUILD_BYPRODUCTS ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/libpqxx.a - ${EP_PREFIX}/src/jtv_libpqxx/src/libpqxx.la - ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/libpqxx.la - ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/libpqxx.lai + BUILD_BYPRODUCTS + ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/${PQXX} + ${EP_PREFIX}/src/jtv_libpqxx/src/libpqxx.la + ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/libpqxx.la + ${EP_PREFIX}/src/jtv_libpqxx/src/.libs/libpqxx.lai INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(jtv_libpqxx source_dir) set(pqxx_INCLUDE_DIR ${source_dir}/include) - set(pqxx_LIBRARY ${source_dir}/src/.libs/libpqxx.a) - file(MAKE_DIRECTORY ${pqxx_INCLUDE_DIR}) + set(pqxx_LIBRARY ${source_dir}/src/.libs/${PQXX}) + file(MAKE_DIRECTORY ${pqxx_INCLUDE_DIR}) - add_dependencies(jtv_libpqxx pq) + add_dependencies(jtv_libpqxx pq pg_config) add_dependencies(pqxx jtv_libpqxx) endif () diff --git a/cmake/Modules/Findprotobuf.cmake b/cmake/Modules/Findprotobuf.cmake index ac033a5391..0df66e7f21 100644 --- a/cmake/Modules/Findprotobuf.cmake +++ b/cmake/Modules/Findprotobuf.cmake @@ -22,23 +22,37 @@ set(URL https://github.com/google/protobuf.git) set(VERSION 80a37e0782d2d702d52234b62dd4b9ec74fd2c95) set_target_description(protobuf "Protocol buffers library" ${URL} ${VERSION}) +iroha_get_lib_name(PROTOLIB protobuf SHARED) + if (NOT protobuf_FOUND) externalproject_add(google_protobuf GIT_REPOSITORY ${URL} GIT_TAG ${VERSION} - CONFIGURE_COMMAND ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} -H${EP_PREFIX}/src/google_protobuf/cmake -B${EP_PREFIX}/src/google_protobuf-build -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON - BUILD_BYPRODUCTS ${EP_PREFIX}/src/google_protobuf-build/protoc - ${EP_PREFIX}/src/google_protobuf-build/${CMAKE_SHARED_LIBRARY_PREFIX}protobuf${CMAKE_SHARED_LIBRARY_SUFFIX} + CMAKE_ARGS + -G${CMAKE_GENERATOR} + -H${EP_PREFIX}/src/google_protobuf/cmake + -B${EP_PREFIX}/src/google_protobuf-build + -Dprotobuf_BUILD_TESTS=OFF + -Dprotobuf_BUILD_SHARED_LIBS=ON + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} + -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR} + BUILD_BYPRODUCTS + ${CMAKE_BINARY_DIR}${XCODE_EXT}/protoc + ${CMAKE_BINARY_DIR}${XCODE_EXT}/${PROTOLIB} INSTALL_COMMAND "" - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(google_protobuf source_dir binary_dir) set(protobuf_INCLUDE_DIR ${source_dir}/src) - set(protobuf_LIBRARY ${binary_dir}/${CMAKE_SHARED_LIBRARY_PREFIX}protobuf${CMAKE_SHARED_LIBRARY_SUFFIX}) - set(protoc_EXECUTABLE ${binary_dir}/protoc) - file(MAKE_DIRECTORY ${protobuf_INCLUDE_DIR}) - link_directories(${binary_dir}) + set(protobuf_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${PROTOLIB}) + set(protoc_EXECUTABLE ${CMAKE_BINARY_DIR}${XCODE_EXT}/protoc) + file(MAKE_DIRECTORY ${protobuf_INCLUDE_DIR}) add_dependencies(protoc google_protobuf) add_dependencies(protobuf google_protobuf protoc) diff --git a/cmake/Modules/Findrapidjson.cmake b/cmake/Modules/Findrapidjson.cmake index eaa3805e9b..012bdd9794 100644 --- a/cmake/Modules/Findrapidjson.cmake +++ b/cmake/Modules/Findrapidjson.cmake @@ -13,13 +13,13 @@ set_target_description(rapidjson "JSON library" ${URL} ${VERSION}) if (NOT rapidjson_FOUND) externalproject_add(miloyip_rapidjson - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} - BUILD_COMMAND "" # remove build step, header only lib + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} + BUILD_COMMAND "" # remove build step, header only lib CONFIGURE_COMMAND "" # remove configure step - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(miloyip_rapidjson source_dir) set(rapidjson_INCLUDE_DIR "${source_dir}/include") diff --git a/cmake/Modules/Findrxcpp.cmake b/cmake/Modules/Findrxcpp.cmake index 8f269d7d63..db917cb2c9 100644 --- a/cmake/Modules/Findrxcpp.cmake +++ b/cmake/Modules/Findrxcpp.cmake @@ -15,13 +15,13 @@ set_target_description(rxcpp "Library for reactive programming" ${URL} ${VERSION if (NOT rxcpp_FOUND) externalproject_add(reactive_extensions_rxcpp - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" # remove install step - UPDATE_COMMAND "" # remove update step - TEST_COMMAND "" # remove test step + BUILD_COMMAND "" + INSTALL_COMMAND "" # remove install step + UPDATE_COMMAND "" # remove update step + TEST_COMMAND "" # remove test step ) externalproject_get_property(reactive_extensions_rxcpp source_dir) set(rxcpp_INCLUDE_DIR ${source_dir}/Rx/v2/src) diff --git a/cmake/Modules/Findspdlog.cmake b/cmake/Modules/Findspdlog.cmake index 0237b97193..0c2cfaf06b 100644 --- a/cmake/Modules/Findspdlog.cmake +++ b/cmake/Modules/Findspdlog.cmake @@ -15,13 +15,13 @@ set_target_description(spdlog "Logging library" ${URL} ${VERSION}) if (NOT SPDLOG_FOUND) externalproject_add(gabime_spdlog - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} CONFIGURE_COMMAND "" # remove configure step - BUILD_COMMAND "" # remove build step - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + BUILD_COMMAND "" # remove build step + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) externalproject_get_property(gabime_spdlog source_dir) set(spdlog_INCLUDE_DIR ${source_dir}/include) diff --git a/cmake/Modules/Findtbb.cmake b/cmake/Modules/Findtbb.cmake index 7c88a98627..b3bebce886 100644 --- a/cmake/Modules/Findtbb.cmake +++ b/cmake/Modules/Findtbb.cmake @@ -16,30 +16,37 @@ set(URL https://github.com/01org/tbb.git) set(VERSION eb6336ad29450f2a64af5123ca1b9429ff6bc11d) set_target_description(tbb "Concurrent queue" ${URL} ${VERSION}) +iroha_get_lib_name(RLIB tbb SHARED) +iroha_get_lib_name(DLIB tbb_debug SHARED) if (NOT tbb_FOUND) + set(release_lib_path ${EP_PREFIX}/src/01org_tbb/build/build_release/${RLIB}) + set(debug_lib_path ${EP_PREFIX}/src/01org_tbb/build/build_debug/${DLIB}) + ExternalProject_Add(01org_tbb - GIT_REPOSITORY ${URL} - GIT_TAG ${VERSION} + GIT_REPOSITORY ${URL} + GIT_TAG ${VERSION} BUILD_IN_SOURCE 1 - BUILD_COMMAND $(MAKE) tbb_build_prefix=build - BUILD_BYPRODUCTS ${EP_PREFIX}/src/01org_tbb/build/build_debug/${CMAKE_SHARED_LIBRARY_PREFIX}tbb_debug${CMAKE_SHARED_LIBRARY_SUFFIX} - ${EP_PREFIX}/src/01org_tbb/build/build_release/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX} + BUILD_COMMAND + $(MAKE) info && $(MAKE) tbb_build_prefix=build + BUILD_BYPRODUCTS + ${release_lib_path} + ${debug_lib_path} CONFIGURE_COMMAND "" # remove configure step - INSTALL_COMMAND "" # remove install step - TEST_COMMAND "" # remove test step - UPDATE_COMMAND "" # remove update step + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step ) ExternalProject_Get_Property(01org_tbb source_dir) set(tbb_INCLUDE_DIR ${source_dir}/include) + if (CMAKE_BUILD_TYPE MATCHES Debug) - set(tbb_LIBRARY ${source_dir}/build/build_debug/${CMAKE_SHARED_LIBRARY_PREFIX}tbb_debug${CMAKE_SHARED_LIBRARY_SUFFIX}) + set(tbb_LIBRARY ${debug_lib_path}) else() - set(tbb_LIBRARY ${source_dir}/build/build_release/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX}) + set(tbb_LIBRARY ${release_lib_path}) endif () + file(MAKE_DIRECTORY ${tbb_INCLUDE_DIR}) - link_directories(${source_dir}/build/build_debug) - link_directories(${source_dir}/build/build_release) add_dependencies(tbb 01org_tbb) endif () diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index b8f69ea7a0..3b4a112a46 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -6,6 +6,12 @@ set_directory_properties(PROPERTIES EP_PREFIX ${EP_PREFIX} ) +if(CMAKE_GENERATOR MATCHES Xcode) + set(XCODE_EXT "/Debug") +else() + set(XCODE_EXT "") +endif() + # Project dependencies. find_package(Threads REQUIRED) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index 2b9368c6c8..559ea44797 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -130,3 +130,21 @@ macro(get_git_revision commit) WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} ) endmacro() + + +function(iroha_get_lib_name out lib type) + if(type STREQUAL "STATIC") + set(${out} ${CMAKE_STATIC_LIBRARY_PREFIX}${lib}${CMAKE_STATIC_LIBRARY_SUFFIX} PARENT_SCOPE) + elseif(type STREQUAL "SHARED") + set(${out} ${CMAKE_SHARED_LIBRARY_PREFIX}${lib}${CMAKE_SHARED_LIBRARY_SUFFIX} PARENT_SCOPE) + else() + message(FATAL_ERROR "type can be either STATIC or SHARED") + endif() +endfunction() + + +function(JOIN VALUES GLUE OUTPUT) + string (REGEX REPLACE "([^\\]|^);" "\\1${GLUE}" _TMP_STR "${VALUES}") + string (REGEX REPLACE "[\\](.)" "\\1" _TMP_STR "${_TMP_STR}") #fixes escaping + set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) +endfunction() From 6258863dd4d568b1da3904d703e2c80c7af02bf1 Mon Sep 17 00:00:00 2001 From: Alexey Chernyshov Date: Fri, 23 Feb 2018 17:46:57 +0300 Subject: [PATCH 19/23] rename orderingServiceState::reset() to resetState() Signed-off-by: Alexey Chernyshov --- .../postgres_ordering_service_persistent_state.cpp | 2 +- .../postgres_ordering_service_persistent_state.hpp | 2 +- irohad/ametsuchi/ordering_service_persistent_state.hpp | 2 +- irohad/main/application.cpp | 2 +- test/module/irohad/ametsuchi/ametsuchi_test.cpp | 10 +++++----- .../mock_ordering_service_persistent_state.hpp | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp index 52cae6818d..e36b75fd2c 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.cpp @@ -121,7 +121,7 @@ namespace iroha { return height; } - bool PostgresOrderingServicePersistentState::reset() { + bool PostgresOrderingServicePersistentState::resetState() { return dropStorgage() & initStorage(); } diff --git a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp index b116037a9b..a6e15f97b7 100644 --- a/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/impl/postgres_ordering_service_persistent_state.hpp @@ -78,7 +78,7 @@ namespace iroha { /** * Reset storage state to default */ - virtual bool reset(); + virtual bool resetState(); private: /** diff --git a/irohad/ametsuchi/ordering_service_persistent_state.hpp b/irohad/ametsuchi/ordering_service_persistent_state.hpp index 4b99c82447..4927b6edf2 100644 --- a/irohad/ametsuchi/ordering_service_persistent_state.hpp +++ b/irohad/ametsuchi/ordering_service_persistent_state.hpp @@ -42,7 +42,7 @@ namespace iroha { /** * Reset storage to default state */ - virtual bool reset() = 0; + virtual bool resetState() = 0; }; } // namespace ordering } // namespace iroha diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index 2f5c69749b..8742740f91 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -129,7 +129,7 @@ void Irohad::initStorage() { } void Irohad::resetOrderingService() { - if (not ordering_service_storage_->reset()) + if (not ordering_service_storage_->resetState()) log_->error("cannot reset ordering service storage"); } diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index fa6512554a..153a08e0cf 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -886,13 +886,13 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageTest) { }); ASSERT_TRUE(ordering_state); - ordering_state->reset(); + ordering_state->resetState(); ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); ASSERT_TRUE(ordering_state->saveProposalHeight(11)); ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); ASSERT_TRUE(ordering_state->saveProposalHeight(33)); ASSERT_EQ(33, ordering_state->loadProposalHeight().value()); - ordering_state->reset(); + ordering_state->resetState(); ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); } @@ -914,7 +914,7 @@ TEST_F(AmetsuchiTest, OrderingServicePersistentStorageRestartTest) { }); ASSERT_TRUE(ordering_state); - ordering_state->reset(); + ordering_state->resetState(); ASSERT_EQ(2, ordering_state->loadProposalHeight().value()); ASSERT_TRUE(ordering_state->saveProposalHeight(11)); ASSERT_EQ(11, ordering_state->loadProposalHeight().value()); @@ -964,14 +964,14 @@ TEST_F(AmetsuchiTest, }); ASSERT_TRUE(ordering_state_2); - ordering_state_2->reset(); + ordering_state_2->resetState(); ASSERT_EQ(2, ordering_state_1->loadProposalHeight().value()); ASSERT_EQ(2, ordering_state_2->loadProposalHeight().value()); ASSERT_TRUE(ordering_state_1->saveProposalHeight(11)); ASSERT_EQ(11, ordering_state_1->loadProposalHeight().value()); ASSERT_EQ(11, ordering_state_2->loadProposalHeight().value()); - ordering_state_2->reset(); + ordering_state_2->resetState(); ASSERT_EQ(2, ordering_state_1->loadProposalHeight().value()); ASSERT_EQ(2, ordering_state_2->loadProposalHeight().value()); ASSERT_TRUE(ordering_state_2->saveProposalHeight(42)); diff --git a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp index e205d00caa..4a4d665e07 100644 --- a/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp +++ b/test/module/irohad/ordering/mock_ordering_service_persistent_state.hpp @@ -37,7 +37,7 @@ class MockOrderingServicePersistentState /** * Reset state */ - MOCK_METHOD0(reset, bool()); + MOCK_METHOD0(resetState, bool()); }; #endif // IROHA_MOCK_ORDERING_SERVICE_PERSISTENT_STATE_HPP From ec69b8faebcfda857aaafde6e602a3c34e4ec28a Mon Sep 17 00:00:00 2001 From: Bogdan Vaneev Date: Fri, 23 Feb 2018 18:18:23 +0300 Subject: [PATCH 20/23] Remove grpc++_reflection Signed-off-by: Bogdan Vaneev --- cmake/Modules/Findgrpc.cmake | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/cmake/Modules/Findgrpc.cmake b/cmake/Modules/Findgrpc.cmake index a032d8afce..5126557246 100644 --- a/cmake/Modules/Findgrpc.cmake +++ b/cmake/Modules/Findgrpc.cmake @@ -1,6 +1,5 @@ add_library(grpc UNKNOWN IMPORTED) add_library(grpc++ UNKNOWN IMPORTED) -add_library(grpc++_reflection UNKNOWN IMPORTED) add_library(gpr UNKNOWN IMPORTED) add_executable(grpc_cpp_plugin IMPORTED) @@ -13,9 +12,6 @@ mark_as_advanced(grpc_LIBRARY) find_library(grpc_grpc++_LIBRARY grpc++) mark_as_advanced(grpc_grpc++_LIBRARY) -find_library(grpc_grpc++_reflection_LIBRARY grpc++_reflection) -mark_as_advanced(grpc_grpc++_reflection_LIBRARY) - find_library(gpr_LIBRARY gpr) mark_as_advanced(gpr_LIBRARY) @@ -26,7 +22,6 @@ find_package_handle_standard_args(grpc DEFAULT_MSG grpc_LIBRARY grpc_INCLUDE_DIR gpr_LIBRARY - grpc_grpc++_reflection_LIBRARY grpc_CPP_PLUGIN ) @@ -37,7 +32,6 @@ set_target_description(grpc "Remote Procedure Call library" ${URL} ${VERSION}) iroha_get_lib_name(GPRLIB gpr SHARED) iroha_get_lib_name(GRPCLIB grpc SHARED) iroha_get_lib_name(GRPCPPLIB grpc++ SHARED) -iroha_get_lib_name(GRPCPPREFLIB grpc++_reflection SHARED) if (NOT grpc_FOUND) find_package(Git REQUIRED) @@ -65,7 +59,6 @@ if (NOT grpc_FOUND) ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GPRLIB} ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCLIB} ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPLIB} -# ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPREFLIB} INSTALL_COMMAND "" # remove install step TEST_COMMAND "" # remove test step UPDATE_COMMAND "" # remove update step @@ -75,7 +68,6 @@ if (NOT grpc_FOUND) set(gpr_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GPRLIB}) set(grpc_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCLIB}) set(grpc_grpc++_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPLIB}) -# set(grpc_grpc++_reflection_LIBRARY ${CMAKE_BINARY_DIR}${XCODE_EXT}/${GRPCPPREFLIB}) set(grpc_CPP_PLUGIN ${CMAKE_BINARY_DIR}${XCODE_EXT}/grpc_cpp_plugin) file(MAKE_DIRECTORY ${grpc_INCLUDE_DIR}) @@ -83,7 +75,6 @@ if (NOT grpc_FOUND) add_dependencies(grpc_grpc protobuf) add_dependencies(grpc grpc_grpc) add_dependencies(grpc++ grpc_grpc) -# add_dependencies(grpc++_reflection grpc_grpc) add_dependencies(grpc_cpp_plugin grpc_grpc protoc) endif () @@ -99,12 +90,6 @@ set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${grpc_grpc++_LIBRARY} ) -#set_target_properties(grpc++_reflection PROPERTIES -# INTERFACE_INCLUDE_DIRECTORIES ${grpc_INCLUDE_DIR} -# INTERFACE_LINK_LIBRARIES grpc++ -# IMPORTED_LOCATION ${grpc_grpc++_reflection_LIBRARY} -# ) - set_target_properties(grpc_cpp_plugin PROPERTIES IMPORTED_LOCATION ${grpc_CPP_PLUGIN} ) @@ -117,5 +102,4 @@ if(ENABLE_LIBS_PACKAGING) add_install_step_for_lib(${gpr_LIBRARY}) add_install_step_for_lib(${grpc_LIBRARY}) add_install_step_for_lib(${grpc_grpc++_LIBRARY}) -# add_install_step_for_lib(${grpc_grpc++_reflection_LIBRARY}) endif() From b71173b4dec1dd939ca0c7015d6e1ae002c0b30e Mon Sep 17 00:00:00 2001 From: Bogdan Vaneev Date: Sat, 24 Feb 2018 14:28:13 +0300 Subject: [PATCH 21/23] Clean postgres in ametsuchi test in SetUp Signed-off-by: Bogdan Vaneev --- .../irohad/ametsuchi/ametsuchi_fixture.hpp | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp index 5bd9e7770a..90d6abdc17 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp @@ -56,7 +56,19 @@ namespace iroha { } protected: - virtual void SetUp() { + virtual void clear() { + pqxx::work txn(*connection); + txn.exec(drop_); + txn.commit(); + + iroha::remove_all(block_store_path); + } + + virtual void disconnect() { + connection->disconnect(); + } + + virtual void connect() { connection = std::make_shared(pgopt_); try { connection->activate(); @@ -64,8 +76,28 @@ namespace iroha { FAIL() << "Connection to PostgreSQL broken: " << e.what(); } } - virtual void TearDown() { - const auto drop = R"( + + void SetUp() override { + connect(); + clear(); + } + + void TearDown() override { + clear(); + disconnect(); + } + + std::shared_ptr connection; + + model::generators::CommandGenerator cmd_gen; + + std::string pgopt_ = + "host=localhost port=5432 user=postgres password=mysecretpassword"; + + std::string block_store_path = "/tmp/block_store"; + + // TODO(warchant): IR-1019 hide SQLs under some interface + const auto drop_ = R"( DROP TABLE IF EXISTS account_has_signatory; DROP TABLE IF EXISTS account_has_asset; DROP TABLE IF EXISTS role_has_permissions; @@ -83,23 +115,6 @@ DROP TABLE IF EXISTS index_by_creator_height; DROP TABLE IF EXISTS index_by_id_height_asset; )"; - pqxx::work txn(*connection); - txn.exec(drop); - txn.commit(); - connection->disconnect(); - - iroha::remove_all(block_store_path); - } - - std::shared_ptr connection; - - model::generators::CommandGenerator cmd_gen; - - std::string pgopt_ = - "host=localhost port=5432 user=postgres password=mysecretpassword"; - - std::string block_store_path = "/tmp/block_store"; - const std::string init_ = R"( CREATE TABLE IF NOT EXISTS role ( role_id character varying(45), From a243851b49904948d84b8dec1a664ad44ce27be8 Mon Sep 17 00:00:00 2001 From: Bogdan Vaneev Date: Sat, 24 Feb 2018 14:50:27 +0300 Subject: [PATCH 22/23] Change auto -> std::string for member Signed-off-by: Bogdan Vaneev --- test/module/irohad/ametsuchi/ametsuchi_fixture.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp index 90d6abdc17..ee8f1f4a03 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp @@ -97,7 +97,7 @@ namespace iroha { std::string block_store_path = "/tmp/block_store"; // TODO(warchant): IR-1019 hide SQLs under some interface - const auto drop_ = R"( + const std::string drop_ = R"( DROP TABLE IF EXISTS account_has_signatory; DROP TABLE IF EXISTS account_has_asset; DROP TABLE IF EXISTS role_has_permissions; From af953004baf1b214330483eccf5d3f132467d81e Mon Sep 17 00:00:00 2001 From: Dumitru Date: Sat, 24 Feb 2018 22:24:37 +0300 Subject: [PATCH 23/23] Solve conflicts Signed-off-by: Dumitru --- irohad/validation/impl/stateful_validator_impl.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index e441e560d2..31370dcc2a 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -22,8 +22,6 @@ #include "builders/protobuf/proposal.hpp" #include "model/account.hpp" #include "validation/impl/stateful_validator_impl.hpp" -#include "backend/protobuf/from_old_model.hpp" -#include "model/account.hpp" namespace iroha { namespace validation { @@ -39,7 +37,7 @@ namespace iroha { log_->info("transactions in proposal: {}", proposal.transactions().size()); auto checking_transaction = [this](const auto &tx, auto &queries) { - return (queries.getAccount(tx.creator_account_id) | + return (queries.getAccount(tx.creatorAccountId()) | [&](const auto &account) { // Check if tx creator has account and has quorum to execute // transaction @@ -57,7 +55,7 @@ namespace iroha { }); // Check if signatures in transaction are account signatory return this->signaturesSubset( - shared_model::proto::from_old(tx).signatures(), + tx.signatures(), std::vector( model_signatories.begin(), model_signatories.end())) @@ -71,7 +69,8 @@ namespace iroha { auto filter = [&temporaryWsv, checking_transaction](auto &acc, const auto &tx) { std::unique_ptr old_tx(tx->makeOldModel()); - auto answer = temporaryWsv.apply(*old_tx, checking_transaction); + auto answer = + temporaryWsv.apply(*(tx.operator->()), checking_transaction); if (answer) { acc.push_back(tx); } @@ -106,8 +105,7 @@ namespace iroha { } bool StatefulValidatorImpl::signaturesSubset( - const shared_model::interface::SignatureSetType - &signatures, + const shared_model::interface::SignatureSetType &signatures, const std::vector &public_keys) { // TODO 09/10/17 Lebedev: simplify the subset verification IR-510 // #goodfirstissue