|
6 | 6 |
|
7 | 7 | #include <chainparams.h>
|
8 | 8 | #include <consensus/merkle.h>
|
| 9 | +#include <consensus/validation.h> |
9 | 10 | #include <key_io.h>
|
10 | 11 | #include <node/context.h>
|
11 | 12 | #include <pow.h>
|
| 13 | +#include <primitives/transaction.h> |
12 | 14 | #include <script/standard.h>
|
13 | 15 | #include <test/util/script.h>
|
14 | 16 | #include <util/check.h>
|
15 | 17 | #include <validation.h>
|
| 18 | +#include <validationinterface.h> |
16 | 19 | #include <versionbits.h>
|
17 | 20 |
|
18 | 21 | using node::BlockAssembler;
|
19 | 22 | using node::NodeContext;
|
20 | 23 |
|
21 |
| -CTxIn generatetoaddress(const NodeContext& node, const std::string& address) |
| 24 | +COutPoint generatetoaddress(const NodeContext& node, const std::string& address) |
22 | 25 | {
|
23 | 26 | const auto dest = DecodeDestination(address);
|
24 | 27 | assert(IsValidDestination(dest));
|
@@ -58,19 +61,52 @@ std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const
|
58 | 61 | return ret;
|
59 | 62 | }
|
60 | 63 |
|
61 |
| -CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey) |
| 64 | +COutPoint MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey) |
62 | 65 | {
|
63 | 66 | 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; |
64 | 75 |
|
| 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 | +{ |
65 | 90 | while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
|
66 | 91 | ++block->nNonce;
|
67 | 92 | assert(block->nNonce);
|
68 | 93 | }
|
69 | 94 |
|
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 {}; |
74 | 110 | }
|
75 | 111 |
|
76 | 112 | std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey,
|
|
0 commit comments