hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
blocks_query_template.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP
7 #define IROHA_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP
8 
14 #include "queries.pb.h"
16 
17 namespace shared_model {
18  namespace proto {
19 
28  template <int S = 0,
30  typename BT = UnsignedWrapper<BlocksQuery>>
32  private:
33  template <int, typename, typename>
35 
36  enum RequiredFields {
37  CreatedTime,
38  CreatorAccountId,
39  QueryCounter,
40  TOTAL
41  };
42 
43  template <int s>
45 
46  using ProtoBlocksQuery = iroha::protocol::BlocksQuery;
47 
48  template <int Sp>
51  : query_(o.query_), stateless_validator_(o.stateless_validator_) {}
52 
59  template <int Fields, typename Transformation>
60  auto transform(Transformation t) const {
61  NextBuilder<Fields> copy = *this;
62  t(copy.query_);
63  return copy;
64  }
65 
66  public:
67  TemplateBlocksQueryBuilder(const SV &validator = SV())
68  : stateless_validator_(validator) {}
69 
70  auto createdTime(interface::types::TimestampType created_time) const {
71  return transform<CreatedTime>([&](auto &qry) {
72  auto *meta = qry.mutable_meta();
73  meta->set_created_time(created_time);
74  });
75  }
76 
78  const interface::types::AccountIdType &creator_account_id) const {
79  return transform<CreatorAccountId>([&](auto &qry) {
80  auto *meta = qry.mutable_meta();
81  meta->set_creator_account_id(creator_account_id);
82  });
83  }
84 
85  auto queryCounter(interface::types::CounterType query_counter) const {
86  return transform<QueryCounter>([&](auto &qry) {
87  auto *meta = qry.mutable_meta();
88  meta->set_query_counter(query_counter);
89  });
90  }
91 
92  auto build() const {
93  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
94  auto result = BlocksQuery(iroha::protocol::BlocksQuery(query_));
95  auto answer = stateless_validator_.validate(result);
96  if (answer.hasErrors()) {
97  throw std::invalid_argument(answer.reason());
98  }
99  return BT(std::move(result));
100  }
101 
102  static const int total = RequiredFields::TOTAL;
103 
104  private:
105  ProtoBlocksQuery query_;
106  SV stateless_validator_;
107  };
108  } // namespace proto
109 } // namespace shared_model
110 
111 #endif // IROHA_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP
BlocksQueryValidator< FieldValidator > DefaultUnsignedBlocksQueryValidator
Definition: default_validator.hpp:71
Definition: blocks_query_template.hpp:31
auto build() const
Definition: blocks_query_template.hpp:92
auto createdTime(interface::types::TimestampType created_time) const
Definition: blocks_query_template.hpp:70
std::string AccountIdType
Type of account id.
Definition: types.hpp:37
TemplateBlocksQueryBuilder(const SV &validator=SV())
Definition: blocks_query_template.hpp:67
auto creatorAccountId(const interface::types::AccountIdType &creator_account_id) const
Definition: blocks_query_template.hpp:77
uint64_t CounterType
Type of counter.
Definition: types.hpp:65
Definition: command_executor.hpp:12
auto queryCounter(interface::types::CounterType query_counter) const
Definition: blocks_query_template.hpp:85
#define DEPRECATED
Definition: swig_keyword_hider.hpp:15
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:61