-
Notifications
You must be signed in to change notification settings - Fork 297
Query Responses Factory #1724
Query Responses Factory #1724
Changes from 12 commits
7978025
8a31c52
4bf2ba9
6960c7d
7e76a93
f1a1d3f
7c9bc9d
bb78344
ce1f5ca
a713d9e
791a82b
6bf4193
16bdc4d
58708e2
685f127
2b23679
722d149
935ce77
bc1b8ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "backend/protobuf/proto_query_response_factory.hpp" | ||
#include "backend/protobuf/permissions.hpp" | ||
#include "backend/protobuf/query_responses/proto_block_query_response.hpp" | ||
#include "backend/protobuf/query_responses/proto_query_response.hpp" | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createAccountAssetResponse( | ||
std::vector<std::shared_ptr<shared_model::interface::AccountAsset>> assets, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a common method for this, so these lines are not duplicated in all the methods. iroha::protocol::QueryResponse protocol_query_response;
protocol_query_response.set_query_hash(query_hash.hex());
...
return std::make_unique<shared_model::proto::QueryResponse>(
std::move(protocol_query_response)); Maybe create a method, so that it is possible to pass something in place of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imo it's better leave as is, otherwise code will be harder to read and modify |
||
|
||
iroha::protocol::AccountAssetResponse *protocol_specific_response = | ||
protocol_query_response.mutable_account_assets_response(); | ||
for (const auto &asset : assets) { | ||
protocol_specific_response->add_account_assets()->CopyFrom( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to |
||
std::static_pointer_cast<shared_model::proto::AccountAsset>(asset) | ||
->getTransport()); | ||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createAccountDetailResponse( | ||
shared_model::interface::types::DetailType account_detail, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::AccountDetailResponse *protocol_specific_response = | ||
protocol_query_response.mutable_account_detail_response(); | ||
protocol_specific_response->set_detail(account_detail); | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createAccountResponse( | ||
std::unique_ptr<shared_model::interface::Account> account, | ||
std::vector<std::string> roles, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::AccountResponse *protocol_specific_response = | ||
protocol_query_response.mutable_account_response(); | ||
*protocol_specific_response->mutable_account() = | ||
static_cast<shared_model::proto::Account *>(account.release()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a memleak |
||
->getTransport(); | ||
for (const auto &role : roles) { | ||
protocol_specific_response->add_account_roles(role); | ||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createErrorQueryResponse( | ||
ErrorQueryType error_type, | ||
std::string error_msg, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::ErrorResponse_Reason reason; | ||
switch (error_type) { | ||
case ErrorQueryType::kStatelessFailed: | ||
reason = iroha::protocol::ErrorResponse_Reason_STATELESS_INVALID; | ||
break; | ||
case ErrorQueryType::kStatefulFailed: | ||
reason = iroha::protocol::ErrorResponse_Reason_STATEFUL_INVALID; | ||
break; | ||
case ErrorQueryType::kNoAccount: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT; | ||
break; | ||
case ErrorQueryType::kNoAccountAssets: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT_ASSETS; | ||
break; | ||
case ErrorQueryType::kNoAccountDetail: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_ACCOUNT_DETAIL; | ||
break; | ||
case ErrorQueryType::kNoSignatories: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_SIGNATORIES; | ||
break; | ||
case ErrorQueryType::kNotSupported: | ||
reason = iroha::protocol::ErrorResponse_Reason_NOT_SUPPORTED; | ||
break; | ||
case ErrorQueryType::kNoAsset: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_ASSET; | ||
break; | ||
case ErrorQueryType::kNoRoles: | ||
reason = iroha::protocol::ErrorResponse_Reason_NO_ROLES; | ||
break; | ||
} | ||
iroha::protocol::ErrorResponse *protocol_specific_response = | ||
protocol_query_response.mutable_error_response(); | ||
protocol_specific_response->set_reason(reason); | ||
protocol_specific_response->set_message(std::move(error_msg)); | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about setting the message? |
||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createSignatoriesResponse( | ||
std::vector<shared_model::interface::types::PubkeyType> signatories, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::SignatoriesResponse *protocol_specific_response = | ||
protocol_query_response.mutable_signatories_response(); | ||
for (const auto &key : signatories) { | ||
const auto &blob = key.blob(); | ||
protocol_specific_response->add_keys(blob.data(), blob.size()); | ||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createTransactionsResponse( | ||
std::vector<std::shared_ptr<shared_model::interface::Transaction>> | ||
transactions, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::TransactionsResponse *protocol_specific_response = | ||
protocol_query_response.mutable_transactions_response(); | ||
for (const auto &tx : transactions) { | ||
protocol_specific_response->add_transactions()->CopyFrom( | ||
std::static_pointer_cast<shared_model::proto::Transaction>(tx) | ||
->getTransport()); | ||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createAssetResponse( | ||
std::unique_ptr<shared_model::interface::Asset> asset, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::AssetResponse *protocol_specific_response = | ||
protocol_query_response.mutable_asset_response(); | ||
*protocol_specific_response->mutable_asset() = | ||
static_cast<shared_model::proto::Asset *>(asset.release()) | ||
->getTransport(); | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createRolesResponse( | ||
std::vector<shared_model::interface::types::RoleIdType> roles, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::RolesResponse *protocol_specific_response = | ||
protocol_query_response.mutable_roles_response(); | ||
for (const auto &role : roles) { | ||
protocol_specific_response->add_roles(role); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it also possible to use |
||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::QueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createRolePermissionsResponse( | ||
shared_model::interface::RolePermissionSet role_permissions, | ||
const crypto::Hash &query_hash) { | ||
iroha::protocol::QueryResponse protocol_query_response; | ||
protocol_query_response.set_query_hash(query_hash.hex()); | ||
|
||
iroha::protocol::RolePermissionsResponse *protocol_specific_response = | ||
protocol_query_response.mutable_role_permissions_response(); | ||
for (size_t i = 0; i < role_permissions.size(); ++i) { | ||
auto perm = static_cast<interface::permissions::Role>(i); | ||
if (role_permissions.test(perm)) { | ||
protocol_specific_response->add_permissions( | ||
shared_model::proto::permissions::toTransport(perm)); | ||
} | ||
} | ||
|
||
return std::make_unique<shared_model::proto::QueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::BlockQueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createBlockQueryResponse( | ||
std::unique_ptr<shared_model::interface::Block> block) { | ||
iroha::protocol::BlockQueryResponse protocol_query_response; | ||
|
||
iroha::protocol::BlockResponse *protocol_specific_response = | ||
protocol_query_response.mutable_block_response(); | ||
*protocol_specific_response->mutable_block() = | ||
static_cast<shared_model::proto::Block *>(block.release()) | ||
->getTransport(); | ||
|
||
return std::make_unique<shared_model::proto::BlockQueryResponse>( | ||
std::move(protocol_query_response)); | ||
} | ||
|
||
std::unique_ptr<shared_model::interface::BlockQueryResponse> | ||
shared_model::proto::ProtoQueryResponseFactory::createBlockQueryResponse( | ||
std::string error_message) { | ||
iroha::protocol::BlockQueryResponse protocol_query_response; | ||
|
||
iroha::protocol::BlockErrorResponse *protocol_specific_response = | ||
protocol_query_response.mutable_block_error_response(); | ||
protocol_specific_response->set_message(error_message); | ||
|
||
return std::make_unique<shared_model::proto::BlockQueryResponse>( | ||
std::move(protocol_query_response)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_PROTO_QUERY_RESPONSE_FACTORY_HPP | ||
#define IROHA_PROTO_QUERY_RESPONSE_FACTORY_HPP | ||
|
||
#include "interfaces/iroha_internal/query_response_factory.hpp" | ||
|
||
namespace shared_model { | ||
namespace proto { | ||
|
||
class ProtoQueryResponseFactory : public interface::QueryResponseFactory { | ||
public: | ||
std::unique_ptr<interface::QueryResponse> createAccountAssetResponse( | ||
std::vector<std::shared_ptr<shared_model::interface::AccountAsset>> | ||
assets, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createAccountDetailResponse( | ||
interface::types::DetailType account_detail, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createAccountResponse( | ||
std::unique_ptr<interface::Account> account, | ||
std::vector<std::string> roles, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createErrorQueryResponse( | ||
ErrorQueryType error_type, | ||
std::string error_msg, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createSignatoriesResponse( | ||
std::vector<interface::types::PubkeyType> signatories, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createTransactionsResponse( | ||
std::vector<std::shared_ptr<shared_model::interface::Transaction>> | ||
transactions, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createAssetResponse( | ||
std::unique_ptr<shared_model::interface::Asset> asset, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createRolesResponse( | ||
std::vector<interface::types::RoleIdType> roles, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::QueryResponse> createRolePermissionsResponse( | ||
interface::RolePermissionSet role_permissions, | ||
const crypto::Hash &query_hash) override; | ||
|
||
std::unique_ptr<interface::BlockQueryResponse> createBlockQueryResponse( | ||
std::unique_ptr<interface::Block> block) override; | ||
|
||
std::unique_ptr<interface::BlockQueryResponse> createBlockQueryResponse( | ||
std::string error_message) override; | ||
}; | ||
|
||
} // namespace proto | ||
} // namespace shared_model | ||
|
||
#endif // IROHA_PROTO_QUERY_RESPONSE_FACTORY_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
vector<unique_ptr>
, so that it is possible to move the assets to query response? Same increateTransactionsResponse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unique_ptr
seems not that handy to use, since most of the methods in iroha works withshared_ptr