Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#30796: test: Use std::span and std::string_view…
Browse files Browse the repository at this point in the history
… for raw data

faecca9 test: Use span for raw data (MarcoFalke)
fac9736 test: Use string_view for json_tests (MarcoFalke)

Pull request description:

  The build system converts raw data into a C++ header file for tests.

  This change modernizes the code to use the convenience wrappers `std::span` and `std::string_view`, so that redundant copies can be avoided.

ACKs for top commit:
  fjahr:
    re-ACK bitcoin/bitcoin@faecca9
  TheCharlatan:
    ACK faecca9
  stickies-v:
    ACK faecca9
  hebasto:
    ACK faecca9, I have reviewed the code and the generated headers.

Tree-SHA512: 1f4951c54aff11ba27c41fb70f2821bdb79e06ca0abae734b970bd0d64dda9d8cced824a891fd51b3e9d4e5715ee9eb49ed5d369010a45eca7c3bec9f8641235
  • Loading branch information
fanquake committed Sep 5, 2024
2 parents fa05ee0 + faecca9 commit 6852d1d
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cmake/module/GenerateHeaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 5 additions & 3 deletions cmake/script/GenerateHeaderFromJson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>\n")
file(WRITE ${HEADER_PATH} "#include <string_view>\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})
Expand All @@ -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}")
13 changes: 9 additions & 4 deletions cmake/script/GenerateHeaderFromRaw.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstddef>\n")
file(APPEND ${HEADER_PATH} "#include <span>\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} "}")
3 changes: 1 addition & 2 deletions src/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <bench/data.h>
#include <bench/data/block413567.raw.h>
#include <chainparams.h>
#include <common/args.h>
#include <consensus/validation.h>
Expand Down
16 changes: 0 additions & 16 deletions src/bench/data.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/bench/data.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/bench/load_external.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <bench/data.h>
#include <bench/data/block413567.raw.h>
#include <chainparams.h>
#include <flatfile.h>
#include <node/blockstorage.h>
Expand Down
2 changes: 1 addition & 1 deletion src/bench/readblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <bench/data.h>
#include <bench/data/block413567.raw.h>
#include <flatfile.h>
#include <node/blockstorage.h>
#include <primitives/block.h>
Expand Down
2 changes: 1 addition & 1 deletion src/bench/rpc_blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <bench/data.h>
#include <bench/data/block413567.raw.h>
#include <chain.h>
#include <core_io.h>
#include <primitives/block.h>
Expand Down
2 changes: 1 addition & 1 deletion src/bench/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <bench/data.h>
#include <bench/data/block413567.raw.h>
#include <span.h>
#include <util/strencodings.h>

Expand Down
2 changes: 1 addition & 1 deletion src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
}


static std::vector<bool> FromBytes(const unsigned char* source, int vector_size)
static std::vector<bool> FromBytes(std::span<const std::byte> source)
{
int vector_size(source.size() * 8);
std::vector<bool> 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<uint8_t>(source[byte_i])};
for (int bit_i = 0; bit_i < 8; ++bit_i) {
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
}
Expand Down Expand Up @@ -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<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
std::vector<bool> asmap = FromBytes(test::data::asmap);
NetGroupManager ngm_asmap{asmap};

CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
Expand Down Expand Up @@ -630,7 +631,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)

BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
{
std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
std::vector<bool> asmap = FromBytes(test::data::asmap);
NetGroupManager ngm_asmap{asmap};

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
Expand Down Expand Up @@ -708,7 +709,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)

BOOST_AUTO_TEST_CASE(addrman_serialization)
{
std::vector<bool> asmap1 = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
std::vector<bool> asmap1 = FromBytes(test::data::asmap);
NetGroupManager netgroupman{asmap1};

const auto ratio = GetCheckRatio(m_node);
Expand Down
8 changes: 4 additions & 4 deletions src/test/util/json.cpp
Original file line number Diff line number Diff line change
@@ -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 <test/util/json.h>

#include <string>
#include <univalue.h>
#include <util/check.h>

#include <univalue.h>
#include <string_view>

UniValue read_json(const std::string& jsondata)
UniValue read_json(std::string_view jsondata)
{
UniValue v;
Assert(v.read(jsondata) && v.isArray());
Expand Down
8 changes: 4 additions & 4 deletions src/test/util/json.h
Original file line number Diff line number Diff line change
@@ -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 <string>

#include <univalue.h>

UniValue read_json(const std::string& jsondata);
#include <string_view>

UniValue read_json(std::string_view jsondata);

#endif // BITCOIN_TEST_UTIL_JSON_H

0 comments on commit 6852d1d

Please sign in to comment.