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