Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "backend/protobuf/queries/proto_query.hpp"
7 :
8 : #include "backend/protobuf/common_objects/signature.hpp"
9 : #include "backend/protobuf/queries/proto_get_account.hpp"
10 : #include "backend/protobuf/queries/proto_get_account_asset_transactions.hpp"
11 : #include "backend/protobuf/queries/proto_get_account_assets.hpp"
12 : #include "backend/protobuf/queries/proto_get_account_detail.hpp"
13 : #include "backend/protobuf/queries/proto_get_account_transactions.hpp"
14 : #include "backend/protobuf/queries/proto_get_asset_info.hpp"
15 : #include "backend/protobuf/queries/proto_get_block.hpp"
16 : #include "backend/protobuf/queries/proto_get_pending_transactions.hpp"
17 : #include "backend/protobuf/queries/proto_get_role_permissions.hpp"
18 : #include "backend/protobuf/queries/proto_get_roles.hpp"
19 : #include "backend/protobuf/queries/proto_get_signatories.hpp"
20 : #include "backend/protobuf/queries/proto_get_transactions.hpp"
21 : #include "backend/protobuf/util.hpp"
22 : #include "utils/variant_deserializer.hpp"
23 :
24 : namespace {
25 : /// type of proto variant
26 : using ProtoQueryVariantType =
27 : boost::variant<shared_model::proto::GetAccount,
28 : shared_model::proto::GetSignatories,
29 : shared_model::proto::GetAccountTransactions,
30 : shared_model::proto::GetAccountAssetTransactions,
31 : shared_model::proto::GetTransactions,
32 : shared_model::proto::GetAccountAssets,
33 : shared_model::proto::GetAccountDetail,
34 : shared_model::proto::GetRoles,
35 : shared_model::proto::GetRolePermissions,
36 : shared_model::proto::GetAssetInfo,
37 : shared_model::proto::GetPendingTransactions,
38 : shared_model::proto::GetBlock>;
39 :
40 : /// list of types in proto variant
41 : using ProtoQueryListType = ProtoQueryVariantType::types;
42 : } // namespace
43 :
44 : namespace shared_model {
45 : namespace proto {
46 :
47 : struct Query::Impl {
48 : explicit Impl(TransportType &&ref) : proto_{std::move(ref)} {}
49 : explicit Impl(const TransportType &ref) : proto_{ref} {}
50 :
51 : TransportType proto_;
52 :
53 : ProtoQueryVariantType variant_{[this] {
54 452 : auto &ar = proto_;
55 452 : int which = ar.payload()
56 452 : .GetDescriptor()
57 452 : ->FindFieldByNumber(ar.payload().query_case())
58 452 : ->index_in_oneof();
59 452 : return shared_model::detail::variant_impl<
60 452 : ProtoQueryListType>::template load<ProtoQueryVariantType>(ar,
61 452 : which);
62 : }()};
63 :
64 432 : QueryVariantType ivariant_{variant_};
65 :
66 416 : interface::types::BlobType blob_{makeBlob(proto_)};
67 :
68 434 : interface::types::BlobType payload_{makeBlob(proto_.payload())};
69 :
70 : SignatureSetType<proto::Signature> signatures_{[this] {
71 451 : SignatureSetType<proto::Signature> set;
72 451 : if (proto_.has_signature()) {
73 167 : set.emplace(proto_.signature());
74 167 : }
75 451 : return set;
76 451 : }()};
77 : };
78 :
79 : Query::Query(const Query &o) : Query(o.impl_->proto_) {}
80 : Query::Query(Query &&o) noexcept = default;
81 :
82 : Query::Query(const TransportType &ref) {
83 19 : impl_ = std::make_unique<Impl>(ref);
84 19 : }
85 : Query::Query(TransportType &&ref) {
86 432 : impl_ = std::make_unique<Impl>(std::move(ref));
87 433 : }
88 :
89 : Query::~Query() = default;
90 :
91 : const Query::QueryVariantType &Query::get() const {
92 411 : return impl_->ivariant_;
93 : }
94 :
95 : const interface::types::AccountIdType &Query::creatorAccountId() const {
96 532 : return impl_->proto_.payload().meta().creator_account_id();
97 : }
98 :
99 : interface::types::CounterType Query::queryCounter() const {
100 211 : return impl_->proto_.payload().meta().query_counter();
101 : }
102 :
103 : const interface::types::BlobType &Query::blob() const {
104 0 : return impl_->blob_;
105 : }
106 :
107 : const interface::types::BlobType &Query::payload() const {
108 569 : return impl_->payload_;
109 : }
110 :
111 : interface::types::SignatureRangeType Query::signatures() const {
112 481 : return impl_->signatures_;
113 : }
114 :
115 : bool Query::addSignature(const crypto::Signed &signed_blob,
116 : const crypto::PublicKey &public_key) {
117 178 : if (impl_->proto_.has_signature()) {
118 0 : return false;
119 : }
120 :
121 172 : auto sig = impl_->proto_.mutable_signature();
122 172 : sig->set_signature(signed_blob.hex());
123 172 : sig->set_public_key(public_key.hex());
124 :
125 178 : impl_->signatures_ =
126 178 : SignatureSetType<proto::Signature>{proto::Signature{*sig}};
127 178 : return true;
128 177 : }
129 :
130 : interface::types::TimestampType Query::createdTime() const {
131 211 : return impl_->proto_.payload().meta().created_time();
132 : }
133 :
134 : const Query::TransportType &Query::getTransport() const {
135 177 : return impl_->proto_;
136 : }
137 :
138 : Query *Query::clone() const {
139 0 : return new Query(impl_->proto_);
140 0 : }
141 :
142 : } // namespace proto
143 : } // namespace shared_model
|