mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-17 21:32:00 +01:00
Compare commits
12 Commits
cb61462f70
...
45c0ca8962
Author | SHA1 | Date | |
---|---|---|---|
|
45c0ca8962 | ||
|
5f4422d68d | ||
|
3301d2cbe8 | ||
|
9bfb0d75ba | ||
|
7ac281c19c | ||
|
9a3397456d | ||
|
cd94cd7de4 | ||
|
da9cf359a8 | ||
|
c161ef8ac4 | ||
|
7c4f0d045c | ||
|
8f81c3d994 | ||
|
6421b986f6 |
@ -25,7 +25,7 @@
|
|||||||
// a block off the wire, but before we can relay the block on to peers using
|
// a block off the wire, but before we can relay the block on to peers using
|
||||||
// compact block relay.
|
// compact block relay.
|
||||||
|
|
||||||
static void DeserializeBlockTest(benchmark::Bench& bench)
|
static void DeserializeBlockBench(benchmark::Bench& bench)
|
||||||
{
|
{
|
||||||
DataStream stream(benchmark::data::block413567);
|
DataStream stream(benchmark::data::block413567);
|
||||||
std::byte a{0};
|
std::byte a{0};
|
||||||
@ -39,26 +39,18 @@ static void DeserializeBlockTest(benchmark::Bench& bench)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
|
static void CheckBlockBench(benchmark::Bench& bench)
|
||||||
{
|
{
|
||||||
DataStream stream(benchmark::data::block413567);
|
CBlock block;
|
||||||
std::byte a{0};
|
DataStream(benchmark::data::block413567) >> TX_WITH_WITNESS(block);
|
||||||
stream.write({&a, 1}); // Prevent compaction
|
const auto chainParams = CreateChainParams(ArgsManager{}, ChainType::MAIN);
|
||||||
|
|
||||||
ArgsManager bench_args;
|
|
||||||
const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN);
|
|
||||||
|
|
||||||
bench.unit("block").run([&] {
|
bench.unit("block").run([&] {
|
||||||
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
|
block.fChecked = block.m_checked_witness_commitment = block.m_checked_merkle_root = false; // Reset the cached state
|
||||||
stream >> TX_WITH_WITNESS(block);
|
|
||||||
bool rewound = stream.Rewind(benchmark::data::block413567.size());
|
|
||||||
assert(rewound);
|
|
||||||
|
|
||||||
BlockValidationState validationState;
|
BlockValidationState validationState;
|
||||||
bool checked = CheckBlock(block, validationState, chainParams->GetConsensus());
|
bool checked = CheckBlock(block, validationState, chainParams->GetConsensus(), /*fCheckPOW=*/true, /*fCheckMerkleRoot=*/true);
|
||||||
assert(checked);
|
assert(checked && validationState.IsValid());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
BENCHMARK(DeserializeBlockTest, benchmark::PriorityLevel::HIGH);
|
BENCHMARK(DeserializeBlockBench, benchmark::PriorityLevel::HIGH);
|
||||||
BENCHMARK(DeserializeAndCheckBlockTest, benchmark::PriorityLevel::HIGH);
|
BENCHMARK(CheckBlockBench, benchmark::PriorityLevel::HIGH);
|
||||||
|
@ -13,10 +13,19 @@
|
|||||||
#include <test/util/txmempool.h>
|
#include <test/util/txmempool.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
#include <bench/data/block413567.raw.h>
|
||||||
|
#include <node/context.h>
|
||||||
|
#include <node/miner.h>
|
||||||
|
#include <primitives/block.h>
|
||||||
|
#include <test/util/script.h>
|
||||||
|
#include <util/check.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <streams.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class CCoinsViewCache;
|
class CCoinsViewCache;
|
||||||
@ -126,5 +135,53 @@ static void MempoolCheck(benchmark::Bench& bench)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ProcessTransactionBench(benchmark::Bench& bench)
|
||||||
|
{
|
||||||
|
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
|
||||||
|
CTxMemPool& pool{*Assert(testing_setup->m_node.mempool)};
|
||||||
|
ChainstateManager& chainman{*testing_setup->m_node.chainman};
|
||||||
|
|
||||||
|
CBlock block;
|
||||||
|
DataStream(benchmark::data::block413567) >> TX_WITH_WITNESS(block);
|
||||||
|
|
||||||
|
std::vector<CTransactionRef> txs(block.vtx.size() - 1);
|
||||||
|
for (size_t i{1}; i < block.vtx.size(); ++i) {
|
||||||
|
CMutableTransaction mtx{*block.vtx[i]};
|
||||||
|
for (auto& txin : mtx.vin) {
|
||||||
|
txin.nSequence = CTxIn::SEQUENCE_FINAL;
|
||||||
|
txin.scriptSig.clear();
|
||||||
|
txin.scriptWitness.stack = {WITNESS_STACK_ELEM_OP_TRUE};
|
||||||
|
}
|
||||||
|
txs[i - 1] = MakeTransactionRef(std::move(mtx));
|
||||||
|
}
|
||||||
|
|
||||||
|
CCoinsViewCache* coins_tip{nullptr};
|
||||||
|
size_t cached_coin_count{0};
|
||||||
|
{
|
||||||
|
LOCK(cs_main);
|
||||||
|
coins_tip = &chainman.ActiveChainstate().CoinsTip();
|
||||||
|
for (const auto& tx : txs) {
|
||||||
|
const Coin coin(CTxOut(2 * tx->GetValueOut(), P2WSH_OP_TRUE), 1, /*fCoinBaseIn=*/false);
|
||||||
|
for (const auto& in : tx->vin) {
|
||||||
|
coins_tip->AddCoin(in.prevout, Coin{coin}, /*possible_overwrite=*/false);
|
||||||
|
cached_coin_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bench.batch(txs.size()).run([&] {
|
||||||
|
LOCK2(cs_main, pool.cs);
|
||||||
|
assert(coins_tip->GetCacheSize() == cached_coin_count);
|
||||||
|
for (const auto& tx : txs) pool.removeRecursive(*tx, MemPoolRemovalReason::REPLACED);
|
||||||
|
assert(pool.size() == 0);
|
||||||
|
|
||||||
|
for (const auto& tx : txs) {
|
||||||
|
const auto res{chainman.ProcessTransaction(tx, /*test_accept=*/true)};
|
||||||
|
assert(res.m_result_type == MempoolAcceptResult::ResultType::VALID);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
BENCHMARK(ComplexMemPool, benchmark::PriorityLevel::HIGH);
|
BENCHMARK(ComplexMemPool, benchmark::PriorityLevel::HIGH);
|
||||||
BENCHMARK(MempoolCheck, benchmark::PriorityLevel::HIGH);
|
BENCHMARK(MempoolCheck, benchmark::PriorityLevel::HIGH);
|
||||||
|
BENCHMARK(ProcessTransactionBench, benchmark::PriorityLevel::HIGH);
|
||||||
|
@ -38,22 +38,36 @@ bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
|
|||||||
// of a tx as spent, it does not check if the tx has duplicate inputs.
|
// of a tx as spent, it does not check if the tx has duplicate inputs.
|
||||||
// Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
|
// Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
|
||||||
// the underlying coins database.
|
// the underlying coins database.
|
||||||
std::set<COutPoint> vInOutPoints;
|
if (tx.vin.size() == 1) {
|
||||||
for (const auto& txin : tx.vin) {
|
if (tx.IsCoinBase()) {
|
||||||
if (!vInOutPoints.insert(txin.prevout).second)
|
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100) {
|
||||||
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (tx.vin.size() == 2) {
|
||||||
|
if (tx.vin[0].prevout == tx.vin[1].prevout) {
|
||||||
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
|
||||||
}
|
}
|
||||||
|
if (tx.vin[0].prevout.IsNull() || tx.vin[1].prevout.IsNull()) {
|
||||||
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::vector<COutPoint> sortedPrevouts;
|
||||||
|
sortedPrevouts.reserve(tx.vin.size());
|
||||||
|
for (const auto& txin : tx.vin) {
|
||||||
|
sortedPrevouts.push_back(txin.prevout);
|
||||||
|
}
|
||||||
|
std::sort(sortedPrevouts.begin(), sortedPrevouts.end());
|
||||||
|
if (std::ranges::adjacent_find(sortedPrevouts) != sortedPrevouts.end()) {
|
||||||
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
|
||||||
|
}
|
||||||
|
|
||||||
if (tx.IsCoinBase())
|
for (const auto& in : sortedPrevouts) {
|
||||||
{
|
if (!in.hash.IsNull()) break; // invalid values can only be at the beginning
|
||||||
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
|
if (in.IsNull()) {
|
||||||
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (const auto& txin : tx.vin)
|
|
||||||
if (txin.prevout.IsNull())
|
|
||||||
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -406,20 +406,110 @@ BOOST_AUTO_TEST_CASE(tx_oversized)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(basic_transaction_tests)
|
static CMutableTransaction CreateTransaction()
|
||||||
{
|
{
|
||||||
// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
|
// Serialized random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
|
||||||
unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
|
static constexpr auto ser_tx{"01000000016bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4000000008c493046022100da0dc6aecefe1e06efdf05773757deb168820930e3b0d03f46f5fcf150bf990c022100d25b5c87040076e4f253f8262e763e2dd51e7ff0be157727c4bc42807f17bd39014104e6c26ef67dc610d2cd192484789a6cf9aea9930b944b7e2db5342b9d9e5b9ff79aff9a2ee1978dd7fd01dfc522ee02283d3b06a9d03acf8096968d7dbb0f9178ffffffff028ba7940e000000001976a914badeecfdef0507247fc8f74241d73bc039972d7b88ac4094a802000000001976a914c10932483fec93ed51f5fe95e72559f2cc7043f988ac0000000000"_hex};
|
||||||
std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
|
|
||||||
DataStream stream(vch);
|
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
stream >> TX_WITH_WITNESS(tx);
|
DataStream(ser_tx) >> TX_WITH_WITNESS(tx);
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(transaction_duplicate_input_test)
|
||||||
|
{
|
||||||
|
auto tx{CreateTransaction()};
|
||||||
|
|
||||||
TxValidationState state;
|
TxValidationState state;
|
||||||
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
|
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
|
||||||
|
|
||||||
// Check that duplicate txins fail
|
// Add duplicate input
|
||||||
tx.vin.push_back(tx.vin[0]);
|
tx.vin.emplace_back(tx.vin[0]);
|
||||||
BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
|
std::ranges::shuffle(tx.vin, m_rng);
|
||||||
|
BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with 2 duplicate txins should be invalid.");
|
||||||
|
|
||||||
|
// ... add a valid input for more complex check
|
||||||
|
tx.vin.emplace_back(COutPoint(Txid::FromUint256(uint256{1}), 1));
|
||||||
|
std::ranges::shuffle(tx.vin, m_rng);
|
||||||
|
BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with 3 inputs (2 valid, 1 duplicate) should be invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(transaction_duplicate_detection_test)
|
||||||
|
{
|
||||||
|
// Randomized testing against hash- and tree-based duplicate check
|
||||||
|
auto reference_duplicate_check_hash{[](const std::vector<CTxIn>& vin) {
|
||||||
|
std::unordered_set<COutPoint, SaltedOutpointHasher> vInOutPoints;
|
||||||
|
for (const auto& txin : vin) {
|
||||||
|
if (!vInOutPoints.insert(txin.prevout).second) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}};
|
||||||
|
auto reference_duplicate_check_tree{[](const std::vector<CTxIn>& vin) {
|
||||||
|
std::set<COutPoint> vInOutPoints;
|
||||||
|
for (const auto& txin : vin) {
|
||||||
|
if (!vInOutPoints.insert(txin.prevout).second) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}};
|
||||||
|
|
||||||
|
std::vector<Txid> hashes;
|
||||||
|
std::vector<uint32_t> ns;
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
hashes.emplace_back(Txid::FromUint256(m_rng.rand256()));
|
||||||
|
ns.emplace_back(m_rng.rand32());
|
||||||
|
}
|
||||||
|
auto tx{CreateTransaction()};
|
||||||
|
TxValidationState state;
|
||||||
|
for (int i{0}; i < 100; ++i) {
|
||||||
|
if (m_rng.randbool()) {
|
||||||
|
tx.vin.clear();
|
||||||
|
}
|
||||||
|
for (int j{0}, num_inputs{1 + m_rng.randrange(5)}; j < num_inputs; ++j) {
|
||||||
|
if (COutPoint outpoint(hashes[m_rng.randrange(hashes.size())], ns[m_rng.randrange(ns.size())]); !outpoint.IsNull()) {
|
||||||
|
tx.vin.emplace_back(outpoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::ranges::shuffle(tx.vin, m_rng);
|
||||||
|
|
||||||
|
bool actual{CheckTransaction(CTransaction(tx), state)};
|
||||||
|
BOOST_CHECK_EQUAL(actual, reference_duplicate_check_hash(tx.vin));
|
||||||
|
BOOST_CHECK_EQUAL(actual, reference_duplicate_check_tree(tx.vin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(transaction_null_prevout_detection_test)
|
||||||
|
{
|
||||||
|
// Randomized testing against linear null prevout check
|
||||||
|
auto reference_null_prevout_check_hash{[](const std::vector<CTxIn>& vin) {
|
||||||
|
for (const auto& txin : vin) {
|
||||||
|
if (txin.prevout.IsNull()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}};
|
||||||
|
|
||||||
|
auto tx{CreateTransaction()};
|
||||||
|
TxValidationState state;
|
||||||
|
for (int i{0}; i < 100; ++i) {
|
||||||
|
if (m_rng.randbool()) {
|
||||||
|
tx.vin.clear();
|
||||||
|
}
|
||||||
|
for (int j{0}, num_inputs{1 + m_rng.randrange(5)}; j < num_inputs; ++j) {
|
||||||
|
switch (m_rng.randrange(5)) {
|
||||||
|
case 0: tx.vin.emplace_back(COutPoint()); break; // Null prevout
|
||||||
|
case 1: tx.vin.emplace_back(Txid::FromUint256(uint256::ZERO), m_rng.rand32()); break; // Null hash, random index
|
||||||
|
case 2: tx.vin.emplace_back(Txid::FromUint256(m_rng.rand256()), COutPoint::NULL_INDEX); break; // Random hash, Null index
|
||||||
|
default: tx.vin.emplace_back(Txid::FromUint256(m_rng.rand256()), m_rng.rand32()); // Random prevout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::ranges::shuffle(tx.vin, m_rng);
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(CheckTransaction(CTransaction(tx), state), reference_null_prevout_check_hash(tx.vin));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_Get)
|
BOOST_AUTO_TEST_CASE(test_Get)
|
||||||
@ -1048,4 +1138,116 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
|
|||||||
CheckIsNotStandard(t, "dust");
|
CheckIsNotStandard(t, "dust");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_uint256_sorting)
|
||||||
|
{
|
||||||
|
// Sorting
|
||||||
|
std::vector original{
|
||||||
|
uint256{1},
|
||||||
|
uint256{2},
|
||||||
|
uint256{3}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector shuffled{original};
|
||||||
|
std::ranges::shuffle(shuffled, m_rng);
|
||||||
|
std::sort(shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL_COLLECTIONS(original.begin(), original.end(), shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
constexpr auto a{uint256{1}},
|
||||||
|
b{uint256{2}},
|
||||||
|
c{uint256{3}};
|
||||||
|
|
||||||
|
BOOST_CHECK(a == a);
|
||||||
|
BOOST_CHECK(a == uint256{1});
|
||||||
|
BOOST_CHECK(b == b);
|
||||||
|
BOOST_CHECK(c == c);
|
||||||
|
BOOST_CHECK(a != b);
|
||||||
|
BOOST_CHECK(a != uint256{10});
|
||||||
|
BOOST_CHECK(a != c);
|
||||||
|
BOOST_CHECK(b != c);
|
||||||
|
|
||||||
|
BOOST_CHECK(a < b);
|
||||||
|
BOOST_CHECK(a < uint256{10});
|
||||||
|
BOOST_CHECK(b < c);
|
||||||
|
BOOST_CHECK(a < c);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_transaction_identifier_sorting)
|
||||||
|
{
|
||||||
|
std::vector original{
|
||||||
|
Txid::FromUint256(uint256{1}),
|
||||||
|
Txid::FromUint256(uint256{2}),
|
||||||
|
Txid::FromUint256(uint256{3})
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector shuffled{original};
|
||||||
|
std::ranges::shuffle(shuffled, m_rng);
|
||||||
|
std::sort(shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL_COLLECTIONS(original.begin(), original.end(), shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
const auto a(Txid::FromUint256(uint256{1})),
|
||||||
|
b(Txid::FromUint256(uint256{2})),
|
||||||
|
c(Txid::FromUint256(uint256{3}));
|
||||||
|
|
||||||
|
BOOST_CHECK(a == uint256{1});
|
||||||
|
|
||||||
|
BOOST_CHECK(a == a);
|
||||||
|
BOOST_CHECK(a == Txid::FromUint256(uint256{1}));
|
||||||
|
BOOST_CHECK(b == b);
|
||||||
|
BOOST_CHECK(c == c);
|
||||||
|
BOOST_CHECK(a != b);
|
||||||
|
BOOST_CHECK(a != Txid::FromUint256(uint256{10}));
|
||||||
|
BOOST_CHECK(a != c);
|
||||||
|
BOOST_CHECK(b != c);
|
||||||
|
|
||||||
|
BOOST_CHECK(a < b);
|
||||||
|
BOOST_CHECK(a < Txid::FromUint256(uint256{10}));
|
||||||
|
BOOST_CHECK(b < c);
|
||||||
|
BOOST_CHECK(a < c);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_coutpoint_sorting)
|
||||||
|
{
|
||||||
|
// Sorting
|
||||||
|
std::vector original{
|
||||||
|
COutPoint(Txid::FromUint256(uint256{1}), 1),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{1}), 2),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{1}), 3),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{2}), 1),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{2}), 2),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{2}), 3),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{3}), 1),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{3}), 2),
|
||||||
|
COutPoint(Txid::FromUint256(uint256{3}), 3)
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector shuffled{original};
|
||||||
|
std::ranges::shuffle(shuffled, m_rng);
|
||||||
|
std::sort(shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL_COLLECTIONS(original.begin(), original.end(), shuffled.begin(), shuffled.end());
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
const auto a{COutPoint(Txid::FromUint256(uint256{1}), 1)},
|
||||||
|
b{COutPoint(Txid::FromUint256(uint256{1}), 2)},
|
||||||
|
c{COutPoint(Txid::FromUint256(uint256{2}), 1)};
|
||||||
|
|
||||||
|
BOOST_CHECK(a == a);
|
||||||
|
BOOST_CHECK(a == COutPoint(Txid::FromUint256(uint256{1}), 1));
|
||||||
|
BOOST_CHECK(b == b);
|
||||||
|
BOOST_CHECK(c == c);
|
||||||
|
BOOST_CHECK(a != b);
|
||||||
|
BOOST_CHECK(a != COutPoint(Txid::FromUint256(uint256{1}), 10));
|
||||||
|
BOOST_CHECK(a != c);
|
||||||
|
BOOST_CHECK(b != c);
|
||||||
|
|
||||||
|
BOOST_CHECK(a < b);
|
||||||
|
BOOST_CHECK(a < COutPoint(Txid::FromUint256(uint256{1}), 10));
|
||||||
|
BOOST_CHECK(b < c);
|
||||||
|
BOOST_CHECK(a < c);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
@ -616,3 +616,8 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
|
|||||||
{
|
{
|
||||||
return os << num.ToString();
|
return os << num.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const COutPoint& outpoint)
|
||||||
|
{
|
||||||
|
return os << outpoint.hash << ", " << outpoint.n;
|
||||||
|
}
|
||||||
|
@ -291,6 +291,7 @@ inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
|
|||||||
std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
|
std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
|
||||||
std::ostream& operator<<(std::ostream& os, const uint160& num);
|
std::ostream& operator<<(std::ostream& os, const uint160& num);
|
||||||
std::ostream& operator<<(std::ostream& os, const uint256& num);
|
std::ostream& operator<<(std::ostream& os, const uint256& num);
|
||||||
|
std::ostream& operator<<(std::ostream& os, const COutPoint& outpoint);
|
||||||
// @}
|
// @}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,7 +88,7 @@ class InitTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
args = ['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1']
|
args = ['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1']
|
||||||
for terminate_line in lines_to_terminate_after:
|
for terminate_line in lines_to_terminate_after:
|
||||||
self.log.info(f"Starting node and will exit after line {terminate_line}")
|
self.log.info(f"Starting node and will terminate after line {terminate_line}")
|
||||||
with node.busy_wait_for_debug_log([terminate_line]):
|
with node.busy_wait_for_debug_log([terminate_line]):
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
# CREATE_NEW_PROCESS_GROUP is required in order to be able
|
# CREATE_NEW_PROCESS_GROUP is required in order to be able
|
||||||
@ -108,12 +108,22 @@ class InitTest(BitcoinTestFramework):
|
|||||||
'blocks/index/*.ldb': 'Error opening block database.',
|
'blocks/index/*.ldb': 'Error opening block database.',
|
||||||
'chainstate/*.ldb': 'Error opening coins database.',
|
'chainstate/*.ldb': 'Error opening coins database.',
|
||||||
'blocks/blk*.dat': 'Error loading block database.',
|
'blocks/blk*.dat': 'Error loading block database.',
|
||||||
|
'indexes/txindex/MANIFEST*': 'LevelDB error: Corruption: CURRENT points to a non-existent file',
|
||||||
|
# Removing these files does not result in a startup error:
|
||||||
|
# 'indexes/blockfilter/basic/*.dat', 'indexes/blockfilter/basic/db/*.*', 'indexes/coinstats/db/*.*',
|
||||||
|
# 'indexes/txindex/*.log', 'indexes/txindex/CURRENT', 'indexes/txindex/LOCK'
|
||||||
}
|
}
|
||||||
|
|
||||||
files_to_perturb = {
|
files_to_perturb = {
|
||||||
'blocks/index/*.ldb': 'Error loading block database.',
|
'blocks/index/*.ldb': 'Error loading block database.',
|
||||||
'chainstate/*.ldb': 'Error opening coins database.',
|
'chainstate/*.ldb': 'Error opening coins database.',
|
||||||
'blocks/blk*.dat': 'Corrupted block database detected.',
|
'blocks/blk*.dat': 'Corrupted block database detected.',
|
||||||
|
'indexes/blockfilter/basic/db/*.*': 'LevelDB error: Corruption',
|
||||||
|
'indexes/coinstats/db/*.*': 'LevelDB error: Corruption',
|
||||||
|
'indexes/txindex/*.log': 'LevelDB error: Corruption',
|
||||||
|
'indexes/txindex/CURRENT': 'LevelDB error: Corruption',
|
||||||
|
# Perturbing these files does not result in a startup error:
|
||||||
|
# 'indexes/blockfilter/basic/*.dat', 'indexes/txindex/MANIFEST*', 'indexes/txindex/LOCK'
|
||||||
}
|
}
|
||||||
|
|
||||||
for file_patt, err_fragment in files_to_delete.items():
|
for file_patt, err_fragment in files_to_delete.items():
|
||||||
@ -135,9 +145,10 @@ class InitTest(BitcoinTestFramework):
|
|||||||
self.stop_node(0)
|
self.stop_node(0)
|
||||||
|
|
||||||
self.log.info("Test startup errors after perturbing certain essential files")
|
self.log.info("Test startup errors after perturbing certain essential files")
|
||||||
|
dirs = ["blocks", "chainstate", "indexes"]
|
||||||
for file_patt, err_fragment in files_to_perturb.items():
|
for file_patt, err_fragment in files_to_perturb.items():
|
||||||
shutil.copytree(node.chain_path / "blocks", node.chain_path / "blocks_bak")
|
for dir in dirs:
|
||||||
shutil.copytree(node.chain_path / "chainstate", node.chain_path / "chainstate_bak")
|
shutil.copytree(node.chain_path / dir, node.chain_path / f"{dir}_bak")
|
||||||
target_files = list(node.chain_path.glob(file_patt))
|
target_files = list(node.chain_path.glob(file_patt))
|
||||||
|
|
||||||
for target_file in target_files:
|
for target_file in target_files:
|
||||||
@ -151,10 +162,9 @@ class InitTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
start_expecting_error(err_fragment)
|
start_expecting_error(err_fragment)
|
||||||
|
|
||||||
shutil.rmtree(node.chain_path / "blocks")
|
for dir in dirs:
|
||||||
shutil.rmtree(node.chain_path / "chainstate")
|
shutil.rmtree(node.chain_path / dir)
|
||||||
shutil.move(node.chain_path / "blocks_bak", node.chain_path / "blocks")
|
shutil.move(node.chain_path / f"{dir}_bak", node.chain_path / dir)
|
||||||
shutil.move(node.chain_path / "chainstate_bak", node.chain_path / "chainstate")
|
|
||||||
|
|
||||||
def init_pid_test(self):
|
def init_pid_test(self):
|
||||||
BITCOIN_PID_FILENAME_CUSTOM = "my_fancy_bitcoin_pid_file.foobar"
|
BITCOIN_PID_FILENAME_CUSTOM = "my_fancy_bitcoin_pid_file.foobar"
|
||||||
|
@ -45,6 +45,7 @@ from test_framework.util import (
|
|||||||
assert_equal,
|
assert_equal,
|
||||||
assert_greater_than,
|
assert_greater_than,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
|
sync_txindex,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
from test_framework.wallet_util import generate_keypair
|
from test_framework.wallet_util import generate_keypair
|
||||||
@ -270,6 +271,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
self.log.info('A coinbase transaction')
|
self.log.info('A coinbase transaction')
|
||||||
# Pick the input of the first tx we created, so it has to be a coinbase tx
|
# Pick the input of the first tx we created, so it has to be a coinbase tx
|
||||||
|
sync_txindex(self, node)
|
||||||
raw_tx_coinbase_spent = node.getrawtransaction(txid=node.decoderawtransaction(hexstring=raw_tx_in_block)['vin'][0]['txid'])
|
raw_tx_coinbase_spent = node.getrawtransaction(txid=node.decoderawtransaction(hexstring=raw_tx_in_block)['vin'][0]['txid'])
|
||||||
tx = tx_from_hex(raw_tx_coinbase_spent)
|
tx = tx_from_hex(raw_tx_coinbase_spent)
|
||||||
self.check_mempool_result(
|
self.check_mempool_result(
|
||||||
|
@ -34,6 +34,7 @@ from test_framework.util import (
|
|||||||
assert_equal,
|
assert_equal,
|
||||||
assert_greater_than,
|
assert_greater_than,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
|
sync_txindex,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import (
|
from test_framework.wallet import (
|
||||||
getnewdestination,
|
getnewdestination,
|
||||||
@ -70,7 +71,7 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
self.num_nodes = 3
|
self.num_nodes = 3
|
||||||
self.extra_args = [
|
self.extra_args = [
|
||||||
["-txindex"],
|
["-txindex"],
|
||||||
["-txindex"],
|
[],
|
||||||
["-fastprune", "-prune=1"],
|
["-fastprune", "-prune=1"],
|
||||||
]
|
]
|
||||||
# whitelist peers to speed up tx relay / mempool sync
|
# whitelist peers to speed up tx relay / mempool sync
|
||||||
@ -109,6 +110,7 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex")
|
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex")
|
||||||
|
|
||||||
if n == 0:
|
if n == 0:
|
||||||
|
sync_txindex(self, self.nodes[n])
|
||||||
# With -txindex.
|
# With -txindex.
|
||||||
# 1. valid parameters - only supply txid
|
# 1. valid parameters - only supply txid
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId), tx['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId), tx['hex'])
|
||||||
|
@ -12,6 +12,7 @@ from test_framework.test_framework import BitcoinTestFramework
|
|||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
|
sync_txindex,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
|
|
||||||
@ -77,6 +78,7 @@ class MerkleBlockTest(BitcoinTestFramework):
|
|||||||
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist))
|
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist))
|
||||||
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist))
|
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist))
|
||||||
# We can always get a proof if we have a -txindex
|
# We can always get a proof if we have a -txindex
|
||||||
|
sync_txindex(self, self.nodes[1])
|
||||||
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent])
|
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent])
|
||||||
# We can't get a proof if we specify transactions from different blocks
|
# We can't get a proof if we specify transactions from different blocks
|
||||||
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
|
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
|
||||||
|
@ -592,3 +592,10 @@ def find_vout_for_address(node, txid, addr):
|
|||||||
if addr == tx["vout"][i]["scriptPubKey"]["address"]:
|
if addr == tx["vout"][i]["scriptPubKey"]["address"]:
|
||||||
return i
|
return i
|
||||||
raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr))
|
raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr))
|
||||||
|
|
||||||
|
|
||||||
|
def sync_txindex(test_framework, node):
|
||||||
|
test_framework.log.debug("Waiting for node txindex to sync")
|
||||||
|
sync_start = int(time.time())
|
||||||
|
test_framework.wait_until(lambda: node.getindexinfo("txindex")["txindex"]["synced"])
|
||||||
|
test_framework.log.debug(f"Synced in {time.time() - sync_start} seconds")
|
||||||
|
@ -117,7 +117,6 @@ class AddressInputTypeGrouping(BitcoinTestFramework):
|
|||||||
self.extra_args = [
|
self.extra_args = [
|
||||||
[
|
[
|
||||||
"-addresstype=bech32",
|
"-addresstype=bech32",
|
||||||
"-txindex",
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"-addresstype=p2sh-segwit",
|
"-addresstype=p2sh-segwit",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user