Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
txindex_tests.cpp
Go to the documentation of this file.
1
// Copyright (c) 2017-present 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 <
addresstype.h
>
6
#include <chainparams.h>
7
#include <
index/txindex.h
>
8
#include <
interfaces/chain.h
>
9
#include <
test/util/setup_common.h
>
10
#include <validation.h>
11
12
#include <boost/test/unit_test.hpp>
13
14
BOOST_AUTO_TEST_SUITE(txindex_tests)
15
16
BOOST_FIXTURE_TEST_CASE
(txindex_initial_sync,
TestChain100Setup
)
17
{
18
TxIndex
txindex(
interfaces::MakeChain
(m_node), 1 << 20,
true
);
19
BOOST_REQUIRE(txindex.
Init
());
20
21
CTransactionRef
tx_disk;
22
uint256
block_hash;
23
24
// Transaction should not be found in the index before it is started.
25
for
(
const
auto
& txn : m_coinbase_txns) {
26
BOOST_CHECK
(!txindex.
FindTx
(txn->GetHash(), block_hash, tx_disk));
27
}
28
29
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
30
BOOST_CHECK
(!txindex.BlockUntilSyncedToCurrentChain());
31
32
txindex.
Sync
();
33
34
// Check that txindex excludes genesis block transactions.
35
const
CBlock
& genesis_block =
Params
().
GenesisBlock
();
36
for
(
const
auto
& txn : genesis_block.
vtx
) {
37
BOOST_CHECK
(!txindex.
FindTx
(txn->GetHash(), block_hash, tx_disk));
38
}
39
40
// Check that txindex has all txs that were in the chain before it started.
41
for
(
const
auto
& txn : m_coinbase_txns) {
42
if
(!txindex.
FindTx
(txn->GetHash(), block_hash, tx_disk)) {
43
BOOST_ERROR(
"FindTx failed"
);
44
}
else
if
(tx_disk->GetHash() != txn->GetHash()) {
45
BOOST_ERROR(
"Read incorrect tx"
);
46
}
47
}
48
49
// Check that new transactions in new blocks make it into the index.
50
for
(
int
i = 0; i < 10; i++) {
51
CScript
coinbase_script_pub_key =
GetScriptForDestination
(
PKHash
(coinbaseKey.GetPubKey()));
52
std::vector<CMutableTransaction> no_txns;
53
const
CBlock
& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
54
const
CTransaction
& txn = *block.
vtx
[0];
55
56
BOOST_CHECK
(txindex.BlockUntilSyncedToCurrentChain());
57
if
(!txindex.
FindTx
(txn.
GetHash
(), block_hash, tx_disk)) {
58
BOOST_ERROR(
"FindTx failed"
);
59
}
else
if
(tx_disk->GetHash() != txn.
GetHash
()) {
60
BOOST_ERROR(
"Read incorrect tx"
);
61
}
62
}
63
64
// shutdown sequence (c.f. Shutdown() in init.cpp)
65
txindex.
Stop
();
66
}
67
68
BOOST_AUTO_TEST_SUITE_END
()
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition
addresstype.cpp:166
addresstype.h
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
Definition
argsman_tests.cpp:171
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition
chainparams.cpp:112
BaseIndex::Stop
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition
base.cpp:461
BaseIndex::Init
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition
base.cpp:104
BaseIndex::Sync
void Sync()
Sync the index with the block index starting from the current best block.
Definition
base.cpp:201
CBlock
Definition
block.h:74
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition
block.h:77
CChainParams::GenesisBlock
const CBlock & GenesisBlock() const
Definition
chainparams.h:94
CScript
Serialized script, used inside transaction inputs and outputs.
Definition
script.h:405
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition
transaction.h:281
CTransaction::GetHash
const Txid & GetHash() const LIFETIMEBOUND
Definition
transaction.h:328
TxIndex
TxIndex is used to look up transactions included in the blockchain by hash.
Definition
txindex.h:27
TxIndex::FindTx
bool FindTx(const Txid &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Definition
txindex.cpp:94
uint256
256-bit opaque blob.
Definition
uint256.h:195
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
chain.h
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
Definition
interfaces.cpp:1023
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition
object.cpp:16
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition
transaction.h:403
setup_common.h
PKHash
Definition
addresstype.h:48
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition
setup_common.h:146
txindex.h
Generated on
for Bitcoin Core by
1.17.0