PIVX Core  5.6.99
P2P Digital Currency
upgrades_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The Zcash Core developers
2 // Copyright (c) 2020 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "chainparams.h"
7 #include "consensus/upgrades.h"
8 #include "optional.h"
9 #include "test/test_pivx.h"
10 
11 #include <boost/test/unit_test.hpp>
12 
13 struct UpgradesTest : public TestingSetup
14 {
17  {
19  // Save activation heights in DefaultActivation and set all upgrades to inactive.
20  for (uint32_t idx = Consensus::BASE_NETWORK + 1; idx < Consensus::MAX_NETWORK_UPGRADES; idx++) {
23  }
24  }
26  {
27  // Revert to default
28  for (uint32_t idx = Consensus::BASE_NETWORK + 1; idx < Consensus::MAX_NETWORK_UPGRADES; idx++) {
30  }
32  }
33 };
34 
35 BOOST_FIXTURE_TEST_SUITE(network_upgrades_tests, UpgradesTest)
36 
37 BOOST_AUTO_TEST_CASE(networkUpgradeStateTest)
38 {
39  const Consensus::Params& params = Params().GetConsensus();
40 
41  // Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
48 
50 
57 
58  int nActivationHeight = 100;
60 
65  NetworkUpgradeState(nActivationHeight - 1, params, Consensus::UPGRADE_TESTDUMMY),
68  NetworkUpgradeState(nActivationHeight, params, Consensus::UPGRADE_TESTDUMMY),
73 }
74 
75 BOOST_AUTO_TEST_CASE(IsActivationHeightTest)
76 {
77  const Consensus::Params& params = Params().GetConsensus();
78 
79  // Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
84 
86 
91 
92  int nActivationHeight = 100;
94 
98  BOOST_CHECK(!IsActivationHeight(nActivationHeight - 1, params, Consensus::UPGRADE_TESTDUMMY));
99  BOOST_CHECK(IsActivationHeight(nActivationHeight, params, Consensus::UPGRADE_TESTDUMMY));
100  BOOST_CHECK(!IsActivationHeight(nActivationHeight + 1, params, Consensus::UPGRADE_TESTDUMMY));
102 }
103 
104 BOOST_AUTO_TEST_CASE(IsActivationHeightForAnyUpgradeTest)
105 {
106  const Consensus::Params& params = Params().GetConsensus();
107 
108  // Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
112  BOOST_CHECK(!IsActivationHeightForAnyUpgrade(1000000, params));
113 
115 
119  BOOST_CHECK(!IsActivationHeightForAnyUpgrade(1000000, params));
120 
121  int nActivationHeight = 100;
123 
127  BOOST_CHECK(!IsActivationHeightForAnyUpgrade(nActivationHeight - 1, params));
128  BOOST_CHECK(IsActivationHeightForAnyUpgrade(nActivationHeight, params));
129  BOOST_CHECK(!IsActivationHeightForAnyUpgrade(nActivationHeight + 1, params));
130  BOOST_CHECK(!IsActivationHeightForAnyUpgrade(1000000, params));
131 }
132 
133 BOOST_AUTO_TEST_CASE(NextActivationHeightTest)
134 {
135  const Consensus::Params& params = Params().GetConsensus();
136 
137  // Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
138  BOOST_CHECK(NextActivationHeight(-1, params) == nullopt);
139  BOOST_CHECK(NextActivationHeight(0, params) == nullopt);
140  BOOST_CHECK(NextActivationHeight(1, params) == nullopt);
141  BOOST_CHECK(NextActivationHeight(1000000, params) == nullopt);
142 
144 
145  BOOST_CHECK(NextActivationHeight(-1, params) == nullopt);
146  BOOST_CHECK(NextActivationHeight(0, params) == nullopt);
147  BOOST_CHECK(NextActivationHeight(1, params) == nullopt);
148  BOOST_CHECK(NextActivationHeight(1000000, params) == nullopt);
149 
150  int nActivationHeight = 100;
152 
153  BOOST_CHECK(NextActivationHeight(-1, params) == nullopt);
154  BOOST_CHECK(NextActivationHeight(0, params) == nActivationHeight);
155  BOOST_CHECK(NextActivationHeight(1, params) == nActivationHeight);
156  BOOST_CHECK(NextActivationHeight(nActivationHeight - 1, params) == nActivationHeight);
157  BOOST_CHECK(NextActivationHeight(nActivationHeight, params) == nullopt);
158  BOOST_CHECK(NextActivationHeight(nActivationHeight + 1, params) == nullopt);
159  BOOST_CHECK(NextActivationHeight(1000000, params) == nullopt);
160 }
161 
void UpdateNetworkUpgradeParameters(Consensus::UpgradeIndex idx, int nActivationHeight)
Allows modifying the network upgrade regtest parameters.
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
const CChainParams & Params()
Return the currently selected parameters.
static const std::string REGTEST
static const std::string MAIN
Chain name strings.
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:72
BOOST_AUTO_TEST_SUITE_END()
UpgradeIndex
Index into Params.vUpgrades and NetworkUpgradeInfo.
Definition: params.h:26
@ UPGRADE_TESTDUMMY
Definition: params.h:42
@ BASE_NETWORK
Definition: params.h:27
@ MAX_NETWORK_UPGRADES
Definition: params.h:44
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static constexpr int ALWAYS_ACTIVE
Special value for nActivationHeight indicating that the upgrade is always active.
Definition: params.h:67
int nActivationHeight
Height of the first block for which the new consensus rules will be active.
Definition: params.h:56
static constexpr int NO_ACTIVATION_HEIGHT
Special value for nActivationHeight indicating that the upgrade will never activate.
Definition: params.h:74
Parameters that influence chain consensus.
Definition: params.h:171
NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES]
Definition: params.h:213
int DefaultActivation[Consensus::MAX_NETWORK_UPGRADES]
UpgradeState NetworkUpgradeState(int nHeight, const Consensus::Params &params, Consensus::UpgradeIndex idx)
Checks the state of a given network upgrade based on block height.
Definition: upgrades.cpp:82
bool IsActivationHeightForAnyUpgrade(int nHeight, const Consensus::Params &params)
Returns true if the given block height is the activation height for any upgrade.
Definition: upgrades.cpp:139
bool IsActivationHeight(int nHeight, const Consensus::Params &params, Consensus::UpgradeIndex idx)
Returns true if the given block height is the activation height for the given upgrade.
Definition: upgrades.cpp:124
Optional< int > NextActivationHeight(int nHeight, const Consensus::Params &params)
Returns the activation height for the next upgrade after the given block height, or nullopt if there ...
Definition: upgrades.cpp:170
@ UPGRADE_PENDING
Definition: upgrades.h:14
@ UPGRADE_DISABLED
Definition: upgrades.h:13
@ UPGRADE_ACTIVE
Definition: upgrades.h:15
BOOST_AUTO_TEST_CASE(networkUpgradeStateTest)