async Task SynchronizeWithPeer(Peer peer) { LABEL_SynchronizeWithPeer: peer.HeaderDownload = new HeaderDownload(); peer.HeaderDownload.Locator = Blockchain.GetLocator(); Header headerRoot = await peer.GetHeaders(); if (headerRoot == null) { return; } if (headerRoot.HeaderPrevious == Blockchain.HeaderTip) { await peer.BuildHeaderchain( headerRoot, Blockchain.Height + 1); bool dataCorrupted = await SynchronizeUTXO( headerRoot, peer); if (dataCorrupted) { await Blockchain.LoadImage(); } return; } headerRoot = await peer.SkipDuplicates(headerRoot); Blockchain.GetStateAtHeader( headerRoot.HeaderPrevious, out int heightAncestor, out double difficultyAncestor); double difficultyFork = difficultyAncestor + await peer.BuildHeaderchain( headerRoot, heightAncestor + 1); double difficultyOld = Blockchain.Difficulty; if (difficultyFork > difficultyOld) { if (!await Blockchain.TryFork( heightAncestor, headerRoot.HashPrevious)) { goto LABEL_SynchronizeWithPeer; } bool dataCorrupted = await SynchronizeUTXO( headerRoot, peer); if ( dataCorrupted || Blockchain.Difficulty <= difficultyOld) { Blockchain.DismissFork(); await Blockchain.LoadImage(); } else { Blockchain.Reorganize(); } } else if (difficultyFork < difficultyOld) { if (peer.IsInbound()) { string.Format("Fork weaker than Main.") .Log(LogFile); peer.FlagDispose = true; } else { peer.SendHeaders( new List <Header>() { Blockchain.HeaderTip }); } } }