private void ProcessBlockHeader(BlockHeaderMessage message)
        {
            if (!(message.Remote is BoundPeer peer))
            {
                _logger.Debug(
                    "BlockHeaderMessage was sent from invalid peer " +
                    "{PeerAddress}; ignored.",
                    message.Remote.Address
                    );
                return;
            }

            if (!message.GenesisHash.Equals(BlockChain.Genesis.Hash))
            {
                _logger.Debug(
                    "BlockHeaderMessage was sent from the peer " +
                    "{PeerAddress} with different genesis block {hash}; ignored.",
                    message.Remote.Address,
                    message.GenesisHash
                    );
                return;
            }

            BlockHeaderReceived.Set();
            BlockHeader header;

            try
            {
                header = message.GetHeader(BlockChain.Policy.GetHashAlgorithm);
            }
            catch (InvalidBlockException ibe)
            {
                _logger.Debug(
                    ibe,
                    "Received header #{BlockIndex} {BlockHash} seems invalid; ignored.",
                    message.HeaderHash,
                    message.HeaderIndex
                    );
                return;
            }

            try
            {
                header.ValidateTimestamp();
            }
            catch (InvalidBlockTimestampException e)
            {
                _logger.Debug(
                    e,
                    "Received #{BlockIndex} {BlockHash}'s timestamp is invalid: {Timestamp}.",
                    header.Index,
                    header.Hash,
                    header.Timestamp
                    );
                return;
            }

            _logger.Information(
                "Received " + nameof(BlockHeader) + " #{BlockIndex} {BlockHash}.",
                header.Index,
                header.Hash
                );

            if (!IsBlockNeeded(header))
            {
                _logger.Debug(
                    "Received header #{BlockIndex} {BlockHash} from peer {Peer} is not needed " +
                    "for the current chain with tip #{TipIndex} {TipHash}.",
                    header.Index,
                    header.Hash,
                    peer,
                    BlockChain.Tip,
                    BlockChain.Tip.Hash);
                return;
            }

            _logger.Information(
                "Adding received header #{BlockIndex} {BlockHash} from peer {Peer} to " +
                $"{nameof(BlockDemandTable)}...",
                header.Index,
                header.Hash,
                peer);
            BlockDemandTable.Add(
                BlockChain,
                IsBlockNeeded,
                new BlockDemand(header, peer, DateTimeOffset.UtcNow));
        }
Exemple #2
0
        private void ProcessBlockHeader(BlockHeaderMessage message)
        {
            if (!(message.Remote is BoundPeer peer))
            {
                _logger.Debug(
                    "{MessageType} message was sent from an invalid peer {Peer}.",
                    nameof(Messages.BlockHeaderMessage),
                    message.Remote
                    );
                return;
            }

            if (!message.GenesisHash.Equals(BlockChain.Genesis.Hash))
            {
                _logger.Debug(
                    "{MessageType} message was sent from a peer {Peer} with " +
                    "a different genesis block {Hash}.",
                    nameof(Messages.BlockHeaderMessage),
                    message.Remote,
                    message.GenesisHash
                    );
                return;
            }

            BlockHeaderReceived.Set();
            BlockHeader header;

            try
            {
                header = message.GetHeader(BlockChain.Policy.GetHashAlgorithm);
            }
            catch (InvalidBlockException ibe)
            {
                _logger.Debug(
                    ibe,
                    "Received header #{BlockIndex} {BlockHash} is invalid.",
                    message.HeaderHash,
                    message.HeaderIndex
                    );
                return;
            }

            try
            {
                header.ValidateTimestamp();
            }
            catch (InvalidBlockTimestampException e)
            {
                _logger.Debug(
                    e,
                    "Received header #{BlockIndex} {BlockHash} has invalid timestamp: {Timestamp}.",
                    header.Index,
                    header.Hash,
                    header.Timestamp
                    );
                return;
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            bool     needed  = IsBlockNeeded(header);
            TimeSpan elapsed = stopwatch.Elapsed;

            stopwatch.Stop();

            _logger.Information(
                "Received " + nameof(BlockHeader) + " #{BlockIndex} {BlockHash}; " +
                "Needed? {Needed}; Elapsed: {Elapsed}",
                header.Index,
                header.Hash,
                needed,
                elapsed
                );

            if (!needed)
            {
                _logger.Debug(
                    "Received header #{BlockIndex} {BlockHash} from peer {Peer} is not needed " +
                    "for the current chain with tip #{TipIndex} {TipHash}.",
                    header.Index,
                    header.Hash,
                    peer,
                    BlockChain.Tip.Index,
                    BlockChain.Tip.Hash);
                return;
            }

            _logger.Information(
                "Adding received header #{BlockIndex} {BlockHash} from peer {Peer} to " +
                nameof(BlockDemandTable) + "...",
                header.Index,
                header.Hash,
                peer);
            BlockDemandTable.Add(
                BlockChain,
                IsBlockNeeded,
                new BlockDemand(header, peer, DateTimeOffset.UtcNow));
        }