Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mining stats for non federation members #829

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.PoA.Tests/PoATestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PoATestsBase(TestPoANetwork network = null)

(this.federationManager, this.federationHistory) = CreateFederationManager(this, this.network, this.loggerFactory, this.signals);

this.slotsManager = new SlotsManager(this.network, this.federationManager, this.federationHistory, this.ChainIndexer, this.loggerFactory);
this.slotsManager = new SlotsManager(this.network, this.federationManager, this.federationHistory, this.ChainIndexer);

this.poaHeaderValidator = new PoABlockHeaderValidator(this.loggerFactory);
this.asyncProvider = new AsyncProvider(this.loggerFactory, this.signals);
Expand Down
6 changes: 3 additions & 3 deletions src/Stratis.Bitcoin.Features.PoA.Tests/SlotsManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SlotsManagerTests()

(this.federationManager, this.federationHistory) = PoATestsBase.CreateFederationManager(this);
this.chainIndexer = new Mock<ChainIndexer>();
this.slotsManager = new SlotsManager(this.network, this.federationManager, this.federationHistory, this.chainIndexer.Object, new LoggerFactory());
this.slotsManager = new SlotsManager(this.network, this.federationManager, this.federationHistory, this.chainIndexer.Object);
}

[Fact]
Expand All @@ -49,7 +49,7 @@ public void GetMiningTimestamp()
(IFederationManager fedManager, IFederationHistory federationHistory) = PoATestsBase.CreateFederationManager(this, this.network, new ExtendedLoggerFactory(), new Signals.Signals(new LoggerFactory(), null));
var header = new BlockHeader();
this.chainIndexer.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
this.slotsManager = new SlotsManager(this.network, fedManager, federationHistory, this.chainIndexer.Object, new LoggerFactory());
this.slotsManager = new SlotsManager(this.network, fedManager, federationHistory, this.chainIndexer.Object);

List<IFederationMember> federationMembers = fedManager.GetFederationMembers();
uint roundStart = this.consensusOptions.TargetSpacingSeconds * (uint)federationMembers.Count * 5;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void GetMiningTimestamp()
});

this.chainIndexer.Setup(x => x.Tip).Returns(new ChainedHeader(header, header.GetHash(), 0));
this.slotsManager = new SlotsManager(this.network, fedManager, federationHistory, this.chainIndexer.Object, new LoggerFactory());
this.slotsManager = new SlotsManager(this.network, fedManager, federationHistory, this.chainIndexer.Object);
Assert.Equal(nextTurnTimestamp, this.slotsManager.GetMiningTimestamp(thisTurnTimestamp + 1));

}
Expand Down
11 changes: 11 additions & 0 deletions src/Stratis.Bitcoin.Features.PoA/PoAMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ private void GatherMiningStatistics()
log.AppendLine(">> Miner");
log.AppendLine();

if (!this.federationManager.IsFederationMember)
{
log.AppendLine("Mining information is not available for non federation members.");
log.AppendLine("It is possible that your node was kicked from the federation due to inactivity.");
Copy link
Contributor

@quantumagi quantumagi Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be added subject to there being a pubkey for this node. One could even check the polls in order to show a more relevant/descriptive message. E.g:

  • "Your node has been voted out due to being inactive for X days and will be kicked after X blocks. Wait until then to re-join."
  • "Your node has been kicked due to being inactive for X days. You can now re-join. See the join_api_url".
  • "You can't participate as a federation member without a wallet."
  • "You have not been voted in as a federation member. See the join_api_url".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Gustav, will add to next PR.

log.AppendLine();

this.miningStatisticsLog = log.ToString();

return;
}

if (this.ibdState.IsInitialBlockDownload())
{
log.AppendLine("Mining information is not available whilst the node is syncing.");
Expand Down
10 changes: 3 additions & 7 deletions src/Stratis.Bitcoin.Features.PoA/SlotsManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Utilities;

Expand Down Expand Up @@ -33,16 +32,14 @@ public class SlotsManager : ISlotsManager

private readonly ChainIndexer chainIndexer;

private readonly ILogger logger;

public SlotsManager(Network network, IFederationManager federationManager, IFederationHistory federationHistory, ChainIndexer chainIndexer, ILoggerFactory loggerFactory)
public SlotsManager(Network network, IFederationManager federationManager, IFederationHistory federationHistory, ChainIndexer chainIndexer)
{
Guard.NotNull(network, nameof(network));

this.federationManager = Guard.NotNull(federationManager, nameof(federationManager));
this.federationHistory = federationHistory;
this.chainIndexer = chainIndexer;
this.consensusOptions = (network as PoANetwork).ConsensusOptions;
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
}

public uint GetMiningTimestamp(uint currentTime)
Expand Down Expand Up @@ -109,8 +106,7 @@ we may need to look a bit further back to find a "reference miner" that still oc
return nextTimestampForMining;
}

/// <inheritdoc />
public uint GetMiningTimestampLegacy(uint currentTime)
private uint GetMiningTimestampLegacy(uint currentTime)
{
if (!this.federationManager.IsFederationMember)
throw new NotAFederationMemberException();
Expand Down