Skip to content

Commit a027e45

Browse files
authored
[API] Add extra mining stats information for the dashboard (#732)
* Fix poll repo progress string * Add mining stats via POAMiner * Review
1 parent eac0c7f commit a027e45

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

src/Stratis.Bitcoin.Features.Api/NodeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public IActionResult Status([FromQuery] bool publish)
152152
ProcessId = Process.GetCurrentProcess().Id,
153153
Network = this.fullNode.Network.Name,
154154
ConsensusHeight = this.chainState.ConsensusTip?.Height,
155+
HeaderHeight = this.consensusManager.HeaderTip,
155156
DataDirectoryPath = this.nodeSettings.DataDir,
156157
Testnet = this.network.IsTest(),
157158
RelayFee = this.nodeSettings.MinRelayTxFeeRate?.FeePerK?.ToUnit(MoneyUnit.BTC) ?? 0,

src/Stratis.Bitcoin.Features.PoA/Models/FederationMemberModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@ public sealed class FederationMemberDetailedModel : FederationMemberModel
4646

4747
[JsonProperty("rewardEstimatePerBlock")]
4848
public double RewardEstimatePerBlock { get; set; }
49+
50+
[JsonProperty("miningStats")]
51+
public MiningStatisticsModel MiningStatistics { get; set; }
4952
}
5053
}

src/Stratis.Bitcoin.Features.PoA/PoAMiner.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.Extensions.Logging;
88
using NBitcoin;
9+
using Newtonsoft.Json;
910
using Stratis.Bitcoin.AsyncWork;
1011
using Stratis.Bitcoin.Configuration;
1112
using Stratis.Bitcoin.Configuration.Logging;
@@ -37,6 +38,12 @@ public interface IPoAMiner : IDisposable
3738
{
3839
/// <summary>Starts mining loop.</summary>
3940
void InitializeMining();
41+
42+
/// <summary>
43+
/// Returns mining statistics in the last amount of blocks equal to the federation size.
44+
/// </summary>
45+
/// <returns>Returns <c>true</c> if the miner produced a block in the last round and block producer hits.</returns>
46+
MiningStatisticsModel MiningStatistics { get; }
4047
}
4148

4249
/// <inheritdoc cref="IPoAMiner"/>
@@ -75,6 +82,8 @@ public class PoAMiner : IPoAMiner
7582

7683
private readonly NodeSettings nodeSettings;
7784

85+
private MiningStatisticsModel miningStatistics;
86+
7887
private readonly IWalletManager walletManager;
7988

8089
protected readonly VotingManager votingManager;
@@ -422,7 +431,6 @@ private void AddComponentStats(StringBuilder log)
422431
log.AppendLine();
423432
return;
424433
}
425-
426434
ChainedHeader tip = this.consensusManager.Tip;
427435
ChainedHeader currentHeader = tip;
428436

@@ -441,14 +449,21 @@ private void AddComponentStats(StringBuilder log)
441449
if (timeHeader < currentHeader.Header.Time)
442450
timeHeader += this.network.ConsensusOptions.TargetSpacingSeconds;
443451

452+
var statistics = new MiningStatisticsModel();
453+
444454
// Iterate mining slots.
445455
for (int i = 0; i < maxDepth; i++)
446456
{
447457
int headerSlot = (int)(timeHeader / this.network.ConsensusOptions.TargetSpacingSeconds) % modifiedFederation.Count;
448458

449459
PubKey pubKey = modifiedFederation[headerSlot].PubKey;
450460

451-
string pubKeyRepresentation = (pubKey == this.federationManager.CurrentFederationKey?.PubKey) ? "█████" : pubKey.ToString().Substring(0, pubKeyTakeCharacters);
461+
string pubKeyRepresentation = pubKey.ToString().Substring(0, pubKeyTakeCharacters);
462+
if (pubKey == this.federationManager.CurrentFederationKey?.PubKey)
463+
{
464+
pubKeyRepresentation = "█████";
465+
statistics.ProducedBlockInLastRound = true;
466+
}
452467

453468
// Mined in this slot?
454469
if (timeHeader == currentHeader.Header.Time)
@@ -471,13 +486,22 @@ private void AddComponentStats(StringBuilder log)
471486
log.AppendLine();
472487
}
473488

