Skip to content
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/Voting/VotingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public List<Poll> GetExpiredPolls()
}

/// <summary>
/// Tells us whether we have already voted to boot a federation member.
/// Tells us whether we have already voted on this federation member.
/// </summary>
public bool AlreadyVotingFor(VoteKey voteKey, byte[] federationMemberBytes)
{
Expand Down
17 changes: 7 additions & 10 deletions src/Stratis.Features.Collateral/CollateralPoAMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Validators;
using Stratis.Bitcoin.EventBus.CoreEvents;
using Stratis.Bitcoin.Features.BlockStore.AddressIndexing;
using Stratis.Bitcoin.Features.Miner;
using Stratis.Bitcoin.Features.PoA;
Expand Down Expand Up @@ -127,25 +128,21 @@ private void OnBeforeFillBlockTemplate()
pendingAddFederationMemberPolls = pendingAddFederationMemberPolls.Where(p => !p.PubKeysHexVotedInFavor.Any(v => v.PubKey == this.federationManager.CurrentFederationKey.PubKey.ToString())).ToList();

if (!pendingAddFederationMemberPolls.Any())
{
this.logger.LogDebug("There are no outstanding add member polls for this node to vote on.");
return;

IFederationMember collateralFederationMember = this.federationManager.GetCurrentFederationMember();

var poaConsensusFactory = this.network.Consensus.ConsensusFactory as PoAConsensusFactory;
}

foreach (Poll poll in pendingAddFederationMemberPolls)
{
this.logger.LogDebug($"Attempting to cast outstanding vote on poll '{poll.Id}'.");

ChainedHeader pollStartHeader = this.chainIndexer.GetHeader(poll.PollStartBlockData.Hash);
ChainedHeader votingRequestHeader = pollStartHeader.Previous;

// Already checked?
if (this.joinFederationRequestMonitor.AlreadyChecked(votingRequestHeader.HashBlock))
continue;

ChainedHeaderBlock blockData = this.consensusManager.GetBlockData(votingRequestHeader.HashBlock);

this.joinFederationRequestMonitor.OnBlockConnected(new Bitcoin.EventBus.CoreEvents.BlockConnected(
new ChainedHeaderBlock(blockData.Block, votingRequestHeader)));
this.joinFederationRequestMonitor.OnBlockConnected(new BlockConnected(new ChainedHeaderBlock(blockData.Block, votingRequestHeader)));
}

return;
Expand Down
17 changes: 5 additions & 12 deletions src/Stratis.Features.Collateral/JoinFederationRequestMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using NBitcoin;
using NLog;
using Stratis.Bitcoin.EventBus;
using Stratis.Bitcoin.EventBus.CoreEvents;
using Stratis.Bitcoin.Features.PoA;
using Stratis.Bitcoin.Features.PoA.Voting;
Expand All @@ -18,12 +17,10 @@ public class JoinFederationRequestMonitor
{
private readonly ILogger logger;
private readonly ISignals signals;
private SubscriptionToken blockConnectedToken;
private readonly VotingManager votingManager;
private readonly Network network;
private readonly Network counterChainNetwork;
private readonly IFederationManager federationManager;
private readonly HashSet<uint256> pollsCheckedWithJoinFederationRequestMonitor;

public JoinFederationRequestMonitor(VotingManager votingManager, Network network, CounterChainNetworkWrapper counterChainNetworkWrapper, IFederationManager federationManager, ISignals signals)
{
Expand All @@ -33,14 +30,11 @@ public JoinFederationRequestMonitor(VotingManager votingManager, Network network
this.network = network;
this.counterChainNetwork = counterChainNetworkWrapper.CounterChainNetwork;
this.federationManager = federationManager;
this.pollsCheckedWithJoinFederationRequestMonitor = new HashSet<uint256>();
}

public bool AlreadyChecked(uint256 hash) => this.pollsCheckedWithJoinFederationRequestMonitor.Contains(hash);

public Task InitializeAsync()
{
this.blockConnectedToken = this.signals.Subscribe<BlockConnected>(this.OnBlockConnected);
this.signals.Subscribe<BlockConnected>(this.OnBlockConnected);

return Task.CompletedTask;
}
Expand All @@ -56,8 +50,6 @@ public void OnBlockConnected(BlockConnected blockConnectedData)

List<IFederationMember> modifiedFederation = null;

this.pollsCheckedWithJoinFederationRequestMonitor.Add(blockConnectedData.ConnectedBlock.ChainedHeader.HashBlock);

List<Transaction> transactions = blockConnectedData.ConnectedBlock.Block.Transactions;

var encoder = new JoinFederationRequestEncoder();
Expand All @@ -78,9 +70,12 @@ public void OnBlockConnected(BlockConnected blockConnectedData)
continue;

// Only mining federation members vote to include new members.
modifiedFederation = modifiedFederation ?? this.votingManager.GetModifiedFederation(blockConnectedData.ConnectedBlock.ChainedHeader);
modifiedFederation ??= this.votingManager.GetModifiedFederation(blockConnectedData.ConnectedBlock.ChainedHeader);
if (!modifiedFederation.Any(m => m.PubKey == this.federationManager.CurrentFederationKey.PubKey))
{
this.logger.Debug($"Ignoring as member '{this.federationManager.CurrentFederationKey.PubKey}' is not part of the federation at block '{blockConnectedData.ConnectedBlock.ChainedHeader}'.");
return;
}

// Check if the collateral amount is valid.
decimal collateralAmount = request.CollateralAmount.ToDecimal(MoneyUnit.BTC);
Expand All @@ -89,7 +84,6 @@ public void OnBlockConnected(BlockConnected blockConnectedData)
if (collateralAmount != expectedCollateralAmount)
{
this.logger.Debug("Ignoring voting collateral amount '{0}', when expecting '{1}'.", collateralAmount, expectedCollateralAmount);

continue;
}

Expand All @@ -104,7 +98,6 @@ public void OnBlockConnected(BlockConnected blockConnectedData)
if (this.votingManager.AlreadyVotingFor(VoteKey.AddFederationMember, federationMemberBytes))
{
this.logger.Debug("Skipping because already voted for adding '{0}'.", request.PubKey.ToHex());

continue;
}

Expand Down