Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gruve-p committed Sep 13, 2024
2 parents a7080af + 1d5b240 commit 09d3ff1
Show file tree
Hide file tree
Showing 25 changed files with 375 additions and 141 deletions.
12 changes: 6 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ insert_final_newline = true
trim_trailing_whitespace = true

# Source code files
[*.{h,cpp,py,sh}]
[*.{h,cpp,rs,py,sh}]
indent_size = 4

# .cirrus.yml, .fuzzbuzz.yml, etc.
# .cirrus.yml, etc.
[*.yml]
indent_size = 2

# Makefiles
[{*.am,Makefile.*.include}]
# Makefiles (only relevant for depends build)
[Makefile]
indent_style = tab

# Autoconf scripts
[configure.ac]
# CMake files
[{CMakeLists.txt,*.cmake,*.cmake.in}]
indent_size = 2
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,11 @@ if(ENABLE_HARDENING)
endif()

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
try_append_cxx_flags("-mbranch-protection=bti" TARGET hardening_interface SKIP_LINK)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
try_append_cxx_flags("-mbranch-protection=bti" TARGET hardening_interface SKIP_LINK)
else()
try_append_cxx_flags("-mbranch-protection=standard" TARGET hardening_interface SKIP_LINK)
endif()
endif()

try_append_linker_flag("-Wl,--enable-reloc-section" TARGET hardening_interface)
Expand Down
22 changes: 10 additions & 12 deletions cmake/script/GenerateHeaderFromJson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")

set(header_content "#include <string_view>\n")
string(APPEND header_content "namespace json_tests{\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)
string(APPEND header_content "inline constexpr char detail_${json_source_basename}_bytes[]{\n")
file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n")

set(i 0)
foreach(byte ${bytes})
math(EXPR i "${i} + 1")
if(i EQUAL 8)
set(i 0)
string(APPEND header_content "0x${byte},\n")
math(EXPR remainder "${i} % 8")
if(remainder EQUAL 0)
file(APPEND ${HEADER_PATH} "0x${byte},\n")
else()
string(APPEND header_content "0x${byte}, ")
file(APPEND ${HEADER_PATH} "0x${byte}, ")
endif()
endforeach()

string(APPEND header_content "\n};\n")
string(APPEND header_content "inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};")
string(APPEND header_content "\n}")

file(WRITE ${HEADER_PATH} "${header_content}")
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}")
24 changes: 11 additions & 13 deletions cmake/script/GenerateHeaderFromRaw.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@
file(READ ${RAW_SOURCE_PATH} hex_content HEX)
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")

set(header_content "#include <cstddef>\n")
string(APPEND header_content "#include <span>\n")
string(APPEND header_content "namespace ${RAW_NAMESPACE} {\n")
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)
string(APPEND header_content "inline constexpr std::byte detail_${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")
if(i EQUAL 8)
set(i 0)
string(APPEND header_content "std::byte{0x${byte}},\n")
math(EXPR remainder "${i} % 8")
if(remainder EQUAL 0)
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}},\n")
else()
string(APPEND header_content "std::byte{0x${byte}}, ")
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}}, ")
endif()
endforeach()

string(APPEND header_content "\n};\n")
string(APPEND header_content "inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};\n")
string(APPEND header_content "}")

