Extract a helper method for finding the next epoch
This commit is contained in:
parent
ebf4c0671e
commit
07d3f947ec
|
@ -114,20 +114,28 @@ bool IsActivationHeightForAnyUpgrade(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::optional<int> NextActivationHeight(
|
boost::optional<int> NextEpoch(int nHeight, const Consensus::Params& params) {
|
||||||
int nHeight,
|
|
||||||
const Consensus::Params& params)
|
|
||||||
{
|
|
||||||
if (nHeight < 0) {
|
if (nHeight < 0) {
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't count Sprout as an activation height
|
// Sprout is never pending
|
||||||
for (auto idx = Consensus::BASE_SPROUT + 1; idx < Consensus::MAX_NETWORK_UPGRADES; idx++) {
|
for (auto idx = Consensus::BASE_SPROUT + 1; idx < Consensus::MAX_NETWORK_UPGRADES; idx++) {
|
||||||
if (NetworkUpgradeState(nHeight, params, Consensus::UpgradeIndex(idx)) == UPGRADE_PENDING) {
|
if (NetworkUpgradeState(nHeight, params, Consensus::UpgradeIndex(idx)) == UPGRADE_PENDING) {
|
||||||
return params.vUpgrades[idx].nActivationHeight;
|
return idx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boost::optional<int> NextActivationHeight(
|
||||||
|
int nHeight,
|
||||||
|
const Consensus::Params& params)
|
||||||
|
{
|
||||||
|
auto idx = NextEpoch(nHeight, params);
|
||||||
|
if (idx) {
|
||||||
|
return params.vUpgrades[idx.get()].nActivationHeight;
|
||||||
|
}
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
|
|
@ -79,6 +79,12 @@ bool IsActivationHeightForAnyUpgrade(
|
||||||
int nHeight,
|
int nHeight,
|
||||||
const Consensus::Params& params);
|
const Consensus::Params& params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the index of the next upgrade after the given block height, or
|
||||||
|
* boost::none if there are no more known upgrades.
|
||||||
|
*/
|
||||||
|
boost::optional<int> NextEpoch(int nHeight, const Consensus::Params& params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the activation height for the next upgrade after the given block height,
|
* Returns the activation height for the next upgrade after the given block height,
|
||||||
* or boost::none if there are no more known upgrades.
|
* or boost::none if there are no more known upgrades.
|
||||||
|
|
|
@ -143,6 +143,35 @@ TEST_F(UpgradesTest, IsActivationHeightForAnyUpgrade) {
|
||||||
EXPECT_FALSE(IsActivationHeightForAnyUpgrade(1000000, params));
|
EXPECT_FALSE(IsActivationHeightForAnyUpgrade(1000000, params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UpgradesTest, NextEpoch) {
|
||||||
|
SelectParams(CBaseChainParams::REGTEST);
|
||||||
|
const Consensus::Params& params = Params().GetConsensus();
|
||||||
|
|
||||||
|
// Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
|
||||||
|
EXPECT_EQ(NextEpoch(-1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(0, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(1000000, params), boost::none);
|
||||||
|
|
||||||
|
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_TESTDUMMY, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
|
||||||
|
|
||||||
|
EXPECT_EQ(NextEpoch(-1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(0, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(1000000, params), boost::none);
|
||||||
|
|
||||||
|
int nActivationHeight = 100;
|
||||||
|
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_TESTDUMMY, nActivationHeight);
|
||||||
|
|
||||||
|
EXPECT_EQ(NextEpoch(-1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(0, params), static_cast<int>(Consensus::UPGRADE_TESTDUMMY));
|
||||||
|
EXPECT_EQ(NextEpoch(1, params), static_cast<int>(Consensus::UPGRADE_TESTDUMMY));
|
||||||
|
EXPECT_EQ(NextEpoch(nActivationHeight - 1, params), static_cast<int>(Consensus::UPGRADE_TESTDUMMY));
|
||||||
|
EXPECT_EQ(NextEpoch(nActivationHeight, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(nActivationHeight + 1, params), boost::none);
|
||||||
|
EXPECT_EQ(NextEpoch(1000000, params), boost::none);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(UpgradesTest, NextActivationHeight) {
|
TEST_F(UpgradesTest, NextActivationHeight) {
|
||||||
SelectParams(CBaseChainParams::REGTEST);
|
SelectParams(CBaseChainParams::REGTEST);
|
||||||
const Consensus::Params& params = Params().GetConsensus();
|
const Consensus::Params& params = Params().GetConsensus();
|
||||||
|
|
Loading…
Reference in New Issue