Skip to content

Commit

Permalink
6
Browse files Browse the repository at this point in the history
Signed-off-by: iceseer <[email protected]>
  • Loading branch information
iceseer committed Nov 22, 2024
1 parent 977c014 commit 97087fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
20 changes: 7 additions & 13 deletions core/scale/encoder/external_scale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace kagome::scale {
using uint128_t = ::scale::uint128_t;

using ::scale::decode;
using ::scale::append_or_new_vec;

constexpr void encode(const Invocable auto &func,
const primitives::BlockHeader &bh);
Expand Down Expand Up @@ -194,6 +195,9 @@ namespace kagome::scale {
constexpr void encode(const Invocable auto &func, const std::string_view &v);

constexpr void encode(const Invocable auto &func, const std::string &v);

template <typename T, size_t S>
constexpr void encode(const Invocable auto &func, const std::span<T, S> &c);

template <typename T>
constexpr void encode(const Invocable auto &func,
Expand Down Expand Up @@ -235,17 +239,7 @@ namespace kagome::scale {
constexpr void encode(const Invocable auto &func,
const T &t,
const Args &...args);

template <typename... Args>
outcome::result<std::vector<uint8_t>> encode(const Args &...args) {
std::vector<uint8_t> res;
kagome::scale::encode(
[&](const uint8_t *const val, size_t count) {
if (count != 0ull) {
res.insert(res.end(), &val[0], &val[count]);
}
},
args...);
return res;
}

template <typename... Args>
outcome::result<std::vector<uint8_t>> encode(const Args &...args);
} // namespace kagome::scale
37 changes: 32 additions & 5 deletions core/scale/encoder/primitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,8 @@ namespace kagome::scale {
kagome::scale::encode(func, c.begin(), c.end());
}

template <typename T, ssize_t S>
template <typename T, size_t S>
constexpr void encode(const Invocable auto &func, const std::span<T, S> &c) {
if constexpr (S == -1) {
kagome::scale::encodeCompact(func, c.size());
kagome::scale::encode(func, c.begin(), c.end());
} else {
using E = std::decay_t<T>;
if constexpr (std::is_integral_v<E> && sizeof(E) == 1u) {
putByte(func, c.data(), c.size());
Expand All @@ -302,6 +298,24 @@ namespace kagome::scale {
encode(func, e);
}
}
}

template <typename T, ssize_t S>
constexpr void encode(const Invocable auto &func, const std::span<T, S> &c) {
if constexpr (S == -1) {
kagome::scale::encodeCompact(func, c.size());
kagome::scale::encode(func, c.begin(), c.end());
} else {
kagome::scale::encode<T, size_t(S)>(func, c);

//using E = std::decay_t<T>;
//if constexpr (std::is_integral_v<E> && sizeof(E) == 1u) {
// putByte(func, c.data(), c.size());
//} else {
// for (const auto &e : c) {
// encode(func, e);
// }
//}
}
}

Expand Down Expand Up @@ -581,4 +595,17 @@ namespace kagome::scale {
kagome::scale::encode(func, t);
kagome::scale::encode(func, args...);
}

template <typename... Args>
outcome::result<std::vector<uint8_t>> encode(const Args &...args) {
std::vector<uint8_t> res;
kagome::scale::encode(
[&](const uint8_t *const val, size_t count) {
if (count != 0ull) {
res.insert(res.end(), &val[0], &val[count]);
}
},
args...);
return res;
}
} // namespace kagome::scale
2 changes: 1 addition & 1 deletion core/scale/std_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <variant>

#include <scale/scale.hpp>
#include "scale/encoder/external_scale.hpp"

namespace scale {

Expand Down
3 changes: 2 additions & 1 deletion core/utils/kagome_db_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "storage/trie/serialization/trie_serializer_impl.hpp"
#include "storage/trie_pruner/impl/trie_pruner_impl.hpp"
#include "utils/profiler.hpp"
#include "scale/kagome_scale.hpp"

namespace di = boost::di;
using namespace kagome; // NOLINT(google-build-using-namespace)
Expand Down Expand Up @@ -362,7 +363,7 @@ int db_editor_main(int argc, const char **argv) {
runtime_upgrade_data{};
runtime_upgrade_data.emplace_back(last_finalized_block,
last_finalized_block_header.state_root);
auto encoded_res = check(scale::encode(runtime_upgrade_data));
auto encoded_res = check(kagome::scale::encode(runtime_upgrade_data));
check(buffer_storage->put(storage::kRuntimeHashesLookupKey,
common::Buffer(encoded_res.value())))
.value();
Expand Down

0 comments on commit 97087fb

Please sign in to comment.