Skip to content

Commit

Permalink
Refactoring/scale encoding (#1782)
Browse files Browse the repository at this point in the history
* scale
  • Loading branch information
iceseer authored Nov 14, 2023
1 parent bf165c0 commit bd93022
Show file tree
Hide file tree
Showing 5 changed files with 769 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/network/helpers/scale_message_read_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace kagome::network {
static auto empty = std::make_shared<std::vector<uint8_t>>();
raw = empty;
}
auto msg_res = scale::decode<MsgType>(*raw);
auto msg_res = ::scale::decode<MsgType>(*raw);
if (!msg_res) {
return cb(outcome::failure(msg_res.error()));
}
Expand Down
17 changes: 17 additions & 0 deletions core/primitives/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ namespace kagome::math {
return res;
}

template <typename T,
std::enable_if_t<std::is_integral_v<std::decay_t<T>>, bool> = true>
constexpr auto toLE(const T &value) {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
constexpr size_t size = sizeof(std::decay_t<T>);
if constexpr (size == 8) {
return __builtin_bswap64(value);
} else if constexpr (size == 4) {
return __builtin_bswap32(value);
} else if constexpr (size == 2) {
return __builtin_bswap16(value);
}
#endif
return value;
}

inline bool isPowerOf2(size_t x) {
return ((x > 0ull) && ((x & (x - 1ull)) == 0));
}
Expand All @@ -46,4 +62,5 @@ namespace kagome::math {
const auto p = k == 0ull ? 0ull : 64ull - __builtin_clzll(k);
return (1ull << p);
}

} // namespace kagome::math
Loading

0 comments on commit bd93022

Please sign in to comment.