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 {
88  if (hash_ == boost::none) {
89  hash_.emplace(HashProvider::makeHash(payload()));
90  }
91  return *hash_;
92  }
93 
94  // ------------------------| Primitive override |-------------------------
95 
96  std::string toString() const override {
98  .init("Signable")
99  .append("created_time", std::to_string(createdTime()))
100  .appendAll(signatures(),
101  [](auto &signature) { return signature.toString(); })
102  .finalize();
103  }
104 
105  protected:
114  protected:
116  public:
121  template <typename T>
122  size_t operator()(const T &sig) const {
123  return std::hash<std::string>{}(sig.publicKey().hex());
124  }
125 
132  template <typename T>
133  bool operator()(const T &lhs, const T &rhs) const {
134  return lhs.publicKey() == rhs.publicKey();
135  }
136  };
137 
138  template <typename T>
139  using SignatureSetType =
140  std::unordered_set<T, SignatureSetTypeOps, SignatureSetTypeOps>;
141 
142  private:
143  mutable boost::optional<types::HashType> hash_;
144  };
145 
146  } // namespace interface
147 } // namespace shared_model
148 
149 #endif // IROHA_SIGNABLE_HPP
std::unordered_set< T, SignatureSetTypeOps, SignatureSetTypeOps > SignatureSetType
Definition: signable.hpp:140
Definition: hash.hpp:18
std::string to_string(const Collection &collection, Lambda transform)
Definition: logger.hpp:78
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
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
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
virtual const types::HashType & hash() const
Definition: signable.hpp:87
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:122
Definition: signed.hpp:17
bool operator()(const T &lhs, const T &rhs) const
Definition: signable.hpp:133
Definition: public_key.hpp:16
Definition: model_primitive.hpp:23
std::string toString() const override
Definition: signable.hpp:96
Definition: command_executor.hpp:12
const std::string end
Definition: logger.cpp:9
bool operator==(const Model &rhs) const override
Definition: signable.hpp:70
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:61