aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.cirrus.yml9
-rw-r--r--.gitignore1
-rw-r--r--CONTRIBUTING.md3
-rw-r--r--build-aux/m4/ax_cxx_compile_stdcxx.m464
-rw-r--r--build_msvc/test_bitcoin-qt/test_bitcoin-qt.vcxproj3
-rw-r--r--build_msvc/vcpkg.json9
-rwxr-xr-xci/test/00_setup_env_native_msan.sh3
-rwxr-xr-xci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh (renamed from ci/test/00_setup_env_native_nowallet.sh)8
-rwxr-xr-xci/test/00_setup_env_native_qt5.sh2
-rwxr-xr-xci/test/00_setup_env_native_tsan.sh6
-rwxr-xr-xci/test/05_before_script.sh7
-rw-r--r--configure.ac20
-rwxr-xr-xcontrib/devtools/test-symbol-check.py2
-rwxr-xr-xcontrib/guix/guix-build18
-rwxr-xr-xcontrib/guix/guix-codesign16
-rwxr-xr-xcontrib/guix/libexec/build.sh14
-rwxr-xr-xcontrib/guix/libexec/codesign.sh2
-rw-r--r--contrib/guix/libexec/prelude.bash16
-rw-r--r--contrib/guix/manifest.scm33
-rwxr-xr-xcontrib/macdeploy/detached-sig-create.sh4
-rwxr-xr-xcontrib/signet/miner7
-rw-r--r--depends/packages/libevent.mk3
-rw-r--r--doc/README.md1
-rw-r--r--doc/build-freebsd.md39
-rw-r--r--doc/build-netbsd.md2
-rw-r--r--doc/build-osx.md39
-rw-r--r--doc/build-unix.md25
-rw-r--r--doc/dependencies.md97
-rw-r--r--doc/descriptors.md6
-rw-r--r--doc/i2p.md6
-rw-r--r--doc/multisig-tutorial.md6
-rw-r--r--doc/p2p-bad-ports.md114
-rw-r--r--doc/release-notes-24198.md6
-rw-r--r--doc/release-notes.md169
-rw-r--r--doc/tor.md8
-rw-r--r--src/Makefile.am102
-rw-r--r--src/Makefile.bench.include31
-rw-r--r--src/Makefile.qttest.include3
-rw-r--r--src/Makefile.test.include1
-rw-r--r--src/addrman.cpp8
-rw-r--r--src/bench/bench_bitcoin.cpp4
-rw-r--r--src/bench/logging.cpp48
-rw-r--r--src/bench/rpc_mempool.cpp2
-rw-r--r--src/bitcoin-chainstate.cpp262
-rw-r--r--src/chainparams.cpp2
-rw-r--r--src/index/coinstatsindex.cpp19
-rw-r--r--src/index/coinstatsindex.h2
-rw-r--r--src/init.cpp45
-rw-r--r--src/init/common.cpp2
-rw-r--r--src/leveldb/util/env_posix.cc4
-rw-r--r--src/leveldb/util/env_windows.cc4
-rw-r--r--src/net.cpp68
-rw-r--r--src/net.h23
-rw-r--r--src/net_processing.cpp17
-rw-r--r--src/netaddress.cpp8
-rw-r--r--src/netaddress.h13
-rw-r--r--src/netbase.cpp90
-rw-r--r--src/netbase.h9
-rw-r--r--src/node/blockstorage.cpp174
-rw-r--r--src/node/blockstorage.h27
-rw-r--r--src/node/interfaces.cpp4
-rw-r--r--src/node/miner.cpp19
-rw-r--r--src/node/miner.h1
-rw-r--r--src/qt/bitcoin.cpp2
-rw-r--r--src/qt/guiutil.cpp33
-rw-r--r--src/qt/paymentserver.cpp23
-rw-r--r--src/qt/sendcoinsdialog.cpp205
-rw-r--r--src/qt/sendcoinsdialog.h13
-rw-r--r--src/qt/test/optiontests.cpp31
-rw-r--r--src/qt/test/optiontests.h25
-rw-r--r--src/qt/test/test_main.cpp5
-rw-r--r--src/rest.cpp5
-rw-r--r--src/rpc/blockchain.cpp486
-rw-r--r--src/rpc/blockchain.h7
-rw-r--r--src/rpc/mempool.cpp476
-rw-r--r--src/rpc/mempool.h17
-rw-r--r--src/rpc/rawtransaction.cpp6
-rw-r--r--src/rpc/register.h3
-rw-r--r--src/script/descriptor.cpp68
-rw-r--r--src/script/script.h3
-rw-r--r--src/script/sign.cpp23
-rw-r--r--src/script/standard.cpp80
-rw-r--r--src/script/standard.h4
-rw-r--r--src/test/coinstatsindex_tests.cpp61
-rw-r--r--src/test/fs_tests.cpp6
-rw-r--r--src/test/fuzz/netaddress.cpp3
-rw-r--r--src/test/fuzz/script_format.cpp5
-rw-r--r--src/test/getarg_tests.cpp492
-rw-r--r--src/test/miner_tests.cpp32
-rw-r--r--src/test/net_peer_eviction_tests.cpp163
-rw-r--r--src/test/net_tests.cpp191
-rw-r--r--src/test/netbase_tests.cpp20
-rw-r--r--src/test/script_segwit_tests.cpp164
-rw-r--r--src/test/timedata_tests.cpp3
-rw-r--r--src/test/util/setup_common.cpp3
-rw-r--r--src/test/util/validation.cpp6
-rw-r--r--src/test/util/validation.h8
-rw-r--r--src/test/util_tests.cpp49
-rw-r--r--src/test/validation_chainstate_tests.cpp3
-rw-r--r--src/test/validation_chainstatemanager_tests.cpp2
-rw-r--r--src/timedata.cpp38
-rw-r--r--src/timedata.h5
-rw-r--r--src/util/readwritefile.cpp4
-rw-r--r--src/util/system.cpp15
-rw-r--r--src/util/system.h22
-rw-r--r--src/validation.cpp226
-rw-r--r--src/validation.h27
-rw-r--r--src/validationinterface.h1
-rw-r--r--src/wallet/coinselection.cpp2
-rw-r--r--src/wallet/rpc/spend.cpp6
-rw-r--r--src/wallet/rpc/transactions.cpp2
-rw-r--r--src/wallet/rpc/wallet.cpp4
-rw-r--r--src/wallet/scriptpubkeyman.cpp131
-rw-r--r--src/wallet/scriptpubkeyman.h1
-rw-r--r--src/wallet/spend.cpp38
-rw-r--r--src/wallet/sqlite.cpp67
-rw-r--r--src/wallet/test/psbt_wallet_tests.cpp1
-rw-r--r--src/wallet/test/wallet_tests.cpp4
-rw-r--r--src/wallet/transaction.h1
-rw-r--r--src/wallet/wallet.cpp5
-rw-r--r--src/wallet/walletdb.cpp4
-rw-r--r--src/wallet/walletdb.h2
-rwxr-xr-xtest/functional/feature_addrman.py11
-rwxr-xr-xtest/functional/feature_bind_port_discover.py78
-rwxr-xr-xtest/functional/feature_bind_port_externalip.py75
-rwxr-xr-xtest/functional/feature_maxuploadtarget.py26
-rwxr-xr-xtest/functional/feature_segwit.py22
-rwxr-xr-xtest/functional/p2p_message_capture.py2
-rwxr-xr-xtest/functional/rpc_blockchain.py4
-rw-r--r--test/functional/test_framework/script.py1
-rwxr-xr-xtest/functional/test_framework/test_node.py3
-rw-r--r--test/functional/test_framework/util.py16
-rwxr-xr-xtest/functional/test_runner.py3
-rwxr-xr-xtest/functional/wallet_basic.py2
-rwxr-xr-xtest/functional/wallet_createwallet.py5
-rwxr-xr-xtest/functional/wallet_importprunedfunds.py20
-rwxr-xr-xtest/functional/wallet_inactive_hdchains.py147
-rwxr-xr-xtest/functional/wallet_taproot.py108
-rw-r--r--test/sanitizer_suppressions/ubsan1
139 files changed, 3785 insertions, 1831 deletions
diff --git a/.cirrus.yml b/.cirrus.yml
index 9b0e2dbd2e..e07ff9796e 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -82,7 +82,7 @@ task:
env:
PATH: 'C:\jom;C:\Python39;C:\Python39\Scripts;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin;%PATH%'
PYTHONUTF8: 1
- CI_VCPKG_TAG: '2021.05.12'
+ CI_VCPKG_TAG: '2022.02.23'
VCPKG_DOWNLOADS: 'C:\Users\ContainerAdministrator\AppData\Local\vcpkg\downloads'
VCPKG_DEFAULT_BINARY_CACHE: 'C:\Users\ContainerAdministrator\AppData\Local\vcpkg\archives'
QT_DOWNLOAD_URL: 'https://download.qt.io/official_releases/qt/5.15/5.15.2/single/qt-everywhere-src-5.15.2.zip'
@@ -130,6 +130,7 @@ task:
reupload_on_changes: true
fingerprint_script:
- echo %CI_VCPKG_TAG%
+ - type build_msvc\vcpkg.json
- msbuild -version
populate_script:
- mkdir %VCPKG_DEFAULT_BINARY_CACHE%
@@ -195,7 +196,7 @@ task:
FILE_ENV: "./ci/test/00_setup_env_i686_centos.sh"
task:
- name: '[previous releases, uses qt5 dev package and some depends packages, DEBUG] [unsigned char] [bionic]'
+ name: '[previous releases, uses qt5 dev package and some depends packages, DEBUG] [unsigned char] [buster]'
previous_releases_cache:
folder: "releases"
<< : *GLOBAL_TASK_TEMPLATE
@@ -258,13 +259,13 @@ task:
FILE_ENV: "./ci/test/00_setup_env_i686_multiprocess.sh"
task:
- name: '[no wallet] [bionic]'
+ name: '[no wallet, libbitcoinkernel] [bionic]'
<< : *GLOBAL_TASK_TEMPLATE
container:
image: ubuntu:bionic
env:
<< : *CIRRUS_EPHEMERAL_WORKER_TEMPLATE_ENV
- FILE_ENV: "./ci/test/00_setup_env_native_nowallet.sh"
+ FILE_ENV: "./ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh"
task:
name: 'macOS 10.15 [gui, no tests] [focal]'
diff --git a/.gitignore b/.gitignore
index f84a53178e..0f07a86401 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@ src/bitcoin-gui
src/bitcoin-node
src/bitcoin-tx
src/bitcoin-util
+src/bitcoin-chainstate
src/bitcoin-wallet
src/test/fuzz/fuzz
src/test/test_bitcoin
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 254e610393..ba78a20ae3 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -153,7 +153,8 @@ the pull request affects. Valid areas as:
- `test`, `qa` or `ci` for changes to the unit tests, QA tests or CI code
- `util` or `lib` for changes to the utils or libraries
- `wallet` for changes to the wallet code
- - `build` for changes to the GNU Autotools or reproducible builds
+ - `build` for changes to the GNU Autotools or MSVC builds
+ - `guix` for changes to the GUIX reproducible builds
Examples:
diff --git a/build-aux/m4/ax_cxx_compile_stdcxx.m4 b/build-aux/m4/ax_cxx_compile_stdcxx.m4
index 43087b2e68..51a35054d0 100644
--- a/build-aux/m4/ax_cxx_compile_stdcxx.m4
+++ b/build-aux/m4/ax_cxx_compile_stdcxx.m4
@@ -10,13 +10,13 @@
#
# Check for baseline language coverage in the compiler for the specified
# version of the C++ standard. If necessary, add switches to CXX and
-# CXXCPP to enable support. VERSION may be '11' (for the C++11 standard)
-# or '14' (for the C++14 standard).
+# CXXCPP to enable support. VERSION may be '11', '14', '17', or '20' for
+# the respective C++ standard version.
#
# The second argument, if specified, indicates whether you insist on an
# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
# -std=c++11). If neither is specified, you get whatever works, with
-# preference for an extended mode.
+# preference for no added switch, and then for an extended mode.
#
# The third argument, if specified 'mandatory' or if left unspecified,
# indicates that baseline support for the specified C++ standard is
@@ -35,13 +35,15 @@
# Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
# Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>
# Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
+# Copyright (c) 2020 Jason Merrill <jason@redhat.com>
+# Copyright (c) 2021 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
-#serial 11
+#serial 14
dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
dnl (serial version number 13).
@@ -50,6 +52,7 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
[$1], [14], [ax_cxx_compile_alternatives="14 1y"],
[$1], [17], [ax_cxx_compile_alternatives="17 1z"],
+ [$1], [20], [ax_cxx_compile_alternatives="20"],
[m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
m4_if([$2], [], [],
[$2], [ext], [],
@@ -62,6 +65,16 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
AC_LANG_PUSH([C++])dnl
ac_success=no
+ m4_if([$2], [], [dnl
+ AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
+ ax_cv_cxx_compile_cxx$1,
+ [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
+ [ax_cv_cxx_compile_cxx$1=yes],
+ [ax_cv_cxx_compile_cxx$1=no])])
+ if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
+ ac_success=yes
+ fi])
+
m4_if([$2], [noext], [], [dnl
if test x$ac_success = xno; then
for alternative in ${ax_cxx_compile_alternatives}; do
@@ -140,7 +153,6 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
_AX_CXX_COMPILE_STDCXX_testbody_new_in_11
)
-
dnl Test body for checking C++14 support
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
@@ -148,12 +160,24 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14
)
+dnl Test body for checking C++17 support
+
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
_AX_CXX_COMPILE_STDCXX_testbody_new_in_11
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14
_AX_CXX_COMPILE_STDCXX_testbody_new_in_17
)
+dnl Test body for checking C++20 support
+
+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
+)
+
+
dnl Tests for new features in C++11
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
@@ -949,3 +973,33 @@ namespace cxx17
#endif // __cplusplus < 201703L
]])
+
+
+dnl Tests for new features in C++20
+
+m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
+
+#ifndef __cplusplus
+
+#error "This is not a C++ compiler"
+
+#elif __cplusplus < 202002L
+
+#error "This is not a C++20 compiler"
+
+#else
+
+#include <version>
+
+namespace cxx20
+{
+
+// As C++20 supports feature test macros in the standard, there is no
+// immediate need to actually test for feature availability on the
+// Autoconf side.
+
+} // namespace cxx20
+
+#endif // __cplusplus < 202002L
+
+]])
diff --git a/build_msvc/test_bitcoin-qt/test_bitcoin-qt.vcxproj b/build_msvc/test_bitcoin-qt/test_bitcoin-qt.vcxproj
index d3be693e99..ec572b4f2e 100644
--- a/build_msvc/test_bitcoin-qt/test_bitcoin-qt.vcxproj
+++ b/build_msvc/test_bitcoin-qt/test_bitcoin-qt.vcxproj
@@ -12,6 +12,7 @@
<ClCompile Include="..\..\src\test\util\setup_common.cpp" />
<ClCompile Include="..\..\src\qt\test\addressbooktests.cpp" />
<ClCompile Include="..\..\src\qt\test\apptests.cpp" />
+ <ClCompile Include="..\..\src\qt\test\optiontests.cpp" />
<ClCompile Include="..\..\src\qt\test\rpcnestedtests.cpp" />
<ClCompile Include="..\..\src\qt\test\test_main.cpp" />
<ClCompile Include="..\..\src\qt\test\uritests.cpp" />
@@ -20,6 +21,7 @@
<ClCompile Include="..\..\src\wallet\test\wallet_test_fixture.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_addressbooktests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_apptests.cpp" />
+ <ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_optiontests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_rpcnestedtests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_uritests.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_wallettests.cpp" />
@@ -88,6 +90,7 @@
<ItemGroup>
<MocTestFiles Include="..\..\src\qt\test\addressbooktests.h" />
<MocTestFiles Include="..\..\src\qt\test\apptests.h" />
+ <MocTestFiles Include="..\..\src\qt\test\optiontests.h" />
<MocTestFiles Include="..\..\src\qt\test\rpcnestedtests.h" />
<MocTestFiles Include="..\..\src\qt\test\uritests.h" />
<MocTestFiles Include="..\..\src\qt\test\wallettests.h" />
diff --git a/build_msvc/vcpkg.json b/build_msvc/vcpkg.json
index d8753ec21f..48b4d1fd20 100644
--- a/build_msvc/vcpkg.json
+++ b/build_msvc/vcpkg.json
@@ -8,11 +8,18 @@
"boost-signals2",
"boost-test",
"sqlite3",
- "double-conversion",
{
"name": "libevent",
"features": ["thread"]
},
"zeromq"
+ ],
+ "builtin-baseline": "b86c0c35b88e2bf3557ff49dc831689c2f085090",
+ "overrides": [
+ {
+ "name": "zeromq",
+ "version": "4.3.4",
+ "port-version": 3
+ }
]
}
diff --git a/ci/test/00_setup_env_native_msan.sh b/ci/test/00_setup_env_native_msan.sh
index b8418106c6..39ecc64f30 100755
--- a/ci/test/00_setup_env_native_msan.sh
+++ b/ci/test/00_setup_env_native_msan.sh
@@ -11,13 +11,12 @@ LIBCXX_DIR="${BASE_SCRATCH_DIR}/msan/build/"
export MSAN_FLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls"
LIBCXX_FLAGS="-nostdinc++ -stdlib=libc++ -L${LIBCXX_DIR}lib -lc++abi -I${LIBCXX_DIR}include -I${LIBCXX_DIR}include/c++/v1 -lpthread -Wl,-rpath,${LIBCXX_DIR}lib -Wno-unused-command-line-argument"
export MSAN_AND_LIBCXX_FLAGS="${MSAN_FLAGS} ${LIBCXX_FLAGS}"
-export BDB_PREFIX="${BASE_ROOT_DIR}/db4"
export CONTAINER_NAME="ci_native_msan"
export PACKAGES="clang-9 llvm-9 cmake"
export DEP_OPTS="NO_BDB=1 NO_QT=1 CC='clang' CXX='clang++' CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' libevent_cflags='${MSAN_FLAGS}' sqlite_cflags='${MSAN_FLAGS}' zeromq_cxxflags='-std=c++17 ${MSAN_AND_LIBCXX_FLAGS}'"
export GOAL="install"
-export BITCOIN_CONFIG="--enable-wallet --with-sanitizers=memory --with-asm=no --prefix=${DEPENDS_DIR}/x86_64-pc-linux-gnu/ CC=clang CXX=clang++ CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' BDB_LIBS='-L${BDB_PREFIX}/lib -ldb_cxx-4.8' BDB_CFLAGS='-I${BDB_PREFIX}/include'"
+export BITCOIN_CONFIG="--enable-wallet --with-sanitizers=memory --with-asm=no --prefix=${DEPENDS_DIR}/x86_64-pc-linux-gnu/ CC=clang CXX=clang++ CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}'"
export USE_MEMORY_SANITIZER="true"
export RUN_FUNCTIONAL_TESTS="false"
export CCACHE_SIZE=250M
diff --git a/ci/test/00_setup_env_native_nowallet.sh b/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh
index d80a7f9633..763a5e8b6f 100755
--- a/ci/test/00_setup_env_native_nowallet.sh
+++ b/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh
@@ -6,9 +6,9 @@
export LC_ALL=C.UTF-8
-export CONTAINER_NAME=ci_native_nowallet
+export CONTAINER_NAME=ci_native_nowallet_libbitcoinkernel
export DOCKER_NAME_TAG=ubuntu:18.04 # Use bionic to have one config run the tests in python3.6, see doc/dependencies.md
-export PACKAGES="python3-zmq clang-7 llvm-7 libc++abi-7-dev libc++-7-dev" # Use clang-7 to test C++17 compatibility, see doc/dependencies.md
-export DEP_OPTS="NO_WALLET=1 CC=clang-7 CXX='clang++-7 -stdlib=libc++'"
+export PACKAGES="python3-zmq clang-8 llvm-8 libc++abi-8-dev libc++-8-dev" # Use clang-8 to test C++17 compatibility, see doc/dependencies.md
+export DEP_OPTS="NO_WALLET=1 CC=clang-8 CXX='clang++-8 -stdlib=libc++'"
export GOAL="install"
-export BITCOIN_CONFIG="--enable-reduce-exports CC=clang-7 CXX='clang++-7 -stdlib=libc++'"
+export BITCOIN_CONFIG="--enable-reduce-exports CC=clang-8 CXX='clang++-8 -stdlib=libc++' --enable-experimental-util-chainstate"
diff --git a/ci/test/00_setup_env_native_qt5.sh b/ci/test/00_setup_env_native_qt5.sh
index f8b366e77f..53f933f514 100755
--- a/ci/test/00_setup_env_native_qt5.sh
+++ b/ci/test/00_setup_env_native_qt5.sh
@@ -7,7 +7,7 @@
export LC_ALL=C.UTF-8
export CONTAINER_NAME=ci_native_qt5
-export DOCKER_NAME_TAG=ubuntu:18.04 # Check that bionic gcc-8 can compile our C++17 and run our functional tests in python3, see doc/dependencies.md
+export DOCKER_NAME_TAG=debian:buster # Check that buster gcc-8 can compile our C++17 and run our functional tests in python3, see doc/dependencies.md
export PACKAGES="gcc-8 g++-8 python3-zmq qtbase5-dev qttools5-dev-tools libdbus-1-dev libharfbuzz-dev"
export DEP_OPTS="NO_QT=1 NO_UPNP=1 NO_NATPMP=1 DEBUG=1 ALLOW_HOST_PACKAGES=1 CC=gcc-8 CXX=g++-8"
export TEST_RUNNER_EXTRA="--previous-releases --coverage --extended --exclude feature_dbcrash" # Run extended tests so that coverage does not fail, but exclude the very slow dbcrash
diff --git a/ci/test/00_setup_env_native_tsan.sh b/ci/test/00_setup_env_native_tsan.sh
index 0036255caf..ae942d892b 100755
--- a/ci/test/00_setup_env_native_tsan.sh
+++ b/ci/test/00_setup_env_native_tsan.sh
@@ -8,7 +8,7 @@ export LC_ALL=C.UTF-8
export CONTAINER_NAME=ci_native_tsan
export DOCKER_NAME_TAG=ubuntu:22.04
-export PACKAGES="clang llvm libc++abi-dev libc++-dev python3-zmq"
-export DEP_OPTS="CC=clang CXX='clang++ -stdlib=libc++'"
+export PACKAGES="clang-13 llvm-13 libc++abi-13-dev libc++-13-dev python3-zmq"
+export DEP_OPTS="CC=clang-13 CXX='clang++-13 -stdlib=libc++'"
export GOAL="install"
-export BITCOIN_CONFIG="--enable-zmq CPPFLAGS='-DARENA_DEBUG -DDEBUG_LOCKORDER' CXXFLAGS='-g' --with-sanitizers=thread CC=clang CXX='clang++ -stdlib=libc++'"
+export BITCOIN_CONFIG="--enable-zmq CPPFLAGS='-DARENA_DEBUG -DDEBUG_LOCKORDER' CXXFLAGS='-g' --with-sanitizers=thread CC=clang-13 CXX='clang++-13 -stdlib=libc++'"
diff --git a/ci/test/05_before_script.sh b/ci/test/05_before_script.sh
index 8f75fbd1fa..d8dcb708aa 100755
--- a/ci/test/05_before_script.sh
+++ b/ci/test/05_before_script.sh
@@ -36,13 +36,6 @@ if [ -n "$ANDROID_HOME" ] && [ ! -d "$ANDROID_HOME" ]; then
CI_EXEC "yes | ${ANDROID_HOME}/cmdline-tools/tools/bin/sdkmanager --install \"build-tools;${ANDROID_BUILD_TOOLS_VERSION}\" \"platform-tools\" \"platforms;android-${ANDROID_API_LEVEL}\" \"ndk;${ANDROID_NDK_VERSION}\""
fi
-if [[ ${USE_MEMORY_SANITIZER} == "true" ]]; then
- # Use BDB compiled using install_db4.sh script to work around linking issue when using BDB
- # from depends. See https://github.com/bitcoin/bitcoin/pull/18288#discussion_r433189350 for
- # details.
- CI_EXEC "contrib/install_db4.sh \$(pwd) --enable-umrw CC=clang CXX=clang++ CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}'"
-fi
-
if [ -z "$NO_DEPENDS" ]; then
if [[ $DOCKER_NAME_TAG == *centos* ]]; then
# CentOS has problems building the depends if the config shell is not explicitly set
diff --git a/configure.ac b/configure.ac
index 1e6ac93e78..63ff6a1ebd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
AC_PREREQ([2.69])
-define(_CLIENT_VERSION_MAJOR, 22)
+define(_CLIENT_VERSION_MAJOR, 23)
define(_CLIENT_VERSION_MINOR, 99)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_RC, 0)
@@ -24,6 +24,7 @@ BITCOIN_GUI_NAME=bitcoin-qt
BITCOIN_CLI_NAME=bitcoin-cli
BITCOIN_TX_NAME=bitcoin-tx
BITCOIN_UTIL_NAME=bitcoin-util
+BITCOIN_CHAINSTATE_NAME=bitcoin-chainstate
BITCOIN_WALLET_TOOL_NAME=bitcoin-wallet
dnl Multi Process
BITCOIN_MP_NODE_NAME=bitcoin-node
@@ -645,6 +646,12 @@ AC_ARG_ENABLE([util-util],
[build_bitcoin_util=$enableval],
[build_bitcoin_util=$build_bitcoin_utils])
+AC_ARG_ENABLE([experimental-util-chainstate],
+ [AS_HELP_STRING([--enable-experimental-util-chainstate],
+ [build experimental bitcoin-chainstate executable (default=no)])],
+ [build_bitcoin_chainstate=$enableval],
+ [build_bitcoin_chainstate=no])
+
AC_ARG_WITH([libs],
[AS_HELP_STRING([--with-libs],
[build libraries (default=yes)])],
@@ -1268,6 +1275,7 @@ if test "$enable_fuzz" = "yes"; then
build_bitcoin_cli=no
build_bitcoin_tx=no
build_bitcoin_util=no
+ build_bitcoin_chainstate=no
build_bitcoin_wallet=no
build_bitcoind=no
build_bitcoin_libs=no
@@ -1301,7 +1309,7 @@ else
BITCOIN_QT_INIT
dnl sets $bitcoin_enable_qt, $bitcoin_enable_qt_test, $bitcoin_enable_qt_dbus
- BITCOIN_QT_CONFIGURE([5.9.5])
+ BITCOIN_QT_CONFIGURE([5.11.3])
dnl Keep a copy of the original $QT_INCLUDES and use it when invoking qt's moc
QT_INCLUDES_UNSUPPRESSED=$QT_INCLUDES
@@ -1438,6 +1446,8 @@ if test "$use_external_signer" != "no"; then
;;
*)
AC_MSG_CHECKING([whether Boost.Process can be used])
+ TEMP_CPPFLAGS="$CPPFLAGS"
+ CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
TEMP_LDFLAGS="$LDFLAGS"
dnl Boost 1.73 and older require the following workaround.
LDFLAGS="$LDFLAGS $PTHREAD_CFLAGS"
@@ -1445,6 +1455,7 @@ if test "$use_external_signer" != "no"; then
[have_boost_process="yes"],
[have_boost_process="no"])
LDFLAGS="$TEMP_LDFLAGS"
+ CPPFLAGS="$TEMP_CPPFLAGS"
AC_MSG_RESULT([$have_boost_process])
if test "$have_boost_process" = "yes"; then
use_external_signer="yes"
@@ -1624,6 +1635,10 @@ AC_MSG_CHECKING([whether to build bitcoin-util])
AM_CONDITIONAL([BUILD_BITCOIN_UTIL], [test $build_bitcoin_util = "yes"])
AC_MSG_RESULT($build_bitcoin_util)
+AC_MSG_CHECKING([whether to build experimental bitcoin-chainstate])
+AM_CONDITIONAL([BUILD_BITCOIN_CHAINSTATE], [test $build_bitcoin_chainstate = "yes"])
+AC_MSG_RESULT($build_bitcoin_chainstate)
+
AC_MSG_CHECKING([whether to build libraries])
AM_CONDITIONAL([BUILD_BITCOIN_LIBS], [test $build_bitcoin_libs = "yes"])
if test "$build_bitcoin_libs" = "yes"; then
@@ -1843,6 +1858,7 @@ AC_SUBST(BITCOIN_GUI_NAME)
AC_SUBST(BITCOIN_CLI_NAME)
AC_SUBST(BITCOIN_TX_NAME)
AC_SUBST(BITCOIN_UTIL_NAME)
+AC_SUBST(BITCOIN_CHAINSTATE_NAME)
AC_SUBST(BITCOIN_WALLET_TOOL_NAME)
AC_SUBST(BITCOIN_MP_NODE_NAME)
AC_SUBST(BITCOIN_MP_GUI_NAME)
diff --git a/contrib/devtools/test-symbol-check.py b/contrib/devtools/test-symbol-check.py
index e1a2ebc491..b4c112b266 100755
--- a/contrib/devtools/test-symbol-check.py
+++ b/contrib/devtools/test-symbol-check.py
@@ -187,7 +187,7 @@ class TestSymbolChecks(unittest.TestCase):
executable = 'test3.exe'
with open(source, 'w', encoding="utf8") as f:
f.write('''
- #include <windows.h>
+ #include <combaseapi.h>
int main()
{
diff --git a/contrib/guix/guix-build b/contrib/guix/guix-build
index b38f91f3c7..c8f2e40f0a 100755
--- a/contrib/guix/guix-build
+++ b/contrib/guix/guix-build
@@ -121,7 +121,7 @@ else
fi
################
-# When building for darwin, the macOS SDK should exists
+# When building for darwin, the macOS SDK should exist
################
for host in $HOSTS; do
@@ -130,6 +130,7 @@ for host in $HOSTS; do
OSX_SDK="$(make -C "${PWD}/depends" --no-print-directory HOST="$host" print-OSX_SDK | sed 's@^[^=]\+=@@g')"
if [ -e "$OSX_SDK" ]; then
echo "Found macOS SDK at '${OSX_SDK}', using..."
+ break
else
echo "macOS SDK does not exist at '${OSX_SDK}', please place the extracted, untarred SDK there to perform darwin builds, exiting..."
exit 1
@@ -234,21 +235,6 @@ host_to_commonname() {
# Determine the reference time used for determinism (overridable by environment)
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git -c log.showSignature=false log --format=%at -1)}"
-# Execute "$@" in a pinned, possibly older version of Guix, for reproducibility
-# across time.
-time-machine() {
- # shellcheck disable=SC2086
- guix time-machine --url=https://git.savannah.gnu.org/git/guix.git \
- --commit=ae03f401381e956c4c41b4cf495cbde964fa43d0 \
- --cores="$JOBS" \
- --keep-failed \
- --fallback \
- ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
- ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_TIMEMACHINE_FLAGS} \
- -- "$@"
-}
-
-
# Precious directories are those which should not be cleaned between successive
# guix builds
depends_precious_dir_names='SOURCES_PATH BASE_CACHE SDK_PATH'
diff --git a/contrib/guix/guix-codesign b/contrib/guix/guix-codesign
index 2dd30bfa64..d726578277 100755
--- a/contrib/guix/guix-codesign
+++ b/contrib/guix/guix-codesign
@@ -155,7 +155,7 @@ unsigned_tarball_for_host() {
echo "$(outdir_for_host "$1")/${DISTNAME}-win-unsigned.tar.gz"
;;
*darwin*)
- echo "$(outdir_for_host "$1")/${DISTNAME}-osx-unsigned.tar.gz"
+ echo "$(outdir_for_host "$1")/${DISTNAME}-${1}-unsigned.tar.gz"
;;
*)
exit 1
@@ -222,20 +222,6 @@ JOBS="${JOBS:-$(nproc)}"
# Determine the reference time used for determinism (overridable by environment)
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git -c log.showSignature=false log --format=%at -1)}"
-# Execute "$@" in a pinned, possibly older version of Guix, for reproducibility
-# across time.
-time-machine() {
- # shellcheck disable=SC2086
- guix time-machine --url=https://git.savannah.gnu.org/git/guix.git \
- --commit=aa34d4d28dfe25ba47d5800d05000fb7221788c0 \
- --cores="$JOBS" \
- --keep-failed \
- --fallback \
- ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
- ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_TIMEMACHINE_FLAGS} \
- -- "$@"
-}
-
# Make sure an output directory exists for our builds
OUTDIR_BASE="${OUTDIR_BASE:-${VERSION_BASE}/output}"
mkdir -p "$OUTDIR_BASE"
diff --git a/contrib/guix/libexec/build.sh b/contrib/guix/libexec/build.sh
index e06a469338..7340b0625f 100755
--- a/contrib/guix/libexec/build.sh
+++ b/contrib/guix/libexec/build.sh
@@ -348,10 +348,10 @@ mkdir -p "$DISTSRC"
find . -print0 \
| sort --zero-terminated \
| tar --create --no-recursion --mode='u+rw,go+r-w,a+X' --null --files-from=- \
- | gzip -9n > "${OUTDIR}/${DISTNAME}-osx-unsigned.tar.gz" \
- || ( rm -f "${OUTDIR}/${DISTNAME}-osx-unsigned.tar.gz" && exit 1 )
+ | gzip -9n > "${OUTDIR}/${DISTNAME}-${HOST}-unsigned.tar.gz" \
+ || ( rm -f "${OUTDIR}/${DISTNAME}-${HOST}-unsigned.tar.gz" && exit 1 )
)
- make deploy ${V:+V=1} OSX_DMG="${OUTDIR}/${DISTNAME}-osx-unsigned.dmg"
+ make deploy ${V:+V=1} OSX_DMG="${OUTDIR}/${DISTNAME}-${HOST}-unsigned.dmg"
;;
esac
(
@@ -423,8 +423,8 @@ mkdir -p "$DISTSRC"
find "${DISTNAME}" -print0 \
| sort --zero-terminated \
| tar --create --no-recursion --mode='u+rw,go+r-w,a+X' --null --files-from=- \
- | gzip -9n > "${OUTDIR}/${DISTNAME}-${HOST//x86_64-apple-darwin/osx64}.tar.gz" \
- || ( rm -f "${OUTDIR}/${DISTNAME}-${HOST//x86_64-apple-darwin/osx64}.tar.gz" && exit 1 )
+ | gzip -9n > "${OUTDIR}/${DISTNAME}-${HOST}.tar.gz" \
+ || ( rm -f "${OUTDIR}/${DISTNAME}-${HOST}.tar.gz" && exit 1 )
;;
esac
) # $DISTSRC/installed
@@ -439,8 +439,8 @@ mkdir -p "$DISTSRC"
find . -print0 \
| sort --zero-terminated \
| tar --create --no-recursion --mode='u+rw,go+r-w,a+X' --null --files-from=- \
- | gzip -9n > "${OUTDIR}/${DISTNAME}-win-unsigned.tar.gz" \
- || ( rm -f "${OUTDIR}/${DISTNAME}-win-unsigned.tar.gz" && exit 1 )
+ | gzip -9n > "${OUTDIR}/${DISTNAME}-win64-unsigned.tar.gz" \
+ || ( rm -f "${OUTDIR}/${DISTNAME}-win64-unsigned.tar.gz" && exit 1 )
)
;;
esac
diff --git a/contrib/guix/libexec/codesign.sh b/contrib/guix/libexec/codesign.sh
index a8867ca351..6ede95f42b 100755
--- a/contrib/guix/libexec/codesign.sh
+++ b/contrib/guix/libexec/codesign.sh
@@ -91,7 +91,7 @@ mkdir -p "$DISTSRC"
-- -volume_date all_file_dates ="$SOURCE_DATE_EPOCH"
# Compress uncompressed.dmg and output to OUTDIR
- ./dmg dmg uncompressed.dmg "${OUTDIR}/${DISTNAME}-osx-signed.dmg"
+ ./dmg dmg uncompressed.dmg "${OUTDIR}/${DISTNAME}-${HOST}.dmg"
;;
*)
exit 1
diff --git a/contrib/guix/libexec/prelude.bash b/contrib/guix/libexec/prelude.bash
index 1f287e9bbb..086df48fbe 100644
--- a/contrib/guix/libexec/prelude.bash
+++ b/contrib/guix/libexec/prelude.bash
@@ -46,6 +46,22 @@ exit 1
fi
################
+# Execute "$@" in a pinned, possibly older version of Guix, for reproducibility
+# across time.
+time-machine() {
+ # shellcheck disable=SC2086
+ guix time-machine --url=https://git.savannah.gnu.org/git/guix.git \
+ --commit=ae03f401381e956c4c41b4cf495cbde964fa43d0 \
+ --cores="$JOBS" \
+ --keep-failed \
+ --fallback \
+ ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
+ ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_TIMEMACHINE_FLAGS} \
+ -- "$@"
+}
+
+
+################
# Set common variables
################
diff --git a/contrib/guix/manifest.scm b/contrib/guix/manifest.scm
index d296eb9543..008531e154 100644
--- a/contrib/guix/manifest.scm
+++ b/contrib/guix/manifest.scm
@@ -352,7 +352,7 @@ thus should be able to compile on most platforms where these exist.")
#t)))))))
(define-public python-certvalidator
- (let ((commit "e5bdb4bfcaa09fa0af355eb8867d00dfeecba08c"))
+ (let ((commit "a145bf25eb75a9f014b3e7678826132efbba6213"))
(package
(name "python-certvalidator")
(version (git-version "0.1" "1" commit))
@@ -365,7 +365,7 @@ thus should be able to compile on most platforms where these exist.")
(file-name (git-file-name name commit))
(sha256
(base32
- "18pvxkvpkfkzgvfylv0kx65pmxfcv1hpsg03cip93krfvrrl4c75"))))
+ "1qw2k7xis53179lpqdqyylbcmp76lj7sagp883wmxg5i7chhc96k"))))
(build-system python-build-system)
(propagated-inputs
`(("python-asn1crypto" ,python-asn1crypto)
@@ -401,11 +401,6 @@ thus should be able to compile on most platforms where these exist.")
(string-append indent
"@unittest.skip(\"Disabled by Guix\")\n"
line)))
- (substitute* "tests/test_validate.py"
- (("^(.*)def test_revocation_mode_soft" line indent)
- (string-append indent
- "@unittest.skip(\"Disabled by Guix\")\n"
- line)))
#t))
(replace 'check
(lambda _
@@ -490,7 +485,7 @@ and endian independent.")
(license license:expat)))
(define-public python-signapple
- (let ((commit "b084cbbf44d5330448ffce0c7d118f75781b64bd"))
+ (let ((commit "8a945a2e7583be2665cf3a6a89d665b70ecd1ab6"))
(package
(name "python-signapple")
(version (git-version "0.1" "1" commit))
@@ -503,7 +498,7 @@ and endian independent.")
(file-name (git-file-name name commit))
(sha256
(base32
- "0k7inccl2mzac3wq4asbr0kl8s4cghm8982z54kfascqg45shv01"))))
+ "0fr1hangvfyiwflca6jg5g8zvg3jc9qr7vd2c12ff89pznf38dlg"))))
(build-system python-build-system)
(propagated-inputs
`(("python-asn1crypto" ,python-asn1crypto)
@@ -593,24 +588,30 @@ inspecting signatures in Mach-O binaries.")
;; Git
git
;; Tests
- lief
- ;; Native gcc 7 toolchain
- gcc-toolchain-7
- (list gcc-toolchain-7 "static"))
+ lief)
(let ((target (getenv "HOST")))
(cond ((string-suffix? "-mingw32" target)
;; Windows
- (list zip
+ (list ;; Native GCC 10 toolchain
+ gcc-toolchain-10
+ (list gcc-toolchain-10 "static")
+ zip
(make-mingw-pthreads-cross-toolchain "x86_64-w64-mingw32")
(make-nsis-for-gcc-10 nsis-x86_64)
osslsigncode))
((string-contains target "-linux-")
- (list (cond ((string-contains target "riscv64-")
+ (list ;; Native GCC 7 toolchain
+ gcc-toolchain-7
+ (list gcc-toolchain-7 "static")
+ (cond ((string-contains target "riscv64-")
(make-bitcoin-cross-toolchain target
#:base-libc glibc-2.27/bitcoin-patched
#:base-kernel-headers linux-libre-headers-4.19))
(else
(make-bitcoin-cross-toolchain target)))))
((string-contains target "darwin")
- (list clang-toolchain-10 binutils cmake xorriso python-signapple))
+ (list ;; Native GCC 10 toolchain
+ gcc-toolchain-10
+ (list gcc-toolchain-10 "static")
+ clang-toolchain-10 binutils cmake xorriso python-signapple))
(else '())))))
diff --git a/contrib/macdeploy/detached-sig-create.sh b/contrib/macdeploy/detached-sig-create.sh
index 0d61ceb9df..f393331084 100755
--- a/contrib/macdeploy/detached-sig-create.sh
+++ b/contrib/macdeploy/detached-sig-create.sh
@@ -8,9 +8,11 @@ set -e
ROOTDIR=dist
BUNDLE="${ROOTDIR}/Bitcoin-Qt.app"
+BINARY="${BUNDLE}/Contents/MacOS/Bitcoin-Qt"
SIGNAPPLE=signapple
TEMPDIR=sign.temp
-OUT=signature-osx.tar.gz
+ARCH=$(${SIGNAPPLE} info ${BINARY} | head -n 1 | cut -d " " -f 1)
+OUT="signature-osx-${ARCH}.tar.gz"
OUTROOT=osx/dist
if [ -z "$1" ]; then
diff --git a/contrib/signet/miner b/contrib/signet/miner
index 012bd6cc31..b366b98e2d 100755
--- a/contrib/signet/miner
+++ b/contrib/signet/miner
@@ -8,7 +8,7 @@ import base64
import json
import logging
import math
-import os.path
+import os
import re
import struct
import sys
@@ -493,10 +493,11 @@ def do_generate(args):
logging.debug("Mining block delta=%s start=%s mine=%s", seconds_to_hms(mine_time-bestheader["time"]), mine_time, is_mine)
mined_blocks += 1
psbt = generate_psbt(tmpl, reward_spk, blocktime=mine_time)
- psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=psbt.encode('utf8')))
+ input_stream = os.linesep.join([psbt, "true", "ALL"]).encode('utf8')
+ psbt_signed = json.loads(args.bcli("-stdin", "walletprocesspsbt", input=input_stream))
if not psbt_signed.get("complete",False):
logging.debug("Generated PSBT: %s" % (psbt,))
- sys.stderr.write("PSBT signing failed")
+ sys.stderr.write("PSBT signing failed\n")
return 1
block, signet_solution = do_decode_psbt(psbt_signed["psbt"])
block = finish_block(block, signet_solution, args.grind_cmd)
diff --git a/depends/packages/libevent.mk b/depends/packages/libevent.mk
index 748ed510c1..1efe6220d3 100644
--- a/depends/packages/libevent.mk
+++ b/depends/packages/libevent.mk
@@ -36,5 +36,6 @@ define $(package)_stage_cmds
endef
define $(package)_postprocess_cmds
- rm lib/*.la
+ rm lib/*.la && \
+ rm include/ev*.h
endef
diff --git a/doc/README.md b/doc/README.md
index 2800937d30..c200ac3753 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -79,6 +79,7 @@ The Bitcoin repo's [root README](/README.md) contains relevant information on th
- [Init Scripts (systemd/upstart/openrc)](init.md)
- [Managing Wallets](managing-wallets.md)
- [Multisig Tutorial](multisig-tutorial.md)
+- [P2P bad ports definition and list](p2p-bad-ports.md)
- [PSBT support](psbt.md)
- [Reduce Memory](reduce-memory.md)
- [Reduce Traffic](reduce-traffic.md)
diff --git a/doc/build-freebsd.md b/doc/build-freebsd.md
index da2ab61c2a..d92c21efac 100644
--- a/doc/build-freebsd.md
+++ b/doc/build-freebsd.md
@@ -4,35 +4,6 @@
This guide describes how to build bitcoind, command-line utilities, and GUI on FreeBSD.
-## Dependencies
-
-The following dependencies are **required**:
-
- Library | Purpose | Description
- ----------------------------------------------------------------------|------------|----------------------
- [autoconf](https://svnweb.freebsd.org/ports/head/devel/autoconf/) | Build | Automatically configure software source code
- [automake](https://svnweb.freebsd.org/ports/head/devel/automake/) | Build | Generate makefile (requires autoconf)
- [libtool](https://svnweb.freebsd.org/ports/head/devel/libtool/) | Build | Shared library support
- [pkgconf](https://svnweb.freebsd.org/ports/head/devel/pkgconf/) | Build | Configure compiler and linker flags
- [git](https://svnweb.freebsd.org/ports/head/devel/git/) | Clone | Version control system
- [gmake](https://svnweb.freebsd.org/ports/head/devel/gmake/) | Compile | Generate executables
- [boost-libs](https://svnweb.freebsd.org/ports/head/devel/boost-libs/) | Utility | Library for threading, data structures, etc
- [libevent](https://svnweb.freebsd.org/ports/head/devel/libevent/) | Networking | OS independent asynchronous networking
-
-
-The following dependencies are **optional**:
-
- Library | Purpose | Description
- ---------------------------------------------------------------------------|------------------|----------------------
- [db5](https://svnweb.freebsd.org/ports/head/databases/db5/) | Berkeley DB | Wallet storage (only needed when wallet enabled)
- [qt5](https://svnweb.freebsd.org/ports/head/devel/qt5/) | GUI | GUI toolkit (only needed when GUI enabled)
- [libqrencode](https://svnweb.freebsd.org/ports/head/graphics/libqrencode/) | QR codes in GUI | Generating QR codes (only needed when GUI enabled)
- [libzmq4](https://svnweb.freebsd.org/ports/head/net/libzmq4/) | ZMQ notification | Allows generating ZMQ notifications (requires ZMQ version >= 4.0.0)
- [sqlite3](https://svnweb.freebsd.org/ports/head/databases/sqlite3/) | SQLite DB | Wallet storage (only needed when wallet enabled)
- [python3](https://svnweb.freebsd.org/ports/head/lang/python3/) | Testing | Python Interpreter (only needed when running the test suite)
-
- See [dependencies.md](dependencies.md) for a complete overview.
-
## Preparation
### 1. Install Required Dependencies
@@ -43,6 +14,8 @@ pkg install autoconf automake boost-libs git gmake libevent libtool pkgconf
```
+See [dependencies.md](dependencies.md) for a complete overview.
+
### 2. Clone Bitcoin Repo
Now that `git` and all the required dependencies are installed, let's clone the Bitcoin Core repository to a directory. All build scripts and commands will run from this directory.
``` bash
@@ -84,6 +57,14 @@ pkg install libqrencode
```
---
+#### Notifications
+###### ZeroMQ
+
+Bitcoin Core can provide notifications via ZeroMQ. If the package is installed, support will be compiled in.
+```bash
+pkg install libzmq4
+```
+
#### Test Suite Dependencies
There is an included test suite that is useful for testing code changes when developing.
To run the test suite (recommended), you will need to have Python 3 installed:
diff --git a/doc/build-netbsd.md b/doc/build-netbsd.md
index edabd71611..762406bf6b 100644
--- a/doc/build-netbsd.md
+++ b/doc/build-netbsd.md
@@ -57,7 +57,6 @@ With wallet:
./configure --with-gui=no CPPFLAGS="-I/usr/pkg/include" \
LDFLAGS="-L/usr/pkg/lib" \
BOOST_CPPFLAGS="-I/usr/pkg/include" \
- BOOST_LDFLAGS="-L/usr/pkg/lib" \
BDB_LIBS="-L${BDB_PREFIX}/lib -ldb_cxx-4.8" \
BDB_CFLAGS="-I${BDB_PREFIX}/include" \
MAKE=gmake
@@ -70,7 +69,6 @@ Without wallet:
CPPFLAGS="-I/usr/pkg/include" \
LDFLAGS="-L/usr/pkg/lib" \
BOOST_CPPFLAGS="-I/usr/pkg/include" \
- BOOST_LDFLAGS="-L/usr/pkg/lib" \
MAKE=gmake
```
diff --git a/doc/build-osx.md b/doc/build-osx.md
index 16dc224aed..84495cfdaf 100644
--- a/doc/build-osx.md
+++ b/doc/build-osx.md
@@ -4,42 +4,6 @@
This guide describes how to build bitcoind, command-line utilities, and GUI on macOS
-**Note:** The following is for Intel Macs only!
-
-## Dependencies
-
-The following dependencies are **required**:
-
-Library | Purpose | Description
------------------------------------------------------------|------------|----------------------
-[automake](https://formulae.brew.sh/formula/automake) | Build | Generate makefile
-[libtool](https://formulae.brew.sh/formula/libtool) | Build | Shared library support
-[pkg-config](https://formulae.brew.sh/formula/pkg-config) | Build | Configure compiler and linker flags
-[boost](https://formulae.brew.sh/formula/boost) | Utility | Library for threading, data structures, etc
-[libevent](https://formulae.brew.sh/formula/libevent) | Networking | OS independent asynchronous networking
-
-The following dependencies are **optional**:
-
-Library | Purpose | Description
---------------------------------------------------------------- |------------------|----------------------
-[berkeley-db@4](https://formulae.brew.sh/formula/berkeley-db@4) | Berkeley DB | Wallet storage (only needed when wallet enabled)
-[qt@5](https://formulae.brew.sh/formula/qt@5) | GUI | GUI toolkit (only needed when GUI enabled)
-[qrencode](https://formulae.brew.sh/formula/qrencode) | QR codes in GUI | Generating QR codes (only needed when GUI enabled)
-[zeromq](https://formulae.brew.sh/formula/zeromq) | ZMQ notification | Allows generating ZMQ notifications (requires ZMQ version >= 4.0.0)
-[sqlite](https://formulae.brew.sh/formula/sqlite) | SQLite DB | Wallet storage (only needed when wallet enabled)
-[miniupnpc](https://formulae.brew.sh/formula/miniupnpc) | UPnP Support | Firewall-jumping support (needed for port mapping support)
-[libnatpmp](https://formulae.brew.sh/formula/libnatpmp) | NAT-PMP Support | Firewall-jumping support (needed for port mapping support)
-[python3](https://formulae.brew.sh/formula/python@3.9) | Testing | Python Interpreter (only needed when running the test suite)
-
-The following dependencies are **optional** packages required for deploying:
-
-Library | Purpose | Description
-----------------------------------------------------|------------------|----------------------
-[ds_store](https://pypi.org/project/ds-store/) | Deploy Dependency| Examine and modify .DS_Store files
-[mac_alias](https://pypi.org/project/mac-alias/) | Deploy Dependency| Generate/Read binary alias and bookmark records
-
-See [dependencies.md](dependencies.md) for a complete overview.
-
## Preparation
The commands in this guide should be executed in a Terminal application.
@@ -78,6 +42,9 @@ Note: If you run into issues while installing Homebrew or pulling packages, refe
The first step is to download the required dependencies.
These dependencies represent the packages required to get a barebones installation up and running.
+
+See [dependencies.md](dependencies.md) for a complete overview.
+
To install, run the following from your terminal:
``` bash
diff --git a/doc/build-unix.md b/doc/build-unix.md
index 15fe63d047..e56c78b0b7 100644
--- a/doc/build-unix.md
+++ b/doc/build-unix.md
@@ -26,30 +26,7 @@ make install # optional
This will build bitcoin-qt as well, if the dependencies are met.
-Dependencies
----------------------
-
-These dependencies are required:
-
- Library | Purpose | Description
- ------------|------------------|----------------------
- libboost | Utility | Library for threading, data structures, etc
- libevent | Networking | OS independent asynchronous networking
-
-Optional dependencies:
-
- Library | Purpose | Description
- ------------|------------------|----------------------
- miniupnpc | UPnP Support | Firewall-jumping support
- libnatpmp | NAT-PMP Support | Firewall-jumping support
- libdb4.8 | Berkeley DB | Wallet storage (only needed when legacy wallet enabled)
- qt | GUI | GUI toolkit (only needed when GUI enabled)
- libqrencode | QR codes in GUI | QR code generation (only needed when GUI enabled)
- libzmq3 | ZMQ notification | ZMQ notifications (requires ZMQ version >= 4.0.0)
- sqlite3 | SQLite DB | Wallet storage (only needed when descriptor wallet enabled)
- systemtap | Tracing (USDT) | Statically defined tracepoints
-
-For the versions used, see [dependencies.md](dependencies.md)
+See [dependencies.md](dependencies.md) for a complete overview.
Memory Requirements
--------------------
diff --git a/doc/dependencies.md b/doc/dependencies.md
index 99ff26f457..33c79696ff 100644
--- a/doc/dependencies.md
+++ b/doc/dependencies.md
@@ -1,48 +1,49 @@
-Dependencies
-============
-
-These are the dependencies currently used by Bitcoin Core. You can find instructions for installing them in the `build-*.md` file for your platform.
-
-| Dependency | Version used | Minimum required | CVEs | Shared | [Bundled Qt library](https://doc.qt.io/qt-5/configure-options.html#third-party-libraries) |
-| --- | --- | --- | --- | --- | --- |
-| Berkeley DB | [4.8.30](https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html) | 4.8.x | No | | |
-| Boost | [1.77.0](https://www.boost.org/users/download/) | [1.64.0](https://github.com/bitcoin/bitcoin/pull/22320) | No | | |
-| Clang<sup>[ \* ](#note1)</sup> | | [7.0](https://releases.llvm.org/download.html) (C++17 & std::filesystem support) | | | |
-| Fontconfig | [2.12.6](https://www.freedesktop.org/software/fontconfig/release/) | | No | Yes | |
-| FreeType | [2.11.0](https://download.savannah.gnu.org/releases/freetype) | | No | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) (Android only) |
-| GCC | | [8.1](https://gcc.gnu.org/) (C++17 & std::filesystem support) | | | |
-| glibc | | [2.18](https://www.gnu.org/software/libc/) | | | | |
-| HarfBuzz-NG | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) |
-| libevent | [2.1.12-stable](https://github.com/libevent/libevent/releases) | [2.0.21](https://github.com/bitcoin/bitcoin/pull/18676) | No | | |
-| libnatpmp | git commit [4536032...](https://github.com/miniupnp/libnatpmp/tree/4536032ae32268a45c073a4d5e91bbab4534773a) | | No | | |
-| libpng | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) |
-| MiniUPnPc | [2.2.2](https://miniupnp.tuxfamily.org/files) | | No | | |
-| PCRE | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) |
-| Python (tests) | | [3.6](https://www.python.org/downloads) | | | |
-| qrencode | [3.4.4](https://fukuchi.org/works/qrencode) | | No | | |
-| Qt | [5.15.2](https://download.qt.io/official_releases/qt/) | [5.9.5](https://github.com/bitcoin/bitcoin/issues/20104) | No | | |
-| SQLite | [3.32.1](https://sqlite.org/download.html) | [3.7.17](https://github.com/bitcoin/bitcoin/pull/19077) | | | |
-| XCB | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) (Linux only) |
-| systemtap ([tracing](tracing.md))| [4.5](https://sourceware.org/systemtap/ftp/releases/) | | | | |
-| xkbcommon | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) (Linux only) |
-| ZeroMQ | [4.3.1](https://github.com/zeromq/libzmq/releases) | 4.0.0 | No | | |
-| zlib | | | | | [Yes](https://github.com/bitcoin/bitcoin/blob/master/depends/packages/qt.mk) |
-
-<a name="note1">Note \*</a> : When compiling with `-stdlib=libc++`, the minimum supported libc++ version is 7.0.
-
-Controlling dependencies
-------------------------
-Some dependencies are not needed in all configurations. The following are some factors that affect the dependency list.
-
-#### Options passed to `./configure`
-* MiniUPnPc is not needed with `--without-miniupnpc`.
-* libnatpmp is not needed with `--without-natpmp`.
-* Berkeley DB is not needed with `--disable-wallet` or `--without-bdb`.
-* SQLite is not needed with `--disable-wallet` or `--without-sqlite`.
-* Qt is not needed with `--without-gui`.
-* If the qrencode dependency is absent, QR support won't be added. To force an error when that happens, pass `--with-qrencode`.
-* If the systemtap dependency is absent, USDT support won't compiled in.
-* ZeroMQ is needed only with the `--with-zmq` option.
-
-#### Other
-* Not-Qt-bundled zlib is required to build the [DMG tool](../contrib/macdeploy/README.md#deterministic-macos-dmg-notes) from the libdmg-hfsplus project.
+# Dependencies
+
+These are the dependencies used by Bitcoin Core.
+You can find installation instructions in the `build-*.md` file for your platform.
+"Runtime" and "Version Used" are both in reference to the release binaries.
+
+| Compiler | Minimum required |
+| --- | --- |
+| [Autoconf](https://www.gnu.org/software/autoconf/) | [2.69](https://github.com/bitcoin/bitcoin/pull/17769) |
+| [Automake](https://www.gnu.org/software/automake/) | [1.13](https://github.com/bitcoin/bitcoin/pull/18290) |
+| [Clang](https://clang.llvm.org) | [8.0](https://github.com/bitcoin/bitcoin/pull/24164) |
+| [GCC](https://gcc.gnu.org) | [8.1](https://github.com/bitcoin/bitcoin/pull/23060) |
+| [Python](https://www.python.org) (tests) | [3.6](https://github.com/bitcoin/bitcoin/pull/19504) |
+| [systemtap](https://sourceware.org/systemtap/) ([tracing](tracing.md))| N/A |
+
+## Required
+
+| Dependency | Version used | Minimum required | Runtime |
+| --- | --- | --- | --- |
+| [Boost](https://www.boost.org/users/download/) | 1.77.0 | [1.64.0](https://github.com/bitcoin/bitcoin/pull/22320) | No |
+| [libevent](https://github.com/libevent/libevent/releases) | 2.1.12-stable | [2.0.21](https://github.com/bitcoin/bitcoin/pull/18676) | No |
+| [glibc](https://www.gnu.org/software/libc/) | N/A | [2.18](https://github.com/bitcoin/bitcoin/pull/23511) | Yes |
+
+## Optional
+
+### GUI
+| Dependency | Version used | Minimum required | Runtime |
+| --- | --- | --- | --- |
+| [Fontconfig](https://www.freedesktop.org/wiki/Software/fontconfig/) | 2.12.6 | 2.6 | Yes |
+| [FreeType](https://freetype.org) | 2.11.0 | 2.3.0 | Yes |
+| [qrencode](https://fukuchi.org/works/qrencode/) | [3.4.4](https://fukuchi.org/works/qrencode) | | No |
+| [Qt](https://www.qt.io) | [5.15.2](https://download.qt.io/official_releases/qt/) | [5.11.3](https://github.com/bitcoin/bitcoin/pull/24132) | No |
+
+### Networking
+| Dependency | Version used | Minimum required | Runtime |
+| --- | --- | --- | --- |
+| [libnatpmp](https://github.com/miniupnp/libnatpmp/) | commit [4536032...](https://github.com/miniupnp/libnatpmp/tree/4536032ae32268a45c073a4d5e91bbab4534773a) | | No |
+| [MiniUPnPc](https://miniupnp.tuxfamily.org/) | 2.2.2 | 1.9 | No |
+
+### Notifications
+| Dependency | Version used | Minimum required | Runtime |
+| --- | --- | --- | --- |
+| [ZeroMQ](https://zeromq.org) | 4.3.4 | 4.0.0 | No |
+
+### Wallet
+| Dependency | Version used | Minimum required | Runtime |
+| --- | --- | --- | --- |
+| [Berkeley DB](https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html) (legacy wallet) | 4.8.30 | 4.8.x | No |
+| [SQLite](https://sqlite.org) | 3.32.1 | [3.7.17](https://github.com/bitcoin/bitcoin/pull/19077) | No |
diff --git a/doc/descriptors.md b/doc/descriptors.md
index 318d065fdb..ab2face4f0 100644
--- a/doc/descriptors.md
+++ b/doc/descriptors.md
@@ -33,6 +33,7 @@ Output descriptors currently support:
- Pay-to-taproot outputs (P2TR), through the `tr` function.
- Multisig scripts, through the `multi` function.
- Multisig scripts where the public keys are sorted lexicographically, through the `sortedmulti` function.
+- Multisig scripts inside taproot script trees, through the `multi_a` (and `sortedmulti_a`) function.
- Any type of supported address through the `addr` function.
- Raw hex scripts through the `raw` function.
- Public keys (compressed and uncompressed) in hex notation, or BIP32 extended pubkeys with derivation paths.
@@ -56,6 +57,7 @@ Output descriptors currently support:
- `wsh(multi(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where the first multisig key is the *1/0/`i`* child of the first specified xpub and the second multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default).
- `wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where one multisig key is the *1/0/`i`* child of the first specified xpub and the other multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default). The order of public keys in the resulting witnessScripts is determined by the lexicographic order of the public keys at that index.
- `tr(c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5,{pk(fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556),pk(e493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13)})` describes a P2TR output with the `c6...` x-only pubkey as internal key, and two script paths.
+- `tr(c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5,sortedmulti_a(2,2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4,5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc))` describes a P2TR output with the `c6...` x-only pubkey as internal key, and a single `multi_a` script that needs 2 signatures with 2 specified x-only keys, which will be sorted lexicographically.
## Reference
@@ -68,8 +70,10 @@ Descriptors consist of several types of expressions. The top level expression is
- `pkh(KEY)` (not inside `tr`): P2PKH output for the given public key (use `addr` if you only know the pubkey hash).
- `wpkh(KEY)` (top level or inside `sh` only): P2WPKH output for the given compressed pubkey.
- `combo(KEY)` (top level only): an alias for the collection of `pk(KEY)` and `pkh(KEY)`. If the key is compressed, it also includes `wpkh(KEY)` and `sh(wpkh(KEY))`.
-- `multi(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script.
+- `multi(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script using OP_CHECKMULTISIG.
- `sortedmulti(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script with keys sorted lexicographically in the resulting script.
+- `multi_a(k,KEY_1,KEY_2,...,KEY_N)` (only inside `tr`): k-of-n multisig script using OP_CHECKSIG, OP_CHECKSIGADD, and OP_NUMEQUAL.
+- `sortedmulti_a(k,KEY_1,KEY_2,...,KEY_N)` (only inside `tr`): similar to `multi_a`, but the (x-only) public keys in it will be sorted lexicographically.
- `tr(KEY)` or `tr(KEY,TREE)` (top level only): P2TR output with the specified key as internal key, and optionally a tree of script paths.
- `addr(ADDR)` (top level only): the script which ADDR expands to.
- `raw(HEX)` (top level only): the script whose hex encoding is HEX.
diff --git a/doc/i2p.md b/doc/i2p.md
index ee650f3999..e45b5efb9b 100644
--- a/doc/i2p.md
+++ b/doc/i2p.md
@@ -65,9 +65,9 @@ logging` for more information.
-onlynet=i2p
```
-Make outgoing connections only to I2P addresses. Incoming connections are not
-affected by this option. It can be specified multiple times to allow multiple
-network types, e.g. onlynet=onion, onlynet=i2p.
+Make automatic outbound connections only to I2P addresses. Inbound and manual
+connections are not affected by this option. It can be specified multiple times
+to allow multiple networks, e.g. onlynet=onion, onlynet=i2p.
I2P support was added to Bitcoin Core in version 22.0 and there may be fewer I2P
peers than Tor or IP ones. Therefore, using I2P alone without other networks may
diff --git a/doc/multisig-tutorial.md b/doc/multisig-tutorial.md
index 0793040418..1d2b3244bc 100644
--- a/doc/multisig-tutorial.md
+++ b/doc/multisig-tutorial.md
@@ -29,7 +29,7 @@ These three wallets should not be used directly for privacy reasons (public key
```bash
for ((n=1;n<=3;n++))
do
- ./src/bitcoin-cli -signet -named createwallet wallet_name="participant_${n}" descriptors=true
+ ./src/bitcoin-cli -signet createwallet "participant_${n}"
done
```
@@ -109,7 +109,7 @@ Then import the descriptors created in the previous step using the `importdescri
After that, `getwalletinfo` can be used to check if the wallet was created successfully.
```bash
-./src/bitcoin-cli -signet -named createwallet wallet_name="multisig_wallet_01" disable_private_keys=true blank=true descriptors=true
+./src/bitcoin-cli -signet -named createwallet wallet_name="multisig_wallet_01" disable_private_keys=true blank=true
./src/bitcoin-cli -signet -rpcwallet="multisig_wallet_01" importdescriptors "$multisig_desc"
@@ -238,4 +238,4 @@ psbt_2=$(./src/bitcoin-cli -signet -rpcwallet="participant_2" walletprocesspsbt
finalized_psbt_hex=$(./src/bitcoin-cli -signet finalizepsbt $psbt_2 | jq -r '.hex')
./src/bitcoin-cli -signet sendrawtransaction $finalized_psbt_hex
-``` \ No newline at end of file
+```
diff --git a/doc/p2p-bad-ports.md b/doc/p2p-bad-ports.md
new file mode 100644
index 0000000000..4f717f97a2
--- /dev/null
+++ b/doc/p2p-bad-ports.md
@@ -0,0 +1,114 @@
+When Bitcoin Core automatically opens outgoing P2P connections, it chooses
+a peer (address and port) from its list of potential peers. This list is
+populated with unchecked data gossiped over the P2P network by other peers.
+
+A malicious actor may gossip an address:port where no Bitcoin node is listening,
+or one where a service is listening that is not related to the Bitcoin network.
+As a result, this service may occasionally get connection attempts from Bitcoin
+nodes.
+
+"Bad" ports are ones used by services which are usually not open to the public
+and usually require authentication. A connection attempt (by Bitcoin Core,
+trying to connect because it thinks there is a Bitcoin node on that
+address:port) to such service may be considered a malicious action by an
+ultra-paranoid administrator. An example for such a port is 22 (ssh). On the
+other hand, connection attempts to public services that usually do not require
+authentication are unlikely to be considered a malicious action,
+e.g. port 80 (http).
+
+Below is a list of "bad" ports which Bitcoin Core avoids when choosing a peer to
+connect to. If a node is listening on such a port, it will likely receive fewer
+incoming connections.
+
+ 1: tcpmux
+ 7: echo
+ 9: discard
+ 11: systat
+ 13: daytime
+ 15: netstat
+ 17: qotd
+ 19: chargen
+ 20: ftp data
+ 21: ftp access
+ 22: ssh
+ 23: telnet
+ 25: smtp
+ 37: time
+ 42: name
+ 43: nicname
+ 53: domain
+ 69: tftp
+ 77: priv-rjs
+ 79: finger
+ 87: ttylink
+ 95: supdup
+ 101: hostname
+ 102: iso-tsap
+ 103: gppitnp
+ 104: acr-nema
+ 109: pop2
+ 110: pop3
+ 111: sunrpc
+ 113: auth
+ 115: sftp
+ 117: uucp-path
+ 119: nntp
+ 123: NTP
+ 135: loc-srv /epmap
+ 137: netbios
+ 139: netbios
+ 143: imap2
+ 161: snmp
+ 179: BGP
+ 389: ldap
+ 427: SLP (Also used by Apple Filing Protocol)
+ 465: smtp+ssl
+ 512: print / exec
+ 513: login
+ 514: shell
+ 515: printer
+ 526: tempo
+ 530: courier
+ 531: chat
+ 532: netnews
+ 540: uucp
+ 548: AFP (Apple Filing Protocol)
+ 554: rtsp
+ 556: remotefs
+ 563: nntp+ssl
+ 587: smtp (rfc6409)
+ 601: syslog-conn (rfc3195)
+ 636: ldap+ssl
+ 989: ftps-data
+ 990: ftps
+ 993: ldap+ssl
+ 995: pop3+ssl
+ 1719: h323gatestat
+ 1720: h323hostcall
+ 1723: pptp
+ 2049: nfs
+ 3659: apple-sasl / PasswordServer
+ 4045: lockd
+ 5060: sip
+ 5061: sips
+ 6000: X11
+ 6566: sane-port
+ 6665: Alternate IRC
+ 6666: Alternate IRC
+ 6667: Standard IRC
+ 6668: Alternate IRC
+ 6669: Alternate IRC
+ 6697: IRC + TLS
+ 10080: Amanda
+
+For further information see:
+
+[pull/23306](https://github.com/bitcoin/bitcoin/pull/23306#issuecomment-947516736)
+
+[pull/23542](https://github.com/bitcoin/bitcoin/pull/23542)
+
+[fetch.spec.whatwg.org](https://fetch.spec.whatwg.org/#port-blocking)
+
+[chromium.googlesource.com](https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/net/base/port_util.cc)
+
+[hg.mozilla.org](https://hg.mozilla.org/mozilla-central/file/tip/netwerk/base/nsIOService.cpp)
diff --git a/doc/release-notes-24198.md b/doc/release-notes-24198.md
new file mode 100644
index 0000000000..e41b2a8e26
--- /dev/null
+++ b/doc/release-notes-24198.md
@@ -0,0 +1,6 @@
+Updated RPCs
+------------
+
+- The `listtransactions`, `gettransaction`, and `listsinceblock`
+ RPC methods now include a wtxid field (hash of serialized transaction,
+ including witness data) for each transaction. \ No newline at end of file
diff --git a/doc/release-notes.md b/doc/release-notes.md
index 5d1aa85593..2342342ae2 100644
--- a/doc/release-notes.md
+++ b/doc/release-notes.md
@@ -6,7 +6,7 @@ template to create the initial release notes draft.*
for the process.*
*Create the draft, named* "*version* Release Notes Draft"
-*(e.g. "22.0 Release Notes Draft"), as a collaborative wiki in:*
+*(e.g. "23.0 Release Notes Draft"), as a collaborative wiki in:*
https://github.com/bitcoin-core/bitcoin-devwiki/wiki/
@@ -54,174 +54,9 @@ unsupported systems.
Notable changes
===============
-P2P and network changes
------------------------
-
-- A bitcoind node will no longer rumour addresses to inbound peers by default.
- They will become eligible for address gossip after sending an ADDR, ADDRV2,
- or GETADDR message. (#21528)
-
-Fee estimation changes
-----------------------
-
-- Fee estimation now takes the feerate of replacement (RBF) transactions into
- account. (#22539)
-
-Rescan startup parameter removed
---------------------------------
-
-The `-rescan` startup parameter has been removed. Wallets which require
-rescanning due to corruption will still be rescanned on startup.
-Otherwise, please use the `rescanblockchain` RPC to trigger a rescan. (#23123)
-
-Updated RPCs
-------------
-
-- The `validateaddress` RPC now returns an `error_locations` array for invalid
- addresses, with the indices of invalid character locations in the address (if
- known). For example, this will attempt to locate up to two Bech32 errors, and
- return their locations if successful. Success and correctness are only guaranteed
- if fewer than two substitution errors have been made.
- The error message returned in the `error` field now also returns more specific
- errors when decoding fails. (#16807)
-
-- The `-deprecatedrpc=addresses` configuration option has been removed. RPCs
- `gettxout`, `getrawtransaction`, `decoderawtransaction`, `decodescript`,
- `gettransaction verbose=true` and REST endpoints `/rest/tx`, `/rest/getutxos`,
- `/rest/block` no longer return the `addresses` and `reqSigs` fields, which
- were previously deprecated in 22.0. (#22650)
-- The `getblock` RPC command now supports verbosity level 3 containing transaction inputs'
- `prevout` information. The existing `/rest/block/` REST endpoint is modified to contain
- this information too. Every `vin` field will contain an additional `prevout` subfield
- describing the spent output. `prevout` contains the following keys:
- - `generated` - true if the spent coins was a coinbase.
- - `height`
- - `value`
- - `scriptPubKey`
-
-- The top-level fee fields `fee`, `modifiedfee`, `ancestorfees` and `descendantfees`
- returned by RPCs `getmempoolentry`,`getrawmempool(verbose=true)`,
- `getmempoolancestors(verbose=true)` and `getmempooldescendants(verbose=true)`
- are deprecated and will be removed in the next major version (use
- `-deprecated=fees` if needed in this version). The same fee fields can be accessed
- through the `fees` object in the result. WARNING: deprecated
- fields `ancestorfees` and `descendantfees` are denominated in sats, whereas all
- fields in the `fees` object are denominated in BTC. (#22689)
-
-- Both `createmultisig` and `addmultisigaddress` now include a `warnings`
- field, which will show a warning if a non-legacy address type is requested
- when using uncompressed public keys. (#23113)
-
-New RPCs
---------
-
-- Information on soft fork status has been moved from `getblockchaininfo`
- to the new `getdeploymentinfo` RPC which allows querying soft fork status at any
- block, rather than just at the chain tip. Inclusion of soft fork
- status in `getblockchaininfo` can currently be restored using the
- configuration `-deprecatedrpc=softforks`, but this will be removed in
- a future release. Note that in either case, the `status` field
- now reflects the status of the current block rather than the next
- block. (#23508)
-
-Build System
+Example item
------------
-Files
------
-
-* On startup, the list of banned hosts and networks (via `setban` RPC) in
- `banlist.dat` is ignored and only `banlist.json` is considered. Bitcoin Core
- version 22.x is the only version that can read `banlist.dat` and also write
- it to `banlist.json`. If `banlist.json` already exists, version 22.x will not
- try to translate the `banlist.dat` into json. After an upgrade, `listbanned`
- can be used to double check the parsed entries. (#22570)
-
-New settings
-------------
-
-Updated settings
-----------------
-
-- In previous releases, the meaning of the command line option
- `-persistmempool` (without a value provided) incorrectly disabled mempool
- persistence. `-persistmempool` is now treated like other boolean options to
- mean `-persistmempool=1`. Passing `-persistmempool=0`, `-persistmempool=1`
- and `-nopersistmempool` is unaffected. (#23061)
-
-- `-maxuploadtarget` now allows human readable byte units [k|K|m|M|g|G|t|T].
- E.g. `-maxuploadtarget=500g`. No whitespace, +- or fractions allowed.
- Default is `M` if no suffix provided. (#23249)
-
-- If `-proxy=` is given together with `-noonion` then the provided proxy will
- not be set as a proxy for reaching the Tor network. So it will not be
- possible to open manual connections to the Tor network for example with the
- `addnode` RPC. To mimic the old behavior use `-proxy=` together with
- `-onlynet=` listing all relevant networks except `onion`. (#22834)
-
-Tools and Utilities
--------------------
-
-- Update `-getinfo` to return data in a user-friendly format that also reduces vertical space. (#21832)
-
-- CLI `-addrinfo` now returns a single field for the number of `onion` addresses
- known to the node instead of separate `torv2` and `torv3` fields, as support
- for Tor V2 addresses was removed from Bitcoin Core in 22.0. (#22544)
-
-Wallet
-------
-
-- `upgradewallet` will now automatically flush the keypool if upgrading
- from a non-HD wallet to an HD wallet, to immediately start using the
- newly-generated HD keys. (#23093)
-
-- a new RPC `newkeypool` has been added, which will flush (entirely
- clear and refill) the keypool. (#23093)
-
-- `listunspent` now includes `ancestorcount`, `ancestorsize`, and
- `ancestorfees` for each transaction output that is still in the mempool.
- (#12677)
-
-- `lockunspent` now optionally takes a third parameter, `persistent`, which
- causes the lock to be written persistently to the wallet database. This
- allows UTXOs to remain locked even after node restarts or crashes. (#23065)
-
-- `receivedby` RPCs now include coinbase transactions. Previously, the
- following wallet RPCs excluded coinbase transactions: `getreceivedbyaddress`,
- `getreceivedbylabel`, `listreceivedbyaddress`, `listreceivedbylabel`. This
- release changes this behaviour and returns results accounting for received
- coins from coinbase outputs. The previous behaviour can be restored using the
- configuration `-deprecatedrpc=exclude_coinbase`, but may be removed in a
- future release. (#14707)
-
-- A new option in the same `receivedby` RPCs, `include_immature_coinbase`
- (default=`false`), determines whether to account for immature coinbase
- transactions. Immature coinbase transactions are coinbase transactions that
- have 100 or fewer confirmations, and are not spendable. (#14707)
-
-GUI changes
------------
-
-- UTXOs which are locked via the GUI are now stored persistently in the
- wallet database, so are not lost on node shutdown or crash. (#23065)
-
-- The Bech32 checkbox has been replaced with a dropdown for all address types, including the new Bech32m (BIP-350) standard for Taproot enabled wallets.
-
-Low-level changes
-=================
-
-RPC
----
-
-- `getblockchaininfo` now returns a new `time` field, that provides the chain tip time. (#22407)
-
-Tests
------
-
-- For the `regtest` network the activation heights of several softforks were
- set to block height 1. They can be changed by the runtime setting
- `-testactivationheight=name@height`. (#22818)
-
Credits
=======
diff --git a/doc/tor.md b/doc/tor.md
index 086e6747bf..b7c4f7d425 100644
--- a/doc/tor.md
+++ b/doc/tor.md
@@ -55,10 +55,10 @@ outgoing connections, but more is possible.
-seednode=X SOCKS5. In Tor mode, such addresses can also be exchanged with
other P2P nodes.
- -onlynet=onion Make outgoing connections only to .onion addresses. Incoming
- connections are not affected by this option. This option can be
- specified multiple times to allow multiple network types, e.g.
- onlynet=onion, onlynet=i2p.
+ -onlynet=onion Make automatic outbound connections only to .onion addresses.
+ Inbound and manual connections are not affected by this option.
+ It can be specified multiple times to allow multiple networks,
+ e.g. onlynet=onion, onlynet=i2p.
In a typical situation, this suffices to run behind a Tor proxy:
diff --git a/src/Makefile.am b/src/Makefile.am
index 417a611181..e940736b71 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -106,6 +106,10 @@ if BUILD_BITCOIN_UTIL
bin_PROGRAMS += bitcoin-util
endif
+if BUILD_BITCOIN_CHAINSTATE
+ bin_PROGRAMS += bitcoin-chainstate
+endif
+
.PHONY: FORCE check-symbols check-security
# bitcoin core #
BITCOIN_CORE_H = \
@@ -205,6 +209,7 @@ BITCOIN_CORE_H = \
reverse_iterator.h \
rpc/blockchain.h \
rpc/client.h \
+ rpc/mempool.h \
rpc/mining.h \
rpc/protocol.h \
rpc/rawtransaction_util.h \
@@ -366,6 +371,7 @@ libbitcoin_node_a_SOURCES = \
pow.cpp \
rest.cpp \
rpc/blockchain.cpp \
+ rpc/mempool.cpp \
rpc/mining.cpp \
rpc/misc.cpp \
rpc/net.cpp \
@@ -769,6 +775,102 @@ bitcoin_util_LDADD = \
$(LIBSECP256K1)
#
+# bitcoin-chainstate binary #
+bitcoin_chainstate_SOURCES = \
+ bitcoin-chainstate.cpp \
+ arith_uint256.cpp \
+ blockfilter.cpp \
+ chain.cpp \
+ chainparamsbase.cpp \
+ chainparams.cpp \
+ clientversion.cpp \
+ coins.cpp \
+ compat/glibcxx_sanity.cpp \
+ compressor.cpp \
+ consensus/merkle.cpp \
+ consensus/tx_check.cpp \
+ consensus/tx_verify.cpp \
+ core_read.cpp \
+ dbwrapper.cpp \
+ deploymentinfo.cpp \
+ deploymentstatus.cpp \
+ flatfile.cpp \
+ fs.cpp \
+ hash.cpp \
+ index/base.cpp \
+ index/blockfilterindex.cpp \
+ index/coinstatsindex.cpp \
+ init/common.cpp \
+ key.cpp \
+ logging.cpp \
+ netaddress.cpp \
+ node/blockstorage.cpp \
+ node/chainstate.cpp \
+ node/coinstats.cpp \
+ node/ui_interface.cpp \
+ policy/feerate.cpp \
+ policy/fees.cpp \
+ policy/packages.cpp \
+ policy/policy.cpp \
+ policy/rbf.cpp \
+ policy/settings.cpp \
+ pow.cpp \
+ primitives/block.cpp \
+ primitives/transaction.cpp \
+ pubkey.cpp \
+ random.cpp \
+ randomenv.cpp \
+ scheduler.cpp \
+ script/interpreter.cpp \
+ script/script.cpp \
+ script/script_error.cpp \
+ script/sigcache.cpp \
+ script/standard.cpp \
+ shutdown.cpp \
+ signet.cpp \
+ support/cleanse.cpp \
+ support/lockedpool.cpp \
+ sync.cpp \
+ threadinterrupt.cpp \
+ timedata.cpp \
+ txdb.cpp \
+ txmempool.cpp \
+ uint256.cpp \
+ util/asmap.cpp \
+ util/bytevectorhash.cpp \
+ util/getuniquepath.cpp \
+ util/hasher.cpp \
+ util/moneystr.cpp \
+ util/rbf.cpp \
+ util/serfloat.cpp \
+ util/settings.cpp \
+ util/strencodings.cpp \
+ util/syscall_sandbox.cpp \
+ util/system.cpp \
+ util/thread.cpp \
+ util/threadnames.cpp \
+ util/time.cpp \
+ util/tokenpipe.cpp \
+ validation.cpp \
+ validationinterface.cpp \
+ versionbits.cpp \
+ warnings.cpp
+bitcoin_chainstate_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
+bitcoin_chainstate_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
+bitcoin_chainstate_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(PTHREAD_FLAGS)
+bitcoin_chainstate_LDADD = \
+ $(LIBBITCOIN_CRYPTO) \
+ $(LIBUNIVALUE) \
+ $(LIBSECP256K1) \
+ $(LIBLEVELDB) \
+ $(LIBLEVELDB_SSE42) \
+ $(LIBMEMENV)
+
+# Required for obj/build.h to be generated first.
+# More details: https://www.gnu.org/software/automake/manual/html_node/Built-Sources-Example.html
+bitcoin_chainstate-clientversion.$(OBJEXT): obj/build.h
+#
+
# bitcoinconsensus library #
if BUILD_BITCOIN_LIBS
include_HEADERS = script/bitcoinconsensus.h
diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include
index 0bcce6ebe1..5dae4374e3 100644
--- a/src/Makefile.bench.include
+++ b/src/Makefile.bench.include
@@ -13,38 +13,39 @@ GENERATED_BENCH_FILES = $(RAW_BENCH_FILES:.raw=.raw.h)
bench_bench_bitcoin_SOURCES = \
$(RAW_BENCH_FILES) \
bench/addrman.cpp \
- bench/bench_bitcoin.cpp \
+ bench/base58.cpp \
+ bench/bech32.cpp \
bench/bench.cpp \
bench/bench.h \
+ bench/bench_bitcoin.cpp \
bench/block_assemble.cpp \
+ bench/ccoins_caching.cpp \
+ bench/chacha20.cpp \
+ bench/chacha_poly_aead.cpp \
bench/checkblock.cpp \
bench/checkqueue.cpp \
- bench/data.h \
+ bench/crypto_hash.cpp \
bench/data.cpp \
+ bench/data.h \
bench/duplicate_inputs.cpp \
bench/examples.cpp \
- bench/rollingbloom.cpp \
- bench/chacha20.cpp \
- bench/chacha_poly_aead.cpp \
- bench/crypto_hash.cpp \
- bench/ccoins_caching.cpp \
bench/gcs_filter.cpp \
bench/hashpadding.cpp \
- bench/merkle_root.cpp \
+ bench/lockedpool.cpp \
+ bench/logging.cpp \
bench/mempool_eviction.cpp \
bench/mempool_stress.cpp \
- bench/nanobench.h \
+ bench/merkle_root.cpp \
bench/nanobench.cpp \
+ bench/nanobench.h \
bench/peer_eviction.cpp \
+ bench/poly1305.cpp \
+ bench/prevector.cpp \
+ bench/rollingbloom.cpp \
bench/rpc_blockchain.cpp \
bench/rpc_mempool.cpp \
bench/util_time.cpp \
- bench/verify_script.cpp \
- bench/base58.cpp \
- bench/bech32.cpp \
- bench/lockedpool.cpp \
- bench/poly1305.cpp \
- bench/prevector.cpp
+ bench/verify_script.cpp
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)
diff --git a/src/Makefile.qttest.include b/src/Makefile.qttest.include
index 8e6fa2eb0d..29c322fbc2 100644
--- a/src/Makefile.qttest.include
+++ b/src/Makefile.qttest.include
@@ -7,6 +7,7 @@ TESTS += qt/test/test_bitcoin-qt
TEST_QT_MOC_CPP = \
qt/test/moc_apptests.cpp \
+ qt/test/moc_optiontests.cpp \
qt/test/moc_rpcnestedtests.cpp \
qt/test/moc_uritests.cpp
@@ -19,6 +20,7 @@ endif # ENABLE_WALLET
TEST_QT_H = \
qt/test/addressbooktests.h \
qt/test/apptests.h \
+ qt/test/optiontests.h \
qt/test/rpcnestedtests.h \
qt/test/uritests.h \
qt/test/util.h \
@@ -30,6 +32,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
qt_test_test_bitcoin_qt_SOURCES = \
init/bitcoin-qt.cpp \
qt/test/apptests.cpp \
+ qt/test/optiontests.cpp \
qt/test/rpcnestedtests.cpp \
qt/test/test_main.cpp \
qt/test/uritests.cpp \
diff --git a/src/Makefile.test.include b/src/Makefile.test.include
index e2e08468a7..a7505b9bcf 100644
--- a/src/Makefile.test.include
+++ b/src/Makefile.test.include
@@ -121,6 +121,7 @@ BITCOIN_TESTS =\
test/scheduler_tests.cpp \
test/script_p2sh_tests.cpp \
test/script_parse_tests.cpp \
+ test/script_segwit_tests.cpp \
test/script_standard_tests.cpp \
test/script_tests.cpp \
test/scriptnum10.h \
diff --git a/src/addrman.cpp b/src/addrman.cpp
index f91a979934..2fd8143c1c 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -246,12 +246,18 @@ void AddrManImpl::Unserialize(Stream& s_)
uint8_t compat;
s >> compat;
+ if (compat < INCOMPATIBILITY_BASE) {
+ throw std::ios_base::failure(strprintf(
+ "Corrupted addrman database: The compat value (%u) "
+ "is lower than the expected minimum value %u.",
+ compat, INCOMPATIBILITY_BASE));
+ }
const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
if (lowest_compatible > FILE_FORMAT) {
throw InvalidAddrManVersionError(strprintf(
"Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
"but the maximum supported by this version of %s is %u.",
- uint8_t{format}, uint8_t{lowest_compatible}, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
+ uint8_t{format}, lowest_compatible, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
}
s >> nKey;
diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp
index 3f8bff4bcf..d6f9c0f8b5 100644
--- a/src/bench/bench_bitcoin.cpp
+++ b/src/bench/bench_bitcoin.cpp
@@ -109,8 +109,8 @@ int main(int argc, char** argv)
args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
args.is_list_only = argsman.GetBoolArg("-list", false);
args.min_time = std::chrono::milliseconds(argsman.GetIntArg("-min_time", DEFAULT_MIN_TIME_MS));
- args.output_csv = fs::PathFromString(argsman.GetArg("-output_csv", ""));
- args.output_json = fs::PathFromString(argsman.GetArg("-output_json", ""));
+ args.output_csv = argsman.GetPathArg("-output_csv");
+ args.output_json = argsman.GetPathArg("-output_json");
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
benchmark::BenchRunner::RunAll(args);
diff --git a/src/bench/logging.cpp b/src/bench/logging.cpp
new file mode 100644
index 0000000000..d28777df9e
--- /dev/null
+++ b/src/bench/logging.cpp
@@ -0,0 +1,48 @@
+// Copyright (c) 2020 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 <bench/bench.h>
+#include <logging.h>
+#include <test/util/setup_common.h>
+
+
+static void Logging(benchmark::Bench& bench, const std::vector<const char*>& extra_args, const std::function<void()>& log)
+{
+ TestingSetup test_setup{
+ CBaseChainParams::REGTEST,
+ extra_args,
+ };
+
+ bench.run([&] { log(); });
+}
+
+static void LoggingYoThreadNames(benchmark::Bench& bench)
+{
+ Logging(bench, {"-logthreadnames=1"}, [] { LogPrintf("%s\n", "test"); });
+}
+static void LoggingNoThreadNames(benchmark::Bench& bench)
+{
+ Logging(bench, {"-logthreadnames=0"}, [] { LogPrintf("%s\n", "test"); });
+}
+static void LoggingYoCategory(benchmark::Bench& bench)
+{
+ Logging(bench, {"-logthreadnames=0", "-debug=net"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
+}
+static void LoggingNoCategory(benchmark::Bench& bench)
+{
+ Logging(bench, {"-logthreadnames=0", "-debug=0"}, [] { LogPrint(BCLog::NET, "%s\n", "test"); });
+}
+static void LoggingNoFile(benchmark::Bench& bench)
+{
+ Logging(bench, {"-nodebuglogfile", "-debug=1"}, [] {
+ LogPrintf("%s\n", "test");
+ LogPrint(BCLog::NET, "%s\n", "test");
+ });
+}
+
+BENCHMARK(LoggingYoThreadNames);
+BENCHMARK(LoggingNoThreadNames);
+BENCHMARK(LoggingYoCategory);
+BENCHMARK(LoggingNoCategory);
+BENCHMARK(LoggingNoFile);
diff --git a/src/bench/rpc_mempool.cpp b/src/bench/rpc_mempool.cpp
index 12dcff5844..64e4c46899 100644
--- a/src/bench/rpc_mempool.cpp
+++ b/src/bench/rpc_mempool.cpp
@@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bench/bench.h>
-#include <rpc/blockchain.h>
+#include <rpc/mempool.h>
#include <txmempool.h>
#include <univalue.h>
diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp
new file mode 100644
index 0000000000..72b8fefcc7
--- /dev/null
+++ b/src/bitcoin-chainstate.cpp
@@ -0,0 +1,262 @@
+// Copyright (c) 2022 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+//
+// The bitcoin-chainstate executable serves to surface the dependencies required
+// by a program wishing to use Bitcoin Core's consensus engine as it is right
+// now.
+//
+// DEVELOPER NOTE: Since this is a "demo-only", experimental, etc. executable,
+// it may diverge from Bitcoin Core's coding style.
+//
+// It is part of the libbitcoinkernel project.
+
+#include <chainparams.h>
+#include <consensus/validation.h>
+#include <core_io.h>
+#include <init/common.h>
+#include <node/blockstorage.h>
+#include <node/chainstate.h>
+#include <scheduler.h>
+#include <script/sigcache.h>
+#include <util/system.h>
+#include <util/thread.h>
+#include <validation.h>
+#include <validationinterface.h>
+
+#include <filesystem>
+#include <functional>
+#include <iosfwd>
+
+const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
+
+int main(int argc, char* argv[])
+{
+ // SETUP: Argument parsing and handling
+ if (argc != 2) {
+ std::cerr
+ << "Usage: " << argv[0] << " DATADIR" << std::endl
+ << "Display DATADIR information, and process hex-encoded blocks on standard input." << std::endl
+ << std::endl
+ << "IMPORTANT: THIS EXECUTABLE IS EXPERIMENTAL, FOR TESTING ONLY, AND EXPECTED TO" << std::endl
+ << " BREAK IN FUTURE VERSIONS. DO NOT USE ON YOUR ACTUAL DATADIR." << std::endl;
+ return 1;
+ }
+ std::filesystem::path abs_datadir = std::filesystem::absolute(argv[1]);
+ std::filesystem::create_directories(abs_datadir);
+ gArgs.ForceSetArg("-datadir", abs_datadir.string());
+
+
+ // SETUP: Misc Globals
+ SelectParams(CBaseChainParams::MAIN);
+ const CChainParams& chainparams = Params();
+
+ init::SetGlobals(); // ECC_Start, etc.
+
+ // Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
+ // which will try the script cache first and fall back to actually
+ // performing the check with the signature cache.
+ InitSignatureCache();
+ InitScriptExecutionCache();
+
+
+ // SETUP: Scheduling and Background Signals
+ CScheduler scheduler{};
+ // Start the lightweight task scheduler thread
+ scheduler.m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { scheduler.serviceQueue(); });
+
+ // Gather some entropy once per minute.
+ scheduler.scheduleEvery(RandAddPeriodic, std::chrono::minutes{1});
+
+ GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
+
+
+ // SETUP: Chainstate
+ ChainstateManager chainman;
+
+ auto rv = node::LoadChainstate(false,
+ std::ref(chainman),
+ nullptr,
+ false,
+ chainparams.GetConsensus(),
+ false,
+ 2 << 20,
+ 2 << 22,
+ (450 << 20) - (2 << 20) - (2 << 22),
+ false,
+ false,
+ []() { return false; });
+ if (rv.has_value()) {
+ std::cerr << "Failed to load Chain state from your datadir." << std::endl;
+ goto epilogue;
+ } else {
+ auto maybe_verify_error = node::VerifyLoadedChainstate(std::ref(chainman),
+ false,
+ false,
+ chainparams.GetConsensus(),
+ DEFAULT_CHECKBLOCKS,
+ DEFAULT_CHECKLEVEL,
+ /*get_unix_time_seconds=*/static_cast<int64_t (*)()>(GetTime));
+ if (maybe_verify_error.has_value()) {
+ std::cerr << "Failed to verify loaded Chain state from your datadir." << std::endl;
+ goto epilogue;
+ }
+ }
+
+ for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
+ BlockValidationState state;
+ if (!chainstate->ActivateBestChain(state, nullptr)) {
+ std::cerr << "Failed to connect best block (" << state.ToString() << ")" << std::endl;
+ goto epilogue;
+ }
+ }
+
+ // Main program logic starts here
+ std::cout
+ << "Hello! I'm going to print out some information about your datadir." << std::endl
+ << "\t" << "Path: " << gArgs.GetDataDirNet() << std::endl
+ << "\t" << "Reindexing: " << std::boolalpha << node::fReindex.load() << std::noboolalpha << std::endl
+ << "\t" << "Snapshot Active: " << std::boolalpha << chainman.IsSnapshotActive() << std::noboolalpha << std::endl
+ << "\t" << "Active Height: " << chainman.ActiveHeight() << std::endl
+ << "\t" << "Active IBD: " << std::boolalpha << chainman.ActiveChainstate().IsInitialBlockDownload() << std::noboolalpha << std::endl;
+ {
+ CBlockIndex* tip = chainman.ActiveTip();
+ if (tip) {
+ std::cout << "\t" << tip->ToString() << std::endl;
+ }
+ }
+
+ for (std::string line; std::getline(std::cin, line);) {
+ if (line.empty()) {
+ std::cerr << "Empty line found" << std::endl;
+ break;
+ }
+
+ std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
+ CBlock& block = *blockptr;
+
+ if (!DecodeHexBlk(block, line)) {
+ std::cerr << "Block decode failed" << std::endl;
+ break;
+ }
+
+ if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
+ std::cerr << "Block does not start with a coinbase" << std::endl;
+ break;
+ }
+
+ uint256 hash = block.GetHash();
+ {
+ LOCK(cs_main);
+ const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
+ if (pindex) {
+ if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
+ std::cerr << "duplicate" << std::endl;
+ break;
+ }
+ if (pindex->nStatus & BLOCK_FAILED_MASK) {
+ std::cerr << "duplicate-invalid" << std::endl;
+ break;
+ }
+ }
+ }
+
+ {
+ LOCK(cs_main);
+ const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
+ if (pindex) {
+ UpdateUncommittedBlockStructures(block, pindex, chainparams.GetConsensus());
+ }
+ }
+
+ // Adapted from rpc/mining.cpp
+ class submitblock_StateCatcher final : public CValidationInterface
+ {
+ public:
+ uint256 hash;
+ bool found;
+ BlockValidationState state;
+
+ explicit submitblock_StateCatcher(const uint256& hashIn) : hash(hashIn), found(false), state() {}
+
+ protected:
+ void BlockChecked(const CBlock& block, const BlockValidationState& stateIn) override
+ {
+ if (block.GetHash() != hash)
+ return;
+ found = true;
+ state = stateIn;
+ }
+ };
+
+ bool new_block;
+ auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
+ RegisterSharedValidationInterface(sc);
+ bool accepted = chainman.ProcessNewBlock(chainparams, blockptr, /* force_processing */ true, /* new_block */ &new_block);
+ UnregisterSharedValidationInterface(sc);
+ if (!new_block && accepted) {
+ std::cerr << "duplicate" << std::endl;
+ break;
+ }
+ if (!sc->found) {
+ std::cerr << "inconclusive" << std::endl;
+ break;
+ }
+ std::cout << sc->state.ToString() << std::endl;
+ switch (sc->state.GetResult()) {
+ case BlockValidationResult::BLOCK_RESULT_UNSET:
+ std::cerr << "initial value. Block has not yet been rejected" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_CONSENSUS:
+ std::cerr << "invalid by consensus rules (excluding any below reasons)" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_RECENT_CONSENSUS_CHANGE:
+ std::cerr << "Invalid by a change to consensus rules more recent than SegWit." << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_CACHED_INVALID:
+ std::cerr << "this block was cached as being invalid and we didn't store the reason why" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_INVALID_HEADER:
+ std::cerr << "invalid proof of work or time too old" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_MUTATED:
+ std::cerr << "the block's data didn't match the data committed to by the PoW" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_MISSING_PREV:
+ std::cerr << "We don't have the previous block the checked one is built on" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_INVALID_PREV:
+ std::cerr << "A block this one builds on is invalid" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_TIME_FUTURE:
+ std::cerr << "block timestamp was > 2 hours in the future (or our clock is bad)" << std::endl;
+ break;
+ case BlockValidationResult::BLOCK_CHECKPOINT:
+ std::cerr << "the block failed to meet one of our checkpoints" << std::endl;
+ break;
+ }
+ }
+
+epilogue:
+ // Without this precise shutdown sequence, there will be a lot of nullptr
+ // dereferencing and UB.
+ scheduler.stop();
+ if (chainman.m_load_block.joinable()) chainman.m_load_block.join();
+ StopScriptCheckWorkerThreads();
+
+ GetMainSignals().FlushBackgroundCallbacks();
+ {
+ LOCK(cs_main);
+ for (CChainState* chainstate : chainman.GetAll()) {
+ if (chainstate->CanFlushToDisk()) {
+ chainstate->ForceFlushStateToDisk();
+ chainstate->ResetCoinsViews();
+ }
+ }
+ }
+ GetMainSignals().UnregisterBackgroundSignalScheduler();
+
+ WITH_LOCK(::cs_main, UnloadBlockIndex(nullptr, chainman));
+
+ init::UnsetGlobals();
+}
diff --git a/src/chainparams.cpp b/src/chainparams.cpp
index d3ae6f4cb2..93510e925f 100644
--- a/src/chainparams.cpp
+++ b/src/chainparams.cpp
@@ -397,7 +397,7 @@ public:
consensus.BIP65Height = 1; // Always active unless overridden
consensus.BIP66Height = 1; // Always active unless overridden
consensus.CSVHeight = 1; // Always active unless overridden
- consensus.SegwitHeight = 1; // Always active unless overridden
+ consensus.SegwitHeight = 0; // Always active unless overridden
consensus.MinBIP9WarningHeight = 0;
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index a1c8a5937c..69078708f9 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -228,10 +228,9 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
m_muhash.Finalize(out);
value.second.muhash = out;
- CDBBatch batch(*m_db);
- batch.Write(DBHeightKey(pindex->nHeight), value);
- batch.Write(DB_MUHASH, m_muhash);
- return m_db->WriteBatch(batch);
+ // Intentionally do not update DB_MUHASH here so it stays in sync with
+ // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
+ return m_db->Write(DBHeightKey(pindex->nHeight), value);
}
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
@@ -278,7 +277,7 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
{
LOCK(cs_main);
- CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
+ const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
const auto& consensus_params{Params().GetConsensus()};
do {
@@ -388,6 +387,14 @@ bool CoinStatsIndex::Init()
return true;
}
+bool CoinStatsIndex::CommitInternal(CDBBatch& batch)
+{
+ // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
+ // to prevent an inconsistent state of the DB.
+ batch.Write(DB_MUHASH, m_muhash);
+ return BaseIndex::CommitInternal(batch);
+}
+
// Reverse a single block as part of a reorg
bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex)
{
@@ -489,5 +496,5 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
Assert(m_total_unspendables_scripts == read_out.second.total_unspendables_scripts);
Assert(m_total_unspendables_unclaimed_rewards == read_out.second.total_unspendables_unclaimed_rewards);
- return m_db->Write(DB_MUHASH, m_muhash);
+ return true;
}
diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h
index d2a6c9c964..24190ac137 100644
--- a/src/index/coinstatsindex.h
+++ b/src/index/coinstatsindex.h
@@ -39,6 +39,8 @@ private:
protected:
bool Init() override;
+ bool CommitInternal(CDBBatch& batch) override;
+
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
diff --git a/src/init.cpp b/src/init.cpp
index ce666534ae..bad402e56e 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -135,7 +135,7 @@ static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
static fs::path GetPidFile(const ArgsManager& args)
{
- return AbsPathForConfigVal(fs::PathFromString(args.GetArg("-pid", BITCOIN_PID_FILENAME)));
+ return AbsPathForConfigVal(args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
}
[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)
@@ -462,10 +462,12 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-onion=<ip:port>", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-i2pacceptincoming", "If set and -i2psam is also set then incoming I2P connections are accepted via the SAM proxy. If this is not set but -i2psam is set then only outgoing connections will be made to the I2P network. Ignored if -i2psam is not set. Listening for incoming I2P connections is done through the SAM proxy, not by binding to a local address and port (default: 1)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
- argsman.AddArg("-onlynet=<net>", "Make automatic outgoing connections only through network <net> (" + Join(GetNetworkNames(), ", ") + "). Incoming connections are not affected by this option. This option can be specified multiple times to allow multiple networks.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
+ argsman.AddArg("-onlynet=<net>", "Make automatic outbound connections only to network <net> (" + Join(GetNetworkNames(), ", ") + "). Inbound and manual connections are not affected by this option. It can be specified multiple times to allow multiple networks.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
+ // TODO: remove the sentence "Nodes not using ... incoming connections." once the changes from
+ // https://github.com/bitcoin/bitcoin/pull/23542 have become widespread.
argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, signet: %u, regtest: %u) are unlikely to get incoming connections. Not relevant for I2P (see doc/i2p.md).", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), signetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
argsman.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1227,10 +1229,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// Read asmap file if configured
std::vector<bool> asmap;
if (args.IsArgSet("-asmap")) {
- fs::path asmap_path = fs::PathFromString(args.GetArg("-asmap", ""));
- if (asmap_path.empty()) {
- asmap_path = fs::PathFromString(DEFAULT_ASMAP_FILENAME);
- }
+ fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
if (!asmap_path.is_absolute()) {
asmap_path = gArgs.GetDataDirNet() / asmap_path;
}
@@ -1666,8 +1665,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
LogPrintf("nBestHeight = %d\n", chain_active_height);
if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
- Discover();
-
// Map ports with UPnP or NAT-PMP.
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), gArgs.GetBoolArg("-natpmp", DEFAULT_NATPMP));
@@ -1687,12 +1684,27 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
connOptions.nMaxOutboundLimit = *opt_max_upload;
connOptions.m_peer_connect_timeout = peer_connect_timeout;
+ // Port to bind to if `-bind=addr` is provided without a `:port` suffix.
+ const uint16_t default_bind_port =
+ static_cast<uint16_t>(args.GetIntArg("-port", Params().GetDefaultPort()));
+
+ const auto BadPortWarning = [](const char* prefix, uint16_t port) {
+ return strprintf(_("%s request to listen on port %u. This port is considered \"bad\" and "
+ "thus it is unlikely that any Bitcoin Core peers connect to it. See "
+ "doc/p2p-bad-ports.md for details and a full list."),
+ prefix,
+ port);
+ };
+
for (const std::string& bind_arg : args.GetArgs("-bind")) {
CService bind_addr;
const size_t index = bind_arg.rfind('=');
if (index == std::string::npos) {
- if (Lookup(bind_arg, bind_addr, GetListenPort(), false)) {
+ if (Lookup(bind_arg, bind_addr, default_bind_port, /*fAllowLookup=*/false)) {
connOptions.vBinds.push_back(bind_addr);
+ if (IsBadPort(bind_addr.GetPort())) {
+ InitWarning(BadPortWarning("-bind", bind_addr.GetPort()));
+ }
continue;
}
} else {
@@ -1719,6 +1731,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// on any address - 0.0.0.0 (IPv4) and :: (IPv6).
connOptions.bind_on_any = args.GetArgs("-bind").empty() && args.GetArgs("-whitebind").empty();
+ // Emit a warning if a bad port is given to -port= but only if -bind and -whitebind are not
+ // given, because if they are, then -port= is ignored.
+ if (connOptions.bind_on_any && args.IsArgSet("-port")) {
+ const uint16_t port_arg = args.GetIntArg("-port", 0);
+ if (IsBadPort(port_arg)) {
+ InitWarning(BadPortWarning("-port", port_arg));
+ }
+ }
+
CService onion_service_target;
if (!connOptions.onion_binds.empty()) {
onion_service_target = connOptions.onion_binds.front();
@@ -1736,6 +1757,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
StartTorControl(onion_service_target);
}
+ if (connOptions.bind_on_any) {
+ // Only add all IP addresses of the machine if we would be listening on
+ // any address - 0.0.0.0 (IPv4) and :: (IPv6).
+ Discover();
+ }
+
for (const auto& net : args.GetArgs("-whitelist")) {
NetWhitelistPermissions subnet;
bilingual_str error;
diff --git a/src/init/common.cpp b/src/init/common.cpp
index 38c60366e3..688471b35d 100644
--- a/src/init/common.cpp
+++ b/src/init/common.cpp
@@ -81,7 +81,7 @@ void AddLoggingArgs(ArgsManager& argsman)
void SetLoggingOptions(const ArgsManager& args)
{
LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
- LogInstance().m_file_path = AbsPathForConfigVal(fs::PathFromString(args.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)));
+ LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
diff --git a/src/leveldb/util/env_posix.cc b/src/leveldb/util/env_posix.cc
index 9f5863a0f3..18626b327c 100644
--- a/src/leveldb/util/env_posix.cc
+++ b/src/leveldb/util/env_posix.cc
@@ -850,7 +850,7 @@ class SingletonEnv {
public:
SingletonEnv() {
#if !defined(NDEBUG)
- env_initialized_.store(true, std::memory_order::memory_order_relaxed);
+ env_initialized_.store(true, std::memory_order_relaxed);
#endif // !defined(NDEBUG)
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
"env_storage_ will not fit the Env");
@@ -867,7 +867,7 @@ class SingletonEnv {
static void AssertEnvNotInitialized() {
#if !defined(NDEBUG)
- assert(!env_initialized_.load(std::memory_order::memory_order_relaxed));
+ assert(!env_initialized_.load(std::memory_order_relaxed));
#endif // !defined(NDEBUG)
}
diff --git a/src/leveldb/util/env_windows.cc b/src/leveldb/util/env_windows.cc
index 1834206562..4dcba222a1 100644
--- a/src/leveldb/util/env_windows.cc
+++ b/src/leveldb/util/env_windows.cc
@@ -798,7 +798,7 @@ class SingletonEnv {
public:
SingletonEnv() {
#if !defined(NDEBUG)
- env_initialized_.store(true, std::memory_order::memory_order_relaxed);
+ env_initialized_.store(true, std::memory_order_relaxed);
#endif // !defined(NDEBUG)
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
"env_storage_ will not fit the Env");
@@ -815,7 +815,7 @@ class SingletonEnv {
static void AssertEnvNotInitialized() {
#if !defined(NDEBUG)
- assert(!env_initialized_.load(std::memory_order::memory_order_relaxed));
+ assert(!env_initialized_.load(std::memory_order_relaxed));
#endif // !defined(NDEBUG)
}
diff --git a/src/net.cpp b/src/net.cpp
index d200da0dea..955eec46e3 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -126,6 +126,31 @@ void CConnman::AddAddrFetch(const std::string& strDest)
uint16_t GetListenPort()
{
+ // If -bind= is provided with ":port" part, use that (first one if multiple are provided).
+ for (const std::string& bind_arg : gArgs.GetArgs("-bind")) {
+ CService bind_addr;
+ constexpr uint16_t dummy_port = 0;
+
+ if (Lookup(bind_arg, bind_addr, dummy_port, /*fAllowLookup=*/false)) {
+ if (bind_addr.GetPort() != dummy_port) {
+ return bind_addr.GetPort();
+ }
+ }
+ }
+
+ // Otherwise, if -whitebind= without NetPermissionFlags::NoBan is provided, use that
+ // (-whitebind= is required to have ":port").
+ for (const std::string& whitebind_arg : gArgs.GetArgs("-whitebind")) {
+ NetWhitebindPermissions whitebind;
+ bilingual_str error;
+ if (NetWhitebindPermissions::TryParse(whitebind_arg, whitebind, error)) {
+ if (!NetPermissions::HasFlag(whitebind.m_flags, NetPermissionFlags::NoBan)) {
+ return whitebind.m_service.GetPort();
+ }
+ }
+ }
+
+ // Otherwise, if -port= is provided, use that. Otherwise use the default port.
return static_cast<uint16_t>(gArgs.GetIntArg("-port", Params().GetDefaultPort()));
}
@@ -221,7 +246,17 @@ std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
{
- addrLocal.SetIP(pnode->GetAddrLocal());
+ if (pnode->IsInboundConn()) {
+ // For inbound connections, assume both the address and the port
+ // as seen from the peer.
+ addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices};
+ } else {
+ // For outbound connections, assume just the address as seen from
+ // the peer and leave the port in `addrLocal` as returned by
+ // `GetLocalAddress()` above. The peer has no way to observe our
+ // listening port when we have initiated the connection.
+ addrLocal.SetIP(pnode->GetAddrLocal());
+ }
}
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
{
@@ -928,17 +963,17 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
// Protect the half of the remaining nodes which have been connected the longest.
// This replicates the non-eviction implicit behavior, and precludes attacks that start later.
// To favorise the diversity of our peer connections, reserve up to half of these protected
- // spots for Tor/onion, localhost and I2P peers, even if they're not longest uptime overall.
- // This helps protect these higher-latency peers that tend to be otherwise
+ // spots for Tor/onion, localhost, I2P, and CJDNS peers, even if they're not longest uptime
+ // overall. This helps protect these higher-latency peers that tend to be otherwise
// disadvantaged under our eviction criteria.
const size_t initial_size = eviction_candidates.size();
const size_t total_protect_size{initial_size / 2};
- // Disadvantaged networks to protect: I2P, localhost, Tor/onion. In case of equal counts, earlier
- // array members have first opportunity to recover unused slots from the previous iteration.
+ // Disadvantaged networks to protect. In the case of equal counts, earlier array members
+ // have the first opportunity to recover unused slots from the previous iteration.
struct Net { bool is_local; Network id; size_t count; };
- std::array<Net, 3> networks{
- {{false, NET_I2P, 0}, {/* localhost */ true, NET_MAX, 0}, {false, NET_ONION, 0}}};
+ std::array<Net, 4> networks{
+ {{false, NET_CJDNS, 0}, {false, NET_I2P, 0}, {/*localhost=*/true, NET_MAX, 0}, {false, NET_ONION, 0}}};
// Count and store the number of eviction candidates per network.
for (Net& n : networks) {
@@ -2120,12 +2155,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
continue;
}
- // Do not allow non-default ports, unless after 50 invalid
- // addresses selected already. This is to prevent malicious peers
- // from advertising themselves as a service on another host and
- // port, causing a DoS attack as nodes around the network attempt
- // to connect to it fruitlessly.
- if (addr.GetPort() != Params().GetDefaultPort(addr.GetNetwork()) && nTries < 50) {
+ // Do not connect to bad ports, unless 50 invalid addresses have been selected already.
+ if (nTries < 50 && (addr.IsIPv4() || addr.IsIPv6()) && IsBadPort(addr.GetPort())) {
continue;
}
@@ -3089,7 +3120,10 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
}
-void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming)
+void CaptureMessageToFile(const CAddress& addr,
+ const std::string& msg_type,
+ Span<const unsigned char> data,
+ bool is_incoming)
{
// Note: This function captures the message at the time of processing,
// not at socket receive/send time.
@@ -3116,3 +3150,9 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa
ser_writedata32(f, size);
f.write(AsBytes(data));
}
+
+std::function<void(const CAddress& addr,
+ const std::string& msg_type,
+ Span<const unsigned char> data,
+ bool is_incoming)>
+ CaptureMessage = CaptureMessageToFile;
diff --git a/src/net.h b/src/net.h
index 3f4c8e38ec..a38310938b 100644
--- a/src/net.h
+++ b/src/net.h
@@ -31,6 +31,7 @@
#include <condition_variable>
#include <cstdint>
#include <deque>
+#include <functional>
#include <map>
#include <memory>
#include <optional>
@@ -182,7 +183,15 @@ enum class ConnectionType {
/** Convert ConnectionType enum to a string value */
std::string ConnectionTypeAsString(ConnectionType conn_type);
+
+/**
+ * Look up IP addresses from all interfaces on the machine and add them to the
+ * list of local addresses to self-advertise.
+ * The loopback interface is skipped and only the first address from each
+ * interface is used.
+ */
void Discover();
+
uint16_t GetListenPort();
enum
@@ -1272,7 +1281,17 @@ private:
};
/** Dump binary message to file, with timestamp */
-void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming);
+void CaptureMessageToFile(const CAddress& addr,
+ const std::string& msg_type,
+ Span<const unsigned char> data,
+ bool is_incoming);
+
+/** Defaults to `CaptureMessageToFile()`, but can be overridden by unit tests. */
+extern std::function<void(const CAddress& addr,
+ const std::string& msg_type,
+ Span<const unsigned char> data,
+ bool is_incoming)>
+ CaptureMessage;
struct NodeEvictionCandidate
{
@@ -1316,6 +1335,8 @@ struct NodeEvictionCandidate
*
* - I2P peers
*
+ * - CJDNS peers
+ *
* This helps protect these privacy network peers, which tend to be otherwise
* disadvantaged under our eviction criteria for their higher min ping times
* relative to IPv4/IPv6 peers, and favorise the diversity of peer connections.
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 3cebca1a77..59cd83e493 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -415,7 +415,7 @@ private:
void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable);
/** Send `feefilter` message. */
- void MaybeSendFeefilter(CNode& node, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ void MaybeSendFeefilter(CNode& node, std::chrono::microseconds current_time);
const CChainParams& m_chainparams;
CConnman& m_connman;
@@ -1775,8 +1775,10 @@ void PeerManagerImpl::RelayAddress(NodeId originator,
// Relay to a limited number of other nodes
// Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the m_addr_knowns of the chosen nodes prevent repeats
- const uint64_t hashAddr{addr.GetHash()};
- const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr).Write((GetTime() + hashAddr) / (24 * 60 * 60))};
+ const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
+ const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
+ .Write(hash_addr)
+ .Write((GetTime() + hash_addr) / (24 * 60 * 60))};
FastRandomContext insecure_rand;
// Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
@@ -2709,6 +2711,10 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
PushAddress(*peer, addr, insecure_rand);
} else if (IsPeerAddrLocalGood(&pfrom)) {
+ // Override just the address with whatever the peer sees us as.
+ // Leave the port in addr as it was returned by GetLocalAddress()
+ // above, as this is an outbound connection and the peer cannot
+ // observe our listening port.
addr.SetIP(addrMe);
LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
PushAddress(*peer, addr, insecure_rand);
@@ -4500,8 +4506,6 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, std::chrono::microseconds current_time)
{
- AssertLockHeld(cs_main);
-
if (m_ignore_incoming_txs) return;
if (!pto.m_tx_relay) return;
if (pto.GetCommonVersion() < FEEFILTER_VERSION) return;
@@ -5048,8 +5052,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (!vGetData.empty())
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
-
- MaybeSendFeefilter(*pto, current_time);
} // release cs_main
+ MaybeSendFeefilter(*pto, current_time);
return true;
}
diff --git a/src/netaddress.cpp b/src/netaddress.cpp
index 4874b82eca..f7640329f8 100644
--- a/src/netaddress.cpp
+++ b/src/netaddress.cpp
@@ -833,14 +833,6 @@ std::vector<unsigned char> CNetAddr::GetAddrBytes() const
return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
}
-uint64_t CNetAddr::GetHash() const
-{
- uint256 hash = Hash(m_addr);
- uint64_t nRet;
- memcpy(&nRet, &hash, sizeof(nRet));
- return nRet;
-}
-
// private extensions to enum Network, only returned by GetExtNetwork,
// and only used in GetReachabilityFrom
static const int NET_UNKNOWN = NET_MAX + 0;
diff --git a/src/netaddress.h b/src/netaddress.h
index b06b6c65b6..6d21dcd5cd 100644
--- a/src/netaddress.h
+++ b/src/netaddress.h
@@ -194,7 +194,6 @@ public:
enum Network GetNetwork() const;
std::string ToString() const;
std::string ToStringIP() const;
- uint64_t GetHash() const;
bool GetInAddr(struct in_addr* pipv4Addr) const;
Network GetNetClass() const;
@@ -562,6 +561,14 @@ public:
class CServiceHash
{
public:
+ CServiceHash()
+ : m_salt_k0{GetRand(std::numeric_limits<uint64_t>::max())},
+ m_salt_k1{GetRand(std::numeric_limits<uint64_t>::max())}
+ {
+ }
+
+ CServiceHash(uint64_t salt_k0, uint64_t salt_k1) : m_salt_k0{salt_k0}, m_salt_k1{salt_k1} {}
+
size_t operator()(const CService& a) const noexcept
{
CSipHasher hasher(m_salt_k0, m_salt_k1);
@@ -572,8 +579,8 @@ public:
}
private:
- const uint64_t m_salt_k0 = GetRand(std::numeric_limits<uint64_t>::max());
- const uint64_t m_salt_k1 = GetRand(std::numeric_limits<uint64_t>::max());
+ const uint64_t m_salt_k0;
+ const uint64_t m_salt_k1;
};
#endif // BITCOIN_NETADDRESS_H
diff --git a/src/netbase.cpp b/src/netbase.cpp
index a36ad7d179..9a0b800565 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -749,3 +749,93 @@ void InterruptSocks5(bool interrupt)
{
interruptSocks5Recv = interrupt;
}
+
+bool IsBadPort(uint16_t port)
+{
+ /* Don't forget to update doc/p2p-bad-ports.md if you change this list. */
+
+ switch (port) {
+ case 1: // tcpmux
+ case 7: // echo
+ case 9: // discard
+ case 11: // systat
+ case 13: // daytime
+ case 15: // netstat
+ case 17: // qotd
+ case 19: // chargen
+ case 20: // ftp data
+ case 21: // ftp access
+ case 22: // ssh
+ case 23: // telnet
+ case 25: // smtp
+ case 37: // time
+ case 42: // name
+ case 43: // nicname
+ case 53: // domain
+ case 69: // tftp
+ case 77: // priv-rjs
+ case 79: // finger
+ case 87: // ttylink
+ case 95: // supdup
+ case 101: // hostname
+ case 102: // iso-tsap
+ case 103: // gppitnp
+ case 104: // acr-nema
+ case 109: // pop2
+ case 110: // pop3
+ case 111: // sunrpc
+ case 113: // auth
+ case 115: // sftp
+ case 117: // uucp-path
+ case 119: // nntp
+ case 123: // NTP
+ case 135: // loc-srv /epmap
+ case 137: // netbios
+ case 139: // netbios
+ case 143: // imap2
+ case 161: // snmp
+ case 179: // BGP
+ case 389: // ldap
+ case 427: // SLP (Also used by Apple Filing Protocol)
+ case 465: // smtp+ssl
+ case 512: // print / exec
+ case 513: // login
+ case 514: // shell
+ case 515: // printer
+ case 526: // tempo
+ case 530: // courier
+ case 531: // chat
+ case 532: // netnews
+ case 540: // uucp
+ case 548: // AFP (Apple Filing Protocol)
+ case 554: // rtsp
+ case 556: // remotefs
+ case 563: // nntp+ssl
+ case 587: // smtp (rfc6409)
+ case 601: // syslog-conn (rfc3195)
+ case 636: // ldap+ssl
+ case 989: // ftps-data
+ case 990: // ftps
+ case 993: // ldap+ssl
+ case 995: // pop3+ssl
+ case 1719: // h323gatestat
+ case 1720: // h323hostcall
+ case 1723: // pptp
+ case 2049: // nfs
+ case 3659: // apple-sasl / PasswordServer
+ case 4045: // lockd
+ case 5060: // sip
+ case 5061: // sips
+ case 6000: // X11
+ case 6566: // sane-port
+ case 6665: // Alternate IRC
+ case 6666: // Alternate IRC
+ case 6667: // Standard IRC
+ case 6668: // Alternate IRC
+ case 6669: // Alternate IRC
+ case 6697: // IRC + TLS
+ case 10080: // Amanda
+ return true;
+ }
+ return false;
+}
diff --git a/src/netbase.h b/src/netbase.h
index b63d53086d..f9e3872c16 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -247,4 +247,13 @@ void InterruptSocks5(bool interrupt);
*/
bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
+/**
+ * Determine if a port is "bad" from the perspective of attempting to connect
+ * to a node on that port.
+ * @see doc/p2p-bad-ports.md
+ * @param[in] port Port to check.
+ * @returns whether the port is bad
+ */
+bool IsBadPort(uint16_t port);
+
#endif // BITCOIN_NETBASE_H
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 8a99130fd0..763fd29744 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -28,39 +28,78 @@ bool fHavePruned = false;
bool fPruneMode = false;
uint64_t nPruneTarget = 0;
+bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
+{
+ // First sort by most total work, ...
+ if (pa->nChainWork > pb->nChainWork) return false;
+ if (pa->nChainWork < pb->nChainWork) return true;
+
+ // ... then by earliest time received, ...
+ if (pa->nSequenceId < pb->nSequenceId) return false;
+ if (pa->nSequenceId > pb->nSequenceId) return true;
+
+ // Use pointer address as tie breaker (should only happen with blocks
+ // loaded from disk, as those all have id 0).
+ if (pa < pb) return false;
+ if (pa > pb) return true;
+
+ // Identical blocks.
+ return false;
+}
+
+bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
+{
+ return pa->nHeight < pb->nHeight;
+}
+
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
static FlatFileSeq BlockFileSeq();
static FlatFileSeq UndoFileSeq();
-CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
+std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
+{
+ AssertLockHeld(cs_main);
+ std::vector<CBlockIndex*> rv;
+ rv.reserve(m_block_index.size());
+ for (auto& [_, block_index] : m_block_index) {
+ rv.push_back(&block_index);
+ }
+ return rv;
+}
+
+CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash)
+{
+ AssertLockHeld(cs_main);
+ BlockMap::iterator it = m_block_index.find(hash);
+ return it == m_block_index.end() ? nullptr : &it->second;
+}
+
+const CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
{
AssertLockHeld(cs_main);
BlockMap::const_iterator it = m_block_index.find(hash);
- return it == m_block_index.end() ? nullptr : it->second;
+ return it == m_block_index.end() ? nullptr : &it->second;
}
CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block)
{
AssertLockHeld(cs_main);
- // Check for duplicate
- uint256 hash = block.GetHash();
- BlockMap::iterator it = m_block_index.find(hash);
- if (it != m_block_index.end()) {
- return it->second;
+ auto [mi, inserted] = m_block_index.try_emplace(block.GetHash(), block);
+ if (!inserted) {
+ return &mi->second;
}
+ CBlockIndex* pindexNew = &(*mi).second;
- // Construct new block index object
- CBlockIndex* pindexNew = new CBlockIndex(block);
// We assign the sequence id to blocks only when the full data is available,
// to avoid miners withholding blocks but broadcasting headers, to get a
// competitive advantage.
pindexNew->nSequenceId = 0;
- BlockMap::iterator mi = m_block_index.insert(std::make_pair(hash, pindexNew)).first;
+
pindexNew->phashBlock = &((*mi).first);
BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);
if (miPrev != m_block_index.end()) {
- pindexNew->pprev = (*miPrev).second;
+ pindexNew->pprev = &(*miPrev).second;
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
pindexNew->BuildSkip();
}
@@ -80,8 +119,8 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
AssertLockHeld(cs_main);
LOCK(cs_LastBlockFile);
- for (const auto& entry : m_block_index) {
- CBlockIndex* pindex = entry.second;
+ for (auto& entry : m_block_index) {
+ CBlockIndex* pindex = &entry.second;
if (pindex->nFile == fileNumber) {
pindex->nStatus &= ~BLOCK_HAVE_DATA;
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
@@ -199,60 +238,27 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
return nullptr;
}
- // Return existing
- BlockMap::iterator mi = m_block_index.find(hash);
- if (mi != m_block_index.end()) {
- return (*mi).second;
+ const auto [mi, inserted]{m_block_index.try_emplace(hash)};
+ CBlockIndex* pindex = &(*mi).second;
+ if (inserted) {
+ pindex->phashBlock = &((*mi).first);
}
-
- // Create new
- CBlockIndex* pindexNew = new CBlockIndex();
- mi = m_block_index.insert(std::make_pair(hash, pindexNew)).first;
- pindexNew->phashBlock = &((*mi).first);
-
- return pindexNew;
+ return pindex;
}
-bool BlockManager::LoadBlockIndex(
- const Consensus::Params& consensus_params,
- ChainstateManager& chainman)
+bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
{
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
return false;
}
// Calculate nChainWork
- std::vector<std::pair<int, CBlockIndex*>> vSortedByHeight;
- vSortedByHeight.reserve(m_block_index.size());
- for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
- CBlockIndex* pindex = item.second;
- vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
- }
- sort(vSortedByHeight.begin(), vSortedByHeight.end());
-
- // Find start of assumed-valid region.
- int first_assumed_valid_height = std::numeric_limits<int>::max();
-
- for (const auto& [height, block] : vSortedByHeight) {
- if (block->IsAssumedValid()) {
- auto chainstates = chainman.GetAll();
-
- // If we encounter an assumed-valid block index entry, ensure that we have
- // one chainstate that tolerates assumed-valid entries and another that does
- // not (i.e. the background validation chainstate), since assumed-valid
- // entries should always be pending validation by a fully-validated chainstate.
- auto any_chain = [&](auto fnc) { return std::any_of(chainstates.cbegin(), chainstates.cend(), fnc); };
- assert(any_chain([](auto chainstate) { return chainstate->reliesOnAssumedValid(); }));
- assert(any_chain([](auto chainstate) { return !chainstate->reliesOnAssumedValid(); }));
-
- first_assumed_valid_height = height;
- break;
- }
- }
+ std::vector<CBlockIndex*> vSortedByHeight{GetAllBlockIndices()};
+ std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
+ CBlockIndexHeightOnlyComparator());
- for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight) {
+ for (CBlockIndex* pindex : vSortedByHeight) {
if (ShutdownRequested()) return false;
- CBlockIndex* pindex = item.second;
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
@@ -276,43 +282,6 @@ bool BlockManager::LoadBlockIndex(
pindex->nStatus |= BLOCK_FAILED_CHILD;
m_dirty_blockindex.insert(pindex);
}
- if (pindex->IsAssumedValid() ||
- (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
- (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))) {
-
- // Fill each chainstate's block candidate set. Only add assumed-valid
- // blocks to the tip candidate set if the chainstate is allowed to rely on
- // assumed-valid blocks.
- //
- // If all setBlockIndexCandidates contained the assumed-valid blocks, the
- // background chainstate's ActivateBestChain() call would add assumed-valid
- // blocks to the chain (based on how FindMostWorkChain() works). Obviously
- // we don't want this since the purpose of the background validation chain
- // is to validate assued-valid blocks.
- //
- // Note: This is considering all blocks whose height is greater or equal to
- // the first assumed-valid block to be assumed-valid blocks, and excluding
- // them from the background chainstate's setBlockIndexCandidates set. This
- // does mean that some blocks which are not technically assumed-valid
- // (later blocks on a fork beginning before the first assumed-valid block)
- // might not get added to the background chainstate, but this is ok,
- // because they will still be attached to the active chainstate if they
- // actually contain more work.
- //
- // Instead of this height-based approach, an earlier attempt was made at
- // detecting "holistically" whether the block index under consideration
- // relied on an assumed-valid ancestor, but this proved to be too slow to
- // be practical.
- for (CChainState* chainstate : chainman.GetAll()) {
- if (chainstate->reliesOnAssumedValid() ||
- pindex->nHeight < first_assumed_valid_height) {
- chainstate->setBlockIndexCandidates.insert(pindex);
- }
- }
- }
- if (pindex->nStatus & BLOCK_FAILED_MASK && (!chainman.m_best_invalid || pindex->nChainWork > chainman.m_best_invalid->nChainWork)) {
- chainman.m_best_invalid = pindex;
- }
if (pindex->pprev) {
pindex->BuildSkip();
}
@@ -327,10 +296,6 @@ void BlockManager::Unload()
{
m_blocks_unlinked.clear();
- for (const BlockMap::value_type& entry : m_block_index) {
- delete entry.second;
- }
-
m_block_index.clear();
m_blockfile_info.clear();
@@ -360,9 +325,9 @@ bool BlockManager::WriteBlockIndexDB()
return true;
}
-bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
+bool BlockManager::LoadBlockIndexDB()
{
- if (!LoadBlockIndex(::Params().GetConsensus(), chainman)) {
+ if (!LoadBlockIndex(::Params().GetConsensus())) {
return false;
}
@@ -386,10 +351,9 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
// Check presence of blk files
LogPrintf("Checking all blk files are present...\n");
std::set<int> setBlkDataFiles;
- for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
- CBlockIndex* pindex = item.second;
- if (pindex->nStatus & BLOCK_HAVE_DATA) {
- setBlkDataFiles.insert(pindex->nFile);
+ for (const auto& [_, block_index] : m_block_index) {
+ if (block_index.nStatus & BLOCK_HAVE_DATA) {
+ setBlkDataFiles.insert(block_index.nFile);
}
}
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
@@ -413,13 +377,13 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
return true;
}
-CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
+const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
{
const MapCheckpoints& checkpoints = data.mapCheckpoints;
for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
const uint256& hash = i.second;
- CBlockIndex* pindex = LookupBlockIndex(hash);
+ const CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex) {
return pindex;
}
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index 42e46797d2..a051e90808 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -5,6 +5,7 @@
#ifndef BITCOIN_NODE_BLOCKSTORAGE_H
#define BITCOIN_NODE_BLOCKSTORAGE_H
+#include <chain.h>
#include <fs.h>
#include <protocol.h> // For CMessageHeader::MessageStartChars
#include <sync.h>
@@ -20,7 +21,6 @@ class ArgsManager;
class BlockValidationState;
class CBlock;
class CBlockFileInfo;
-class CBlockIndex;
class CBlockUndo;
class CChain;
class CChainParams;
@@ -52,12 +52,21 @@ extern bool fPruneMode;
/** Number of MiB of block files that we're trying to stay below. */
extern uint64_t nPruneTarget;
-typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
+// Because validation code takes pointers to the map's CBlockIndex objects, if
+// we ever switch to another associative container, we need to either use a
+// container that has stable addressing (true of all std associative
+// containers), or make the key a `std::unique_ptr<CBlockIndex>`
+using BlockMap = std::unordered_map<uint256, CBlockIndex, BlockHasher>;
struct CBlockIndexWorkComparator {
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
};
+struct CBlockIndexHeightOnlyComparator {
+ /* Only compares the height of two block indices, doesn't try to tie-break */
+ bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
+};
+
/**
* Maintains a tree of blocks (stored in `m_block_index`) which is consulted
* to determine where the most-work tip is.
@@ -114,6 +123,8 @@ private:
public:
BlockMap m_block_index GUARDED_BY(cs_main);
+ std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+
/**
* All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
* Pruned nodes may have entries where B is missing data.
@@ -123,16 +134,15 @@ public:
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
- bool LoadBlockIndexDB(ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+ bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/**
* Load the blocktree off disk and into memory. Populate certain metadata
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
* collections like m_dirty_blockindex.
*/
- bool LoadBlockIndex(
- const Consensus::Params& consensus_params,
- ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ bool LoadBlockIndex(const Consensus::Params& consensus_params)
+ EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Clear all data members. */
void Unload() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@@ -144,7 +154,8 @@ public:
//! Mark one block file as pruned (modify associated database entries)
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
- CBlockIndex* LookupBlockIndex(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ CBlockIndex* LookupBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ const CBlockIndex* LookupBlockIndex(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Get block file info entry for one block file */
CBlockFileInfo* GetBlockFileInfo(size_t n);
@@ -158,7 +169,7 @@ public:
uint64_t CalculateCurrentUsage();
//! Returns last CBlockIndex* that is a checkpoint
- CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
~BlockManager()
{
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
index cb063ae9f8..74d53d2062 100644
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -490,7 +490,7 @@ public:
{
LOCK(cs_main);
const CChainState& active = Assert(m_node.chainman)->ActiveChainstate();
- if (CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
+ if (const CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
return fork->nHeight;
}
return std::nullopt;
@@ -557,7 +557,7 @@ public:
// used to limit the range, and passing min_height that's too low or
// max_height that's too high will not crash or change the result.
LOCK(::cs_main);
- if (CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
+ if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
// Check pprev to not segfault if min_height is too low
diff --git a/src/node/miner.cpp b/src/node/miner.cpp
index 7fe10ecabc..917df91933 100644
--- a/src/node/miner.cpp
+++ b/src/node/miner.cpp
@@ -50,7 +50,7 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
block.vtx.at(0) = MakeTransactionRef(tx);
- CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
+ const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus());
block.hashMerkleRoot = BlockMerkleRoot(block);
@@ -97,7 +97,6 @@ void BlockAssembler::resetBlock()
// Reserve space for coinbase tx
nBlockWeight = 4000;
nBlockSigOpsCost = 400;
- fIncludeWitness = false;
// These counters do not include coinbase tx
nBlockTx = 0;
@@ -137,17 +136,6 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->nTime = GetAdjustedTime();
m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
- // Decide whether to include witness transactions
- // This is only needed in case the witness softfork activation is reverted
- // (which would require a very deep reorganization).
- // Note that the mempool would accept transactions with witness data before
- // the deployment is active, but we would only ever mine blocks after activation
- // unless there is a massive block reorganization with the witness softfork
- // not activated.
- // TODO: replace this with a call to main to assess validity of a mempool
- // transaction (which in most cases can be a no-op).
- fIncludeWitness = DeploymentActiveAfter(pindexPrev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT);
-
int nPackagesSelected = 0;
int nDescendantsUpdated = 0;
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
@@ -215,17 +203,12 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
// Perform transaction-level checks before adding to block:
// - transaction finality (locktime)
-// - premature witness (in case segwit transactions are added to mempool before
-// segwit activation)
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package) const
{
for (CTxMemPool::txiter it : package) {
if (!IsFinalTx(it->GetTx(), nHeight, m_lock_time_cutoff)) {
return false;
}
- if (!fIncludeWitness && it->GetTx().HasWitness()) {
- return false;
- }
}
return true;
}
diff --git a/src/node/miner.h b/src/node/miner.h
index c96da874a7..97c55f2864 100644
--- a/src/node/miner.h
+++ b/src/node/miner.h
@@ -132,7 +132,6 @@ private:
std::unique_ptr<CBlockTemplate> pblocktemplate;
// Configuration parameters for the block size
- bool fIncludeWitness;
unsigned int nBlockMaxWeight;
CFeeRate blockMinFeeRate;
diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp
index d6f80e1558..c6b884e40a 100644
--- a/src/qt/bitcoin.cpp
+++ b/src/qt/bitcoin.cpp
@@ -587,7 +587,7 @@ int GuiMain(int argc, char* argv[])
return EXIT_FAILURE;
}
#ifdef ENABLE_WALLET
- // Parse URIs on command line -- this can affect Params()
+ // Parse URIs on command line
PaymentServer::ipcParseCommandLine(argc, argv);
#endif
diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp
index 9565fa508f..3108c93d7c 100644
--- a/src/qt/guiutil.cpp
+++ b/src/qt/guiutil.cpp
@@ -713,23 +713,18 @@ QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction
QString formatDurationStr(std::chrono::seconds dur)
{
- const auto secs = count_seconds(dur);
- QStringList strList;
- int days = secs / 86400;
- int hours = (secs % 86400) / 3600;
- int mins = (secs % 3600) / 60;
- int seconds = secs % 60;
-
- if (days)
- strList.append(QObject::tr("%1 d").arg(days));
- if (hours)
- strList.append(QObject::tr("%1 h").arg(hours));
- if (mins)
- strList.append(QObject::tr("%1 m").arg(mins));
- if (seconds || (!days && !hours && !mins))
- strList.append(QObject::tr("%1 s").arg(seconds));
-
- return strList.join(" ");
+ using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20
+ const auto d{std::chrono::duration_cast<days>(dur)};
+ const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)};
+ const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)};
+ const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)};
+ QStringList str_list;
+ if (auto d2{d.count()}) str_list.append(QObject::tr("%1 d").arg(d2));
+ if (auto h2{h.count()}) str_list.append(QObject::tr("%1 h").arg(h2));
+ if (auto m2{m.count()}) str_list.append(QObject::tr("%1 m").arg(m2));
+ const auto s2{s.count()};
+ if (s2 || str_list.empty()) str_list.append(QObject::tr("%1 s").arg(s2));
+ return str_list.join(" ");
}
QString formatServicesStr(quint64 mask)
@@ -888,11 +883,7 @@ void PolishProgressDialog(QProgressDialog* dialog)
int TextWidth(const QFontMetrics& fm, const QString& text)
{
-#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
return fm.horizontalAdvance(text);
-#else
- return fm.width(text);
-#endif
}
void LogQtInfo()
diff --git a/src/qt/paymentserver.cpp b/src/qt/paymentserver.cpp
index cb09035b45..c82f0683fc 100644
--- a/src/qt/paymentserver.cpp
+++ b/src/qt/paymentserver.cpp
@@ -78,32 +78,11 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[])
for (int i = 1; i < argc; i++)
{
QString arg(argv[i]);
- if (arg.startsWith("-"))
- continue;
+ if (arg.startsWith("-")) continue;
- // If the bitcoin: URI contains a payment request, we are not able to detect the
- // network as that would require fetching and parsing the payment request.
- // That means clicking such an URI which contains a testnet payment request
- // will start a mainnet instance and throw a "wrong network" error.
if (arg.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) // bitcoin: URI
{
- if (savedPaymentRequests.contains(arg)) continue;
savedPaymentRequests.insert(arg);
-
- SendCoinsRecipient r;
- if (GUIUtil::parseBitcoinURI(arg, &r) && !r.address.isEmpty())
- {
- auto tempChainParams = CreateChainParams(gArgs, CBaseChainParams::MAIN);
-
- if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
- SelectParams(CBaseChainParams::MAIN);
- } else {
- tempChainParams = CreateChainParams(gArgs, CBaseChainParams::TESTNET);
- if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
- SelectParams(CBaseChainParams::TESTNET);
- }
- }
- }
}
}
}
diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp
index 579ef0c3fd..c924789796 100644
--- a/src/qt/sendcoinsdialog.cpp
+++ b/src/qt/sendcoinsdialog.cpp
@@ -401,6 +401,80 @@ bool SendCoinsDialog::PrepareSendText(QString& question_string, QString& informa
return true;
}
+void SendCoinsDialog::presentPSBT(PartiallySignedTransaction& psbtx)
+{
+ // Serialize the PSBT
+ CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
+ ssTx << psbtx;
+ GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
+ QMessageBox msgBox;
+ msgBox.setText("Unsigned Transaction");
+ msgBox.setInformativeText("The PSBT has been copied to the clipboard. You can also save it.");
+ msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
+ msgBox.setDefaultButton(QMessageBox::Discard);
+ switch (msgBox.exec()) {
+ case QMessageBox::Save: {
+ QString selectedFilter;
+ QString fileNameSuggestion = "";
+ bool first = true;
+ for (const SendCoinsRecipient &rcp : m_current_transaction->getRecipients()) {
+ if (!first) {
+ fileNameSuggestion.append(" - ");
+ }
+ QString labelOrAddress = rcp.label.isEmpty() ? rcp.address : rcp.label;
+ QString amount = BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount);
+ fileNameSuggestion.append(labelOrAddress + "-" + amount);
+ first = false;
+ }
+ fileNameSuggestion.append(".psbt");
+ QString filename = GUIUtil::getSaveFileName(this,
+ tr("Save Transaction Data"), fileNameSuggestion,
+ //: Expanded name of the binary PSBT file format. See: BIP 174.
+ tr("Partially Signed Transaction (Binary)") + QLatin1String(" (*.psbt)"), &selectedFilter);
+ if (filename.isEmpty()) {
+ return;
+ }
+ std::ofstream out{filename.toLocal8Bit().data(), std::ofstream::out | std::ofstream::binary};
+ out << ssTx.str();
+ out.close();
+ Q_EMIT message(tr("PSBT saved"), "PSBT saved to disk", CClientUIInterface::MSG_INFORMATION);
+ break;
+ }
+ case QMessageBox::Discard:
+ break;
+ default:
+ assert(false);
+ } // msgBox.exec()
+}
+
+bool SendCoinsDialog::signWithExternalSigner(PartiallySignedTransaction& psbtx, CMutableTransaction& mtx, bool& complete) {
+ TransactionError err;
+ try {
+ err = model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/true, /*bip32derivs=*/true, /*n_signed=*/nullptr, psbtx, complete);
+ } catch (const std::runtime_error& e) {
+ QMessageBox::critical(nullptr, tr("Sign failed"), e.what());
+ return false;
+ }
+ if (err == TransactionError::EXTERNAL_SIGNER_NOT_FOUND) {
+ //: "External signer" means using devices such as hardware wallets.
+ QMessageBox::critical(nullptr, tr("External signer not found"), "External signer not found");
+ return false;
+ }
+ if (err == TransactionError::EXTERNAL_SIGNER_FAILED) {
+ //: "External signer" means using devices such as hardware wallets.
+ QMessageBox::critical(nullptr, tr("External signer failure"), "External signer failure");
+ return false;
+ }
+ if (err != TransactionError::OK) {
+ tfm::format(std::cerr, "Failed to sign PSBT");
+ processSendCoinsReturn(WalletModel::TransactionCreationFailed);
+ return false;
+ }
+ // fillPSBT does not always properly finalize
+ complete = FinalizeAndExtractPSBT(psbtx, mtx);
+ return true;
+}
+
void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
{
if(!model || !model->getOptionsModel())
@@ -411,7 +485,9 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
assert(m_current_transaction);
const QString confirmation = tr("Confirm send coins");
- auto confirmationDialog = new SendConfirmationDialog(confirmation, question_string, informative_text, detailed_text, SEND_CONFIRM_DELAY, !model->wallet().privateKeysDisabled(), model->getOptionsModel()->getEnablePSBTControls(), this);
+ const bool enable_send{!model->wallet().privateKeysDisabled() || model->wallet().hasExternalSigner()};
+ const bool always_show_unsigned{model->getOptionsModel()->getEnablePSBTControls()};
+ auto confirmationDialog = new SendConfirmationDialog(confirmation, question_string, informative_text, detailed_text, SEND_CONFIRM_DELAY, enable_send, always_show_unsigned, this);
confirmationDialog->setAttribute(Qt::WA_DeleteOnClose);
// TODO: Replace QDialog::exec() with safer QDialog::show().
const auto retval = static_cast<QMessageBox::StandardButton>(confirmationDialog->exec());
@@ -424,49 +500,50 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
bool send_failure = false;
if (retval == QMessageBox::Save) {
+ // "Create Unsigned" clicked
CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
PartiallySignedTransaction psbtx(mtx);
bool complete = false;
- // Always fill without signing first. This prevents an external signer
- // from being called prematurely and is not expensive.
- TransactionError err = model->wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, nullptr, psbtx, complete);
+ // Fill without signing
+ TransactionError err = model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/false, /*bip32derivs=*/true, /*n_signed=*/nullptr, psbtx, complete);
assert(!complete);
assert(err == TransactionError::OK);
+
+ // Copy PSBT to clipboard and offer to save
+ presentPSBT(psbtx);
+ } else {
+ // "Send" clicked
+ assert(!model->wallet().privateKeysDisabled() || model->wallet().hasExternalSigner());
+ bool broadcast = true;
if (model->wallet().hasExternalSigner()) {
- try {
- err = model->wallet().fillPSBT(SIGHASH_ALL, true /* sign */, true /* bip32derivs */, nullptr, psbtx, complete);
- } catch (const std::runtime_error& e) {
- QMessageBox::critical(nullptr, tr("Sign failed"), e.what());
- send_failure = true;
- return;
- }
- if (err == TransactionError::EXTERNAL_SIGNER_NOT_FOUND) {
- //: "External signer" means using devices such as hardware wallets.
- QMessageBox::critical(nullptr, tr("External signer not found"), "External signer not found");
- send_failure = true;
- return;
- }
- if (err == TransactionError::EXTERNAL_SIGNER_FAILED) {
- //: "External signer" means using devices such as hardware wallets.
- QMessageBox::critical(nullptr, tr("External signer failure"), "External signer failure");
- send_failure = true;
- return;
- }
- if (err != TransactionError::OK) {
- tfm::format(std::cerr, "Failed to sign PSBT");
- processSendCoinsReturn(WalletModel::TransactionCreationFailed);
- send_failure = true;
- return;
+ CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
+ PartiallySignedTransaction psbtx(mtx);
+ bool complete = false;
+ // Always fill without signing first. This prevents an external signer
+ // from being called prematurely and is not expensive.
+ TransactionError err = model->wallet().fillPSBT(SIGHASH_ALL, /*sign=*/false, /*bip32derivs=*/true, /*n_signed=*/nullptr, psbtx, complete);
+ assert(!complete);
+ assert(err == TransactionError::OK);
+ send_failure = !signWithExternalSigner(psbtx, mtx, complete);
+ // Don't broadcast when user rejects it on the device or there's a failure:
+ broadcast = complete && !send_failure;
+ if (!send_failure) {
+ // A transaction signed with an external signer is not always complete,
+ // e.g. in a multisig wallet.
+ if (complete) {
+ // Prepare transaction for broadcast transaction if complete
+ const CTransactionRef tx = MakeTransactionRef(mtx);
+ m_current_transaction->setWtx(tx);
+ } else {
+ presentPSBT(psbtx);
+ }
}
- // fillPSBT does not always properly finalize
- complete = FinalizeAndExtractPSBT(psbtx, mtx);
}
- // Broadcast transaction if complete (even with an external signer this
- // is not always the case, e.g. in a multisig wallet).
- if (complete) {
- const CTransactionRef tx = MakeTransactionRef(mtx);
- m_current_transaction->setWtx(tx);
+ // Broadcast the transaction, unless an external signer was used and it
+ // failed, or more signatures are needed.
+ if (broadcast) {
+ // now send the prepared transaction
WalletModel::SendCoinsReturn sendStatus = model->sendCoins(*m_current_transaction);
// process sendStatus and on error generate message shown to user
processSendCoinsReturn(sendStatus);
@@ -476,64 +553,6 @@ void SendCoinsDialog::sendButtonClicked([[maybe_unused]] bool checked)
} else {
send_failure = true;
}
- return;
- }
-
- // Copy PSBT to clipboard and offer to save
- assert(!complete);
- // Serialize the PSBT
- CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
- ssTx << psbtx;
- GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
- QMessageBox msgBox;
- msgBox.setText("Unsigned Transaction");
- msgBox.setInformativeText("The PSBT has been copied to the clipboard. You can also save it.");
- msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
- msgBox.setDefaultButton(QMessageBox::Discard);
- switch (msgBox.exec()) {
- case QMessageBox::Save: {
- QString selectedFilter;
- QString fileNameSuggestion = "";
- bool first = true;
- for (const SendCoinsRecipient &rcp : m_current_transaction->getRecipients()) {
- if (!first) {
- fileNameSuggestion.append(" - ");
- }
- QString labelOrAddress = rcp.label.isEmpty() ? rcp.address : rcp.label;
- QString amount = BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount);
- fileNameSuggestion.append(labelOrAddress + "-" + amount);
- first = false;
- }
- fileNameSuggestion.append(".psbt");
- QString filename = GUIUtil::getSaveFileName(this,
- tr("Save Transaction Data"), fileNameSuggestion,
- //: Expanded name of the binary PSBT file format. See: BIP 174.
- tr("Partially Signed Transaction (Binary)") + QLatin1String(" (*.psbt)"), &selectedFilter);
- if (filename.isEmpty()) {
- return;
- }
- std::ofstream out{filename.toLocal8Bit().data(), std::ofstream::out | std::ofstream::binary};
- out << ssTx.str();
- out.close();
- Q_EMIT message(tr("PSBT saved"), "PSBT saved to disk", CClientUIInterface::MSG_INFORMATION);
- break;
- }
- case QMessageBox::Discard:
- break;
- default:
- assert(false);
- } // msgBox.exec()
- } else {
- assert(!model->wallet().privateKeysDisabled());
- // now send the prepared transaction
- WalletModel::SendCoinsReturn sendStatus = model->sendCoins(*m_current_transaction);
- // process sendStatus and on error generate message shown to user
- processSendCoinsReturn(sendStatus);
-
- if (sendStatus.status == WalletModel::OK) {
- Q_EMIT coinsSent(m_current_transaction->getWtx()->GetHash());
- } else {
- send_failure = true;
}
}
if (!send_failure) {
diff --git a/src/qt/sendcoinsdialog.h b/src/qt/sendcoinsdialog.h
index 4a16702756..400503d0c0 100644
--- a/src/qt/sendcoinsdialog.h
+++ b/src/qt/sendcoinsdialog.h
@@ -70,6 +70,8 @@ private:
bool fFeeMinimized;
const PlatformStyle *platformStyle;
+ // Copy PSBT to clipboard and offer to save it.
+ void presentPSBT(PartiallySignedTransaction& psbt);
// Process WalletModel::SendCoinsReturn and generate a pair consisting
// of a message and message flags for use in Q_EMIT message().
// Additional parameter msgArg can be used via .arg(msgArg).
@@ -77,6 +79,15 @@ private:
void minimizeFeeSection(bool fMinimize);
// Format confirmation message
bool PrepareSendText(QString& question_string, QString& informative_text, QString& detailed_text);
+ /* Sign PSBT using external signer.
+ *
+ * @param[in,out] psbtx the PSBT to sign
+ * @param[in,out] mtx needed to attempt to finalize
+ * @param[in,out] complete whether the PSBT is complete (a successfully signed multisig transaction may not be complete)
+ *
+ * @returns false if any failure occurred, which may include the user rejection of a transaction on the device.
+ */
+ bool signWithExternalSigner(PartiallySignedTransaction& psbt, CMutableTransaction& mtx, bool& complete);
void updateFeeMinimizedLabel();
void updateCoinControlState();
@@ -117,6 +128,8 @@ class SendConfirmationDialog : public QMessageBox
public:
SendConfirmationDialog(const QString& title, const QString& text, const QString& informative_text = "", const QString& detailed_text = "", int secDelay = SEND_CONFIRM_DELAY, bool enable_send = true, bool always_show_unsigned = true, QWidget* parent = nullptr);
+ /* Returns QMessageBox::Cancel, QMessageBox::Yes when "Send" is
+ clicked and QMessageBox::Save when "Create Unsigned" is clicked. */
int exec() override;
private Q_SLOTS:
diff --git a/src/qt/test/optiontests.cpp b/src/qt/test/optiontests.cpp
new file mode 100644
index 0000000000..51894e1915
--- /dev/null
+++ b/src/qt/test/optiontests.cpp
@@ -0,0 +1,31 @@
+// Copyright (c) 2018 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 <qt/bitcoin.h>
+#include <qt/test/optiontests.h>
+#include <test/util/setup_common.h>
+#include <util/system.h>
+
+#include <QSettings>
+#include <QTest>
+
+#include <univalue.h>
+
+//! Entry point for BitcoinApplication tests.
+void OptionTests::optionTests()
+{
+ // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure
+ // that setting integer prune value doesn't cause an exception to be thrown
+ // in the OptionsModel constructor
+ gArgs.LockSettings([&](util::Settings& settings) {
+ settings.forced_settings.erase("prune");
+ settings.rw_settings["prune"] = 3814;
+ });
+ gArgs.WriteSettingsFile();
+ OptionsModel{};
+ gArgs.LockSettings([&](util::Settings& settings) {
+ settings.rw_settings.erase("prune");
+ });
+ gArgs.WriteSettingsFile();
+}
diff --git a/src/qt/test/optiontests.h b/src/qt/test/optiontests.h
new file mode 100644
index 0000000000..779d4cc209
--- /dev/null
+++ b/src/qt/test/optiontests.h
@@ -0,0 +1,25 @@
+// 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_QT_TEST_OPTIONTESTS_H
+#define BITCOIN_QT_TEST_OPTIONTESTS_H
+
+#include <qt/optionsmodel.h>
+
+#include <QObject>
+
+class OptionTests : public QObject
+{
+ Q_OBJECT
+public:
+ explicit OptionTests(interfaces::Node& node) : m_node(node) {}
+
+private Q_SLOTS:
+ void optionTests();
+
+private:
+ interfaces::Node& m_node;
+};
+
+#endif // BITCOIN_QT_TEST_OPTIONTESTS_H
diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp
index 10b7e2ffe7..07d256f05a 100644
--- a/src/qt/test/test_main.cpp
+++ b/src/qt/test/test_main.cpp
@@ -10,6 +10,7 @@
#include <interfaces/node.h>
#include <qt/bitcoin.h>
#include <qt/test/apptests.h>
+#include <qt/test/optiontests.h>
#include <qt/test/rpcnestedtests.h>
#include <qt/test/uritests.h>
#include <test/util/setup_common.h>
@@ -89,6 +90,10 @@ int main(int argc, char* argv[])
if (QTest::qExec(&app_tests) != 0) {
fInvalid = true;
}
+ OptionTests options_tests(app.node());
+ if (QTest::qExec(&options_tests) != 0) {
+ fInvalid = true;
+ }
URITests test1;
if (QTest::qExec(&test1) != 0) {
fInvalid = true;
diff --git a/src/rest.cpp b/src/rest.cpp
index 063872b47a..4b6bb7ecaf 100644
--- a/src/rest.cpp
+++ b/src/rest.cpp
@@ -15,6 +15,7 @@
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <rpc/blockchain.h>
+#include <rpc/mempool.h>
#include <rpc/protocol.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
@@ -283,8 +284,8 @@ static bool rest_block(const std::any& context,
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
CBlock block;
- CBlockIndex* pblockindex = nullptr;
- CBlockIndex* tip = nullptr;
+ const CBlockIndex* pblockindex = nullptr;
+ const CBlockIndex* tip = nullptr;
{
ChainstateManager* maybe_chainman = GetChainman(context, req);
if (!maybe_chainman) return false;
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 9817c80cbd..35b5b979eb 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -26,10 +26,6 @@
#include <node/coinstats.h>
#include <node/context.h>
#include <node/utxo_snapshot.h>
-#include <policy/feerate.h>
-#include <policy/fees.h>
-#include <policy/policy.h>
-#include <policy/rbf.h>
#include <primitives/transaction.h>
#include <rpc/server.h>
#include <rpc/server_util.h>
@@ -40,8 +36,8 @@
#include <txdb.h>
#include <txmempool.h>
#include <undo.h>
+#include <univalue.h>
#include <util/strencodings.h>
-#include <util/string.h>
#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
@@ -50,8 +46,6 @@
#include <stdint.h>
-#include <univalue.h>
-
#include <condition_variable>
#include <memory>
#include <mutex>
@@ -110,7 +104,8 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
return blockindex == tip ? 1 : -1;
}
-CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman) {
+static const CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainman)
+{
LOCK(::cs_main);
CChain& active_chain = chainman.ActiveChain();
@@ -127,7 +122,7 @@ CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateManager& chainma
return active_chain[height];
} else {
const uint256 hash{ParseHashV(param, "hash_or_height")};
- CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
+ const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
if (!pindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
@@ -426,366 +421,6 @@ static RPCHelpMan getdifficulty()
};
}
-static std::vector<RPCResult> MempoolEntryDescription() { return {
- RPCResult{RPCResult::Type::NUM, "vsize", "virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted."},
- RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
- RPCResult{RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true,
- "transaction fee, denominated in " + CURRENCY_UNIT + " (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
- RPCResult{RPCResult::Type::STR_AMOUNT, "modifiedfee", /*optional=*/true,
- "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT +
- " (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
- RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
- RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
- RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
- RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
- RPCResult{RPCResult::Type::STR_AMOUNT, "descendantfees", /*optional=*/true,
- "transaction fees of in-mempool descendants (including this one) with fee deltas used for mining priority, denominated in " +
- CURRENCY_ATOM + "s (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
- RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
- RPCResult{RPCResult::Type::NUM, "ancestorsize", "virtual transaction size of in-mempool ancestors (including this one)"},
- RPCResult{RPCResult::Type::STR_AMOUNT, "ancestorfees", /*optional=*/true,
- "transaction fees of in-mempool ancestors (including this one) with fee deltas used for mining priority, denominated in " +
- CURRENCY_ATOM + "s (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
- RPCResult{RPCResult::Type::STR_HEX, "wtxid", "hash of serialized transaction, including witness data"},
- RPCResult{RPCResult::Type::OBJ, "fees", "",
- {
- RPCResult{RPCResult::Type::STR_AMOUNT, "base", "transaction fee, denominated in " + CURRENCY_UNIT},
- RPCResult{RPCResult::Type::STR_AMOUNT, "modified", "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
- RPCResult{RPCResult::Type::STR_AMOUNT, "ancestor", "transaction fees of in-mempool ancestors (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
- RPCResult{RPCResult::Type::STR_AMOUNT, "descendant", "transaction fees of in-mempool descendants (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
- }},
- RPCResult{RPCResult::Type::ARR, "depends", "unconfirmed transactions used as inputs for this transaction",
- {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "parent transaction id"}}},
- RPCResult{RPCResult::Type::ARR, "spentby", "unconfirmed transactions spending outputs from this transaction",
- {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "child transaction id"}}},
- RPCResult{RPCResult::Type::BOOL, "bip125-replaceable", "Whether this transaction could be replaced due to BIP125 (replace-by-fee)"},
- RPCResult{RPCResult::Type::BOOL, "unbroadcast", "Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)"},
-};}
-
-static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
-{
- AssertLockHeld(pool.cs);
-
- info.pushKV("vsize", (int)e.GetTxSize());
- info.pushKV("weight", (int)e.GetTxWeight());
- // TODO: top-level fee fields are deprecated. deprecated_fee_fields_enabled blocks should be removed in v24
- const bool deprecated_fee_fields_enabled{IsDeprecatedRPCEnabled("fees")};
- if (deprecated_fee_fields_enabled) {
- info.pushKV("fee", ValueFromAmount(e.GetFee()));
- info.pushKV("modifiedfee", ValueFromAmount(e.GetModifiedFee()));
- }
- info.pushKV("time", count_seconds(e.GetTime()));
- info.pushKV("height", (int)e.GetHeight());
- info.pushKV("descendantcount", e.GetCountWithDescendants());
- info.pushKV("descendantsize", e.GetSizeWithDescendants());
- if (deprecated_fee_fields_enabled) {
- info.pushKV("descendantfees", e.GetModFeesWithDescendants());
- }
- info.pushKV("ancestorcount", e.GetCountWithAncestors());
- info.pushKV("ancestorsize", e.GetSizeWithAncestors());
- if (deprecated_fee_fields_enabled) {
- info.pushKV("ancestorfees", e.GetModFeesWithAncestors());
- }
- info.pushKV("wtxid", pool.vTxHashes[e.vTxHashesIdx].first.ToString());
-
- UniValue fees(UniValue::VOBJ);
- fees.pushKV("base", ValueFromAmount(e.GetFee()));
- fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
- fees.pushKV("ancestor", ValueFromAmount(e.GetModFeesWithAncestors()));
- fees.pushKV("descendant", ValueFromAmount(e.GetModFeesWithDescendants()));
- info.pushKV("fees", fees);
-
- const CTransaction& tx = e.GetTx();
- std::set<std::string> setDepends;
- for (const CTxIn& txin : tx.vin)
- {
- if (pool.exists(GenTxid::Txid(txin.prevout.hash)))
- setDepends.insert(txin.prevout.hash.ToString());
- }
-
- UniValue depends(UniValue::VARR);
- for (const std::string& dep : setDepends)
- {
- depends.push_back(dep);
- }
-
- info.pushKV("depends", depends);
-
- UniValue spent(UniValue::VARR);
- const CTxMemPool::txiter& it = pool.mapTx.find(tx.GetHash());
- const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
- for (const CTxMemPoolEntry& child : children) {
- spent.push_back(child.GetTx().GetHash().ToString());
- }
-
- info.pushKV("spentby", spent);
-
- // Add opt-in RBF status
- bool rbfStatus = false;
- RBFTransactionState rbfState = IsRBFOptIn(tx, pool);
- if (rbfState == RBFTransactionState::UNKNOWN) {
- throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool");
- } else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) {
- rbfStatus = true;
- }
-
- info.pushKV("bip125-replaceable", rbfStatus);
- info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
-}
-
-UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
-{
- if (verbose) {
- if (include_mempool_sequence) {
- throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
- }
- LOCK(pool.cs);
- UniValue o(UniValue::VOBJ);
- for (const CTxMemPoolEntry& e : pool.mapTx) {
- const uint256& hash = e.GetTx().GetHash();
- UniValue info(UniValue::VOBJ);
- entryToJSON(pool, info, e);
- // Mempool has unique entries so there is no advantage in using
- // UniValue::pushKV, which checks if the key already exists in O(N).
- // UniValue::__pushKV is used instead which currently is O(1).
- o.__pushKV(hash.ToString(), info);
- }
- return o;
- } else {
- uint64_t mempool_sequence;
- std::vector<uint256> vtxid;
- {
- LOCK(pool.cs);
- pool.queryHashes(vtxid);
- mempool_sequence = pool.GetSequence();
- }
- UniValue a(UniValue::VARR);
- for (const uint256& hash : vtxid)
- a.push_back(hash.ToString());
-
- if (!include_mempool_sequence) {
- return a;
- } else {
- UniValue o(UniValue::VOBJ);
- o.pushKV("txids", a);
- o.pushKV("mempool_sequence", mempool_sequence);
- return o;
- }
- }
-}
-
-static RPCHelpMan getrawmempool()
-{
- return RPCHelpMan{"getrawmempool",
- "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
- "\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
- {
- {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
- {"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
- },
- {
- RPCResult{"for verbose = false",
- RPCResult::Type::ARR, "", "",
- {
- {RPCResult::Type::STR_HEX, "", "The transaction id"},
- }},
- RPCResult{"for verbose = true",
- RPCResult::Type::OBJ_DYN, "", "",
- {
- {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
- }},
- RPCResult{"for verbose = false and mempool_sequence = true",
- RPCResult::Type::OBJ, "", "",
- {
- {RPCResult::Type::ARR, "txids", "",
- {
- {RPCResult::Type::STR_HEX, "", "The transaction id"},
- }},
- {RPCResult::Type::NUM, "mempool_sequence", "The mempool sequence value."},
- }},
- },
- RPCExamples{
- HelpExampleCli("getrawmempool", "true")
- + HelpExampleRpc("getrawmempool", "true")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- bool fVerbose = false;
- if (!request.params[0].isNull())
- fVerbose = request.params[0].get_bool();
-
- bool include_mempool_sequence = false;
- if (!request.params[1].isNull()) {
- include_mempool_sequence = request.params[1].get_bool();
- }
-
- return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
-},
- };
-}
-
-static RPCHelpMan getmempoolancestors()
-{
- return RPCHelpMan{"getmempoolancestors",
- "\nIf txid is in the mempool, returns all in-mempool ancestors.\n",
- {
- {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
- {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
- },
- {
- RPCResult{"for verbose = false",
- RPCResult::Type::ARR, "", "",
- {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool ancestor transaction"}}},
- RPCResult{"for verbose = true",
- RPCResult::Type::OBJ_DYN, "", "",
- {
- {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
- }},
- },
- RPCExamples{
- HelpExampleCli("getmempoolancestors", "\"mytxid\"")
- + HelpExampleRpc("getmempoolancestors", "\"mytxid\"")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- bool fVerbose = false;
- if (!request.params[1].isNull())
- fVerbose = request.params[1].get_bool();
-
- uint256 hash = ParseHashV(request.params[0], "parameter 1");
-
- const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
- LOCK(mempool.cs);
-
- CTxMemPool::txiter it = mempool.mapTx.find(hash);
- if (it == mempool.mapTx.end()) {
- throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
- }
-
- CTxMemPool::setEntries setAncestors;
- uint64_t noLimit = std::numeric_limits<uint64_t>::max();
- std::string dummy;
- mempool.CalculateMemPoolAncestors(*it, setAncestors, noLimit, noLimit, noLimit, noLimit, dummy, false);
-
- if (!fVerbose) {
- UniValue o(UniValue::VARR);
- for (CTxMemPool::txiter ancestorIt : setAncestors) {
- o.push_back(ancestorIt->GetTx().GetHash().ToString());
- }
- return o;
- } else {
- UniValue o(UniValue::VOBJ);
- for (CTxMemPool::txiter ancestorIt : setAncestors) {
- const CTxMemPoolEntry &e = *ancestorIt;
- const uint256& _hash = e.GetTx().GetHash();
- UniValue info(UniValue::VOBJ);
- entryToJSON(mempool, info, e);
- o.pushKV(_hash.ToString(), info);
- }
- return o;
- }
-},
- };
-}
-
-static RPCHelpMan getmempooldescendants()
-{
- return RPCHelpMan{"getmempooldescendants",
- "\nIf txid is in the mempool, returns all in-mempool descendants.\n",
- {
- {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
- {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
- },
- {
- RPCResult{"for verbose = false",
- RPCResult::Type::ARR, "", "",
- {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool descendant transaction"}}},
- RPCResult{"for verbose = true",
- RPCResult::Type::OBJ_DYN, "", "",
- {
- {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
- }},
- },
- RPCExamples{
- HelpExampleCli("getmempooldescendants", "\"mytxid\"")
- + HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- bool fVerbose = false;
- if (!request.params[1].isNull())
- fVerbose = request.params[1].get_bool();
-
- uint256 hash = ParseHashV(request.params[0], "parameter 1");
-
- const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
- LOCK(mempool.cs);
-
- CTxMemPool::txiter it = mempool.mapTx.find(hash);
- if (it == mempool.mapTx.end()) {
- throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
- }
-
- CTxMemPool::setEntries setDescendants;
- mempool.CalculateDescendants(it, setDescendants);
- // CTxMemPool::CalculateDescendants will include the given tx
- setDescendants.erase(it);
-
- if (!fVerbose) {
- UniValue o(UniValue::VARR);
- for (CTxMemPool::txiter descendantIt : setDescendants) {
- o.push_back(descendantIt->GetTx().GetHash().ToString());
- }
-
- return o;
- } else {
- UniValue o(UniValue::VOBJ);
- for (CTxMemPool::txiter descendantIt : setDescendants) {
- const CTxMemPoolEntry &e = *descendantIt;
- const uint256& _hash = e.GetTx().GetHash();
- UniValue info(UniValue::VOBJ);
- entryToJSON(mempool, info, e);
- o.pushKV(_hash.ToString(), info);
- }
- return o;
- }
-},
- };
-}
-
-static RPCHelpMan getmempoolentry()
-{
- return RPCHelpMan{"getmempoolentry",
- "\nReturns mempool data for given transaction\n",
- {
- {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
- },
- RPCResult{
- RPCResult::Type::OBJ, "", "", MempoolEntryDescription()},
- RPCExamples{
- HelpExampleCli("getmempoolentry", "\"mytxid\"")
- + HelpExampleRpc("getmempoolentry", "\"mytxid\"")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- uint256 hash = ParseHashV(request.params[0], "parameter 1");
-
- const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
- LOCK(mempool.cs);
-
- CTxMemPool::txiter it = mempool.mapTx.find(hash);
- if (it == mempool.mapTx.end()) {
- throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
- }
-
- const CTxMemPoolEntry &e = *it;
- UniValue info(UniValue::VOBJ);
- entryToJSON(mempool, info, e);
- return info;
-},
- };
-}
-
static RPCHelpMan getblockfrompeer()
{
return RPCHelpMan{
@@ -854,7 +489,7 @@ static RPCHelpMan getblockhash()
if (nHeight < 0 || nHeight > active_chain.Height())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
- CBlockIndex* pblockindex = active_chain[nHeight];
+ const CBlockIndex* pblockindex = active_chain[nHeight];
return pblockindex->GetBlockHash().GetHex();
},
};
@@ -1132,7 +767,7 @@ static RPCHelpMan pruneblockchain()
// too low to be a block time (corresponds to timestamp from Sep 2001).
if (heightParam > 1000000000) {
// Add a 2 hour buffer to include blocks which might have had old timestamps
- CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0);
+ const CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0);
if (!pindex) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp.");
}
@@ -1226,7 +861,7 @@ static RPCHelpMan gettxoutsetinfo()
{
UniValue ret(UniValue::VOBJ);
- CBlockIndex* pindex{nullptr};
+ const CBlockIndex* pindex{nullptr};
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
CCoinsStats stats{hash_type};
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
@@ -1481,7 +1116,7 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, UniValue& softfo
// BIP9 status
bip9.pushKV("status", get_state_name(current_state));
bip9.pushKV("since", g_versionbitscache.StateSinceHeight(blockindex->pprev, consensusParams, id));
- bip9.pushKV("status-next", get_state_name(next_state));
+ bip9.pushKV("status_next", get_state_name(next_state));
// BIP9 signalling status, if applicable
if (has_signal) {
@@ -1623,7 +1258,7 @@ const std::vector<RPCResult> RPCHelpForDeployment{
{RPCResult::Type::NUM, "min_activation_height", "minimum height of blocks for which the rules may be enforced"},
{RPCResult::Type::STR, "status", "status of deployment at specified block (one of \"defined\", \"started\", \"locked_in\", \"active\", \"failed\")"},
{RPCResult::Type::NUM, "since", "height of the first block to which the status applies"},
- {RPCResult::Type::STR, "status-next", "status of deployment at the next block"},
+ {RPCResult::Type::STR, "status_next", "status of deployment at the next block"},
{RPCResult::Type::OBJ, "statistics", /*optional=*/true, "numeric statistics about signalling for a softfork (only for \"started\" and \"locked_in\" status)",
{
{RPCResult::Type::NUM, "period", "the length in blocks of the signalling period"},
@@ -1753,10 +1388,10 @@ static RPCHelpMan getchaintips()
std::set<const CBlockIndex*> setOrphans;
std::set<const CBlockIndex*> setPrevs;
- for (const std::pair<const uint256, CBlockIndex*>& item : chainman.BlockIndex()) {
- if (!active_chain.Contains(item.second)) {
- setOrphans.insert(item.second);
- setPrevs.insert(item.second->pprev);
+ for (const auto& [_, block_index] : chainman.BlockIndex()) {
+ if (!active_chain.Contains(&block_index)) {
+ setOrphans.insert(&block_index);
+ setPrevs.insert(block_index.pprev);
}
}
@@ -1809,53 +1444,6 @@ static RPCHelpMan getchaintips()
};
}
-UniValue MempoolInfoToJSON(const CTxMemPool& pool)
-{
- // Make sure this call is atomic in the pool.
- LOCK(pool.cs);
- UniValue ret(UniValue::VOBJ);
- ret.pushKV("loaded", pool.IsLoaded());
- ret.pushKV("size", (int64_t)pool.size());
- ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
- ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
- ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
- size_t maxmempool = gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
- ret.pushKV("maxmempool", (int64_t) maxmempool);
- ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));
- ret.pushKV("minrelaytxfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));
- ret.pushKV("unbroadcastcount", uint64_t{pool.GetUnbroadcastTxs().size()});
- return ret;
-}
-
-static RPCHelpMan getmempoolinfo()
-{
- return RPCHelpMan{"getmempoolinfo",
- "\nReturns details on the active state of the TX memory pool.\n",
- {},
- RPCResult{
- RPCResult::Type::OBJ, "", "",
- {
- {RPCResult::Type::BOOL, "loaded", "True if the mempool is fully loaded"},
- {RPCResult::Type::NUM, "size", "Current tx count"},
- {RPCResult::Type::NUM, "bytes", "Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted"},
- {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
- {RPCResult::Type::STR_AMOUNT, "total_fee", "Total fees for the mempool in " + CURRENCY_UNIT + ", ignoring modified fees through prioritisetransaction"},
- {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
- {RPCResult::Type::STR_AMOUNT, "mempoolminfee", "Minimum fee rate in " + CURRENCY_UNIT + "/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee"},
- {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
- {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"}
- }},
- RPCExamples{
- HelpExampleCli("getmempoolinfo", "")
- + HelpExampleRpc("getmempoolinfo", "")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- return MempoolInfoToJSON(EnsureAnyMemPool(request.context));
-},
- };
-}
-
static RPCHelpMan preciousblock()
{
return RPCHelpMan{"preciousblock",
@@ -2177,7 +1765,7 @@ static RPCHelpMan getblockstats()
{
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(cs_main);
- CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)};
+ const CBlockIndex* pindex{ParseHashOrHeight(request.params[0], chainman)};
CHECK_NONFATAL(pindex != nullptr);
std::set<std::string> stats;
@@ -2352,41 +1940,6 @@ static RPCHelpMan getblockstats()
};
}
-static RPCHelpMan savemempool()
-{
- return RPCHelpMan{"savemempool",
- "\nDumps the mempool to disk. It will fail until the previous dump is fully loaded.\n",
- {},
- RPCResult{
- RPCResult::Type::OBJ, "", "",
- {
- {RPCResult::Type::STR, "filename", "the directory and file where the mempool was saved"},
- }},
- RPCExamples{
- HelpExampleCli("savemempool", "")
- + HelpExampleRpc("savemempool", "")
- },
- [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
-{
- const ArgsManager& args{EnsureAnyArgsman(request.context)};
- const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
-
- if (!mempool.IsLoaded()) {
- throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
- }
-
- if (!DumpMempool(mempool)) {
- throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
- }
-
- UniValue ret(UniValue::VOBJ);
- ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
-
- return ret;
-},
- };
-}
-
namespace {
//! Search for a given set of pubkey scripts
bool FindScriptPubKey(std::atomic<int>& scan_progress, const std::atomic<bool>& should_abort, int64_t& count, CCoinsViewCursor* cursor, const std::set<CScript>& needles, std::map<COutPoint, Coin>& out_results, std::function<void()>& interruption_point)
@@ -2572,7 +2125,7 @@ static RPCHelpMan scantxoutset()
g_should_abort_scan = false;
int64_t count = 0;
std::unique_ptr<CCoinsViewCursor> pcursor;
- CBlockIndex* tip;
+ const CBlockIndex* tip;
NodeContext& node = EnsureAnyNodeContext(request.context);
{
ChainstateManager& chainman = EnsureChainman(node);
@@ -2760,7 +2313,7 @@ UniValue CreateUTXOSnapshot(
{
std::unique_ptr<CCoinsViewCursor> pcursor;
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
- CBlockIndex* tip;
+ const CBlockIndex* tip;
{
// We need to lock cs_main to ensure that the coinsdb isn't written to
@@ -2825,6 +2378,7 @@ UniValue CreateUTXOSnapshot(
return result;
}
+
void RegisterBlockchainRPCCommands(CRPCTable &t)
{
// clang-format off
@@ -2843,15 +2397,9 @@ static const CRPCCommand commands[] =
{ "blockchain", &getchaintips, },
{ "blockchain", &getdifficulty, },
{ "blockchain", &getdeploymentinfo, },
- { "blockchain", &getmempoolancestors, },
- { "blockchain", &getmempooldescendants, },
- { "blockchain", &getmempoolentry, },
- { "blockchain", &getmempoolinfo, },
- { "blockchain", &getrawmempool, },
{ "blockchain", &gettxout, },
{ "blockchain", &gettxoutsetinfo, },
{ "blockchain", &pruneblockchain, },
- { "blockchain", &savemempool, },
{ "blockchain", &verifychain, },
{ "blockchain", &preciousblock, },
diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h
index 1f51d7c1ad..a8c6d171cc 100644
--- a/src/rpc/blockchain.h
+++ b/src/rpc/blockchain.h
@@ -20,7 +20,6 @@ extern RecursiveMutex cs_main;
class CBlock;
class CBlockIndex;
class CChainState;
-class CTxMemPool;
class UniValue;
namespace node {
struct NodeContext;
@@ -42,12 +41,6 @@ void RPCNotifyBlockChange(const CBlockIndex*);
/** Block description to JSON */
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity) LOCKS_EXCLUDED(cs_main);
-/** Mempool information to JSON */
-UniValue MempoolInfoToJSON(const CTxMemPool& pool);
-
-/** Mempool to JSON */
-UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
-
/** Block header to JSON */
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);
diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp
new file mode 100644
index 0000000000..bc7ef0c08e
--- /dev/null
+++ b/src/rpc/mempool.cpp
@@ -0,0 +1,476 @@
+// Copyright (c) 2010 Satoshi Nakamoto
+// Copyright (c) 2009-2022 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 <rpc/blockchain.h>
+
+#include <core_io.h>
+#include <fs.h>
+#include <policy/rbf.h>
+#include <primitives/transaction.h>
+#include <rpc/server.h>
+#include <rpc/server_util.h>
+#include <rpc/util.h>
+#include <txmempool.h>
+#include <univalue.h>
+#include <validation.h>
+
+static std::vector<RPCResult> MempoolEntryDescription() { return {
+ RPCResult{RPCResult::Type::NUM, "vsize", "virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted."},
+ RPCResult{RPCResult::Type::NUM, "weight", "transaction weight as defined in BIP 141."},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "fee", /*optional=*/true,
+ "transaction fee, denominated in " + CURRENCY_UNIT + " (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "modifiedfee", /*optional=*/true,
+ "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT +
+ " (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
+ RPCResult{RPCResult::Type::NUM_TIME, "time", "local time transaction entered pool in seconds since 1 Jan 1970 GMT"},
+ RPCResult{RPCResult::Type::NUM, "height", "block height when transaction entered pool"},
+ RPCResult{RPCResult::Type::NUM, "descendantcount", "number of in-mempool descendant transactions (including this one)"},
+ RPCResult{RPCResult::Type::NUM, "descendantsize", "virtual transaction size of in-mempool descendants (including this one)"},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "descendantfees", /*optional=*/true,
+ "transaction fees of in-mempool descendants (including this one) with fee deltas used for mining priority, denominated in " +
+ CURRENCY_ATOM + "s (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
+ RPCResult{RPCResult::Type::NUM, "ancestorcount", "number of in-mempool ancestor transactions (including this one)"},
+ RPCResult{RPCResult::Type::NUM, "ancestorsize", "virtual transaction size of in-mempool ancestors (including this one)"},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "ancestorfees", /*optional=*/true,
+ "transaction fees of in-mempool ancestors (including this one) with fee deltas used for mining priority, denominated in " +
+ CURRENCY_ATOM + "s (DEPRECATED, returned only if config option -deprecatedrpc=fees is passed)"},
+ RPCResult{RPCResult::Type::STR_HEX, "wtxid", "hash of serialized transaction, including witness data"},
+ RPCResult{RPCResult::Type::OBJ, "fees", "",
+ {
+ RPCResult{RPCResult::Type::STR_AMOUNT, "base", "transaction fee, denominated in " + CURRENCY_UNIT},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "modified", "transaction fee with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "ancestor", "transaction fees of in-mempool ancestors (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
+ RPCResult{RPCResult::Type::STR_AMOUNT, "descendant", "transaction fees of in-mempool descendants (including this one) with fee deltas used for mining priority, denominated in " + CURRENCY_UNIT},
+ }},
+ RPCResult{RPCResult::Type::ARR, "depends", "unconfirmed transactions used as inputs for this transaction",
+ {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "parent transaction id"}}},
+ RPCResult{RPCResult::Type::ARR, "spentby", "unconfirmed transactions spending outputs from this transaction",
+ {RPCResult{RPCResult::Type::STR_HEX, "transactionid", "child transaction id"}}},
+ RPCResult{RPCResult::Type::BOOL, "bip125-replaceable", "Whether this transaction could be replaced due to BIP125 (replace-by-fee)"},
+ RPCResult{RPCResult::Type::BOOL, "unbroadcast", "Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)"},
+};}
+
+static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
+{
+ AssertLockHeld(pool.cs);
+
+ info.pushKV("vsize", (int)e.GetTxSize());
+ info.pushKV("weight", (int)e.GetTxWeight());
+ // TODO: top-level fee fields are deprecated. deprecated_fee_fields_enabled blocks should be removed in v24
+ const bool deprecated_fee_fields_enabled{IsDeprecatedRPCEnabled("fees")};
+ if (deprecated_fee_fields_enabled) {
+ info.pushKV("fee", ValueFromAmount(e.GetFee()));
+ info.pushKV("modifiedfee", ValueFromAmount(e.GetModifiedFee()));
+ }
+ info.pushKV("time", count_seconds(e.GetTime()));
+ info.pushKV("height", (int)e.GetHeight());
+ info.pushKV("descendantcount", e.GetCountWithDescendants());
+ info.pushKV("descendantsize", e.GetSizeWithDescendants());
+ if (deprecated_fee_fields_enabled) {
+ info.pushKV("descendantfees", e.GetModFeesWithDescendants());
+ }
+ info.pushKV("ancestorcount", e.GetCountWithAncestors());
+ info.pushKV("ancestorsize", e.GetSizeWithAncestors());
+ if (deprecated_fee_fields_enabled) {
+ info.pushKV("ancestorfees", e.GetModFeesWithAncestors());
+ }
+ info.pushKV("wtxid", pool.vTxHashes[e.vTxHashesIdx].first.ToString());
+
+ UniValue fees(UniValue::VOBJ);
+ fees.pushKV("base", ValueFromAmount(e.GetFee()));
+ fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
+ fees.pushKV("ancestor", ValueFromAmount(e.GetModFeesWithAncestors()));
+ fees.pushKV("descendant", ValueFromAmount(e.GetModFeesWithDescendants()));
+ info.pushKV("fees", fees);
+
+ const CTransaction& tx = e.GetTx();
+ std::set<std::string> setDepends;
+ for (const CTxIn& txin : tx.vin)
+ {
+ if (pool.exists(GenTxid::Txid(txin.prevout.hash)))
+ setDepends.insert(txin.prevout.hash.ToString());
+ }
+
+ UniValue depends(UniValue::VARR);
+ for (const std::string& dep : setDepends)
+ {
+ depends.push_back(dep);
+ }
+
+ info.pushKV("depends", depends);
+
+ UniValue spent(UniValue::VARR);
+ const CTxMemPool::txiter& it = pool.mapTx.find(tx.GetHash());
+ const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
+ for (const CTxMemPoolEntry& child : children) {
+ spent.push_back(child.GetTx().GetHash().ToString());
+ }
+
+ info.pushKV("spentby", spent);
+
+ // Add opt-in RBF status
+ bool rbfStatus = false;
+ RBFTransactionState rbfState = IsRBFOptIn(tx, pool);
+ if (rbfState == RBFTransactionState::UNKNOWN) {
+ throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool");
+ } else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) {
+ rbfStatus = true;
+ }
+
+ info.pushKV("bip125-replaceable", rbfStatus);
+ info.pushKV("unbroadcast", pool.IsUnbroadcastTx(tx.GetHash()));
+}
+
+UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempool_sequence)
+{
+ if (verbose) {
+ if (include_mempool_sequence) {
+ throw JSONRPCError(RPC_INVALID_PARAMETER, "Verbose results cannot contain mempool sequence values.");
+ }
+ LOCK(pool.cs);
+ UniValue o(UniValue::VOBJ);
+ for (const CTxMemPoolEntry& e : pool.mapTx) {
+ const uint256& hash = e.GetTx().GetHash();
+ UniValue info(UniValue::VOBJ);
+ entryToJSON(pool, info, e);
+ // Mempool has unique entries so there is no advantage in using
+ // UniValue::pushKV, which checks if the key already exists in O(N).
+ // UniValue::__pushKV is used instead which currently is O(1).
+ o.__pushKV(hash.ToString(), info);
+ }
+ return o;
+ } else {
+ uint64_t mempool_sequence;
+ std::vector<uint256> vtxid;
+ {
+ LOCK(pool.cs);
+ pool.queryHashes(vtxid);
+ mempool_sequence = pool.GetSequence();
+ }
+ UniValue a(UniValue::VARR);
+ for (const uint256& hash : vtxid)
+ a.push_back(hash.ToString());
+
+ if (!include_mempool_sequence) {
+ return a;
+ } else {
+ UniValue o(UniValue::VOBJ);
+ o.pushKV("txids", a);
+ o.pushKV("mempool_sequence", mempool_sequence);
+ return o;
+ }
+ }
+}
+
+RPCHelpMan getrawmempool()
+{
+ return RPCHelpMan{"getrawmempool",
+ "\nReturns all transaction ids in memory pool as a json array of string transaction ids.\n"
+ "\nHint: use getmempoolentry to fetch a specific transaction from the mempool.\n",
+ {
+ {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ {"mempool_sequence", RPCArg::Type::BOOL, RPCArg::Default{false}, "If verbose=false, returns a json object with transaction list and mempool sequence number attached."},
+ },
+ {
+ RPCResult{"for verbose = false",
+ RPCResult::Type::ARR, "", "",
+ {
+ {RPCResult::Type::STR_HEX, "", "The transaction id"},
+ }},
+ RPCResult{"for verbose = true",
+ RPCResult::Type::OBJ_DYN, "", "",
+ {
+ {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ }},
+ RPCResult{"for verbose = false and mempool_sequence = true",
+ RPCResult::Type::OBJ, "", "",
+ {
+ {RPCResult::Type::ARR, "txids", "",
+ {
+ {RPCResult::Type::STR_HEX, "", "The transaction id"},
+ }},
+ {RPCResult::Type::NUM, "mempool_sequence", "The mempool sequence value."},
+ }},
+ },
+ RPCExamples{
+ HelpExampleCli("getrawmempool", "true")
+ + HelpExampleRpc("getrawmempool", "true")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ bool fVerbose = false;
+ if (!request.params[0].isNull())
+ fVerbose = request.params[0].get_bool();
+
+ bool include_mempool_sequence = false;
+ if (!request.params[1].isNull()) {
+ include_mempool_sequence = request.params[1].get_bool();
+ }
+
+ return MempoolToJSON(EnsureAnyMemPool(request.context), fVerbose, include_mempool_sequence);
+},
+ };
+}
+
+RPCHelpMan getmempoolancestors()
+{
+ return RPCHelpMan{"getmempoolancestors",
+ "\nIf txid is in the mempool, returns all in-mempool ancestors.\n",
+ {
+ {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
+ {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ },
+ {
+ RPCResult{"for verbose = false",
+ RPCResult::Type::ARR, "", "",
+ {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool ancestor transaction"}}},
+ RPCResult{"for verbose = true",
+ RPCResult::Type::OBJ_DYN, "", "",
+ {
+ {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ }},
+ },
+ RPCExamples{
+ HelpExampleCli("getmempoolancestors", "\"mytxid\"")
+ + HelpExampleRpc("getmempoolancestors", "\"mytxid\"")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ bool fVerbose = false;
+ if (!request.params[1].isNull())
+ fVerbose = request.params[1].get_bool();
+
+ uint256 hash = ParseHashV(request.params[0], "parameter 1");
+
+ const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
+ LOCK(mempool.cs);
+
+ CTxMemPool::txiter it = mempool.mapTx.find(hash);
+ if (it == mempool.mapTx.end()) {
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
+ }
+
+ CTxMemPool::setEntries setAncestors;
+ uint64_t noLimit = std::numeric_limits<uint64_t>::max();
+ std::string dummy;
+ mempool.CalculateMemPoolAncestors(*it, setAncestors, noLimit, noLimit, noLimit, noLimit, dummy, false);
+
+ if (!fVerbose) {
+ UniValue o(UniValue::VARR);
+ for (CTxMemPool::txiter ancestorIt : setAncestors) {
+ o.push_back(ancestorIt->GetTx().GetHash().ToString());
+ }
+ return o;
+ } else {
+ UniValue o(UniValue::VOBJ);
+ for (CTxMemPool::txiter ancestorIt : setAncestors) {
+ const CTxMemPoolEntry &e = *ancestorIt;
+ const uint256& _hash = e.GetTx().GetHash();
+ UniValue info(UniValue::VOBJ);
+ entryToJSON(mempool, info, e);
+ o.pushKV(_hash.ToString(), info);
+ }
+ return o;
+ }
+},
+ };
+}
+
+RPCHelpMan getmempooldescendants()
+{
+ return RPCHelpMan{"getmempooldescendants",
+ "\nIf txid is in the mempool, returns all in-mempool descendants.\n",
+ {
+ {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
+ {"verbose", RPCArg::Type::BOOL, RPCArg::Default{false}, "True for a json object, false for array of transaction ids"},
+ },
+ {
+ RPCResult{"for verbose = false",
+ RPCResult::Type::ARR, "", "",
+ {{RPCResult::Type::STR_HEX, "", "The transaction id of an in-mempool descendant transaction"}}},
+ RPCResult{"for verbose = true",
+ RPCResult::Type::OBJ_DYN, "", "",
+ {
+ {RPCResult::Type::OBJ, "transactionid", "", MempoolEntryDescription()},
+ }},
+ },
+ RPCExamples{
+ HelpExampleCli("getmempooldescendants", "\"mytxid\"")
+ + HelpExampleRpc("getmempooldescendants", "\"mytxid\"")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ bool fVerbose = false;
+ if (!request.params[1].isNull())
+ fVerbose = request.params[1].get_bool();
+
+ uint256 hash = ParseHashV(request.params[0], "parameter 1");
+
+ const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
+ LOCK(mempool.cs);
+
+ CTxMemPool::txiter it = mempool.mapTx.find(hash);
+ if (it == mempool.mapTx.end()) {
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
+ }
+
+ CTxMemPool::setEntries setDescendants;
+ mempool.CalculateDescendants(it, setDescendants);
+ // CTxMemPool::CalculateDescendants will include the given tx
+ setDescendants.erase(it);
+
+ if (!fVerbose) {
+ UniValue o(UniValue::VARR);
+ for (CTxMemPool::txiter descendantIt : setDescendants) {
+ o.push_back(descendantIt->GetTx().GetHash().ToString());
+ }
+
+ return o;
+ } else {
+ UniValue o(UniValue::VOBJ);
+ for (CTxMemPool::txiter descendantIt : setDescendants) {
+ const CTxMemPoolEntry &e = *descendantIt;
+ const uint256& _hash = e.GetTx().GetHash();
+ UniValue info(UniValue::VOBJ);
+ entryToJSON(mempool, info, e);
+ o.pushKV(_hash.ToString(), info);
+ }
+ return o;
+ }
+},
+ };
+}
+
+RPCHelpMan getmempoolentry()
+{
+ return RPCHelpMan{"getmempoolentry",
+ "\nReturns mempool data for given transaction\n",
+ {
+ {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id (must be in mempool)"},
+ },
+ RPCResult{
+ RPCResult::Type::OBJ, "", "", MempoolEntryDescription()},
+ RPCExamples{
+ HelpExampleCli("getmempoolentry", "\"mytxid\"")
+ + HelpExampleRpc("getmempoolentry", "\"mytxid\"")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ uint256 hash = ParseHashV(request.params[0], "parameter 1");
+
+ const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
+ LOCK(mempool.cs);
+
+ CTxMemPool::txiter it = mempool.mapTx.find(hash);
+ if (it == mempool.mapTx.end()) {
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
+ }
+
+ const CTxMemPoolEntry &e = *it;
+ UniValue info(UniValue::VOBJ);
+ entryToJSON(mempool, info, e);
+ return info;
+},
+ };
+}
+
+UniValue MempoolInfoToJSON(const CTxMemPool& pool)
+{
+ // Make sure this call is atomic in the pool.
+ LOCK(pool.cs);
+ UniValue ret(UniValue::VOBJ);
+ ret.pushKV("loaded", pool.IsLoaded());
+ ret.pushKV("size", (int64_t)pool.size());
+ ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
+ ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
+ ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
+ int64_t maxmempool{gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000};
+ ret.pushKV("maxmempool", maxmempool);
+ ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));
+ ret.pushKV("minrelaytxfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));
+ ret.pushKV("unbroadcastcount", uint64_t{pool.GetUnbroadcastTxs().size()});
+ return ret;
+}
+
+RPCHelpMan getmempoolinfo()
+{
+ return RPCHelpMan{"getmempoolinfo",
+ "\nReturns details on the active state of the TX memory pool.\n",
+ {},
+ RPCResult{
+ RPCResult::Type::OBJ, "", "",
+ {
+ {RPCResult::Type::BOOL, "loaded", "True if the mempool is fully loaded"},
+ {RPCResult::Type::NUM, "size", "Current tx count"},
+ {RPCResult::Type::NUM, "bytes", "Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted"},
+ {RPCResult::Type::NUM, "usage", "Total memory usage for the mempool"},
+ {RPCResult::Type::STR_AMOUNT, "total_fee", "Total fees for the mempool in " + CURRENCY_UNIT + ", ignoring modified fees through prioritisetransaction"},
+ {RPCResult::Type::NUM, "maxmempool", "Maximum memory usage for the mempool"},
+ {RPCResult::Type::STR_AMOUNT, "mempoolminfee", "Minimum fee rate in " + CURRENCY_UNIT + "/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee"},
+ {RPCResult::Type::STR_AMOUNT, "minrelaytxfee", "Current minimum relay fee for transactions"},
+ {RPCResult::Type::NUM, "unbroadcastcount", "Current number of transactions that haven't passed initial broadcast yet"}
+ }},
+ RPCExamples{
+ HelpExampleCli("getmempoolinfo", "")
+ + HelpExampleRpc("getmempoolinfo", "")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ return MempoolInfoToJSON(EnsureAnyMemPool(request.context));
+},
+ };
+}
+
+RPCHelpMan savemempool()
+{
+ return RPCHelpMan{"savemempool",
+ "\nDumps the mempool to disk. It will fail until the previous dump is fully loaded.\n",
+ {},
+ RPCResult{
+ RPCResult::Type::OBJ, "", "",
+ {
+ {RPCResult::Type::STR, "filename", "the directory and file where the mempool was saved"},
+ }},
+ RPCExamples{
+ HelpExampleCli("savemempool", "")
+ + HelpExampleRpc("savemempool", "")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+{
+ const ArgsManager& args{EnsureAnyArgsman(request.context)};
+ const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
+
+ if (!mempool.IsLoaded()) {
+ throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
+ }
+
+ if (!DumpMempool(mempool)) {
+ throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
+ }
+
+ UniValue ret(UniValue::VOBJ);
+ ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
+
+ return ret;
+},
+ };
+}
+
+void RegisterMempoolRPCCommands(CRPCTable& t)
+{
+ static const CRPCCommand commands[]{
+ // category actor (function)
+ // -------- ----------------
+ {"blockchain", &getmempoolancestors},
+ {"blockchain", &getmempooldescendants},
+ {"blockchain", &getmempoolentry},
+ {"blockchain", &getmempoolinfo},
+ {"blockchain", &getrawmempool},
+ {"blockchain", &savemempool},
+ };
+ for (const auto& c : commands) {
+ t.appendCommand(c.name, &c);
+ }
+}
diff --git a/src/rpc/mempool.h b/src/rpc/mempool.h
new file mode 100644
index 0000000000..229d7d52dd
--- /dev/null
+++ b/src/rpc/mempool.h
@@ -0,0 +1,17 @@
+// Copyright (c) 2017-2022 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_RPC_MEMPOOL_H
+#define BITCOIN_RPC_MEMPOOL_H
+
+class CTxMemPool;
+class UniValue;
+
+/** Mempool information to JSON */
+UniValue MempoolInfoToJSON(const CTxMemPool& pool);
+
+/** Mempool to JSON */
+UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
+
+#endif // BITCOIN_RPC_MEMPOOL_H
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index 6272a7c8cf..1ef531b293 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -67,7 +67,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
LOCK(cs_main);
entry.pushKV("blockhash", hashBlock.GetHex());
- CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
+ const CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
if (pindex) {
if (active_chainstate.m_chain.Contains(pindex)) {
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
@@ -207,7 +207,7 @@ static RPCHelpMan getrawtransaction()
bool in_active_chain = true;
uint256 hash = ParseHashV(request.params[0], "parameter 1");
- CBlockIndex* blockindex = nullptr;
+ const CBlockIndex* blockindex = nullptr;
if (hash == Params().GenesisBlock().hashMerkleRoot) {
// Special exception for the genesis block coinbase transaction
@@ -302,7 +302,7 @@ static RPCHelpMan gettxoutproof()
}
}
- CBlockIndex* pblockindex = nullptr;
+ const CBlockIndex* pblockindex = nullptr;
uint256 hashBlock;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
if (!request.params[1].isNull()) {
diff --git a/src/rpc/register.h b/src/rpc/register.h
index c5055cc9d7..cc3a5e0a63 100644
--- a/src/rpc/register.h
+++ b/src/rpc/register.h
@@ -11,6 +11,8 @@ class CRPCTable;
/** Register block chain RPC commands */
void RegisterBlockchainRPCCommands(CRPCTable &tableRPC);
+/** Register mempool RPC commands */
+void RegisterMempoolRPCCommands(CRPCTable&);
/** Register P2P networking RPC commands */
void RegisterNetRPCCommands(CRPCTable &tableRPC);
/** Register miscellaneous RPC commands */
@@ -25,6 +27,7 @@ void RegisterSignerRPCCommands(CRPCTable &tableRPC);
static inline void RegisterAllCoreRPCCommands(CRPCTable &t)
{
RegisterBlockchainRPCCommands(t);
+ RegisterMempoolRPCCommands(t);
RegisterNetRPCCommands(t);
RegisterMiscRPCCommands(t);
RegisterMiningRPCCommands(t);
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp
index 798c4b3ea0..23540f6aef 100644
--- a/src/script/descriptor.cpp
+++ b/src/script/descriptor.cpp
@@ -802,6 +802,30 @@ public:
bool IsSingleType() const final { return true; }
};
+/** A parsed (sorted)multi_a(...) descriptor. Always uses x-only pubkeys. */
+class MultiADescriptor final : public DescriptorImpl
+{
+ const int m_threshold;
+ const bool m_sorted;
+protected:
+ std::string ToStringExtra() const override { return strprintf("%i", m_threshold); }
+ std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override {
+ CScript ret;
+ std::vector<XOnlyPubKey> xkeys;
+ for (const auto& key : keys) xkeys.emplace_back(key);
+ if (m_sorted) std::sort(xkeys.begin(), xkeys.end());
+ ret << ToByteVector(xkeys[0]) << OP_CHECKSIG;
+ for (size_t i = 1; i < keys.size(); ++i) {
+ ret << ToByteVector(xkeys[i]) << OP_CHECKSIGADD;
+ }
+ ret << m_threshold << OP_NUMEQUAL;
+ return Vector(std::move(ret));
+ }
+public:
+ MultiADescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers, bool sorted = false) : DescriptorImpl(std::move(providers), sorted ? "sortedmulti_a" : "multi_a"), m_threshold(threshold), m_sorted(sorted) {}
+ bool IsSingleType() const final { return true; }
+};
+
/** A parsed sh(...) descriptor. */
class SHDescriptor final : public DescriptorImpl
{
@@ -1040,7 +1064,6 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
using namespace spanparsing;
auto expr = Expr(sp);
- bool sorted_multi = false;
if (Func("pk", expr)) {
auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
if (!pubkey) return nullptr;
@@ -1065,7 +1088,12 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
error = "Can only have combo() at top level";
return nullptr;
}
- if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && ((sorted_multi = Func("sortedmulti", expr)) || Func("multi", expr))) {
+ const bool multi = Func("multi", expr);
+ const bool sortedmulti = !multi && Func("sortedmulti", expr);
+ const bool multi_a = !(multi || sortedmulti) && Func("multi_a", expr);
+ const bool sortedmulti_a = !(multi || sortedmulti || multi_a) && Func("sortedmulti_a", expr);
+ if (((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && (multi || sortedmulti)) ||
+ (ctx == ParseScriptContext::P2TR && (multi_a || sortedmulti_a))) {
auto threshold = Expr(expr);
uint32_t thres;
std::vector<std::unique_ptr<PubkeyProvider>> providers;
@@ -1086,9 +1114,12 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
providers.emplace_back(std::move(pk));
key_exp_index++;
}
- if (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTISIG) {
+ if ((multi || sortedmulti) && (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTISIG)) {
error = strprintf("Cannot have %u keys in multisig; must have between 1 and %d keys, inclusive", providers.size(), MAX_PUBKEYS_PER_MULTISIG);
return nullptr;
+ } else if ((multi_a || sortedmulti_a) && (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTI_A)) {
+ error = strprintf("Cannot have %u keys in multi_a; must have between 1 and %d keys, inclusive", providers.size(), MAX_PUBKEYS_PER_MULTI_A);
+ return nullptr;
} else if (thres < 1) {
error = strprintf("Multisig threshold cannot be %d, must be at least 1", thres);
return nullptr;
@@ -1109,10 +1140,17 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
return nullptr;
}
}
- return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sorted_multi);
- } else if (Func("sortedmulti", expr) || Func("multi", expr)) {
+ if (multi || sortedmulti) {
+ return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sortedmulti);
+ } else {
+ return std::make_unique<MultiADescriptor>(thres, std::move(providers), sortedmulti_a);
+ }
+ } else if (multi || sortedmulti) {
error = "Can only have multi/sortedmulti at top level, in sh(), or in wsh()";
return nullptr;
+ } else if (multi_a || sortedmulti_a) {
+ error = "Can only have multi_a/sortedmulti_a inside tr()";
+ return nullptr;
}
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH) && Func("wpkh", expr)) {
auto pubkey = ParsePubkey(key_exp_index, expr, ParseScriptContext::P2WPKH, out, error);
@@ -1257,6 +1295,21 @@ std::unique_ptr<PubkeyProvider> InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseS
return key_provider;
}
+std::unique_ptr<DescriptorImpl> InferMultiA(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
+{
+ auto match = MatchMultiA(script);
+ if (!match) return {};
+ std::vector<std::unique_ptr<PubkeyProvider>> keys;
+ keys.reserve(match->second.size());
+ for (const auto keyspan : match->second) {
+ if (keyspan.size() != 32) return {};
+ auto key = InferXOnlyPubkey(XOnlyPubKey{keyspan}, ctx, provider);
+ if (!key) return {};
+ keys.push_back(std::move(key));
+ }
+ return std::make_unique<MultiADescriptor>(match->first, std::move(keys));
+}
+
std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
{
if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) {
@@ -1264,6 +1317,11 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), true);
}
+ if (ctx == ParseScriptContext::P2TR) {
+ auto ret = InferMultiA(script, ctx, provider);
+ if (ret) return ret;
+ }
+
std::vector<std::vector<unsigned char>> data;
TxoutType txntype = Solver(script, data);
diff --git a/src/script/script.h b/src/script/script.h
index 8b7a7bb7b3..a89c987306 100644
--- a/src/script/script.h
+++ b/src/script/script.h
@@ -29,6 +29,9 @@ static const int MAX_OPS_PER_SCRIPT = 201;
// Maximum number of public keys per multisig
static const int MAX_PUBKEYS_PER_MULTISIG = 20;
+/** The limit of keys in OP_CHECKSIGADD-based scripts. It is due to the stack limit in BIP342. */
+static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A = 999;
+
// Maximum script length in bytes
static const int MAX_SCRIPT_SIZE = 10000;
diff --git a/src/script/sign.cpp b/src/script/sign.cpp
index 371a937bc8..2e5c49e0b6 100644
--- a/src/script/sign.cpp
+++ b/src/script/sign.cpp
@@ -174,6 +174,29 @@ static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatu
result = Vector(std::move(sig));
return true;
}
+ return false;
+ }
+
+ // multi_a scripts (<key> OP_CHECKSIG <key> OP_CHECKSIGADD <key> OP_CHECKSIGADD <k> OP_NUMEQUAL)
+ if (auto match = MatchMultiA(script)) {
+ std::vector<std::vector<unsigned char>> sigs;
+ int good_sigs = 0;
+ for (size_t i = 0; i < match->second.size(); ++i) {
+ XOnlyPubKey pubkey{*(match->second.rbegin() + i)};
+ std::vector<unsigned char> sig;
+ bool good_sig = CreateTaprootScriptSig(creator, sigdata, provider, sig, pubkey, leaf_hash, sigversion);
+ if (good_sig && good_sigs < match->first) {
+ ++good_sigs;
+ sigs.push_back(std::move(sig));
+ } else {
+ sigs.emplace_back();
+ }
+ }
+ if (good_sigs == match->first) {
+ result = std::move(sigs);
+ return true;
+ }
+ return false;
}
return false;
diff --git a/src/script/standard.cpp b/src/script/standard.cpp
index 5fb98cc307..b77c78769f 100644
--- a/src/script/standard.cpp
+++ b/src/script/standard.cpp
@@ -96,51 +96,83 @@ static constexpr bool IsPushdataOp(opcodetype opcode)
return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
}
-static constexpr bool IsValidMultisigKeyCount(int n_keys)
-{
- return n_keys > 0 && n_keys <= MAX_PUBKEYS_PER_MULTISIG;
-}
-
-static bool GetMultisigKeyCount(opcodetype opcode, valtype data, int& count)
+/** Retrieve a minimally-encoded number in range [min,max] from an (opcode, data) pair,
+ * whether it's OP_n or through a push. */
+static std::optional<int> GetScriptNumber(opcodetype opcode, valtype data, int min, int max)
{
+ int count;
if (IsSmallInteger(opcode)) {
count = CScript::DecodeOP_N(opcode);
- return IsValidMultisigKeyCount(count);
- }
-
- if (IsPushdataOp(opcode)) {
- if (!CheckMinimalPush(data, opcode)) return false;
+ } else if (IsPushdataOp(opcode)) {
+ if (!CheckMinimalPush(data, opcode)) return {};
try {
count = CScriptNum(data, /* fRequireMinimal = */ true).getint();
- return IsValidMultisigKeyCount(count);
} catch (const scriptnum_error&) {
- return false;
+ return {};
}
+ } else {
+ return {};
}
-
- return false;
+ if (count < min || count > max) return {};
+ return count;
}
static bool MatchMultisig(const CScript& script, int& required_sigs, std::vector<valtype>& pubkeys)
{
opcodetype opcode;
valtype data;
- int num_keys;
CScript::const_iterator it = script.begin();
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
- if (!script.GetOp(it, opcode, data) || !GetMultisigKeyCount(opcode, data, required_sigs)) return false;
+ if (!script.GetOp(it, opcode, data)) return false;
+ auto req_sigs = GetScriptNumber(opcode, data, 1, MAX_PUBKEYS_PER_MULTISIG);
+ if (!req_sigs) return false;
+ required_sigs = *req_sigs;
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
pubkeys.emplace_back(std::move(data));
}
- if (!GetMultisigKeyCount(opcode, data, num_keys)) return false;
-
- if (pubkeys.size() != static_cast<unsigned long>(num_keys) || num_keys < required_sigs) return false;
+ auto num_keys = GetScriptNumber(opcode, data, required_sigs, MAX_PUBKEYS_PER_MULTISIG);
+ if (!num_keys) return false;
+ if (pubkeys.size() != static_cast<unsigned long>(*num_keys)) return false;
return (it + 1 == script.end());
}
+std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script)
+{
+ std::vector<Span<const unsigned char>> keyspans;
+
+ // Redundant, but very fast and selective test.
+ if (script.size() == 0 || script[0] != 32 || script.back() != OP_NUMEQUAL) return {};
+
+ // Parse keys
+ auto it = script.begin();
+ while (script.end() - it >= 34) {
+ if (*it != 32) return {};
+ ++it;
+ keyspans.emplace_back(&*it, 32);
+ it += 32;
+ if (*it != (keyspans.size() == 1 ? OP_CHECKSIG : OP_CHECKSIGADD)) return {};
+ ++it;
+ }
+ if (keyspans.size() == 0 || keyspans.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
+
+ // Parse threshold.
+ opcodetype opcode;
+ std::vector<unsigned char> data;
+ if (!script.GetOp(it, opcode, data)) return {};
+ if (it == script.end()) return {};
+ if (*it != OP_NUMEQUAL) return {};
+ ++it;
+ if (it != script.end()) return {};
+ auto threshold = GetScriptNumber(opcode, data, 1, (int)keyspans.size());
+ if (!threshold) return {};
+
+ // Construct result.
+ return std::pair{*threshold, std::move(keyspans)};
+}
+
TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
{
vSolutionsRet.clear();
@@ -366,13 +398,7 @@ void TaprootSpendData::Merge(TaprootSpendData other)
merkle_root = other.merkle_root;
}
for (auto& [key, control_blocks] : other.scripts) {
- // Once P0083R3 is supported by all our targeted platforms,
- // this loop body can be replaced with:
- // scripts[key].merge(std::move(control_blocks));
- auto& target = scripts[key];
- for (auto& control_block: control_blocks) {
- target.insert(std::move(control_block));
- }
+ scripts[key].merge(std::move(control_blocks));
}
}
diff --git a/src/script/standard.h b/src/script/standard.h
index eb50421768..75bfe2db38 100644
--- a/src/script/standard.h
+++ b/src/script/standard.h
@@ -191,6 +191,10 @@ CScript GetScriptForDestination(const CTxDestination& dest);
/** Generate a P2PK script for the given pubkey. */
CScript GetScriptForRawPubKey(const CPubKey& pubkey);
+/** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise.
+ * The keyspans refer to bytes in the passed script. */
+std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
+
/** Generate a multisig script. */
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
diff --git a/src/test/coinstatsindex_tests.cpp b/src/test/coinstatsindex_tests.cpp
index 92de4ec7ba..5b73481bc1 100644
--- a/src/test/coinstatsindex_tests.cpp
+++ b/src/test/coinstatsindex_tests.cpp
@@ -2,8 +2,10 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <chainparams.h>
#include <index/coinstatsindex.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
#include <util/time.h>
#include <validation.h>
@@ -16,6 +18,17 @@ using node::CoinStatsHashType;
BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
+static void IndexWaitSynced(BaseIndex& index)
+{
+ // Allow the CoinStatsIndex to catch up with the block index that is syncing
+ // in a background thread.
+ const auto timeout = GetTime<std::chrono::seconds>() + 120s;
+ while (!index.BlockUntilSyncedToCurrentChain()) {
+ BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
+ UninterruptibleSleep(100ms);
+ }
+}
+
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
{
CoinStatsIndex coin_stats_index{1 << 20, true};
@@ -36,13 +49,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
BOOST_REQUIRE(coin_stats_index.Start(m_node.chainman->ActiveChainstate()));
- // Allow the CoinStatsIndex to catch up with the block index that is syncing
- // in a background thread.
- const auto timeout = GetTime<std::chrono::seconds>() + 120s;
- while (!coin_stats_index.BlockUntilSyncedToCurrentChain()) {
- BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
- UninterruptibleSleep(100ms);
- }
+ IndexWaitSynced(coin_stats_index);
// Check that CoinStatsIndex works for genesis block.
const CBlockIndex* genesis_block_index;
@@ -78,4 +85,44 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
// Rest of shutdown sequence and destructors happen in ~TestingSetup()
}
+// Test shutdown between BlockConnected and ChainStateFlushed notifications,
+// make sure index is not corrupted and is able to reload.
+BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
+{
+ CChainState& chainstate = Assert(m_node.chainman)->ActiveChainstate();
+ const CChainParams& params = Params();
+ {
+ CoinStatsIndex index{1 << 20};
+ BOOST_REQUIRE(index.Start(chainstate));
+ IndexWaitSynced(index);
+ std::shared_ptr<const CBlock> new_block;
+ CBlockIndex* new_block_index = nullptr;
+ {
+ const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
+ const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
+
+ new_block = std::make_shared<CBlock>(block);
+
+ LOCK(cs_main);
+ BlockValidationState state;
+ BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
+ BOOST_CHECK(chainstate.AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr));
+ CCoinsViewCache view(&chainstate.CoinsTip());
+ BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
+ }
+ // Send block connected notification, then stop the index without
+ // sending a chainstate flushed notification. Prior to #24138, this
+ // would cause the index to be corrupted and fail to reload.
+ ValidationInterfaceTest::BlockConnected(index, new_block, new_block_index);
+ index.Stop();
+ }
+
+ {
+ CoinStatsIndex index{1 << 20};
+ // Make sure the index can be loaded.
+ BOOST_REQUIRE(index.Start(chainstate));
+ index.Stop();
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/fs_tests.cpp b/src/test/fs_tests.cpp
index 5875f0218f..d2554483d4 100644
--- a/src/test/fs_tests.cpp
+++ b/src/test/fs_tests.cpp
@@ -46,8 +46,8 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
{
fs::path tmpfolder = m_args.GetDataDirBase();
// tmpfile1 should be the same as tmpfile2
- fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
- fs::path tmpfile2 = tmpfolder / "fs_tests_₿_🏃";
+ fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
+ fs::path tmpfile2 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
{
std::ofstream file{tmpfile1};
file << "bitcoin";
@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
}
{
// Join an absolute path and a relative path.
- fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
+ fs::path p = fsbridge::AbsPathJoin(tmpfolder, fs::u8path("fs_tests_₿_🏃"));
BOOST_CHECK(p.is_absolute());
BOOST_CHECK_EQUAL(tmpfile1, p);
}
diff --git a/src/test/fuzz/netaddress.cpp b/src/test/fuzz/netaddress.cpp
index 6cb81901cb..35e6688c61 100644
--- a/src/test/fuzz/netaddress.cpp
+++ b/src/test/fuzz/netaddress.cpp
@@ -16,7 +16,6 @@ FUZZ_TARGET(netaddress)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider);
- (void)net_addr.GetHash();
(void)net_addr.GetNetClass();
if (net_addr.GetNetwork() == Network::NET_IPV4) {
assert(net_addr.IsIPv4());
@@ -84,6 +83,8 @@ FUZZ_TARGET(netaddress)
(void)service.ToString();
(void)service.ToStringIPPort();
(void)service.ToStringPort();
+ (void)CServiceHash()(service);
+ (void)CServiceHash(0, 0)(service);
const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
(void)net_addr.GetReachabilityFrom(&other_net_addr);
diff --git a/src/test/fuzz/script_format.cpp b/src/test/fuzz/script_format.cpp
index 2fa893f812..241bdfe666 100644
--- a/src/test/fuzz/script_format.cpp
+++ b/src/test/fuzz/script_format.cpp
@@ -3,7 +3,9 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
+#include <consensus/consensus.h>
#include <core_io.h>
+#include <policy/policy.h>
#include <script/script.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
@@ -19,6 +21,9 @@ FUZZ_TARGET_INIT(script_format, initialize_script_format)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const CScript script{ConsumeScript(fuzzed_data_provider)};
+ if (script.size() > MAX_STANDARD_TX_WEIGHT / WITNESS_SCALE_FACTOR) {
+ return;
+ }
(void)FormatScript(script);
(void)ScriptToAsmStr(script, /*fAttemptSighashDecode=*/fuzzed_data_provider.ConsumeBool());
diff --git a/src/test/getarg_tests.cpp b/src/test/getarg_tests.cpp
index 597d774673..c877105fe7 100644
--- a/src/test/getarg_tests.cpp
+++ b/src/test/getarg_tests.cpp
@@ -3,6 +3,8 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
+#include <univalue.h>
+#include <util/settings.h>
#include <util/strencodings.h>
#include <util/system.h>
@@ -14,22 +16,13 @@
#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
-namespace getarg_tests{
- class LocalTestingSetup : BasicTestingSetup {
- protected:
- void SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args);
- void ResetArgs(const std::string& strArg);
- ArgsManager m_local_args;
- };
-}
+BOOST_FIXTURE_TEST_SUITE(getarg_tests, BasicTestingSetup)
-BOOST_FIXTURE_TEST_SUITE(getarg_tests, LocalTestingSetup)
-
-void LocalTestingSetup :: ResetArgs(const std::string& strArg)
+void ResetArgs(ArgsManager& local_args, const std::string& strArg)
{
std::vector<std::string> vecArg;
if (strArg.size())
- boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
+ boost::split(vecArg, strArg, IsSpace, boost::token_compress_on);
// Insert dummy executable name:
vecArg.insert(vecArg.begin(), "testbitcoin");
@@ -40,264 +33,403 @@ void LocalTestingSetup :: ResetArgs(const std::string& strArg)
vecChar.push_back(s.c_str());
std::string error;
- BOOST_CHECK(m_local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
+ BOOST_CHECK(local_args.ParseParameters(vecChar.size(), vecChar.data(), error));
}
-void LocalTestingSetup :: SetupArgs(const std::vector<std::pair<std::string, unsigned int>>& args)
+void SetupArgs(ArgsManager& local_args, const std::vector<std::pair<std::string, unsigned int>>& args)
{
- m_local_args.ClearArgs();
for (const auto& arg : args) {
- m_local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
+ local_args.AddArg(arg.first, "", arg.second, OptionsCategory::OPTIONS);
}
}
+// Test behavior of GetArg functions when string, integer, and boolean types
+// are specified in the settings.json file. GetArg functions are convenience
+// functions. The GetSetting method can always be used instead of GetArg
+// methods to retrieve original values, and there's not always an objective
+// answer to what GetArg behavior is best in every case. This test makes sure
+// there's test coverage for whatever the current behavior is, so it's not
+// broken or changed unintentionally.
+BOOST_AUTO_TEST_CASE(setting_args)
+{
+ ArgsManager args;
+ SetupArgs(args, {{"-foo", ArgsManager::ALLOW_ANY}});
+
+ auto set_foo = [&](const util::SettingsValue& value) {
+ args.LockSettings([&](util::Settings& settings) {
+ settings.rw_settings["foo"] = value;
+ });
+ };
+
+ set_foo("str");
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"str\"");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "str");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+
+ set_foo("99");
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"99\"");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+
+ set_foo("3.25");
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"3.25\"");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 3);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+
+ set_foo("0");
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"0\"");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+
+ set_foo("");
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "\"\"");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+
+ set_foo(99);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
+
+ set_foo(3.25);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25");
+ BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
+
+ set_foo(0);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
+
+ set_foo(true);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "true");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "1");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 1);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), true);
+
+ set_foo(false);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "false");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), false);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+
+ set_foo(UniValue::VOBJ);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "{}");
+ BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
+
+ set_foo(UniValue::VARR);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "[]");
+ BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error);
+ BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error);
+
+ set_foo(UniValue::VNULL);
+ BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "null");
+ BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "default");
+ BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 100);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", true), true);
+ BOOST_CHECK_EQUAL(args.GetBoolArg("foo", false), false);
+}
+
BOOST_AUTO_TEST_CASE(boolarg)
{
+ ArgsManager local_args;
+
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
- SetupArgs({foo});
- ResetArgs("-foo");
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
+ SetupArgs(local_args, {foo});
+ ResetArgs(local_args, "-foo");
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(!m_local_args.GetBoolArg("-fo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-fo", true));
+ BOOST_CHECK(!local_args.GetBoolArg("-fo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-fo", true));
- BOOST_CHECK(!m_local_args.GetBoolArg("-fooo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-fooo", true));
+ BOOST_CHECK(!local_args.GetBoolArg("-fooo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-fooo", true));
- ResetArgs("-foo=0");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-foo=0");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
- ResetArgs("-foo=1");
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-foo=1");
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
// New 0.6 feature: auto-map -nosomething to !-something:
- ResetArgs("-nofoo");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-nofoo");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
- ResetArgs("-nofoo=1");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-nofoo=1");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
- ResetArgs("-foo -nofoo"); // -nofoo should win
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-foo -nofoo"); // -nofoo should win
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
- ResetArgs("-foo=1 -nofoo=1"); // -nofoo should win
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-foo=1 -nofoo=1"); // -nofoo should win
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
- ResetArgs("-foo=0 -nofoo=0"); // -nofoo=0 should win
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "-foo=0 -nofoo=0"); // -nofoo=0 should win
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
// New 0.6 feature: treat -- same as -:
- ResetArgs("--foo=1");
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
-
- ResetArgs("--nofoo=1");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "--foo=1");
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ ResetArgs(local_args, "--nofoo=1");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
}
BOOST_AUTO_TEST_CASE(stringarg)
{
+ ArgsManager local_args;
+
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
- SetupArgs({foo, bar});
- ResetArgs("");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
-
- ResetArgs("-foo -bar");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
-
- ResetArgs("-foo=");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "");
-
- ResetArgs("-foo=11");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "11");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "11");
-
- ResetArgs("-foo=eleven");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "eleven");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", "eleven"), "eleven");
-
+ SetupArgs(local_args, {foo, bar});
+ ResetArgs(local_args, "");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
+
+ ResetArgs(local_args, "-foo -bar");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
+
+ ResetArgs(local_args, "-foo=");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "");
+
+ ResetArgs(local_args, "-foo=11");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "11");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "11");
+
+ ResetArgs(local_args, "-foo=eleven");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "eleven");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", "eleven"), "eleven");
}
BOOST_AUTO_TEST_CASE(intarg)
{
+ ArgsManager local_args;
+
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
- SetupArgs({foo, bar});
- ResetArgs("");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 11);
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 0);
+ SetupArgs(local_args, {foo, bar});
+ ResetArgs(local_args, "");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 11);
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 0);
- ResetArgs("-foo -bar");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 11), 0);
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
+ ResetArgs(local_args, "-foo -bar");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 11), 0);
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
// Check under-/overflow behavior.
- ResetArgs("-foo=-9223372036854775809 -bar=9223372036854775808");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
+ ResetArgs(local_args, "-foo=-9223372036854775809 -bar=9223372036854775808");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), std::numeric_limits<int64_t>::min());
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), std::numeric_limits<int64_t>::max());
- ResetArgs("-foo=11 -bar=12");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 0), 11);
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 12);
+ ResetArgs(local_args, "-foo=11 -bar=12");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 0), 11);
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 12);
- ResetArgs("-foo=NaN -bar=NotANumber");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-foo", 1), 0);
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
+ ResetArgs(local_args, "-foo=NaN -bar=NotANumber");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-foo", 1), 0);
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 11), 0);
}
BOOST_AUTO_TEST_CASE(patharg)
{
+ ArgsManager local_args;
+
const auto dir = std::make_pair("-dir", ArgsManager::ALLOW_ANY);
- SetupArgs({dir});
- ResetArgs("");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), fs::path{});
+ SetupArgs(local_args, {dir});
+ ResetArgs(local_args, "");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), fs::path{});
const fs::path root_path{"/"};
- ResetArgs("-dir=/");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
+ ResetArgs(local_args, "-dir=/");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
- ResetArgs("-dir=/.");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
+ ResetArgs(local_args, "-dir=/.");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
- ResetArgs("-dir=/./");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
+ ResetArgs(local_args, "-dir=/./");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
- ResetArgs("-dir=/.//");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), root_path);
+ ResetArgs(local_args, "-dir=/.//");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), root_path);
#ifdef WIN32
const fs::path win_root_path{"C:\\"};
- ResetArgs("-dir=C:\\");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:\\");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
- ResetArgs("-dir=C:/");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:/");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
- ResetArgs("-dir=C:\\\\");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:\\\\");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
- ResetArgs("-dir=C:\\.");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:\\.");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
- ResetArgs("-dir=C:\\.\\");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:\\.\\");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
- ResetArgs("-dir=C:\\.\\\\");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), win_root_path);
+ ResetArgs(local_args, "-dir=C:\\.\\\\");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), win_root_path);
#endif
const fs::path absolute_path{"/home/user/.bitcoin"};
- ResetArgs("-dir=/home/user/.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/root/../home/user/.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/root/../home/user/.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/./user/.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/./user/.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/user/.bitcoin/");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin/");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/user/.bitcoin//");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin//");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/user/.bitcoin/.");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin/.");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/user/.bitcoin/./");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin/./");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
- ResetArgs("-dir=/home/user/.bitcoin/.//");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), absolute_path);
+ ResetArgs(local_args, "-dir=/home/user/.bitcoin/.//");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), absolute_path);
const fs::path relative_path{"user/.bitcoin"};
- ResetArgs("-dir=user/.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=somewhere/../user/.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/./.bitcoin");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/.bitcoin/");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/.bitcoin//");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/.bitcoin/.");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/.bitcoin/./");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
-
- ResetArgs("-dir=user/.bitcoin/.//");
- BOOST_CHECK_EQUAL(m_local_args.GetPathArg("-dir"), relative_path);
+ ResetArgs(local_args, "-dir=user/.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=somewhere/../user/.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/./.bitcoin");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/.bitcoin/");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/.bitcoin//");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/.bitcoin/.");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/.bitcoin/./");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ ResetArgs(local_args, "-dir=user/.bitcoin/.//");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
+
+ // Check negated and default argument handling. Specifying an empty argument
+ // is the same as not specifying the argument. This is convenient for
+ // scripting so later command line arguments can override earlier command
+ // line arguments or bitcoin.conf values. Currently the -dir= case cannot be
+ // distinguished from -dir case with no assignment, but #16545 would add the
+ // ability to distinguish these in the future (and treat the no-assign case
+ // like an imperative command or an error).
+ ResetArgs(local_args, "");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ ResetArgs(local_args, "-dir=override");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
+ ResetArgs(local_args, "-dir=");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ ResetArgs(local_args, "-dir");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
+ ResetArgs(local_args, "-nodir");
+ BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
}
BOOST_AUTO_TEST_CASE(doubledash)
{
+ ArgsManager local_args;
+
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
- SetupArgs({foo, bar});
- ResetArgs("--foo");
- BOOST_CHECK_EQUAL(m_local_args.GetBoolArg("-foo", false), true);
+ SetupArgs(local_args, {foo, bar});
+ ResetArgs(local_args, "--foo");
+ BOOST_CHECK_EQUAL(local_args.GetBoolArg("-foo", false), true);
- ResetArgs("--foo=verbose --bar=1");
- BOOST_CHECK_EQUAL(m_local_args.GetArg("-foo", ""), "verbose");
- BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 0), 1);
+ ResetArgs(local_args, "--foo=verbose --bar=1");
+ BOOST_CHECK_EQUAL(local_args.GetArg("-foo", ""), "verbose");
+ BOOST_CHECK_EQUAL(local_args.GetIntArg("-bar", 0), 1);
}
BOOST_AUTO_TEST_CASE(boolargno)
{
+ ArgsManager local_args;
+
const auto foo = std::make_pair("-foo", ArgsManager::ALLOW_ANY);
const auto bar = std::make_pair("-bar", ArgsManager::ALLOW_ANY);
- SetupArgs({foo, bar});
- ResetArgs("-nofoo");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
-
- ResetArgs("-nofoo=1");
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
-
- ResetArgs("-nofoo=0");
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
-
- ResetArgs("-foo --nofoo"); // --nofoo should win
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(!m_local_args.GetBoolArg("-foo", false));
-
- ResetArgs("-nofoo -foo"); // foo always wins:
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", true));
- BOOST_CHECK(m_local_args.GetBoolArg("-foo", false));
+ SetupArgs(local_args, {foo, bar});
+ ResetArgs(local_args, "-nofoo");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+
+ ResetArgs(local_args, "-nofoo=1");
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+
+ ResetArgs(local_args, "-nofoo=0");
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
+
+ ResetArgs(local_args, "-foo --nofoo"); // --nofoo should win
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", true));
+ BOOST_CHECK(!local_args.GetBoolArg("-foo", false));
+
+ ResetArgs(local_args, "-nofoo -foo"); // foo always wins:
+ BOOST_CHECK(local_args.GetBoolArg("-foo", true));
+ BOOST_CHECK(local_args.GetBoolArg("-foo", false));
}
BOOST_AUTO_TEST_CASE(logargs)
{
+ ArgsManager local_args;
+
const auto okaylog_bool = std::make_pair("-okaylog-bool", ArgsManager::ALLOW_ANY);
const auto okaylog_negbool = std::make_pair("-okaylog-negbool", ArgsManager::ALLOW_ANY);
const auto okaylog = std::make_pair("-okaylog", ArgsManager::ALLOW_ANY);
const auto dontlog = std::make_pair("-dontlog", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE);
- SetupArgs({okaylog_bool, okaylog_negbool, okaylog, dontlog});
- ResetArgs("-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
+ SetupArgs(local_args, {okaylog_bool, okaylog_negbool, okaylog, dontlog});
+ ResetArgs(local_args, "-okaylog-bool -nookaylog-negbool -okaylog=public -dontlog=private");
// Everything logged to debug.log will also append to str
std::string str;
@@ -307,7 +439,7 @@ BOOST_AUTO_TEST_CASE(logargs)
});
// Log the arguments
- m_local_args.LogArgs();
+ local_args.LogArgs();
LogInstance().DeleteCallback(print_connection);
// Check that what should appear does, and what shouldn't doesn't.
diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp
index c453dae701..f6c1d1efad 100644
--- a/src/test/miner_tests.cpp
+++ b/src/test/miner_tests.cpp
@@ -30,10 +30,10 @@ using node::CBlockTemplate;
namespace miner_tests {
struct MinerTestingSetup : public TestingSetup {
void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
- bool TestSequenceLocks(const CTransaction& tx, int flags) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
+ bool TestSequenceLocks(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
{
CCoinsViewMemPool view_mempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
- return CheckSequenceLocks(m_node.chainman->ActiveChain().Tip(), view_mempool, tx, flags);
+ return CheckSequenceLocksAtTip(m_node.chainman->ActiveChain().Tip(), view_mempool, tx);
}
BlockAssembler AssemblerForTest(const CChainParams& params);
};
@@ -410,7 +410,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
// non-final txs in mempool
SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1);
- int flags = LOCKTIME_VERIFY_SEQUENCE|LOCKTIME_MEDIAN_TIME_PAST;
+ const int flags{LOCKTIME_VERIFY_SEQUENCE | LOCKTIME_MEDIAN_TIME_PAST};
// height map
std::vector<int> prevheights;
@@ -429,8 +429,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.nLockTime = 0;
hash = tx.GetHash();
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
- BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime passes
- BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
+ BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
+ BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
{
CBlockIndex* active_chain_tip = m_node.chainman->ActiveChain().Tip();
@@ -443,8 +443,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
prevheights[0] = baseheight + 2;
hash = tx.GetHash();
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
- BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime passes
- BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
+ BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
+ BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
m_node.chainman->ActiveChain().Tip()->GetAncestor(m_node.chainman->ActiveChain().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
@@ -464,8 +464,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
hash = tx.GetHash();
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
- BOOST_CHECK(!CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime fails
- BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
+ BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
+ BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
// absolute time locked
@@ -475,8 +475,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
prevheights[0] = baseheight + 4;
hash = tx.GetHash();
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
- BOOST_CHECK(!CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime fails
- BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
+ BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
+ BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
// mempool-dependent transactions (not added)
@@ -484,14 +484,14 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
tx.nLockTime = 0;
tx.vin[0].nSequence = 0;
- BOOST_CHECK(CheckFinalTx(m_node.chainman->ActiveChain().Tip(), CTransaction(tx), flags)); // Locktime passes
- BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
+ BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
+ BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
tx.vin[0].nSequence = 1;
- BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
+ BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG;
- BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
+ BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
- BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
+ BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
diff --git a/src/test/net_peer_eviction_tests.cpp b/src/test/net_peer_eviction_tests.cpp
index 78ad24a408..6ec3fb0c6b 100644
--- a/src/test/net_peer_eviction_tests.cpp
+++ b/src/test/net_peer_eviction_tests.cpp
@@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
// Test protection of onion, localhost, and I2P peers...
// Expect 1/4 onion peers to be protected from eviction,
- // if no localhost or I2P peers.
+ // if no localhost, I2P, or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_is_local = false;
@@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
random_context));
// Expect 1/4 onion peers and 1/4 of the other peers to be protected,
- // sorted by longest uptime (lowest m_connected), if no localhost or I2P peers.
+ // sorted by longest uptime (lowest m_connected), if no localhost, I2P or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_connected = std::chrono::seconds{c.id};
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
random_context));
// Expect 1/4 localhost peers to be protected from eviction,
- // if no onion or I2P peers.
+ // if no onion, I2P, or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_is_local = (c.id == 1 || c.id == 9 || c.id == 11);
@@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
random_context));
// Expect 1/4 localhost peers and 1/4 of the other peers to be protected,
- // sorted by longest uptime (lowest m_connected), if no onion or I2P peers.
+ // sorted by longest uptime (lowest m_connected), if no onion, I2P, or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_connected = std::chrono::seconds{c.id};
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
random_context));
// Expect 1/4 I2P peers to be protected from eviction,
- // if no onion or localhost peers.
+ // if no onion, localhost, or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_is_local = false;
@@ -146,8 +146,8 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
/*unprotected_peer_ids=*/{},
random_context));
- // Expect 1/4 I2P peers and 1/4 of the other peers to be protected,
- // sorted by longest uptime (lowest m_connected), if no onion or localhost peers.
+ // Expect 1/4 I2P peers and 1/4 of the other peers to be protected, sorted
+ // by longest uptime (lowest m_connected), if no onion, localhost, or CJDNS peers.
BOOST_CHECK(IsProtected(
num_peers, [](NodeEvictionCandidate& c) {
c.m_connected = std::chrono::seconds{c.id};
@@ -158,6 +158,29 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
/*unprotected_peer_ids=*/{3, 5, 6, 7, 8, 11},
random_context));
+ // Expect 1/4 CJDNS peers to be protected from eviction,
+ // if no onion, localhost, or I2P peers.
+ BOOST_CHECK(IsProtected(
+ num_peers, [](NodeEvictionCandidate& c) {
+ c.m_is_local = false;
+ c.m_network = (c.id == 2 || c.id == 7 || c.id == 10) ? NET_CJDNS : NET_IPV4;
+ },
+ /*protected_peer_ids=*/{2, 7, 10},
+ /*unprotected_peer_ids=*/{},
+ random_context));
+
+ // Expect 1/4 CJDNS peers and 1/4 of the other peers to be protected, sorted
+ // by longest uptime (lowest m_connected), if no onion, localhost, or I2P peers.
+ BOOST_CHECK(IsProtected(
+ num_peers, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = false;
+ c.m_network = (c.id == 4 || c.id > 8) ? NET_CJDNS : NET_IPV6;
+ },
+ /*protected_peer_ids=*/{0, 1, 2, 4, 9, 10},
+ /*unprotected_peer_ids=*/{3, 5, 6, 7, 8, 11},
+ random_context));
+
// Tests with 2 networks...
// Combined test: expect having 1 localhost and 1 onion peer out of 4 to
@@ -289,16 +312,16 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
BOOST_CHECK(IsProtected(
4, [](NodeEvictionCandidate& c) {
c.m_connected = std::chrono::seconds{c.id};
- c.m_is_local = (c.id == 3);
- if (c.id == 4) {
+ c.m_is_local = (c.id == 2);
+ if (c.id == 3) {
c.m_network = NET_I2P;
- } else if (c.id == 2) {
+ } else if (c.id == 1) {
c.m_network = NET_ONION;
} else {
c.m_network = NET_IPV6;
}
},
- /*protected_peer_ids=*/{0, 4},
+ /*protected_peer_ids=*/{0, 3},
/*unprotected_peer_ids=*/{1, 2},
random_context));
@@ -416,15 +439,15 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
/*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 16, 19, 20, 21, 22, 23},
random_context));
- // Combined test: expect having 8 localhost, 4 I2P, and 3 onion peers out of
- // 24 to protect 2 of each (6 total), plus 6 others for 12/24 total, sorted
- // by longest uptime.
+ // Combined test: expect having 8 localhost, 4 CJDNS, and 3 onion peers out
+ // of 24 to protect 2 of each (6 total), plus 6 others for 12/24 total,
+ // sorted by longest uptime.
BOOST_CHECK(IsProtected(
24, [](NodeEvictionCandidate& c) {
c.m_connected = std::chrono::seconds{c.id};
c.m_is_local = (c.id > 15);
if (c.id > 10 && c.id < 15) {
- c.m_network = NET_I2P;
+ c.m_network = NET_CJDNS;
} else if (c.id > 6 && c.id < 10) {
c.m_network = NET_ONION;
} else {
@@ -434,6 +457,116 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 7, 8, 11, 12, 16, 17},
/*unprotected_peer_ids=*/{6, 9, 10, 13, 14, 15, 18, 19, 20, 21, 22, 23},
random_context));
+
+ // Tests with 4 networks...
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 5 to protect 1 CJDNS, 0 I2P, 0 localhost, 0 onion and 1 other peer
+ // (2 total), sorted by longest uptime; stable sort breaks tie with array
+ // order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 5, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 3);
+ if (c.id == 4) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 1) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 2) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /* protected_peer_ids */ {0, 4},
+ /* unprotected_peer_ids */ {1, 2, 3},
+ random_context));
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 7 to protect 1 CJDNS, 0, I2P, 0 localhost, 0 onion and 2 other
+ // peers (3 total) sorted by longest uptime; stable sort breaks tie with
+ // array order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 7, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 4);
+ if (c.id == 6) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 5) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 3) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV4;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 6},
+ /*unprotected_peer_ids=*/{2, 3, 4, 5},
+ random_context));
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 8 to protect 1 CJDNS, 1 I2P, 0 localhost, 0 onion and 2 other
+ // peers (4 total) sorted by longest uptime; stable sort breaks tie with
+ // array order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 8, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 3);
+ if (c.id == 5) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 6) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 3) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 5, 6},
+ /*unprotected_peer_ids=*/{2, 3, 4, 7},
+ random_context));
+
+ // Combined test: expect having 2 CJDNS, 2 I2P, 4 localhost, and 2 onion
+ // peers out of 16 to protect 1 CJDNS, 1 I2P, 1 localhost, 1 onion (4/16
+ // total), plus 4 others for 8 total, sorted by longest uptime.
+ BOOST_CHECK(IsProtected(
+ 16, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id > 5);
+ if (c.id == 11 || c.id == 15) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 10 || c.id == 14) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 8 || c.id == 9) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV4;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 2, 3, 6, 8, 10, 11},
+ /*unprotected_peer_ids=*/{4, 5, 7, 9, 12, 13, 14, 15},
+ random_context));
+
+ // Combined test: expect having 6 CJDNS, 1 I2P, 1 localhost, and 4 onion
+ // peers out of 24 to protect 2 CJDNS, 1 I2P, 1 localhost, and 2 onions (6
+ // total), plus 6 others for 12/24 total, sorted by longest uptime.
+ BOOST_CHECK(IsProtected(
+ 24, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 13);
+ if (c.id > 17) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 17) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 12 || c.id == 14 || c.id == 15 || c.id == 16) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 12, 13, 14, 17, 18, 19},
+ /*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 15, 16, 20, 21, 22, 23},
+ random_context));
}
// Returns true if any of the node ids in node_ids are selected for eviction.
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 908b030eea..fcb1a80765 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -4,17 +4,23 @@
#include <chainparams.h>
#include <clientversion.h>
+#include <compat.h>
#include <cstdint>
#include <net.h>
+#include <net_processing.h>
#include <netaddress.h>
#include <netbase.h>
+#include <netmessagemaker.h>
#include <serialize.h>
#include <span.h>
#include <streams.h>
#include <test/util/setup_common.h>
+#include <test/util/validation.h>
+#include <timedata.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/system.h>
+#include <validation.h>
#include <version.h>
#include <boost/test/unit_test.hpp>
@@ -27,7 +33,7 @@
using namespace std::literals;
-BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)
+BOOST_FIXTURE_TEST_SUITE(net_tests, RegTestingSetup)
BOOST_AUTO_TEST_CASE(cnode_listen_port)
{
@@ -607,15 +613,15 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
// set up local addresses; all that's necessary to reproduce the bug is
// that a normal IPv4 address is among the entries, but if this address is
// !IsRoutable the undefined behavior is easier to trigger deterministically
+ in_addr raw_addr;
+ raw_addr.s_addr = htonl(0x7f000001);
+ const CNetAddr mapLocalHost_entry = CNetAddr(raw_addr);
{
LOCK(g_maplocalhost_mutex);
- in_addr ipv4AddrLocal;
- ipv4AddrLocal.s_addr = 0x0100007f;
- CNetAddr addr = CNetAddr(ipv4AddrLocal);
LocalServiceInfo lsi;
lsi.nScore = 23;
lsi.nPort = 42;
- mapLocalHost[addr] = lsi;
+ mapLocalHost[mapLocalHost_entry] = lsi;
}
// create a peer with an IPv4 address
@@ -646,8 +652,79 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
BOOST_CHECK(1);
+
+ // Cleanup, so that we don't confuse other tests.
+ {
+ LOCK(g_maplocalhost_mutex);
+ mapLocalHost.erase(mapLocalHost_entry);
+ }
}
+BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
+{
+ // Test that GetLocalAddrForPeer() properly selects the address to self-advertise:
+ //
+ // 1. GetLocalAddrForPeer() calls GetLocalAddress() which returns an address that is
+ // not routable.
+ // 2. GetLocalAddrForPeer() overrides the address with whatever the peer has told us
+ // he sees us as.
+ // 2.1. For inbound connections we must override both the address and the port.
+ // 2.2. For outbound connections we must override only the address.
+
+ // Pretend that we bound to this port.
+ const uint16_t bind_port = 20001;
+ m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
+
+ // Our address:port as seen from the peer, completely different from the above.
+ in_addr peer_us_addr;
+ peer_us_addr.s_addr = htonl(0x02030405);
+ const CAddress peer_us{CService{peer_us_addr, 20002}, NODE_NETWORK};
+
+ // Create a peer with a routable IPv4 address (outbound).
+ in_addr peer_out_in_addr;
+ peer_out_in_addr.s_addr = htonl(0x01020304);
+ CNode peer_out{/*id=*/0,
+ /*nLocalServicesIn=*/NODE_NETWORK,
+ /*sock=*/nullptr,
+ /*addrIn=*/CAddress{CService{peer_out_in_addr, 8333}, NODE_NETWORK},
+ /*nKeyedNetGroupIn=*/0,
+ /*nLocalHostNonceIn=*/0,
+ /*addrBindIn=*/CAddress{},
+ /*addrNameIn=*/std::string{},
+ /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
+ /*inbound_onion=*/false};
+ peer_out.fSuccessfullyConnected = true;
+ peer_out.SetAddrLocal(peer_us);
+
+ // Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
+ auto chosen_local_addr = GetLocalAddrForPeer(&peer_out);
+ BOOST_REQUIRE(chosen_local_addr);
+ const CService expected{peer_us_addr, bind_port};
+ BOOST_CHECK(*chosen_local_addr == expected);
+
+ // Create a peer with a routable IPv4 address (inbound).
+ in_addr peer_in_in_addr;
+ peer_in_in_addr.s_addr = htonl(0x05060708);
+ CNode peer_in{/*id=*/0,
+ /*nLocalServicesIn=*/NODE_NETWORK,
+ /*sock=*/nullptr,
+ /*addrIn=*/CAddress{CService{peer_in_in_addr, 8333}, NODE_NETWORK},
+ /*nKeyedNetGroupIn=*/0,
+ /*nLocalHostNonceIn=*/0,
+ /*addrBindIn=*/CAddress{},
+ /*addrNameIn=*/std::string{},
+ /*conn_type_in=*/ConnectionType::INBOUND,
+ /*inbound_onion=*/false};
+ peer_in.fSuccessfullyConnected = true;
+ peer_in.SetAddrLocal(peer_us);
+
+ // Without the fix peer_us:8333 is chosen instead of the proper peer_us:peer_us.GetPort().
+ chosen_local_addr = GetLocalAddrForPeer(&peer_in);
+ BOOST_REQUIRE(chosen_local_addr);
+ BOOST_CHECK(*chosen_local_addr == peer_us);
+
+ m_node.args->ForceSetArg("-bind", "");
+}
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
{
@@ -728,4 +805,108 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
BOOST_CHECK(!IsLocal(addr));
}
+BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
+{
+ // Tests the following scenario:
+ // * -bind=3.4.5.6:20001 is specified
+ // * we make an outbound connection to a peer
+ // * the peer reports he sees us as 2.3.4.5:20002 in the version message
+ // (20002 is a random port assigned by our OS for the outgoing TCP connection,
+ // we cannot accept connections to it)
+ // * we should self-advertise to that peer as 2.3.4.5:20001
+
+ // Pretend that we bound to this port.
+ const uint16_t bind_port = 20001;
+ m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
+ m_node.args->ForceSetArg("-capturemessages", "1");
+
+ // Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
+ in_addr peer_us_addr;
+ peer_us_addr.s_addr = htonl(0x02030405);
+ const CService peer_us{peer_us_addr, 20002};
+
+ // Create a peer with a routable IPv4 address.
+ in_addr peer_in_addr;
+ peer_in_addr.s_addr = htonl(0x01020304);
+ CNode peer{/*id=*/0,
+ /*nLocalServicesIn=*/NODE_NETWORK,
+ /*sock=*/nullptr,
+ /*addrIn=*/CAddress{CService{peer_in_addr, 8333}, NODE_NETWORK},
+ /*nKeyedNetGroupIn=*/0,
+ /*nLocalHostNonceIn=*/0,
+ /*addrBindIn=*/CAddress{},
+ /*addrNameIn=*/std::string{},
+ /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
+ /*inbound_onion=*/false};
+
+ const uint64_t services{NODE_NETWORK | NODE_WITNESS};
+ const int64_t time{0};
+ const CNetMsgMaker msg_maker{PROTOCOL_VERSION};
+
+ // Force CChainState::IsInitialBlockDownload() to return false.
+ // Otherwise PushAddress() isn't called by PeerManager::ProcessMessage().
+ TestChainState& chainstate =
+ *static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
+ chainstate.JumpOutOfIbd();
+
+ m_node.peerman->InitializeNode(&peer);
+
+ std::atomic<bool> interrupt_dummy{false};
+ std::chrono::microseconds time_received_dummy{0};
+
+ const auto msg_version =
+ msg_maker.Make(NetMsgType::VERSION, PROTOCOL_VERSION, services, time, services, peer_us);
+ CDataStream msg_version_stream{msg_version.data, SER_NETWORK, PROTOCOL_VERSION};
+
+ m_node.peerman->ProcessMessage(
+ peer, NetMsgType::VERSION, msg_version_stream, time_received_dummy, interrupt_dummy);
+
+ const auto msg_verack = msg_maker.Make(NetMsgType::VERACK);
+ CDataStream msg_verack_stream{msg_verack.data, SER_NETWORK, PROTOCOL_VERSION};
+
+ // Will set peer.fSuccessfullyConnected to true (necessary in SendMessages()).
+ m_node.peerman->ProcessMessage(
+ peer, NetMsgType::VERACK, msg_verack_stream, time_received_dummy, interrupt_dummy);
+
+ // Ensure that peer_us_addr:bind_port is sent to the peer.
+ const CService expected{peer_us_addr, bind_port};
+ bool sent{false};
+
+ const auto CaptureMessageOrig = CaptureMessage;
+ CaptureMessage = [&sent, &expected](const CAddress& addr,
+ const std::string& msg_type,
+ Span<const unsigned char> data,
+ bool is_incoming) -> void {
+ if (!is_incoming && msg_type == "addr") {
+ CDataStream s(data, SER_NETWORK, PROTOCOL_VERSION);
+ std::vector<CAddress> addresses;
+
+ s >> addresses;
+
+ for (const auto& addr : addresses) {
+ if (addr == expected) {
+ sent = true;
+ return;
+ }
+ }
+ }
+ };
+
+ {
+ LOCK(peer.cs_sendProcessing);
+ m_node.peerman->SendMessages(&peer);
+ }
+
+ BOOST_CHECK(sent);
+
+ CaptureMessage = CaptureMessageOrig;
+ chainstate.ResetIbd();
+ m_node.args->ForceSetArg("-capturemessages", "0");
+ m_node.args->ForceSetArg("-bind", "");
+ // PeerManager::ProcessMessage() calls AddTimeData() which changes the internal state
+ // in timedata.cpp and later confuses the test "timedata_tests/addtimedata". Thus reset
+ // that state as it was before our test was run.
+ TestOnlyResetTimeData();
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
index 6410ac565a..8e6e911ec2 100644
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -576,4 +576,24 @@ BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
BOOST_CHECK(fixture_addresses == addresses_unserialized);
}
+BOOST_AUTO_TEST_CASE(isbadport)
+{
+ BOOST_CHECK(IsBadPort(1));
+ BOOST_CHECK(IsBadPort(22));
+ BOOST_CHECK(IsBadPort(6000));
+
+ BOOST_CHECK(!IsBadPort(80));
+ BOOST_CHECK(!IsBadPort(443));
+ BOOST_CHECK(!IsBadPort(8333));
+
+ // Check all ports, there must be 80 bad ports in total.
+ size_t total_bad_ports{0};
+ for (uint16_t port = std::numeric_limits<uint16_t>::max(); port > 0; --port) {
+ if (IsBadPort(port)) {
+ ++total_bad_ports;
+ }
+ }
+ BOOST_CHECK_EQUAL(total_bad_ports, 80);
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/script_segwit_tests.cpp b/src/test/script_segwit_tests.cpp
new file mode 100644
index 0000000000..2bad59805f
--- /dev/null
+++ b/src/test/script_segwit_tests.cpp
@@ -0,0 +1,164 @@
+// Copyright (c) 2012-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 <script/script.h>
+#include <test/util/setup_common.h>
+
+#include <boost/test/unit_test.hpp>
+
+BOOST_FIXTURE_TEST_SUITE(script_segwit_tests, BasicTestingSetup)
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)
+{
+ uint256 dummy;
+ CScript p2wsh;
+ p2wsh << OP_0 << ToByteVector(dummy);
+ BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash());
+
+ std::vector<unsigned char> bytes = {OP_0, 32};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_NotOp0)
+{
+ uint256 dummy;
+ CScript notp2wsh;
+ notp2wsh << OP_1 << ToByteVector(dummy);
+ BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Size)
+{
+ uint160 dummy;
+ CScript notp2wsh;
+ notp2wsh << OP_0 << ToByteVector(dummy);
+ BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Nop)
+{
+ uint256 dummy;
+ CScript notp2wsh;
+ notp2wsh << OP_0 << OP_NOP << ToByteVector(dummy);
+ BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_EmptyScript)
+{
+ CScript notp2wsh;
+ BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
+}
+
+BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Pushdata)
+{
+ // A script is not P2WSH if OP_PUSHDATA is used to push the hash.
+ std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
+
+ bytes = {OP_0, OP_PUSHDATA2, 32, 0};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
+
+ bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
+}
+
+namespace {
+
+bool IsExpectedWitnessProgram(const CScript& script, const int expectedVersion, const std::vector<unsigned char>& expectedProgram)
+{
+ int actualVersion;
+ std::vector<unsigned char> actualProgram;
+ if (!script.IsWitnessProgram(actualVersion, actualProgram)) {
+ return false;
+ }
+ BOOST_CHECK_EQUAL(actualVersion, expectedVersion);
+ BOOST_CHECK(actualProgram == expectedProgram);
+ return true;
+}
+
+bool IsNoWitnessProgram(const CScript& script)
+{
+ int dummyVersion;
+ std::vector<unsigned char> dummyProgram;
+ return !script.IsWitnessProgram(dummyVersion, dummyProgram);
+}
+
+} // anonymous namespace
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Valid)
+{
+ // Witness programs have a minimum data push of 2 bytes.
+ std::vector<unsigned char> program = {42, 18};
+ CScript wit;
+ wit << OP_0 << program;
+ BOOST_CHECK(IsExpectedWitnessProgram(wit, 0, program));
+
+ wit.clear();
+ // Witness programs have a maximum data push of 40 bytes.
+ program.resize(40);
+ wit << OP_16 << program;
+ BOOST_CHECK(IsExpectedWitnessProgram(wit, 16, program));
+
+ program.resize(32);
+ std::vector<unsigned char> bytes = {OP_5, static_cast<unsigned char>(program.size())};
+ bytes.insert(bytes.end(), program.begin(), program.end());
+ BOOST_CHECK(IsExpectedWitnessProgram(CScript(bytes.begin(), bytes.end()), 5, program));
+}
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Version)
+{
+ std::vector<unsigned char> program(10);
+ CScript nowit;
+ nowit << OP_1NEGATE << program;
+ BOOST_CHECK(IsNoWitnessProgram(nowit));
+}
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Size)
+{
+ std::vector<unsigned char> program(1);
+ CScript nowit;
+ nowit << OP_0 << program;
+ BOOST_CHECK(IsNoWitnessProgram(nowit));
+
+ nowit.clear();
+ program.resize(41);
+ nowit << OP_0 << program;
+ BOOST_CHECK(IsNoWitnessProgram(nowit));
+}
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Nop)
+{
+ std::vector<unsigned char> program(10);
+ CScript nowit;
+ nowit << OP_0 << OP_NOP << program;
+ BOOST_CHECK(IsNoWitnessProgram(nowit));
+}
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_EmptyScript)
+{
+ CScript nowit;
+ BOOST_CHECK(IsNoWitnessProgram(nowit));
+}
+
+BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Pushdata)
+{
+ // A script is no witness program if OP_PUSHDATA is used to push the hash.
+ std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
+
+ bytes = {OP_0, OP_PUSHDATA2, 32, 0};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
+
+ bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
+ bytes.insert(bytes.end(), 32, 0);
+ BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/timedata_tests.cpp b/src/test/timedata_tests.cpp
index 1dcee23bbb..478d61d5e2 100644
--- a/src/test/timedata_tests.cpp
+++ b/src/test/timedata_tests.cpp
@@ -96,9 +96,10 @@ BOOST_AUTO_TEST_CASE(addtimedata)
// not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521
// for a more detailed explanation.
MultiAddTimeData(2, 100); // filter median is 100 now, but nTimeOffset will not change
+ // We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
- // We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
+ TestOnlyResetTimeData();
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index c968e4d124..211153f06c 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -180,10 +180,9 @@ ChainTestingSetup::~ChainTestingSetup()
m_node.banman.reset();
m_node.addrman.reset();
m_node.args = nullptr;
- UnloadBlockIndex(m_node.mempool.get(), *m_node.chainman);
+ WITH_LOCK(::cs_main, UnloadBlockIndex(m_node.mempool.get(), *m_node.chainman));
m_node.mempool.reset();
m_node.scheduler.reset();
- m_node.chainman->Reset();
m_node.chainman.reset();
}
diff --git a/src/test/util/validation.cpp b/src/test/util/validation.cpp
index 1aed492c3c..49535855f9 100644
--- a/src/test/util/validation.cpp
+++ b/src/test/util/validation.cpp
@@ -7,6 +7,7 @@
#include <util/check.h>
#include <util/time.h>
#include <validation.h>
+#include <validationinterface.h>
void TestChainState::ResetIbd()
{
@@ -20,3 +21,8 @@ void TestChainState::JumpOutOfIbd()
m_cached_finished_ibd = true;
Assert(!IsInitialBlockDownload());
}
+
+void ValidationInterfaceTest::BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
+{
+ obj.BlockConnected(block, pindex);
+}
diff --git a/src/test/util/validation.h b/src/test/util/validation.h
index b13aa0be60..b0bc717b6c 100644
--- a/src/test/util/validation.h
+++ b/src/test/util/validation.h
@@ -7,6 +7,8 @@
#include <validation.h>
+class CValidationInterface;
+
struct TestChainState : public CChainState {
/** Reset the ibd cache to its initial state */
void ResetIbd();
@@ -14,4 +16,10 @@ struct TestChainState : public CChainState {
void JumpOutOfIbd();
};
+class ValidationInterfaceTest
+{
+public:
+ static void BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex);
+};
+
#endif // BITCOIN_TEST_UTIL_VALIDATION_H
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 9f78215de2..1881573e7a 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -17,6 +17,7 @@
#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
#include <util/moneystr.h>
#include <util/overflow.h>
+#include <util/readwritefile.h>
#include <util/spanparsing.h>
#include <util/strencodings.h>
#include <util/string.h>
@@ -24,9 +25,10 @@
#include <util/vector.h>
#include <array>
-#include <optional>
+#include <fstream>
#include <limits>
#include <map>
+#include <optional>
#include <stdint.h>
#include <string.h>
#include <thread>
@@ -2592,4 +2594,49 @@ BOOST_AUTO_TEST_CASE(util_ParseByteUnits)
BOOST_CHECK(!ParseByteUnits("1x", noop));
}
+BOOST_AUTO_TEST_CASE(util_ReadBinaryFile)
+{
+ fs::path tmpfolder = m_args.GetDataDirBase();
+ fs::path tmpfile = tmpfolder / "read_binary.dat";
+ std::string expected_text;
+ for (int i = 0; i < 30; i++) {
+ expected_text += "0123456789";
+ }
+ {
+ std::ofstream file{tmpfile};
+ file << expected_text;
+ }
+ {
+ // read all contents in file
+ auto [valid, text] = ReadBinaryFile(tmpfile);
+ BOOST_CHECK(valid);
+ BOOST_CHECK_EQUAL(text, expected_text);
+ }
+ {
+ // read half contents in file
+ auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2);
+ BOOST_CHECK(valid);
+ BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2));
+ }
+ {
+ // read from non-existent file
+ fs::path invalid_file = tmpfolder / "invalid_binary.dat";
+ auto [valid, text] = ReadBinaryFile(invalid_file);
+ BOOST_CHECK(!valid);
+ BOOST_CHECK(text.empty());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(util_WriteBinaryFile)
+{
+ fs::path tmpfolder = m_args.GetDataDirBase();
+ fs::path tmpfile = tmpfolder / "write_binary.dat";
+ std::string expected_text = "bitcoin";
+ auto valid = WriteBinaryFile(tmpfile, expected_text);
+ std::string actual_text;
+ std::ifstream file{tmpfile};
+ file >> actual_text;
+ BOOST_CHECK(valid);
+ BOOST_CHECK_EQUAL(actual_text, expected_text);
+}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/validation_chainstate_tests.cpp b/src/test/validation_chainstate_tests.cpp
index 1beef5cf04..b0d7389d39 100644
--- a/src/test/validation_chainstate_tests.cpp
+++ b/src/test/validation_chainstate_tests.cpp
@@ -72,9 +72,6 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
// The view cache should be empty since we had to destruct to downsize.
BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint));
}
-
- // Avoid triggering the address sanitizer.
- WITH_LOCK(::cs_main, manager.Unload());
}
//! Test UpdateTip behavior for both active and background chainstates.
diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp
index 26392e690d..5d0ec593e3 100644
--- a/src/test/validation_chainstatemanager_tests.cpp
+++ b/src/test/validation_chainstatemanager_tests.cpp
@@ -99,8 +99,6 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
// Let scheduler events finish running to avoid accessing memory that is going to be unloaded
SyncWithValidationInterfaceQueue();
-
- WITH_LOCK(::cs_main, manager.Unload());
}
//! Test rebalancing the caches associated with each chainstate.
diff --git a/src/timedata.cpp b/src/timedata.cpp
index 541580b3ff..06659871e5 100644
--- a/src/timedata.cpp
+++ b/src/timedata.cpp
@@ -39,29 +39,31 @@ int64_t GetAdjustedTime()
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
+static std::set<CNetAddr> g_sources;
+static CMedianFilter<int64_t> g_time_offsets{BITCOIN_TIMEDATA_MAX_SAMPLES, 0};
+static bool g_warning_emitted;
+
void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
{
LOCK(g_timeoffset_mutex);
// Ignore duplicates
- static std::set<CNetAddr> setKnown;
- if (setKnown.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
+ if (g_sources.size() == BITCOIN_TIMEDATA_MAX_SAMPLES)
return;
- if (!setKnown.insert(ip).second)
+ if (!g_sources.insert(ip).second)
return;
// Add data
- static CMedianFilter<int64_t> vTimeOffsets(BITCOIN_TIMEDATA_MAX_SAMPLES, 0);
- vTimeOffsets.input(nOffsetSample);
- LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample / 60);
+ g_time_offsets.input(nOffsetSample);
+ LogPrint(BCLog::NET, "added time data, samples %d, offset %+d (%+d minutes)\n", g_time_offsets.size(), nOffsetSample, nOffsetSample / 60);
// There is a known issue here (see issue #4521):
//
- // - The structure vTimeOffsets contains up to 200 elements, after which
+ // - The structure g_time_offsets contains up to 200 elements, after which
// any new element added to it will not increase its size, replacing the
// oldest element.
//
// - The condition to update nTimeOffset includes checking whether the
- // number of elements in vTimeOffsets is odd, which will never happen after
+ // number of elements in g_time_offsets is odd, which will never happen after
// there are 200 elements.
//
// But in this case the 'bug' is protective against some attacks, and may
@@ -71,9 +73,9 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
// So we should hold off on fixing this and clean it up as part of
// a timing cleanup that strengthens it in a number of other ways.
//
- if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) {
- int64_t nMedian = vTimeOffsets.median();
- std::vector<int64_t> vSorted = vTimeOffsets.sorted();
+ if (g_time_offsets.size() >= 5 && g_time_offsets.size() % 2 == 1) {
+ int64_t nMedian = g_time_offsets.median();
+ std::vector<int64_t> vSorted = g_time_offsets.sorted();
// Only let other nodes change our time by so much
int64_t max_adjustment = std::max<int64_t>(0, gArgs.GetIntArg("-maxtimeadjustment", DEFAULT_MAX_TIME_ADJUSTMENT));
if (nMedian >= -max_adjustment && nMedian <= max_adjustment) {
@@ -81,8 +83,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
} else {
nTimeOffset = 0;
- static bool fDone;
- if (!fDone) {
+ if (!g_warning_emitted) {
// If nobody has a time different than ours but within 5 minutes of ours, give a warning
bool fMatch = false;
for (const int64_t nOffset : vSorted) {
@@ -90,7 +91,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
}
if (!fMatch) {
- fDone = true;
+ g_warning_emitted = true;
bilingual_str strMessage = strprintf(_("Please check that your computer's date and time are correct! If your clock is wrong, %s will not work properly."), PACKAGE_NAME);
SetMiscWarning(strMessage);
uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING);
@@ -108,3 +109,12 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
}
}
}
+
+void TestOnlyResetTimeData()
+{
+ LOCK(g_timeoffset_mutex);
+ nTimeOffset = 0;
+ g_sources.clear();
+ g_time_offsets = CMedianFilter<int64_t>{BITCOIN_TIMEDATA_MAX_SAMPLES, 0};
+ g_warning_emitted = false;
+}
diff --git a/src/timedata.h b/src/timedata.h
index b165ecde26..2f039d5465 100644
--- a/src/timedata.h
+++ b/src/timedata.h
@@ -75,4 +75,9 @@ int64_t GetTimeOffset();
int64_t GetAdjustedTime();
void AddTimeData(const CNetAddr& ip, int64_t nTime);
+/**
+ * Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
+ */
+void TestOnlyResetTimeData();
+
#endif // BITCOIN_TIMEDATA_H
diff --git a/src/util/readwritefile.cpp b/src/util/readwritefile.cpp
index a45c41d367..628e6a3980 100644
--- a/src/util/readwritefile.cpp
+++ b/src/util/readwritefile.cpp
@@ -18,7 +18,7 @@ std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxs
std::string retval;
char buffer[128];
do {
- const size_t n = fread(buffer, 1, sizeof(buffer), f);
+ const size_t n = fread(buffer, 1, std::min(sizeof(buffer), maxsize - retval.size()), f);
// Check for reading errors so we don't return any data if we couldn't
// read the entire file (or up to maxsize)
if (ferror(f)) {
@@ -26,7 +26,7 @@ std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxs
return std::make_pair(false,"");
}
retval.append(buffer, buffer+n);
- } while (!feof(f) && retval.size() <= maxsize);
+ } while (!feof(f) && retval.size() < maxsize);
fclose(f);
return std::make_pair(true,retval);
}
diff --git a/src/util/system.cpp b/src/util/system.cpp
index aa9122106b..8e45453d31 100644
--- a/src/util/system.cpp
+++ b/src/util/system.cpp
@@ -387,9 +387,12 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
return std::nullopt;
}
-fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
+fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
{
- auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal();
+ if (IsArgNegated(arg)) return fs::path{};
+ std::string path_str = GetArg(arg, "");
+ if (path_str.empty()) return default_value;
+ fs::path result = fs::PathFromString(path_str).lexically_normal();
// Remove trailing slash, if present.
return result.has_filename() ? result : result.parent_path();
}
@@ -516,12 +519,12 @@ bool ArgsManager::InitSettings(std::string& error)
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
{
- if (IsArgNegated("-settings")) {
+ fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME});
+ if (settings.empty()) {
return false;
}
if (filepath) {
- std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
- *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), fs::PathFromString(temp ? settings + ".tmp" : settings));
+ *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
}
return true;
}
@@ -588,7 +591,7 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
{
const util::SettingsValue value = GetSetting(strArg);
- return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str();
+ return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.isNum() ? value.getValStr() : value.get_str();
}
int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const
diff --git a/src/util/system.h b/src/util/system.h
index f193c8ac0b..a66b597d41 100644
--- a/src/util/system.h
+++ b/src/util/system.h
@@ -271,16 +271,6 @@ protected:
std::optional<const Command> GetCommand() const;
/**
- * Get a normalized path from a specified pathlike argument
- *
- * It is guaranteed that the returned path has no trailing slashes.
- *
- * @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
- * @return Normalized path which is get from a specified pathlike argument
- */
- fs::path GetPathArg(std::string pathlike_arg) const;
-
- /**
* Get blocks directory path
*
* @return Blocks path which is network specific
@@ -343,6 +333,18 @@ protected:
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
/**
+ * Return path argument or default value
+ *
+ * @param arg Argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
+ * @param default_value Optional default value to return instead of the empty path.
+ * @return normalized path if argument is set, with redundant "." and ".."
+ * path components and trailing separators removed (see patharg unit test
+ * for examples or implementation for details). If argument is empty or not
+ * set, default_value is returned unchanged.
+ */
+ fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const;
+
+ /**
* Return integer argument or default value
*
* @param strArg Argument to get (e.g. "-foo")
diff --git a/src/validation.cpp b/src/validation.cpp
index 214112e2bd..f399acb9fd 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -65,21 +65,22 @@
using node::BLOCKFILE_CHUNK_SIZE;
using node::BlockManager;
using node::BlockMap;
+using node::CBlockIndexHeightOnlyComparator;
using node::CBlockIndexWorkComparator;
using node::CCoinsStats;
using node::CoinStatsHashType;
+using node::fHavePruned;
+using node::fImporting;
+using node::fPruneMode;
+using node::fReindex;
using node::GetUTXOStats;
+using node::nPruneTarget;
using node::OpenBlockFile;
using node::ReadBlockFromDisk;
using node::SnapshotMetadata;
using node::UNDOFILE_CHUNK_SIZE;
using node::UndoReadFromDisk;
using node::UnlinkPrunedFiles;
-using node::fHavePruned;
-using node::fImporting;
-using node::fPruneMode;
-using node::fReindex;
-using node::nPruneTarget;
#define MICRO 0.000001
#define MILLI 0.001
@@ -107,24 +108,6 @@ const std::vector<std::string> CHECKLEVEL_DOC {
"each level includes the checks of the previous levels",
};
-bool CBlockIndexWorkComparator::operator()(const CBlockIndex *pa, const CBlockIndex *pb) const {
- // First sort by most total work, ...
- if (pa->nChainWork > pb->nChainWork) return false;
- if (pa->nChainWork < pb->nChainWork) return true;
-
- // ... then by earliest time received, ...
- if (pa->nSequenceId < pb->nSequenceId) return false;
- if (pa->nSequenceId > pb->nSequenceId) return true;
-
- // Use pointer address as tie breaker (should only happen with blocks
- // loaded from disk, as those all have id 0).
- if (pa < pb) return false;
- if (pa > pb) return true;
-
- // Identical blocks.
- return false;
-}
-
/**
* Mutex to guard access to validation specific variables, such as reading
* or changing the chainstate.
@@ -152,14 +135,14 @@ arith_uint256 nMinimumChainWork;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
-CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
+const CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
{
AssertLockHeld(cs_main);
// Find the latest block common to locator and chain - we expect that
// locator.vHave is sorted descending by height.
for (const uint256& hash : locator.vHave) {
- CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
+ const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
if (pindex) {
if (m_chain.Contains(pindex)) {
return pindex;
@@ -178,20 +161,12 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
std::vector<CScriptCheck>* pvChecks = nullptr)
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
-bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, int flags)
+bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx)
{
AssertLockHeld(cs_main);
assert(active_chain_tip); // TODO: Make active_chain_tip a reference
- // By convention a negative value for flags indicates that the
- // current network-enforced consensus rules should be used. In
- // a future soft-fork scenario that would mean checking which
- // rules would be enforced for the next block and setting the
- // appropriate flags. At the present time no soft-forks are
- // scheduled, so no flags are set.
- flags = std::max(flags, 0);
-
- // CheckFinalTx() uses active_chain_tip.Height()+1 to evaluate
+ // CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate
// nLockTime because when IsFinalTx() is called within
// AcceptBlock(), the height of the block *being*
// evaluated is what is used. Thus if we want to know if a
@@ -203,18 +178,15 @@ bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, i
// less than the median time of the previous block they're contained in.
// When the next block is created its previous block will be the current
// chain tip, so we use that to calculate the median time passed to
- // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
- const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
- ? active_chain_tip->GetMedianTimePast()
- : GetAdjustedTime();
+ // IsFinalTx().
+ const int64_t nBlockTime{active_chain_tip->GetMedianTimePast()};
return IsFinalTx(tx, nBlockHeight, nBlockTime);
}
-bool CheckSequenceLocks(CBlockIndex* tip,
+bool CheckSequenceLocksAtTip(CBlockIndex* tip,
const CCoinsView& coins_view,
const CTransaction& tx,
- int flags,
LockPoints* lp,
bool useExistingLockPoints)
{
@@ -222,7 +194,7 @@ bool CheckSequenceLocks(CBlockIndex* tip,
CBlockIndex index;
index.pprev = tip;
- // CheckSequenceLocks() uses active_chainstate.m_chain.Height()+1 to evaluate
+ // CheckSequenceLocksAtTip() uses active_chainstate.m_chain.Height()+1 to evaluate
// height based locks because when SequenceLocks() is called within
// ConnectBlock(), the height of the block *being*
// evaluated is what is used.
@@ -252,7 +224,7 @@ bool CheckSequenceLocks(CBlockIndex* tip,
prevheights[txinIndex] = coin.nHeight;
}
}
- lockPair = CalculateSequenceLocks(tx, flags, prevheights, index);
+ lockPair = CalculateSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, prevheights, index);
if (lp) {
lp->height = lockPair.first;
lp->time = lockPair.second;
@@ -268,7 +240,7 @@ bool CheckSequenceLocks(CBlockIndex* tip,
// lockPair from CalculateSequenceLocks against tip+1. We know
// EvaluateSequenceLocks will fail if there was a non-zero sequence
// lock on a mempool input, so we can use the return value of
- // CheckSequenceLocks to indicate the LockPoints validity
+ // CheckSequenceLocksAtTip to indicate the LockPoints validity
int maxInputHeight = 0;
for (const int height : prevheights) {
// Can ignore mempool inputs since we'll fail if they had non-zero locks
@@ -358,26 +330,26 @@ void CChainState::MaybeUpdateMempoolForReorg(
// Also updates valid entries' cached LockPoints if needed.
// If false, the tx is still valid and its lockpoints are updated.
// If true, the tx would be invalid in the next block; remove this entry and all of its descendants.
- const auto filter_final_and_mature = [this, flags=STANDARD_LOCKTIME_VERIFY_FLAGS](CTxMemPool::txiter it)
+ const auto filter_final_and_mature = [this](CTxMemPool::txiter it)
EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {
AssertLockHeld(m_mempool->cs);
AssertLockHeld(::cs_main);
const CTransaction& tx = it->GetTx();
// The transaction must be final.
- if (!CheckFinalTx(m_chain.Tip(), tx, flags)) return true;
+ if (!CheckFinalTxAtTip(m_chain.Tip(), tx)) return true;
LockPoints lp = it->GetLockPoints();
const bool validLP{TestLockPointValidity(m_chain, lp)};
CCoinsViewMemPool view_mempool(&CoinsTip(), *m_mempool);
- // CheckSequenceLocks checks if the transaction will be final in the next block to be
+ // CheckSequenceLocksAtTip checks if the transaction will be final in the next block to be
// created on top of the new chain. We use useExistingLockPoints=false so that, instead of
// using the information in lp (which might now refer to a block that no longer exists in
// the chain), it will update lp to contain LockPoints relevant to the new chain.
- if (!CheckSequenceLocks(m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
- // If CheckSequenceLocks fails, remove the tx and don't depend on the LockPoints.
+ if (!CheckSequenceLocksAtTip(m_chain.Tip(), view_mempool, tx, &lp, validLP)) {
+ // If CheckSequenceLocksAtTip fails, remove the tx and don't depend on the LockPoints.
return true;
} else if (!validLP) {
- // If CheckSequenceLocks succeeded, it also updated the LockPoints.
+ // If CheckSequenceLocksAtTip succeeded, it also updated the LockPoints.
// Now update the mempool entry lockpoints as well.
m_mempool->mapTx.modify(it, [&lp](CTxMemPoolEntry& e) { e.UpdateLockPoints(lp); });
}
@@ -528,9 +500,26 @@ public:
/* m_package_submission */ true,
};
}
- // No default ctor to avoid exposing details to clients and allowing the possibility of
+
+ private:
+ // Private ctor to avoid exposing details to clients and allowing the possibility of
// mixing up the order of the arguments. Use static functions above instead.
- ATMPArgs() = delete;
+ ATMPArgs(const CChainParams& chainparams,
+ int64_t accept_time,
+ bool bypass_limits,
+ std::vector<COutPoint>& coins_to_uncache,
+ bool test_accept,
+ bool allow_bip125_replacement,
+ bool package_submission)
+ : m_chainparams{chainparams},
+ m_accept_time{accept_time},
+ m_bypass_limits{bypass_limits},
+ m_coins_to_uncache{coins_to_uncache},
+ m_test_accept{test_accept},
+ m_allow_bip125_replacement{allow_bip125_replacement},
+ m_package_submission{package_submission}
+ {
+ }
};
// Single transaction acceptance
@@ -705,8 +694,9 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// Only accept nLockTime-using transactions that can be mined in the next
// block; we don't want our mempool filled up with transactions that can't
// be mined yet.
- if (!CheckFinalTx(m_active_chainstate.m_chain.Tip(), tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
+ if (!CheckFinalTxAtTip(m_active_chainstate.m_chain.Tip(), tx)) {
return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
+ }
if (m_pool.exists(GenTxid::Wtxid(tx.GetWitnessHash()))) {
// Exact transaction already exists in the mempool.
@@ -786,8 +776,9 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// be mined yet.
// Pass in m_view which has all of the relevant inputs cached. Note that, since m_view's
// backend was removed, it no longer pulls coins from the mempool.
- if (!CheckSequenceLocks(m_active_chainstate.m_chain.Tip(), m_view, tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
+ if (!CheckSequenceLocksAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx, &lp)) {
return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-BIP68-final");
+ }
// The mempool holds txs for the next block, so pass height+1 to CheckTxInputs
if (!Consensus::CheckTxInputs(tx, state, m_view, m_active_chainstate.m_chain.Height() + 1, ws.m_base_fees)) {
@@ -1978,7 +1969,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
// effectively caching the result of part of the verification.
BlockMap::const_iterator it = m_blockman.m_block_index.find(hashAssumeValid);
if (it != m_blockman.m_block_index.end()) {
- if (it->second->GetAncestor(pindex->nHeight) == pindex &&
+ if (it->second.GetAncestor(pindex->nHeight) == pindex &&
pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
pindexBestHeader->nChainWork >= nMinimumChainWork) {
// This block is a member of the assumed verified chain and an ancestor of the best header.
@@ -3035,8 +3026,8 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
{
LOCK(cs_main);
- for (const auto& entry : m_blockman.m_block_index) {
- CBlockIndex *candidate = entry.second;
+ for (auto& entry : m_blockman.m_block_index) {
+ CBlockIndex* candidate = &entry.second;
// We don't need to put anything in our active chain into the
// multimap, because those candidates will be found and considered
// as we disconnect.
@@ -3133,12 +3124,10 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
// it up here, this should be an essentially unobservable error.
// Loop back over all block index entries and add any missing entries
// to setBlockIndexCandidates.
- BlockMap::iterator it = m_blockman.m_block_index.begin();
- while (it != m_blockman.m_block_index.end()) {
- if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && !setBlockIndexCandidates.value_comp()(it->second, m_chain.Tip())) {
- setBlockIndexCandidates.insert(it->second);
+ for (auto& [_, block_index] : m_blockman.m_block_index) {
+ if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveTxsDownloaded() && !setBlockIndexCandidates.value_comp()(&block_index, m_chain.Tip())) {
+ setBlockIndexCandidates.insert(&block_index);
}
- it++;
}
InvalidChainFound(to_mark_failed);
@@ -3157,21 +3146,19 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
int nHeight = pindex->nHeight;
// Remove the invalidity flag from this block and all its descendants.
- BlockMap::iterator it = m_blockman.m_block_index.begin();
- while (it != m_blockman.m_block_index.end()) {
- if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
- it->second->nStatus &= ~BLOCK_FAILED_MASK;
- m_blockman.m_dirty_blockindex.insert(it->second);
- if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) {
- setBlockIndexCandidates.insert(it->second);
+ for (auto& [_, block_index] : m_blockman.m_block_index) {
+ if (!block_index.IsValid() && block_index.GetAncestor(nHeight) == pindex) {
+ block_index.nStatus &= ~BLOCK_FAILED_MASK;
+ m_blockman.m_dirty_blockindex.insert(&block_index);
+ if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
+ setBlockIndexCandidates.insert(&block_index);
}
- if (it->second == m_chainman.m_best_invalid) {
+ if (&block_index == m_chainman.m_best_invalid) {
// Reset invalid block marker if it was pointing to one of those.
m_chainman.m_best_invalid = nullptr;
}
- m_chainman.m_failed_blocks.erase(it->second);
+ m_chainman.m_failed_blocks.erase(&block_index);
}
- it++;
}
// Remove the invalidity flag from all ancestors too.
@@ -3377,7 +3364,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
// Don't accept any forks from the main chain prior to last checkpoint.
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
// BlockIndex().
- CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints());
+ const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(params.Checkpoints());
if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
LogPrintf("ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__, nHeight);
return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "bad-fork-prior-to-checkpoint");
@@ -3500,7 +3487,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
if (hash != chainparams.GetConsensus().hashGenesisBlock) {
if (miSelf != m_blockman.m_block_index.end()) {
// Block header is already known.
- CBlockIndex* pindex = miSelf->second;
+ CBlockIndex* pindex = &(miSelf->second);
if (ppindex)
*ppindex = pindex;
if (pindex->nStatus & BLOCK_FAILED_MASK) {
@@ -3522,7 +3509,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
LogPrint(BCLog::VALIDATION, "%s: %s prev block not found\n", __func__, hash.ToString());
return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
}
- pindexPrev = (*mi).second;
+ pindexPrev = &((*mi).second);
if (pindexPrev->nStatus & BLOCK_FAILED_MASK) {
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
@@ -3994,13 +3981,13 @@ bool CChainState::ReplayBlocks()
if (m_blockman.m_block_index.count(hashHeads[0]) == 0) {
return error("ReplayBlocks(): reorganization to unknown block requested");
}
- pindexNew = m_blockman.m_block_index[hashHeads[0]];
+ pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
if (m_blockman.m_block_index.count(hashHeads[1]) == 0) {
return error("ReplayBlocks(): reorganization from unknown block requested");
}
- pindexOld = m_blockman.m_block_index[hashHeads[1]];
+ pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
pindexFork = LastCommonAncestor(pindexOld, pindexNew);
assert(pindexFork != nullptr);
}
@@ -4070,7 +4057,7 @@ void CChainState::UnloadBlockIndex()
// block index state
void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
{
- LOCK(cs_main);
+ AssertLockHeld(::cs_main);
chainman.Unload();
pindexBestHeader = nullptr;
if (mempool) mempool->clear();
@@ -4087,8 +4074,74 @@ bool ChainstateManager::LoadBlockIndex()
// Load block index from databases
bool needs_init = fReindex;
if (!fReindex) {
- bool ret = m_blockman.LoadBlockIndexDB(*this);
+ bool ret = m_blockman.LoadBlockIndexDB();
if (!ret) return false;
+
+ std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
+ std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
+ CBlockIndexHeightOnlyComparator());
+
+ // Find start of assumed-valid region.
+ int first_assumed_valid_height = std::numeric_limits<int>::max();
+
+ for (const CBlockIndex* block : vSortedByHeight) {
+ if (block->IsAssumedValid()) {
+ auto chainstates = GetAll();
+
+ // If we encounter an assumed-valid block index entry, ensure that we have
+ // one chainstate that tolerates assumed-valid entries and another that does
+ // not (i.e. the background validation chainstate), since assumed-valid
+ // entries should always be pending validation by a fully-validated chainstate.
+ auto any_chain = [&](auto fnc) { return std::any_of(chainstates.cbegin(), chainstates.cend(), fnc); };
+ assert(any_chain([](auto chainstate) { return chainstate->reliesOnAssumedValid(); }));
+ assert(any_chain([](auto chainstate) { return !chainstate->reliesOnAssumedValid(); }));
+
+ first_assumed_valid_height = block->nHeight;
+ break;
+ }
+ }
+
+ for (CBlockIndex* pindex : vSortedByHeight) {
+ if (ShutdownRequested()) return false;
+ if (pindex->IsAssumedValid() ||
+ (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
+ (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))) {
+
+ // Fill each chainstate's block candidate set. Only add assumed-valid
+ // blocks to the tip candidate set if the chainstate is allowed to rely on
+ // assumed-valid blocks.
+ //
+ // If all setBlockIndexCandidates contained the assumed-valid blocks, the
+ // background chainstate's ActivateBestChain() call would add assumed-valid
+ // blocks to the chain (based on how FindMostWorkChain() works). Obviously
+ // we don't want this since the purpose of the background validation chain
+ // is to validate assued-valid blocks.
+ //
+ // Note: This is considering all blocks whose height is greater or equal to
+ // the first assumed-valid block to be assumed-valid blocks, and excluding
+ // them from the background chainstate's setBlockIndexCandidates set. This
+ // does mean that some blocks which are not technically assumed-valid
+ // (later blocks on a fork beginning before the first assumed-valid block)
+ // might not get added to the background chainstate, but this is ok,
+ // because they will still be attached to the active chainstate if they
+ // actually contain more work.
+ //
+ // Instead of this height-based approach, an earlier attempt was made at
+ // detecting "holistically" whether the block index under consideration
+ // relied on an assumed-valid ancestor, but this proved to be too slow to
+ // be practical.
+ for (CChainState* chainstate : GetAll()) {
+ if (chainstate->reliesOnAssumedValid() ||
+ pindex->nHeight < first_assumed_valid_height) {
+ chainstate->setBlockIndexCandidates.insert(pindex);
+ }
+ }
+ }
+ if (pindex->nStatus & BLOCK_FAILED_MASK && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
+ m_best_invalid = pindex;
+ }
+ }
+
needs_init = m_blockman.m_block_index.empty();
}
@@ -4190,7 +4243,7 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
}
// process in case the block isn't known yet
- CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
+ const CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
BlockValidationState state;
if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr)) {
@@ -4267,8 +4320,8 @@ void CChainState::CheckBlockIndex()
// Build forward-pointing map of the entire block tree.
std::multimap<CBlockIndex*,CBlockIndex*> forward;
- for (const std::pair<const uint256, CBlockIndex*>& entry : m_blockman.m_block_index) {
- forward.insert(std::make_pair(entry.second->pprev, entry.second));
+ for (auto& [_, block_index] : m_blockman.m_block_index) {
+ forward.emplace(block_index.pprev, &block_index);
}
assert(forward.size() == m_blockman.m_block_index.size());
@@ -5056,15 +5109,6 @@ void ChainstateManager::Unload()
m_best_invalid = nullptr;
}
-void ChainstateManager::Reset()
-{
- LOCK(::cs_main);
- m_ibd_chainstate.reset();
- m_snapshot_chainstate.reset();
- m_active_chainstate = nullptr;
- m_snapshot_validated = false;
-}
-
void ChainstateManager::MaybeRebalanceCaches()
{
AssertLockHeld(::cs_main);
diff --git a/src/validation.h b/src/validation.h
index 7766d77a88..b13e7f3d8b 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -138,7 +138,7 @@ extern CBlockIndex *pindexBestHeader;
extern const std::vector<std::string> CHECKLEVEL_DOC;
/** Unload database information */
-void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman);
+void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/** Run instances of script checking worker threads */
void StartScriptCheckWorkerThreads(int threads_num);
/** Stop all of the script checking worker threads */
@@ -273,16 +273,12 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx
const Package& txns, bool test_accept)
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
-/** Transaction validation functions */
+/* Transaction policy functions */
/**
* Check if transaction will be final in the next block to be created.
- *
- * Calls IsFinalTx() with current block height and appropriate block time.
- *
- * See consensus/consensus.h for flag definitions.
*/
-bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, int flags = -1) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/**
* Check if transaction will be BIP68 final in the next block to be created on top of tip.
@@ -299,14 +295,11 @@ bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, i
* Optionally stores in LockPoints the resulting height and time calculated and the hash
* of the block needed for calculation or skips the calculation and uses the LockPoints
* passed in for evaluation.
- * The LockPoints should not be considered valid if CheckSequenceLocks returns false.
- *
- * See consensus/consensus.h for flag definitions.
+ * The LockPoints should not be considered valid if CheckSequenceLocksAtTip returns false.
*/
-bool CheckSequenceLocks(CBlockIndex* tip,
+bool CheckSequenceLocksAtTip(CBlockIndex* tip,
const CCoinsView& coins_view,
const CTransaction& tx,
- int flags,
LockPoints* lp = nullptr,
bool useExistingLockPoints = false);
@@ -693,7 +686,7 @@ public:
bool IsInitialBlockDownload() const;
/** Find the last common block of this chain and a locator. */
- CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+ const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/**
* Make various assertions about the state of the block index.
@@ -836,7 +829,7 @@ private:
bool m_snapshot_validated{false};
CBlockIndex* m_best_invalid;
- friend bool node::BlockManager::LoadBlockIndex(const Consensus::Params&, ChainstateManager&);
+ friend bool node::BlockManager::LoadBlockIndex(const Consensus::Params&);
//! Internal helper for ActivateSnapshot().
[[nodiscard]] bool PopulateAndValidateSnapshot(
@@ -991,17 +984,13 @@ public:
//! Unload block index and chain data before shutdown.
void Unload() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
- //! Clear (deconstruct) chainstate data.
- void Reset();
-
//! Check to see if caches are out of balance and if so, call
//! ResizeCoinsCaches() as needed.
void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
~ChainstateManager() {
LOCK(::cs_main);
- UnloadBlockIndex(/* mempool */ nullptr, *this);
- Reset();
+ UnloadBlockIndex(/*mempool=*/nullptr, *this);
}
};
diff --git a/src/validationinterface.h b/src/validationinterface.h
index 7c3ce00fbc..ac62f8b467 100644
--- a/src/validationinterface.h
+++ b/src/validationinterface.h
@@ -174,6 +174,7 @@ protected:
* has been received and connected to the headers tree, though not validated yet */
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
friend class CMainSignals;
+ friend class ValidationInterfaceTest;
};
struct MainSignalsInstance;
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp
index 23faad027f..513572da45 100644
--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -163,6 +163,8 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
result.AddInput(utxo_pool.at(i));
}
}
+ result.ComputeAndSetWaste(CAmount{0});
+ assert(best_waste == result.GetWaste());
return result;
}
diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp
index 433b5a1815..072879a42a 100644
--- a/src/wallet/rpc/spend.cpp
+++ b/src/wallet/rpc/spend.cpp
@@ -659,7 +659,7 @@ RPCHelpMan fundrawtransaction()
{"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include inputs that are not safe to spend (unconfirmed transactions from outside keys and unconfirmed replacement transactions).\n"
"Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
"If that happens, you will need to fund the transaction with different inputs and republish it."},
- {"changeAddress", RPCArg::Type::STR, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
+ {"changeAddress", RPCArg::Type::STR, RPCArg::DefaultHint{"automatic"}, "The bitcoin address to receive the change"},
{"changePosition", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
{"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if changeAddress is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
{"includeWatching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch only.\n"
@@ -1056,7 +1056,7 @@ RPCHelpMan send()
"Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
"If that happens, you will need to fund the transaction with different inputs and republish it."},
{"add_to_wallet", RPCArg::Type::BOOL, RPCArg::Default{true}, "When false, returns a serialized transaction which will not be added to the wallet or broadcast"},
- {"change_address", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
+ {"change_address", RPCArg::Type::STR, RPCArg::DefaultHint{"automatic"}, "The bitcoin address to receive the change"},
{"change_position", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
{"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if change_address is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
{"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."},
@@ -1351,7 +1351,7 @@ RPCHelpMan walletcreatefundedpsbt()
{"include_unsafe", RPCArg::Type::BOOL, RPCArg::Default{false}, "Include inputs that are not safe to spend (unconfirmed transactions from outside keys and unconfirmed replacement transactions).\n"
"Warning: the resulting transaction may become invalid if one of the unsafe inputs disappears.\n"
"If that happens, you will need to fund the transaction with different inputs and republish it."},
- {"changeAddress", RPCArg::Type::STR_HEX, RPCArg::DefaultHint{"pool address"}, "The bitcoin address to receive the change"},
+ {"changeAddress", RPCArg::Type::STR, RPCArg::DefaultHint{"automatic"}, "The bitcoin address to receive the change"},
{"changePosition", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"},
{"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if changeAddress is not specified. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
{"includeWatching", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Also select inputs which are watch only"},
diff --git a/src/wallet/rpc/transactions.cpp b/src/wallet/rpc/transactions.cpp
index eef2c13ee1..ad94ce4b32 100644
--- a/src/wallet/rpc/transactions.cpp
+++ b/src/wallet/rpc/transactions.cpp
@@ -34,6 +34,7 @@ static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue
}
uint256 hash = wtx.GetHash();
entry.pushKV("txid", hash.GetHex());
+ entry.pushKV("wtxid", wtx.GetWitnessHash().GetHex());
UniValue conflicts(UniValue::VARR);
for (const uint256& conflict : wallet.GetTxConflicts(wtx))
conflicts.push_back(conflict.GetHex());
@@ -431,6 +432,7 @@ static const std::vector<RPCResult> TransactionDescriptionString()
{RPCResult::Type::NUM, "blockindex", /*optional=*/true, "The index of the transaction in the block that includes it."},
{RPCResult::Type::NUM_TIME, "blocktime", /*optional=*/true, "The block time expressed in " + UNIX_EPOCH_TIME + "."},
{RPCResult::Type::STR_HEX, "txid", "The transaction id."},
+ {RPCResult::Type::STR_HEX, "wtxid", "The hash of serialized transaction, including witness data."},
{RPCResult::Type::ARR, "walletconflicts", "Conflicting transaction ids.",
{
{RPCResult::Type::STR_HEX, "txid", "The transaction id."},
diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp
index 883a3c102b..1380f1a77a 100644
--- a/src/wallet/rpc/wallet.cpp
+++ b/src/wallet/rpc/wallet.cpp
@@ -315,7 +315,9 @@ static RPCHelpMan createwallet()
{"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed."},
{"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Encrypt the wallet with this passphrase."},
{"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{false}, "Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind."},
- {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "Create a native descriptor wallet. The wallet will use descriptors internally to handle address creation"},
+ {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "Create a native descriptor wallet. The wallet will use descriptors internally to handle address creation."
+ " Setting to \"false\" will create a legacy wallet; however, the legacy wallet type is being deprecated and"
+ " support for creating and opening legacy wallets will be removed in the future."},
{"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED_NAMED_ARG, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
{"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."},
},
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index 53f7b773b4..8633e7c62c 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -322,8 +322,6 @@ bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t i
{
LOCK(cs_KeyStore);
- if (m_storage.IsLocked()) return false;
-
auto it = m_inactive_hd_chains.find(seed_id);
if (it == m_inactive_hd_chains.end()) {
return false;
@@ -331,27 +329,14 @@ bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t i
CHDChain& chain = it->second;
- // Top up key pool
- int64_t target_size = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 1);
-
- // "size" of the keypools. Not really the size, actually the difference between index and the chain counter
- // Since chain counter is 1 based and index is 0 based, one of them needs to be offset by 1.
- int64_t kp_size = (internal ? chain.nInternalChainCounter : chain.nExternalChainCounter) - (index + 1);
+ if (internal) {
+ chain.m_next_internal_index = std::max(chain.m_next_internal_index, index + 1);
+ } else {
+ chain.m_next_external_index = std::max(chain.m_next_external_index, index + 1);
+ }
- // make sure the keypool fits the user-selected target (-keypool)
- int64_t missing = std::max(target_size - kp_size, (int64_t) 0);
+ TopUpChain(chain, 0);
- if (missing > 0) {
- WalletBatch batch(m_storage.GetDatabase());
- for (int64_t i = missing; i > 0; --i) {
- GenerateNewKey(batch, chain, internal);
- }
- if (internal) {
- WalletLogPrintf("inactive seed with id %s added %d internal keys\n", HexStr(seed_id), missing);
- } else {
- WalletLogPrintf("inactive seed with id %s added %d keys\n", HexStr(seed_id), missing);
- }
- }
return true;
}
@@ -383,11 +368,26 @@ std::vector<WalletDestination> LegacyScriptPubKeyMan::MarkUnusedAddresses(const
if (it != mapKeyMetadata.end()){
CKeyMetadata meta = it->second;
if (!meta.hd_seed_id.IsNull() && meta.hd_seed_id != m_hd_chain.seed_id) {
- bool internal = (meta.key_origin.path[1] & ~BIP32_HARDENED_KEY_LIMIT) != 0;
- int64_t index = meta.key_origin.path[2] & ~BIP32_HARDENED_KEY_LIMIT;
-
- if (!TopUpInactiveHDChain(meta.hd_seed_id, index, internal)) {
- WalletLogPrintf("%s: Adding inactive seed keys failed\n", __func__);
+ std::vector<uint32_t> path;
+ if (meta.has_key_origin) {
+ path = meta.key_origin.path;
+ } else if (!ParseHDKeypath(meta.hdKeypath, path)) {
+ WalletLogPrintf("%s: Adding inactive seed keys failed, invalid hdKeypath: %s\n",
+ __func__,
+ meta.hdKeypath);
+ }
+ if (path.size() != 3) {
+ WalletLogPrintf("%s: Adding inactive seed keys failed, invalid path size: %d, has_key_origin: %s\n",
+ __func__,
+ path.size(),
+ meta.has_key_origin);
+ } else {
+ bool internal = (path[1] & ~BIP32_HARDENED_KEY_LIMIT) != 0;
+ int64_t index = path[2] & ~BIP32_HARDENED_KEY_LIMIT;
+
+ if (!TopUpInactiveHDChain(meta.hd_seed_id, index, internal)) {
+ WalletLogPrintf("%s: Adding inactive seed keys failed\n", __func__);
+ }
}
}
}
@@ -1265,44 +1265,69 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
if (!CanGenerateKeys()) {
return false;
}
- {
- LOCK(cs_KeyStore);
- if (m_storage.IsLocked()) return false;
+ if (!TopUpChain(m_hd_chain, kpSize)) {
+ return false;
+ }
+ for (auto& [chain_id, chain] : m_inactive_hd_chains) {
+ if (!TopUpChain(chain, kpSize)) {
+ return false;
+ }
+ }
+ NotifyCanGetAddressesChanged();
+ return true;
+}
- // Top up key pool
- unsigned int nTargetSize;
- if (kpSize > 0)
- nTargetSize = kpSize;
- else
- nTargetSize = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 0);
+bool LegacyScriptPubKeyMan::TopUpChain(CHDChain& chain, unsigned int kpSize)
+{
+ LOCK(cs_KeyStore);
- // count amount of available keys (internal, external)
- // make sure the keypool of external and internal keys fits the user selected target (-keypool)
- int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setExternalKeyPool.size(), (int64_t) 0);
- int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setInternalKeyPool.size(), (int64_t) 0);
+ if (m_storage.IsLocked()) return false;
- if (!IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD_SPLIT))
- {
- // don't create extra internal keys
- missingInternal = 0;
+ // Top up key pool
+ unsigned int nTargetSize;
+ if (kpSize > 0) {
+ nTargetSize = kpSize;
+ } else {
+ nTargetSize = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), int64_t{0});
+ }
+ int64_t target = std::max((int64_t) nTargetSize, int64_t{1});
+
+ // count amount of available keys (internal, external)
+ // make sure the keypool of external and internal keys fits the user selected target (-keypool)
+ int64_t missingExternal;
+ int64_t missingInternal;
+ if (chain == m_hd_chain) {
+ missingExternal = std::max(target - (int64_t)setExternalKeyPool.size(), int64_t{0});
+ missingInternal = std::max(target - (int64_t)setInternalKeyPool.size(), int64_t{0});
+ } else {
+ missingExternal = std::max(target - (chain.nExternalChainCounter - chain.m_next_external_index), int64_t{0});
+ missingInternal = std::max(target - (chain.nInternalChainCounter - chain.m_next_internal_index), int64_t{0});
+ }
+
+ if (!IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) {
+ // don't create extra internal keys
+ missingInternal = 0;
+ }
+ bool internal = false;
+ WalletBatch batch(m_storage.GetDatabase());
+ for (int64_t i = missingInternal + missingExternal; i--;) {
+ if (i < missingInternal) {
+ internal = true;
}
- bool internal = false;
- WalletBatch batch(m_storage.GetDatabase());
- for (int64_t i = missingInternal + missingExternal; i--;)
- {
- if (i < missingInternal) {
- internal = true;
- }
- CPubKey pubkey(GenerateNewKey(batch, m_hd_chain, internal));
+ CPubKey pubkey(GenerateNewKey(batch, chain, internal));
+ if (chain == m_hd_chain) {
AddKeypoolPubkeyWithDB(pubkey, internal, batch);
}
- if (missingInternal + missingExternal > 0) {
+ }
+ if (missingInternal + missingExternal > 0) {
+ if (chain == m_hd_chain) {
WalletLogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size(), setInternalKeyPool.size());
+ } else {
+ WalletLogPrintf("inactive seed with id %s added %d external keys, %d internal keys\n", HexStr(chain.seed_id), missingExternal, missingInternal);
}
}
- NotifyCanGetAddressesChanged();
return true;
}
diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h
index 6eda133771..ad924791af 100644
--- a/src/wallet/scriptpubkeyman.h
+++ b/src/wallet/scriptpubkeyman.h
@@ -355,6 +355,7 @@ private:
*/
bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal);
+ bool TopUpChain(CHDChain& chain, unsigned int size);
public:
using ScriptPubKeyMan::ScriptPubKeyMan;
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index 83eaececc1..a707ef89d2 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -379,7 +379,6 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
// Note that unlike KnapsackSolver, we do not include the fee for creating a change output as BnB will not create a change output.
std::vector<OutputGroup> positive_groups = GroupOutputs(wallet, coins, coin_selection_params, eligibility_filter, true /* positive_only */);
if (auto bnb_result{SelectCoinsBnB(positive_groups, nTargetValue, coin_selection_params.m_cost_of_change)}) {
- bnb_result->ComputeAndSetWaste(CAmount(0));
results.push_back(*bnb_result);
}
@@ -583,12 +582,13 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, const uint256&
}
/**
- * Return a height-based locktime for new transactions (uses the height of the
+ * Set a height-based locktime for new transactions (uses the height of the
* current chain tip unless we are not synced with the current chain
*/
-static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uint256& block_hash, int block_height)
+static void DiscourageFeeSniping(CMutableTransaction& tx, interfaces::Chain& chain, const uint256& block_hash, int block_height)
{
- uint32_t locktime;
+ // All inputs must be added by now
+ assert(!tx.vin.empty());
// Discourage fee sniping.
//
// For a large miner the value of the transactions in the best block and
@@ -610,22 +610,34 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, const uin
// now we ensure code won't be written that makes assumptions about
// nLockTime that preclude a fix later.
if (IsCurrentForAntiFeeSniping(chain, block_hash)) {
- locktime = block_height;
+ tx.nLockTime = block_height;
// Secondly occasionally randomly pick a nLockTime even further back, so
// that transactions that are delayed after signing for whatever reason,
// e.g. high-latency mix networks and some CoinJoin implementations, have
// better privacy.
- if (GetRandInt(10) == 0)
- locktime = std::max(0, (int)locktime - GetRandInt(100));
+ if (GetRandInt(10) == 0) {
+ tx.nLockTime = std::max(0, int(tx.nLockTime) - GetRandInt(100));
+ }
} else {
// If our chain is lagging behind, we can't discourage fee sniping nor help
// the privacy of high-latency transactions. To avoid leaking a potentially
// unique "nLockTime fingerprint", set nLockTime to a constant.
- locktime = 0;
+ tx.nLockTime = 0;
+ }
+ // Sanity check all values
+ assert(tx.nLockTime < LOCKTIME_THRESHOLD); // Type must be block height
+ assert(tx.nLockTime <= uint64_t(block_height));
+ for (const auto& in : tx.vin) {
+ // Can not be FINAL for locktime to work
+ assert(in.nSequence != CTxIn::SEQUENCE_FINAL);
+ // May be MAX NONFINAL to disable both BIP68 and BIP125
+ if (in.nSequence == CTxIn::MAX_SEQUENCE_NONFINAL) continue;
+ // May be MAX BIP125 to disable BIP68 and enable BIP125
+ if (in.nSequence == MAX_BIP125_RBF_SEQUENCE) continue;
+ // The wallet does not support any other sequence-use right now.
+ assert(false);
}
- assert(locktime < LOCKTIME_THRESHOLD);
- return locktime;
}
static bool CreateTransactionInternal(
@@ -642,7 +654,6 @@ static bool CreateTransactionInternal(
AssertLockHeld(wallet.cs_wallet);
CMutableTransaction txNew; // The resulting transaction that we make
- txNew.nLockTime = GetLocktimeForNewTransaction(wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
@@ -788,8 +799,8 @@ static bool CreateTransactionInternal(
// Shuffle selected coins and fill in final vin
std::vector<CInputCoin> selected_coins = result->GetShuffledInputVector();
- // Note how the sequence number is set to non-maxint so that
- // the nLockTime set above actually works.
+ // The sequence number is set to non-maxint so that DiscourageFeeSniping
+ // works.
//
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
// we use the highest possible value in that range (maxint-2)
@@ -800,6 +811,7 @@ static bool CreateTransactionInternal(
for (const auto& coin : selected_coins) {
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
}
+ DiscourageFeeSniping(txNew, wallet.chain(), wallet.GetLastBlockHash(), wallet.GetLastBlockHeight());
// Calculate the transaction fee
TxSize tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), &wallet, &coin_control);
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp
index 2b2181e70b..55949e6e7a 100644
--- a/src/wallet/sqlite.cpp
+++ b/src/wallet/sqlite.cpp
@@ -37,6 +37,22 @@ static void ErrorLogCallback(void* arg, int code, const char* msg)
LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
}
+static bool BindBlobToStatement(sqlite3_stmt* stmt,
+ int index,
+ Span<const std::byte> blob,
+ const std::string& description)
+{
+ int res = sqlite3_bind_blob(stmt, index, blob.data(), blob.size(), SQLITE_STATIC);
+ if (res != SQLITE_OK) {
+ LogPrintf("Unable to bind %s to statement: %s\n", description, sqlite3_errstr(res));
+ sqlite3_clear_bindings(stmt);
+ sqlite3_reset(stmt);
+ return false;
+ }
+
+ return true;
+}
+
static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
{
std::string stmt_text = strprintf("PRAGMA %s", key);
@@ -377,14 +393,8 @@ bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)
assert(m_read_stmt);
// Bind: leftmost parameter in statement is index 1
- int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
- if (res != SQLITE_OK) {
- LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
- sqlite3_clear_bindings(m_read_stmt);
- sqlite3_reset(m_read_stmt);
- return false;
- }
- res = sqlite3_step(m_read_stmt);
+ if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
+ int res = sqlite3_step(m_read_stmt);
if (res != SQLITE_ROW) {
if (res != SQLITE_DONE) {
// SQLITE_DONE means "not found", don't log an error in that case.
@@ -418,23 +428,11 @@ bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrit
// Bind: leftmost parameter in statement is index 1
// Insert index 1 is key, 2 is value
- int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
- if (res != SQLITE_OK) {
- LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
- sqlite3_clear_bindings(stmt);
- sqlite3_reset(stmt);
- return false;
- }
- res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
- if (res != SQLITE_OK) {
- LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
- sqlite3_clear_bindings(stmt);
- sqlite3_reset(stmt);
- return false;
- }
+ if (!BindBlobToStatement(stmt, 1, key, "key")) return false;
+ if (!BindBlobToStatement(stmt, 2, value, "value")) return false;
// Execute
- res = sqlite3_step(stmt);
+ int res = sqlite3_step(stmt);
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
if (res != SQLITE_DONE) {
@@ -449,16 +447,10 @@ bool SQLiteBatch::EraseKey(CDataStream&& key)
assert(m_delete_stmt);
// Bind: leftmost parameter in statement is index 1
- int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
- if (res != SQLITE_OK) {
- LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
- sqlite3_clear_bindings(m_delete_stmt);
- sqlite3_reset(m_delete_stmt);
- return false;
- }
+ if (!BindBlobToStatement(m_delete_stmt, 1, key, "key")) return false;
// Execute
- res = sqlite3_step(m_delete_stmt);
+ int res = sqlite3_step(m_delete_stmt);
sqlite3_clear_bindings(m_delete_stmt);
sqlite3_reset(m_delete_stmt);
if (res != SQLITE_DONE) {
@@ -473,18 +465,11 @@ bool SQLiteBatch::HasKey(CDataStream&& key)
assert(m_read_stmt);
// Bind: leftmost parameter in statement is index 1
- bool ret = false;
- int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
- if (res == SQLITE_OK) {
- res = sqlite3_step(m_read_stmt);
- if (res == SQLITE_ROW) {
- ret = true;
- }
- }
-
+ if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
+ int res = sqlite3_step(m_read_stmt);
sqlite3_clear_bindings(m_read_stmt);
sqlite3_reset(m_read_stmt);
- return ret;
+ return res == SQLITE_ROW;
}
bool SQLiteBatch::StartCursor()
diff --git a/src/wallet/test/psbt_wallet_tests.cpp b/src/wallet/test/psbt_wallet_tests.cpp
index b953f402a2..62053ae8d2 100644
--- a/src/wallet/test/psbt_wallet_tests.cpp
+++ b/src/wallet/test/psbt_wallet_tests.cpp
@@ -68,7 +68,6 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
// Try to sign the mutated input
SignatureData sigdata;
BOOST_CHECK(m_wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, true, true) != TransactionError::OK);
- //BOOST_CHECK(spk_man->FillPSBT(psbtx, PrecomputePSBTData(psbtx), SIGHASH_ALL, true, true) != TransactionError::OK);
}
BOOST_AUTO_TEST_CASE(parse_hd_keypath)
diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
index 7693c9c0e8..c59f7e6f05 100644
--- a/src/wallet/test/wallet_tests.cpp
+++ b/src/wallet/test/wallet_tests.cpp
@@ -367,10 +367,10 @@ static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lock
CBlockIndex* block = nullptr;
if (blockTime > 0) {
LOCK(cs_main);
- auto inserted = chainman.BlockIndex().emplace(GetRandHash(), new CBlockIndex);
+ auto inserted = chainman.BlockIndex().emplace(std::piecewise_construct, std::make_tuple(GetRandHash()), std::make_tuple());
assert(inserted.second);
const uint256& hash = inserted.first->first;
- block = inserted.first->second;
+ block = &inserted.first->second;
block->nTime = blockTime;
block->phashBlock = &hash;
state = TxStateConfirmed{hash, block->nHeight, /*position_in_block=*/0};
diff --git a/src/wallet/transaction.h b/src/wallet/transaction.h
index 00f9c9f154..271d698e56 100644
--- a/src/wallet/transaction.h
+++ b/src/wallet/transaction.h
@@ -296,6 +296,7 @@ public:
bool isUnconfirmed() const { return !isAbandoned() && !isConflicted() && !isConfirmed(); }
bool isConfirmed() const { return state<TxStateConfirmed>(); }
const uint256& GetHash() const { return tx->GetHash(); }
+ const uint256& GetWitnessHash() const { return tx->GetWitnessHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
// Disable copying of CWalletTx objects to prevent bugs where instances get
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 6cf9f9ce74..261d042529 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -354,6 +354,11 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
// Write the wallet settings
UpdateWalletSetting(*context.chain, name, load_on_start, warnings);
+ // Legacy wallets are being deprecated, warn if a newly created wallet is legacy
+ if (!(wallet_creation_flags & WALLET_FLAG_DESCRIPTORS)) {
+ warnings.push_back(_("Wallet created successfully. The legacy wallet type is being deprecated and support for creating and opening legacy wallets will be removed in the future."));
+ }
+
status = DatabaseStatus::SUCCESS;
return wallet;
}
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 2d88b07146..6e100524a4 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -557,9 +557,9 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
}
if (internal) {
chain.nVersion = CHDChain::VERSION_HD_CHAIN_SPLIT;
- chain.nInternalChainCounter = std::max(chain.nInternalChainCounter, index);
+ chain.nInternalChainCounter = std::max(chain.nInternalChainCounter, index + 1);
} else {
- chain.nExternalChainCounter = std::max(chain.nExternalChainCounter, index);
+ chain.nExternalChainCounter = std::max(chain.nExternalChainCounter, index + 1);
}
}
} else if (strType == DBKeys::WATCHMETA) {
diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h
index 760019e76b..3dfe781d56 100644
--- a/src/wallet/walletdb.h
+++ b/src/wallet/walletdb.h
@@ -93,6 +93,8 @@ public:
uint32_t nExternalChainCounter;
uint32_t nInternalChainCounter;
CKeyID seed_id; //!< seed hash160
+ int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
+ int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
static const int VERSION_HD_BASE = 1;
static const int VERSION_HD_CHAIN_SPLIT = 2;
diff --git a/test/functional/feature_addrman.py b/test/functional/feature_addrman.py
index 0fdefaa9c3..5e49d0214a 100755
--- a/test/functional/feature_addrman.py
+++ b/test/functional/feature_addrman.py
@@ -68,6 +68,17 @@ class AddrmanTest(BitcoinTestFramework):
self.start_node(0, extra_args=["-checkaddrman=1"])
assert_equal(self.nodes[0].getnodeaddresses(), [])
+ self.log.info("Check that addrman with negative lowest_compatible cannot be read")
+ self.stop_node(0)
+ write_addrman(peers_dat, lowest_compatible=-32)
+ self.nodes[0].assert_start_raises_init_error(
+ expected_msg=init_error(
+ "Corrupted addrman database: The compat value \\(0\\) is lower "
+ "than the expected minimum value 32.: (.+)"
+ ),
+ match=ErrorMatch.FULL_REGEX,
+ )
+
self.log.info("Check that addrman from future is overwritten with new addrman")
self.stop_node(0)
write_addrman(peers_dat, lowest_compatible=111)
diff --git a/test/functional/feature_bind_port_discover.py b/test/functional/feature_bind_port_discover.py
new file mode 100755
index 0000000000..6e07f2f16c
--- /dev/null
+++ b/test/functional/feature_bind_port_discover.py
@@ -0,0 +1,78 @@
+#!/usr/bin/env python3
+# Copyright (c) 2020-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.
+"""
+Test that -discover does not add all interfaces' addresses if we listen on only some of them
+"""
+
+from test_framework.test_framework import BitcoinTestFramework, SkipTest
+from test_framework.util import assert_equal
+
+# We need to bind to a routable address for this test to exercise the relevant code
+# and also must have another routable address on another interface which must not
+# be named "lo" or "lo0".
+# To set these routable addresses on the machine, use:
+# Linux:
+# ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up # to set up
+# ifconfig lo:0 down && ifconfig lo:1 down # to remove it, after the test
+# FreeBSD:
+# ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias # to set up
+# ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias # to remove it, after the test
+ADDR1 = '1.1.1.1'
+ADDR2 = '2.2.2.2'
+
+BIND_PORT = 31001
+
+class BindPortDiscoverTest(BitcoinTestFramework):
+ def set_test_params(self):
+ # Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
+ self.setup_clean_chain = True
+ self.bind_to_localhost_only = False
+ self.extra_args = [
+ ['-discover', f'-port={BIND_PORT}'], # bind on any
+ ['-discover', f'-bind={ADDR1}:{BIND_PORT}'],
+ ]
+ self.num_nodes = len(self.extra_args)
+
+ def add_options(self, parser):
+ parser.add_argument(
+ "--ihave1111and2222", action='store_true', dest="ihave1111and2222",
+ help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
+ default=False)
+
+ def skip_test_if_missing_module(self):
+ if not self.options.ihave1111and2222:
+ raise SkipTest(
+ f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
+ "assigned to the interfaces on this machine and rerun with --ihave1111and2222")
+
+ def run_test(self):
+ self.log.info(
+ "Test that if -bind= is not passed then all addresses are "
+ "added to localaddresses")
+ found_addr1 = False
+ found_addr2 = False
+ for local in self.nodes[0].getnetworkinfo()['localaddresses']:
+ if local['address'] == ADDR1:
+ found_addr1 = True
+ assert_equal(local['port'], BIND_PORT)
+ if local['address'] == ADDR2:
+ found_addr2 = True
+ assert_equal(local['port'], BIND_PORT)
+ assert found_addr1
+ assert found_addr2
+
+ self.log.info(
+ "Test that if -bind= is passed then only that address is "
+ "added to localaddresses")
+ found_addr1 = False
+ for local in self.nodes[1].getnetworkinfo()['localaddresses']:
+ if local['address'] == ADDR1:
+ found_addr1 = True
+ assert_equal(local['port'], BIND_PORT)
+ assert local['address'] != ADDR2
+ assert found_addr1
+
+if __name__ == '__main__':
+ BindPortDiscoverTest().main()
diff --git a/test/functional/feature_bind_port_externalip.py b/test/functional/feature_bind_port_externalip.py
new file mode 100755
index 0000000000..6a74ce5738
--- /dev/null
+++ b/test/functional/feature_bind_port_externalip.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+# Copyright (c) 2020-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.
+"""
+Test that the proper port is used for -externalip=
+"""
+
+from test_framework.test_framework import BitcoinTestFramework, SkipTest
+from test_framework.util import assert_equal, p2p_port
+
+# We need to bind to a routable address for this test to exercise the relevant code.
+# To set a routable address on the machine use:
+# Linux:
+# ifconfig lo:0 1.1.1.1/32 up # to set up
+# ifconfig lo:0 down # to remove it, after the test
+# FreeBSD:
+# ifconfig lo0 1.1.1.1/32 alias # to set up
+# ifconfig lo0 1.1.1.1 -alias # to remove it, after the test
+ADDR = '1.1.1.1'
+
+# array of tuples [arguments, expected port in localaddresses]
+EXPECTED = [
+ [['-externalip=2.2.2.2', '-port=30001'], 30001],
+ [['-externalip=2.2.2.2', '-port=30002', f'-bind={ADDR}'], 30002],
+ [['-externalip=2.2.2.2', f'-bind={ADDR}'], 'default_p2p_port'],
+ [['-externalip=2.2.2.2', '-port=30003', f'-bind={ADDR}:30004'], 30004],
+ [['-externalip=2.2.2.2', f'-bind={ADDR}:30005'], 30005],
+ [['-externalip=2.2.2.2:30006', '-port=30007'], 30006],
+ [['-externalip=2.2.2.2:30008', '-port=30009', f'-bind={ADDR}'], 30008],
+ [['-externalip=2.2.2.2:30010', f'-bind={ADDR}'], 30010],
+ [['-externalip=2.2.2.2:30011', '-port=30012', f'-bind={ADDR}:30013'], 30011],
+ [['-externalip=2.2.2.2:30014', f'-bind={ADDR}:30015'], 30014],
+ [['-externalip=2.2.2.2', '-port=30016', f'-bind={ADDR}:30017',
+ f'-whitebind={ADDR}:30018'], 30017],
+ [['-externalip=2.2.2.2', '-port=30019',
+ f'-whitebind={ADDR}:30020'], 30020],
+]
+
+class BindPortExternalIPTest(BitcoinTestFramework):
+ def set_test_params(self):
+ # Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
+ self.setup_clean_chain = True
+ self.bind_to_localhost_only = False
+ self.num_nodes = len(EXPECTED)
+ self.extra_args = list(map(lambda e: e[0], EXPECTED))
+
+ def add_options(self, parser):
+ parser.add_argument(
+ "--ihave1111", action='store_true', dest="ihave1111",
+ help=f"Run the test, assuming {ADDR} is configured on the machine",
+ default=False)
+
+ def skip_test_if_missing_module(self):
+ if not self.options.ihave1111:
+ raise SkipTest(
+ f"To run this test make sure that {ADDR} (a routable address) is assigned "
+ "to one of the interfaces on this machine and rerun with --ihave1111")
+
+ def run_test(self):
+ self.log.info("Test the proper port is used for -externalip=")
+ for i in range(len(EXPECTED)):
+ expected_port = EXPECTED[i][1]
+ if expected_port == 'default_p2p_port':
+ expected_port = p2p_port(i)
+ found = False
+ for local in self.nodes[i].getnetworkinfo()['localaddresses']:
+ if local['address'] == '2.2.2.2':
+ assert_equal(local['port'], expected_port)
+ found = True
+ break
+ assert found
+
+if __name__ == '__main__':
+ BindPortExternalIPTest().main()
diff --git a/test/functional/feature_maxuploadtarget.py b/test/functional/feature_maxuploadtarget.py
index 24f79dda67..0b9d651226 100755
--- a/test/functional/feature_maxuploadtarget.py
+++ b/test/functional/feature_maxuploadtarget.py
@@ -13,10 +13,19 @@ if uploadtarget has been reached.
from collections import defaultdict
import time
-from test_framework.messages import CInv, MSG_BLOCK, msg_getdata
+from test_framework.messages import (
+ CInv,
+ MSG_BLOCK,
+ msg_getdata,
+)
from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
-from test_framework.util import assert_equal, mine_large_block
+from test_framework.util import (
+ assert_equal,
+ mine_large_block,
+)
+from test_framework.wallet import MiniWallet
+
class TestP2PConn(P2PInterface):
def __init__(self):
@@ -41,12 +50,6 @@ class MaxUploadTest(BitcoinTestFramework):
]]
self.supports_cli = False
- # Cache for utxos, as the listunspent may take a long time later in the test
- self.utxo_cache = []
-
- def skip_test_if_missing_module(self):
- self.skip_if_no_wallet()
-
def run_test(self):
# Before we connect anything, we first set the time on the node
# to be in the past, otherwise things break because the CNode
@@ -55,7 +58,8 @@ class MaxUploadTest(BitcoinTestFramework):
self.nodes[0].setmocktime(old_time)
# Generate some old blocks
- self.generate(self.nodes[0], 130)
+ self.wallet = MiniWallet(self.nodes[0])
+ self.generate(self.wallet, 130)
# p2p_conns[0] will only request old blocks
# p2p_conns[1] will only request new blocks
@@ -66,7 +70,7 @@ class MaxUploadTest(BitcoinTestFramework):
p2p_conns.append(self.nodes[0].add_p2p_connection(TestP2PConn()))
# Now mine a big block
- mine_large_block(self, self.nodes[0], self.utxo_cache)
+ mine_large_block(self, self.wallet, self.nodes[0])
# Store the hash; we'll request this later
big_old_block = self.nodes[0].getbestblockhash()
@@ -77,7 +81,7 @@ class MaxUploadTest(BitcoinTestFramework):
self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
# Mine one more block, so that the prior block looks old
- mine_large_block(self, self.nodes[0], self.utxo_cache)
+ mine_large_block(self, self.wallet, self.nodes[0])
# We'll be requesting this new block too
big_new_block = self.nodes[0].getbestblockhash()
diff --git a/test/functional/feature_segwit.py b/test/functional/feature_segwit.py
index 6d7f1def88..50adc08d9a 100755
--- a/test/functional/feature_segwit.py
+++ b/test/functional/feature_segwit.py
@@ -117,11 +117,9 @@ class SegWitTest(BitcoinTestFramework):
assert_equal(len(node.getblock(block[0])["tx"]), 2)
self.sync_blocks()
- def skip_mine(self, node, txid, sign, redeem_script=""):
+ def fail_mine(self, node, txid, sign, redeem_script=""):
send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
- block = self.generate(node, 1)
- assert_equal(len(node.getblock(block[0])["tx"]), 1)
- self.sync_blocks()
+ assert_raises_rpc_error(-1, "unexpected witness data found", self.generate, node, 1)
def fail_accept(self, node, error_msg, txid, sign, redeem_script=""):
assert_raises_rpc_error(-26, error_msg, send_to_witness, use_p2wsh=1, node=node, utxo=getutxo(txid), pubkey=self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=sign, insert_redeem_script=redeem_script)
@@ -197,21 +195,21 @@ class SegWitTest(BitcoinTestFramework):
assert_equal(self.nodes[1].getbalance(), 20 * Decimal("49.999"))
assert_equal(self.nodes[2].getbalance(), 20 * Decimal("49.999"))
- self.generate(self.nodes[0], 260) # block 423
+ self.generate(self.nodes[0], 264) # block 427
- self.log.info("Verify witness txs are skipped for mining before the fork")
- self.skip_mine(self.nodes[2], wit_ids[NODE_2][P2WPKH][0], True) # block 424
- self.skip_mine(self.nodes[2], wit_ids[NODE_2][P2WSH][0], True) # block 425
- self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][P2WPKH][0], True) # block 426
- self.skip_mine(self.nodes[2], p2sh_ids[NODE_2][P2WSH][0], True) # block 427
+ self.log.info("Verify witness txs cannot be mined before the fork")
+ self.fail_mine(self.nodes[2], wit_ids[NODE_2][P2WPKH][0], True)
+ self.fail_mine(self.nodes[2], wit_ids[NODE_2][P2WSH][0], True)
+ self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][P2WPKH][0], True)
+ self.fail_mine(self.nodes[2], p2sh_ids[NODE_2][P2WSH][0], True)
self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid")
self.fail_accept(self.nodes[2], "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WPKH][1], sign=False)
self.fail_accept(self.nodes[2], "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WSH][1], sign=False)
- self.generate(self.nodes[2], 4) # blocks 428-431
+ self.generate(self.nodes[0], 4) # blocks 428-431
- self.log.info("Verify previous witness txs skipped for mining can now be mined")
+ self.log.info("Verify previous witness txs can now be mined")
assert_equal(len(self.nodes[2].getrawmempool()), 4)
blockhash = self.generate(self.nodes[2], 1)[0] # block 432 (first block with new rules; 432 = 144 * 3)
assert_equal(len(self.nodes[2].getrawmempool()), 0)
diff --git a/test/functional/p2p_message_capture.py b/test/functional/p2p_message_capture.py
index edde9a6ecf..0a7ae44de4 100755
--- a/test/functional/p2p_message_capture.py
+++ b/test/functional/p2p_message_capture.py
@@ -20,7 +20,7 @@ LENGTH_SIZE = 4
MSGTYPE_SIZE = 12
def mini_parser(dat_file):
- """Parse a data file created by CaptureMessage.
+ """Parse a data file created by CaptureMessageToFile.
From the data file we'll only check the structure.
diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py
index b264f23fb5..193bd3f1cd 100755
--- a/test/functional/rpc_blockchain.py
+++ b/test/functional/rpc_blockchain.py
@@ -200,7 +200,7 @@ class BlockchainTest(BitcoinTestFramework):
'timeout': 0x7fffffffffffffff, # testdummy does not have a timeout so is set to the max int64 value
'min_activation_height': 0,
'status': 'started',
- 'status-next': status_next,
+ 'status_next': status_next,
'since': 144,
'statistics': {
'period': 144,
@@ -220,7 +220,7 @@ class BlockchainTest(BitcoinTestFramework):
'timeout': 9223372036854775807,
'min_activation_height': 0,
'status': 'active',
- 'status-next': 'active',
+ 'status_next': 'active',
'since': 0,
},
'height': 0,
diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py
index 7791ae5392..2b70eab4e4 100644
--- a/test/functional/test_framework/script.py
+++ b/test/functional/test_framework/script.py
@@ -27,6 +27,7 @@ from .messages import (
from .ripemd160 import ripemd160
MAX_SCRIPT_ELEMENT_SIZE = 520
+MAX_PUBKEYS_PER_MULTI_A = 999
LOCKTIME_THRESHOLD = 500000000
ANNEX_TAG = 0x50
diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py
index 289e83579b..e56d4aa492 100755
--- a/test/functional/test_framework/test_node.py
+++ b/test/functional/test_framework/test_node.py
@@ -743,6 +743,9 @@ class RPCOverloadWrapper():
def __getattr__(self, name):
return getattr(self.rpc, name)
+ def createwallet_passthrough(self, *args, **kwargs):
+ return self.__getattr__("createwallet")(*args, **kwargs)
+
def createwallet(self, wallet_name, disable_private_keys=None, blank=None, passphrase='', avoid_reuse=None, descriptors=None, load_on_startup=None, external_signer=None):
if descriptors is None:
descriptors = self.descriptors
diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py
index 210025104e..8651bcf636 100644
--- a/test/functional/test_framework/util.py
+++ b/test/functional/test_framework/util.py
@@ -573,17 +573,17 @@ def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
return txids
-def mine_large_block(test_framework, node, utxos=None):
+def mine_large_block(test_framework, mini_wallet, node):
# generate a 66k transaction,
# and 14 of them is close to the 1MB block limit
- num = 14
txouts = gen_return_txouts()
- utxos = utxos if utxos is not None else []
- if len(utxos) < num:
- utxos.clear()
- utxos.extend(node.listunspent())
- fee = 100 * node.getnetworkinfo()["relayfee"]
- create_lots_of_big_transactions(node, txouts, utxos, num, fee=fee)
+ from .messages import COIN
+ fee = 100 * int(node.getnetworkinfo()["relayfee"] * COIN)
+ for _ in range(14):
+ tx = mini_wallet.create_self_transfer(from_node=node, fee_rate=0, mempool_valid=False)['tx']
+ tx.vout[0].nValue -= fee
+ tx.vout.extend(txouts)
+ mini_wallet.sendrawtransaction(from_node=node, tx_hex=tx.serialize().hex())
test_framework.generate(node, 1)
diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py
index 516e8be638..b0f24e3b97 100755
--- a/test/functional/test_runner.py
+++ b/test/functional/test_runner.py
@@ -276,11 +276,13 @@ BASE_SCRIPTS = [
'feature_minchainwork.py',
'rpc_estimatefee.py',
'rpc_getblockstats.py',
+ 'feature_bind_port_externalip.py',
'wallet_create_tx.py --legacy-wallet',
'wallet_send.py --legacy-wallet',
'wallet_send.py --descriptors',
'wallet_create_tx.py --descriptors',
'wallet_taproot.py',
+ 'wallet_inactive_hdchains.py',
'p2p_fingerprint.py',
'feature_uacomment.py',
'feature_init.py',
@@ -290,6 +292,7 @@ BASE_SCRIPTS = [
'feature_loadblock.py',
'p2p_dos_header_tree.py',
'p2p_add_connections.py',
+ 'feature_bind_port_discover.py',
'p2p_unrequested_blocks.py',
'p2p_blockfilters.py',
'p2p_message_capture.py',
diff --git a/test/functional/wallet_basic.py b/test/functional/wallet_basic.py
index 69f9df57d8..a7873838be 100755
--- a/test/functional/wallet_basic.py
+++ b/test/functional/wallet_basic.py
@@ -668,7 +668,7 @@ class WalletTest(BitcoinTestFramework):
"category": baz["category"],
"vout": baz["vout"]}
expected_fields = frozenset({'amount', 'bip125-replaceable', 'confirmations', 'details', 'fee',
- 'hex', 'time', 'timereceived', 'trusted', 'txid', 'walletconflicts'})
+ 'hex', 'time', 'timereceived', 'trusted', 'txid', 'wtxid', 'walletconflicts'})
verbose_field = "decoded"
expected_verbose_fields = expected_fields | {verbose_field}
diff --git a/test/functional/wallet_createwallet.py b/test/functional/wallet_createwallet.py
index 4416a9655f..e8234de032 100755
--- a/test/functional/wallet_createwallet.py
+++ b/test/functional/wallet_createwallet.py
@@ -164,5 +164,10 @@ class CreateWalletTest(BitcoinTestFramework):
self.log.info('Using a passphrase with private keys disabled returns error')
assert_raises_rpc_error(-4, 'Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.', self.nodes[0].createwallet, wallet_name='w9', disable_private_keys=True, passphrase='thisisapassphrase')
+ if self.is_bdb_compiled():
+ self.log.info("Test legacy wallet deprecation")
+ res = self.nodes[0].createwallet(wallet_name="legacy_w0", descriptors=False, passphrase=None)
+ assert_equal(res["warning"], "Wallet created successfully. The legacy wallet type is being deprecated and support for creating and opening legacy wallets will be removed in the future.")
+
if __name__ == '__main__':
CreateWalletTest().main()
diff --git a/test/functional/wallet_importprunedfunds.py b/test/functional/wallet_importprunedfunds.py
index cdb5823109..2a4d0981c7 100755
--- a/test/functional/wallet_importprunedfunds.py
+++ b/test/functional/wallet_importprunedfunds.py
@@ -5,9 +5,13 @@
"""Test the importprunedfunds and removeprunedfunds RPCs."""
from decimal import Decimal
-from test_framework.blocktools import COINBASE_MATURITY
from test_framework.address import key_to_p2wpkh
+from test_framework.blocktools import COINBASE_MATURITY
from test_framework.key import ECKey
+from test_framework.messages import (
+ CMerkleBlock,
+ from_hex,
+)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
@@ -15,6 +19,7 @@ from test_framework.util import (
)
from test_framework.wallet_util import bytes_to_wif
+
class ImportPrunedFundsTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
@@ -124,5 +129,18 @@ class ImportPrunedFundsTest(BitcoinTestFramework):
w1.removeprunedfunds(txnid3)
assert not [tx for tx in w1.listtransactions(include_watchonly=True) if tx['txid'] == txnid3]
+ # Check various RPC parameter validation errors
+ assert_raises_rpc_error(-22, "TX decode failed", w1.importprunedfunds, b'invalid tx'.hex(), proof1)
+ assert_raises_rpc_error(-5, "Transaction given doesn't exist in proof", w1.importprunedfunds, rawtxn2, proof1)
+
+ mb = from_hex(CMerkleBlock(), proof1)
+ mb.header.hashMerkleRoot = 0xdeadbeef # cause mismatch between merkle root and merkle block
+ assert_raises_rpc_error(-5, "Something wrong with merkleblock", w1.importprunedfunds, rawtxn1, mb.serialize().hex())
+
+ mb = from_hex(CMerkleBlock(), proof1)
+ mb.header.nTime += 1 # modify arbitrary block header field to change block hash
+ assert_raises_rpc_error(-5, "Block not found in chain", w1.importprunedfunds, rawtxn1, mb.serialize().hex())
+
+
if __name__ == '__main__':
ImportPrunedFundsTest().main()
diff --git a/test/functional/wallet_inactive_hdchains.py b/test/functional/wallet_inactive_hdchains.py
new file mode 100755
index 0000000000..e1dad00876
--- /dev/null
+++ b/test/functional/wallet_inactive_hdchains.py
@@ -0,0 +1,147 @@
+#!/usr/bin/env python3
+# Copyright (c) 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.
+"""
+Test Inactive HD Chains.
+"""
+import os
+import shutil
+import time
+
+from test_framework.authproxy import JSONRPCException
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.wallet_util import (
+ get_generate_key,
+)
+
+
+class InactiveHDChainsTest(BitcoinTestFramework):
+ def set_test_params(self):
+ self.setup_clean_chain = True
+ self.num_nodes = 2
+ self.extra_args = [["-keypool=10"], ["-nowallet", "-keypool=10"]]
+
+ def skip_test_if_missing_module(self):
+ self.skip_if_no_wallet()
+ self.skip_if_no_bdb()
+ self.skip_if_no_previous_releases()
+
+ def setup_nodes(self):
+ self.add_nodes(self.num_nodes, extra_args=self.extra_args, versions=[
+ None,
+ 170200, # 0.17.2 Does not have the key metadata upgrade
+ ])
+
+ self.start_nodes()
+ self.init_wallet(node=0)
+
+ def prepare_wallets(self, wallet_basename, encrypt=False):
+ self.nodes[0].createwallet(wallet_name=f"{wallet_basename}_base", descriptors=False, blank=True)
+ self.nodes[0].createwallet(wallet_name=f"{wallet_basename}_test", descriptors=False, blank=True)
+ base_wallet = self.nodes[0].get_wallet_rpc(f"{wallet_basename}_base")
+ test_wallet = self.nodes[0].get_wallet_rpc(f"{wallet_basename}_test")
+
+ # Setup both wallets with the same HD seed
+ seed = get_generate_key()
+ base_wallet.sethdseed(True, seed.privkey)
+ test_wallet.sethdseed(True, seed.privkey)
+
+ if encrypt:
+ # Encrypting will generate a new HD seed and flush the keypool
+ test_wallet.encryptwallet("pass")
+ else:
+ # Generate a new HD seed on the test wallet
+ test_wallet.sethdseed()
+
+ return base_wallet, test_wallet
+
+ def do_inactive_test(self, base_wallet, test_wallet, encrypt=False):
+ default = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
+
+ # The first address should be known by both wallets.
+ addr1 = base_wallet.getnewaddress()
+ assert test_wallet.getaddressinfo(addr1)["ismine"]
+ # The address at index 9 is the first address that the test wallet will not know initially
+ for _ in range(0, 9):
+ base_wallet.getnewaddress()
+ addr2 = base_wallet.getnewaddress()
+ assert not test_wallet.getaddressinfo(addr2)["ismine"]
+
+ # Send to first address on the old seed
+ txid = default.sendtoaddress(addr1, 10)
+ self.generate(self.nodes[0], 1)
+
+ # Wait for the test wallet to see the transaction
+ while True:
+ try:
+ test_wallet.gettransaction(txid)
+ break
+ except JSONRPCException:
+ time.sleep(0.1)
+
+ if encrypt:
+ # The test wallet will not be able to generate the topped up keypool
+ # until it is unlocked. So it still should not know about the second address
+ assert not test_wallet.getaddressinfo(addr2)["ismine"]
+ test_wallet.walletpassphrase("pass", 1)
+
+ # The test wallet should now know about the second address as it
+ # should have generated it in the inactive chain's keypool
+ assert test_wallet.getaddressinfo(addr2)["ismine"]
+
+ # Send to second address on the old seed
+ txid = default.sendtoaddress(addr2, 10)
+ self.generate(self.nodes[0], 1)
+ test_wallet.gettransaction(txid)
+
+ def test_basic(self):
+ self.log.info("Test basic case for inactive HD chains")
+ self.do_inactive_test(*self.prepare_wallets("basic"))
+
+ def test_encrypted_wallet(self):
+ self.log.info("Test inactive HD chains when wallet is encrypted")
+ self.do_inactive_test(*self.prepare_wallets("enc", encrypt=True), encrypt=True)
+
+ def test_without_upgraded_keymeta(self):
+ # Test that it is possible to top up inactive hd chains even if there is no key origin
+ # in CKeyMetadata. This tests for the segfault reported in
+ # https://github.com/bitcoin/bitcoin/issues/21605
+ self.log.info("Test that topping up inactive HD chains does not need upgraded key origin")
+
+ self.nodes[0].createwallet(wallet_name="keymeta_base", descriptors=False, blank=True)
+ # Createwallet is overridden in the test framework so that the descriptor option can be filled
+ # depending on the test's cli args. However we don't want to do that when using old nodes that
+ # do not support descriptors. So we use the createwallet_passthrough function.
+ self.nodes[1].createwallet_passthrough(wallet_name="keymeta_test")
+ base_wallet = self.nodes[0].get_wallet_rpc("keymeta_base")
+ test_wallet = self.nodes[1].get_wallet_rpc("keymeta_test")
+
+ # Setup both wallets with the same HD seed
+ seed = get_generate_key()
+ base_wallet.sethdseed(True, seed.privkey)
+ test_wallet.sethdseed(True, seed.privkey)
+
+ # Encrypting will generate a new HD seed and flush the keypool
+ test_wallet.encryptwallet("pass")
+
+ # Copy test wallet to node 0
+ test_wallet.unloadwallet()
+ test_wallet_dir = os.path.join(self.nodes[1].datadir, "regtest/wallets/keymeta_test")
+ new_test_wallet_dir = os.path.join(self.nodes[0].datadir, "regtest/wallets/keymeta_test")
+ shutil.copytree(test_wallet_dir, new_test_wallet_dir)
+ self.nodes[0].loadwallet("keymeta_test")
+ test_wallet = self.nodes[0].get_wallet_rpc("keymeta_test")
+
+ self.do_inactive_test(base_wallet, test_wallet, encrypt=True)
+
+ def run_test(self):
+ self.generate(self.nodes[0], 101)
+
+ self.test_basic()
+ self.test_encrypted_wallet()
+ self.test_without_upgraded_keymeta()
+
+
+if __name__ == '__main__':
+ InactiveHDChainsTest().main()
diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py
index 17eab25457..d3731b135a 100755
--- a/test/functional/wallet_taproot.py
+++ b/test/functional/wallet_taproot.py
@@ -12,8 +12,11 @@ from test_framework.util import assert_equal
from test_framework.descriptors import descsum_create
from test_framework.script import (
CScript,
+ MAX_PUBKEYS_PER_MULTI_A,
OP_1,
OP_CHECKSIG,
+ OP_CHECKSIGADD,
+ OP_NUMEQUAL,
taproot_construct,
)
from test_framework.segwit_addr import encode_segwit_address
@@ -167,6 +170,17 @@ def pk(hex_key):
"""Construct a script expression for taproot_construct for pk(hex_key)."""
return (None, CScript([bytes.fromhex(hex_key), OP_CHECKSIG]))
+def multi_a(k, hex_keys, sort=False):
+ """Construct a script expression for taproot_construct for a multi_a script."""
+ xkeys = [bytes.fromhex(hex_key) for hex_key in hex_keys]
+ if sort:
+ xkeys.sort()
+ ops = [xkeys[0], OP_CHECKSIG]
+ for i in range(1, len(hex_keys)):
+ ops += [xkeys[i], OP_CHECKSIGADD]
+ ops += [k, OP_NUMEQUAL]
+ return (None, CScript(ops))
+
def compute_taproot_address(pubkey, scripts):
"""Compute the address for a taproot output with given inner key and scripts."""
tap = taproot_construct(pubkey, scripts)
@@ -194,19 +208,6 @@ class WalletTaprootTest(BitcoinTestFramework):
pass
@staticmethod
- def rand_keys(n):
- ret = []
- idxes = set()
- for _ in range(n):
- while True:
- i = random.randrange(len(KEYS))
- if not i in idxes:
- break
- idxes.add(i)
- ret.append(KEYS[i])
- return ret
-
- @staticmethod
def make_desc(pattern, privmap, keys, pub_only = False):
pat = pattern.replace("$H", H_POINT)
for i in range(len(privmap)):
@@ -275,7 +276,8 @@ class WalletTaprootTest(BitcoinTestFramework):
self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op)
test_balance = int(self.rpc_online.getbalance() * 100000000)
ret_amnt = random.randrange(100000, test_balance)
- res = self.rpc_online.sendtoaddress(address=self.boring.getnewaddress(), amount=Decimal(ret_amnt) / 100000000, subtractfeefromamount=True)
+ # Increase fee_rate to compensate for the wallet's inability to estimate fees for script path spends.
+ res = self.rpc_online.sendtoaddress(address=self.boring.getnewaddress(), amount=Decimal(ret_amnt) / 100000000, subtractfeefromamount=True, fee_rate=200)
self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op)
assert(self.rpc_online.gettransaction(res)["confirmations"] > 0)
@@ -306,7 +308,8 @@ class WalletTaprootTest(BitcoinTestFramework):
self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op)
test_balance = int(self.psbt_online.getbalance() * 100000000)
ret_amnt = random.randrange(100000, test_balance)
- psbt = self.psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0]})['psbt']
+ # Increase fee_rate to compensate for the wallet's inability to estimate fees for script path spends.
+ psbt = self.psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0], "fee_rate": 200})['psbt']
res = self.psbt_offline.walletprocesspsbt(psbt)
assert(res['complete'])
rawtx = self.nodes[0].finalizepsbt(res['psbt'])['hex']
@@ -314,8 +317,9 @@ class WalletTaprootTest(BitcoinTestFramework):
self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op)
assert(self.psbt_online.gettransaction(txid)['confirmations'] > 0)
- def do_test(self, comment, pattern, privmap, treefn, nkeys):
- keys = self.rand_keys(nkeys * 4)
+ def do_test(self, comment, pattern, privmap, treefn):
+ nkeys = len(privmap)
+ keys = random.sample(KEYS, nkeys * 4)
self.do_test_addr(comment, pattern, privmap, treefn, keys[0:nkeys])
self.do_test_sendtoaddress(comment, pattern, privmap, treefn, keys[0:nkeys], keys[nkeys:2*nkeys])
self.do_test_psbt(comment, pattern, privmap, treefn, keys[2*nkeys:3*nkeys], keys[3*nkeys:4*nkeys])
@@ -349,64 +353,98 @@ class WalletTaprootTest(BitcoinTestFramework):
"tr(XPRV)",
"tr($1/*)",
[True],
- lambda k1: (key(k1), []),
- 1
+ lambda k1: (key(k1), [])
)
self.do_test(
"tr(H,XPRV)",
"tr($H,pk($1/*))",
[True],
- lambda k1: (key(H_POINT), [pk(k1)]),
- 1
+ lambda k1: (key(H_POINT), [pk(k1)])
)
self.do_test(
"wpkh(XPRV)",
"wpkh($1/*)",
[True],
- None,
- 1
+ None
)
self.do_test(
"tr(XPRV,{H,{H,XPUB}})",
"tr($1/*,{pk($H),{pk($H),pk($2/*)}})",
[True, False],
- lambda k1, k2: (key(k1), [pk(H_POINT), [pk(H_POINT), pk(k2)]]),
- 2
+ lambda k1, k2: (key(k1), [pk(H_POINT), [pk(H_POINT), pk(k2)]])
)
self.do_test(
"wsh(multi(1,XPRV,XPUB))",
"wsh(multi(1,$1/*,$2/*))",
[True, False],
- None,
- 2
+ None
)
self.do_test(
"tr(XPRV,{XPUB,XPUB})",
"tr($1/*,{pk($2/*),pk($2/*)})",
[True, False],
- lambda k1, k2: (key(k1), [pk(k2), pk(k2)]),
- 2
+ lambda k1, k2: (key(k1), [pk(k2), pk(k2)])
)
self.do_test(
"tr(XPRV,{{XPUB,H},{H,XPUB}})",
"tr($1/*,{{pk($2/*),pk($H)},{pk($H),pk($2/*)}})",
[True, False],
- lambda k1, k2: (key(k1), [[pk(k2), pk(H_POINT)], [pk(H_POINT), pk(k2)]]),
- 2
+ lambda k1, k2: (key(k1), [[pk(k2), pk(H_POINT)], [pk(H_POINT), pk(k2)]])
)
self.do_test(
"tr(XPUB,{{H,{H,XPUB}},{H,{H,{H,XPRV}}}})",
"tr($1/*,{{pk($H),{pk($H),pk($2/*)}},{pk($H),{pk($H),{pk($H),pk($3/*)}}}})",
[False, False, True],
- lambda k1, k2, k3: (key(k1), [[pk(H_POINT), [pk(H_POINT), pk(k2)]], [pk(H_POINT), [pk(H_POINT), [pk(H_POINT), pk(k3)]]]]),
- 3
+ lambda k1, k2, k3: (key(k1), [[pk(H_POINT), [pk(H_POINT), pk(k2)]], [pk(H_POINT), [pk(H_POINT), [pk(H_POINT), pk(k3)]]]])
)
self.do_test(
"tr(XPRV,{XPUB,{{XPUB,{H,H}},{{H,H},XPUB}}})",
"tr($1/*,{pk($2/*),{{pk($2/*),{pk($H),pk($H)}},{{pk($H),pk($H)},pk($2/*)}}})",
[True, False],
- lambda k1, k2: (key(k1), [pk(k2), [[pk(k2), [pk(H_POINT), pk(H_POINT)]], [[pk(H_POINT), pk(H_POINT)], pk(k2)]]]),
- 2
+ lambda k1, k2: (key(k1), [pk(k2), [[pk(k2), [pk(H_POINT), pk(H_POINT)]], [[pk(H_POINT), pk(H_POINT)], pk(k2)]]])
+ )
+ self.do_test(
+ "tr(H,multi_a(1,XPRV))",
+ "tr($H,multi_a(1,$1/*))",
+ [True],
+ lambda k1: (key(H_POINT), [multi_a(1, [k1])])
+ )
+ self.do_test(
+ "tr(H,sortedmulti_a(1,XPRV,XPUB))",
+ "tr($H,sortedmulti_a(1,$1/*,$2/*))",
+ [True, False],
+ lambda k1, k2: (key(H_POINT), [multi_a(1, [k1, k2], True)])
+ )
+ self.do_test(
+ "tr(H,{H,multi_a(1,XPUB,XPRV)})",
+ "tr($H,{pk($H),multi_a(1,$1/*,$2/*)})",
+ [False, True],
+ lambda k1, k2: (key(H_POINT), [pk(H_POINT), [multi_a(1, [k1, k2])]])
+ )
+ self.do_test(
+ "tr(H,sortedmulti_a(1,XPUB,XPRV,XPRV))",
+ "tr($H,sortedmulti_a(1,$1/*,$2/*,$3/*))",
+ [False, True, True],
+ lambda k1, k2, k3: (key(H_POINT), [multi_a(1, [k1, k2, k3], True)])
+ )
+ self.do_test(
+ "tr(H,multi_a(2,XPRV,XPUB,XPRV))",
+ "tr($H,multi_a(2,$1/*,$2/*,$3/*))",
+ [True, False, True],
+ lambda k1, k2, k3: (key(H_POINT), [multi_a(2, [k1, k2, k3])])
+ )
+ self.do_test(
+ "tr(XPUB,{{XPUB,{XPUB,sortedmulti_a(2,XPRV,XPUB,XPRV)}})",
+ "tr($2/*,{pk($2/*),{pk($2/*),sortedmulti_a(2,$1/*,$2/*,$3/*)}})",
+ [True, False, True],
+ lambda k1, k2, k3: (key(k2), [pk(k2), [pk(k2), multi_a(2, [k1, k2, k3], True)]])
+ )
+ rnd_pos = random.randrange(MAX_PUBKEYS_PER_MULTI_A)
+ self.do_test(
+ "tr(XPUB,multi_a(1,H...,XPRV,H...))",
+ "tr($2/*,multi_a(1" + (",$H" * rnd_pos) + ",$1/*" + (",$H" * (MAX_PUBKEYS_PER_MULTI_A - 1 - rnd_pos)) + "))",
+ [True, False],
+ lambda k1, k2: (key(k2), [multi_a(1, ([H_POINT] * rnd_pos) + [k1] + ([H_POINT] * (MAX_PUBKEYS_PER_MULTI_A - 1 - rnd_pos)))])
)
self.log.info("Sending everything back...")
diff --git a/test/sanitizer_suppressions/ubsan b/test/sanitizer_suppressions/ubsan
index b06dd253be..e6cfe5f81a 100644
--- a/test/sanitizer_suppressions/ubsan
+++ b/test/sanitizer_suppressions/ubsan
@@ -62,7 +62,6 @@ implicit-integer-sign-change:script/bitcoinconsensus.cpp
implicit-integer-sign-change:script/interpreter.cpp
implicit-integer-sign-change:serialize.h
implicit-integer-sign-change:txmempool.cpp
-implicit-signed-integer-truncation:addrman.cpp
implicit-signed-integer-truncation:crypto/
implicit-unsigned-integer-truncation:crypto/
shift-base:arith_uint256.cpp