Bitcoin Core 30.99.0
P2P Digital Currency
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
5#include <chainparams.h>
8#include <util/time.h>
9#include <validation.h>
10
11#include <boost/test/unit_test.hpp>
12
13BOOST_AUTO_TEST_SUITE(txospenderindex_tests)
14
15BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)
16{
17 TxoSpenderIndex txospenderindex(interfaces::MakeChain(m_node), 1 << 20, true);
18 BOOST_REQUIRE(txospenderindex.Init());
19
20 // Mine blocks for coinbase maturity, so we can spend some coinbase outputs in the test.
21 for (int i = 0; i < 50; i++) {
22 std::vector<CMutableTransaction> no_txns;
23 CreateAndProcessBlock(no_txns, this->m_coinbase_txns[i]->vout[0].scriptPubKey);
24 }
25 std::vector<COutPoint> spent(10);
26 std::vector<CMutableTransaction> spender(spent.size());
27
28 for (size_t i = 0; i < spent.size(); i++) {
29 spent[i] = COutPoint(this->m_coinbase_txns[i]->GetHash(), 0);
30 spender[i].version = 1;
31 spender[i].vin.resize(1);
32 spender[i].vin[0].prevout.hash = spent[i].hash;
33 spender[i].vin[0].prevout.n = spent[i].n;
34 spender[i].vout.resize(1);
35 spender[i].vout[0].nValue = this->m_coinbase_txns[i]->GetValueOut();
36 spender[i].vout[0].scriptPubKey = this->m_coinbase_txns[i]->vout[0].scriptPubKey;
37
38 // Sign:
39 std::vector<unsigned char> vchSig;
40 const uint256 hash = SignatureHash(this->m_coinbase_txns[i]->vout[0].scriptPubKey, spender[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);
41 coinbaseKey.Sign(hash, vchSig);
42 vchSig.push_back((unsigned char)SIGHASH_ALL);
43 spender[i].vin[0].scriptSig << vchSig;
44 }
45
46 CBlock block = CreateAndProcessBlock(spender, this->m_coinbase_txns[0]->vout[0].scriptPubKey);
47
48 // Transaction should not be found in the index before it is started.
49 for (const auto& outpoint : spent) {
50 BOOST_CHECK(!txospenderindex.FindSpender(outpoint).value());
51 }
52
53 // BlockUntilSyncedToCurrentChain should return false before txospenderindex is started.
54 BOOST_CHECK(!txospenderindex.BlockUntilSyncedToCurrentChain());
55
56 txospenderindex.Sync();
57 for (size_t i = 0; i < spent.size(); i++) {
58 const auto tx_spender{txospenderindex.FindSpender(spent[i])};
59 BOOST_REQUIRE(tx_spender.has_value());
60 BOOST_REQUIRE(tx_spender->has_value());
61 BOOST_CHECK_EQUAL((*tx_spender)->tx->GetHash(), spender[i].GetHash());
62 BOOST_CHECK_EQUAL((*tx_spender)->block_hash, block.GetHash());
63 }
64
65 // It is not safe to stop and destroy the index until it finishes handling
66 // the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
67 // call above is sufficient to ensure this, but the
68 // SyncWithValidationInterfaceQueue() call below is also needed to ensure
69 // TSAN always sees the test thread waiting for the notification thread, and
70 // avoid potential false positive reports.
71 m_node.validation_signals->SyncWithValidationInterfaceQueue();
72
73 // shutdown sequence (c.f. Shutdown() in init.cpp)
74 txospenderindex.Stop();
75}
76
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:461
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:104
void Sync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:201
uint256 GetHash() const
Definition: block.cpp:15
Definition: block.h:74
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
TxoSpenderIndex is used to look up which transaction spent a given output.
util::Expected< std::optional< TxoSpender >, std::string > FindSpender(const COutPoint &txo) const
256-bit opaque blob.
Definition: uint256.h:195
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:31
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:995
#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.
Definition: setup_common.h:146
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)