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 
23 namespace shared_model {
24  namespace proto {
25 
33  template <int S = 0,
35  typename BT = UnsignedWrapper<Transaction>>
37  private:
38  template <int, typename, typename>
40 
41  enum RequiredFields {
42  Command,
43  CreatorAccountId,
44  CreatedTime,
45  Quorum,
46  TOTAL
47  };
48 
49  template <int s>
51 
52  using ProtoTx = iroha::protocol::Transaction;
53  using ProtoCommand = iroha::protocol::Command;
54 
55  template <int Sp>
58  : transaction_(o.transaction_),
59  stateless_validator_(o.stateless_validator_) {}
60 
67  template <int Fields, typename Transformation>
68  auto transform(Transformation t) const {
69  NextBuilder<Fields> copy = *this;
70  t(copy.transaction_);
71  return copy;
72  }
73 
80  template <typename Transformation>
81  auto addCommand(Transformation t) const {
82  NextBuilder<Command> copy = *this;
83  t(copy.transaction_.mutable_payload()
84  ->mutable_reduced_payload()
85  ->add_commands());
86  return copy;
87  }
88 
89  public:
90  TemplateTransactionBuilder(const SV &validator = SV())
91  : stateless_validator_(validator) {}
92 
94  const interface::types::AccountIdType &account_id) const {
95  return transform<CreatorAccountId>([&](auto &tx) {
96  tx.mutable_payload()
97  ->mutable_reduced_payload()
98  ->set_creator_account_id(account_id);
99  });
100  }
101 
103  std::vector<interface::types::HashType> hashes) const {
104  return transform<0>([&](auto &tx) {
105  tx.mutable_payload()->mutable_batch()->set_type(
106  static_cast<
108  type));
109  for (const auto &hash : hashes) {
110  tx.mutable_payload()->mutable_batch()->add_reduced_hashes(
111  hash.hex());
112  }
113  });
114  }
115 
116  auto createdTime(interface::types::TimestampType created_time) const {
117  return transform<CreatedTime>([&](auto &tx) {
118  tx.mutable_payload()->mutable_reduced_payload()->set_created_time(
119  created_time);
120  });
121  }
122 
124  return transform<Quorum>([&](auto &tx) {
125  tx.mutable_payload()->mutable_reduced_payload()->set_quorum(quorum);
126  });
127  }
128 
130  const std::string &amount) const {
131  return addCommand([&](auto proto_command) {
132  auto command = proto_command->mutable_add_asset_quantity();
133  command->set_asset_id(asset_id);
134  command->set_amount(amount);
135  });
136  }
137 
139  const interface::types::PubkeyType &peer_key) const {
140  return addCommand([&](auto proto_command) {
141  auto command = proto_command->mutable_add_peer();
142  auto peer = command->mutable_peer();
143  peer->set_address(address);
144  peer->set_peer_key(peer_key.hex());
145  });
146  }
147 
149  const interface::types::PubkeyType &public_key) const {
150  return addCommand([&](auto proto_command) {
151  auto command = proto_command->mutable_add_signatory();
152  command->set_account_id(account_id);
153  command->set_public_key(public_key.hex());
154  });
155  }
156 
158  const interface::types::AccountIdType &account_id,
159  const interface::types::PubkeyType &public_key) const {
160  return addCommand([&](auto proto_command) {
161  auto command = proto_command->mutable_remove_signatory();
162  command->set_account_id(account_id);
163  command->set_public_key(public_key.hex());
164  });
165  }
166 
168  const interface::types::RoleIdType &role_name) const {
169  return addCommand([&](auto proto_command) {
170  auto command = proto_command->mutable_append_role();
171  command->set_account_id(account_id);
172  command->set_role_name(role_name);
173  });
174  }
175 
177  const interface::types::DomainIdType &domain_id,
178  interface::types::PrecisionType precision) const {
179  return addCommand([&](auto proto_command) {
180  auto command = proto_command->mutable_create_asset();
181  command->set_asset_name(asset_name);
182  command->set_domain_id(domain_id);
183  command->set_precision(precision);
184  });
185  }
186 
188  const interface::types::AccountNameType &account_name,
189  const interface::types::DomainIdType &domain_id,
190  const interface::types::PubkeyType &main_pubkey) const {
191  return addCommand([&](auto proto_command) {
192  auto command = proto_command->mutable_create_account();
193  command->set_account_name(account_name);
194  command->set_domain_id(domain_id);
195  command->set_public_key(main_pubkey.hex());
196  });
197  }
198 
200  const interface::types::DomainIdType &domain_id,
201  const interface::types::RoleIdType &default_role) const {
202  return addCommand([&](auto proto_command) {
203  auto command = proto_command->mutable_create_domain();
204  command->set_domain_id(domain_id);
205  command->set_default_role(default_role);
206  });
207  }
208 
210  const interface::RolePermissionSet &permissions) const {
211  return addCommand([&](auto proto_command) {
212  auto command = proto_command->mutable_create_role();
213  command->set_role_name(role_name);
214  for (size_t i = 0; i < permissions.size(); ++i) {
215  auto perm = static_cast<interface::permissions::Role>(i);
216  if (permissions.test(perm)) {
217  command->add_permissions(permissions::toTransport(perm));
218  }
219  }
220  });
221  }
222 
224  const interface::types::RoleIdType &role_name) const {
225  return addCommand([&](auto proto_command) {
226  auto command = proto_command->mutable_detach_role();
227  command->set_account_id(account_id);
228  command->set_role_name(role_name);
229  });
230  }
231 
233  interface::permissions::Grantable permission) const {
234  return addCommand([&](auto proto_command) {
235  auto command = proto_command->mutable_grant_permission();
236  command->set_account_id(account_id);
237  command->set_permission(permissions::toTransport(permission));
238  });
239  }
240 
242  const interface::types::AccountIdType &account_id,
243  interface::permissions::Grantable permission) const {
244  return addCommand([&](auto proto_command) {
245  auto command = proto_command->mutable_revoke_permission();
246  command->set_account_id(account_id);
247  command->set_permission(permissions::toTransport(permission));
248  });
249  }
250 
252  const interface::types::AccountIdType &account_id,
254  const interface::types::AccountDetailValueType &value) const {
255  return addCommand([&](auto proto_command) {
256  auto command = proto_command->mutable_set_account_detail();
257  command->set_account_id(account_id);
258  command->set_key(key);
259  command->set_value(value);
260  });
261  }
262 
264  interface::types::QuorumType quorum) const {
265  return addCommand([&](auto proto_command) {
266  auto command = proto_command->mutable_set_account_quorum();
267  command->set_account_id(account_id);
268  command->set_quorum(quorum);
269  });
270  }
271 
273  const std::string &amount) const {
274  return addCommand([&](auto proto_command) {
275  auto command = proto_command->mutable_subtract_asset_quantity();
276  command->set_asset_id(asset_id);
277  command->set_amount(amount);
278  });
279  }
280 
281  auto transferAsset(const interface::types::AccountIdType &src_account_id,
282  const interface::types::AccountIdType &dest_account_id,
283  const interface::types::AssetIdType &asset_id,
285  const std::string &amount) const {
286  return addCommand([&](auto proto_command) {
287  auto command = proto_command->mutable_transfer_asset();
288  command->set_src_account_id(src_account_id);
289  command->set_dest_account_id(dest_account_id);
290  command->set_asset_id(asset_id);
291  command->set_description(description);
292  command->set_amount(amount);
293  });
294  }
295 
296  auto build() const {
297  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
298  auto result = Transaction(iroha::protocol::Transaction(transaction_));
299  auto answer = stateless_validator_.validate(result);
300  if (answer.hasErrors()) {
301  throw std::invalid_argument(answer.reason());
302  }
303  return BT(std::move(result));
304  }
305 
306  static const int total = RequiredFields::TOTAL;
307 
308  private:
309  ProtoTx transaction_;
310  SV stateless_validator_;
311  };
312 
313  } // namespace proto
314 } // namespace shared_model
315 
316 #endif // IROHA_PROTO_TRANSACTION_BUILDER_TEMPLATE_HPP
auto createdTime(interface::types::TimestampType created_time) const
Definition: transaction_template.hpp:116
auto createDomain(const interface::types::DomainIdType &domain_id, const interface::types::RoleIdType &default_role) const
Definition: transaction_template.hpp:199
auto revokePermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:241
Definition: proto_command.hpp:15
iroha::protocol::RolePermission toTransport(interface::permissions::Role r)
Definition: permissions.cpp:17
bool test(Perm p) const
Definition: permissions.cpp:97
Grantable
Definition: permissions.hpp:66
std::string AddressType
Type of peer address.
Definition: types.hpp:43
auto addAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:129
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:123
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:281
auto setAccountQuorum(const interface::types::AddressType &account_id, interface::types::QuorumType quorum) const
Definition: transaction_template.hpp:263
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
static size_t size()
Definition: permissions.cpp:68
auto build() const
Definition: transaction_template.hpp:296
std::string DescriptionType
Type of the transfer message.
Definition: types.hpp:81
auto subtractAssetQuantity(const interface::types::AssetIdType &asset_id, const std::string &amount) const
Definition: transaction_template.hpp:272
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:75
Role
Definition: permissions.hpp:17
TemplateTransactionBuilder(const SV &validator=SV())
Definition: transaction_template.hpp:90
auto batchMeta(interface::types::BatchType type, std::vector< interface::types::HashType > hashes) const
Definition: transaction_template.hpp:102
description
Definition: setup.py:71
virtual const std::string & hex() const
Definition: blob.cpp:45
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:138
auto createAsset(const interface::types::AssetNameType &asset_name, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) const
Definition: transaction_template.hpp:176
auto addSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:148
Definition: transaction_template.hpp:36
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:187
auto creatorAccountId(const interface::types::AccountIdType &account_id) const
Definition: transaction_template.hpp:93
auto removeSignatory(const interface::types::AccountIdType &account_id, const interface::types::PubkeyType &public_key) const
Definition: transaction_template.hpp:157
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:39
std::string AccountNameType
Type of account name.
Definition: types.hpp:67
Definition: public_key.hpp:16
auto grantPermission(const interface::types::AccountIdType &account_id, interface::permissions::Grantable permission) const
Definition: transaction_template.hpp:232
auto createRole(const interface::types::RoleIdType &role_name, const interface::RolePermissionSet &permissions) const
Definition: transaction_template.hpp:209
BatchType
Definition: types.hpp:83
auto detachRole(const interface::types::AccountIdType &account_id, const interface::types::RoleIdType &role_name) const
Definition: transaction_template.hpp:223
std::string AccountDetailValueType
Type of account detail value.
Definition: types.hpp:77
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:251
std::string AssetNameType
Type of asset name.
Definition: types.hpp:69
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:167
#define DEPRECATED
Definition: swig_keyword_hider.hpp:15
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