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"
22 
23 namespace shared_model {
24  namespace proto {
30  template <typename Validator>
32  public:
34  std::shared_ptr<validation::ValidatorsConfig> config)
35  : validator_(config) {}
36 
38  const interface::types::AddressType &address,
39  const interface::types::PubkeyType &public_key,
40  const boost::optional<interface::types::TLSCertificateType>
41  &tls_certificate = boost::none) override {
42  iroha::protocol::Peer peer;
43  peer.set_address(address);
44  peer.set_peer_key(public_key.hex());
45  if (tls_certificate) {
46  peer.set_tls_certificate(*tls_certificate);
47  }
48  auto proto_peer = std::make_unique<Peer>(std::move(peer));
49 
50  auto errors =
51  validate(*proto_peer, [this](const auto &peer, auto &reasons) {
52  validator_.validatePeer(reasons, peer);
53  });
54 
55  if (errors) {
56  return iroha::expected::makeError(errors.reason());
57  }
58 
59  return iroha::expected::makeValue<std::unique_ptr<interface::Peer>>(
60  std::move(proto_peer));
61  }
62 
64  const interface::types::AccountIdType &account_id,
65  const interface::types::DomainIdType &domain_id,
67  const interface::types::JsonType &jsonData) override {
68  iroha::protocol::Account account;
69  account.set_account_id(account_id);
70  account.set_domain_id(domain_id);
71  account.set_quorum(quorum);
72  account.set_json_data(jsonData);
73 
74  auto proto_account = std::make_unique<Account>(std::move(account));
75 
76  auto errors = validate(
77  *proto_account, [this](const auto &account, auto &reasons) {
78  validator_.validateAccountId(reasons, account.accountId());
79  validator_.validateDomainId(reasons, account.domainId());
80  validator_.validateQuorum(reasons, account.quorum());
81  });
82 
83  if (errors) {
84  return iroha::expected::makeError(errors.reason());
85  }
86 
87  return iroha::expected::makeValue<std::unique_ptr<interface::Account>>(
88  std::move(proto_account));
89  }
90 
93  const interface::types::AssetIdType &asset_id,
94  const interface::Amount &balance) override {
95  iroha::protocol::AccountAsset asset;
96  asset.set_account_id(account_id);
97  asset.set_asset_id(asset_id);
98  asset.set_balance(balance.toStringRepr());
99 
100  auto proto_asset = std::make_unique<AccountAsset>(std::move(asset));
101 
102  auto errors =
103  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
104  validator_.validateAccountId(reasons, asset.accountId());
105  validator_.validateAssetId(reasons, asset.assetId());
106  });
107 
108  if (errors) {
109  return iroha::expected::makeError(errors.reason());
110  }
111 
112  return iroha::expected::makeValue<
113  std::unique_ptr<interface::AccountAsset>>(std::move(proto_asset));
114  }
115 
117  const interface::types::AssetIdType &asset_id,
118  const interface::types::DomainIdType &domain_id,
119  interface::types::PrecisionType precision) override {
120  iroha::protocol::Asset asset;
121  asset.set_asset_id(asset_id);
122  asset.set_domain_id(domain_id);
123  asset.set_precision(precision);
124 
125  auto proto_asset = std::make_unique<Asset>(std::move(asset));
126 
127  auto errors =
128  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
129  validator_.validateAssetId(reasons, asset.assetId());
130  validator_.validateDomainId(reasons, asset.domainId());
131  });
132 
133  if (errors) {
134  return iroha::expected::makeError(errors.reason());
135  }
136 
137  return iroha::expected::makeValue<std::unique_ptr<interface::Asset>>(
138  std::move(proto_asset));
139  }
140 
142  const interface::types::DomainIdType &domain_id,
143  const interface::types::RoleIdType &default_role) override {
144  iroha::protocol::Domain domain;
145  domain.set_domain_id(domain_id);
146  domain.set_default_role(default_role);
147 
148  auto proto_domain = std::make_unique<Domain>(std::move(domain));
149 
150  auto errors =
151  validate(*proto_domain, [this](const auto &domain, auto &reason) {
152  validator_.validateDomainId(reason, domain.domainId());
153  validator_.validateRoleId(reason, domain.defaultRole());
154  });
155 
156  if (errors) {
157  return iroha::expected::makeError(errors.reason());
158  }
159 
160  return iroha::expected::makeValue<std::unique_ptr<interface::Domain>>(
161  std::move(proto_domain));
162  }
163 
165  const interface::types::PubkeyType &key,
166  const interface::Signature::SignedType &signed_data) override {
167  iroha::protocol::Signature signature;
168  signature.set_public_key(key.hex());
169  signature.set_signature(signed_data.hex());
170 
171  auto proto_singature =
172  std::make_unique<Signature>(std::move(signature));
173 
174  auto errors = validate(
175  *proto_singature, [this](const auto &signature, auto &reason) {
176  validator_.validatePubkey(reason, signature.publicKey());
177  });
178 
179  if (errors) {
180  return iroha::expected::makeError(errors.reason());
181  }
182 
183  return iroha::expected::makeValue<
184  std::unique_ptr<interface::Signature>>(std::move(proto_singature));
185  }
186 
187  private:
195  template <typename T, typename ValidationFunc>
196  validation::Answer validate(const T &o, ValidationFunc &&f) const {
199  f(o, reasons);
200  if (not reasons.second.empty()) {
201  errors.addReason(std::move(reasons));
202  }
203  return errors;
204  }
205 
206  Validator validator_;
207  };
208  } // namespace proto
209 } // namespace shared_model
210 
211 #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:116
std::string AddressType
Type of peer address.
Definition: types.hpp:45
std::string DomainIdType
Iroha domain id type.
Definition: types.hpp:53
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
Definition: amount.hpp:19
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:92
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:141
Definition: proto_common_objects_factory.hpp:31
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:39
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:164
ProtoCommonObjectsFactory(std::shared_ptr< validation::ValidatorsConfig > config)
Definition: proto_common_objects_factory.hpp:33
Definition: signed.hpp:17
uint8_t PrecisionType
Type of precision.
Definition: types.hpp:41
Definition: public_key.hpp:16
FactoryResult< std::unique_ptr< interface::Peer > > createPeer(const interface::types::AddressType &address, const interface::types::PubkeyType &public_key, const boost::optional< interface::types::TLSCertificateType > &tls_certificate=boost::none) override
Definition: proto_common_objects_factory.hpp:37
std::string toStringRepr() const
Definition: amount.cpp:69
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:63
uint16_t QuorumType
Type of Quorum used in transaction and set quorum.
Definition: types.hpp:62
std::string JsonType
Type of JSON data.
Definition: types.hpp:74
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:51
Definition: answer.hpp:28
std::string AssetIdType
Type of asset id.
Definition: types.hpp:55