Commit 046c7d88 authored by Sergei Zubov's avatar Sergei Zubov

Merge block definition

parent 13033521
...@@ -164,7 +164,7 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -164,7 +164,7 @@ namespace Stratis.Bitcoin.Features.Miner
/// Configures (resets) the builder to its default state /// Configures (resets) the builder to its default state
/// before constructing a new block. /// before constructing a new block.
/// </summary> /// </summary>
private void Configure() protected void Configure()
{ {
this.BlockSize = 1000; this.BlockSize = 1000;
this.BlockTemplate = new BlockTemplate(this.Network); this.BlockTemplate = new BlockTemplate(this.Network);
...@@ -182,7 +182,7 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -182,7 +182,7 @@ namespace Stratis.Bitcoin.Features.Miner
/// <param name="chainTip">Tip of the chain that this instance will work with without touching any shared chain resources.</param> /// <param name="chainTip">Tip of the chain that this instance will work with without touching any shared chain resources.</param>
/// <param name="scriptPubKey">Script that explains what conditions must be met to claim ownership of a coin.</param> /// <param name="scriptPubKey">Script that explains what conditions must be met to claim ownership of a coin.</param>
/// <returns>The contructed <see cref="Mining.BlockTemplate"/>.</returns> /// <returns>The contructed <see cref="Mining.BlockTemplate"/>.</returns>
protected void OnBuild(ChainedHeader chainTip, Script scriptPubKey) protected virtual void OnBuild(ChainedHeader chainTip, Script scriptPubKey)
{ {
this.Configure(); this.Configure();
......
...@@ -3,6 +3,7 @@ using System.Linq; ...@@ -3,6 +3,7 @@ using System.Linq;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Features.Consensus; using Stratis.Bitcoin.Features.Consensus;
using Stratis.Bitcoin.Features.Consensus.Interfaces; using Stratis.Bitcoin.Features.Consensus.Interfaces;
using Stratis.Bitcoin.Features.Consensus.Rules.CommonRules; using Stratis.Bitcoin.Features.Consensus.Rules.CommonRules;
...@@ -14,8 +15,14 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -14,8 +15,14 @@ namespace Stratis.Bitcoin.Features.Miner
{ {
public class DeStreamPosPowBlockDefinition : PosPowBlockDefinition public class DeStreamPosPowBlockDefinition : PosPowBlockDefinition
{ {
public DeStreamPosPowBlockDefinition(IConsensusLoop consensusLoop, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, ITxMempool mempool, MempoolSchedulerLock mempoolLock, Network network, IStakeChain stakeChain, IStakeValidator stakeValidator) : base(consensusLoop, dateTimeProvider, loggerFactory, mempool, mempoolLock, network, stakeChain, stakeValidator) private DeStreamNetwork DeStreamNetwork
{ {
get
{
if (!(this.Network is DeStreamNetwork))
throw new NotSupportedException($"Network must be {nameof(NBitcoin.DeStreamNetwork)}");
return (DeStreamNetwork) this.Network;
}
} }
protected override void OnBuild(ChainedHeader chainTip, Script scriptPubKey) protected override void OnBuild(ChainedHeader chainTip, Script scriptPubKey)
...@@ -37,8 +44,7 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -37,8 +44,7 @@ namespace Stratis.Bitcoin.Features.Miner
// pblock->nVersion = GetArg("-blockversion", pblock->nVersion); // pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
this.MedianTimePast = Utils.DateTimeToUnixTime(this.ChainTip.GetMedianTimePast()); this.MedianTimePast = Utils.DateTimeToUnixTime(this.ChainTip.GetMedianTimePast());
this.LockTimeCutoff = this.LockTimeCutoff = MempoolValidator.StandardLocktimeVerifyFlags.HasFlag(Transaction.LockTimeFlags.MedianTimePast)
MempoolValidator.StandardLocktimeVerifyFlags.HasFlag(Transaction.LockTimeFlags.MedianTimePast)
? this.MedianTimePast ? this.MedianTimePast
: this.block.Header.Time; : this.block.Header.Time;
...@@ -63,16 +69,14 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -63,16 +69,14 @@ namespace Stratis.Bitcoin.Features.Miner
// TODO: Implement Witness Code // TODO: Implement Witness Code
// pblocktemplate->CoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus()); // pblocktemplate->CoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
var coinviewRule = this.ConsensusLoop.ConsensusRules.GetRule<CoinViewRule>(); var coinviewRule = this.ConsensusManager.ConsensusRules.GetRule<CoinViewRule>();
this.Network.SplitFee(this.fees.Satoshi, out long deStreamFee, out long minerReward); this.DeStreamNetwork.SplitFee(this.fees.Satoshi, out long deStreamFee, out long minerReward);
this.coinbase.Outputs[0].Value = minerReward; this.coinbase.Outputs[0].Value = minerReward;
this.coinbase.Outputs[1].Value = deStreamFee; this.coinbase.Outputs[1].Value = deStreamFee;
this.BlockTemplate.TotalFee = this.fees; this.BlockTemplate.TotalFee = this.fees;
int nSerializeSize = this.block.GetSerializedSize(); int nSerializeSize = this.block.GetSerializedSize();
this.logger.LogDebug( this.logger.LogDebug("Serialized size is {0} bytes, block weight is {1}, number of txs is {2}, tx fees are {3}, number of sigops is {4}.", nSerializeSize, coinviewRule.GetBlockWeight(this.block), this.BlockTx, this.fees, this.BlockSigOpsCost);
"Serialized size is {0} bytes, block weight is {1}, number of txs is {2}, tx fees are {3}, number of sigops is {4}.",
nSerializeSize, coinviewRule.GetBlockWeight(this.block), this.BlockTx, this.fees, this.BlockSigOpsCost);
this.UpdateHeaders(); this.UpdateHeaders();
} }
...@@ -82,9 +86,13 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -82,9 +86,13 @@ namespace Stratis.Bitcoin.Features.Miner
base.CreateCoinbase(); base.CreateCoinbase();
Script deStreamAddressKey = new KeyId(new uint160(Encoders.Base58Check Script deStreamAddressKey = new KeyId(new uint160(Encoders.Base58Check
.DecodeData(this.Network.DeStreamWallet) .DecodeData(this.DeStreamNetwork.DeStreamWallet)
.Skip(this.Network.Base58Prefixes[(int) Base58Type.PUBKEY_ADDRESS].Length).ToArray())).ScriptPubKey; .Skip(this.Network.Base58Prefixes[(int) Base58Type.PUBKEY_ADDRESS].Length).ToArray())).ScriptPubKey;
this.coinbase.AddOutput(new TxOut(Money.Zero, deStreamAddressKey)); this.coinbase.AddOutput(new TxOut(Money.Zero, deStreamAddressKey));
} }
public DeStreamPosPowBlockDefinition(IConsensusManager consensusManager, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, ITxMempool mempool, MempoolSchedulerLock mempoolLock, Network network, MinerSettings minerSettings, IStakeChain stakeChain, IStakeValidator stakeValidator) : base(consensusManager, dateTimeProvider, loggerFactory, mempool, mempoolLock, network, minerSettings, stakeChain, stakeValidator)
{
}
} }
} }
\ No newline at end of file
...@@ -3,6 +3,7 @@ using System.Linq; ...@@ -3,6 +3,7 @@ using System.Linq;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Rules; using Stratis.Bitcoin.Consensus.Rules;
using Stratis.Bitcoin.Features.Consensus.Interfaces; using Stratis.Bitcoin.Features.Consensus.Interfaces;
using Stratis.Bitcoin.Features.Consensus.Rules.CommonRules; using Stratis.Bitcoin.Features.Consensus.Rules.CommonRules;
...@@ -14,8 +15,14 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -14,8 +15,14 @@ namespace Stratis.Bitcoin.Features.Miner
{ {
public class DeStreamPowBlockDefinition : PowBlockDefinition public class DeStreamPowBlockDefinition : PowBlockDefinition
{ {
public DeStreamPowBlockDefinition(IConsensusLoop consensusLoop, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, ITxMempool mempool, MempoolSchedulerLock mempoolLock, Network network, IConsensusRules consensusRules, BlockDefinitionOptions options = null) : base(consensusLoop, dateTimeProvider, loggerFactory, mempool, mempoolLock, network, consensusRules, options) private DeStreamNetwork DeStreamNetwork
{ {
get
{
if (!(this.Network is DeStreamNetwork))
throw new NotSupportedException($"Network must be {nameof(NBitcoin.DeStreamNetwork)}");
return (DeStreamNetwork) this.Network;
}
} }
protected override void OnBuild(ChainedHeader chainTip, Script scriptPubKey) protected override void OnBuild(ChainedHeader chainTip, Script scriptPubKey)
...@@ -37,8 +44,7 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -37,8 +44,7 @@ namespace Stratis.Bitcoin.Features.Miner
// pblock->nVersion = GetArg("-blockversion", pblock->nVersion); // pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
this.MedianTimePast = Utils.DateTimeToUnixTime(this.ChainTip.GetMedianTimePast()); this.MedianTimePast = Utils.DateTimeToUnixTime(this.ChainTip.GetMedianTimePast());
this.LockTimeCutoff = this.LockTimeCutoff = MempoolValidator.StandardLocktimeVerifyFlags.HasFlag(Transaction.LockTimeFlags.MedianTimePast)
MempoolValidator.StandardLocktimeVerifyFlags.HasFlag(Transaction.LockTimeFlags.MedianTimePast)
? this.MedianTimePast ? this.MedianTimePast
: this.block.Header.Time; : this.block.Header.Time;
...@@ -63,16 +69,14 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -63,16 +69,14 @@ namespace Stratis.Bitcoin.Features.Miner
// TODO: Implement Witness Code // TODO: Implement Witness Code
// pblocktemplate->CoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus()); // pblocktemplate->CoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
var coinviewRule = this.ConsensusLoop.ConsensusRules.GetRule<CoinViewRule>(); var coinviewRule = this.ConsensusManager.ConsensusRules.GetRule<CoinViewRule>();
this.Network.SplitFee(this.fees.Satoshi, out long deStreamFee, out long minerReward); this.DeStreamNetwork.SplitFee(this.fees.Satoshi, out long deStreamFee, out long minerReward);
this.coinbase.Outputs[0].Value = minerReward; this.coinbase.Outputs[0].Value = minerReward;
this.coinbase.Outputs[1].Value = deStreamFee; this.coinbase.Outputs[1].Value = deStreamFee;
this.BlockTemplate.TotalFee = this.fees; this.BlockTemplate.TotalFee = this.fees;
int nSerializeSize = this.block.GetSerializedSize(); int nSerializeSize = this.block.GetSerializedSize();
this.logger.LogDebug( this.logger.LogDebug("Serialized size is {0} bytes, block weight is {1}, number of txs is {2}, tx fees are {3}, number of sigops is {4}.", nSerializeSize, coinviewRule.GetBlockWeight(this.block), this.BlockTx, this.fees, this.BlockSigOpsCost);
"Serialized size is {0} bytes, block weight is {1}, number of txs is {2}, tx fees are {3}, number of sigops is {4}.",
nSerializeSize, coinviewRule.GetBlockWeight(this.block), this.BlockTx, this.fees, this.BlockSigOpsCost);
this.UpdateHeaders(); this.UpdateHeaders();
} }
...@@ -82,9 +86,13 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -82,9 +86,13 @@ namespace Stratis.Bitcoin.Features.Miner
base.CreateCoinbase(); base.CreateCoinbase();
Script deStreamAddressKey = new KeyId(new uint160(Encoders.Base58Check Script deStreamAddressKey = new KeyId(new uint160(Encoders.Base58Check
.DecodeData(this.Network.DeStreamWallet) .DecodeData(this.DeStreamNetwork.DeStreamWallet)
.Skip(this.Network.Base58Prefixes[(int) Base58Type.PUBKEY_ADDRESS].Length).ToArray())).ScriptPubKey; .Skip(this.Network.Base58Prefixes[(int) Base58Type.PUBKEY_ADDRESS].Length).ToArray())).ScriptPubKey;
this.coinbase.AddOutput(new TxOut(Money.Zero, deStreamAddressKey)); this.coinbase.AddOutput(new TxOut(Money.Zero, deStreamAddressKey));
} }
public DeStreamPowBlockDefinition(IConsensusManager consensusManager, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, ITxMempool mempool, MempoolSchedulerLock mempoolLock, MinerSettings minerSettings, Network network, IConsensusRuleEngine consensusRules, BlockDefinitionOptions options = null) : base(consensusManager, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network, consensusRules, options)
{
}
} }
} }
\ No newline at end of file
...@@ -14,10 +14,10 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -14,10 +14,10 @@ namespace Stratis.Bitcoin.Features.Miner
/// <summary> /// <summary>
/// Defines how a proof of work block will be built on a proof of stake network. /// Defines how a proof of work block will be built on a proof of stake network.
/// </summary> /// </summary>
public sealed class PosPowBlockDefinition : BlockDefinition public class PosPowBlockDefinition : BlockDefinition
{ {
/// <summary>Instance logger.</summary> /// <summary>Instance logger.</summary>
private readonly ILogger logger; protected readonly ILogger logger;
/// <summary>Database of stake related data for the current blockchain.</summary> /// <summary>Database of stake related data for the current blockchain.</summary>
private readonly IStakeChain stakeChain; private readonly IStakeChain stakeChain;
......
...@@ -11,7 +11,7 @@ namespace Stratis.Bitcoin.Features.Miner ...@@ -11,7 +11,7 @@ namespace Stratis.Bitcoin.Features.Miner
public class PowBlockDefinition : BlockDefinition public class PowBlockDefinition : BlockDefinition
{ {
private readonly IConsensusRuleEngine consensusRules; private readonly IConsensusRuleEngine consensusRules;
private readonly ILogger logger; protected readonly ILogger logger;
public PowBlockDefinition( public PowBlockDefinition(
IConsensusManager consensusManager, IConsensusManager consensusManager,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment