Skip to content

Commit

Permalink
Add query validator and executor
Browse files Browse the repository at this point in the history
- Add creator_account to query
- Remove client from query-response
  • Loading branch information
grimadas committed Jul 20, 2017
1 parent 28a9704 commit 027a331
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 9 deletions.
1 change: 1 addition & 0 deletions irohad/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(model
converters/impl/pb_block_factory.cpp
converters/impl/pb_transaction_factory.cpp
converters/impl/pb_command_factory.cpp
impl/query_execution.cpp
)

target_link_libraries(model
Expand Down
214 changes: 214 additions & 0 deletions irohad/model/impl/query_execution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/**
* 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 "model/query_execution.hpp"
#include <ametsuchi/block_query.hpp>
#include "common/types.hpp"
#include "model/queries/responses/account_assets_response.hpp"
#include "model/queries/responses/account_response.hpp"
#include "model/queries/responses/error_response.hpp"
#include "model/queries/responses/signatories_response.hpp"
#include "model/queries/responses/transactions_response.hpp"

iroha::model::QueryProcessingFactory::QueryProcessingFactory(
ametsuchi::WsvQuery& wsvQuery, ametsuchi::BlockQuery& blockQuery) {
_wsvQuery = wsvQuery;
_blockQuery = blockQuery;
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccount& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
creator.has_value() &&
// Creator has permission to read, or account = creator
(creator.value().permissions.read_all_accounts ||
query.account_id == query.creator_account_id);
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetSignatories& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
creator.has_value() &&
// Creator has permission to read, or account = creator
(creator.value().permissions.read_all_accounts ||
query.account_id == query.creator_account_id);
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccountAssets& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
creator.has_value() &&
// Creator has permission to read, or account = creator
(creator.value().permissions.read_all_accounts ||
query.account_id == query.creator_account_id);
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccountTransactions& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
creator.has_value() &&
// Creator has permission to read, or account = creator
(creator.value().permissions.read_all_accounts ||
query.account_id == query.creator_account_id);
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccountAssetTransactions& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
creator.has_value() &&
// Creator has permission to read, or account = creator
(creator.value().permissions.read_all_accounts ||
query.account_id == query.creator_account_id);
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetAccount(
const model::GetAccount& query) {
auto acc = _wsvQuery.getAccount(query.account_id);
if (!acc.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No account";
return response;
}
iroha::model::AccountResponse response;
response.account = acc.value();
response.query = query;
return response;
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetAccountAssets(
const model::GetAccountAssets& query) {
auto acc_assets = _wsvQuery.getAccountAsset(query.account_id, query.asset_id);
if (!acc_assets.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No Account Asset";
return response;
}
iroha::model::AccountAssetsResponse response;
response.= acc_assets.value();
response.query = query;
return response;
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetAccountAssetTransactions(
const model::GetAccountAssetTransactions& query) {
// auto acc_asset_tx = _blockQuery.
// TODO: implement
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "Not implemented";
return response;
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetAccountTransactions(
const model::GetAccountTransactions& query) {
auto acc_tx = _blockQuery.getAccountTransactions(query.account_id);
iroha::model::TransactionsResponse response;
response.query = query;
response.transactions = acc_tx;
return response;
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetSignatories(
const model::GetSignatories& query) {
auto signs = _wsvQuery.getSignatories(query.account_id);
if (!signs.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No signatories";
return response;
}
iroha::model::SignatoriesResponse response;
response.query = query;
response.keys = signs.value();
return response;
}

iroha::model::QueryResponse iroha::model::QueryProcessingFactory::execute(
const model::Query& query) {
if (instanceof <iroha::model::GetAccount>(query)) {
auto qry = static_cast<const iroha::model::GetAccount&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
}
return executeGetAccount(qry);
}
if (instanceof <iroha::model::GetAccountAssets>(query)) {
auto qry = static_cast<const iroha::model::GetAccountAssets&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
}
return executeGetAccountAssets(qry);
}
if (instanceof <iroha::model::GetSignatories>(query)) {
auto qry = static_cast<const iroha::model::GetSignatories&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
}
return executeGetSignatories(qry);
}
if (instanceof <iroha::model::GetAccountTransactions>(query)) {
auto qry = static_cast<const iroha::model::GetAccountTransactions&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
}
return executeGetAccountTransactions(qry);
}
if (instanceof <iroha::model::GetAccountAssetTransactions>(query)) {
auto qry =
static_cast<const iroha::model::GetAccountAssetTransactions&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
}
return executeGetAccountAssetTransactions(qry);
}
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "Not implemented";
return response;
}
1 change: 1 addition & 0 deletions irohad/model/queries/get_account_assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace iroha {
*/
struct GetAccountAssets : Query {
std::string account_id;
std::string asset_id;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 2 additions & 1 deletion irohad/model/queries/responses/account_assets_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#ifndef IROHA_ACCOUNT_ASSETS_RESPONSE_HPP
#define IROHA_ACCOUNT_ASSETS_RESPONSE_HPP

#include <model/query_response.hpp>
#include "model/query_response.hpp"
#include "model/account_asset.hpp"
#include <vector>

namespace iroha {
Expand Down
6 changes: 3 additions & 3 deletions irohad/model/queries/responses/account_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#ifndef IROHA_ACCOUNT_RESPONSE_HPP
#define IROHA_ACCOUNT_RESPONSE_HPP

#include <model/model.hpp>
#include "model/account.hpp"
#include "model/query_response.hpp"

namespace iroha {
namespace model {
Expand All @@ -27,12 +28,11 @@ namespace iroha {
* Provide response with account
*/
struct AccountResponse : public QueryResponse {

/**
* Attached account
*/
Account account;
};
} // namespace model
} // namespace iroha
#endif //IROHA_ACCOUNT_RESPONSE_HPP
#endif // IROHA_ACCOUNT_RESPONSE_HPP
2 changes: 1 addition & 1 deletion irohad/model/queries/responses/error_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef IROHA_ERROR_RESPONSE_HPP
#define IROHA_ERROR_RESPONSE_HPP

#include <model/query.hpp>
#include "model/query_response.hpp"
#include <string>

namespace iroha {
Expand Down
6 changes: 6 additions & 0 deletions irohad/model/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ namespace iroha {
*/
Signature signature;

/**
* Account id of transaction creator.
* META field
*/
std::string creator_account_id;

virtual ~Query() {}
};
} //namespace model
Expand Down
67 changes: 67 additions & 0 deletions irohad/model/query_execution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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_QUERY_EXECUTION_HPP
#define IROHA_QUERY_EXECUTION_HPP

#include <nonstd/optional.hpp>
#include "model/query.hpp"
#include "model/query_response.hpp"

#include "model/queries/get_account.hpp"
#include "model/queries/get_account_assets.hpp"
#include "model/queries/get_signatories.hpp"
#include "model/queries/get_transactions.hpp"

#include "ametsuchi/wsv_query.hpp"

namespace iroha {
namespace model {

/**
* Converting business objects to protobuf and vice versa
*/
class QueryProcessingFactory {
public:
model::QueryResponse execute(const model::Query& query);
QueryProcessingFactory(ametsuchi::WsvQuery& wsvQuery,
ametsuchi::BlockQuery& blockQuery);

private:
bool validate(const model::GetAccountAssets& query);
bool validate(const model::GetAccount& query);
bool validate(const model::GetSignatories& query);
bool validate(const model::GetAccountAssetTransactions& query);
bool validate(const model::GetAccountTransactions& query);

model::QueryResponse executeGetAccountAssets(
const model::GetAccountAssets& query);
model::QueryResponse executeGetAccount(const model::GetAccount& query);
model::QueryResponse executeGetSignatories(
const model::GetSignatories& query);
model::QueryResponse executeGetAccountAssetTransactions(
const model::GetAccountAssetTransactions& query);
model::QueryResponse executeGetAccountTransactions(
const model::GetAccountTransactions& query);

ametsuchi::WsvQuery _wsvQuery;
ametsuchi::BlockQuery _blockQuery;
};

} // namespace model
} // namespace iroha

#endif // IROHA_QUERY_EXECUTION_HPP
5 changes: 1 addition & 4 deletions irohad/model/query_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ namespace iroha {
*/
Query query;

/**
* Client identifier
*/
Client client;

};
} //namespace model
} //namespace iroha
Expand Down

0 comments on commit 027a331

Please sign in to comment.