Skip to content

Commit 44787d1

Browse files
committed
Start Template Provider with -sv2
1 parent fe053b0 commit 44787d1

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

src/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,15 @@ target_link_libraries(bitcoin_node
307307
$<TARGET_NAME_IF_EXISTS:libevent::extra>
308308
$<TARGET_NAME_IF_EXISTS:libevent::pthreads>
309309
$<TARGET_NAME_IF_EXISTS:USDT::headers>
310+
$<TARGET_NAME_IF_EXISTS:bitcoin_sv2>
310311
)
311312

313+
if(WITH_SV2)
314+
target_compile_definitions(bitcoin_node
315+
PUBLIC
316+
WITH_SV2=1
317+
)
318+
endif()
312319

313320
# Bitcoin Core bitcoind.
314321
if(BUILD_DAEMON)

src/init.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#include <node/mempool_persist_args.h>
5757
#include <node/miner.h>
5858
#include <node/peerman_args.h>
59+
#ifdef WITH_SV2
60+
#include <sv2/template_provider.h>
61+
#endif
5962
#include <policy/feerate.h>
6063
#include <policy/fees.h>
6164
#include <policy/fees_args.h>
@@ -271,6 +274,11 @@ void Interrupt(NodeContext& node)
271274
InterruptRPC();
272275
InterruptREST();
273276
InterruptTorControl();
277+
#ifdef WITH_SV2
278+
if (node.sv2_template_provider) {
279+
node.sv2_template_provider->Interrupt();
280+
}
281+
#endif
274282
InterruptMapPort();
275283
if (node.connman)
276284
node.connman->Interrupt();
@@ -310,6 +318,11 @@ void Shutdown(NodeContext& node)
310318

311319
StopTorControl();
312320

321+
#ifdef WITH_SV2
322+
// Stop Template Provider
323+
if (node.sv2_template_provider) node.sv2_template_provider->StopThreads();
324+
#endif
325+
313326
if (node.background_init_thread.joinable()) node.background_init_thread.join();
314327
// After everything has been shut down, but before things get flushed, stop the
315328
// the scheduler. After this point, SyncWithValidationInterfaceQueue() should not be called anymore
@@ -323,6 +336,9 @@ void Shutdown(NodeContext& node)
323336
node.banman.reset();
324337
node.addrman.reset();
325338
node.netgroupman.reset();
339+
#ifdef WITH_SV2
340+
node.sv2_template_provider.reset();
341+
#endif
326342

