]> Git Repo - VerusCoin.git/commitdiff
Add method for fetching the next activation height after a given block height
authorJack Grigg <[email protected]>
Fri, 2 Feb 2018 15:13:48 +0000 (15:13 +0000)
committerJack Grigg <[email protected]>
Sun, 4 Feb 2018 22:58:42 +0000 (22:58 +0000)
src/consensus/upgrades.cpp
src/consensus/upgrades.h
src/gtest/test_upgrades.cpp

index f06e7063cc8790b014dd1c0f202f3ea815121ec8..42089bbb16f676ec91c89b848164a1e4ee3fd8c4 100644 (file)
@@ -102,3 +102,21 @@ bool IsActivationHeightForAnyUpgrade(
 
     return false;
 }
+
+boost::optional<int> NextActivationHeight(
+    int nHeight,
+    const Consensus::Params& params)
+{
+    if (nHeight < 0) {
+        return boost::none;
+    }
+
+    // Don't count Sprout as an activation height
+    for (auto idx = Consensus::BASE_SPROUT + 1; idx < Consensus::MAX_NETWORK_UPGRADES; idx++) {
+        if (NetworkUpgradeState(nHeight, params, Consensus::UpgradeIndex(idx)) == UPGRADE_PENDING) {
+            return params.vUpgrades[idx].nActivationHeight;
+        }
+    }
+
+    return boost::none;
+}
index c62655530826fb3f6f98f1b3b34c79f7d41437f9..0c5462c2e41b78c713be86e67c1b4848e0804b7f 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "consensus/params.h"
 
+#include <boost/optional.hpp>
+
 enum UpgradeState {
     UPGRADE_DISABLED,
     UPGRADE_PENDING,
@@ -74,4 +76,12 @@ bool IsActivationHeightForAnyUpgrade(
     int nHeight,
     const Consensus::Params& params);
 
+/**
+ * Returns the activation height for the next upgrade after the given block height,
+ * or boost::none if there are no more known upgrades.
+ */
+boost::optional<int> NextActivationHeight(
+    int nHeight,
+    const Consensus::Params& params);
+
 #endif // ZCASH_CONSENSUS_UPGRADES_H
index 3312e05693e4f358d9ed1d01b0dc0439d30de354..1066a28d0158ff55d4fb09bf9ff872494885467d 100644 (file)
@@ -3,6 +3,8 @@
 #include "chainparams.h"
 #include "consensus/upgrades.h"
 
+#include <boost/optional.hpp>
+
 class UpgradesTest : public ::testing::Test {
 protected:
     virtual void SetUp() {
@@ -140,3 +142,32 @@ TEST_F(UpgradesTest, IsActivationHeightForAnyUpgrade) {
     EXPECT_FALSE(IsActivationHeightForAnyUpgrade(nActivationHeight + 1, params));
     EXPECT_FALSE(IsActivationHeightForAnyUpgrade(1000000, params));
 }
+
+TEST_F(UpgradesTest, NextActivationHeight) {
+    SelectParams(CBaseChainParams::REGTEST);
+    const Consensus::Params& params = Params().GetConsensus();
+
+    // Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT
+    EXPECT_EQ(NextActivationHeight(-1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(0, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(1000000, params), boost::none);
+
+    UpdateNetworkUpgradeParameters(Consensus::UPGRADE_TESTDUMMY, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
+
+    EXPECT_EQ(NextActivationHeight(-1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(0, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(1000000, params), boost::none);
+
+    int nActivationHeight = 100;
+    UpdateNetworkUpgradeParameters(Consensus::UPGRADE_TESTDUMMY, nActivationHeight);
+
+    EXPECT_EQ(NextActivationHeight(-1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(0, params), nActivationHeight);
+    EXPECT_EQ(NextActivationHeight(1, params), nActivationHeight);
+    EXPECT_EQ(NextActivationHeight(nActivationHeight - 1, params), nActivationHeight);
+    EXPECT_EQ(NextActivationHeight(nActivationHeight, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(nActivationHeight + 1, params), boost::none);
+    EXPECT_EQ(NextActivationHeight(1000000, params), boost::none);
+}
This page took 0.029417 seconds and 4 git commands to generate.