Exemple #1
0
        public void Valid_when_valid()
        {
            bool result = _validator.Validate(_block.Header);

            if (!result)
            {
                foreach (string error in _testLogger.LogList)
                {
                    Console.WriteLine(error);
                }
            }

            Assert.True(result);
        }
Exemple #2
0
        public void When_gaslimit_is_on_london_fork(long parentGasLimit, long blockNumber, long gasLimit, bool expectedResult)
        {
            OverridableReleaseSpec spec = new(London.Instance)
            {
                Eip1559TransitionBlock = 5
            };
            TestSpecProvider specProvider = new(spec);

            _validator   = new HeaderValidator(_blockTree, _ethash, specProvider, new OneLoggerLogManager(_testLogger));
            _parentBlock = Build.A.Block.WithDifficulty(1)
                           .WithGasLimit(parentGasLimit)
                           .WithNumber(blockNumber)
                           .TestObject;
            _block = Build.A.Block.WithParent(_parentBlock)
                     .WithDifficulty(131072)
                     .WithMixHash(new Keccak("0xd7db5fdd332d3a65d6ac9c4c530929369905734d3ef7a91e373e81d0f010b8e8"))
                     .WithGasLimit(gasLimit)
                     .WithNumber(_parentBlock.Number + 1)
                     .WithBaseFeePerGas(BaseFeeCalculator.Calculate(_parentBlock.Header, specProvider.GetSpec(_parentBlock.Number + 1)))
                     .WithNonce(0).TestObject;
            _block.Header.SealEngineType = SealEngineType.None;
            _block.Header.Hash           = _block.CalculateHash();

            bool result = _validator.Validate(_block.Header, _parentBlock.Header);

            Assert.AreEqual(expectedResult, result);
        }
Exemple #3
0
        public bool ValidateSuggestedBlock(Block suggestedBlock)
        {
            if (!_ommersValidator.Validate(suggestedBlock.Header, suggestedBlock.Ommers))
            {
                _logger?.Info($"Invalid block ({suggestedBlock.Hash}) - invalid ommers");
                return(false);
            }

            foreach (Transaction transaction in suggestedBlock.Transactions)
            {
                if (!_transactionValidator.IsWellFormed(transaction, _specProvider.GetSpec(suggestedBlock.Number)))
                {
                    _logger?.Info($"Invalid block ({suggestedBlock.Hash}) - invalid transaction ({transaction.Hash})");
                    return(false);
                }
            }

            // TODO it may not be needed here (computing twice?)
            if (suggestedBlock.Header.OmmersHash != Keccak.Compute(Rlp.Encode(suggestedBlock.Ommers)))
            {
                _logger?.Info($"Invalid block ({suggestedBlock.Hash}) - invalid ommers hash");
                return(false);
            }

            bool blockHeaderValid = _headerValidator.Validate(suggestedBlock.Header);

            if (!blockHeaderValid)
            {
                _logger?.Info($"Invalid block ({suggestedBlock.Hash}) - invalid header");
                return(false);
            }

            return(true);
        }
Exemple #4
0
    public void TestValidateHeader(bool baseReturnValue, bool isInvalidBlockReported)
    {
        BlockHeader header = Build.A.BlockHeader.TestObject;

        _baseValidator.Validate(header, false).Returns(baseReturnValue);
        _invalidHeaderInterceptor.Validate(header, false);

        _tracker.Received().SetChildParent(header.Hash, header.ParentHash);
        if (isInvalidBlockReported)
        {
            _tracker.Received().OnInvalidBlock(header.Hash, header.ParentHash);
        }
        else
        {
            _tracker.DidNotReceive().OnInvalidBlock(header.Hash, header.ParentHash);
        }
    }
Exemple #5
0
        public bool ValidateSuggestedBlock(Block block)
        {
            if (!_ommersValidator.Validate(block.Header, block.Ommers))
            {
                _logger?.Debug($"Invalid block ({block.Hash}) - invalid ommers");
                return(false);
            }

            Transaction[] txs = block.Transactions;
            for (int i = 0; i < txs.Length; i++)
            {
                if (!_transactionValidator.IsWellFormed(txs[i], _specProvider.GetSpec(block.Number)))
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Invalid block ({block.ToString(Block.Format.FullHashAndNumber)}) - invalid transaction ({txs[i].Hash})");
                    }
                    return(false);
                }
            }

            Keccak txsRoot = block.CalculateTransactionsRoot();

            if (txsRoot != block.Header.TransactionsRoot)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Invalid block ({block.ToString(Block.Format.FullHashAndNumber)}) tx root {txsRoot} != stated tx root {block.Header.TransactionsRoot}");
                }
                return(false);
            }

            if (block.Header.OmmersHash != Keccak.Compute(Rlp.Encode(block.Ommers)))
            {
                _logger?.Debug($"Invalid block ({block.ToString(Block.Format.FullHashAndNumber)}) - invalid ommers hash");
                return(false);
            }

            bool blockHeaderValid = _headerValidator.Validate(block.Header);

            if (!blockHeaderValid)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Invalid block ({block.ToString(Block.Format.FullHashAndNumber)}) - invalid header");
                }
                return(false);
            }

            return(true);
        }
