5#include <bitcoin-build-config.h>
37 argsman.AddCommand(
"grind",
"Perform proof of work on hex header string");
49 tfm::format(std::cerr,
"Error parsing command line arguments: %s\n", error);
61 "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"
63 "Usage: bitcoin-util [options] [command]\n"
64 "or: bitcoin-util [options] grind <hex-block-header>\n";
71 tfm::format(std::cerr,
"Error: too few parameters\n");
80 }
catch (
const std::exception&
e) {
96 uint32_t finish = std::numeric_limits<uint32_t>::max() -
step;
97 finish = finish - (finish %
step) + offset;
99 while (!found && header.
nNonce < finish) {
103 if (!found.exchange(
true)) {
109 }
while(header.
nNonce != next);
115 if (
args.size() != 1) {
116 strPrint =
"Must specify block header to grind";
122 strPrint =
"Could not decode block header";
127 std::atomic<bool> found{
false};
130 std::vector<std::thread>
threads;
131 int n_tasks = std::max(1u, std::thread::hardware_concurrency());
133 for (
int i = 0; i <
n_tasks; ++i) {
142 strPrint =
"Could not satisfy difficulty target";
162 }
catch (
const std::exception&
e) {
172 tfm::format(std::cerr,
"Error: must specify a command\n");
179 if (
cmd->command ==
"grind") {
184 }
catch (
const std::exception&
e) {
185 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)
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.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
@ ALLOW_ANY
disable validation
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
bool ParseParameters(int argc, const char *const argv[], std::string &error)
std::string GetHelpMessage() const
Get the help string.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
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()
std::string LicenseInfo()
Returns licensing information (for -version)
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::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 auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.