Skip to content

Commit fa846ee

Browse files
author
MarcoFalke
committed
test: Add util to mine invalid blocks
With the current utils it is only possible to mine valid blocks. This commit adds new util methods to mine invalid blocks.
1 parent 7b45d17 commit fa846ee

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

src/bench/block_assemble.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void AssembleBlock(benchmark::Bench& bench)
2727
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
2828
for (size_t b{0}; b < NUM_BLOCKS; ++b) {
2929
CMutableTransaction tx;
30-
tx.vin.push_back(MineBlock(test_setup->m_node, P2WSH_OP_TRUE));
30+
tx.vin.push_back(CTxIn{MineBlock(test_setup->m_node, P2WSH_OP_TRUE)});
3131
tx.vin.back().scriptWitness = witness;
3232
tx.vout.emplace_back(1337, P2WSH_OP_TRUE);
3333
if (NUM_BLOCKS - b >= COINBASE_MATURITY)

src/test/fuzz/tx_pool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ void initialize_tx_pool()
4242
g_setup = testing_setup.get();
4343

4444
for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
45-
CTxIn in = MineBlock(g_setup->m_node, P2WSH_OP_TRUE);
45+
COutPoint prevout{MineBlock(g_setup->m_node, P2WSH_OP_TRUE)};
4646
// Remember the txids to avoid expensive disk access later on
4747
auto& outpoints = i < COINBASE_MATURITY ?
4848
g_outpoints_coinbase_init_mature :
4949
g_outpoints_coinbase_init_immature;
50-
outpoints.push_back(in.prevout);
50+
outpoints.push_back(prevout);
5151
}
5252
SyncWithValidationInterfaceQueue();
5353
}

src/test/util/mining.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77
#include <chainparams.h>
88
#include <consensus/merkle.h>
9+
#include <consensus/validation.h>
910
#include <key_io.h>
1011
#include <node/context.h>
1112
#include <pow.h>
13+
#include <primitives/transaction.h>
1214
#include <script/standard.h>
1315
#include <test/util/script.h>
1416
#include <util/check.h>
1517
#include <validation.h>
18+
#include <validationinterface.h>
1619
#include <versionbits.h>
1720

1821
using node::BlockAssembler;
1922
using node::NodeContext;
2023

21-
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
24+
COutPoint generatetoaddress(const NodeContext& node, const std::string& address)
2225
{
2326
const auto dest = DecodeDestination(address);
2427
assert(IsValidDestination(dest));
@@ -58,19 +61,52 @@ std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const
5861
return ret;
5962
}
6063

61-
CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
64+
COutPoint MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
6265
{
6366
auto block = PrepareBlock(node, coinbase_scriptPubKey);
67+
auto valid = MineBlock(node, block);
68+
assert(!valid.IsNull());
69+
return valid;
70+
}
71+
72+
struct BlockValidationStateCatcher : public CValidationInterface {
73+
const uint256 m_hash;
74+
std::optional<BlockValidationState> m_state;
6475

76+
BlockValidationStateCatcher(const uint256& hash)
77+
: m_hash{hash},
78+
m_state{} {}
79+
80+
protected:
81+
void BlockChecked(const CBlock& block, const BlockValidationState& state) override
82+
{
83+
if (block.GetHash() != m_hash) return;
84+
m_state = state;
85+
}
86+
};
87+
88+
COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
89+
{
6590
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
6691
++block->nNonce;
6792
assert(block->nNonce);
6893
}
6994

70-
bool processed{Assert(node.chainman)->ProcessNewBlock(block, true, true, nullptr)};
71-
assert(processed);
72-
73-
return CTxIn{block->vtx[0]->GetHash(), 0};
95+
auto& chainman{*Assert(node.chainman)};
96+
const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
97+
bool new_block;
98+
BlockValidationStateCatcher bvsc{block->GetHash()};
99+
RegisterValidationInterface(&bvsc);
100+
const bool processed{chainman.ProcessNewBlock(block, true, true, &new_block)};
101+
const bool duplicate{!new_block && processed};
102+
assert(!duplicate);
103+
UnregisterValidationInterface(&bvsc);
104+
SyncWithValidationInterfaceQueue();
105+
const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
106+
assert(old_height + was_valid == WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight()));
107+
108+
if (was_valid) return {block->vtx[0]->GetHash(), 0};
109+
return {};
74110
}
75111

76112
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey,

src/test/util/mining.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
class CBlock;
1515
class CChainParams;
16+
class COutPoint;
1617
class CScript;
17-
class CTxIn;
1818
namespace node {
1919
struct NodeContext;
2020
} // namespace node
@@ -23,14 +23,20 @@ struct NodeContext;
2323
std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params);
2424

2525
/** Returns the generated coin */
26-
CTxIn MineBlock(const node::NodeContext&, const CScript& coinbase_scriptPubKey);
26+
COutPoint MineBlock(const node::NodeContext&, const CScript& coinbase_scriptPubKey);
27+
28+
/**
29+
* Returns the generated coin (or Null if the block was invalid).
30+
* It is recommended to call RegenerateCommitments before mining the block to avoid merkle tree mismatches.
31+
**/
32+
COutPoint MineBlock(const node::NodeContext&, std::shared_ptr<CBlock>& block);
2733

2834
/** Prepare a block to be mined */
2935
std::shared_ptr<CBlock> PrepareBlock(const node::NodeContext&, const CScript& coinbase_scriptPubKey);
3036
std::shared_ptr<CBlock> PrepareBlock(const node::NodeContext& node, const CScript& coinbase_scriptPubKey,
3137
const node::BlockAssembler::Options& assembler_options);
3238

3339
/** RPC-like helper function, returns the generated coin */
34-
CTxIn generatetoaddress(const node::NodeContext&, const std::string& address);
40+
COutPoint generatetoaddress(const node::NodeContext&, const std::string& address);
3541

3642
#endif // BITCOIN_TEST_UTIL_MINING_H

0 commit comments

Comments
 (0)