Repository navigation
Expand file tree
/
Copy pathinterfaces_tests.cpp
More file actions
592 lines (525 loc) · 29.5 KB
/
Copy pathinterfaces_tests.cpp
File metadata and controls
592 lines (525 loc) · 29.5 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright (c) 2020-2022 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 <bls/bls.h>
#include <chainlock/chainlock.h>
#include <chainparams.h>
#include <consensus/validation.h>
#include <evo/evochainstate.h>
#include <evo/evodb.h>
#include <evo/providertx.h>
#include <instantsend/instantsend.h>
#include <instantsend/lock.h>
#include <interfaces/chain.h>
#include <interfaces/node.h>
#include <llmq/commitment.h>
#include <llmq/context.h>
#include <llmq/params.h>
#include <node/protocol_version.h>
#include <script/solver.h>
#include <spork.h>
#include <streams.h>
#include <test/util/llmq_tests.h>
#include <test/util/setup_common.h>
#include <tinyformat.h>
#include <util/std23.h>
#include <util/strencodings.h>
#include <validation.h>
#include <algorithm>
#include <chrono>
#include <future>
#include <map>
#include <string>
#include <string_view>
#include <thread>
#include <boost/test/unit_test.hpp>
using interfaces::FoundBlock;
BOOST_FIXTURE_TEST_SUITE(interfaces_tests, TestChain100Setup)
BOOST_AUTO_TEST_CASE(findBlock)
{
LOCK(Assert(m_node.chainman)->GetMutex());
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
uint256 hash;
BOOST_CHECK(chain->findBlock(active[10]->GetBlockHash(), FoundBlock().hash(hash)));
BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
int height = -1;
BOOST_CHECK(chain->findBlock(active[20]->GetBlockHash(), FoundBlock().height(height)));
BOOST_CHECK_EQUAL(height, active[20]->nHeight);
CBlock data;
BOOST_CHECK(chain->findBlock(active[30]->GetBlockHash(), FoundBlock().data(data)));
BOOST_CHECK_EQUAL(data.GetHash(), active[30]->GetBlockHash());
int64_t time = -1;
BOOST_CHECK(chain->findBlock(active[40]->GetBlockHash(), FoundBlock().time(time)));
BOOST_CHECK_EQUAL(time, active[40]->GetBlockTime());
int64_t max_time = -1;
BOOST_CHECK(chain->findBlock(active[50]->GetBlockHash(), FoundBlock().maxTime(max_time)));
BOOST_CHECK_EQUAL(max_time, active[50]->GetBlockTimeMax());
int64_t mtp_time = -1;
BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), FoundBlock().mtpTime(mtp_time)));
BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast());
bool cur_active{false}, next_active{false};
uint256 next_hash;
BOOST_CHECK_EQUAL(active.Height(), 100);
BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
BOOST_CHECK(cur_active);
BOOST_CHECK(next_active);
BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());
cur_active = next_active = false;
BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
BOOST_CHECK(cur_active);
BOOST_CHECK(!next_active);
BOOST_CHECK(!chain->findBlock({}, FoundBlock()));
}
BOOST_AUTO_TEST_CASE(providerTxCapabilities)
{
auto node{interfaces::MakeNode(m_node)};
const auto capabilities{node->evo().getProviderTxCapabilities()};
const uint16_t expected_version{
WITH_LOCK(::cs_main, return DeploymentToProtxVersion(Assert(m_node.chainman)->ActiveChain().Tip(),
*Assert(m_node.chainman)))};
BOOST_CHECK_EQUAL(capabilities.version, expected_version);
BOOST_CHECK_EQUAL(capabilities.extended_addresses, expected_version >= ProTxVersion::ExtAddr);
BOOST_CHECK(!capabilities.ipv4_optional); // Not active on regtest by default
}
struct IPv4OptionalChainSetup : public TestChain100Setup {
IPv4OptionalChainSetup()
: TestChain100Setup{ChainType::REGTEST, {"-testactivationheight=ipv4_optional@1"}} {}
};
BOOST_FIXTURE_TEST_CASE(providerTxCapabilitiesIPv4Optional, IPv4OptionalChainSetup)
{
BOOST_CHECK(interfaces::MakeNode(m_node)->evo().getProviderTxCapabilities().ipv4_optional);
}
BOOST_AUTO_TEST_CASE(providerNetInfoValidationDoesNotWaitForChain)
{
// Dialogs validate endpoints on every keystroke, so that must not wait for cs_main
auto node{interfaces::MakeNode(m_node)};
const interfaces::ProviderNetInfo net_info{.core_p2p = {"1.1.1.1:9998"},
.platform_p2p = std::vector<std::string>{"1.1.1.1:22200"},
.platform_https = std::vector<std::string>{"1.1.1.1:443"}};
std::promise<void> locked;
std::promise<void> release;
bool lock_timed_out{false};
std::thread holder{[&] {
LOCK(::cs_main);
locked.set_value();
lock_timed_out = release.get_future().wait_for(std::chrono::seconds{10}) == std::future_status::timeout;
}};
locked.get_future().wait();
const auto error{node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false)};
release.set_value();
holder.join();
BOOST_CHECK(!lock_timed_out);
BOOST_CHECK(!error);
}
BOOST_AUTO_TEST_CASE(providerNetInfoValidation)
{
auto node{interfaces::MakeNode(m_node)};
interfaces::ProviderNetInfo net_info{
.core_p2p = {"1.1.1.1:9998"},
.platform_p2p = std::vector<std::string>{"1.1.1.2:22200"},
.platform_https = std::vector<std::string>{"server.example.com:443"},
};
// Unless the caller's capabilities say masternodes may operate without IPv4, Platform must use the Core address
auto error{node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false)};
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->reject_reason, "bad-protx-netinfo-addr");
net_info.platform_p2p = std::vector<std::string>{"1.1.1.1:22200"};
net_info.platform_https = std::vector<std::string>{"1.1.1.1:443", "server.example.com:443"};
BOOST_CHECK(!node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false));
net_info.platform_p2p = std::vector<std::string>{"server.example.com:22200"};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->code, interfaces::ProviderTxErrorCode::INVALID_PARAMETER);
BOOST_CHECK_EQUAL(error->message.original,
"Error setting platformP2PAddrs[0] to 'server.example.com:22200' (invalid input)");
net_info.platform_p2p = std::vector<std::string>{"1.1.1.1:9998"};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->message.original, "Error setting platformP2PAddrs[0] to '1.1.1.1:9998' (duplicate)");
net_info.platform_p2p = std::vector<std::string>{"1.1.1.2:22200", "1.1.1.3:22200", "1.1.1.4:22200", "1.1.1.5:22200",
"1.1.1.6:22200"};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->message.original,
"Error setting platformP2PAddrs[4] to '1.1.1.6:22200' (too many entries)");
net_info.platform_p2p = uint16_t{22200};
net_info.platform_https = uint16_t{22201};
BOOST_CHECK(!node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::BasicBLS,
/*ipv4_optional=*/false, /*optional=*/false));
net_info.platform_p2p = std::vector<std::string>{"1.1.1.2:22200"};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::BasicBLS,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK(error->message.original.find("only accepts a bare port number") != std::string::npos);
net_info = {
.core_p2p = {},
.platform_p2p = std::vector<std::string>{"1.1.1.2:22200"},
.platform_https = std::vector<std::string>{"server.example.com:443"},
};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/true);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->reject_reason, "bad-protx-netinfo-empty");
net_info = {
.core_p2p = {"1.1.1.1:9998"},
.platform_p2p = std::monostate{},
.platform_https = std::monostate{},
};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/true);
BOOST_REQUIRE(error);
BOOST_CHECK(error->message.original.find("platformP2PAddrs, cannot be empty if other fields populated") !=
std::string::npos);
BOOST_CHECK(!node->evo().validateProviderNetInfo(net_info, MnType::Regular, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/true));
net_info.platform_p2p = std::vector<std::string>{"1.1.1.2:22200"};
error = node->evo().validateProviderNetInfo(net_info, MnType::Regular, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/true);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->message.original, "Platform endpoints are only valid for an EvoNode");
net_info = {
.core_p2p = {"1.1.1.1:9998"},
.platform_p2p = std::vector<std::string>{"1.1.1.2:22200"},
.platform_https = std::monostate{},
};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::ExtAddr,
/*ipv4_optional=*/false, /*optional=*/true);
BOOST_REQUIRE(error);
BOOST_CHECK(error->message.original.find("platformHTTPSAddrs, cannot be empty if other fields populated") !=
std::string::npos);
net_info = {
.core_p2p = {"1.1.1.1:9998"},
.platform_p2p = uint16_t{22200},
.platform_https = uint16_t{22200},
};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::BasicBLS,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->reject_reason, "bad-protx-platform-dup-ports");
net_info.platform_p2p = uint16_t{MainParams().GetDefaultPort()};
net_info.platform_https = uint16_t{22201};
error = node->evo().validateProviderNetInfo(net_info, MnType::Evo, ProTxVersion::BasicBLS,
/*ipv4_optional=*/false, /*optional=*/false);
BOOST_REQUIRE(error);
BOOST_CHECK_EQUAL(error->reject_reason, "bad-protx-platform-p2p-port");
}
BOOST_AUTO_TEST_CASE(findFirstBlockWithTimeAndHeight)
{
LOCK(Assert(m_node.chainman)->GetMutex());
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
uint256 hash;
int height;
BOOST_CHECK(chain->findFirstBlockWithTimeAndHeight(/* min_time= */ 0, /* min_height= */ 5, FoundBlock().hash(hash).height(height)));
BOOST_CHECK_EQUAL(hash, active[5]->GetBlockHash());
BOOST_CHECK_EQUAL(height, 5);
BOOST_CHECK(!chain->findFirstBlockWithTimeAndHeight(/* min_time= */ active.Tip()->GetBlockTimeMax() + 1, /* min_height= */ 0));
}
BOOST_AUTO_TEST_CASE(findAncestorByHeight)
{
LOCK(Assert(m_node.chainman)->GetMutex());
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
uint256 hash;
BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, FoundBlock().hash(hash)));
BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20));
}
BOOST_AUTO_TEST_CASE(findAncestorByHash)
{
LOCK(Assert(m_node.chainman)->GetMutex());
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
int height = -1;
BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), active[10]->GetBlockHash(), FoundBlock().height(height)));
BOOST_CHECK_EQUAL(height, 10);
BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), active[20]->GetBlockHash()));
}
BOOST_AUTO_TEST_CASE(findCommonAncestor)
{
auto& chain = m_node.chain;
const CChain& active = *WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain());
auto* orig_tip = active.Tip();
for (int i = 0; i < 10; ++i) {
BlockValidationState state;
m_node.chainman->ActiveChainstate().InvalidateBlock(state, active.Tip());
}
BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10);
coinbaseKey.MakeNewKey(true);
for (int i = 0; i < 20; ++i) {
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
}
BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10);
uint256 fork_hash;
int fork_height;
int orig_height;
BOOST_CHECK(chain->findCommonAncestor(orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), FoundBlock().height(fork_height).hash(fork_hash), FoundBlock().height(orig_height)));
BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);
BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);
BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());
uint256 active_hash, orig_hash;
BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));
BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));
BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());
BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());
}
BOOST_AUTO_TEST_CASE(hasBlocks)
{
LOCK(::cs_main);
auto& chain = m_node.chain;
const CChain& active = Assert(m_node.chainman)->ActiveChain();
// Test ranges
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
active[5]->nStatus &= ~BLOCK_HAVE_DATA;
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
active[95]->nStatus &= ~BLOCK_HAVE_DATA;
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
active[50]->nStatus &= ~BLOCK_HAVE_DATA;
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
// Test edge cases
BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 49));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 5, 49));
BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 50));
}
namespace {
using PlatformQuorumStatus = interfaces::LLMQ::PlatformQuorumStatus;
const char* StatusName(PlatformQuorumStatus status)
{
switch (status) {
case PlatformQuorumStatus::FOUND: return "FOUND";
case PlatformQuorumStatus::INVALID: return "INVALID";
case PlatformQuorumStatus::UNAVAILABLE: return "UNAVAILABLE";
}
return "?";
}
interfaces::LLMQ::PlatformQuorumResult CheckPlatformQuorum(interfaces::LLMQ& llmq, std::string_view label,
uint8_t llmq_type, const uint256& quorum_hash,
uint32_t height, PlatformQuorumStatus expected)
{
auto result{llmq.getPlatformQuorum(llmq_type, quorum_hash, height)};
BOOST_CHECK_MESSAGE(result.status == expected, strprintf("%s (height %u): got %s, expected %s", label, height,
StatusName(result.status), StatusName(expected)));
BOOST_CHECK_MESSAGE(result.public_key.empty() == (result.status != PlatformQuorumStatus::FOUND),
strprintf("%s: only FOUND carries a key", label));
return result;
}
// Records a ChainLock at `height` on `block`, or on a block this node does not know if null.
void RecordChainLock(chainlock::Chainlocks& chainlocks, int height, const CBlockIndex* block)
{
const auto clsig{llmq::testutils::CreateChainLock(height, block ? block->GetBlockHash() : InsecureRand256())};
BOOST_REQUIRE(chainlocks.UpdateBestChainlock(::SerializeHash(clsig), clsig, block));
}
// Adds a header-only block on `prev`, unique and otherwise copied from `like`.
const CBlockIndex* AddHeader(ChainstateManager& chainman, const CBlockIndex& prev, const CBlockIndex& like)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
CBlockHeader header{like.GetBlockHeader()};
header.hashPrevBlock = prev.GetBlockHash();
++header.nNonce;
return chainman.m_blockman.AddToBlockIndex(header, header.GetHash(), chainman.m_best_header);
}
} // namespace
// getPlatformQuorum answers whether a quorum could have signed Platform state carrying a given Core
// chain-locked height. Mined commitments are installed directly in EvoDB (the same shortcut
// evo_cbtx_tests uses) since a real DKG cannot run in a unit test. After the first checks a
// ChainLock on the tip covers the whole active chain, and the staleness bound equals the tip's.
BOOST_AUTO_TEST_CASE(getPlatformQuorum)
{
auto node{interfaces::MakeNode(m_node)};
auto& llmq{node->llmq()};
const auto platform_type{Params().GetConsensus().llmqTypePlatform};
BOOST_REQUIRE(platform_type == Consensus::LLMQType::LLMQ_TEST_PLATFORM);
const auto& params{llmq::testutils::GetLLMQParams(platform_type)};
const uint8_t type{std23::to_underlying(platform_type)};
// Four commitments are eligible at any height (signingActiveQuorumCount plus a slack of two) and
// state older than 4 * 24 = 96 blocks below the ChainLock is stale.
BOOST_REQUIRE_EQUAL(params.signingActiveQuorumCount, 2);
BOOST_REQUIRE_EQUAL(params.dkgInterval, 24);
const int tip{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Height())};
BOOST_REQUIRE_EQUAL(tip, 100);
BOOST_REQUIRE_EQUAL(llmq::PlatformQuorumMinSignedHeight(params, tip), 4);
// Quorum bases mined one block later: 2 (mined at 3), 10, 20, 30, 40, 50 (mined at 51).
std::map<int, llmq::CFinalCommitment> qcs;
llmq::CFinalCommitment other_type_qc;
{
LOCK(::cs_main);
const CChain& active{m_node.chainman->ActiveChain()};
auto db_tx{m_node.chainman->ActiveEvo().evodb->BeginTransaction()};
for (const int base : {2, 10, 20, 30, 40, 50}) {
auto qc{llmq::testutils::CreateValidCommitment(params, active[base]->GetBlockHash())};
llmq::testutils::WriteMinedCommitment(*m_node.chainman->ActiveEvo().evodb, qc, active[base + 1]->GetBlockHash(),
/*mined_height=*/base + 1, /*quorum_height=*/base);
qcs.emplace(base, qc);
}
other_type_qc = llmq::testutils::CreateValidCommitment(llmq::testutils::GetLLMQParams(Consensus::LLMQType::LLMQ_TEST),
active[60]->GetBlockHash());
llmq::testutils::WriteMinedCommitment(*m_node.chainman->ActiveEvo().evodb, other_type_qc, active[61]->GetBlockHash(),
/*mined_height=*/61, /*quorum_height=*/60);
db_tx->Commit();
}
const auto check = [&](std::string_view label, uint8_t llmq_type, const uint256& hash, uint32_t height,
PlatformQuorumStatus expected) {
return CheckPlatformQuorum(llmq, label, llmq_type, hash, height, expected);
};
using enum PlatformQuorumStatus;
// The newest quorum, with the commitment's 48-byte basic-scheme key. The hash is in Core's
// internal byte order.
const auto found{check("newest", type, qcs.at(50).quorumHash, tip, FOUND)};
BOOST_CHECK(found.public_key == qcs.at(50).quorumPublicKey.ToByteVector());
BOOST_CHECK_EQUAL(found.public_key.size(), 48U);
CBLSPublicKey roundtrip;
roundtrip.SetBytes(found.public_key);
BOOST_CHECK(roundtrip.IsValid());
BOOST_CHECK(roundtrip == qcs.at(50).quorumPublicKey);
// Without a ChainLock this node's chain is not final, so a missing quorum is not evidence yet.
check("unknown quorum, no ChainLock", type, InsecureRand256(), tip, UNAVAILABLE);
// Base 2 is eligible at heights 3 and 4; only 4 is within the staleness bound the tip gives.
// A stale height is no evidence against the quorum, with or without a ChainLock.
check("at the staleness bound", type, qcs.at(2).quorumHash, 4, FOUND);
check("below the staleness bound, no ChainLock", type, qcs.at(2).quorumHash, 3, UNAVAILABLE);
const auto* tip_block{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip())};
RecordChainLock(*m_node.chainlocks, tip, tip_block);
check("below the staleness bound", type, qcs.at(2).quorumHash, 3, UNAVAILABLE);
uint256 reversed{qcs.at(50).quorumHash};
std::reverse(reversed.begin(), reversed.end());
check("byte-reversed hash", type, reversed, tip, INVALID);
// Window edges at the tip, newest first: 50, 40, 30, 20 are eligible and 10 is fifth.
check("position 3", type, qcs.at(30).quorumHash, tip, FOUND);
check("position 4", type, qcs.at(20).quorumHash, tip, FOUND);
// Base 10 is fourth at height 50. When Platform's Core height jumps from 50 to the tip, its
// validator set signs the first block carrying the tip's height, so fifth proves nothing.
check("position 4 before a Core-height jump", type, qcs.at(10).quorumHash, 50, FOUND);
check("position 5 after a Core-height jump", type, qcs.at(10).quorumHash, tip, UNAVAILABLE);
// A quorum stays valid for state signed while it was in the window.
check("historical", type, qcs.at(10).quorumHash, 15, FOUND);
// Only bases 10 and 2 were mined by height 15, and nothing older exists.
check("unknown quorum, window shorter than four", type, InsecureRand256(), 15, INVALID);
check("height above the tip", type, qcs.at(50).quorumHash, tip + 1, UNAVAILABLE);
check("height beyond int32", type, qcs.at(50).quorumHash, uint32_t{1} << 31, INVALID);
check("non-Platform type", std23::to_underlying(Consensus::LLMQType::LLMQ_TEST), other_type_qc.quorumHash, tip,
INVALID);
check("unknown type", 0xEE, qcs.at(50).quorumHash, tip, INVALID);
check("unknown quorum", type, InsecureRand256(), tip, INVALID);
check("mined above the height", type, qcs.at(50).quorumHash, 50, INVALID);
// A quorum that could sign at a height has its base block on the active chain below it.
uint256 block_45, block_70, off_chain;
{
LOCK(::cs_main);
const CChain& active{m_node.chainman->ActiveChain()};
block_45 = active[45]->GetBlockHash();
block_70 = active[70]->GetBlockHash();
off_chain = AddHeader(*m_node.chainman, *active[50], *active[51])->GetBlockHash();
}
check("block above the height", type, block_70, 50, INVALID);
check("block off the active chain", type, off_chain, tip, INVALID);
check("block below the height, outside the window", type, block_45, tip, UNAVAILABLE);
// ChainLocks stalled at 50 while the tip reached 100: the staleness bound follows the ChainLock,
// so height 3 is fresh.
m_node.chainlocks->ResetChainlock();
const auto* block_50{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain()[50])};
RecordChainLock(*m_node.chainlocks, 50, block_50);
check("stalled ChainLock", type, qcs.at(2).quorumHash, 3, FOUND);
// Above the ChainLock the active chain may be a competing branch that lacks the newest
// commitment, so only a quorum found there is decisive.
check("unknown quorum at the ChainLock", type, InsecureRand256(), 50, INVALID);
check("unknown quorum above the ChainLock", type, InsecureRand256(), 51, UNAVAILABLE);
check("unknown quorum far above the ChainLock", type, InsecureRand256(), 86, UNAVAILABLE);
check("above the ChainLock", type, qcs.at(50).quorumHash, 86, FOUND);
// A higher ChainLock on a block this node does not know covers nothing yet.
RecordChainLock(*m_node.chainlocks, 88, nullptr);
check("unknown quorum below a ChainLock on an unknown block", type, InsecureRand256(), 86, UNAVAILABLE);
// One on a branch forking from the active chain after block 83 covers the active chain to 83.
const CBlockIndex* branch_tip;
{
LOCK(::cs_main);
const CChain& active{m_node.chainman->ActiveChain()};
branch_tip = active[83];
for (int height{84}; height <= 90; ++height) {
branch_tip = AddHeader(*m_node.chainman, *branch_tip, *active[height]);
}
BOOST_REQUIRE(!active.Contains(branch_tip));
}
RecordChainLock(*m_node.chainlocks, 90, branch_tip);
check("unknown quorum above a competing ChainLock's fork", type, InsecureRand256(), 86, UNAVAILABLE);
check("unknown quorum below a competing ChainLock's fork", type, InsecureRand256(), 83, INVALID);
// One on a header built on the tip covers the whole active chain before its block arrives.
const auto* next_header{WITH_LOCK(::cs_main, return AddHeader(*m_node.chainman, *tip_block, *tip_block))};
RecordChainLock(*m_node.chainlocks, tip + 1, next_header);
check("unknown quorum below a ChainLock beyond the tip", type, InsecureRand256(), tip, INVALID);
// A ChainLock on a block not known yet still raises the staleness bound, but only up to the tip.
m_node.chainlocks->ResetChainlock();
RecordChainLock(*m_node.chainlocks, 50, block_50);
RecordChainLock(*m_node.chainlocks, 1000, nullptr);
check("ChainLock ahead of the tip, at the bound", type, qcs.at(2).quorumHash, 4, FOUND);
check("ChainLock ahead of the tip, below the bound", type, qcs.at(2).quorumHash, 3, UNAVAILABLE);
// An index entry whose commitment record is missing is a local fault, not evidence against the quorum.
uint256 block_60;
{
LOCK(::cs_main);
block_60 = m_node.chainman->ActiveChain()[60]->GetBlockHash();
auto db_tx{m_node.chainman->ActiveEvo().evodb->BeginTransaction()};
m_node.chainman->ActiveEvo().evodb->Write(llmq::testutils::BuildInversedHeightKey(platform_type, 61), 60);
db_tx->Commit();
}
check("commitment record missing", type, block_60, tip, UNAVAILABLE);
}
// getInstantSendLock returns the network-serialized islock for a locked txid, and an empty vector
// both for unknown txids and while InstantSend is disabled (spork gate inside the manager).
BOOST_AUTO_TEST_CASE(getInstantSendLock)
{
constexpr const char* REGTEST_SPORK_PRIVKEY{"cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"};
auto node{interfaces::MakeNode(m_node)};
auto& isman{*Assert(m_node.isman)};
auto islock{std::make_shared<instantsend::InstantSendLock>()};
islock->inputs.emplace_back(uint256::ONE, 0);
islock->txid = uint256::TWO;
islock->cycleHash = InsecureRand256();
CBLSSecretKey sk;
sk.MakeNewKey();
islock->sig.Set(sk.Sign(islock->GetRequestId()));
isman.WriteNewISLock(::SerializeHash(*islock), islock, /*minedHeight=*/std::nullopt);
// SPORK_2 defaults to OFF: the stored lock must not leak through the interface.
BOOST_CHECK(node->llmq().getInstantSendLock(islock->txid).empty());
// The fixture builds a bare CSporkManager, so wire up the regtest signer before setting the spork.
BOOST_REQUIRE(m_node.sporkman->SetSporkAddress(Params().SporkAddress()));
BOOST_REQUIRE(m_node.sporkman->SetPrivKey(REGTEST_SPORK_PRIVKEY));
BOOST_REQUIRE(m_node.sporkman->UpdateSpork(SPORK_2_INSTANTSEND_ENABLED, 0).has_value());
DataStream expected{};
expected << *islock;
const auto ret{node->llmq().getInstantSendLock(islock->txid)};
BOOST_REQUIRE(!ret.empty());
BOOST_CHECK_EQUAL(HexStr(ret), HexStr(expected));
// The exported bytes deserialize back into the same lock.
DataStream ds{Span<const uint8_t>{ret}};
instantsend::InstantSendLock roundtrip;
ds >> roundtrip;
BOOST_CHECK_EQUAL(roundtrip.txid, islock->txid);
BOOST_CHECK(roundtrip.inputs == islock->inputs);
BOOST_CHECK_EQUAL(roundtrip.GetRequestId(), islock->GetRequestId());
// Unknown txid.
BOOST_CHECK(node->llmq().getInstantSendLock(uint256::ONE).empty());
}
BOOST_AUTO_TEST_SUITE_END()