Skip to content

Commit

Permalink
Shared model: simplify proto query backend
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Lebedev <lebdron@gmail.com>
  • Loading branch information
lebdron committed Oct 1, 2019
1 parent c79819d commit 7394c02
Show file tree
Hide file tree
Showing 46 changed files with 299 additions and 547 deletions.
1 change: 1 addition & 0 deletions irohad/torii/impl/query_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rxcpp/operators/rx-take_while.hpp>
#include "backend/protobuf/query_responses/proto_block_query_response.hpp"
#include "backend/protobuf/query_responses/proto_query_response.hpp"
#include "backend/protobuf/util.hpp"
#include "common/run_loop_handler.hpp"
#include "cryptography/default_hash_provider.hpp"
#include "interfaces/iroha_internal/abstract_transport_factory.hpp"
Expand Down
34 changes: 15 additions & 19 deletions shared_model/backend/protobuf/batch_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,51 @@
#ifndef IROHA_PROTO_BATCH_META_HPP
#define IROHA_PROTO_BATCH_META_HPP

#include "interfaces/iroha_internal/batch_meta.hpp"

#include <boost/range/numeric.hpp>
#include "backend/protobuf/common_objects/trivial_proto.hpp"
#include "cryptography/hash.hpp"
#include "interfaces/common_objects/types.hpp"
#include "interfaces/iroha_internal/batch_meta.hpp"
#include "transaction.pb.h"

namespace shared_model {
namespace proto {
class BatchMeta final
: public TrivialProto<
interface::BatchMeta,
iroha::protocol::Transaction::Payload::BatchMeta> {
class BatchMeta final : public interface::BatchMeta {
public:
template <typename BatchMetaType>
explicit BatchMeta(BatchMetaType &&batch_meta)
: TrivialProto(std::forward<BatchMetaType>(batch_meta)),
explicit BatchMeta(
iroha::protocol::Transaction::Payload::BatchMeta &batch_meta)
: batch_meta_{batch_meta},
type_{[this] {
unsigned which = proto_->GetDescriptor()
unsigned which = batch_meta_.GetDescriptor()
->FindFieldByName("type")
->enum_type()
->FindValueByNumber(proto_->type())
->FindValueByNumber(batch_meta_.type())
->index();
return static_cast<interface::types::BatchType>(which);
}()},
reduced_hashes_{boost::accumulate(
proto_->reduced_hashes(),
batch_meta_.reduced_hashes(),
ReducedHashesType{},
[](auto &&acc, const auto &hash) {
acc.emplace_back(crypto::Hash::fromHexString(hash));
return std::forward<decltype(acc)>(acc);
})} {}

BatchMeta(const BatchMeta &o) : BatchMeta(o.proto_) {}

BatchMeta(BatchMeta &&o) noexcept : BatchMeta(std::move(o.proto_)) {}

interface::types::BatchType type() const override {
return type_;
};
}

const ReducedHashesType &reducedHashes() const override {
return reduced_hashes_;
};
}

private:
const iroha::protocol::Transaction::Payload::BatchMeta &batch_meta_;

interface::types::BatchType type_;

const ReducedHashesType reduced_hashes_;
};
} // namespace proto
} // namespace shared_model
#endif // IROHA_PROTO_AMOUNT_HPP
#endif // IROHA_PROTO_BATCH_META_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "backend/protobuf/proto_query_response_factory.hpp"

#include "backend/protobuf/block.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"
Expand Down
1 change: 0 additions & 1 deletion shared_model/backend/protobuf/permissions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <string>
#include <vector>

#include <boost/optional.hpp>
#include "primitive.pb.h"

