Skip to content

Commit

Permalink
Merge pull request stratisproject#185 from quantumagi/dltiminer2
Browse files Browse the repository at this point in the history
Add IMinerSettings interface
  • Loading branch information
quantumagi authored Feb 11, 2020
2 parents 4a5893c + 228cdb0 commit f9db120
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.Miner/BlockDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected BlockDefinition(
ILoggerFactory loggerFactory,
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
MinerSettings minerSettings,
IMinerSettings minerSettings,
Network network)
{
this.ConsensusManager = consensusManager;
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.Miner/MinerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Stratis.Bitcoin.Features.Miner
/// <summary>
/// Configuration related to the miner interface.
/// </summary>
public class MinerSettings
public class MinerSettings : IMinerSettings
{
private const ulong MinimumSplitCoinValueDefaultValue = 100 * Money.COIN;

Expand Down
13 changes: 8 additions & 5 deletions src/Stratis.Bitcoin.Features.Miner/MiningFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
using Stratis.Bitcoin.Configuration.Settings;
using Stratis.Bitcoin.Features.BlockStore;
using Stratis.Bitcoin.Features.MemoryPool;
using Stratis.Bitcoin.Features.Miner.Controllers;
using Stratis.Bitcoin.Features.Miner.Interfaces;
using Stratis.Bitcoin.Features.Miner.Staking;
using Stratis.Bitcoin.Features.RPC;
using Stratis.Bitcoin.Features.Wallet;
using Stratis.Bitcoin.Mining;
using Stratis.Bitcoin.Utilities;

[assembly: InternalsVisibleTo("Stratis.Bitcoin.Features.Miner.Tests")]

Expand Down Expand Up @@ -55,7 +55,7 @@ public class MiningFeature : FullNodeFeature
public MiningFeature(
ConnectionManagerSettings connectionManagerSettings,
Network network,
MinerSettings minerSettings,
IMinerSettings minerSettings,
NodeSettings nodeSettings,
ILoggerFactory loggerFactory,
ITimeSyncBehaviorState timeSyncBehaviorState,
Expand All @@ -64,7 +64,10 @@ public MiningFeature(
{
this.connectionManagerSettings = connectionManagerSettings;
this.network = network;
this.minerSettings = minerSettings;

Guard.Assert(minerSettings is MinerSettings);
this.minerSettings = (MinerSettings)minerSettings;

this.nodeSettings = nodeSettings;
this.powMining = powMining;
this.timeSyncBehaviorState = timeSyncBehaviorState;
Expand Down Expand Up @@ -222,7 +225,7 @@ public static IFullNodeBuilder AddMining(this IFullNodeBuilder fullNodeBuilder)
services.AddSingleton<IPowMining, PowMining>();
services.AddSingleton<IBlockProvider, BlockProvider>();
services.AddSingleton<BlockDefinition, PowBlockDefinition>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<IMinerSettings, MinerSettings>();
});
});

Expand Down Expand Up @@ -255,7 +258,7 @@ public static IFullNodeBuilder AddPowPosMining(this IFullNodeBuilder fullNodeBui
services.AddSingleton<BlockDefinition, PowBlockDefinition>();
services.AddSingleton<BlockDefinition, PosBlockDefinition>();
services.AddSingleton<BlockDefinition, PosPowBlockDefinition>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<IMinerSettings, MinerSettings>();
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.Miner/PosBlockDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public PosBlockDefinition(
ILoggerFactory loggerFactory,
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
MinerSettings minerSettings,
IMinerSettings minerSettings,
Network network,
IStakeChain stakeChain,
IStakeValidator stakeValidator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PosPowBlockDefinition(
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
Network network,
MinerSettings minerSettings,
IMinerSettings minerSettings,
IStakeChain stakeChain,
IStakeValidator stakeValidator)
: base(consensusManager, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network)
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.Miner/PowBlockDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PowBlockDefinition(
ILoggerFactory loggerFactory,
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
MinerSettings minerSettings,
IMinerSettings minerSettings,
Network network,
IConsensusRuleEngine consensusRules,
BlockDefinitionOptions options = null)
Expand Down
12 changes: 8 additions & 4 deletions src/Stratis.Bitcoin.Features.Miner/Staking/PosMinting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public PosMinting(
IAsyncProvider asyncProvider,
ITimeSyncBehaviorState timeSyncBehaviorState,
ILoggerFactory loggerFactory,
MinerSettings minerSettings)
IMinerSettings minerSettings)
{
this.blockProvider = blockProvider;
this.consensusManager = consensusManager;
Expand Down Expand Up @@ -260,9 +260,13 @@ public PosMinting(

this.rpcGetStakingInfoModel = new Models.GetStakingInfoModel();

this.CoinstakeSplitEnabled = minerSettings.EnableCoinStakeSplitting;
this.MinimumStakingCoinValue = minerSettings.MinimumStakingCoinValue;
this.MinimumSplitCoinValue = minerSettings.MinimumSplitCoinValue;
Guard.Assert(minerSettings is MinerSettings);

var posMinerSettings = (MinerSettings)minerSettings;

this.CoinstakeSplitEnabled = posMinerSettings.EnableCoinStakeSplitting;
this.MinimumStakingCoinValue = posMinerSettings.MinimumStakingCoinValue;
this.MinimumSplitCoinValue = posMinerSettings.MinimumSplitCoinValue;
this.ValidStakingTemplates = walletManager.GetValidStakingTemplates();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Stratis.Bitcoin.Features.Miner;
using Stratis.Bitcoin.Features.PoA.Voting;
using Stratis.Bitcoin.Interfaces;
using Stratis.Bitcoin.Mining;
using Stratis.Bitcoin.Utilities;
using Stratis.Feature.PoA.Tokenless.Core;

Expand Down Expand Up @@ -40,7 +41,7 @@ public TestPoAMiner(
IMiningKeyProvider miningKeyProvider,
INodeStats nodeStats,
VotingManager votingManager,
PoAMinerSettings poAMinerSettings,
IMinerSettings poAMinerSettings,
IAsyncProvider asyncProvider) : base(consensusManager, dateTimeProvider, network, nodeLifetime, loggerFactory, ibdState, blockDefinition, slotsManager,
connectionManager, poaHeaderValidator, federationManager, integrityValidator, miningKeyProvider, nodeStats, votingManager, poAMinerSettings, asyncProvider)
{
Expand Down Expand Up @@ -106,7 +107,7 @@ public TokenlessTestPoAMiner(
IIntegrityValidator integrityValidator,
IMiningKeyProvider miningKeyProvider,
INodeStats nodeStats,
PoAMinerSettings poAMinerSettings,
IMinerSettings poAMinerSettings,
IAsyncProvider asyncProvider,
VotingManager votingManager)
: base(coreComponent.ConsensusManager, coreComponent.DateTimeProvider, coreComponent.Network, coreComponent.NodeLifetime, coreComponent.LoggerFactory, coreComponent.InitialBlockDownloadState, blockDefinition, slotsManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DBreeze.Utils;
using NBitcoin;
using NBitcoin.Crypto;
using Stratis.Bitcoin.Controllers.Models;
using Stratis.Bitcoin.Features.PoA.IntegrationTests.Common;
using Stratis.Bitcoin.Features.PoA.Voting;
using Stratis.Bitcoin.Features.Wallet;
Expand Down Expand Up @@ -337,7 +338,7 @@ public async Task TransactionSentFeesReceivedByMinerAsync()
CoreNodePoAExtensions.WaitTillSynced(nodeA, nodeB);

// Will send funds to one of nodeB's addresses.
Script destination = nodeB.FullNode.WalletManager().GetUnusedAddress().ScriptPubKey;
NBitcoin.Script destination = nodeB.FullNode.WalletManager().GetUnusedAddress().ScriptPubKey;

var context = new TransactionBuildContext(network)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin.Features.PoA/PoABlockDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PoABlockDefinition(
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
Network network,
MinerSettings minerSettings)
IMinerSettings minerSettings)
: base(consensusManager, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network)
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/Stratis.Bitcoin.Features.PoA/PoAFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Stratis.Bitcoin.Features.PoA.ProtocolEncryption;
using Stratis.Bitcoin.Features.PoA.Voting;
using Stratis.Bitcoin.Interfaces;
using Stratis.Bitcoin.Mining;
using Stratis.Bitcoin.P2P.Peer;
using Stratis.Bitcoin.P2P.Protocol.Behaviors;
using Stratis.Bitcoin.P2P.Protocol.Payloads;
Expand Down Expand Up @@ -181,8 +182,7 @@ public static IFullNodeBuilder UsePoAConsensus(this IFullNodeBuilder fullNodeBui
services.AddSingleton<IFederationManager, FederationManager>();
services.AddSingleton<PoABlockHeaderValidator>();
services.AddSingleton<IPoAMiner, PoAMiner>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<PoAMinerSettings>();
services.AddSingleton<IMinerSettings, PoAMinerSettings>();
services.AddSingleton<ISlotsManager, SlotsManager>();
services.AddSingleton<BlockDefinition, PoABlockDefinition>();
});
Expand Down
7 changes: 5 additions & 2 deletions src/Stratis.Bitcoin.Features.PoA/PoAMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public PoAMiner(
IMiningKeyProvider miningKeyProvider,
INodeStats nodeStats,
VotingManager votingManager,
PoAMinerSettings poAMinerSettings,
IMinerSettings poAMinerSettings,
IAsyncProvider asyncProvider)
{
this.consensusManager = consensusManager;
Expand All @@ -108,7 +108,10 @@ public PoAMiner(
this.integrityValidator = integrityValidator;
this.miningKeyProvider = miningKeyProvider;
this.votingManager = votingManager;
this.settings = poAMinerSettings;

Guard.Assert(poAMinerSettings is PoAMinerSettings);
this.settings = (PoAMinerSettings)poAMinerSettings;

this.asyncProvider = asyncProvider;

this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
Expand Down
49 changes: 47 additions & 2 deletions src/Stratis.Bitcoin.Features.PoA/PoAMinerSettings.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
using Stratis.Bitcoin.Configuration;
using System.Text;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Mining;
using Stratis.Bitcoin.Utilities;

namespace Stratis.Bitcoin.Features.PoA
{
public class PoAMinerSettings
public class PoAMinerSettings : IMinerSettings
{
/// <summary>Allows mining in case node is in IBD and not connected to anyone.</summary>
public bool BootstrappingMode { get; private set; }

/// <inheritdoc />
public BlockDefinitionOptions BlockDefinitionOptions { get; private set; }

public PoAMinerSettings(NodeSettings nodeSettings)
{
Guard.NotNull(nodeSettings, nameof(nodeSettings));

TextFileConfiguration config = nodeSettings.ConfigReader;

this.BootstrappingMode = config.GetOrDefault<bool>("bootstrap", false);

uint blockMaxSize = (uint)config.GetOrDefault<int>("blockmaxsize", (int)nodeSettings.Network.Consensus.Options.MaxBlockSerializedSize);
uint blockMaxWeight = (uint)config.GetOrDefault<int>("blockmaxweight", (int)nodeSettings.Network.Consensus.Options.MaxBlockWeight);

this.BlockDefinitionOptions = new BlockDefinitionOptions(blockMaxWeight, blockMaxSize).RestrictForNetwork(nodeSettings.Network);
}


/// <summary>
/// Displays mining help information on the console.
/// </summary>
/// <param name="network">Not used.</param>
public static void PrintHelp(Network network)
{
NodeSettings defaults = NodeSettings.Default(network);
var builder = new StringBuilder();

builder.AppendLine("-bootstrap Bootstraps the blockchain by allowing the node to perform solo mining.");
builder.AppendLine("-blockmaxsize=<number> Maximum block size (in bytes) for the miner to generate.");
builder.AppendLine("-blockmaxweight=<number> Maximum block weight (in weight units) for the miner to generate.");

defaults.Logger.LogInformation(builder.ToString());
}

/// <summary>
/// Get the default configuration.
/// </summary>
/// <param name="builder">The string builder to add the settings to.</param>
/// <param name="network">The network to base the defaults off.</param>
public static void BuildDefaultConfigurationFile(StringBuilder builder, Network network)
{
builder.AppendLine("####Miner Settings####");
builder.AppendLine("#Bootstraps the blockchain by allowing the node to perform solo mining.");
builder.AppendLine($"#bootstrap=0");
builder.AppendLine("#Maximum block size (in bytes) for the miner to generate.");
builder.AppendLine($"#blockmaxsize={network.Consensus.Options.MaxBlockSerializedSize}");
builder.AppendLine("#Maximum block weight (in weight units) for the miner to generate.");
builder.AppendLine($"#blockmaxweight={network.Consensus.Options.MaxBlockWeight}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Features.Wallet.Models;
using Stratis.Bitcoin.Controllers.Models;
using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Utilities.JsonErrors;
using Stratis.Bitcoin.Utilities.ModelStateErrors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Stratis.Bitcoin.Features.PoA.Voting;
using Stratis.Bitcoin.Features.SmartContracts.ReflectionExecutor.Consensus.Rules;
using Stratis.Bitcoin.Features.SmartContracts.Rules;
using Stratis.Bitcoin.Mining;

namespace Stratis.Bitcoin.Features.SmartContracts.PoA
{
Expand Down Expand Up @@ -68,8 +69,7 @@ public static IFullNodeBuilder UseSmartContractPoAMining(this IFullNodeBuilder f
services.AddSingleton<IFederationManager, FederationManager>();
services.AddSingleton<PoABlockHeaderValidator>();
services.AddSingleton<IPoAMiner, PoAMiner>();
services.AddSingleton<PoAMinerSettings>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<IMinerSettings, PoAMinerSettings>();
services.AddSingleton<ISlotsManager, SlotsManager>();
services.AddSingleton<BlockDefinition, SmartContractPoABlockDefinition>();
services.AddSingleton<IBlockBufferGenerator, BlockBufferGenerator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Stratis.Bitcoin.Features.PoA.BasePoAFeatureConsensusRules;
using Stratis.Bitcoin.Features.SmartContracts.Caching;
using Stratis.Bitcoin.Features.SmartContracts.PoW;
using Stratis.Bitcoin.Mining;
using Stratis.Bitcoin.Utilities;
using Stratis.SmartContracts.CLR;
using Stratis.SmartContracts.Core;
Expand Down Expand Up @@ -35,7 +36,7 @@ public SmartContractPoABlockDefinition(
IStateRepositoryRoot stateRoot,
IBlockExecutionResultCache executionCache,
ICallDataSerializer callDataSerializer,
MinerSettings minerSettings)
IMinerSettings minerSettings)
: base(blockBufferGenerator, coinView, consensusManager, dateTimeProvider, executorFactory, loggerFactory, mempool,
mempoolLock, minerSettings, network, senderRetriever, stateRoot, executionCache, callDataSerializer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static IFullNodeBuilder UseSmartContractPosPowMining(this IFullNodeBuilde
services.AddSingleton<BlockDefinition, SmartContractBlockDefinition>();
services.AddSingleton<BlockDefinition, SmartContractPosPowBlockDefinition>();
services.AddSingleton<IBlockBufferGenerator, BlockBufferGenerator>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<IMinerSettings, MinerSettings>();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SmartContractPosPowBlockDefinition(
ILoggerFactory loggerFactory,
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
MinerSettings minerSettings,
IMinerSettings minerSettings,
Network network,
ISenderRetriever senderRetriever,
IStakeChain stakeChain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static IFullNodeBuilder UseSmartContractPowMining(this IFullNodeBuilder f
services.AddSingleton<IBlockProvider, SmartContractBlockProvider>();
services.AddSingleton<BlockDefinition, SmartContractBlockDefinition>();
services.AddSingleton<IBlockBufferGenerator, BlockBufferGenerator>();
services.AddSingleton<MinerSettings>();
services.AddSingleton<IMinerSettings, MinerSettings>();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public SmartContractBlockDefinition(
ILoggerFactory loggerFactory,
ITxMempool mempool,
MempoolSchedulerLock mempoolLock,
MinerSettings minerSettings,
IMinerSettings minerSettings,
Network network,
ISenderRetriever senderRetriever,
IStateRepositoryRoot stateRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Controllers.Models;
using Stratis.Bitcoin.Features.MemoryPool.Broadcasting;
using Stratis.Bitcoin.Features.Wallet.Helpers;
using Stratis.Bitcoin.Features.Wallet.Interfaces;
Expand Down
10 changes: 10 additions & 0 deletions src/Stratis.Bitcoin/Mining/IMinerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Stratis.Bitcoin.Mining
{
public interface IMinerSettings
{
/// <summary>
/// Settings for <see cref="BlockDefinition"/>.
/// </summary>
BlockDefinitionOptions BlockDefinitionOptions { get; }
}
}
2 changes: 1 addition & 1 deletion src/Stratis.Bitcoin/P2P/SelfEndpointTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Add(IPEndPoint ipEndPoint)
[NoTrace]
public bool IsSelf(IPEndPoint ipEndPoint)
{
return this.knownSelfEndpoints.Contains(ipEndPoint);
return this.knownSelfEndpoints.Contains(ipEndPoint.MapToIpv6());
}

/// <inheritdoc/>
Expand Down
Loading

0 comments on commit f9db120

Please sign in to comment.