hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
transaction_template.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_TRANSACTION_BUILDER_TEMPLATE_HPP
7 #define IROHA_PROTO_TRANSACTION_BUILDER_TEMPLATE_HPP
8 
10 
11 #include <boost/range/algorithm/for_each.hpp>
12 
13 #include "commands.pb.h"
14 #include "primitive.pb.h"
15 #include "transaction.pb.h"
16 
21 #include "module/irohad/common/validators_config.hpp"
23 
24 namespace shared_model {
25  namespace proto {
26 
34  template <int S = 0,
36  typename BT = UnsignedWrapper<Transaction>>
37  class [[deprecated]] TemplateTransactionBuilder {
38  private:
39  template <int, typename, typename>
41 
42  enum RequiredFields {
43  Command,
44  CreatorAccountId,
45  CreatedTime,
46  Quorum,
47  TOTAL
48  };
49 
50  template <int s>
52 
53  using ProtoTx = iroha::protocol::Transaction;
54  using ProtoCommand = iroha::protocol::Command;
55 
56  template <int Sp>
59  : transaction_(o.transaction_),
60  stateless_validator_(o.stateless_validator_) {}
61 
68  template <int Fields, typename Transformation>
69  auto transform(Transformation t) const {
70  NextBuilder<Fields> copy = *this;
71  t(copy.transaction_);
72  return copy;
73  }
74 
81  template <typename Transformation>
82  auto addCommand(Transformation t) const {
83  NextBuilder<Command> copy = *this;
84  t(copy.transaction_.mutable_payload()
85  ->mutable_reduced_payload()
86  ->add_commands());
87  return copy;
88  }
89 
90  TemplateTransactionBuilder(const SV &validator)
91  : stateless_validator_(validator) {}
92 
93  public:
94  // we do such default initialization only because it is deprecated and
95  // used only in tests
98  SV(iroha::test::kTestsValidatorsConfig)) {}
99 
101  const {
102  return transform<CreatorAccountId>([&](auto &tx) {
103  tx.mutable_payload()
104  ->mutable_reduced_payload()
105  ->set_creator_account_id(account_id);
106  });
107  }
108 
110  std::vector<interface::types::HashType> hashes) const {
111  return transform<0>([&](auto &tx) {
112  tx.mutable_payload()->mutable_batch()->set_type(
113  static_cast<
115  type));
116  for (const auto &hash : hashes) {
117  tx.mutable_payload()->mutable_batch()->add_reduced_hashes(
118  hash.hex());
119  }
120  });
121  }
122 
123  auto createdTime(interface::types::TimestampType created_time) const {
124  return transform<CreatedTime>([&](auto &tx) {
125  tx.mutable_payload()->mutable_reduced_payload()->set_created_time(
126  created_time);
127  });
128  }
129 
131  return transform<Quorum>([&](auto &tx) {
132  tx.mutable_payload()->mutable_reduced_payload()->set_quorum(quorum);
133  });
134  }
135 
137  const std::string &amount) const {
138  return addCommand([&](auto proto_command) {
139  auto command = proto_command->mutable_add_asset_quantity();
140  command->set_asset_id(asset_id);
141  command->set_amount(amount);
142  });
143  }
144 
146  const interface::types::PubkeyType &peer_key) const {
147  return addCommand([&](auto proto_command) {
148  auto command = proto_command->mutable_add_peer();
149  auto peer = command->mutable_peer();
150  peer->set_address(address);
151  peer->set_peer_key(peer_key.hex());
152  });
153  }
154 
156  const interface::types::PubkeyType &public_key) const {
157  return addCommand([&](auto proto_command) {
158  auto command = proto_command->mutable_add_signatory();
159  command->set_account_id(account_id);
160  command->set_public_key(public_key.hex());
161  });
162  }
163 
165  const interface::types::PubkeyType &public_key)
166  const {
167  return addCommand([&](auto proto_command) {
168  auto command = proto_command->mutable_remove_signatory();
169  command->set_account_id(account_id);
170  command->set_public_key(public_key.hex());
171  });
172  }
173 
175  const interface::types::RoleIdType &role_name) const {
176  return addCommand([&](auto proto_command) {
177  auto command = proto_command->mutable_append_role();
178  command->set_account_id(account_id);
179  command->set_role_name(role_name);
180  });
181  }
182 
184  const interface::types::DomainIdType &domain_id,
185  interface::types::PrecisionType precision) const {
186  return addCommand([&](auto proto_command) {
187  auto command = proto_command->mutable_create_asset();
188  command->set_asset_name(asset_name);
189  command->set_domain_id(domain_id);
190  command->set_precision(precision);
191  });
192  }
193 
195  const interface::types::DomainIdType &domain_id,
196  const interface::types::PubkeyType &main_pubkey)
197  const {
198  return addCommand([&](auto proto_command) {
199  auto command = proto_command->mutable_create_account();
200  command->set_account_name(account_name);
201  command->set_domain_id(domain_id);
202  command->set_public_key(main_pubkey.hex());
203  });
204  }
205 
207  const interface::types::RoleIdType &default_role)
208  const {
209  return addCommand([&](auto proto_command) {
210  auto command = proto_command->mutable_create_domain();
211  command->set_domain_id(domain_id);
212  command->set_default_role(default_role);
213  });
214  }
215 
217  const interface::RolePermissionSet &permissions) const {
218  return addCommand([&](auto proto_command) {
219  auto command = proto_command->mutable_create_role();
220  command->set_role_name(role_name);
221  for (size_t i = 0; i < permissions.size(); ++i) {
222  auto perm = static_cast<interface::permissions::Role>(i);
223  if (permissions.test(perm)) {
224  command->add_permissions(permissions::toTransport(perm));
225  }
226  }
227  });
228  }
229 
231  const interface::types::RoleIdType &role_name) const {
232  return addCommand([&](auto proto_command) {
233  auto command = proto_command->mutable_detach_role();
234  command->set_account_id(account_id);
235  command->set_role_name(role_name);
236  });
237  }
238 
240  interface::permissions::Grantable permission) const {
241  return addCommand([&](auto proto_command) {
242  auto command = proto_command->mutable_grant_permission();
243  command->set_account_id(account_id);
244  command->set_permission(permissions::toTransport(permission));
245  });
246  }
247 
250  const {
251  return addCommand([&](auto proto_command) {
252  auto command = proto_command->mutable_revoke_permission();
253  command->set_account_id(account_id);
254  command->set_permission(permissions::toTransport(permission));
255  });
256  }
257 
259  const interface::types::AccountIdType &account_id,
261  const interface::types::AccountDetailValueType &value) const {
262  return addCommand([&](auto proto_command) {
263  auto command = proto_command->mutable_set_account_detail();
264  command->set_account_id(account_id);
265  command->set_key(key);
266  command->set_value(value);
267  });
268  }
269 
271  interface::types::QuorumType quorum) const {
272  return addCommand([&](auto proto_command) {
273  auto command = proto_command->mutable_set_account_quorum();
274  command->set_account_id(account_id);
275  command->set_quorum(quorum);
276  });
277  }
278 
280  const std::string &amount) const {
281  return addCommand([&](auto proto_command) {
282  auto command = proto_command->mutable_subtract_asset_quantity();
283  command->set_asset_id(asset_id);
284  command->set_amount(amount);
285  });
286  }
287 
288  auto transferAsset(const interface::types::AccountIdType &src_account_id,
289  const interface::types::AccountIdType &dest_account_id,
290  const interface::types::AssetIdType &asset_id,
291  const interface::types::DescriptionType &description,
292  const std::string &amount) const {
293  return addCommand([&](auto proto_command) {
294  auto command = proto_command->mutable_transfer_asset();
295  command->set_src_account_id(src_account_id);
296  command->set_dest_account_id(dest_account_id);
297  command->set_asset_id(asset_id);
298  command->set_description(description);
299  command->set_amount(amount);
300  });
301  }
302 
303  auto build() const {
304  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
305  auto result = Transaction(iroha::protocol::Transaction(transaction_));
306  auto answer = stateless_validator_.validate(result);
307  if (answer.hasErrors()) {
308  throw std::invalid_argument(answer.reason());
309  }
310  return BT(std::move(result));
311  }
312 
313  static const int total = RequiredFields::TOTAL;
314 
315  private:
316  ProtoTx transaction_;
317  SV stateless_validator_;
318  };
319 
320  } // namespace proto
321 } // namespace shared_model
322 
323 #endif // IROHA_PROTO_TRANSACTION_BUILDER_TEMPLATE_HPP
auto createdTime(interface::types::TimestampType created_time) const
Definition: transaction_template.hpp:123
auto createDomain(const interface::types::DomainIdType &domain_id, const interface::types::RoleIdType &default_role) const
Definition: transaction_template.hpp:206
auto revokePermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:248
Definition: proto_command.hpp:15
iroha::protocol::RolePermission toTransport(interface::permissions::Role r)
Definition: permissions.cpp:17
bool test(Perm p) const
Definition: permissions.cpp:97
Definition: transaction.hpp:14
Grantable
Definition: permissions.hpp:66
std::string AddressType
Type of peer address.
Definition: types.hpp:41
auto addAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:136
std::string DomainIdType
Iroha domain id type.
Definition: types.hpp:49
Definition: permissions.hpp:92
auto quorum(interface::types::QuorumType quorum) const
Definition: transaction_template.hpp:130
auto transferAsset(const interface::types::AccountIdType &src_account_id, const interface::types::AccountIdType &dest_account_id, const interface::types::AssetIdType &asset_id, const interface::types::DescriptionType &description, const std::string &amount) const
Definition: transaction_template.hpp:288
auto setAccountQuorum(const interface::types::AddressType &account_id, interface::types::QuorumType quorum) const
Definition: transaction_template.hpp:270
static size_t size()
Definition: permissions.cpp:68
auto build() const
Definition: transaction_template.hpp:303
std::string DescriptionType
Type of the transfer message.
Definition: types.hpp:79
auto subtractAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:279
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:73
Role
Definition: permissions.hpp:17
auto batchMeta(interface::types::BatchType type, std::vector< interface::types::HashType > hashes) const
Definition: transaction_template.hpp:109
virtual const std::string & hex() const
Definition: blob.cpp:45
Definition: block_query.hpp:16
std::string AccountIdType
Type of account id.
Definition: types.hpp:35
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
auto addPeer(const interface::types::AddressType &address, const interface::types::PubkeyType &peer_key) const
Definition: transaction_template.hpp:145
auto createAsset(const interface::types::AssetNameType &asset_name, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) const
Definition: transaction_template.hpp:183
auto addSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:155
Definition: transaction_template.hpp:37
auto createAccount(const interface::types::AccountNameType &account_name, const interface::types::DomainIdType &domain_id, const interface::types::PubkeyType &main_pubkey) const
Definition: transaction_template.hpp:194
auto creatorAccountId(const interface::types::AccountIdType &account_id) const
Definition: transaction_template.hpp:100
auto removeSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:164
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:37
std::string AccountNameType
Type of account name.
Definition: types.hpp:65
Definition: public_key.hpp:16
auto grantPermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:239
auto createRole(const interface::types::RoleIdType &role_name, const interface::RolePermissionSet &permissions) const
Definition: transaction_template.hpp:216
BatchType
Definition: types.hpp:81
auto detachRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:230
std::string AccountDetailValueType
Type of account detail value.
Definition: types.hpp:75
Definition: command_executor.hpp:12
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:57
auto setAccountDetail(const interface::types::AccountIdType &account_id, const interface::types::AccountDetailKeyType &key, const interface::types::AccountDetailValueType &value) const
Definition: transaction_template.hpp:258
std::string AssetNameType
Type of asset name.
Definition: types.hpp:67
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:47
auto appendRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:174
TemplateTransactionBuilder()
Definition: transaction_template.hpp:96
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:59
TransactionValidator< FieldValidator, CommandValidatorVisitor< FieldValidator >> DefaultUnsignedTransactionValidator
Definition: default_validator.hpp:30
std::string AssetIdType
Type of asset id.
Definition: types.hpp:51