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 std::string &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);
152  });
153  }
154 
156  const interface::types::PubkeyType &peer_key) const {
157  return addPeerRaw(address, peer_key.hex());
158  }
159 
160  auto removePeer(const interface::types::PubkeyType &public_key) const {
161  return addCommand([&](auto proto_command) {
162  auto command = proto_command->mutable_remove_peer();
163  command->set_public_key(public_key.hex());
164  });
165  }
166 
168  const std::string &public_key) const {
169  return addCommand([&](auto proto_command) {
170  auto command = proto_command->mutable_add_signatory();
171  command->set_account_id(account_id);
172  command->set_public_key(public_key);
173  });
174  }
175 
177  const interface::types::PubkeyType &public_key) const {
178  return addSignatoryRaw(account_id, public_key.hex());
179  }
180 
182  const std::string &public_key) const {
183  return addCommand([&](auto proto_command) {
184  auto command = proto_command->mutable_remove_signatory();
185  command->set_account_id(account_id);
186  command->set_public_key(public_key);
187  });
188  }
189 
191  const interface::types::PubkeyType &public_key)
192  const {
193  return removeSignatoryRaw(account_id, public_key.hex());
194  }
195 
197  const interface::types::RoleIdType &role_name) const {
198  return addCommand([&](auto proto_command) {
199  auto command = proto_command->mutable_append_role();
200  command->set_account_id(account_id);
201  command->set_role_name(role_name);
202  });
203  }
204 
206  const interface::types::DomainIdType &domain_id,
207  interface::types::PrecisionType precision) const {
208  return addCommand([&](auto proto_command) {
209  auto command = proto_command->mutable_create_asset();
210  command->set_asset_name(asset_name);
211  command->set_domain_id(domain_id);
212  command->set_precision(precision);
213  });
214  }
215 
217  const interface::types::AccountNameType &account_name,
218  const interface::types::DomainIdType &domain_id,
219  const std::string &main_pubkey) const {
220  return addCommand([&](auto proto_command) {
221  auto command = proto_command->mutable_create_account();
222  command->set_account_name(account_name);
223  command->set_domain_id(domain_id);
224  command->set_public_key(main_pubkey);
225  });
226  }
227 
229  const interface::types::DomainIdType &domain_id,
230  const interface::types::PubkeyType &main_pubkey)
231  const {
232  return createAccountRaw(account_name, domain_id, main_pubkey.hex());
233  }
234 
236  const interface::types::RoleIdType &default_role)
237  const {
238  return addCommand([&](auto proto_command) {
239  auto command = proto_command->mutable_create_domain();
240  command->set_domain_id(domain_id);
241  command->set_default_role(default_role);
242  });
243  }
244 
246  const interface::RolePermissionSet &permissions) const {
247  return addCommand([&](auto proto_command) {
248  auto command = proto_command->mutable_create_role();
249  command->set_role_name(role_name);
250  for (size_t i = 0; i < permissions.size(); ++i) {
251  auto perm = static_cast<interface::permissions::Role>(i);
252  if (permissions.isSet(perm)) {
253  command->add_permissions(permissions::toTransport(perm));
254  }
255  }
256  });
257  }
258 
260  const interface::types::RoleIdType &role_name) const {
261  return addCommand([&](auto proto_command) {
262  auto command = proto_command->mutable_detach_role();
263  command->set_account_id(account_id);
264  command->set_role_name(role_name);
265  });
266  }
267 
269  interface::permissions::Grantable permission) const {
270  return addCommand([&](auto proto_command) {
271  auto command = proto_command->mutable_grant_permission();
272  command->set_account_id(account_id);
273  command->set_permission(permissions::toTransport(permission));
274  });
275  }
276 
279  const {
280  return addCommand([&](auto proto_command) {
281  auto command = proto_command->mutable_revoke_permission();
282  command->set_account_id(account_id);
283  command->set_permission(permissions::toTransport(permission));
284  });
285  }
286 
288  const interface::types::AccountIdType &account_id,
290  const interface::types::AccountDetailValueType &value) const {
291  return addCommand([&](auto proto_command) {
292  auto command = proto_command->mutable_set_account_detail();
293  command->set_account_id(account_id);
294  command->set_key(key);
295  command->set_value(value);
296  });
297  }
298 
300  interface::types::QuorumType quorum) const {
301  return addCommand([&](auto proto_command) {
302  auto command = proto_command->mutable_set_account_quorum();
303  command->set_account_id(account_id);
304  command->set_quorum(quorum);
305  });
306  }
307 
309  const std::string &amount) const {
310  return addCommand([&](auto proto_command) {
311  auto command = proto_command->mutable_subtract_asset_quantity();
312  command->set_asset_id(asset_id);
313  command->set_amount(amount);
314  });
315  }
316 
317  auto transferAsset(const interface::types::AccountIdType &src_account_id,
318  const interface::types::AccountIdType &dest_account_id,
319  const interface::types::AssetIdType &asset_id,
320  const interface::types::DescriptionType &description,
321  const std::string &amount) const {
322  return addCommand([&](auto proto_command) {
323  auto command = proto_command->mutable_transfer_asset();
324  command->set_src_account_id(src_account_id);
325  command->set_dest_account_id(dest_account_id);
326  command->set_asset_id(asset_id);
327  command->set_description(description);
328  command->set_amount(amount);
329  });
330  }
331 
332  auto build() const {
333  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
334  auto result = Transaction(iroha::protocol::Transaction(transaction_));
335  auto answer = stateless_validator_.validate(result);
336  if (answer.hasErrors()) {
337  throw std::invalid_argument(answer.reason());
338  }
339  return BT(std::move(result));
340  }
341 
342  static const int total = RequiredFields::TOTAL;
343 
344  private:
345  ProtoTx transaction_;
346  SV stateless_validator_;
347  };
348 
349  } // namespace proto
350 } // namespace shared_model
351 
352 #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:235
auto revokePermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:277
std::string AddressType
Type of peer address.
Definition: types.hpp:45
auto addPeerRaw(const interface::types::AddressType &address, const std::string &peer_key) const
Definition: transaction_template.hpp:145
Definition: proto_command.hpp:16
iroha::protocol::RolePermission toTransport(interface::permissions::Role r)
Definition: permissions.cpp:17
Definition: transaction.hpp:14
Grantable
Definition: permissions.hpp:69
auto removeSignatoryRaw(const interface::types::AccountIdType &account_id, const std::string &public_key) const
Definition: transaction_template.hpp:181
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:53
Definition: permissions.hpp:95
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:317
auto setAccountQuorum(const interface::types::AddressType &account_id, interface::types::QuorumType quorum) const
Definition: transaction_template.hpp:299
auto createAccountRaw(const interface::types::AccountNameType &account_name, const interface::types::DomainIdType &domain_id, const std::string &main_pubkey) const
Definition: transaction_template.hpp:216
static constexpr size_t size()
Definition: permissions.hpp:104
auto build() const
Definition: transaction_template.hpp:332
std::string DescriptionType
Type of the transfer message.
Definition: types.hpp:83
auto subtractAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:308
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:76
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:14
std::string AccountIdType
Type of account id.
Definition: types.hpp:39
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:155
auto createAsset(const interface::types::AssetNameType &asset_name, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) const
Definition: transaction_template.hpp:205
auto addSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:176
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:228
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:190
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:41
auto addSignatoryRaw(const interface::types::AccountIdType &account_id, const std::string &public_key) const
Definition: transaction_template.hpp:167
std::string AccountNameType
Type of account name.
Definition: types.hpp:68
Definition: public_key.hpp:16
auto grantPermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:268
auto removePeer(const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:160
auto createRole(const interface::types::RoleIdType &role_name, const interface::RolePermissionSet &permissions) const
Definition: transaction_template.hpp:245
BatchType
Definition: types.hpp:88
auto detachRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:259
std::string AccountDetailValueType
Type of account detail value.
Definition: types.hpp:78
Definition: command_executor.hpp:12
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:62
auto setAccountDetail(const interface::types::AccountIdType &account_id, const interface::types::AccountDetailKeyType &key, const interface::types::AccountDetailValueType &value) const
Definition: transaction_template.hpp:287
std::string AssetNameType
Type of asset name.
Definition: types.hpp:70
bool isSet(Perm p) const
Definition: permissions.cpp:92
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:51
auto appendRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:196
TemplateTransactionBuilder()
Definition: transaction_template.hpp:96
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:64
TransactionValidator< FieldValidator, CommandValidatorVisitor< FieldValidator >> DefaultUnsignedTransactionValidator
Definition: default_validator.hpp:30
std::string AssetIdType
Type of asset id.
Definition: types.hpp:55