6 #ifndef IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP 7 #define IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP 9 #include <boost/range/algorithm/for_each.hpp> 11 #include <string_view> 19 #include "module/irohad/common/validators_config.hpp" 20 #include "queries.pb.h" 36 typename BT = UnsignedWrapper<Query>>
39 template <
int,
typename,
typename>
57 : query_(o.query_), stateless_validator_(o.stateless_validator_) {}
65 template <
int Fields,
typename Transformation>
66 auto transform(Transformation t)
const {
78 template <
typename Transformation>
79 auto queryField(Transformation t)
const {
81 t(copy.query_.mutable_payload());
86 template <
typename PageMetaPayload>
87 static auto setTxPaginationMeta(
88 PageMetaPayload * page_meta_payload,
90 const std::optional<interface::types::HashType> &first_hash =
93 auto from_interface_2_proto_field =
100 return iroha::protocol::Field::kPosition;
103 BOOST_ASSERT_MSG(
false,
"Unexpected Field value!");
107 auto from_interface_2_proto_direction =
111 return iroha::protocol::Direction::kAscending;
114 return iroha::protocol::Direction::kDescending;
117 BOOST_ASSERT_MSG(
false,
"Unexpected Direction value!");
122 page_meta_payload->set_page_size(page_size);
124 page_meta_payload->set_first_tx_hash(first_hash->hex());
129 (*ordering).get(ptr, count);
131 for (
size_t ix = 0; ix < count; ++ix) {
135 page_meta_payload->mutable_ordering()->add_sequence();
136 sequence->set_field(from_interface_2_proto_field(entry.
field));
137 sequence->set_direction(
138 from_interface_2_proto_direction(entry.
direction));
144 : stateless_validator_(validator) {}
153 return transform<CreatedTime>([&](
auto &qry) {
154 qry.mutable_payload()->mutable_meta()->set_created_time(created_time);
160 return transform<CreatorAccountId>([&](
auto &qry) {
161 qry.mutable_payload()->mutable_meta()->set_creator_account_id(
167 return transform<QueryCounter>([&](
auto &qry) {
168 qry.mutable_payload()->mutable_meta()->set_query_counter(
174 return queryField([&](
auto proto_query) {
175 auto query = proto_query->mutable_get_account();
176 query->set_account_id(account_id);
182 return queryField([&](
auto proto_query) {
183 auto query = proto_query->mutable_get_signatories();
184 query->set_account_id(account_id);
191 const std::optional<interface::types::HashType> &first_hash =
194 return queryField([&](
auto proto_query) {
195 auto query = proto_query->mutable_get_account_transactions();
196 query->set_account_id(account_id);
197 setTxPaginationMeta(query->mutable_pagination_meta(),
208 const std::optional<interface::types::HashType> &first_hash =
211 return queryField([&](
auto proto_query) {
212 auto query = proto_query->mutable_get_account_asset_transactions();
213 query->set_account_id(account_id);
214 query->set_asset_id(asset_id);
215 setTxPaginationMeta(query->mutable_pagination_meta(),
225 std::optional<shared_model::interface::types::AssetIdType>
226 first_asset_id)
const {
227 return queryField([&](
auto proto_query) {
228 auto query = proto_query->mutable_get_account_assets();
229 query->set_account_id(account_id);
230 auto pagination_meta = query->mutable_pagination_meta();
231 pagination_meta->set_page_size(page_size);
232 if (first_asset_id) {
233 pagination_meta->set_first_asset_id(*first_asset_id);
243 const std::optional<plain::AccountDetailRecordId> &first_record_id =
245 return queryField([&](
auto proto_query) {
246 auto query = proto_query->mutable_get_account_detail();
247 if (not account_id.empty()) {
248 query->set_account_id(account_id);
250 if (not key.empty()) {
253 if (not writer.empty()) {
254 query->set_writer(writer);
256 auto pagination_meta = query->mutable_pagination_meta();
257 pagination_meta->set_page_size(page_size);
258 if (first_record_id) {
259 auto proto_first_record_id =
260 pagination_meta->mutable_first_record_id();
261 proto_first_record_id->set_writer(first_record_id->writer());
262 proto_first_record_id->set_key(first_record_id->key());
268 return queryField([&](
auto proto_query) {
269 auto query = proto_query->mutable_get_block();
270 query->set_height(height);
275 return queryField([&](
auto proto_query) {
276 auto query = proto_query->mutable_get_engine_receipts();
277 query->set_tx_hash(tx_hash.data(), tx_hash.size());
283 [&](
auto proto_query) { proto_query->mutable_get_roles(); });
287 return queryField([&](
auto proto_query) {
288 auto query = proto_query->mutable_get_asset_info();
289 query->set_asset_id(asset_id);
295 return queryField([&](
auto proto_query) {
296 auto query = proto_query->mutable_get_role_permissions();
297 query->set_role_id(role_id);
301 template <
typename Collection>
303 return queryField([&](
auto proto_query) {
304 auto query = proto_query->mutable_get_transactions();
305 boost::for_each(hashes, [&query](
const auto &
hash) {
306 query->add_tx_hashes(
hash.hex());
312 std::initializer_list<interface::types::HashType> hashes)
const {
316 template <
typename... Hash>
322 return queryField([&](
auto proto_query) {
323 proto_query->mutable_get_pending_transactions();
329 const std::optional<interface::types::HashType> &first_hash =
330 std::nullopt)
const {
331 return queryField([&](
auto proto_query) {
332 auto query = proto_query->mutable_get_pending_transactions();
334 query->mutable_pagination_meta(), page_size, first_hash);
340 [&](
auto proto_query) { proto_query->mutable_get_peers(); });
344 static_assert(S == (1 << TOTAL) - 1,
"Required fields are not set");
345 if (not query_.has_payload()) {
346 throw std::invalid_argument(
"Query missing payload");
348 if (query_.payload().query_case()
349 == iroha::protocol::Query_Payload::QueryCase::QUERY_NOT_SET) {
350 throw std::invalid_argument(
"Missing concrete query");
353 if (
auto error = stateless_validator_.validate(result)) {
354 throw std::invalid_argument(error->toString());
356 return BT(std::move(result));
359 static const int total = RequiredFields::TOTAL;
363 SV stateless_validator_;
368 #endif // IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP auto getBlock(interface::types::HeightType height) const
Definition: query_template.hpp:267
Definition: ordering.hpp:19
auto getTransactions(const Hash &... hashes) const
Definition: query_template.hpp:317
uint64_t HeightType
Type of height (for Block, Proposal etc)
Definition: types.hpp:48
Direction direction
Definition: ordering.hpp:48
auto createdTime(interface::types::TimestampType created_time) const
Definition: query_template.hpp:152
QueryValidator< FieldValidator, QueryValidatorVisitor< FieldValidator > > DefaultUnsignedQueryValidator
Definition: default_validator.hpp:57
auto getAccountAssets(const interface::types::AccountIdType &account_id, size_t page_size, std::optional< shared_model::interface::types::AssetIdType > first_asset_id) const
Definition: query_template.hpp:222
auto creatorAccountId(const interface::types::AccountIdType &creator_account_id) const
Definition: query_template.hpp:158
Definition: query_response_handler.cpp:72
auto getTransactions(const Collection &hashes) const
Definition: query_template.hpp:302
auto getAssetInfo(const interface::types::AssetIdType &asset_id) const
Definition: query_template.hpp:286
auto getPeers() const
Definition: query_template.hpp:338
auto getAccountTransactions(const interface::types::AccountIdType &account_id, interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt, const interface::Ordering *ordering=nullptr) const
Definition: query_template.hpp:188
Field field
Definition: ordering.hpp:47
Field
Definition: ordering.hpp:24
auto getAccount(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:173
auto getPendingTransactions(interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt) const
Definition: query_template.hpp:327
TemplateQueryBuilder()
Definition: query_template.hpp:149
auto queryCounter(interface::types::CounterType query_counter) const
Definition: query_template.hpp:166
auto getPendingTransactions() const
Definition: query_template.hpp:321
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:79
Definition: block_query.hpp:15
Definition: proto_query.hpp:16
static std::vector< std::shared_ptr< shared_model::interface::Transaction > > getTransactions(size_t requested_tx_amount, detail::BatchSetType &batch_collection, boost::optional< size_t &> discarded_txs_amount)
Definition: on_demand_ordering_service_impl.cpp:109
std::string AccountIdType
Type of account id.
Definition: types.hpp:38
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
auto getSignatories(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:180
auto getRoles() const
Definition: query_template.hpp:281
uint16_t TransactionsNumberType
Type of a number of transactions in block and query response page.
Definition: types.hpp:84
auto getTransactions(std::initializer_list< interface::types::HashType > hashes) const
Definition: query_template.hpp:311
uint64_t CounterType
Type of counter.
Definition: types.hpp:69
auto getAccountDetail(size_t page_size, const interface::types::AccountIdType &account_id="", const interface::types::AccountDetailKeyType &key="", const interface::types::AccountIdType &writer="", const std::optional< plain::AccountDetailRecordId > &first_record_id=std::nullopt)
Definition: query_template.hpp:238
auto getAccountAssetTransactions(const interface::types::AccountIdType &account_id, const interface::types::AssetIdType &asset_id, interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt, const interface::Ordering *ordering=nullptr) const
Definition: query_template.hpp:204
Definition: ordering.hpp:46
Definition: command_executor.hpp:12
auto build() const
Definition: query_template.hpp:343
auto getEngineReceipts(std::string_view tx_hash)
Definition: query_template.hpp:274
Direction
Definition: ordering.hpp:35
Definition: query_template.hpp:37
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:54
auto getRolePermissions(const interface::types::RoleIdType &role_id) const
Definition: query_template.hpp:293
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:67
std::string AssetIdType
Type of asset id.
Definition: types.hpp:58