hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
signable.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_SIGNABLE_HPP
7 #define IROHA_SIGNABLE_HPP
8 
10 
11 #include <boost/functional/hash.hpp>
12 #include <boost/optional.hpp>
13 #include <unordered_set>
18 #include "utils/string_builder.hpp"
19 
20 namespace shared_model {
21 
22  namespace crypto {
23  class Signed;
24  class PublicKey;
25  } // namespace crypto
26 
27  namespace interface {
28 
33  template <typename Model,
34  typename HashProvider = shared_model::crypto::Sha3_256>
35  class Signable : public ModelPrimitive<Model> {
36  public:
40  virtual types::SignatureRangeType signatures() const = 0;
41 
47  virtual bool addSignature(const crypto::Signed &signed_blob,
48  const crypto::PublicKey &public_key) = 0;
49 
53  virtual types::TimestampType createdTime() const = 0;
54 
58  virtual const types::BlobType &payload() const = 0;
59 
63  virtual const types::BlobType &blob() const = 0;
64 
70  bool operator==(const Model &rhs) const override {
71  return equalsByValue(rhs)
72  // is_permutation consumes ~O(N^2)
73  and std::is_permutation(signatures().begin(),
74  signatures().end(),
75  rhs.signatures().begin());
76  }
77 
83  bool equalsByValue(const Model &rhs) const {
84  return this->hash() == rhs.hash();
85  }
86 
87  virtual const types::HashType &hash() const = 0;
88 
89  // ------------------------| Primitive override |-------------------------
90 
91  std::string toString() const override {
93  .init("Signable")
94  .append("created_time", std::to_string(createdTime()))
95  .appendAll(signatures(),
96  [](auto &signature) { return signature.toString(); })
97  .finalize();
98  }
99 
100  protected:
109  protected:
111  public:
116  template <typename T>
117  size_t operator()(const T &sig) const {
118  return std::hash<std::string>{}(sig.publicKey().hex());
119  }
120 
127  template <typename T>
128  bool operator()(const T &lhs, const T &rhs) const {
129  return lhs.publicKey() == rhs.publicKey();
130  }
131  };
132 
133  template <typename T>
134  using SignatureSetType =
135  std::unordered_set<T, SignatureSetTypeOps, SignatureSetTypeOps>;
136 
137  protected:
138  static auto makeHash(const types::BlobType &payload) {
139  return HashProvider::makeHash(payload);
140  }
141  };
142 
143  } // namespace interface
144 } // namespace shared_model
145 
146 #endif // IROHA_SIGNABLE_HPP
std::unordered_set< T, SignatureSetTypeOps, SignatureSetTypeOps > SignatureSetType
Definition: signable.hpp:135
Definition: hash.hpp:18
std::string to_string(const Collection &collection, Lambda transform)
Definition: logger.hpp:132
Definition: sha3_256.hpp:15
Definition: blob.hpp:24
PrettyStringBuilder & init(const std::string &name)
Definition: string_builder.cpp:18
Definition: signable.hpp:35
bool equalsByValue(const Model &rhs) const
Definition: signable.hpp:83
boost::any_range< interface::Signature, boost::forward_traversal_tag, const Signature & > SignatureRangeType
Type of signature range, which returns when signatures are invoked.
Definition: range_types.hpp:24
PrettyStringBuilder & appendAll(Collection &&c, Transform &&t)
Definition: string_builder.hpp:56
Definition: string_builder.hpp:16
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
const char * PublicKey
Definition: iroha_conf_literals.cpp:33
PrettyStringBuilder & append(const std::string &name, const std::string &value)
Definition: string_builder.cpp:36
size_t operator()(const T &sig) const
Definition: signable.hpp:117
Definition: signed.hpp:17
bool operator()(const T &lhs, const T &rhs) const
Definition: signable.hpp:128
static auto makeHash(const types::BlobType &payload)
Definition: signable.hpp:138
Definition: public_key.hpp:16
Definition: model_primitive.hpp:23
std::string toString() const override
Definition: signable.hpp:91
Definition: command_executor.hpp:12
bool operator==(const Model &rhs) const override
Definition: signable.hpp:70
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:61