Changeset View
Changeset View
Standalone View
Standalone View
src/test/coins_tests.cpp
Show First 20 Lines • Show All 183 Lines • ▼ Show 20 Lines | for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
} | } | ||||
Coin newcoin(txout, 1, false); | Coin newcoin(txout, 1, false); | ||||
stack.back()->AddCoin(COutPoint(txid, 0), newcoin, | stack.back()->AddCoin(COutPoint(txid, 0), newcoin, | ||||
!coin.IsSpent() || InsecureRand32() & 1); | !coin.IsSpent() || InsecureRand32() & 1); | ||||
} else { | } else { | ||||
removed_an_entry = true; | removed_an_entry = true; | ||||
coin.Clear(); | coin.Clear(); | ||||
stack.back()->SpendCoin(COutPoint(txid, 0)); | BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0))); | ||||
} | } | ||||
} | } | ||||
// One every 10 iterations, remove a random entry from the cache | // One every 10 iterations, remove a random entry from the cache | ||||
if (InsecureRandRange(10) == 0) { | if (InsecureRandRange(10) == 0) { | ||||
COutPoint out(txids[InsecureRand32() % txids.size()], 0); | COutPoint out(txids[InsecureRand32() % txids.size()], 0); | ||||
int cacheid = InsecureRand32() % stack.size(); | int cacheid = InsecureRand32() % stack.size(); | ||||
stack[cacheid]->Uncache(out); | stack[cacheid]->Uncache(out); | ||||
Show All 19 Lines | for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
test->SelfTest(); | test->SelfTest(); | ||||
} | } | ||||
} | } | ||||
// Every 100 iterations, flush an intermediate cache | // Every 100 iterations, flush an intermediate cache | ||||
if (InsecureRandRange(100) == 0) { | if (InsecureRandRange(100) == 0) { | ||||
if (stack.size() > 1 && InsecureRandBool() == 0) { | if (stack.size() > 1 && InsecureRandBool() == 0) { | ||||
unsigned int flushIndex = InsecureRandRange(stack.size() - 1); | unsigned int flushIndex = InsecureRandRange(stack.size() - 1); | ||||
stack[flushIndex]->Flush(); | BOOST_CHECK(stack[flushIndex]->Flush()); | ||||
} | } | ||||
} | } | ||||
if (InsecureRandRange(100) == 0) { | if (InsecureRandRange(100) == 0) { | ||||
// Every 100 iterations, change the cache stack. | // Every 100 iterations, change the cache stack. | ||||
if (stack.size() > 0 && InsecureRandBool() == 0) { | if (stack.size() > 0 && InsecureRandBool() == 0) { | ||||
// Remove the top cache | // Remove the top cache | ||||
stack.back()->Flush(); | BOOST_CHECK(stack.back()->Flush()); | ||||
delete stack.back(); | delete stack.back(); | ||||
stack.pop_back(); | stack.pop_back(); | ||||
} | } | ||||
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { | if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { | ||||
// Add a new cache | // Add a new cache | ||||
CCoinsView *tip = &base; | CCoinsView *tip = &base; | ||||
if (stack.size() > 0) { | if (stack.size() > 0) { | ||||
tip = stack.back(); | tip = stack.back(); | ||||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | for (int64_t i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
// If not coinbase restore prevout | // If not coinbase restore prevout | ||||
if (!tx.IsCoinBase()) { | if (!tx.IsCoinBase()) { | ||||
result[tx.vin[0].prevout] = orig_coin; | result[tx.vin[0].prevout] = orig_coin; | ||||
} | } | ||||
// Disconnect the tx from the current UTXO | // Disconnect the tx from the current UTXO | ||||
// See code in DisconnectBlock | // See code in DisconnectBlock | ||||
// remove outputs | // remove outputs | ||||
stack.back()->SpendCoin(utxod->first); | BOOST_CHECK(stack.back()->SpendCoin(utxod->first)); | ||||
// restore inputs | // restore inputs | ||||
if (!tx.IsCoinBase()) { | if (!tx.IsCoinBase()) { | ||||
const COutPoint &out = tx.vin[0].prevout; | const COutPoint &out = tx.vin[0].prevout; | ||||
UndoCoinSpend(undo.vprevout[0], *(stack.back()), out); | UndoCoinSpend(undo.vprevout[0], *(stack.back()), out); | ||||
} | } | ||||
// Store as a candidate for reconnection | // Store as a candidate for reconnection | ||||
Show All 30 Lines | for (int64_t i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
stack[InsecureRand32() % stack.size()]->Uncache( | stack[InsecureRand32() % stack.size()]->Uncache( | ||||
FindRandomFrom(duplicate_coins)->first); | FindRandomFrom(duplicate_coins)->first); | ||||
} | } | ||||
if (InsecureRandRange(100) == 0) { | if (InsecureRandRange(100) == 0) { | ||||
// Every 100 iterations, flush an intermediate cache | // Every 100 iterations, flush an intermediate cache | ||||
if (stack.size() > 1 && InsecureRandBool() == 0) { | if (stack.size() > 1 && InsecureRandBool() == 0) { | ||||
unsigned int flushIndex = InsecureRandRange(stack.size() - 1); | unsigned int flushIndex = InsecureRandRange(stack.size() - 1); | ||||
stack[flushIndex]->Flush(); | BOOST_CHECK(stack[flushIndex]->Flush()); | ||||
} | } | ||||
} | } | ||||
if (InsecureRandRange(100) == 0) { | if (InsecureRandRange(100) == 0) { | ||||
// Every 100 iterations, change the cache stack. | // Every 100 iterations, change the cache stack. | ||||
if (stack.size() > 0 && InsecureRandBool() == 0) { | if (stack.size() > 0 && InsecureRandBool() == 0) { | ||||
stack.back()->Flush(); | BOOST_CHECK(stack.back()->Flush()); | ||||
delete stack.back(); | delete stack.back(); | ||||
stack.pop_back(); | stack.pop_back(); | ||||
} | } | ||||
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { | if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { | ||||
CCoinsView *tip = &base; | CCoinsView *tip = &base; | ||||
if (stack.size() > 0) { | if (stack.size() > 0) { | ||||
tip = stack.back(); | tip = stack.back(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | if (it == map.end()) { | ||||
flags = it->second.flags; | flags = it->second.flags; | ||||
assert(flags != NO_ENTRY); | assert(flags != NO_ENTRY); | ||||
} | } | ||||
} | } | ||||
void WriteCoinViewEntry(CCoinsView &view, const Amount value, char flags) { | void WriteCoinViewEntry(CCoinsView &view, const Amount value, char flags) { | ||||
CCoinsMap map; | CCoinsMap map; | ||||
InsertCoinMapEntry(map, value, flags); | InsertCoinMapEntry(map, value, flags); | ||||
view.BatchWrite(map, BlockHash()); | BOOST_CHECK(view.BatchWrite(map, BlockHash())); | ||||
} | } | ||||
class SingleEntryCacheTest { | class SingleEntryCacheTest { | ||||
public: | public: | ||||
SingleEntryCacheTest(const Amount base_value, const Amount cache_value, | SingleEntryCacheTest(const Amount base_value, const Amount cache_value, | ||||
char cache_flags) { | char cache_flags) { | ||||
WriteCoinViewEntry(base, base_value, | WriteCoinViewEntry(base, base_value, | ||||
base_value == ABSENT ? NO_ENTRY : DIRTY); | base_value == ABSENT ? NO_ENTRY : DIRTY); | ||||
▲ Show 20 Lines • Show All 277 Lines • Show Last 20 Lines |