]> Git Repo - VerusCoin.git/commitdiff
Add block download progress to metrics UI
authorJack Grigg <[email protected]>
Mon, 26 Jun 2017 23:16:08 +0000 (16:16 -0700)
committerJack Grigg <[email protected]>
Wed, 26 Jul 2017 00:51:12 +0000 (00:51 +0000)
src/gtest/test_metrics.cpp
src/metrics.cpp
src/metrics.h

index c199b323e16cfaa80852ca5bc8904f74e72439be..8e0aa9e02858b52438119ec04d46bf1e1d882f9c 100644 (file)
@@ -92,3 +92,28 @@ TEST(Metrics, GetLocalSolPS) {
     SetMockTime(104);
     EXPECT_EQ(1, GetLocalSolPS());
 }
+
+TEST(Metrics, EstimateNetHeightInner) {
+    // Ensure that the (rounded) current height is returned if the tip is current
+    SetMockTime(15000);
+    EXPECT_EQ(100, EstimateNetHeightInner(100, 14250, 50, 7500, 150));
+    SetMockTime(15150);
+    EXPECT_EQ(100, EstimateNetHeightInner(101, 14400, 50, 7500, 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, 14250, 50, 7500, 150));
+    SetMockTime(15900); // Tip is 6 blocks behind
+    EXPECT_EQ(110, EstimateNetHeightInner(100, 14250, 50, 7500, 150));
+
+    // More complex calculations:
+    SetMockTime(20000);
+    // - Checkpoint spacing: 200
+    //   -> Average spacing: 175
+    //   -> estimated height: 127 -> 130
+    EXPECT_EQ(130, EstimateNetHeightInner(100, 14250, 50, 5250, 150));
+    // - Checkpoint spacing: 50
+    //   -> Average spacing: 100
+    //   -> estimated height: 153 -> 150
+    EXPECT_EQ(150, EstimateNetHeightInner(100, 14250, 50, 12000, 150));
+}
index af8052049dc206c15453a53eeba3f4b09bcee0c6..b10e946304a743618b03447d66197c0c31cbc608 100644 (file)
@@ -5,6 +5,7 @@
 #include "metrics.h"
 
 #include "chainparams.h"
+#include "checkpoints.h"
 #include "main.h"
 #include "ui_interface.h"
 #include "util.h"
@@ -103,6 +104,30 @@ double GetLocalSolPS()
     return miningTimer.rate(solutionTargetChecks);
 }
 
+int EstimateNetHeightInner(int height, int64_t tipmediantime,
+                           int heightLastCheckpoint, int64_t timeLastCheckpoint,
+                           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);
+    double averageSpacing = (targetSpacing + checkpointSpacing) / 2;
+    int netheight = medianHeight + ((GetTime() - tipmediantime) / averageSpacing);
+    // Round to nearest ten to reduce noise
+    return ((netheight + 5) / 10) * 10;
+}
+
+int EstimateNetHeight(int height, int64_t tipmediantime, CChainParams chainParams)
+{
+    auto checkpointData = chainParams.Checkpoints();
+    return EstimateNetHeightInner(
+        height, tipmediantime,
+        Checkpoints::GetTotalBlocksEstimate(checkpointData),
+        checkpointData.nTimeLastCheckpoint,
+        chainParams.GetConsensus().nPowTargetSpacing);
+}
+
 void TriggerRefresh()
 {
     *nNextRefresh = GetTime();
@@ -169,17 +194,25 @@ int printStats(bool mining)
     int lines = 4;
 
     int height;
+    int64_t tipmediantime;
     size_t connections;
     int64_t netsolps;
     {
         LOCK2(cs_main, cs_vNodes);
         height = chainActive.Height();
+        tipmediantime = chainActive.Tip()->GetMedianTimePast();
         connections = vNodes.size();
         netsolps = GetNetworkHashPS(120, -1);
     }
     auto localsolps = GetLocalSolPS();
 
-    std::cout << "           " << _("Block height") << " | " << height << std::endl;
+    if (IsInitialBlockDownload()) {
+        int netheight = EstimateNetHeight(height, tipmediantime, Params());
+        int downloadPercent = height * 100 / netheight;
+        std::cout << "     " << _("Downloading blocks") << " | " << height << " / ~" << netheight << " (" << downloadPercent << "%)" << std::endl;
+    } else {
+        std::cout << "           " << _("Block height") << " | " << height << std::endl;
+    }
     std::cout << "            " << _("Connections") << " | " << connections << std::endl;
     std::cout << "  " << _("Network solution rate") << " | " << netsolps << " Sol/s" << std::endl;
     if (mining && miningTimer.running()) {
index 701306a4a567527064ce52e66a135bc648be7b38..7f7efdea6605d634135b19618eed2c715f282a7b 100644 (file)
@@ -63,6 +63,9 @@ void TrackMinedBlock(uint256 hash);
 
 void MarkStartTime();
 double GetLocalSolPS();
+int EstimateNetHeightInner(int height, int64_t tipmediantime,
+                           int heightLastCheckpoint, int64_t timeLastCheckpoint,
+                           int64_t targetSpacing);
 
 void TriggerRefresh();
 
This page took 0.02857 seconds and 4 git commands to generate.