Commit 711c9139 authored by Sergei Zubov's avatar Sergei Zubov

Add funds preservation rule

parent 781d0b29
......@@ -74,5 +74,7 @@ namespace Stratis.Bitcoin.Features.Consensus
public static readonly ConsensusError ProofOfWorkTooHigh = new ConsensusError("proof-of-work-too-heigh", "proof of work too heigh");
public static readonly ConsensusError CheckpointViolation = new ConsensusError("checkpoint-violation", "block header hash does not match the checkpointed value");
public static readonly ConsensusError BadBlockTotalFundsChanged = new ConsensusError("bad-block-total-funds-changed", "total amount of funds in blockchain changed");
}
}
......@@ -133,6 +133,7 @@ namespace Stratis.Bitcoin.Features.Consensus
// rules that require the store to be loaded (coinview)
new DeStreamLoadCoinviewRule(),
new DeStreamFundsPreservationRule(),
new TransactionDuplicationActivationRule(), // implements BIP30
new PowCoinviewRule(), // implements BIP68, MaxSigOps and BlockReward calculation
new DeStreamBlockFeeRule()
......@@ -178,6 +179,7 @@ namespace Stratis.Bitcoin.Features.Consensus
// rules that require the store to be loaded (coinview)
new DeStreamLoadCoinviewRule(),
new DeStreamFundsPreservationRule(),
new TransactionDuplicationActivationRule(), // implements BIP30
new PosCoinviewRule(), // implements BIP68, MaxSigOps and BlockReward calculation
new DeStreamBlockFeeRule()
......
using System;
using System.Linq;
using System.Threading.Tasks;
using NBitcoin;
using Stratis.Bitcoin.Consensus.Rules;
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)
{
UnspentOutputSet inputs =
(context as UtxoRuleContext ??
throw new NotSupportedException($"Rule context must be {nameof(UtxoRuleContext)}")).UnspentOutputSet;
long totalIn = context.ValidationContext.Block.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))
.Sum(p => p);
if (totalIn != totalOut)
ConsensusErrors.BadBlockTotalFundsChanged.Throw();
return Task.CompletedTask;
}
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<Version>1.1.12-beta</Version>
<Version>1.1.12-beta</Version>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
......@@ -36,7 +36,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1705;IDE0008;</NoWarn>
<DocumentationFile>
</DocumentationFile>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
</Project>
\ No newline at end of file
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