Skip to content

Commit caa2240

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26120: refactor: Make bitcoin-util grind_task tsan friendly
fafcc94 Make bitcoin-util grind_task tsan friendly (MacroFake) Pull request description: While there is no issue with the current code, `libtsan-12.2.1` on my machine does not seem to like it. This is understandable, because the nonce isn't protected by a mutex that the sanitizer can see (only by an atomic, which achieves the same). Fix this by guarding the nonce by the existing atomic bool, which tsan seems to understand. ACKs for top commit: ajtowns: ACK fafcc94 hebasto: ACK fafcc94, I have reviewed the code and it looks OK, I agree it can be merged. Confirming that initial bug has been fixed. Tree-SHA512: 4e67fab5833ec7d91678b85a300368892ee9f7cd89a52cc5e15a7df65b2da813b24eaffd8362d0d8a3c8951e024041d69ebddf25101b11d0a1a62c1208ddc9a5
2 parents 66c08e7 + fafcc94 commit caa2240

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/bitcoin-util.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,12 @@ static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
8282
return CONTINUE_EXECUTION;
8383
}
8484

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)
8686
{
8787
arith_uint256 target;
8888
bool neg, over;
8989
target.SetCompact(nBits, &neg, &over);
9090
if (target == 0 || neg || over) return;
91-
CBlockHeader header = header_orig; // working copy
9291
header.nNonce = offset;
9392

9493
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
9998
do {
10099
if (UintToArith256(header.GetHash()) <= target) {
101100
if (!found.exchange(true)) {
102-
header_orig.nNonce = header.nNonce;
101+
proposed_nonce = header.nNonce;
103102
}
104103
return;
105104
}
@@ -123,16 +122,19 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
123122

124123
uint32_t nBits = header.nBits;
125124
std::atomic<bool> found{false};
125+
uint32_t proposed_nonce{};
126126

127127
std::vector<std::thread> threads;
128128
int n_tasks = std::max(1u, std::thread::hardware_concurrency());
129129
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));
131131
}
132132
for (auto& t : threads) {
133133
t.join();
134134
}
135-
if (!found) {
135+
if (found) {
136+
header.nNonce = proposed_nonce;
137+
} else {
136138
strPrint = "Could not satisfy difficulty target";
137139
return EXIT_FAILURE;
138140
}

0 commit comments

Comments
 (0)