namespace shared_model {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@

#include "backend/protobuf/queries/proto_asset_pagination_meta.hpp"

#include <boost/optional.hpp>

namespace types = shared_model::interface::types;

using namespace shared_model::proto;

AssetPaginationMeta::AssetPaginationMeta(const TransportType &query)
: TrivialProto(query) {}

AssetPaginationMeta::AssetPaginationMeta(TransportType &&query)
: TrivialProto(std::move(query)) {}

AssetPaginationMeta::AssetPaginationMeta(const AssetPaginationMeta &o)
: AssetPaginationMeta(*o.proto_) {}

AssetPaginationMeta::AssetPaginationMeta(AssetPaginationMeta &&o) noexcept
: TrivialProto(std::move(*o.proto_)) {}
AssetPaginationMeta::AssetPaginationMeta(
iroha::protocol::AssetPaginationMeta &meta)
: meta_{meta} {}

types::TransactionsNumberType AssetPaginationMeta::pageSize() const {
return proto_->page_size();
return meta_.page_size();
}

boost::optional<types::AssetIdType> AssetPaginationMeta::firstAssetId() const {
if (proto_->opt_first_asset_id_case()
== TransportType::OptFirstAssetIdCase::OPT_FIRST_ASSET_ID_NOT_SET) {
if (meta_.opt_first_asset_id_case()
== iroha::protocol::AssetPaginationMeta::OptFirstAssetIdCase::
OPT_FIRST_ASSET_ID_NOT_SET) {
return boost::none;
}
return proto_->first_asset_id();
return meta_.first_asset_id();
}
42 changes: 20 additions & 22 deletions shared_model/backend/protobuf/queries/impl/proto_blocks_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,35 @@
*/

#include "backend/protobuf/queries/proto_blocks_query.hpp"

#include "backend/protobuf/util.hpp"

namespace shared_model {
namespace proto {

template <typename BlocksQueryType>
BlocksQuery::BlocksQuery(BlocksQueryType &&query)
: TrivialProto(std::forward<BlocksQueryType>(query)),
blob_{makeBlob(*proto_)},
payload_{makeBlob(proto_->meta())},
BlocksQuery::BlocksQuery(const TransportType &query)
: BlocksQuery(TransportType(query)) {}

BlocksQuery::BlocksQuery(TransportType &&query)
: proto_{std::move(query)},
blob_{makeBlob(proto_)},
payload_{makeBlob(proto_.meta())},
signatures_{[this] {
SignatureSetType<proto::Signature> set;
if (proto_->has_signature()) {
set.emplace(proto_->signature());
if (proto_.has_signature()) {
set.emplace(*proto_.mutable_signature());
}
return set;
}()},
hash_(makeHash(payload_)) {}

template BlocksQuery::BlocksQuery(BlocksQuery::TransportType &);
template BlocksQuery::BlocksQuery(const BlocksQuery::TransportType &);
template BlocksQuery::BlocksQuery(BlocksQuery::TransportType &&);

BlocksQuery::BlocksQuery(const BlocksQuery &o) : BlocksQuery(o.proto_) {}

BlocksQuery::BlocksQuery(BlocksQuery &&o) noexcept
: BlocksQuery(std::move(o.proto_)) {}

const interface::types::AccountIdType &BlocksQuery::creatorAccountId()
const {
return proto_->meta().creator_account_id();
return proto_.meta().creator_account_id();
}

interface::types::CounterType BlocksQuery::queryCounter() const {
return proto_->meta().query_counter();
return proto_.meta().query_counter();
}

const interface::types::BlobType &BlocksQuery::blob() const {
Expand All @@ -55,15 +49,15 @@ namespace shared_model {

bool BlocksQuery::addSignature(const crypto::Signed &signed_blob,
const crypto::PublicKey &public_key) {
if (proto_->has_signature()) {
if (proto_.has_signature()) {
return false;
}

auto sig = proto_->mutable_signature();
auto sig = proto_.mutable_signature();
sig->set_signature(signed_blob.hex());
sig->set_public_key(public_key.hex());
// TODO: nickaleks IR-120 12.12.2018 remove set
signatures_.emplace(proto_->signature());
signatures_.emplace(*proto_.mutable_signature());
return true;
}

Expand All @@ -72,7 +66,11 @@ namespace shared_model {
}

interface::types::TimestampType BlocksQuery::createdTime() const {
return proto_->meta().created_time();
return proto_.meta().created_time();
}

const BlocksQuery::TransportType &BlocksQuery::getTransport() const {
return proto_;
}

} // namespace proto
Expand Down
15 changes: 2 additions & 13 deletions shared_model/backend/protobuf/queries/impl/proto_get_account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@
namespace shared_model {
namespace proto {

template <typename QueryType>
GetAccount::GetAccount(QueryType &&query)
: TrivialProto(std::forward<QueryType>(query)),
account_{proto_->payload().get_account()} {}

template GetAccount::GetAccount(GetAccount::TransportType &);
template GetAccount::GetAccount(const GetAccount::TransportType &);
template GetAccount::GetAccount(GetAccount::TransportType &&);

GetAccount::GetAccount(const GetAccount &o) : GetAccount(o.proto_) {}

GetAccount::GetAccount(GetAccount &&o) noexcept
: GetAccount(std::move(o.proto_)) {}
GetAccount::GetAccount(iroha::protocol::Query &query)
: account_{query.payload().get_account()} {}

const interface::types::AccountIdType &GetAccount::accountId() const {
return account_.account_id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,13 @@
namespace shared_model {
namespace proto {

template <typename QueryType>
GetAccountAssetTransactions::GetAccountAssetTransactions(QueryType &&query)
: TrivialProto(std::forward<QueryType>(query)),
account_asset_transactions_{
proto_->payload().get_account_asset_transactions()},
pagination_meta_{account_asset_transactions_.pagination_meta()} {}

template GetAccountAssetTransactions::GetAccountAssetTransactions(
GetAccountAssetTransactions::TransportType &);
template GetAccountAssetTransactions::GetAccountAssetTransactions(
const GetAccountAssetTransactions::TransportType &);
template GetAccountAssetTransactions::GetAccountAssetTransactions(
GetAccountAssetTransactions::TransportType &&);

GetAccountAssetTransactions::GetAccountAssetTransactions(
const GetAccountAssetTransactions &o)
: GetAccountAssetTransactions(o.proto_) {}

GetAccountAssetTransactions::GetAccountAssetTransactions(
GetAccountAssetTransactions &&o) noexcept
: GetAccountAssetTransactions(std::move(o.proto_)) {}
iroha::protocol::Query &query)
: account_asset_transactions_{query.payload()
.get_account_asset_transactions()},
pagination_meta_{*query.mutable_payload()
->mutable_get_account_asset_transactions()
->mutable_pagination_meta()} {}

const interface::types::AccountIdType &
GetAccountAssetTransactions::accountId() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,17 @@
namespace shared_model {
namespace proto {

template <typename QueryType>
GetAccountAssets::GetAccountAssets(QueryType &&query)
: TrivialProto(std::forward<QueryType>(query)),
account_assets_{proto_->payload().get_account_assets()},
pagination_meta_{
[this]() -> boost::optional<const AssetPaginationMeta> {
if (this->account_assets_.has_pagination_meta()) {
return AssetPaginationMeta{
this->account_assets_.pagination_meta()};
} else {
return boost::none;
}
}()} {}

template GetAccountAssets::GetAccountAssets(
GetAccountAssets::TransportType &);
template GetAccountAssets::GetAccountAssets(
const GetAccountAssets::TransportType &);
template GetAccountAssets::GetAccountAssets(
GetAccountAssets::TransportType &&);

GetAccountAssets::GetAccountAssets(const GetAccountAssets &o)
: GetAccountAssets(o.proto_) {}

GetAccountAssets::GetAccountAssets(GetAccountAssets &&o) noexcept
: GetAccountAssets(std::move(o.proto_)) {}
GetAccountAssets::GetAccountAssets(iroha::protocol::Query &query)
: account_assets_{query.payload().get_account_assets()},
pagination_meta_{[&]() -> boost::optional<const AssetPaginationMeta> {
if (query.payload().get_account_assets().has_pagination_meta()) {
return AssetPaginationMeta{*query.mutable_payload()
->mutable_get_account_assets()
->mutable_pagination_meta()};
} else {
return boost::none;
}
}()} {}

const interface::types::AccountIdType &GetAccountAssets::accountId() const {
return account_assets_.account_id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,23 @@
namespace shared_model {
namespace proto {

template <typename QueryType>
GetAccountDetail::GetAccountDetail(QueryType &&query)
: TrivialProto(std::forward<QueryType>(query)),
account_detail_{proto_->payload().get_account_detail()},
pagination_meta_{[this]() -> decltype(pagination_meta_) {
if (this->account_detail_.has_pagination_meta()) {
GetAccountDetail::GetAccountDetail(iroha::protocol::Query &query)
: query_{query},
account_detail_{query.payload().get_account_detail()},
pagination_meta_{[&]() -> decltype(pagination_meta_) {
if (query.payload().get_account_detail().has_pagination_meta()) {
return AccountDetailPaginationMeta{
*this->proto_->mutable_payload()
*query.mutable_payload()
->mutable_get_account_detail()
->mutable_pagination_meta()};
}
return boost::none;
}()} {}

template GetAccountDetail::GetAccountDetail(
GetAccountDetail::TransportType &);
template GetAccountDetail::GetAccountDetail(
const GetAccountDetail::TransportType &);
template GetAccountDetail::GetAccountDetail(
GetAccountDetail::TransportType &&);

GetAccountDetail::GetAccountDetail(const GetAccountDetail &o)
: GetAccountDetail(o.proto_) {}

GetAccountDetail::GetAccountDetail(GetAccountDetail &&o) noexcept
: GetAccountDetail(std::move(o.proto_)) {}

const interface::types::AccountIdType &GetAccountDetail::accountId() const {
return account_detail_.opt_account_id_case()
? account_detail_.account_id()
: proto_->payload().meta().creator_account_id();
: query_.payload().meta().creator_account_id();
}

boost::optional<interface::types::AccountDetailKeyType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,12 @@
namespace shared_model {
namespace proto {

template <typename QueryType>
GetAccountTransactions::GetAccountTransactions(QueryType &&query)
: TrivialProto(std::forward<QueryType>(query)),
account_transactions_{proto_->payload().get_account_transactions()},
pagination_meta_{account_transactions_.pagination_meta()} {}

template GetAccountTransactions::GetAccountTransactions(
GetAccountTransactions::TransportType &);
template GetAccountTransactions::GetAccountTransactions(
const GetAccountTransactions::TransportType &);
template GetAccountTransactions::GetAccountTransactions(
GetAccountTransactions::TransportType &&);

GetAccountTransactions::GetAccountTransactions(
const GetAccountTransactions &o)
: GetAccountTransactions(o.proto_) {}

GetAccountTransactions::GetAccountTransactions(
GetAccountTransactions &&o) noexcept
: GetAccountTransactions(std::move(o.proto_)) {}
iroha::protocol::Query &query)
: account_transactions_{query.payload().get_account_transactions()},
pagination_meta_{*query.mutable_payload()
->mutable_get_account_transactions()
->mutable_pagination_meta()} {}

const interface::types::AccountIdType &GetAccountTransactions::accountId()
const {
Expand Down

0 comments on commit 7394c02

Please sign in to comment.