From 39f1a71359e5d17f48485444a2a856fd1090e872 Mon Sep 17 00:00:00 2001 From: kamilsa Date: Mon, 24 Jul 2017 13:40:42 +0300 Subject: [PATCH] Implementation of query processor, client removed from transaction and query responses --- irohad/main/application.hpp | 2 +- .../query_responses/stateless_response.hpp | 38 +++++++++++++ irohad/model/transaction_response.hpp | 5 -- .../model/tx_responses/stateless_response.hpp | 2 +- irohad/torii/processor/CMakeLists.txt | 3 +- .../processor/impl/query_processor_impl.cpp | 48 ++++++++++++++++ .../processor/impl/query_processor_stub.cpp | 55 ------------------- .../impl/transaction_processor_impl.cpp | 8 +-- irohad/torii/processor/query_processor.hpp | 3 +- ...ssor_stub.hpp => query_processor_impl.hpp} | 39 ++++++------- .../torii/processor/transaction_processor.hpp | 4 +- .../processor/transaction_processor_impl.hpp | 3 +- .../processor/transaction_processor_test.cpp | 8 +-- 13 files changed, 118 insertions(+), 100 deletions(-) create mode 100644 irohad/model/query_responses/stateless_response.hpp rename {libs => irohad}/model/tx_responses/stateless_response.hpp (94%) create mode 100644 irohad/torii/processor/impl/query_processor_impl.cpp delete mode 100644 irohad/torii/processor/impl/query_processor_stub.cpp rename irohad/torii/processor/{query_processor_stub.hpp => query_processor_impl.hpp} (60%) diff --git a/irohad/main/application.hpp b/irohad/main/application.hpp index 5430d53ea9..a065186bdb 100644 --- a/irohad/main/application.hpp +++ b/irohad/main/application.hpp @@ -20,7 +20,7 @@ #include
#include #include -#include +#include #include #include #include diff --git a/irohad/model/query_responses/stateless_response.hpp b/irohad/model/query_responses/stateless_response.hpp new file mode 100644 index 0000000000..4c3e7bf6a6 --- /dev/null +++ b/irohad/model/query_responses/stateless_response.hpp @@ -0,0 +1,38 @@ +/** + * Copyright Soramitsu Co., Ltd. 2017 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_STATELESS_RESPONSE_HPP +#define IROHA_STATELESS_RESPONSE_HPP + +namespace iroha { +namespace model { + +/** + * Query response that contains + */ +struct QueryStatelessResponse : QueryResponse { + + /** + * Is stateless validation passed + */ + bool passed; +}; +} // namespace model +} // namespace iroha + + +#endif //IROHA_STATELESS_RESPONSE_HPP diff --git a/irohad/model/transaction_response.hpp b/irohad/model/transaction_response.hpp index e06e40d989..33f1203ef0 100644 --- a/irohad/model/transaction_response.hpp +++ b/irohad/model/transaction_response.hpp @@ -34,11 +34,6 @@ namespace iroha { */ Transaction transaction; - /** - * Transaction emitter - */ - Client client; - virtual ~TransactionResponse() = default; }; } //namespace model diff --git a/libs/model/tx_responses/stateless_response.hpp b/irohad/model/tx_responses/stateless_response.hpp similarity index 94% rename from libs/model/tx_responses/stateless_response.hpp rename to irohad/model/tx_responses/stateless_response.hpp index e4c4f97ad1..ce2aa923db 100644 --- a/libs/model/tx_responses/stateless_response.hpp +++ b/irohad/model/tx_responses/stateless_response.hpp @@ -26,7 +26,7 @@ namespace iroha { /** * Transaction response that contains */ - struct StatelessResponse : TransactionResponse { + struct TransactionStatelessResponse : TransactionResponse { /** * Is stateless validation passed diff --git a/irohad/torii/processor/CMakeLists.txt b/irohad/torii/processor/CMakeLists.txt index df4f61f85a..9ca342e07c 100644 --- a/irohad/torii/processor/CMakeLists.txt +++ b/irohad/torii/processor/CMakeLists.txt @@ -1,9 +1,10 @@ add_library(processors impl/transaction_processor_impl.cpp - impl/query_processor_stub.cpp + impl/query_processor_impl.cpp ) target_link_libraries(processors PUBLIC model rxcpp + stateless_validator ) diff --git a/irohad/torii/processor/impl/query_processor_impl.cpp b/irohad/torii/processor/impl/query_processor_impl.cpp new file mode 100644 index 0000000000..6e46dd3aa7 --- /dev/null +++ b/irohad/torii/processor/impl/query_processor_impl.cpp @@ -0,0 +1,48 @@ +/** + * Copyright Soramitsu Co., Ltd. 2017 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 "torii/processor/query_processor_impl.hpp" +#include "model/query_responses/stateless_response.hpp" + +namespace iroha { + namespace torii { + + QueryProcessorImpl::QueryProcessorImpl( + model::QueryProcessingFactory &qpf, + validation::StatelessValidator &stateless_validator) + : qpf_(qpf), validator_(stateless_validator) {} + + void QueryProcessorImpl::query_handle(const model::Query &query) { + model::QueryStatelessResponse response; + response.query = query; + response.passed = false; + + if (validator_.validate(query)) { + response.passed = true; + qpf_.execute(query); + } + + subject_.get_subscriber().on_next( + std::make_shared(response)); + } + + rxcpp::observable> + QueryProcessorImpl::query_notifier() { + return subject_.get_observable(); + } + } +} \ No newline at end of file diff --git a/irohad/torii/processor/impl/query_processor_stub.cpp b/irohad/torii/processor/impl/query_processor_stub.cpp deleted file mode 100644 index 0e726ea6c3..0000000000 --- a/irohad/torii/processor/impl/query_processor_stub.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright Soramitsu Co., Ltd. 2017 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 - -namespace iroha { - namespace torii { - using rxcpp::subscriber; - using std::shared_ptr; - using model::Query; - using model::QueryResponse; - using model::Client; - - // QueryProcessingFactory - // stateless validation - QueryProcessorStub::QueryProcessorStub(ametsuchi::WsvQuery &wsv, - ametsuchi::BlockQuery &block) : - wsv_(wsv), block_(block) { - - } - - void QueryProcessorStub::query_handle(model::Client client, - const model::Query &query) { - // stateless validate - auto handle = handler_.find(query).value_or([](auto &) { - std::cout << "[Q] Handler not found" << std::endl; - return; - }); - handle(query); - return; - } - - rxcpp::observable> - QueryProcessorStub::query_notifier() { - return subject_.get_observable(); - } - - - - } // namespace torii -} // namespace iroha diff --git a/irohad/torii/processor/impl/transaction_processor_impl.cpp b/irohad/torii/processor/impl/transaction_processor_impl.cpp index 6615c7e4c6..ad7b58f5df 100644 --- a/irohad/torii/processor/impl/transaction_processor_impl.cpp +++ b/irohad/torii/processor/impl/transaction_processor_impl.cpp @@ -36,10 +36,8 @@ namespace iroha { validator_(validator) { } - void TransactionProcessorImpl::transaction_handle(model::Client client, - model::Transaction &transaction) { - model::StatelessResponse response; - response.client = client; + void TransactionProcessorImpl::transaction_handle(model::Transaction &transaction) { + model::TransactionStatelessResponse response; response.transaction = transaction; response.passed = false; @@ -49,7 +47,7 @@ namespace iroha { } notifier_.get_subscriber().on_next( - std::make_shared(response)); + std::make_shared(response)); } rxcpp::observable> diff --git a/irohad/torii/processor/query_processor.hpp b/irohad/torii/processor/query_processor.hpp index c7e91dc807..a01a3d6643 100644 --- a/irohad/torii/processor/query_processor.hpp +++ b/irohad/torii/processor/query_processor.hpp @@ -37,8 +37,7 @@ namespace iroha { * @param client - query emitter * @param query - client intent */ - virtual void query_handle(model::Client client, - const model::Query &query) = 0; + virtual void query_handle(const model::Query &query) = 0; /** * Subscribe for query responses diff --git a/irohad/torii/processor/query_processor_stub.hpp b/irohad/torii/processor/query_processor_impl.hpp similarity index 60% rename from irohad/torii/processor/query_processor_stub.hpp rename to irohad/torii/processor/query_processor_impl.hpp index ee5cc60191..9a84b58e9b 100644 --- a/irohad/torii/processor/query_processor_stub.hpp +++ b/irohad/torii/processor/query_processor_impl.hpp @@ -15,13 +15,12 @@ * limitations under the License. */ -#ifndef IROHA_STUB_QUERY_PROCESSOR_HPP -#define IROHA_STUB_QUERY_PROCESSOR_HPP +#ifndef IROHA_QUERY_PROCESSOR_IMPL_HPP +#define IROHA_QUERY_PROCESSOR_IMPL_HPP -#include -#include -#include -#include +#include "model/query_execution.hpp" +#include "validation/stateless_validator.hpp" +#include "torii/processor/query_processor.hpp" namespace iroha { namespace torii { @@ -29,33 +28,31 @@ namespace iroha { /** * QueryProcessor provides start point for queries in the whole system */ - class QueryProcessorStub : public QueryProcessor { + class QueryProcessorImpl : public QueryProcessor { public: - - explicit QueryProcessorStub(ametsuchi::WsvQuery &wsv, - ametsuchi::BlockQuery &block); + explicit QueryProcessorImpl( + model::QueryProcessingFactory &qpf, + validation::StatelessValidator &stateless_validator); /** * Register client query - * @param client - query emitter * @param query - client intent */ - void query_handle(model::Client client, const model::Query &query) override; + void query_handle(const model::Query &query) override; /** * Subscribe for query responses * @return observable with query responses */ - rxcpp::observable> query_notifier() override; + rxcpp::observable> query_notifier() + override; private: - HandlerMap handler_; rxcpp::subjects::subject> subject_; - ametsuchi::WsvQuery &wsv_; - ametsuchi::BlockQuery &block_; - - + model::QueryProcessingFactory &qpf_; + validation::StatelessValidator &validator_; }; - } //namespace torii -} //namespace iroha -#endif //IROHA_STUB_QUERY_PROCESSOR_HPP + } +} + +#endif // IROHA_QUERY_PROCESSOR_IMPL_HPP diff --git a/irohad/torii/processor/transaction_processor.hpp b/irohad/torii/processor/transaction_processor.hpp index 866b1e0cdd..8dd412a24d 100644 --- a/irohad/torii/processor/transaction_processor.hpp +++ b/irohad/torii/processor/transaction_processor.hpp @@ -35,11 +35,9 @@ namespace iroha { /** * Add transaction to the system for processing - * @param client - transaction owner * @param transaction - transaction for processing */ - virtual void transaction_handle(model::Client client, - model::Transaction &transaction) = 0; + virtual void transaction_handle(model::Transaction &transaction) = 0; /** * Subscribers will be notified with transaction status diff --git a/irohad/torii/processor/transaction_processor_impl.hpp b/irohad/torii/processor/transaction_processor_impl.hpp index bf363d2af8..bd44c2f187 100644 --- a/irohad/torii/processor/transaction_processor_impl.hpp +++ b/irohad/torii/processor/transaction_processor_impl.hpp @@ -40,8 +40,7 @@ namespace iroha { ordering::OrderingService &os, const validation::StatelessValidator &validator); - void transaction_handle(model::Client client, - model::Transaction &transaction) override; + void transaction_handle(model::Transaction &transaction) override; rxcpp::observable> transaction_notifier() override; diff --git a/test/module/irohad/torii/processor/transaction_processor_test.cpp b/test/module/irohad/torii/processor/transaction_processor_test.cpp index 55289148e4..799dc11359 100644 --- a/test/module/irohad/torii/processor/transaction_processor_test.cpp +++ b/test/module/irohad/torii/processor/transaction_processor_test.cpp @@ -72,10 +72,10 @@ TEST(TransactionProcessorTest, model::Transaction tx; // TODO subscribe with testable subscriber tp.transaction_notifier().subscribe([](auto response) { - auto resp = static_cast(*response); + auto resp = static_cast(*response); ASSERT_EQ(resp.passed, true); }); - tp.transaction_handle(model::Client(), tx); + tp.transaction_handle(tx); } /** @@ -96,8 +96,8 @@ TEST(TransactionProcessorTest, model::Transaction tx; // TODO subscribe with testable subscriber tp.transaction_notifier().subscribe([](auto response) { - auto resp = static_cast(*response); + auto resp = static_cast(*response); ASSERT_EQ(resp.passed, false); }); - tp.transaction_handle(model::Client(), tx); + tp.transaction_handle(tx); }