Skip to content

Commit

Permalink
Fix query pipeline:
Browse files Browse the repository at this point in the history
- Fix main flags
- Add created ts in json factory
  • Loading branch information
grimadas committed Aug 6, 2017
1 parent 43c9746 commit d0a5239
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
39 changes: 22 additions & 17 deletions iroha-cli/impl/query_response_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@ using namespace iroha::protocol;
namespace iroha_cli {

QueryResponseHandler::QueryResponseHandler()
: log_(logger::log("QueryResponseHandler")) {
handler_map_[typeid(ErrorResponse)] =
&QueryResponseHandler::handleErrorResponse;
handler_map_[typeid(AccountResponse)] =
&QueryResponseHandler::handleAccountResponse;
handler_map_[typeid(AccountAssetResponse)] =
&QueryResponseHandler::handleAccountAssetsResponse;
handler_map_[typeid(TransactionsResponse)] =
&QueryResponseHandler::handleTransactionsResponse;
handler_map_[typeid(SignatoriesResponse)] =
&QueryResponseHandler::handleSignatoriesResponse;
}
: log_(logger::log("QueryResponseHandler")) {}

void QueryResponseHandler::handle(
const iroha::protocol::QueryResponse &response) {
auto it = handler_map_.find(typeid(response));
if (it != handler_map_.end()) {
(this->*it->second)(response);
} else {
log_->error("Response Handle not Implemented");
if (response.has_error_response()) {
handleErrorResponse(response);
return;
}
if (response.has_account_assets_response()) {
handleAccountAssetsResponse(response);
return;
}
if (response.has_account_response()) {
handleAccountResponse(response);
return;
}
if (response.has_signatories_response()) {
handleSignatoriesResponse(response);
return;
}
if (response.has_transactions_response()) {
handleTransactionsResponse(response);
return;
}
// Response of some other type received
log_->error("Response Handle not Implemented");
}

void QueryResponseHandler::handleErrorResponse(
Expand Down
2 changes: 1 addition & 1 deletion iroha-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int main(int argc, char* argv[]) {
if (not FLAGS_json_query.empty()) {
logger->info("Send query to {}:{}", FLAGS_address, FLAGS_torii_port);
iroha_cli::QueryResponseHandler responseHandler;
std::ifstream file(FLAGS_json_transaction);
std::ifstream file(FLAGS_json_query);
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto response = client.sendQuery(str);
Expand Down
1 change: 1 addition & 0 deletions irohad/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ target_link_libraries(model_converters
rapidjson
optional
schema
logger
)
15 changes: 13 additions & 2 deletions irohad/model/converters/impl/json_query_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace iroha {

using namespace rapidjson;

JsonQueryFactory::JsonQueryFactory() {
JsonQueryFactory::JsonQueryFactory() :log_(logger::log("JsonQueryFactory")) {
deserializers_["GetAccount"] = &JsonQueryFactory::deserializeGetAccount;
deserializers_["GetAccountAssets"] =
&JsonQueryFactory::deserializeGetAccountAssets;
Expand All @@ -38,12 +38,13 @@ namespace iroha {

nonstd::optional<iroha::protocol::Query> JsonQueryFactory::deserialize(
const std::string query_json) {
log_->info("Deserialize query json");
iroha::protocol::Query pb_query;
Document doc;
if (doc.Parse(query_json.c_str()).HasParseError()) {
log_->error("Json is ill-formed");
return nonstd::nullopt;
}

// check if all necessary fields are there
auto obj_query = doc.GetObject();
auto req_fields = {"signature", "creator_account_id", "created_ts",
Expand All @@ -52,20 +53,24 @@ namespace iroha {
[&obj_query](auto &&field) {
return not obj_query.HasMember(field);
})) {
log_->error("No required fields in json");
return nonstd::nullopt;
}

auto sig = obj_query["signature"].GetObject();

// check if signature has all needed fields
if (not sig.HasMember("pubkey")) {
log_->error("No pubkey in signature in json");
return nonstd::nullopt;
}
if (not sig.HasMember("signature")) {
log_->error("No signature in json");
return nonstd::nullopt;
}

auto pb_header = pb_query.mutable_header();
pb_header->set_created_time(obj_query["created_ts"].GetUint64());
auto pb_sig = pb_header->mutable_signature();
pb_sig->set_pubkey(sig["pubkey"].GetString());
pb_sig->set_signature(sig["signature"].GetString());
Expand All @@ -77,6 +82,7 @@ namespace iroha {
// set query counter
pb_query.set_query_counter(obj_query["query_counter"].GetUint64());


auto query_type = obj_query["query_type"].GetString();

auto it = deserializers_.find(query_type);
Expand All @@ -92,6 +98,7 @@ namespace iroha {
rapidjson::GenericValue<rapidjson::UTF8<char>>::Object &obj_query,
protocol::Query &pb_query) {
if (not obj_query.HasMember("account_id")) {
log_->error("No account id in json");
return false;
}
auto pb_get_account = pb_query.mutable_get_account();
Expand All @@ -104,6 +111,7 @@ namespace iroha {
rapidjson::GenericValue<rapidjson::UTF8<char>>::Object &obj_query,
protocol::Query &pb_query) {
if (not obj_query.HasMember("account_id")) {
log_->error("No account id in json");
return false;
}
auto pb_get_signatories = pb_query.mutable_get_account_signatories();
Expand All @@ -116,6 +124,7 @@ namespace iroha {
rapidjson::GenericValue<rapidjson::UTF8<char>>::Object &obj_query,
protocol::Query &pb_query) {
if (not obj_query.HasMember("account_id")) {
log_->error("No account id in json");
return false;
}
auto pb_get_account_transactions =
Expand All @@ -131,6 +140,7 @@ namespace iroha {
protocol::Query &pb_query) {
if (not(obj_query.HasMember("account_id") &&
obj_query.HasMember("asset_id"))) {
log_->error("No account, asset id in json");
return false;
}

Expand All @@ -149,6 +159,7 @@ namespace iroha {
protocol::Query &pb_query) {
if (not(obj_query.HasMember("account_id") &&
obj_query.HasMember("asset_id"))) {
log_->error("No account, asset id in json");
return false;
}
auto pb_get_account_assets = pb_query.mutable_get_account_assets();
Expand Down
5 changes: 5 additions & 0 deletions irohad/model/converters/json_query_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "model/query.hpp"
#include "queries.pb.h"

#include "logger/logger.hpp"

namespace iroha {
namespace model {
namespace converters {
Expand Down Expand Up @@ -70,6 +72,9 @@ namespace iroha {
bool deserializeGetAccountAssets(
GenericValue<UTF8<char>>::Object &obj_query,
iroha::protocol::Query &pb_query);
// Logger
std::shared_ptr<spdlog::logger> log_;

};
} // namespace converters
} // namespace model
Expand Down

0 comments on commit d0a5239

Please sign in to comment.