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 "Usage: bitcoin-util [options] [commands] Do stuff\n";
68 tfm::format(std::cerr,
"Error: too few parameters\n");
77 }
catch (
const std::exception& e) {
85 static void grind_task(uint32_t nBits,
CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
90 if (target == 0 || neg || over)
return;
93 uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
94 finish = finish - (finish % step) + offset;
96 while (!found && header.
nNonce < finish) {
97 const uint32_t next = (finish - header.
nNonce < 5000*step) ? finish : header.
nNonce + 5000*step;
100 if (!found.exchange(
true)) {
101 proposed_nonce = header.
nNonce;
106 }
while(header.
nNonce != next);
112 if (
args.size() != 1) {
113 strPrint =
"Must specify block header to grind";
119 strPrint =
"Could not decode block header";
123 uint32_t nBits = header.
nBits;
124 std::atomic<bool> found{
false};
125 uint32_t proposed_nonce{};
127 std::vector<std::thread> threads;
128 int n_tasks = std::max(1u, std::thread::hardware_concurrency());
129 threads.reserve(n_tasks);
130 for (
int i = 0; i < n_tasks; ++i) {
131 threads.emplace_back(
grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
133 for (
auto&
t : threads) {
137 header.
nNonce = proposed_nonce;
139 strPrint =
"Could not satisfy difficulty target";
159 }
catch (
const std::exception& e) {
169 tfm::format(std::cerr,
"Error: must specify a command\n");
176 if (
cmd->command ==
"grind") {
181 }
catch (
const std::exception& e) {
182 strPrint = std::string(
"error: ") + e.what();
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
bool ParseParameters(int argc, const char *const argv[], std::string &error)
std::string LicenseInfo()
Returns licensing information (for -version)
static const int CONTINUE_EXECUTION
bool DecodeHexBlockHeader(CBlockHeader &, const std::string &hex_header)
static int Grind(const std::vector< std::string > &args, std::string &strPrint)
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
arith_uint256 UintToArith256(const uint256 &a)
std::string GetHelpMessage() const
Get the help string.
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
Double ended buffer combining vector and stream-like interfaces.
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
if(ret !=CONTINUE_EXECUTION)
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...
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
256-bit unsigned big integer.
std::string FormatFullVersion()
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
bool HelpRequested(const ArgsManager &args)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static int AppInitUtil(ArgsManager &args, int argc, char *argv[])
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...
static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic< bool > &found, uint32_t &proposed_nonce)
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.