Commit 1887f997 authored by Sergei Zubov's avatar Sergei Zubov

Merge funds preservation rule

parent 12dd1aa9
......@@ -116,6 +116,7 @@ namespace Stratis.Bitcoin.Features.Consensus
// rules that require the store to be loaded (coinview)
new DeStreamLoadCoinviewRule(),
new DeStreamFundsPreservationRule(),
new TransactionDuplicationActivationRule(), // implements BIP30
new DeStreamPowCoinviewRule(), // implements BIP68, MaxSigOps and BlockReward calculation
new SaveCoinviewRule(),
......@@ -174,6 +175,7 @@ namespace Stratis.Bitcoin.Features.Consensus
// rules that require the store to be loaded (coinview)
new DeStreamLoadCoinviewRule(),
new DeStreamFundsPreservationRule(),
new TransactionDuplicationActivationRule(), // implements BIP30
new DeStreamPosCoinviewRule(), // implements BIP68, MaxSigOps and BlockReward calculation
// Place the PosColdStakingRule after the PosCoinviewRule to ensure that all input scripts have been evaluated
......
......@@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using NBitcoin;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Rules;
namespace Stratis.Bitcoin.Features.Consensus.Rules.CommonRules
......@@ -9,7 +10,6 @@ namespace Stratis.Bitcoin.Features.Consensus.Rules.CommonRules
/// <summary>
/// A rule verifies that total amount of coins in blockchain is not changed
/// </summary>
[ExecutionRule]
public class DeStreamFundsPreservationRule : UtxoStoreConsensusRule
{
public override Task RunAsync(RuleContext context)
......@@ -17,12 +17,12 @@ namespace Stratis.Bitcoin.Features.Consensus.Rules.CommonRules
UnspentOutputSet inputs =
(context as UtxoRuleContext ??
throw new NotSupportedException($"Rule context must be {nameof(UtxoRuleContext)}")).UnspentOutputSet;
long totalIn = context.ValidationContext.Block.Transactions.SelectMany(p =>
long totalIn = context.ValidationContext.BlockToValidate.Transactions.SelectMany(p =>
p.Inputs.RemoveChangePointer().Where(q => q.PrevOut != null)
.Select(q => inputs.AccessCoins(q.PrevOut.Hash)?.TryGetOutput(q.PrevOut.N)?.Value ?? 0))
.Sum(p => p ?? 0);
long totalOut = context.ValidationContext.Block.Transactions.SelectMany(p => p.Outputs.Select(q => q.Value))
long totalOut = context.ValidationContext.BlockToValidate.Transactions.SelectMany(p => p.Outputs.Select(q => q.Value))
.Sum(p => p);
if (totalIn != totalOut)
......
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