forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathevo_cbtx_tests.cpp
More file actions
221 lines (187 loc) · 8.33 KB
/
Copy pathevo_cbtx_tests.cpp
File metadata and controls
221 lines (187 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/llmq_tests.h>
#include <test/util/setup_common.h>
#include <bls/bls.h>
#include <chain.h>
#include <chainlock/chainlock.h>
#include <chainparams.h>
#include <compat/endian.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <evo/cbtx.h>
#include <evo/evodb.h>
#include <evo/specialtx.h>
#include <evo/specialtxman.h>
#include <hash.h>
#include <llmq/blockprocessor.h>
#include <llmq/commitment.h>
#include <llmq/context.h>
#include <llmq/params.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <uint256.h>
#include <validation.h>
#include <cstdint>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <boost/test/unit_test.hpp>
using namespace llmq;
using namespace llmq::testutils;
BOOST_AUTO_TEST_SUITE(evo_cbtx_tests)
// Out-of-range bestCLHeightDiff (>= pindex->nHeight) must be rejected with
// "bad-cbtx-cldiff" so that the subsequent GetAncestor() call sees a valid height.
//
// The defensive nullptr branch after GetAncestor() returns "bad-cbtx-cldiff-ancestor".
// That branch is unreachable in practice (the range check guarantees the requested
// ancestor height is in [0, pindex->nHeight - 1], for which GetAncestor() never returns
// nullptr) and cannot be exercised from a unit test: a fake CBlockIndex with no pprev
// would trip GetAncestor()'s `assert(pprev)` while walking, not return nullptr.
BOOST_FIXTURE_TEST_CASE(check_cbtx_best_chainlock_rejects_excessive_height_diff, RegTestingSetup)
{
const auto& consensus_params = Params().GetConsensus();
const auto& chain = *WITH_LOCK(::cs_main, return &m_node.chainman->ActiveChain());
auto& qman = *Assert(m_node.llmq_ctx)->qman;
auto& chainlocks = *Assert(m_node.chainlocks);
// Standalone fake block index with no predecessor, so the prevBlockCoinbaseChainlock
// branch is skipped and the validation path under test is reached directly.
CBlockIndex pindex;
pindex.nHeight = 5;
// A structurally-valid BLS signature is required for the IsValid() guard.
CBLSSecretKey sk;
sk.MakeNewKey();
const bool legacy_scheme = bls::bls_legacy_scheme.load();
CBLSSignature valid_sig = sk.Sign(uint256::ONE, legacy_scheme);
BOOST_REQUIRE(valid_sig.IsValid());
CCbTx cbTx;
cbTx.nVersion = CCbTx::Version::CLSIG_AND_BALANCE;
cbTx.bestCLSignature = valid_sig;
// bestCLHeightDiff == nHeight: lower boundary of the rejected range.
cbTx.bestCLHeightDiff = static_cast<uint32_t>(pindex.nHeight);
BlockValidationState state;
BOOST_CHECK(!CheckCbTxBestChainlock(cbTx, &pindex, consensus_params, chain, qman, chainlocks, state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-cbtx-cldiff");
// Upper boundary: uint32_t max.
cbTx.bestCLHeightDiff = std::numeric_limits<uint32_t>::max();
BlockValidationState state_big;
BOOST_CHECK(!CheckCbTxBestChainlock(cbTx, &pindex, consensus_params, chain, qman, chainlocks, state_big));
BOOST_CHECK_EQUAL(state_big.GetRejectReason(), "bad-cbtx-cldiff");
}
namespace {
// Mirrors private DB keys in llmq/blockprocessor.cpp so tests can install
// mined-commitment state without a full DKG/mining path.
static const std::string DB_MINED_COMMITMENT = "q_mc";
static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT = "q_mcih";
std::tuple<std::string, Consensus::LLMQType, uint32_t> BuildInversedHeightKey(Consensus::LLMQType llmqType, int nMinedHeight)
{
return std::make_tuple(DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT, llmqType,
htobe32_internal(std::numeric_limits<uint32_t>::max() - nMinedHeight));
}
// Store a mined commitment as if it was mined at `mined_height` for the genesis
// quorum base (quorumHeight 0). GetMinedCommitmentsUntilBlock iterates inverted-
// height keys in [pindex->nHeight, 0), so scan height must be >= mined_height
// and mined_height must be > 0 for the entry to be returned.
void WriteMinedCommitment(CEvoDB& evoDb, const CFinalCommitment& qc, const uint256& mined_block_hash, int mined_height)
{
assert(mined_height > 0);
evoDb.Write(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash)),
std::make_pair(qc, mined_block_hash));
evoDb.Write(BuildInversedHeightKey(qc.llmqType, mined_height), /*quorumHeight=*/0);
}
CTransactionRef MakeCommitmentTx(const CFinalCommitment& qc, int height)
{
CFinalCommitmentTxPayload payload;
payload.nHeight = height;
payload.commitment = qc;
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_QUORUM_COMMITMENT;
SetTxPayload(tx, payload);
return MakeTransactionRef(std::move(tx));
}
uint256 CalcQuorumMerkleRootForCommitment(const CFinalCommitment& qc)
{
std::vector<uint256> hashes{::SerializeHash(qc)};
bool mutated{false};
return ComputeMerkleRoot(hashes, &mutated);
}
CFinalCommitment MakeDistinctCommitment(const Consensus::LLMQParams& params, const uint256& quorum_hash, uint8_t salt)
{
CFinalCommitment qc = CreateValidCommitment(params, quorum_hash);
// Force a deterministic difference even if random BLS material collides.
qc.quorumVvecHash = uint256{std::vector<unsigned char>(32, salt)};
return qc;
}
CBlock MakeEmptyBlock()
{
CBlock block;
block.vtx.emplace_back(MakeTransactionRef(CMutableTransaction{}));
return block;
}
void ExpectQuorumMerkleRoot(const CBlock& block, const CBlockIndex* pindex, const CQuorumBlockProcessor& qblockman,
const CFinalCommitment& qc)
{
uint256 merkle_root;
BlockValidationState state;
BOOST_REQUIRE(CalcCbTxMerkleRootQuorums(block, pindex, qblockman, merkle_root, state));
BOOST_CHECK_EQUAL(merkle_root.ToString(), CalcQuorumMerkleRootForCommitment(qc).ToString());
}
const CBlockIndex* GenesisIndex(const node::NodeContext& node)
{
LOCK(cs_main);
return node.chainman->ActiveChain()[0];
}
} // anonymous namespace
// Activate DIP0003 immediately so GetCommitmentsFromBlock accepts the payload
// at a low height without a long fake chain.
struct Dip3ActiveSetup : public RegTestingSetup {
Dip3ActiveSetup() :
RegTestingSetup({"-dip3params=1:1"})
{
}
};
// End to end: disconnecting the block that mined a commitment must make a replacement
// commitment for the same quorum base visible, even though the active base-block list
// that keys the caches is unchanged across the swap.
BOOST_FIXTURE_TEST_CASE(qc_hash_cache_invalidated_by_undoblock, Dip3ActiveSetup)
{
auto& evoDb = *Assert(m_node.evodb);
auto& qblockman = *Assert(m_node.llmq_ctx)->quorum_block_processor;
const auto& params = GetLLMQParams(Consensus::LLMQType::LLMQ_TEST);
const CBlockIndex* pindex_genesis = GenesisIndex(m_node);
BOOST_REQUIRE(pindex_genesis != nullptr);
const uint256 quorum_hash = pindex_genesis->GetBlockHash();
const CFinalCommitment qc_a = MakeDistinctCommitment(params, quorum_hash, /*salt=*/0x33);
const CFinalCommitment qc_b = MakeDistinctCommitment(params, quorum_hash, /*salt=*/0x44);
BOOST_REQUIRE(::SerializeHash(qc_a) != ::SerializeHash(qc_b));
const uint256 mined_hash_a = GetTestBlockHash(11);
const uint256 mined_hash_b = GetTestBlockHash(12);
constexpr int mined_height = 1;
{
auto dbTx = evoDb.BeginTransaction();
WriteMinedCommitment(evoDb, qc_a, mined_hash_a, mined_height);
dbTx->Commit();
}
CBlockIndex pindex_mined;
pindex_mined.nHeight = mined_height;
pindex_mined.pprev = const_cast<CBlockIndex*>(pindex_genesis);
pindex_mined.phashBlock = &mined_hash_a;
CBlock block_with_qc = MakeEmptyBlock();
block_with_qc.vtx.emplace_back(MakeCommitmentTx(qc_a, mined_height));
const CBlock empty_block = MakeEmptyBlock();
ExpectQuorumMerkleRoot(empty_block, &pindex_mined, qblockman, qc_a);
{
LOCK(cs_main);
auto dbTx = evoDb.BeginTransaction();
BOOST_REQUIRE(qblockman.UndoBlock(block_with_qc, &pindex_mined));
// Install the replacement while the disconnect transaction is still open.
WriteMinedCommitment(evoDb, qc_b, mined_hash_b, mined_height);
dbTx->Commit();
}
ExpectQuorumMerkleRoot(empty_block, &pindex_mined, qblockman, qc_b);
}
BOOST_AUTO_TEST_SUITE_END()