489+
statistics.FederationSize = maxDepth;
490+
statistics.MinerHits = hitCount;
491+
474492
log.Append("...");
475493
log.AppendLine();
476494
log.AppendLine($"Miner hits".PadRight(LoggingConfiguration.ColumnLength) + $": {hitCount} of {maxDepth}({(((float)hitCount / (float)maxDepth)).ToString("P2")})");
477495
log.AppendLine($"Miner idle time".PadRight(LoggingConfiguration.ColumnLength) + $": { TimeSpan.FromSeconds(this.network.ConsensusOptions.TargetSpacingSeconds * (maxDepth - hitCount)).ToString(@"hh\:mm\:ss")}");
478496
log.AppendLine();
479497
}
480498

499+
/// <inheritdoc/>
500+
public MiningStatisticsModel MiningStatistics
501+
{
502+
get { return this.miningStatistics; }
503+
}
504+
481505
/// <inheritdoc/>
482506
public virtual void Dispose()
483507
{
@@ -487,4 +511,16 @@ public virtual void Dispose()
487511
this.cancellation.Dispose();
488512
}
489513
}
514+
515+
public sealed class MiningStatisticsModel
516+
{
517+
[JsonProperty(PropertyName = "federationSize")]
518+
public int FederationSize { get; set; }
519+
520+
[JsonProperty(PropertyName = "minerHits")]
521+
public int MinerHits { get; set; }
522+
523+
[JsonProperty(PropertyName = "producedBlockInLastRound")]
524+
public bool ProducedBlockInLastRound { get; set; }
525+
}
490526
}

src/Stratis.Bitcoin.Features.PoA/Voting/FederationController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public sealed class FederationController : Controller
2121
private readonly IFederationHistory federationHistory;
2222
private readonly ILogger logger;
2323
private readonly Network network;
24+
private readonly IPoAMiner poaMiner;
2425
private readonly ReconstructFederationService reconstructFederationService;
2526
private readonly VotingManager votingManager;
2627

@@ -31,13 +32,15 @@ public FederationController(
3132
Network network,
3233
IIdleFederationMembersKicker idleFederationMembersKicker,
3334
IFederationHistory federationHistory,
35+
IPoAMiner poAMiner,
3436
ReconstructFederationService reconstructFederationService)
3537
{
3638
this.chainIndexer = chainIndexer;
3739
this.federationManager = federationManager;
3840
this.idleFederationMembersKicker = idleFederationMembersKicker;
3941
this.federationHistory = federationHistory;
4042
this.network = network;
43+
this.poaMiner = poAMiner;
4144
this.reconstructFederationService = reconstructFederationService;
4245
this.votingManager = votingManager;
4346

@@ -129,6 +132,8 @@ public IActionResult GetCurrentMemberInfo()
129132

130133
federationMemberModel.RewardEstimatePerBlock = 9d / this.federationManager.GetFederationMembers().Count;
131134

135+
federationMemberModel.MiningStatistics = this.poaMiner.MiningStatistics;
136+
132137
return Json(federationMemberModel);
133138
}
134139
catch (Exception e)

src/Stratis.Bitcoin.Features.PoA/Voting/VotingManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,10 @@ internal bool Synchronize(ChainedHeader newTip)
832832
if (header.Height % 10000 == 0)
833833
{
834834
var progress = (int)((decimal)header.Height / this.chainIndexer.Tip.Height * 100);
835-
this.logger.Info($"Synchronizing voting data at height {header.Height} / {this.chainIndexer.Tip.Height} ({progress}%).");
836-
this.signals.Publish(new FullNodeEvent() { Message = $"Synchronizing voting data at height {header.Height} / {this.chainIndexer.Tip.Height} ({progress})%.", State = FullNodeState.Initializing.ToString() });
835+
var progressString = $"Synchronizing voting data at height {header.Height} / {this.chainIndexer.Tip.Height} ({progress} %).";
836+
837+
this.logger.Info(progressString);
838+
this.signals.Publish(new FullNodeEvent() { Message = progressString, State = FullNodeState.Initializing.ToString() });
837839
}
838840
}
839841

src/Stratis.Bitcoin/Controllers/Models/StatusModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public StatusModel()
3838
/// <summary>System identifier of the node's process.</summary>
3939
public int ProcessId { get; set; }
4040

41-
/// <summary>The height of the consensus.</summary>
41+
/// <summary>The height of consensus.</summary>
4242
public int? ConsensusHeight { get; set; }
4343

44+
/// <summary>The height of the header store.</summary>
45+
public int? HeaderHeight { get; set; }
46+
4447
/// <summary>Height of the most recent block in persistent storage.</summary>
4548
/// <seealso cref="Stratis.Bitcoin.Features.BlockRepository.HighestPersistedBlock.Height"/>
4649
public int BlockStoreHeight { get; set; }

0 commit comments

Comments
 (0)