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