Skip to content

Commit

Permalink
Merge branch 'fix/proto_query_response' into feature/shared_model
Browse files Browse the repository at this point in the history
  • Loading branch information
luckychess authored and lebdron committed Dec 19, 2017
2 parents 67ef479 + 3df7bae commit 5b3ac8a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
44 changes: 23 additions & 21 deletions irohad/torii/impl/query_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"
#include "torii/query_service.hpp"
#include "common/types.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"

namespace torii {

Expand All @@ -40,29 +41,30 @@ namespace torii {
});
}

void QueryService::FindAsync(iroha::protocol::Query const& request,
iroha::protocol::QueryResponse& response) {
// Get iroha model query
auto query = pb_query_factory_->deserialize(request);

if (not query.has_value()) {
response.mutable_error_response()->set_reason(
iroha::protocol::ErrorResponse::NOT_SUPPORTED);
return;
}
void QueryService::FindAsync(iroha::protocol::Query const &request,
iroha::protocol::QueryResponse &response) {
using iroha::operator|;
auto deserializedRequest = pb_query_factory_->deserialize(request);
deserializedRequest | [&](const auto &query) {
auto hash = iroha::hash(*query).to_string();
if (handler_map_.count(hash) > 0) {
// Query was already processed
response.mutable_error_response()->set_reason(
iroha::protocol::ErrorResponse::STATELESS_INVALID);
}

auto hash = iroha::hash(**query).to_string();
else {
// Query - response relationship
handler_map_.emplace(hash, response);
// Send query to iroha
query_processor_->queryHandle(query);
}
response.set_query_hash(hash);
};

if (handler_map_.count(hash) > 0) {
// Query was already processed
if (not deserializedRequest) {
response.mutable_error_response()->set_reason(
iroha::protocol::ErrorResponse::STATELESS_INVALID);
return;
iroha::protocol::ErrorResponse::NOT_SUPPORTED);
}

// Query - response relationship
handler_map_.emplace(hash, response);
// Send query to iroha
query_processor_->queryHandle(query.value());
}
} // namespace torii
1 change: 1 addition & 0 deletions schema/responses.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ message QueryResponse {
RolesResponse roles_response = 7;
RolePermissionsResponse role_permissions_response = 8;
}
bytes query_hash = 9;
}
10 changes: 10 additions & 0 deletions test/module/irohad/torii/torii_queries_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ TEST_F(ToriiQueriesTest, FindWhenResponseInvalid) {
// Must return Error Response
ASSERT_EQ(response.error_response().reason(),
iroha::model::ErrorResponse::STATELESS_INVALID);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

/**
Expand Down Expand Up @@ -180,6 +181,7 @@ TEST_F(ToriiQueriesTest, FindAccountWhenNoGrantPermissions) {
// to read account
ASSERT_EQ(response.error_response().reason(),
iroha::model::ErrorResponse::STATEFUL_INVALID);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

TEST_F(ToriiQueriesTest, FindAccountWhenHasReadPermissions) {
Expand Down Expand Up @@ -220,6 +222,7 @@ TEST_F(ToriiQueriesTest, FindAccountWhenHasReadPermissions) {
ASSERT_FALSE(response.has_error_response());
ASSERT_EQ(response.account_response().account().account_id(), "accountB");
ASSERT_EQ(response.account_response().account_roles().size(), 1);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

TEST_F(ToriiQueriesTest, FindAccountWhenHasRolePermission) {
Expand Down Expand Up @@ -254,6 +257,7 @@ TEST_F(ToriiQueriesTest, FindAccountWhenHasRolePermission) {
// Should not return Error Response because tx is stateless and stateful valid
ASSERT_FALSE(response.has_error_response());
ASSERT_EQ(response.account_response().account().account_id(), "accountA");
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

/**
Expand Down Expand Up @@ -309,6 +313,7 @@ TEST_F(ToriiQueriesTest, FindAccountAssetWhenNoGrantPermissions) {
// to read account asset
ASSERT_EQ(response.error_response().reason(),
iroha::model::ErrorResponse::STATEFUL_INVALID);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

TEST_F(ToriiQueriesTest, FindAccountAssetWhenHasRolePermissions) {
Expand Down Expand Up @@ -362,6 +367,7 @@ TEST_F(ToriiQueriesTest, FindAccountAssetWhenHasRolePermissions) {
auto iroha_amount_asset = iroha::model::converters::deserializeAmount(
response.account_assets_response().account_asset().balance());
ASSERT_EQ(iroha_amount_asset, account_asset.balance);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

/**
Expand Down Expand Up @@ -406,6 +412,7 @@ TEST_F(ToriiQueriesTest, FindSignatoriesWhenNoGrantPermissions) {
// to read account
ASSERT_EQ(response.error_response().reason(),
iroha::model::ErrorResponse::STATEFUL_INVALID);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

TEST_F(ToriiQueriesTest, FindSignatoriesHasRolePermissions) {
Expand Down Expand Up @@ -449,6 +456,7 @@ TEST_F(ToriiQueriesTest, FindSignatoriesHasRolePermissions) {
decltype(pubkey) response_pubkey;
std::copy(signatory.begin(), signatory.end(), response_pubkey.begin());
ASSERT_EQ(response_pubkey, pubkey);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

/**
Expand Down Expand Up @@ -508,6 +516,7 @@ TEST_F(ToriiQueriesTest, FindTransactionsWhenValid) {
response.transactions_response().transactions(i).payload().tx_counter(),
i);
}
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}

TEST_F(ToriiQueriesTest, FindManyTimesWhereQueryServiceSync) {
Expand All @@ -530,5 +539,6 @@ TEST_F(ToriiQueriesTest, FindManyTimesWhereQueryServiceSync) {
// Must return Error Response
ASSERT_EQ(response.error_response().reason(),
iroha::model::ErrorResponse::STATELESS_INVALID);
ASSERT_EQ(iroha::hash(query).to_string(), response.query_hash());
}
}

0 comments on commit 5b3ac8a

Please sign in to comment.