@@ -154,9 +154,16 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
154
154
bool test_havecoin_after = InsecureRandBits (2 ) == 0 ;
155
155
156
156
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 ));
158
162
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
+ }
160
167
161
168
if (test_havecoin_after) {
162
169
bool ret = stack.back ()->HaveCoin (COutPoint (txid, 0 ));
@@ -167,24 +174,29 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
167
174
Coin newcoin;
168
175
newcoin.out .nValue = InsecureRand32 ();
169
176
newcoin.nHeight = 1 ;
177
+
178
+ // Infrequently test adding unspendable coins.
170
179
if (InsecureRandRange (16 ) == 0 && coin.IsSpent ()) {
171
180
newcoin.out .scriptPubKey .assign (1 + InsecureRandBits (6 ), OP_RETURN);
172
181
BOOST_CHECK (newcoin.out .scriptPubKey .IsUnspendable ());
173
182
added_an_unspendable_entry = true ;
174
183
} 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 );
176
186
(coin.IsSpent () ? added_an_entry : updated_an_entry) = true ;
177
187
coin = newcoin;
178
188
}
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);
180
191
} else {
192
+ // Spend the coin.
181
193
removed_an_entry = true ;
182
194
coin.Clear ();
183
195
BOOST_CHECK (stack.back ()->SpendCoin (COutPoint (txid, 0 )));
184
196
}
185
197
}
186
198
187
- // One every 10 iterations, remove a random entry from the cache
199
+ // Once every 10 iterations, remove a random entry from the cache
188
200
if (InsecureRandRange (10 ) == 0 ) {
189
201
COutPoint out (txids[InsecureRand32 () % txids.size ()], 0 );
190
202
int cacheid = InsecureRand32 () % stack.size ();
0 commit comments