diff --git a/cmake/module/GenerateHeaders.cmake b/cmake/module/GenerateHeaders.cmake index 35dc54eebb5..c69007acb68 100644 --- a/cmake/module/GenerateHeaders.cmake +++ b/cmake/module/GenerateHeaders.cmake @@ -11,10 +11,10 @@ function(generate_header_from_json json_source_relpath) ) endfunction() -function(generate_header_from_raw raw_source_relpath) +function(generate_header_from_raw raw_source_relpath raw_namespace) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h - COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake + COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -DRAW_NAMESPACE=${raw_namespace} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake VERBATIM ) diff --git a/cmake/script/GenerateHeaderFromJson.cmake b/cmake/script/GenerateHeaderFromJson.cmake index 279ceedf044..53d11652728 100644 --- a/cmake/script/GenerateHeaderFromJson.cmake +++ b/cmake/script/GenerateHeaderFromJson.cmake @@ -5,10 +5,10 @@ file(READ ${JSON_SOURCE_PATH} hex_content HEX) string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}") -file(WRITE ${HEADER_PATH} "#include \n") +file(WRITE ${HEADER_PATH} "#include \n") file(APPEND ${HEADER_PATH} "namespace json_tests{\n") get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE) -file(APPEND ${HEADER_PATH} "static const std::string ${json_source_basename}{\n") +file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n") set(i 0) foreach(byte ${bytes}) @@ -21,4 +21,6 @@ foreach(byte ${bytes}) endif() endforeach() -file(APPEND ${HEADER_PATH} "\n};};") +file(APPEND ${HEADER_PATH} "\n};\n") +file(APPEND ${HEADER_PATH} "inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};") +file(APPEND ${HEADER_PATH} "\n}") diff --git a/cmake/script/GenerateHeaderFromRaw.cmake b/cmake/script/GenerateHeaderFromRaw.cmake index 18c5b4bef25..18a6c2b4078 100644 --- a/cmake/script/GenerateHeaderFromRaw.cmake +++ b/cmake/script/GenerateHeaderFromRaw.cmake @@ -5,18 +5,23 @@ file(READ ${RAW_SOURCE_PATH} hex_content HEX) string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}") +file(WRITE ${HEADER_PATH} "#include \n") +file(APPEND ${HEADER_PATH} "#include \n") +file(APPEND ${HEADER_PATH} "namespace ${RAW_NAMESPACE} {\n") get_filename_component(raw_source_basename ${RAW_SOURCE_PATH} NAME_WE) -file(WRITE ${HEADER_PATH} "static unsigned const char ${raw_source_basename}_raw[] = {\n") +file(APPEND ${HEADER_PATH} "inline constexpr std::byte detail_${raw_source_basename}_raw[]{\n") set(i 0) foreach(byte ${bytes}) math(EXPR i "${i} + 1") math(EXPR remainder "${i} % 8") if(remainder EQUAL 0) - file(APPEND ${HEADER_PATH} "0x${byte},\n") + file(APPEND ${HEADER_PATH} "std::byte{0x${byte}},\n") else() - file(APPEND ${HEADER_PATH} "0x${byte}, ") + file(APPEND ${HEADER_PATH} "std::byte{0x${byte}}, ") endif() endforeach() -file(APPEND ${HEADER_PATH} "\n};") +file(APPEND ${HEADER_PATH} "\n};\n") +file(APPEND ${HEADER_PATH} "inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};\n") +file(APPEND ${HEADER_PATH} "}") diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt index 61a1126904e..8a52980e072 100644 --- a/src/bench/CMakeLists.txt +++ b/src/bench/CMakeLists.txt @@ -3,12 +3,11 @@ # file COPYING or https://opensource.org/license/mit/. include(GenerateHeaders) -generate_header_from_raw(data/block413567.raw) +generate_header_from_raw(data/block413567.raw benchmark::data) add_executable(bench_bitcoin bench_bitcoin.cpp bench.cpp - data.cpp nanobench.cpp ${CMAKE_CURRENT_BINARY_DIR}/data/block413567.raw.h # Benchmarks: diff --git a/src/bench/checkblock.cpp b/src/bench/checkblock.cpp index 580265fc520..9558d64f199 100644 --- a/src/bench/checkblock.cpp +++ b/src/bench/checkblock.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include #include diff --git a/src/bench/data.cpp b/src/bench/data.cpp deleted file mode 100644 index 8c5bb13f754..00000000000 --- a/src/bench/data.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2019-2021 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include - -#include - -namespace benchmark { -namespace data { - -#include -const std::vector block413567{std::begin(block413567_raw), std::end(block413567_raw)}; - -} // namespace data -} // namespace benchmark diff --git a/src/bench/data.h b/src/bench/data.h deleted file mode 100644 index 5f13d766ea8..00000000000 --- a/src/bench/data.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2019 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_BENCH_DATA_H -#define BITCOIN_BENCH_DATA_H - -#include -#include - -namespace benchmark { -namespace data { - -extern const std::vector block413567; - -} // namespace data -} // namespace benchmark - -#endif // BITCOIN_BENCH_DATA_H diff --git a/src/bench/load_external.cpp b/src/bench/load_external.cpp index 2ed5a3979dc..8f9399c60d5 100644 --- a/src/bench/load_external.cpp +++ b/src/bench/load_external.cpp @@ -3,7 +3,7 @@ // file COPYING or https://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include #include diff --git a/src/bench/readblock.cpp b/src/bench/readblock.cpp index 0b88663db67..058d953b4e9 100644 --- a/src/bench/readblock.cpp +++ b/src/bench/readblock.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include #include diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp index 54356598e7e..7e3e2d8e48a 100644 --- a/src/bench/rpc_blockchain.cpp +++ b/src/bench/rpc_blockchain.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include #include diff --git a/src/bench/strencodings.cpp b/src/bench/strencodings.cpp index 72eb6b442ba..dd5829caf26 100644 --- a/src/bench/strencodings.cpp +++ b/src/bench/strencodings.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a666a76f8f3..4e24b87e9b7 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,7 +12,7 @@ generate_header_from_json(data/script_tests.json) generate_header_from_json(data/sighash.json) generate_header_from_json(data/tx_invalid.json) generate_header_from_json(data/tx_valid.json) -generate_header_from_raw(data/asmap.raw) +generate_header_from_raw(data/asmap.raw test::data) # Do not use generator expressions in test sources because the # SOURCES property is processed to gather test suite macros. diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index e5d25637bd4..da6ff779243 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -47,11 +47,12 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0) } -static std::vector FromBytes(const unsigned char* source, int vector_size) +static std::vector FromBytes(std::span source) { + int vector_size(source.size() * 8); std::vector result(vector_size); for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) { - unsigned char cur_byte = source[byte_i]; + uint8_t cur_byte{std::to_integer(source[byte_i])}; for (int bit_i = 0; bit_i < 8; ++bit_i) { result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1; } @@ -576,7 +577,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy) // 101.8.0.0/16 AS8 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { - std::vector asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{asmap}; CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); @@ -630,7 +631,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { - std::vector asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{asmap}; CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); @@ -708,7 +709,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) BOOST_AUTO_TEST_CASE(addrman_serialization) { - std::vector asmap1 = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector asmap1 = FromBytes(test::data::asmap); NetGroupManager netgroupman{asmap1}; const auto ratio = GetCheckRatio(m_node); diff --git a/src/test/util/json.cpp b/src/test/util/json.cpp index ad3c346c84e..46a4a9f9a19 100644 --- a/src/test/util/json.cpp +++ b/src/test/util/json.cpp @@ -1,15 +1,15 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include -#include +#include -UniValue read_json(const std::string& jsondata) +UniValue read_json(std::string_view jsondata) { UniValue v; Assert(v.read(jsondata) && v.isArray()); diff --git a/src/test/util/json.h b/src/test/util/json.h index 5b1026762e6..f6f4e6ab71b 100644 --- a/src/test/util/json.h +++ b/src/test/util/json.h @@ -1,14 +1,14 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_TEST_UTIL_JSON_H #define BITCOIN_TEST_UTIL_JSON_H -#include - #include -UniValue read_json(const std::string& jsondata); +#include + +UniValue read_json(std::string_view jsondata); #endif // BITCOIN_TEST_UTIL_JSON_H