file(WRITE ${HEADER_PATH} "${header_content}")
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} "}")
11 changes: 11 additions & 0 deletions libbitcoinkernel.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PACKAGE_NAME@ kernel library
Description: Experimental library for the Bitcoin Core validation engine.
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lbitcoinkernel
Libs.private: -L${libdir} @LIBS_PRIVATE@
Cflags: -I${includedir}
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ set(SECP256K1_ENABLE_MODULE_RECOVERY ON CACHE BOOL "" FORCE)
set(SECP256K1_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE)
set(SECP256K1_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
set(SECP256K1_BUILD_EXHAUSTIVE_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
if(NOT BUILD_TESTS)
# Always skip the ctime tests, if we are building no other tests.
# Otherwise, they are built if Valgrind is available. See SECP256K1_VALGRIND.
set(SECP256K1_BUILD_CTIME_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
endif()
set(SECP256K1_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
include(GetTargetInterface)
# -fsanitize and related flags apply to both C++ and C,
Expand Down
5 changes: 3 additions & 2 deletions src/index/base.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2022 The Bitcoin Core developers
// Copyright (c) 2017-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.

Expand All @@ -14,6 +14,7 @@
#include <node/database_args.h>
#include <node/interface_ui.h>
#include <tinyformat.h>
#include <util/string.h>
#include <util/thread.h>
#include <util/translation.h>
#include <validation.h> // For g_chainman
Expand All @@ -27,7 +28,7 @@ constexpr auto SYNC_LOG_INTERVAL{30s};
constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s};

template <typename... Args>
void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
void BaseIndex::FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
{
auto message = tfm::format(fmt, args...);
node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
Expand Down
5 changes: 3 additions & 2 deletions src/index/base.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2022 The Bitcoin Core developers
// Copyright (c) 2017-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.

Expand All @@ -7,6 +7,7 @@

#include <dbwrapper.h>
#include <interfaces/chain.h>
#include <util/string.h>
#include <util/threadinterrupt.h>
#include <validationinterface.h>

Expand Down Expand Up @@ -94,7 +95,7 @@ class BaseIndex : public CValidationInterface
virtual bool AllowPrune() const = 0;

template <typename... Args>
void FatalErrorf(const char* fmt, const Args&... args);
void FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args);

protected:
std::unique_ptr<interfaces::Chain> m_chain;
Expand Down
34 changes: 34 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ set_target_properties(groestlcoinkernel PROPERTIES
CXX_VISIBILITY_PRESET default
)

# When building the static library, install all static libraries the
# bitcoinkernel depends on.
if(NOT BUILD_SHARED_LIBS)
# Recursively get all the static libraries a target depends on and put them in libs_out
function(get_target_static_link_libs target libs_out)
get_target_property(linked_libraries ${target} LINK_LIBRARIES)
foreach(dep ${linked_libraries})
if(TARGET ${dep})
get_target_property(dep_type ${dep} TYPE)
if(dep_type STREQUAL "STATIC_LIBRARY")
list(APPEND ${libs_out} ${dep})
get_target_static_link_libs(${dep} ${libs_out})
endif()
endif()
endforeach()
set(${libs_out} ${${libs_out}} PARENT_SCOPE)
endfunction()

set(all_kernel_static_link_libs "")
get_target_static_link_libs(bitcoinkernel all_kernel_static_link_libs)

# LIBS_PRIVATE is substituted in the pkg-config file.
set(LIBS_PRIVATE "")
foreach(lib ${all_kernel_static_link_libs})
install(TARGETS ${lib} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
string(APPEND LIBS_PRIVATE " -l${lib}")
endforeach()

string(STRIP "${LIBS_PRIVATE}" LIBS_PRIVATE)
endif()

configure_file(${PROJECT_SOURCE_DIR}/libbitcoinkernel.pc.in ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc @ONLY)
install(FILES ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

include(GNUInstallDirs)
install(TARGETS groestlcoinkernel
RUNTIME
Expand Down
3 changes: 2 additions & 1 deletion src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ std::unique_ptr<Sock> CreateSockOS(int domain, int type, int protocol)
std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock = CreateSockOS;

template<typename... Args>
static void LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args) {
static void LogConnectFailure(bool manual_connection, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
{
std::string error_message = tfm::format(fmt, args...);
if (manual_connection) {
LogPrintf("%s\n", error_message);
Expand Down
18 changes: 10 additions & 8 deletions src/qt/walletcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ std::map<std::string, std::pair<bool, std::string>> WalletController::listWallet
return wallets;
}

void WalletController::removeWallet(WalletModel* wallet_model)
{
// Once the wallet is successfully removed from the node, the model will emit the 'WalletModel::unload' signal.
// This signal is already connected and will complete the removal of the view from the GUI.
// Look at 'WalletController::getOrCreateWallet' for the signal connection.
wallet_model->wallet().remove();
}

void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
{
QMessageBox box(parent);
Expand All @@ -89,10 +97,7 @@ void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
box.setDefaultButton(QMessageBox::Yes);
if (box.exec() != QMessageBox::Yes) return;

// First remove wallet from node.
wallet_model->wallet().remove();
// Now release the model.
removeAndDeleteWallet(wallet_model);
removeWallet(wallet_model);
}

void WalletController::closeAllWallets(QWidget* parent)
Expand All @@ -105,11 +110,8 @@ void WalletController::closeAllWallets(QWidget* parent)

QMutexLocker locker(&m_mutex);
for (WalletModel* wallet_model : m_wallets) {
wallet_model->wallet().remove();
Q_EMIT walletRemoved(wallet_model);
delete wallet_model;
removeWallet(wallet_model);
}
m_wallets.clear();
}

WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
Expand Down
3 changes: 3 additions & 0 deletions src/qt/walletcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class WalletController : public QObject

friend class WalletControllerActivity;
friend class MigrateWalletActivity;

//! Starts the wallet closure procedure
void removeWallet(WalletModel* wallet_model);
};

class WalletControllerActivity : public QObject
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ add_executable(test_groestlcoin
txvalidation_tests.cpp
txvalidationcache_tests.cpp
uint256_tests.cpp
util_string_tests.cpp
util_tests.cpp
util_threadnames_tests.cpp
validation_block_tests.cpp
Expand Down
48 changes: 31 additions & 17 deletions src/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ The build system is set up to compile an executable called `test_bitcoin`
that runs all of the unit tests. The main source file for the test library is found in
`util/setup_common.cpp`.

The examples in this document assume the build directory is named
`build`. You'll need to adapt them if you named it differently.

### Compiling/running unit tests

Unit tests will be automatically compiled if dependencies were met
during the generation of the Bitcoin Core build system
and tests weren't explicitly disabled.

Assuming the build directory is named `build`, the unit tests can be run
with `ctest --test-dir build`, which includes unit tests from subtrees.
The unit tests can be run with `ctest --test-dir build`, which includes unit
tests from subtrees.

Run `test_bitcoin --list_content` for the full list of tests.

To run the unit tests manually, launch `build/src/test/test_bitcoin`. To recompile
after a test file was modified, run `cmake --build build` and then run the test again. If you
Expand All @@ -35,34 +40,45 @@ the `src/qt/test/test_main.cpp` file.

### Running individual tests

`test_bitcoin` accepts the command line arguments from the boost framework.
For example, to run just the `getarg_tests` suite of tests:
The `test_bitcoin` runner accepts command line arguments from the Boost
framework. To see the list of arguments that may be passed, run:

```
test_bitcoin --help
```

For example, to run only the tests in the `getarg_tests` file, with full logging:

```bash
build/src/test/test_bitcoin --log_level=all --run_test=getarg_tests
```

`log_level` controls the verbosity of the test framework, which logs when a
test case is entered, for example.
or

`test_bitcoin` also accepts some of the command line arguments accepted by
`bitcoind`. Use `--` to separate these sets of arguments:
```bash
build/src/test/test_bitcoin -l all -t getarg_tests
```

or to run only the doubledash test in `getarg_tests`

```bash
build/src/test/test_bitcoin --log_level=all --run_test=getarg_tests -- -printtoconsole=1
build/src/test/test_bitcoin --run_test=getarg_tests/doubledash
```

The `-printtoconsole=1` after the two dashes sends debug logging, which
normally goes only to `debug.log` within the data directory, also to the
standard terminal output.
The `--log_level=` (or `-l`) argument controls the verbosity of the test output.

... or to run just the doubledash test:
The `test_bitcoin` runner also accepts some of the command line arguments accepted by
`bitcoind`. Use `--` to separate these sets of arguments:

```bash
build/src/test/test_bitcoin --run_test=getarg_tests/doubledash
build/src/test/test_bitcoin --log_level=all --run_test=getarg_tests -- -printtoconsole=1
```

`test_bitcoin` creates a temporary working (data) directory with a randomly
The `-printtoconsole=1` after the two dashes sends debug logging, which
normally goes only to `debug.log` within the data directory, to the
standard terminal output as well.

Running `test_bitcoin` creates a temporary working (data) directory with a randomly
generated pathname within `test_common bitcoin/`, which in turn is within
the system's temporary directory (see
[`temp_directory_path`](https://en.cppreference.com/w/cpp/filesystem/temp_directory_path)).
Expand Down Expand Up @@ -97,8 +113,6 @@ If you run an entire test suite, such as `--run_test=getarg_tests`, or all the t
(by not specifying `--run_test`), a separate directory
will be created for each individual test.

Run `test_bitcoin --help` for the full list of tests.

### Adding test cases

To add a new unit test file to our test suite, you need
Expand Down
8 changes: 7 additions & 1 deletion src/test/fuzz/hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ FUZZ_TARGET(hex)
assert(uint256::FromUserHex(random_hex_string));
}
if (const auto result{uint256::FromUserHex(random_hex_string)}) {
assert(uint256::FromHex(result->ToString()));
const auto result_string{result->ToString()}; // ToString() returns a fixed-length string without "0x" prefix
assert(result_string.length() == 64);
assert(IsHex(result_string));
assert(TryParseHex(result_string));
assert(Txid::FromHex(result_string));
assert(Wtxid::FromHex(result_string));
assert(uint256::FromHex(result_string));
}
try {
(void)HexToPubKey(random_hex_string);
Expand Down
Loading

0 comments on commit 09d3ff1

Please sign in to comment.