]> Git Repo - VerusCoin.git/commitdiff
Improve network height estimation
authorJack Grigg <[email protected]>
Fri, 28 Jul 2017 10:50:07 +0000 (10:50 +0000)
committerJack Grigg <[email protected]>
Fri, 28 Jul 2017 10:50:07 +0000 (10:50 +0000)
src/gtest/test_metrics.cpp
src/metrics.cpp
src/metrics.h

index 8f313b5d2403ad513e5fe2dfebb9afa530ac399b..143fe46d6012ede26e4f38700bc257bf98beb67e 100644 (file)
@@ -96,40 +96,40 @@ TEST(Metrics, GetLocalSolPS) {
 TEST(Metrics, EstimateNetHeightInner) {
     // Ensure that the (rounded) current height is returned if the tip is current
     SetMockTime(15000);
-    EXPECT_EQ(100, EstimateNetHeightInner(100, 14100, 50, 7500, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(100, 14100, 50, 7500, 0, 150));
     SetMockTime(15150);
-    EXPECT_EQ(100, EstimateNetHeightInner(101, 14250, 50, 7500, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(101, 14250, 50, 7500, 0, 150));
 
     // Ensure that correct estimates are returned if the tip is in the past
     SetMockTime(15300); // Tip is 2 blocks behind
-    EXPECT_EQ(100, EstimateNetHeightInner(100, 14100, 50, 7500, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(100, 14100, 50, 7500, 0, 150));
     SetMockTime(15900); // Tip is 6 blocks behind
-    EXPECT_EQ(110, EstimateNetHeightInner(100, 14100, 50, 7500, 150));
+    EXPECT_EQ(110, EstimateNetHeightInner(100, 14100, 50, 7500, 0, 150));
 
     // Check estimates during resync
     SetMockTime(15000);
-    EXPECT_EQ(100, EstimateNetHeightInner( 0,     0, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner( 7,   600, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner( 8,   600, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(10,   750, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(11,   900, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(20,  2100, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(49,  6450, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(50,  6600, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(51,  6750, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(55,  7350, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(56,  7500, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(57,  7650, 50, 7500, 150));
-    EXPECT_EQ(100, EstimateNetHeightInner(75, 10350, 50, 7500, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner( 0,     0, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner( 7,   600, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner( 8,   600, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(10,   750, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(11,   900, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(20,  2100, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(49,  6450, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(50,  6600, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(51,  6750, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(55,  7350, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(56,  7500, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(57,  7650, 50, 7500, 0, 150));
+    EXPECT_EQ(100, EstimateNetHeightInner(75, 10350, 50, 7500, 0, 150));
 
     // More complex calculations:
     SetMockTime(20000);
     // - Checkpoint spacing: 200
     //   -> Average spacing: 175
     //   -> estimated height: 127 -> 130
-    EXPECT_EQ(130, EstimateNetHeightInner(100, 14100, 50, 5250, 150));
+    EXPECT_EQ(130, EstimateNetHeightInner(100, 14100, 50, 5250, 0, 150));
     // - Checkpoint spacing: 50
     //   -> Average spacing: 100
     //   -> estimated height: 153 -> 150
-    EXPECT_EQ(150, EstimateNetHeightInner(100, 14100, 50, 12000, 150));
+    EXPECT_EQ(150, EstimateNetHeightInner(100, 14100, 50, 12000, 0, 150));
 }
index b10e946304a743618b03447d66197c0c31cbc608..1bac2c9c0a7c730d0e77d81936e6f6a56e570cdf 100644 (file)
@@ -106,12 +106,17 @@ double GetLocalSolPS()
 
 int EstimateNetHeightInner(int height, int64_t tipmediantime,
                            int heightLastCheckpoint, int64_t timeLastCheckpoint,
-                           int64_t targetSpacing)
+                           int64_t genesisTime, int64_t targetSpacing)
 {
     // We average the target spacing with the observed spacing to the last
-    // checkpoint, and use that to estimate the current network height.
-    int medianHeight = height - CBlockIndex::nMedianTimeSpan / 2;
-    double checkpointSpacing = (double (tipmediantime - timeLastCheckpoint)) / (medianHeight - heightLastCheckpoint);
+    // checkpoint (either from below or above depending on the current height),
+    // and use that to estimate the current network height.
+    int medianHeight = height > CBlockIndex::nMedianTimeSpan ?
+            height - (1 + ((CBlockIndex::nMedianTimeSpan - 1) / 2)) :
+            height / 2;
+    double checkpointSpacing = medianHeight > heightLastCheckpoint ?
+            (double (tipmediantime - timeLastCheckpoint)) / (medianHeight - heightLastCheckpoint) :
+            (double (timeLastCheckpoint - genesisTime)) / heightLastCheckpoint;
     double averageSpacing = (targetSpacing + checkpointSpacing) / 2;
     int netheight = medianHeight + ((GetTime() - tipmediantime) / averageSpacing);
     // Round to nearest ten to reduce noise
@@ -125,6 +130,7 @@ int EstimateNetHeight(int height, int64_t tipmediantime, CChainParams chainParam
         height, tipmediantime,
         Checkpoints::GetTotalBlocksEstimate(checkpointData),
         checkpointData.nTimeLastCheckpoint,
+        chainParams.GenesisBlock().nTime,
         chainParams.GetConsensus().nPowTargetSpacing);
 }
 
index 7f7efdea6605d634135b19618eed2c715f282a7b..2d60d30ca6a066ec006c0acc44540a0e0ce5d4ba 100644 (file)
@@ -65,7 +65,7 @@ void MarkStartTime();
 double GetLocalSolPS();
 int EstimateNetHeightInner(int height, int64_t tipmediantime,
                            int heightLastCheckpoint, int64_t timeLastCheckpoint,
-                           int64_t targetSpacing);
+                           int64_t genesisTime, int64_t targetSpacing);
 
 void TriggerRefresh();
 
This page took 0.029449 seconds and 4 git commands to generate.