1 // Copyright (c) 2016 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "chainparams.h"
8 #include "checkpoints.h"
10 #include "ui_interface.h"
13 #include "utilmoneystr.h"
14 #include "utilstrencodings.h"
16 #include <boost/thread.hpp>
17 #include <boost/thread/synchronized_value.hpp>
23 #include <sys/ioctl.h>
27 extern int64_t ASSETCHAINS_TIMELOCKGTE;
28 extern uint32_t ASSETCHAINS_ALGO, ASSETCHAINS_VERUSHASH;
29 int64_t komodo_block_unlocktime(uint32_t nHeight);
31 void AtomicTimer::start()
33 std::unique_lock<std::mutex> lock(mtx);
35 start_time = GetTime();
40 void AtomicTimer::stop()
42 std::unique_lock<std::mutex> lock(mtx);
43 // Ignore excess calls to stop()
47 int64_t time_span = GetTime() - start_time;
48 total_time += time_span;
53 bool AtomicTimer::running()
55 std::unique_lock<std::mutex> lock(mtx);
59 uint64_t AtomicTimer::threadCount()
61 std::unique_lock<std::mutex> lock(mtx);
65 double AtomicTimer::rate(const AtomicCounter& count)
67 std::unique_lock<std::mutex> lock(mtx);
68 int64_t duration = total_time;
70 // Timer is running, so get the latest count
71 duration += GetTime() - start_time;
73 return duration > 0 ? (double)count.get() / duration : 0;
76 CCriticalSection cs_metrics;
78 double AtomicTimer::rate(const int64_t count)
80 std::unique_lock<std::mutex> lock(mtx);
82 int64_t duration = total_time;
84 // Timer is running, so get the latest count
85 duration += GetTime() - start_time;
87 return duration > 0 ? (double)count / duration : 0;
90 boost::synchronized_value<int64_t> nNodeStartTime;
91 boost::synchronized_value<int64_t> nNextRefresh;
93 AtomicCounter transactionsValidated;
94 AtomicCounter ehSolverRuns;
95 AtomicCounter solutionTargetChecks;
96 AtomicCounter minedBlocks;
97 AtomicTimer miningTimer;
99 boost::synchronized_value<std::list<uint256>> trackedBlocks;
101 boost::synchronized_value<std::list<std::string>> messageBox;
102 boost::synchronized_value<std::string> initMessage;
105 extern int64_t GetNetworkHashPS(int lookup, int height);
107 void TrackMinedBlock(uint256 hash)
110 minedBlocks.increment();
111 trackedBlocks->push_back(hash);
116 *nNodeStartTime = GetTime();
121 return GetTime() - *nNodeStartTime;
124 double GetLocalSolPS()
126 if (ASSETCHAINS_ALGO == ASSETCHAINS_VERUSHASH)
128 return miningTimer.rate(nHashCount);
131 return miningTimer.rate(solutionTargetChecks);
134 int EstimateNetHeightInner(int height, int64_t tipmediantime,
135 int heightLastCheckpoint, int64_t timeLastCheckpoint,
136 int64_t genesisTime, int64_t targetSpacing)
138 // We average the target spacing with the observed spacing to the last
139 // checkpoint (either from below or above depending on the current height),
140 // and use that to estimate the current network height.
141 int medianHeight = height > CBlockIndex::nMedianTimeSpan ?
142 height - (1 + ((CBlockIndex::nMedianTimeSpan - 1) / 2)) :
144 double checkpointSpacing = medianHeight > heightLastCheckpoint ?
145 (double (tipmediantime - timeLastCheckpoint)) / (medianHeight - heightLastCheckpoint) :
146 (double (timeLastCheckpoint - genesisTime)) / heightLastCheckpoint;
147 double averageSpacing = (targetSpacing + checkpointSpacing) / 2;
148 int netheight = medianHeight + ((GetTime() - tipmediantime) / averageSpacing);
149 // Round to nearest ten to reduce noise
150 return ((netheight + 5) / 10) * 10;
153 int EstimateNetHeight(int height, int64_t tipmediantime, CChainParams chainParams)
155 auto checkpointData = chainParams.Checkpoints();
156 return EstimateNetHeightInner(
157 height, tipmediantime,
158 Checkpoints::GetTotalBlocksEstimate(checkpointData),
159 checkpointData.nTimeLastCheckpoint,
160 chainParams.GenesisBlock().nTime,
161 chainParams.GetConsensus().nPowTargetSpacing);
164 void TriggerRefresh()
166 *nNextRefresh = GetTime();
167 // Ensure that the refresh has started before we return
171 static bool metrics_ThreadSafeMessageBox(const std::string& message,
172 const std::string& caption,
175 // The SECURE flag has no effect in the metrics UI.
176 style &= ~CClientUIInterface::SECURE;
178 std::string strCaption;
179 // Check for usage of predefined caption
181 case CClientUIInterface::MSG_ERROR:
182 strCaption += _("Error");
184 case CClientUIInterface::MSG_WARNING:
185 strCaption += _("Warning");
187 case CClientUIInterface::MSG_INFORMATION:
188 strCaption += _("Information");
191 strCaption += caption; // Use supplied caption (can be empty)
194 boost::strict_lock_ptr<std::list<std::string>> u = messageBox.synchronize();
195 u->push_back(strCaption + ": " + message);
204 static bool metrics_ThreadSafeQuestion(const std::string& /* ignored interactive message */, const std::string& message, const std::string& caption, unsigned int style)
206 return metrics_ThreadSafeMessageBox(message, caption, style);
209 static void metrics_InitMessage(const std::string& message)
211 *initMessage = message;
214 void ConnectMetricsScreen()
216 uiInterface.ThreadSafeMessageBox.disconnect_all_slots();
217 uiInterface.ThreadSafeMessageBox.connect(metrics_ThreadSafeMessageBox);
218 uiInterface.ThreadSafeQuestion.disconnect_all_slots();
219 uiInterface.ThreadSafeQuestion.connect(metrics_ThreadSafeQuestion);
220 uiInterface.InitMessage.disconnect_all_slots();
221 uiInterface.InitMessage.connect(metrics_InitMessage);
224 int printStats(bool mining)
226 // Number of lines that are always displayed
230 int64_t tipmediantime;
234 LOCK2(cs_main, cs_vNodes);
235 height = chainActive.Height();
236 tipmediantime = chainActive.LastTip()->GetMedianTimePast();
237 connections = vNodes.size();
238 netsolps = GetNetworkHashPS(120, -1);
240 auto localsolps = GetLocalSolPS();
242 if (IsInitialBlockDownload()) {
243 int netheight = EstimateNetHeight(height, tipmediantime, Params());
244 int downloadPercent = height * 100 / netheight;
245 std::cout << " " << _("Downloading blocks") << " | " << height << " / ~" << netheight << " (" << downloadPercent << "%)" << std::endl;
247 std::cout << " " << _("Block height") << " | " << height << std::endl;
249 std::cout << " " << _("Connections") << " | " << connections << std::endl;
250 std::cout << " " << _("Network solution rate") << " | " << netsolps << " Sol/s" << std::endl;
251 if (mining && miningTimer.running()) {
252 std::cout << " " << _("Local solution rate") << " | " << strprintf("%.4f Sol/s", localsolps) << std::endl;
255 std::cout << std::endl;
260 int printMiningStatus(bool mining)
263 // Number of lines that are always displayed
267 auto nThreads = miningTimer.threadCount();
269 std::cout << strprintf(_("You are mining with the %s solver on %d threads."),
270 GetArg("-equihashsolver", "default"), nThreads) << std::endl;
275 fvNodesEmpty = vNodes.empty();
278 std::cout << _("Mining is paused while waiting for connections.") << std::endl;
279 } else if (IsInitialBlockDownload()) {
280 std::cout << _("Mining is paused while downloading blocks.") << std::endl;
282 std::cout << _("Mining is paused (a JoinSplit may be in progress).") << std::endl;
287 std::cout << _("You are currently not mining.") << std::endl;
288 std::cout << _("To enable mining, add 'gen=1' to your zcash.conf and restart.") << std::endl;
291 std::cout << std::endl;
294 #else // ENABLE_MINING
296 #endif // !ENABLE_MINING
299 int printMetrics(size_t cols, bool mining)
301 // Number of lines that are always displayed
305 int64_t uptime = GetUptime();
306 int days = uptime / (24 * 60 * 60);
307 int hours = (uptime - (days * 24 * 60 * 60)) / (60 * 60);
308 int minutes = (uptime - (((days * 24) + hours) * 60 * 60)) / 60;
309 int seconds = uptime - (((((days * 24) + hours) * 60) + minutes) * 60);
312 std::string duration;
314 duration = strprintf(_("%d days, %d hours, %d minutes, %d seconds"), days, hours, minutes, seconds);
315 } else if (hours > 0) {
316 duration = strprintf(_("%d hours, %d minutes, %d seconds"), hours, minutes, seconds);
317 } else if (minutes > 0) {
318 duration = strprintf(_("%d minutes, %d seconds"), minutes, seconds);
320 duration = strprintf(_("%d seconds"), seconds);
322 std::string strDuration = strprintf(_("Since starting this node %s ago:"), duration);
323 std::cout << strDuration << std::endl;
324 lines += (strDuration.size() / cols);
326 int validatedCount = transactionsValidated.get();
327 if (validatedCount > 1) {
328 std::cout << "- " << strprintf(_("You have validated %d transactions!"), validatedCount) << std::endl;
329 } else if (validatedCount == 1) {
330 std::cout << "- " << _("You have validated a transaction!") << std::endl;
332 std::cout << "- " << _("You have validated no transactions.") << std::endl;
335 if (mining && loaded) {
336 std::cout << "- " << strprintf(_("You have completed %d Equihash solver runs."), ehSolverRuns.get()) << std::endl;
341 CAmount immature {0};
344 LOCK2(cs_main, cs_metrics);
345 boost::strict_lock_ptr<std::list<uint256>> u = trackedBlocks.synchronize();
346 auto consensusParams = Params().GetConsensus();
347 auto tipHeight = chainActive.Height();
349 // Update orphans and calculate subsidies
350 std::list<uint256>::iterator it = u->begin();
351 while (it != u->end()) {
353 if (mapBlockIndex.count(hash) > 0 &&
354 chainActive.Contains(mapBlockIndex[hash])) {
355 int height = mapBlockIndex[hash]->nHeight;
356 CAmount subsidy = GetBlockSubsidy(height, consensusParams);
357 if ((height > 0) && (height <= consensusParams.GetLastFoundersRewardBlockHeight())) {
358 subsidy -= subsidy/5;
361 if ((std::max(0, COINBASE_MATURITY - (tipHeight - height)) > 0) ||
362 (tipHeight < komodo_block_unlocktime(height) && subsidy >= ASSETCHAINS_TIMELOCKGTE)) {
373 mined = minedBlocks.get();
374 orphaned = mined - u->size();
378 std::string units = Params().CurrencyUnits();
379 std::cout << "- " << strprintf(_("You have mined %d blocks!"), mined) << std::endl;
381 << strprintf(_("Orphaned: %d blocks, Immature: %u %s, Mature: %u %s"),
383 FormatMoney(immature), units,
384 FormatMoney(mature), units)
389 std::cout << std::endl;
394 int printMessageBox(size_t cols)
396 boost::strict_lock_ptr<std::list<std::string>> u = messageBox.synchronize();
398 if (u->size() == 0) {
402 int lines = 2 + u->size();
403 std::cout << _("Messages:") << std::endl;
404 for (auto it = u->cbegin(); it != u->cend(); ++it) {
405 auto msg = FormatParagraph(*it, cols, 2);
406 std::cout << "- " << msg << std::endl;
407 // Handle newlines and wrapped lines
410 while (j < msg.size()) {
411 i = msg.find('\n', j);
412 if (i == std::string::npos) {
421 std::cout << std::endl;
425 int printInitMessage()
431 std::string msg = *initMessage;
432 std::cout << _("Init message:") << " " << msg << std::endl;
433 std::cout << std::endl;
435 if (msg == _("Done loading")) {
442 void ThreadShowMetricsScreen()
444 // Make this thread recognisable as the metrics screen thread
445 RenameThread("zcash-metrics-screen");
447 // Determine whether we should render a persistent UI or rolling metrics
448 bool isTTY = isatty(STDOUT_FILENO);
449 bool isScreen = GetBoolArg("-metricsui", isTTY);
450 int64_t nRefresh = GetArg("-metricsrefreshtime", isTTY ? 1 : 600);
454 std::cout << "\e[2J";
457 std::cout << METRICS_ART << std::endl;
458 std::cout << std::endl;
461 std::cout << _("Thank you for running a Zcash node!") << std::endl;
462 std::cout << _("You're helping to strengthen the network and contributing to a social good :)") << std::endl;
464 // Privacy notice text
465 std::cout << PrivacyInfo();
466 std::cout << std::endl;
470 // Number of lines that are always displayed
474 // Get current window size
477 CONSOLE_SCREEN_BUFFER_INFO csbi;
478 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
479 cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
483 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1 && w.ws_col != 0) {
490 // Erase below current position
496 bool mining = GetBoolArg("-gen", false);
502 lines += printStats(mining);
503 lines += printMiningStatus(mining);
505 lines += printMetrics(cols, mining);
506 lines += printMessageBox(cols);
507 lines += printInitMessage();
510 // Explain how to exit
511 std::cout << "[" << _("Press Ctrl+C to exit") << "] [" << _("Set 'showmetrics=0' to hide") << "]" << std::endl;
514 std::cout << "----------------------------------------" << std::endl;
517 *nNextRefresh = GetTime() + nRefresh;
518 while (GetTime() < *nNextRefresh) {
519 boost::this_thread::interruption_point();
524 // Return to the top of the updating section
525 std::cout << "\e[" << lines << "A";