hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
proto_common_objects_factory.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
7 #define IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
8 
9 #include <regex>
10 
17 #include "common/result.hpp"
19 #include "primitive.pb.h"
20 #include "validators/answer.hpp"
21 
22 namespace shared_model {
23  namespace proto {
29  template <typename Validator>
31  public:
33  const interface::types::AddressType &address,
34  const interface::types::PubkeyType &public_key) override {
35  iroha::protocol::Peer peer;
36  peer.set_address(address);
37  peer.set_peer_key(public_key.hex());
38  auto proto_peer = std::make_unique<Peer>(std::move(peer));
39 
40  auto errors =
41  validate(*proto_peer, [this](const auto &peer, auto &reasons) {
42  validator_.validatePeer(reasons, peer);
43  });
44 
45  if (errors) {
46  return iroha::expected::makeError(errors.reason());
47  }
48 
49  return iroha::expected::makeValue<std::unique_ptr<interface::Peer>>(
50  std::move(proto_peer));
51  }
52 
54  const interface::types::AccountIdType &account_id,
55  const interface::types::DomainIdType &domain_id,
57  const interface::types::JsonType &jsonData) override {
58  iroha::protocol::Account account;
59  account.set_account_id(account_id);
60  account.set_domain_id(domain_id);
61  account.set_quorum(quorum);
62  account.set_json_data(jsonData);
63 
64  auto proto_account = std::make_unique<Account>(std::move(account));
65 
66  auto errors = validate(
67  *proto_account, [this](const auto &account, auto &reasons) {
68  validator_.validateAccountId(reasons, account.accountId());
69  validator_.validateDomainId(reasons, account.domainId());
70  validator_.validateQuorum(reasons, account.quorum());
71  });
72 
73  if (errors) {
74  return iroha::expected::makeError(errors.reason());
75  }
76 
77  return iroha::expected::makeValue<std::unique_ptr<interface::Account>>(
78  std::move(proto_account));
79  }
80 
83  const interface::types::AssetIdType &asset_id,
84  const interface::Amount &balance) override {
85  iroha::protocol::AccountAsset asset;
86  asset.set_account_id(account_id);
87  asset.set_asset_id(asset_id);
88  asset.set_balance(balance.toStringRepr());
89 
90  auto proto_asset = std::make_unique<AccountAsset>(std::move(asset));
91 
92  auto errors =
93  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
94  validator_.validateAccountId(reasons, asset.accountId());
95  validator_.validateAssetId(reasons, asset.assetId());
96  });
97 
98  if (errors) {
99  return iroha::expected::makeError(errors.reason());
100  }
101 
102  return iroha::expected::makeValue<
103  std::unique_ptr<interface::AccountAsset>>(std::move(proto_asset));
104  }
105 
107  const interface::types::AssetIdType &asset_id,
108  const interface::types::DomainIdType &domain_id,
109  interface::types::PrecisionType precision) override {
110  iroha::protocol::Asset asset;
111  asset.set_asset_id(asset_id);
112  asset.set_domain_id(domain_id);
113  asset.set_precision(precision);
114 
115  auto proto_asset = std::make_unique<Asset>(std::move(asset));
116 
117  auto errors =
118  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
119  validator_.validateAssetId(reasons, asset.assetId());
120  validator_.validateDomainId(reasons, asset.domainId());
121  });
122 
123  if (errors) {
124  return iroha::expected::makeError(errors.reason());
125  }
126 
127  return iroha::expected::makeValue<std::unique_ptr<interface::Asset>>(
128  std::move(proto_asset));
129  }
130 
132  const interface::types::DomainIdType &domain_id,
133  const interface::types::RoleIdType &default_role) override {
134  iroha::protocol::Domain domain;
135  domain.set_domain_id(domain_id);
136  domain.set_default_role(default_role);
137 
138  auto proto_domain = std::make_unique<Domain>(std::move(domain));
139 
140  auto errors =
141  validate(*proto_domain, [this](const auto &domain, auto &reason) {
142  validator_.validateDomainId(reason, domain.domainId());
143  validator_.validateRoleId(reason, domain.defaultRole());
144  });
145 
146  if (errors) {
147  return iroha::expected::makeError(errors.reason());
148  }
149 
150  return iroha::expected::makeValue<std::unique_ptr<interface::Domain>>(
151  std::move(proto_domain));
152  }
153 
155  const interface::types::PubkeyType &key,
156  const interface::Signature::SignedType &signed_data) override {
157  iroha::protocol::Signature signature;
158  signature.set_public_key(key.hex());
159  signature.set_signature(signed_data.hex());
160 
161  auto proto_singature =
162  std::make_unique<Signature>(std::move(signature));
163 
164  auto errors = validate(
165  *proto_singature, [this](const auto &signature, auto &reason) {
166  validator_.validatePubkey(reason, signature.publicKey());
167  });
168 
169  if (errors) {
170  return iroha::expected::makeError(errors.reason());
171  }
172 
173  return iroha::expected::makeValue<
174  std::unique_ptr<interface::Signature>>(std::move(proto_singature));
175  }
176 
177  private:
185  template <typename T, typename ValidationFunc>
186  validation::Answer validate(const T &o, ValidationFunc &&f) const {
189  f(o, reasons);
190  if (not reasons.second.empty()) {
191  errors.addReason(std::move(reasons));
192  }
193  return errors;
194  }
195 
196  Validator validator_;
197  };
198  } // namespace proto
199 } // namespace shared_model
200 
201 #endif // IROHA_PROTO_COMMON_OBJECTS_FACTORY_HPP
FactoryResult< std::unique_ptr< interface::Asset > > createAsset(const interface::types::AssetIdType &asset_id, const interface::types::DomainIdType &domain_id, interface::types::PrecisionType precision) override
Definition: proto_common_objects_factory.hpp:106
std::string AddressType
Type of peer address.
Definition: types.hpp:43
std::string toStringRepr() const
Definition: amount.cpp:54
std::string DomainIdType
Iroha domain id type.
Definition: types.hpp:51
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
Definition: amount.hpp:20
FactoryResult< std::unique_ptr< interface::AccountAsset > > createAccountAsset(const interface::types::AccountIdType &account_id, const interface::types::AssetIdType &asset_id, const interface::Amount &balance) override
Definition: proto_common_objects_factory.hpp:82
iroha::expected::Result< T, std::string > FactoryResult
Definition: common_objects_factory.hpp:29
FactoryResult< std::unique_ptr< interface::Domain > > createDomain(const interface::types::DomainIdType &domain_id, const interface::types::RoleIdType &default_role) override
Definition: proto_common_objects_factory.hpp:131
Definition: proto_common_objects_factory.hpp:30
Definition: common_objects_factory.hpp:26
virtual const std::string & hex() const
Definition: blob.cpp:45
std::string AccountIdType
Type of account id.
Definition: types.hpp:37
FactoryResult< std::unique_ptr< interface::Signature > > createSignature(const interface::types::PubkeyType &key, const interface::Signature::SignedType &signed_data) override
Definition: proto_common_objects_factory.hpp:154
Definition: signed.hpp:17
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:39
Definition: public_key.hpp:16
FactoryResult< std::unique_ptr< interface::Peer > > createPeer(const interface::types::AddressType &address, const interface::types::PubkeyType &public_key) override
Definition: proto_common_objects_factory.hpp:32
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
Definition: command_executor.hpp:12
FactoryResult< std::unique_ptr< interface::Account > > createAccount(const interface::types::AccountIdType &account_id, const interface::types::DomainIdType &domain_id, interface::types::QuorumType quorum, const interface::types::JsonType &jsonData) override
Definition: proto_common_objects_factory.hpp:53
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:59
std::string JsonType
Type of JSON data.
Definition: types.hpp:73
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:49
Definition: answer.hpp:28
std::string AssetIdType
Type of asset id.
Definition: types.hpp:53