Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
txospenderindex_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 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
6#include <test/util/common.h>
9#include <validation.h>
10
11#include <boost/test/unit_test.hpp>
12
14
16{
17 // Setup phase:
18 // Mine blocks for coinbase maturity, so we can spend some coinbase outputs in the test.
19 const CScript& coinbase_script = m_coinbase_txns[0]->vout[0].scriptPubKey;
20 for (int i = 0; i < 10; i++) CreateAndProcessBlock({}, coinbase_script);
21
22 // Spend 10 outputs
23 std::vector<COutPoint> spent(10);
24 std::vector<CMutableTransaction> spender(spent.size());
25 for (size_t i = 0; i < spent.size(); i++) {
26 // Outpoint
27 auto coinbase_tx = m_coinbase_txns[i];
28 spent[i] = COutPoint(coinbase_tx->GetHash(), 0);
29
30 // Spending tx
31 spender[i].version = 1;
32 spender[i].vin.resize(1);
33 spender[i].vin[0].prevout.hash = spent[i].hash;
34 spender[i].vin[0].prevout.n = spent[i].n;
35 spender[i].vout.resize(1);
36 spender[i].vout[0].nValue = coinbase_tx->GetValueOut();
37 spender[i].vout[0].scriptPubKey = coinbase_script;
38
39 // Sign
40 std::vector<unsigned char> vchSig;
42 BOOST_REQUIRE(coinbaseKey.Sign(hash, vchSig));
43 vchSig.push_back((unsigned char)SIGHASH_ALL);
44 spender[i].vin[0].scriptSig << vchSig;
45 }
46
47 // Generate and ensure block has been fully processed
48 const uint256 tip_hash = CreateAndProcessBlock(spender, coinbase_script).GetHash();
49 m_node.validation_signals->SyncWithValidationInterfaceQueue();
50 BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return m_node.chainman->ActiveTip()->GetBlockHash()), tip_hash);
51
52 // Now we concluded the setup phase, run index
55 BOOST_CHECK(!txospenderindex.BlockUntilSyncedToCurrentChain()); // false when not synced
56 BOOST_CHECK_NE(txospenderindex.GetSummary().best_block_hash, tip_hash);
57
58 // Transaction should not be found in the index before it is synced.
59 for (const auto& outpoint : spent) {
60 BOOST_CHECK(!txospenderindex.FindSpender(outpoint).value());
61 }
62
63 txospenderindex.Sync();
64 BOOST_CHECK_EQUAL(txospenderindex.GetSummary().best_block_hash, tip_hash);
65
66 for (size_t i = 0; i < spent.size(); i++) {
67 const auto tx_spender{txospenderindex.FindSpender(spent[i])};
68 BOOST_REQUIRE(tx_spender.has_value());
69 BOOST_REQUIRE(tx_spender->has_value());
70 BOOST_CHECK_EQUAL((*tx_spender)->tx->GetHash(), spender[i].GetHash());
71 BOOST_CHECK_EQUAL((*tx_spender)->block_hash, tip_hash);
72 }
73
75}
76
node::NodeContext m_node
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:30
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
TxoSpenderIndex is used to look up which transaction spent a given output.
256-bit opaque blob.
Definition uint256.h:196
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
BOOST_AUTO_TEST_SUITE_END()
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int32_t nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache, SigHashCache *sighash_cache)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ SIGHASH_ALL
Definition interpreter.h:32
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition context.h:89
std::unique_ptr< ChainstateManager > chainman
Definition context.h:74
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition sync.h:299
void StopIndex(BaseIndex &index, node::NodeContext &node)
Drain validation callbacks before stopping an index, matching Shutdown() in init.cpp.
BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)
BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32))
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82