public void Register_WithRulesInRuleRegistration_UpdatesConsensusRule_AddsConensusRuleToRules()
        {
            this.loggerFactory.Setup(l => l.CreateLogger(typeof(BlockSizeRule).FullName))
            .Returns(new Mock <ILogger>().Object)
            .Verifiable();
            this.loggerFactory.Setup(l => l.CreateLogger(typeof(SetActivationDeploymentsRule).FullName))
            .Returns(new Mock <ILogger>().Object)
            .Verifiable();
            this.ruleRegistrations = new List <ConsensusRule> {
                new BlockSizeRule(),
                new SetActivationDeploymentsRule()
            };

            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules = consensusRules.Register(this.ruleRegistration.Object) as TestConsensusRules;

            List <ConsensusRule> rules = consensusRules.Rules.ToList();

            Assert.Equal(2, rules.Count);
            ConsensusRule rule = rules[0];

            Assert.Equal(typeof(TestConsensusRules), rule.Parent.GetType());
            Assert.NotNull(rule.Logger);

            rule = rules[1];
            Assert.Equal(typeof(TestConsensusRules), rule.Parent.GetType());
            Assert.NotNull(rule.Logger);

            this.loggerFactory.Verify();
        }
        public async Task ExecuteAsync_ConsensusErrorException_SetsConsensusErrorOnBlockValidationContextAsync()
        {
            ConsensusError consensusError = ConsensusErrors.BadBlockLength;
            var            rule           = new Mock <ConsensusRule>();

            rule.Setup(r => r.RunAsync(It.Is <RuleContext>(c => c.SkipValidation == false)))
            .Throws(new ConsensusErrorException(consensusError))
            .Verifiable();

            this.ruleRegistrations = new List <ConsensusRule> {
                new TestRule()
            };
            var blockValidationContext = new ValidationContext()
            {
                RuleContext = new RuleContext()
                {
                    SkipValidation = false
                }
            };
            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules.Register(this.ruleRegistration.Object);

            await consensusRules.AcceptBlockAsync(blockValidationContext, new ChainedHeader(this.network.GetGenesis().Header, this.network.GenesisHash, 0));

            Assert.NotNull(blockValidationContext.Error);
            Assert.Equal(consensusError.Message, blockValidationContext.Error.Message);
            Assert.Equal(consensusError.Code, blockValidationContext.Error.Code);
        }
        public async Task ExecuteAsync_RuleCanSkipValidation_ContextCanSkipValidation_DoesNotRunRuleAsync()
        {
            var rule = new ConsensusRuleWithSkipValidationAttribute();

            this.ruleRegistrations = new List <ConsensusRule> {
                rule
            };

            var blockValidationContext = new ValidationContext()
            {
                ChainedHeader = this.concurrentChain.Tip,
            };
            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules.RuleContext = new RuleContext()
            {
                SkipValidation = true
            };

            consensusRules.Register(this.ruleRegistration.Object);

            await consensusRules.AcceptBlockAsync(blockValidationContext, new ChainedHeader(this.network.GetGenesis().Header, this.network.GenesisHash, 0));

            Assert.False(rule.RunCalled);
            Assert.Null(blockValidationContext.Error);
        }
Esempio n. 4
0
        public async Task ValidateAsync_RuleWithoutAttributes_GetsRunAsync()
        {
            var rule = new Mock <ConsensusRule>();

            rule.Setup(r => r.RunAsync(It.Is <RuleContext>(c => c.SkipValidation == false)));

            TestConsensusRules consensusRules = InitializeConsensusRules();

            this.network.Consensus.Rules = new List <IConsensusRule> {
                rule.Object
            };
            Assert.Throws <ConsensusException>(() => { consensusRules.Register(); });
        }
Esempio n. 5
0
        public void FindRule_RuleNotFound_ThrowsException()
        {
            Assert.Throws <Exception>(() =>
            {
                TestConsensusRules consensusRules = this.InitializeConsensusRules();
                this.network.Consensus.Rules      = new List <IConsensusRule> {
                    new SetActivationDeploymentsRule()
                };
                consensusRules = consensusRules.Register() as TestConsensusRules;

                consensusRules.Rules.FindRule <BlockSizeRule>();
            });
        }
