Esempio n. 1
0
        public override void NotifyOfNewBlock(Block block, SendBlockPriority priority)
        {
            if (RequestedAnnounceType == LesAnnounceType.None)
            {
                return;
            }
            if (!block.TotalDifficulty.HasValue)
            {
                throw new InvalidOperationException($"Trying to send a block {block.Hash} with null total difficulty");
            }

            if (block.TotalDifficulty <= _lastSentBlock.TotalDifficulty)
            {
                return;
            }

            AnnounceMessage announceMessage = new AnnounceMessage();

            announceMessage.HeadHash        = block.Hash;
            announceMessage.HeadBlockNo     = block.Number;
            announceMessage.TotalDifficulty = block.TotalDifficulty.Value;
            if (_lastSentBlock == null || block.ParentHash == _lastSentBlock.Hash)
            {
                announceMessage.ReorgDepth = 0;
            }
            else
            {
                BlockHeader firstCommonAncestor = SyncServer.FindLowestCommonAncestor(block.Header, _lastSentBlock);
                if (firstCommonAncestor == null)
                {
                    throw new SubprotocolException($"Unable to send announcment to LES peer - No common ancestor found between {block.Header} and {_lastSentBlock}");
                }
                announceMessage.ReorgDepth = _lastSentBlock.Number - firstCommonAncestor.Number;
            }

            _lastSentBlock = block.Header;

            Send(announceMessage);
        }