Skip to content

Commit 6d8affc

Browse files
committed
test: refactor: clarify the coins simulation
Adds comments, slight refactor clarifications to make the code easier to follow.
1 parent 79cedc3 commit 6d8affc

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/test/coins_tests.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,16 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
154154
bool test_havecoin_after = InsecureRandBits(2) == 0;
155155

156156
bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
157-
const Coin& entry = (InsecureRandRange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
157+
158+
// Infrequently, test usage of AccessByTxid instead of AccessCoin - the
159+
// former just delegates to the latter and returns the first unspent in a txn.
160+
const Coin& entry = (InsecureRandRange(500) == 0) ?
161+
AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
158162
BOOST_CHECK(coin == entry);
159-
BOOST_CHECK(!test_havecoin_before || result_havecoin == !entry.IsSpent());
163+
164+
if (test_havecoin_before) {
165+
BOOST_CHECK(result_havecoin == !entry.IsSpent());
166+
}
160167

161168
if (test_havecoin_after) {
162169
bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
@@ -167,24 +174,29 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
167174
Coin newcoin;
168175
newcoin.out.nValue = InsecureRand32();
169176
newcoin.nHeight = 1;
177+
178+
// Infrequently test adding unspendable coins.
170179
if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
171180
newcoin.out.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN);
172181
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
173182
added_an_unspendable_entry = true;
174183
} else {
175-
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0); // Random sizes so we can test memory usage accounting
184+
// Random sizes so we can test memory usage accounting
185+
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
176186
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
177187
coin = newcoin;
178188
}
179-
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), !coin.IsSpent() || InsecureRand32() & 1);
189+
bool is_overwrite = !coin.IsSpent() || InsecureRand32() & 1;
190+
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
180191
} else {
192+
// Spend the coin.
181193
removed_an_entry = true;
182194
coin.Clear();
183195
BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
184196
}
185197
}
186198

187-
// One every 10 iterations, remove a random entry from the cache
199+
// Once every 10 iterations, remove a random entry from the cache
188200
if (InsecureRandRange(10) == 0) {
189201
COutPoint out(txids[InsecureRand32() % txids.size()], 0);
190202
int cacheid = InsecureRand32() % stack.size();

0 commit comments

Comments
 (0)