forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_rh44_thread_safety_tests.cpp
More file actions
312 lines (263 loc) · 10.1 KB
/
Copy pathdigidollar_rh44_thread_safety_tests.cpp
File metadata and controls
312 lines (263 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) 2025 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
/**
* RH-44: Race Condition & Thread Safety Deep Dive
*
* Tests for thread safety bugs in DigiDollar-specific code paths:
* 1. SystemHealthMonitor static metrics (OnMintConnected/OnRedeemConnected)
* 2. ERR static state (s_currentState read/write across threads)
* 3. OracleBundleManager::RemovePriceCache (wrong mutex for cached_price)
* 4. Concurrent oracle price updates
* 5. Health monitor read/write across threads
*/
#include <boost/test/unit_test.hpp>
#include <consensus/err.h>
#include <digidollar/health.h>
#include <oracle/bundle_manager.h>
#include <atomic>
#include <thread>
#include <vector>
struct RH44ThreadSafetyTestSetup {
RH44ThreadSafetyTestSetup()
{
ResetSharedState();
}
~RH44ThreadSafetyTestSetup()
{
ResetSharedState();
}
static void ResetSharedState()
{
DigiDollar::ERR::EmergencyRedemptionRatio::ResetForTesting();
DigiDollar::SystemHealthMonitor::ResetMetrics();
}
};
BOOST_FIXTURE_TEST_SUITE(digidollar_rh44_thread_safety_tests, RH44ThreadSafetyTestSetup)
// ---------------------------------------------------------------------------
// Test 1: SystemHealthMonitor concurrent OnMintConnected / OnRedeemConnected
// Pre-fix: bare static increments = data races (UB under TSan)
// Post-fix: mutex-protected, totals consistent
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(health_monitor_concurrent_mint_redeem)
{
// Reset metrics
DigiDollar::SystemHealthMonitor::ResetMetrics();
constexpr int N = 1000;
constexpr CAmount dd_per_op = 100; // cents
constexpr CAmount dgb_per_op = 100000000; // 1 DGB
// Phase 1: Concurrent mints from 10 threads
std::vector<std::thread> threads;
threads.reserve(10);
for (int t = 0; t < 10; ++t) {
threads.emplace_back([&] {
for (int i = 0; i < N; ++i) {
DigiDollar::SystemHealthMonitor::OnMintConnected(dd_per_op, dgb_per_op);
}
});
}
for (auto& th : threads) th.join();
threads.clear();
// After 10 * 1000 mints:
auto m1 = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
BOOST_CHECK_EQUAL(m1.totalDDSupply, 10 * N * dd_per_op);
BOOST_CHECK_EQUAL(m1.totalCollateral, 10 * N * dgb_per_op);
// Phase 2: Concurrent redeems from 10 threads (same count)
for (int t = 0; t < 10; ++t) {
threads.emplace_back([&] {
for (int i = 0; i < N; ++i) {
DigiDollar::SystemHealthMonitor::OnRedeemConnected(dd_per_op, dgb_per_op);
}
});
}
for (auto& th : threads) th.join();
// After equal redeems, totals should be 0
auto m = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
BOOST_CHECK_EQUAL(m.totalDDSupply, 0);
BOOST_CHECK_EQUAL(m.totalCollateral, 0);
}
// ---------------------------------------------------------------------------
// Test 2: SystemHealthMonitor concurrent mint + read (GetCachedMetrics)
// Pre-fix: torn reads on s_currentMetrics fields
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(health_monitor_concurrent_write_read)
{
DigiDollar::SystemHealthMonitor::ResetMetrics();
std::atomic<bool> stop{false};
constexpr CAmount dd_per = 100;
constexpr CAmount dgb_per = 100000000;
// Writer thread
std::thread writer([&] {
for (int i = 0; i < 5000 && !stop; ++i) {
DigiDollar::SystemHealthMonitor::OnMintConnected(dd_per, dgb_per);
}
});
// Reader thread — must not see negative values (torn read symptom)
std::atomic<bool> saw_negative{false};
std::thread reader([&] {
for (int i = 0; i < 5000 && !stop; ++i) {
auto m = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
if (m.totalDDSupply < 0 || m.totalCollateral < 0) {
saw_negative = true;
stop = true;
}
}
});
writer.join();
stop = true;
reader.join();
BOOST_CHECK(!saw_negative);
// Supply should be exactly 5000 * 100 = 500000
auto m = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
BOOST_CHECK_EQUAL(m.totalDDSupply, 500000);
}
// ---------------------------------------------------------------------------
// Test 3: ERR state concurrent read/write
// Pre-fix: s_currentState written by UpdateERRState, read by GetCurrentState
// with zero synchronization
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(err_state_concurrent_access)
{
using namespace DigiDollar::ERR;
// Reset ERR state
EmergencyRedemptionRatio::ReconstructERRState(100, 1000);
std::atomic<bool> stop{false};
std::atomic<int> read_count{0};
// Writer: toggle ERR state
std::thread writer([&] {
for (int i = 0; i < 2000 && !stop; ++i) {
int health = (i % 2 == 0) ? 80 : 100;
EmergencyRedemptionRatio::ReconstructERRState(health, 1000 + i);
}
});
// Readers: GetCurrentState should never crash or return garbage
std::atomic<bool> saw_bad{false};
std::vector<std::thread> readers;
for (int t = 0; t < 4; ++t) {
readers.emplace_back([&] {
for (int i = 0; i < 2000 && !stop; ++i) {
ERRState state = EmergencyRedemptionRatio::GetCurrentState();
// Adjustment ratio must be in [0.0, 1.0] or exactly 0
if (state.adjustmentRatio < 0.0 || state.adjustmentRatio > 1.0) {
saw_bad = true;
stop = true;
}
// systemHealth must be non-negative
if (state.systemHealth < 0) {
saw_bad = true;
stop = true;
}
read_count++;
}
});
}
writer.join();
stop = true;
for (auto& th : readers) th.join();
BOOST_CHECK(!saw_bad);
BOOST_CHECK(read_count > 0);
}
// ---------------------------------------------------------------------------
// Test 4: OracleBundleManager::RemovePriceCache writes cached_price
// under mtx_price_cache instead of mtx_bundles — data race with GetLatestPrice
// Pre-fix: RemovePriceCache updates cached_price under wrong mutex
// Post-fix: RemovePriceCache holds mtx_bundles when writing cached_price
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(bundle_manager_remove_price_cache_mutex)
{
OracleBundleManager mgr;
mgr.SetEnabled(true);
// Populate price cache
for (int h = 100; h < 200; ++h) {
mgr.UpdatePriceCache(h, 50000 + h);
}
std::atomic<bool> stop{false};
std::atomic<bool> saw_bad{false};
// Thread 1: remove prices
std::thread remover([&] {
for (int h = 100; h < 200 && !stop; ++h) {
mgr.RemovePriceCache(h);
}
});
// Thread 2: read latest price
std::thread reader([&] {
for (int i = 0; i < 5000 && !stop; ++i) {
CAmount p = mgr.GetLatestPrice();
// Price must be non-negative
if (p < 0) {
saw_bad = true;
stop = true;
}
}
});
remover.join();
stop = true;
reader.join();
BOOST_CHECK(!saw_bad);
}
// ---------------------------------------------------------------------------
// Test 5: OnMintConnected / OnRedeemConnected during simulated reorg
// Pre-fix: no atomicity guarantee between connect/disconnect
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(health_monitor_reorg_consistency)
{
DigiDollar::SystemHealthMonitor::ResetMetrics();
// Simulate: connect 100 mints, then disconnect them all (reorg)
constexpr CAmount dd = 500;
constexpr CAmount dgb = 500000000;
std::vector<std::thread> threads;
// 5 threads each connect 20 mints
for (int t = 0; t < 5; ++t) {
threads.emplace_back([&] {
for (int i = 0; i < 20; ++i) {
DigiDollar::SystemHealthMonitor::OnMintConnected(dd, dgb);
}
});
}
for (auto& th : threads) th.join();
threads.clear();
auto m1 = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
BOOST_CHECK_EQUAL(m1.totalDDSupply, 100 * dd);
BOOST_CHECK_EQUAL(m1.totalCollateral, 100 * dgb);
// Now disconnect all 100 from 5 threads
for (int t = 0; t < 5; ++t) {
threads.emplace_back([&] {
for (int i = 0; i < 20; ++i) {
DigiDollar::SystemHealthMonitor::OnMintDisconnected(dd, dgb);
}
});
}
for (auto& th : threads) th.join();
auto m2 = DigiDollar::SystemHealthMonitor::GetCachedMetrics();
BOOST_CHECK_EQUAL(m2.totalDDSupply, 0);
BOOST_CHECK_EQUAL(m2.totalCollateral, 0);
}
// ---------------------------------------------------------------------------
// Test 6: ERR queue concurrent access
// Pre-fix: s_errQueue/s_queuedRedemptions unprotected
// ---------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(err_queue_concurrent_access)
{
using namespace DigiDollar::ERR;
// Activate ERR
EmergencyRedemptionRatio::ReconstructERRState(80, 1000);
// Force active for queueing
ERRState state = EmergencyRedemptionRatio::GetCurrentState();
std::atomic<int> queued{0};
std::vector<std::thread> threads;
for (int t = 0; t < 4; ++t) {
threads.emplace_back([&, t] {
for (int i = 0; i < 100; ++i) {
COutPoint outpoint(uint256::ONE, t * 100 + i);
if (EmergencyRedemptionRatio::QueueERRRedemption(outpoint, 100, 1000 + i)) {
queued++;
}
}
});
}
for (auto& th : threads) th.join();
// All 400 should be queued (unique outpoints)
auto queue = EmergencyRedemptionRatio::GetERRQueue();
BOOST_CHECK_EQUAL(queue.size(), static_cast<size_t>(queued.load()));
}
BOOST_AUTO_TEST_SUITE_END()