forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multiprocess: Add bitcoin-mine test program
See src/bitcoin-mine.cpp for usage information. Co-authored-by: Sjors Provoost <[email protected]>
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters