Skip to content

Commit

Permalink
update: scale
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon committed Feb 9, 2025
1 parent 75b130a commit 70f8192
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 59 deletions.
42 changes: 42 additions & 0 deletions src/scale/jam_scale.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <scale/scale.hpp>

namespace jam {
using scale::decode;
using scale::encode;

template <typename T, typename... Configs>
[[nodiscard]] outcome::result<std::vector<uint8_t>> encode_with_config(
T &&value, Configs &&...configs) {
scale::Encoder<scale::backend::ToBytes> encoder{
std::forward<Configs>(configs)...};
try {
encode(std::forward<T>(value), encoder);
} catch (std::system_error &e) {
return outcome::failure(e.code());
}
return std::move(encoder).backend().to_vector();
}

template <typename T, typename... Configs>
[[nodiscard]] outcome::result<T> decode_with_config(
scale::ConstSpanOfBytes bytes, Configs &&...configs) {
scale::Decoder<scale::backend::FromBytes> decoder{
bytes, std::forward<Configs>(configs)...};
T value;
try {
decode(value, decoder);
} catch (std::system_error &e) {
return outcome::failure(e.code());
}
return std::move(value);
}

} // namespace jam
24 changes: 10 additions & 14 deletions test-vectors/config-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#pragma once

#include <vector>
#include <limits>
#include <vector>

#include <jam_types/config.hpp>
#include <scale/scale.hpp>
#include <scale/jam_scale.hpp>

namespace jam {
template <typename T, typename ConfigField>
Expand All @@ -29,32 +29,28 @@ namespace jam {
using std::vector<T>::emplace;

public:
friend scale::ScaleEncoderStream &operator<<(scale::ScaleEncoderStream &s,
const ConfigVec &v) {
const auto &config = s.getConfig<test_vectors::Config>();
friend void encode(const ConfigVec &v, scale::ScaleEncoder auto &encoder) {
const auto &config = encoder.template getConfig<test_vectors::Config>();
auto n = v.configSize(config);
if (n == std::numeric_limits<decltype(n)>::max()) {
return s << static_cast<const std::vector<T>&>(v);
return encode(static_cast<const std::vector<T> &>(v), encoder);
}
assert(v.size() == n);
for (auto &item : v) {
s << item;
encode(item, encoder);
}
return s;
}

friend scale::ScaleDecoderStream &operator>>(scale::ScaleDecoderStream &s,
ConfigVec &v) {
const auto &config = s.getConfig<test_vectors::Config>();
friend void decode(ConfigVec &v, scale::ScaleDecoder auto &decoder) {
const auto &config = decoder.template getConfig<test_vectors::Config>();
auto n = v.configSize(config);
if (n == std::numeric_limits<decltype(n)>::max()) {
return s >> static_cast<std::vector<T>&>(v);
return decode(static_cast<std::vector<T> &>(v), decoder);
}
v.resize(n);
for (auto &item : v) {
s >> item;
decode(item, decoder);
}
return s;
}
};
} // namespace jam
2 changes: 1 addition & 1 deletion test-vectors/disputes/disputes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ namespace jam::disputes {
for (auto &row_work_report : work_reports) {
if (row_work_report.has_value()) {
auto work_report = mathcal_H(
scale::encode(row_work_report.value().report, config).value());
encode_with_config(row_work_report.value().report, config).value());
if (new_bad_set.contains(work_report)
or new_wonky_set.contains(work_report)) {
row_work_report.reset();
Expand Down
83 changes: 43 additions & 40 deletions test-vectors/vectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <fmt/format.h>
#include <qtils/read_file.hpp>
#include <qtils/test/outcome.hpp>
#include <scale/scale.hpp>
#include <scale/jam_scale.hpp>
#include <test-vectors/config-types.hpp>


Expand Down Expand Up @@ -48,30 +48,31 @@
* @when transition with `input`
* @then get expected `post_state` and `output`
*/
#define GTEST_VECTORS_TEST_TRANSITION(VectorName, NsPart) \
TEST_P(VectorName##Test, Transition) { \
using jam::test_vectors::getTestLabel; \
fmt::println("Test transition for '{}'\n", getTestLabel(path)); \
\
ASSERT_OUTCOME_SUCCESS(raw_data, qtils::readBytes(path)); \
\
ASSERT_OUTCOME_SUCCESS(testcase, \
scale::decode<jam::test_vectors::NsPart::TestCase>( \
raw_data, vectors.config)); \
\
auto [state, output] = jam::NsPart::transition( \
vectors.config, testcase.pre_state, testcase.input); \
Indent indent{1}; \
EXPECT_EQ(state, testcase.post_state) \
<< "Actual and expected states are differ"; \
if (state != testcase.post_state) { \
diff_m(indent, state, testcase.post_state, "state"); \
} \
EXPECT_EQ(output, testcase.output) \
<< "Actual and expected outputs are differ"; \
if (output != testcase.output) { \
diff_m(indent, output, testcase.output, "output"); \
} \
#define GTEST_VECTORS_TEST_TRANSITION(VectorName, NsPart) \
TEST_P(VectorName##Test, Transition) { \
using jam::test_vectors::getTestLabel; \
fmt::println("Test transition for '{}'\n", getTestLabel(path)); \
\
ASSERT_OUTCOME_SUCCESS(raw_data, qtils::readBytes(path)); \
\
ASSERT_OUTCOME_SUCCESS( \
testcase, \
(jam::decode_with_config<jam::test_vectors::NsPart::TestCase>( \
raw_data, vectors.config))); \
\
auto [state, output] = jam::NsPart::transition( \
vectors.config, testcase.pre_state, testcase.input); \
Indent indent{1}; \
EXPECT_EQ(state, testcase.post_state) \
<< "Actual and expected states are differ"; \
if (state != testcase.post_state) { \
diff_m(indent, state, testcase.post_state, "state"); \
} \
EXPECT_EQ(output, testcase.output) \
<< "Actual and expected outputs are differ"; \
if (output != testcase.output) { \
diff_m(indent, output, testcase.output, "output"); \
} \
}

/**
Expand All @@ -80,21 +81,23 @@
* @when decode it and encode back
* @then `actual` result has the same value as `original`
*/
#define GTEST_VECTORS_TEST_REENCODE(VectorName, NsPart) \
TEST_P(VectorName##Test, Reencode) { \
using jam::test_vectors::getTestLabel; \
fmt::println("Test reencode for '{}'\n", getTestLabel(path)); \
\
ASSERT_OUTCOME_SUCCESS(raw_data, qtils::readBytes(path)); \
const auto &original = raw_data; \
\
ASSERT_OUTCOME_SUCCESS(decoded, \
scale::decode<jam::test_vectors::NsPart::TestCase>( \
original, vectors.config)); \
\
ASSERT_OUTCOME_SUCCESS(reencoded, scale::encode(decoded, vectors.config)); \
\
EXPECT_EQ(reencoded, original); \
#define GTEST_VECTORS_TEST_REENCODE(VectorName, NsPart) \
TEST_P(VectorName##Test, Reencode) { \
using jam::test_vectors::getTestLabel; \
fmt::println("Test reencode for '{}'\n", getTestLabel(path)); \
\
ASSERT_OUTCOME_SUCCESS(raw_data, qtils::readBytes(path)); \
const auto &original = raw_data; \
\
ASSERT_OUTCOME_SUCCESS( \
decoded, \
(jam::decode_with_config<jam::test_vectors::NsPart::TestCase>( \
original, vectors.config))); \
\
ASSERT_OUTCOME_SUCCESS( \
reencoded, (jam::encode_with_config(decoded, vectors.config))); \
\
EXPECT_EQ(reencoded, original); \
}

namespace jam::test_vectors {
Expand Down
4 changes: 2 additions & 2 deletions vcpkg-overlay/qtils/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO qdrvm/qtils
REF df89ca404e0e251c89490030a6be57901d9e136a
SHA512 5621274e69780a869890edc97692cb974057c90cb8ef9ed74db5a7c1d97add352c92ad91bb527497243e44bb29d99b656c23312404e1e7572116a3a3d811c29b
REF 16e7c819dd50af2f64e2d319b918d0d815332266
SHA512 02c613ee2870b8b5956f7a0494a5a49b9ba499801541dc16d5fc915480111da56e6dd998ca7d43803197006b9a74ec8201ae9952de97810849a7e208e8f4b0dc
)
vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}")
vcpkg_cmake_install()
Expand Down
4 changes: 2 additions & 2 deletions vcpkg-overlay/scale/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO qdrvm/scale-codec-cpp
REF fb0a319eb7eb79f0ef8c1e01088daa489042d04d
SHA512 fea9aec94fef53ccf84854dccc57bde74f8eddfbcfd03abed30252944d610bf84581be7176541c2e3d20c5ac99e2965e2959ed13296c0ce123c778d4017ea660
REF c59aa2b98eec0b16a6f224740d9c4c0de616818e
SHA512 a4e3f18e8ade1e18fdd2df0791a47c62f856f0ea5dde663fd00c82dc71302e21b7fc99fd29392cd6f131d7ed263eafebccd2311c4f977297174b6332891a938e
)
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
Expand Down

0 comments on commit 70f8192

Please sign in to comment.