Skip to content

Commit d4bc620

Browse files
committed
Merge bitcoin#34488: refactor: Small style and test fixups for bitcoinkernel
fad9dd1 test: kernel test fixups (MarcoFalke) fabb58d test: Use clang-tidy named args for create_chainman (MarcoFalke) fa51594 refactor: Small style fixups in src/kernel/bitcoinkernel.cpp (MarcoFalke) Pull request description: Just some small style and test fixups after bitcoin#30595 (review) ACKs for top commit: stickies-v: re-ACK fad9dd1 frankomosh: Code Review ACK fad9dd1. All changes are sound refactoring with no functional issues. Nice improvements to readability (named args in create_chainman, span.data(), range checks now properly require non-empty). Tree-SHA512: 0a92e871b4db75a590acad39672594625e402895bc0d36635d36ec2fe8dce7cc2c5cb6ebf2a92bc14617d94648b84bffb95ff783cea71bd91ac4a9871ef5dbef
2 parents eb3dbba + fad9dd1 commit d4bc620

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include <cstring>
4242
#include <exception>
4343
#include <functional>
44-
#include <iterator>
4544
#include <list>
4645
#include <memory>
4746
#include <span>
@@ -56,7 +55,7 @@ using util::ImmediateTaskRunner;
5655

5756
// Define G_TRANSLATION_FUN symbol in libbitcoinkernel library so users of the
5857
// library aren't required to export this symbol
59-
extern const std::function<std::string(const char*)> G_TRANSLATION_FUN{nullptr};
58+
extern const TranslateFn G_TRANSLATION_FUN{nullptr};
6059

6160
static const kernel::Context btck_context_static{};
6261

@@ -84,7 +83,7 @@ class WriterStream
8483
//
8584
void write(std::span<const std::byte> src)
8685
{
87-
if (m_writer(std::data(src), src.size(), m_user_data) != 0) {
86+
if (m_writer(src.data(), src.size(), m_user_data) != 0) {
8887
throw std::runtime_error("Failed to write serialization data");
8988
}
9089
}
@@ -113,13 +112,13 @@ struct Handle {
113112
static C* create(Args&&... args)
114113
{
115114
auto cpp_obj{std::make_unique<CPP>(std::forward<Args>(args)...)};
116-
return reinterpret_cast<C*>(cpp_obj.release());
115+
return ref(cpp_obj.release());
117116
}
118117

119118
static C* copy(const C* ptr)
120119
{
121120
auto cpp_obj{std::make_unique<CPP>(get(ptr))};
122-
return reinterpret_cast<C*>(cpp_obj.release());
121+
return ref(cpp_obj.release());
123122
}
124123

125124
static const CPP& get(const C* ptr)

src/kernel/bitcoinkernel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ extern "C" {
8282
* @section error Error handling
8383
*
8484
* Functions communicate an error through their return types, usually returning
85-
* a nullptr, 0, or false if an error is encountered. Additionally, verification
86-
* functions, e.g. for scripts, may communicate more detailed error information
87-
* through status code out parameters.
85+
* a nullptr or a status code as documented by the returning function.
86+
* Additionally, verification functions, e.g. for scripts, may communicate more
87+
* detailed error information through status code out parameters.
8888
*
8989
* Fine-grained validation information is communicated through the validation
9090
* interface.

src/test/kernel/test_kernel.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,7 @@ void run_verify_test(
265265
}
266266

267267
template <typename T>
268-
concept HasToBytes = requires(T t) {
269-
{ t.ToBytes() } -> std::convertible_to<std::vector<std::byte>>;
270-
};
268+
concept HasToBytes = requires(T t) { t.ToBytes(); };
271269

272270
template <typename T>
273271
void CheckHandle(T object, T distinct_object)
@@ -277,7 +275,9 @@ void CheckHandle(T object, T distinct_object)
277275
BOOST_CHECK(object.get() != distinct_object.get());
278276

279277
if constexpr (HasToBytes<T>) {
280-
BOOST_CHECK_NE(object.ToBytes().size(), distinct_object.ToBytes().size());
278+
const auto object_bytes = object.ToBytes();
279+
const auto distinct_bytes = distinct_object.ToBytes();
280+
BOOST_CHECK(!std::ranges::equal(object_bytes, distinct_bytes));
281281
}
282282

283283
// Copy constructor
@@ -321,7 +321,8 @@ void CheckRange(const RangeType& range, size_t expected_size)
321321
using value_type = std::ranges::range_value_t<RangeType>;
322322

323323
BOOST_CHECK_EQUAL(range.size(), expected_size);
324-
BOOST_CHECK_EQUAL(range.empty(), (expected_size == 0));
324+
BOOST_REQUIRE(range.size() > 0); // Some checks below assume a non-empty range
325+
BOOST_REQUIRE(!range.empty());
325326

326327
BOOST_CHECK(range.begin() != range.end());
327328
BOOST_CHECK_EQUAL(std::distance(range.begin(), range.end()), static_cast<std::ptrdiff_t>(expected_size));
@@ -332,7 +333,6 @@ void CheckRange(const RangeType& range, size_t expected_size)
332333
BOOST_CHECK_EQUAL(range[i].get(), (*(range.begin() + i)).get());
333334
}
334335

335-
BOOST_CHECK_NE(range.at(0).get(), range.at(expected_size - 1).get());
336336
BOOST_CHECK_THROW(range.at(expected_size), std::out_of_range);
337337

338338
BOOST_CHECK_EQUAL(range.front().get(), range[0].get());
@@ -792,7 +792,9 @@ void chainman_reindex_test(TestDirectory& test_directory)
792792
{
793793
auto notifications{std::make_shared<TestKernelNotifications>()};
794794
auto context{create_context(notifications, ChainType::MAINNET)};
795-
auto chainman{create_chainman(test_directory, true, false, false, false, context)};
795+
auto chainman{create_chainman(
796+
test_directory, /*reindex=*/true, /*wipe_chainstate=*/false,
797+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
796798

797799
std::vector<std::string> import_files;
798800
BOOST_CHECK(chainman->ImportBlocks(import_files));
@@ -835,7 +837,9 @@ void chainman_reindex_chainstate_test(TestDirectory& test_directory)
835837
{
836838
auto notifications{std::make_shared<TestKernelNotifications>()};
837839
auto context{create_context(notifications, ChainType::MAINNET)};
838-
auto chainman{create_chainman(test_directory, false, true, false, false, context)};
840+
auto chainman{create_chainman(
841+
test_directory, /*reindex=*/false, /*wipe_chainstate=*/true,
842+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
839843

840844
std::vector<std::string> import_files;
841845
import_files.push_back((test_directory.m_directory / "blocks" / "blk00000.dat").string());
@@ -847,7 +851,9 @@ void chainman_mainnet_validation_test(TestDirectory& test_directory)
847851
auto notifications{std::make_shared<TestKernelNotifications>()};
848852
auto validation_interface{std::make_shared<TestValidationInterface>()};
849853
auto context{create_context(notifications, ChainType::MAINNET, validation_interface)};
850-
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
854+
auto chainman{create_chainman(
855+
test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
856+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
851857

852858
// mainnet block 1
853859
auto raw_block = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
@@ -975,7 +981,9 @@ BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
975981

976982
auto notifications{std::make_shared<TestKernelNotifications>()};
977983
auto context{create_context(notifications, ChainType::REGTEST)};
978-
auto chainman{create_chainman(in_memory_test_directory, false, false, true, true, context)};
984+
auto chainman{create_chainman(
985+
in_memory_test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
986+
/*block_tree_db_in_memory=*/true, /*chainstate_db_in_memory=*/true, context)};
979987

980988
for (auto& raw_block : REGTEST_BLOCK_DATA) {
981989
Block block{hex_string_to_byte_vec(raw_block)};
@@ -999,7 +1007,9 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
9991007
auto context{create_context(notifications, ChainType::REGTEST)};
10001008

10011009
{
1002-
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
1010+
auto chainman{create_chainman(
1011+
test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1012+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
10031013
for (const auto& data : REGTEST_BLOCK_DATA) {
10041014
Block block{hex_string_to_byte_vec(data)};
10051015
BlockHeader header = block.GetHeader();
@@ -1021,7 +1031,9 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
10211031
const size_t mid{REGTEST_BLOCK_DATA.size() / 2};
10221032

10231033
{
1024-
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
1034+
auto chainman{create_chainman(
1035+
test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1036+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
10251037
for (size_t i{0}; i < mid; i++) {
10261038
Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
10271039
bool new_block{false};
@@ -1030,7 +1042,9 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
10301042
}
10311043
}
10321044

1033-
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
1045+
auto chainman{create_chainman(
1046+
test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1047+
/*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
10341048

10351049
for (size_t i{mid}; i < REGTEST_BLOCK_DATA.size(); i++) {
10361050
Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};

0 commit comments

Comments
 (0)