Skip to content

Commit

Permalink
Implement shared model proto query response:
Browse files Browse the repository at this point in the history
- Implement proto container for Query responses
- Implement account asset response proto class

Signed-off-by: luckychess <[email protected]>
  • Loading branch information
luckychess authored and lebdron committed Dec 19, 2017
1 parent 9dddcbb commit a2b002d
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 8 deletions.
1 change: 1 addition & 0 deletions shared_model/backend/protobuf/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
*/

#include "backend/protobuf/commands/proto_command.hpp"
#include "backend/protobuf/query_responses/proto_query_response.hpp"
#include "backend/protobuf/transaction.hpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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_PROTO_ACCOUNT_ASSET_RESPONSE_HPP
#define IROHA_PROTO_ACCOUNT_ASSET_RESPONSE_HPP

#include "backend/protobuf/common_objects/proto_account_asset.hpp"
#include "interfaces/query_responses/account_asset_response.hpp"
#include "responses.pb.h"
#include "utils/lazy_initializer.hpp"
#include "utils/reference_holder.hpp"

namespace shared_model {
namespace proto {
class AccountAssetResponse final : public interface::AccountAssetResponse {
public:
template <typename QueryResponseType>
explicit AccountAssetResponse(QueryResponseType &&queryResponse)
: queryResponse_(std::forward<QueryResponseType>(queryResponse)),
accountAssetResponse_(
[this] { return queryResponse_->account_assets_response(); }),
accountAsset_([this] {
return AccountAsset(accountAssetResponse_->account_asset());
}) {}

AccountAssetResponse(const AccountAssetResponse &accountAssetResponse)
: AccountAssetResponse(*accountAssetResponse.queryResponse_) {}

AccountAssetResponse(AccountAssetResponse &&accountAssetResponse)
: AccountAssetResponse(
std::move(accountAssetResponse.queryResponse_.variant())) {}

const interface::AccountAsset &accountAsset() const override {
return *accountAsset_;
}

ModelType *copy() const override {
return new AccountAssetResponse(
iroha::protocol::QueryResponse(*queryResponse_));
}

private:
detail::ReferenceHolder<iroha::protocol::QueryResponse> queryResponse_;

template <typename T>
using Lazy = detail::LazyInitializer<T>;

const Lazy<const iroha::protocol::AccountAssetResponse &>
accountAssetResponse_;

const Lazy<AccountAsset> accountAsset_;
};
} // namespace proto
} // namespace shared_model

#endif // IROHA_PROTO_ACCOUNT_ASSET_RESPONSE_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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_PROTO_QUERY_RESPONSE_HPP
#define IROHA_PROTO_QUERY_RESPONSE_HPP

#include "backend/protobuf/query_responses/proto_account_asset_response.hpp"
#include "interfaces/queries/query.hpp"
#include "interfaces/query_responses/query_response.hpp"
#include "responses.pb.h"
#include "utils/lazy_initializer.hpp"
#include "utils/variant_deserializer.hpp"

template <typename... T, typename Archive>
auto loadQueryResponse(Archive &&ar) {
int which =
ar.GetDescriptor()->FindFieldByNumber(ar.response_case())->index();
return shared_model::detail::variant_impl<T...>::template load<
shared_model::interface::QueryResponse::QueryResponseVariantType>(
std::forward<Archive>(ar), which);
}

namespace shared_model {
namespace proto {
class QueryResponse final : public interface::QueryResponse {
private:
template <typename Value>
using w = detail::PolymorphicWrapper<Value>;

template <typename T>
using Lazy = detail::LazyInitializer<T>;

using LazyVariantType = Lazy<QueryResponseVariantType>;

public:
/// type of proto variant
using ProtoQueryResponseVariantType =
boost::variant<w<AccountAssetResponse>>;

/// list of types in variant
using ProtoQueryResponseListType = ProtoQueryResponseVariantType::types;

/// Type of query hash
using QueryHashType = interface::Query::HashType;

template <typename QueryResponseType>
explicit QueryResponse(QueryResponseType &&queryResponse)
: queryResponse_(std::forward<QueryResponseType>(queryResponse)),
variant_([this] {
return loadQueryResponse<ProtoQueryResponseListType>(
*queryResponse_);
}),
hash_([this] {
return QueryHashType(queryResponse_->query_hash());
}) {}

QueryResponse(const QueryResponse &o)
: QueryResponse(*o.queryResponse_) {}

QueryResponse(QueryResponse &&o) noexcept
: QueryResponse(std::move(o.queryResponse_.variant())) {}

const QueryResponseVariantType &get() const override { return *variant_; }

const QueryHashType &queryHash() const override { return *hash_; }

ModelType *copy() const override {
return new QueryResponse(
iroha::protocol::QueryResponse(*queryResponse_));
}

private:
detail::ReferenceHolder<iroha::protocol::QueryResponse> queryResponse_;

const LazyVariantType variant_;

const Lazy<QueryHashType> hash_;
};
} // namespace proto
} // namespace shared_model

#endif // IROHA_PROTO_QUERY_RESPONSE_HPP
16 changes: 8 additions & 8 deletions shared_model/interfaces/query_responses/query_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ namespace shared_model {
public:
/// Type of container with all concrete query response
using QueryResponseVariantType =
boost::variant<w<AccountAssetResponse>,
w<AccountResponse>,
w<AssetResponse>,
w<RolePermissionsResponse>,
w<RolesResponse>,
w<SignatoriesResponse>,
w<TransactionsResponse>,
w<ErrorQueryResponse>>;
boost::variant<w<AccountAssetResponse>>;
// w<AccountResponse>,
// w<AssetResponse>,
// w<RolePermissionsResponse>,
// w<RolesResponse>,
// w<SignatoriesResponse>,
// w<TransactionsResponse>,
// w<ErrorQueryResponse>>;

/// Type of all available query responses
using QueryResponseListType = QueryResponseVariantType::types;
Expand Down

0 comments on commit a2b002d

Please sign in to comment.