Skip to content

Commit cdc092f

Browse files
author
Yura Zarudniy
authored
Fix multihash (#53)
* fix multihash Signed-off-by: Yura Zarudniy <[email protected]> * clean up code Signed-off-by: Yura Zarudniy <[email protected]>
1 parent 4b8265f commit cdc092f

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

include/libp2p/multi/multihash.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ namespace libp2p::multi {
111111
* hash itself
112112
*/
113113
std::vector<uint8_t> data_;
114-
// does not store hash itself, points on data_
115-
gsl::span<const uint8_t> hash_;
114+
uint8_t hash_offset_{}; ///< size of non-hash data from the beginning
116115
HashType type_;
117116
};
118117

src/multi/multihash.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ namespace libp2p::multi {
4444
data_.insert(data_.end(), bytes.begin(), bytes.end());
4545
BOOST_ASSERT(hash.size() <= std::numeric_limits<uint8_t>::max());
4646
data_.push_back(static_cast<uint8_t>(hash.size()));
47-
size_t size = data_.size();
47+
hash_offset_ = data_.size();
4848
data_.insert(data_.end(), hash.begin(), hash.end());
49-
50-
// hash_ points to a data_.begin() + size ... data_.end()
51-
hash_ = gsl::span<const uint8_t>(data_).subspan(size);
5249
}
5350

5451
outcome::result<Multihash> Multihash::create(HashType type,
@@ -93,7 +90,7 @@ namespace libp2p::multi {
9390
}
9491

9592
gsl::span<const uint8_t> Multihash::getHash() const {
96-
return hash_;
93+
return gsl::span<const uint8_t>(data_).subspan(hash_offset_);
9794
}
9895

9996
std::string Multihash::toHex() const {
@@ -113,8 +110,8 @@ namespace libp2p::multi {
113110
}
114111

115112
bool Multihash::operator<(const class libp2p::multi::Multihash &other) const {
116-
return this->type_ < other.type_ ||
117-
(this->type_ == other.type_ && this->data_ < other.data_);
113+
return this->type_ < other.type_
114+
|| (this->type_ == other.type_ && this->data_ < other.data_);
118115
}
119116

120117
} // namespace libp2p::multi

0 commit comments

Comments
 (0)