diff --git a/doc/dependencies.md b/doc/dependencies.md index eb802f558..0ca5112e1 100644 --- a/doc/dependencies.md +++ b/doc/dependencies.md @@ -1,50 +1,50 @@ Dependencies ============ These are the dependencies currently used by Bitcoin ABC. You can find instructions for installing them in the [`build-*.md`](../INSTALL.md) file for your platform. | Dependency | Version used | Minimum required | CVEs | Shared | [Bundled Qt library](https://doc.qt.io/qt-5/configure-options.html) | | --- | --- | --- | --- | --- | --- | | Berkeley DB | [5.3.28](http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html) | 5.3 | No | | | | Boost | [1.81.0](https://www.boost.org/users/download/) | 1.64.0 | No | | | | Clang | | [5](https://releases.llvm.org/download.html) (C++17 support) | | | | | CMake | | [3.16](https://cmake.org/download/) | | | | | fontconfig | [2.12.6](https://www.freedesktop.org/software/fontconfig/release/) | | No | Yes | | | FreeType | [2.11.0](http://download.savannah.gnu.org/releases/freetype) | | No | | | | GCC | | [8.3](https://gcc.gnu.org/) | | | | | HarfBuzz-NG | | | | | | | jemalloc | [5.2.1](https://github.com/jemalloc/jemalloc/releases) | 3.6.0 | | | | -| libevent | [2.1.12-stable](https://github.com/libevent/libevent/releases) | 2.0.22 | No | | | +| libevent | [2.1.12-stable](https://github.com/libevent/libevent/releases) | 2.1.8 | No | | | | libnatpmp | commit [07004b9...](https://github.com/miniupnp/libnatpmp/commit/07004b97cf691774efebe70404cf22201e4d330d) | | No | | | | libpng | | | | | Yes | | librsvg | | | | | | | MiniUPnPc | [2.2.7](https://miniupnp.tuxfamily.org/files) | 1.9 | No | | | | Ninja | | [1.5.1](https://github.com/ninja-build/ninja/releases) | | | | | OpenSSL | [1.0.1k](https://www.openssl.org/source) | | Yes | | | | PCRE | | | | | Yes | | protobuf | [21.12](https://github.com/protocolbuffers/protobuf/releases/tag/v21.12) | | No | | | | Python (tests) | | [3.9](https://www.python.org/downloads) | | | | | qrencode | [3.4.4](https://fukuchi.org/works/qrencode) | | No | | | | Qt | [5.15.14](https://download.qt.io/official_releases/qt/) | 5.9.5 | No | | | | SQLite | [3.32.1](https://sqlite.org/download.html) | 3.7.17 | | | | | systemtap ([tracing](tracing.md))| | | | | | | XCB | | | | | Yes (Linux only) | | xkbcommon | | | | | Yes (Linux only) | | ZeroMQ | [4.3.1](https://github.com/zeromq/libzmq/releases) | 4.1.5 | No | | | | zlib | [1.2.11](http://zlib.net/) | | | | No | Controlling dependencies ------------------------ Some dependencies are not needed in all configurations. The following are some factors that affect the dependency list. #### Options passed to `cmake` * MiniUPnPc is not needed with `-DENABLE_UPNP=OFF`. * MiniUPnPc is not needed with `-DENABLE_NATPMP=OFF`. * Berkeley DB and SQLite are not needed with `-DBUILD_BITCOIN_WALLET=OFF`. * OpenSSL is not needed with `-DENABLE_BIP70=OFF`. * protobuf is not needed with `-DENABLE_BIP70=OFF`. * Qt is not needed with `-DBUILD_BITCOIN_QT=OFF`. * qrencode is not needed with `-DENABLE_QRCODE=OFF`. * systemtap is not needed with `-DENABLE_TRACING=OFF`. * ZeroMQ is not needed with the `-DBUILD_BITCOIN_ZMQ=OFF`. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69000b66c..33d930717 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,1043 +1,1043 @@ # Copyright (c) 2017 The Bitcoin developers project(bitcoind) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Default visibility is hidden on all targets. set(CMAKE_C_VISIBILITY_PRESET hidden) set(CMAKE_CXX_VISIBILITY_PRESET hidden) option(BUILD_BITCOIN_WALLET "Activate the wallet functionality" ON) option(BUILD_BITCOIN_ZMQ "Activate the ZeroMQ functionalities" ON) option(BUILD_BITCOIN_CLI "Build bitcoin-cli" ON) option(BUILD_BITCOIN_TX "Build bitcoin-tx" ON) option(BUILD_BITCOIN_QT "Build bitcoin-qt" ON) option(BUILD_BITCOIN_SEEDER "Build bitcoin-seeder" ON) option(BUILD_LIBBITCOINCONSENSUS "Build the bitcoinconsenus shared library" ON) option(BUILD_BITCOIN_CHAINSTATE "Build bitcoin-chainstate" OFF) option(BUILD_BITCOIN_IGUANA "Activate the Iguana debugger" ON) option(ENABLE_BIP70 "Enable BIP70 (payment protocol) support in GUI" ON) option(ENABLE_HARDENING "Harden the executables" ON) option(ENABLE_REDUCE_EXPORTS "Reduce the amount of exported symbols" OFF) option(ENABLE_STATIC_LIBSTDCXX "Statically link libstdc++" OFF) option(ENABLE_GLIBC_BACK_COMPAT "Enable Glibc compatibility features" OFF) option(ENABLE_QRCODE "Enable QR code display" ON) option(ENABLE_UPNP "Enable UPnP support" ON) option(START_WITH_UPNP "Make UPnP the default to map ports" OFF) option(ENABLE_NATPMP "Enable NAT-PMP support" ON) option(START_WITH_NATPMP "Make NAT-PMP the default to map ports" OFF) option(ENABLE_CLANG_TIDY "Enable clang-tidy checks for Bitcoin ABC" OFF) option(ENABLE_PROFILING "Select the profiling tool to use" OFF) option(ENABLE_TRACING "Enable eBPF user static defined tracepoints" OFF) # Linker option if(CMAKE_CROSSCOMPILING) set(DEFAULT_LINKER "") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(DEFAULT_LINKER lld) elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(DEFAULT_LINKER gold) else() set(DEFAULT_LINKER "") endif() set(USE_LINKER "${DEFAULT_LINKER}" CACHE STRING "Linker to be used (default: ${DEFAULT_LINKER}). Set to empty string to use the system's default.") set(OS_WITH_JEMALLOC_AS_SYSTEM_DEFAULT "Android" "FreeBSD" "NetBSD" ) if(NOT CMAKE_SYSTEM_NAME IN_LIST OS_WITH_JEMALLOC_AS_SYSTEM_DEFAULT) set(USE_JEMALLOC_DEFAULT ON) endif() # FIXME: Building against jemalloc causes the software to segfault on OSX. # See https://github.com/Bitcoin-ABC/bitcoin-abc/issues/401 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT CMAKE_CROSSCOMPILING) set(USE_JEMALLOC_DEFAULT OFF) endif() option(USE_JEMALLOC "Use jemalloc as an allocation library" ${USE_JEMALLOC_DEFAULT}) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(DEFAULT_ENABLE_DBUS_NOTIFICATIONS ON) endif() option(ENABLE_DBUS_NOTIFICATIONS "Enable DBus desktop notifications. Linux only." ${DEFAULT_ENABLE_DBUS_NOTIFICATIONS}) # If ccache is available, then use it. find_program(CCACHE ccache) if(CCACHE) message(STATUS "Using ccache: ${CCACHE}") set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE}) set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE}) endif(CCACHE) # Disable what we do not need for the native build. include(NativeExecutable) native_add_cmake_flags( "-DBUILD_BITCOIN_WALLET=OFF" "-DBUILD_BITCOIN_CHRONIK=OFF" "-DBUILD_BITCOIN_QT=OFF" "-DBUILD_BITCOIN_ZMQ=OFF" "-DENABLE_QRCODE=OFF" "-DENABLE_NATPMP=OFF" "-DENABLE_UPNP=OFF" "-DUSE_JEMALLOC=OFF" "-DENABLE_CLANG_TIDY=OFF" "-DENABLE_BIP70=OFF" "-DUSE_LINKER=" ) if(ENABLE_CLANG_TIDY) include(ClangTidy) endif() if(ENABLE_SANITIZERS) include(Sanitizers) enable_sanitizers(${ENABLE_SANITIZERS}) endif() include(AddCompilerFlags) if(USE_LINKER) set(LINKER_FLAG "-fuse-ld=${USE_LINKER}") custom_check_linker_flag(IS_LINKER_SUPPORTED ${LINKER_FLAG}) if(NOT IS_LINKER_SUPPORTED) message(FATAL_ERROR "The ${USE_LINKER} linker is not supported, make sure ${USE_LINKER} is properly installed or use -DUSE_LINKER= to use the system's linker") endif() add_linker_flags(${LINKER_FLAG}) # Remember the selected linker, it will be used for the subsequent # custom_check_linker_flag calls set(GLOBAL_LINKER_FLAGS ${LINKER_FLAG} CACHE INTERNAL "Additional linker flags for flag support checking") endif() # Prefer -g3, defaults to -g if unavailable foreach(LANGUAGE C CXX) set(COMPILER_DEBUG_LEVEL -g) check_compiler_flags(G3_IS_SUPPORTED ${LANGUAGE} -g3) if(${G3_IS_SUPPORTED}) set(COMPILER_DEBUG_LEVEL -g3) endif() add_compile_options_to_configuration_for_language(Debug ${LANGUAGE} ${COMPILER_DEBUG_LEVEL}) endforeach() # Define some debugging symbols when the Debug build type is selected. add_compile_definitions_to_configuration(Debug DEBUG DEBUG_LOCKORDER ABORT_ON_FAILED_ASSUME) # Add -ftrapv when building in Debug add_compile_options_to_configuration(Debug -ftrapv) # All versions of gcc that we commonly use for building are subject to bug # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348. To work around that, set # -fstack-reuse=none for all gcc builds. (Only gcc understands this flag) if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_compiler_flags(-fstack-reuse=none) endif() if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Ensure that WINDRES_PREPROC is enabled when using windres. list(APPEND CMAKE_RC_FLAGS "-DWINDRES_PREPROC") # Build all static so there is no dll file to distribute. add_linker_flags(-static) add_compile_definitions( # Windows 7 _WIN32_WINNT=0x0601 # Internet Explorer 5.01 (!) _WIN32_IE=0x0501 # Define WIN32_LEAN_AND_MEAN to exclude APIs such as Cryptography, DDE, # RPC, Shell, and Windows Sockets. WIN32_LEAN_AND_MEAN ) # We require Windows 7 (NT 6.1) or later add_linker_flags(-Wl,--major-subsystem-version,6 -Wl,--minor-subsystem-version,1) endif() if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") add_compile_definitions(MAC_OSX OBJC_OLD_DISPATCH_PROTOTYPES=0) add_linker_flags(-Wl,-dead_strip_dylibs) endif() if(ENABLE_REDUCE_EXPORTS) # Default visibility is set by CMAKE_<LANG>_VISIBILITY_PRESET, but this # doesn't tell if the visibility set is effective. # Check if the flag -fvisibility=hidden is supported, as using the hidden # visibility is a requirement to reduce exports. check_compiler_flags(HAS_CXX_FVISIBILITY CXX -fvisibility=hidden) if(NOT HAS_CXX_FVISIBILITY) message(FATAL_ERROR "Cannot set default symbol visibility. Use -DENABLE_REDUCE_EXPORTS=OFF.") endif() # Also hide symbols from static libraries add_linker_flags(-Wl,--exclude-libs,libstdc++) endif() # Enable statically linking libstdc++ if(ENABLE_STATIC_LIBSTDCXX) add_linker_flags(-static-libstdc++) endif() set(CMAKE_POSITION_INDEPENDENT_CODE ON) if(ENABLE_HARDENING) # Enable stack protection add_cxx_compiler_flags(-fstack-protector-all -Wstack-protector) # Enable control-flow enforcement add_cxx_compiler_flags(-fcf-protection=full) if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") # stack-clash-protection does not work properly when building for Windows. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 add_cxx_compiler_flags(-fstack-clash-protection) endif() add_linker_flags(-Wl,-z,noexecstack) # Enable some buffer overflow checking, except in -O0 builds which # do not support them add_compiler_flags(-U_FORTIFY_SOURCE) add_compile_options($<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>) # Enable ASLR (these flags are primarily targeting MinGw) add_linker_flags(-Wl,--enable-reloc-section -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va) # Make the relocated sections read-only add_linker_flags(-Wl,-z,relro -Wl,-z,now) # Avoids mixing code pages with data to improve cache performance as well # as security add_linker_flags(-Wl,-z,separate-code) # CMake provides the POSITION_INDEPENDENT_CODE property to set PIC/PIE. cmake_policy(SET CMP0083 NEW) include(CheckPIESupported) check_pie_supported() if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # MinGw provides its own libssp for stack smashing protection. # It might not be needed for recent versions of mingw, but the version # shipped with debian bullseye needs it and we still support it at the # time of writing. link_libraries(ssp) endif() if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") add_linker_flags(-Wl,-fixup_chains) endif() endif() if(ENABLE_PROFILING MATCHES "gprof") message(STATUS "Enable profiling with gprof") # -pg is incompatible with -pie. Since hardening and profiling together # doesn't make sense, we simply make them mutually exclusive here. # Additionally, hardened toolchains may force -pie by default, in which # case it needs to be turned off with -no-pie. if(ENABLE_HARDENING) message(FATAL_ERROR "Profiling with gprof requires disabling hardening with -DENABLE_HARDENING=OFF.") endif() add_linker_flags(-no-pie) add_compiler_flags(-pg) add_linker_flags(-pg) endif() # Enable warning add_c_compiler_flags(-Wnested-externs -Wstrict-prototypes) add_compiler_flags( -Wall -Wextra -Wformat -Wgnu -Wvla -Wcast-align -Wunused-parameter -Wmissing-braces -Wthread-safety -Wrange-loop-analysis -Wredundant-decls -Wunreachable-code-loop-increment -Wsign-compare -Wconditional-uninitialized -Wduplicated-branches -Wduplicated-cond -Wlogical-op -Wdocumentation ) add_compiler_flag_group(-Wformat -Wformat-security) add_cxx_compiler_flags( -Wredundant-move -Woverloaded-virtual ) if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # GCC has no flag variant which is granular enough to avoid raising the clang # -Wshadow-uncaptured-local equivalent. This is causing a lot of warnings # on serialize.h which cannot be disabled locally, so drop the flag. add_compiler_flags( -Wshadow -Wshadow-field ) endif() option(EXTRA_WARNINGS "Enable extra warnings" OFF) if(EXTRA_WARNINGS) add_cxx_compiler_flags(-Wsuggest-override) else() add_compiler_flags( -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-psabi ) endif() # libtool style configure add_subdirectory(config) # Enable LFS (Large File Support) on targets that don't have it natively. # This should be defined before the libraries are included as leveldb need the # definition to be set. if(NOT HAVE_LARGE_FILE_SUPPORT) add_compile_definitions(_FILE_OFFSET_BITS=64) add_linker_flags(-Wl,--large-address-aware) endif() if(ENABLE_GLIBC_BACK_COMPAT) # Wrap some glibc functions with ours add_linker_flags(-Wl,--wrap=exp) add_linker_flags(-Wl,--wrap=log) add_linker_flags(-Wl,--wrap=log2) add_linker_flags(-Wl,--wrap=pow) if(NOT HAVE_LARGE_FILE_SUPPORT) add_linker_flags(-Wl,--wrap=fcntl -Wl,--wrap=fcntl64) endif() endif() if(USE_JEMALLOC) # Most of the sanitizers require their instrumented allocation functions to # be fully functional. This is obviously the case for all the memory related # sanitizers (asan, lsan, msan) but not only. if(ENABLE_SANITIZERS) message(WARNING "Jemalloc is incompatible with the sanitizers and has been disabled.") else() find_package(Jemalloc 3.6.0 REQUIRED) link_libraries(Jemalloc::jemalloc) endif() endif() # These flags are needed for using std::filesystem with GCC < 9.1 & Clang < 9.0 # Since these are optional libraries they need to be placed accordingly on the # command line. add_linker_flags(-lstdc++fs -lc++fs) custom_check_linker_flag(LINKER_HAS_STDCXXFS "-lstdc++fs") if(LINKER_HAS_STDCXXFS) link_libraries(stdc++fs) endif() custom_check_linker_flag(LINKER_HAS_CXXFS "-lc++fs") if(LINKER_HAS_CXXFS) link_libraries(c++fs) endif() # Make sure that all the global compiler and linker flags are set BEFORE # including the libraries so they apply as needed. # libraries add_subdirectory(crypto) add_subdirectory(leveldb) add_subdirectory(secp256k1) add_subdirectory(univalue) # Find the git root, and returns the full path to the .git/logs/HEAD file if # it exists. function(find_git_head_logs_file RESULT) find_package(Git) if(GIT_FOUND) execute_process( COMMAND "${GIT_EXECUTABLE}" "rev-parse" "--show-toplevel" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE GIT_ROOT RESULT_VARIABLE GIT_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) if(GIT_RESULT EQUAL 0) set(GIT_LOGS_DIR "${GIT_ROOT}/.git/logs") set(GIT_HEAD_LOGS_FILE "${GIT_LOGS_DIR}/HEAD") # If the .git/logs/HEAD does not exist, create it if(NOT EXISTS "${GIT_HEAD_LOGS_FILE}") file(MAKE_DIRECTORY "${GIT_LOGS_DIR}") file(TOUCH "${GIT_HEAD_LOGS_FILE}") endif() set(${RESULT} "${GIT_HEAD_LOGS_FILE}" PARENT_SCOPE) endif() endif() endfunction() find_git_head_logs_file(GIT_HEAD_LOGS_FILE) set(OBJ_DIR "${CMAKE_CURRENT_BINARY_DIR}/obj") file(MAKE_DIRECTORY "${OBJ_DIR}") set(BUILD_HEADER "${OBJ_DIR}/build.h") set(BUILD_HEADER_TMP "${BUILD_HEADER}.tmp") add_custom_command( DEPENDS "${GIT_HEAD_LOGS_FILE}" "${CMAKE_SOURCE_DIR}/share/genbuild.sh" OUTPUT "${BUILD_HEADER}" COMMAND "${CMAKE_SOURCE_DIR}/share/genbuild.sh" "${BUILD_HEADER_TMP}" "${CMAKE_SOURCE_DIR}" COMMAND ${CMAKE_COMMAND} -E copy_if_different "${BUILD_HEADER_TMP}" "${BUILD_HEADER}" COMMAND ${CMAKE_COMMAND} -E remove "${BUILD_HEADER_TMP}" ) # Because the Bitcoin ABc source code is disorganised, we # end up with a bunch of libraries without any apparent # cohesive structure. This is inherited from Bitcoin Core # and reflecting this. # TODO: Improve the structure once cmake is rocking. # Various completely unrelated features shared by all executables. add_library(util chainparamsbase.cpp clientversion.cpp compat/glibcxx_sanity.cpp compat/strnlen.cpp currencyunit.cpp interfaces/handler.cpp logging.cpp random.cpp randomenv.cpp rcu.cpp rpc/request.cpp support/cleanse.cpp support/lockedpool.cpp sync.cpp threadinterrupt.cpp uint256.cpp util/asmap.cpp util/batchpriority.cpp util/bip32.cpp util/bytevectorhash.cpp util/check.cpp util/hasher.cpp util/exception.cpp util/error.cpp util/fs.cpp util/fs_helpers.cpp util/getuniquepath.cpp util/message.cpp util/moneystr.cpp util/readwritefile.cpp util/settings.cpp util/string.cpp util/sock.cpp util/spanparsing.cpp util/strencodings.cpp util/string.cpp util/syserror.cpp util/thread.cpp util/threadnames.cpp util/time.cpp util/tokenpipe.cpp util/url.cpp # obj/build.h "${BUILD_HEADER}" ) target_compile_definitions(util PUBLIC HAVE_CONFIG_H HAVE_BUILD_INFO) target_include_directories(util PUBLIC . # To access the config/ and obj/ directories ${CMAKE_CURRENT_BINARY_DIR} ) if(ENABLE_GLIBC_BACK_COMPAT) target_sources(util PRIVATE compat/glibc_compat.cpp) endif() # Work around jemalloc printing harmless warnings to stderr with qemu. # We match the emulator against qemu but not ^qemu so we are compatible with the # use of absolute paths. if(CMAKE_CROSSCOMPILING AND USE_JEMALLOC AND CMAKE_CROSSCOMPILING_EMULATOR MATCHES "qemu-.+") message(WARNING "Overriding Jemalloc malloc_message() function for use with QEMU.") target_sources(util PRIVATE jemalloc_message.cpp) endif() set(Boost_USE_STATIC_LIBS ON) macro(link_windows_dependencies TARGET) find_package(SHLWAPI REQUIRED) target_link_libraries(${TARGET} SHLWAPI::shlwapi) find_library(WS2_32_LIBRARY NAMES ws2_32) target_link_libraries(${TARGET} ${WS2_32_LIBRARY}) endmacro() # Target specific configs if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(Boost_USE_STATIC_RUNTIME ON) set(Boost_THREADAPI win32) link_windows_dependencies(util) endif() target_link_libraries(util univalue crypto) macro(link_event TARGET) - non_native_target_link_libraries(${TARGET} Event 2.0.22 ${ARGN}) + non_native_target_link_libraries(${TARGET} Event 2.1.8 ${ARGN}) endmacro() link_event(util event) macro(link_boost_headers_only TARGET) non_native_target_link_headers_only(${TARGET} Boost 1.64 ${ARGN}) endmacro() link_boost_headers_only(util headers) target_compile_definitions(util PUBLIC BOOST_NO_CXX98_FUNCTION_BASE) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) target_link_libraries(util Threads::Threads) function(add_network_sources NETWORK_SOURCES) set(NETWORK_DIR abc) list(TRANSFORM ARGN PREPEND "networks/${NETWORK_DIR}/" OUTPUT_VARIABLE NETWORK_SOURCES ) set(NETWORK_SOURCES ${NETWORK_SOURCES} PARENT_SCOPE) endfunction() add_network_sources(NETWORK_SOURCES checkpoints.cpp chainparamsconstants.cpp ) # More completely unrelated features shared by all executables. # Because nothing says this is different from util than "common" add_library(common base58.cpp common/args.cpp common/bloom.cpp common/configfile.cpp common/system.cpp cashaddr.cpp cashaddrenc.cpp chainparams.cpp config.cpp consensus/merkle.cpp coins.cpp compressor.cpp eventloop.cpp feerate.cpp core_read.cpp core_write.cpp kernel/chainparams.cpp key.cpp key_io.cpp merkleblock.cpp net_permissions.cpp netaddress.cpp netbase.cpp outputtype.cpp policy/policy.cpp primitives/block.cpp protocol.cpp psbt.cpp rpc/rawtransaction_util.cpp rpc/util.cpp scheduler.cpp warnings.cpp ${NETWORK_SOURCES} ) target_link_libraries(common bitcoinconsensus util secp256k1 script) # script library add_library(script script/bitfield.cpp script/descriptor.cpp script/interpreter.cpp script/intmath.cpp script/script.cpp script/script_error.cpp script/sigencoding.cpp script/sign.cpp script/signingprovider.cpp script/standard.cpp ) target_link_libraries(script common) # libbitcoinconsensus add_library(bitcoinconsensus arith_uint256.cpp hash.cpp primitives/transaction.cpp pubkey.cpp uint256.cpp util/strencodings.cpp consensus/amount.cpp consensus/tx_check.cpp ) target_link_libraries(bitcoinconsensus script) include(InstallationHelper) if(BUILD_LIBBITCOINCONSENSUS) target_compile_definitions(bitcoinconsensus PUBLIC BUILD_BITCOIN_INTERNAL HAVE_CONSENSUS_LIB ) install_shared_library(bitcoinconsensus script/bitcoinconsensus.cpp PUBLIC_HEADER script/bitcoinconsensus.h ) endif() # Bitcoin server facilities add_library(server addrdb.cpp addrman.cpp avalanche/compactproofs.cpp avalanche/delegation.cpp avalanche/delegationbuilder.cpp avalanche/peermanager.cpp avalanche/processor.cpp avalanche/proof.cpp avalanche/proofid.cpp avalanche/proofbuilder.cpp avalanche/proofpool.cpp avalanche/stakecontendercache.cpp avalanche/voterecord.cpp banman.cpp blockencodings.cpp blockfileinfo.cpp blockfilter.cpp blockindex.cpp chain.cpp checkpoints.cpp config.cpp consensus/activation.cpp consensus/tx_verify.cpp dbwrapper.cpp deploymentstatus.cpp dnsseeds.cpp flatfile.cpp headerssync.cpp httprpc.cpp httpserver.cpp i2p.cpp index/base.cpp index/blockfilterindex.cpp index/coinstatsindex.cpp index/txindex.cpp init.cpp init/common.cpp invrequest.cpp kernel/coinstats.cpp kernel/cs_main.cpp kernel/disconnected_transactions.cpp kernel/mempool_persist.cpp mapport.cpp mempool_args.cpp minerfund.cpp net.cpp net_processing.cpp node/blockmanager_args.cpp node/blockstorage.cpp node/caches.cpp node/chainstate.cpp node/chainstatemanager_args.cpp node/coin.cpp node/coinstats.cpp node/coins_view_args.cpp node/context.cpp node/database_args.cpp node/interfaces.cpp node/kernel_notifications.cpp node/mempool_persist_args.cpp node/miner.cpp node/peerman_args.cpp node/psbt.cpp node/transaction.cpp node/ui_interface.cpp node/utxo_snapshot.cpp node/validation_cache_args.cpp noui.cpp policy/block/minerfund.cpp policy/block/preconsensus.cpp policy/block/rtt.cpp policy/block/stakingrewards.cpp policy/fees.cpp policy/packages.cpp policy/settings.cpp pow/aserti32d.cpp pow/daa.cpp pow/eda.cpp pow/grasberg.cpp pow/pow.cpp rest.cpp rpc/abc.cpp rpc/avalanche.cpp rpc/blockchain.cpp rpc/command.cpp rpc/mempool.cpp rpc/mining.cpp rpc/misc.cpp rpc/net.cpp rpc/rawtransaction.cpp rpc/server.cpp rpc/server_util.cpp rpc/txoutproof.cpp script/scriptcache.cpp script/sigcache.cpp shutdown.cpp timedata.cpp torcontrol.cpp txdb.cpp txmempool.cpp txpool.cpp validation.cpp validationinterface.cpp versionbits.cpp ) target_include_directories(server PRIVATE leveldb/helpers/memenv) target_link_libraries(server bitcoinconsensus leveldb memenv ) link_event(server event) if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") link_event(server pthreads) endif() if(ENABLE_UPNP) find_package(MiniUPnPc 1.9 REQUIRED) target_link_libraries(server MiniUPnPc::miniupnpc) if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # TODO: check if we are really using a static library. Assume this is # the one from the depends for now since the native windows build is not # supported. target_compile_definitions(server PUBLIC -DSTATICLIB PUBLIC -DMINIUPNP_STATICLIB ) endif() endif() if(ENABLE_NATPMP) find_package(NATPMP REQUIRED) target_link_libraries(server NATPMP::natpmp) if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") target_compile_definitions(server PUBLIC -DSTATICLIB PUBLIC -DNATPMP_STATICLIB ) endif() endif() # Test suites. add_subdirectory(test) add_subdirectory(avalanche/test) add_subdirectory(pow/test) # Benchmark suite. add_subdirectory(bench) include(BinaryTest) include(WindowsVersionInfo) # Wallet if(BUILD_BITCOIN_WALLET) add_subdirectory(wallet) target_link_libraries(server wallet) # bitcoin-wallet add_executable(bitcoin-wallet bitcoin-wallet.cpp) generate_windows_version_info(bitcoin-wallet DESCRIPTION "CLI tool for ${PACKAGE_NAME} wallets" ) target_link_libraries(bitcoin-wallet wallet-tool common util) add_to_symbols_check(bitcoin-wallet) add_to_security_check(bitcoin-wallet) install_target(bitcoin-wallet) install_manpages(bitcoin-wallet) else() target_sources(server PRIVATE dummywallet.cpp) endif() # ZeroMQ if(BUILD_BITCOIN_ZMQ) add_subdirectory(zmq) target_link_libraries(server zmq) # FIXME: This is needed because of an unwanted dependency: # zmqpublishnotifier.cpp -> blockstorage.h -> txdb.h -> dbwrapper.h -> leveldb/db.h target_link_libraries(zmq leveldb) endif() # RPC client support add_library(rpcclient compat/stdin.cpp rpc/client.cpp ) target_link_libraries(rpcclient univalue util) # bitcoin-seeder if(BUILD_BITCOIN_SEEDER) add_subdirectory(seeder) endif() # bitcoin-cli if(BUILD_BITCOIN_CLI) add_executable(bitcoin-cli bitcoin-cli.cpp) generate_windows_version_info(bitcoin-cli DESCRIPTION "JSON-RPC client for ${PACKAGE_NAME}" ) target_link_libraries(bitcoin-cli common rpcclient) link_event(bitcoin-cli event) add_to_symbols_check(bitcoin-cli) add_to_security_check(bitcoin-cli) install_target(bitcoin-cli) install_manpages(bitcoin-cli) endif() # bitcoin-tx if(BUILD_BITCOIN_TX) add_executable(bitcoin-tx bitcoin-tx.cpp) generate_windows_version_info(bitcoin-tx DESCRIPTION "CLI Bitcoin transaction editor utility" ) target_link_libraries(bitcoin-tx bitcoinconsensus) add_to_symbols_check(bitcoin-tx) add_to_security_check(bitcoin-tx) install_target(bitcoin-tx) install_manpages(bitcoin-tx) endif() # bitcoin-chainstate if(BUILD_BITCOIN_CHAINSTATE) # TODO: libbitcoinkernel is a work in progress consensus engine library, as more # and more modules are decoupled from the consensus engine, this list will # shrink to only those which are absolutely necessary. For example, things # like index/*.cpp will be removed. add_library(bitcoinkernel kernel/bitcoinkernel.cpp kernel/chainparams.cpp kernel/coinstats.cpp kernel/cs_main.cpp kernel/disconnected_transactions.cpp kernel/mempool_persist.cpp arith_uint256.cpp blockfileinfo.cpp blockindex.cpp common/args.cpp common/bloom.cpp common/configfile.cpp chain.cpp chainparamsbase.cpp chainparams.cpp checkpoints.cpp clientversion.cpp coins.cpp compat/glibcxx_sanity.cpp compressor.cpp config.cpp consensus/activation.cpp consensus/amount.cpp consensus/merkle.cpp consensus/tx_check.cpp consensus/tx_verify.cpp core_read.cpp dbwrapper.cpp deploymentstatus.cpp eventloop.cpp feerate.cpp flatfile.cpp hash.cpp init/common.cpp key.cpp logging.cpp networks/abc/chainparamsconstants.cpp networks/abc/checkpoints.cpp node/blockstorage.cpp node/chainstate.cpp node/ui_interface.cpp node/utxo_snapshot.cpp policy/fees.cpp policy/packages.cpp policy/policy.cpp policy/settings.cpp pow/aserti32d.cpp pow/daa.cpp pow/eda.cpp pow/pow.cpp primitives/block.cpp primitives/transaction.cpp pubkey.cpp random.cpp randomenv.cpp rcu.cpp scheduler.cpp script/bitfield.cpp script/interpreter.cpp script/script.cpp script/scriptcache.cpp script/script_error.cpp script/sigcache.cpp script/sigencoding.cpp script/standard.cpp shutdown.cpp support/cleanse.cpp support/lockedpool.cpp sync.cpp threadinterrupt.cpp txdb.cpp txmempool.cpp txpool.cpp uint256.cpp util/batchpriority.cpp util/bytevectorhash.cpp util/check.cpp util/exception.cpp util/fs.cpp util/fs_helpers.cpp util/getuniquepath.cpp util/hasher.cpp util/moneystr.cpp util/settings.cpp util/strencodings.cpp util/string.cpp util/syserror.cpp util/thread.cpp util/threadnames.cpp util/time.cpp util/tokenpipe.cpp validation.cpp validationinterface.cpp versionbits.cpp warnings.cpp # Bitcoin ABC specific dependencies that will not go away with Core backports addrdb.cpp # via banman.cpp addrman.cpp # via net.cpp banman.cpp # via net.cpp base58.cpp # via key_io.cpp avalanche/delegation.cpp avalanche/delegationbuilder.cpp avalanche/peermanager.cpp avalanche/processor.cpp avalanche/proof.cpp avalanche/proofid.cpp avalanche/proofpool.cpp avalanche/stakecontendercache.cpp avalanche/voterecord.cpp cashaddr.cpp # via cashaddrenc.cpp cashaddrenc.cpp # via key_io.cpp dnsseeds.cpp # via net.cpp (GetRandomizedDNSSeeds) i2p.cpp # via net.cppTx key_io.cpp # avalanche/processor.cpp uses DecodeSecret minerfund.cpp # via policy/block/minerfund.cpp net.cpp # avalanche uses CConnman netaddress.cpp # via net.cpp netbase.cpp # via net.cpp net_permissions.cpp # via net.cpp policy/block/minerfund.cpp policy/block/preconsensus.cpp policy/block/rtt.cpp policy/block/stakingrewards.cpp protocol.cpp # avalanche/processor.cpp uses NetMsgType timedata.cpp # via net.cpp util/asmap.cpp # via netaddress.cpp util/error.cpp # via net_permissions.cpp (ResolveErrMsg) util/readwritefile.cpp # via i2p.cpp util/sock.cpp # via net.cpp ) target_include_directories(bitcoinkernel PUBLIC . leveldb/helpers/memenv # To access the config/ and obj/ directories ${CMAKE_CURRENT_BINARY_DIR} ) target_link_libraries(bitcoinkernel crypto univalue secp256k1 leveldb memenv) link_boost_headers_only(bitcoinkernel headers) if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") link_windows_dependencies(bitcoinkernel) endif() add_executable(bitcoin-chainstate bitcoin-chainstate.cpp) target_link_libraries(bitcoin-chainstate bitcoinkernel) generate_windows_version_info(bitcoin-chainstate DESCRIPTION "CLI Datadir information utility (experimental)" ) add_to_symbols_check(bitcoin-chainstate) add_to_security_check(bitcoin-chainstate) endif() # bitcoind add_executable(bitcoind bitcoind.cpp) target_link_libraries(bitcoind server) generate_windows_version_info(bitcoind DESCRIPTION "Bitcoin node with a JSON-RPC server" ) add_to_symbols_check(bitcoind) add_to_security_check(bitcoind) install_target(bitcoind) install_manpages(bitcoind) # Bitcoin-qt if(BUILD_BITCOIN_QT) add_subdirectory(qt) endif() if(BUILD_BITCOIN_IGUANA) add_subdirectory(iguana) endif() diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index deee10561..9e025b563 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -1,1169 +1,1164 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> #endif #include <chainparamsbase.h> #include <clientversion.h> #include <common/args.h> #include <common/system.h> #include <currencyunit.h> #include <rpc/client.h> #include <rpc/mining.h> #include <rpc/protocol.h> #include <rpc/request.h> #include <support/events.h> #include <tinyformat.h> #include <util/exception.h> #include <util/strencodings.h> #include <util/string.h> #include <util/time.h> #include <util/translation.h> #include <event2/buffer.h> #include <event2/keyvalq_struct.h> #include <compat/stdin.h> #include <univalue.h> #include <algorithm> #include <chrono> #include <cmath> #include <cstdio> #include <functional> #include <memory> #include <string> #include <tuple> // The server returns time values from a mockable system clock, but it is not // trivial to get the mocked time from the server, nor is it needed for now, so // just use a plain system_clock. using CliClock = std::chrono::system_clock; const std::function<std::string(const char *)> G_TRANSLATION_FUN = nullptr; static const char DEFAULT_RPCCONNECT[] = "127.0.0.1"; static const int DEFAULT_HTTP_CLIENT_TIMEOUT = 900; static const bool DEFAULT_NAMED = false; static const int CONTINUE_EXECUTION = -1; /** Default number of blocks to generate for RPC generatetoaddress. */ static const std::string DEFAULT_NBLOCKS = "1"; static void SetupCliArgs(ArgsManager &argsman) { SetupHelpOptions(argsman); const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN); const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET); const auto regtestBaseParams = CreateBaseChainParams(CBaseChainParams::REGTEST); SetupCurrencyUnitOptions(argsman); argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-conf=<file>", strprintf("Specify configuration file. Relative paths will be " "prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-generate", strprintf( "Generate blocks immediately, equivalent to RPC getnewaddress " "followed by RPC generatetoaddress. Optional positional integer " "arguments are number of blocks to generate (default: %s) and " "maximum iterations to try (default: %s), equivalent to RPC " "generatetoaddress nblocks and maxtries arguments. Example: " "bitcoin-cli -generate 4 1000", DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-getinfo", "Get general information from the remote server. Note that unlike " "server-side RPC calls, the results of -getinfo is the result of " "multiple non-atomic requests. Some entries in the result may " "represent results from different states (e.g. wallet balance may be " "as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-netinfo", "Get network peer connection information from the remote " "server. An optional integer argument from 0 to 4 can be " "passed for different peers listings (default: 0).", ArgsManager::ALLOW_INT, OptionsCategory::OPTIONS); SetupChainParamsBaseOptions(argsman); argsman.AddArg( "-named", strprintf("Pass named instead of positional arguments (default: %s)", DEFAULT_NAMED), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-rpcconnect=<ip>", strprintf("Send commands to node running on <ip> (default: %s)", DEFAULT_RPCCONNECT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-rpccookiefile=<loc>", "Location of the auth cookie. Relative paths will be prefixed " "by a net-specific datadir location. (default: data dir)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-rpcport=<port>", strprintf("Connect to JSON-RPC on <port> (default: %u, " "testnet: %u, regtest: %u)", defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort(), regtestBaseParams->RPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS); argsman.AddArg("-rpcwait", "Wait for RPC server to start", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-rpcuser=<user>", "Username for JSON-RPC connections", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-rpcpassword=<pw>", "Password for JSON-RPC connections", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-rpcclienttimeout=<n>", strprintf("Timeout in seconds during HTTP requests, or 0 for " "no timeout. (default: %d)", DEFAULT_HTTP_CLIENT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single " "line. When combined with -stdin, the first line " "from standard input is used for the RPC password. When " "combined with -stdinwalletpassphrase, -stdinrpcpass " "consumes the first line, and -stdinwalletpassphrase " "consumes the second.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-stdinwalletpassphrase", "Read wallet passphrase from standard input as a single " "line. When combined with -stdin, the first line " "from standard input is used for the wallet passphrase.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-stdin", "Read extra arguments from standard input, one per line until " "EOF/Ctrl-D (recommended for sensitive information such as " "passphrases). When combined with -stdinrpcpass, the first " "line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg( "-rpcwallet=<walletname>", "Send RPC for non-default wallet on RPC server (needs to exactly match " "corresponding -wallet option passed to bitcoind). This changes the " "RPC endpoint used, e.g. http://127.0.0.1:8332/wallet/<walletname>", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); } /** libevent event log callback */ static void libevent_log_cb(int severity, const char *msg) { #ifndef EVENT_LOG_ERR // EVENT_LOG_ERR was added in 2.0.19; but before then _EVENT_LOG_ERR existed. #define EVENT_LOG_ERR _EVENT_LOG_ERR #endif // Ignore everything other than errors if (severity >= EVENT_LOG_ERR) { throw std::runtime_error(strprintf("libevent error: %s", msg)); } } ////////////////////////////////////////////////////////////////////////////// // // Start // // // Exception thrown on connection error. This error is used to determine when // to wait if -rpcwait is given. // class CConnectionFailed : public std::runtime_error { public: explicit inline CConnectionFailed(const std::string &msg) : std::runtime_error(msg) {} }; // // This function returns either one of EXIT_ codes when it's expected to stop // the process or CONTINUE_EXECUTION when it's expected to continue further. // static int AppInitRPC(int argc, char *argv[]) { // // Parameters // SetupCliArgs(gArgs); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error); return EXIT_FAILURE; } if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { std::string strUsage = PACKAGE_NAME " RPC client version " + FormatFullVersion() + "\n"; if (gArgs.IsArgSet("-version")) { strUsage += FormatParagraph(LicenseInfo()); } else { strUsage += "\n" "Usage: bitcoin-cli [options] <command> [params] " "Send command to " PACKAGE_NAME "\n" "or: bitcoin-cli [options] -named <command> " "[name=value]... Send command to " PACKAGE_NAME " (with named arguments)\n" "or: bitcoin-cli [options] help " "List commands\n" "or: bitcoin-cli [options] help <command> Get " "help for a command\n"; strUsage += "\n" + gArgs.GetHelpMessage(); } tfm::format(std::cout, "%s", strUsage); if (argc < 2) { tfm::format(std::cerr, "Error: too few parameters\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } if (!CheckDataDirOption(gArgs)) { tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "")); return EXIT_FAILURE; } if (!gArgs.ReadConfigFiles(error, true)) { tfm::format(std::cerr, "Error reading configuration file: %s\n", error); return EXIT_FAILURE; } // Check for -chain, -testnet or -regtest parameter (BaseParams() calls are // only valid after this clause) try { SelectBaseParams(gArgs.GetChainName()); } catch (const std::exception &e) { tfm::format(std::cerr, "Error: %s\n", e.what()); return EXIT_FAILURE; } return CONTINUE_EXECUTION; } /** Reply structure for request_done to fill in */ struct HTTPReply { HTTPReply() : status(0), error(-1) {} int status; int error; std::string body; }; static std::string http_errorstring(int code) { switch (code) { -#if LIBEVENT_VERSION_NUMBER >= 0x02010300 case EVREQ_HTTP_TIMEOUT: return "timeout reached"; case EVREQ_HTTP_EOF: return "EOF reached"; case EVREQ_HTTP_INVALID_HEADER: return "error while reading header, or invalid header"; case EVREQ_HTTP_BUFFER_ERROR: return "error encountered while reading or writing"; case EVREQ_HTTP_REQUEST_CANCEL: return "request was canceled"; case EVREQ_HTTP_DATA_TOO_LONG: return "response body is larger than allowed"; -#endif default: return "unknown"; } } static void http_request_done(struct evhttp_request *req, void *ctx) { HTTPReply *reply = static_cast<HTTPReply *>(ctx); if (req == nullptr) { /** * If req is nullptr, it means an error occurred while connecting: the * error code will have been passed to http_error_cb. */ reply->status = 0; return; } reply->status = evhttp_request_get_response_code(req); struct evbuffer *buf = evhttp_request_get_input_buffer(req); if (buf) { size_t size = evbuffer_get_length(buf); const char *data = (const char *)evbuffer_pullup(buf, size); if (data) { reply->body = std::string(data, size); } evbuffer_drain(buf, size); } } -#if LIBEVENT_VERSION_NUMBER >= 0x02010300 static void http_error_cb(enum evhttp_request_error err, void *ctx) { HTTPReply *reply = static_cast<HTTPReply *>(ctx); reply->error = err; } -#endif /** * Class that handles the conversion from a command-line to a JSON-RPC request, * as well as converting back to a JSON object that can be shown as result. */ class BaseRequestHandler { public: virtual ~BaseRequestHandler() {} virtual UniValue PrepareRequest(const std::string &method, const std::vector<std::string> &args) = 0; virtual UniValue ProcessReply(const UniValue &batch_in) = 0; }; /** Process getinfo requests */ class GetinfoRequestHandler : public BaseRequestHandler { public: const int ID_NETWORKINFO = 0; const int ID_BLOCKCHAININFO = 1; const int ID_WALLETINFO = 2; const int ID_BALANCES = 3; /** Create a simulated `getinfo` request. */ UniValue PrepareRequest(const std::string &method, const std::vector<std::string> &args) override { if (!args.empty()) { throw std::runtime_error("-getinfo takes no arguments"); } UniValue result(UniValue::VARR); result.push_back( JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO)); result.push_back(JSONRPCRequestObj("getblockchaininfo", NullUniValue, ID_BLOCKCHAININFO)); result.push_back( JSONRPCRequestObj("getwalletinfo", NullUniValue, ID_WALLETINFO)); result.push_back( JSONRPCRequestObj("getbalances", NullUniValue, ID_BALANCES)); return result; } /** Collect values from the batch and form a simulated `getinfo` reply. */ UniValue ProcessReply(const UniValue &batch_in) override { UniValue result(UniValue::VOBJ); const std::vector<UniValue> batch = JSONRPCProcessBatchReply(batch_in); // Errors in getnetworkinfo() and getblockchaininfo() are fatal, pass // them on; getwalletinfo() and getbalances are allowed to fail if there // is no wallet. if (!batch[ID_NETWORKINFO]["error"].isNull()) { return batch[ID_NETWORKINFO]; } if (!batch[ID_BLOCKCHAININFO]["error"].isNull()) { return batch[ID_BLOCKCHAININFO]; } result.pushKV("version", batch[ID_NETWORKINFO]["result"]["version"]); result.pushKV("blocks", batch[ID_BLOCKCHAININFO]["result"]["blocks"]); result.pushKV("headers", batch[ID_BLOCKCHAININFO]["result"]["headers"]); result.pushKV( "verificationprogress", batch[ID_BLOCKCHAININFO]["result"]["verificationprogress"]); result.pushKV("timeoffset", batch[ID_NETWORKINFO]["result"]["timeoffset"]); UniValue connections(UniValue::VOBJ); connections.pushKV("in", batch[ID_NETWORKINFO]["result"]["connections_in"]); connections.pushKV("out", batch[ID_NETWORKINFO]["result"]["connections_out"]); connections.pushKV("total", batch[ID_NETWORKINFO]["result"]["connections"]); result.pushKV("connections", connections); result.pushKV("proxy", batch[ID_NETWORKINFO]["result"]["networks"][0]["proxy"]); result.pushKV("difficulty", batch[ID_BLOCKCHAININFO]["result"]["difficulty"]); result.pushKV("chain", UniValue(batch[ID_BLOCKCHAININFO]["result"]["chain"])); if (!batch[ID_WALLETINFO]["result"].isNull()) { result.pushKV("keypoolsize", batch[ID_WALLETINFO]["result"]["keypoolsize"]); if (!batch[ID_WALLETINFO]["result"]["unlocked_until"].isNull()) { result.pushKV("unlocked_until", batch[ID_WALLETINFO]["result"]["unlocked_until"]); } result.pushKV("paytxfee", batch[ID_WALLETINFO]["result"]["paytxfee"]); } if (!batch[ID_BALANCES]["result"].isNull()) { result.pushKV("balance", batch[ID_BALANCES]["result"]["mine"]["trusted"]); } result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]); result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]); return JSONRPCReplyObj(result, NullUniValue, 1); } }; /** Process netinfo requests */ class NetinfoRequestHandler : public BaseRequestHandler { private: static constexpr int8_t UNKNOWN_NETWORK{-1}; static constexpr uint8_t m_networks_size{3}; const std::array<std::string, m_networks_size> m_networks{ {"ipv4", "ipv6", "onion"}}; //! Peer counts by (in/out/total, networks/total/block-relay) std::array<std::array<uint16_t, m_networks_size + 2>, 3> m_counts{{{}}}; int8_t NetworkStringToId(const std::string &str) const { for (uint8_t i = 0; i < m_networks_size; ++i) { if (str == m_networks.at(i)) { return i; } } return UNKNOWN_NETWORK; } //! Optional user-supplied arg to set dashboard details level uint8_t m_details_level{0}; bool DetailsRequested() const { return m_details_level > 0 && m_details_level < 5; } bool IsAddressSelected() const { return m_details_level == 2 || m_details_level == 4; } bool IsVersionSelected() const { return m_details_level == 3 || m_details_level == 4; } bool m_is_asmap_on{false}; size_t m_max_addr_length{0}; size_t m_max_age_length{4}; size_t m_max_id_length{2}; struct Peer { std::string addr; std::string sub_version; std::string network; std::string age; double min_ping; double ping; int64_t last_blck; int64_t last_recv; int64_t last_send; int64_t last_trxn; int id; int mapped_as; int version; bool is_block_relay; bool is_outbound; bool operator<(const Peer &rhs) const { return std::tie(is_outbound, min_ping) < std::tie(rhs.is_outbound, rhs.min_ping); } }; std::vector<Peer> m_peers; std::string ChainToString() const { if (gArgs.GetChainName() == CBaseChainParams::TESTNET) { return " testnet"; } if (gArgs.GetChainName() == CBaseChainParams::REGTEST) { return " regtest"; } return ""; } std::string PingTimeToString(double seconds) const { if (seconds < 0) { return ""; } const double milliseconds{round(1000 * seconds)}; return milliseconds > 999999 ? "-" : ToString(milliseconds); } const int64_t m_time_now{ TicksSinceEpoch<std::chrono::seconds>(CliClock::now())}; public: static constexpr int ID_PEERINFO = 0; static constexpr int ID_NETWORKINFO = 1; UniValue PrepareRequest(const std::string &method, const std::vector<std::string> &args) override { if (!args.empty()) { uint8_t n{0}; if (ParseUInt8(args.at(0), &n)) { m_details_level = n; } } UniValue result(UniValue::VARR); result.push_back( JSONRPCRequestObj("getpeerinfo", NullUniValue, ID_PEERINFO)); result.push_back( JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO)); return result; } UniValue ProcessReply(const UniValue &batch_in) override { const std::vector<UniValue> batch{JSONRPCProcessBatchReply(batch_in)}; if (!batch[ID_PEERINFO]["error"].isNull()) { return batch[ID_PEERINFO]; } if (!batch[ID_NETWORKINFO]["error"].isNull()) { return batch[ID_NETWORKINFO]; } const UniValue &networkinfo{batch[ID_NETWORKINFO]["result"]}; if (networkinfo["version"].getInt<int>() < 230000) { throw std::runtime_error("-netinfo requires bitcoind server to be " "running v0.23.0 and up"); } // Count peer connection totals, and if DetailsRequested(), store peer // data in a vector of structs. for (const UniValue &peer : batch[ID_PEERINFO]["result"].getValues()) { const std::string network{peer["network"].get_str()}; const int8_t network_id{NetworkStringToId(network)}; if (network_id == UNKNOWN_NETWORK) { continue; } const bool is_outbound{!peer["inbound"].get_bool()}; const bool is_block_relay{!peer["relaytxes"].get_bool()}; // in/out by network ++m_counts.at(is_outbound).at(network_id); // in/out overall ++m_counts.at(is_outbound).at(m_networks_size); // total by network ++m_counts.at(2).at(network_id); // total overall ++m_counts.at(2).at(m_networks_size); if (is_block_relay) { // in/out block-relay ++m_counts.at(is_outbound).at(m_networks_size + 1); // total block-relay ++m_counts.at(2).at(m_networks_size + 1); } if (DetailsRequested()) { // Push data for this peer to the peers vector. const int peer_id{peer["id"].getInt<int>()}; const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].getInt<int>()}; const int version{peer["version"].getInt<int>()}; const int64_t conn_time{peer["conntime"].getInt<int64_t>()}; const int64_t last_blck{peer["last_block"].getInt<int64_t>()}; const int64_t last_recv{peer["lastrecv"].getInt<int64_t>()}; const int64_t last_send{peer["lastsend"].getInt<int64_t>()}; const int64_t last_trxn{ peer["last_transaction"].getInt<int64_t>()}; const double min_ping{ peer["minping"].isNull() ? -1 : peer["minping"].get_real()}; const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()}; const std::string addr{peer["addr"].get_str()}; const std::string age{ conn_time == 0 ? "" : ToString((m_time_now - conn_time) / 60)}; const std::string sub_version{peer["subver"].get_str()}; m_peers.push_back({addr, sub_version, network, age, min_ping, ping, last_blck, last_recv, last_send, last_trxn, peer_id, mapped_as, version, is_block_relay, is_outbound}); m_max_addr_length = std::max(addr.length() + 1, m_max_addr_length); m_max_age_length = std::max(age.length(), m_max_age_length); m_max_id_length = std::max(ToString(peer_id).length(), m_max_id_length); m_is_asmap_on |= (mapped_as != 0); } } // Generate report header. std::string result{strprintf( "%s %s%s - %i%s\n\n", PACKAGE_NAME, FormatFullVersion(), ChainToString(), networkinfo["protocolversion"].getInt<int>(), networkinfo["subversion"].get_str())}; // Report detailed peer connections list sorted by direction and minimum // ping time. if (DetailsRequested() && !m_peers.empty()) { std::sort(m_peers.begin(), m_peers.end()); result += strprintf( "Peer connections sorted by direction and min ping\n<-> relay " " net mping ping send recv txn blk %*s ", m_max_age_length, "age"); if (m_is_asmap_on) { result += " asmap "; } result += strprintf("%*s %-*s%s\n", m_max_id_length, "id", IsAddressSelected() ? m_max_addr_length : 0, IsAddressSelected() ? "address" : "", IsVersionSelected() ? "version" : ""); for (const Peer &peer : m_peers) { std::string version{ToString(peer.version) + peer.sub_version}; result += strprintf( "%3s %5s %5s%7s%7s%5s%5s%5s%5s %*s%*i %*s %-*s%s\n", peer.is_outbound ? "out" : "in", peer.is_block_relay ? "block" : "full", peer.network, PingTimeToString(peer.min_ping), PingTimeToString(peer.ping), peer.last_send == 0 ? "" : ToString(m_time_now - peer.last_send), peer.last_recv == 0 ? "" : ToString(m_time_now - peer.last_recv), peer.last_trxn == 0 ? "" : ToString((m_time_now - peer.last_trxn) / 60), peer.last_blck == 0 ? "" : ToString((m_time_now - peer.last_blck) / 60), // variable spacing m_max_age_length, peer.age, // variable spacing m_is_asmap_on ? 7 : 0, m_is_asmap_on && peer.mapped_as != 0 ? ToString(peer.mapped_as) : "", // variable spacing m_max_id_length, peer.id, // variable spacing IsAddressSelected() ? m_max_addr_length : 0, IsAddressSelected() ? peer.addr : "", IsVersionSelected() && version != "0" ? version : ""); } result += strprintf( " ms ms sec sec min min %*s\n\n", m_max_age_length, "min"); } // Report peer connection totals by type. result += " ipv4 ipv6 onion total block-relay\n"; const std::array<std::string, 3> rows{{"in", "out", "total"}}; for (uint8_t i = 0; i < m_networks_size; ++i) { result += strprintf("%-5s %5i %5i %5i %5i %5i\n", rows.at(i), m_counts.at(i).at(0), m_counts.at(i).at(1), m_counts.at(i).at(2), m_counts.at(i).at(m_networks_size), m_counts.at(i).at(m_networks_size + 1)); } // Report local addresses, ports, and scores. result += "\nLocal addresses"; const std::vector<UniValue> &local_addrs{ networkinfo["localaddresses"].getValues()}; if (local_addrs.empty()) { result += ": n/a\n"; } else { size_t max_addr_size{0}; for (const UniValue &addr : local_addrs) { max_addr_size = std::max(addr["address"].get_str().length() + 1, max_addr_size); } for (const UniValue &addr : local_addrs) { result += strprintf("\n%-*s port %6i score %6i", max_addr_size, addr["address"].get_str(), addr["port"].getInt<int>(), addr["score"].getInt<int>()); } } return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1); } }; /** Process RPC generatetoaddress request. */ class GenerateToAddressRequestHandler : public BaseRequestHandler { public: UniValue PrepareRequest(const std::string &method, const std::vector<std::string> &args) override { address_str = args.at(1); UniValue params{RPCConvertValues("generatetoaddress", args)}; return JSONRPCRequestObj("generatetoaddress", params, 1); } UniValue ProcessReply(const UniValue &reply) override { UniValue result(UniValue::VOBJ); result.pushKV("address", address_str); result.pushKV("blocks", reply.get_obj()["result"]); return JSONRPCReplyObj(result, NullUniValue, 1); } protected: std::string address_str; }; /** Process default single requests */ class DefaultRequestHandler : public BaseRequestHandler { public: UniValue PrepareRequest(const std::string &method, const std::vector<std::string> &args) override { UniValue params; if (gArgs.GetBoolArg("-named", DEFAULT_NAMED)) { params = RPCConvertNamedValues(method, args); } else { params = RPCConvertValues(method, args); } return JSONRPCRequestObj(method, params, 1); } UniValue ProcessReply(const UniValue &reply) override { return reply.get_obj(); } }; static UniValue CallRPC(BaseRequestHandler *rh, const std::string &strMethod, const std::vector<std::string> &args, const std::optional<std::string> &rpcwallet = {}) { std::string host; // In preference order, we choose the following for the port: // 1. -rpcport // 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6) // 3. default port for chain uint16_t port{BaseParams().RPCPort()}; SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host); port = static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", port)); // Obtain event base raii_event_base base = obtain_event_base(); // Synchronously look up hostname raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port); // Set connection timeout { const int timeout = gArgs.GetIntArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT); if (timeout > 0) { evhttp_connection_set_timeout(evcon.get(), timeout); } else { // Indefinite request timeouts are not possible in libevent-http, // so we set the timeout to a very long time period instead. // Average length of year in Gregorian calendar constexpr int YEAR_IN_SECONDS = 31556952; evhttp_connection_set_timeout(evcon.get(), 5 * YEAR_IN_SECONDS); } } HTTPReply response; raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void *)&response); if (req == nullptr) { throw std::runtime_error("create http request failed"); } -#if LIBEVENT_VERSION_NUMBER >= 0x02010300 + evhttp_request_set_error_cb(req.get(), http_error_cb); -#endif // Get credentials std::string strRPCUserColonPass; bool failedToGetAuthCookie = false; if (gArgs.GetArg("-rpcpassword", "") == "") { // Try fall back to cookie-based authentication if no password is // provided if (!GetAuthCookie(&strRPCUserColonPass)) { failedToGetAuthCookie = true; } } else { strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", ""); } struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req.get()); assert(output_headers); evhttp_add_header(output_headers, "Host", host.c_str()); evhttp_add_header(output_headers, "Connection", "close"); evhttp_add_header(output_headers, "Content-Type", "application/json"); evhttp_add_header( output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str()); // Attach request data std::string strRequest = rh->PrepareRequest(strMethod, args).write() + "\n"; struct evbuffer *output_buffer = evhttp_request_get_output_buffer(req.get()); assert(output_buffer); evbuffer_add(output_buffer, strRequest.data(), strRequest.size()); // check if we should use a special wallet endpoint std::string endpoint = "/"; if (rpcwallet) { char *encodedURI = evhttp_uriencode(rpcwallet->data(), rpcwallet->size(), false); if (encodedURI) { endpoint = "/wallet/" + std::string(encodedURI); free(encodedURI); } else { throw CConnectionFailed("uri-encode failed"); } } int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, endpoint.c_str()); // ownership moved to evcon in above call req.release(); if (r != 0) { throw CConnectionFailed("send http request failed"); } event_base_dispatch(base.get()); if (response.status == 0) { std::string responseErrorMessage; if (response.error != -1) { responseErrorMessage = strprintf(" (error code %d - \"%s\")", response.error, http_errorstring(response.error)); } throw CConnectionFailed( strprintf("Could not connect to the server %s:%d%s\n\nMake sure " "the bitcoind server is running and that you are " "connecting to the correct RPC port.", host, port, responseErrorMessage)); } else if (response.status == HTTP_UNAUTHORIZED) { if (failedToGetAuthCookie) { throw std::runtime_error(strprintf( "Could not locate RPC credentials. No authentication cookie " "could be found, and RPC password is not set. See " "-rpcpassword and -stdinrpcpass. Configuration file: (%s)", fs::PathToString(gArgs.GetConfigFilePath()))); } else { throw std::runtime_error( "Authorization failed: Incorrect rpcuser or rpcpassword"); } } else if (response.status == HTTP_SERVICE_UNAVAILABLE) { throw std::runtime_error( strprintf("Server response: %s", response.body)); } else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR) { throw std::runtime_error( strprintf("server returned HTTP error %d", response.status)); } else if (response.body.empty()) { throw std::runtime_error("no response from server"); } // Parse reply UniValue valReply(UniValue::VSTR); if (!valReply.read(response.body)) { throw std::runtime_error("couldn't parse reply from server"); } const UniValue reply = rh->ProcessReply(valReply); if (reply.empty()) { throw std::runtime_error( "expected reply to have result, error and id properties"); } return reply; } /** * ConnectAndCallRPC wraps CallRPC with -rpcwait and an exception handler. * * @param[in] rh Pointer to RequestHandler. * @param[in] strMethod Reference to const string method to forward to CallRPC. * @param[in] rpcwallet Reference to const optional string wallet name to * forward to CallRPC. * @returns the RPC response as a UniValue object. * @throws a CConnectionFailed std::runtime_error if connection failed or RPC * server still in warmup. */ static UniValue ConnectAndCallRPC(BaseRequestHandler *rh, const std::string &strMethod, const std::vector<std::string> &args, const std::optional<std::string> &rpcwallet = {}) { UniValue response(UniValue::VOBJ); // Execute and handle connection failures with -rpcwait. const bool fWait = gArgs.GetBoolArg("-rpcwait", false); do { try { response = CallRPC(rh, strMethod, args, rpcwallet); if (fWait) { const UniValue &error = response.find_value("error"); if (!error.isNull() && error["code"].getInt<int>() == RPC_IN_WARMUP) { throw CConnectionFailed("server in warmup"); } } break; // Connection succeeded, no need to retry. } catch (const CConnectionFailed &) { if (fWait) { UninterruptibleSleep(std::chrono::milliseconds{1000}); } else { throw; } } } while (fWait); return response; } /** Parse UniValue result to update the message to print to std::cout. */ static void ParseResult(const UniValue &result, std::string &strPrint) { if (result.isNull()) { return; } strPrint = result.isStr() ? result.get_str() : result.write(2); } /** * Parse UniValue error to update the message to print to std::cerr and the * code to return. */ static void ParseError(const UniValue &error, std::string &strPrint, int &nRet) { if (error.isObject()) { const UniValue &err_code = error.find_value("code"); const UniValue &err_msg = error.find_value("message"); if (!err_code.isNull()) { strPrint = "error code: " + err_code.getValStr() + "\n"; } if (err_msg.isStr()) { strPrint += ("error message:\n" + err_msg.get_str()); } if (err_code.isNum() && err_code.getInt<int>() == RPC_WALLET_NOT_SPECIFIED) { strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to " "bitcoin-cli command line."; } } else { strPrint = "error: " + error.write(); } nRet = abs(error["code"].getInt<int>()); } /** * GetWalletBalances calls listwallets; if more than one wallet is loaded, it * then fetches mine.trusted balances for each loaded wallet and pushes them to * `result`. * * @param result Reference to UniValue object the wallet names and balances are * pushed to. */ static void GetWalletBalances(UniValue &result) { DefaultRequestHandler rh; const UniValue listwallets = ConnectAndCallRPC(&rh, "listwallets", /* args=*/{}); if (!listwallets.find_value("error").isNull()) { return; } const UniValue &wallets = listwallets.find_value("result"); if (wallets.size() <= 1) { return; } UniValue balances(UniValue::VOBJ); for (const UniValue &wallet : wallets.getValues()) { const std::string wallet_name = wallet.get_str(); const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name); const UniValue &balance = getbalances.find_value("result")["mine"]["trusted"]; balances.pushKV(wallet_name, balance); } result.pushKV("balances", balances); } /** * Call RPC getnewaddress. * @returns getnewaddress response as a UniValue object. */ static UniValue GetNewAddress() { std::optional<std::string> wallet_name{}; if (gArgs.IsArgSet("-rpcwallet")) { wallet_name = gArgs.GetArg("-rpcwallet", ""); } DefaultRequestHandler rh; return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name); } /** * Check bounds and set up args for RPC generatetoaddress params: nblocks, * address, maxtries. * @param[in] address Reference to const string address to insert into the * args. * @param args Reference to vector of string args to modify. */ static void SetGenerateToAddressArgs(const std::string &address, std::vector<std::string> &args) { if (args.size() > 2) { throw std::runtime_error( "too many arguments (maximum 2 for nblocks and maxtries)"); } if (args.size() == 0) { args.emplace_back(DEFAULT_NBLOCKS); } else if (args.at(0) == "0") { throw std::runtime_error( "the first argument (number of blocks to generate, default: " + DEFAULT_NBLOCKS + ") must be an integer value greater than zero"); } args.emplace(args.begin() + 1, address); } static int CommandLineRPC(int argc, char *argv[]) { std::string strPrint; int nRet = 0; try { // Skip switches while (argc > 1 && IsSwitchChar(argv[1][0])) { argc--; argv++; } std::string rpcPass; if (gArgs.GetBoolArg("-stdinrpcpass", false)) { NO_STDIN_ECHO(); if (!StdinReady()) { fputs("RPC password> ", stderr); fflush(stderr); } if (!std::getline(std::cin, rpcPass)) { throw std::runtime_error("-stdinrpcpass specified but failed " "to read from standard input"); } if (StdinTerminal()) { fputc('\n', stdout); } gArgs.ForceSetArg("-rpcpassword", rpcPass); } std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]); if (gArgs.GetBoolArg("-stdinwalletpassphrase", false)) { NO_STDIN_ECHO(); std::string walletPass; if (args.size() < 1 || args[0].substr(0, 16) != "walletpassphrase") { throw std::runtime_error( "-stdinwalletpassphrase is only applicable for " "walletpassphrase(change)"); } if (!StdinReady()) { fputs("Wallet passphrase> ", stderr); fflush(stderr); } if (!std::getline(std::cin, walletPass)) { throw std::runtime_error("-stdinwalletpassphrase specified but " "failed to read from standard input"); } if (StdinTerminal()) { fputc('\n', stdout); } args.insert(args.begin() + 1, walletPass); } if (gArgs.GetBoolArg("-stdin", false)) { // Read one arg per line from stdin and append std::string line; while (std::getline(std::cin, line)) { args.push_back(line); } if (StdinTerminal()) { fputc('\n', stdout); } } std::unique_ptr<BaseRequestHandler> rh; std::string method; if (gArgs.IsArgSet("-getinfo")) { rh.reset(new GetinfoRequestHandler()); } else if (gArgs.GetBoolArg("-generate", false)) { const UniValue getnewaddress{GetNewAddress()}; const UniValue &error{getnewaddress.find_value("error")}; if (error.isNull()) { SetGenerateToAddressArgs( getnewaddress.find_value("result").get_str(), args); rh.reset(new GenerateToAddressRequestHandler()); } else { ParseError(error, strPrint, nRet); } } else if (gArgs.GetBoolArg("-netinfo", false)) { rh.reset(new NetinfoRequestHandler()); } else { rh.reset(new DefaultRequestHandler()); if (args.size() < 1) { throw std::runtime_error( "too few parameters (need at least command)"); } method = args[0]; // Remove trailing method name from arguments vector args.erase(args.begin()); } if (nRet == 0) { // Perform RPC call std::optional<std::string> wallet_name{}; if (gArgs.IsArgSet("-rpcwallet")) { wallet_name = gArgs.GetArg("-rpcwallet", ""); } const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name); // Parse reply UniValue result = reply.find_value("result"); const UniValue &error = reply.find_value("error"); if (error.isNull()) { if (gArgs.IsArgSet("-getinfo") && !gArgs.IsArgSet("-rpcwallet")) { // fetch multiwallet balances and append to result GetWalletBalances(result); } ParseResult(result, strPrint); } else { ParseError(error, strPrint, nRet); } } } catch (const std::exception &e) { strPrint = std::string("error: ") + e.what(); nRet = EXIT_FAILURE; } catch (...) { PrintExceptionContinue(nullptr, "CommandLineRPC()"); throw; } if (strPrint != "") { tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint); } return nRet; } #ifdef WIN32 // Export main() and ensure working ASLR on Windows. // Exporting a symbol will prevent the linker from stripping // the .reloc section from the binary, which is a requirement // for ASLR. This is a temporary workaround until a fixed // version of binutils is used for releases. __declspec(dllexport) int main(int argc, char *argv[]) { common::WinCmdLineArgs winArgs; std::tie(argc, argv) = winArgs.get(); #else int main(int argc, char *argv[]) { #endif SetupEnvironment(); if (!SetupNetworking()) { tfm::format(std::cerr, "Error: Initializing networking failed\n"); return EXIT_FAILURE; } event_set_log_callback(&libevent_log_cb); try { int ret = AppInitRPC(argc, argv); if (ret != CONTINUE_EXECUTION) { return ret; } } catch (const std::exception &e) { PrintExceptionContinue(&e, "AppInitRPC()"); return EXIT_FAILURE; } catch (...) { PrintExceptionContinue(nullptr, "AppInitRPC()"); return EXIT_FAILURE; } int ret = EXIT_FAILURE; try { ret = CommandLineRPC(argc, argv); } catch (const std::exception &e) { PrintExceptionContinue(&e, "CommandLineRPC()"); } catch (...) { PrintExceptionContinue(nullptr, "CommandLineRPC()"); } return ret; } diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 3e1727d9f..626e5a3f4 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1,704 +1,693 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Copyright (c) 2018-2019 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <httpserver.h> #include <chainparamsbase.h> #include <common/args.h> #include <compat.h> #include <config.h> #include <logging.h> #include <netbase.h> #include <node/ui_interface.h> #include <rpc/protocol.h> // For HTTP status codes #include <shutdown.h> #include <sync.h> #include <util/strencodings.h> #include <util/threadnames.h> #include <util/translation.h> #include <event2/buffer.h> #include <event2/bufferevent.h> #include <event2/keyvalq_struct.h> #include <event2/thread.h> #include <event2/util.h> #include <support/events.h> #include <sys/stat.h> #include <sys/types.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <memory> /** Maximum size of http request (request line + headers) */ static const size_t MAX_HEADERS_SIZE = 8192; /** * Maximum HTTP post body size. Twice the maximum block size is added to this * value in practice. */ static const size_t MIN_SUPPORTED_BODY_SIZE = 0x02000000; /** HTTP request work item */ class HTTPWorkItem final : public HTTPClosure { public: HTTPWorkItem(Config &_config, std::unique_ptr<HTTPRequest> _req, const std::string &_path, const HTTPRequestHandler &_func) : req(std::move(_req)), path(_path), func(_func), config(&_config) {} void operator()() override { func(*config, req.get(), path); } std::unique_ptr<HTTPRequest> req; private: std::string path; HTTPRequestHandler func; Config *config; }; /** * Simple work queue for distributing work over multiple threads. * Work items are simply callable objects. */ template <typename WorkItem> class WorkQueue { private: /** Mutex protects entire object */ Mutex cs; std::condition_variable cond; std::deque<std::unique_ptr<WorkItem>> queue; bool running; size_t maxDepth; public: explicit WorkQueue(size_t _maxDepth) : running(true), maxDepth(_maxDepth) {} /** * Precondition: worker threads have all stopped (they have all been joined) */ ~WorkQueue() {} /** Enqueue a work item */ bool Enqueue(WorkItem *item) EXCLUSIVE_LOCKS_REQUIRED(!cs) { LOCK(cs); if (queue.size() >= maxDepth) { return false; } queue.emplace_back(std::unique_ptr<WorkItem>(item)); cond.notify_one(); return true; } /** Thread function */ void Run() EXCLUSIVE_LOCKS_REQUIRED(!cs) { while (true) { std::unique_ptr<WorkItem> i; { WAIT_LOCK(cs, lock); while (running && queue.empty()) { cond.wait(lock); } if (!running) { break; } i = std::move(queue.front()); queue.pop_front(); } (*i)(); } } /** Interrupt and exit loops */ void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!cs) { LOCK(cs); running = false; cond.notify_all(); } }; struct HTTPPathHandler { HTTPPathHandler(std::string _prefix, bool _exactMatch, HTTPRequestHandler _handler) : prefix(_prefix), exactMatch(_exactMatch), handler(_handler) {} std::string prefix; bool exactMatch; HTTPRequestHandler handler; }; /** HTTP module state */ //! libevent event loop static struct event_base *eventBase = nullptr; //! HTTP server static struct evhttp *eventHTTP = nullptr; //! List of subnets to allow RPC connections from static std::vector<CSubNet> rpc_allow_subnets; //! Work queue for handling longer requests off the event loop thread static WorkQueue<HTTPClosure> *workQueue = nullptr; //! Handlers for (sub)paths static std::vector<HTTPPathHandler> pathHandlers; //! Bound listening sockets static std::vector<evhttp_bound_socket *> boundSockets; /** Check if a network address is allowed to access the HTTP server */ static bool ClientAllowed(const CNetAddr &netaddr) { if (!netaddr.IsValid()) { return false; } for (const CSubNet &subnet : rpc_allow_subnets) { if (subnet.Match(netaddr)) { return true; } } return false; } /** Initialize ACL list for HTTP server */ static bool InitHTTPAllowList() { rpc_allow_subnets.clear(); CNetAddr localv4; CNetAddr localv6; LookupHost("127.0.0.1", localv4, false); LookupHost("::1", localv6, false); // always allow IPv4 local subnet. rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv6 localhost. rpc_allow_subnets.push_back(CSubNet(localv6)); for (const std::string &strAllow : gArgs.GetArgs("-rpcallowip")) { CSubNet subnet; LookupSubNet(strAllow, subnet); if (!subnet.IsValid()) { uiInterface.ThreadSafeMessageBox( strprintf( Untranslated("Invalid -rpcallowip subnet specification: " "%s. Valid are a single IP (e.g. 1.2.3.4), a " "network/netmask (e.g. 1.2.3.4/255.255.255.0) " "or a network/CIDR (e.g. 1.2.3.4/24)."), strAllow), "", CClientUIInterface::MSG_ERROR); return false; } rpc_allow_subnets.push_back(subnet); } std::string strAllowed; for (const CSubNet &subnet : rpc_allow_subnets) { strAllowed += subnet.ToString() + " "; } LogPrint(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed); return true; } /** HTTP request method as string - use for logging only */ std::string RequestMethodString(HTTPRequest::RequestMethod m) { switch (m) { case HTTPRequest::GET: return "GET"; case HTTPRequest::POST: return "POST"; case HTTPRequest::HEAD: return "HEAD"; case HTTPRequest::PUT: return "PUT"; case HTTPRequest::OPTIONS: return "OPTIONS"; default: return "unknown"; } } /** HTTP request callback */ static void http_request_cb(struct evhttp_request *req, void *arg) { Config &config = *reinterpret_cast<Config *>(arg); // Disable reading to work around a libevent bug, fixed in 2.2.0. if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) { evhttp_connection *conn = evhttp_request_get_connection(req); if (conn) { bufferevent *bev = evhttp_connection_get_bufferevent(conn); if (bev) { bufferevent_disable(bev, EV_READ); } } } auto hreq = std::make_unique<HTTPRequest>(req); // Early address-based allow check if (!ClientAllowed(hreq->GetPeer())) { LogPrint(BCLog::HTTP, "HTTP request from %s rejected: Client network is not allowed " "RPC access\n", hreq->GetPeer().ToString()); hreq->WriteReply(HTTP_FORBIDDEN); return; } // Early reject unknown HTTP methods if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) { LogPrint(BCLog::HTTP, "HTTP request from %s rejected: Unknown HTTP request method\n", hreq->GetPeer().ToString()); hreq->WriteReply(HTTP_BAD_METHOD); return; } LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n", RequestMethodString(hreq->GetRequestMethod()), SanitizeString(hreq->GetURI(), SAFE_CHARS_URI).substr(0, 100), hreq->GetPeer().ToString()); // Find registered handler for prefix std::string strURI = hreq->GetURI(); std::string path; std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin(); std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end(); for (; i != iend; ++i) { bool match = false; if (i->exactMatch) { match = (strURI == i->prefix); } else { match = (strURI.substr(0, i->prefix.size()) == i->prefix); } if (match) { path = strURI.substr(i->prefix.size()); break; } } // Dispatch to worker thread. if (i != iend) { std::unique_ptr<HTTPWorkItem> item( new HTTPWorkItem(config, std::move(hreq), path, i->handler)); assert(workQueue); if (workQueue->Enqueue(item.get())) { /* if true, queue took ownership */ item.release(); } else { LogPrintf("WARNING: request rejected because http work queue depth " "exceeded, it can be increased with the -rpcworkqueue= " "setting\n"); item->req->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded"); } } else { hreq->WriteReply(HTTP_NOT_FOUND); } } /** Callback to reject HTTP requests after shutdown. */ static void http_reject_request_cb(struct evhttp_request *req, void *) { LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n"); evhttp_send_error(req, HTTP_SERVUNAVAIL, nullptr); } /** Event dispatcher thread */ static bool ThreadHTTP(struct event_base *base) { util::ThreadRename("http"); LogPrint(BCLog::HTTP, "Entering http event loop\n"); event_base_dispatch(base); // Event loop will be interrupted by InterruptHTTPServer() LogPrint(BCLog::HTTP, "Exited http event loop\n"); return event_base_got_break(base) == 0; } /** Bind HTTP server to specified addresses */ static bool HTTPBindAddresses(struct evhttp *http) { uint16_t http_port{static_cast<uint16_t>( gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))}; std::vector<std::pair<std::string, uint16_t>> endpoints; // Determine what addresses to bind to if (!(gArgs.IsArgSet("-rpcallowip") && gArgs.IsArgSet("-rpcbind"))) { // Default to loopback if not allowing external IPs. endpoints.push_back(std::make_pair("::1", http_port)); endpoints.push_back(std::make_pair("127.0.0.1", http_port)); if (gArgs.IsArgSet("-rpcallowip")) { LogPrintf("WARNING: option -rpcallowip was specified without " "-rpcbind; this doesn't usually make sense\n"); } if (gArgs.IsArgSet("-rpcbind")) { LogPrintf("WARNING: option -rpcbind was ignored because " "-rpcallowip was not specified, refusing to allow " "everyone to connect\n"); } } else if (gArgs.IsArgSet("-rpcbind")) { // Specific bind address. for (const std::string &strRPCBind : gArgs.GetArgs("-rpcbind")) { uint16_t port{http_port}; std::string host; SplitHostPort(strRPCBind, port, host); endpoints.push_back(std::make_pair(host, port)); } } // Bind addresses for (std::vector<std::pair<std::string, uint16_t>>::iterator i = endpoints.begin(); i != endpoints.end(); ++i) { LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second); evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle( http, i->first.empty() ? nullptr : i->first.c_str(), i->second); if (bind_handle) { CNetAddr addr; if (i->first.empty() || (LookupHost(i->first, addr, false) && addr.IsBindAny())) { LogPrintf("WARNING: the RPC server is not safe to expose to " "untrusted networks such as the public internet\n"); } boundSockets.push_back(bind_handle); } else { LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second); } } return !boundSockets.empty(); } /** Simple wrapper to set thread name and run work queue */ static void HTTPWorkQueueRun(WorkQueue<HTTPClosure> *queue, int worker_num) { util::ThreadRename(strprintf("httpworker.%i", worker_num)); queue->Run(); } /** libevent event log callback */ static void libevent_log_cb(int severity, const char *msg) { #ifndef EVENT_LOG_WARN // EVENT_LOG_WARN was added in 2.0.19; but before then _EVENT_LOG_WARN existed. #define EVENT_LOG_WARN _EVENT_LOG_WARN #endif BCLog::Level level; switch (severity) { case EVENT_LOG_DEBUG: level = BCLog::Level::Debug; break; case EVENT_LOG_MSG: level = BCLog::Level::Info; break; case EVENT_LOG_WARN: level = BCLog::Level::Warning; break; default: // EVENT_LOG_ERR and others are mapped to error level = BCLog::Level::Error; break; } LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg); } bool InitHTTPServer(Config &config) { if (!InitHTTPAllowList()) { return false; } // Redirect libevent's logging to our own log event_set_log_callback(&libevent_log_cb); - // Update libevent's log handling. Returns false if our version of - // libevent doesn't support debug logging, in which case we should - // clear the BCLog::LIBEVENT flag. - if (!UpdateHTTPServerLogging( - LogInstance().WillLogCategory(BCLog::LIBEVENT))) { - LogInstance().DisableCategory(BCLog::LIBEVENT); - } + // Update libevent's log handling. + UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT)); #ifdef WIN32 evthread_use_windows_threads(); #else evthread_use_pthreads(); #endif raii_event_base base_ctr = obtain_event_base(); /* Create a new evhttp object to handle requests. */ raii_evhttp http_ctr = obtain_evhttp(base_ctr.get()); struct evhttp *http = http_ctr.get(); if (!http) { LogPrintf("couldn't create evhttp. Exiting.\n"); return false; } evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)); evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); evhttp_set_max_body_size(http, MIN_SUPPORTED_BODY_SIZE + 2 * config.GetMaxBlockSize()); evhttp_set_gencb(http, http_request_cb, &config); // Only POST and OPTIONS are supported, but we return HTTP 405 for the // others evhttp_set_allowed_methods( http, EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_HEAD | EVHTTP_REQ_PUT | EVHTTP_REQ_DELETE | EVHTTP_REQ_OPTIONS); if (!HTTPBindAddresses(http)) { LogPrintf("Unable to bind any endpoint for RPC server\n"); return false; } LogPrint(BCLog::HTTP, "Initialized HTTP server\n"); int workQueueDepth = std::max( (long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L); LogPrintfCategory(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth); workQueue = new WorkQueue<HTTPClosure>(workQueueDepth); // transfer ownership to eventBase/HTTP via .release() eventBase = base_ctr.release(); eventHTTP = http_ctr.release(); return true; } -bool UpdateHTTPServerLogging(bool enable) { -#if LIBEVENT_VERSION_NUMBER >= 0x02010100 +void UpdateHTTPServerLogging(bool enable) { if (enable) { event_enable_debug_logging(EVENT_DBG_ALL); } else { event_enable_debug_logging(EVENT_DBG_NONE); } - return true; -#else - // Can't update libevent logging if version < 02010100 - return false; -#endif } static std::thread g_thread_http; static std::vector<std::thread> g_thread_http_workers; void StartHTTPServer() { LogPrint(BCLog::HTTP, "Starting HTTP server\n"); int rpcThreads = std::max( (long)gArgs.GetIntArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); LogPrintfCategory(BCLog::HTTP, "starting %d worker threads\n", rpcThreads); g_thread_http = std::thread(ThreadHTTP, eventBase); for (int i = 0; i < rpcThreads; i++) { g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); } } void InterruptHTTPServer() { LogPrint(BCLog::HTTP, "Interrupting HTTP server\n"); if (eventHTTP) { // Reject requests on current connections evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr); } if (workQueue) { workQueue->Interrupt(); } } void StopHTTPServer() { LogPrint(BCLog::HTTP, "Stopping HTTP server\n"); if (workQueue) { LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n"); for (auto &thread : g_thread_http_workers) { thread.join(); } g_thread_http_workers.clear(); delete workQueue; workQueue = nullptr; } // Unlisten sockets, these are what make the event loop running, which means // that after this and all connections are closed the event loop will quit. for (evhttp_bound_socket *socket : boundSockets) { evhttp_del_accept_socket(eventHTTP, socket); } boundSockets.clear(); if (eventBase) { LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n"); if (g_thread_http.joinable()) { g_thread_http.join(); } } if (eventHTTP) { evhttp_free(eventHTTP); eventHTTP = nullptr; } if (eventBase) { event_base_free(eventBase); eventBase = nullptr; } LogPrint(BCLog::HTTP, "Stopped HTTP server\n"); } struct event_base *EventBase() { return eventBase; } static void httpevent_callback_fn(evutil_socket_t, short, void *data) { // Static handler: simply call inner handler HTTPEvent *self = static_cast<HTTPEvent *>(data); self->handler(); if (self->deleteWhenTriggered) { delete self; } } HTTPEvent::HTTPEvent(struct event_base *base, bool _deleteWhenTriggered, const std::function<void()> &_handler) : deleteWhenTriggered(_deleteWhenTriggered), handler(_handler) { ev = event_new(base, -1, 0, httpevent_callback_fn, this); assert(ev); } HTTPEvent::~HTTPEvent() { event_free(ev); } void HTTPEvent::trigger(struct timeval *tv) { if (tv == nullptr) { // Immediately trigger event in main thread. event_active(ev, 0, 0); } else { // Trigger after timeval passed. evtimer_add(ev, tv); } } HTTPRequest::HTTPRequest(struct evhttp_request *_req, bool _replySent) : req(_req), replySent(_replySent) {} HTTPRequest::~HTTPRequest() { if (!replySent) { // Keep track of whether reply was sent to avoid request leaks LogPrintf("%s: Unhandled request\n", __func__); WriteReply(HTTP_INTERNAL_SERVER_ERROR, "Unhandled request"); } // evhttpd cleans up the request, as long as a reply was sent. } std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string &hdr) const { const struct evkeyvalq *headers = evhttp_request_get_input_headers(req); assert(headers); const char *val = evhttp_find_header(headers, hdr.c_str()); if (val) { return std::make_pair(true, val); } else { return std::make_pair(false, ""); } } std::string HTTPRequest::ReadBody() { struct evbuffer *buf = evhttp_request_get_input_buffer(req); if (!buf) { return ""; } size_t size = evbuffer_get_length(buf); /** * Trivial implementation: if this is ever a performance bottleneck, * internal copying can be avoided in multi-segment buffers by using * evbuffer_peek and an awkward loop. Though in that case, it'd be even * better to not copy into an intermediate string but use a stream * abstraction to consume the evbuffer on the fly in the parsing algorithm. */ const char *data = (const char *)evbuffer_pullup(buf, size); // returns nullptr in case of empty buffer. if (!data) { return ""; } std::string rv(data, size); evbuffer_drain(buf, size); return rv; } void HTTPRequest::WriteHeader(const std::string &hdr, const std::string &value) { struct evkeyvalq *headers = evhttp_request_get_output_headers(req); assert(headers); evhttp_add_header(headers, hdr.c_str(), value.c_str()); } /** * Closure sent to main thread to request a reply to be sent to a HTTP request. * Replies must be sent in the main loop in the main http thread, this cannot be * done from worker threads. */ void HTTPRequest::WriteReply(int nStatus, const std::string &strReply) { assert(!replySent && req); if (ShutdownRequested()) { WriteHeader("Connection", "close"); } // Send event to main http thread to send reply message struct evbuffer *evb = evhttp_request_get_output_buffer(req); assert(evb); evbuffer_add(evb, strReply.data(), strReply.size()); auto req_copy = req; HTTPEvent *ev = new HTTPEvent(eventBase, true, [req_copy, nStatus] { evhttp_send_reply(req_copy, nStatus, nullptr, nullptr); // Re-enable reading from the socket. This is the second part of the // libevent workaround above. if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) { evhttp_connection *conn = evhttp_request_get_connection(req_copy); if (conn) { bufferevent *bev = evhttp_connection_get_bufferevent(conn); if (bev) { bufferevent_enable(bev, EV_READ | EV_WRITE); } } } }); ev->trigger(nullptr); replySent = true; // transferred back to main thread. req = nullptr; } CService HTTPRequest::GetPeer() const { evhttp_connection *con = evhttp_request_get_connection(req); CService peer; if (con) { // evhttp retains ownership over returned address string const char *address = ""; uint16_t port = 0; evhttp_connection_get_peer(con, (char **)&address, &port); peer = LookupNumeric(address, port); } return peer; } std::string HTTPRequest::GetURI() const { return evhttp_request_get_uri(req); } HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const { switch (evhttp_request_get_command(req)) { case EVHTTP_REQ_GET: return GET; case EVHTTP_REQ_POST: return POST; case EVHTTP_REQ_HEAD: return HEAD; case EVHTTP_REQ_PUT: return PUT; case EVHTTP_REQ_OPTIONS: return OPTIONS; default: return UNKNOWN; } } void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler) { LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler)); } void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch) { std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin(); std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end(); for (; i != iend; ++i) { if (i->prefix == prefix && i->exactMatch == exactMatch) { break; } } if (i != iend) { LogPrint(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); pathHandlers.erase(i); } } diff --git a/src/httpserver.h b/src/httpserver.h index d25def0b9..9c4f5f2dc 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -1,164 +1,161 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Copyright (c) 2018-2019 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_HTTPSERVER_H #define BITCOIN_HTTPSERVER_H #include <functional> #include <string> static const int DEFAULT_HTTP_THREADS = 4; static const int DEFAULT_HTTP_WORKQUEUE = 16; static const int DEFAULT_HTTP_SERVER_TIMEOUT = 30; struct evhttp_request; struct event_base; class Config; class CService; class HTTPRequest; /** * Initialize HTTP server. * Call this before RegisterHTTPHandler or EventBase(). */ bool InitHTTPServer(Config &config); /** * Start HTTP server. * This is separate from InitHTTPServer to give users race-condition-free time * to register their handlers between InitHTTPServer and StartHTTPServer. */ void StartHTTPServer(); /** Interrupt HTTP server threads */ void InterruptHTTPServer(); /** Stop HTTP server */ void StopHTTPServer(); -/** - * Change logging level for libevent. Removes BCLog::LIBEVENT from - * log categories if libevent doesn't support debug logging. - */ -bool UpdateHTTPServerLogging(bool enable); +/** Change logging level for libevent. */ +void UpdateHTTPServerLogging(bool enable); /** Handler for requests to a certain HTTP path */ typedef std::function<bool(Config &config, HTTPRequest *req, const std::string &)> HTTPRequestHandler; /** * Register handler for prefix. * If multiple handlers match a prefix, the first-registered one will * be invoked. */ void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler); /** Unregister handler for prefix */ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch); /** * Return evhttp event base. This can be used by submodules to * queue timers or custom events. */ struct event_base *EventBase(); /** * In-flight HTTP request. * Thin C++ wrapper around evhttp_request. */ class HTTPRequest { private: struct evhttp_request *req; bool replySent; public: explicit HTTPRequest(struct evhttp_request *req, bool replySent = false); ~HTTPRequest(); enum RequestMethod { UNKNOWN, GET, POST, HEAD, PUT, OPTIONS }; /** Get requested URI */ std::string GetURI() const; /** Get CService (address:ip) for the origin of the http request */ CService GetPeer() const; /** Get request method */ RequestMethod GetRequestMethod() const; /** * Get the request header specified by hdr, or an empty string. * Return a pair (isPresent,string). */ std::pair<bool, std::string> GetHeader(const std::string &hdr) const; /** * Read request body. * * @note As this consumes the underlying buffer, call this only once. * Repeated calls will return an empty string. */ std::string ReadBody(); /** * Write output header. * * @note call this before calling WriteErrorReply or Reply. */ void WriteHeader(const std::string &hdr, const std::string &value); /** * Write HTTP reply. * nStatus is the HTTP status code to send. * strReply is the body of the reply. Keep it empty to send a standard * message. * * @note Can be called only once. As this will give the request back to the * main thread, do not call any other HTTPRequest methods after calling * this. */ void WriteReply(int nStatus, const std::string &strReply = ""); }; /** Event handler closure */ class HTTPClosure { public: virtual void operator()() = 0; virtual ~HTTPClosure() {} }; /** * Event class. This can be used either as a cross-thread trigger or as a * timer. */ class HTTPEvent { public: /** * Create a new event. * deleteWhenTriggered deletes this event object after the event is * triggered (and the handler called) * handler is the handler to call when the event is triggered. */ HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function<void()> &handler); ~HTTPEvent(); /** * Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger * it after the given time has elapsed. */ void trigger(struct timeval *tv); bool deleteWhenTriggered; std::function<void()> handler; private: struct event *ev; }; #endif // BITCOIN_HTTPSERVER_H diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 23c79fa70..d7082df90 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -1,985 +1,971 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2016 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 <chainparams.h> #include <config.h> #include <consensus/amount.h> #include <httpserver.h> #include <index/blockfilterindex.h> #include <index/coinstatsindex.h> #include <index/txindex.h> #include <interfaces/chain.h> #include <key_io.h> #include <logging.h> #include <node/context.h> #include <outputtype.h> #include <rpc/blockchain.h> #include <rpc/server.h> #include <rpc/server_util.h> #include <rpc/util.h> #include <scheduler.h> #include <script/descriptor.h> #include <timedata.h> #include <util/any.h> #include <util/check.h> #include <util/message.h> // For MessageSign(), MessageVerify() #include <util/strencodings.h> #include <util/time.h> #include <univalue.h> #include <cstdint> #include <tuple> #ifdef HAVE_MALLOC_INFO #include <malloc.h> #endif using node::NodeContext; static RPCHelpMan validateaddress() { return RPCHelpMan{ "validateaddress", "Return information about the given bitcoin address.\n", { {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"}, }, RPCResult{ RPCResult::Type::OBJ, "", "", { {RPCResult::Type::BOOL, "isvalid", "If the address is valid or not. If not, this is the only " "property returned."}, {RPCResult::Type::STR, "address", "The bitcoin address validated"}, {RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address"}, {RPCResult::Type::BOOL, "isscript", "If the key is a script"}, }}, RPCExamples{HelpExampleCli("validateaddress", EXAMPLE_ADDRESS) + HelpExampleRpc("validateaddress", EXAMPLE_ADDRESS)}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { CTxDestination dest = DecodeDestination(request.params[0].get_str(), config.GetChainParams()); bool isValid = IsValidDestination(dest); UniValue ret(UniValue::VOBJ); ret.pushKV("isvalid", isValid); if (isValid) { if (ret["address"].isNull()) { std::string currentAddress = EncodeDestination(dest, config); ret.pushKV("address", currentAddress); CScript scriptPubKey = GetScriptForDestination(dest); ret.pushKV("scriptPubKey", HexStr(scriptPubKey)); UniValue detail = DescribeAddress(dest); ret.pushKVs(detail); } } return ret; }, }; } static RPCHelpMan createmultisig() { return RPCHelpMan{ "createmultisig", "Creates a multi-signature address with n signature of m keys " "required.\n" "It returns a json object with the address and redeemScript.\n", { {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys."}, {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The hex-encoded public keys.", { {"key", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "The hex-encoded public key"}, }}, }, RPCResult{ RPCResult::Type::OBJ, "", "", { {RPCResult::Type::STR, "address", "The value of the new multisig address."}, {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script."}, {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig"}, }}, RPCExamples{ "\nCreate a multisig address from 2 public keys\n" + HelpExampleCli("createmultisig", "2 " "\"[" "\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd3" "42cf11ae157a7ace5fd\\\"," "\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e1" "7e107ef3f6aa5a61626\\\"]\"") + "\nAs a JSON-RPC call\n" + HelpExampleRpc("createmultisig", "2, " "\"[" "\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd3" "42cf11ae157a7ace5fd\\\"," "\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e1" "7e107ef3f6aa5a61626\\\"]\"")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { int required = request.params[0].getInt<int>(); // Get the public keys const UniValue &keys = request.params[1].get_array(); std::vector<CPubKey> pubkeys; for (size_t i = 0; i < keys.size(); ++i) { if ((keys[i].get_str().length() == 2 * CPubKey::COMPRESSED_SIZE || keys[i].get_str().length() == 2 * CPubKey::SIZE) && IsHex(keys[i].get_str())) { pubkeys.push_back(HexToPubKey(keys[i].get_str())); } else { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid public key: %s\n", keys[i].get_str())); } } // Get the output type OutputType output_type = OutputType::LEGACY; // Construct using pay-to-script-hash: FillableSigningProvider keystore; CScript inner; const CTxDestination dest = AddAndGetMultisigDestination( required, pubkeys, output_type, keystore, inner); // Make the descriptor std::unique_ptr<Descriptor> descriptor = InferDescriptor(GetScriptForDestination(dest), keystore); UniValue result(UniValue::VOBJ); result.pushKV("address", EncodeDestination(dest, config)); result.pushKV("redeemScript", HexStr(inner)); result.pushKV("descriptor", descriptor->ToString()); return result; }, }; } static RPCHelpMan getdescriptorinfo() { return RPCHelpMan{ "getdescriptorinfo", {"Analyses a descriptor.\n"}, { {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", { {RPCResult::Type::STR, "descriptor", "The descriptor in canonical form, without private keys"}, {RPCResult::Type::STR, "checksum", "The checksum for the input descriptor"}, {RPCResult::Type::BOOL, "isrange", "Whether the descriptor is ranged"}, {RPCResult::Type::BOOL, "issolvable", "Whether the descriptor is solvable"}, {RPCResult::Type::BOOL, "hasprivatekeys", "Whether the input descriptor contained at least one private " "key"}, }}, RPCExamples{"Analyse a descriptor\n" + HelpExampleCli("getdescriptorinfo", "\"pkh([d34db33f/84h/0h/" "0h]" "0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2" "dce28d959f2815b16f81798)\"")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { FlatSigningProvider provider; std::string error; auto desc = Parse(request.params[0].get_str(), provider, error); if (!desc) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); } UniValue result(UniValue::VOBJ); result.pushKV("descriptor", desc->ToString()); result.pushKV("checksum", GetDescriptorChecksum(request.params[0].get_str())); result.pushKV("isrange", desc->IsRange()); result.pushKV("issolvable", desc->IsSolvable()); result.pushKV("hasprivatekeys", provider.keys.size() > 0); return result; }, }; } static RPCHelpMan deriveaddresses() { return RPCHelpMan{ "deriveaddresses", {"Derives one or more addresses corresponding to an output " "descriptor.\n" "Examples of output descriptors are:\n" " pkh(<pubkey>) P2PKH outputs for the given " "pubkey\n" " sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for " "the given threshold and pubkeys\n" " raw(<hex script>) Outputs whose scriptPubKey " "equals the specified hex scripts\n" "\nIn the above, <pubkey> either refers to a fixed public key in " "hexadecimal notation, or to an xpub/xprv optionally followed by one\n" "or more path elements separated by \"/\", where \"h\" represents a " "hardened child key.\n" "For more information on output descriptors, see the documentation in " "the doc/descriptors.md file.\n"}, { {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."}, {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED_NAMED_ARG, "If a ranged descriptor is used, this specifies the end or the " "range (in [begin,end] notation) to derive."}, }, RPCResult{ RPCResult::Type::ARR, "", "", { {RPCResult::Type::STR, "address", "the derived addresses"}, }}, RPCExamples{"First three pkh receive addresses\n" + HelpExampleCli( "deriveaddresses", "\"pkh([d34db33f/84h/0h/0h]" "xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8P" "hqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKE" "u3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#3vhfv5h5\" \"[0,2]\"")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { const std::string desc_str = request.params[0].get_str(); int64_t range_begin = 0; int64_t range_end = 0; if (request.params.size() >= 2 && !request.params[1].isNull()) { std::tie(range_begin, range_end) = ParseDescriptorRange(request.params[1]); } FlatSigningProvider key_provider; std::string error; auto desc = Parse(desc_str, key_provider, error, /* require_checksum = */ true); if (!desc) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); } if (!desc->IsRange() && request.params.size() > 1) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an " "un-ranged descriptor"); } if (desc->IsRange() && request.params.size() == 1) { throw JSONRPCError( RPC_INVALID_PARAMETER, "Range must be specified for a ranged descriptor"); } UniValue addresses(UniValue::VARR); for (int i = range_begin; i <= range_end; ++i) { FlatSigningProvider provider; std::vector<CScript> scripts; if (!desc->Expand(i, key_provider, scripts, provider)) { throw JSONRPCError( RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys")); } for (const CScript &script : scripts) { CTxDestination dest; if (!ExtractDestination(script, dest)) { throw JSONRPCError( RPC_INVALID_ADDRESS_OR_KEY, strprintf("Descriptor does not have a " "corresponding address")); } addresses.push_back(EncodeDestination(dest, config)); } } // This should not be possible, but an assert seems overkill: if (addresses.empty()) { throw JSONRPCError(RPC_MISC_ERROR, "Unexpected empty result"); } return addresses; }, }; } static RPCHelpMan verifymessage() { return RPCHelpMan{ "verifymessage", "Verify a signed message\n", { {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."}, {"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see " "signmessage)."}, {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."}, }, RPCResult{RPCResult::Type::BOOL, "", "If the signature is verified or not."}, RPCExamples{ "\nUnlock the wallet for 30 seconds\n" + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") + "\nCreate the signature\n" + HelpExampleCli( "signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") + "\nVerify the signature\n" + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4" "XX\" \"signature\" \"my " "message\"") + "\nAs a JSON-RPC call\n" + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4" "XX\", \"signature\", \"my " "message\"")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { LOCK(cs_main); std::string strAddress = request.params[0].get_str(); std::string strSign = request.params[1].get_str(); std::string strMessage = request.params[2].get_str(); switch (MessageVerify(config.GetChainParams(), strAddress, strSign, strMessage)) { case MessageVerificationResult::ERR_INVALID_ADDRESS: throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address"); case MessageVerificationResult::ERR_ADDRESS_NO_KEY: throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key"); case MessageVerificationResult::ERR_MALFORMED_SIGNATURE: throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding"); case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED: case MessageVerificationResult::ERR_NOT_SIGNED: return false; case MessageVerificationResult::OK: return true; } return false; }, }; } static RPCHelpMan signmessagewithprivkey() { return RPCHelpMan{ "signmessagewithprivkey", "Sign a message with the private key of an address\n", { {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."}, {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."}, }, RPCResult{RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"}, RPCExamples{"\nCreate the signature\n" + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") + "\nVerify the signature\n" + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" " "\"signature\" \"my message\"") + "\nAs a JSON-RPC call\n" + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { std::string strPrivkey = request.params[0].get_str(); std::string strMessage = request.params[1].get_str(); CKey key = DecodeSecret(strPrivkey); if (!key.IsValid()) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key"); } std::string signature; if (!MessageSign(key, strMessage, signature)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed"); } return signature; }, }; } static RPCHelpMan setmocktime() { return RPCHelpMan{ "setmocktime", "Set the local time to given timestamp (-regtest only)\n", { {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, UNIX_EPOCH_TIME + "\n" "Pass 0 to go back to using the system time."}, }, RPCResult{RPCResult::Type::NONE, "", ""}, RPCExamples{""}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { if (!config.GetChainParams().IsMockableChain()) { throw std::runtime_error( "setmocktime for regression testing (-regtest mode) only"); } // For now, don't change mocktime if we're in the middle of // validation, as this could have an effect on mempool time-based // eviction, as well as IsInitialBlockDownload(). // TODO: figure out the right way to synchronize around mocktime, // and ensure all call sites of GetTime() are accessing this safely. LOCK(cs_main); const int64_t time{request.params[0].getInt<int64_t>()}; if (time < 0) { throw JSONRPCError( RPC_INVALID_PARAMETER, strprintf("Mocktime can not be negative: %s.", time)); } SetMockTime(time); auto node_context = util::AnyPtr<NodeContext>(request.context); if (node_context) { for (const auto &chain_client : node_context->chain_clients) { chain_client->setMockTime(time); } } return NullUniValue; }, }; } static RPCHelpMan mockscheduler() { return RPCHelpMan{ "mockscheduler", "Bump the scheduler into the future (-regtest only)\n", { {"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future."}, }, RPCResult{RPCResult::Type::NONE, "", ""}, RPCExamples{""}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { if (!Params().IsMockableChain()) { throw std::runtime_error("mockscheduler is for regression " "testing (-regtest mode) only"); } int64_t delta_seconds = request.params[0].getInt<int64_t>(); if ((delta_seconds <= 0) || (delta_seconds > 3600)) { throw std::runtime_error( "delta_time must be between 1 and 3600 seconds (1 hr)"); } auto node_context = CHECK_NONFATAL(util::AnyPtr<NodeContext>(request.context)); // protect against null pointer dereference CHECK_NONFATAL(node_context->scheduler); node_context->scheduler->MockForward( std::chrono::seconds(delta_seconds)); return NullUniValue; }, }; } static UniValue RPCLockedMemoryInfo() { LockedPool::Stats stats = LockedPoolManager::Instance().stats(); UniValue obj(UniValue::VOBJ); obj.pushKV("used", uint64_t(stats.used)); obj.pushKV("free", uint64_t(stats.free)); obj.pushKV("total", uint64_t(stats.total)); obj.pushKV("locked", uint64_t(stats.locked)); obj.pushKV("chunks_used", uint64_t(stats.chunks_used)); obj.pushKV("chunks_free", uint64_t(stats.chunks_free)); return obj; } #ifdef HAVE_MALLOC_INFO static std::string RPCMallocInfo() { char *ptr = nullptr; size_t size = 0; FILE *f = open_memstream(&ptr, &size); if (f) { malloc_info(0, f); fclose(f); if (ptr) { std::string rv(ptr, size); free(ptr); return rv; } } return ""; } #endif static RPCHelpMan getmemoryinfo() { /* Please, avoid using the word "pool" here in the RPC interface or help, * as users will undoubtedly confuse it with the other "memory pool" */ return RPCHelpMan{ "getmemoryinfo", "Returns an object containing information about memory usage.\n", { {"mode", RPCArg::Type::STR, RPCArg::Default{"stats"}, "determines what kind of information is returned.\n" " - \"stats\" returns general statistics about memory usage in " "the daemon.\n" " - \"mallocinfo\" returns an XML string describing low-level " "heap state (only available if compiled with glibc 2.10+)."}, }, { RPCResult{ "mode \"stats\"", RPCResult::Type::OBJ, "", "", { {RPCResult::Type::OBJ, "locked", "Information about locked memory manager", { {RPCResult::Type::NUM, "used", "Number of bytes used"}, {RPCResult::Type::NUM, "free", "Number of bytes available in current arenas"}, {RPCResult::Type::NUM, "total", "Total number of bytes managed"}, {RPCResult::Type::NUM, "locked", "Amount of bytes that succeeded locking. If this " "number is smaller than total, locking pages failed " "at some point and key data could be swapped to " "disk."}, {RPCResult::Type::NUM, "chunks_used", "Number allocated chunks"}, {RPCResult::Type::NUM, "chunks_free", "Number unused chunks"}, }}, }}, RPCResult{"mode \"mallocinfo\"", RPCResult::Type::STR, "", "\"<malloc version=\"1\">...\""}, }, RPCExamples{HelpExampleCli("getmemoryinfo", "") + HelpExampleRpc("getmemoryinfo", "")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str(); if (mode == "stats") { UniValue obj(UniValue::VOBJ); obj.pushKV("locked", RPCLockedMemoryInfo()); return obj; } else if (mode == "mallocinfo") { #ifdef HAVE_MALLOC_INFO return RPCMallocInfo(); #else throw JSONRPCError(RPC_INVALID_PARAMETER, "mallocinfo is only available when compiled " "with glibc 2.10+"); #endif } else { throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode); } }, }; } static void EnableOrDisableLogCategories(UniValue cats, bool enable) { cats = cats.get_array(); for (size_t i = 0; i < cats.size(); ++i) { std::string cat = cats[i].get_str(); bool success; if (enable) { success = LogInstance().EnableCategory(cat); } else { success = LogInstance().DisableCategory(cat); } if (!success) { throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat); } } } static RPCHelpMan logging() { return RPCHelpMan{ "logging", "Gets and sets the logging configuration.\n" "When called without an argument, returns the list of categories with " "status that are currently being debug logged or not.\n" "When called with arguments, adds or removes categories from debug " "logging and return the lists above.\n" "The arguments are evaluated in order \"include\", \"exclude\".\n" "If an item is both included and excluded, it will thus end up being " "excluded.\n" "The valid logging categories are: " + LogInstance().LogCategoriesString() + "\n" "In addition, the following are available as category names with " "special meanings:\n" " - \"all\", \"1\" : represent all logging categories.\n" " - \"none\", \"0\" : even if other logging categories are " "specified, ignore all of them.\n", { {"include", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "The categories to add to debug logging", { {"include_category", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "the valid logging category"}, }}, {"exclude", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "The categories to remove from debug logging", { {"exclude_category", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "the valid logging category"}, }}, }, RPCResult{ RPCResult::Type::OBJ_DYN, "", "keys are the logging categories, and values indicates its status", { {RPCResult::Type::BOOL, "category", "if being debug logged or not. false:inactive, true:active"}, }}, RPCExamples{ HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"") + HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { uint32_t original_log_categories = LogInstance().GetCategoryMask(); if (request.params[0].isArray()) { EnableOrDisableLogCategories(request.params[0], true); } if (request.params[1].isArray()) { EnableOrDisableLogCategories(request.params[1], false); } uint32_t updated_log_categories = LogInstance().GetCategoryMask(); uint32_t changed_log_categories = original_log_categories ^ updated_log_categories; - /** - * Update libevent logging if BCLog::LIBEVENT has changed. - * If the library version doesn't allow it, - * UpdateHTTPServerLogging() returns false, in which case we should - * clear the BCLog::LIBEVENT flag. Throw an error if the user has - * explicitly asked to change only the libevent flag and it failed. - */ + // Update libevent logging if BCLog::LIBEVENT has changed. if (changed_log_categories & BCLog::LIBEVENT) { - if (!UpdateHTTPServerLogging( - LogInstance().WillLogCategory(BCLog::LIBEVENT))) { - LogInstance().DisableCategory(BCLog::LIBEVENT); - if (changed_log_categories == BCLog::LIBEVENT) { - throw JSONRPCError( - RPC_INVALID_PARAMETER, - "libevent logging cannot be updated when " - "using libevent before v2.1.1."); - } - } + UpdateHTTPServerLogging( + LogInstance().WillLogCategory(BCLog::LIBEVENT)); } UniValue result(UniValue::VOBJ); for (const auto &logCatActive : LogInstance().LogCategoriesList()) { result.pushKV(logCatActive.category, logCatActive.active); } return result; }, }; } static RPCHelpMan echo(const std::string &name) { return RPCHelpMan{ name, "Simply echo back the input arguments. This command is for " "testing.\n" "\nIt will return an internal bug report when " "arg9='trigger_internal_bug' is passed.\n" "\nThe difference between echo and echojson is that echojson has " "argument conversion enabled in the client-side table in " "bitcoin-cli and the GUI. There is no server-side difference.", { {"arg0", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg1", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg2", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg3", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg4", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg5", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg6", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg7", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg8", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, {"arg9", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "", RPCArgOptions{.skip_type_check = true}}, }, RPCResult{RPCResult::Type::ANY, "", "Returns whatever was passed in"}, RPCExamples{""}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { if (request.params[9].isStr()) { CHECK_NONFATAL(request.params[9].get_str() != "trigger_internal_bug"); } return request.params; }, }; } static RPCHelpMan echo() { return echo("echo"); } static RPCHelpMan echojson() { return echo("echojson"); } static RPCHelpMan getcurrencyinfo() { return RPCHelpMan{ "getcurrencyinfo", "Returns an object containing information about the currency.\n", {}, { RPCResult{ RPCResult::Type::OBJ, "", "", { {RPCResult::Type::STR, "ticker", "Ticker symbol"}, {RPCResult::Type::NUM, "satoshisperunit", "Number of satoshis per base unit"}, {RPCResult::Type::NUM, "decimals", "Number of digits to the right of the decimal point."}, }}, }, RPCExamples{HelpExampleCli("getcurrencyinfo", "") + HelpExampleRpc("getcurrencyinfo", "")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { const Currency ¤cy = Currency::get(); UniValue res(UniValue::VOBJ); res.pushKV("ticker", currency.ticker); res.pushKV("satoshisperunit", currency.baseunit / SATOSHI); res.pushKV("decimals", currency.decimals); return res; }, }; } static UniValue SummaryToJSON(const IndexSummary &&summary, std::string index_name) { UniValue ret_summary(UniValue::VOBJ); if (!index_name.empty() && index_name != summary.name) { return ret_summary; } UniValue entry(UniValue::VOBJ); entry.pushKV("synced", summary.synced); entry.pushKV("best_block_height", summary.best_block_height); ret_summary.pushKV(summary.name, entry); return ret_summary; } static RPCHelpMan getindexinfo() { return RPCHelpMan{ "getindexinfo", "Returns the status of one or all available indices currently " "running in the node.\n", { {"index_name", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Filter results for an index with a specific name."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", { {RPCResult::Type::OBJ, "name", "The name of the index", { {RPCResult::Type::BOOL, "synced", "Whether the index is synced or not"}, {RPCResult::Type::NUM, "best_block_height", "The block height to which the index is synced"}, }}, }, }, RPCExamples{HelpExampleCli("getindexinfo", "") + HelpExampleRpc("getindexinfo", "") + HelpExampleCli("getindexinfo", "txindex") + HelpExampleRpc("getindexinfo", "txindex")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { UniValue result(UniValue::VOBJ); const std::string index_name = request.params[0].isNull() ? "" : request.params[0].get_str(); if (g_txindex) { result.pushKVs( SummaryToJSON(g_txindex->GetSummary(), index_name)); } if (g_coin_stats_index) { result.pushKVs(SummaryToJSON(g_coin_stats_index->GetSummary(), index_name)); } ForEachBlockFilterIndex([&result, &index_name]( const BlockFilterIndex &index) { result.pushKVs(SummaryToJSON(index.GetSummary(), index_name)); }); return result; }, }; } static RPCHelpMan gettime() { return RPCHelpMan{ "gettime", "Returns the node time information\n", {}, RPCResult{ RPCResult::Type::OBJ, "time", "", { {RPCResult::Type::NUM, "local", "The node local timestamp"}, {RPCResult::Type::NUM, "offset", "The time offset gathered from the other nodes on the " "network"}, {RPCResult::Type::NUM, "adjusted", "The adjusted timestamp of this node"}, }, }, RPCExamples{HelpExampleCli("gettime", "") + HelpExampleRpc("gettime", "")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { UniValue timeObj(UniValue::VOBJ); timeObj.pushKV("local", GetTime()); timeObj.pushKV("offset", GetTimeOffset()); timeObj.pushKV("adjusted", TicksSinceEpoch<std::chrono::seconds>( GetAdjustedTime())); return timeObj; }, }; } static RPCHelpMan getinfo() { return RPCHelpMan{ "getinfo", "Returns basic information about the node\n", {}, RPCResult{ RPCResult::Type::OBJ, "info", "", { {RPCResult::Type::STR_HEX, "version_number", "The version number"}, {RPCResult::Type::STR, "version_full", "The full version as a string"}, {RPCResult::Type::BOOL, "avalanche", "Wether avalanche is enabled"}, }, }, RPCExamples{HelpExampleCli("getinfo", "") + HelpExampleRpc("getinfo", "")}, [&](const RPCHelpMan &self, const Config &config, const JSONRPCRequest &request) -> UniValue { NodeContext &node = EnsureAnyNodeContext(request.context); UniValue infoObj(UniValue::VOBJ); infoObj.pushKV("version_number", CLIENT_VERSION); infoObj.pushKV("version_full", FormatFullVersion()); infoObj.pushKV("avalanche", !!node.avalanche); return infoObj; }, }; } void RegisterMiscRPCCommands(CRPCTable &t) { // clang-format off static const CRPCCommand commands[] = { // category actor (function) // ------------------ ---------------------- { "control", getmemoryinfo, }, { "control", logging, }, { "util", validateaddress, }, { "util", createmultisig, }, { "util", deriveaddresses, }, { "util", getdescriptorinfo, }, { "util", verifymessage, }, { "util", signmessagewithprivkey, }, { "util", getcurrencyinfo, }, { "util", getindexinfo, }, { "util", gettime, }, { "util", getinfo, }, /* Not shown in help */ { "hidden", setmocktime, }, { "hidden", mockscheduler, }, { "hidden", echo, }, { "hidden", echojson, }, }; // clang-format on for (const auto &c : commands) { t.appendCommand(c.name, &c); } } diff --git a/src/test/fuzz/http_request.cpp b/src/test/fuzz/http_request.cpp index 69bd50ceb..dc3a17c32 100644 --- a/src/test/fuzz/http_request.cpp +++ b/src/test/fuzz/http_request.cpp @@ -1,93 +1,78 @@ // 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 <httpserver.h> #include <netaddress.h> #include <util/strencodings.h> #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> #include <event2/buffer.h> #include <event2/event.h> #include <event2/http.h> #include <event2/http_struct.h> #include <cassert> #include <cstdint> #include <string> #include <vector> -// workaround for libevent versions before 2.1.1, -// when internal functions didn't have underscores at the end -#if LIBEVENT_VERSION_NUMBER < 0x02010100 -extern "C" int evhttp_parse_firstline(struct evhttp_request *, - struct evbuffer *); -extern "C" int evhttp_parse_headers(struct evhttp_request *, struct evbuffer *); -inline int evhttp_parse_firstline_(struct evhttp_request *r, - struct evbuffer *b) { - return evhttp_parse_firstline(r, b); -} -inline int evhttp_parse_headers_(struct evhttp_request *r, struct evbuffer *b) { - return evhttp_parse_headers(r, b); -} -#else extern "C" int evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer *); extern "C" int evhttp_parse_headers_(struct evhttp_request *, struct evbuffer *); -#endif std::string RequestMethodString(HTTPRequest::RequestMethod m); FUZZ_TARGET(http_request) { FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; evhttp_request *evreq = evhttp_request_new(nullptr, nullptr); assert(evreq != nullptr); evreq->kind = EVHTTP_REQUEST; evbuffer *evbuf = evbuffer_new(); assert(evbuf != nullptr); const std::vector<uint8_t> http_buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, 4096); evbuffer_add(evbuf, http_buffer.data(), http_buffer.size()); // Avoid constructing requests that will be interpreted by libevent as PROXY // requests to avoid triggering a nullptr dereference. The dereference // (req->evcon->http_server) takes place in evhttp_parse_request_line and is // a consequence of our hacky but necessary use of the internal function // evhttp_parse_firstline_ in this fuzzing harness. The workaround is not // aesthetically pleasing, but it successfully avoids the troublesome code // path. " http:// HTTP/1.1\n" was a crashing input prior to this // workaround. const std::string http_buffer_str = ToLower(std::string{http_buffer.begin(), http_buffer.end()}); if (http_buffer_str.find(" http://") != std::string::npos || http_buffer_str.find(" https://") != std::string::npos || evhttp_parse_firstline_(evreq, evbuf) != 1 || evhttp_parse_headers_(evreq, evbuf) != 1) { evbuffer_free(evbuf); evhttp_request_free(evreq); return; } HTTPRequest http_request{evreq, true}; const HTTPRequest::RequestMethod request_method = http_request.GetRequestMethod(); (void)RequestMethodString(request_method); (void)http_request.GetURI(); (void)http_request.GetHeader("Host"); const std::string header = fuzzed_data_provider.ConsumeRandomLengthString(16); (void)http_request.GetHeader(header); (void)http_request.WriteHeader( header, fuzzed_data_provider.ConsumeRandomLengthString(16)); (void)http_request.GetHeader(header); const std::string body = http_request.ReadBody(); assert(body.empty()); const CService service = http_request.GetPeer(); assert(service.ToString() == "[::]:0"); evbuffer_free(evbuf); evhttp_request_free(evreq); }