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  .appendNamed("created_time", std::to_string(createdTime()))
95  .append(signatures())
96  .finalize();
97  }
98 
99  protected:
108  protected:
110  public:
115  template <typename T>
116  size_t operator()(const T &sig) const {
117  return std::hash<std::string>{}(sig.publicKey().hex());
118  }
119 
126  template <typename T>
127  bool operator()(const T &lhs, const T &rhs) const {
128  return lhs.publicKey() == rhs.publicKey();
129  }
130  };
131 
132  template <typename T>
133  using SignatureSetType =
134  std::unordered_set<T, SignatureSetTypeOps, SignatureSetTypeOps>;
135 
136  protected:
137  static auto makeHash(const types::BlobType &payload) {
138  return HashProvider::makeHash(payload);
139  }
140  };
141 
142  } // namespace interface
143 } // namespace shared_model
144 
145 #endif // IROHA_SIGNABLE_HPP
std::unordered_set< T, SignatureSetTypeOps, SignatureSetTypeOps > SignatureSetType
Definition: signable.hpp:134
Definition: hash.hpp:18
Definition: sha3_256.hpp:15
Definition: blob.hpp:25
PrettyStringBuilder & init(const std::string &name)
Definition: string_builder.cpp:18
Definition: signable.hpp:35
std::string finalize()
Definition: string_builder.cpp:52
PrettyStringBuilder & appendNamed(const std::string &name, const Value &value)
----—— Augmented appending functions. ----—— ///
Definition: string_builder.hpp:53
PrettyStringBuilder & append(const std::string &o)
-------— Single element undecorated append. -------— ///
Definition: string_builder.cpp:38
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
bool operator()(const T &lhs, const T &rhs) const
Definition: signable.hpp:127
Definition: string_builder.hpp:18
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
const char * PublicKey
Definition: iroha_conf_literals.cpp:47
Definition: signed.hpp:17
static auto makeHash(const types::BlobType &payload)
Definition: signable.hpp:137
Definition: public_key.hpp:16
Definition: model_primitive.hpp:22
std::string toString() const override
Definition: signable.hpp:91
Definition: command_executor.hpp:12
bool equalsByValue(const Model &rhs) const
Definition: signable.hpp:83
size_t operator()(const T &sig) const
Definition: signable.hpp:116
bool operator==(const Model &rhs) const override
Definition: signable.hpp:70
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:64