public Chain BuildChain(Chain chain) { Dictionary <uint256, BlockHeader> headers = new Dictionary <uint256, BlockHeader>(); HashSet <uint256> inChain = new HashSet <uint256>(); inChain.Add(chain.GetBlock(chain.StartHeight).HashBlock); foreach (var header in Enumerate(true).Select(b => b.Item.Header)) { var hash = header.GetHash(); headers.Add(hash, header); } List <uint256> toRemove = new List <uint256>(); while (headers.Count != 0) { foreach (var header in headers) { if (inChain.Contains(header.Value.HashPrevBlock)) { toRemove.Add(header.Key); chain.TrySetTip(header.Value); inChain.Add(header.Key); } } foreach (var item in toRemove) { headers.Remove(item); } if (toRemove.Count == 0) { break; } toRemove.Clear(); } return(chain); }