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) override {
40  iroha::protocol::Peer peer;
41  peer.set_address(address);
42  peer.set_peer_key(public_key.hex());
43  auto proto_peer = std::make_unique<Peer>(std::move(peer));
44 
45  auto errors =
46  validate(*proto_peer, [this](const auto &peer, auto &reasons) {
47  validator_.validatePeer(reasons, peer);
48  });
49 
50  if (errors) {
51  return iroha::expected::makeError(errors.reason());
52  }
53 
54  return iroha::expected::makeValue<std::unique_ptr<interface::Peer>>(
55  std::move(proto_peer));
56  }
57 
59  const interface::types::AccountIdType &account_id,
60  const interface::types::DomainIdType &domain_id,
62  const interface::types::JsonType &jsonData) override {
63  iroha::protocol::Account account;
64  account.set_account_id(account_id);
65  account.set_domain_id(domain_id);
66  account.set_quorum(quorum);
67  account.set_json_data(jsonData);
68 
69  auto proto_account = std::make_unique<Account>(std::move(account));
70 
71  auto errors = validate(
72  *proto_account, [this](const auto &account, auto &reasons) {
73  validator_.validateAccountId(reasons, account.accountId());
74  validator_.validateDomainId(reasons, account.domainId());
75  validator_.validateQuorum(reasons, account.quorum());
76  });
77 
78  if (errors) {
79  return iroha::expected::makeError(errors.reason());
80  }
81 
82  return iroha::expected::makeValue<std::unique_ptr<interface::Account>>(
83  std::move(proto_account));
84  }
85 
88  const interface::types::AssetIdType &asset_id,
89  const interface::Amount &balance) override {
90  iroha::protocol::AccountAsset asset;
91  asset.set_account_id(account_id);
92  asset.set_asset_id(asset_id);
93  asset.set_balance(balance.toStringRepr());
94 
95  auto proto_asset = std::make_unique<AccountAsset>(std::move(asset));
96 
97  auto errors =
98  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
99  validator_.validateAccountId(reasons, asset.accountId());
100  validator_.validateAssetId(reasons, asset.assetId());
101  });
102 
103  if (errors) {
104  return iroha::expected::makeError(errors.reason());
105  }
106 
107  return iroha::expected::makeValue<
108  std::unique_ptr<interface::AccountAsset>>(std::move(proto_asset));
109  }
110 
112  const interface::types::AssetIdType &asset_id,
113  const interface::types::DomainIdType &domain_id,
114  interface::types::PrecisionType precision) override {
115  iroha::protocol::Asset asset;
116  asset.set_asset_id(asset_id);
117  asset.set_domain_id(domain_id);
118  asset.set_precision(precision);
119 
120  auto proto_asset = std::make_unique<Asset>(std::move(asset));
121 
122  auto errors =
123  validate(*proto_asset, [this](const auto &asset, auto &reasons) {
124  validator_.validateAssetId(reasons, asset.assetId());
125  validator_.validateDomainId(reasons, asset.domainId());
126  });
127 
128  if (errors) {
129  return iroha::expected::makeError(errors.reason());
130  }
131 
132  return iroha::expected::makeValue<std::unique_ptr<interface::Asset>>(
133  std::move(proto_asset));
134  }
135 
137  const interface::types::DomainIdType &domain_id,
138  const interface::types::RoleIdType &default_role) override {
139  iroha::protocol::Domain domain;
140  domain.set_domain_id(domain_id);
141  domain.set_default_role(default_role);
142 
143  auto proto_domain = std::make_unique<Domain>(std::move(domain));
144 
145  auto errors =
146  validate(*proto_domain, [this](const auto &domain, auto &reason) {
147  validator_.validateDomainId(reason, domain.domainId());
148  validator_.validateRoleId(reason, domain.defaultRole());
149  });
150 
151  if (errors) {
152  return iroha::expected::makeError(errors.reason());
153  }
154 
155  return iroha::expected::makeValue<std::unique_ptr<interface::Domain>>(
156  std::move(proto_domain));
157  }
158 
160  const interface::types::PubkeyType &key,
161  const interface::Signature::SignedType &signed_data) override {
162  iroha::protocol::Signature signature;
163  signature.set_public_key(key.hex());
164  signature.set_signature(signed_data.hex());
165 
166  auto proto_singature =
167  std::make_unique<Signature>(std::move(signature));
168 
169  auto errors = validate(
170  *proto_singature, [this](const auto &signature, auto &reason) {
171  validator_.validatePubkey(reason, signature.publicKey());
172  });
173 
174  if (errors) {
175  return iroha::expected::makeError(errors.reason());
176  }
177 
178  return iroha::expected::makeValue<
179  std::unique_ptr<interface::Signature>>(std::move(proto_singature));
180  }
181 
182  private:
190  template <typename T, typename ValidationFunc>
191  validation::Answer validate(const T &o, ValidationFunc &&f) const {
194  f(o, reasons);
195  if (not reasons.second.empty()) {
196  errors.addReason(std::move(reasons));
197  }
198  return errors;
199  }
200 
201  Validator validator_;
202  };
203  } // namespace proto
204 } // namespace shared_model
205 
206 #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:111
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: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:87
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:136
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:159
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) override
Definition: proto_common_objects_factory.hpp:37
std::string toStringRepr() const
Definition: amount.cpp:68
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:58
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