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