public List <Block> FindNewBlocks(string address, long startHeight, int startDifficulty) { //this function finds the "longest" chain of blocks when given an address incase of a chainsplit //first we get all possible blocks var allBlocks = _tangleAccessor.GetAllFromAddress <Block>(address) .Where(b => b.Height == startHeight) .Where(b => b.Verify(startDifficulty)) .ToList(); //we then generate a list of all possible ways from this block list var wayList = allBlocks.ToWayList(); //we then grow the list until we find the longest way while (wayList.Count > 1) { //we get the size before and if we add not a single more way, it means we only need to compare the sum of all lengths. //If the difference is 1 or less we only added a single way => longest chain for now int size = wayList.Count; int sumBefore = 0; wayList.ForEach(obj => { sumBefore += obj.Length; }); //here happens the magic wayList = GrowWays(wayList); int sumAfter = 0; wayList.ForEach(obj => { sumAfter += obj.Length; }); if (size == wayList.Count && sumAfter <= (sumBefore + 1)) { break; } } //growth stopped now because we only added a single block //we choosed now the longest way if (wayList.Count == 0) { return(new List <Block>()); } return(wayList.OrderByDescending(item => item.Length).First().ToBlockList()); }
public List <T> GetAllFromAddress <T>(string addr) where T : IDownloadable { return(_tangleAccessor.GetAllFromAddress <T>(addr)); }