Skip to content

Commit

Permalink
[API] Add extra mining stats information for the dashboard (#732)
Browse files Browse the repository at this point in the history
* Fix poll repo progress string

* Add mining stats via POAMiner

* Review
  • Loading branch information
fassadlr committed Oct 8, 2021
1 parent 555470a commit 3358628
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Stratis.Bitcoin.Features.Api/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public IActionResult Status()
ProcessId = Process.GetCurrentProcess().Id,
Network = this.fullNode.Network.Name,
ConsensusHeight = this.chainState.ConsensusTip?.Height,
HeaderHeight = this.consensusManager.HeaderTip,
DataDirectoryPath = this.nodeSettings.DataDir,
Testnet = this.network.IsTest(),
RelayFee = this.nodeSettings.MinRelayTxFeeRate?.FeePerK?.ToUnit(MoneyUnit.BTC) ?? 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public sealed class FederationMemberDetailedModel : FederationMemberModel

[JsonProperty("rewardEstimatePerBlock")]
public double RewardEstimatePerBlock { get; set; }

[JsonProperty("miningStats")]
public MiningStatisticsModel MiningStatistics { get; set; }
}
}
40 changes: 38 additions & 2 deletions src/Stratis.Bitcoin.Features.PoA/PoAMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Newtonsoft.Json;
using Stratis.Bitcoin.AsyncWork;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Configuration.Logging;
Expand Down Expand Up @@ -37,6 +38,12 @@ public interface IPoAMiner : IDisposable
{
/// <summary>Starts mining loop.</summary>
void InitializeMining();

/// <summary>
/// Returns mining statistics in the last amount of blocks equal to the federation size.
/// </summary>
/// <returns>Returns <c>true</c> if the miner produced a block in the last round and block producer hits.</returns>
MiningStatisticsModel MiningStatistics { get; }
}

/// <inheritdoc cref="IPoAMiner"/>
Expand Down Expand Up @@ -73,6 +80,8 @@ public class PoAMiner : IPoAMiner

private readonly NodeSettings nodeSettings;

private MiningStatisticsModel miningStatistics;

private readonly IWalletManager walletManager;

protected readonly VotingManager votingManager;
Expand Down Expand Up @@ -418,7 +427,6 @@ private void AddComponentStats(StringBuilder log)
log.AppendLine();
return;
}

ChainedHeader tip = this.consensusManager.Tip;
ChainedHeader currentHeader = tip;

Expand All @@ -442,14 +450,21 @@ private void AddComponentStats(StringBuilder log)
if (timeHeader < currentHeader.Header.Time)
timeHeader += this.network.ConsensusOptions.TargetSpacingSeconds;

var statistics = new MiningStatisticsModel();

// Iterate mining slots.
for (int i = 0; i < maxDepth; i++)
{
int headerSlot = (int)(timeHeader / this.network.ConsensusOptions.TargetSpacingSeconds) % modifiedFederation.Count;

PubKey pubKey = modifiedFederation[headerSlot].PubKey;

string pubKeyRepresentation = (pubKey == this.federationManager.CurrentFederationKey?.PubKey) ? "█████" : pubKey.ToString().Substring(0, pubKeyTakeCharacters);
string pubKeyRepresentation = pubKey.ToString().Substring(0, pubKeyTakeCharacters);
if (pubKey == this.federationManager.CurrentFederationKey?.PubKey)
{
pubKeyRepresentation = "█████";
statistics.ProducedBlockInLastRound = true;
}

// Mined in this slot?
if (timeHeader == currentHeader.Header.Time)
Expand All @@ -475,13 +490,22 @@ private void AddComponentStats(StringBuilder log)
log.AppendLine();
}

statistics.FederationSize = maxDepth;
statistics.MinerHits = hitCount;

log.Append("...");
log.AppendLine();
log.AppendLine($"Miner hits".PadRight(LoggingConfiguration.ColumnLength) + $": {hitCount} of {maxDepth}({(((float)hitCount / (float)maxDepth)).ToString("P2")})");
log.AppendLine($"Miner idle time".PadRight(LoggingConfiguration.ColumnLength) + $": { TimeSpan.FromSeconds(this.network.ConsensusOptions.TargetSpacingSeconds * (maxDepth - hitCount)).ToString(@"hh\:mm\:ss")}");
log.AppendLine();
}

/// <inheritdoc/>
public MiningStatisticsModel MiningStatistics
{
get { return this.miningStatistics; }
}

/// <inheritdoc/>
public virtual void Dispose()
{
Expand All @@ -491,4 +515,16 @@ public virtual void Dispose()
this.cancellation.Dispose();
}
}

public sealed class MiningStatisticsModel
{
[JsonProperty(PropertyName = "federationSize")]
public int FederationSize { get; set; }

[JsonProperty(PropertyName = "minerHits")]
public int MinerHits { get; set; }

[JsonProperty(PropertyName = "producedBlockInLastRound")]
public bool ProducedBlockInLastRound { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed class FederationController : Controller
private readonly IIdleFederationMembersKicker idleFederationMembersKicker;
private readonly ILogger logger;
private readonly Network network;
private readonly IPoAMiner poaMiner;
private readonly ReconstructFederationService reconstructFederationService;
private readonly VotingManager votingManager;

Expand All @@ -29,12 +30,14 @@ public sealed class FederationController : Controller
VotingManager votingManager,
Network network,
IIdleFederationMembersKicker idleFederationMembersKicker,
IPoAMiner poAMiner,
ReconstructFederationService reconstructFederationService)
{
this.chainIndexer = chainIndexer;
this.federationManager = federationManager;
this.idleFederationMembersKicker = idleFederationMembersKicker;
this.network = network;
this.poaMiner = poAMiner;
this.reconstructFederationService = reconstructFederationService;
this.votingManager = votingManager;

Expand Down Expand Up @@ -126,6 +129,8 @@ public IActionResult GetCurrentMemberInfo()

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

federationMemberModel.MiningStatistics = this.poaMiner.MiningStatistics;

return Json(federationMemberModel);
}
catch (Exception e)
Expand Down
5 changes: 4 additions & 1 deletion src/Stratis.Bitcoin/Controllers/Models/StatusModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ public StatusModel()
/// <summary>System identifier of the node's process.</summary>
public int ProcessId { get; set; }

/// <summary>The height of the consensus.</summary>
/// <summary>The height of consensus.</summary>
public int? ConsensusHeight { get; set; }

/// <summary>The height of the header store.</summary>
public int? HeaderHeight { get; set; }

/// <summary>Height of the most recent block in persistent storage.</summary>
/// <seealso cref="Stratis.Bitcoin.Features.BlockRepository.HighestPersistedBlock.Height"/>
public int BlockStoreHeight { get; set; }
Expand Down

0 comments on commit 3358628

Please sign in to comment.