327343
if (node.mempool && node.mempool->GetLoadTried() && ShouldPersistMempool(*node.args)) {
328344
DumpMempool(*node.mempool, MempoolPath(*node.args));
@@ -652,6 +668,11 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
652668
argsman.AddArg("-blockreservedweight=<n>", strprintf("Reserve space for the fixed-size block header plus the largest coinbase transaction the mining software may add to the block. (default: %d).", DEFAULT_BLOCK_RESERVED_WEIGHT), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
653669
argsman.AddArg("-blockmintxfee=<amt>", strprintf("Set lowest fee rate (in %s/kvB) for transactions to be included in block creation. (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_BLOCK_MIN_TX_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
654670
argsman.AddArg("-blockversion=<n>", "Override block version to test forking scenarios", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::BLOCK_CREATION);
671+
#ifdef WITH_SV2
672+
argsman.AddArg("-sv2", "Bitcoind will act as a Stratum v2 Template Provider (default: false)", ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
673+
argsman.AddArg("-sv2interval", strprintf("Template Provider block template update interval (default: %d seconds)", Sv2TemplateProviderOptions().fee_check_interval.count()), ArgsManager::ALLOW_ANY, OptionsCategory::BLOCK_CREATION);
674+
argsman.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);
675+
#endif
655676

656677
argsman.AddArg("-rest", strprintf("Accept public REST requests (default: %u)", DEFAULT_REST_ENABLE), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
657678
argsman.AddArg("-rpcallowip=<ip>", "Allow JSON-RPC connections from specified source. Valid values for <ip> are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0), a network/CIDR (e.g. 1.2.3.4/24), all ipv4 (0.0.0.0/0), or all ipv6 (::/0). This option can be specified multiple times", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
@@ -673,6 +694,11 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
673694
argsman.AddArg("-ipcbind=<address>", "Bind to Unix socket address and listen for incoming connections. Valid address values are \"unix\" to listen on the default path, <datadir>/node.sock, or \"unix:/custom/path\" to specify a custom path. Can be specified multiple times to listen on multiple paths. Default behavior is not to listen on any path. If relative paths are specified, they are interpreted relative to the network data directory. If paths include any parent directory components and the parent directories do not exist, they will be created.", ArgsManager::ALLOW_ANY, OptionsCategory::IPC);
674695
}
675696

697+
#ifdef WITH_SV2
698+
argsman.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);
699+
argsman.AddArg("-sv2port=<port>", strprintf("Listen for Stratum v2 connections on <port> (default: %u, testnet: %u, signet: %u, regtest: %u)", defaultBaseParams->Sv2Port(), testnetBaseParams->Sv2Port(), signetBaseParams->Sv2Port(), regtestBaseParams->Sv2Port()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
700+
#endif
701+
676702
#if HAVE_DECL_FORK
677703
argsman.AddArg("-daemon", strprintf("Run in the background as a daemon and accept commands (default: %d)", DEFAULT_DAEMON), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
678704
argsman.AddArg("-daemonwait", strprintf("Wait for initialization to be finished before exiting. This implies -daemon (default: %d)", DEFAULT_DAEMONWAIT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -1171,6 +1197,9 @@ bool CheckHostPortOptions(const ArgsManager& args) {
11711197
{"-onion", true},
11721198
{"-proxy", true},
11731199
{"-rpcbind", false},
1200+
#ifdef WITH_SV2
1201+
{"-sv2bind", false},
1202+
#endif
11741203
{"-torcontrol", false},
11751204
{"-whitebind", false},
11761205
{"-zmqpubhashblock", true},
@@ -2032,6 +2061,49 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20322061
return false;
20332062
}
20342063

2064+
#ifdef WITH_SV2
2065+
if (args.GetBoolArg("-sv2", false)) {
2066+
assert(!node.sv2_template_provider);
2067+
assert(node.mining);
2068+
2069+
Sv2TemplateProviderOptions options{};
2070+
2071+
node.sv2_template_provider = std::make_unique<Sv2TemplateProvider>(*node.mining);
2072+
2073+
const std::string sv2_port_arg = args.GetArg("-sv2port", "");
2074+
2075+
if (sv2_port_arg.empty()) {
2076+
options.port = BaseParams().Sv2Port();
2077+
} else {
2078+
if (!ParseUInt16(sv2_port_arg, &options.port) || options.port == 0) {
2079+
return InitError(InvalidPortErrMsg("sv2port", sv2_port_arg));
2080+
}
2081+
}
2082+
2083+
if (gArgs.IsArgSet("-sv2bind")) { // Specific bind address
2084+
std::optional<std::string> sv2_bind{gArgs.GetArg("-sv2bind")};
2085+
if (sv2_bind) {
2086+
if (!SplitHostPort(sv2_bind.value(), options.port, options.host)) {
2087+
throw std::runtime_error(strprintf("Invalid port %d", options.port));
2088+
}
2089+
}
2090+
}
2091+
2092+
options.fee_delta = gArgs.GetIntArg("-sv2feedelta", Sv2TemplateProviderOptions().fee_delta);
2093+
2094+
if (gArgs.IsArgSet("-sv2interval")) {
2095+
if (args.GetIntArg("-sv2interval", 0) < 1) {
2096+
return InitError(Untranslated("-sv2interval must be at least one second"));
2097+
}
2098+
options.fee_check_interval = std::chrono::seconds(gArgs.GetIntArg("-sv2interval", 0));
2099+
}
2100+
2101+
if (!node.sv2_template_provider->Start(options)) {
2102+
return InitError(_("Unable to start Stratum v2 Template Provider"));
2103+
}
2104+
}
2105+
#endif
2106+
20352107
// ********************************************************* Step 13: finished
20362108

20372109
// At this point, the RPC is "started", but still in warmup, which means it

src/node/context.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <node/warnings.h>
1818
#include <policy/fees.h>
1919
#include <scheduler.h>
20+
#ifdef WITH_SV2
21+
#include <sv2/template_provider.h>
22+
#endif
2023
#include <txmempool.h>
2124
#include <validation.h>
2225
#include <validationinterface.h>

src/node/context.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class ChainstateManager;
2525
class ECC_Context;
2626
class NetGroupManager;
2727
class PeerManager;
28+
#ifdef WITH_SV2
2829
class Sv2TemplateProvider;
30+
#endif
2931
namespace interfaces {
3032
class Chain;
3133
class ChainClient;
@@ -40,6 +42,11 @@ namespace util {
4042
class SignalInterrupt;
4143
}
4244

45+
#ifndef WITH_SV2
46+
// Dummy
47+
class Sv2TemplateProvider {};
48+
#endif
49+
4350
namespace node {
4451
class KernelNotifications;
4552
class Warnings;
@@ -72,6 +79,7 @@ struct NodeContext {
7279
std::unique_ptr<PeerManager> peerman;
7380
std::unique_ptr<ChainstateManager> chainman;
7481
std::unique_ptr<BanMan> banman;
82+
std::unique_ptr<Sv2TemplateProvider> sv2_template_provider;
7583
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
7684
std::vector<BaseIndex*> indexes; // raw pointers because memory is not managed by this struct
7785
std::unique_ptr<interfaces::Chain> chain;

src/test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ if(WITH_SV2)
185185
sv2_messages_tests.cpp
186186
sv2_template_provider_tests.cpp
187187
)
188-
target_link_libraries(test_bitcoin bitcoin_sv2)
188+
# target_link_libraries is not needed here, because bitcoin_sv2 is included
189+
# by bitcoin_node below
189190
endif()
190191

191192
function(add_boost_test source_file)

0 commit comments

Comments
 (0)