This repository was archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathTestRulesContext.cs
More file actions
216 lines (178 loc) · 9.79 KB
/
TestRulesContext.cs
File metadata and controls
216 lines (178 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NBitcoin;
using NBitcoin.Rules;
using Stratis.Bitcoin.AsyncWork;
using Stratis.Bitcoin.Base;
using Stratis.Bitcoin.Base.Deployments;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Configuration.Logging;
using Stratis.Bitcoin.Configuration.Settings;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Rules;
using Stratis.Bitcoin.Features.Consensus.CoinViews;
using Stratis.Bitcoin.Features.Consensus.Interfaces;
using Stratis.Bitcoin.Features.Consensus.ProvenBlockHeaders;
using Stratis.Bitcoin.Features.Consensus.Rules;
using Stratis.Bitcoin.Signals;
using Stratis.Bitcoin.Utilities;
using Xunit.Sdk;
namespace Stratis.Bitcoin.Features.Consensus.Tests.Rules
{
/// <summary>
/// Concrete instance of the test chain.
/// </summary>
internal class TestRulesContext
{
public ConsensusRuleEngine ConsensusRuleEngine { get; set; }
public IDateTimeProvider DateTimeProvider { get; set; }
public ILoggerFactory LoggerFactory { get; set; }
public NodeSettings NodeSettings { get; set; }
public ChainIndexer ChainIndexer { get; set; }
public Network Network { get; set; }
public ICheckpoints Checkpoints { get; set; }
public IChainState ChainState { get; set; }
public ISignals Signals { get; set; }
public IAsyncProvider AsyncProvider { get; internal set; }
public T CreateRule<T>() where T : ConsensusRuleBase, new()
{
var rule = new T();
rule.Parent = this.ConsensusRuleEngine;
rule.Logger = this.LoggerFactory.CreateLogger(rule.GetType().FullName);
rule.Initialize();
return rule;
}
}
public class RuleRegistrationHelper
{
private readonly ConsensusRuleEngine ruleEngine;
private readonly ConsensusRulesContainer consensusRulesContainer;
public RuleRegistrationHelper(ConsensusRuleEngine ruleEngine, ConsensusRulesContainer consensusRulesContainer)
{
this.ruleEngine = ruleEngine;
this.consensusRulesContainer = consensusRulesContainer;
}
public T RegisterRule<T>() where T : ConsensusRuleBase, new()
{
var rule = new T();
if (rule is IHeaderValidationConsensusRule validationConsensusRule)
this.consensusRulesContainer.HeaderValidationRules = new List<HeaderValidationConsensusRule>() { validationConsensusRule as HeaderValidationConsensusRule };
else if (rule is IIntegrityValidationConsensusRule consensusRule)
this.consensusRulesContainer.IntegrityValidationRules = new List<IntegrityValidationConsensusRule>() { consensusRule as IntegrityValidationConsensusRule };
else if (rule is IPartialValidationConsensusRule partialValidationConsensusRule)
this.consensusRulesContainer.PartialValidationRules = new List<PartialValidationConsensusRule>() { partialValidationConsensusRule as PartialValidationConsensusRule };
else if (rule is IFullValidationConsensusRule fullValidationConsensusRule)
this.consensusRulesContainer.FullValidationRules = new List<FullValidationConsensusRule>() { fullValidationConsensusRule as FullValidationConsensusRule };
else
throw new Exception("Rule type wasn't recognized.");
this.ruleEngine.SetupRulesEngineParent();
return rule;
}
}
/// <summary>
/// Test consensus rules for unit tests.
/// </summary>
public class TestConsensusRules : ConsensusRuleEngine
{
public RuleContext RuleContext { get; set; }
private RuleRegistrationHelper ruleRegistrationHelper;
public TestConsensusRules(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments,
ConsensusSettings consensusSettings, ICheckpoints checkpoints, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, ConsensusRulesContainer consensusRulesContainer)
: base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, chainState, invalidBlockHashStore, nodeStats, consensusRulesContainer)
{
this.ruleRegistrationHelper = new RuleRegistrationHelper(this, consensusRulesContainer);
}
public T RegisterRule<T>() where T : ConsensusRuleBase, new()
{
return this.ruleRegistrationHelper.RegisterRule<T>();
}
public override RuleContext CreateRuleContext(ValidationContext validationContext)
{
return this.RuleContext ?? new PowRuleContext();
}
public override uint256 GetBlockHash()
{
throw new NotImplementedException();
}
public override Task<RewindState> RewindAsync(int targetHeight)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Test PoS consensus rules for unit tests.
/// </summary>
public class TestPosConsensusRules : PosConsensusRuleEngine
{
private RuleRegistrationHelper ruleRegistrationHelper;
public TestPosConsensusRules(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer,
NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ICoinView uxtoSet, IStakeChain stakeChain,
IStakeValidator stakeValidator, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, IRewindDataIndexCache rewindDataIndexCache, IAsyncProvider asyncProvider, ConsensusRulesContainer consensusRulesContainer)
: base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, uxtoSet, stakeChain, stakeValidator, chainState, invalidBlockHashStore, nodeStats, rewindDataIndexCache, asyncProvider, consensusRulesContainer)
{
this.ruleRegistrationHelper = new RuleRegistrationHelper(this, consensusRulesContainer);
}
public T RegisterRule<T>() where T : ConsensusRuleBase, new()
{
return this.ruleRegistrationHelper.RegisterRule<T>();
}
}
/// <summary>
/// Factory for creating the test chain.
/// Much of this logic was taken directly from the embedded TestContext class in MinerTest.cs in the integration tests.
/// </summary>
internal static class TestRulesContextFactory
{
/// <summary>
/// Creates test chain with a consensus loop.
/// </summary>
public static TestRulesContext CreateAsync(Network network, [CallerMemberName]string pathName = null)
{
var testRulesContext = new TestRulesContext() { Network = network };
string dataDir = Path.Combine("TestData", pathName);
Directory.CreateDirectory(dataDir);
testRulesContext.NodeSettings = new NodeSettings(network, args: new[] { $"-datadir={dataDir}" });
testRulesContext.LoggerFactory = testRulesContext.NodeSettings.LoggerFactory;
testRulesContext.LoggerFactory.AddConsoleWithFilters();
testRulesContext.DateTimeProvider = DateTimeProvider.Default;
network.Consensus.Options = new ConsensusOptions();
var consensusSettings = new ConsensusSettings(testRulesContext.NodeSettings);
testRulesContext.Checkpoints = new Checkpoints();
testRulesContext.ChainIndexer = new ChainIndexer(network);
testRulesContext.ChainState = new ChainState();
testRulesContext.Signals = new Signals.Signals(testRulesContext.LoggerFactory, null);
testRulesContext.AsyncProvider = new AsyncProvider(testRulesContext.LoggerFactory, testRulesContext.Signals, new NodeLifetime());
var deployments = new NodeDeployments(testRulesContext.Network, testRulesContext.ChainIndexer);
testRulesContext.ConsensusRuleEngine = new PowConsensusRuleEngine(testRulesContext.Network, testRulesContext.LoggerFactory, testRulesContext.DateTimeProvider,
testRulesContext.ChainIndexer, deployments, consensusSettings, testRulesContext.Checkpoints, new InMemoryCoinView(new uint256()), testRulesContext.ChainState,
new InvalidBlockHashStore(DateTimeProvider.Default), new NodeStats(DateTimeProvider.Default, testRulesContext.LoggerFactory), testRulesContext.AsyncProvider, new ConsensusRulesContainer()).SetupRulesEngineParent();
return testRulesContext;
}
public static Block MineBlock(Network network, ChainIndexer chainIndexer)
{
var block = network.Consensus.ConsensusFactory.CreateBlock();
var coinbase = new Transaction();
coinbase.AddInput(TxIn.CreateCoinbase(chainIndexer.Height + 1));
coinbase.AddOutput(new TxOut(Money.Zero, new Key()));
block.AddTransaction(coinbase);
block.Header.Version = (int)ThresholdConditionCache.VersionbitsTopBits;
block.Header.HashPrevBlock = chainIndexer.Tip.HashBlock;
block.Header.UpdateTime(DateTimeProvider.Default.GetTimeOffset(), network, chainIndexer.Tip);
block.Header.Bits = block.Header.GetWorkRequired(network, chainIndexer.Tip);
block.Header.Nonce = 0;
int maxTries = int.MaxValue;
while (maxTries > 0 && !block.CheckProofOfWork())
{
++block.Header.Nonce;
--maxTries;
}
if (maxTries == 0)
throw new XunitException("Test failed no blocks found");
return block;
}
}
}