Exemple #6
0
    public bool Validate(BlockHeader header, BlockHeader?parent, bool isUncle = false)
    {
        bool result = _baseValidator.Validate(header, parent, isUncle);

        if (!result)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Intercepted a bad header {header}");
            }
            if (ShouldNotTrackInvalidation(header))
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Header invalidation should not be tracked");
                }
                return(false);
            }
            _invalidChainTracker.OnInvalidBlock(header.Hash !, header.ParentHash);
        }
        _invalidChainTracker.SetChildParent(header.Hash !, header.ParentHash !);
        return(result);
    }
        public bool Validate(BlockHeader header, BlockHeader[] ommers)
        {
            if (ommers.Length > 2)
            {
                _logger?.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - too many ommers");
                return(false);
            }

            if (ommers.Length == 2 && ommers[0].Hash == ommers[1].Hash)
            {
                _logger?.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - duplicated ommer");
                return(false);
            }

            for (int i = 0; i < ommers.Length; i++)
            {
                BlockHeader ommer = ommers[i];
                if (!_headerValidator.Validate(ommer, true))
                {
                    _logger?.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - ommer's header invalid");
                    return(false);
                }

                if (!IsKin(header, ommer, 6))
                {
                    _logger?.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - ommer just pretending to be ommer");
                    return(false);
                }

                Block ancestor = _blockTree.FindBlock(header.ParentHash, false);
                for (int ancestorLevel = 0; ancestorLevel < 5; ancestorLevel++)
                {
                    if (ancestor == null)
                    {
                        break;
                    }

                    if (ancestor.Ommers.Any(o => o.Hash == ommer.Hash))
                    {
                        _logger?.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - ommers has already been included by an ancestor");
                        return(false);
                    }

                    ancestor = _blockTree.FindBlock(ancestor.Header.ParentHash, false);
                }
            }

            return(true);
        }
        public bool Validate(BlockHeader header, BlockHeader[] uncles)
        {
            if (uncles.Length > 2)
            {
                _logger.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - too many uncles");
                return(false);
            }

            if (uncles.Length == 2 && uncles[0].Hash == uncles[1].Hash)
            {
                _logger.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - duplicated uncle");
                return(false);
            }

            for (int i = 0; i < uncles.Length; i++)
            {
                BlockHeader uncle = uncles[i];
                if (!_headerValidator.Validate(uncle, true))
                {
                    _logger.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - uncle's header invalid");
                    return(false);
                }

                if (!IsKin(header, uncle, 6))
                {
                    _logger.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - uncle just pretending to be uncle");
                    return(false);
                }

                Block ancestor = _blockTree.FindBlock(header.ParentHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
                for (int ancestorLevel = 0; ancestorLevel < 5; ancestorLevel++)
                {
                    if (ancestor == null)
                    {
                        break;
                    }

                    if (ancestor.Uncles.Any(o => o.Hash == uncle.Hash))
                    {
                        _logger.Info($"Invalid block ({header.ToString(BlockHeader.Format.Full)}) - uncles has already been included by an ancestor");
                        return(false);
                    }

                    ancestor = _blockTree.FindBlock(ancestor.Header.ParentHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
                }
            }

            return(true);
        }
Exemple #9
0
        public void When_gas_limit_is_long_max_value()
        {
            _validator   = new HeaderValidator(_blockTree, _ethash, _specProvider, new OneLoggerLogManager(_testLogger));
            _parentBlock = Build.A.Block.WithDifficulty(1)
                           .WithGasLimit(long.MaxValue)
                           .WithNumber(5)
                           .TestObject;
            _block = Build.A.Block.WithParent(_parentBlock)
                     .WithDifficulty(131072)
                     .WithMixHash(new Keccak("0xd7db5fdd332d3a65d6ac9c4c530929369905734d3ef7a91e373e81d0f010b8e8"))
                     .WithGasLimit(long.MaxValue)
                     .WithNumber(_parentBlock.Number + 1)
                     .WithNonce(0).TestObject;
            _block.Header.SealEngineType = SealEngineType.None;
            _block.Header.Hash           = _block.CalculateHash();

            bool result = _validator.Validate(_block.Header, _parentBlock.Header);

            Assert.True(result);
        }
Exemple #10
0
 public bool ValidateHeader(BlockHeader header, bool isOmmer)
 {
     return(_headerValidator.Validate(header, isOmmer));
 }
Exemple #11
0
 public void Setup()
 {
     _headerValidator = Substitute.For <IHeaderValidator>();
     _headerValidator.Validate(Arg.Any <BlockHeader>(), true).Returns(true);
 }
Exemple #12
0
 public bool Validate(BlockHeader header, BlockHeader?parent, bool isOmmer)
 {
     return(_headerValidator.Validate(header, parent, isOmmer));
 }
 public override bool Validate(BlockHeader header, BlockHeader?parent, bool isUncle = false)
 {
     return(_poSSwitcher.IsPostMerge(header)
         ? ValidateTheMergeChecks(header) && base.Validate(header, parent, isUncle)
         : _preMergeHeaderValidator.Validate(header, parent, isUncle));
 }