Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <index/txospenderindex.h>6#include <test/util/common.h>7#include <test/util/setup_common.h>8#include <validation.h>10#include <boost/test/unit_test.hpp>12BOOST_AUTO_TEST_SUITE(txospenderindex_tests)14BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)15{16// Setup phase:17// Mine blocks for coinbase maturity, so we can spend some coinbase outputs in the test.18const CScript& coinbase_script = m_coinbase_txns[0]->vout[0].scriptPubKey;19for (int i = 0; i < 10; i++) CreateAndProcessBlock({}, coinbase_script);21// Spend 10 outputs22std::vector<COutPoint> spent(10);23std::vector<CMutableTransaction> spender(spent.size());24for (size_t i = 0; i < spent.size(); i++) {25// Outpoint26auto coinbase_tx = m_coinbase_txns[i];27spent[i] = COutPoint(coinbase_tx->GetHash(), 0);29// Spending tx30spender[i].version = 1;31spender[i].vin.resize(1);32spender[i].vin[0].prevout.hash = spent[i].hash;33spender[i].vin[0].prevout.n = spent[i].n;34spender[i].vout.resize(1);35spender[i].vout[0].nValue = coinbase_tx->GetValueOut();36spender[i].vout[0].scriptPubKey = coinbase_script;38// Sign39std::vector<unsigned char> vchSig;40const uint256 hash = SignatureHash(coinbase_script, spender[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);41BOOST_REQUIRE(coinbaseKey.Sign(hash, vchSig));42vchSig.push_back((unsigned char)SIGHASH_ALL);43spender[i].vin[0].scriptSig << vchSig;44}46// Generate and ensure block has been fully processed47const uint256 tip_hash = CreateAndProcessBlock(spender, coinbase_script).GetHash();48m_node.validation_signals->SyncWithValidationInterfaceQueue();49BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return m_node.chainman->ActiveTip()->GetBlockHash()), tip_hash);51// Now we concluded the setup phase, run index52TxoSpenderIndex txospenderindex(interfaces::MakeChain(m_node), 1 << 20, true);53BOOST_REQUIRE(txospenderindex.Init());54BOOST_CHECK(!txospenderindex.BlockUntilSyncedToCurrentChain()); // false when not synced55BOOST_CHECK_NE(txospenderindex.GetSummary().best_block_hash, tip_hash);57// Transaction should not be found in the index before it is synced.58for (const auto& outpoint : spent) {59BOOST_CHECK(!txospenderindex.FindSpender(outpoint).value());60}62txospenderindex.Sync();63BOOST_CHECK_EQUAL(txospenderindex.GetSummary().best_block_hash, tip_hash);65for (size_t i = 0; i < spent.size(); i++) {66const auto tx_spender{txospenderindex.FindSpender(spent[i])};67BOOST_REQUIRE(tx_spender.has_value());68BOOST_REQUIRE(tx_spender->has_value());69BOOST_CHECK_EQUAL((*tx_spender)->tx->GetHash(), spender[i].GetHash());70BOOST_CHECK_EQUAL((*tx_spender)->block_hash, tip_hash);71}73// Shutdown sequence (c.f. Shutdown() in init.cpp)74txospenderindex.Stop();75}77BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.