From d67c98ff357cf626c3519d9e8915456ed455503c Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 9 Aug 2024 21:36:08 +0500 Subject: [PATCH 01/10] vcpkg, asn1 scale Signed-off-by: turuslan --- .clang-format | 27 +++ .gitignore | 2 + CMakeLists.txt | 8 + CMakePresets.json | 14 ++ python/README.md | 9 + python/asn1.py | 267 ++++++++++++++++++++++++++++ python/requirements.txt | 1 + test-vectors/CMakeLists.txt | 2 + test-vectors/safrole/.gitignore | 2 + test-vectors/safrole/CMakeLists.txt | 26 +++ test-vectors/safrole/types.test.cpp | 52 ++++++ vcpkg-configuration.json | 17 ++ vcpkg-overlay/qtils/portfile.cmake | 11 ++ vcpkg-overlay/qtils/vcpkg.json | 11 ++ vcpkg-overlay/scale/portfile.cmake | 11 ++ vcpkg-overlay/scale/vcpkg.json | 12 ++ vcpkg.json | 5 + 17 files changed, 477 insertions(+) create mode 100644 .clang-format create mode 100644 CMakePresets.json create mode 100644 python/README.md create mode 100755 python/asn1.py create mode 100644 python/requirements.txt create mode 100644 test-vectors/safrole/.gitignore create mode 100644 test-vectors/safrole/types.test.cpp create mode 100644 vcpkg-configuration.json create mode 100644 vcpkg-overlay/qtils/portfile.cmake create mode 100644 vcpkg-overlay/qtils/vcpkg.json create mode 100644 vcpkg-overlay/scale/portfile.cmake create mode 100644 vcpkg-overlay/scale/vcpkg.json create mode 100644 vcpkg.json diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5c1d881 --- /dev/null +++ b/.clang-format @@ -0,0 +1,27 @@ +# +# Copyright Quadrivium LLC +# All Rights Reserved +# SPDX-License-Identifier: Apache-2.0 +# + +BasedOnStyle: Google + +AlignAfterOpenBracket: DontAlign +AlignOperands: DontAlign +AllowShortFunctionsOnASingleLine: Empty +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: NonAssignment +BreakStringLiterals: false +DerivePointerAlignment: false +FixNamespaceComments: true +IncludeBlocks: Preserve +InsertBraces: true +InsertTrailingCommas: Wrapped +NamespaceIndentation: All +PointerAlignment: Right +QualifierAlignment: Custom +QualifierOrder: ['inline', 'static', 'constexpr', 'const', 'volatile', 'type', 'restrict'] +ReferenceAlignment: Right +ShortNamespaceLines: 0 +SortIncludes: CaseSensitive diff --git a/.gitignore b/.gitignore index 259148f..20b2df6 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ *.exe *.out *.app + +/build diff --git a/CMakeLists.txt b/CMakeLists.txt index a7b62fc..f2a590f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,11 +10,19 @@ project(cpp-jam VERSION 0.0.1 LANGUAGES C CXX ) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) option(TESTING "Build and run test suite" ON) +find_package(Boost CONFIG REQUIRED) +find_package(fmt CONFIG REQUIRED) +find_package(scale CONFIG REQUIRED) + add_subdirectory(src) if(TESTING) + enable_testing() add_subdirectory(test-vectors) endif () diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..69e9a20 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,14 @@ +{ + "version": 2, + "configurePresets": [ + { + "name": "default", + "generator": "Ninja", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", + "CMAKE_BUILD_TYPE": "Debug" + } + } + ] +} diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..425663e --- /dev/null +++ b/python/README.md @@ -0,0 +1,9 @@ +create venv\ +`uv venv`\ +`source .venv/bin/activate` + +install dependencies using [uv](https://github.com/astral-sh/uv)\ +`uv pip install -r requirements.txt` + +format python code using [ruff](https://github.com/astral-sh/ruff)\ +`ruff format asn1.py` diff --git a/python/asn1.py b/python/asn1.py new file mode 100755 index 0000000..e7c6f36 --- /dev/null +++ b/python/asn1.py @@ -0,0 +1,267 @@ +#!/usr/bin/env python3 + +import asn1tools +import os.path + +DIR = os.path.dirname(__file__) +TEST_VECTORS_DIR = os.path.join(DIR, "../test-vectors") +OUTPUT_NAME = "types" +OUTPUT_DIR = os.path.join(TEST_VECTORS_DIR, "safrole") +SAFROLE_DIR = os.path.join(TEST_VECTORS_DIR, "jamtestvectors/safrole") +ARGS = ["validators-count", "epoch-length"] +NS = "jam::test_vectors_safrole" + + +def flatten(aaa: list[list]): + return [a for aa in aaa for a in aa] + + +def safrole_file(name: str): + return os.path.join(SAFROLE_DIR, name + ".asn") + + +def write(ext: str, lines: list[str]): + with open(os.path.join(OUTPUT_DIR, OUTPUT_NAME + ext), "w") as f: + f.writelines(line + "\n" for line in lines) + + +def c_using(name: str, other: str): + return ["using %s = %s;" % (name, other)] + + +def c_struct(name: str, members): + return ["struct %s {" % name, *((" %s %s;" % (t, k) for k, t in members)), "};"] + + +def c_dash(s: int | str): + if isinstance(s, int): + return s + return s.replace("-", "_") + + +def indent(lines): + return [" " + x for x in lines] + + +class Type: + def __init__(self, name: str): + self.name = name + self.args: list[str] = [] + self.decl: list[str] = [] + self.scale: list[str] = [] + + def c_tdecl(self): + if not self.args: + return [] + return ["template<%s>" % ", ".join("size_t %s" % c_dash(x) for x in self.args)] + + def c_targs(self): + if not self.args: + return "" + return "<%s>" % ", ".join(c_dash(x) for x in self.args) + + def c_tname(self): + return self.name + self.c_targs() + + +def asn_recurse(t: dict): + yield t + e = t.get("element", None) + if e: + yield from asn_recurse(e) + m = t.get("members", None) + if m: + for x in m: + yield from asn_recurse(x) + + +def asn_deps(types: dict): + def deep(k: str): + yield k + for dep in deps1[k]: + yield from deep(dep) + + deps1 = { + k: {x for x in (x["type"] for x in asn_recurse(t)) if x in types} + for k, t in types.items() + } + deps1 = {k: set(flatten(map(deep, deps))) for k, deps in deps1.items()} + deps2: dict[str, set[Type]] = {k: set() for k in types} + for k, deps in deps1.items(): + for dep in deps: + deps2[dep].add(k) + return deps1, deps2 + + +def asn_args(types: dict, deps2: dict[str, set[str]]): + args = { + k: { + s[0] + for s in (x.get("size", None) for x in asn_recurse(t)) + if s and isinstance(s[0], str) + } + for k, t in types.items() + } + for tname, deps in deps2.items(): + for dep in deps: + args[dep].update(args[tname]) + return {k: [a for a in ARGS if a in aa] for k, aa in args.items()} + + +def c_scale(ty: Type, encode: list[str], decode: list[str]): + return [ + *ty.c_tdecl(), + "inline scale::ScaleEncoderStream &operator<<(scale::ScaleEncoderStream &s, const %s &v) {" + % ty.c_tname(), + *indent(encode), + " return s;", + "}", + *ty.c_tdecl(), + "inline scale::ScaleDecoderStream &operator>>(scale::ScaleDecoderStream &s, %s &v) {" + % ty.c_tname(), + *indent(decode), + " return s;", + "}", + ] + + +def c_scale_struct(ty: Type, members: list[str]): + return c_scale( + ty, + ["s << v.%s;" % x for x in members], + ["s >> v.%s;" % x for x in members], + ) + + +def parse_types(path: str): + def asn_sequence_of(t): + (size,) = t["size"] + fixed = isinstance(size, (int, str)) + T = t["element"]["type"] + assert T in asn_types + if T == "U8": + if fixed: + return "qtils::BytesN<%s>" % c_dash(size) + return "qtils::Bytes" + if fixed: + return "std::array<%s, %s>" % (T, c_dash(size)) + return "std::vector<%s>" % T + + def asn_member(t): + if t["type"] == "SEQUENCE OF": + r = asn_sequence_of(t) + elif t["type"] in asn_types: + r = types[t["type"]].c_tname() + else: + raise TypeError(t) + if t.get("optional", False): + return "std::optional<%s>" % r + return r + + asn_types: dict = asn1tools.parse_files([path])["SafroleModule"]["types"] + deps1, deps2 = asn_deps(asn_types) + types = {tname: Type(tname) for tname in asn_types} + for tname, args in asn_args(asn_types, deps2).items(): + types[tname].args = args + enum_trait = [] + for tname, t in asn_types.items(): + ty = types[tname] + if tname == "U8": + ty.decl = c_using(tname, "uint8_t") + continue + if tname == "U32": + ty.decl = c_using(tname, "uint32_t") + continue + if t["type"] == "CHOICE": + ty.decl = c_struct( + tname, + [ + ( + "v", + "boost::variant<%s>" + % ", ".join(asn_member(x) for x in t["members"]), + ) + ], + ) + ty.scale.extend(c_scale_struct(ty, ["v"])) + continue + if t["type"] == "ENUMERATED": + values = [x[1] for x in t["values"]] + assert all(x == i and x < 0xFF for i, x in enumerate(values)) + ty.decl = [ + "enum class %s : uint8_t {" % tname, + *(" %s," % c_dash(x[0]) for x in t["values"]), + "};", + ] + ns = "%s::generic" % NS + enum_trait.append( + "SCALE_DEFINE_ENUM_VALUE_LIST(%s, %s, %s)" + % ( + ns, + ty.name, + ", ".join( + "%s::%s::%s" % (ns, ty.name, c_dash(x[0])) for x in t["values"] + ), + ) + ) + continue + if t["type"] == "SEQUENCE": + ty.decl = c_struct( + tname, [(c_dash(x["name"]), asn_member(x)) for x in t["members"]] + ) + ty.scale.extend( + c_scale_struct(ty, [c_dash(x["name"]) for x in t["members"]]) + ) + continue + ty.decl = c_using(tname, asn_member(t)) + + order = asn1tools.c.utils.topological_sort(deps1) + return [types[k] for k in order], enum_trait + + +def parse_const(path: str): + values: dict = asn1tools.parse_files([path])["SafroleConstants"]["values"] + assert all(v["type"] == "INTEGER" for v in values.values()) + return {k: v["value"] for k, v in values.items()} + + +types, enum_trait = parse_types(safrole_file("safrole")) + + +g_types = flatten([*ty.c_tdecl(), *ty.decl] for ty in types) +g_types = ["namespace %s::generic {" % NS, *indent(g_types), "}"] +for name in ["tiny", "full"]: + args = parse_const(safrole_file(name)) + g_args = [ + *["static constexpr size_t %s = %s;" % (c_dash(a), args[a]) for a in ARGS], + *flatten( + c_using(ty.name, "%s::generic::%s" % (NS, ty.c_tname())) for ty in types + ), + ] + g_args = ["struct %s {" % name, *indent(g_args), "};"] + g_types.extend( + [ + "namespace %s {" % NS, + *indent(g_args), + "}", + ] + ) +g_types = [ + "#pragma once", + "#include ", + "#include ", + *g_types, +] + +g_scale = flatten(ty.scale for ty in types) +g_scale = ["namespace %s::generic {" % NS, *indent(g_scale), "}"] +g_scale = [ + "#pragma once", + "#include ", + '#include "%s.hpp"' % OUTPUT_NAME, + *g_scale, + *enum_trait, +] + +write(".hpp", g_types) +write(".scale.hpp", g_scale) diff --git a/python/requirements.txt b/python/requirements.txt new file mode 100644 index 0000000..1dc2202 --- /dev/null +++ b/python/requirements.txt @@ -0,0 +1 @@ +asn1tools diff --git a/test-vectors/CMakeLists.txt b/test-vectors/CMakeLists.txt index 4fd9231..bf96af0 100644 --- a/test-vectors/CMakeLists.txt +++ b/test-vectors/CMakeLists.txt @@ -3,3 +3,5 @@ # All Rights Reserved # SPDX-License-Identifier: Apache-2.0 # + +add_subdirectory(safrole) diff --git a/test-vectors/safrole/.gitignore b/test-vectors/safrole/.gitignore new file mode 100644 index 0000000..cb56060 --- /dev/null +++ b/test-vectors/safrole/.gitignore @@ -0,0 +1,2 @@ +/types.hpp +/types.scale.hpp diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index 4fd9231..f66d5c0 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -3,3 +3,29 @@ # All Rights Reserved # SPDX-License-Identifier: Apache-2.0 # + +set(SAFROLE_ASN_DIR ${PROJECT_SOURCE_DIR}/test-vectors/jamtestvectors/safrole) +set(SAFROLE_ASN ${SAFROLE_ASN_DIR}/safrole.asn ${SAFROLE_ASN_DIR}/tiny.asn ${SAFROLE_ASN_DIR}/full.asn) +set(SAFROLE_OUTPUT_PREFIX ${PROJECT_SOURCE_DIR}/test-vectors/safrole/types) +set(SAFROLE_OUTPUT ${SAFROLE_OUTPUT_PREFIX}.hpp ${SAFROLE_OUTPUT_PREFIX}.scale.hpp) +set(SAFROLE_PY ${PROJECT_SOURCE_DIR}/python/asn1.py) +add_custom_command( + OUTPUT ${SAFROLE_OUTPUT} + COMMAND python3 ${SAFROLE_PY} + DEPENDS ${SAFROLE_PY} ${SAFROLE_ASN} +) + +add_library(test_vectors_safrole_types INTERFACE ${SAFROLE_OUTPUT}) +target_link_libraries(test_vectors_safrole_types INTERFACE + scale::scale +) + +add_executable(test_vectors_safrole_types_test + types.test.cpp +) +target_compile_definitions(test_vectors_safrole_types_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") +target_link_libraries(test_vectors_safrole_types_test + fmt::fmt + test_vectors_safrole_types +) +add_test(NAME test_vectors_safrole_types_test COMMAND $) diff --git a/test-vectors/safrole/types.test.cpp b/test-vectors/safrole/types.test.cpp new file mode 100644 index 0000000..b9f26cb --- /dev/null +++ b/test-vectors/safrole/types.test.cpp @@ -0,0 +1,52 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "types.hpp" +#include "types.scale.hpp" + +template +void test_dir() { + using types = std::conditional_t; + auto type = full ? "full" : "tiny"; + auto dir = std::filesystem::path{PROJECT_SOURCE_DIR} + / "test-vectors/jamtestvectors/safrole" / type; + constexpr std::string_view kScale = ".scale"; + using Files = std::map; + Files files_scale; + auto push_if = + [](const std::filesystem::path &path, Files &to, std::string_view ext) { + auto name = path.filename().native(); + if (name.ends_with(ext)) { + to.emplace(name.substr(0, name.size() - ext.size()), + qtils::readBytes(path).value()); + } + }; + for (auto &file : std::filesystem::directory_iterator{dir}) { + push_if(file.path(), files_scale, kScale); + } + for (auto &[name, scale] : files_scale) { + fmt::println("{}/{}.scale", type, name); + auto decoded = std::make_unique(); + scale::ScaleDecoderStream s{scale}; + s >> *decoded; + auto reencoded = scale::encode(*decoded).value(); + if (reencoded != scale) { + throw std::logic_error{"reencoded"}; + } + } +} + +int main() { + test_dir(); + test_dir(); + fmt::println("ok"); + return 0; +} diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 0000000..1ad42d2 --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,17 @@ +{ + "default-registry": { + "kind": "git", + "baseline": "fe1cde61e971d53c9687cf9a46308f8f55da19fa", + "repository": "https://github.com/microsoft/vcpkg" + }, + "registries": [ + { + "kind": "artifact", + "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", + "name": "microsoft" + } + ], + "overlay-ports": [ + "vcpkg-overlay" + ] +} diff --git a/vcpkg-overlay/qtils/portfile.cmake b/vcpkg-overlay/qtils/portfile.cmake new file mode 100644 index 0000000..9f91a98 --- /dev/null +++ b/vcpkg-overlay/qtils/portfile.cmake @@ -0,0 +1,11 @@ +vcpkg_check_linkage(ONLY_STATIC_LIBRARY) +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO qdrvm/qtils + REF 665971b1774b0b3e6b0bdba864480e85a5c7d1d5 + SHA512 23f443e48c66d8c3a20c419597c24717ef330f967b81ba3c7ad90575c53a6e321cd7d5ecd629ce772b1852f72c43a0b2e3d4a0ab1f7776a8ec9c7294b82b3c3d +) +vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") +vcpkg_cmake_install() +vcpkg_cmake_config_fixup(PACKAGE_NAME "qtils") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") diff --git a/vcpkg-overlay/qtils/vcpkg.json b/vcpkg-overlay/qtils/vcpkg.json new file mode 100644 index 0000000..96668a6 --- /dev/null +++ b/vcpkg-overlay/qtils/vcpkg.json @@ -0,0 +1,11 @@ +{ + "name": "qtils", + "version": "0.0.3", + "dependencies": [ + { "name": "vcpkg-cmake", "host": true }, + { "name": "vcpkg-cmake-config", "host": true }, + "boost-algorithm", + "boost-outcome", + "fmt" + ] +} diff --git a/vcpkg-overlay/scale/portfile.cmake b/vcpkg-overlay/scale/portfile.cmake new file mode 100644 index 0000000..4df6b84 --- /dev/null +++ b/vcpkg-overlay/scale/portfile.cmake @@ -0,0 +1,11 @@ +vcpkg_check_linkage(ONLY_STATIC_LIBRARY) +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO qdrvm/scale-codec-cpp + REF ab9fb76632aa8ae98978ce3c54ed88a2fd29fd4e + SHA512 51e013a7a6e40f30f64a490251c37636b13566660c43a5245c5e5c1c3e4de3f18b81b730cacbaccfc5dd9d64b82ddec1ebcf04a47089a9be2353c82a3f3b0f25 +) +vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") +vcpkg_cmake_install() +vcpkg_cmake_config_fixup(PACKAGE_NAME "scale") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") diff --git a/vcpkg-overlay/scale/vcpkg.json b/vcpkg-overlay/scale/vcpkg.json new file mode 100644 index 0000000..e58f73f --- /dev/null +++ b/vcpkg-overlay/scale/vcpkg.json @@ -0,0 +1,12 @@ +{ + "name": "scale", + "version": "1.1.3", + "dependencies": [ + { "name": "vcpkg-cmake", "host": true }, + { "name": "vcpkg-cmake-config", "host": true }, + "boost-endian", + "boost-multiprecision", + "boost-variant", + "qtils" + ] +} diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..90f777b --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,5 @@ +{ + "dependencies": [ + "scale" + ] +} From 1560d269e30da13317ff310da75f99cf16d01e71 Mon Sep 17 00:00:00 2001 From: kamilsa Date: Mon, 12 Aug 2024 15:09:50 +0500 Subject: [PATCH 02/10] Add fmt to vcpkg json --- vcpkg.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vcpkg.json b/vcpkg.json index 90f777b..ba0eff2 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,5 +1,9 @@ { "dependencies": [ - "scale" + "scale", + { + "name": "fmt", + "version>=": "10.2.1#2" + } ] -} +} \ No newline at end of file From 1c4c6e9eddf950e42cc6fa0e0d6cde6e144172ad Mon Sep 17 00:00:00 2001 From: kamilsa Date: Mon, 12 Aug 2024 15:11:09 +0500 Subject: [PATCH 03/10] Add name and version to vcpkg.json --- vcpkg.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vcpkg.json b/vcpkg.json index ba0eff2..c8c1883 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,4 +1,6 @@ { + "name": "cpp-jam", + "version": "0.01", "dependencies": [ "scale", { From 639781244c3aea9b51f79158315c1ac715d2a05b Mon Sep 17 00:00:00 2001 From: kamilsa Date: Mon, 12 Aug 2024 15:11:33 +0500 Subject: [PATCH 04/10] Fix typo --- vcpkg.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcpkg.json b/vcpkg.json index c8c1883..4c94a5a 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,6 @@ { "name": "cpp-jam", - "version": "0.01", + "version": "0.0.1", "dependencies": [ "scale", { From cce812f7f6b5a0636da4ee88381567704836d2fa Mon Sep 17 00:00:00 2001 From: kamilsa Date: Mon, 12 Aug 2024 15:11:54 +0500 Subject: [PATCH 05/10] Add empty line --- vcpkg.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcpkg.json b/vcpkg.json index 4c94a5a..d044aa4 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -8,4 +8,4 @@ "version>=": "10.2.1#2" } ] -} \ No newline at end of file +} From ee41f3b531c16d697cef04d7f09cd90d205a87d5 Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 12 Aug 2024 15:46:51 +0500 Subject: [PATCH 06/10] pr comment Signed-off-by: turuslan --- CMakeLists.txt | 2 ++ test-vectors/safrole/CMakeLists.txt | 2 +- test-vectors/safrole/types.test.cpp | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f2a590f..5d97c4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ set(CMAKE_CXX_EXTENSIONS OFF) option(TESTING "Build and run test suite" ON) +find_package(Python3) + find_package(Boost CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED) find_package(scale CONFIG REQUIRED) diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index f66d5c0..c382151 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -11,7 +11,7 @@ set(SAFROLE_OUTPUT ${SAFROLE_OUTPUT_PREFIX}.hpp ${SAFROLE_OUTPUT_PREFIX}.scale.h set(SAFROLE_PY ${PROJECT_SOURCE_DIR}/python/asn1.py) add_custom_command( OUTPUT ${SAFROLE_OUTPUT} - COMMAND python3 ${SAFROLE_PY} + COMMAND ${Python3_EXECUTABLE} ${SAFROLE_PY} DEPENDS ${SAFROLE_PY} ${SAFROLE_ASN} ) diff --git a/test-vectors/safrole/types.test.cpp b/test-vectors/safrole/types.test.cpp index b9f26cb..fe0c36a 100644 --- a/test-vectors/safrole/types.test.cpp +++ b/test-vectors/safrole/types.test.cpp @@ -10,6 +10,9 @@ #include "types.hpp" #include "types.scale.hpp" +/** + * Find and validate "*.scale" files inside "full" or "tiny" directory. + */ template void test_dir() { using types = std::conditional_t Date: Mon, 12 Aug 2024 18:00:29 +0500 Subject: [PATCH 07/10] refactor reuse safrole test vectors Signed-off-by: turuslan --- test-vectors/safrole/types.test.cpp | 46 ++++++++--------------------- test-vectors/safrole/vectors.hpp | 45 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 test-vectors/safrole/vectors.hpp diff --git a/test-vectors/safrole/types.test.cpp b/test-vectors/safrole/types.test.cpp index fe0c36a..466d72f 100644 --- a/test-vectors/safrole/types.test.cpp +++ b/test-vectors/safrole/types.test.cpp @@ -4,52 +4,30 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include +#include "vectors.hpp" -#include "types.hpp" -#include "types.scale.hpp" +using jam::test_vectors_safrole::Vectors; /** - * Find and validate "*.scale" files inside "full" or "tiny" directory. + * Check python generated scale encoding/decoding against test vectors. */ template -void test_dir() { - using types = std::conditional_t; - auto type = full ? "full" : "tiny"; - auto dir = std::filesystem::path{PROJECT_SOURCE_DIR} - / "test-vectors/jamtestvectors/safrole" / type; - constexpr std::string_view kScale = ".scale"; - using Files = std::map; - Files files_scale; - auto push_if = - [](const std::filesystem::path &path, Files &to, std::string_view ext) { - auto name = path.filename().native(); - if (name.ends_with(ext)) { - to.emplace(name.substr(0, name.size() - ext.size()), - qtils::readBytes(path).value()); - } - }; - for (auto &file : std::filesystem::directory_iterator{dir}) { - push_if(file.path(), files_scale, kScale); - } - for (auto &[name, scale] : files_scale) { - fmt::println("{}/{}.scale", type, name); - auto decoded = std::make_unique(); - scale::ScaleDecoderStream s{scale}; - s >> *decoded; +void test_reencode() { + Vectors vectors; + for (auto &path : vectors.paths) { + fmt::println("{}", path.native()); + auto expected = vectors.readRaw(path); + auto decoded = vectors.decode(expected); auto reencoded = scale::encode(*decoded).value(); - if (reencoded != scale) { + if (reencoded != expected) { throw std::logic_error{"reencoded"}; } } } int main() { - test_dir(); - test_dir(); + test_reencode(); + test_reencode(); fmt::println("ok"); return 0; } diff --git a/test-vectors/safrole/vectors.hpp b/test-vectors/safrole/vectors.hpp new file mode 100644 index 0000000..3f5b731 --- /dev/null +++ b/test-vectors/safrole/vectors.hpp @@ -0,0 +1,45 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "types.scale.hpp" + +namespace jam::test_vectors_safrole { + template + struct Vectors { + using types = std::conditional_t; + + std::string type = is_full ? "full" : "tiny"; + std::filesystem::path dir = std::filesystem::path{PROJECT_SOURCE_DIR} + / "test-vectors/jamtestvectors/safrole" / type; + std::set paths; + + Vectors() { + for (auto &file : std::filesystem::directory_iterator{dir}) { + if (file.path().extension() == ".scale") { + paths.emplace(file.path()); + } + } + } + + static auto decode(qtils::BytesIn raw) { + scale::ScaleDecoderStream s{raw}; + auto testcase = std::make_unique(); + s >> *testcase; + return testcase; + } + + static auto readRaw(const std::filesystem::path &path) { + return qtils::readBytes(path).value(); + } + + static auto read(const std::filesystem::path &path) { + return decode(readRaw(path)); + } + }; +} // namespace jam::test_vectors_safrole From d491ae198bc02f080c2ac7b12a3589a452e0ac1e Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 12 Aug 2024 18:24:20 +0500 Subject: [PATCH 08/10] derive equality Signed-off-by: turuslan --- python/asn1.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/asn1.py b/python/asn1.py index e7c6f36..b3f72a1 100755 --- a/python/asn1.py +++ b/python/asn1.py @@ -30,7 +30,12 @@ def c_using(name: str, other: str): def c_struct(name: str, members): - return ["struct %s {" % name, *((" %s %s;" % (t, k) for k, t in members)), "};"] + return [ + "struct %s {" % name, + *((" %s %s;" % (t, k) for k, t in members)), + " bool operator==(const %s &)const = default;" % name, + "};", + ] def c_dash(s: int | str): From 49bce0b5266fd47b8c5a3978280f8033f9f2d642 Mon Sep 17 00:00:00 2001 From: turuslan Date: Tue, 13 Aug 2024 15:37:41 +0500 Subject: [PATCH 09/10] u32 const Signed-off-by: turuslan --- python/asn1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/asn1.py b/python/asn1.py index b3f72a1..6a70a7d 100755 --- a/python/asn1.py +++ b/python/asn1.py @@ -58,7 +58,7 @@ def __init__(self, name: str): def c_tdecl(self): if not self.args: return [] - return ["template<%s>" % ", ".join("size_t %s" % c_dash(x) for x in self.args)] + return ["template<%s>" % ", ".join("uint32_t %s" % c_dash(x) for x in self.args)] def c_targs(self): if not self.args: @@ -238,7 +238,7 @@ def parse_const(path: str): for name in ["tiny", "full"]: args = parse_const(safrole_file(name)) g_args = [ - *["static constexpr size_t %s = %s;" % (c_dash(a), args[a]) for a in ARGS], + *["static constexpr uint32_t %s = %s;" % (c_dash(a), args[a]) for a in ARGS], *flatten( c_using(ty.name, "%s::generic::%s" % (NS, ty.c_tname())) for ty in types ), From d4d258d91fc7203a9dcdf33711e1f6e2d825630c Mon Sep 17 00:00:00 2001 From: turuslan Date: Tue, 13 Aug 2024 15:37:50 +0500 Subject: [PATCH 10/10] python required Signed-off-by: turuslan --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d97c4e..7ecad11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) option(TESTING "Build and run test suite" ON) -find_package(Python3) +find_package(Python3 REQUIRED) find_package(Boost CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED)