Skip to content

Commit

Permalink
multiprocess: Add bitcoin-mine test program
Browse files Browse the repository at this point in the history
See src/bitcoin-mine.cpp for usage information.

Co-authored-by: Sjors Provoost <[email protected]>
  • Loading branch information
ryanofsky and Sjors committed Sep 26, 2024
1 parent aec7e7d commit 2f9c2f4
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ if(WITH_MULTIPROCESS)
)
list(APPEND installable_targets bitcoin-node)

add_executable(bitcoin-mine
bitcoin-mine.cpp
init/bitcoin-mine.cpp
)
target_link_libraries(bitcoin-mine
core_interface
bitcoin_common
bitcoin_ipc
)
list(APPEND installable_targets bitcoin-mine)

if(BUILD_TESTS)
# bitcoin_ipc_test library target is defined here in src/CMakeLists.txt
# instead of src/test/CMakeLists.txt so capnp files in src/test/ are able to
Expand Down
124 changes: 124 additions & 0 deletions src/bitcoin-mine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) 2024 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 <config/bitcoin-config.h> // IWYU pragma: keep

#include <chainparams.h>
#include <chainparamsbase.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/system.h>
#include <compat/compat.h>
#include <init/common.h>
#include <interfaces/init.h>
#include <interfaces/ipc.h>
#include <logging.h>
#include <tinyformat.h>
#include <util/translation.h>

static const char* const HELP_USAGE{R"(
bitcoin-mine is a test program for interacting with bitcoin-node via IPC.
Usage:
bitcoin-mine [options]
)"};

static const char* HELP_EXAMPLES{R"(
Examples:
# Start separate bitcoin-node that bitcoin-mine can connect to.
bitcoin-node -regtest -ipcbind=unix
# Connect to bitcoin-node and print tip block hash.
bitcoin-mine -regtest
# Run with debug output.
bitcoin-mine -regtest -debug
)"};

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;

static void AddArgs(ArgsManager& args)
{
SetupHelpOptions(args);
SetupChainParamsBaseOptions(args);
args.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg("-ipcconnect=<address>", "Connect to bitcoin-node process in the background to perform online operations. Valid <address> values are 'unix' to connect to the default socket, 'unix:<socket path>' to connect to a socket at a nonstandard path. Default value: unix", ArgsManager::ALLOW_ANY, OptionsCategory::IPC);
init::AddLoggingArgs(args);
}

MAIN_FUNCTION
{
ArgsManager& args = gArgs;
AddArgs(args);
std::string error_message;
if (!args.ParseParameters(argc, argv, error_message)) {
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
return EXIT_FAILURE;
}
if (!args.ReadConfigFiles(error_message, true)) {
tfm::format(std::cerr, "Error reading config files: %s\n", error_message);
return EXIT_FAILURE;
}
if (HelpRequested(args) || args.IsArgSet("-version")) {
std::string output{strprintf("%s bitcoin-mine version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n"};
if (args.IsArgSet("-version")) {
output += FormatParagraph(LicenseInfo());
} else {
output += HELP_USAGE;
output += args.GetHelpMessage();
output += HELP_EXAMPLES;
}
tfm::format(std::cout, "%s", output);
return EXIT_SUCCESS;
}
if (!CheckDataDirOption(args)) {
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
return EXIT_FAILURE;
}
SelectParams(args.GetChainType());

// Set logging options but override -printtoconsole default to depend on -debug rather than -daemon
init::SetLoggingOptions(args);
if (auto result{init::SetLoggingCategories(args)}; !result) {
tfm::format(std::cerr, "Error: %s\n", util::ErrorString(result).original);
return EXIT_FAILURE;
}
if (auto result{init::SetLoggingLevel(args)}; !result) {
tfm::format(std::cerr, "Error: %s\n", util::ErrorString(result).original);
return EXIT_FAILURE;
}
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", LogInstance().GetCategoryMask());
if (!init::StartLogging(args)) {
tfm::format(std::cerr, "Error: StartLogging failed\n");
return EXIT_FAILURE;
}

// Connect to existing bitcoin-node process or spawn new one.
std::unique_ptr<interfaces::Init> mine_init{interfaces::MakeMineInit(argc, argv)};
assert(mine_init);
std::unique_ptr<interfaces::Init> node_init;
try {
std::string address{args.GetArg("-ipcconnect", "unix")};
node_init = mine_init->ipc()->connectAddress(address);
} catch (const std::exception& exception) {
tfm::format(std::cerr, "Error: %s\n", exception.what());
tfm::format(std::cerr, "Probably bitcoin-node is not running or not listening on a unix socket. Can be started with:\n\n");
tfm::format(std::cerr, " bitcoin-node -chain=%s -ipcbind=unix\n", args.GetChainTypeString());
return EXIT_FAILURE;
}
assert(node_init);
tfm::format(std::cout, "Connected to bitcoin-node\n");
std::unique_ptr<interfaces::Mining> mining{node_init->makeMining()};
assert(mining);

auto tip{mining->getTip()};
if (tip) {
tfm::format(std::cout, "Tip hash is %s.\n", tip->hash.ToString());
} else {
tfm::format(std::cout, "Tip hash is null.\n");
}

return EXIT_SUCCESS;
}
29 changes: 29 additions & 0 deletions src/init/bitcoin-mine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024 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 <interfaces/init.h>
#include <interfaces/ipc.h>

namespace init {
namespace {
const char* EXE_NAME = "bitcoin-mine";

class BitcoinMineInit : public interfaces::Init
{
public:
BitcoinMineInit(const char* arg0) : m_ipc(interfaces::MakeIpc(EXE_NAME, arg0, *this))
{
}
interfaces::Ipc* ipc() override { return m_ipc.get(); }
std::unique_ptr<interfaces::Ipc> m_ipc;
};
} // namespace
} // namespace init

namespace interfaces {
std::unique_ptr<Init> MakeMineInit(int argc, char* argv[])
{
return std::make_unique<init::BitcoinMineInit>(argc > 0 ? argv[0] : "");
}
} // namespace interfaces
3 changes: 3 additions & 0 deletions src/interfaces/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ std::unique_ptr<Init> MakeWalletInit(int argc, char* argv[], int& exit_status);

//! Return implementation of Init interface for the gui process.
std::unique_ptr<Init> MakeGuiInit(int argc, char* argv[]);

//! Return implementation of Init interface for the bitcoin-mine process.
std::unique_ptr<Init> MakeMineInit(int argc, char* argv[]);
} // namespace interfaces

#endif // BITCOIN_INTERFACES_INIT_H

0 comments on commit 2f9c2f4

Please sign in to comment.