5#include <bitcoin-build-config.h>
39 argsman.
AddCommand(
"grind",
"Perform proof of work on hex header string");
40 argsman.
AddCommand(
"getchainparams",
"Get hardcoded parameters for the selected chain");
52 tfm::format(std::cerr,
"Error parsing command line arguments: %s\n", error);
58 std::string strUsage = CLIENT_NAME
" bitcoin-util utility version " +
FormatFullVersion() +
"\n";
64 "The bitcoin-util tool provides bitcoin related functionality that does not rely on the ability to access a running node. Available [commands] are listed below.\n"
66 "Usage: bitcoin-util [options] [command]\n"
67 "or: bitcoin-util [options] grind <hex-block-header>\n";
74 tfm::format(std::cerr,
"Error: too few parameters\n");
83 }
catch (
const std::exception& e) {
91static void grind_task(uint32_t nBits,
CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
96 if (target == 0 || neg || over)
return;
99 uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
100 finish = finish - (finish % step) + offset;
102 while (!found && header.
nNonce < finish) {
103 const uint32_t next = (finish - header.
nNonce < 5000*step) ? finish : header.
nNonce + 5000*step;
106 if (!found.exchange(
true)) {
107 proposed_nonce = header.
nNonce;
112 }
while(header.
nNonce != next);
118 if (
args.size() != 1) {
119 strPrint =
"Must specify block header to grind";
125 strPrint =
"Could not decode block header";
129 uint32_t nBits = header.
nBits;
130 std::atomic<bool> found{
false};
131 uint32_t proposed_nonce{};
133 std::vector<std::thread> threads;
134 int n_tasks = std::max(1u, std::thread::hardware_concurrency());
135 threads.reserve(n_tasks);
136 for (
int i = 0; i < n_tasks; ++i) {
137 threads.emplace_back(
grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
139 for (
auto&
t : threads) {
143 header.
nNonce = proposed_nonce;
145 strPrint =
"Could not satisfy difficulty target";
158 strPrint =
"getchainparams does not take arguments";
162 const auto& params =
Params();
163 const auto& consensus = params.GetConsensus();
166 result.pushKV(
"chain", params.GetChainTypeString());
167 result.pushKV(
"test_chain", params.IsTestChain());
168 result.pushKV(
"genesis",
HexStr(consensus.hashGenesisBlock));
169 result.pushKV(
"subsidy_halving_interval", consensus.nSubsidyHalvingInterval);
171 if (consensus.signet_blocks) {
173 signet.pushKV(
"challenge",
HexStr(consensus.signet_challenge));
174 result.pushKV(
"signet", signet);
179 pow.pushKV(
"limit", consensus.powLimit.ToString());
180 if (!consensus.fPowNoRetargeting) {
181 pow.pushKV(
"target_spacing",
TicksSeconds(consensus.PowTargetSpacing()));
182 pow.pushKV(
"difficulty_retarget_interval", consensus.DifficultyAdjustmentInterval());
183 std::string mindiff_blocks = (consensus.fPowAllowMinDifficultyBlocks ?
184 (consensus.enforce_BIP94 ?
"bip94" :
"yes") :
"no");
185 pow.pushKV(
"mindiff_blocks", mindiff_blocks);
187 result.pushKV(
"pow", pow);
192 net.pushKV(
"default_port", params.GetDefaultPort());
193 net.pushKV(
"magic",
HexStr(params.MessageStart()));
195 for (
const auto& seed : params.DNSSeeds()) {
198 net.pushKV(
"dns_seeds", dns);
199 result.pushKV(
"net", net);
204 addr.pushKV(
"bech32_hrp", params.Bech32HRP());
205 result.pushKV(
"addresses", addr);
222 }
catch (
const std::exception& e) {
232 tfm::format(std::cerr,
"Error: must specify a command\n");
239 if (
cmd->command ==
"grind") {
241 }
else if (
cmd->command ==
"getchainparams") {
246 }
catch (
const std::exception& e) {
247 strPrint = std::string(
"error: ") + e.what();
bool HelpRequested(const ArgsManager &args)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
arith_uint256 UintToArith256(const uint256 &a)
static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic< bool > &found, uint32_t &proposed_nonce)
static const int CONTINUE_EXECUTION
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
if(ret !=CONTINUE_EXECUTION)
static int GetChainParams(const std::vector< std::string > &args, std::string &strPrint)
static int Grind(const std::vector< std::string > &args, std::string &strPrint)
const TranslateFn G_TRANSLATION_FUN
Translate string to current locale using Qt.
static int AppInitUtil(ArgsManager &args, int argc, char *argv[])
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
const CChainParams & Params()
Return the currently selected parameters.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
bool ParseParameters(int argc, const char *const argv[], std::string &error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
ChainType GetChainType() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type from the program arguments.
@ ALLOW_ANY
disable validation
void AddCommand(const std::string &cmd, const std::string &help, std::set< std::string > options={}) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add command.
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add argument.
std::optional< const Command > GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the command and command args (returns std::nullopt if no command provided)
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
std::string GetHelpMessage() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the help string.
Double ended buffer combining vector and stream-like interfaces.
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
std::string FormatFullVersion()
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string LicenseInfo()
Returns licensing information (for -version)
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
constexpr int64_t TicksSeconds(Duration d)