Esempio n. 6
0
        public void TryFindRule_RuleNotFound_ReturnsNull()
        {
            TestConsensusRules consensusRules = this.InitializeConsensusRules();

            this.network.Consensus.Rules = new List <IConsensusRule> {
                new SetActivationDeploymentsRule()
            };
            consensusRules = consensusRules.Register() as TestConsensusRules;

            var rule = consensusRules.Rules.TryFindRule <BlockSizeRule>();

            Assert.Null(rule);
        }
Esempio n. 7
0
        public void TryFindRule_RuleFound_ReturnsConsensusRule()
        {
            TestConsensusRules consensusRules = this.InitializeConsensusRules();

            this.network.Consensus.Rules = new List <IConsensusRule> {
                new BlockSizeRule()
            }; consensusRules = consensusRules.Register() as TestConsensusRules;

            var rule = consensusRules.Rules.TryFindRule <BlockSizeRule>();

            Assert.NotNull(rule);
            Assert.True(rule is BlockSizeRule);
        }
        public void TryFindRule_RuleNotFound_ReturnsNull()
        {
            this.ruleRegistrations = new List <ConsensusRule> {
                new SetActivationDeploymentsRule()
            };

            TestConsensusRules consensusRules = this.InitializeConsensusRules();

            consensusRules = consensusRules.Register(this.ruleRegistration.Object) as TestConsensusRules;

            var rule = consensusRules.Rules.TryFindRule <BlockSizeRule>();

            Assert.Null(rule);
        }
        public void FindRule_RuleNotFound_ThrowsException()
        {
            Assert.Throws <Exception>(() =>
            {
                this.ruleRegistrations = new List <ConsensusRule> {
                    new SetActivationDeploymentsRule()
                };

                TestConsensusRules consensusRules = this.InitializeConsensusRules();
                consensusRules = consensusRules.Register(this.ruleRegistration.Object) as TestConsensusRules;

                consensusRules.Rules.FindRule <BlockSizeRule>();
            });
        }
        public void FindRule_RuleFound_ReturnsConsensusRule()
        {
            this.ruleRegistrations = new List <ConsensusRule> {
                new BlockSizeRule()
            };

            TestConsensusRules consensusRules = this.InitializeConsensusRules();

            consensusRules = consensusRules.Register(this.ruleRegistration.Object) as TestConsensusRules;

            var rule = consensusRules.Rules.FindRule <BlockSizeRule>();

            Assert.NotNull(rule);
            Assert.True(rule is BlockSizeRule);
        }
Esempio n. 11
0
        public async Task ValidateAsync_RuleWithValidationRuleAttribute_GetsRunAsync()
        {
            var rule = new ConsensusRuleWithValidationAttribute();
            TestConsensusRules consensusRules = InitializeConsensusRules();

            this.network.Consensus.Rules = new List <IConsensusRule> {
                rule
            };
            consensusRules.Register();

            await consensusRules.ValidateAsync(new RuleContext()
            {
                SkipValidation = true
            });

            Assert.True(rule.RunCalled);
        }
        public async Task ValidateAsync_RuleWithNonValidationRuleAttribute_GetsRunAsync()
        {
            var rule = new ConsensusRuleWithoutNonValidationRuleAttribute();

            this.ruleRegistrations = new List <ConsensusRule> {
                rule
            };
            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules.Register(this.ruleRegistration.Object);

            await consensusRules.ValidateAsync(new RuleContext()
            {
                SkipValidation = true
            });

            Assert.False(rule.RunCalled);
        }
