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::AddressType &address,
147  const std::string &peer_key,
148  const boost::optional<interface::types::TLSCertificateType>
149  &tls_certificate = boost::none) 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  peer->set_peer_key(peer_key);
155  if (tls_certificate) {
156  peer->set_tls_certificate(*tls_certificate);
157  }
158  });
159  }
160 
162  const interface::types::PubkeyType &peer_key,
163  const boost::optional<interface::types::TLSCertificateType>
164  &tls_certificate = boost::none) const {
165  return addPeerRaw(address, peer_key.hex(), tls_certificate);
166  }
167 
168  auto removePeer(const interface::types::PubkeyType &public_key) const {
169  return addCommand([&](auto proto_command) {
170  auto command = proto_command->mutable_remove_peer();
171  command->set_public_key(public_key.hex());
172  });
173  }
174 
176  const std::string &public_key) const {
177  return addCommand([&](auto proto_command) {
178  auto command = proto_command->mutable_add_signatory();
179  command->set_account_id(account_id);
180  command->set_public_key(public_key);
181  });
182  }
183 
185  const interface::types::PubkeyType &public_key) const {
186  return addSignatoryRaw(account_id, public_key.hex());
187  }
188 
190  const std::string &public_key) const {
191  return addCommand([&](auto proto_command) {
192  auto command = proto_command->mutable_remove_signatory();
193  command->set_account_id(account_id);
194  command->set_public_key(public_key);
195  });
196  }
197 
199  const interface::types::PubkeyType &public_key)
200  const {
201  return removeSignatoryRaw(account_id, public_key.hex());
202  }
203 
205  const interface::types::RoleIdType &role_name) const {
206  return addCommand([&](auto proto_command) {
207  auto command = proto_command->mutable_append_role();
208  command->set_account_id(account_id);
209  command->set_role_name(role_name);
210  });
211  }
212 
214  const interface::types::DomainIdType &domain_id,
215  interface::types::PrecisionType precision) const {
216  return addCommand([&](auto proto_command) {
217  auto command = proto_command->mutable_create_asset();
218  command->set_asset_name(asset_name);
219  command->set_domain_id(domain_id);
220  command->set_precision(precision);
221  });
222  }
223 
225  const interface::types::AccountNameType &account_name,
226  const interface::types::DomainIdType &domain_id,
227  const std::string &main_pubkey) const {
228  return addCommand([&](auto proto_command) {
229  auto command = proto_command->mutable_create_account();
230  command->set_account_name(account_name);
231  command->set_domain_id(domain_id);
232  command->set_public_key(main_pubkey);
233  });
234  }
235 
237  const interface::types::DomainIdType &domain_id,
238  const interface::types::PubkeyType &main_pubkey)
239  const {
240  return createAccountRaw(account_name, domain_id, main_pubkey.hex());
241  }
242 
244  const interface::types::RoleIdType &default_role)
245  const {
246  return addCommand([&](auto proto_command) {
247  auto command = proto_command->mutable_create_domain();
248  command->set_domain_id(domain_id);
249  command->set_default_role(default_role);
250  });
251  }
252 
254  const interface::RolePermissionSet &permissions) const {
255  return addCommand([&](auto proto_command) {
256  auto command = proto_command->mutable_create_role();
257  command->set_role_name(role_name);
258  for (size_t i = 0; i < permissions.size(); ++i) {
259  auto perm = static_cast<interface::permissions::Role>(i);
260  if (permissions.isSet(perm)) {
261  command->add_permissions(permissions::toTransport(perm));
262  }
263  }
264  });
265  }
266 
268  const interface::types::RoleIdType &role_name) const {
269  return addCommand([&](auto proto_command) {
270  auto command = proto_command->mutable_detach_role();
271  command->set_account_id(account_id);
272  command->set_role_name(role_name);
273  });
274  }
275 
277  interface::permissions::Grantable permission) const {
278  return addCommand([&](auto proto_command) {
279  auto command = proto_command->mutable_grant_permission();
280  command->set_account_id(account_id);
281  command->set_permission(permissions::toTransport(permission));
282  });
283  }
284 
287  const {
288  return addCommand([&](auto proto_command) {
289  auto command = proto_command->mutable_revoke_permission();
290  command->set_account_id(account_id);
291  command->set_permission(permissions::toTransport(permission));
292  });
293  }
294 
296  const interface::types::AccountIdType &account_id,
298  const interface::types::AccountDetailValueType &value) const {
299  return addCommand([&](auto proto_command) {
300  auto command = proto_command->mutable_set_account_detail();
301  command->set_account_id(account_id);
302  command->set_key(key);
303  command->set_value(value);
304  });
305  }
306 
308  interface::types::QuorumType quorum) const {
309  return addCommand([&](auto proto_command) {
310  auto command = proto_command->mutable_set_account_quorum();
311  command->set_account_id(account_id);
312  command->set_quorum(quorum);
313  });
314  }
315 
317  const std::string &amount) const {
318  return addCommand([&](auto proto_command) {
319  auto command = proto_command->mutable_subtract_asset_quantity();
320  command->set_asset_id(asset_id);
321  command->set_amount(amount);
322  });
323  }
324 
325  auto transferAsset(const interface::types::AccountIdType &src_account_id,
326  const interface::types::AccountIdType &dest_account_id,
327  const interface::types::AssetIdType &asset_id,
328  const interface::types::DescriptionType &description,
329  const std::string &amount) const {
330  return addCommand([&](auto proto_command) {
331  auto command = proto_command->mutable_transfer_asset();
332  command->set_src_account_id(src_account_id);
333  command->set_dest_account_id(dest_account_id);
334  command->set_asset_id(asset_id);
335  command->set_description(description);
336  command->set_amount(amount);
337  });
338  }
339 
340  auto build() const {
341  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
342  auto result = Transaction(iroha::protocol::Transaction(transaction_));
343  auto answer = stateless_validator_.validate(result);
344  if (answer.hasErrors()) {
345  throw std::invalid_argument(answer.reason());
346  }
347  return BT(std::move(result));
348  }
349 
350  static const int total = RequiredFields::TOTAL;
351 
352  private:
353  ProtoTx transaction_;
354  SV stateless_validator_;
355  };
356 
357  } // namespace proto
358 } // namespace shared_model
359 
360 #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:267
auto createRole(const interface::types::RoleIdType &role_name, const interface::RolePermissionSet &permissions) const
Definition: transaction_template.hpp:253
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:236
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:307
Grantable
Definition: permissions.hpp:70
auto addSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:184
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 addPeer(const interface::types::AddressType &address, const interface::types::PubkeyType &peer_key, const boost::optional< interface::types::TLSCertificateType > &tls_certificate=boost::none) const
Definition: transaction_template.hpp:161
auto addPeerRaw(const interface::types::AddressType &address, const std::string &peer_key, const boost::optional< interface::types::TLSCertificateType > &tls_certificate=boost::none) const
Definition: transaction_template.hpp:145
auto appendRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:204
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:198
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:276
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:325
Definition: block_query.hpp:14
auto createDomain(const interface::types::DomainIdType &domain_id, const interface::types::RoleIdType &default_role) const
Definition: transaction_template.hpp:243
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:295
auto createAccountRaw(const interface::types::AccountNameType &account_name, const interface::types::DomainIdType &domain_id, const std::string &main_pubkey) const
Definition: transaction_template.hpp:224
auto createAsset(const interface::types::AssetNameType &asset_name, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) const
Definition: transaction_template.hpp:213
auto batchMeta(interface::types::BatchType type, std::vector< interface::types::HashType > hashes) const
Definition: transaction_template.hpp:109
Definition: transaction_template.hpp:37
auto subtractAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:316
auto build() const
Definition: transaction_template.hpp:340
auto removePeer(const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:168
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:189
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:285
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:175
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