mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-16 17:04:05 +02:00
Replace hard-coded MiB byte conversions (e.g. `1024*1024`, `1<<20`, `1048576`) with the existing `_MiB` literal to improve readability and avoid repeating constants.
In the few spots where arithmetic involves signed values, the result is identical to the previous code assuming those quantities never turn negative.
Also switch to brace init on every declaration assigned from `_MiB`/`_GiB` literals so a future oversized value (e.g. `unsigned int x{4096_MiB}`) becomes a compile error through the C++11 narrowing check instead of silently truncating.
Extend unit tests to cover the 32-bit `size_t` overflow boundary and to assert equivalence for integer and floating-point conversions.
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
70 lines
2.4 KiB
C++
70 lines
2.4 KiB
C++
// Copyright (c) 2017-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <addresstype.h>
|
|
#include <chainparams.h>
|
|
#include <index/txindex.h>
|
|
#include <interfaces/chain.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <util/byte_units.h>
|
|
#include <validation.h>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
BOOST_AUTO_TEST_SUITE(txindex_tests)
|
|
|
|
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
|
|
{
|
|
TxIndex txindex(interfaces::MakeChain(m_node), 1_MiB, true);
|
|
BOOST_REQUIRE(txindex.Init());
|
|
|
|
CTransactionRef tx_disk;
|
|
uint256 block_hash;
|
|
|
|
// Transaction should not be found in the index before it is started.
|
|
for (const auto& txn : m_coinbase_txns) {
|
|
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
|
|
}
|
|
|
|
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
|
|
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
|
|
|
|
txindex.Sync();
|
|
|
|
// Check that txindex excludes genesis block transactions.
|
|
const CBlock& genesis_block = Params().GenesisBlock();
|
|
for (const auto& txn : genesis_block.vtx) {
|
|
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
|
|
}
|
|
|
|
// Check that txindex has all txs that were in the chain before it started.
|
|
for (const auto& txn : m_coinbase_txns) {
|
|
if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
|
|
BOOST_ERROR("FindTx failed");
|
|
} else if (tx_disk->GetHash() != txn->GetHash()) {
|
|
BOOST_ERROR("Read incorrect tx");
|
|
}
|
|
}
|
|
|
|
// Check that new transactions in new blocks make it into the index.
|
|
for (int i = 0; i < 10; i++) {
|
|
CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
|
|
std::vector<CMutableTransaction> no_txns;
|
|
const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
|
|
const CTransaction& txn = *block.vtx[0];
|
|
|
|
BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
|
|
if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
|
|
BOOST_ERROR("FindTx failed");
|
|
} else if (tx_disk->GetHash() != txn.GetHash()) {
|
|
BOOST_ERROR("Read incorrect tx");
|
|
}
|
|
}
|
|
|
|
// shutdown sequence (c.f. Shutdown() in init.cpp)
|
|
txindex.Stop();
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|