Esempio n. 13
0
        public async Task ExecuteAsync_RuleCannotSkipValidation_ContextCannotSkipValidation_RunsRuleAsync()
        {
            var rule = new ConsensusRuleWithValidationAttribute();
            var blockValidationContext = new ValidationContext()
            {
                RuleContext = new RuleContext()
                {
                    SkipValidation = false
                }
            };
            TestConsensusRules consensusRules = InitializeConsensusRules();

            this.network.Consensus.Rules = new List <IConsensusRule> {
                rule
            };
            consensusRules.Register();

            await consensusRules.AcceptBlockAsync(blockValidationContext, new ChainedHeader(this.network.GetGenesis().Header, this.network.GenesisHash, 0));

            Assert.True(rule.RunCalled);
            Assert.Null(blockValidationContext.Error);
        }
        public ConsensusRuleUnitTestBase()
        {
            Block.BlockSignature  = false;
            Transaction.TimeStamp = false;
            this.network          = Network.TestNet;
            this.loggerFactory    = new Mock <ILoggerFactory>();
            this.loggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>()))
            .Returns(new Mock <ILogger>().Object);
            this.dateTimeProvider = new Mock <IDateTimeProvider>();

            this.concurrentChain   = new ConcurrentChain(this.network);
            this.nodeDeployments   = new NodeDeployments(this.network, this.concurrentChain);
            this.consensusSettings = new ConsensusSettings();
            this.checkpoints       = new Mock <ICheckpoints>();

            this.ruleRegistrations = new List <ConsensusRule>();
            this.ruleRegistration  = new Mock <IRuleRegistration>();
            this.ruleRegistration.Setup(r => r.GetRules())
            .Returns(() => { return(this.ruleRegistrations); });

            this.consensusRules = InitializeConsensusRules();
        }
        public void Constructor_InitializesClass()
        {
            this.consensusSettings = new ConsensusSettings()
            {
                BlockAssumedValid = null,
                UseCheckpoints    = true
            };

            this.checkpoints.Setup(c => c.GetLastCheckpointHeight())
            .Returns(15);
            this.dateTimeProvider.Setup(d => d.GetTime())
            .Returns(2);

            TestConsensusRules consensusRules = InitializeConsensusRules();

            Assert.Equal(this.network.Name, consensusRules.Network.Name);
            Assert.Equal(this.dateTimeProvider.Object.GetTime(), consensusRules.DateTimeProvider.GetTime());
            Assert.Equal(this.concurrentChain.Tip.HashBlock, consensusRules.Chain.Tip.HashBlock);
            Assert.Equal(this.nodeDeployments.GetFlags(this.concurrentChain.Tip).EnforceBIP30, consensusRules.NodeDeployments.GetFlags(this.concurrentChain.Tip).EnforceBIP30);
            Assert.True(consensusRules.ConsensusSettings.UseCheckpoints);
            Assert.Equal(15, consensusRules.Checkpoints.GetLastCheckpointHeight());
            this.loggerFactory.Verify(l => l.CreateLogger(typeof(TestConsensusRules).FullName));
        }
Esempio n. 16
0
        public async Task ValidateAsync_RuleWithoutAttributes_GetsRunAsync()
        {
            var rule = new Mock <ConsensusRule>();

            rule.Setup(r => r.RunAsync(It.Is <RuleContext>(c => c.SkipValidation == false)))
            .Returns(Task.FromResult(1))
            .Verifiable();

            this.ruleRegistrations = new List <ConsensusRule> {
                rule.Object
            };

            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules.Register(this.ruleRegistration.Object);

            await consensusRules.ValidateAsync(new RuleContext()
            {
                SkipValidation = false
            });

            rule.Verify();
        }
        public async Task ExecuteAsync_RuleCannotSkipValidation_ContextCannotSkipValidation_RunsRuleAsync()
        {
            var rule = new ConsensusRuleWithValidationAttribute();

            this.ruleRegistrations = new List <ConsensusRule> {
                rule
            };
            var blockValidationContext = new ValidationContext()
            {
                RuleContext = new RuleContext()
                {
                    SkipValidation = false
                }
            };
            TestConsensusRules consensusRules = InitializeConsensusRules();

            consensusRules.Register(this.ruleRegistration.Object);

            await consensusRules.AcceptBlockAsync(blockValidationContext);

            Assert.True(rule.RunCalled);
            Assert.Null(blockValidationContext.Error);
        }