Changeset View
Changeset View
Standalone View
Standalone View
src/test/undo_tests.cpp
Show All 33 Lines | |||||
static void UndoBlock(const CBlock &block, CCoinsViewCache &view, | static void UndoBlock(const CBlock &block, CCoinsViewCache &view, | ||||
const CBlockUndo &blockUndo, | const CBlockUndo &blockUndo, | ||||
const CChainParams &chainparams, uint32_t nHeight) { | const CChainParams &chainparams, uint32_t nHeight) { | ||||
CBlockIndex pindex; | CBlockIndex pindex; | ||||
pindex.nHeight = nHeight; | pindex.nHeight = nHeight; | ||||
ApplyBlockUndo(blockUndo, block, &pindex, view); | ApplyBlockUndo(blockUndo, block, &pindex, view); | ||||
} | } | ||||
static bool HasSpendableCoin(const CCoinsViewCache &view, const uint256 &txid) { | static bool HasSpendableCoin(const CCoinsViewCache &view, | ||||
return !view.AccessCoin(COutPoint(txid, 0)).IsSpent(); | const unspentid_t &unspentid) { | ||||
return !view.AccessCoin(COutPoint(unspentid, 0)).IsSpent(); | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(connect_utxo_extblock) { | BOOST_AUTO_TEST_CASE(connect_utxo_extblock) { | ||||
SelectParams(CBaseChainParams::MAIN); | SelectParams(CBaseChainParams::MAIN); | ||||
const CChainParams &chainparams = Params(); | const CChainParams &chainparams = Params(); | ||||
CBlock block; | CBlock block; | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
Show All 10 Lines | BOOST_AUTO_TEST_CASE(connect_utxo_extblock) { | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = 42; | tx.vout[0].nValue = 42; | ||||
auto coinbaseTx = CTransaction(tx); | auto coinbaseTx = CTransaction(tx); | ||||
block.vtx.resize(2); | block.vtx.resize(2); | ||||
block.vtx[0] = MakeTransactionRef(tx); | block.vtx[0] = MakeTransactionRef(tx); | ||||
tx.vout[0].scriptPubKey = CScript() << OP_TRUE; | tx.vout[0].scriptPubKey = CScript() << OP_TRUE; | ||||
tx.vin[0].prevout.hash = GetRandHash(); | tx.vin[0].prevout.unspentid = unspentid_t(GetRandHash()); | ||||
tx.vin[0].prevout.n = 0; | tx.vin[0].prevout.n = 0; | ||||
tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL; | tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL; | ||||
tx.vin[0].scriptSig.resize(0); | tx.vin[0].scriptSig.resize(0); | ||||
tx.nVersion = 2; | tx.nVersion = 2; | ||||
auto prevTx0 = CTransaction(tx); | auto prevTx0 = CTransaction(tx); | ||||
AddCoins(view, prevTx0, 100); | AddCoins(view, prevTx0, 100); | ||||
tx.vin[0].prevout.hash = prevTx0.GetId(); | tx.vin[0].prevout.unspentid = prevTx0.GetUnspentid(); | ||||
auto tx0 = CTransaction(tx); | auto tx0 = CTransaction(tx); | ||||
block.vtx[1] = MakeTransactionRef(tx0); | block.vtx[1] = MakeTransactionRef(tx0); | ||||
// Now update the UTXO set. | // Now update the UTXO set. | ||||
CBlockUndo blockundo; | CBlockUndo blockundo; | ||||
UpdateUTXOSet(block, view, blockundo, chainparams, 123456); | UpdateUTXOSet(block, view, blockundo, chainparams, 123456); | ||||
BOOST_CHECK(view.GetBestBlock() == block.GetHash()); | BOOST_CHECK(view.GetBestBlock() == block.GetHash()); | ||||
BOOST_CHECK(HasSpendableCoin(view, coinbaseTx.GetId())); | BOOST_CHECK(HasSpendableCoin(view, coinbaseTx.GetUnspentid())); | ||||
BOOST_CHECK(HasSpendableCoin(view, tx0.GetId())); | BOOST_CHECK(HasSpendableCoin(view, tx0.GetUnspentid())); | ||||
BOOST_CHECK(!HasSpendableCoin(view, prevTx0.GetId())); | BOOST_CHECK(!HasSpendableCoin(view, prevTx0.GetUnspentid())); | ||||
UndoBlock(block, view, blockundo, chainparams, 123456); | UndoBlock(block, view, blockundo, chainparams, 123456); | ||||
BOOST_CHECK(view.GetBestBlock() == block.hashPrevBlock); | BOOST_CHECK(view.GetBestBlock() == block.hashPrevBlock); | ||||
BOOST_CHECK(!HasSpendableCoin(view, coinbaseTx.GetId())); | BOOST_CHECK(!HasSpendableCoin(view, coinbaseTx.GetUnspentid())); | ||||
BOOST_CHECK(!HasSpendableCoin(view, tx0.GetId())); | BOOST_CHECK(!HasSpendableCoin(view, tx0.GetUnspentid())); | ||||
BOOST_CHECK(HasSpendableCoin(view, prevTx0.GetId())); | BOOST_CHECK(HasSpendableCoin(view, prevTx0.GetUnspentid())); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |