forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_oracle_startup_tests.cpp
More file actions
89 lines (77 loc) · 4.04 KB
/
Copy pathdigidollar_oracle_startup_tests.cpp
File metadata and controls
89 lines (77 loc) · 4.04 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
// Copyright (c) 2026 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
// Consensus-safety tests for the DigiDollar startup oracle price-cache scan
// (OracleBundleManager::LoadPricesFromChain / ShouldLoadStartupOraclePriceForBlock).
//
// The per-block startup gate was changed to evaluate the BIP9 DigiDollar
// activation predicate through the SHARED, memoized versionbits cache
// (chainman.m_versionbitscache) instead of allocating a throwaway
// VersionBitsCache on every one of the up-to ~172,800 iterations. That is a
// pure performance change: it MUST compute the identical activation boolean, so
// it can neither load a price from a block that would previously have been
// skipped (a bypass) nor skip a block that would previously have been loaded (a
// consensus divergence). These tests pin exactly that invariant.
//
// End-to-end loader correctness (a real MuSig2 v0x03 coinbase bundle -> price
// cache) is already covered by rh66_startup_oracle_price_loading_tests in
// rh61_coinbase_price_cache_poisoning_tests.cpp; the exhaustive BIP9 State()
// machine is covered by versionbits_tests.cpp. Here we prove only that the
// startup gate tracks that predicate exactly, on a real connected chain.
#include <chain.h>
#include <consensus/params.h>
#include <digidollar/digidollar.h>
#include <oracle/bundle_manager.h>
#include <sync.h>
#include <validation.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(digidollar_oracle_startup_tests, TestChain100Setup)
// The two DigiDollar::IsDigiDollarEnabled overloads — the shared-cache one
// (const ChainstateManager&) the fix switches TO, and the throwaway-cache one
// (const Consensus::Params&) it switches AWAY from — must return the SAME
// activation boolean for every block on a real chain. Any divergence here would
// be a consensus divergence hiding behind a "performance" change.
BOOST_AUTO_TEST_CASE(startup_predicate_overloads_agree_on_real_chain)
{
// Extend the pre-mined 100-block regtest chain so the scan spans a
// substantial run of genuinely-connected blocks.
mineBlocks(600);
const Consensus::Params& consensus = m_node.chainman->GetConsensus();
LOCK(cs_main);
const CChain& chain = m_node.chainman->ActiveChain();
BOOST_REQUIRE(chain.Height() > 200);
for (int h = 0; h <= chain.Height(); ++h) {
const CBlockIndex* index = chain[h];
BOOST_REQUIRE(index != nullptr);
const bool shared = DigiDollar::IsDigiDollarEnabled(index, *m_node.chainman);
const bool throwaway = DigiDollar::IsDigiDollarEnabled(index, consensus);
BOOST_CHECK_MESSAGE(shared == throwaway,
"IsDigiDollarEnabled overloads diverged at height " << h
<< " (shared=" << shared << " throwaway=" << throwaway << ")");
}
}
// The startup per-block gate must equal the BIP9 activation predicate applied to
// the block's parent — exactly the predicate ConnectBlock uses. Proving equality
// proves there is NO bypass (it never accepts a price from an un-activated block)
// and NO over-skip (it never drops an activated block's price).
BOOST_AUTO_TEST_CASE(should_load_startup_matches_bip9_predicate)
{
mineBlocks(600);
LOCK(cs_main);
const CChain& chain = m_node.chainman->ActiveChain();
BOOST_REQUIRE(chain.Height() > 200);
for (int h = 1; h <= chain.Height(); ++h) {
const CBlockIndex* index = chain[h];
BOOST_REQUIRE(index != nullptr);
BOOST_REQUIRE(index->pprev != nullptr);
const bool gate = OracleBundleManager::ShouldLoadStartupOraclePriceForBlock(
h, index, *m_node.chainman);
const bool predicate = DigiDollar::IsDigiDollarEnabled(index->pprev, *m_node.chainman);
BOOST_CHECK_MESSAGE(gate == predicate,
"ShouldLoadStartupOraclePriceForBlock diverged from the BIP9 predicate at height "
<< h << " (gate=" << gate << " predicate=" << predicate << ")");
}
}
BOOST_AUTO_TEST_SUITE_END()