LCOV - code coverage report
Current view: top level - src/test - txvalidationcache_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 39 39 100.0 %
Date: 2015-10-12 22:39:14 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2014 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #include "consensus/validation.h"
       6             : #include "key.h"
       7             : #include "main.h"
       8             : #include "miner.h"
       9             : #include "pubkey.h"
      10             : #include "txmempool.h"
      11             : #include "random.h"
      12             : #include "script/standard.h"
      13             : #include "test/test_bitcoin.h"
      14             : #include "utiltime.h"
      15             : 
      16             : #include <boost/test/unit_test.hpp>
      17             : 
      18           1 : BOOST_AUTO_TEST_SUITE(tx_validationcache_tests)
      19             : 
      20             : static bool
      21           3 : ToMemPool(CMutableTransaction& tx)
      22             : {
      23           3 :     LOCK(cs_main);
      24             : 
      25           3 :     CValidationState state;
      26           9 :     return AcceptToMemoryPool(mempool, state, tx, false, NULL, false);
      27             : }
      28             : 
      29           6 : BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
      30             : {
      31             :     // Make sure skipping validation of transctions that were
      32             :     // validated going into the memory pool does not allow
      33             :     // double-spends in blocks to pass validation when they should not.
      34             : 
      35           4 :     CScript scriptPubKey = CScript() <<  ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
      36             : 
      37             :     // Create a double-spend of mature coinbase txn:
      38           1 :     std::vector<CMutableTransaction> spends;
      39           2 :     spends.resize(2);
      40           3 :     for (int i = 0; i < 2; i++)
      41             :     {
      42           6 :         spends[i].vin.resize(1);
      43           4 :         spends[i].vin[0].prevout.hash = coinbaseTxns[0].GetHash();
      44           2 :         spends[i].vin[0].prevout.n = 0;
      45           4 :         spends[i].vout.resize(1);
      46           4 :         spends[i].vout[0].nValue = 11*CENT;
      47           2 :         spends[i].vout[0].scriptPubKey = scriptPubKey;
      48             : 
      49             :         // Sign:
      50             :         std::vector<unsigned char> vchSig;
      51           6 :         uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL);
      52          16 :         BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
      53           2 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
      54           4 :         spends[i].vin[0].scriptSig << vchSig;
      55             :     }
      56             : 
      57           1 :     CBlock block;
      58             : 
      59             :     // Test 1: block with both of those transactions should be rejected.
      60           2 :     block = CreateAndProcessBlock(spends, scriptPubKey);
      61          10 :     BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
      62             : 
      63             :     // Test 2: ... and should be rejected if spend1 is in the memory pool
      64           7 :     BOOST_CHECK(ToMemPool(spends[0]));
      65           2 :     block = CreateAndProcessBlock(spends, scriptPubKey);
      66          10 :     BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
      67           1 :     mempool.clear();
      68             : 
      69             :     // Test 3: ... and should be rejected if spend2 is in the memory pool
      70           9 :     BOOST_CHECK(ToMemPool(spends[1]));
      71           2 :     block = CreateAndProcessBlock(spends, scriptPubKey);
      72          10 :     BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
      73           1 :     mempool.clear();
      74             : 
      75             :     // Final sanity test: first spend in mempool, second in block, that's OK:
      76           1 :     std::vector<CMutableTransaction> oneSpend;
      77           1 :     oneSpend.push_back(spends[0]);
      78           9 :     BOOST_CHECK(ToMemPool(spends[1]));
      79           2 :     block = CreateAndProcessBlock(oneSpend, scriptPubKey);
      80          10 :     BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
      81             :     // spends[1] should have been removed from the mempool when the
      82             :     // block with spends[0] is accepted:
      83           5 :     BOOST_CHECK_EQUAL(mempool.size(), 0);
      84           1 : }
      85             : 
      86           3 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.11