@@ -82,13 +82,12 @@ static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
82
82
return CONTINUE_EXECUTION;
83
83
}
84
84
85
- static void grind_task (uint32_t nBits, CBlockHeader& header_orig , uint32_t offset, uint32_t step, std::atomic<bool >& found)
85
+ static void grind_task (uint32_t nBits, CBlockHeader header , uint32_t offset, uint32_t step, std::atomic<bool >& found, uint32_t & proposed_nonce )
86
86
{
87
87
arith_uint256 target;
88
88
bool neg, over;
89
89
target.SetCompact (nBits, &neg, &over);
90
90
if (target == 0 || neg || over) return ;
91
- CBlockHeader header = header_orig; // working copy
92
91
header.nNonce = offset;
93
92
94
93
uint32_t finish = std::numeric_limits<uint32_t >::max () - step;
@@ -99,7 +98,7 @@ static void grind_task(uint32_t nBits, CBlockHeader& header_orig, uint32_t offse
99
98
do {
100
99
if (UintToArith256 (header.GetHash ()) <= target) {
101
100
if (!found.exchange (true )) {
102
- header_orig. nNonce = header.nNonce ;
101
+ proposed_nonce = header.nNonce ;
103
102
}
104
103
return ;
105
104
}
@@ -123,16 +122,19 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
123
122
124
123
uint32_t nBits = header.nBits ;
125
124
std::atomic<bool > found{false };
125
+ uint32_t proposed_nonce{};
126
126
127
127
std::vector<std::thread> threads;
128
128
int n_tasks = std::max (1u , std::thread::hardware_concurrency ());
129
129
for (int i = 0 ; i < n_tasks; ++i) {
130
- threads.emplace_back ( grind_task, nBits, std::ref ( header) , i, n_tasks, std::ref (found) );
130
+ threads.emplace_back (grind_task, nBits, header, i, n_tasks, std::ref (found), std::ref (proposed_nonce) );
131
131
}
132
132
for (auto & t : threads) {
133
133
t.join ();
134
134
}
135
- if (!found) {
135
+ if (found) {
136
+ header.nNonce = proposed_nonce;
137
+ } else {
136
138
strPrint = " Could not satisfy difficulty target" ;
137
139
return EXIT_FAILURE;
138
140
}
0 commit comments