-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsv2-tp.cpp
More file actions
320 lines (274 loc) · 12.4 KB
/
Copy pathsv2-tp.cpp
File metadata and controls
320 lines (274 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) 2025 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 <bitcoin-build-config.h> // IWYU pragma: keep
#include <chainparamsbase.h>
#include <chainparams.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 <sv2/template_provider.h>
#include <tinyformat.h>
#include <util/chaintype.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/syserror.h>
#include <util/translation.h>
#include <cerrno>
#ifndef WIN32
#include <csignal>
#endif
#include <fstream>
#include <system_error>
static const char* const HELP_USAGE{R"(
sv2-tp implements the Stratum v2 Template Provider role. It connects to Bitcoin
Core via IPC.
Usage:
sv2-tp [options]
)"};
static const char* HELP_EXAMPLES{R"(
Examples:
# Start separate bitcoin node that sv2-tp can connect to.
bitcoin -m node -testnet4 -ipcbind=unix
# Connect to the node:
sv2-tp -testnet4 -debug=sv2 -loglevel=sv2:trace
# Now start the SRI Job Declarator Client of Pool role, you should see
# it connect in the logs.
)"};
const TranslateFn G_TRANSLATION_FUN{nullptr};
namespace {
constexpr const char* SV2_TP_PID_FILENAME{"sv2-tp.pid"};
bool g_generated_pid{false};
fs::path GetPidFile(const ArgsManager& args)
{
return AbsPathForConfigVal(args, args.GetPathArg("-pid", SV2_TP_PID_FILENAME));
}
[[nodiscard]] bool CreatePidFile(const ArgsManager& args)
{
if (args.IsArgDisabled("-pid")) return true;
std::ofstream file{GetPidFile(args)};
if (file) {
#ifdef WIN32
file << GetCurrentProcessId() << '\n';
#else
file << getpid() << '\n';
#endif
g_generated_pid = true;
LogInfo("Created PID file %s", fs::PathToString(GetPidFile(args)));
return true;
}
LogError("Unable to create PID file '%s': %s", fs::PathToString(GetPidFile(args)), SysErrorString(errno));
return false;
}
void RemovePidFile(const ArgsManager& args)
{
if (!g_generated_pid) return;
const auto pid_path{GetPidFile(args)};
if (std::error_code error; !fs::remove(pid_path, error)) {
std::string msg{error ? error.message() : "File does not exist"};
LogPrintf("Unable to remove PID file (%s): %s\n", fs::PathToString(pid_path), msg);
}
}
class PidFileGuard
{
public:
explicit PidFileGuard(const ArgsManager& args) : m_args(args) {}
~PidFileGuard() { RemovePidFile(m_args); }
private:
const ArgsManager& m_args;
};
} // namespace
static void AddArgs(ArgsManager& args)
{
SetupHelpOptions(args);
SetupChainParamsBaseOptions(args);
const auto defaultBaseParams = CreateBaseChainParams(ChainType::MAIN);
const auto testnetBaseParams = CreateBaseChainParams(ChainType::TESTNET);
const auto testnet4BaseParams = CreateBaseChainParams(ChainType::TESTNET4);
const auto signetBaseParams = CreateBaseChainParams(ChainType::SIGNET);
const auto regtestBaseParams = CreateBaseChainParams(ChainType::REGTEST);
args.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg("-datadir=<dir>", "Specify non-default Bitcoin Core data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg("-conf=<file>", "Specify path to the configuration file (default: sv2-tp.conf inside the datadir). Set -conf=0 to disable loading any configuration file.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg("-pid=<file>", strprintf("Specify pid file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)", SV2_TP_PID_FILENAME), 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);
args.AddArg("-sv2bind=<addr>[:<port>]", strprintf("Bind to given address and always listen on it (default: 127.0.0.1). Use [host]:port notation for IPv6."), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
args.AddArg("-sv2port=<port>", strprintf("Listen for Stratum v2 connections on <port> (default: %u, testnet3: %u, testnet4: %u, signet: %u, regtest: %u).", defaultBaseParams->Sv2Port(), testnetBaseParams->Sv2Port(), testnet4BaseParams->Sv2Port(), signetBaseParams->Sv2Port(), regtestBaseParams->Sv2Port()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
args.AddArg("-templateinterval", strprintf("Minimum seconds between fee-based template updates. New blocks always propagate immediately. (default: %d)", Sv2TemplateProviderOptions().template_interval.count()), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
args.AddArg("-sv2feedelta", strprintf("Minimum fee delta for Template Provider to send update upstream (default: %d sat)", uint64_t(Sv2TemplateProviderOptions().fee_delta)), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
init::AddLoggingArgs(args);
}
static bool g_interrupt{false};
namespace {
constexpr auto IPC_RECONNECT_INITIAL_DELAY{std::chrono::seconds{1}};
constexpr auto IPC_RECONNECT_MAX_DELAY{std::chrono::seconds{32}};
constexpr auto IPC_RECONNECT_POLL_INTERVAL{100ms};
}
#ifndef WIN32
static void registerSignalHandler(int signal, void(*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(signal, &sa, nullptr);
}
static void HandleSIGTERM(int)
{
g_interrupt = true;
}
#endif
MAIN_FUNCTION
{
ArgsManager& args = gArgs;
PidFileGuard pid_file_guard{args};
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 sv2-tp version", CLIENT_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 using sv2-tp defaults
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", DEFAULT_PRINTTOCONSOLE);
if (!init::StartLogging(args)) {
tfm::format(std::cerr, "Error: StartLogging failed\n");
return EXIT_FAILURE;
}
if (!CreatePidFile(args)) {
tfm::format(std::cerr, "Error: Unable to create PID file at %s\n", fs::PathToString(GetPidFile(args)));
return EXIT_FAILURE;
}
const ChainType chain_type = args.GetChainType();
LogPrintf("Starting sv2-tp on %s network\n", ChainTypeToString(chain_type));
ECC_Context ecc_context{};
std::string sha256_algo = SHA256AutoDetect();
LogInfo("Using the '%s' SHA256 implementation\n", sha256_algo);
RandomInit();
// Parse -sv2... params
Sv2TemplateProviderOptions options{};
const std::string sv2_port_arg = args.GetArg("-sv2port", "");
// TODO: check -sv2port using something like CheckHostPortOptions
options.port = static_cast<uint16_t>(gArgs.GetIntArg("-sv2port", BaseParams().Sv2Port()));
if (args.IsArgSet("-sv2bind")) { // Specific bind address
std::optional<std::string> sv2_bind{args.GetArg("-sv2bind")};
if (sv2_bind) {
if (!SplitHostPort(sv2_bind.value(), options.port, options.host)) {
tfm::format(std::cerr, "Invalid port %d\n", options.port);
return EXIT_FAILURE;
}
}
}
options.fee_delta = args.GetIntArg("-sv2feedelta", Sv2TemplateProviderOptions().fee_delta);
if (args.IsArgSet("-templateinterval")) {
if (args.GetIntArg("-templateinterval", 0) < 1) {
tfm::format(std::cerr, "-templateinterval must be at least one second\n");
return EXIT_FAILURE;
}
options.template_interval = std::chrono::seconds(args.GetIntArg("-templateinterval", 0));
}
std::unique_ptr<interfaces::Init> mine_init{interfaces::MakeBasicInit("sv2-tp", argc > 0 ? argv[0] : "")};
assert(mine_init);
std::string address{args.GetArg("-ipcconnect", "unix")};
LogPrintf("Attempting IPC connection to bitcoin-node at %s\n", address);
LogPrintf("Ensure Bitcoin Core is running with '-ipcbind=unix' and the correct network (%s)\n",
ChainTypeToString(chain_type));
const auto connect_to_node = [&]() -> std::pair<std::unique_ptr<interfaces::Init>, std::unique_ptr<interfaces::Mining>> {
auto retry_delay = IPC_RECONNECT_INITIAL_DELAY;
while (!g_interrupt) {
try {
std::unique_ptr<interfaces::Init> node_init = mine_init->ipc()->connectAddress(address);
if (!node_init) {
throw std::runtime_error("IPC connection returned no remote init interface");
}
std::unique_ptr<interfaces::Mining> mining = node_init->makeMining();
if (!mining) {
throw std::runtime_error("IPC connection returned no mining interface");
}
LogPrintf("Connected to bitcoin-node via IPC at: %s\n", address);
return {std::move(node_init), std::move(mining)};
} catch (const std::exception& exception) {
LogPrintf("IPC connection failed: %s\n", exception.what());
LogPrintf("Retrying in %d seconds... (Ensure Bitcoin Core is running with '-ipcbind=unix')\n",
retry_delay.count());
auto waited{0ms};
while (!g_interrupt && waited < retry_delay) {
std::this_thread::sleep_for(IPC_RECONNECT_POLL_INTERVAL);
waited += IPC_RECONNECT_POLL_INTERVAL;
}
retry_delay = std::min(retry_delay * 2, IPC_RECONNECT_MAX_DELAY);
}
}
return {};
};
auto connection = connect_to_node();
auto node_init = std::move(connection.first);
auto mining = std::move(connection.second);
if (g_interrupt) return EXIT_SUCCESS;
assert(node_init);
assert(mining);
auto tp = std::make_unique<Sv2TemplateProvider>();
tp->ReplaceBackend(std::move(node_init), std::move(mining));
if (!tp->Start(options)) {
tfm::format(std::cerr, "Unable to start Stratum v2 Template Provider");
return EXIT_FAILURE;
}
LogPrintf("sv2-tp listening for Stratum v2 connections on %s:%d\n",
options.host, options.port);
#ifndef WIN32
registerSignalHandler(SIGTERM, HandleSIGTERM);
registerSignalHandler(SIGINT, HandleSIGTERM);
#endif
while (!g_interrupt) {
if (!tp->BackendDisconnected()) {
UninterruptibleSleep(100ms);
continue;
}
LogPrintf("Restarting sv2-tp after Bitcoin Core IPC disconnect\n");
connection = connect_to_node();
node_init = std::move(connection.first);
mining = std::move(connection.second);
if (g_interrupt) break;
assert(node_init);
assert(mining);
tp->ReplaceBackend(std::move(node_init), std::move(mining));
}
tp->Interrupt();
tp->StopThreads();
tp.reset();
return EXIT_SUCCESS;
}