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 <optional>
13 #include <unordered_set>
19 #include "utils/string_builder.hpp"
20 
21 namespace shared_model {
22 
23  namespace interface {
24 
29  template <typename Model,
30  typename HashProvider = shared_model::crypto::Sha3_256>
31  class Signable : public ModelPrimitive<Model> {
32  public:
36  virtual types::SignatureRangeType signatures() const = 0;
37 
42  virtual bool addSignature(types::SignedHexStringView signed_blob,
43  types::PublicKeyHexStringView public_key) = 0;
44 
48  virtual types::TimestampType createdTime() const = 0;
49 
53  virtual const types::BlobType &payload() const = 0;
54 
58  virtual const types::BlobType &blob() const = 0;
59 
65  bool operator==(const Model &rhs) const override {
66  return equalsByValue(rhs)
67  // is_permutation consumes ~O(N^2)
68  and std::is_permutation(signatures().begin(),
69  signatures().end(),
70  rhs.signatures().begin(),
71  rhs.signatures().end());
72  }
73 
79  bool equalsByValue(const Model &rhs) const {
80  return this->hash() == rhs.hash();
81  }
82 
83  virtual const types::HashType &hash() const = 0;
84 
85  // ------------------------| Primitive override |-------------------------
86 
87  std::string toString() const override {
89  .init("Signable")
90  .appendNamed("created_time", std::to_string(createdTime()))
91  .append(signatures())
92  .finalize();
93  }
94 
95  protected:
104  protected:
106  public:
111  template <typename T>
112  size_t operator()(const T &sig) const {
113  return std::hash<std::string>{}(sig.publicKey());
114  }
115 
122  template <typename T>
123  bool operator()(const T &lhs, const T &rhs) const {
124  return lhs.publicKey() == rhs.publicKey();
125  }
126  };
127 
128  template <typename T>
129  using SignatureSetType =
130  std::unordered_set<T, SignatureSetTypeOps, SignatureSetTypeOps>;
131 
132  protected:
133  static auto makeHash(const types::BlobType &payload) {
134  return HashProvider::makeHash(payload);
135  }
136  };
137 
138  } // namespace interface
139 } // namespace shared_model
140 
141 #endif // IROHA_SIGNABLE_HPP
std::unordered_set< T, SignatureSetTypeOps, SignatureSetTypeOps > SignatureSetType
Definition: signable.hpp:130
Definition: hash.hpp:18
Definition: sha3_256.hpp:15
Definition: blob.hpp:27
PrettyStringBuilder & init(const std::string &name)
Definition: string_builder.cpp:18
Definition: signable.hpp:31
std::string finalize()
Definition: string_builder.cpp:44
virtual const types::BlobType & payload() const =0
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
virtual const types::BlobType & blob() const =0
bool operator()(const T &lhs, const T &rhs) const
Definition: signable.hpp:123
virtual types::TimestampType createdTime() const =0
Definition: string_builder.hpp:18
virtual const types::HashType & hash() const =0
virtual bool addSignature(types::SignedHexStringView signed_blob, types::PublicKeyHexStringView public_key)=0
static auto makeHash(const types::BlobType &payload)
Definition: signable.hpp:133
Definition: model_primitive.hpp:22
virtual types::SignatureRangeType signatures() const =0
std::string toString() const override
Definition: signable.hpp:87
Definition: command_executor.hpp:12
PrettyStringBuilder & appendNamed(const Name &name, const Value &value)
----—— Augmented appending functions. ----—— ///
Definition: string_builder.hpp:53
bool equalsByValue(const Model &rhs) const
Definition: signable.hpp:79
size_t operator()(const T &sig) const
Definition: signable.hpp:112
bool operator==(const Model &rhs) const override
Definition: signable.hpp:65
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:67