1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading . Tasks ;
4+ using Microsoft . Extensions . Logging ;
5+ using Moq ;
6+ using NBitcoin ;
7+ using Stratis . Bitcoin . Base ;
8+ using Stratis . Bitcoin . Base . Deployments ;
9+ using Stratis . Bitcoin . Configuration ;
10+ using Stratis . Bitcoin . Configuration . Logging ;
11+ using Stratis . Bitcoin . Configuration . Settings ;
12+ using Stratis . Bitcoin . Consensus ;
13+ using Stratis . Bitcoin . Consensus . Rules ;
14+ using Stratis . Bitcoin . Utilities ;
15+ using Stratis . Features . Collateral ;
16+ using Stratis . Features . PoA . Collateral ;
17+ using Stratis . Sidechains . Networks ;
18+ using Xunit ;
19+
20+ namespace Stratis . Features . FederatedPeg . Tests
21+ {
22+ public class CheckCollateralCommitmentHeightRuleTests
23+ {
24+ private readonly CheckCollateralCommitmentHeightRule rule ;
25+ private readonly RuleContext ruleContext ;
26+ private readonly CollateralHeightCommitmentEncoder commitmentHeightEncoder ;
27+
28+ private void EncodePreviousHeaderCommitmentHeight ( int commitmentHeight )
29+ {
30+ // Setup previous block.
31+ var encodedHeight = this . commitmentHeightEncoder . EncodeCommitmentHeight ( commitmentHeight ) ;
32+ var commitmentHeightData = new Script ( OpcodeType . OP_RETURN , Op . GetPushOp ( encodedHeight ) ) ;
33+
34+ Block prevBlock = this . ruleContext . ValidationContext . ChainedHeaderToValidate . Previous . Block ;
35+ prevBlock . Transactions = new List < Transaction > ( ) ;
36+ prevBlock . AddTransaction ( new Transaction ( ) ) ;
37+ prevBlock . Transactions [ 0 ] . AddOutput ( Money . Zero , commitmentHeightData ) ;
38+ }
39+
40+ public CheckCollateralCommitmentHeightRuleTests ( )
41+ {
42+ this . ruleContext = new RuleContext ( new ValidationContext ( ) , DateTimeOffset . Now ) ;
43+ var prevHeader = new BlockHeader { Time = 5200 } ;
44+ var prevChainedHeader = new ChainedHeader ( prevHeader , prevHeader . GetHash ( ) , int . MaxValue - 1 ) ;
45+ var prevBlock = new Block ( prevHeader ) ;
46+ prevChainedHeader . Block = prevBlock ;
47+ prevChainedHeader . BlockDataAvailability = BlockDataAvailabilityState . BlockAvailable ;
48+ var header = new BlockHeader ( ) { Time = 5234 , HashPrevBlock = prevHeader . GetHash ( ) } ;
49+ this . ruleContext . ValidationContext . BlockToValidate = new Block ( header ) ;
50+ this . ruleContext . ValidationContext . ChainedHeaderToValidate = new ChainedHeader ( header , header . GetHash ( ) , prevChainedHeader ) ;
51+
52+ Block block = this . ruleContext . ValidationContext . BlockToValidate ;
53+ block . AddTransaction ( new Transaction ( ) ) ;
54+
55+ var loggerFactory = new ExtendedLoggerFactory ( ) ;
56+ ILogger logger = loggerFactory . CreateLogger ( this . GetType ( ) . FullName ) ;
57+
58+ this . commitmentHeightEncoder = new CollateralHeightCommitmentEncoder ( ) ;
59+
60+ // Setup block.
61+ byte [ ] encodedHeight = this . commitmentHeightEncoder . EncodeCommitmentHeight ( 1000 ) ;
62+ var commitmentHeightData = new Script ( OpcodeType . OP_RETURN , Op . GetPushOp ( encodedHeight ) ) ;
63+ block . Transactions [ 0 ] . AddOutput ( Money . Zero , commitmentHeightData ) ;
64+
65+ var network = new CirrusMain ( ) ;
66+ var chainIndexer = new ChainIndexer ( network ) ;
67+
68+ var consensusRules = new Mock < ConsensusRuleEngine > (
69+ network ,
70+ loggerFactory ,
71+ new Mock < IDateTimeProvider > ( ) . Object ,
72+ chainIndexer ,
73+ new NodeDeployments ( network , chainIndexer ) ,
74+ new ConsensusSettings ( new NodeSettings ( network ) ) ,
75+ new Mock < ICheckpoints > ( ) . Object ,
76+ new Mock < IChainState > ( ) . Object ,
77+ new Mock < IInvalidBlockHashStore > ( ) . Object ,
78+ new Mock < INodeStats > ( ) . Object ,
79+ new ConsensusRulesContainer ( ) ) ;
80+
81+ this . rule = new CheckCollateralCommitmentHeightRule ( )
82+ {
83+ Logger = logger ,
84+ Parent = consensusRules . Object
85+ } ;
86+
87+ this . rule . Initialize ( ) ;
88+ }
89+
90+ [ Fact ]
91+ public async Task PassesIfCollateralHeightsAreOrderedAsync ( )
92+ {
93+ EncodePreviousHeaderCommitmentHeight ( 999 ) ;
94+ await this . rule . RunAsync ( this . ruleContext ) ;
95+ }
96+
97+ [ Fact ]
98+ public async Task FailsIfCollateralHeightsAreDisorderedAsync ( )
99+ {
100+ EncodePreviousHeaderCommitmentHeight ( 1001 ) ;
101+ await Assert . ThrowsAsync < ConsensusErrorException > ( ( ) => this . rule . RunAsync ( this . ruleContext ) ) ;
102+ }
103+ }
104+ }
0 commit comments