2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2014-2017 The Bitcoin Core developers
|
2014-09-17 01:27:06 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <coins.h>
|
|
|
|
#include <script/standard.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <undo.h>
|
|
|
|
#include <utilstrencodings.h>
|
|
|
|
#include <test/test_bitcoin.h>
|
|
|
|
#include <validation.h>
|
|
|
|
#include <consensus/validation.h>
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2017-04-25 20:29:27 +02:00
|
|
|
int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
|
2016-11-07 21:30:41 +01:00
|
|
|
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
|
|
|
|
|
2014-09-17 01:27:06 +02:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-25 20:29:34 +02:00
|
|
|
//! equality test
|
|
|
|
bool operator==(const Coin &a, const Coin &b) {
|
|
|
|
// Empty Coin objects are always equal.
|
2017-05-31 02:58:54 +02:00
|
|
|
if (a.IsSpent() && b.IsSpent()) return true;
|
2017-04-25 20:29:34 +02:00
|
|
|
return a.fCoinBase == b.fCoinBase &&
|
|
|
|
a.nHeight == b.nHeight &&
|
|
|
|
a.out == b.out;
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:27:06 +02:00
|
|
|
class CCoinsViewTest : public CCoinsView
|
|
|
|
{
|
|
|
|
uint256 hashBestBlock_;
|
2017-04-25 20:29:39 +02:00
|
|
|
std::map<COutPoint, Coin> map_;
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
public:
|
2017-06-08 15:28:28 +02:00
|
|
|
bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
|
2014-09-17 01:27:06 +02:00
|
|
|
{
|
2017-04-25 20:29:39 +02:00
|
|
|
std::map<COutPoint, Coin>::const_iterator it = map_.find(outpoint);
|
2014-09-17 01:27:06 +02:00
|
|
|
if (it == map_.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-25 20:29:39 +02:00
|
|
|
coin = it->second;
|
2017-06-07 21:03:17 +02:00
|
|
|
if (coin.IsSpent() && InsecureRandBool() == 0) {
|
2014-09-17 01:27:06 +02:00
|
|
|
// Randomly return false in case of an empty entry.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:28:28 +02:00
|
|
|
uint256 GetBestBlock() const override { return hashBestBlock_; }
|
2014-09-17 01:27:06 +02:00
|
|
|
|
2017-06-08 15:28:28 +02:00
|
|
|
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock) override
|
2014-09-17 01:27:06 +02:00
|
|
|
{
|
|
|
|
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
|
2015-11-11 19:02:02 +01:00
|
|
|
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
|
|
|
|
// Same optimization used in CCoinsViewDB is to only write dirty entries.
|
2017-05-04 09:15:36 +02:00
|
|
|
map_[it->first] = it->second.coin;
|
2017-06-07 21:03:17 +02:00
|
|
|
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
|
2015-11-11 19:02:02 +01:00
|
|
|
// Randomly delete empty entries on write.
|
|
|
|
map_.erase(it->first);
|
|
|
|
}
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
mapCoins.erase(it++);
|
|
|
|
}
|
2015-11-11 19:02:02 +01:00
|
|
|
if (!hashBlock.IsNull())
|
|
|
|
hashBestBlock_ = hashBlock;
|
2014-09-17 01:27:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2015-05-04 01:31:11 +02:00
|
|
|
|
|
|
|
class CCoinsViewCacheTest : public CCoinsViewCache
|
|
|
|
{
|
|
|
|
public:
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
|
2015-05-04 01:31:11 +02:00
|
|
|
|
|
|
|
void SelfTest() const
|
|
|
|
{
|
|
|
|
// Manually recompute the dynamic usage of the whole data, and compare it.
|
|
|
|
size_t ret = memusage::DynamicUsage(cacheCoins);
|
2017-05-21 21:01:29 +02:00
|
|
|
size_t count = 0;
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : cacheCoins) {
|
|
|
|
ret += entry.second.coin.DynamicMemoryUsage();
|
2017-05-21 21:01:29 +02:00
|
|
|
++count;
|
2015-05-04 01:31:11 +02:00
|
|
|
}
|
2017-05-21 21:01:29 +02:00
|
|
|
BOOST_CHECK_EQUAL(GetCacheSize(), count);
|
2015-07-17 19:46:18 +02:00
|
|
|
BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
|
2015-05-04 01:31:11 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:34:54 +01:00
|
|
|
CCoinsMap& map() const { return cacheCoins; }
|
|
|
|
size_t& usage() const { return cachedCoinsUsage; }
|
2015-05-04 01:31:11 +02:00
|
|
|
};
|
|
|
|
|
2017-05-31 22:21:25 +02:00
|
|
|
} // namespace
|
2014-09-17 01:27:06 +02:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
|
|
|
|
|
|
|
|
// This is a large randomized insert/remove simulation test on a variable-size
|
|
|
|
// stack of caches on top of CCoinsViewTest.
|
|
|
|
//
|
2017-04-25 20:29:39 +02:00
|
|
|
// It will randomly create/update/delete Coin entries to a tip of caches, with
|
2014-09-17 01:27:06 +02:00
|
|
|
// txids picked from a limited list of random 256-bit hashes. Occasionally, a
|
|
|
|
// new tip is added to the stack of caches, or the tip is flushed and removed.
|
|
|
|
//
|
|
|
|
// During the process, booleans are kept to make sure that the randomized
|
|
|
|
// operation hits all branches.
|
|
|
|
BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
|
|
|
|
{
|
|
|
|
// Various coverage trackers.
|
|
|
|
bool removed_all_caches = false;
|
|
|
|
bool reached_4_caches = false;
|
|
|
|
bool added_an_entry = false;
|
2017-05-21 21:01:29 +02:00
|
|
|
bool added_an_unspendable_entry = false;
|
2014-09-17 01:27:06 +02:00
|
|
|
bool removed_an_entry = false;
|
|
|
|
bool updated_an_entry = false;
|
|
|
|
bool found_an_entry = false;
|
|
|
|
bool missed_an_entry = false;
|
2017-05-21 21:01:29 +02:00
|
|
|
bool uncached_an_entry = false;
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
// A simple map to track what we expect the cache stack to represent.
|
2017-04-25 20:29:39 +02:00
|
|
|
std::map<COutPoint, Coin> result;
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
// The cache stack.
|
|
|
|
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
|
2015-05-04 01:31:11 +02:00
|
|
|
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
|
|
|
|
stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
|
2014-09-17 01:27:06 +02:00
|
|
|
|
|
|
|
// Use a limited set of random transaction ids, so we do test overwriting entries.
|
|
|
|
std::vector<uint256> txids;
|
|
|
|
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
|
|
|
|
for (unsigned int i = 0; i < txids.size(); i++) {
|
2017-06-07 21:03:17 +02:00
|
|
|
txids[i] = InsecureRand256();
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
|
|
|
|
// Do a random modification.
|
|
|
|
{
|
2017-06-07 21:03:17 +02:00
|
|
|
uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
|
2017-04-25 20:29:39 +02:00
|
|
|
Coin& coin = result[COutPoint(txid, 0)];
|
2017-06-13 21:17:30 +02:00
|
|
|
|
|
|
|
// Determine whether to test HaveCoin before or after Access* (or both). As these functions
|
|
|
|
// can influence each other's behaviour by pulling things into the cache, all combinations
|
|
|
|
// are tested.
|
|
|
|
bool test_havecoin_before = InsecureRandBits(2) == 0;
|
|
|
|
bool test_havecoin_after = InsecureRandBits(2) == 0;
|
|
|
|
|
|
|
|
bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
|
2017-06-07 21:03:17 +02:00
|
|
|
const Coin& entry = (InsecureRandRange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
|
2017-04-25 20:29:39 +02:00
|
|
|
BOOST_CHECK(coin == entry);
|
2017-06-13 21:17:30 +02:00
|
|
|
BOOST_CHECK(!test_havecoin_before || result_havecoin == !entry.IsSpent());
|
|
|
|
|
|
|
|
if (test_havecoin_after) {
|
|
|
|
bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
|
|
|
|
BOOST_CHECK(ret == !entry.IsSpent());
|
|
|
|
}
|
2017-04-25 20:29:34 +02:00
|
|
|
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
|
2017-05-21 21:01:29 +02:00
|
|
|
Coin newcoin;
|
2017-06-07 21:03:17 +02:00
|
|
|
newcoin.out.nValue = InsecureRand32();
|
2017-05-21 21:01:29 +02:00
|
|
|
newcoin.nHeight = 1;
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
|
|
|
|
newcoin.out.scriptPubKey.assign(1 + InsecureRandBits(6), OP_RETURN);
|
2017-05-21 21:01:29 +02:00
|
|
|
BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
|
|
|
|
added_an_unspendable_entry = true;
|
2014-09-17 01:27:06 +02:00
|
|
|
} else {
|
2017-06-07 21:03:17 +02:00
|
|
|
newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0); // Random sizes so we can test memory usage accounting
|
2017-05-31 02:58:54 +02:00
|
|
|
(coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
|
2017-05-21 21:01:29 +02:00
|
|
|
coin = newcoin;
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), !coin.IsSpent() || InsecureRand32() & 1);
|
2014-09-17 01:27:06 +02:00
|
|
|
} else {
|
|
|
|
removed_an_entry = true;
|
2017-05-21 21:01:29 +02:00
|
|
|
coin.Clear();
|
2017-04-25 20:29:34 +02:00
|
|
|
stack.back()->SpendCoin(COutPoint(txid, 0));
|
|
|
|
}
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 21:01:29 +02:00
|
|
|
// One every 10 iterations, remove a random entry from the cache
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(10) == 0) {
|
|
|
|
COutPoint out(txids[InsecureRand32() % txids.size()], 0);
|
|
|
|
int cacheid = InsecureRand32() % stack.size();
|
2017-05-21 21:01:29 +02:00
|
|
|
stack[cacheid]->Uncache(out);
|
2017-05-31 02:58:54 +02:00
|
|
|
uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
|
2017-05-21 21:01:29 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:27:06 +02:00
|
|
|
// Once every 1000 iterations and at the end, verify the full cache.
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : result) {
|
|
|
|
bool have = stack.back()->HaveCoin(entry.first);
|
|
|
|
const Coin& coin = stack.back()->AccessCoin(entry.first);
|
2017-05-31 02:58:54 +02:00
|
|
|
BOOST_CHECK(have == !coin.IsSpent());
|
2017-06-04 22:02:43 +02:00
|
|
|
BOOST_CHECK(coin == entry.second);
|
2017-05-31 02:58:54 +02:00
|
|
|
if (coin.IsSpent()) {
|
2014-09-17 01:27:06 +02:00
|
|
|
missed_an_entry = true;
|
2017-04-25 20:29:39 +02:00
|
|
|
} else {
|
2017-06-04 22:02:43 +02:00
|
|
|
BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
|
2017-04-25 20:29:39 +02:00
|
|
|
found_an_entry = true;
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CCoinsViewCacheTest *test : stack) {
|
2015-05-04 01:31:11 +02:00
|
|
|
test->SelfTest();
|
|
|
|
}
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(100) == 0) {
|
2015-03-26 18:52:10 +01:00
|
|
|
// Every 100 iterations, flush an intermediate cache
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() > 1 && InsecureRandBool() == 0) {
|
|
|
|
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
|
2015-03-26 18:52:10 +01:00
|
|
|
stack[flushIndex]->Flush();
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(100) == 0) {
|
2014-09-17 01:27:06 +02:00
|
|
|
// Every 100 iterations, change the cache stack.
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() > 0 && InsecureRandBool() == 0) {
|
2015-03-26 18:52:10 +01:00
|
|
|
//Remove the top cache
|
2014-09-17 01:27:06 +02:00
|
|
|
stack.back()->Flush();
|
|
|
|
delete stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
|
2015-03-26 18:52:10 +01:00
|
|
|
//Add a new cache
|
2014-09-17 01:27:06 +02:00
|
|
|
CCoinsView* tip = &base;
|
|
|
|
if (stack.size() > 0) {
|
|
|
|
tip = stack.back();
|
|
|
|
} else {
|
|
|
|
removed_all_caches = true;
|
|
|
|
}
|
2015-05-04 01:31:11 +02:00
|
|
|
stack.push_back(new CCoinsViewCacheTest(tip));
|
2014-09-17 01:27:06 +02:00
|
|
|
if (stack.size() == 4) {
|
|
|
|
reached_4_caches = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the stack.
|
|
|
|
while (stack.size() > 0) {
|
|
|
|
delete stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify coverage.
|
|
|
|
BOOST_CHECK(removed_all_caches);
|
|
|
|
BOOST_CHECK(reached_4_caches);
|
|
|
|
BOOST_CHECK(added_an_entry);
|
2017-05-21 21:01:29 +02:00
|
|
|
BOOST_CHECK(added_an_unspendable_entry);
|
2014-09-17 01:27:06 +02:00
|
|
|
BOOST_CHECK(removed_an_entry);
|
|
|
|
BOOST_CHECK(updated_an_entry);
|
|
|
|
BOOST_CHECK(found_an_entry);
|
|
|
|
BOOST_CHECK(missed_an_entry);
|
2017-05-21 21:01:29 +02:00
|
|
|
BOOST_CHECK(uncached_an_entry);
|
2014-09-17 01:27:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// Store of all necessary tx and undo data for next test
|
2017-04-25 20:29:39 +02:00
|
|
|
typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
|
|
|
|
UtxoData utxoData;
|
|
|
|
|
|
|
|
UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
|
|
|
|
assert(utxoSet.size());
|
2017-06-07 21:03:17 +02:00
|
|
|
auto utxoSetIt = utxoSet.lower_bound(COutPoint(InsecureRand256(), 0));
|
2017-04-25 20:29:39 +02:00
|
|
|
if (utxoSetIt == utxoSet.end()) {
|
|
|
|
utxoSetIt = utxoSet.begin();
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
2017-04-25 20:29:39 +02:00
|
|
|
auto utxoDataIt = utxoData.find(*utxoSetIt);
|
|
|
|
assert(utxoDataIt != utxoData.end());
|
|
|
|
return utxoDataIt;
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 02:36:16 +01:00
|
|
|
// This test is similar to the previous test
|
|
|
|
// except the emphasis is on testing the functionality of UpdateCoins
|
|
|
|
// random txs are created and UpdateCoins is used to update the cache stack
|
|
|
|
// In particular it is tested that spending a duplicate coinbase tx
|
2017-08-16 00:24:39 +02:00
|
|
|
// has the expected effect (the other duplicate is overwritten at all cache levels)
|
2015-11-12 02:36:16 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
|
|
|
|
{
|
|
|
|
bool spent_a_duplicate_coinbase = false;
|
|
|
|
// A simple map to track what we expect the cache stack to represent.
|
2017-04-25 20:29:39 +02:00
|
|
|
std::map<COutPoint, Coin> result;
|
2015-11-12 02:36:16 +01:00
|
|
|
|
|
|
|
// The cache stack.
|
|
|
|
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
|
|
|
|
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
|
|
|
|
stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
|
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// Track the txids we've used in various sets
|
2017-05-31 02:58:54 +02:00
|
|
|
std::set<COutPoint> coinbase_coins;
|
|
|
|
std::set<COutPoint> disconnected_coins;
|
|
|
|
std::set<COutPoint> duplicate_coins;
|
2017-04-25 20:29:39 +02:00
|
|
|
std::set<COutPoint> utxoset;
|
2015-11-12 02:36:16 +01:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
|
2017-06-07 21:03:17 +02:00
|
|
|
uint32_t randiter = InsecureRand32();
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// 19/20 txs add a new transaction
|
|
|
|
if (randiter % 20 < 19) {
|
2015-11-12 02:36:16 +01:00
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.vin.resize(1);
|
|
|
|
tx.vout.resize(1);
|
|
|
|
tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
|
2017-06-07 21:03:17 +02:00
|
|
|
tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
|
|
|
|
unsigned int height = InsecureRand32();
|
2017-05-31 02:58:54 +02:00
|
|
|
Coin old_coin;
|
2015-11-12 02:36:16 +01:00
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// 2/20 times create a new coinbase
|
2017-05-31 02:58:54 +02:00
|
|
|
if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
|
2016-11-07 21:30:41 +01:00
|
|
|
// 1/10 of those times create a duplicate coinbase
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
|
2017-05-31 02:58:54 +02:00
|
|
|
auto utxod = FindRandomFrom(coinbase_coins);
|
2016-11-07 21:30:41 +01:00
|
|
|
// Reuse the exact same coinbase
|
2017-04-25 20:29:39 +02:00
|
|
|
tx = std::get<0>(utxod->second);
|
2018-03-18 15:26:45 +01:00
|
|
|
// shouldn't be available for reconnection if it's been duplicated
|
2017-05-31 02:58:54 +02:00
|
|
|
disconnected_coins.erase(utxod->first);
|
2016-11-07 21:30:41 +01:00
|
|
|
|
2017-05-31 02:58:54 +02:00
|
|
|
duplicate_coins.insert(utxod->first);
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-05-31 02:58:54 +02:00
|
|
|
coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
assert(CTransaction(tx).IsCoinBase());
|
|
|
|
}
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// 17/20 times reconnect previous or add a regular tx
|
2015-11-12 02:36:16 +01:00
|
|
|
else {
|
2016-11-07 21:30:41 +01:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
COutPoint prevout;
|
2016-11-07 21:30:41 +01:00
|
|
|
// 1/20 times reconnect a previously disconnected tx
|
2017-05-31 02:58:54 +02:00
|
|
|
if (randiter % 20 == 2 && disconnected_coins.size()) {
|
|
|
|
auto utxod = FindRandomFrom(disconnected_coins);
|
2017-04-25 20:29:39 +02:00
|
|
|
tx = std::get<0>(utxod->second);
|
|
|
|
prevout = tx.vin[0].prevout;
|
|
|
|
if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
|
2017-05-31 02:58:54 +02:00
|
|
|
disconnected_coins.erase(utxod->first);
|
2016-11-07 21:30:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
|
2017-04-25 20:29:39 +02:00
|
|
|
if (utxoset.count(utxod->first)) {
|
2016-11-07 21:30:41 +01:00
|
|
|
assert(CTransaction(tx).IsCoinBase());
|
2017-05-31 02:58:54 +02:00
|
|
|
assert(duplicate_coins.count(utxod->first));
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
2017-05-31 02:58:54 +02:00
|
|
|
disconnected_coins.erase(utxod->first);
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// 16/20 times create a regular tx
|
|
|
|
else {
|
2017-04-25 20:29:39 +02:00
|
|
|
auto utxod = FindRandomFrom(utxoset);
|
|
|
|
prevout = utxod->first;
|
2015-11-12 02:36:16 +01:00
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// Construct the tx to spend the coins of prevouthash
|
2017-04-25 20:29:39 +02:00
|
|
|
tx.vin[0].prevout = prevout;
|
2016-11-07 21:30:41 +01:00
|
|
|
assert(!CTransaction(tx).IsCoinBase());
|
|
|
|
}
|
|
|
|
// In this simple test coins only have two states, spent or unspent, save the unspent state to restore
|
2017-05-31 02:58:54 +02:00
|
|
|
old_coin = result[prevout];
|
2015-11-12 02:36:16 +01:00
|
|
|
// Update the expected result of prevouthash to know these coins are spent
|
2017-04-25 20:29:39 +02:00
|
|
|
result[prevout].Clear();
|
2015-11-12 02:36:16 +01:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
utxoset.erase(prevout);
|
2015-11-12 02:36:16 +01:00
|
|
|
|
|
|
|
// The test is designed to ensure spending a duplicate coinbase will work properly
|
|
|
|
// if that ever happens and not resurrect the previously overwritten coinbase
|
2017-05-31 02:58:54 +02:00
|
|
|
if (duplicate_coins.count(prevout)) {
|
2015-11-12 02:36:16 +01:00
|
|
|
spent_a_duplicate_coinbase = true;
|
2017-04-25 20:29:34 +02:00
|
|
|
}
|
2015-11-12 02:36:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
// Update the expected result to know about the new output coins
|
2017-04-25 20:29:39 +02:00
|
|
|
assert(tx.vout.size() == 1);
|
|
|
|
const COutPoint outpoint(tx.GetHash(), 0);
|
|
|
|
result[outpoint] = Coin(tx.vout[0], height, CTransaction(tx).IsCoinBase());
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// Call UpdateCoins on the top cache
|
|
|
|
CTxUndo undo;
|
|
|
|
UpdateCoins(tx, *(stack.back()), undo, height);
|
2015-11-12 02:36:16 +01:00
|
|
|
|
2016-11-07 21:30:41 +01:00
|
|
|
// Update the utxo set for future spends
|
2017-04-25 20:29:39 +02:00
|
|
|
utxoset.insert(outpoint);
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// Track this tx and undo info to use later
|
2017-05-31 02:58:54 +02:00
|
|
|
utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
|
2017-04-25 20:29:34 +02:00
|
|
|
} else if (utxoset.size()) {
|
|
|
|
//1/20 times undo a previous transaction
|
2017-04-25 20:29:39 +02:00
|
|
|
auto utxod = FindRandomFrom(utxoset);
|
2016-11-07 21:30:41 +01:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
CTransaction &tx = std::get<0>(utxod->second);
|
|
|
|
CTxUndo &undo = std::get<1>(utxod->second);
|
2017-05-31 02:58:54 +02:00
|
|
|
Coin &orig_coin = std::get<2>(utxod->second);
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// Update the expected result
|
|
|
|
// Remove new outputs
|
2017-04-25 20:29:39 +02:00
|
|
|
result[utxod->first].Clear();
|
2016-11-07 21:30:41 +01:00
|
|
|
// If not coinbase restore prevout
|
|
|
|
if (!tx.IsCoinBase()) {
|
2017-05-31 02:58:54 +02:00
|
|
|
result[tx.vin[0].prevout] = orig_coin;
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the tx from the current UTXO
|
|
|
|
// See code in DisconnectBlock
|
|
|
|
// remove outputs
|
2017-04-25 20:29:39 +02:00
|
|
|
stack.back()->SpendCoin(utxod->first);
|
2016-11-07 21:30:41 +01:00
|
|
|
// restore inputs
|
|
|
|
if (!tx.IsCoinBase()) {
|
|
|
|
const COutPoint &out = tx.vin[0].prevout;
|
2017-04-25 20:29:27 +02:00
|
|
|
Coin coin = undo.vprevout[0];
|
|
|
|
ApplyTxInUndo(std::move(coin), *(stack.back()), out);
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
|
|
|
// Store as a candidate for reconnection
|
2017-05-31 02:58:54 +02:00
|
|
|
disconnected_coins.insert(utxod->first);
|
2016-11-07 21:30:41 +01:00
|
|
|
|
|
|
|
// Update the utxoset
|
2017-04-25 20:29:39 +02:00
|
|
|
utxoset.erase(utxod->first);
|
2016-11-07 21:30:41 +01:00
|
|
|
if (!tx.IsCoinBase())
|
2017-04-25 20:29:39 +02:00
|
|
|
utxoset.insert(tx.vin[0].prevout);
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once every 1000 iterations and at the end, verify the full cache.
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : result) {
|
|
|
|
bool have = stack.back()->HaveCoin(entry.first);
|
|
|
|
const Coin& coin = stack.back()->AccessCoin(entry.first);
|
2017-05-31 02:58:54 +02:00
|
|
|
BOOST_CHECK(have == !coin.IsSpent());
|
2017-06-04 22:02:43 +02:00
|
|
|
BOOST_CHECK(coin == entry.second);
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 21:01:29 +02:00
|
|
|
// One every 10 iterations, remove a random entry from the cache
|
2017-06-07 21:03:17 +02:00
|
|
|
if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
|
|
|
|
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
|
2017-05-21 21:01:29 +02:00
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
|
|
|
|
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
|
2017-05-21 21:01:29 +02:00
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
|
|
|
|
stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
|
2017-05-21 21:01:29 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(100) == 0) {
|
2015-03-26 18:52:10 +01:00
|
|
|
// Every 100 iterations, flush an intermediate cache
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() > 1 && InsecureRandBool() == 0) {
|
|
|
|
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
|
2015-03-26 18:52:10 +01:00
|
|
|
stack[flushIndex]->Flush();
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (InsecureRandRange(100) == 0) {
|
2015-11-12 02:36:16 +01:00
|
|
|
// Every 100 iterations, change the cache stack.
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() > 0 && InsecureRandBool() == 0) {
|
2015-11-12 02:36:16 +01:00
|
|
|
stack.back()->Flush();
|
|
|
|
delete stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
2017-06-07 21:03:17 +02:00
|
|
|
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
|
2015-11-12 02:36:16 +01:00
|
|
|
CCoinsView* tip = &base;
|
|
|
|
if (stack.size() > 0) {
|
|
|
|
tip = stack.back();
|
|
|
|
}
|
|
|
|
stack.push_back(new CCoinsViewCacheTest(tip));
|
2016-11-07 21:30:41 +01:00
|
|
|
}
|
2015-11-12 02:36:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the stack.
|
|
|
|
while (stack.size() > 0) {
|
|
|
|
delete stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify coverage.
|
|
|
|
BOOST_CHECK(spent_a_duplicate_coinbase);
|
|
|
|
}
|
|
|
|
|
2016-04-25 14:05:36 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(ccoins_serialization)
|
|
|
|
{
|
|
|
|
// Good example
|
2017-04-25 20:29:39 +02:00
|
|
|
CDataStream ss1(ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"), SER_DISK, CLIENT_VERSION);
|
|
|
|
Coin cc1;
|
2016-04-25 14:05:36 +02:00
|
|
|
ss1 >> cc1;
|
|
|
|
BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
|
2018-04-09 09:50:19 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
|
|
|
|
BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
|
2017-04-25 20:29:39 +02:00
|
|
|
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
|
2016-04-25 14:05:36 +02:00
|
|
|
|
|
|
|
// Good example
|
2017-04-25 20:29:39 +02:00
|
|
|
CDataStream ss2(ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"), SER_DISK, CLIENT_VERSION);
|
|
|
|
Coin cc2;
|
2016-04-25 14:05:36 +02:00
|
|
|
ss2 >> cc2;
|
|
|
|
BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
|
2018-04-09 09:50:19 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
|
2017-04-25 20:29:39 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
|
2016-04-25 14:05:36 +02:00
|
|
|
|
|
|
|
// Smallest possible example
|
2017-04-25 20:29:39 +02:00
|
|
|
CDataStream ss3(ParseHex("000006"), SER_DISK, CLIENT_VERSION);
|
|
|
|
Coin cc3;
|
2016-04-25 14:05:36 +02:00
|
|
|
ss3 >> cc3;
|
|
|
|
BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
|
2018-04-09 09:50:19 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
|
2017-04-25 20:29:39 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
|
2018-04-09 09:50:19 +02:00
|
|
|
BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
|
2016-04-25 14:05:36 +02:00
|
|
|
|
|
|
|
// scriptPubKey that ends beyond the end of the stream
|
2017-04-25 20:29:39 +02:00
|
|
|
CDataStream ss4(ParseHex("000007"), SER_DISK, CLIENT_VERSION);
|
2016-04-25 14:05:36 +02:00
|
|
|
try {
|
2017-04-25 20:29:39 +02:00
|
|
|
Coin cc4;
|
2016-04-25 14:05:36 +02:00
|
|
|
ss4 >> cc4;
|
|
|
|
BOOST_CHECK_MESSAGE(false, "We should have thrown");
|
|
|
|
} catch (const std::ios_base::failure& e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Very large scriptPubKey (3*10^9 bytes) past the end of the stream
|
|
|
|
CDataStream tmp(SER_DISK, CLIENT_VERSION);
|
|
|
|
uint64_t x = 3000000000ULL;
|
|
|
|
tmp << VARINT(x);
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(tmp.begin(), tmp.end()), "8a95c0bb00");
|
2017-04-25 20:29:39 +02:00
|
|
|
CDataStream ss5(ParseHex("00008a95c0bb00"), SER_DISK, CLIENT_VERSION);
|
2016-04-25 14:05:36 +02:00
|
|
|
try {
|
2017-04-25 20:29:39 +02:00
|
|
|
Coin cc5;
|
2016-04-25 14:05:36 +02:00
|
|
|
ss5 >> cc5;
|
|
|
|
BOOST_CHECK_MESSAGE(false, "We should have thrown");
|
|
|
|
} catch (const std::ios_base::failure& e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
const static COutPoint OUTPOINT;
|
2016-12-06 00:30:46 +01:00
|
|
|
const static CAmount PRUNED = -1;
|
|
|
|
const static CAmount ABSENT = -2;
|
2016-11-07 21:30:41 +01:00
|
|
|
const static CAmount FAIL = -3;
|
2016-12-06 00:30:46 +01:00
|
|
|
const static CAmount VALUE1 = 100;
|
|
|
|
const static CAmount VALUE2 = 200;
|
|
|
|
const static CAmount VALUE3 = 300;
|
|
|
|
const static char DIRTY = CCoinsCacheEntry::DIRTY;
|
|
|
|
const static char FRESH = CCoinsCacheEntry::FRESH;
|
|
|
|
const static char NO_ENTRY = -1;
|
|
|
|
|
|
|
|
const static auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)};
|
|
|
|
const static auto CLEAN_FLAGS = {char(0), FRESH};
|
|
|
|
const static auto ABSENT_FLAGS = {NO_ENTRY};
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
void SetCoinsValue(CAmount value, Coin& coin)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
|
|
|
assert(value != ABSENT);
|
2017-04-25 20:29:39 +02:00
|
|
|
coin.Clear();
|
2017-05-31 02:58:54 +02:00
|
|
|
assert(coin.IsSpent());
|
2016-12-06 00:30:46 +01:00
|
|
|
if (value != PRUNED) {
|
2017-04-25 20:29:39 +02:00
|
|
|
coin.out.nValue = value;
|
|
|
|
coin.nHeight = 1;
|
2017-05-31 02:58:54 +02:00
|
|
|
assert(!coin.IsSpent());
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
|
|
|
|
{
|
|
|
|
if (value == ABSENT) {
|
|
|
|
assert(flags == NO_ENTRY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(flags != NO_ENTRY);
|
|
|
|
CCoinsCacheEntry entry;
|
|
|
|
entry.flags = flags;
|
2017-05-04 09:15:36 +02:00
|
|
|
SetCoinsValue(value, entry.coin);
|
2017-04-25 20:29:39 +02:00
|
|
|
auto inserted = map.emplace(OUTPOINT, std::move(entry));
|
2016-12-06 00:30:46 +01:00
|
|
|
assert(inserted.second);
|
2017-05-04 09:15:36 +02:00
|
|
|
return inserted.first->second.coin.DynamicMemoryUsage();
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags)
|
|
|
|
{
|
2017-04-25 20:29:39 +02:00
|
|
|
auto it = map.find(OUTPOINT);
|
2016-12-06 00:30:46 +01:00
|
|
|
if (it == map.end()) {
|
|
|
|
value = ABSENT;
|
|
|
|
flags = NO_ENTRY;
|
|
|
|
} else {
|
2017-05-31 02:58:54 +02:00
|
|
|
if (it->second.coin.IsSpent()) {
|
2016-12-06 00:30:46 +01:00
|
|
|
value = PRUNED;
|
|
|
|
} else {
|
2017-05-04 09:15:36 +02:00
|
|
|
value = it->second.coin.out.nValue;
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
flags = it->second.flags;
|
|
|
|
assert(flags != NO_ENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteCoinsViewEntry(CCoinsView& view, CAmount value, char flags)
|
|
|
|
{
|
|
|
|
CCoinsMap map;
|
|
|
|
InsertCoinsMapEntry(map, value, flags);
|
|
|
|
view.BatchWrite(map, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
class SingleEntryCacheTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
|
|
|
|
{
|
|
|
|
WriteCoinsViewEntry(base, base_value, base_value == ABSENT ? NO_ENTRY : DIRTY);
|
|
|
|
cache.usage() += InsertCoinsMapEntry(cache.map(), cache_value, cache_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCoinsView root;
|
|
|
|
CCoinsViewCacheTest base{&root};
|
|
|
|
CCoinsViewCacheTest cache{&base};
|
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
|
|
|
SingleEntryCacheTest test(base_value, cache_value, cache_flags);
|
2017-04-25 20:29:34 +02:00
|
|
|
test.cache.AccessCoin(OUTPOINT);
|
2016-12-06 00:30:46 +01:00
|
|
|
test.cache.SelfTest();
|
|
|
|
|
|
|
|
CAmount result_value;
|
|
|
|
char result_flags;
|
|
|
|
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
|
|
|
|
BOOST_CHECK_EQUAL(result_value, expected_value);
|
|
|
|
BOOST_CHECK_EQUAL(result_flags, expected_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(ccoins_access)
|
|
|
|
{
|
|
|
|
/* Check AccessCoin behavior, requesting a coin from a cache view layered on
|
|
|
|
* top of a base view, and checking the resulting entry in the cache after
|
|
|
|
* the access.
|
|
|
|
*
|
|
|
|
* Base Cache Result Cache Result
|
|
|
|
* Value Value Value Flags Flags
|
|
|
|
*/
|
2017-04-25 20:29:34 +02:00
|
|
|
CheckAccessCoin(ABSENT, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY );
|
|
|
|
CheckAccessCoin(ABSENT, PRUNED, PRUNED, 0 , 0 );
|
|
|
|
CheckAccessCoin(ABSENT, PRUNED, PRUNED, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(ABSENT, PRUNED, PRUNED, DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckAccessCoin(ABSENT, VALUE2, VALUE2, 0 , 0 );
|
|
|
|
CheckAccessCoin(ABSENT, VALUE2, VALUE2, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(ABSENT, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH);
|
2017-06-13 21:17:30 +02:00
|
|
|
CheckAccessCoin(PRUNED, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY );
|
2017-04-25 20:29:34 +02:00
|
|
|
CheckAccessCoin(PRUNED, PRUNED, PRUNED, 0 , 0 );
|
|
|
|
CheckAccessCoin(PRUNED, PRUNED, PRUNED, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(PRUNED, PRUNED, PRUNED, DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckAccessCoin(PRUNED, VALUE2, VALUE2, 0 , 0 );
|
|
|
|
CheckAccessCoin(PRUNED, VALUE2, VALUE2, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(PRUNED, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckAccessCoin(VALUE1, ABSENT, VALUE1, NO_ENTRY , 0 );
|
|
|
|
CheckAccessCoin(VALUE1, PRUNED, PRUNED, 0 , 0 );
|
|
|
|
CheckAccessCoin(VALUE1, PRUNED, PRUNED, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(VALUE1, PRUNED, PRUNED, DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckAccessCoin(VALUE1, VALUE2, VALUE2, 0 , 0 );
|
|
|
|
CheckAccessCoin(VALUE1, VALUE2, VALUE2, FRESH , FRESH );
|
|
|
|
CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY , DIRTY );
|
|
|
|
CheckAccessCoin(VALUE1, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH);
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
|
|
|
SingleEntryCacheTest test(base_value, cache_value, cache_flags);
|
2017-04-25 20:29:34 +02:00
|
|
|
test.cache.SpendCoin(OUTPOINT);
|
2016-12-06 00:30:46 +01:00
|
|
|
test.cache.SelfTest();
|
|
|
|
|
|
|
|
CAmount result_value;
|
|
|
|
char result_flags;
|
|
|
|
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
|
|
|
|
BOOST_CHECK_EQUAL(result_value, expected_value);
|
|
|
|
BOOST_CHECK_EQUAL(result_flags, expected_flags);
|
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(ccoins_spend)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
2017-04-25 20:29:34 +02:00
|
|
|
/* Check SpendCoin behavior, requesting a coin from a cache view layered on
|
|
|
|
* top of a base view, spending, and then checking
|
2016-12-06 00:30:46 +01:00
|
|
|
* the resulting entry in the cache after the modification.
|
|
|
|
*
|
2017-04-25 20:29:34 +02:00
|
|
|
* Base Cache Result Cache Result
|
|
|
|
* Value Value Value Flags Flags
|
2016-12-06 00:30:46 +01:00
|
|
|
*/
|
2017-04-25 20:29:34 +02:00
|
|
|
CheckSpendCoins(ABSENT, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY );
|
|
|
|
CheckSpendCoins(ABSENT, PRUNED, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(ABSENT, PRUNED, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(ABSENT, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(ABSENT, PRUNED, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckSpendCoins(ABSENT, VALUE2, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(ABSENT, VALUE2, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(ABSENT, VALUE2, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(ABSENT, VALUE2, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckSpendCoins(PRUNED, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY );
|
|
|
|
CheckSpendCoins(PRUNED, PRUNED, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(PRUNED, PRUNED, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(PRUNED, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(PRUNED, PRUNED, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckSpendCoins(PRUNED, VALUE2, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(PRUNED, VALUE2, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(PRUNED, VALUE2, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(PRUNED, VALUE2, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckSpendCoins(VALUE1, ABSENT, PRUNED, NO_ENTRY , DIRTY );
|
|
|
|
CheckSpendCoins(VALUE1, PRUNED, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(VALUE1, PRUNED, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(VALUE1, PRUNED, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(VALUE1, PRUNED, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckSpendCoins(VALUE1, VALUE2, PRUNED, 0 , DIRTY );
|
|
|
|
CheckSpendCoins(VALUE1, VALUE2, ABSENT, FRESH , NO_ENTRY );
|
|
|
|
CheckSpendCoins(VALUE1, VALUE2, PRUNED, DIRTY , DIRTY );
|
|
|
|
CheckSpendCoins(VALUE1, VALUE2, ABSENT, DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
|
|
|
SingleEntryCacheTest test(base_value, cache_value, cache_flags);
|
|
|
|
|
|
|
|
CAmount result_value;
|
|
|
|
char result_flags;
|
2016-11-07 21:30:41 +01:00
|
|
|
try {
|
2017-04-25 20:29:34 +02:00
|
|
|
CTxOut output;
|
|
|
|
output.nValue = modify_value;
|
|
|
|
test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), coinbase);
|
|
|
|
test.cache.SelfTest();
|
2016-11-07 21:30:41 +01:00
|
|
|
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
|
|
|
|
} catch (std::logic_error& e) {
|
|
|
|
result_value = FAIL;
|
|
|
|
result_flags = NO_ENTRY;
|
|
|
|
}
|
|
|
|
|
2016-12-06 00:30:46 +01:00
|
|
|
BOOST_CHECK_EQUAL(result_value, expected_value);
|
|
|
|
BOOST_CHECK_EQUAL(result_flags, expected_flags);
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
// Simple wrapper for CheckAddCoinBase function above that loops through
|
2016-12-06 00:30:46 +01:00
|
|
|
// different possible base_values, making sure each one gives the same results.
|
2017-04-25 20:29:34 +02:00
|
|
|
// This wrapper lets the coins_add test below be shorter and less repetitive,
|
|
|
|
// while still verifying that the CoinsViewCache::AddCoin implementation
|
2016-12-06 00:30:46 +01:00
|
|
|
// ignores base values.
|
|
|
|
template <typename... Args>
|
2017-04-25 20:29:34 +02:00
|
|
|
void CheckAddCoin(Args&&... args)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
|
|
|
for (CAmount base_value : {ABSENT, PRUNED, VALUE1})
|
2017-04-25 20:29:34 +02:00
|
|
|
CheckAddCoinBase(base_value, std::forward<Args>(args)...);
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 20:29:34 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(ccoins_add)
|
2016-12-06 00:30:46 +01:00
|
|
|
{
|
2017-04-25 20:29:34 +02:00
|
|
|
/* Check AddCoin behavior, requesting a new coin from a cache view,
|
2016-12-06 00:30:46 +01:00
|
|
|
* writing a modification to the coin, and then checking the resulting
|
|
|
|
* entry in the cache after the modification. Verify behavior with the
|
2017-04-25 20:29:34 +02:00
|
|
|
* with the AddCoin potential_overwrite argument set to false, and to true.
|
2016-12-06 00:30:46 +01:00
|
|
|
*
|
2017-04-25 20:29:34 +02:00
|
|
|
* Cache Write Result Cache Result potential_overwrite
|
|
|
|
* Value Value Value Flags Flags
|
2016-12-06 00:30:46 +01:00
|
|
|
*/
|
2017-04-25 20:29:34 +02:00
|
|
|
CheckAddCoin(ABSENT, VALUE3, VALUE3, NO_ENTRY , DIRTY|FRESH, false);
|
|
|
|
CheckAddCoin(ABSENT, VALUE3, VALUE3, NO_ENTRY , DIRTY , true );
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, 0 , DIRTY|FRESH, false);
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, 0 , DIRTY , true );
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, FRESH , DIRTY|FRESH, false);
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, FRESH , DIRTY|FRESH, true );
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, DIRTY , DIRTY , false);
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, DIRTY , DIRTY , true );
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, DIRTY|FRESH, DIRTY|FRESH, false);
|
|
|
|
CheckAddCoin(PRUNED, VALUE3, VALUE3, DIRTY|FRESH, DIRTY|FRESH, true );
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, FAIL , 0 , NO_ENTRY , false);
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, VALUE3, 0 , DIRTY , true );
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, FAIL , FRESH , NO_ENTRY , false);
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, VALUE3, FRESH , DIRTY|FRESH, true );
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, FAIL , DIRTY , NO_ENTRY , false);
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, VALUE3, DIRTY , DIRTY , true );
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, FAIL , DIRTY|FRESH, NO_ENTRY , false);
|
|
|
|
CheckAddCoin(VALUE2, VALUE3, VALUE3, DIRTY|FRESH, DIRTY|FRESH, true );
|
2016-12-06 00:30:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
|
|
|
|
{
|
|
|
|
SingleEntryCacheTest test(ABSENT, parent_value, parent_flags);
|
|
|
|
|
|
|
|
CAmount result_value;
|
|
|
|
char result_flags;
|
2016-12-09 18:28:22 +01:00
|
|
|
try {
|
|
|
|
WriteCoinsViewEntry(test.cache, child_value, child_flags);
|
|
|
|
test.cache.SelfTest();
|
|
|
|
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
|
|
|
|
} catch (std::logic_error& e) {
|
|
|
|
result_value = FAIL;
|
|
|
|
result_flags = NO_ENTRY;
|
|
|
|
}
|
|
|
|
|
2016-12-06 00:30:46 +01:00
|
|
|
BOOST_CHECK_EQUAL(result_value, expected_value);
|
|
|
|
BOOST_CHECK_EQUAL(result_flags, expected_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(ccoins_write)
|
|
|
|
{
|
|
|
|
/* Check BatchWrite behavior, flushing one entry from a child cache to a
|
|
|
|
* parent cache, and checking the resulting entry in the parent cache
|
|
|
|
* after the write.
|
|
|
|
*
|
|
|
|
* Parent Child Result Parent Child Result
|
|
|
|
* Value Value Value Flags Flags Flags
|
|
|
|
*/
|
|
|
|
CheckWriteCoins(ABSENT, ABSENT, ABSENT, NO_ENTRY , NO_ENTRY , NO_ENTRY );
|
|
|
|
CheckWriteCoins(ABSENT, PRUNED, PRUNED, NO_ENTRY , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(ABSENT, PRUNED, ABSENT, NO_ENTRY , DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckWriteCoins(ABSENT, VALUE2, VALUE2, NO_ENTRY , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(ABSENT, VALUE2, VALUE2, NO_ENTRY , DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(PRUNED, ABSENT, PRUNED, 0 , NO_ENTRY , 0 );
|
|
|
|
CheckWriteCoins(PRUNED, ABSENT, PRUNED, FRESH , NO_ENTRY , FRESH );
|
|
|
|
CheckWriteCoins(PRUNED, ABSENT, PRUNED, DIRTY , NO_ENTRY , DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, ABSENT, PRUNED, DIRTY|FRESH, NO_ENTRY , DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, PRUNED, 0 , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, PRUNED, 0 , DIRTY|FRESH, DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, ABSENT, FRESH , DIRTY , NO_ENTRY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, ABSENT, FRESH , DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, PRUNED, DIRTY , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, PRUNED, DIRTY , DIRTY|FRESH, DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, ABSENT, DIRTY|FRESH, DIRTY , NO_ENTRY );
|
|
|
|
CheckWriteCoins(PRUNED, PRUNED, ABSENT, DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, 0 , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, 0 , DIRTY|FRESH, DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, FRESH , DIRTY , DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, FRESH , DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, DIRTY , DIRTY , DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, DIRTY , DIRTY|FRESH, DIRTY );
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, DIRTY|FRESH, DIRTY , DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(PRUNED, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH, DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(VALUE1, ABSENT, VALUE1, 0 , NO_ENTRY , 0 );
|
|
|
|
CheckWriteCoins(VALUE1, ABSENT, VALUE1, FRESH , NO_ENTRY , FRESH );
|
|
|
|
CheckWriteCoins(VALUE1, ABSENT, VALUE1, DIRTY , NO_ENTRY , DIRTY );
|
|
|
|
CheckWriteCoins(VALUE1, ABSENT, VALUE1, DIRTY|FRESH, NO_ENTRY , DIRTY|FRESH);
|
|
|
|
CheckWriteCoins(VALUE1, PRUNED, PRUNED, 0 , DIRTY , DIRTY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, FAIL , 0 , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, ABSENT, FRESH , DIRTY , NO_ENTRY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, FAIL , FRESH , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, PRUNED, DIRTY , DIRTY , DIRTY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, FAIL , DIRTY , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, ABSENT, DIRTY|FRESH, DIRTY , NO_ENTRY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, PRUNED, FAIL , DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, VALUE2, 0 , DIRTY , DIRTY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, FAIL , 0 , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, VALUE2, FRESH , DIRTY , DIRTY|FRESH);
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, FAIL , FRESH , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY , DIRTY , DIRTY );
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, FAIL , DIRTY , DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY|FRESH, DIRTY , DIRTY|FRESH);
|
2016-12-09 18:28:22 +01:00
|
|
|
CheckWriteCoins(VALUE1, VALUE2, FAIL , DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
|
2016-12-06 00:30:46 +01:00
|
|
|
|
|
|
|
// The checks above omit cases where the child flags are not DIRTY, since
|
|
|
|
// they would be too repetitive (the parent cache is never updated in these
|
|
|
|
// cases). The loop below covers these cases and makes sure the parent cache
|
|
|
|
// is always left unchanged.
|
|
|
|
for (CAmount parent_value : {ABSENT, PRUNED, VALUE1})
|
|
|
|
for (CAmount child_value : {ABSENT, PRUNED, VALUE2})
|
|
|
|
for (char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
|
|
|
|
for (char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
|
|
|
|
CheckWriteCoins(parent_value, child_value, parent_value, parent_flags, child_flags, parent_flags);
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:27:06 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|